lens-family-1.1.0: Lens Families

Safe HaskellSafe-Inferred

Lens.Family2

Contents

Description

This is the main module for end-users of lens-families. If you are not building your own lenses or traversals, but just using functional references made by others, this is the only module you need.

Synopsis

Lenses

This module provides ^. for accessing fields and .~ and %~ for setting and modifying fields. Lenses are composed with . from the Prelude and id is the identity lens.

Lens composition in this library enjoys the following identities.

  • x^.l1.l2 === x^.l1^.l2
  • l1.l2 %~ f === l1 %~ l2 %~ f

The identity lens behaves as follows.

  • x^.id === x
  • id %~ f === f

The & operator, allows for a convenient way to sequence record updating:

record & l1 .~ value1 & l2 .~ value2

Lenses are implemented in van Laarhoven style. Lenses have type Functor f => (b -> f b) -> a -> f a and lens families have type Functor f => (b i -> f (b j)) -> a i -> f (a j).

Keep in mind that lenses and lens families can be used directly for functorial updates. For example, _2 id gives you strength.

 _2 id :: Functor f => (a, f b) -> f (a, b)

Here is an example of code that uses the Maybe functor to preserves sharing during update when possible.

 -- | 'sharedUpdate' returns the *identical* object if the update doesn't change anything.
 -- This is useful for preserving sharing.
 sharedUpdate :: Eq b => LensLike' Maybe a b -> (b -> b) -> a -> a
 sharedUpdate l f a = fromMaybe a (l f' a)
  where
   f' b | fb == b  = Nothing
        | otherwise = Just fb
    where
     fb = f b

Traversals

^. can be used with traversals to access monoidal fields. The result will be a mconcat of all the fields referenced. The various fooOf functions can be used to access different monoidal summaries of some kinds of values.

^? can be used to access the first value of a traversal. Nothing is returned when the traversal has no references.

^.. can be used with a traversals and will return a list of all fields referenced.

When .~ is used with a traversal, all referenced fields will be set to the same value, and when %~ is used with a traversal, all referenced fields will be modified with the same function.

Like lenses, traversals can be composed with ., and because every lens is automatically a traversal, lenses and traversals can be composed with . yielding a traversal.

Traversals are implemented in van Laarhoven style. Traversals have type Applicative f => (b -> f b) -> a -> f a and traversal families have type Applicative f => (b i -> f (b j)) -> a i -> f (a j).

For stock lenses and traversals, see Lens.Family2.Stock.

To build your own lenses and traversals, see Lens.Family2.Unchecked.

References:

Documentation

to :: (a -> b) -> Getter a a' b b'Source

to promotes a projection function to a read-only lens called a getter. To demote a lens to a projection function, use the section (^.l) or view l.

>>> (3 :+ 4, "example")^._1.to(abs)
5.0 :+ 0.0

view :: FoldLike b a a' b b' -> a -> b

 view :: Getter a a' b b' -> a -> b

Demote a lens or getter to a projection function.

 view :: Monoid b => Fold a a' b b' -> a -> b

Returns the monoidal summary of a traversal or a fold.

(^.) :: a -> FoldLike b a a' b b' -> b

 (^.) :: a -> Getter a a' b b' -> b

Access the value referenced by a getter or lens.

 (^.) :: Monoid b => a -> Fold a a' b b' -> b

Access the monoidal summary referenced by a getter or lens.

folding :: Foldable f => (a -> f b) -> Fold a a' b b'Source

folding promotes a "toList" function to a read-only traversal called a fold.

To demote a traversal or fold to a "toList" function use the section (^..l) or toListOf l.

views :: FoldLike r a a' b b' -> (b -> r) -> a -> r

 views :: Monoid r => Fold a a' b b' -> (b -> r) -> a -> r

Given a fold or traversal, return the foldMap of all the values using the given function.

 views :: Getter a a' b b' -> (b -> r) -> a -> r

views is not particularly useful for getters or lenses, but given a getter or lens, it returns the referenced value passed through the given function.

 views l f a = f (view l a)

(^..) :: a -> Fold a a' b b' -> [b]Source

Returns a list of all of the referenced values in order.

(^?) :: a -> Fold a a' b b' -> Maybe bSource

Returns Just the first referenced value. Returns Nothing if there are no referenced values.

toListOf :: Fold a a' b b' -> a -> [b]Source

Returns a list of all of the referenced values in order.

allOf :: Fold a a' b b' -> (b -> Bool) -> a -> BoolSource

Returns true if all of the referenced values satisfy the given predicate.

anyOf :: Fold a a' b b' -> (b -> Bool) -> a -> BoolSource

Returns true if any of the referenced values satisfy the given predicate.

firstOf :: Fold a a' b b' -> a -> Maybe bSource

Returns Just the first referenced value. Returns Nothing if there are no referenced values. See ^? for an infix version of firstOf

lastOf :: Fold a a' b b' -> a -> Maybe bSource

Returns Just the last referenced value. Returns Nothing if there are no referenced values.

sumOf :: Num b => Fold a a' b b' -> a -> bSource

Returns the sum of all the referenced values.

productOf :: Num b => Fold a a' b b' -> a -> bSource

Returns the product of all the referenced values.

lengthOf :: Num r => Fold a a' b b' -> a -> rSource

Counts the number of references in a traversal or fold for the input.

nullOf :: Fold a a' b b' -> a -> BoolSource

Returns true if the number of references in the input is zero.

backwards :: LensLike (Backwards f) a a' b b' -> LensLike f a a' b b'

 backwards :: Traversal a a' b b' -> Traversal a a' b b'
 backwards :: Fold a a' b b' -> Fold a a' b b'

Given a traversal or fold, reverse the order that elements are traversed.

 backwards :: Lens a a' b b' -> Lens a a' b b'
 backwards :: Getter a a' b b' -> Getter a a' b b'
 backwards :: Setter a a' b b' -> Setter a a' b b'

No effect on lenses, getters or setters.

over :: Setter a a' b b' -> (b -> b') -> a -> a'Source

Demote a setter to a semantic editor combinator.

(%~) :: Setter a a' b b' -> (b -> b') -> a -> a'Source

Modify all referenced fields.

set :: Setter a a' b b' -> b' -> a -> a'Source

Set all referenced fields to the given value.

(.~) :: Setter a a' b b' -> b' -> a -> a'Source

Set all referenced fields to the given value.

(&) :: a -> (a -> b) -> b

A flipped version of ($).

Pseudo-imperatives

(+~) :: Num b => Setter' a b -> b -> a -> aSource

(*~) :: Num b => Setter' a b -> b -> a -> aSource

(-~) :: Num b => Setter' a b -> b -> a -> aSource

(//~) :: Fractional b => Setter' a b -> b -> a -> aSource

(&&~) :: Setter' a Bool -> Bool -> a -> aSource

(||~) :: Setter' a Bool -> Bool -> a -> aSource

(<>~) :: Monoid o => Setter' a o -> o -> a -> aSource

Monoidally append a value to all referenced fields.

Types

type Lens a a' b b' = forall f. Functor f => LensLike f a a' b b'Source

type Lens' a b = forall f. Functor f => LensLike' f a bSource

type Traversal a a' b b' = forall f. Applicative f => LensLike f a a' b b'Source

type Traversal' a b = forall f. Applicative f => LensLike' f a bSource

type Setter a a' b b' = forall f. Identical f => LensLike f a a' b b'Source

type Setter' a b = forall f. Identical f => LensLike' f a bSource

type Getter a a' b b' = forall f. Phantom f => LensLike f a a' b b'Source

type Getter' a b = forall f. Phantom f => LensLike' f a bSource

type Fold a a' b b' = forall f. (Phantom f, Applicative f) => LensLike f a a' b b'Source

type Fold' a b = forall f. (Phantom f, Applicative f) => LensLike' f a bSource

type LensLike f a a' b b' = (b -> f b') -> a -> f a'

type LensLike' f a b = (b -> f b) -> a -> f a

type FoldLike r a a' b b' = LensLike (Constant r) a a' b b'

type FoldLike' r a b = LensLike' (Constant r) a b

data Constant a b

Constant functor.

class Functor f => Phantom f

Re-exports

class Functor f => Applicative f

A functor with application, providing operations to

  • embed pure expressions (pure), and
  • sequence computations and combine their results (<*>).

A minimal complete definition must include implementations of these functions satisfying the following laws:

identity
pure id <*> v = v
composition
pure (.) <*> u <*> v <*> w = u <*> (v <*> w)
homomorphism
pure f <*> pure x = pure (f x)
interchange
u <*> pure y = pure ($ y) <*> u

The other methods have the following default definitions, which may be overridden with equivalent specialized implementations:

      u *> v = pure (const id) <*> u <*> v
      u <* v = pure const <*> u <*> v

As a consequence of these laws, the Functor instance for f will satisfy

      fmap f x = pure f <*> x

If f is also a Monad, it should satisfy pure = return and (<*>) = ap (which implies that pure and <*> satisfy the applicative functor laws).

class Foldable t

Data structures that can be folded.

Minimal complete definition: foldMap or foldr.

For example, given a data type

 data Tree a = Empty | Leaf a | Node (Tree a) a (Tree a)

a suitable instance would be

 instance Foldable Tree where
    foldMap f Empty = mempty
    foldMap f (Leaf x) = f x
    foldMap f (Node l k r) = foldMap f l `mappend` f k `mappend` foldMap f r

This is suitable even for abstract types, as the monoid is assumed to satisfy the monoid laws. Alternatively, one could define foldr:

 instance Foldable Tree where
    foldr f z Empty = z
    foldr f z (Leaf x) = f x z
    foldr f z (Node l k r) = foldr f (f k (foldr f z r)) l

class Monoid a

The class of monoids (types with an associative binary operation that has an identity). Instances should satisfy the following laws:

  • mappend mempty x = x
  • mappend x mempty = x
  • mappend x (mappend y z) = mappend (mappend x y) z
  • mconcat = foldr mappend mempty

The method names refer to the monoid of lists under concatenation, but there are many other instances.

Minimal complete definition: mempty and mappend.

Some types can be viewed as a monoid in more than one way, e.g. both addition and multiplication on numbers. In such cases we often define newtypes and make those instances of Monoid, e.g. Sum and Product.

Instances

Monoid Ordering 
Monoid () 
Monoid All 
Monoid Any 
Monoid IntSet 
Monoid [a] 
Monoid a => Monoid (Dual a) 
Monoid (Endo a) 
Num a => Monoid (Sum a) 
Num a => Monoid (Product a) 
Monoid (First a) 
Monoid (Last a) 
Monoid a => Monoid (Maybe a)

Lift a semigroup into Maybe forming a Monoid according to http://en.wikipedia.org/wiki/Monoid: "Any semigroup S may be turned into a monoid simply by adjoining an element e not in S and defining e*e = e and e*s = s = s*e for all s ∈ S." Since there is no "Semigroup" typeclass providing just mappend, we use Monoid instead.

Monoid (IntMap a) 
Ord a => Monoid (Set a) 
Monoid b => Monoid (a -> b) 
(Monoid a, Monoid b) => Monoid (a, b) 
Ord k => Monoid (Map k v) 
(Monoid a, Monoid b, Monoid c) => Monoid (a, b, c) 
(Monoid a, Monoid b, Monoid c, Monoid d) => Monoid (a, b, c, d) 
(Monoid a, Monoid b, Monoid c, Monoid d, Monoid e) => Monoid (a, b, c, d, e) 

data Backwards f a

The same functor, but with an Applicative instance that performs actions in the reverse order.

Instances

Functor f => Functor (Backwards f)

Derived instance.

Applicative f => Applicative (Backwards f)

Apply f-actions in the reverse order.

Foldable f => Foldable (Backwards f)

Derived instance.

Traversable f => Traversable (Backwards f)

Derived instance.

Alternative f => Alternative (Backwards f)

Try alternatives in the same order as f.

Identical f => Identical (Backwards f) 
Phantom f => Phantom (Backwards f)