[Haskell] Re: help with some basic code that doesn't work

Remi Turk rturk at science.uva.nl
Mon Feb 14 06:36:22 EST 2005


On Mon, Feb 14, 2005 at 02:31:54PM +0900, Shin-Cheng Mu wrote:
> Malcolm Wallace <Malcolm.Wallace at cs.york.ac.uk> wrote:
> >Just a comment, since a couple of people have made similar statements.
> >Haskell will derive Eq for arbitrarily complex types - there is no
> >restriction to "simple" types, whatever they might be.
> 
> Now that this topic is brought up...
> 
> Occasionally I would need to define recursive datatypes
> using an explicit fixed-point operator, such as:
> 
> > data Fix f = In (f (Fix f))      deriving (Show, Eq)
> > data L a x = Nil | Cons a x      deriving (Show, Eq)
> 
> However, Haskell was not able to  derive from Fix f any
> instances.
>
[snip]
> 
> This is rather unsatisfactory, because I would not be able
> to inspect values of type Fix f in the interpreter. Is there
> a way to get around this?
> 
> sincerely,
> Shin

Funny this comes up at this time, as Fix was on-topic yesterday
at #haskell. One way to make Fix an instance of Show/Eq is this
(based on http://www.haskell.org/hawiki/PreludeExts, where Fix is
called Rec):

class RecShow f where
    recShow :: Show a => f a -> String

instance (RecShow f) => Show (Rec f) where
     show (In x) = "(In (" ++ recShow x ++ "))"
instance           RecShow Maybe       where recShow = show
instance           RecShow []          where recShow = show
instance Show a => RecShow (Either a)  where recShow = show

Happy Hacking,
Remi

-- 
Nobody can be exactly like me. Even I have trouble doing it.


More information about the Haskell mailing list