[英]How to derive Additive generically on Haskell, without defining an Applicative instance?
Given a type, there is only one obvious way to implement an Additive
instance, from the Linear library, to it. 给定一个类型,只有一种明显的方法可以实现
Additive
实例,从Linear库到它。 Conveniently, Additive
has a generic implementation, so we can use deriving
for it. 方便的是,
Additive
有一个通用的实现,所以我们可以使用deriving
它。 Unfortunately, it depends on the existence of an Applicative
instance, which is not derivable, so you still have to declare it: 不幸的是,它取决于
Applicative
实例的存在,它不是可派生的,所以你仍然必须声明它:
{-# LANGUAGE DeriveGeneric, DeriveFunctor #-}
import Linear
import GHC.Generics
import Control.Applicative
data Foo a = Foo a a a deriving (Show, Functor, Generic1)
instance Additive Foo
instance Applicative Foo where
pure x = Foo x x x
Foo f g h <*> Foo x y z = Foo (f x) (g y) (h z)
main = print $ Foo 1 2 3 ^+^ Foo 4 5 6
Is there any way to derive Additive automatically, without having to declare an Applicative instance? 有没有办法自动派生Additive,而不必声明Applicative实例?
No. 没有。
The canonical example of a datatype which has two perfectly cromulent Applicative
instances is []
/ ZipList
. 具有两个完美的
Applicative
实例的数据类型的规范示例是[]
/ ZipList
。 This proves that a generic derivation of Applicative
for []
would need to somehow choose one or the other, when in fact neither choice is more valid than the other. 这证明了
[]
的Applicative
的泛型推导需要以某种方式选择一个或另一个,而实际上这两个选择都不比另一个更有效。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.