A constraint in a data type definition does not do much. It only
restricts the type of the data constructor(s), e.g. here FooType :: Foo
a => a -> FooType a.
Functions that use the data type FooType a cannot take any advantage of
the constraint.

There has been a long discussion on this issue on the Haskell 98
committee. However, in the end it was left as unsatisfactory as it is,
because giving a constraint in a data type definition the semantics that
you would like, would require considerable changes of Haskell. John
Hughes wrote a proposal for a better semantics of constraints in data
type definitions:
Restricted Datatypes in Haskell,
http://www.md.chalmers.se/~rjmh/Papers/restricted-datatypes.ps
>From that paper you will see that the semantics you would like to have
is not that easy to obtain.

Olaf


Zhanyong Wan wrote:
> The following piece of code was rejected by Hugs:
> 
> > class Foo a where
> >   write :: a -> String
> >   write _ = "I'm a foo!"
> >
> > data Foo a => FooType a = FooType a
> >
> > writeFoo :: FooType a -> String
> > writeFoo (FooType a) = write a
> 
> ERROR "Test.hs" (line xx): Cannot justify constraints in explicitly
> typed binding
> *** Expression    : writeFoo
> *** Type          : FooType a -> String
> *** Given context : ()
> *** Constraints   : Foo a
> 
> By changing the type signature for writeFoo to:
> 
> > writeFoo :: Foo a => FooType a -> String
> 
> or just deleting the signature, I could get it compile.  What I don't
> understand is, since the constraint "Foo a" is redundant here (Hugs
> should be able to figure it out from the line "data Foo a => FooType a =
> FooType a"), why doesn't Hugs allow it to be omitted?  After all, there
> could not really be any "FooType a" where a is *not* an instance of
> Foo.  Could anybody shed a light on this?  Thanks.
> 
> -- Zhanyong Wan

-- 
OLAF CHITIL, 
 Dept. of Computer Science, University of York, York YO10 5DD, UK. 
 URL: http://www.cs.york.ac.uk/~olaf/
 Tel: +44 1904 434756; Fax: +44 1904 432767

Reply via email to