我刚尝试使用writer
:
writer (5, "Hello, World")
但我注意到我收到了一个错误:
约束
中的非类型变量参数MonadWriter [Char] m
但我想知道我不能达到什么样的约束? [a]
毕竟是Monoid ......
答案 0 :(得分:0)
只是检查以确保类型检查“简单”
writer (5, "Hello world") :: (MonadWriter String m, Num n) => m n
这是一种非常明智的类型。但是,默认情况下,不允许这样做,因为约束MonadWriter String m
包含类型参数String
,它不是变量。这样做的目的是它可以停止延迟类型错误:
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
只需设置-XFlexibleContexts
即可停用支票。
至于其他monad,我非常确定Reader
,State
和Free
也会这样做。