Hello, world. Let me share some of my findings of Haskell and Swift.

I got interested in functional programming, especially in Haskell, when I realized that most of my favorite Swift features come from functional programming. My favorite features include enum with associated values, Result type, higher-order functions (i.e. passing functions themselves), KeyPath type, and so on.

After started learning Haskell, I found there are more common features between Swift and Haskell than I expected. Some data types are essentially the same, some syntax is similar, and some concepts are common. In fact, Chris Lattner, who is the main author of Swift, confirms that Generics in Swift is inspired by Haskell according to an interview.

I’m gonna share some similarities I found in Haskell and Swift, and the limitations of Swift compared to Haskell.


Corresponding concepts

These are some syntax, data types, and concepts both languages have.

Swift Haskell
Generics:
e.g. struct GenericType<A, B>
Type Constructor:
e.g. data SomeType a b
a and b are called type parameters
Protocol: protocol Type Class: class
Protocol conformance:
extension SomeType: SomeProtocol {
Instance declaration:
instance SomeClass SomeType where
enum with associated value Algebraic Data Type: data
enum Optional<Wrapped> data Maybe a
enum Result<Success, Failure> data Either a b
Closure: { A -> B in ... } Lambda: \a -> ...
Closure type declaration:
var foo: (A -> B -> C)
Function signature:
foo :: A -> B -> C

There are more common features such as:

By the way, “Type constructor”, “Type parameter”, “Type class”, … It seems Haskell terminology is built around Types, and Haskell seems to be very aware of handling types.


In Haskell, but not in Swift

Even though there are a lot of similarities, there are also some patterns that cannot be written simply in Swift.

Higher-Kinded Type

For example, Functor class, which is like a protocol that has fmap function in Swift terms, cannot be directly translated into Swift.

-- Haskell
class Functor f where
    fmap :: (a -> b) -> f a -> f b
    ...
// Psuedo-Swift
protocol Functor F { // `F` needs to be a type constructor. General type constructor cannot be written in Swift.
    func fmap<A, B>(_ f: (A) -> B) -> F<A> -> F<B>
    ...
}

F could be Optional (not Optional<A>), or Result<Int, /* some general type parameter */>. However, Swift hasn’t supported this kind of syntax.
Higher-Kinded Type (HKT) support is necessary for this. Bow-Swift includes a great document about this topic here.

IO type

In Haskell, since all functions are pure, side effects can be written only in a type called IO. On the other hand, in Swift, side effects can be written anywhere.
As limiting the place to write side effects leads to more robust and testable codes, I sometimes miss this feature when I go back to writing Swift.

And, more and more!

The Haskell world is vast. There are numerous interesting concepts and ways of thinking about programming, that don’t exist in other languages.


References

For Swift programmers

If you’re a Swift programmer, these materials are excellent to get into functional programming. I learned a lot from these. Highly recommended.

  • Point-Free: A video series exploring functional programming and Swift.
  • Bow-Swift: A library for Typed Functional Programming in Swift

Start learning Haskell

Haskell conferences/events

  • ZuriHac: June 18-20, 2021
    I attended this year. Great talks and a very welcoming atmosphere!
  • Applied Functional Programming in Haskell at Summer School Utrecht, July 05-09, 2021
    I also attended this course this summer. There were a lot of intermediate/advanced topics. It’s also nice to know how Haskell is discussed in academics.
  • Haskell Love Conference: Sep 10, 2021, online
  • Haskell eXchange: Nov 16-17, 2021, online
    Looking forward to these two this fall.

Thank you for reading! Please reach out to @yoshikuni_kato if you have any questions or comments. Enjoy functional programming!