[Haskell-cafe] Adding Ord constraint to instance Monad Set?
wolfgang at jeltsch.net
Wed Mar 31 10:44:15 EST 2004
Am Mittwoch, 31. März 2004 10:28 schrieb Henning Thielemann:
> On Wed, 31 Mar 2004, Simon Peyton-Jones wrote:
> > I'm sorry to say that you just can't make Set an instance of Monad.
> > To make an instance of Monad for a type constructor T you must have a
> > function
> > bindT :: forall a b. T a -> (a -> T b) -> T b
> > And there just *is* no such function for Set, because of the need for
> > ordering. There's a less polymorphic function
> > bindSet :: forall a b. (Ord a, Ord b)
> > => T a -> (a -> T b) -> T b
> To translate the question from my problem to this one:
> What would be if Set would be declared within Ord context, i.e.
> data (Ord a) => Set a = ...
> ? Now it would be safe to use Set as Monad because Sets could only
> constructed from Ord types.
> But GHC doesn't accept this and an answer to my surprise was (see
> "Constraints on datatype declarations are a misfeature of Haskell, and
> have no useful effect."
This answer is, of course, not very descriptive. The problem is that the
context (Ord a) in the data declaration only says that the the data
constructor(s) is/are restricted by this context. So if you have a
data (Ord a) => Set a = MkSet ...,
it means that MkSet has type
(Ord a) => ... -> Set a
instead of just
... -> Set a.
There still exist types Set element where element is not an instance of Ord.
You just cannot construct any value of such types except bottom (undefined).
More information about the Haskell-Cafe