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

Control.Lens.PartialIso

Description

See Rendel & Ostermann, Invertible syntax descriptions

Synopsis

PartialIso

dimapMaybe :: (Choice p, Cochoice p) => (s -> Maybe a) -> (b -> Maybe t) -> p a b -> p s t Source #

The dimapMaybe function endows Choice & Cochoice "partial profunctors" with an action >?< of PartialIsos.

type PartialIso s t a b = forall (p :: Type -> Type -> Type) (f :: Type -> Type). (Choice p, Cochoice p, Applicative f, Filterable f) => p a (f b) -> p s (f t) Source #

PartialIso is a first class inexhaustive pattern, similar to how Prism is a first class exhaustive pattern, by combining Prisms and coPrisms.

Every Iso & Prism is APartialIso.

PartialIsos are isomorphic to PartialExchanges.

type PartialIso' s a = PartialIso s s a a Source #

A simple PartialIso' s a is an identification of a subset of s with a subset of a.

Given a simple PartialIso', partialIso f g, has properties:

Just = f <=< g
Just = g <=< f

These are left and right inverse laws for proper PartialIso's. However, sometimes an improper PartialIso' will be useful. For an improper PartialIso', only the left inverse law holds.

Just = f <=< g

For an improper PartialIso', norm = g <=< f is an idempotent

norm = norm <=< norm

and can be regarded as a normalization within some equivalence class of terms.

type APartialIso s t a b = PartialExchange a b a (Maybe b) -> PartialExchange a b s (Maybe t) Source #

If you see APartialIso in a signature for a function, the function is expecting a PartialIso.

data PartialExchange a b s t Source #

A PartialExchange provides efficient access to the two functions that make up a PartialIso.

Constructors

PartialExchange (s -> Maybe a) (b -> Maybe t) 

Instances

Instances details
Tokenized a b (PartialExchange a b) Source # 
Instance details

Defined in Data.Profunctor.Distributor

Methods

anyToken :: PartialExchange a b a b Source #

Filtrator (PartialExchange a b) Source # 
Instance details

Defined in Data.Profunctor.Distributor

Methods

filtrate :: PartialExchange a b (Either a0 c) (Either b0 d) -> (PartialExchange a b a0 b0, PartialExchange a b c d) Source #

Choice (PartialExchange a b) Source # 
Instance details

Defined in Control.Lens.PartialIso

Methods

left' :: PartialExchange a b a0 b0 -> PartialExchange a b (Either a0 c) (Either b0 c) #

right' :: PartialExchange a b a0 b0 -> PartialExchange a b (Either c a0) (Either c b0) #

Cochoice (PartialExchange a b) Source # 
Instance details

Defined in Control.Lens.PartialIso

Methods

unleft :: PartialExchange a b (Either a0 d) (Either b0 d) -> PartialExchange a b a0 b0 #

unright :: PartialExchange a b (Either d a0) (Either d b0) -> PartialExchange a b a0 b0 #

Profunctor (PartialExchange a b) Source # 
Instance details

Defined in Control.Lens.PartialIso

Methods

dimap :: (a0 -> b0) -> (c -> d) -> PartialExchange a b b0 c -> PartialExchange a b a0 d #

lmap :: (a0 -> b0) -> PartialExchange a b b0 c -> PartialExchange a b a0 c #

rmap :: (b0 -> c) -> PartialExchange a b a0 b0 -> PartialExchange a b a0 c #

