gpt4 book ai didi

haskell - 'Monad (Writer String)' 的非法实例声明

转载 作者:行者123 更新时间:2023-12-05 08:40:45 24 4
gpt4 key购买 nike

<分区>

我尝试创建自己的 Writer 类型,之后我还为它创建了一个实例。无论如何,我不断收到此错误:

Illegal instance declaration for ‘Monad (Writer String)’
(All instance types must be of the form (T a1 ... an)
where a1 ... an are *distinct type variables*,
and each type variable appears at most once in the instance head.
Use FlexibleInstances if you want to disable this.)
In the instance declaration for ‘Monad (Writer String)’

这是我的代码:

newtype Writer log a = Writer {runWriter :: (a,log)} 
instance Monad (Writer String) where
return a = Writer (a, "")
ma >>= k = let (a, log1) = runWriter ma
(b, log2) = runWriter (k a)
in Writer (b, log1 ++ log2)

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com