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

Control.Functor.Linear

The control functor hierarchy

The functors in this module are called control functors, which are different from the data functors in Data.Functor.Linear.

This distinction and the use-cases of each group of functors is explained in this blog post.

  • 5 types
  • 5 classes
  • 35 values

Control functor hierarchy

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

Control linear functors. The functor of type f a holds only one value of type a and represents a computation producing an a with an effect. All control functors are data functors, but not all data functors are control functors.

Methods

  • fmap :: (a %1 -> b) %1 -> f a %1 -> f b

    Map a linear function g over a control functor f a. Note that g is used linearly over the single a in f a.

Instances25Functor, …
  • Functor IdentityDefined in linear-base-0.4.0 · Control.Functor.Linear.Internal.Class
  • Functor Par1Defined in linear-base-0.4.0 · Control.Functor.Linear.Internal.Class
  • 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 (Tuple2 a)Defined in linear-base-0.4.0 · Control.Functor.Linear.Internal.Class
  • 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 (Of a)Defined in linear-base-0.4.0 · Streaming.Linear.Internal.Type
  • Functor (Tuple3 a b)Defined in linear-base-0.4.0 · Control.Functor.Linear.Internal.Class
  • 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 (ReaderT r m)Defined in linear-base-0.4.0 · Control.Functor.Linear.Internal.Reader · orphan
  • Functor m => Functor (StateT s m)Defined in linear-base-0.4.0 · Control.Functor.Linear.Internal.State · orphan
  • (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 · Control.Functor.Linear.Internal.Class
  • Functor (Tuple4 a b c)Defined in linear-base-0.4.0 · Control.Functor.Linear.Internal.Class
  • (Functor f, Functor g) => Functor (Sum f g)Defined in linear-base-0.4.0 · Control.Functor.Linear.Internal.Class
  • (Functor l, Functor r) => Functor (l :+: r)Defined in linear-base-0.4.0 · Control.Functor.Linear.Internal.Class
  • (Functor l, Functor r, EitherNoPar1 b1 b2 l r) => Functor (l :*: r)Defined in linear-base-0.4.0 · Control.Functor.Linear.Internal.Class
  • Functor (FUN 'One a)Defined in linear-base-0.4.0 · Control.Functor.Linear.Internal.Class
  • Functor (Tuple5 a b c d)Defined in linear-base-0.4.0 · Control.Functor.Linear.Internal.Class
  • Functor f => Functor (M1 j c f)Defined in linear-base-0.4.0 · Control.Functor.Linear.Internal.Class
  • (Functor f, Functor g) => Functor (Compose f g)Defined in linear-base-0.4.0 · Control.Functor.Linear.Internal.Class
  • (Functor f, Functor g) => Functor (f :.: g)Defined in linear-base-0.4.0 · Control.Functor.Linear.Internal.Class
value(<$>) :: Functor f => (a %1 -> b) %1 -> f a %1 -> f b
#
value(<$) :: (Functor f, Consumable b) => a %1 -> f b %1 -> f a
#

Linearly typed replacement for the standard (Prelude.<$) function.

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

Discard a consumable value stored in a control functor.

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

Control linear applicative functors. These represent effectful computations that could produce continuations that can be applied with <*>.

Methods

  • pure :: a %1 -> f a

    Inject (and consume) a value into an applicative control functor.

  • (<*>) :: f (a %1 -> b) %1 -> f a %1 -> f binfixl 4

    Apply the linear function in a control applicative functor to the value of type a in another functor. This is essentialy composing two effectful computations, one that produces a function f :: a %1-> b and one that produces a value of type a into a single effectful computation that produces a value of type b.

  • liftA2 :: (a %1 -> b %1 -> c) %1 -> f a %1 -> f b %1 -> f c

    liftA2 g consumes g linearly as it lifts it over two functors: liftA2 g :: f a %1-> f b %1-> f c.

Instances13Applicative, …
  • Applicative IdentityDefined in linear-base-0.4.0 · Control.Functor.Linear.Internal.Class
  • Applicative IODefined in linear-base-0.4.0 · System.IO.Linear
  • Applicative RIODefined in linear-base-0.4.0 · System.IO.Resource.Linear.Internal
  • Applicative (StateR s)Defined in linear-base-0.4.0 · Data.Functor.Linear.Internal.Traversable
  • Applicative f => Applicative (Yoneda f)Defined in linear-base-0.4.0 · Control.Functor.Linear.Internal.Kan
  • Monoid a => Applicative (Tuple2 a)Defined in linear-base-0.4.0 · Control.Functor.Linear.Internal.Class
  • Applicative m => Applicative (ReaderT r m)Defined in linear-base-0.4.0 · Control.Functor.Linear.Internal.Reader · orphan
  • Monad m => Applicative (StateT s m)Defined in linear-base-0.4.0 · Control.Functor.Linear.Internal.State
  • (Applicative m, Dupable r) => Applicative (ReaderT r m)Defined in linear-base-0.4.0 · Control.Functor.Linear.Internal.Reader
  • (Functor g, g ~ h) => Applicative (Curried g h)Defined in linear-base-0.4.0 · Control.Functor.Linear.Internal.Kan
  • (Functor m, Functor f) => Applicative (Stream f m)Defined in linear-base-0.4.0 · Streaming.Linear.Internal.Type
  • (Monoid a, Monoid b) => Applicative (Tuple3 a b)Defined in linear-base-0.4.0 · Control.Functor.Linear.Internal.Class
  • (Monoid a, Monoid b, Monoid c) => Applicative (Tuple4 a b c)Defined in linear-base-0.4.0 · Control.Functor.Linear.Internal.Class
classclass Applicative m => Monad (m :: Type -> Type) where
#

Control linear monads. A linear monad is one in which you sequence linear functions in a context, i.e., you sequence functions of the form a %1-> m b.

Methods

  • (>>=) :: m a %1 -> (a %1 -> m b) %1 -> m binfixl 1

    x >>= g applies a linear function g linearly (i.e., using it exactly once) on the value of type a inside the value of type m a

  • (>>) :: m () %1 -> m a %1 -> m ainfixl 1
Instances8Monad, …
  • Monad IdentityDefined in linear-base-0.4.0 · Control.Functor.Linear.Internal.Class
  • Monad IODefined in linear-base-0.4.0 · System.IO.Linear
  • Monad RIODefined in linear-base-0.4.0 · System.IO.Resource.Linear.Internal
  • Monoid a => Monad (Tuple2 a)Defined in linear-base-0.4.0 · Control.Functor.Linear.Internal.Class
  • Monad m => Monad (StateT s m)Defined in linear-base-0.4.0 · Control.Functor.Linear.Internal.State
  • Monad m => Monad (ReaderT r m)Defined in linear-base-0.4.0 · Control.Functor.Linear.Internal.Reader · orphan
  • (Functor m, Functor f) => Monad (Stream f m)Defined in linear-base-0.4.0 · Streaming.Linear.Internal.Type
  • (Monad m, Dupable r) => Monad (ReaderT r m)Defined in linear-base-0.4.0 · Control.Functor.Linear.Internal.Reader
valuejoin :: Monad m => m (m a) %1 -> m a
#

Given an effect-producing computation that produces an effect-producing computation that produces an a, simplify it to an effect-producing computation that produces an a.

valueap :: Monad m => m (a %1 -> b) %1 -> m a %1 -> m b
#

Use this operator to define Applicative instances in terms of Monad instances.

valuefoldM :: Monad m => (b %1 -> a %1 -> m b) -> b %1 -> [a] %1 -> m b
#

Fold from left to right with a linear monad. This is a linear version of NonLinear.foldM.

newtypenewtype Data (f :: Type -> Type) a
#

This is a newtype for deriving Data.XXX classes from Control.XXX classes.

Constructors

Instances2Functor, Applicative

Monad transformers

0 declarations

ReaderT monad transformer

See here to learn about the basics of reader monads. To know about the standard reader monad functions, see the documentation of the standard reader monad here.

newtypenewtype ReaderT r (m :: Type -> Type) a
#

A linear reader monad transformer. This reader monad requires that use of the read-only state is explict.

The monad instance requires that r be Dupable. This means that you should use the linear reader monad just like the non-linear monad, except that the type system ensures that you explicity use or discard the read-only state (with the Consumable instance).

Constructors

Instances6MonadTrans, Monad, Functor, Applicative
valuerunReaderT :: ReaderT r m a %1 -> r %1 -> m a
#

Provide an intial read-only state and run the monadic computation in a reader monad transformer

StateT monad

This is a linear version of the standard state monad. The linear arrows ensure that the state is threaded linearly through functions of the form a %1-> StateT s m a. That is, when sequencing f :: a %1-> StateT s m b and g :: b %1-> StateT s m c, the type system enforces that state produced by $f$ is fed into g.

For this reason, there is only one way to define (>>=):

instance Monad m => Applicative (StateT s m) where
StateT mx >>= f = StateT $ \s -> do
  (x, s') <- mx s
  runStateT (f x) s'

To see examples and learn about all the standard state monad functions, see here. To learn the basics of the state monad, see here.

valueevalState :: Consumable s => State s a %1 -> s %1 -> a
#

Use with care! This consumes the final state, so might be costly at runtime.

newtypenewtype StateT s (m :: Type -> Type) a
#

A (strict) linear state monad transformer.

Constructors

Instances6MonadTrans, Monad, Functor, Applicative
  • MonadTrans (StateT s)Defined in linear-base-0.4.0 · Control.Functor.Linear.Internal.State
  • Monad m => Applicative (StateT s m)Defined in linear-base-0.4.0 · Control.Functor.Linear.Internal.State
  • Functor m => Functor (StateT s m)Defined in linear-base-0.4.0 · Control.Functor.Linear.Internal.State
  • Monad m => Monad (StateT s m)Defined in linear-base-0.4.0 · Control.Functor.Linear.Internal.State
  • Functor m => Functor (StateT s m)Defined in linear-base-0.4.0 · Control.Functor.Linear.Internal.State
  • Monad m => Applicative (StateT s m)Defined in linear-base-0.4.0 · Control.Functor.Linear.Internal.State
classclass (forall (m :: Type -> Type). Monad m => Monad (t m)) => MonadTrans (t :: (Type -> Type) -> Type -> Type) where
#

Methods

Instances4MonadTrans
  • Functor f => MonadTrans (Stream f)Defined in linear-base-0.4.0 · Streaming.Linear.Internal.Type
  • MonadTrans (StateT s)Defined in linear-base-0.4.0 · Control.Functor.Linear.Internal.State
  • MonadTrans (ReaderT r)Defined in linear-base-0.4.0 · Control.Functor.Linear.Internal.Reader · orphan
  • Dupable r => MonadTrans (ReaderT r)Defined in linear-base-0.4.0 · Control.Functor.Linear.Internal.Reader