lens-family-simple-0.1.0.0: simplified import of lens family

Safe HaskellNone
LanguageHaskell2010

Lens.Simple

Contents

Synopsis

Stock Lenses

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

_1 :: Lens (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'

_2 :: Lens (a, b) (a, b') b b'

Lens on the second element of a pair.

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

chosen :: Lens (Either a a) (Either b b) a b

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

ix :: (Eq k, Functor f) => k -> LensLike' f (k -> v) v

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

Lens on a given point of a function.

at :: (Ord k, Functor f) => k -> LensLike' f (Map k v) (Maybe v)

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

Lens on a given point of a Map.

intAt :: Functor f => Int -> LensLike' f (IntMap v) (Maybe v)

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

Lens on a given point of a IntMap.

contains :: (Ord k, Functor f) => k -> LensLike' f (Set k) Bool

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

Lens on a given point of a Set.

intContains :: Functor f => Int -> LensLike' f IntSet Bool

intContains :: Int -> Lens' IntSet.IntSet Bool

Lens on a given point of a IntSet.

Stock Traversals

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

both :: Traversal (a,a) (b,b) a b

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

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

_Left :: Traversal (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'

_Right :: Traversal (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'

_Just :: Traversal (Maybe a) (Maybe a') a a'

Traversal on the Just element of a Maybe.

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

_Nothing :: Traversal' (Maybe a) ()

Traversal on the Nothing element of a Maybe.

ignored :: Applicative f => null -> a -> f a

ignored :: Traversal a a b b'

The empty traversal on any type.

Basic lens combinators

to :: Phantom f => (a -> b) -> LensLike f a a' b b'

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

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

folding :: (Foldable g, Phantom f, Applicative f) => (a -> g b) -> LensLike f a a' b b'

folding :: (a -> [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.

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 -> FoldLike [b] a a' b b' -> [b] infixl 8

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

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

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

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

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

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

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

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

allOf :: FoldLike All a a' b b' -> (b -> Bool) -> a -> Bool

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

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

anyOf :: FoldLike Any a a' b b' -> (b -> Bool) -> a -> Bool

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

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

firstOf :: FoldLike (First b) a a' b b' -> a -> Maybe b

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 :: FoldLike (Last b) a a' b b' -> a -> Maybe b

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 => FoldLike (Sum b) a a' b b' -> a -> b

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

Returns the sum of all the referenced values.

productOf :: Num b => FoldLike (Product b) a a' b b' -> a -> b

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

Returns the product of all the referenced values.

lengthOf :: Num r => FoldLike (Sum r) a a' b b' -> a -> r

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

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

nullOf :: FoldLike All a a' b b' -> a -> Bool

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.

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

Demote a setter to a semantic editor combinator.

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

Modify all referenced fields.

set :: ASetter a a' b b' -> b' -> a -> a'

Set all referenced fields to the given value.

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

Set all referenced fields to the given value.

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

A flipped version of ($).

Pseudo-imperatives

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

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

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

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

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

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

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

Monoidally append a value to all referenced fields.

State related combinators

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, Moand 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 :: Monad m => FoldLike b a a' b b' -> StateT a m b

use :: Monad m => Getter a a' b b' -> StateT a m b

Retrieve a field of the state

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

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

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

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

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

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

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

uses l f = f <$> use l

(%=) :: Monad m => ASetter a a b b' -> (b -> b') -> StateT a m () infix 4

Modify a field of the state.

assign :: Monad m => ASetter a a b b' -> b' -> StateT a m ()

Set a field of the state.

(.=) :: Monad m => ASetter a a b b' -> b' -> StateT a m () infix 4

Set a field of the state.

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

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

Modify a field of the state while returning another value.

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

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

(<~) :: Monad m => ASetter a a b b' -> StateT a m b' -> StateT a m () infixr 2

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

Compound state assignments

(+=) :: (Monad m, Num b) => ASetter' a b -> b -> StateT a m () infixr 4

(-=) :: (Monad m, Num b) => ASetter' a b -> b -> StateT a m () infixr 4

(*=) :: (Monad m, Num b) => ASetter' a b -> b -> StateT a m () infixr 4

(//=) :: (Monad m, Fractional b) => ASetter' a b -> b -> StateT a m () infixr 4

(&&=) :: Monad m => ASetter' a Bool -> Bool -> StateT a m () infixr 4

(||=) :: Monad m => ASetter' a Bool -> Bool -> StateT a m () infixr 4

(<>=) :: (Monoid o, Monad m) => ASetter' a o -> o -> StateT a m () infixr 4

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

Stock Semantic Editor Combinators

mapped :: (Identical f, Functor g) => LensLike f (g a) (g a') a a'

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

An SEC referencing the parameter of a functor.

Lens formers

lens

Arguments

:: forall (f :: * -> *). Functor f 
=> (a -> b)

getter

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

setter

-> LensLike f a a' b b' 
lens :: (a -> b) -> (a -> b' -> a') -> 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

:: forall (f :: * -> *). Functor f 
=> (a -> b)

yin

-> (b' -> a')

yang

-> LensLike f a a' b b' 
iso :: (a -> b) -> (b' -> a') -> 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

setting

Arguments

:: forall (f :: * -> *). Identical f 
=> ((b -> b') -> a -> a')

sec (semantic editor combinator)

-> LensLike f 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.Family.

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

Combining Combinators

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)

Types

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

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

type ASetter' a b = LensLike' Identity a b

class Functor f => Phantom f

Minimal complete definition

coerce

data Constant a b :: * -> * -> *

Constant functor.

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

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:

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

If f is also a Monad, it should satisfy

(which implies that pure and <*> satisfy the applicative functor laws).

Minimal complete definition

pure, (<*>)

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

Minimal complete definition

foldMap | foldr

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.

Minimal complete definition

mempty, mappend

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) 
Monoid a => Monoid (Const a b) 
Monoid (Proxy * s) 
Ord k => Monoid (Map k v) 
Typeable (* -> Constraint) Monoid 
(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

Alternative f => Alternative (Backwards f)

Try alternatives in the same order as f.

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.

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

data All :: *

Boolean monoid under conjunction.

Instances

Bounded All 
Eq All 
Ord All 
Read All 
Show All 
Generic All 
Monoid All 
type Rep All = D1 D1All (C1 C1_0All (S1 S1_0_0All (Rec0 Bool))) 

data Any :: *

Boolean monoid under disjunction.

Instances

Bounded Any 
Eq Any 
Ord Any 
Read Any 
Show Any 
Generic Any 
Monoid Any 
type Rep Any = D1 D1Any (C1 C1_0Any (S1 S1_0_0Any (Rec0 Bool))) 

data First a :: * -> *

Maybe monoid returning the leftmost non-Nothing value.

Instances

Generic1 First 
Eq a => Eq (First a) 
Ord a => Ord (First a) 
Read a => Read (First a) 
Show a => Show (First a) 
Generic (First a) 
Monoid (First a) 
type Rep1 First = D1 D1First (C1 C1_0First (S1 S1_0_0First (Rec1 Maybe))) 
type Rep (First a) = D1 D1First (C1 C1_0First (S1 S1_0_0First (Rec0 (Maybe a)))) 

data Last a :: * -> *

Maybe monoid returning the rightmost non-Nothing value.

Instances

Generic1 Last 
Eq a => Eq (Last a) 
Ord a => Ord (Last a) 
Read a => Read (Last a) 
Show a => Show (Last a) 
Generic (Last a) 
Monoid (Last a) 
type Rep1 Last = D1 D1Last (C1 C1_0Last (S1 S1_0_0Last (Rec1 Maybe))) 
type Rep (Last a) = D1 D1Last (C1 C1_0Last (S1 S1_0_0Last (Rec0 (Maybe a)))) 

data Sum a :: * -> *

Monoid under addition.

Instances

Generic1 Sum 
Bounded a => Bounded (Sum a) 
Eq a => Eq (Sum a) 
Num a => Num (Sum a) 
Ord a => Ord (Sum a) 
Read a => Read (Sum a) 
Show a => Show (Sum a) 
Generic (Sum a) 
Num a => Monoid (Sum a) 
type Rep1 Sum = D1 D1Sum (C1 C1_0Sum (S1 S1_0_0Sum Par1)) 
type Rep (Sum a) = D1 D1Sum (C1 C1_0Sum (S1 S1_0_0Sum (Rec0 a))) 

data Product a :: * -> *

Monoid under multiplication.

Instances

Generic1 Product 
Bounded a => Bounded (Product a) 
Eq a => Eq (Product a) 
Num a => Num (Product a) 
Ord a => Ord (Product a) 
Read a => Read (Product a) 
Show a => Show (Product a) 
Generic (Product a) 
Num a => Monoid (Product a) 
type Rep1 Product = D1 D1Product (C1 C1_0Product (S1 S1_0_0Product Par1)) 
type Rep (Product a) = D1 D1Product (C1 C1_0Product (S1 S1_0_0Product (Rec0 a))) 

data StateT s m a :: * -> (* -> *) -> * -> *

A state transformer monad parameterized by:

  • s - The state.
  • m - The inner monad.

The return function leaves the state unchanged, while >>= uses the final state of the first computation as the initial state of the second.

Instances

MonadTrans (StateT s) 
(Functor m, MonadPlus m) => Alternative (StateT s m) 
Monad m => Monad (StateT s m) 
Functor m => Functor (StateT s m) 
MonadFix m => MonadFix (StateT s m) 
MonadPlus m => MonadPlus (StateT s m) 
(Functor m, Monad m) => Applicative (StateT s m) 
MonadIO m => MonadIO (StateT s m) 

type Writer w = WriterT w Identity

A writer monad parameterized by the type w of output to accumulate.

The return function produces the output mempty, while >>= combines the outputs of the subcomputations using mappend.