[Haskell-cafe] AT solution: rebinding >>= for restricted monads

David Roundy droundy at darcs.net
Sun Dec 17 19:27:21 EST 2006

Here's a sketch of an idea as a solution to my dilemma, which unfortunately
requires associated types.  Any suggestions how it might be translatable
into functional dependencies? (I should say, I've not got a HEAD ghc, and
am just going by memory on my indexed types syntax.)

class Witness w where
    type M w a b
    (>>=) :: M w a b x -> (x -> M w b c y) -> M w a c y
    (>>) :: M w a b x -> M w b c y -> M w a c y
    f >> g = f >>= const g
    return :: x -> M w a a x
    fail :: String -> M w a b x

instance Monad m => Witness m where
    M m a b = m
    (>>=) = Prelude.(>>=)
    (>>) = Prelude.(>>)
    return = Prelude.return
    fail = Prelude.fail

with these definitions it seems that any existing monad will continue to
work as it always had, but I can now add new special sorts of monadish
objects, such as

data RepositoryMonad a b x = RM ...
instance Witness RepositoryMonad where
   M RepositoryMonad x y = RepositoryMonad x y

which would allow me to create a monad in which the actions are limited
according to witness types, such as

applyPatchToWorking :: Patch a b -> RepositoryMonad (rec,a) (rec,b) ()
David Roundy

More information about the Haskell-Cafe mailing list