[Haskell-cafe] Adding Ord constraint to instance Monad Set?

MR K P SCHUPKE k.schupke at imperial.ac.uk
Wed Mar 31 12:33:38 EST 2004

| "Constraints on datatype declarations are a misfeature of Haskell, and
| have no useful effect."
| Is this the final conclusion?

>Yes, it is, I believe.  Constraints on data type declarations are a
>mis-feature.  I tried to get them removed, but there was some argument
>that they could be made useful
Might I try and pursuade you that mis-feature is a bit strong.

A data type declaration creates two things - the data type itself
and the constructor function. 

If you consider the constrains in the data type declaration to
apply only to the constructor then it all makes sense.

functions do not infer constrains from enclosed functions, for

fna :: Num a => a -> a
fna a = -a

fnb :: a -> a
fnb a = fna a

In this case fna has a type error, the function is declared without the
required Num a constraint. This is the same for data constructors, consider:

data Num a => MyList a = MyList [a]

This means that the constuctor for mylist has the (Num a) constraint.
Simple! This is not a misfeature - more a mis-understood feature.

So, using:

print $ MyList ['a','b','c'] -- fails: No instance for (Num Char)
print $ MyList [1,2,3]       -- is Okay.

and unexpectedly a use of the constructor does not get the constraint:

fnc :: a -> MyList a
fnc a = MyList [a]           -- fails: Could not deduce (Num a) from the context ()

But if you consider the constructor to be just like a normal function this
is perfectly consistant.

This should definitely not be removed, as there is no other way to apply a
constaint to a constructor function.


More information about the Haskell-Cafe mailing list