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

Shin-Cheng Mu scm at ipl.t.u-tokyo.ac.jp
Mon Feb 14 00:31:54 EST 2005


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. The following is what happens in GHCi:

   *Main> In Nil == In Nil

   Context reduction stack overflow; size = 21
   Use -fcontext-stack20 to increase stack size to (e.g.) 20
       `Eq (L e (Fix (L e)))' arising from use of `==' at <interactive>:1
       `Eq (Fix (L e))' arising from use of `==' at <interactive>:1
   <<deleted>>

   *Main> In Nil

   Context reduction stack overflow; size = 21
   Use -fcontext-stack20 to increase stack size to (e.g.) 20
       `Show (L e (Fix (L e)))'
         arising from use of `print' at <interactive>:1
   <<deleted>>

Probably Malcolm meant that Haskell will derive standard instances
for arbitrarily complex type **if they are definable manually**.
If so Malcolm's statement is true -- indeed I cannot even declare
the instances by hand. The following declaration is acceptable
by GHC after using the option -fallow-undecidable-instances,
but even though, I got the same error message as I try to print
In Nil.

 > Instance Show (f (Fix f)) => Show (Fix f) where
 >   showsPrec _ (In x) = ("In ("++) . showsPrec 1 x . (')':)

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



More information about the Haskell mailing list