Type class problem

oleg@pobox.com oleg@pobox.com
Sun, 17 Aug 2003 17:04:26 -0700 (PDT)


> I defined type recursion and naturals as

> >newtype Mu f = In {unIn :: f (Mu f)}
> >data N f = S f | Z
> >type Nat = Mu N

> An application of Mu should be showable if the functor maps showable types
> to showable types, so the most natural way to define the instance seemed
> to be

> instance (Show a => Show (f a)) => Show (Mu f) where
>   show (In x) = show x

> Of course that constraint didn't work.

Well, it can if we write it in a bit different way:

instance (Show (f (Mu f))) => Show (Mu f) where
   show (In x) = show x

instance Show (N (Mu N)) where
   show Z = "Z"
   show (S k) = "S "++show k

*Main> show (In (S (In (S (In Z)))))
"S S Z"

This solution is akin to that of breaking the type recursion when
defining the fixpoint combinator fix. Only here we face the recursion
on constraints. I believe the same solution should work for the
Observable class. You didn't post the definition of the Observable
class, so I couldn't test my claim.

Flags used:
	ghci -fglasgow-exts -fallow-undecidable-instances  /tmp/a.hs