HORIZON HASKELLDocslts/ghc-9.10.x248f8f02026-10-05Search names, modules, packages, or :: a typeCtrl K

GHC 9.10.3 · lts/ghc-9.10.x · 248f8f0 · 2026-10-05

Moduleone-liner-2.1Haskell2010

Generics.OneLiner

All functions without postfix are for instances of Generic, and functions with postfix 1 are for instances of Generic1 (with kind Type -> Type) which get an extra argument to specify how to deal with the parameter. Functions with postfix 01 are also for Generic1 but they get yet another argument that, like the Generic functions, allows handling of constant leaves. The function createA_ does not require any such instance, but must be given a constructor explicitly.

  • 9 types
  • 11 classes
  • 44 values
  • Packageone-liner-2.1
  • Exports65
  • LanguageHaskell2010
  • LicenceBSD-3-Clause
  • SourceOneLiner.hs

Producing values

7 declarations
valuectorIndex :: ADT t => t -> Int
#

Get the index in the lists returned by create and createA of the constructor of the given value.

For example, this is the implementation of put that generates the binary data that the above implentation of get expects:

put t = putWord8 (toEnum (ctorIndex t)) <> gfoldMap @Binary put t
valuecreateA_
  1. :: (FunConstraints c t, Applicative f)
  2. => forall s. c s => f s
  3. -> t
  4. -> f (FunResult t)
#

Create a value, given a constructor (or a function) and how to construct its components, under an applicative effect.

For example, this is the implementation of Test.QuickCheck.arbitrary for a type with a single constructor (e.g., quadruples (,,,)).

arbitrary = createA_ @Arbitrary arbitrary (,,,)

Traversing values

12 declarations
valuegmap :: (ADT t, Constraints t c) => (forall s. c s => s -> s) -> t -> t
#

Map over a structure, updating each component.

gmap is generic specialized to (->).

valuegfoldMap
  1. :: (ADT t, Constraints t c, Monoid m)
  2. => forall s. c s => s -> m
  3. -> t
  4. -> m
#

Map each component of a structure to a monoid, and combine the results.

If you have a class Size, which measures the size of a structure, then this could be the default implementation:

size = succ . getSum . gfoldMap @Size (Sum . size)

gfoldMap is gtraverse specialized to Const.

valueglmap
  1. :: (ADT t, Constraints t c)
  2. => forall s. c s => s %1 -> s
  3. -> t
  4. -> t
#

Map over a structure linearly, updating each component.

glmap is generic specialized to the linear arrow.

Combining values

6 declarations
valuezipWithA
  1. :: (ADT t, Constraints t c, Alternative f)
  2. => forall s. c s => s -> s -> f s
  3. -> t
  4. -> t
  5. -> f t
#

Combine two values by combining each component of the structures with the given function, under an applicative effect. Returns empty if the constructors don't match.

zipWithA is generic specialized to Zip

valuemzipWith1'
  1. :: (ADT1 t, Constraints1 t c, Monoid m)
  2. => m
  3. -> forall (s :: Type -> Type) b. c s => (b -> b -> m) -> s b -> s b -> m
  4. -> a -> a -> m
  5. -> t a
  6. -> t a
  7. -> m
#

Variant of mzipWith1 where you can choose the value which is returned when the constructors don't match.

Consuming values

2 declarations

Functions for records

8 declarations

These functions only work for single constructor data types.

valuecreateA'
  1. :: (ADTRecord t, Constraints t c, Applicative f)
  2. => forall s. c s => f s
  3. -> f t
#

Create a value of a record type (with exactly one constructor), given how to construct the components, under an applicative effect.

Here's how to implement get from the binary package:

get = createA' (For :: For Binary) get

createA' is record specialized to Joker.

Generic programming with profunctors

9 declarations

All the above functions have been implemented using these functions, using different profunctors.

Classes

A generic function using a GenericRecordProfunctor works on any data type with exactly one constructor, a.k.a. records, with multiple fields (mult) or no fields (unit).

GenericRecordProfunctor is similar to ProductProfuctor from the product-profunctor package, but using types from GHC.Generics.

Instances1GenericRecordProfunctor
classclass Profunctor p => GenericUnitProfunctor (p :: Type -> Type -> Type) where
#

Methods

Instances13GenericUnitProfunctor, …
classclass Profunctor p => GenericProductProfunctor (p :: Type -> Type -> Type) where
#

Methods

  • mult :: p (f a) (f' a') -> p (g a) (g' a') -> p ((:*:) f g a) ((:*:) f' g' a')
Instances13GenericProductProfunctor, …
classclass Profunctor p => GenericSumProfunctor (p :: Type -> Type -> Type) where
#

Methods

  • plus :: p (f a) (f' a') -> p (g a) (g' a') -> p ((:+:) f g a) ((:+:) f' g' a')
Instances10GenericSumProfunctor, …
classclass Profunctor p => GenericEmptyProfunctor (p :: Type -> Type -> Type) where
#

Methods

Instances10GenericEmptyProfunctor, …
classclass Profunctor p => GenericConstantProfunctor (p :: Type -> Type -> Type) where
#

Methods

Instances10GenericConstantProfunctor, …

Types

12 declarations
classclass FunConstraints (c :: Type -> Constraint) t where
#

Automatically apply a lifted function to a polymorphic argument as many times as possible.

A constraint `FunConstraint c t` is equivalent to the conjunction of constraints `c s` for every argument type of t.

If r is not a function type:

c a :- FunConstraints c (a -> r)
(c a, c b) :- FunConstraints c (a -> b -> r)
(c a, c b, c d) :- FunConstraints c (a -> b -> d -> r)
Instances2FunConstraints
classclass AnyType (a :: k)
#

Any type is instance of AnyType, you can use it with @AnyType if you don't actually need a class constraint.

Instances1AnyType
  • AnyType aDefined in one-liner-2.1 · Generics.OneLiner.Internal.Unary