distributors
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.Monoidal

Description

 
Synopsis

Monoidal

type Monoidal (p :: Type -> Type -> Type) = (Profunctor p, forall x. Applicative (p x)) Source #

A lax Monoidal product Profunctor has unit oneP and product >*< lax monoidal structure morphisms. This is equivalent to the Profunctor also being Applicative.

Laws:

>>> let lunit = dimap (\((),a) -> a) (\a -> ((),a))
>>> let runit = dimap (\(a,()) -> a) (\a -> (a,()))
>>> let assoc = dimap (\(a,(b,c)) -> ((a,b),c)) (\((a,b),c) -> (a,(b,c)))
prop> oneP >*< p = lunit p
prop> p >*< oneP = runit p
prop> p >*< q >*< r = assoc ((p >*< q) >*< r)
prop> dimap (f >*< g) (h >*< i) (p >*< q) = dimap f h p >*< dimap g i q

oneP :: Monoidal p => p () () Source #

oneP is the unit of a Monoidal Profunctor.

(>*<) :: Monoidal p => p a b -> p c d -> p (a, c) (b, d) infixr 5 Source #

>*< is the product of a Monoidal Profunctor.

(>*) :: Monoidal p => p () c -> p a b -> p a b infixl 6 Source #

>* sequences actions, discarding the value of the first argument; analogous to *>, extending it to Monoidal.

prop> oneP >* p = p

(*<) :: Monoidal p => p a b -> p () c -> p a b infixl 6 Source #

*< sequences actions, discarding the value of the second argument; analogous to <*, extending it to Monoidal.

prop> p *< oneP = p

dimap2 Source #

Arguments

:: Monoidal p 
=> (s -> a)

first projection, e.g. fst

-> (s -> c)

second projection, e.g. snd

-> (b -> d -> t)

pairing function, e.g. (,)

-> p a b 
-> p c d 
-> p s t 

dimap2 is a curried, functionalized form of >*<, analogous to liftA2.

foreverP :: Monoidal p => p () c -> p a b Source #

foreverP repeats an action a countable infinity of times; analogous to forever, extending it to Monoidal.

ditraverse :: (Traversable t, Distributive t, Monoidal p) => p a b -> p (t a) (t b) Source #

Thanks to Fy on Monoidal Café Discord.

A Traversable & Distributive type is a homogeneous countable product. That means it is a static countable-length container, so unlike replicateP, ditraverse doesn't need an additional argument for number of repetitions.

Monoidal & Choice

pureP Source #

Arguments

:: (Monoidal p, Choice p) 
=> APrism a b () ()

bidirectional element

-> p a b 

Lift a single bidirectional element into a Monoidal & Choice structure. Bidirectionality is encoded by APrism. Singularity is encoded by the unit type (). Bidirectional elements can be generated from nilary constructors of algebraic datatypes using makeNestedPrisms, from terms of a type with an Eq instance using only, from nil elements using _Empty, or from any .-composition of Prisms terminating with a bidirectional element.

asEmpty :: (AsEmpty s, Monoidal p, Choice p) => p s s Source #

A Monoidal & Choice nil combinator.

(>:<) :: (Cons s t a b, Monoidal p, Choice p) => p a b -> p s t -> p s t infixr 5 Source #

A Monoidal & Choice cons combinator.

snocP :: (Snoc s t a b, Monoidal p, Choice p) => p s t -> p a b -> p s t Source #

A Monoidal & Choice snoc combinator.

replicateP Source #

Arguments

:: (Monoidal p, Choice p, AsEmpty s, Cons s s a a) 
=> Int

number of repetitions

-> p a a 
-> p s s 

replicateP is analogous to replicateM, for Monoidal & Choice Profunctors. When the number of repetitions is less than or equal to 0, it returns asEmpty.

onlyOne :: (Monoidal p, Choice p, IsList s) => p (Item s) (Item s) -> p s s Source #

Use when IsList with onlyOne Item.

Monoidal & Strong

idP :: (Monoidal p, Strong p) => p a a Source #

The unit map of a Monoidal & Strong Profunctor, the strength of oneP, which is natural, & whose product is the strength. It needn't be an identity; idP = Forget (const mempty).

