| Copyright | (C) 2025 - 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 |
Control.Lens.PartialIso
Description
See Rendel & Ostermann, Invertible syntax descriptions
Synopsis
- dimapMaybe :: (Choice p, Cochoice p) => (s -> Maybe a) -> (b -> Maybe t) -> p a b -> p s t
- 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)
- type PartialIso' s a = PartialIso s s a a
- type APartialIso s t a b = PartialExchange a b a (Maybe b) -> PartialExchange a b s (Maybe t)
- data PartialExchange a b s t = PartialExchange (s -> Maybe a) (b -> Maybe t)
- partialIso :: (s -> Maybe a) -> (b -> Maybe t) -> PartialIso s t a b
- withPartialIso :: APartialIso s t a b -> ((s -> Maybe a) -> (b -> Maybe t) -> r) -> r
- clonePartialIso :: APartialIso s t a b -> PartialIso s t a b
- coPartialIso :: APartialIso b a t s -> PartialIso s t a b
- crossPartialIso :: APartialIso s t a b -> APartialIso u v c d -> PartialIso (s, u) (t, v) (a, c) (b, d)
- altPartialIso :: APartialIso s t a b -> APartialIso u v c d -> PartialIso (Either s u) (Either t v) (Either a c) (Either b d)
- (>?) :: Choice p => APrism s t a b -> p a b -> p s t
- (?<) :: Cochoice p => APrism b a t s -> p a b -> p s t
- (>?<) :: (Choice p, Cochoice p) => APartialIso s t a b -> p a b -> p s t
- mapIso :: Profunctor p => AnIso s t a b -> p a b -> p s t
- coPrism :: (Profunctor p, Filterable f) => APrism b a t s -> p a (f b) -> p s (f t)
- satisfied :: (a -> Bool) -> PartialIso' a a
- nulled :: (AsEmpty s, AsEmpty t) => PartialIso s t () ()
- notNulled :: (AsEmpty s, AsEmpty t) => PartialIso s t s t
- streamed :: (AsEmpty s, AsEmpty t, Cons s s c c, Cons t t c c) => Iso' s t
- maybeEot :: forall a b p f. (Profunctor p, Functor f) => p (Either () a) (f (Either () b)) -> p (Maybe a) (f (Maybe b))
- listEot :: (Cons s s a a, AsEmpty t, Cons t t b b) => Iso s t (Either () (a, s)) (Either () (b, t))
- iterating :: APartialIso a b a b -> Iso a b a b
- difoldl1 :: Cons s t a b => APartialIso (c, a) (d, b) c d -> Iso (c, s) (d, t) (c, s) (d, t)
- difoldr1 :: Cons s t a b => APartialIso (a, c) (b, d) c d -> Iso (s, c) (t, d) (s, c) (t, d)
- difoldl :: (AsEmpty s, AsEmpty t, Cons s t a b) => APartialIso (c, a) (d, b) c d -> PartialIso (c, s) (d, t) c d
- difoldr :: (AsEmpty s, AsEmpty t, Cons s t a b) => APartialIso (a, c) (b, d) c d -> PartialIso (s, c) (t, d) c d
- difoldl' :: (AsEmpty s, Cons s s a a) => APrism' (c, a) c -> Prism' (c, s) c
- difoldr' :: (AsEmpty s, Cons s s a a) => APrism' (a, c) c -> Prism' (s, c) c
- makeNestedPrisms :: Name -> DecsQ
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
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 #
Action of APartialIso on Choice and Cochoice Profunctors.
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.
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.
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.