[Haskell-cafe] Re: Why is type 'b' forced to be type 'm a' and not possibly 'm a -> m a'

Vivian McPhail vivian.mcphail at paradise.net.nz
Wed Sep 20 07:01:41 EDT 2006


> Vivian McPhail wrote:
> > > > class Forkable a where
> > > >     fork :: String -> a -> a -> a
> ....
> > What I would like to be able to do is
> > differentiate between Forkable (m a ->
> > b) and Forkable (<function type> -> b).
> 
> Have you tried this combination of instances?
> 
>     instance Forkable (IO a) where ...
>     -- and similarly for all the concrete
>     -- monad types you will use fork with
> 
>     instance (Forkable a, Forkable b) =>
>              Forkable (a -> b) where ...
> 
> Alternatively, since the fork function seems to be all about
> propagating a value (the String), would Control.Monad.Reader
> serve your purpose?

The value that gets 'forked' is not actually the string, it is the result of
a monadic computation.

> 
> Regards,
> Tom
> 

I have tried:

> instance Forkable (USM NRef) where...

Which is my Monad

The problem lies with 

> instance (Forkable a, Forkable b) => Forkable (a -> b) where
>     fork n a1 a2 a = fork n (a1 a) (a2 a)

because I need the arg a to be evaluated before it gets passed to a1 and a2.
This definition does the right thing when type 'a' is a function type,
because it is not a value, but with something like 'm a -> (m a -> m a) -> m
a' with Forkable (a -> b) the first arg gets evaluated twice, to be more
concrete:

With

(and golden white) eggs

I want the 'eggs' that is passed to 'golden' to be the same as the 'eggs'
that is passed to 'white', i.e.

-> and1 (golden2 eggs3) (white4 eggs3) and not -> and1 (golden2 eggs3)
(white4 eggs5)

So to do this I need to be able to recognise the case where the 'a' of (a ->
b) is of type 'm a' so that I can evaluate it

-- doesn't typecheck
instance (Monad m, Forkable (m a), Forkable b) => Forkable (m a -> b) where
    fork n a1 a2 a = do
                     a' <- a
                     fork n (a1 $ return a') (a2 $ return a')

Tom suggested that I might be able to use the Reader monad, but I'm not
clear as to how I could do this.

Cheers,

Vivian





-- 
No virus found in this outgoing message.
Checked by AVG Free Edition.
Version: 7.1.405 / Virus Database: 268.12.5/451 - Release Date: 19/09/2006
 



More information about the Haskell-Cafe mailing list