profunctor-optics-0.0.2: A compact optics library compatible with the typeclasses in profunctors.

Data.Profunctor.Optic.Lens

Contents

Synopsis

# Lens

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

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

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

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

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

type Colens' t b = Lens t t b b 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.

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:

• o id ≡ id
• o (Procompose p q) ≡ Procompose (o p) (o q)

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 :: (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  Caution: Colenses are recursive, similar to ArrowLoop. In addition to the normal optic laws, the input functions must have the correct laziness annotations. For example, this is a perfectly valid Colens: ct21 :: Colens a b (a, c) (b, c) ct21 = flip colens fst$  ~(_,c) b -> (b,c)


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:

ct21 :: Colens a b (a, c) (b, c)
ct21 = 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.

>>> fib = comatching (uncurry L.take . swap) (id &&& L.reverse) --fib :: Colens Int [Int] [Int] [Int]
>>> 10 & fib ..~ \xs -> 1 : 1 : Prelude.zipWith (+) xs (Prelude.tail xs)
[89,55,34,21,13,8,5,3,2,1,1]


cloneColens :: AColens s t a b -> Colens s t a b Source #

TODO: Document

# Grate

type Grate s t a b = forall p. Closed p => Optic p s t a b Source #

$$\mathsf{Grate}\;S\;A = \exists I, S \cong I \to A$$

type Grate' s a = Grate s s a a Source #

grate :: (((s -> a) -> b) -> t) -> Grate s t a b Source #

Obtain a Grate from a nested continuation.

The resulting optic is the corepresentable counterpart to Lens, and sits between Iso and Setter.

A Grate lets you lift a profunctor through any representable functor (aka Naperian container). In the special case where the indexing type is finitary (e.g. Bool) then the tabulated type is isomorphic to a fied length vector (e.g. 'V2 a').

The identity container is representable, and representable functors are closed under composition.

See https://www.cs.ox.ac.uk/jeremy.gibbons/publications/proyo.pdf section 4.6 for more background on Grates, and compare to the lens-family version.

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

• sabt ($s) ≡ s • sabt (k -> f (k . sabt)) ≡ sabt (k -> f ($ k))

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

• o id ≡ id
• o (Procompose p q) ≡ Procompose (o p) (o q)

See Property.

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

Transform a Van Laarhoven grate into a profunctor grate.

Compare lensVl & cotraversalVl.

Caution: In order for the generated family to be well-defined, you must ensure that the traversal1 law holds for the input function:

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

See Property.

inverting :: (s -> a) -> (b -> t) -> Grate s t a b Source #

Construct a Grate from a pair of inverses.

cloneGrate :: AGrate s t a b -> Grate s t a b Source #

TODO: Document

# Optics

united :: Lens' a () Source #

There is a '()' in everything.

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


There is everything in a Void.

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


represented :: Representable f => Grate (f a) (f b) a b Source #

Obtain a Grate from a Representable functor.

distributed :: Distributive f => Grate (f a) (f b) a b Source #

Obtain a Grate from a distributive functor.

endomorphed :: Grate' (Endo a) a Source #

Obtain a Grate from an endomorphism.

>>> flip appEndo 2 $zipsWith2 endomorphed (+) (Endo (*3)) (Endo (*4)) 14  precomposed :: Grate (Lin a b1 c) (Lin a b2 c) (Vec a b1) (Vec a b2) Source # Obtain a Grate from a linear map. dotted :: Grate c (Cov a c) a a Source # Obtain a Grate from a linear functional. continued :: Grate c (Cont a c) a a Source # Obtain a Grate from a continuation. zipsWith2 continued :: (a -> a -> a) -> c -> c -> Cont a c  continuedT :: Grate c (ContT a m c) (m a) (m a) Source # Obtain a Grate from a continuation. zipsWith2 continued :: (m a -> m a -> m a) -> c -> c -> ContT a m c  calledCC :: MonadCont m => Grate a (m a) (m b) (m a) Source # Lift the current continuation into the calling context. zipsWith2 calledCC :: MonadCont m => (m b -> m b -> m s) -> s -> s -> m s  # Operators zipsWith0 :: AGrate s t a b -> b -> t Source # Set all fields to the given value. This is essentially a restricted variant of review. zipsWith2 :: AGrate s t a b -> (a -> a -> b) -> s -> s -> t Source # Zip over a Grate. \f -> zipsWith2 closed (zipsWith2 closed f) ≡ zipsWith2 (closed . closed) zipsWith3 :: AGrate s t a b -> (a -> a -> a -> b) -> s -> s -> s -> t Source # Zip over a Grate with 3 arguments. zipsWith4 :: AGrate s t a b -> (a -> a -> a -> a -> b) -> s -> s -> s -> s -> t Source # Zip over a Grate with 4 arguments. zipsWithF :: Functor f => AGrate s t a b -> (f a -> b) -> f s -> t Source # Extract the higher order function that characterizes a Grate. The grate laws can be stated in terms or withGrate: Identity: zipsWithF o runIdentity ≡ runIdentity  Composition: zipsWithF o f . fmap (zipsWithF o g) ≡ zipsWithF o (f . fmap g . getCompose) . Compose  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. toClosure :: Closed p => AGrate s t a b -> p a b -> Closure p s t Source # Use a Grate to construct a Closure. toEnvironment :: Closed p => AGrate s t a b -> p a b -> Environment p s t Source # Use a Grate to construct an Environment. # 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.riec.tohoku.ac.jp/~asada/papers/arrStrMnd.pdf Minimal complete definition Methods first' :: p a b -> p (a, c) (b, c) # Laws: first' ≡ dimap swap swap . second' lmap fst ≡ rmap 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 snd ≡ rmap 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 detailsDefined in Data.Profunctor.Strong Methodsfirst' :: Kleisli m a b -> Kleisli m (a, c) (b, c) #second' :: Kleisli m a b -> Kleisli m (c, a) (c, b) # Strong p => Strong (Yoneda p) Instance detailsDefined in Data.Profunctor.Yoneda Methodsfirst' :: Yoneda p a b -> Yoneda p (a, c) (b, c) #second' :: Yoneda p a b -> Yoneda p (c, a) (c, b) # Strong p => Strong (Coyoneda p) Instance detailsDefined in Data.Profunctor.Yoneda Methodsfirst' :: Coyoneda p a b -> Coyoneda p (a, c) (b, c) #second' :: Coyoneda p a b -> Coyoneda p (c, a) (c, b) # Strong p => Strong (Closure p) Instance detailsDefined in Data.Profunctor.Closed Methodsfirst' :: 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 detailsDefined in Data.Profunctor.Strong Methodsfirst' :: Tambara p a b -> Tambara p (a, c) (b, c) #second' :: Tambara p a b -> Tambara p (c, a) (c, b) # Strong (Pastro p) Instance detailsDefined in Data.Profunctor.Strong Methodsfirst' :: Pastro p a b -> Pastro p (a, c) (b, c) #second' :: Pastro p a b -> Pastro p (c, a) (c, b) # Functor m => Strong (Star m) Instance detailsDefined in Data.Profunctor.Strong Methodsfirst' :: Star m a b -> Star m (a, c) (b, c) #second' :: Star m a b -> Star m (c, a) (c, b) # Arrow p => Strong (WrappedArrow p) Arrow is Strong Category Instance detailsDefined in Data.Profunctor.Strong Methodsfirst' :: 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 detailsDefined in Data.Profunctor.Strong Methodsfirst' :: Forget r a b -> Forget r (a, c) (b, c) #second' :: Forget r a b -> Forget r (c, a) (c, b) # Strong (Lin a) Instance detailsDefined in Data.Semimodule.Free Methodsfirst' :: Lin a a0 b -> Lin a (a0, c) (b, c) #second' :: Lin a a0 b -> Lin a (c, a0) (c, b) # Source # Instance detailsDefined in Data.Profunctor.Optic.Carrier Methodsfirst' :: Conjoin j a b -> Conjoin j (a, c) (b, c) #second' :: Conjoin j a b -> Conjoin j (c, a) (c, b) # Source # Instance detailsDefined in Data.Profunctor.Optic.Carrier Methodsfirst' :: 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 detailsDefined in Data.Profunctor.Strong Methodsfirst' :: (a -> b) -> (a, c) -> (b, c) #second' :: (a -> b) -> (c, a) -> (c, b) # Strong (Traversal0Rep a b) Source # Instance detailsDefined in Data.Profunctor.Optic.Carrier Methodsfirst' :: Traversal0Rep a b a0 b0 -> Traversal0Rep a b (a0, c) (b0, c) #second' :: Traversal0Rep a b a0 b0 -> Traversal0Rep a b (c, a0) (c, b0) # Strong (LensRep a b) Source # Instance detailsDefined in Data.Profunctor.Optic.Carrier Methodsfirst' :: 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) # Contravariant f => Strong (Clown f :: Type -> Type -> Type) Instance detailsDefined in Data.Profunctor.Strong Methodsfirst' :: 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 detailsDefined in Data.Profunctor.Optic.Types Methodsfirst' :: 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 p, Strong q) => Strong (Sum p q) Instance detailsDefined in Data.Profunctor.Strong Methodsfirst' :: Sum p q a b -> Sum p q (a, c) (b, c) #second' :: Sum p q a b -> Sum p q (c, a) (c, b) # (Strong p, Strong q) => Strong (Product p q) Instance detailsDefined in Data.Profunctor.Strong Methodsfirst' :: 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 detailsDefined in Data.Profunctor.Strong Methodsfirst' :: 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 Methods unfirst :: p (a, d) (b, d) -> p a b # Laws: unfirst ≡ unsecond . 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: unsecond ≡ unfirst . 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 detailsDefined in Data.Profunctor.Strong Methodsunfirst :: Kleisli m (a, d) (b, d) -> Kleisli m a b #unsecond :: Kleisli m (d, a) (d, b) -> Kleisli m a b # Costrong p => Costrong (Yoneda p) Instance detailsDefined in Data.Profunctor.Yoneda Methodsunfirst :: Yoneda p (a, d) (b, d) -> Yoneda p a b #unsecond :: Yoneda p (d, a) (d, b) -> Yoneda p a b # Costrong p => Costrong (Coyoneda p) Instance detailsDefined in Data.Profunctor.Yoneda Methodsunfirst :: Coyoneda p (a, d) (b, d) -> Coyoneda p a b #unsecond :: Coyoneda p (d, a) (d, b) -> Coyoneda p a b # Instance detailsDefined in Data.Profunctor.Strong Methodsunfirst :: Cotambara p (a, d) (b, d) -> Cotambara p a b #unsecond :: Cotambara p (d, a) (d, b) -> Cotambara p a b # Instance detailsDefined in Data.Profunctor.Strong Methodsunfirst :: 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 detailsDefined in Data.Profunctor.Strong Methodsunfirst :: Costar f (a, d) (b, d) -> Costar f a b #unsecond :: Costar f (d, a) (d, b) -> Costar f a b # Instance detailsDefined in Data.Profunctor.Strong Methodsunfirst :: 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 detailsDefined in Data.Profunctor.Strong Methodsunfirst :: Tagged (a, d) (b, d) -> Tagged a b #unsecond :: Tagged (d, a) (d, b) -> Tagged a b # Source # Instance detailsDefined in Data.Profunctor.Optic.Carrier Methodsunfirst :: Conjoin j (a, d) (b, d) -> Conjoin j a b #unsecond :: Conjoin j (d, a) (d, b) -> Conjoin j a b # Costrong ((->) :: Type -> Type -> Type) Instance detailsDefined in Data.Profunctor.Strong Methodsunfirst :: ((a, d) -> (b, d)) -> a -> b #unsecond :: ((d, a) -> (d, b)) -> a -> b # Functor f => Costrong (Cokleisli f) Instance detailsDefined in Data.Profunctor.Strong Methodsunfirst :: 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 detailsDefined in Data.Profunctor.Optic.Carrier Methodsunfirst :: 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 detailsDefined in Data.Profunctor.Optic.Types Methodsunfirst :: 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 (Sum p q) Instance detailsDefined in Data.Profunctor.Strong Methodsunfirst :: Sum p q (a, d) (b, d) -> Sum p q a b #unsecond :: Sum p q (d, a) (d, b) -> Sum p q a b # (Costrong p, Costrong q) => Costrong (Product p q) Instance detailsDefined in Data.Profunctor.Strong Methodsunfirst :: 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 detailsDefined in Data.Profunctor.Strong Methodsunfirst :: 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 # class Profunctor p => Closed (p :: Type -> Type -> Type) where # A strong profunctor allows the monoidal structure to pass through. A closed profunctor allows the closed structure to pass through. Methods closed :: p a b -> p (x -> a) (x -> b) # Laws: lmap (. f) . closed ≡ rmap (. f) . closed closed . closed ≡ dimap uncurry curry . closed dimap const ($()) . closed ≡ id

Instances
 (Distributive f, Monad f) => Closed (Kleisli f) Instance detailsDefined in Data.Profunctor.Closed Methodsclosed :: Kleisli f a b -> Kleisli f (x -> a) (x -> b) # Closed p => Closed (Yoneda p) Instance detailsDefined in Data.Profunctor.Yoneda Methodsclosed :: Yoneda p a b -> Yoneda p (x -> a) (x -> b) # Closed p => Closed (Coyoneda p) Instance detailsDefined in Data.Profunctor.Yoneda Methodsclosed :: Coyoneda p a b -> Coyoneda p (x -> a) (x -> b) # Profunctor p => Closed (Closure p) Instance detailsDefined in Data.Profunctor.Closed Methodsclosed :: Closure p a b -> Closure p (x -> a) (x -> b) # Instance detailsDefined in Data.Profunctor.Closed Methodsclosed :: Environment p a b -> Environment p (x -> a) (x -> b) # Distributive f => Closed (Star f) Instance detailsDefined in Data.Profunctor.Closed Methodsclosed :: Star f a b -> Star f (x -> a) (x -> b) # Functor f => Closed (Costar f) Instance detailsDefined in Data.Profunctor.Closed Methodsclosed :: Costar f a b -> Costar f (x -> a) (x -> b) # Closed (Tagged :: Type -> Type -> Type) Instance detailsDefined in Data.Profunctor.Closed Methodsclosed :: Tagged a b -> Tagged (x -> a) (x -> b) # Source # Instance detailsDefined in Data.Profunctor.Optic.Carrier Methodsclosed :: Conjoin j a b -> Conjoin j (x -> a) (x -> b) # Closed ((->) :: Type -> Type -> Type) Instance detailsDefined in Data.Profunctor.Closed Methodsclosed :: (a -> b) -> (x -> a) -> (x -> b) # Functor f => Closed (Cokleisli f) Instance detailsDefined in Data.Profunctor.Closed Methodsclosed :: Cokleisli f a b -> Cokleisli f (x -> a) (x -> b) # Source # Instance detailsDefined in Data.Profunctor.Optic.Carrier Methodsclosed :: Cotraversal0Rep a b a0 b0 -> Cotraversal0Rep a b (x -> a0) (x -> b0) # Closed (GrateRep a b) Source # Instance detailsDefined in Data.Profunctor.Optic.Carrier Methodsclosed :: GrateRep a b a0 b0 -> GrateRep a b (x -> a0) (x -> b0) # (Closed p, Closed q) => Closed (Sum p q) Instance detailsDefined in Data.Profunctor.Closed Methodsclosed :: Sum p q a b -> Sum p q (x -> a) (x -> b) # (Closed p, Closed q) => Closed (Product p q) Instance detailsDefined in Data.Profunctor.Closed Methodsclosed :: Product p q a b -> Product p q (x -> a) (x -> b) # (Functor f, Closed p) => Closed (Tannen f p) Instance detailsDefined in Data.Profunctor.Closed Methodsclosed :: Tannen f p a b -> Tannen f p (x -> a) (x -> b) #