0

There's a Monad I'm trying to define, lets call it MyData.

Inside MyData there's some IO actions going on, there's a function runMyMonad that executes them.

Naturally I have to define >>=, with the following type signature:

instance Monad MyData ...
(>>=) :: MyData a -> (a -> MyData b) -> MyData b

I'd particularly like to define a Monad because I'd like users to be able to use "do" notation.

The thing is, to actually do what I want to do, I need to know some information that entirely depends on the type of b. Like the following:

myfunc (undefined :: b)

I could create a type class like so, but then there's a problem, my Monad no longer works because Monad requires a and b to be unrestricted.

I could create MyData like the following:

data MyData a where
  MyData :: (MyTypeClass a) => a -> a -> MyData a

Or something similar. That way when I pattern match on it, I get the typeclass for free. But I can only see this working on the first argument of >>=, as the second is wrapped up in a function.

So this is what I want to do:

a) Be able to apply a function that depends only on the type b in >>=.
b) Be able to use do notation.
c) Not affect other uses of Monad.

Any ideas will be appreciated.

Clinton
  • 22,361
  • 15
  • 67
  • 163
  • 3
    You cannot write a `Monad` instance and have a class constraint which would allow you to get value-level information about a type in the bind function - you *can* but you must change the specification of `Monad`, obviously. If you just need do notation, maybe you should look into `RebindableSyntax`. – user2407038 Dec 05 '15 at 08:33
  • 2
    Possible duplicate of [What's the current status of restricted monads?](http://stackoverflow.com/questions/11599685/whats-the-current-status-of-restricted-monads). – effectfully Dec 05 '15 at 14:41

0 Answers0