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

Moduleselective-0.7.0.1Haskell2010

Control.Selective.Multi

This is a library for selective applicative functors, or just selective functors for short, an abstraction between applicative functors and monads, introduced in this paper: https://dl.acm.org/doi/10.1145/3341694.

This module defines multi-way selective functors, which are more efficient when selecting from a large number of options. They also fully subsume the Applicative type class because they allow to express the notion of independet effects.

This definition is inspired by the following construction by Daniel Peebles, with the main difference being the added Enumerable constraint: https://gist.github.com/copumpkin/d5bdbc7afda54ff04049b6bdbcffb67e

  • 11 types
  • 4 classes
  • 24 values

Generalised sum types

10 declarations
datadata Sigma (t :: Type -> Type) where
#

A generalised sum type where t stands for the type of constructor "tags". Each tag has a type parameter x which determines the type of the payload. A Sigma t value therefore contains a payload whose type is not visible externally but is revealed when pattern-matching on the tag.

See Two, eitherToSigma and sigmaToEither for an example.

Constructors

valueinject :: t x -> x -> Sigma t
#

An injection into a generalised sum. An alias for Sigma.

datadata Zero a
#

A data type defining no tags. Similar to Void but parameterised.

Instances1Enumerable
  • Enumerable ZeroDefined in selective-0.7.0.1 · Control.Selective.Multi
datadata One a b where
#

A data type with a single tag. This data type is commonly known as Refl, see Data.Type.Equality.

Constructors

Instances1Enumerable
  • Enumerable (One a)Defined in selective-0.7.0.1 · Control.Selective.Multi
datadata Many a b where
#

A potentially uncountable collection of tags for the same unit () payload.

Constructors

Instances1Enumerable
valuematchPure :: Sigma t -> (forall x. t x -> x -> a) -> a
#

Generalised pattern matching on a Sigma type using a Pi type to describe how to handle each case.

This is a specialisation of matchCases for f = Identity. We could also have also given it the following type:

matchPure :: Sigma t -> (t ~> Case Identity a) -> a

We chose to simplify it by inlining ~>, Case and Identity.

Selective functors

9 declarations
classclass Enumerable (t :: Type -> Type) where
#

A class of tags that can be enumerated.

A valid instance must list every tag in the resulting list exactly once.

Methods

Instances4Enumerable
  • Enumerable ZeroDefined in selective-0.7.0.1 · Control.Selective.Multi
  • Enum a => Enumerable (Many a)Defined in selective-0.7.0.1 · Control.Selective.Multi
  • Enumerable (One a)Defined in selective-0.7.0.1 · Control.Selective.Multi
  • Enumerable (Two a b)Defined in selective-0.7.0.1 · Control.Selective.Multi
classclass Applicative f => Selective (f :: Type -> Type) where
#

Multi-way selective functors. Given a computation that produces a value of a sum type, we can match it to the corresponding computation in a given product type.

For greater similarity with matchCases, we could have given the following type to match:

match :: f (Sigma t) -> (t ~> Case f a) -> f a

We chose to simplify it by inlining ~> and Case.

Methods

Instances2Selective
newtypenewtype Over m a
#

Static analysis of selective functors with over-approximation.

Constructors

Instances6Functor, Applicative, Selective, Eq, Ord, Show
  • Functor (Over m)Defined in selective-0.7.0.1 · Control.Selective.Multi
  • Monoid m => Applicative (Over m)Defined in selective-0.7.0.1 · Control.Selective.Multi
  • Monoid m => Selective (Over m)Defined in selective-0.7.0.1 · Control.Selective.Multi
  • Eq m => Eq (Over m a)Defined in selective-0.7.0.1 · Control.Selective.Multi
  • Ord m => Ord (Over m a)Defined in selective-0.7.0.1 · Control.Selective.Multi
  • Show m => Show (Over m a)Defined in selective-0.7.0.1 · Control.Selective.Multi
newtypenewtype Under m a
#

Static analysis of selective functors with under-approximation.

Constructors

Instances6Functor, Applicative, Selective, Eq, Ord, Show
  • Functor (Under m)Defined in selective-0.7.0.1 · Control.Selective.Multi
  • Monoid m => Applicative (Under m)Defined in selective-0.7.0.1 · Control.Selective.Multi
  • Monoid m => Selective (Under m)Defined in selective-0.7.0.1 · Control.Selective.Multi
  • Eq m => Eq (Under m a)Defined in selective-0.7.0.1 · Control.Selective.Multi
  • Ord m => Ord (Under m a)Defined in selective-0.7.0.1 · Control.Selective.Multi
  • Show m => Show (Under m a)Defined in selective-0.7.0.1 · Control.Selective.Multi
valueapS :: Selective f => f a -> f (a -> b) -> f b
#

Recover the application operator <*> from match.

valuebindS :: (Enum a, Selective f) => f a -> (a -> f b) -> f b
#

A restricted version of monadic bind.

Applicative functors

3 declarations

Monads

3 declarations
valuebind :: MonadS f => f a -> (a -> f b) -> f b
#

Monadic bind.

valuematchM :: Monad f => f (Sigma t) -> (forall x. t x -> f (x -> a)) -> f a
#

Every monad is a multi-way selective functor.

Generalised products and various combinators

14 declarations
typetype (~>) (t :: Type -> Type) (u :: Type -> Type) = forall x. t x -> u x
#

A generalised product type (Pi), which holds an appropriately tagged payload u x for every possible tag t x.

Note that this looks different than the standard formulation of Pi types. Maybe it's just all wrong!

See Two, pairToPi and piToPair for an example.

typetype Pi (t :: Type -> Type) = t ~> Identity
#

A product type where the payload has the type specified with the tag.

valueproject :: t a -> Pi t -> a
#

A projection from a generalised product.

valueidentity :: t x -> t x
#

A trivial product type that stores nothing and simply returns the given tag as the result.

valuecompose :: u ~> v -> t ~> u -> t ~> v
#

As it turns out, one can compose such generalised products. Why not: given a tag, get the payload of the first product and then pass it as input to the second. This feels too trivial to be useful but is still somewhat cute.

valueapply :: t ~> u -> Sigma t -> Sigma u
#

Update a generalised sum given a generalised product that takes care of all possible cases.

valuetoSigma :: a -> Sigma (One a)
#

Encode a value into a generalised sum type that has a single tag One.

valuefromSigma :: Sigma (One a) -> a
#

Decode a value from a generalised sum type that has a single tag One.

valuetoPi :: a -> Pi (One a)
#

Encode a value into a generalised product type that has a single tag One.

valuefromPi :: Pi (One a) -> a
#

Decode a value from a generalised product type that has a single tag One.

valuepairToPi :: (a, b) -> Pi (Two a b)
#

Encode (a, b) into a generalised product type.

valuepiToPair :: Pi (Two a b) -> (a, b)
#

Decode (a, b) from a generalised product type.

valuematchCases :: Functor f => Sigma t -> t ~> Case f a -> f a
#

Generalised pattern matching on a Sigma type using a Pi type to describe how to handle each case.