proarrow
Safe HaskellNone
LanguageGHC2024

Proarrow.Optic.Prism

Description

The prism: the optic for the coproduct, with legs

Prism s t a b = (b ~> t, s ~> (t || a))

witnessed by Rep/Corep (Coproduct t) (PrismFl / matchingP). A prism reviews and matches, sitting below Review, AffineTraversal and MonoidalTraversal in the lattice. Build with prism, eliminate to the two legs with withPrism via the generic ExOptic carrier. toOpLens/fromOpLens witness the equivalence with the op-lens encoding. This module also hosts affineTraversal, the lens-then-prism builder for affine traversals.

Synopsis

Documentation

class (AffineTravFl p q, GetterFl q p, MonTravFl p q) => PrismFl (p :: k +-> k) (q :: k +-> k) where Source Github #

The prism flavor. The GetterFl q p superclass says a reversed prism views its build leg (getP on the swapped pair): re prism is a getter.

Methods

matchingP :: forall (s :: k) (a :: k) (b :: k) (t :: k). HasBinaryCoproducts k => p s a -> q b t -> s ~> (t || a) Source Github #

Like affineMatch, but asking only for binary coproducts. Prism witnesses never need more, so prisms stay usable in categories without products.

Instances

Instances details
CategoryOf k => PrismFl (Id :: k -> k -> Type) (Id :: k -> k -> Type) Source Github # 
Instance details

Defined in Proarrow.Optic.Prism

Methods

matchingP :: forall (s :: k) (a :: k) (b :: k) (t :: k). HasBinaryCoproducts k => Id s a -> Id b t -> s ~> (t || a) Source Github #

(CopyDiscard k, HasCoproducts k, Ob t) => PrismFl (Rep (Coproduct t) :: k -> k -> Type) (Corep (Coproduct t) :: k -> k -> Type) Source Github # 
Instance details

Defined in Proarrow.Optic.Prism

Methods

matchingP :: forall (s :: k) (a :: k) (b :: k) (t0 :: k). HasBinaryCoproducts k => Rep (Coproduct t) s a -> Corep (Coproduct t) b t0 -> s ~> (t0 || a) Source Github #

(PrismFl f g, PrismFl f' g') => PrismFl (f :.: f' :: i -> i -> Type) (g' :.: g :: i -> i -> Type) Source Github # 
Instance details

Defined in Proarrow.Optic.Prism

Methods

matchingP :: forall (s :: i) (a :: i) (b :: i) (t :: i). HasBinaryCoproducts i => (f :.: f') s a -> (g' :.: g) b t -> s ~> (t || a) Source Github #

type Prism (s :: k) (t :: k) (a :: k) (b :: k) = Optic (Prostrong (PrismFl :: (k +-> k) -> (k +-> k) -> Constraint)) s t a b Source Github #

type Prism' (s :: k) (a :: k) = Prism s s a a Source Github #

prism :: forall {k} (s :: k) (t :: k) (a :: k) (b :: k). (CopyDiscard k, HasCoproducts k, Ob a) => (b ~> t) -> (s ~> (t || a)) -> Prism s t a b Source Github #

affineTraversal :: forall {k} (s :: k) (t :: k) (x :: k) (y :: k) (a :: k) (b :: k). CategoryOf k => Lens s t x y -> Prism x y a b -> AffineTraversal s t a b Source Github #

Build an AffineTraversal by composing a Lens with a Prism: focus a field with the lens, then match a case of that field with the prism. There is no from-legs builder for a bare affine traversal (its witness only ever arises by composition), so this is the way to make one. It is convert (l % p).

withPrism :: forall {k} (c :: (k -> k -> Type) -> Constraint) (s :: k) (t :: k) (a :: k) (b :: k) r. (HasBinaryCoproducts k, (Ob a, Ob b) => c (ExOptic (PrismFl :: (k +-> k) -> (k +-> k) -> Constraint) a b)) => Optic c s t a b -> ((b ~> t) -> (s ~> (t || a)) -> r) -> r Source Github #

Eliminate any optic that is at least an iso and at most a prism to its two legs, in either encoding: run it at its witness pair (ExOptic PrismFl, via withLegs) and read the legs off with matchingP and getP on the flipped pair (a prism's build leg is a getter read backwards).

type OpLens (s :: k) (t :: k) (a :: k) (b :: k) = Optic (OpConstraint (Prostrong (LensFl :: (OPPOSITE k +-> OPPOSITE k) -> (OPPOSITE k +-> OPPOSITE k) -> Constraint))) s t a b Source Github #

A Prism and its op-lens encoding (Prism, a Lens over the opposite category) carry the same data, the two legs (b ~> t, s ~> t || a), so they are equivalent. toOpLens eliminates a PrismFl prism to its legs (via withPrism) and rebuilds the op-lens; fromOpLens eliminates the op-lens (via withLens on opOptic, i.e. as a lens over OPPOSITE) and rebuilds the PrismFl prism.

The op-lens encoding of a prism: a Lens over the opposite category.

toOpLens :: forall {k} (s :: k) (t :: k) (a :: k) (b :: k). (HasCoproducts k, Ob a, Ob b) => Prism s t a b -> OpLens s t a b Source Github #

fromOpLens :: forall {k} (s :: k) (t :: k) (a :: k) (b :: k). (CopyDiscard k, HasCoproducts k, Ob a) => OpLens s t a b -> Prism s t a b Source Github #