0

Consider a kind m : * -> * and assume that I know how to define (let's say) a monadic structure on m, but only when its arguments satisfy some constraint C, for instance Eq. In other words, I have a function

(>>=) :: => m a -> (a -> m b) -> b`

that satisfies the monad laws, but in order to define it I would need to have (Eq a, Eq b).

Unfortunately, the definition

instance Monad m where
    (>>=) :: (Eq a, Eq b) => m a -> (a -> m b) -> b

is considered illegal. This is a bummer because I only plan on using m on Eq-types, and this condition could easily be statically checked.

Is there some way to get around this? Either conceptually, or practically?

Many thanks

user1892304
  • 617
  • 1
  • 6
  • 11
  • May be [`{-# LANGUAGE InstanceSigs #-}`](https://downloads.haskell.org/~ghc/latest/docs/html/users_guide/glasgow_exts.html#extension-InstanceSigs) language extension can help you. – Redu Apr 19 '20 at 18:23
  • 1
    @Redu `InstanceSigs` won't help. Quoting the docs: "The type signature in the instance declaration must be more polymorphic than (or the same as) the one in the class declaration, instantiated with the instance type". – duplode Apr 19 '20 at 18:28
  • In brief, it is not really possible with the usual `Monad` class. The suggested questions and their answers cover a number of possible workarounds. – duplode Apr 19 '20 at 18:37

0 Answers0