MonadWriter的非类型变量参数

时间:2017-12-14 15:40:08

标签: haskell monads

我刚尝试使用writer

writer (5, "Hello, World")

但我注意到我收到了一个错误:

  

约束MonadWriter [Char] m

中的非类型变量参数

但我想知道我不能达到什么样的约束? [a]毕竟是Monoid ......

1 个答案:

答案 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,我非常确定ReaderStateFree也会这样做。