[Haskell-cafe] Constraints on data-types, mis-feature?
Jonathan Cast
jcast at ou.edu
Mon Jul 9 12:10:29 EDT 2007
On Monday 09 July 2007, Daniil Elovkov wrote:
> Hello
>
> In the archives of haskell-cafe I found a mention of constraints on
> datatypes as a mis-feature of Haskell. In particular, that they're not
> propagated well. Can someone elaborate on that?
>
> Also, are they still considered a mis-feature with the emergence of GADTs ?
>
> If I have
>
> data GADT a where
> ...
> Alt :: (a -> b -> c) -> GADT a -> GADT b -> GADT c
> ...
>
> and I want to constrain all a, b, c.
>
> Would it be better to expose all of them as type vars, rather than
> constrain in-place? It would lead to a rather verbose code.
GADTs don't change anything (at least, not the last time I checked). If you
say
class C a where
...
data GADT a where
...
Alt :: (C a, C b, C c) => (a -> b -> c) -> GADT a -> GADT b -> GADT c
...
when you pattern match on Alt, the compiler finds the instances for C a and C
b, but the constraint C c is ignored. So constraints on data types work
exactly the same way they always have, and the standard arguments against
them all still work. (Although now I think the status of this `feature' can
be down-graded to wart: after all, if you say
newtype Id a = Id a
data GADT a where
...
Alt :: (C a, C b, C c) => (a -> b -> c)
-> GADT (Id a) -> GADT (Id b) -> GADT (Id c)
...
pattern-matching on Alt introduces all three constraints into the current
context. . .)
Jonathan Cast
http://sourceforge.net/projects/fid-core
http://sourceforge.net/projects/fid-emacs
More information about the Haskell-Cafe
mailing list