有一个实例Monoid a => Monoid (Const a b)用于来自Control.Applicative的Const函子。还有一个实例Monoid m => Applicative (Const m)。
因此,我希望也有一个实例Monoid m => Alternative (Const m)与Monoid的实例相吻合。这只是一个应该纠正的遗漏,还是有更深层次的原因?
发布于 2015-02-25 08:15:04
我相信还有更深层次的原因。虽然Alternative似乎没有规范的规则集,但是为了使Alternative有意义,Alternative和它的Applicative操作之间肯定应该有一种关系(否则它只是一个任意的单子)。
This answer to Confused by the meaning of the 'Alternative' type class and its relationship to other type classes声明了这些法律
<*>**):*‘(f <|> g) <> a=(f <> a) <|> (g <> a)’‘<*>**):*‘ <> a=空)fmap**):** f <$> (a <|> b) = (f <$> a) <|> (f <$> b)的)左分布fmap**):** 左吸收( f <$> empty = empty )这对我来说很有意义。粗略地说,empty和<|>对于pure和<$>/<*>就像0和+对于1和*一样。
现在,如果我们添加与Monoid / Applicative的实例相同的实例,那么正确的法律就不成立了。
例如,2失败,因为
empty <*> (Const x)
= Const mempty <*> Const x -- by the suggested definition of Alternative
= Const $ mempty `mappend` x -- by the definition of <*> for COnst
= Const x -- by monoid laws这不等于empty = Const mempty。类似地,1失败,一个简单的反示例就是设置f = Const (Sum 1); g = Const (Sum 1) ; a = Const (Sum 1)。
另请参阅:
https://stackoverflow.com/questions/28681260
复制相似问题