我想为新类型写一个Alternative
实例Identity
。骨架并不难:
instance Alternative Identity where
empty = _
(<|>) = _
但是,所有类型都无法实现。如果我有一个 Monoid 实例,那将很容易a
:
instance Alternative Identity where
empty = Identity mempty
(Identity a) <|> (Identity a') = Identity (a <> a')
有没有办法告诉编译器我只想在Alternative
内部类型具有 Monoid 实例时才定义实例?由于a
没有在任何地方提及,我不能只使用约束Monoid a =>
。