| Copyright | (C) 2026 - Eitan Chatav |
|---|---|
| License | BSD-style (see the file LICENSE) |
| Maintainer | Eitan Chatav <eitan.chatav@gmail.com> |
| Stability | provisional |
| Portability | non-portable |
| Safe Haskell | None |
| Language | Haskell2010 |
Data.Profunctor.Distributor
Contents
Description
Synopsis
- class (Monoidal p, CocartesianMonoidal p) => Distributor (p :: Type -> Type -> Type) where
- skipManyP :: Distributor p => p a b -> p () ()
- skipOptionalP :: Distributor p => p a b -> p () ()
- class (Distributor p, CocartesianChoice p, forall x. Alternative (p x)) => Alternator (p :: Type -> Type -> Type) where
Distributor
class (Monoidal p, CocartesianMonoidal 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, being both
Monoidal and CocartesianMonoidal.
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
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
skipManyP :: Distributor p => p a b -> p () () Source #
Zero or more, skipped: sequencing any number of actions forwards, & none backwards, like whitespace that a parser accepts & a printer omits.
prop> skipManyP p = dimap (const []) (const ()) (manyP p)
>>>[r | (r, "") <- readP_to_S (runJoker (skipManyP (Joker (char ' ')) *< Joker (char 'x'))) " x"][()]
skipOptionalP :: Distributor p => p a b -> p () () Source #
Zero or one, skipped: sequencing one action or none forwards, & none backwards, like a trailing separator that a parser accepts & a printer omits.
>>>[r | (r, "") <- readP_to_S (runJoker (Joker (char 'x') *< skipOptionalP (Joker (char ',')))) "x,"]"x"
Alternator
class (Distributor p, CocartesianChoice p, forall x. Alternative (p x)) => Alternator (p :: Type -> Type -> Type) where Source #
The Alternator class co-extends CocartesianChoice and Distributor,
as well as Alternative, adding methods someP & optionP,
with these laws relating them.
prop> manyP p = optionP _Empty (someP p) prop> optionalP p = optionP _Nothing (_Just >? p) prop> someP p = p >:< manyP p
These mirror the laws of a KleeneStarAlgebra, starK x = optK (plusK x)
& plusK x = x <> starK x, unfolding someP on the left.
Its unfolding on the right, someP p = snocP (manyP p) p,
mirroring plusK x = starK x <> x, isn't a law though,
since a greedy or predictive parser's manyP consumes every p,
leaving none for the last; only a backtracking one finds it.
Its Alternative structure agrees with its Alternator structure,
many & some repeating an input for manyP & someP.
prop> empty = falseP prop> (<|>) = (>|<) prop> many = lmap repeat . manyP prop> some = lmap repeat . someP
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
someP :: p a b -> p [a] [b] Source #
One or more.
Arguments
| :: APrism a b () () | default |
| -> p a b | |
| -> p a b |
One or zero-with-default.
manyTillP :: p a b -> p [a] [b] -> p [a] [b] Source #
Zero or more, then the rest, p* q, trying the rest first,
like manyTill of parsec,
the least solution of its unfolding.
prop> manyTillP p q = q >|< p >:< manyTillP p q prop> manyTillP falseP q = q
It mirrors a KleeneStarAlgebra's starK x <> y, so that
a static Alternator, which sequences no input, may define it
without recursing, as an input-consuming one, like a parser
or printer, can't, since only consuming finds where q begins.
It recognizes what manyP p does with q = ,
but trying fewer asEmptyps first, rather than more.
quantP :: Int -> Maybe Int -> p a b -> p [a] [b] Source #
A Kleene quantifier, between some numbers of times, inclusive,
at least a lower bound, & at most an upper bound, if there's one,
like a regular expression's {lo,hi}, or {lo,}. It takes the mandatory times first, then,
unbounded, someP or manyP, or, bounded, the optional times,
trying more first. It never matches where lo > hi,
nor where hi is negative, its counts being those n >= 0 within
its bounds.
It mirrors a KleeneStarAlgebra's quantK, so that a static
Alternator may keep its counts, rather than unfolding them.
prop> quantP 0 Nothing = manyP prop> quantP 1 Nothing = someP prop> quantP n (Just n) = replicateP n, for @n >= 0@