lens-simple-0.1.0.9: simplified import of elementary lens-family combinators

Safe HaskellNone
LanguageHaskell2010

Lens.Simple

Contents

Synopsis

The fundamental lens operations: view, set and over

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.

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

Set all referenced fields to the given value.

over :: ASetter a a' b b' -> (b -> b') -> a -> a'

Demote a setter to a semantic editor combinator.

Stock lenses (_1, _2) and mere traversals (_Left, _Right, _Just, etc.) for simple Prelude types

_1 :: Functor f => LensLike f (a, b) (a', b) a a'

Lens on the first element of a pair.

_2 :: Functor f => LensLike f (a, b) (a, b') b b'

Lens on the second element of a pair.

_Left :: Applicative f => LensLike f (Either a b) (Either a' b) a a'

Traversal on the Left element of an Either.

_Right :: Applicative f => LensLike f (Either a b) (Either a b') b b'

Traversal on the Right element of an Either.

_Just :: Applicative f => LensLike f (Maybe a) (Maybe a') a a'

Traversal on the Just element of a Maybe.

_Nothing :: Applicative f => LensLike' f (Maybe a) ()

Traversal on the Nothing element of a Maybe.

both :: Applicative f => LensLike f (a, a) (b, b) a b

Traversals on both elements of a pair (a,a).

LensLike and important strength-expressing synonyms, from the all-powerful Lens downward

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

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

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

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

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

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

type SetterLike a a' b b' = LensLike Identity a a' b b' Source

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

Support for defining lenses and weaker 'optics' (see also the TH incantations below )

lens

Arguments

:: (a -> b)

getter

-> (a -> b' -> a')

setter

-> Lens a a' b b' 

Build a lens from a getter and setter families.

Caution: In order for the generated lens family to be well-defined, you must ensure that the three lens laws hold:

  • getter (setter a b) === b
  • setter a (getter a) === a
  • setter (setter a b1) b2) === setter a b2

iso

Arguments

:: (a -> b)

yin

-> (b' -> a')

yang

-> Lens a a' b b' 

Build a lens from isomorphism families.

Caution: In order for the generated lens family to be well-defined, you must ensure that the two isomorphism laws hold:

  • yin . yang === id
  • yang . yin === id

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

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

setting

Arguments

:: ((b -> b') -> a -> a')

sec (semantic editor combinator)

-> Setter a a' b b' 

setting promotes a "semantic editor combinator" to a modify-only lens. To demote a lens to a semantic edit combinator, use the section (l %~) or over l from Lens.Family2.

>>> setting map . fstL %~ length $ [("The",0),("quick",1),("brown",1),("fox",2)]
[(3,0),(5,1),(5,1),(3,2)]

Caution: In order for the generated setter family to be well-defined, you must ensure that the two functors laws hold:

  • sec id === id
  • sec f . sec g === sec (f . g)

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

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.

Operator equivalents for common lens-applying operators: particularly (^.), (.~) and (%~) (i.e. view, set and over)

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

(^.) :: 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.

(%~) :: Setter a a' b b' -> (b -> b') -> a -> a' infixr 4

Modify all referenced fields.

(.~) :: Setter a a' b b' -> b' -> a -> a' infixr 4

Set all referenced fields to the given value.

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

& is a reverse application operator. This provides notational convenience. Its precedence is one higher than that of the forward application operator $, which allows & to be nested in $.

Since: 4.8.0.0

(??) :: Functor f => f (a -> b) -> a -> f b infixl 1 Source

Generalized infix flip, replicating Control.Lens.Lens.??

>>> execStateT ?? (0,"") $ do _1 += 1; _1 += 1; _2 <>= "hello"
(2,"hello")

(?~) :: Setter a a' b (Maybe b') -> b' -> a -> a' Source

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

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

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

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

Basic state-related combinators: zoom, use, assign/(.=) etc.

zoom :: Monad m => LensLike' (Zooming m c) a b -> StateT b m c -> StateT a m c

zoom :: Monad m => Lens' a b -> StateT b m c -> StateT a m c

Lift a stateful operation on a field to a stateful operation on the whole state. This is a good way to call a "subroutine" that only needs access to part of the state.

zoom :: (Monoid c, Monad m) => Traversal' a b -> StateT b m c -> StateT a m c

Run the "subroutine" on each element of the traversal in turn and mconcat all the results together.

zoom :: Monad m => Traversal' a b -> StateT b m () -> StateT a m ()

Run the "subroutine" on each element the traversal in turn.

use :: MonadState a m => FoldLike b a a' b b' -> m b

use :: MonadState a m => Getter a a' b b' -> m b

Retrieve a field of the state

use :: (Monoid b, MonadState a m) => Fold a a' b b' -> m b

Retrieve a monoidal summary of all the referenced fields from the state

uses :: MonadState a m => FoldLike r a a' b b' -> (b -> r) -> m r

uses :: (MonadState a m, Monoid r) => Fold a a' b b' -> (b -> r) -> m r

Retrieve all the referenced fields from the state and foldMap the results together with f :: b -> r.

uses :: MonadState a m => Getter a a' b b' -> (b -> r) -> m r

Retrieve a field of the state and pass it through the function f :: b -> r.

uses l f = f <$> use l

assign :: MonadState a m => Setter a a b b' -> b' -> m ()

Set a field of the state.

Convenient state-related operators

(%=) :: MonadState a m => Setter a a b b' -> (b -> b') -> m () infix 4

Modify a field of the state.

(.=) :: MonadState a m => Setter a a b b' -> b' -> m () infix 4

Set a field of the state.

(%%=) :: MonadState a m => LensLike (Writer c) a a b b' -> (b -> (c, b')) -> m c infix 4

(%%=) :: MonadState a m => Lens a a b b' -> (b -> (c, b')) -> m c

Modify a field of the state while returning another value.

(%%=) :: (MonadState a m, Monoid c) => Traversal a a b b' -> (b -> (c, b')) -> m c

