distributors-0.6.0.0: Unifying Parsers, Printers & Grammars
Copyright(C) 2026 - Eitan Chatav
LicenseBSD-style (see the file LICENSE)
MaintainerEitan Chatav <eitan.chatav@gmail.com>
Stabilityprovisional
Portabilitynon-portable
Safe HaskellNone
LanguageHaskell2010

Data.Profunctor.Distributor

Description

 
Synopsis

Distributor

class Monoidal p => Distributor (p :: Type -> Type -> Type) where Source #

A Distributor, or lax distributive profunctor, respects distributive category structure, that is nilary and binary products and coproducts, (), (,), Void and Either. It has zero zeroP and sum >+< lax monoidal structure morphisms.

In addition to the product laws for Monoidal, we have sum laws for Distributor.

Laws:

>>> let lunit = dimap (either absurd id) Right
>>> let runit = dimap (either id absurd) Left
>>> :{
let assoc = dimap
      (either (Left . Left) (either (Left . Right) Right))
      (either (either Left (Right . Left)) (Right . Right))
:}
zeroP >+< p = lunit p
p >+< zeroP = runit p
p >+< q >+< r = assoc ((p >+< q) >+< r)
dimap (f >+< g) (h >+< i) (p >+< q) = dimap f h p >+< dimap g i q

Distributor additionally has methods manyP & optionalP, distributing an action over [] and Maybe datatypes, which generalize to homogeneously distributing an action over Homogeneous sum-of-products datatypes.

Minimal complete definition

Nothing

Methods

zeroP :: p Void Void Source #

The zero structure morphism of a Distributor.

zeroP has a default for Alternators.

zeroP = empty

default zeroP :: Alternator p => p Void Void Source #

(>+<) :: p a b -> p c d -> p (Either a c) (Either b d) infixr 3 Source #

The sum structure morphism of a Distributor.

>+< has a default for Alternators.

x >+< y = alternate (Left x) <|> alternate (Right y)

default (>+<) :: Alternator p => p a b -> p c d -> p (Either a c) (Either b d) Source #

optionalP :: p a b -> p (Maybe a) (Maybe b) Source #

One or none.

manyP :: p a b -> p [a] [b] Source #

Zero or more.

Instances

Instances details
(ArrowZero p, ArrowChoice p) => Distributor (WrappedArrow p) Source # 
Instance details

Defined in Data.Profunctor.Distributor

Monad m => Distributor (Kleisli m) Source # 
Instance details

Defined in Data.Profunctor.Distributor

Methods

zeroP :: Kleisli m Void Void Source #

(>+<) :: Kleisli m a b -> Kleisli m c d -> Kleisli m (Either a c) (Either b d) Source #

optionalP :: Kleisli m a b -> Kleisli m (Maybe a) (Maybe b) Source #

manyP :: Kleisli m a b -> Kleisli m [a] [b] Source #

KleeneStarAlgebra k => Distributor (Grammor k) Source # 
Instance details

Defined in Data.Profunctor.Grammar

Methods

zeroP :: Grammor k Void Void Source #

(>+<) :: Grammor k a b -> Grammor k c d -> Grammor k (Either a c) (Either b d) Source #

optionalP :: Grammor k a b -> Grammor k (Maybe a) (Maybe b) Source #

manyP :: Grammor k a b -> Grammor k [a] [b] Source #

Categorized (Item s) => Distributor (Parsector s) Source # 
Instance details

Defined in Data.Profunctor.Grammar.Parsector

Methods

zeroP :: Parsector s Void Void Source #

(>+<) :: Parsector s a b -> Parsector s c d -> Parsector s (Either a c) (Either b d) Source #

optionalP :: Parsector s a b -> Parsector s (Maybe a) (Maybe b) Source #

manyP :: Parsector s a b -> Parsector s [a] [b] Source #

Distributor p => Distributor (Coyoneda p) Source # 
Instance details

Defined in Data.Profunctor.Distributor

Methods

zeroP :: Coyoneda p Void Void Source #

(>+<) :: Coyoneda p a b -> Coyoneda p c d -> Coyoneda p (Either a c) (Either b d) Source #

optionalP :: Coyoneda p a b -> Coyoneda p (Maybe a) (Maybe b) Source #

manyP :: Coyoneda p a b -> Coyoneda p [a] [b] Source #

Distributor p => Distributor (Yoneda p) Source # 
Instance details

