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

Modulelinear-base-0.4.0Haskell2010

Data.Replicator.Linear

This module defines a stream-like type named Replicator, which is mainly used in the definition of the Dupable class to provide efficient linear duplication. The API of Replicator is close to the one of an infinite stream: it can either produce a new value linearly (with next or next#), or be linearly discarded (with consume or extract).

A crucial aspect, from a performance standpoint, is that the pure function (which takes an unrestricted argument) is implemented efficiently: the Replicator returns the same value on each call to next. That is, the pointer is always shared. This will allow Movable types to be given an efficient instance of Dupable. Instances of both Movable and Dupable typically involve deep copies. The implementation of pure lets us make sure that, for Movable types, only one deep copy is performed, rather than one per additional replica.

Strictly speaking, the implementation of (<*>) plays a role in all this as well: For two pure Replicators fs and as, fs <*> as is a pure Replicator. Together, pure and (<*>) form the Applicative instance of Replicator.

  • 1 type
  • 1 class
  • 11 values
datadata Replicator a where
#

Replicator is a stream-like data structure used to linearly duplicate values.

Instances4Functor, Applicative, Consumable, Dupable
  • Functor ReplicatorDefined in linear-base-0.4.0 · Data.Replicator.Linear.Internal.Instances · orphan
  • Applicative ReplicatorDefined in linear-base-0.4.0 · Data.Replicator.Linear.Internal.Instances · orphan
  • Consumable (Replicator a)Defined in linear-base-0.4.0 · Data.Unrestricted.Linear.Internal.Consumable
  • Dupable (Replicator a)Defined in linear-base-0.4.0 · Data.Unrestricted.Linear.Internal.Dupable
classclass Elim (n :: Peano) a b where
#

Elim n a b is used to implement elim without recursion so that we can guarantee that elim will be inlined and unrolled.

Elim is solely used in the signature of elim.

Instances3Elim
  • Elim 'Z a bDefined in linear-base-0.4.0 · Data.Replicator.Linear.Internal
  • Elim ('S 'Z) a bDefined in linear-base-0.4.0 · Data.Replicator.Linear.Internal
  • Elim ('S n) a b => Elim ('S ('S n)) a bDefined in linear-base-0.4.0 · Data.Replicator.Linear.Internal
valueelim
  1. :: (Elim (NatToPeano n) a b, IsFunN a b f, f ~ FunN (NatToPeano n) a b, n ~ Arity b f)
  2. => f
  3. -> Replicator a
  4. -> b
#

Takes a function of type a %1 -> a %1 -> ... %1 -> a %1 -> b, and returns a b . The replicator is used to supply all the items of type a required by the function.

For instance:

elim @1 :: (a %1 -> b) %1 -> Replicator a %1 -> b
elim @2 :: (a %1 -> a %1 -> b) %1 -> Replicator a %1 -> b
elim @3 :: (a %1 -> a %1 -> a %1 -> b) %1 -> Replicator a %1 -> b

It is not always necessary to give the arity argument. It can be inferred from the function argument.

elim (,) :: Replicator a %1 -> (a, a)
elim (,,) :: Replicator a %1 -> (a, a, a)

About the constraints of this function (they won't get in your way):

  • Elim (NatToPeano n) a b provides the actual implementation of elim; there is an instance of this class for any (n, a, b)

  • IsFunN a b f, f ~ FunN (NatToPeano n) a b, n ~ Arity b f indicate the shape of f to the typechecker (see documentation of IsFunN).