Modify each field of the state and return the mconcat of the other values.

(<~) :: MonadState a m => Setter a a b b' -> m b' -> m () infixr 2

Set a field of the state using the result of executing a stateful command.

Pseudo-imperatives

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

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

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

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

(&&~) :: Setter' a Bool -> Bool -> a -> a infixr 4

(||~) :: Setter' a Bool -> Bool -> a -> a infixr 4

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

Monoidally append a value to all referenced fields.

Corresponding state-related imperatives

(+=) :: (MonadState a m, Num b) => Setter' a b -> b -> m () infixr 4

(-=) :: (MonadState a m, Num b) => Setter' a b -> b -> m () infixr 4

(*=) :: (MonadState a m, Num b) => Setter' a b -> b -> m () infixr 4

(//=) :: (MonadState a m, Fractional b) => Setter' a b -> b -> m () infixr 4

(&&=) :: MonadState a m => Setter' a Bool -> Bool -> m () infixr 4

(||=) :: MonadState a m => Setter' a Bool -> Bool -> m () infixr 4

(<>=) :: (Monoid o, MonadState a m) => Setter' a o -> o -> m () infixr 4

Monoidally append a value to all referenced fields of the state.

More stock lenses

chosen :: Functor f => LensLike f (Either a a) (Either b b) a b

Lens on the Left or Right element of an (Either a a).

ix :: Eq k => k -> Lens' (k -> v) v

Lens on a given point of a function.

at :: Ord k => k -> Lens' (Map k v) (Maybe v)

Lens on a given point of a Map.

intAt :: Int -> Lens' (IntMap v) (Maybe v)

Lens on a given point of a IntMap.

contains :: Ord k => k -> Lens' (Set k) Bool

Lens on a given point of a Set.

intContains :: Int -> Lens' IntSet Bool

Lens on a given point of a IntSet.

More stock traversals

ignored :: Applicative f => LensLike f a a b b'

The empty traveral on any type.

More stock setters

mapped :: Functor f => Setter (f a) (f a') a a'

An SEC referencing the parameter of a functor.

Other combinators

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)

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

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

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

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

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

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

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

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 b

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

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

Returns the sum of all the referenced values.

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

Returns the product of all the referenced values.

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

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

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

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.

choosing :: Functor f => LensLike f a a' c c' -> LensLike f b b' c c' -> LensLike f (Either a b) (Either a' b') c c'

choosing :: Lens a a' c c' -> Lens b b' c c' -> Lens (Either a b) (Either a' b') c c'
choosing :: Traversal a a' c c' -> Traversal b b' c c' -> Traversal (Either a b) (Either a' b') c c'
choosing :: Getter a a' c c' -> Getter b b' c c' -> Getter (Either a b) (Either a' b') c c'
choosing :: Fold a a' c c' -> Fold b b' c c' -> Fold (Either a b) (Either a' b') c c'
choosing :: Setter a a' c c' -> Setter b b' c c' -> Setter (Either a b) (Either a' b') c c'

Given two lens/traversal/getter/fold/setter families with the same substructure, make a new lens/traversal/getter/fold/setter on Either.

alongside :: Functor f => LensLike (AlongsideLeft f b2') a1 a1' b1 b1' -> LensLike (AlongsideRight f a1') a2 a2' b2 b2' -> LensLike f (a1, a2) (a1', a2') (b1, b2) (b1', b2')

alongside :: Lens a1 a1' b1 b1' -> Lens a2 a2' b2 b2' -> Lens (a1, a2) (a1', a2') (b1, b2) (b1', b2')
alongside :: Getter a1 a1' b1 b1' -> Getter a2 a2' b2 b2' -> Getter (a1, a2) (a1', a2') (b1, b2) (b1', b2')

Given two lens/getter families, make a new lens/getter on their product.

beside :: Applicative f => LensLike f a a' c c' -> LensLike f b b' c c' -> LensLike f (a, b) (a', b') c c'

beside :: Traversal a a' c c' -> Traversal b' b' c c' -> Traversal (a,b) (a',b') c c'
beside :: Fold a a' c c' -> Fold b' b' c c' -> Fold (a,b) (a',b') c c'
beside :: Setter a a' c c' -> Setter b' b' c c' -> Setter (a,b) (a',b') c c'

Given two traversals/folds/setters referencing a type c, create a traversal/fold/setter on the pair referencing c.

TH incantations

makeLenses :: Name -> Q [Dec]

Derive lenses for the record selectors in a single-constructor data declaration, or for the record selector in a newtype declaration. Lenses will only be generated for record fields which are prefixed with an underscore.

Example usage:

$(makeLenses ''Foo)

makeTraversals :: Name -> Q [Dec]

Derive traversals for each constructor in a data or newtype declaration, Traversals will be named by prefixing the constructor name with an underscore.

Example usage:

$(makeTraversals ''Foo)

makeLensesBy :: (String -> Maybe String) -> Name -> Q [Dec]

Derive lenses with the provided name transformation and filtering function. Produce Just lensName to generate a lens of the resultant name, or Nothing to not generate a lens for the input record name.

Example usage:

$(makeLensesBy (\n -> Just (n ++ "L")) ''Foo)

makeLensesFor :: [(String, String)] -> Name -> Q [Dec]

Derive lenses, specifying explicit pairings of (fieldName, lensName).

Example usage:

$(makeLensesFor [("_foo", "fooLens"), ("bar", "lbar")] ''Foo)

Other type synonyms

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

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

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

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

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

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

type ASetter' a b = LensLike' Identity a b

type ASetter a a' b b' = LensLike Identity a a' b b'

Helper classes

class Applicative f => Identical f

Minimal complete definition

extract

class Functor f => Phantom f

Minimal complete definition

coerce

Helper types

data AlongsideLeft f b a :: (* -> *) -> * -> * -> *

Instances

data AlongsideRight f a b :: (* -> *) -> * -> * -> *

Instances

data Zooming m c a :: (* -> *) -> * -> * -> *

Instances

Monad m => Functor (Zooming m c) 
(Monoid c, Monad m) => Applicative (Zooming m c) 

Re-exports

newtype Constant a b :: * -> * -> *

Constant functor.

Constructors

Constant 

Fields

getConstant :: a
 

Instances

Functor (Constant a) 
Monoid a => Applicative (Constant a) 
Foldable (Constant a) 
Traversable (Constant a) 
Phantom (Constant a) 
Eq a => Eq1 (Constant a) 
Ord a => Ord1 (Constant a) 
Read a => Read1 (Constant a) 
Show a => Show1 (Constant a) 
Eq a => Eq (Constant a b) 
Ord a => Ord (Constant a b) 
Read a => Read (Constant a b) 
Show a => Show (Constant a b) 

newtype Identity a :: * -> *

Identity functor and monad. (a non-strict monad)

Since: 4.8.0.0

Constructors

Identity 

Fields

runIdentity :: a
 

Instances

Monad Identity 
Functor Identity 
MonadFix Identity 
Applicative Identity 
Foldable Identity 
Traversable Identity 
Generic1 Identity 
MonadZip Identity 
Identical Identity 
Eq a => Eq (Identity a) 
Data a => Data (Identity a) 
Ord a => Ord (Identity a) 
Read a => Read (Identity a)

This instance would be equivalent to the derived instances of the Identity newtype if the runIdentity field were removed

Show a => Show (Identity a)

This instance would be equivalent to the derived instances of the Identity newtype if the runIdentity field were removed

Generic (Identity a) 
type Rep1 Identity = D1 D1Identity (C1 C1_0Identity (S1 S1_0_0Identity Par1)) 
type Rep (Identity a) = D1 D1Identity (C1 C1_0Identity (S1 S1_0_0Identity (Rec0 a))) 

class Monoid a where

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.

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.

Minimal complete definition

mempty, mappend

Methods

mempty :: a

Identity of mappend

mappend :: a -> a -> a

An associative operation

mconcat :: [a] -> a

Fold a list using the monoid. For most types, the default definition for mconcat will be used, but the function is included in the class definition so that an optimized version can be provided for specific types.

Instances

Monoid Ordering 
Monoid () 
Monoid All 
Monoid Any 
Monoid IntSet 
Monoid Doc 
Monoid [a] 
Ord a => Monoid (Max a) 
Ord a => Monoid (Min 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) 
Monoid (Proxy k s) 
Ord k => Monoid (Map k v) 
(Monoid a, Monoid b, Monoid c) => Monoid (a, b, c) 
Alternative f => Monoid (Alt * f a) 
(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) 

(<>) :: Monoid m => m -> m -> m infixr 6

An infix synonym for mappend.

Since: 4.5.0.0