[Haskell] Type Lambdas in Gofer

Wolfgang Lux wlux at uni-muenster.de
Wed Aug 1 01:57:48 EDT 2007


Jim Apple wrote:

> data Rec f = In (f (Rec f))
> type P f a = f (Rec f, a)
>
> mapP :: Functor f => (a -> b) -> P f a -> P f b
> mapP g = fmap (\(x,a) -> (x, g a))
>
> instance Functor f => Functor (P f) where
>     fmap = mapP
>
> Why did Gofer have this power while Haskell does not?

Haskell does have the same power as Gofer, it simply does
not allow you to define instances for type synonyms (just
in order to prevent overlapping instances, I guess). If
you use a newtype instead of a type synonym everything
works fine:

   data Rec f = In (f (Rec f))
   newtype P f a = P (f (Rec f, a))
   unP (P x) = x

   mapP :: Functor f => (a -> b) -> P f a -> P f b
   mapP g = P . fmap (\((x,a)) -> (x, g a)) . unP

   instance Functor f => Functor (P f) where
     fmap = mapP

Wolfgang



More information about the Haskell mailing list