1
votes

I just tried to use writer:

writer (5, "Hello, World")

But I noticed I get an error:

Non type-variable argument in the constraint MonadWriter [Char] m

But I wonder what constraint I don't fulfill? [a] after all is a Monoid...

1
The other part of that particular error message should tell you how to fix it. You can enable what it suggests with a {-# LANGUAGE ... #-} pragma.David Young
It tells me to allow FlexibleContexts. But why is that even necessary? I fulfill all constraints?hgiesel
There's actually no problem here. You do fulfill all the constraints. The reason GHCi won't print out any result is that it doesn't know what exact type the whole thing is supposed to be – you didn't specify that. Indeed, generally it's not necessary because the type will be inferred from the context, but only if there is some context (e.g. wrap the entire thing in runWriter).leftaroundabout
Yes, enforcing the type writer (5,"foo") :: Writer String Char works. But why this behavior? All the other monads in Control.Monad. don't have this behavior...hgiesel
Without FlexibleContexts, MonadWriter [Char] m simply isn't a valid type (but it is the constraint of the inferred type of the given expression, so the given expression is not type correct). The same thing happens with other monad transformers (e.g. put "x" or ('a':) <$> ask)user2407038

1 Answers

0
votes

It’s just a check to keep type checking “simple”

writer (5, "Hello world") :: (MonadWriter String m, Num n) => m n

which is a perfectly sensible type. However, by default, it is not allowed, because the constraint MonadWriter String m contains a type argument String, which isn’t a variable. This was done with the intent that it would stop the delaying of type errors:

class C c where c :: c -> c
-- no instance C Int
x :: C Int => Int
x = c (5 :: Int)
-- illegal with above restriction
-- missing instance error (maybe) delayed to user without

Simply set -XFlexibleContexts to disable the check.

As for other monads, I’m pretty sure Reader, State, and Free will do this too.