profunctor-optics-0.0.1: 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 Lens' s a = Lens s s a a Source #

type Ixlens i s t a b = forall p. Strong p => IndexedOptic p i s t a b 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.

ilens :: (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 itraversal.

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:

ilensVl :: (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 Ixtraversal. Compare itraversalVl.

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

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

indexed :: Representable f => Eq (Rep f) => Rep f -> Lens' (f a) a Source #

Obtain a Lens from a representable functor.

>>> V2 3 1 ^. indexed E21
3
>>> V3 "foo" "bar" "baz" & indexed E32 .~ "bip"
V3 "foo" "bip" "baz"

Indexed optics

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

TODO: Document

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

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

TODO: Document

Primitive operators

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

Extract the two functions that characterize a Lens.

withLensVl :: Functor f => ALens s t a b -> (a -> f b) -> s -> f t Source #

Extract the higher order function that characterizes a Lens.

The lens laws can be stated in terms of withLens:

Identity:

withLensVl o Identity ≡ Identity

Composition:

Compose . fmap (withLensVl o f) . withLensVl o g ≡ withLensVl o (Compose . fmap f . g)

See Property.

withIxlens :: (Additive - 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.

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

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

Defined in Data.Profunctor.Optic.Index

Methods

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

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

Strong (OptionRep r) Source # 
Instance details

Defined in Data.Profunctor.Optic.Carrier

Methods

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

second' :: OptionRep r a b -> OptionRep 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 (AffineRep a b) Source # 
Instance details

Defined in Data.Profunctor.Optic.Carrier

Methods

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

second' :: AffineRep a b a0 b0 -> AffineRep 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 (IxlensRep i a b) Source # 
Instance details

Defined in Data.Profunctor.Optic.Carrier

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