profunctor-optics-0.0.0.2: An optics library compatible with the typeclasses in 'profunctors'.

Safe HaskellNone
LanguageHaskell2010

Data.Profunctor.Optic.Prism

Contents

Synopsis

Prism & Cxprism

type Prism s t a b = forall p. Choice p => Optic p s t a b Source #

Prisms access one piece of a sum.

\( \mathsf{Prism}\;S\;A = \exists D, S \cong D + A \)

type Prism' s a = Prism s s a a Source #

type Cxprism k s t a b = forall p. Choice p => CoindexedOptic p k s t a b Source #

type Cxprism' k s a = Cxprism k s s a a Source #

type APrism s t a b = Optic (PrismRep a b) s t a b Source #

type APrism' s a = APrism s s a a Source #

prism :: (s -> t + a) -> (b -> t) -> Prism s t a b Source #

Obtain a Prism from a constructor and a matcher function.

Caution: In order for the generated optic to be well-defined, you must ensure that the input functions satisfy the following properties:

  • sta (bt b) ≡ Right b
  • (id ||| bt) (sta s) ≡ s
  • left sta (sta s) ≡ left Left (sta s)

More generally, a profunctor optic must be monoidal as a natural transformation:

See Property.

prism' :: (s -> Maybe a) -> (a -> s) -> Prism' s a Source #

Obtain a Prism' from a reviewer and a matcher function that produces a Maybe.

cxprism :: (s -> (k -> t) + a) -> (b -> t) -> Cxprism k s t a b Source #

Obtain a Cxprism' from a reviewer and a matcher function that returns either a match or a failure handler.

handling :: (s -> c + a) -> ((c + b) -> t) -> Prism s t a b Source #

Obtain a Prism from its free tensor representation.

Useful for constructing prisms from try and handle functions.

clonePrism :: APrism s t a b -> Prism s t a b Source #

TODO: Document

Coprism & Ixprism

type Coprism s t a b = forall p. Cochoice p => Optic p s t a b Source #

type Coprism' t b = Coprism t t b b Source #

type Ixprism i s t a b = forall p. Cochoice p => IndexedOptic p i s t a b Source #

type Ixprism' i s a = Coprism s s a a Source #

type ACoprism s t a b = Optic (CoprismRep a b) s t a b Source #

type ACoprism' s a = ACoprism s s a a Source #

coprism :: (s -> a) -> (b -> a + t) -> Coprism s t a b Source #

Obtain a Cochoice optic from a constructor and a matcher function.

coprism f g ≡ f g -> re (prism f g)

Caution: In order for the generated optic to be well-defined, you must ensure that the input functions satisfy the following properties:

  • bat (bt b) ≡ Right b
  • (id ||| bt) (bat b) ≡ b
  • left bat (bat b) ≡ left Left (bat b)

A Coprism is a View, so you can specialise types to obtain:

 view :: Coprism' s a -> s -> a

coprism' :: (s -> a) -> (a -> Maybe s) -> Coprism' s a Source #

Create a Coprism from a reviewer and a matcher function that produces a Maybe.

rehandling :: ((c + s) -> a) -> (b -> c + t) -> Coprism s t a b Source #

Obtain a Coprism from its free tensor representation.

cloneCoprism :: ACoprism s t a b -> Coprism s t a b Source #

TODO: Document

Optics

l1 :: Prism ((a :+: c) t) ((b :+: c) t) (a t) (b t) Source #

r1 :: Prism ((c :+: a) t) ((c :+: b) t) (a t) (b t) Source #

left :: Prism (a + c) (b + c) a b Source #

Prism into the Left constructor of Either.

right :: Prism (c + a) (c + b) a b Source #

Prism into the Right constructor of Either.

cxright :: (e -> k -> e + b) -> Cxprism k (e + a) (e + b) a b Source #

Coindexed prism into the Right constructor of Either.

>>> cxset (catchFoo "Caught foo") id $ Left "fooError"
Right "Caught foo"
>>> cxset (catchFoo "Caught foo") id $ Left "barError"
Left "barError"

just :: Prism (Maybe a) (Maybe b) a b Source #

Prism into the Just constructor of Maybe.

nothing :: Prism (Maybe a) (Maybe b) () () Source #

Prism into the Nothing constructor of Maybe.

cxjust :: (k -> Maybe b) -> Cxprism k (Maybe a) (Maybe b) a b Source #

Coindexed prism into the Just constructor of Maybe.