(#.) :: forall a0 b0 c q. Coercible c b0 => q b0 c -> PartialExchange a b a0 b0 -> PartialExchange a b a0 c #

(.#) :: forall a0 b0 c q. Coercible b0 a0 => PartialExchange a b b0 c -> q a0 b0 -> PartialExchange a b a0 c #

Functor (PartialExchange a b s) Source # 
Instance details

Defined in Control.Lens.PartialIso

Methods

fmap :: (a0 -> b0) -> PartialExchange a b s a0 -> PartialExchange a b s b0 #

(<$) :: a0 -> PartialExchange a b s b0 -> PartialExchange a b s a0 #

Filterable (PartialExchange a b s) Source # 
Instance details

Defined in Control.Lens.PartialIso

Methods

mapMaybe :: (a0 -> Maybe b0) -> PartialExchange a b s a0 -> PartialExchange a b s b0 #

catMaybes :: PartialExchange a b s (Maybe a0) -> PartialExchange a b s a0 #

filter :: (a0 -> Bool) -> PartialExchange a b s a0 -> PartialExchange a b s a0 #

drain :: PartialExchange a b s a0 -> PartialExchange a b s b0 #

partialIso :: (s -> Maybe a) -> (b -> Maybe t) -> PartialIso s t a b Source #

Build a PartialIso.

withPartialIso :: APartialIso s t a b -> ((s -> Maybe a) -> (b -> Maybe t) -> r) -> r Source #

Convert APartialIso to the pair of functions that characterize it.

clonePartialIso :: APartialIso s t a b -> PartialIso s t a b Source #

Clone APartialIso so that you can reuse the same monomorphically typed partial isomorphism for different purposes.

coPartialIso :: APartialIso b a t s -> PartialIso s t a b Source #

Clone and invert APartialIso.

crossPartialIso :: APartialIso s t a b -> APartialIso u v c d -> PartialIso (s, u) (t, v) (a, c) (b, d) Source #

Construct a PartialIso on pairs from components.

altPartialIso :: APartialIso s t a b -> APartialIso u v c d -> PartialIso (Either s u) (Either t v) (Either a c) (Either b d) Source #

Construct a PartialIso on Eithers from components.

Actions

(>?) :: Choice p => APrism s t a b -> p a b -> p s t infixl 4 Source #

Action of APrism on Choice Profunctors.

(?<) :: Cochoice p => APrism b a t s -> p a b -> p s t infixl 4 Source #

Action of a coPrism on Cochoice Profunctors.

(>?<) :: (Choice p, Cochoice p) => APartialIso s t a b -> p a b -> p s t infixl 4 Source #

mapIso :: Profunctor p => AnIso s t a b -> p a b -> p s t Source #

Action of AnIso on Profunctors.

coPrism :: (Profunctor p, Filterable f) => APrism b a t s -> p a (f b) -> p s (f t) Source #

Action of a coPrism on the composition of a Profunctor and Filterable.

Patterns

satisfied :: (a -> Bool) -> PartialIso' a a Source #

satisfied is the prototypical proper partial isomorphism, identifying a subset which satisfies a predicate.

nulled :: (AsEmpty s, AsEmpty t) => PartialIso s t () () Source #

nulled matches an Empty pattern, like _Empty.

notNulled :: (AsEmpty s, AsEmpty t) => PartialIso s t s t Source #

notNulled matches a non-Empty pattern.

streamed :: (AsEmpty s, AsEmpty t, Cons s s c c, Cons t t c c) => Iso' s t Source #

streamed is an isomorphism between two stream types with the same token type.

maybeEot :: forall a b p f. (Profunctor p, Functor f) => p (Either () a) (f (Either () b)) -> p (Maybe a) (f (Maybe b)) Source #

The either-of-tuples representation of Maybe.

listEot :: (Cons s s a a, AsEmpty t, Cons t t b b) => Iso s t (Either () (a, s)) (Either () (b, t)) Source #

The either-of-tuples representation of list-like streams.

Iterations

iterating :: APartialIso a b a b -> Iso a b a b Source #

Iterate the application of a partial isomorphism, useful for constructing fold/unfold isomorphisms.

difoldl1 :: Cons s t a b => APartialIso (c, a) (d, b) c d -> Iso (c, s) (d, t) (c, s) (d, t) Source #

Left fold & unfold APartialIso to an Iso.

difoldr1 :: Cons s t a b => APartialIso (a, c) (b, d) c d -> Iso (s, c) (t, d) (s, c) (t, d) Source #

Right fold & unfold APartialIso to an Iso.

difoldl :: (AsEmpty s, AsEmpty t, Cons s t a b) => APartialIso (c, a) (d, b) c d -> PartialIso (c, s) (d, t) c d Source #

Left fold & unfold APartialIso to a PartialIso.

difoldr :: (AsEmpty s, AsEmpty t, Cons s t a b) => APartialIso (a, c) (b, d) c d -> PartialIso (s, c) (t, d) c d Source #

Right fold & unfold APartialIso to a PartialIso.

difoldl' :: (AsEmpty s, Cons s s a a) => APrism' (c, a) c -> Prism' (c, s) c Source #

Left fold & unfold APrism' to a Prism'.

difoldr' :: (AsEmpty s, Cons s s a a) => APrism' (a, c) c -> Prism' (s, c) c Source #

Right fold & unfold APrism' to a Prism'.

Template Haskell

makeNestedPrisms :: Name -> DecsQ Source #

Generate a Prism for each constructor of a data type. Isos generated when possible. Reviews are created for constructors with existentially quantified constructors and GADTs.

See makePrisms for details and examples. The difference in makeNestedPrisms is that constructors with n > 2 arguments will use right-nested pairs, rather than a flat n-tuple. This makes them suitable for use on the left-hand-side of >? and >?<; with repeated use of >*< on the right-hand-side, resulting in right-nested pairs.

Orphan instances

(Profunctor p, Filterable f) => Cochoice (WrappedPafb f p) Source # 
Instance details

Methods

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

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

(Profunctor p, Filterable (p a)) => Filterable (Coyoneda p a) Source # 
Instance details

Methods

mapMaybe :: (a0 -> Maybe b) -> Coyoneda p a a0 -> Coyoneda p a b #

catMaybes :: Coyoneda p a (Maybe a0) -> Coyoneda p a a0 #

filter :: (a0 -> Bool) -> Coyoneda p a a0 -> Coyoneda p a a0 #

drain :: Coyoneda p a a0 -> Coyoneda p a b #

(Profunctor p, Filterable (p a)) => Filterable (Yoneda p a) Source # 
Instance details

Methods

mapMaybe :: (a0 -> Maybe b) -> Yoneda p a a0 -> Yoneda p a b #

catMaybes :: Yoneda p a (Maybe a0) -> Yoneda p a a0 #

filter :: (a0 -> Bool) -> Yoneda p a a0 -> Yoneda p a a0 #

drain :: Yoneda p a a0 -> Yoneda p a b #

(Profunctor p, Functor f) => Functor (WrappedPafb f p a) Source # 
Instance details

Methods

fmap :: (a0 -> b) -> WrappedPafb f p a a0 -> WrappedPafb f p a b #

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

(Profunctor p, Functor (p a), Filterable f) => Filterable (WrappedPafb f p a) Source # 
Instance details

Methods

mapMaybe :: (a0 -> Maybe b) -> WrappedPafb f p a a0 -> WrappedPafb f p a b #

catMaybes :: WrappedPafb f p a (Maybe a0) -> WrappedPafb f p a a0 #

filter :: (a0 -> Bool) -> WrappedPafb f p a a0 -> WrappedPafb f p a a0 #

drain :: WrappedPafb f p a a0 -> WrappedPafb f p a b #

Filterable (Forget r a :: Type -> Type) Source # 
Instance details

Methods

mapMaybe :: (a0 -> Maybe b) -> Forget r a a0 -> Forget r a b #

catMaybes :: Forget r a (Maybe a0) -> Forget r a a0 #

filter :: (a0 -> Bool) -> Forget r a a0 -> Forget r a a0 #

drain :: Forget r a a0 -> Forget r a b #

Filterable f => Filterable (Star f a) Source # 
Instance details

Methods

mapMaybe :: (a0 -> Maybe b) -> Star f a a0 -> Star f a b #

catMaybes :: Star f a (Maybe a0) -> Star f a a0 #

filter :: (a0 -> Bool) -> Star f a a0 -> Star f a a0 #

drain :: Star f a a0 -> Star f a b #