I try to work with the Reader Monad
m = Map.fromList [("a","b"), ("b","c"), ("c","d"), ("d","e")]
f x m = fromMaybe "default value" $ Map.lookup x m
lookup' x = f x m
Now I wanted to create a reader monad:
r = reader lookup'
-- Non type variable argument:
-- in the constraint MonadReader [Char] m
-- When checking the inferred type:
-- b :: forall (m :: * -> *). MonadReader [Char] m => m [Char]
The solution was to define the type:
r = reader lookup' :: Reader String String
Why does this solve the problem?
Control.Monad.Trans.Reader
instead ofControl.Monad.Reader
. The latter exposes a more general interface which sometimes makes more concise code, but also sometimes causes ambiguity messages like the one you got. To find out more about this particular sort of stuff, read up on the difference betweentransformers
andmtl
. – Alec