>>> Just "foo" & catchOn 1 ##~ (\k msg -> show k ++ ": " ++ msg)
Just "0: foo"
>>> Nothing & catchOn 1 ##~ (\k msg -> show k ++ ": " ++ msg)
Nothing
>>> Nothing & catchOn 0 ##~ (\k msg -> show k ++ ": " ++ msg)
Just "caught"

keyed :: Eq a => a -> Prism' (a, b) b Source #

Match a given key to obtain the associated value.

filtered :: (a -> Bool) -> Prism' a a Source #

Filter another optic.

>>> [1..10] ^.. folded . filtered even
[2,4,6,8,10]

compared :: Eq a => Prd a => a -> Prism' a Ordering Source #

Focus on comparability to a given element of a partial order.

prefixed :: Eq a => [a] -> Prism' [a] [a] Source #

Prism into the remainder of a list with a given prefix.

only :: Eq a => a -> Prism' a () Source #

Focus not just on a case, but a specific value of that case.

nearly :: a -> (a -> Bool) -> Prism' a () Source #

Create a Prism from a value and a predicate.

nthbit :: Bits s => Int -> Prism' s () Source #

Focus on the truth value of the nth bit in a bit array.

sync :: Exception e => Prism' e e Source #

Check whether an exception is synchronous.

async :: Exception e => Prism' e e Source #

Check whether an exception is asynchronous.

Primitive operators

withPrism :: APrism s t a b -> ((s -> t + a) -> (b -> t) -> r) -> r Source #

Extract the two functions that characterize a Prism.

withCoprism :: ACoprism s t a b -> ((s -> a) -> (b -> a + t) -> r) -> r Source #

Extract the two functions that characterize a Coprism.

Operators

aside :: APrism s t a b -> Prism (e, s) (e, t) (e, a) (e, b) Source #

Use a Prism to lift part of a structure.

without :: APrism s t a b -> APrism u v c d -> Prism (s + u) (t + v) (a + c) (b + d) Source #

Given a pair of prisms, project sums.

below :: Traversable f => APrism' s a -> Prism' (f s) (f a) Source #

Lift a Prism through a Traversable functor.

Returns a Prism that matches only if each element matches the original Prism.

>>> [Left 1, Right "foo", Left 4, Right "woot"] ^.. below right
[]
>>> [Right "hail hydra!", Right "foo", Right "blah", Right "woot"] ^.. below right
[["hail hydra!","foo","blah","woot"]]

toPastroSum :: APrism s t a b -> p a b -> PastroSum p s t Source #

Use a Prism to construct a PastroSum.

toTambaraSum :: Choice p => APrism s t a b -> p a b -> TambaraSum p s t Source #

Use a Prism to construct a TambaraSum.

Carriers

data PrismRep a b s t Source #

The PrismRep profunctor precisely characterizes a Prism.

Constructors

PrismRep (s -> t + a) (b -> t) 
Instances
Choice (PrismRep a b) Source # 
Instance details

Defined in Data.Profunctor.Optic.Prism

Methods

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

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

Profunctor (PrismRep a b) Source # 
Instance details

Defined in Data.Profunctor.Optic.Prism

Methods

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

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

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

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

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

Functor (PrismRep a b s) Source # 
Instance details

Defined in Data.Profunctor.Optic.Prism

Methods

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

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

data CoprismRep a b s t Source #

Constructors

CoprismRep (s -> a) (b -> a + t) 
Instances
Cochoice (CoprismRep a b) Source # 
Instance details

Defined in Data.Profunctor.Optic.Prism

Methods

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

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

Profunctor (CoprismRep a b) Source # 
Instance details

Defined in Data.Profunctor.Optic.Prism

Methods

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

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

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

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

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

Functor (CoprismRep a b s) Source # 
Instance details

Defined in Data.Profunctor.Optic.Prism

Methods

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

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

Classes

class Profunctor p => Choice (p :: Type -> Type -> Type) where #

The generalization of Costar of Functor that is strong with respect to Either.

Note: This is also a notion of strength, except with regards to another monoidal structure that we can choose to equip Hask with: the cocartesian coproduct.

Minimal complete definition

left' | right'

Methods

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

Laws:

left'dimap swapE swapE . right' where
  swapE :: Either a b -> Either b a
  swapE = either Right Left
