我有一个类似于这样的函数:
返回的错误如下:
transition :: State -> ([State], [State])
鉴于我的问题领域,我知道如何链接两个连续的transition
函数调用,就像这样:
transition `chain` trainsition ... `chain` transition
然而,我希望将其表达为Monoid
并使用<>
和mappend
进行链接。不幸的是,我似乎不能使以下内容或类似变体起作用:
instance Monoid (State -> ([State], [State])) where
mempty = ...
mappend = ...
返回的错误如下:
• Illegal instance declaration for
‘Monoid (State -> ([State], [State]))’
(All instance types must be of the form (T a1 ... an)
where a1 ... an are *distinct type variables*,
and each type variable appears at most once in the instance head.
Use FlexibleInstances if you want to disable this.)
• In the instance declaration for
‘Monoid (State -> ([State], [State]))’
一般来说,如何将函数表示为Monoid
的实例?
FlexibleInstances
扩展。 - jakubdanielMonoid
,箭头类型就有一个实例Monoid
。如果a
和b
都是Monoid
,那么(a, b)
也是Monoid
,因此Haskell可能会选择与您认为的不同的实例。 - jakubdanielMonad
版本。 - Anton Xue