I was playing with the Reader monad in Purescript and I encountered a weird behaviour. I don't know if it's because my lack of comprehension of this monad or if I'm missing something else.
This is my code :
type Level = Number
type Doc = Reader Level String
renderLine :: String -> Level -> String
renderLine s 0 = s
renderLine s l = "\t" ++ (renderLine s (l - 1))
line :: String -> Doc
line s = do
level <- ask
return (renderLine s level)
This is okay and will compile. Nevertheless, before this I tried something simpler in my function line like this :
line :: String -> Doc
line s = do
level <- ask
return "Hello Reader monad"
And it won't compile, despite the fact that (renderLine s level) and "Hello Reader monad" has the same type. It will throw me this error : "No instance found for Control.Monad.Reader.Class.MonadReader u14555 (Control.Monad.Reader.Trans.ReaderT Prim.Number Control.Monad.Identity.Identity)"
I'm sure I'm missing something, but I don't know what.