constrained datatype

Tom Pledger Tom.Pledger at peace.com
Thu Jan 15 09:03:14 EST 2004


Wang Meng writes:
 :
 | > class Foo n
 | > data Erk n = Foo n => Erk
 | 
 | test.hs:53:
 |     All of the type variables in the constraint `Foo n' are already in
 | scope
 |         (at least one must be universally quantified here)
 :
 | Is there any reason for this error?

I think it implies that the 'Foo n =>' must follow the 'data' keyword,
because there is no 'forall n .' to hold it on the right hand side of
the '='.

That is,

    data Foo n => Erk n = Erk

But beware that you can still construct values of type Erk n, even
when n is not in Foo:

    class Foo n
    data Foo n => Erk n = Erk

    test = case Erk :: Erk () of    -- () is not an instance of Foo
               Erk -> "Unrestricted!"

See John Hughes's paper "Restricted Datatypes in Haskell" for more.
http://www.md.chalmers.se/~rjmh/Papers/restricted-datatypes.ps

Regards,
Tom



More information about the Haskell mailing list