将类型定义为Monad

时间:2014-12-25 16:21:44

标签: haskell monads

我试图运行以下代码:

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)'

如何更正此定义?感谢

1 个答案:

答案 0 :(得分:5)

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

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

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