如何解决无法创建幻像 Applicative functor 类
我正在尝试从 Monoid 实现幻影 Applicative functor,在论文 Applicative programming with effects 的第 4 节中进行了描述:
newtype Accy o a = Acc {acc :: o}
instance Monoid o => Applicative (Accy o) where
pure _ = Acc mempty
Acc o1 <*> Acc o2 = Acc (o1 `mappend` o2)
但是,我收到以下错误:
* Could not deduce (Functor (Accy o))
arising from the superclasses of an instance declaration
from the context: Monoid o
bound by the instance declaration
at phantom-applicative-functor.hs:7:10-41
* In the instance declaration for `Applicative (Accy o)'
|
7 | instance Monoid o => Applicative (Accy o) where
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
我尝试添加语言扩展,例如:
{-# LANGUAGE MultiParamTypeClasses #-}
但是,它没有帮助。是否可以实现论文中描述的幻象应用功能?
解决方法
正如错误所说,除了 Applicative
实例之外,您还必须提供一个 Functor
实例。你可以自己写:
instance Functor (Accy o) where fmap _ (Acc o) = Acc o
或请 GHC 为您编写:
{-# LANGUAGE DeriveFunctor #-}
newtype Accy o a = Acc {acc :: o} deriving Functor
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。