0

I have a question about generalizing. Starting with this function:

test0 :: String -> String
test0 s = s

we can generalize it in its argument:

test1 :: forall a. Show a => a -> String
test1 s = show s

or in its functional result:

test12 :: forall a. Show a => String -> a
test12 s = s

Now consider the following function:

test2 :: forall e. Aff e Int
test2 s = pure 0

I would like to generalize it in its functional result:

test3 :: forall e m. MonadAff e m => m e Int
test3 s = pure 0

However, I now get an error: Could not match kind type with kind # Control.Monad.Eff.Effect while checking the kind of MonadAff e m => m e Int in value declaration test3.

I cannot understand why. Moreover, I've found an example of similar such generalizing in Hyper.Node.Server, for example in this type:

write :: forall m e. MonadAff e m => Buffer -> NodeResponse m e

1 Answers1

2

The constraint MonadAff e m asserts that the monad m somehow wraps Aff e somewhere inside. But it doesn't assert that monad m itself must have a type argument e. That would be awfully restrictive, wouldn't it?

Therefore, when constructing your return type, don't apply m to e:

test3 :: forall e m. MonadAff e m => m Int
test3 = pure 0

The example you found is quite different. Here, the function is not returning a value in m, like in your test3, but rather a value NodeResponse, which is a wrapper around a function that returns m Unit.

Fyodor Soikin
  • 78,590
  • 9
  • 125
  • 172
  • I see. Thank you! However, the resulting expression is still not OK, I find. The type does not describe the functional argument. Only if I change it to test3 :: forall e m. MonadAff e m => Int -> m Int the compiler accepts it. – Joop Ringelberg Feb 26 '18 at 07:33
  • `test3` doesn't need to accept an argument if you want to type it as `m Int` - it should just be `test3 = pure 0` – gb. Feb 26 '18 at 11:22
  • 1
    Quite true, @gb. However, I just wanted to point out that my original expression(s) contained two errors, not just one. – Joop Ringelberg Feb 26 '18 at 15:53