gpt4 book ai didi

haskell - 将类型定义为 Monad

转载 作者:行者123 更新时间:2023-12-02 01:01:49 24 4
gpt4 key购买 nike

我正在尝试从以下位置运行代码:

http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.39.8039&rep=rep1&type=pdf

使用 ghci 7.6.3

{-# LANGUAGE LiberalTypeSynonyms, TypeSynonymInstances #-}
type C m a = (a -> Action m) -> Action m
data Action m = Atom (m (Action m)) | Fork (Action m) (Action m) | Stop

这个原始形式:

instance (Monad m) => Monad (C m) where
f >>= k = \c -> f (\a -> k a c)
return x = \c -> c x

给出这个错误:

Type synonym `C' should have 2 arguments, but has been given 1
In the instance declaration for `Monad (C m)'

尝试使用附加参数:

instance (Monad m) => Monad (C m b) where
f >>= k = \c -> f (\a -> k a c)
return x = \c -> c x

显示此错误:

Kind mis-match
The first argument of `Monad' should have kind `* -> *',
but `C m b' has kind `*'
In the instance declaration for `Monad (C m b)'

如何更正这个定义?谢谢

最佳答案

部分应用的类型同义词不能是类型类实例,在这种情况下避免这种情况的唯一方法是将其设为数据或新类型声明。

您必须更改 C 的定义才能使其工作,例如

newtype C m a = C ((a -> Action m) -> Action m)

关于haskell - 将类型定义为 Monad,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27648898/

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