{{ message }}

Instantly share code, notes, and snippets.

Created Oct 7, 2013
Comparison of the straightforward embedding of a basic tenet of category theory in Scala vs Haskell.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
 yonedaLemma = Iso (flip fmap) (\$ id)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
 def yonedaLemma[F[_]:Functor, A]: F[A] <=> (({type λ[α] = A => α })#λ ~> F) = new (F[A] <=> (({type λ[α] = A => α })#λ ~> F)) { def to(fa: F[A]) = new (({type λ[α] = A => α })#λ ~> F) { def apply[R](f: A => R) = Functor[F].map(fa)(f) } def from(f: (({type λ[α] = A => α })#λ ~> F)) = f(a => a) }

### non commented Oct 7, 2013

Not that I disagree with the conclusion here, but to be fair you could use some type aliases to make the Scala version easier to read:

```type X[A] = A => X
type Y[F[_], A] = F[A] <=> X ~> F

def yonedaLemma[F[_]:Functor, A]: Y[F, A] = new Y[F, A] {
def to(fa: F[A]) = new (X ~> F) {
def apply[R](f: A => R) = Functor[F].map(fa)(f)
}
def from(f: X ~> F) = f(a => a)
}```

### djspiewak commented Oct 7, 2013

If Scala had higher rank types, I could do this in three concise lines (two if you allow me to omit the definition of `Iso`, as you did). So, this is really a better demonstration of how Scala's type system needs a specific feature, rather than the general depravity of the language.

I do really, really wish Scala had higher rank types though…

### runarorama commented Oct 7, 2013

Also to be fair, the Haskell version should have a type annotation because the Scala version does:

```yonedaLemma :: forall f a. (Functor f) => Iso (f a) (forall b. (a -> b) -> f b)
yonedaLemma = Iso (flip fmap) (\$ id)```