Defined in Data.Profunctor.Distributor

Methods

zeroP :: Yoneda p Void Void Source #

(>+<) :: Yoneda p a b -> Yoneda p c d -> Yoneda p (Either a c) (Either b d) Source #

optionalP :: Yoneda p a b -> Yoneda p (Maybe a) (Maybe b) Source #

manyP :: Yoneda p a b -> Yoneda p [a] [b] Source #

Distributor (Binocular a b) Source # 
Instance details

Defined in Control.Lens.Bifocal

Methods

zeroP :: Binocular a b Void Void Source #

(>+<) :: Binocular a b a0 b0 -> Binocular a b c d -> Binocular a b (Either a0 c) (Either b0 d) Source #

optionalP :: Binocular a b a0 b0 -> Binocular a b (Maybe a0) (Maybe b0) Source #

manyP :: Binocular a b a0 b0 -> Binocular a b [a0] [b0] Source #

Distributor (Dioptrice a b) Source # 
Instance details

Defined in Control.Lens.Diopter

Methods

zeroP :: Dioptrice a b Void Void Source #

(>+<) :: Dioptrice a b a0 b0 -> Dioptrice a b c d -> Dioptrice a b (Either a0 c) (Either b0 d) Source #

optionalP :: Dioptrice a b a0 b0 -> Dioptrice a b (Maybe a0) (Maybe b0) Source #

manyP :: Dioptrice a b a0 b0 -> Dioptrice a b [a0] [b0] Source #

(Alternative m, Monad m) => Distributor (Parsor s m) Source # 
Instance details

Defined in Data.Profunctor.Grammar

Methods

zeroP :: Parsor s m Void Void Source #

(>+<) :: Parsor s m a b -> Parsor s m c d -> Parsor s m (Either a c) (Either b d) Source #

optionalP :: Parsor s m a b -> Parsor s m (Maybe a) (Maybe b) Source #

manyP :: Parsor s m a b -> Parsor s m [a] [b] Source #

Applicative f => Distributor (Printor s f) Source # 
Instance details

Defined in Data.Profunctor.Grammar

Methods

zeroP :: Printor s f Void Void Source #

(>+<) :: Printor s f a b -> Printor s f c d -> Printor s f (Either a c) (Either b d) Source #

optionalP :: Printor s f a b -> Printor s f (Maybe a) (Maybe b) Source #

manyP :: Printor s f a b -> Printor s f [a] [b] Source #

(Distributor p, Applicative f) => Distributor (WrappedPafb f p) Source # 
Instance details

Defined in Data.Profunctor.Distributor

Methods

zeroP :: WrappedPafb f p Void Void Source #

(>+<) :: WrappedPafb f p a b -> WrappedPafb f p c d -> WrappedPafb f p (Either a c) (Either b d) Source #

optionalP :: WrappedPafb f p a b -> WrappedPafb f p (Maybe a) (Maybe b) Source #

manyP :: WrappedPafb f p a b -> WrappedPafb f p [a] [b] Source #

Adjunction f u => Distributor (Costar f) Source # 
Instance details

Defined in Data.Profunctor.Distributor

Methods

zeroP :: Costar f Void Void Source #

(>+<) :: Costar f a b -> Costar f c d -> Costar f (Either a c) (Either b d) Source #

optionalP :: Costar f a b -> Costar f (Maybe a) (Maybe b) Source #

manyP :: Costar f a b -> Costar f [a] [b] Source #

Monoid s => Distributor (Forget s :: Type -> Type -> Type) Source # 
Instance details

Defined in Data.Profunctor.Distributor

Methods

zeroP :: Forget s Void Void Source #

(>+<) :: Forget s a b -> Forget s c d -> Forget s (Either a c) (Either b d) Source #

optionalP :: Forget s a b -> Forget s (Maybe a) (Maybe b) Source #

manyP :: Forget s a b -> Forget s [a] [b] Source #

Applicative f => Distributor (Star f) Source # 
Instance details

Defined in Data.Profunctor.Distributor

Methods

zeroP :: Star f Void Void Source #

(>+<) :: Star f a b -> Star f c d -> Star f (Either a c) (Either b d) Source #

optionalP :: Star f a b -> Star f (Maybe a) (Maybe b) Source #

manyP :: Star f a b -> Star f [a] [b] Source #

Distributor (->) Source # 
Instance details

Defined in Data.Profunctor.Distributor

Methods

zeroP :: Void -> Void Source #

(>+<) :: (a -> b) -> (c -> d) -> Either a c -> Either b d Source #

optionalP :: (a -> b) -> Maybe a -> Maybe b Source #

manyP :: (a -> b) -> [a] -> [b] Source #

Decidable f => Distributor (Clown f :: Type -> Type -> Type) Source # 
Instance details

Defined in Data.Profunctor.Distributor

Methods

zeroP :: Clown f Void Void Source #

(>+<) :: Clown f a b -> Clown f c d -> Clown f (Either a c) (Either b d) Source #

optionalP :: Clown f a b -> Clown f (Maybe a) (Maybe b) Source #

manyP :: Clown f a b -> Clown f [a] [b] Source #

Alternative f => Distributor (Joker f :: Type -> Type -> Type) Source # 
Instance details

Defined in Data.Profunctor.Distributor

Methods

zeroP :: Joker f Void Void Source #

(>+<) :: Joker f a b -> Joker f c d -> Joker f (Either a c) (Either b d) Source #

optionalP :: Joker f a b -> Joker f (Maybe a) (Maybe b) Source #

manyP :: Joker f a b -> Joker f [a] [b] Source #

(ArrowZero p, ArrowChoice p) => Distributor (WrappedArrow p) Source # 
Instance details

Defined in Data.Profunctor.Distributor

(Distributor p, Distributor q) => Distributor (Product p q) Source # 
Instance details

Defined in Data.Profunctor.Distributor

Methods

zeroP :: Product p q Void Void Source #

(>+<) :: Product p q a b -> Product p q c d -> Product p q (Either a c) (Either b d) Source #

optionalP :: Product p q a b -> Product p q (Maybe a) (Maybe b) Source #

manyP :: Product p q a b -> Product p q [a] [b] Source #

(Applicative f, Distributor p) => Distributor (Cayley f p) Source # 
Instance details

Defined in Data.Profunctor.Distributor

Methods

zeroP :: Cayley f p Void Void Source #

(>+<) :: Cayley f p a b -> Cayley f p c d -> Cayley f p (Either a c) (Either b d) Source #

optionalP :: Cayley f p a b -> Cayley f p (Maybe a) (Maybe b) Source #

manyP :: Cayley f p a b -> Cayley f p [a] [b] Source #

(Distributor p, Distributor q) => Distributor (Procompose p q) Source # 
Instance details

Defined in Data.Profunctor.Distributor

Methods

zeroP :: Procompose p q Void Void Source #

(>+<) :: Procompose p q a b -> Procompose p q c d -> Procompose p q (Either a c) (Either b d) Source #

optionalP :: Procompose p q a b -> Procompose p q (Maybe a) (Maybe b) Source #

manyP :: Procompose p q a b -> Procompose p q [a] [b] Source #

dialt :: Distributor p => (s -> Either a c) -> (b -> t) -> (d -> t) -> p a b -> p c d -> p s t Source #

dialt is a functionalized form of >+<.

Alternator

class (Choice p, Distributor p, forall x. Alternative (p x)) => Alternator (p :: Type -> Type -> Type) where Source #

The Alternator class co-extends Choice and Distributor, as well as Alternative, adding the alternate method, which is a lax monoidal structure morphism on sums, and methods someP & optionP, with these these laws relating them.

left' = alternate . Left
right' = alternate . Right
zeroP = empty
x >+< y = alternate (Left x) <|> alternate (Right y)
manyP p = optionP _Empty (someP p)
optionalP p = optionP _Nothing (_Just >? p)
someP p = p >:< manyP p

For the case of Functors, the analog of alternate can be defined without any other constraint, but the case of Profunctors turns out to be slighly more complex, necessitating Alternator.

>>> :{
alternateF :: Functor f => Either (f a) (f b) -> f (Either a b)
alternateF = either (fmap Left) (fmap Right)
:}

Not all Distributors are Alternators, in particular (->) is a Distributor but cannot be Alternative, because there is no general polymorphic function empty :: a -> b, so (->) isn't an Alternator.

Minimal complete definition

Nothing

Methods

alternate :: Either (p a b) (p c d) -> p (Either a c) (Either b d) Source #

The structure morphism for an Alternator, alternate has a default for Choice & Cochoice partial distributors.

