Joop Ringelberg
Joop Ringelberg

Reputation: 37

generalize purescript function using MonadAff

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

Upvotes: 0

Views: 99

Answers (1)

Fyodor Soikin
Fyodor Soikin

Reputation: 80744

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.

Upvotes: 2

Related Questions