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

Safe HaskellNone
LanguageHaskell2010

Data.Profunctor.Optic.Lens

Contents

Synopsis

Lens & Ixlens

type Lens s t a b = forall p. Strong p => Optic p s t a b Source #

Lenses access one piece of a product.

\( \mathsf{Lens}\;S\;A = \exists C, S \cong C \times A \)

type Ixlens i s t a b = forall p. Strong p => IndexedOptic p i s t a b Source #

type Lens' s a = Lens s s a a Source #

type Ixlens' i s a = Ixlens i s s a a Source #

lens :: (s -> a) -> (s -> b -> t) -> Lens s t a b Source #

Obtain a Lens from a getter and setter.

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

  • sa (sbt s a) ≡ a
  • sbt s (sa s) ≡ s
  • sbt (sbt s a1) a2 ≡ sbt s a2

See Property.

ixlens :: (s -> (i, a)) -> (s -> b -> t) -> Ixlens i s t a b Source #

Obtain an indexed Lens from an indexed getter and a setter.

Compare lens and ixtraversal.

Caution: In order for the generated optic to be well-defined, you must ensure that the input functions constitute a legal indexed lens:

  • snd . sia (sbt s a) ≡ a
  • sbt s (snd $ sia s) ≡ s
  • sbt (sbt s a1) a2 ≡ sbt s a2

See Property.

lensVl :: (forall f. Functor f => (a -> f b) -> s -> f t) -> Lens s t a b Source #

Transform a Van Laarhoven lens into a profunctor lens.

Compare grateVl and traversalVl.

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

  • abst Identity ≡ Identity
  • fmap (abst f) . (abst g) ≡ getCompose . abst (Compose . fmap f . g)

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

ixlensVl :: (forall f. Functor f => (i -> a -> f b) -> s -> f t) -> Ixlens i s t a b Source #

Transform an indexed Van Laarhoven lens into an indexed profunctor Lens.

An Ixlens is a valid Lens and a valid IxTraversal.

Compare lensVl & ixtraversalVl.

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

  • iabst (const Identity) ≡ Identity
  • fmap (iabst $ const f) . (iabst $ const g) ≡ getCompose . iabst (const $ Compose . fmap f . g)

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

See Property.

matching :: (s -> (c, a)) -> ((c, b) -> t) -> Lens s t a b Source #

Obtain a Lens from its free tensor representation.

cloneLens :: ALens s t a b -> Lens s t a b Source #

TODO: Document

Colens & Cxlens

type Colens s t a b = forall p. Costrong p => Optic p s t a b Source #

type Cxlens k s t a b = forall p. Costrong p => CoindexedOptic p k s t a b Source #

type Colens' s a = Colens s s a a Source #

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

colens :: (b -> s -> a) -> (b -> t) -> Colens s t a b Source #

Obtain a Colens from a getter and setter.

colens f g ≡ \f g -> re (lens f g)
colens bsia bt ≡ colensVl $ \ts b -> bsia b <$> (ts . bt $ b)
review $ colens f g ≡ f
set . re $ re (lens f g) ≡ g

A Colens is a Review, so you can specialise types to obtain:

 review :: Colens' s a -> a -> s

Caution: In addition to the normal optic laws, the input functions must have the correct laziness annotations.

For example, this is a perfectly valid Colens:

co1 :: Colens a b (a, c) (b, c)
co1 = flip colens fst $  ~(_,y) b -> (b,y)

However removing the annotation will result in a faulty optic.

See Property.

colensVl :: (forall f. Functor f => (t -> f s) -> b -> f a) -> Colens s t a b Source #

Transform a Van Laarhoven colens into a profunctor colens.

Compare grateVl.

Caution: In addition to the normal optic laws, the input functions must have the correct laziness annotations.

For example, this is a perfectly valid Colens:

co1 = colensVl $ f ~(a,b) -> (,b) $ f a

However removing the annotation will result in a faulty optic.

comatching :: ((c, s) -> a) -> (b -> (c, t)) -> Colens s t a b Source #

Obtain a Colens from its free tensor representation.

Optics

ixfirst :: Ixlens i (a, c) (b, c) a b Source #

TODO: Document

>>> ixlists (ix @Int traversed . ix first' . ix traversed) [("foo",1), ("bar",2)]
[(0,'f'),(1,'o'),(2,'o'),(0,'b'),(1,'a'),(2,'r')]
>>> ixlists (ix @Int traversed . ixfirst . ix traversed) [("foo",1), ("bar",2)]
[(0,'f'),(1,'o'),(2,'o'),(0,'b'),(1,'a'),(2,'r')]
>>> ixlists (ix @Int traversed % ix first' % ix traversed) [("foo",1), ("bar",2)]
[(0,'f'),(1,'o'),(2,'o'),(1,'b'),(2,'a'),(3,'r')]
>>> ixlists (ix @Int traversed % ixfirst % ix traversed) [("foo",1), ("bar",2)]
[(0,'f'),(1,'o'),(2,'o'),(2,'b'),(3,'a'),(4,'r')]

cofirst :: Colens a b (a, c) (b, c) Source #

TODO: Document

ixsecond :: Ixlens i (c, a) (c, b) a b Source #

TODO: Document

cosecond :: Colens a b (c, a) (c, b) Source #

TODO: Document

united :: Lens' a () Source #

There is a Unit in everything.

>>> "hello" ^. united
()
>>> "hello" & united .~ ()
"hello"

voided :: Lens' Void a Source #

There is everything in a Void.

>>> [] & fmapped . voided <>~ "Void"
[]
>>> Nothing & fmapped . voided ..~ abs
Nothing

valued :: Eq k => k -> Lens' (k -> v) v Source #

TODO: Document

Compare keyed.

root :: Lens' (Tree a) a Source #

A Lens that focuses on the root of a Tree.

>>> view root $ Node 42 []
42

branches :: Lens' (Tree a) [Tree a] Source #

A Lens returning the direct descendants of the root of a Tree

view branchessubForest

Primitive operators

withLens :: ALens s t a b -> ((s -> a) -> (s -> b -> t) -> r) -> r Source #

Extract the two functions that characterize a Lens.

withIxlens :: Monoid i => AIxlens i s t a b -> ((s -> (i, a)) -> (s -> b -> t) -> r) -> r Source #

Extract the two functions that characterize a Lens.

Operators

toPastro :: ALens s t a b -> p a b -> Pastro p s t Source #

Use a Lens to construct a Pastro.

toTambara :: Strong p => ALens s t a b -> p a b -> Tambara p s t Source #

Use a Lens to construct a Tambara.

Carriers

type ALens s t a b = Optic (LensRep a b) s t a b Source #

type ALens' s a = ALens s s a a Source #

type AIxlens i s t a b = IndexedOptic (IxlensRep i a b) i s t a b Source #

type AIxlens' i s a = AIxlens i s s a a Source #

data LensRep a b s t Source #

The LensRep profunctor precisely characterizes a Lens.

Constructors

LensRep (s -> a) (s -> b -> t) 
Instances
Representable (LensRep a b) Source # 
Instance details

Defined in Data.Profunctor.Optic.Lens

Associated Types

type Rep (LensRep a b) :: Type -> Type #

Methods

tabulate :: (d -> Rep (LensRep a b) c) -> LensRep a b d c #

Strong (LensRep a b) Source # 
Instance details

Defined in Data.Profunctor.Optic.Lens

Methods

first' :: LensRep a b a0 b0 -> LensRep a b (a0, c) (b0, c) #

second' :: LensRep a b a0 b0 -> LensRep a b (c, a0) (c, b0) #

Profunctor (LensRep a b) Source # 
Instance details

Defined in Data.Profunctor.Optic.Lens

Methods

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

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

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

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

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

Sieve (LensRep a b) (Index a b) Source # 
Instance details

Defined in Data.Profunctor.Optic.Lens

Methods

sieve :: LensRep a b a0 b0 -> a0 -> Index a b b0 #

type Rep (LensRep a b) Source # 
Instance details

Defined in Data.Profunctor.Optic.Lens

type Rep (LensRep a b) = Index a b

data IxlensRep i a b s t Source #

Constructors

IxlensRep (s -> (i, a)) (s -> b -> t) 
Instances
Strong (IxlensRep i a b) Source # 
Instance details

Defined in Data.Profunctor.Optic.Lens

Methods

first' :: IxlensRep i a b a0 b0 -> IxlensRep i a b (a0, c) (b0, c) #

second' :: IxlensRep i a b a0 b0 -> IxlensRep i a b (c, a0) (c, b0) #

Profunctor (IxlensRep i a b) Source # 
Instance details

Defined in Data.Profunctor.Optic.Lens

Methods

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

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

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

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

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

Classes

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

Generalizing Star of a strong Functor

Note: Every Functor in Haskell is strong with respect to (,).

This describes profunctor strength with respect to the product structure of Hask.

http://www-kb.is.s.u-tokyo.ac.jp/~asada/papers/arrStrMnd.pdf

Minimal complete definition

first' | second'

Methods

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

Laws:

first'dimap swap swap . second'
lmap fstrmap fst . first'
lmap (second f) . first'rmap (second f) . first
first' . first'dimap assoc unassoc . first' where
  assoc ((a,b),c) = (a,(b,c))
  unassoc (a,(b,c)) = ((a,b),c)

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

Laws:

second'dimap swap swap . first'
lmap sndrmap snd . second'
lmap (first f) . second'rmap (first f) . second'
second' . second'dimap unassoc assoc . second' where
  assoc ((a,b),c) = (a,(b,c))
  unassoc (a,(b,c)) = ((a,b),c)
Instances
Monad m => Strong (Kleisli m) 
Instance details

Defined in Data.Profunctor.Strong

Methods

first' :: Kleisli m a b -> Kleisli m (a, c) (b, c) #

second' :: Kleisli m a b -> Kleisli m (c, a) (c, b) #

Strong (Pastro p) 
Instance details

Defined in Data.Profunctor.Strong

Methods

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

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

Strong p => Strong (Coyoneda p) 
Instance details

Defined in Data.Profunctor.Yoneda

Methods

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

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

Strong p => Strong (Yoneda p) 
Instance details

Defined in Data.Profunctor.Yoneda

Methods

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

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

Strong p => Strong (Closure p) 
Instance details

Defined in Data.Profunctor.Closed

Methods

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

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

Profunctor p => Strong (Tambara p) 
Instance details

Defined in Data.Profunctor.Strong

Methods

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

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

Functor m => Strong (Star m) 
Instance details

Defined in Data.Profunctor.Strong

Methods

first' :: Star m a b -> Star m (a, c) (b, c) #

second' :: Star m a b -> Star m (c, a) (c, b) #

Comonad f => Strong (Costar f) Source # 
Instance details

Defined in Data.Profunctor.Optic.Type

Methods

first' :: Costar f a b -> Costar f (a, c) (b, c) #

second' :: Costar f a b -> Costar f (c, a) (c, b) #

Arrow p => Strong (WrappedArrow p)

Arrow is Strong Category

Instance details

Defined in Data.Profunctor.Strong

Methods

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

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

Strong (Forget r) 
Instance details

Defined in Data.Profunctor.Strong

Methods

first' :: Forget r a b -> Forget r (a, c) (b, c) #

second' :: Forget r a b -> Forget r (c, a) (c, b) #

Strong (Fold0Rep r) Source # 
Instance details

Defined in Data.Profunctor.Optic.Fold0

Methods

first' :: Fold0Rep r a b -> Fold0Rep r (a, c) (b, c) #

second' :: Fold0Rep r a b -> Fold0Rep r (c, a) (c, b) #

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

Defined in Data.Profunctor.Strong

Methods

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

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

Strong (LensRep a b) Source # 
Instance details

Defined in Data.Profunctor.Optic.Lens

Methods

first' :: LensRep a b a0 b0 -> LensRep a b (a0, c) (b0, c) #

second' :: LensRep a b a0 b0 -> LensRep a b (c, a0) (c, b0) #

Strong (Traversal0Rep u v) Source # 
Instance details

Defined in Data.Profunctor.Optic.Traversal0

Methods

first' :: Traversal0Rep u v a b -> Traversal0Rep u v (a, c) (b, c) #

second' :: Traversal0Rep u v a b -> Traversal0Rep u v (c, a) (c, b) #

Contravariant f => Strong (Clown f :: Type -> Type -> Type) 
Instance details

Defined in Data.Profunctor.Strong

Methods

first' :: Clown f a b -> Clown f (a, c) (b, c) #

second' :: Clown f a b -> Clown f (c, a) (c, b) #

Costrong p => Strong (Re p s t) Source # 
Instance details

Defined in Data.Profunctor.Optic.Type

Methods

first' :: Re p s t a b -> Re p s t (a, c) (b, c) #

second' :: Re p s t a b -> Re p s t (c, a) (c, b) #

Strong (IxlensRep i a b) Source # 
Instance details

Defined in Data.Profunctor.Optic.Lens

Methods

first' :: IxlensRep i a b a0 b0 -> IxlensRep i a b (a0, c) (b0, c) #

second' :: IxlensRep i a b a0 b0 -> IxlensRep i a b (c, a0) (c, b0) #

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

Defined in Data.Profunctor.Strong

Methods

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

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

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

Defined in Data.Profunctor.Strong

Methods

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

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

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

Analogous to ArrowLoop, loop = unfirst

Minimal complete definition

unfirst | unsecond

Methods

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

Laws:

unfirstunsecond . dimap swap swap
lmap (,()) ≡ unfirst . rmap (,())
unfirst . lmap (second f) ≡ unfirst . rmap (second f)
unfirst . unfirst = unfirst . dimap assoc unassoc where
  assoc ((a,b),c) = (a,(b,c))
  unassoc (a,(b,c)) = ((a,b),c)

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

Laws:

unsecondunfirst . dimap swap swap
lmap ((),) ≡ unsecond . rmap ((),)
unsecond . lmap (first f) ≡ unsecond . rmap (first f)
unsecond . unsecond = unsecond . dimap unassoc assoc where
  assoc ((a,b),c) = (a,(b,c))
  unassoc (a,(b,c)) = ((a,b),c)
Instances
MonadFix m => Costrong (Kleisli m) 
Instance details

Defined in Data.Profunctor.Strong

Methods

unfirst :: Kleisli m (a, d) (b, d) -> Kleisli m a b #

unsecond :: Kleisli m (d, a) (d, b) -> Kleisli m a b #

Costrong p => Costrong (Coyoneda p) 
Instance details

Defined in Data.Profunctor.Yoneda

Methods

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

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

Costrong p => Costrong (Yoneda p) 
Instance details

Defined in Data.Profunctor.Yoneda

Methods

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

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

Costrong (Cotambara p) 
Instance details

Defined in Data.Profunctor.Strong

Methods

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

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

Costrong (Copastro p) 
Instance details

Defined in Data.Profunctor.Strong

Methods

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

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

Functor f => Costrong (Costar f) 
Instance details

Defined in Data.Profunctor.Strong

Methods

unfirst :: Costar f (a, d) (b, d) -> Costar f a b #

unsecond :: Costar f (d, a) (d, b) -> Costar f a b #

ArrowLoop p => Costrong (WrappedArrow p) 
Instance details

Defined in Data.Profunctor.Strong

Methods

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

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

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

Defined in Data.Profunctor.Strong

Methods

unfirst :: Tagged (a, d) (b, d) -> Tagged a b #

unsecond :: Tagged (d, a) (d, b) -> Tagged a b #

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

Defined in Data.Profunctor.Strong

Methods

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

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

Functor f => Costrong (Cokleisli f) 
Instance details

Defined in Data.Profunctor.Strong

Methods

unfirst :: Cokleisli f (a, d) (b, d) -> Cokleisli f a b #

unsecond :: Cokleisli f (d, a) (d, b) -> Cokleisli f a b #

Costrong (GrateRep a b) Source # 
Instance details

Defined in Data.Profunctor.Optic.Grate

Methods

unfirst :: GrateRep a b (a0, d) (b0, d) -> GrateRep a b a0 b0 #

unsecond :: GrateRep a b (d, a0) (d, b0) -> GrateRep a b a0 b0 #

Strong p => Costrong (Re p s t) Source # 
Instance details

Defined in Data.Profunctor.Optic.Type

Methods

unfirst :: Re p s t (a, d) (b, d) -> Re p s t a b #

unsecond :: Re p s t (d, a) (d, b) -> Re p s t a b #

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

Defined in Data.Profunctor.Strong

Methods

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

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

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

Defined in Data.Profunctor.Strong

Methods

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

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