rmap Leftlmap Left . left'
lmap (right f) . left'rmap (right f) . left'
left' . left'dimap assocE unassocE . left' where
  assocE :: Either (Either a b) c -> Either a (Either b c)
  assocE (Left (Left a)) = Left a
  assocE (Left (Right b)) = Right (Left b)
  assocE (Right c) = Right (Right c)
  unassocE :: Either a (Either b c) -> Either (Either a b) c
  unassocE (Left a) = Left (Left a)
  unassocE (Right (Left b) = Left (Right b)
  unassocE (Right (Right c)) = Right c)

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

Laws:

right'dimap swapE swapE . left' where
  swapE :: Either a b -> Either b a
  swapE = either Right Left
rmap Rightlmap Right . right'
lmap (left f) . right'rmap (left f) . right'
right' . right'dimap unassocE assocE . right' where
  assocE :: Either (Either a b) c -> Either a (Either b c)
  assocE (Left (Left a)) = Left a
  assocE (Left (Right b)) = Right (Left b)
  assocE (Right c) = Right (Right c)
  unassocE :: Either a (Either b c) -> Either (Either a b) c
  unassocE (Left a) = Left (Left a)
  unassocE (Right (Left b) = Left (Right b)
  unassocE (Right (Right c)) = Right c)
Instances
Monad m => Choice (Kleisli m) 
Instance details

Defined in Data.Profunctor.Choice

Methods

left' :: Kleisli m a b -> Kleisli m (Either a c) (Either b c) #

right' :: Kleisli m a b -> Kleisli m (Either c a) (Either c b) #

Choice (PastroSum p) 
Instance details

Defined in Data.Profunctor.Choice

Methods

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

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

Choice p => Choice (Coyoneda p) 
Instance details

Defined in Data.Profunctor.Yoneda

Methods

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

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

Choice p => Choice (Yoneda p) 
Instance details

Defined in Data.Profunctor.Yoneda

Methods

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

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

Profunctor p => Choice (TambaraSum p) 
Instance details

Defined in Data.Profunctor.Choice

Methods

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

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

Choice p => Choice (Tambara p) 
Instance details

Defined in Data.Profunctor.Choice

Methods

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

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

Applicative f => Choice (Star f) 
Instance details

Defined in Data.Profunctor.Choice

Methods

left' :: Star f a b -> Star f (Either a c) (Either b c) #

right' :: Star f a b -> Star f (Either c a) (Either c b) #

Traversable w => Choice (Costar w) 
Instance details

Defined in Data.Profunctor.Choice

Methods

left' :: Costar w a b -> Costar w (Either a c) (Either b c) #

right' :: Costar w a b -> Costar w (Either c a) (Either c b) #

ArrowChoice p => Choice (WrappedArrow p) 
Instance details

Defined in Data.Profunctor.Choice

Methods

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

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

Monoid r => Choice (Forget r) 
Instance details

Defined in Data.Profunctor.Choice

Methods

left' :: Forget r a b -> Forget r (Either a c) (Either b c) #

right' :: Forget r a b -> Forget r (Either c a) (Either c b) #

Choice (Tagged :: Type -> Type -> Type) 
Instance details

Defined in Data.Profunctor.Choice

Methods

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

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

Choice (Fold0Rep r) Source # 
Instance details

Defined in Data.Profunctor.Optic.Fold0

Methods

left' :: Fold0Rep r a b -> Fold0Rep r (Either a c) (Either b c) #

right' :: Fold0Rep r a b -> Fold0Rep r (Either c a) (Either c b) #

Choice ((->) :: Type -> Type -> Type) 
Instance details

Defined in Data.Profunctor.Choice

Methods

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

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

Comonad w => Choice (Cokleisli w)

extract approximates costrength

Instance details

Defined in Data.Profunctor.Choice

Methods

left' :: Cokleisli w a b -> Cokleisli w (Either a c) (Either b c) #

right' :: Cokleisli w a b -> Cokleisli w (Either c a) (Either c b) #

Choice (PrismRep a b) Source # 
Instance details

Defined in Data.Profunctor.Optic.Prism

Methods

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

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

Choice (Traversal0Rep u v) Source # 
Instance details

Defined in Data.Profunctor.Optic.Traversal0

Methods

left' :: Traversal0Rep u v a b -> Traversal0Rep u v (Either a c) (Either b c) #

right' :: Traversal0Rep u v a b -> Traversal0Rep u v (Either c a) (Either c b) #

Functor f => Choice (Joker f :: Type -> Type -> Type) 
Instance details

Defined in Data.Profunctor.Choice

Methods

left' :: Joker f a b -> Joker f (Either a c) (Either b c) #

right' :: Joker f a b -> Joker f (Either c a) (Either c b) #