prop> lmap f idP = rmap f idP
prop> first' p = p >*< idP
prop> second' p = idP >*< p

arrP :: (Monoidal p, Strong p) => (a -> b) -> p a b Source #

Lift a function into a Monoidal & Strong Profunctor, like arr.

prop> arrP f = rmap f idP = lmap f idP

Monoidal, Strong & Costrong

fixP :: (Monoidal p, Strong p, Costrong p) => p (a -> a) a Source #

The fixed point of a function, like fix, by Costrong's unfirst, like loop.

Monoidal, Choice & Strong

meander :: (Monoidal p, Choice p, Strong p) => ATraversal s t a b -> p a b -> p s t Source #

For any Monoidal, Choice & Strong Profunctor, meander is invertible and gives a default implementation for the wander method of Traversing, though Strong isn't needed for its definition, but for its invertibility property.

>>> :{
traversalP :: (forall p. (Monoidal p, Choice p, Strong p) => p a b -> p s t) -> Traversal s t a b
traversalP f = runStar . f . Star
:}
prop> traversalP . meander = id
prop> meander . traversalP = id

See Pickering, Gibbons & Wu, Profunctor Optics - Modular Data Accessors

traverseP :: (Traversable f, Monoidal p, Choice p, Strong p) => p a b -> p (f a) (f b) Source #

traverseP gives a default implementation for the traverse' method of Traversing.

foldP :: (Foldable f, Monoidal p, Choice p, Strong p, forall x. Contravariant (p x)) => p a b -> p (f a) (f b) Source #

foldP gives a contravariant, profunctorial Foldable method. However, a Profunctor which is also Contravariant in its last argument is constant over or phantom in its last argument.

prop> foldMap f = getConst . runStar (foldP (Star (Const . f)))

Deriving via Monoidally

newtype Monoidally (p :: Type -> Type -> Type) a b Source #

Monoidally derives instances of a Monoidal Profunctor, via meander for Traversing, & via arrP & >*< for Arrow, given Category.

newtype P a b = P ...
  deriving (Traversing, Arrow) via Monoidally P

Constructors

Monoidally 

Fields

Instances

Instances details
Category p => Category (Monoidally p :: Type -> Type -> Type) Source # 
Instance details

Defined in Data.Profunctor.Monoidal

Methods

id :: Monoidally p a a #

(.) :: Monoidally p b c -> Monoidally p a b -> Monoidally p a c #

(Category p, Monoidal p, Strong p) => Arrow (Monoidally p) Source # 
Instance details

Defined in Data.Profunctor.Monoidal

Methods

arr :: (b -> c) -> Monoidally p b c #

first :: Monoidally p b c -> Monoidally p (b, d) (c, d) #

second :: Monoidally p b c -> Monoidally p (d, b) (d, c) #

(***) :: Monoidally p b c -> Monoidally p b' c' -> Monoidally p (b, b') (c, c') #

(&&&) :: Monoidally p b c -> Monoidally p b c' -> Monoidally p b (c, c') #

(Category p, Monoidal p, Strong p, Choice p) => ArrowChoice (Monoidally p) Source # 
Instance details

Defined in Data.Profunctor.Monoidal

Methods

left :: Monoidally p b c -> Monoidally p (Either b d) (Either c d) #

right :: Monoidally p b c -> Monoidally p (Either d b) (Either d c) #

(+++) :: Monoidally p b c -> Monoidally p b' c' -> Monoidally p (Either b b') (Either c c') #

(|||) :: Monoidally p b d -> Monoidally p c d -> Monoidally p (Either b c) d #

(Category p, Monoidal p, Strong p, Costrong p) => ArrowLoop (Monoidally p) Source # 
Instance details

Defined in Data.Profunctor.Monoidal

Methods

loop :: Monoidally p (b, d) (c, d) -> Monoidally p b c #

(Category p, Monoidal p, Strong p, forall x y. DisjunctiveMonoid (p x y)) => ArrowPlus (Monoidally p) Source # 
Instance details

Defined in Data.Profunctor.Monoidal

Methods

(<+>) :: Monoidally p b c -> Monoidally p b c -> Monoidally p b c #

