# Calling Haskell from Swift

DevFeed: [Calling Haskell from Swift](<https://devfeed.tech/articles/calling-haskell-from-swift-27915.md>)

Original publisher: [Read original article](<http://alt-romes.github.io/posts/2024-04-02-calling-haskell-from-swift.html>)

Published: 2024-04-02T00:00:00Z

Content type: tutorial

Language: en

Sources: [Romes' Musings](<https://devfeed.tech/sources/romes-musings.md>)

Topics: [Haskell](<https://devfeed.tech/topics/haskell.md>), [Swift](<https://devfeed.tech/topics/swift.md>), [interoperability](<https://devfeed.tech/topics/interoperability.md>), [data type](<https://devfeed.tech/topics/data-type.md>), [iOS](<https://devfeed.tech/topics/ios.md>), [macOS](<https://devfeed.tech/topics/macos.md>), [Xcode](<https://devfeed.tech/topics/xcode.md>), [SwiftUI](<https://devfeed.tech/topics/swiftui.md>)

Tags: [data-type](<https://devfeed.tech/tags/data-type.md>), [haskell](<https://devfeed.tech/tags/haskell.md>), [interoperability](<https://devfeed.tech/tags/interoperability.md>), [ios](<https://devfeed.tech/tags/ios.md>), [macos](<https://devfeed.tech/tags/macos.md>), [metaprogramming](<https://devfeed.tech/tags/metaprogramming.md>), [swift](<https://devfeed.tech/tags/swift.md>), [swiftui](<https://devfeed.tech/tags/swiftui.md>), [tutorial](<https://devfeed.tech/tags/tutorial.md>), [xcode](<https://devfeed.tech/tags/xcode.md>)

## AI overview

This tutorial explains how to call non-trivial Haskell functions from Swift in native macOS and iOS applications. It uses foreign function exports, argument and result marshaling, serialization of user-defined data types, and Swift interoperability so Haskell functions can be exposed through Swift structs, classes, and idiomatic interfaces.

## Source excerpt

Contents 1 Introduction 2 Marshaling Inputs and Outputs 2.1 Haskell's Perspective 2.2 Swift's Perspective 3 Metaprogramming at the boundaries 3.1 Haskell's perspective 3.2 Swift's perspective 4 Remarks This is the second installment of the in-depth series of blog-posts on developing native macOS and iOS applications using both Haskell and Swift/SwiftUI. This post covers how to call (non-trivial) Haskell functions from Swift by using a foreign function calling-convention strategy similar to that described by Calling Purgatory from Heaven: Binding to Rust in Haskell that requires argument and result marshaling. You may find the other blog posts in this series interesting: Creating a macOS app with Haskell and Swift The series of blog posts is further accompanied by a github repository where each commit matches a step of this tutorial. If in doubt regarding any step, check the matching commit to make it clearer. This write-up has been cross-posted to Well-Typed's Blog. 1 Introduction We'll pick up from where the last post ended - we have set up an XCode project that includes our headers generated from Haskell modules with foreign exports and linking against the foreign library declared in the cabal file. We have already been able to call a very simple Haskell function on integers from Swift via Haskell's C foreign export feature and Swift's C interoperability. This part concerns itself with calling idiomatic Haskell functions, which typically involve user-defined datatypes as inputs and outputs, from Swift. Moreover, these functions should be made available to Swift transparently, such that Swift calls them as it does other idiomatic functions, with user defined structs and classes. For the running example, the following not-very-interesting function will suffice to showcase the method we will use to expose this function from Haskell to Swift, which easily scales to other complex data types and functions. data User = User { name :: String , age :: Int } birthday :: Use