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

Anatoly Zaretsky anatoly.zaretsky at gmail.com
Fri Sep 15 11:36:35 EDT 2006


On 9/15/06, Vivian McPhail <vivian.mcphail at paradise.net.nz> wrote:
>
> class Forkable a where
>     fork :: String -> a -> a -> a
>
> ...
> {-
> 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')
> -}
>

Let's do manual type checking.
First, fork :: Forkable a => String -> a -> a -> a
So for Forkable (m a -> b)
  fork :: String -> (m a -> b) -> (m a -> b) -> m a -> b
Then
  fork n a1 a2 a :: b
But you define it as
  fork n a1 a2 a = do {...}
So it should be of type Monad t => t a, not just any `b'.

Instead, you can define
  instance (Monad m, Forkable (m b)) => Forkable (m a -> m b) where
    ...

Note that to compile it you also need -fallow-undecidable-instances
and -fallow-overlapping-instances.

--
Tolik


More information about the Haskell-Cafe mailing list