(Category p, Monoidal p, Strong p, forall x y. DisjunctiveMonoid (p x y)) => ArrowZero (Monoidally p) Source # 
Instance details

Defined in Data.Profunctor.Monoidal

Methods

zeroArrow :: Monoidally p b c #

Choice p => Choice (Monoidally p) Source # 
Instance details

Defined in Data.Profunctor.Monoidal

Methods

left' :: Monoidally p a b -> Monoidally p (Either a c) (Either b c) #

right' :: Monoidally p a b -> Monoidally p (Either c a) (Either c b) #

Cochoice p => Cochoice (Monoidally p) Source # 
Instance details

Defined in Data.Profunctor.Monoidal

Methods

unleft :: Monoidally p (Either a d) (Either b d) -> Monoidally p a b #

unright :: Monoidally p (Either d a) (Either d b) -> Monoidally p a b #

Costrong p => Costrong (Monoidally p) Source # 
Instance details

Defined in Data.Profunctor.Monoidal

Methods

unfirst :: Monoidally p (a, d) (b, d) -> Monoidally p a b #

unsecond :: Monoidally p (d, a) (d, b) -> Monoidally p a b #

Strong p => Strong (Monoidally p) Source # 
Instance details

Defined in Data.Profunctor.Monoidal

Methods

first' :: Monoidally p a b -> Monoidally p (a, c) (b, c) #

second' :: Monoidally p a b -> Monoidally p (c, a) (c, b) #

(Monoidal p, Choice p, Strong p) => Traversing (Monoidally p) Source # 
Instance details

Defined in Data.Profunctor.Monoidal

Methods

traverse' :: Traversable f => Monoidally p a b -> Monoidally p (f a) (f b) #

wander :: (forall (f :: Type -> Type). Applicative f => (a -> f b) -> s -> f t) -> Monoidally p a b -> Monoidally p s t #

Profunctor p => Profunctor (Monoidally p) Source # 
Instance details

Defined in Data.Profunctor.Monoidal

Methods

dimap :: (a -> b) -> (c -> d) -> Monoidally p b c -> Monoidally p a d #

lmap :: (a -> b) -> Monoidally p b c -> Monoidally p a c #

rmap :: (b -> c) -> Monoidally p a b -> Monoidally p a c #

(#.) :: forall a b c q. Coercible c b => q b c -> Monoidally p a b -> Monoidally p a c #

(.#) :: forall a b c q. Coercible b a => Monoidally p b c -> q a b -> Monoidally p a c #

Alternative (p a) => Alternative (Monoidally p a) Source # 
Instance details

Defined in Data.Profunctor.Monoidal

Methods

empty :: Monoidally p a a0 #

(<|>) :: Monoidally p a a0 -> Monoidally p a a0 -> Monoidally p a a0 #

some :: Monoidally p a a0 -> Monoidally p a [a0] #

many :: Monoidally p a a0 -> Monoidally p a [a0] #

Applicative (p a) => Applicative (Monoidally p a) Source # 
Instance details

Defined in Data.Profunctor.Monoidal

Methods

pure :: a0 -> Monoidally p a a0 #

(<*>) :: Monoidally p a (a0 -> b) -> Monoidally p a a0 -> Monoidally p a b #

liftA2 :: (a0 -> b -> c) -> Monoidally p a a0 -> Monoidally p a b -> Monoidally p a c #

(*>) :: Monoidally p a a0 -> Monoidally p a b -> Monoidally p a b #

(<*) :: Monoidally p a a0 -> Monoidally p a b -> Monoidally p a a0 #

Functor (p a) => Functor (Monoidally p a) Source # 
Instance details

Defined in Data.Profunctor.Monoidal

Methods

fmap :: (a0 -> b) -> Monoidally p a a0 -> Monoidally p a b #

(<$) :: a0 -> Monoidally p a b -> Monoidally p a a0 #

DisjunctiveMonoid (p a b) => DisjunctiveMonoid (Monoidally p a b) Source # 
Instance details

Defined in Data.Profunctor.Monoidal

Methods

falseP :: Monoidally p a b Source #

(>|<) :: Monoidally p a b -> Monoidally p a b -> Monoidally p a b Source #