@hackage idiomatic0.1.1.0

Deriving Applicative for sum types.. Idiomatically.

  • Installation

  • Dependencies (1)

  • Dependents (0)

  • Package Flags

      examples
       (off by default)

      Enable examples

Idiomatically is used with DerivingVia to derive Applicative for types with multiple constructors.

The name comes from the original paper on Applicatives: Idioms: applicative programming with effects.

It features an extensible domain-specific language of sums with Applicative instances. Idiomatically is then passed a type-level list of applicative sums that specify how deriving should take place.

{-# Language DataKinds          #-}
{-# Language DeriveGeneric      #-}
{-# Language DerivingStrategies #-}
{-# Language DerivingVia        #-}

import Generic.Applicative

data Zip a = No | a ::: Zip a
  deriving
  stock (Show, Generic1)

  deriving (Functor, Applicative)
  via Idiomatically Zip '[RightBias Terminal]

This derives the standard behaviour of ZipList but this same "RightBias Terminal" behaviour describes the Maybe and Validation applicative as well.

pure @Zip a = a ::: a ::: a ::: ...

liftA2 (+) No       No        = No
liftA2 (+) No       (⊥:::⊥)   = No
liftA2 (+) (⊥:::⊥)  No        = No
liftA2 (+) (2:::No) (10:::No) = 12:::No

Idiomatically shares an intimate relationship with Generically1: it is defined in terms of Generically1 and they are interchangeable when there is an empty list of sums:

type Generically1 :: (k -> Type) -> (k -> Type)
type Generically1 f = Idiomatically f '[]

Based on Abstracting with Applicatives.