在Haskell,我想作一個作家單子幺半羣的一個實例:讓作家(哈斯克爾)的含半幺羣實例
instance (Monoid a) => Monoid (Writer (Sum Int) a) where
mempty = return mempty
w1 `mappend` w2 = writer((s++t, s'++t'), Sum (m+n)) where
((s,s'), Sum m) = runWriter w1
((t,t'), Sum n) = runWriter w2
因此,直觀地,如果「數據」類型的作家單子是一個獨異,我希望能夠考慮整個作家事,作爲一個獨異,以及(如mempty和mappend實施
這不工作,雖然:本GHCI編譯器說
Illegal instance declaration for `Monoid (Writer (Sum Int) a)'
(All instance types must be of the form (T t1 ... tn)
where T is not a synonym.
Use -XTypeSynonymInstances if you want to disable this.)
In the instance declaration for `Monoid (Writer (Sum Int) a)'
我真的沒有知道什麼類型應該是這裏的同義詞,以及我如何符合編譯器的規則。
不符合規則:按照編譯器的建議啓用'-XTypeSynonymInstances'來放鬆它們。 '-XFlexiblesInstances'也可能是必需的。 – user2407038