在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