一组转换 f::a->a 在函数组合上形成一个幺半群——如何使它成为一个幺半群的实例?
Set of transformations f::a->a form a monoid over function composition - how do I make this an instance of Monoid?
看起来好像变换应该形成一个Monoid,恒等函数为空元素,标准函数组合为二元运算。我不认为它特别有用,但它应该是可能的。大致如下:
instance Monoid (a -> a) where
mempty = id
mappend f g = (.)
以上无法编译,可能是因为它被预先存在的定义屏蔽了
instance Monoid b => Monoid (a -> b) where
mempty _ = mempty
mappend f g x = f x `mappend` g x
错误是:
Illegal instance declaration for ‘Monoid (a -> a)’
(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 (a -> a)’
我仍然是一个 Haskell 磨砂膏,所以我不确定如何解决这个问题 - 有什么帮助吗?
实际上错误信息描述得很好:a -> a
是一个太具体的类型:
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.
这里的T
是函数类型->
,你可以不用特殊的中缀符号写
instance Monoid ((->) a a) where …
显然 a
不止出现一次。
关于如何解决这个问题,错误消息再次推荐
Use FlexibleInstances
if you want to disable this [restriction].
看起来好像变换应该形成一个Monoid,恒等函数为空元素,标准函数组合为二元运算。我不认为它特别有用,但它应该是可能的。大致如下:
instance Monoid (a -> a) where
mempty = id
mappend f g = (.)
以上无法编译,可能是因为它被预先存在的定义屏蔽了
instance Monoid b => Monoid (a -> b) where
mempty _ = mempty
mappend f g x = f x `mappend` g x
错误是:
Illegal instance declaration for ‘Monoid (a -> a)’
(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 (a -> a)’
我仍然是一个 Haskell 磨砂膏,所以我不确定如何解决这个问题 - 有什么帮助吗?
实际上错误信息描述得很好:a -> a
是一个太具体的类型:
All instance types must be of the form
(T a1 ... an)
wherea1 ... an
are distinct type variables, and each type variable appears at most once in the instance head.
这里的T
是函数类型->
,你可以不用特殊的中缀符号写
instance Monoid ((->) a a) where …
显然 a
不止出现一次。
关于如何解决这个问题,错误消息再次推荐
Use
FlexibleInstances
if you want to disable this [restriction].