Skip to content

Instantly share code, notes, and snippets.

Created October 7, 2013 19:22
Show Gist options
  • Select an option

  • Save anonymous/6873422 to your computer and use it in GitHub Desktop.

Select an option

Save anonymous/6873422 to your computer and use it in GitHub Desktop.
Comparison of the straightforward embedding of a basic tenet of category theory in Scala vs Haskell.
yonedaLemma = Iso (flip fmap) ($ id)
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)
}
@djspiewak

Copy link
Copy Markdown

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

Copy link
Copy Markdown

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)

@nafg

nafg commented Sep 11, 2017

Copy link
Copy Markdown

How is the affected by recent scala, typelevel scala, or dotty features?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment