[Haskell] Mixing monadic and non-monadic functions

Frederik Eaton frederik at a5.repetae.net
Fri Sep 9 16:17:57 EDT 2005


On Thu, Sep 08, 2005 at 09:34:33AM +0100, Keean Schupke wrote:
> Can't you do automatic lifting with a "Runnable" class:
> 
>     class Runnable x y where
>        run :: x -> y
> 
>     instance Runnable (m a) (m a) where
>         run = id
> 
>     instance Runnable (s -> m a) (s -> m a) where
>         run = id
>      instance (Monad m,Monad n,MonadT t m,Runnable (m a) (n a)) => Runnable (t m a) (n a) where
>         run = run . down

Interesting...

>     instance (Monad m,MonadT t m,Monad (t m)) => Runnable (t m a) (m a) where
>         run = down

The above is redundant, right?

> Where:
> 
>     class (Monad m,Monad (t m)) => MonadT t m where
>        up :: m a -> t m a
>        down :: t m a -> m a
> 
> For example for StateT:
> ...

So, 'run' is more like a form of casting than running, right?

How do I use it to add two lists? Where do the 'run' applications go? 
Do you have an explicit example?

I was trying to test things out, and I'm running into problems with
the type system, for instance when I declare:

class Cast x y where
    cast :: x -> y

instance Monad m => Cast x (m x) where
    cast = return

p1 :: (Monad m, Num a) => m (a -> a -> a)
p1 = cast (+)

it says:

    Could not deduce (Cast (a1 -> a1 -> a1) (m (a -> a -> a)))
      from the context (Monad m, Num a)
      arising from use of `cast' at runnable1.hs:14:5-8

But this should match the instance I declared, I don't understand what
the problem is.

Frederik

-- 
http://ofb.net/~frederik/


More information about the Haskell mailing list