How to get Additive in general on Haskell without defining an applicative instance?

Given the type, there is only one obvious way to implement an Additive instance, from the Linear library, to it. Conveniently, Additive has a common implementation, so we can use deriving for it. Unfortunately, this depends on the existence of an Applicative instance that is not output, so you should still declare it:

 {-# LANGUAGE DeriveGeneric, DeriveFunctor #-} import Linear import GHC.Generics import Control.Applicative data Foo a = Foo aaa deriving (Show, Functor, Generic1) instance Additive Foo instance Applicative Foo where pure x = Foo xxx Foo fgh <*> Foo xyz = Foo (fx) (gy) (hz) main = print $ Foo 1 2 3 ^+^ Foo 4 5 6 

Is there a way to get Additive automatically without declaring an applicative instance?

+5
source share
1 answer

Not.

A canonical example of a data type that has two perfectly rotated Applicative instances is [] / ZipList . This proves that Applicative general conclusion for [] must somehow choose one or the other, when in fact none of the options is more correct than the other.

+1
source

All Articles