[swift-evolution] [Proposal] Higher Kinded Types (Monads, Functors, etc.)

Douglas Gregor dgregor at apple.com
Wed Dec 16 15:34:10 CST 2015

> On Dec 16, 2015, at 1:10 PM, Will Fancher <willfancher38 at gmail.com> wrote:
> With respect specifically to Monads, there are numerous useful abstractions. For one, Applicative and Functor can be derived for free, requiring the no effort on the Monad instance's part. This alone eliminates a lot of code repetition. Additionally, the large majority of functions described for Haskell here are very useful:
> https://hackage.haskell.org/package/base-
> Those functions come as abstractions over monads which can be easily understood (if you understand monads) and which eliminate a lot of code repetition. The existence of these abstractions makes any Monad type much more robust that it would be on its own.

To make this argument, you’ll need to make it more concrete and more relevant to more Swift programmers. Haskell is an amazing language in which one can express some truly beautiful abstractions… but Swift is not Haskell, and it caters to a different audience. If we’re to bring in more advanced type system features from Haskell, they need to be justified in the context of how this helps a significant population of Swift programmers in their everyday programming.

> There are plenty of typeclasses in Haskell that could be implemented in Swift and would be useful if we had HKTs. Foldables and Traversables, for example, are simple and powerful abstractions that would be well worth having. And there are various simpler things besides Haskell typeclasses that can't be done in Swift as it stands. For example, you can't currently take a collection of type C1: CollectionType, and return a value of type C2 where C2 ~= C1, which means you can't perform arbitrary transformations on arbitrary collections. You have to choose an implementation type and use that one.

*I* know what you mean here, but make it concrete. For example, our primary map operation currently returns an array:

  extension SequenceType {
    func map<T>(
      @noescape includeElement: (Generator.Element) throws -> T
    ) rethrows -> [T]

With higher-kinded types, one could potentially [*] produce a collection of the same kind as Self but with the element type T. So, mapping a Set produces a Set, mapping an Array produces an Array, etc. That’s a 

And you’re going to hear me say this a lot, but we are severely bandwidth-limited on design of major features, especially in the generics system because of it’s complexity.

	- Doug

[*] You wouldn’t actually be able to do this on SequenceType, because you can’t necessarily construct it.

More information about the swift-evolution mailing list