2014-12-25 89 views
1

我試圖從運行該代碼:使用ghci 7.6.3定義類型爲Monad的

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

這原始形式

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

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

http://stackoverflow.com/questions/16273896/implementing-this-monad-type-in​​-haskell http://stackoverflow.com/questions/24881351/why-cant-you-totally- apply-a-type-synonym-that-has-arguments-using-an-other-t – gliptak

+0

'C m'是'Cont(Action m)',所以你應該在這裏使用Cont(如果你想看看如何這是有效的,只是看Cont的來源)。 – user2407038

回答

5

部分應用的類型同義詞不能是類型實例的類型,在這種情況下避免這種情況的唯一方法是將其作爲數據或新類型聲明。

您必須更改C的定義才能使此工作成爲例如

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

你還可以添加綁定預計將如何改變?謝謝 – gliptak