HORIZON HASKELLDocslts/ghc-9.10.xc74966e2026-09-27Search names, modules, packages, or :: a typeCtrl K

GHC 9.10.3 · lts/ghc-9.10.x · c74966e · 2026-09-27

Modulelinear-base-0.4.0Haskell2010

Data.Functor.Linear

The data functor hierarchy

This module defines the data functor library. Unlike in the case of non-linear, unrestricted, functors, there is a split between data functors, which represent containers, and control functors which represent effects. Please read this blog post. For more details, see Control.Functor.Linear.

  • Linear data functors should be thought of as containers of data.

  • Linear data applicative functors should be thought of as containers that can be zipped.

  • Linear data traversable functors should be thought of as containers which store a finite number of values.

This module also defines genericTraverse for types implementing Generic1.

  • 1 type
  • 4 classes
  • 10 values

Data Functor Hierarchy

6 declarations
classclass Functor (f :: Type -> Type) where
#

Linear Data Functors should be thought of as containers holding values of type a over which you are able to apply a linear function of type a %1-> b on each value of type a in the functor and consume a given functor of type f a.

Methods

  • fmap :: (a %1 -> b) -> f a %1 -> f b
Instances53Functor, …
  • Functor IdentityDefined in linear-base-0.4.0 · Data.Functor.Linear.Internal.Functor
  • Functor Par1Defined in linear-base-0.4.0 · Data.Functor.Linear.Internal.Functor
  • Functor MaybeDefined in linear-base-0.4.0 · Data.Functor.Linear.Internal.Functor
  • Functor ArrayDefined in linear-base-0.4.0 · Data.Array.Mutable.Linear.Internal
  • Functor ArrayDefined in linear-base-0.4.0 · Data.Array.Polarized.Pull.Internal
  • Functor ArrayDefined in linear-base-0.4.0 · Data.Array.Polarized.Push
  • Functor ReplicatorDefined in linear-base-0.4.0 · Data.Replicator.Linear.Internal.Instances · orphan
  • Functor ReplicationStreamDefined in linear-base-0.4.0 · Data.Replicator.Linear.Internal.Instances · orphan
  • Functor UrDefined in linear-base-0.4.0 · Data.Functor.Linear.Internal.Functor
  • Functor VectorDefined in linear-base-0.4.0 · Data.Vector.Mutable.Linear.Internal
  • Functor IODefined in linear-base-0.4.0 · System.IO.Linear
  • Functor RIODefined in linear-base-0.4.0 · System.IO.Resource.Linear.Internal
  • Functor []Defined in linear-base-0.4.0 · Data.Functor.Linear.Internal.Functor
  • Functor f => Functor (Data f)Defined in linear-base-0.4.0 · Control.Functor.Linear.Internal.Instances
  • Functor U1Defined in linear-base-0.4.0 · Data.Functor.Linear.Internal.Functor
  • Functor UAddrDefined in linear-base-0.4.0 · Data.Functor.Linear.Internal.Functor
  • Functor UCharDefined in linear-base-0.4.0 · Data.Functor.Linear.Internal.Functor
  • Functor UDoubleDefined in linear-base-0.4.0 · Data.Functor.Linear.Internal.Functor
  • Functor UFloatDefined in linear-base-0.4.0 · Data.Functor.Linear.Internal.Functor
  • Functor UIntDefined in linear-base-0.4.0 · Data.Functor.Linear.Internal.Functor
  • Functor UWordDefined in linear-base-0.4.0 · Data.Functor.Linear.Internal.Functor
  • Functor V1Defined in linear-base-0.4.0 · Data.Functor.Linear.Internal.Functor
  • Functor (Either e)Defined in linear-base-0.4.0 · Data.Functor.Linear.Internal.Functor
  • Functor (Tuple2 a)Defined in linear-base-0.4.0 · Data.Functor.Linear.Internal.Functor
  • Functor (Yoneda f)Defined in linear-base-0.4.0 · Control.Functor.Linear.Internal.Kan
  • Functor (StateR s)Defined in linear-base-0.4.0 · Data.Functor.Linear.Internal.Traversable
  • Functor (HashMap k)Defined in linear-base-0.4.0 · Data.HashMap.Mutable.Linear.Internal
  • Functor (V n)Defined in linear-base-0.4.0 · Data.V.Linear.Internal.Instances · orphan
  • Functor (Of a)Defined in linear-base-0.4.0 · Streaming.Linear.Internal.Type
  • Functor m => Functor (MaybeT m)Defined in linear-base-0.4.0 · Data.Functor.Linear.Internal.Functor
  • Functor (Const x)Defined in linear-base-0.4.0 · Data.Functor.Linear.Internal.Functor
  • Functor (Tuple3 a b)Defined in linear-base-0.4.0 · Data.Functor.Linear.Internal.Functor
  • Functor g => Functor (Curried g h)Defined in linear-base-0.4.0 · Control.Functor.Linear.Internal.Kan
  • Functor m => Functor (ReaderT r m)Defined in linear-base-0.4.0 · Control.Functor.Linear.Internal.Reader
  • Functor m => Functor (StateT s m)Defined in linear-base-0.4.0 · Control.Functor.Linear.Internal.State
  • Functor m => Functor (ExceptT e m)Defined in linear-base-0.4.0 · Data.Functor.Linear.Internal.Functor
  • Functor m => Functor (ReaderT r m)Defined in linear-base-0.4.0 · Data.Functor.Linear.Internal.Functor
  • Functor m => Functor (StateT s m)Defined in linear-base-0.4.0 · Data.Functor.Linear.Internal.Functor
  • (Functor m, Functor f) => Functor (Stream f m)Defined in linear-base-0.4.0 · Streaming.Linear.Internal.Type
  • (Generic1 f, Functor (Rep1 f)) => Functor (Generically1 f)Defined in linear-base-0.4.0 · Data.Functor.Linear.Internal.Functor
  • Functor (K1 i v)Defined in linear-base-0.4.0 · Data.Functor.Linear.Internal.Functor
  • Functor (Tuple4 a b c)Defined in linear-base-0.4.0 · Data.Functor.Linear.Internal.Functor
  • Functor (ContT r m)Defined in linear-base-0.4.0 · Data.Functor.Linear.Internal.Functor
  • Functor f => Functor (MP1 m f)Defined in linear-base-0.4.0 · Data.Functor.Linear.Internal.Functor
  • (Functor f, Functor g) => Functor (Product f g)Defined in linear-base-0.4.0 · Data.Functor.Linear.Internal.Functor
  • (Functor f, Functor g) => Functor (Sum f g)Defined in linear-base-0.4.0 · Data.Functor.Linear.Internal.Functor
  • (Functor f, Functor g) => Functor (f :*: g)Defined in linear-base-0.4.0 · Data.Functor.Linear.Internal.Functor
  • (Functor f, Functor g) => Functor (f :+: g)Defined in linear-base-0.4.0 · Data.Functor.Linear.Internal.Functor
  • Functor (FUN 'One a)Defined in linear-base-0.4.0 · Data.Functor.Linear.Internal.Functor
  • Functor (Tuple5 a b c d)Defined in linear-base-0.4.0 · Data.Functor.Linear.Internal.Functor
  • Functor f => Functor (M1 i c f)Defined in linear-base-0.4.0 · Data.Functor.Linear.Internal.Functor
  • (Functor f, Functor g) => Functor (Compose f g)Defined in linear-base-0.4.0 · Data.Functor.Linear.Internal.Functor
  • (Functor f, Functor g) => Functor (f :.: g)Defined in linear-base-0.4.0 · Data.Functor.Linear.Internal.Functor
value(<$>) :: Functor f => (a %1 -> b) -> f a %1 -> f b
#
value(<$) :: (Functor f, Consumable b) => a -> f b %1 -> f a
#

Replace all occurances of b with the given a and consume the functor f b.

valuevoid :: (Functor f, Consumable a) => f a %1 -> f ()
#

Discard a consumable value stored in a data functor.

classclass Functor f => Applicative (f :: Type -> Type) where
#

Data Applicative-s can be seen as containers which can be zipped together. A prime example of data Applicative are vectors of known length (ZipLists would be, if it were not for the fact that zipping them together drops values, which we are not allowed to do in a linear container).

In fact, an applicative functor is precisely a functor equipped with (pure and) liftA2 :: (a %1-> b %1-> c) -> f a %1-> f b %1-> f c. In the case where f = [], the signature of liftA2 would specialise to that of zipWith.

Intuitively, the type of liftA2 means that Applicatives can be seen as containers whose "number" of elements is known at compile-time. This includes vectors of known length but excludes Maybe, since this may contain either zero or one value. Similarly, ((->) r) forms a Data Applicative, since this is a (possibly infinitary) container indexed by r, while lists do not, since they may contain any number of elements.

Remarks for the mathematically inclined

An Applicative is, as in the restricted case, a lax monoidal endofunctor of the category of linear types. That is, it is equipped with

  • a (linear) function () %1-> f ()

  • a (linear) natural transformation (f a, f b) %1-> f (a, b)

It is a simple exercise to verify that these are equivalent to the definition of Applicative. Hence that the choice of linearity of the various arrow is indeed natural.

Methods

  • pure :: a -> f a
  • (<*>) :: f (a %1 -> b) %1 -> f a %1 -> f binfixl 4
  • liftA2 :: (a %1 -> b %1 -> c) -> f a %1 -> f b %1 -> f c
Instances23Applicative, …
newtypenewtype Const a (b :: k)
#

The Const functor.

Examples
Example1 expression
fmap (++ "World") (Const "Hello")Const "Hello"

Because we ignore the second type parameter to Const, the Applicative instance, which has (<*>) :: Monoid m => Const m (a -> b) -> Const m a -> Const m b essentially turns into Monoid m => m -> m -> m, which is (<>)

Example1 expression
Const [1, 2, 3] <*> Const [4, 5, 6]Const [1,2,3,4,5,6]

Constructors

Instances69Strong, Generic1, Bifoldable, Bifoldable1, Bifunctor, Bitraversable, …

Linear traversable hierarchy

9 declarations
classclass Functor t => Traversable (t :: Type -> Type) where
#

A linear data traversible is a functor of type t a where you can apply a linear effectful action of type a %1-> f b on each value of type a and compose this to perform an action on the whole functor, resulting in a value of type f (t b).

To learn more about Traversable, see here:

Methods

Instances23Traversable, …
  • Traversable Par1Defined in linear-base-0.4.0 · Data.Functor.Linear.Internal.Traversable
  • Traversable MaybeDefined in linear-base-0.4.0 · Data.Functor.Linear.Internal.Traversable
  • Traversable []Defined in linear-base-0.4.0 · Data.Functor.Linear.Internal.Traversable
  • KnownNat n => Traversable (V n)Defined in linear-base-0.4.0 · Data.V.Linear.Internal.Instances · orphan
  • Traversable U1Defined in linear-base-0.4.0 · Data.Functor.Linear.Internal.Traversable
  • Traversable UAddrDefined in linear-base-0.4.0 · Data.Functor.Linear.Internal.Traversable
  • Traversable UCharDefined in linear-base-0.4.0 · Data.Functor.Linear.Internal.Traversable
  • Traversable UDoubleDefined in linear-base-0.4.0 · Data.Functor.Linear.Internal.Traversable
  • Traversable UFloatDefined in linear-base-0.4.0 · Data.Functor.Linear.Internal.Traversable
  • Traversable UIntDefined in linear-base-0.4.0 · Data.Functor.Linear.Internal.Traversable
  • Traversable UWordDefined in linear-base-0.4.0 · Data.Functor.Linear.Internal.Traversable
  • Traversable V1Defined in linear-base-0.4.0 · Data.Functor.Linear.Internal.Traversable
  • Traversable (Either a)Defined in linear-base-0.4.0 · Data.Functor.Linear.Internal.Traversable
  • Traversable (Tuple2 a)Defined in linear-base-0.4.0 · Data.Functor.Linear.Internal.Traversable
  • Traversable (Const a)Defined in linear-base-0.4.0 · Data.Functor.Linear.Internal.Traversable
  • Traversable (Tuple3 a b)Defined in linear-base-0.4.0 · Data.Functor.Linear.Internal.Traversable
  • Traversable (K1 i v)Defined in linear-base-0.4.0 · Data.Functor.Linear.Internal.Traversable
  • Traversable (Tuple4 a b c)Defined in linear-base-0.4.0 · Data.Functor.Linear.Internal.Traversable
  • (Traversable f, Traversable g) => Traversable (f :*: g)Defined in linear-base-0.4.0 · Data.Functor.Linear.Internal.Traversable
  • (Traversable f, Traversable g) => Traversable (f :+: g)Defined in linear-base-0.4.0 · Data.Functor.Linear.Internal.Traversable
  • Traversable (Tuple5 a b c d)Defined in linear-base-0.4.0 · Data.Functor.Linear.Internal.Traversable
  • Traversable f => Traversable (M1 i c f)Defined in linear-base-0.4.0 · Data.Functor.Linear.Internal.Traversable
  • (Traversable f, Traversable g) => Traversable (f :.: g)Defined in linear-base-0.4.0 · Data.Functor.Linear.Internal.Traversable
valuegenericTraverse
  1. :: (Generic1 t, GTraversable (Rep1 t), Applicative f)
  2. => a %1 -> f b
  3. -> t a
  4. -> f (t b)
#

Implementation of traverse for types which derive (linear) Generic1.

### Performance note

At present, this function does not perform well for recursive types like lists; it will not specialize to either

### Example

data T
$(deriveGeneric1 ''T)

instance Traversable T where
  traverse = genericTraverse

Note that, contrary to many other classes in linear-base, we can't define `Traversable T` using deriving via, because the role of t, in the type of traverse, is nominal.

classclass GTraversable (t :: Type -> Type) where
#

This type class derives the definition of genericTraverse by induction on the generic representation of a type.

Instances15GTraversable, …