Thursday 11 December 2008

About Haskell type inference

When somebody tries to tell you that Haskell would be able to infer types, and that type declarations would be unnecessary, ask them to compile the following Haskell code with and without the marked line:

data LC a
= Var a
| App (LC a) (LC a)
| Abs (LC (Maybe a))
deriving (Show, Eq)

my_maybe :: (a -> b) -> Maybe a -> Maybe b
my_maybe f (Just x) = Just (f x)
my_maybe f (Nothing) = Nothing

lcFunc :: (a -> b) -> LC a -> LC b -- try with and without this line --
lcFunc f (Var x) = Var (f x)
lcFunc f (App left right) = App (lcFunc f left) (lcFunc f right)
lcFunc f (Abs t') = Abs (lcFunc (my_maybe f) t')

Remark: i used the ghc 6.8.2 which comes with ubuntu 8.04
Remark2: u might want to use the built-in fmap instead of my_maybe.

No comments: