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

Safe HaskellNone
LanguageHaskell2010

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:

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:

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"

voided :: Lens' Void a Source #

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 #

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

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 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 (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 (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) #

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) #

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) #

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 (Lin a) 
Instance details

Defined in Data.Semimodule.Free

Methods

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

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

Strong (Conjoin j) Source # 
Instance details

Defined in Data.Profunctor.Optic.Carrier

Methods

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

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

Strong (Fold0Rep r) Source # 
Instance details

Defined in Data.Profunctor.Optic.Carrier

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 (Traversal0Rep a b) Source # 
Instance details

Defined in Data.Profunctor.Optic.Carrier

Methods

first' :: 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 details

Defined in Data.Profunctor.Optic.Carrier

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) #

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.Types

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 p, Strong q) => Strong (Sum p q) 
Instance details

Defined in Data.Profunctor.Strong

Methods

first' :: 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 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 (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 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 (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 (Conjoin j) Source # 
Instance details

Defined in Data.Profunctor.Optic.Carrier

Methods

unfirst :: 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 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.Carrier

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.Types

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 (Sum p q) 
Instance details

Defined in Data.Profunctor.Strong

Methods

unfirst :: 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 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 #

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) #

Instances
(Distributive f, Monad f) => Closed (Kleisli f) 
Instance details

Defined in Data.Profunctor.Closed

Methods

closed :: Kleisli f a b -> Kleisli f (x -> a) (x -> b) #

Closed p => Closed (Yoneda p) 
Instance details

Defined in Data.Profunctor.Yoneda

Methods

closed :: Yoneda p a b -> Yoneda p (x -> a) (x -> b) #

Closed p => Closed (Coyoneda p) 
Instance details

Defined in Data.Profunctor.Yoneda

Methods

closed :: Coyoneda p a b -> Coyoneda p (x -> a) (x -> b) #

Profunctor p => Closed (Closure p) 
Instance details

Defined in Data.Profunctor.Closed

Methods

closed :: Closure p a b -> Closure p (x -> a) (x -> b) #

Closed (Environment p) 
Instance details

Defined in Data.Profunctor.Closed

Methods

closed :: Environment p a b -> Environment p (x -> a) (x -> b) #

Distributive f => Closed (Star f) 
Instance details

Defined in Data.Profunctor.Closed

Methods

closed :: Star f a b -> Star f (x -> a) (x -> b) #

Functor f => Closed (Costar f) 
Instance details

Defined in Data.Profunctor.Closed

Methods

closed :: Costar f a b -> Costar f (x -> a) (x -> b) #

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

Defined in Data.Profunctor.Closed

Methods

closed :: Tagged a b -> Tagged (x -> a) (x -> b) #

Closed (Conjoin j) Source # 
Instance details

Defined in Data.Profunctor.Optic.Carrier

Methods

closed :: Conjoin j a b -> Conjoin j (x -> a) (x -> b) #

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

Defined in Data.Profunctor.Closed

Methods

closed :: (a -> b) -> (x -> a) -> (x -> b) #

Functor f => Closed (Cokleisli f) 
Instance details

Defined in Data.Profunctor.Closed

Methods

closed :: Cokleisli f a b -> Cokleisli f (x -> a) (x -> b) #

Closed (Cotraversal0Rep a b) Source # 
Instance details

Defined in Data.Profunctor.Optic.Carrier

Methods

closed :: Cotraversal0Rep a b a0 b0 -> Cotraversal0Rep a b (x -> a0) (x -> b0) #

Closed (GrateRep a b) Source # 
Instance details

Defined in Data.Profunctor.Optic.Carrier

Methods

closed :: GrateRep a b a0 b0 -> GrateRep a b (x -> a0) (x -> b0) #

(Closed p, Closed q) => Closed (Sum p q) 
Instance details

Defined in Data.Profunctor.Closed

Methods

closed :: Sum p q a b -> Sum p q (x -> a) (x -> b) #

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

Defined in Data.Profunctor.Closed

Methods

closed :: Product p q a b -> Product p q (x -> a) (x -> b) #

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

Defined in Data.Profunctor.Closed

Methods

closed :: Tannen f p a b -> Tannen f p (x -> a) (x -> b) #