default alternate :: Cochoice p => Either (p a b) (p c d) -> p (Either a c) (Either b d) Source #

someP :: p a b -> p [a] [b] Source #

One or more.

optionP Source #

Arguments

:: APrism a b () ()

default

-> p a b 
-> p a b 

One or zero-with-default.

Instances

Instances details
KleeneStarAlgebra k => Alternator (Grammor k) Source # 
Instance details

Defined in Data.Profunctor.Grammar

Methods

alternate :: Either (Grammor k a b) (Grammor k c d) -> Grammor k (Either a c) (Either b d) Source #

someP :: Grammor k a b -> Grammor k [a] [b] Source #

optionP :: APrism a b () () -> Grammor k a b -> Grammor k a b Source #

Categorized (Item s) => Alternator (Parsector s) Source # 
Instance details

Defined in Data.Profunctor.Grammar.Parsector

Methods

alternate :: Either (Parsector s a b) (Parsector s c d) -> Parsector s (Either a c) (Either b d) Source #

someP :: Parsector s a b -> Parsector s [a] [b] Source #

optionP :: APrism a b () () -> Parsector s a b -> Parsector s a b Source #

Alternator p => Alternator (Coyoneda p) Source # 
Instance details

Defined in Data.Profunctor.Distributor

Methods

alternate :: Either (Coyoneda p a b) (Coyoneda p c d) -> Coyoneda p (Either a c) (Either b d) Source #

someP :: Coyoneda p a b -> Coyoneda p [a] [b] Source #

optionP :: APrism a b () () -> Coyoneda p a b -> Coyoneda p a b Source #

Alternator p => Alternator (Yoneda p) Source # 
Instance details

Defined in Data.Profunctor.Distributor

Methods

alternate :: Either (Yoneda p a b) (Yoneda p c d) -> Yoneda p (Either a c) (Either b d) Source #

someP :: Yoneda p a b -> Yoneda p [a] [b] Source #

optionP :: APrism a b () () -> Yoneda p a b -> Yoneda p a b Source #

Alternator (Binocular a b) Source # 
Instance details

Defined in Control.Lens.Bifocal

Methods

alternate :: Either (Binocular a b a0 b0) (Binocular a b c d) -> Binocular a b (Either a0 c) (Either b0 d) Source #

someP :: Binocular a b a0 b0 -> Binocular a b [a0] [b0] Source #

optionP :: APrism a0 b0 () () -> Binocular a b a0 b0 -> Binocular a b a0 b0 Source #

(Alternative m, Monad m) => Alternator (Parsor s m) Source # 
Instance details

Defined in Data.Profunctor.Grammar

Methods

alternate :: Either (Parsor s m a b) (Parsor s m c d) -> Parsor s m (Either a c) (Either b d) Source #

someP :: Parsor s m a b -> Parsor s m [a] [b] Source #

optionP :: APrism a b () () -> Parsor s m a b -> Parsor s m a b Source #

Alternative f => Alternator (Printor s f) Source # 
Instance details

Defined in Data.Profunctor.Grammar

Methods

alternate :: Either (Printor s f a b) (Printor s f c d) -> Printor s f (Either a c) (Either b d) Source #

someP :: Printor s f a b -> Printor s f [a] [b] Source #

optionP :: APrism a b () () -> Printor s f a b -> Printor s f a b Source #

(Alternator p, Applicative f) => Alternator (WrappedPafb f p) Source # 
Instance details

Defined in Data.Profunctor.Distributor

Methods

alternate :: Either (WrappedPafb f p a b) (WrappedPafb f p c d) -> WrappedPafb f p (Either a c) (Either b d) Source #

someP :: WrappedPafb f p a b -> WrappedPafb f p [a] [b] Source #

optionP :: APrism a b () () -> WrappedPafb f p a b -> WrappedPafb f p a b Source #

Alternative f => Alternator (Joker f :: Type -> Type -> Type) Source # 
Instance details

Defined in Data.Profunctor.Distributor

Methods

alternate :: Either (Joker f a b) (Joker f c d) -> Joker f (Either a c) (Either b d) Source #

someP :: Joker f a b -> Joker f [a] [b] Source #

optionP :: APrism a b () () -> Joker f a b -> Joker f a b Source #

choice :: (Foldable f, Alternative p) => f (p a) -> p a Source #

Combines all Alternative choices in the specified list.