Skip to content

Instantly share code, notes, and snippets.

@merijn
Last active December 22, 2016 08:21
Show Gist options
  • Save merijn/77e3fa9757658e59b01d to your computer and use it in GitHub Desktop.
Save merijn/77e3fa9757658e59b01d to your computer and use it in GitHub Desktop.
Haskell RankNType example
module RankN where
data Foo = Foo Int | Bar Double
mangle :: Num a => (a -> a) -> Foo -> Foo
mangle f (Foo i) = Foo (f i)
mangle f (Bar d) = Bar (f d)
{-
NoRankN.hs:6:25:
Could not deduce (a ~ Int)
from the context (Num a)
bound by the type signature for
mangle :: Num a => (a -> a) -> Foo -> Foo
at NoRankN.hs:5:11-41
‘a’ is a rigid type variable bound by
the type signature for mangle :: Num a => (a -> a) -> Foo -> Foo
at NoRankN.hs:5:11
Relevant bindings include
f :: a -> a (bound at NoRankN.hs:6:8)
mangle :: (a -> a) -> Foo -> Foo (bound at NoRankN.hs:6:1)
In the first argument of ‘Foo’, namely ‘(f i)’
In the expression: Foo (f i)
-}
{-# LANGUAGE RankNTypes #-}
module RankN where
data Foo = Foo Int | Bar Double
mangle :: (forall a . Num a => a -> a) -> Foo -> Foo
mangle f (Foo i) = Foo (f i)
mangle f (Bar d) = Bar (f d)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment