First of all let me apologize, yet I simply couldn't find the answer to my question although I'm pretty sure that this has been asked before. Now:
I would like to write Functor and Monad instances for a binary (search)
tree. More precisely, some of the functions like insert or merge require
instances of Ord, e.g.:
data Tree a = Empty | Node a (Tree a) (Tree a)
insert :: (Ord a) => Tree a -> a -> Tree a
merge :: (Ord a) => Tree a -> Tree a -> Tree a
Consequently, the following code doesn't compile:
instance Monad Tree where
{- ... -}
Empty >>= f = Empty
(Node v l r) >>= f = merge (f v) (merge newL newR)
where newL = l >>= f
newR = r >>= f
-- No instance for (Ord b) arising from a use of `merge'...
As I didn't know any better, I've tried declaring the Ord constraint on the
ADT using DatatypeContexts but that's deprecated and didn't work anyway. Other
extensions like FlexibleInstances etc. all seemed useful until I realized they
really meant something else.
So, is there a way and how would I go about it? Thank you for your time.
EDIT: Meanwhile I found this useful answer, but it appears as if doing this kind of stuff for typeclasses like Functor is still an issue.
MonadandFunctortype-classes. The constraint cannot hold because in any generic, monadic computation you can simply doreturn <some non-Ord value>.