Cochoice p => Choice (Re p s t) Source # 
Instance details

Defined in Data.Profunctor.Optic.Type

Methods

left' :: Re p s t a b -> Re p s t (Either a c) (Either b c) #

right' :: Re p s t a b -> Re p s t (Either c a) (Either c b) #

(Choice p, Choice q) => Choice (Product p q) 
Instance details

Defined in Data.Profunctor.Choice

Methods

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

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

(Functor f, Choice p) => Choice (Tannen f p) 
Instance details

Defined in Data.Profunctor.Choice

Methods

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

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

class Profunctor p => Cochoice (p :: Type -> Type -> Type) where #

Minimal complete definition

unleft | unright

Methods

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

Laws:

unleftunright . dimap swapE swapE where
  swapE :: Either a b -> Either b a
  swapE = either Right Left
rmap (either id absurd) ≡ unleft . lmap (either id absurd)
unfirst . rmap (second f) ≡ unfirst . lmap (second f)
unleft . unleftunleft . dimap assocE unassocE where
  assocE :: Either (Either a b) c -> Either a (Either b c)
  assocE (Left (Left a)) = Left a
  assocE (Left (Right b)) = Right (Left b)
  assocE (Right c) = Right (Right c)
  unassocE :: Either a (Either b c) -> Either (Either a b) c
  unassocE (Left a) = Left (Left a)
  unassocE (Right (Left b) = Left (Right b)
  unassocE (Right (Right c)) = Right c)

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

Laws:

unrightunleft . dimap swapE swapE where
  swapE :: Either a b -> Either b a
  swapE = either Right Left
rmap (either absurd id) ≡ unright . lmap (either absurd id)
unsecond . rmap (first f) ≡ unsecond . lmap (first f)
unright . unrightunright . dimap unassocE assocE where
  assocE :: Either (Either a b) c -> Either a (Either b c)
  assocE (Left (Left a)) = Left a
  assocE (Left (Right b)) = Right (Left b)
  assocE (Right c) = Right (Right c)
  unassocE :: Either a (Either b c) -> Either (Either a b) c
  unassocE (Left a) = Left (Left a)
  unassocE (Right (Left b) = Left (Right b)
  unassocE (Right (Right c)) = Right c)
Instances
Cochoice p => Cochoice (Coyoneda p) 
Instance details

Defined in Data.Profunctor.Yoneda

Methods

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

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

Cochoice p => Cochoice (Yoneda p) 
Instance details

Defined in Data.Profunctor.Yoneda

Methods

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

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

Cochoice (CotambaraSum p) 
Instance details

Defined in Data.Profunctor.Choice

Methods

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

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

Cochoice (CopastroSum p) 
Instance details

Defined in Data.Profunctor.Choice

Methods

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

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

Traversable f => Cochoice (Star f) 
Instance details

Defined in Data.Profunctor.Choice

Methods

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

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

Applicative f => Cochoice (Costar f) 
Instance details

Defined in Data.Profunctor.Choice

Methods

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

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

Cochoice (Forget r) Source # 
Instance details

Defined in Data.Profunctor.Optic.Type

Methods

unleft :: Forget r (Either a d) (Either b d) -> Forget r a b #

unright :: Forget r (Either d a) (Either d b) -> Forget r a b #

Cochoice (Fold0Rep r) Source # 
Instance details

Defined in Data.Profunctor.Optic.Fold0

Methods

unleft :: Fold0Rep r (Either a d) (Either b d) -> Fold0Rep r a b #

unright :: Fold0Rep r (Either d a) (Either d b) -> Fold0Rep r a b #

Cochoice ((->) :: Type -> Type -> Type) 
Instance details

Defined in Data.Profunctor.Choice

Methods

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

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

Cochoice (CoprismRep a b) Source # 
Instance details

Defined in Data.Profunctor.Optic.Prism

Methods

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

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

Choice p => Cochoice (Re p s t) Source # 
Instance details

Defined in Data.Profunctor.Optic.Type

Methods

unleft :: Re p s t (Either a d) (Either b d) -> Re p s t a b #

unright :: Re p s t (Either d a) (Either d b) -> Re p s t a b #

(Cochoice p, Cochoice q) => Cochoice (Product p q) 
Instance details

Defined in Data.Profunctor.Choice

Methods

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

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

(Functor f, Cochoice p) => Cochoice (Tannen f p) 
Instance details

Defined in Data.Profunctor.Choice

Methods

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

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