无法创建幻影应用仿函数 class

Cannot create phantom Applicative functor class

我正在尝试从论文 Applicative programming with effects 的第 4 节中描述的 Monoid 实现幻影 Applicative 仿函数:

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 #-}

但是,它没有帮助。是否可以实现论文中描述的phantom applicative功能?

如错误所述,除了 Applicative 实例之外,您还必须提供 Functor 实例。可以自己写:

instance Functor (Accy o) where fmap _ (Acc o) = Acc o

或请GHC为您编写:

{-# LANGUAGE DeriveFunctor #-}
newtype Accy o a = Acc {acc :: o} deriving Functor