-- Hoogle documentation, generated by Haddock -- See Hoogle, http://www.haskell.org/hoogle/ -- | Lenses, Folds and Traversals -- -- This package comes "Batteries Included" with many useful lenses for -- the types commonly used from the Haskell Platform, and with tools for -- automatically generating lenses and isomorphisms for user-supplied -- data types. -- -- The combinators in Control.Lens provide a highly generic -- toolbox for composing families of getters, folds, isomorphisms, -- traversals, setters and lenses and their indexed variants. -- -- An overview, with a large number of examples can be found in the -- README. -- -- https://github.com/ekmett/lens#lens-lenses-folds-and-traversals -- -- More information on the care and feeding of lenses, including a brief -- tutorial and motivation for their types can be found on the lens wiki. -- -- https://github.com/ekmett/lens/wiki -- -- A small game that manages its state using lenses can be found in the -- example folder. -- -- https://github.com/ekmett/lens/blob/master/examples/Pong.hs -- -- Lenses, Folds and Traversals -- -- The core of this hierarchy looks like: -- -- -- You can compose any two elements of the hierarchy above using (.) from -- the Prelude, and you can use any element of the hierarchy as any type -- it links to above it. -- -- The result is their lowest upper bound in the hierarchy (or an error -- if that bound doesn't exist). -- -- For instance: -- --
-- data Foo a = Foo Int Int a ---- -- You can define lenses such as -- --
-- -- bar :: Simple Lens (Foo a) Int -- bar :: Functor f => (Int -> f Int) -> Foo a -> f (Foo a) -- bar f (Foo a b c) = fmap (\a' -> Foo a' b c) (f a) ---- --
-- -- baz :: Lens (Foo a) (Foo b) a b -- quux :: Functor f => (a -> f b) -> Foo a -> f (Foo b) -- quux f (Foo a b c) = fmap (Foo a b) (f c) ---- -- without the need to use any type that isn't already defined in the -- Prelude. -- -- And you can define a traversal of multiple fields with -- Control.Applicative.Applicative: -- --
-- -- traverseBarAndBaz :: Simple Traversal (Foo a) Int -- traverseBarAndBaz :: Applicative f => (Int -> f Int) -> Foo a -> f (Foo a) -- traverseBarAndBaz f (Foo a b c) = Foo <$> f a <*> f b <*> pure c ---- -- What is provided in this library is a number of stock lenses and -- traversals for common haskell types, a wide array of combinators for -- working them, and more exotic functionality, (e.g. getters, -- setters, indexed folds, isomorphisms). @package lens @version 3.2 -- | Infix type operators for simple lenses and traversals. module Control.Lens.Simple -- | This is a commonly used infix alias for a Simple -- Lens. type (:->) s a = forall f. Functor f => (a -> f a) -> s -> f s -- | This is a commonly-used infix alias for a Simple -- Traversal. type (:=>) s a = forall f. Applicative f => (a -> f a) -> s -> f s module Control.Lens.Isomorphic -- | Used to provide overloading of isomorphism application -- -- This is a Category with a canonical mapping to it from the -- category of isomorphisms over Haskell types. class Category k => Isomorphic k isomorphic :: Isomorphic k => (a -> b) -> (b -> a) -> k a b isomap :: Isomorphic k => ((a -> b) -> c -> d) -> ((b -> a) -> d -> c) -> k a b -> k c d -- | A concrete data type for isomorphisms. -- -- This lets you place an isomorphism inside a container without using -- ImpredicativeTypes. data Isomorphism a b Isomorphism :: (a -> b) -> (b -> a) -> Isomorphism a b -- | Invert an isomorphism. -- -- Note to compose an isomorphism and receive an isomorphism in turn -- you'll need to use Category -- --
-- from (from l) ≡ l ---- -- If you imported . from Control.Category, then: -- --
-- from l . from r ≡ from (r . l) --from :: Isomorphic k => Isomorphism a b -> k b a -- | Convert from an Isomorphism back to any Isomorphic -- value. -- -- This is useful when you need to store an isomoprhism as a data type -- inside a container and later reconstitute it as an overloaded -- function. via :: Isomorphic k => Isomorphism a b -> k a b instance Typeable2 Isomorphism instance Isomorphic Isomorphism instance Category Isomorphism instance Isomorphic (->) -- | These are some of the explicit Functor instances that leak into the -- type signatures of Control.Lens. You shouldn't need to import this -- module directly, unless you are coming up with a whole new kind of -- "Family" and need to add instances. module Control.Lens.Internal const# :: (a -> b) -> a -> Const b r getConst# :: (a -> Const b r) -> a -> b zipList# :: (a -> [b]) -> a -> ZipList b getZipList# :: (a -> ZipList b) -> a -> [b] wrapMonad# :: (a -> m b) -> a -> WrappedMonad m b unwrapMonad# :: (a -> WrappedMonad m b) -> a -> m b last# :: (a -> Maybe b) -> a -> Last b getLast# :: (a -> Last b) -> a -> Maybe b first# :: (a -> Maybe b) -> a -> First b getFirst# :: (a -> First b) -> a -> Maybe b product# :: (a -> b) -> a -> Product b getProduct# :: (a -> Product b) -> a -> b sum# :: (a -> b) -> a -> Sum b getSum# :: (a -> Sum b) -> a -> b any# :: (a -> Bool) -> a -> Any getAny# :: (a -> Any) -> a -> Bool all# :: (a -> Bool) -> a -> All getAll# :: (a -> All) -> a -> Bool dual# :: (a -> b) -> a -> Dual b getDual# :: (a -> Dual b) -> a -> b endo# :: (a -> b -> b) -> a -> Endo b appEndo# :: (a -> Endo b) -> a -> b -> b may# :: (a -> Maybe b) -> a -> May b getMay# :: (a -> May b) -> a -> Maybe b folding# :: (a -> f b) -> a -> Folding f b getFolding# :: (a -> Folding f b) -> a -> f b effect# :: (a -> m r) -> a -> Effect m r b getEffect# :: (a -> Effect m r b) -> a -> m r effectRWS# :: (a -> st -> m (s, st, w)) -> a -> EffectRWS w st m s b getEffectRWS# :: (a -> EffectRWS w st m s b) -> a -> st -> m (s, st, w) accessor# :: (a -> r) -> a -> Accessor r b runAccessor# :: (a -> Accessor r b) -> a -> r err# :: (a -> Either e b) -> a -> Err e b getErr# :: (a -> Err e b) -> a -> Either e b traversed# :: (a -> f ()) -> a -> Traversed f getTraversed# :: (a -> Traversed f) -> a -> f () sequenced# :: (a -> f ()) -> a -> Sequenced f getSequenced# :: (a -> Sequenced f) -> a -> f () focusing# :: (a -> m (s, b)) -> a -> Focusing m s b unfocusing# :: (a -> Focusing m s b) -> a -> m (s, b) focusingWith# :: (a -> m (s, b, w)) -> a -> FocusingWith w m s b unfocusingWith# :: (a -> FocusingWith w m s b) -> a -> m (s, b, w) focusingPlus# :: (a -> k (s, w) b) -> a -> FocusingPlus w k s b unfocusingPlus# :: (a -> FocusingPlus w k s b) -> a -> k (s, w) b focusingOn# :: (a -> k (f s) b) -> a -> FocusingOn f k s b unfocusingOn# :: (a -> FocusingOn f k s b) -> a -> k (f s) b focusingMay# :: (a -> k (May s) b) -> a -> FocusingMay k s b unfocusingMay# :: (a -> FocusingMay k s b) -> a -> k (May s) b focusingErr# :: (a -> k (Err e s) b) -> a -> FocusingErr e k s b unfocusingErr# :: (a -> FocusingErr e k s b) -> a -> k (Err e s) b bazaar# :: (forall f. Applicative f => a -> (c -> f d) -> f t) -> a -> Bazaar c d t runBazaar# :: Applicative f => (a -> Bazaar c d t) -> a -> (c -> f d) -> f t mutator# :: (a -> b) -> a -> Mutator b runMutator# :: (a -> Mutator b) -> a -> b backwards# :: (a -> f b) -> a -> Backwards f b forwards# :: (a -> Backwards f b) -> a -> f b -- | Used by Zoom to zoom into StateT newtype Focusing m s a Focusing :: m (s, a) -> Focusing m s a unfocusing :: Focusing m s a -> m (s, a) -- | Used by Zoom to zoom into RWST newtype FocusingWith w m s a FocusingWith :: m (s, a, w) -> FocusingWith w m s a unfocusingWith :: FocusingWith w m s a -> m (s, a, w) -- | Used by Zoom to zoom into WriterT. newtype FocusingPlus w k s a FocusingPlus :: k (s, w) a -> FocusingPlus w k s a unfocusingPlus :: FocusingPlus w k s a -> k (s, w) a -- | Used by Zoom to zoom into MaybeT or ListT newtype FocusingOn f k s a FocusingOn :: k (f s) a -> FocusingOn f k s a unfocusingOn :: FocusingOn f k s a -> k (f s) a -- | Make a monoid out of Maybe for error handling newtype May a May :: Maybe a -> May a getMay :: May a -> Maybe a -- | Used by Zoom to zoom into ErrorT newtype FocusingMay k s a FocusingMay :: k (May s) a -> FocusingMay k s a unfocusingMay :: FocusingMay k s a -> k (May s) a -- | Make a monoid out of Either for error handling newtype Err e a Err :: Either e a -> Err e a getErr :: Err e a -> Either e a -- | Used by Zoom to zoom into ErrorT newtype FocusingErr e k s a FocusingErr :: k (Err e s) a -> FocusingErr e k s a unfocusingErr :: FocusingErr e k s a -> k (Err e s) a -- | The result of Indexing data IndexingResult f a IndexingResult :: (f a) -> {-# UNPACK #-} !Int -> IndexingResult f a -- | Applicative composition of State Int with a -- Functor, used by indexed newtype Indexing f a Indexing :: (Int -> IndexingResult f a) -> Indexing f a runIndexing :: Indexing f a -> Int -> IndexingResult f a -- | Used internally by traverseOf_ and the like. newtype Traversed f Traversed :: f () -> Traversed f getTraversed :: Traversed f -> f () -- | Used internally by mapM_ and the like. newtype Sequenced m Sequenced :: m () -> Sequenced m getSequenced :: Sequenced m -> m () -- | Used for minimumOf data Min a NoMin :: Min a Min :: a -> Min a -- | Obtain the minimum. getMin :: Min a -> Maybe a -- | Used for maximumOf data Max a NoMax :: Max a Max :: a -> Max a -- | Obtain the maximum getMax :: Max a -> Maybe a -- | The indexed store can be used to characterize a Lens and is -- used by clone -- -- Context a b t is isomorphic to newtype Context a b -- t = Context { runContext :: forall f. Functor f => (a -> f b) -- -> f t }, and to exists s. (s, Lens s t a b). -- -- A Context is like a Lens that has already been applied -- to a some structure. data Context a b t Context :: (b -> t) -> a -> Context a b t -- | This is used to characterize a Traversal. -- -- a.k.a. indexed Cartesian store comonad, indexed Kleene store comonad, -- or an indexed FunList. -- -- http://twanvl.nl/blog/haskell/non-regular1 -- -- Bazaar a b t is isomorphic to data Bazaar a b t = -- Buy t | Trade (Bazaar a b (b -> t)) a, and to exists s. -- (s, Traversal s t a b). -- -- A Bazaar is like a Traversal that has already been -- applied to some structure. -- -- Where a Context a b t holds an a and a -- function from b to t, a Bazaar a b t -- holds N as and a function from N bs to t. -- -- Mnemonically, a Bazaar holds many stores and you can easily add -- more. -- -- This is a final encoding of Bazaar. newtype Bazaar a b t Bazaar :: (forall f. Applicative f => (a -> f b) -> f t) -> Bazaar a b t runBazaar :: Bazaar a b t -> forall f. Applicative f => (a -> f b) -> f t -- | Given an action to run for each matched pair, traverse a bazaar. -- --
-- bazaar :: Traversal (Bazaar a b t) t a b --bazaar :: Applicative f => (a -> f b) -> Bazaar a b t -> f t -- | Bazaar is an indexed Comonad. duplicateBazaar :: Bazaar a c t -> Bazaar a b (Bazaar b c t) -- | A trivial Bazaar. sell :: a -> Bazaar a b b -- | Wrap a monadic effect with a phantom type argument. newtype Effect m r a Effect :: m r -> Effect m r a getEffect :: Effect m r a -> m r -- | Wrap a monadic effect with a phantom type argument. Used when -- magnifying RWST. newtype EffectRWS w st m s a EffectRWS :: (st -> m (s, st, w)) -> EffectRWS w st m s a getEffectRWS :: EffectRWS w st m s a -> st -> m (s, st, w) -- | Generalizing Const so we can apply simple Applicative -- transformations to it and so we can get nicer error messages -- -- A Gettable Functor ignores its argument, which it -- carries solely as a phantom type parameter. -- -- To ensure this, an instance of Gettable is required to satisfy: -- --
-- id = fmap f = coerce --class Functor f => Gettable f coerce :: Gettable f => f a -> f b -- | Used instead of Const to report -- --
-- No instance of (Settable Accessor) ---- -- when the user attempts to misuse a Setter as a Getter, -- rather than a monolithic unification error. newtype Accessor r a Accessor :: r -> Accessor r a runAccessor :: Accessor r a -> r -- | An Effective Functor ignores its argument and is -- isomorphic to a monad wrapped around a value. -- -- That said, the monad is possibly rather unrelated to any -- Applicative structure. class (Monad m, Gettable f) => Effective m r f | f -> m r effective :: (Effective m r f, Isomorphic k) => k (m r) (f a) -- | A convenient antonym that is used internally. ineffective :: Effective m r f => Isomorphic k => k (f a) (m r) -- | A Monoid for a Gettable Applicative. newtype Folding f a Folding :: f a -> Folding f a getFolding :: Folding f a -> f a -- | The mempty equivalent for a Gettable Applicative -- Functor. noEffect :: (Applicative f, Gettable f) => f a -- | Anything Settable must be isomorphic to the Identity -- Functor. class Applicative f => Settable f where untainted# f = untainted . f untainted :: Settable f => f a -> a untainted# :: Settable f => (b -> f a) -> b -> a -- | Mutator is just a renamed Identity functor to give -- better error messages when someone attempts to use a getter as a -- setter. -- -- Most user code will never need to see this type. newtype Mutator a Mutator :: a -> Mutator a runMutator :: Mutator a -> a -- | A basic non-empty list zipper -- -- All combinators assume the invariant that the length stored matches -- the number of elements in list of items to the left, and the list of -- items to the left is stored reversed. data Level a Level :: {-# UNPACK #-} !Int -> [a] -> a -> [a] -> Level a -- | How many entries are there in this level? levelWidth :: Level a -> Int -- | Pull the non-emtpy list zipper left one entry leftLevel :: Level a -> Maybe (Level a) -- | Pull the non-empty list zipper left one entry, stopping at the first -- entry. left1Level :: Level a -> Level a -- | Pull the non-empty list zipper all the way to the left. leftmostLevel :: Level a -> Level a -- | Pul the non-empty list zipper all the way to the right. NB:, -- when given an infinite list this may not terminate. rightmostLevel :: Level a -> Level a -- | Pull the non-empty list zipper right one entry. rightLevel :: Level a -> Maybe (Level a) -- | Pull the non-empty list zipper right one entry, stopping at the last -- entry. right1Level :: Level a -> Level a -- | This is a Lens targeting the value that we would -- extract from the non-empty list zipper. -- --
-- view focusLevel ≡ extract ---- --
-- focusLevel :: Simple Lens (Level a) a --focusLevel :: Functor f => (a -> f a) -> Level a -> f (Level a) -- | Zip a non-empty list zipper back up, and return the result. rezipLevel :: Level a -> NonEmpty a instance ComonadStore Int Level instance Comonad Level instance Traversable Level instance Foldable Level instance Functor Level instance Applicative Mutator instance Functor Mutator instance Settable Mutator instance (Settable f, Settable g) => Settable (Compose f g) instance Settable f => Settable (Backwards f) instance Settable Identity instance (Gettable f, Applicative f) => Monoid (Folding f a) instance Monad m => Effective m r (Effect m r) instance Effective m r f => Effective m (Dual r) (Backwards f) instance Effective Identity r (Accessor r) instance Monoid r => Applicative (Accessor r) instance Functor (Accessor r) instance Gettable (Accessor r) instance Gettable (EffectRWS w st m s) instance Gettable (Effect m r) instance (Functor f, Gettable g) => Gettable (Compose f g) instance Gettable f => Gettable (Backwards f) instance Gettable (Const r) instance (Monoid s, Monoid w, Monad m) => Applicative (EffectRWS w st m s) instance Functor (EffectRWS w st m s) instance (Monad m, Monoid r) => Applicative (Effect m r) instance (Monad m, Monoid r) => Monoid (Effect m r a) instance Functor (Effect m r) instance a ~ b => ComonadApply (Bazaar a b) instance a ~ b => Comonad (Bazaar a b) instance Applicative (Bazaar a b) instance Functor (Bazaar a b) instance a ~ b => ComonadStore a (Context a b) instance a ~ b => Comonad (Context a b) instance Functor (Context a b) instance Ord a => Monoid (Max a) instance Ord a => Monoid (Min a) instance Monad m => Monoid (Sequenced m) instance Applicative f => Monoid (Traversed f) instance Gettable f => Gettable (Indexing f) instance Applicative f => Applicative (Indexing f) instance Functor f => Functor (Indexing f) instance Functor f => Functor (IndexingResult f) instance Applicative (k (Err e s)) => Applicative (FocusingErr e k s) instance Functor (k (Err e s)) => Functor (FocusingErr e k s) instance Monoid a => Monoid (Err e a) instance Applicative (k (May s)) => Applicative (FocusingMay k s) instance Functor (k (May s)) => Functor (FocusingMay k s) instance Monoid a => Monoid (May a) instance Applicative (k (f s)) => Applicative (FocusingOn f k s) instance Functor (k (f s)) => Functor (FocusingOn f k s) instance (Monoid w, Applicative (k (s, w))) => Applicative (FocusingPlus w k s) instance Functor (k (s, w)) => Functor (FocusingPlus w k s) instance (Monad m, Monoid s, Monoid w) => Applicative (FocusingWith w m s) instance Monad m => Functor (FocusingWith w m s) instance (Monad m, Monoid s) => Applicative (Focusing m s) instance Monad m => Functor (Focusing m s) -- | Combinators for working with Indexed functions. module Control.Lens.Indexed -- | Permit overloading of function application for things that also admit -- a notion of a key or index. -- -- Provides overloading for Indexed functions. class Indexed i k index :: Indexed i k => ((i -> a) -> b) -> k a b -- | Type alias for passing around polymorphic Indexed functions -- that can be called withIndex or directly as a function type Indexable i a b = forall k. Indexed i k => k a b -- | A function with access to a index. This constructor may be useful when -- you need to store a Indexable in a container to avoid -- ImpredicativeTypes. newtype Index i a b Index :: ((i -> a) -> b) -> Index i a b withIndex :: Index i a b -> (i -> a) -> b -- | Composition of Indexed functions -- -- Mnemonically, the < and > points to the fact -- that we want to preserve the indices. (<.>) :: Indexed (i, j) k => Index i b c -> Index j a b -> k a c -- | Compose an Indexed function with a non-indexed function. -- -- Mnemonically, the < points to the index we want to -- preserve. (<.) :: Indexed i k => Index i b c -> (a -> b) -> k a c -- | Compose a non-indexed function with an Indexed function. -- -- Mnemonically, the > points to the index we want to -- preserve. (.>) :: Indexed i k => (b -> c) -> Index i a b -> k a c -- | Composition of Indexed functions with a user supplied function -- for combining indexs icompose :: Indexed k r => (i -> j -> k) -> Index i b c -> Index j a b -> r a c -- | Remap the index. reindex :: Indexed j k => (i -> j) -> Index i a b -> k a b -- | Transform an Traversal into an IndexedTraversal, a Fold into an -- IndexedFold, etc. -- --
-- indexed :: Traversal s t a b -> IndexedTraversal Int s t a b -- indexed :: Lens s t a b -> IndexedLens Int s t a b -- indexed :: Fold s t -> IndexedFold Int s t -- indexed :: Iso s t a b -> IndexedLens Int s t a b -- indexed :: Getter s t -> IndexedGetter Int s t a b --indexed :: Indexed Int k => ((a -> Indexing f b) -> s -> Indexing f t) -> k (a -> f b) (s -> f t) instance i ~ j => Indexed i (Index j) instance Indexed i (->) module Control.Lens.IndexedGetter -- | Every IndexedGetter is a valid IndexedFold and -- Getter. type IndexedGetter i s a = forall k f. (Indexed i k, Gettable f) => k (a -> f a) (s -> f s) -- | Used to consume an IndexedFold. type IndexedGetting i m s t a b = Index i (a -> Accessor m b) (s -> Accessor m t) -- | Useful for storage. newtype ReifiedIndexedGetter i s a ReifyIndexedGetter :: IndexedGetter i s a -> ReifiedIndexedGetter i s a reflectIndexedGetter :: ReifiedIndexedGetter i s a -> IndexedGetter i s a module Control.Lens.Combinators -- | A strict version of (<$>) for monads. -- --
-- >>> (+1) <$!> [1,2,3,4] -- [2,3,4,5] --(<$!>) :: Monad m => (a -> b) -> m a -> m b -- | A strict version of (<$) for monads. -- --
-- >>> () <$! [1,2,3,4] -- [(),(),(),()] --(<$!) :: Monad m => b -> m a -> m b module Control.Lens.Action -- | An Action is a Getter enriched with access to a -- Monad for side-effects. -- -- Every Getter can be used as an Action -- -- You can compose an Action with another Action using -- (.) from the Prelude. type Action m s a = forall f r. Effective m r f => (a -> f a) -> s -> f s -- | Construct an Action from a monadic side-effect act :: Monad m => (s -> m a) -> Action m s a -- | A self-running Action, analogous to join. -- --
-- acts = act id ---- --
-- >>> (1,"hello")^!_2.acts.to succ -- "ifmmp" --acts :: Action m (m a) a -- | Perform an Action. -- --
-- perform = flip (^!) --perform :: Monad m => Acting m a s t a b -> s -> m a -- | Perform an Action and modify the result. performs :: Monad m => Acting m e s t a b -> (a -> e) -> s -> m e -- | Apply a Monad transformer to an Action. liftAct :: (MonadTrans trans, Monad m) => Acting m a s t a b -> Action (trans m) s a -- | Perform an Action -- --
-- >>> ["hello","world"]^!folded.act putStrLn -- hello -- world --(^!) :: Monad m => s -> Acting m a s t a b -> m a -- | A MonadicFold is a Fold enriched with access to a -- Monad for side-effects. -- -- Every Fold can be used as a MonadicFold, that simply -- ignores the access to the Monad. -- -- You can compose a MonadicFold with another MonadicFold -- using (.) from the Prelude. type MonadicFold m s a = forall f r. (Effective m r f, Applicative f) => (a -> f a) -> s -> f s -- | Used to evaluate an Action. type Acting m r s t a b = (a -> Effect m r b) -> s -> Effect m r t -- | A Lens s t a b is a purely functional reference. -- -- While a Traversal could be used for Getting like a valid -- Fold, it wasn't a valid Getter as Applicative -- wasn't a superclass of Gettable. -- -- Functor, however is the superclass of both. -- --
-- type Lens s t a b = forall f. Functor f => (a -> f b) -> s -> f t ---- -- Every Lens is a valid Setter. -- -- Every Lens can be used for Getting like a Fold -- that doesn't use the Applicative or Gettable. -- -- Every Lens is a valid Traversal that only uses the -- Functor part of the Applicative it is supplied. -- -- Every Lens can be used for Getting like a valid -- Getter, since Functor is a superclass of Gettable -- -- Since every Lens can be used for Getting like a valid -- Getter it follows that it must view exactly one element in the -- structure. -- -- The lens laws follow from this property and the desire for it to act -- like a Traversable when used as a Traversal. module Control.Lens.Type -- | A Lens is actually a lens family as described in -- http://comonad.com/reader/2012/mirrored-lenses/. -- -- With great power comes great responsibility and a Lens is -- subject to the three common sense lens laws: -- -- 1) You get back what you put in: -- --
-- view l (set l b a) ≡ b ---- -- 2) Putting back what you got doesn't change anything: -- --
-- set l (view l a) a ≡ a ---- -- 3) Setting twice is the same as setting once: -- --
-- set l c (set l b a) ≡ set l c a ---- -- These laws are strong enough that the 4 type parameters of a -- Lens cannot vary fully independently. For more on how they -- interact, read the "Why is it a Lens Family?" section of -- http://comonad.com/reader/2012/mirrored-lenses/. -- -- Every Lens can be used directly as a Setter or -- Traversal. -- -- You can also use a Lens for Getting as if it were a -- Fold or Getter. -- -- Since every lens is a valid Traversal, the traversal laws are -- required of any lenses you create: -- --
-- l pure ≡ pure -- fmap (l f) . l g ≡ getCompose . l (Compose . fmap f . g) ---- --
-- type Lens s t a b = forall f. Functor f => LensLike f s t a b --type Lens s t a b = forall f. Functor f => (a -> f b) -> s -> f t -- | A Simple Lens, Simple Traversal, ... can -- be used instead of a Lens,Traversal, ... whenever the -- type variables don't change upon setting a value. -- --
-- imaginary :: Simple Lens (Complex a) a -- traverseHead :: Simple Traversal [a] a ---- -- Note: To use this alias in your own code with LensLike -- f or Setter, you may have to turn on -- LiberalTypeSynonyms. type Simple f s a = f s s a a -- | Build a Lens from a getter and a setter. -- --
-- lens :: Functor f => (s -> a) -> (s -> b -> t) -> (a -> f b) -> s -> f t --lens :: (s -> a) -> (s -> b -> t) -> Lens s t a b -- | This is occasionally useful when your Lens (or -- Traversal) has a constraint on an unused argument to force that -- argument to agree with the type of a used argument and avoid -- ScopedTypeVariables or other ugliness. simple :: SimpleLensLike f s a -> SimpleLensLike f s a -- | (%%~) can be used in one of two scenarios: -- -- When applied to a Lens, it can edit the target of the -- Lens in a structure, extracting a functorial result. -- -- When applied to a Traversal, it can edit the targets of the -- Traversals, extracting an applicative summary of its actions. -- -- For all that the definition of this combinator is just: -- --
-- (%%~) ≡ id ---- --
-- (%%~) :: Functor f => Iso s t a b -> (a -> f b) -> s -> f t -- (%%~) :: Functor f => Lens s t a b -> (a -> f b) -> s -> f t -- (%%~) :: Applicative f => Traversal s t a b -> (a -> f b) -> s -> f t ---- -- It may be beneficial to think about it as if it had these even more -- restrictive types, however: -- -- When applied to a Traversal, it can edit the targets of the -- Traversals, extracting a supplemental monoidal summary of its -- actions, by choosing f = ((,) m) -- --
-- (%%~) :: Iso s t a b -> (a -> (r, b)) -> s -> (r, t) -- (%%~) :: Lens s t a b -> (a -> (r, b)) -> s -> (r, t) -- (%%~) :: Monoid m => Traversal s t a b -> (a -> (m, b)) -> s -> (m, t) --(%%~) :: LensLike f s t a b -> (a -> f b) -> s -> f t -- | Modify the target of a Lens in the current state returning some -- extra information of type r or modify all targets of a -- Traversal in the current state, extracting extra information of -- type r and return a monoidal summary of the changes. -- --
-- (%%=) ≡ (state .) ---- -- It may be useful to think of (%%=), instead, as having either -- of the following more restricted type signatures: -- --
-- (%%=) :: MonadState s m => Iso s s a b -> (a -> (r, b)) -> m r -- (%%=) :: MonadState s m => Lens s s a b -> (a -> (r, b)) -> m r -- (%%=) :: (MonadState s m, Monoid r) => Traversal s s a b -> (a -> (r, b)) -> m r --(%%=) :: MonadState s m => LensLike ((,) r) s s a b -> (a -> (r, b)) -> m r -- | This lens can be used to change the result of a function but only -- where the arguments match the key given. resultAt :: Eq e => e -> Simple Lens (e -> a) a -- | Merge two lenses, getters, setters, folds or traversals. -- --
-- chosen ≡ choosing id id ---- --
-- choosing :: Getter s a -> Getter s' a -> Getter (Either s s') a -- choosing :: Fold s a -> Fold s' a -> Fold (Either s s') a -- choosing :: Simple Lens s a -> Simple Lens s' a -> Simple Lens (Either s s') a -- choosing :: Simple Traversal s a -> Simple Traversal s' a -> Simple Traversal (Either s s') a -- choosing :: Simple Setter s a -> Simple Setter s' a -> Simple Setter (Either s s') a --choosing :: Functor f => LensLike f s t a a -> LensLike f s' t' a a -> LensLike f (Either s s') (Either t t') a a -- | This is a Lens that updates either side of an Either, -- where both sides have the same type. -- --
-- chosen ≡ choosing id id ---- --
-- >>> Left 12^.chosen -- 12 -- -- >>> Right "hello"^.chosen -- "hello" -- -- >>> chosen *~ 10 $ Right 2 -- Right 20 --chosen :: Lens (Either a a) (Either b b) a b -- | alongside makes a Lens from two other lenses. -- --
-- alongside :: Lens s t a b -> Lens s' t' a' b' -> Lens (s,s') (t,t') (a,a') (b,b') --alongside :: LensLike (Context a b) s t a b -> LensLike (Context a' b') s' t' a' b' -> Lens (s, s') (t, t') (a, a') (b, b') -- | Modify the target of a Lens and return the result -- -- When you do not need the result of the addition, (%~) is more -- flexible. -- --
-- (<%~) :: Lens s t a b -> (a -> b) -> s -> (b, t) -- (<%~) :: Iso s t a b -> (a -> b) -> s -> (b, t) -- (<%~) :: Monoid b => Traversal s t a b -> (a -> b) -> s -> (b, t) --(<%~) :: LensLike ((,) b) s t a b -> (a -> b) -> s -> (b, t) -- | Increment the target of a numerically valued Lens and return -- the result -- -- When you do not need the result of the addition, (+~) is more -- flexible. -- --
-- (<+~) :: Num a => Simple Lens s a -> a -> s -> (a, s) -- (<+~) :: Num a => Simple Iso s a -> a -> s -> (a, s) --(<+~) :: Num a => LensLike ((,) a) s t a a -> a -> s -> (a, t) -- | Decrement the target of a numerically valued Lens and return -- the result -- -- When you do not need the result of the subtraction, (-~) is -- more flexible. -- --
-- (<-~) :: Num a => Simple Lens s a -> a -> s -> (a, s) -- (<-~) :: Num a => Simple Iso s a -> a -> s -> (a, s) --(<-~) :: Num a => LensLike ((,) a) s t a a -> a -> s -> (a, t) -- | Multiply the target of a numerically valued Lens and return the -- result -- -- When you do not need the result of the multiplication, (*~) is -- more flexible. -- --
-- (<*~) :: Num b => Simple Lens s a -> a -> a -> (s, a) -- (<*~) :: Num b => Simple Iso s a -> a -> a -> (s, a)) --(<*~) :: Num a => LensLike ((,) a) s t a a -> a -> s -> (a, t) -- | Divide the target of a fractionally valued Lens and return the -- result. -- -- When you do not need the result of the division, (//~) is more -- flexible. -- --
-- (<//~) :: Fractional b => Simple Lens s a -> a -> a -> (s, a) -- (<//~) :: Fractional b => Simple Iso s a -> a -> a -> (s, a)) --(/~) :: Fractional a => LensLike ((,) a) s t a a -> a -> s -> (a, t) -- | Raise the target of a numerically valued Lens to a non-negative -- Integral power and return the result -- -- When you do not need the result of the division, (^~) is more -- flexible. -- --
-- (<^~) :: (Num b, Integral e) => Simple Lens s a -> e -> a -> (a, s) -- (<^~) :: (Num b, Integral e) => Simple Iso s a -> e -> a -> (a, s) --(<^~) :: (Num a, Integral e) => LensLike ((,) a) s t a a -> e -> s -> (a, t) -- | Raise the target of a fractionally valued Lens to an -- Integral power and return the result. -- -- When you do not need the result of the division, (^^~) is more -- flexible. -- --
-- (<^^~) :: (Fractional b, Integral e) => Simple Lens s a -> e -> a -> (a, s) -- (<^^~) :: (Fractional b, Integral e) => Simple Iso s a -> e -> a -> (a, s) --(<^^~) :: (Fractional a, Integral e) => LensLike ((,) a) s t a a -> e -> s -> (a, t) -- | Raise the target of a floating-point valued Lens to an -- arbitrary power and return the result. -- -- When you do not need the result of the division, (**~) is more -- flexible. -- --
-- (<**~) :: Floating a => Simple Lens s a -> a -> s -> (a, s) -- (<**~) :: Floating a => Simple Iso s a -> a -> s -> (a, s) --(<**~) :: Floating a => LensLike ((,) a) s t a a -> a -> s -> (a, t) -- | Logically || a Boolean valued Lens and return the result -- -- When you do not need the result of the operation, (||~) is more -- flexible. -- --
-- (<||~) :: Simple Lens s Bool -> Bool -> s -> (Bool, s) -- (<||~) :: Simple Iso s Bool -> Bool -> s -> (Bool, s) --(<||~) :: LensLike ((,) Bool) s t Bool Bool -> Bool -> s -> (Bool, t) -- | Logically && a Boolean valued Lens and return -- the result -- -- When you do not need the result of the operation, (&&~) -- is more flexible. -- --
-- (<&&~) :: Simple Lens s Bool -> Bool -> s -> (Bool, s) -- (<&&~) :: Simple Iso s Bool -> Bool -> s -> (Bool, s) --(<&&~) :: LensLike ((,) Bool) s t Bool Bool -> Bool -> s -> (Bool, t) -- | Modify the target of a Lens, but return the old value. -- -- When you do not need the result of the addition, (%~) is more -- flexible. -- --
-- (<<%~) :: Lens s t a b -> (a -> b) -> s -> (b, t) -- (<<%~) :: Iso s t a b -> (a -> b) -> s -> (b, t) -- (<<%~) :: Monoid b => Traversal s t a b -> (a -> b) -> s -> (b, t) --(<<%~) :: LensLike ((,) a) s t a b -> (a -> b) -> s -> (a, t) -- | Modify the target of a Lens, but return the old value. -- -- When you do not need the old value, (%~) is more flexible. -- --
-- (<<%~) :: Lens s t a b -> b -> s -> (a, t) -- (<<%~) :: Iso s t a b -> b -> s -> (a, t) -- (<<%~) :: Monoid b => Traversal s t a b -> b -> s -> (a, t) --(<<.~) :: LensLike ((,) a) s t a b -> b -> s -> (a, t) -- | Modify the target of a Lens into your monad's state by a user -- supplied function and return the result. -- -- When applied to a Traversal, it this will return a monoidal -- summary of all of the intermediate results. -- -- When you do not need the result of the operation, (%=) is more -- flexible. -- --
-- (<%=) :: MonadState s m => Simple Lens s a -> (a -> a) -> m a -- (<%=) :: MonadState s m => Simple Iso s a -> (a -> a) -> m a -- (<%=) :: (MonadState s m, Monoid a) => Simple Traveral s a -> (a -> a) -> m a --(<%=) :: MonadState s m => LensLike ((,) b) s s a b -> (a -> b) -> m b -- | Add to the target of a numerically valued Lens into your -- monad's state and return the result. -- -- When you do not need the result of the addition, (+=) is more -- flexible. -- --
-- (<+=) :: (MonadState s m, Num a) => Simple Lens s a -> a -> m a -- (<+=) :: (MonadState s m, Num a) => Simple Iso s a -> a -> m a --(<+=) :: (MonadState s m, Num a) => SimpleLensLike ((,) a) s a -> a -> m a -- | Subtract from the target of a numerically valued Lens into your -- monad's state and return the result. -- -- When you do not need the result of the subtraction, (-=) is -- more flexible. -- --
-- (<-=) :: (MonadState s m, Num a) => Simple Lens s a -> a -> m a -- (<-=) :: (MonadState s m, Num a) => Simple Iso s a -> a -> m a --(<-=) :: (MonadState s m, Num a) => SimpleLensLike ((,) a) s a -> a -> m a -- | Multiply the target of a numerically valued Lens into your -- monad's state and return the result. -- -- When you do not need the result of the multiplication, (*=) is -- more flexible. -- --
-- (<*=) :: (MonadState s m, Num a) => Simple Lens s a -> a -> m a -- (<*=) :: (MonadState s m, Num a) => Simple Iso s a -> a -> m a --(<*=) :: (MonadState s m, Num a) => SimpleLensLike ((,) a) s a -> a -> m a -- | Divide the target of a fractionally valued Lens into your -- monad's state and return the result. -- -- When you do not need the result of the division, (//=) is more -- flexible. -- --
-- (<//=) :: (MonadState s m, Fractional a) => Simple Lens s a -> a -> m a -- (<//=) :: (MonadState s m, Fractional a) => Simple Iso s a -> a -> m a --(/=) :: (MonadState s m, Fractional a) => SimpleLensLike ((,) a) s a -> a -> m a -- | Raise the target of a numerically valued Lens into your monad's -- state to a non-negative Integral power and return the result. -- -- When you do not need the result of the operation, (**=) is more -- flexible. -- --
-- (<^=) :: (MonadState s m, Num a, Integral e) => Simple Lens s a -> e -> m a -- (<^=) :: (MonadState s m, Num a, Integral e) => Simple Iso s a -> e -> m a --(<^=) :: (MonadState s m, Num a, Integral e) => SimpleLensLike ((,) a) s a -> e -> m a -- | Raise the target of a fractionally valued Lens into your -- monad's state to an Integral power and return the result. -- -- When you do not need the result of the operation, (^^=) is more -- flexible. -- --
-- (<^^=) :: (MonadState s m, Fractional b, Integral e) => Simple Lens s a -> e -> m a -- (<^^=) :: (MonadState s m, Fractional b, Integral e) => Simple Iso s a -> e -> m a --(<^^=) :: (MonadState s m, Fractional a, Integral e) => SimpleLensLike ((,) a) s a -> e -> m a -- | Raise the target of a floating-point valued Lens into your -- monad's state to an arbitrary power and return the result. -- -- When you do not need the result of the operation, (**=) is more -- flexible. -- --
-- (<**=) :: (MonadState s m, Floating a) => Simple Lens s a -> a -> m a -- (<**=) :: (MonadState s m, Floating a) => Simple Iso s a -> a -> m a --(<**=) :: (MonadState s m, Floating a) => SimpleLensLike ((,) a) s a -> a -> m a -- | Logically || a Boolean valued Lens into your monad's -- state and return the result. -- -- When you do not need the result of the operation, (||=) is more -- flexible. -- --
-- (<||=) :: MonadState s m => Simple Lens s Bool -> Bool -> m Bool -- (<||=) :: MonadState s m => Simple Iso s Bool -> Bool -> m Bool --(<||=) :: MonadState s m => SimpleLensLike ((,) Bool) s Bool -> Bool -> m Bool -- | Logically && a Boolean valued Lens into your -- monad's state and return the result. -- -- When you do not need the result of the operation, (&&=) -- is more flexible. -- --
-- (<&&=) :: MonadState s m => Simple Lens s Bool -> Bool -> m Bool -- (<&&=) :: MonadState s m => Simple Iso s Bool -> Bool -> m Bool --(<&&=) :: MonadState s m => SimpleLensLike ((,) Bool) s Bool -> Bool -> m Bool -- | Modify the target of a Lens into your monad's state by a user -- supplied function and return the old value that was replaced. -- -- When applied to a Traversal, it this will return a monoidal -- summary of all of the old values present. -- -- When you do not need the result of the operation, (%=) is more -- flexible. -- --
-- (<<%=) :: MonadState s m => Simple Lens s a -> (a -> a) -> m a -- (<<%=) :: MonadState s m => Simple Iso s a -> (a -> a) -> m a -- (<<%=) :: (MonadState s m, Monoid b) => Simple Traveral s a -> (a -> a) -> m a --(<<%=) :: MonadState s m => LensLike ((,) a) s s a b -> (a -> b) -> m a -- | Modify the target of a Lens into your monad's state by a user -- supplied function and return the old value that was replaced. -- -- When applied to a Traversal, it this will return a monoidal -- summary of all of the old values present. -- -- When you do not need the result of the operation, (%=) is more -- flexible. -- --
-- (<<%=) :: MonadState s m => Simple Lens s a -> (a -> a) -> m a -- (<<%=) :: MonadState s m => Simple Iso s a -> (a -> a) -> m a -- (<<%=) :: (MonadState s m, Monoid t) => Simple Traveral s a -> (a -> a) -> m a --(<<.=) :: MonadState s m => LensLike ((,) a) s s a b -> b -> m a -- | Run a monadic action, and set the target of Lens to its result. -- --
-- (<<~) :: MonadState s m => Iso s s a b -> m b -> m b -- (<<~) :: MonadState s m => Lens s s a b -> m b -> m b ---- -- NB: This is limited to taking an actual Lens than admitting a -- Traversal because there are potential loss of state issues -- otherwise. (<<~) :: MonadState s m => LensLike (Context a b) s s a b -> m b -> m b -- | Cloning a Lens is one way to make sure you aren't given -- something weaker, such as a Traversal and can be used as a way -- to pass around lenses that have to be monomorphic in f. -- -- Note: This only accepts a proper Lens. -- -- \"Costate Comonad Coalgebra is equivalent of Java's member variable -- update technology for Haskell\" -- @PLT_Borat on Twitter cloneLens :: Functor f => LensLike (Context a b) s t a b -> (a -> f b) -> s -> f t -- | Useful for storing lenses in containers. newtype ReifiedLens s t a b ReifyLens :: Lens s t a b -> ReifiedLens s t a b reflectLens :: ReifiedLens s t a b -> Lens s t a b -- | Many combinators that accept a Lens can also accept a -- Traversal in limited situations. -- -- They do so by specializing the type of Functor that they -- require of the caller. -- -- If a function accepts a LensLike f s t a b for some -- Functor f, then they may be passed a Lens. -- -- Further, if f is an Applicative, they may also be -- passed a Traversal. type LensLike f s t a b = (a -> f b) -> s -> f t -- |
-- type LensLike f s t a b = Overloaded (->) f s t a b --type Overloaded k f s t a b = k (a -> f b) (s -> f t) -- |
-- type SimpleLens = Simple Lens --type SimpleLens s a = Lens s s a a -- |
-- type SimpleLensLike f = Simple (LensLike f) --type SimpleLensLike f s a = LensLike f s s a a -- |
-- type SimpleOverloaded k f s a = Simple (Overloaded k f) s a --type SimpleOverloaded k f s a = Overloaded k f s s a a -- |
-- type SimpleReifiedLens = Simple ReifiedLens --type SimpleReifiedLens s a = ReifiedLens s s a a module Control.Lens.IndexedLens -- | Every IndexedLens is a valid Lens and a valid -- IndexedTraversal. type IndexedLens i s t a b = forall f k. (Indexed i k, Functor f) => k (a -> f b) (s -> f t) -- | Provides an IndexedLens that can be used to read, write or -- delete the value associated with a key in a map-like container. class At k m | m -> k at :: At k m => k -> SimpleIndexedLens k (m v) (Maybe v) -- | Provides an IndexedLens that can be used to read, write or -- delete a member of a set-like container class Contains k m | m -> k contains :: Contains k m => k -> SimpleIndexedLens k m Bool -- | Adjust the target of an IndexedLens returning a supplementary -- result, or adjust all of the targets of an IndexedTraversal and -- return a monoidal summary of the supplementary results and the answer. -- --
-- (%%@~) = withIndex ---- --
-- (%%@~) :: Functor f => IndexedLens i s t a b -> (i -> a -> f b) -> s -> f t -- (%%@~) :: Functor f => IndexedTraversal i s t a b -> (i -> a -> f b) -> s -> f t ---- -- In particular, it is often useful to think of this function as having -- one of these even more restrictive type signatures -- --
-- (%%@~) :: IndexedLens i s t a b -> (i -> a -> (r, b)) -> s -> (r, t) -- (%%@~) :: Monoid r => IndexedTraversal i s t a b -> (i -> a -> (r, b)) -> s -> (r, t) --(%%@~) :: Overloaded (Index i) f s t a b -> (i -> a -> f b) -> s -> f t -- | Adjust the target of an IndexedLens returning the intermediate -- result, or adjust all of the targets of an IndexedTraversal and -- return a monoidal summary along with the answer. -- --
-- l <%~ f = l <%@~ const f ---- -- When you do not need access to the index then (<%~) is more -- liberal in what it can accept. -- -- If you do not need the intermediate result, you can use (%@~) -- or even (%~). -- --
-- (<%@~) :: IndexedLens i s t a b -> (i -> a -> b) -> s -> (b, t) -- (<%@~) :: Monoid b => IndexedTraversal i s t a b -> (i -> a -> b) -> s -> (b, t) --(<%@~) :: Overloaded (Index i) ((,) b) s t a b -> (i -> a -> b) -> s -> (b, t) -- | Adjust the target of an IndexedLens returning a supplementary -- result, or adjust all of the targets of an IndexedTraversal -- within the current state, and return a monoidal summary of the -- supplementary results. -- --
-- l %%@= f = state (l %%@~ f) ---- --
-- (%%@=) :: MonadState s m IndexedLens i s s a b -> (i -> a -> (r, b)) -> s -> m r -- (%%@=) :: (MonadState s m, Monoid r) => IndexedTraversal i s s a b -> (i -> a -> (r, b)) -> s -> m r --(%%@=) :: MonadState s m => Overloaded (Index i) ((,) r) s s a b -> (i -> a -> (r, b)) -> m r -- | Adjust the target of an IndexedLens returning the intermediate -- result, or adjust all of the targets of an IndexedTraversal -- within the current state, and return a monoidal summary of the -- intermediate results. -- --
-- (<%@=) :: MonadState s m IndexedLens i s s a b -> (i -> a -> b) -> m b -- (<%@=) :: (MonadState s m, Monoid b) => IndexedTraversal i s s a b -> (i -> a -> b) -> m b --(<%@=) :: MonadState s m => Overloaded (Index i) ((,) b) s s a b -> (i -> a -> b) -> m b -- | Useful for storage. newtype ReifiedIndexedLens i s t a b ReifyIndexedLens :: IndexedLens i s t a b -> ReifiedIndexedLens i s t a b reflectIndexedLens :: ReifiedIndexedLens i s t a b -> IndexedLens i s t a b -- |
-- type SimpleIndexedLens i = Simple (IndexedLens i) --type SimpleIndexedLens i s a = IndexedLens i s s a a -- |
-- type SimpleIndexedLens i = Simple (ReifiedIndexedLens i) --type SimpleReifiedIndexedLens i s a = ReifiedIndexedLens i s s a a instance (Eq k, Hashable k) => Contains k (HashSet k) instance Ord k => Contains k (Set k) instance Contains Int IntSet instance (Eq k, Hashable k) => At k (HashMap k) instance Ord k => At k (Map k) instance At Int IntMap module Control.Lens.IndexedFold -- | Every IndexedFold is a valid Fold. type IndexedFold i s a = forall k f. (Indexed i k, Applicative f, Gettable f) => k (a -> f a) (s -> f s) -- | Fold an IndexedFold or IndexedTraversal by mapping -- indices and values to an arbitrary Monoid with access to the -- index i. -- -- When you don't need access to the index then foldMapOf is more -- flexible in what it accepts. -- --
-- foldMapOf l ≡ ifoldMapOf l . const ---- --
-- ifoldMapOf :: IndexedGetter i a s -> (i -> s -> m) -> a -> m -- ifoldMapOf :: Monoid m => IndexedFold i a s -> (i -> s -> m) -> a -> m -- ifoldMapOf :: SimpleIndexedLens i a s -> (i -> s -> m) -> a -> m -- ifoldMapOf :: Monoid m => SimpleIndexedTraversal i a s -> (i -> s -> m) -> a -> m --ifoldMapOf :: IndexedGetting i m s t a b -> (i -> a -> m) -> s -> m -- | Right-associative fold of parts of a structure that are viewed through -- an IndexedFold or IndexedTraversal with access to the -- index i. -- -- When you don't need access to the index then foldrOf is more -- flexible in what it accepts. -- --
-- foldrOf l ≡ ifoldrOf l . const ---- --
-- ifoldrOf :: IndexedGetter i s a -> (i -> a -> r -> r) -> r -> s -> r -- ifoldrOf :: IndexedFold i s a -> (i -> a -> r -> r) -> r -> s -> r -- ifoldrOf :: SimpleIndexedLens i s a -> (i -> a -> r -> r) -> r -> s -> r -- ifoldrOf :: SimpleIndexedTraversal i s a -> (i -> a -> r -> r) -> r -> s -> r --ifoldrOf :: IndexedGetting i (Endo r) s t a b -> (i -> a -> r -> r) -> r -> s -> r -- | Left-associative fold of the parts of a structure that are viewed -- through an IndexedFold or IndexedTraversal with access -- to the index i. -- -- When you don't need access to the index then foldlOf is more -- flexible in what it accepts. -- --
-- foldlOf l ≡ ifoldlOf l . const ---- --
-- ifoldlOf :: IndexedGetter i s a -> (i -> r -> a -> r) -> r -> s -> r -- ifoldlOf :: IndexedFold i s a -> (i -> r -> a -> r) -> r -> s -> r -- ifoldlOf :: SimpleIndexedLens i s a -> (i -> r -> a -> r) -> r -> s -> r -- ifoldlOf :: SimpleIndexedTraversal i s a -> (i -> r -> a -> r) -> r -> s -> r --ifoldlOf :: IndexedGetting i (Dual (Endo r)) s t a b -> (i -> r -> a -> r) -> r -> s -> r -- | Return whether or not any element viewed through an IndexedFold -- or IndexedTraversal satisfy a predicate, with access to the -- index i. -- -- When you don't need access to the index then anyOf is more -- flexible in what it accepts. -- --
-- anyOf l ≡ ianyOf l . const ---- --
-- ianyOf :: IndexedGetter i s a -> (i -> a -> Bool) -> s -> Bool -- ianyOf :: IndexedFold i s a -> (i -> a -> Bool) -> s -> Bool -- ianyOf :: SimpleIndexedLens i s a -> (i -> a -> Bool) -> s -> Bool -- ianyOf :: SimpleIndexedTraversal i s a -> (i -> a -> Bool) -> s -> Bool --ianyOf :: IndexedGetting i Any s t a b -> (i -> a -> Bool) -> s -> Bool -- | Return whether or not all elements viewed through an -- IndexedFold or IndexedTraversal satisfy a predicate, -- with access to the index i. -- -- When you don't need access to the index then allOf is more -- flexible in what it accepts. -- --
-- allOf l ≡ iallOf l . const ---- --
-- iallOf :: IndexedGetter i s a -> (i -> a -> Bool) -> s -> Bool -- iallOf :: IndexedFold i s a -> (i -> a -> Bool) -> s -> Bool -- iallOf :: SimpleIndexedLens i s a -> (i -> a -> Bool) -> s -> Bool -- iallOf :: SimpleIndexedTraversal i s a -> (i -> a -> Bool) -> s -> Bool --iallOf :: IndexedGetting i All s t a b -> (i -> a -> Bool) -> s -> Bool -- | Traverse the targets of an IndexedFold or -- IndexedTraversal with access to the index i, -- discarding the results. -- -- When you don't need access to the index then traverseOf_ is -- more flexible in what it accepts. -- --
-- traverseOf_ l ≡ itraverseOf l . const ---- --
-- itraverseOf_ :: Functor f => IndexedGetter i s a -> (i -> a -> f r) -> s -> f () -- itraverseOf_ :: Applicative f => IndexedFold i s a -> (i -> a -> f r) -> s -> f () -- itraverseOf_ :: Functor f => SimpleIndexedLens i s a -> (i -> a -> f r) -> s -> f () -- itraverseOf_ :: Applicative f => SimpleIndexedTraversal i s a -> (i -> a -> f r) -> s -> f () --itraverseOf_ :: Functor f => IndexedGetting i (Traversed f) s t a b -> (i -> a -> f r) -> s -> f () -- | Traverse the targets of an IndexedFold or -- IndexedTraversal with access to the index, discarding the -- results (with the arguments flipped). -- --
-- iforOf_ ≡ flip . itraverseOf_ ---- -- When you don't need access to the index then forOf_ is more -- flexible in what it accepts. -- --
-- forOf_ l a ≡ iforOf_ l a . const ---- --
-- iforOf_ :: Functor f => IndexedGetter i s a -> s -> (i -> a -> f r) -> f () -- iforOf_ :: Applicative f => IndexedFold i s a -> s -> (i -> a -> f r) -> f () -- iforOf_ :: Functor f => SimpleIndexedLens i s a -> s -> (i -> a -> f r) -> f () -- iforOf_ :: Applicative f => SimpleIndexedTraversal i s a -> s -> (i -> a -> f r) -> f () --iforOf_ :: Functor f => IndexedGetting i (Traversed f) s t a b -> s -> (i -> a -> f r) -> f () -- | Run monadic actions for each target of an IndexedFold or -- IndexedTraversal with access to the index, discarding the -- results. -- -- When you don't need access to the index then mapMOf_ is more -- flexible in what it accepts. -- --
-- mapMOf_ l ≡ imapMOf l . const ---- --
-- imapMOf_ :: Monad m => IndexedGetter i s a -> (i -> a -> m r) -> s -> m () -- imapMOf_ :: Monad m => IndexedFold i s a -> (i -> a -> m r) -> s -> m () -- imapMOf_ :: Monad m => SimpleIndexedLens i s a -> (i -> a -> m r) -> s -> m () -- imapMOf_ :: Monad m => SimpleIndexedTraversal i s a -> (i -> a -> m r) -> s -> m () --imapMOf_ :: Monad m => IndexedGetting i (Sequenced m) s t a b -> (i -> a -> m r) -> s -> m () -- | Run monadic actions for each target of an IndexedFold or -- IndexedTraversal with access to the index, discarding the -- results (with the arguments flipped). -- --
-- iforMOf_ ≡ flip . imapMOf_ ---- -- When you don't need access to the index then forMOf_ is more -- flexible in what it accepts. -- --
-- forMOf_ l a ≡ iforMOf l a . const ---- --
-- iforMOf_ :: Monad m => IndexedGetter i s a -> s -> (i -> a -> m r) -> m () -- iforMOf_ :: Monad m => IndexedFold i s a -> s -> (i -> a -> m r) -> m () -- iforMOf_ :: Monad m => SimpleIndexedLens i s a -> s -> (i -> a -> m r) -> m () -- iforMOf_ :: Monad m => SimpleIndexedTraversal i s a -> s -> (i -> a -> m r) -> m () --iforMOf_ :: Monad m => IndexedGetting i (Sequenced m) s t a b -> s -> (i -> a -> m r) -> m () -- | Concatenate the results of a function of the elements of an -- IndexedFold or IndexedTraversal with access to the -- index. -- -- When you don't need access to the index then concatMapOf is -- more flexible in what it accepts. -- --
-- concatMapOf l ≡ iconcatMapOf l . const -- iconcatMapOf ≡ ifoldMapOf ---- --
-- iconcatMapOf :: IndexedGetter i s a -> (i -> a -> [r]) -> s -> [r] -- iconcatMapOf :: IndexedFold i s a -> (i -> a -> [r]) -> s -> [r] -- iconcatMapOf :: SimpleIndexedLens i s a -> (i -> a -> [r]) -> s -> [r] -- iconcatMapOf :: SimpleIndexedTraversal i s a -> (i -> a -> [r]) -> s -> [r] --iconcatMapOf :: IndexedGetting i [r] s t a b -> (i -> a -> [r]) -> s -> [r] -- | The findOf function takes an IndexedFold or -- IndexedTraversal, a predicate that is also supplied the index, -- a structure and returns the left-most element of the structure -- matching the predicate, or Nothing if there is no such element. -- -- When you don't need access to the index then findOf is more -- flexible in what it accepts. -- --
-- findOf l ≡ ifindOf l . const ---- --
-- ifindOf :: IndexedGetter s a -> (i -> a -> Bool) -> s -> Maybe (i, a) -- ifindOf :: IndexedFold s a -> (i -> a -> Bool) -> s -> Maybe (i, a) -- ifindOf :: SimpleIndexedLens s a -> (i -> a -> Bool) -> s -> Maybe (i, a) -- ifindOf :: SimpleIndexedTraversal s a -> (i -> a -> Bool) -> s -> Maybe (i, a) --ifindOf :: IndexedGetting i (First (i, a)) s t a b -> (i -> a -> Bool) -> s -> Maybe (i, a) -- | Strictly fold right over the elements of a structure with an -- index. -- -- When you don't need access to the index then foldrOf' is more -- flexible in what it accepts. -- --
-- foldrOf' l ≡ ifoldrOf' l . const ---- --
-- ifoldrOf' :: IndexedGetter i s a -> (i -> a -> r -> r) -> r -> s -> r -- ifoldrOf' :: IndexedFold i s a -> (i -> a -> r -> r) -> r -> s -> r -- ifoldrOf' :: SimpleIndexedLens i s a -> (i -> a -> r -> r) -> r -> s -> r -- ifoldrOf' :: SimpleIndexedTraversal i s a -> (i -> a -> r -> r) -> r -> s -> r --ifoldrOf' :: IndexedGetting i (Dual (Endo (r -> r))) s t a b -> (i -> a -> r -> r) -> r -> s -> r -- | Fold over the elements of a structure with an index, associating to -- the left, but strictly. -- -- When you don't need access to the index then foldlOf' is more -- flexible in what it accepts. -- --
-- foldlOf' l ≡ ifoldlOf' l . const ---- --
-- ifoldlOf' :: IndexedGetter i s a -> (i -> r -> a -> r) -> r -> s -> r -- ifoldlOf' :: IndexedFold i s a -> (i -> r -> a -> r) -> r -> s -> r -- ifoldlOf' :: SimpleIndexedLens i s a -> (i -> r -> a -> r) -> r -> s -> r -- ifoldlOf' :: SimpleIndexedTraversal i s a -> (i -> r -> a -> r) -> r -> s -> r --ifoldlOf' :: IndexedGetting i (Endo (r -> r)) s t a b -> (i -> r -> a -> r) -> r -> s -> r -- | Monadic fold right over the elements of a structure with an index. -- -- When you don't need access to the index then foldrMOf is more -- flexible in what it accepts. -- --
-- foldrMOf l ≡ ifoldrMOf l . const ---- --
-- ifoldrMOf :: Monad m => IndexedGetter i s a -> (i -> a -> r -> m r) -> r -> s -> r -- ifoldrMOf :: Monad m => IndexedFold i s a -> (i -> a -> r -> m r) -> r -> s -> r -- ifoldrMOf :: Monad m => SimpleIndexedLens i s a -> (i -> a -> r -> m r) -> r -> s -> r -- ifoldrMOf :: Monad m => SimpleIndexedTraversal i s a -> (i -> a -> r -> m r) -> r -> s -> r --ifoldrMOf :: Monad m => IndexedGetting i (Dual (Endo (r -> m r))) s t a b -> (i -> a -> r -> m r) -> r -> s -> m r -- | Monadic fold over the elements of a structure with an index, -- associating to the left. -- -- When you don't need access to the index then foldlMOf is more -- flexible in what it accepts. -- --
-- foldlMOf l ≡ ifoldlMOf l . const ---- --
-- ifoldlOf' :: Monad m => IndexedGetter i s a -> (i -> r -> a -> m r) -> r -> s -> r -- ifoldlOf' :: Monad m => IndexedFold i s a -> (i -> r -> a -> m r) -> r -> s -> r -- ifoldlOf' :: Monad m => SimpleIndexedLens i s a -> (i -> r -> a -> m r) -> r -> s -> r -- ifoldlOf' :: Monad m => SimpleIndexedTraversal i s a -> (i -> r -> a -> m r) -> r -> s -> r --ifoldlMOf :: Monad m => IndexedGetting i (Endo (r -> m r)) s t a b -> (i -> r -> a -> m r) -> r -> s -> m r -- | Extract the key-value pairs from a structure. -- -- When you don't need access to the indices in the result, then -- toListOf is more flexible in what it accepts. -- --
-- toListOf l ≡ map fst . itoListOf l ---- --
-- itoListOf :: IndexedGetter i s a -> s -> [(i,a)] -- itoListOf :: IndexedFold i s a -> s -> [(i,a)] -- itoListOf :: SimpleIndexedLens i s a -> s -> [(i,a)] -- itoListOf :: SimpleIndexedTraversal i s a -> s -> [(i,a)] --itoListOf :: IndexedGetting i [(i, a)] s t a b -> s -> [(i, a)] -- | Transform an indexed fold into a fold of both the indices and the -- values. -- --
-- withIndicesOf :: IndexedFold i s a -> Fold s (i, a) -- withIndicesOf :: SimpleIndexedLens i s a -> Getter s (i, a) -- withIndicesOf :: SimpleIndexedTraversal i s a -> Fold s (i, a) ---- -- All Fold operations are safe, and comply with the laws. -- However, -- -- Passing this an IndexedTraversal will still allow many -- Traversal combinators to type check on the result, but the -- result can only be legally traversed by operations that do not edit -- the indices. -- --
-- withIndicesOf :: IndexedTraversal i s t a b -> Traversal s t (i, a) (j, b) ---- -- Change made to the indices will be discarded. withIndicesOf :: Functor f => Overloaded (Index i) f s t a b -> LensLike f s t (i, a) (j, b) -- | Transform an indexed fold into a fold of the indices. -- --
-- indicesOf :: IndexedFold i s a -> Fold s i -- indicesOf :: SimpleIndexedLens i s a -> Getter s i -- indicesOf :: SimpleIndexedTraversal i s a -> Fold s i --indicesOf :: Gettable f => Overloaded (Index i) f s t a a -> LensLike f s t i j -- | Obtain an IndexedFold by filtering an IndexedLens, -- IndexedGetter, or IndexedFold. -- -- When passed an IndexedTraversal, sadly the result is not -- a legal IndexedTraversal. -- -- See filtered for a related counter-example. ifiltering :: (Applicative f, Indexed i k) => (i -> a -> Bool) -> Index i (a -> f a) (s -> f t) -> k (a -> f a) (s -> f t) -- | Reverse the order of the elements of an IndexedFold or -- IndexedTraversal. This has no effect on an IndexedLens, -- IndexedGetter, or IndexedSetter. ibackwards :: Indexed i k => Index i (a -> (Backwards f) b) (s -> (Backwards f) t) -> k (a -> f b) (s -> f t) -- | Obtain an IndexedFold by taking elements from another -- IndexedFold, IndexedLens, IndexedGetter or -- IndexedTraversal while a predicate holds. itakingWhile :: (Gettable f, Applicative f, Indexed i k) => (i -> a -> Bool) -> IndexedGetting i (Endo (f s)) s s a a -> k (a -> f a) (s -> f s) -- | Obtain an IndexedFold by dropping elements from another -- IndexedFold, IndexedLens, IndexedGetter or -- IndexedTraversal while a predicate holds. idroppingWhile :: (Gettable f, Applicative f, Indexed i k) => (i -> a -> Bool) -> IndexedGetting i (Endo (f s, f s)) s s a a -> k (a -> f a) (s -> f s) -- | Useful for storage. newtype ReifiedIndexedFold i s a ReifyIndexedFold :: IndexedFold i s a -> ReifiedIndexedFold i s a reflectIndexedFold :: ReifiedIndexedFold i s a -> IndexedFold i s a module Control.Lens.IndexedTraversal -- | Every indexed traversal is a valid Traversal or -- IndexedFold. -- -- The Indexed constraint is used to allow an -- IndexedTraversal to be used directly as a Traversal. -- -- The Traversal laws are still required to hold. type IndexedTraversal i s t a b = forall f k. (Indexed i k, Applicative f) => k (a -> f b) (s -> f t) -- | Traverse the value at a given key in a map -- --
-- traverseAt k = at k <. traverse --traverseAt :: At k m => k -> SimpleIndexedTraversal k (m v) v -- | Access the element of an IndexedTraversal where the index -- matches a predicate. -- --
-- >>> over (iwhereOf (indexed traverse) (>0)) reverse $ ["He","was","stressed","o_O"] -- ["He","saw","desserts","O_o"] ---- --
-- iwhereOf :: IndexedFold i s a -> (i -> Bool) -> IndexedFold i s a -- iwhereOf :: IndexedGetter i s a -> (i -> Bool) -> IndexedFold i s a -- iwhereOf :: SimpleIndexedLens i s a -> (i -> Bool) -> SimpleIndexedTraversal i s a -- iwhereOf :: SimpleIndexedTraversal i s a -> (i -> Bool) -> SimpleIndexedTraversal i s a -- iwhereOf :: SimpleIndexedSetter i s a -> (i -> Bool) -> SimpleIndexedSetter i s a --iwhereOf :: (Indexed i k, Applicative f) => Overloaded (Index i) f s t a a -> (i -> Bool) -> Overloaded k f s t a a -- | This provides a Traversal that checks a predicate on a key -- before allowing you to traverse into a value. value :: (k -> Bool) -> SimpleIndexedTraversal k (k, v) v -- | Allows IndexedTraversal the value at the smallest index. class Ord k => TraverseMin k m | m -> k traverseMin :: TraverseMin k m => SimpleIndexedTraversal k (m v) v -- | Allows IndexedTraversal of the value at the largest index. class Ord k => TraverseMax k m | m -> k traverseMax :: TraverseMax k m => SimpleIndexedTraversal k (m v) v -- | Traversal with an index. -- -- NB: When you don't need access to the index then you can just apply -- your IndexedTraversal directly as a function! -- --
-- itraverseOf ≡ withIndex -- traverseOf l = itraverseOf l . const = id ---- --
-- itraverseOf :: IndexedLens i s t a b -> (i -> a -> f b) -> s -> f t -- itraverseOf :: IndexedTraversal i s t a b -> (i -> a -> f b) -> s -> f t --itraverseOf :: Overloaded (Index i) f s t a b -> (i -> a -> f b) -> s -> f t -- | Traverse with an index (and the arguments flipped) -- --
-- forOf l a ≡ iforOf l a . const -- iforOf ≡ flip . itraverseOf ---- --
-- iforOf :: IndexedLens i s t a b -> s -> (i -> a -> f b) -> f t -- iforOf :: IndexedTraversal i s t a b -> s -> (i -> a -> f b) -> f t --iforOf :: Overloaded (Index i) f s t a b -> s -> (i -> a -> f b) -> f t -- | Map each element of a structure targeted by a lens to a monadic -- action, evaluate these actions from left to right, and collect the -- results, with access its position. -- -- When you don't need access to the index mapMOf is more -- liberal in what it can accept. -- --
-- mapMOf l ≡ imapMOf l . const ---- --
-- imapMOf :: Monad m => IndexedLens i s t a b -> (i -> a -> m b) -> s -> m t -- imapMOf :: Monad m => IndexedTraversal i s t a b -> (i -> a -> m b) -> s -> m t --imapMOf :: Overloaded (Index i) (WrappedMonad m) s t a b -> (i -> a -> m b) -> s -> m t -- | Map each element of a structure targeted by a lens to a monadic -- action, evaluate these actions from left to right, and collect the -- results, with access its position (and the arguments flipped). -- --
-- forMOf l a ≡ iforMOf l a . const -- iforMOf ≡ flip . imapMOf ---- --
-- iforMOf :: Monad m => IndexedLens i s t a b -> s -> (i -> a -> m b) -> m t -- iforMOf :: Monad m => IndexedTraversal i s t a b -> s -> (i -> a -> m b) -> m t --iforMOf :: Overloaded (Index i) (WrappedMonad m) s t a b -> s -> (i -> a -> m b) -> m t -- | Generalizes mapAccumR to an arbitrary IndexedTraversal -- with access to the index. -- -- imapAccumROf accumulates state from right to left. -- --
-- mapAccumROf l ≡ imapAccumROf l . const ---- --
-- imapAccumROf :: IndexedLens i s t a b -> (i -> s -> a -> (s, b)) -> s -> s -> (s, t) -- imapAccumROf :: IndexedTraversal i s t a b -> (i -> s -> a -> (s, b)) -> s -> s -> (s, t) --imapAccumROf :: Overloaded (Index i) (State s) s t a b -> (i -> s -> a -> (s, b)) -> s -> s -> (s, t) -- | Generalizes mapAccumL to an arbitrary IndexedTraversal -- with access to the index. -- -- imapAccumLOf accumulates state from left to right. -- --
-- mapAccumLOf l ≡ imapAccumLOf l . const ---- --
-- imapAccumLOf :: IndexedLens i s t a b -> (i -> s -> a -> (s, b)) -> s -> s -> (s, t) -- imapAccumLOf :: IndexedTraversal i s t a b -> (i -> s -> a -> (s, b)) -> s -> s -> (s, t) --imapAccumLOf :: Overloaded (Index i) (Backwards (State s)) s t a b -> (i -> s -> a -> (s, b)) -> s -> s -> (s, t) -- | Useful for storage. newtype ReifiedIndexedTraversal i s t a b ReifyIndexedTraversal :: IndexedTraversal i s t a b -> ReifiedIndexedTraversal i s t a b reflectIndexedTraversal :: ReifiedIndexedTraversal i s t a b -> IndexedTraversal i s t a b -- |
-- type SimpleIndexedTraversal i = Simple (IndexedTraversal i) --type SimpleIndexedTraversal i s a = IndexedTraversal i s s a a -- |
-- type SimpleIndexedTraversal i = Simple (ReifiedIndexedTraversal i) --type SimpleReifiedIndexedTraversal i s a = ReifiedIndexedTraversal i s s a a instance Ord k => TraverseMax k (Map k) instance TraverseMax Int IntMap instance Ord k => TraverseMin k (Map k) instance TraverseMin Int IntMap module Control.Lens.IndexedSetter -- | Every IndexedSetter is a valid Setter -- -- The Setter laws are still required to hold. type IndexedSetter i s t a b = forall f k. (Indexed i k, Settable f) => k (a -> f b) (s -> f t) -- | Map with index. -- -- When you do not need access to the index, then mapOf is more -- liberal in what it can accept. -- --
-- mapOf l ≡ imapOf l . const ---- --
-- imapOf :: IndexedSetter i s t a b -> (i -> a -> b) -> s -> t -- imapOf :: IndexedLens i s t a b -> (i -> a -> b) -> s -> t -- imapOf :: IndexedTraversal i s t a b -> (i -> a -> b) -> s -> t --imapOf :: Overloaded (Index i) Mutator s t a b -> (i -> a -> b) -> s -> t -- | Map with index. This is an alias for imapOf. -- -- When you do not need access to the index, then over is more -- liberal in what it can accept. -- --
-- over l ≡ iover l . const ---- --
-- iover :: IndexedSetter i s t a b -> (i -> a -> b) -> s -> t -- iover :: IndexedLens i s t a b -> (i -> a -> b) -> s -> t -- iover :: IndexedTraversal i s t a b -> (i -> a -> b) -> s -> t --iover :: Overloaded (Index i) Mutator s t a b -> (i -> a -> b) -> s -> t -- | Build an IndexedSetter from an imap-like function. -- -- Your supplied function f is required to satisfy: -- --
-- f id ≡ id -- f g . f h ≡ f (g . h) ---- -- Equational reasoning: -- --
-- isets . iover ≡ id -- iover . isets ≡ id ---- -- Another way to view sets is that it takes a "semantic editor -- combinator" and transforms it into a Setter. isets :: ((i -> a -> b) -> s -> t) -> IndexedSetter i s t a b -- | Adjust every target of an IndexedSetter, IndexedLens or -- IndexedTraversal with access to the index. -- --
-- (%@~) ≡ imapOf ---- -- When you do not need access to the index then (%@~) is more -- liberal in what it can accept. -- --
-- l %~ f ≡ l %@~ const f ---- --
-- (%@~) :: IndexedSetter i s t a b -> (i -> a -> b) -> s -> t -- (%@~) :: IndexedLens i s t a b -> (i -> a -> b) -> s -> t -- (%@~) :: IndexedTraversal i s t a b -> (i -> a -> b) -> s -> t --(%@~) :: Overloaded (Index i) Mutator s t a b -> (i -> a -> b) -> s -> t -- | Adjust every target in the current state of an IndexedSetter, -- IndexedLens or IndexedTraversal with access to the -- index. -- -- When you do not need access to the index then (%=) is more -- liberal in what it can accept. -- --
-- l %= f ≡ l %@= const f ---- --
-- (%@=) :: MonadState s m => IndexedSetter i s s a b -> (i -> a -> b) -> m () -- (%@=) :: MonadState s m => IndexedLens i s s a b -> (i -> a -> b) -> m () -- (%@=) :: MonadState s m => IndexedTraversal i s t a b -> (i -> a -> b) -> m () --(%@=) :: MonadState s m => Overloaded (Index i) Mutator s s a b -> (i -> a -> b) -> m () -- | Useful for storage. newtype ReifiedIndexedSetter i s t a b ReifyIndexedSetter :: IndexedSetter i s t a b -> ReifiedIndexedSetter i s t a b reflectIndexedSetter :: ReifiedIndexedSetter i s t a b -> IndexedSetter i s t a b -- |
-- type SimpleIndexedSetter i = Simple (IndexedSetter i) --type SimpleIndexedSetter i s a = IndexedSetter i s s a a -- |
-- type SimpleIndexedSetter i = Simple (ReifiedIndexedSetter i) --type SimpleReifiedIndexedSetter i s a = ReifiedIndexedSetter i s s a a -- | A Loupe is a minimalist Lens suitable for storing in -- containers or returning monadically that can still be composed with -- other lenses. module Control.Lens.Loupe -- | A Loupe s t a b is almost a Lens. It can be -- composed on the left of other lenses, you can use cloneLens to -- promote it to a Lens, and it provides a minimalist lens-like -- interface. They can be used in an API where you need to pass around -- lenses inside containers or as monadic results. Unlike a -- ReifiedLens they can be composed and used directly, but they -- are slightly lower performance. type Loupe s t a b = LensLike (Context a b) s t a b -- | A Loupe-specific version of set storing :: Loupe s t a b -> b -> s -> t -- | A Loupe-specific version of (^.) (^#) :: s -> Loupe s t a b -> a -- | A Loupe-specific version of (.~) (#~) :: Loupe s t a b -> b -> s -> t -- | A Loupe-specific version of (%~) (#%~) :: Loupe s t a b -> (a -> b) -> s -> t -- | A Loupe-specific version of (%%~) (#%%~) :: Functor f => Loupe s t a b -> (a -> f b) -> s -> f t -- | Replace the target of a Loupe and return the new value. (<#~) :: Loupe s t a b -> b -> s -> (b, t) -- | Modify the target of a Loupe and return the result. (<#%~) :: Loupe s t a b -> (a -> b) -> s -> (b, t) -- | A Loupe-specific version of (.=) (#=) :: MonadState s m => Loupe s s a b -> b -> m () -- | A Loupe-specific version of (%=) (#%=) :: MonadState s m => Loupe s s a b -> (a -> b) -> m () -- | Modify the target of a Loupe in the current monadic state, -- returning an auxillary result. (#%%=) :: MonadState s m => Loupe s s a b -> (a -> (r, b)) -> m r -- | Replace the target of a Loupe in the current monadic state, -- returning the new value. (<#=) :: MonadState s m => Loupe s s a b -> b -> m b -- | Modify the target of a Loupe into your monad's state by a user -- supplied function and return the result. (<#%=) :: MonadState s m => Loupe s s a b -> (a -> b) -> m b -- |
-- type SimpleLoupe = Simple Loupe --type SimpleLoupe s a = Loupe s s a a module Control.Lens.Tuple -- | Provides access to 1st field of a tuple. class Field1 s t a b | s -> a, t -> b, s b -> t, t a -> s _1 :: Field1 s t a b => Lens s t a b -- | Provides access to the 2nd field of a tuple class Field2 s t a b | s -> a, t -> b, s b -> t, t a -> s _2 :: Field2 s t a b => Lens s t a b -- | Provides access to the 3rd field of a tuple class Field3 s t a b | s -> a, t -> b, s b -> t, t a -> s _3 :: Field3 s t a b => Lens s t a b -- | Provide access to the 4th field of a tuple class Field4 s t a b | s -> a, t -> b, s b -> t, t a -> s _4 :: Field4 s t a b => Lens s t a b -- | Provides access to the 5th field of a tuple class Field5 s t a b | s -> a, t -> b, s b -> t, t a -> s _5 :: Field5 s t a b => Lens s t a b -- | Provides access to the 6th element of a tuple class Field6 s t a b | s -> a, t -> b, s b -> t, t a -> s _6 :: Field6 s t a b => Lens s t a b -- | Provide access to the 7th field of a tuple class Field7 s t a b | s -> a, t -> b, s b -> t, t a -> s _7 :: Field7 s t a b => Lens s t a b -- | Provide access to the 8th field of a tuple class Field8 s t a b | s -> a, t -> b, s b -> t, t a -> s _8 :: Field8 s t a b => Lens s t a b -- | Provides access to the 9th field of a tuple class Field9 s t a b | s -> a, t -> b, s b -> t, t a -> s _9 :: Field9 s t a b => Lens s t a b instance Field9 (a, b, c, d, e, f, g, h, i) (a, b, c, d, e, f, g, h, i') i i' instance Field8 (a, b, c, d, e, f, g, h, i) (a, b, c, d, e, f, g, h', i) h h' instance Field8 (a, b, c, d, e, f, g, h) (a, b, c, d, e, f, g, h') h h' instance Field7 (a, b, c, d, e, f, g, h, i) (a, b, c, d, e, f, g', h, i) g g' instance Field7 (a, b, c, d, e, f, g, h) (a, b, c, d, e, f, g', h) g g' instance Field7 (a, b, c, d, e, f, g) (a, b, c, d, e, f, g') g g' instance Field6 (a, b, c, d, e, f, g, h, i) (a, b, c, d, e, f', g, h, i) f f' instance Field6 (a, b, c, d, e, f, g, h) (a, b, c, d, e, f', g, h) f f' instance Field6 (a, b, c, d, e, f, g) (a, b, c, d, e, f', g) f f' instance Field6 (a, b, c, d, e, f) (a, b, c, d, e, f') f f' instance Field5 (a, b, c, d, e, f, g, h, i) (a, b, c, d, e', f, g, h, i) e e' instance Field5 (a, b, c, d, e, f, g, h) (a, b, c, d, e', f, g, h) e e' instance Field5 (a, b, c, d, e, f, g) (a, b, c, d, e', f, g) e e' instance Field5 (a, b, c, d, e, f) (a, b, c, d, e', f) e e' instance Field5 (a, b, c, d, e) (a, b, c, d, e') e e' instance Field4 (a, b, c, d, e, f, g, h, i) (a, b, c, d', e, f, g, h, i) d d' instance Field4 (a, b, c, d, e, f, g, h) (a, b, c, d', e, f, g, h) d d' instance Field4 (a, b, c, d, e, f, g) (a, b, c, d', e, f, g) d d' instance Field4 (a, b, c, d, e, f) (a, b, c, d', e, f) d d' instance Field4 (a, b, c, d, e) (a, b, c, d', e) d d' instance Field4 (a, b, c, d) (a, b, c, d') d d' instance Field3 (a, b, c, d, e, f, g, h, i) (a, b, c', d, e, f, g, h, i) c c' instance Field3 (a, b, c, d, e, f, g, h) (a, b, c', d, e, f, g, h) c c' instance Field3 (a, b, c, d, e, f, g) (a, b, c', d, e, f, g) c c' instance Field3 (a, b, c, d, e, f) (a, b, c', d, e, f) c c' instance Field3 (a, b, c, d, e) (a, b, c', d, e) c c' instance Field3 (a, b, c, d) (a, b, c', d) c c' instance Field3 (a, b, c) (a, b, c') c c' instance Field2 (a, b, c, d, e, f, g, h, i) (a, b', c, d, e, f, g, h, i) b b' instance Field2 (a, b, c, d, e, f, g, h) (a, b', c, d, e, f, g, h) b b' instance Field2 (a, b, c, d, e, f, g) (a, b', c, d, e, f, g) b b' instance Field2 (a, b, c, d, e, f) (a, b', c, d, e, f) b b' instance Field2 (a, b, c, d, e) (a, b', c, d, e) b b' instance Field2 (a, b, c, d) (a, b', c, d) b b' instance Field2 (a, b, c) (a, b', c) b b' instance Field2 (a, b) (a, b') b b' instance Field1 (a, b, c, d, e, f, g, h, i) (a', b, c, d, e, f, g, h, i) a a' instance Field1 (a, b, c, d, e, f, g, h) (a', b, c, d, e, f, g, h) a a' instance Field1 (a, b, c, d, e, f, g) (a', b, c, d, e, f, g) a a' instance Field1 (a, b, c, d, e, f) (a', b, c, d, e, f) a a' instance Field1 (a, b, c, d, e) (a', b, c, d, e) a a' instance Field1 (a, b, c, d) (a', b, c, d) a a' instance Field1 (a, b, c) (a', b, c) a a' instance Field1 (a, b) (a', b) a a' -- | A Setter s t a b is a generalization of fmap -- from Functor. It allows you to map into a structure and change -- out the contents, but it isn't strong enough to allow you to enumerate -- those contents. Starting with fmap :: Functor f => (a -- -> b) -> f a -> f b we monomorphize the type to obtain -- (a -> b) -> s -> t and then decorate it with -- Identity to obtain -- --
-- type Setter s t a b = (a -> Identity b) -> s -> Identity t ---- -- Every Traversal is a valid Setter, since Identity -- is Applicative. -- -- Everything you can do with a Functor, you can do with a -- Setter. There are combinators that generalize fmap and -- (<$). module Control.Lens.Setter -- | The only Lens-like law that can apply to a Setter -- l is that -- --
-- set l y (set l x a) ≡ set l x a ---- -- You can't view a Setter in general, so the other two -- laws are irrelevant. -- -- However, two Functor laws apply to a Setter: -- --
-- over l id ≡ id -- over l f . over l g ≡ over l (f . g) ---- -- These an be stated more directly: -- --
-- l pure ≡ pure -- l f . untainted . l g ≡ l (f . untainted . g) ---- -- You can compose a Setter with a Lens or a -- Traversal using (.) from the Prelude and the result is -- always only a Setter and nothing more. type Setter s t a b = forall f. Settable f => (a -> f b) -> s -> f t -- | Build a Setter from a map-like function. -- -- Your supplied function f is required to satisfy: -- --
-- f id ≡ id -- f g . f h ≡ f (g . h) ---- -- Equational reasoning: -- --
-- sets . over ≡ id -- over . sets ≡ id ---- -- Another way to view sets is that it takes a "semantic editor -- combinator" and transforms it into a Setter. sets :: ((a -> b) -> s -> t) -> Setter s t a b -- | This setter can be used to map over all of the values in a -- Functor. -- --
-- fmap ≡ over mapped -- fmapDefault ≡ over traverse -- (<$) ≡ set mapped ---- --
-- >>> over mapped (+1) [1,2,3] -- [2,3,4] ---- --
-- >>> set mapped () [1,2,3] -- [(),(),()] ---- --
-- >>> mapped.mapped %~ (+1) $ [[1,2],[3]] -- [[2,3],[4]] ---- --
-- >>> over (mapped._2) length [("hello","world"),("leaders","!!!")]
-- [("hello",5),("leaders",3)]
--
mapped :: Functor f => Setter (f a) (f b) a b
-- | Modify the target of a Lens or all the targets of a
-- Setter or Traversal with a function.
--
-- -- fmap ≡ over mapped -- fmapDefault ≡ over traverse -- sets . over ≡ id -- over . sets ≡ id ---- --
-- >>> over mapped (*10) [1,2,3] -- [10,20,30] ---- --
-- >>> over _1 show (10,20)
-- ("10",20)
--
--
-- Another way to view over is to say that it transformers a
-- Setter into a "semantic editor combinator".
--
-- -- over :: Setter s t a b -> (a -> b) -> s -> t --over :: Setting s t a b -> (a -> b) -> s -> t -- | Modify the target of a Lens or all the targets of a -- Setter or Traversal with a function. This is an alias -- for over that is provided for consistency. -- --
-- mapOf ≡ over -- fmap ≡ mapOf mapped -- fmapDefault ≡ mapOf traverse -- sets . mapOf ≡ id -- mapOf . sets ≡ id ---- --
-- >>> mapOf mapped (+1) [1,2,3,4] -- [2,3,4,5] ---- --
-- >>> mapOf _1 (+1) (1,2) -- (2,2) ---- --
-- >>> mapOf both (+1) (1,2) -- (2,3) ---- --
-- mapOf :: Setter s t a b -> (a -> b) -> s -> t -- mapOf :: Iso s t a b -> (a -> b) -> s -> t -- mapOf :: Lens s t a b -> (a -> b) -> s -> t -- mapOf :: Traversal s t a b -> (a -> b) -> s -> t --mapOf :: Setting s t a b -> (a -> b) -> s -> t -- | Replace the target of a Lens or all of the targets of a -- Setter or Traversal with a constant value. -- --
-- (<$) ≡ set mapped ---- --
-- >>> set _2 "hello" (1,()) -- (1,"hello") ---- --
-- >>> set mapped () [1,2,3,4] -- [(),(),(),()] ---- -- Note: Attempting to set a Fold or Getter will -- fail at compile time with an relatively nice error message. -- --
-- set :: Setter s t a b -> b -> s -> t -- set :: Iso s t a b -> b -> s -> t -- set :: Lens s t a b -> b -> s -> t -- set :: Traversal s t a b -> b -> s -> t --set :: Setting s t a b -> b -> s -> t -- | Replace the target of a Lens or all of the targets of a -- Setter or Traversal with a constant value. -- -- This is an infix version of set, provided for consistency with -- (.=) -- --
-- f <$ a ≡ mapped .~ f $ a ---- --
-- >>> _1 .~ "hello" $ (42,"world")
-- ("hello","world")
--
--
-- -- (.~) :: Setter s t a b -> b -> s -> t -- (.~) :: Iso s t a b -> b -> s -> t -- (.~) :: Lens s t a b -> b -> s -> t -- (.~) :: Traversal s t a b -> b -> s -> t --(.~) :: Setting s t a b -> b -> s -> t -- | Modifies the target of a Lens or all of the targets of a -- Setter or Traversal with a user supplied function. -- -- This is an infix version of over -- --
-- fmap f ≡ mapped %~ f -- fmapDefault f ≡ traverse %~ f ---- --
-- >>> _2 %~ length $ (1,"hello") -- (1,5) ---- --
-- >>> traverse %~ (+1) $ [1,2,3] -- [2,3,4] ---- --
-- >>> _2 %~ (+1) $ (3,4) -- (3,5) ---- --
-- >>> traverse.traverse %~ length $ [["hello","world"],["!!!"]] -- [[5,5],[3]] ---- --
-- (%~) :: Setter s t a b -> (a -> b) -> s -> t -- (%~) :: Iso s t a b -> (a -> b) -> s -> t -- (%~) :: Lens s t a b -> (a -> b) -> s -> t -- (%~) :: Traversal s t a b -> (a -> b) -> s -> t --(%~) :: Setting s t a b -> (a -> b) -> s -> t -- | Increment the target(s) of a numerically valued Lens, -- Setter or Traversal -- --
-- >>> _1 +~ 1 $ (1,2) -- (2,2) ---- --
-- >>> both +~ 2 $ (5,6) -- (7,8) ---- --
-- (+~) :: Num a => Simple Setter s a -> a -> s -> s -- (+~) :: Num a => Simple Iso s a -> a -> s -> s -- (+~) :: Num a => Simple Lens s a -> a -> s -> s -- (+~) :: Num a => Simple Traversal s a -> a -> s -> s --(+~) :: Num a => Setting s t a a -> a -> s -> t -- | Decrement the target(s) of a numerically valued Lens, -- Iso, Setter or Traversal -- --
-- >>> _1 -~ 2 $ (1,2) -- (-1,2) ---- --
-- >>> mapped.mapped -~ 1 $ [[4,5],[6,7]] -- [[3,4],[5,6]] ---- --
-- (-~) :: Num a => Simple Setter s a -> a -> s -> s -- (-~) :: Num a => Simple Iso s a -> a -> s -> s -- (-~) :: Num a => Simple Lens s a -> a -> s -> s -- (-~) :: Num a => Simple Traversal s a -> a -> s -> s --(-~) :: Num a => Setting s t a a -> a -> s -> t -- | Multiply the target(s) of a numerically valued Lens, -- Iso, Setter or Traversal -- --
-- >>> _2 *~ 4 $ (1,2) -- (1,8) ---- --
-- >>> mapped *~ 2 $ Just 24 -- Just 48 ---- --
-- (*~) :: Num a => Simple Setter s a -> a -> s -> s -- (*~) :: Num a => Simple Iso s a -> a -> s -> s -- (*~) :: Num a => Simple Lens s a -> a -> s -> s -- (*~) :: Num a => Simple Traversal s a -> a -> s -> s --(*~) :: Num a => Setting s t a a -> a -> s -> t -- | Divide the target(s) of a numerically valued Lens, Iso, -- Setter or Traversal -- --
-- >>> _2 //~ 2 $ ("Hawaii",10)
-- ("Hawaii",5.0)
--
--
-- -- (//~) :: Fractional a => Simple Setter s a -> a -> s -> s -- (//~) :: Fractional a => Simple Iso s a -> a -> s -> s -- (//~) :: Fractional a => Simple Lens s a -> a -> s -> s -- (//~) :: Fractional a => Simple Traversal s a -> a -> s -> s --(//~) :: Fractional s => Setting a b s s -> s -> a -> b -- | Raise the target(s) of a numerically valued Lens, Setter -- or Traversal to a non-negative integral power -- --
-- >>> _2 ^~ 2 $ (1,3) -- (1,9) ---- --
-- (^~) :: (Num a, Integral e) => Simple Setter s a -> e -> s -> s -- (^~) :: (Num a, Integral e) => Simple Iso s a -> e -> s -> s -- (^~) :: (Num a, Integral e) => Simple Lens s a -> e -> s -> s -- (^~) :: (Num a, Integral e) => Simple Traversal s a -> e -> s -> s --(^~) :: (Num a, Integral e) => Setting s t a a -> e -> s -> t -- | Raise the target(s) of a fractionally valued Lens, -- Setter or Traversal to an integral power -- --
-- >>> _2 ^^~ (-1) $ (1,2) -- (1,0.5) ---- --
-- (^^~) :: (Fractional a, Integral e) => Simple Setter s a -> e -> s -> s -- (^^~) :: (Fractional a, Integral e) => Simple Iso s a -> e -> s -> s -- (^^~) :: (Fractional a, Integral e) => Simple Lens s a -> e -> s -> s -- (^^~) :: (Fractional a, Integral e) => Simple Traversal s a -> e -> s -> s --(^^~) :: (Fractional a, Integral e) => Setting s t a a -> e -> s -> t -- | Raise the target(s) of a floating-point valued Lens, -- Setter or Traversal to an arbitrary power. -- --
-- >>> _2 **~ pi $ (1,3) -- (1,31.54428070019754) ---- --
-- (**~) :: Floating a => Simple Setter s a -> a -> s -> s -- (**~) :: Floating a => Simple Iso s a -> a -> s -> s -- (**~) :: Floating a => Simple Lens s a -> a -> s -> s -- (**~) :: Floating a => Simple Traversal s a -> a -> s -> s --(**~) :: Floating a => Setting s t a a -> a -> s -> t -- | Logically || the target(s) of a Bool-valued Lens -- or Setter -- --
-- >>> both ||~ True $ (False,True) -- (True,True) ---- --
-- >>> both ||~ False $ (False,True) -- (False,True) ---- --
-- (||~) :: Simple Setter s Bool -> Bool -> s -> s -- (||~) :: Simple Iso s Bool -> Bool -> s -> s -- (||~) :: Simple Lens s Bool -> Bool -> s -> s -- (||~) :: Simple Traversal s Bool -> Bool -> s -> s --(||~) :: Setting s t Bool Bool -> Bool -> s -> t -- | Logically && the target(s) of a Bool-valued -- Lens or Setter -- --
-- >>> both &&~ True $ (False, True) -- (False,True) ---- --
-- >>> both &&~ False $ (False, True) -- (False,False) ---- --
-- (&&~) :: Simple Setter s Bool -> Bool -> s -> s -- (&&~) :: Simple Iso s Bool -> Bool -> s -> s -- (&&~) :: Simple Lens s Bool -> Bool -> s -> s -- (&&~) :: Simple Traversal s Bool -> Bool -> s -> s --(&&~) :: Setting s t Bool Bool -> Bool -> s -> t -- | Set with pass-through -- -- This is mostly present for consistency, but may be useful for for -- chaining assignments -- -- If you do not need a copy of the intermediate result, then using l -- .~ t directly is a good idea. -- --
-- >>> _3 <.~ "world" $ ("good","morning","vietnam")
-- ("world",("good","morning","world"))
--
--
--
-- >>> import Data.Map as Map
--
-- >>> _2.at "hello" <.~ Just "world" $ (42,Map.fromList [("goodnight","gracie")])
-- (Just "world",(42,fromList [("goodnight","gracie"),("hello","world")]))
--
--
-- -- (<.~) :: Setter s t a b -> b -> s -> (b, t) -- (<.~) :: Iso s t a b -> b -> s -> (b, t) -- (<.~) :: Lens s t a b -> b -> s -> (b, t) -- (<.~) :: Traversal s t a b -> b -> s -> (b, t) --(<.~) :: Setting s t a b -> b -> s -> (b, t) -- | Set the target of a Lens, Traversal or Setter to -- Just a value. -- --
-- l ?~ t ≡ set l (Just t) ---- --
-- (?~) :: Setter s t a (Maybe b) -> b -> s -> t -- (?~) :: Iso s t a (Maybe b) -> b -> s -> t -- (?~) :: Lens s t a (Maybe b) -> b -> s -> t -- (?~) :: Traversal s t a (Maybe b) -> b -> s -> t --(?~) :: Setting s t a (Maybe b) -> b -> s -> t -- | Set to Just a value with pass-through -- -- This is mostly present for consistency, but may be useful for for -- chaining assignments -- -- If you do not need a copy of the intermediate result, then using l -- ?~ d directly is a good idea. -- --
-- >>> import Data.Map as Map
--
-- >>> _2.at "hello" <?~ "world" $ (42,Map.fromList [("goodnight","gracie")])
-- ("world",(42,fromList [("goodnight","gracie"),("hello","world")]))
--
--
-- -- (<?~) :: Setter s t a b -> (Maybe b) -> s -> (b, t) -- (<?~) :: Iso s t a (Maybe b) -> b -> s -> (b, t) -- (<?~) :: Lens s t a (Maybe b) -> b -> s -> (b, t) -- (<?~) :: Traversal s t a (Maybe b) -> b -> s -> (b, t) --(~) :: Setting s t a (Maybe b) -> b -> s -> (b, t) -- | Replace the target of a Lens or all of the targets of a -- Setter or Traversal in our monadic state with a new -- value, irrespective of the old. -- -- This is an alias for (.=). -- --
-- assign :: MonadState s m => Simple Iso s a -> a -> m () -- assign :: MonadState s m => Simple Lens s a -> a -> m () -- assign :: MonadState s m => Simple Traversal s a -> a -> m () -- assign :: MonadState s m => Simple Setter s a -> a -> m () --assign :: MonadState s m => Setting s s a b -> b -> m () -- | Replace the target of a Lens or all of the targets of a -- Setter or Traversal in our monadic state with a new -- value, irrespective of the old. -- -- This is an infix version of assign. -- --
-- (.=) :: MonadState s m => Simple Iso s a -> a -> m () -- (.=) :: MonadState s m => Simple Lens s a -> a -> m () -- (.=) :: MonadState s m => Simple Traversal s a -> a -> m () -- (.=) :: MonadState s m => Simple Setter s a -> a -> m () ---- -- It puts the state in the monad or it gets the hose again. (.=) :: MonadState s m => Setting s s a b -> b -> m () -- | Map over the target of a Lens or all of the targets of a -- Setter or Traversal in our monadic state. -- --
-- (%=) :: MonadState s m => Simple Iso s a -> (a -> a) -> m () -- (%=) :: MonadState s m => Simple Lens s a -> (a -> a) -> m () -- (%=) :: MonadState s m => Simple Traversal s a -> (a -> a) -> m () -- (%=) :: MonadState s m => Simple Setter s a -> (a -> a) -> m () --(%=) :: MonadState s m => Setting s s a b -> (a -> b) -> m () -- | Modify the target(s) of a Simple Lens, Iso, -- Setter or Traversal by adding a value -- -- Example: -- --
-- fresh :: MonadState Int m => m Int -- fresh = do -- id += 1 -- use id ---- --
-- (+=) :: (MonadState s m, Num a) => Simple Setter s a -> a -> m () -- (+=) :: (MonadState s m, Num a) => Simple Iso s a -> a -> m () -- (+=) :: (MonadState s m, Num a) => Simple Lens s a -> a -> m () -- (+=) :: (MonadState s m, Num a) => Simple Traversal s a -> a -> m () --(+=) :: (MonadState s m, Num a) => SimpleSetting s a -> a -> m () -- | Modify the target(s) of a Simple Lens, Iso, -- Setter or Traversal by subtracting a value -- --
-- (-=) :: (MonadState s m, Num a) => Simple Setter s a -> a -> m () -- (-=) :: (MonadState s m, Num a) => Simple Iso s a -> a -> m () -- (-=) :: (MonadState s m, Num a) => Simple Lens s a -> a -> m () -- (-=) :: (MonadState s m, Num a) => Simple Traversal s a -> a -> m () --(-=) :: (MonadState s m, Num a) => SimpleSetting s a -> a -> m () -- | Modify the target(s) of a Simple Lens, Iso, -- Setter or Traversal by multiplying by value. -- --
-- (*=) :: (MonadState s m, Num a) => Simple Setter s a -> a -> m () -- (*=) :: (MonadState s m, Num a) => Simple Iso s a -> a -> m () -- (*=) :: (MonadState s m, Num a) => Simple Lens s a -> a -> m () -- (*=) :: (MonadState s m, Num a) => Simple Traversal s a -> a -> m () --(*=) :: (MonadState s m, Num a) => SimpleSetting s a -> a -> m () -- | Modify the target(s) of a Simple Lens, Iso, -- Setter or Traversal by dividing by a value. -- --
-- (//=) :: (MonadState s m, Fractional a) => Simple Setter s a -> a -> m () -- (//=) :: (MonadState s m, Fractional a) => Simple Iso s a -> a -> m () -- (//=) :: (MonadState s m, Fractional a) => Simple Lens s a -> a -> m () -- (//=) :: (MonadState s m, Fractional a) => Simple Traversal s a -> a -> m () --(//=) :: (MonadState s m, Fractional a) => SimpleSetting s a -> a -> m () -- | Raise the target(s) of a numerically valued Lens, Setter -- or Traversal to a non-negative integral power. -- --
-- (^=) :: (MonadState s m, Num a, Integral e) => Simple Setter s a -> e -> m () -- (^=) :: (MonadState s m, Num a, Integral e) => Simple Iso s a -> e -> m () -- (^=) :: (MonadState s m, Num a, Integral e) => Simple Lens s a -> e -> m () -- (^=) :: (MonadState s m, Num a, Integral e) => Simple Traversal s a -> e -> m () --(^=) :: (MonadState s m, Num a, Integral e) => SimpleSetting s a -> e -> m () -- | Raise the target(s) of a numerically valued Lens, Setter -- or Traversal to an integral power. -- --
-- (^^=) :: (MonadState s m, Fractional a, Integral e) => Simple Setter s a -> e -> m () -- (^^=) :: (MonadState s m, Fractional a, Integral e) => Simple Iso s a -> e -> m () -- (^^=) :: (MonadState s m, Fractional a, Integral e) => Simple Lens s a -> e -> m () -- (^^=) :: (MonadState s m, Fractional a, Integral e) => Simple Traversal s a -> e -> m () --(^^=) :: (MonadState s m, Fractional a, Integral e) => SimpleSetting s a -> e -> m () -- | Raise the target(s) of a numerically valued Lens, Setter -- or Traversal to an arbitrary power -- --
-- (**=) :: (MonadState s m, Floating a) => Simple Setter s a -> a -> m () -- (**=) :: (MonadState s m, Floating a) => Simple Iso s a -> a -> m () -- (**=) :: (MonadState s m, Floating a) => Simple Lens s a -> a -> m () -- (**=) :: (MonadState s m, Floating a) => Simple Traversal s a -> a -> m () --(**=) :: (MonadState s m, Floating a) => SimpleSetting s a -> a -> m () -- | Modify the target(s) of a Simple Lens, 'Iso, -- Setter or Traversal by taking their logical || -- with a value -- --
-- (||=) :: MonadState s m => Simple Setter s Bool -> Bool -> m () -- (||=) :: MonadState s m => Simple Iso s Bool -> Bool -> m () -- (||=) :: MonadState s m => Simple Lens s Bool -> Bool -> m () -- (||=) :: MonadState s m => Simple Traversal s Bool -> Bool -> m () --(||=) :: MonadState s m => SimpleSetting s Bool -> Bool -> m () -- | Modify the target(s) of a Simple Lens, Iso, -- Setter or Traversal by taking their logical -- && with a value -- --
-- (&&=) :: MonadState s m => Simple Setter s Bool -> Bool -> m () -- (&&=) :: MonadState s m => Simple Iso s Bool -> Bool -> m () -- (&&=) :: MonadState s m => Simple Lens s Bool -> Bool -> m () -- (&&=) :: MonadState s m => Simple Traversal s Bool -> Bool -> m () --(&&=) :: MonadState s m => SimpleSetting s Bool -> Bool -> m () -- | Set with pass-through -- -- This is useful for chaining assignment without round-tripping through -- your monad stack. -- --
-- do x <- _2 <.= ninety_nine_bottles_of_beer_on_the_wall ---- -- If you do not need a copy of the intermediate result, then using l -- .= d will avoid unused binding warnings -- --
-- (<.=) :: MonadState s m => Setter s s a b -> b -> m b -- (<.=) :: MonadState s m => Iso s s a b -> b -> m b -- (<.=) :: MonadState s m => Lens s s a b -> b -> m b -- (<.=) :: MonadState s m => Traversal s s a b -> b -> m b --(<.=) :: MonadState s m => Setting s s a b -> b -> m b -- | Replace the target of a Lens or all of the targets of a -- Setter or Traversal in our monadic state with -- Just a new value, irrespective of the old. -- --
-- (?=) :: MonadState s m => Simple Iso s (Maybe a) -> a -> m () -- (?=) :: MonadState s m => Simple Lens s (Maybe a) -> a -> m () -- (?=) :: MonadState s m => Simple Traversal s (Maybe a) -> a -> m () -- (?=) :: MonadState s m => Simple Setter s (Maybe a) -> a -> m () --(?=) :: MonadState s m => Setting s s a (Maybe b) -> b -> m () -- | Set Just a value with pass-through -- -- This is useful for chaining assignment without round-tripping through -- your monad stack. -- --
-- do x <- at foo <?= ninety_nine_bottles_of_beer_on_the_wall ---- -- If you do not need a copy of the intermediate result, then using l -- ?= d will avoid unused binding warnings -- --
-- (<?=) :: MonadState s m => Setter s s a (Maybe b) -> b -> m b -- (<?=) :: MonadState s m => Iso s s a (Maybe b) -> b -> m b -- (<?=) :: MonadState s m => Lens s s a (Maybe b) -> b -> m b -- (<?=) :: MonadState s m => Traversal s s a (Maybe b) -> b -> m b --(=) :: MonadState s m => Setting s s a (Maybe b) -> b -> m b -- | Run a monadic action, and set all of the targets of a Lens, -- Setter or Traversal to its result. -- --
-- (<~) :: MonadState s m => Iso s s a b -> m b -> m () -- (<~) :: MonadState s m => Lens s s a b -> m b -> m () -- (<~) :: MonadState s m => Traversal s s a b -> m b -> m () -- (<~) :: MonadState s m => Setter s s a b -> m b -> m () ---- -- As a reasonable mnemonic, this lets you store the result of a monadic -- action in a lens rather than in a local variable. -- --
-- do foo <- bar -- ... ---- -- will store the result in a variable, while -- --
-- do foo <~ bar -- ... ---- -- will store the result in a Lens, Setter, or -- Traversal. (<~) :: MonadState s m => Setting s s a b -> m b -> m () -- | Reify a setter so it can be stored safely in a container. newtype ReifiedSetter s t a b ReifySetter :: Setter s t a b -> ReifiedSetter s t a b reflectSetter :: ReifiedSetter s t a b -> Setter s t a b -- | Running a Setter instantiates it to a concrete type. -- -- When consuming a setter directly to perform a mapping, you can use -- this type, but most user code will not need to use this type. -- -- By choosing Mutator rather than Identity, we get nicer -- error messages. type Setting s t a b = (a -> Mutator b) -> s -> Mutator t -- | This is a useful alias for use when consuming a SimpleSetter. -- -- Most user code will never have to use this type. -- --
-- type SimpleSetting m = Simple Setting --type SimpleSetting s a = Setting s s a a -- | A Simple Setter is just a Setter that doesn't change the types. -- -- These are particularly common when talking about monomorphic -- containers. e.g. -- --
-- sets Data.Text.map :: SimpleSetter Text Char ---- --
-- type SimpleSetter = Simple Setter --type SimpleSetter s a = Setter s s a a -- |
-- type SimpleReifiedSetter = Simple ReifiedSetter --type SimpleReifiedSetter s a = ReifiedSetter s s a a -- | Anything Settable must be isomorphic to the Identity -- Functor. class Applicative f => Settable f where untainted# f = untainted . f -- | Mutator is just a renamed Identity functor to give -- better error messages when someone attempts to use a getter as a -- setter. -- -- Most user code will never need to see this type. data Mutator a -- | A Getter s a is just any function (s -> -- a), which we've flipped into continuation passing style, (a -- -> r) -> s -> r and decorated with Accessor to -- obtain: -- --
-- type Getting r s t a b = (a -> Accessor r b) -> s -> Accessor r t ---- -- If we restrict access to knowledge about the type r and can -- work for any b and t, we could get: -- --
-- type Getter s a = forall r. Getting r s s a a ---- -- But we actually hide the use of Accessor behind a class -- Gettable to error messages from type class resolution rather -- than at unification time, where they are much uglier. -- --
-- type Getter s a = forall f. Gettable f => (a -> f a) -> s -> f s ---- -- Everything you can do with a function, you can do with a -- Getter, but note that because of the continuation passing style -- (.) composes them in the opposite order. -- -- Since it is only a function, every Getter obviously only -- retrieves a single value for a given input. module Control.Lens.Getter -- | A Getter describes how to retrieve a single value in a way that -- can be composed with other lens-like constructions. -- -- Unlike a Lens a Getter is read-only. Since a -- Getter cannot be used to write back there are no lens laws that -- can be applied to it. In fact, it is isomorphic to an arbitrary -- function from (a -> s). -- -- Moreover, a Getter can be used directly as a Fold, since -- it just ignores the Applicative. type Getter s a = forall f. Gettable f => (a -> f a) -> s -> f s -- | Most Getter combinators are able to be used with both a -- Getter or a Fold in limited situations, to do so, they -- need to be monomorphic in what we are going to extract with -- Const. To be compatible with Lens, Traversal -- and Iso we also restricted choices of the irrelevant t -- and b parameters. -- -- If a function accepts a Getting r s t a b, then when -- r is a Monoid, then you can pass a Fold (or -- Traversal), otherwise you can only pass this a Getter or -- Lens. type Getting r s t a b = (a -> Accessor r b) -> s -> Accessor r t -- | Build a Getter from an arbitrary Haskell function. -- --
-- to f . to g = to (g . f) ---- --
-- a ^. to f = f a ---- --
-- >>> ("hello","world")^.to snd
-- "world"
--
--
-- -- >>> 5^.to succ -- 6 ---- --
-- >>> (0, -5)^._2.to abs -- 5 --to :: (s -> a) -> Getter s a -- | View the value pointed to by a Getter or Lens or the -- result of folding over all the results of a Fold or -- Traversal that points at a monoidal values. -- -- This is the same operation as view with the arguments flipped. -- -- The fixity and semantics are such that subsequent field accesses can -- be performed with (.) -- --
-- >>> ("hello","world")^._2
-- "world"
--
--
-- -- >>> import Data.Complex -- -- >>> ((0, 1 :+ 2), 3)^._1._2.to magnitude -- 2.23606797749979 ---- --
-- (^.) :: s -> Getter s a -> a -- (^.) :: Monoid m => s -> Fold s m -> m -- (^.) :: s -> Simple Iso s a -> a -- (^.) :: s -> Simple Lens s a -> a -- (^.) :: Monoid m => s -> Simple Traversal s m -> m --(^.) :: s -> Getting a s t a b -> a -- | View the value pointed to by a Getter, Iso or -- Lens or the result of folding over all the results of a -- Fold or Traversal that points at a monoidal values. -- -- This is the same operation as view, only infix. -- --
-- to f ^$ x = f x ---- --
-- >>> _2 ^$ (1, "hello") -- "hello" ---- --
-- (^$) :: Getter s a -> s -> a -- (^$) :: Monoid m => Fold s m -> s -> m -- (^$) :: Simple Iso s a -> s -> a -- (^$) :: Simple Lens s a -> s -> a -- (^$) :: Monoid m => Simple Traversal s m -> s -> m --(^$) :: Getting a s t a b -> s -> a -- | Passes the result of the left side to the function on the right side -- (forward pipe operator). -- -- This is the flipped version of ($), which is more common in -- languages like F# as (|>) where it is needed for -- inference. Here it is supplied for notational convenience and given a -- precedence that allows it to be nested inside uses of ($). -- --
-- >>> "hello" % length % succ -- 6 --(%) :: a -> (a -> b) -> b -- | A version of (%) with much tighter precedence that can be -- interleaved with (^.) -- --
-- >>> "hello"^%length
-- 5
--
-- >>> import Data.List.Lens
--
-- >>> ("hello","world")^._1^%reverse^._head
-- 'o'
--
(^%) :: a -> (a -> b) -> b
-- | View the value pointed to by a Getter, Iso or
-- Lens or the result of folding over all the results of a
-- Fold or Traversal that points at a monoidal values.
--
-- -- view . to = id ---- --
-- >>> view _2 (1,"hello") -- "hello" ---- --
-- >>> view (to succ) 5 -- 6 ---- --
-- >>> view (_2._1) ("hello",("world","!!!"))
-- "world"
--
--
-- It may be useful to think of view as having one of these more
-- restrictive signatures:
--
-- -- view :: Getter s a -> s -> a -- view :: Monoid m => Fold s m -> s -> m -- view :: Simple Iso s a -> s -> a -- view :: Simple Lens s a -> s -> a -- view :: Monoid m => Simple Traversal s m -> s -> m --view :: Getting a s t a b -> s -> a -- | View the value of a Getter, Iso, Lens or the -- result of folding over the result of mapping the targets of a -- Fold or Traversal. -- -- It may be useful to think of views as having these more -- restrictive signatures: -- --
-- views l f = view (l . to f) ---- --
-- >>> views _2 length (1,"hello") -- 5 ---- --
-- views :: Getter s a -> (a -> r) -> s -> r -- views :: Monoid m => Fold s a -> (a -> m) -> s -> m -- views :: Simple Iso s a -> (a -> r) -> s -> r -- views :: Simple Lens s a -> (a -> r) -> s -> r -- views :: Monoid m => Simple Traversal s a -> (a -> m) -> s -> m --views :: Getting r s t a b -> (a -> r) -> s -> r -- | Use the target of a Lens, Iso, or Getter in the -- current state, or use a summary of a Fold or Traversal -- that points to a monoidal value. -- --
-- use :: MonadState s m => Getter s a -> m a -- use :: (MonadState s m, Monoid r) => Fold s r -> m r -- use :: MonadState s m => Simple Iso s a -> m a -- use :: MonadState s m => Simple Lens s a -> m a -- use :: (MonadState s m, Monoid r) => Simple Traversal s r -> m r --use :: MonadState s m => Getting a s t a b -> m a -- | Use the target of a Lens, Iso or Getter in the -- current state, or use a summary of a Fold or Traversal -- that points to a monoidal value. -- --
-- uses :: MonadState s m => Getter s a -> (a -> r) -> m r -- uses :: (MonadState s m, Monoid r) => Fold s a -> (a -> r) -> m r -- uses :: MonadState s m => Simple Lens s a -> (a -> r) -> m r -- uses :: MonadState s m => Simple Iso s a -> (a -> r) -> m r -- uses :: (MonadState s m, Monoid r) => Simple Traversal s a -> (a -> r) -> m r --uses :: MonadState s m => Getting r s t a b -> (a -> r) -> m r -- | Query the target of a Lens, Iso or Getter in the -- current state, or use a summary of a Fold or Traversal -- that points to a monoidal value. -- --
-- query :: MonadReader s m => Getter s a -> m a -- query :: (MonadReader s m, Monoid a) => Fold s a -> m a -- query :: MonadReader s m => Simple Iso s a -> m a -- query :: MonadReader s m => Simple Lens s a -> m a -- query :: (MonadReader s m, Monoid a) => Simple Traversal s a -> m a --query :: MonadReader s m => Getting a s t a b -> m a -- | Use the target of a Lens, Iso or Getter in the -- current state, or use a summary of a Fold or Traversal -- that points to a monoidal value. -- --
-- queries :: MonadReader s m => Getter s a -> (a -> r) -> m r -- queries :: (MonadReader s m, Monoid a) => Fold s a -> (a -> r) -> m r -- queries :: MonadReader s m => Simple Iso s a -> (a -> r) -> m r -- queries :: MonadReader s m => Simple Lens s a -> (a -> r) -> m r -- queries :: (MonadReader s m, Monoid a) => Simple Traversal s a -> (a -> r) -> m r --queries :: MonadReader s m => Getting r s t a b -> (a -> r) -> m r -- | Useful for storing getters in containers. newtype ReifiedGetter s a ReifyGetter :: Getter s a -> ReifiedGetter s a reflectGetter :: ReifiedGetter s a -> Getter s a -- | Generalizing Const so we can apply simple Applicative -- transformations to it and so we can get nicer error messages -- -- A Gettable Functor ignores its argument, which it -- carries solely as a phantom type parameter. -- -- To ensure this, an instance of Gettable is required to satisfy: -- --
-- id = fmap f = coerce --class Functor f => Gettable f -- | Used instead of Const to report -- --
-- No instance of (Settable Accessor) ---- -- when the user attempts to misuse a Setter as a Getter, -- rather than a monolithic unification error. data Accessor r a module Control.Lens.Iso -- | Isomorphim families can be composed with other lenses using either -- (.) and id from the Prelude or from Control.Category. -- However, if you compose them with each other using (.) from the -- Prelude, they will be dumbed down to a mere Lens. -- --
-- import Control.Category -- import Prelude hiding ((.),id) ---- --
-- type Iso s t a b = forall k f. (Isomorphic k, Functor f) => Overloaded k f s t a b --type Iso s t a b = forall k f. (Isomorphic k, Functor f) => k (a -> f b) (s -> f t) -- | An commonly used infix alias for Simple Iso type (:<->) s a = Iso s s a a -- | Build a simple isomorphism from a pair of inverse functions -- --
-- view (iso f g) ≡ f -- view (from (iso f g)) ≡ g -- set (isos f g) h ≡ g . h . f -- set (from (iso f g')) h ≡ f . h . g ---- --
-- iso :: (s -> a) -> (a -> s) -> Simple Iso s a --iso :: (Isomorphic k, Functor f) => (s -> a) -> (a -> s) -> k (a -> f a) (s -> f s) -- | Build an isomorphism family from two pairs of inverse functions -- --
-- view (isos sa as tb bt) ≡ sa -- view (from (isos sa as tb bt)) ≡ as -- set (isos sa as tb bt) ab ≡ bt . ab . sa -- set (from (isos ac ca bd db')) ab ≡ bd . ab . ca -- set (from (isos sa as tb bt')) s t ≡ tb . st . as ---- --
-- isos :: (s -> a) -> (a -> s) -> (t -> b) -> (b -> t) -> Iso s t a b --isos :: (Isomorphic k, Functor f) => (s -> a) -> (a -> s) -> (t -> b) -> (b -> t) -> k (a -> f b) (s -> f t) -- | Based on ala from Conor McBride's work on Epigram. -- --
-- >>> :m + Data.Monoid.Lens Data.Foldable -- -- >>> ala _sum foldMap [1,2,3,4] -- 10 --ala :: Simple Iso s a -> ((s -> a) -> e -> a) -> e -> s -- | Based on ala' from Conor McBride's work on Epigram. -- -- Mnemonically, the German auf plays a similar role to à -- la, and the combinator is ala with an extra function -- argument. auf :: Simple Iso s a -> ((b -> a) -> e -> a) -> (b -> s) -> e -> s -- | The opposite of working over a Setter is working under -- an Isomorphism. -- --
-- under = over . from ---- --
-- under :: Iso s t a b -> (s -> t) -> a -> b --under :: Isomorphism (a -> Mutator b) (s -> Mutator t) -> (s -> t) -> a -> b -- | Invert an isomorphism. -- -- Note to compose an isomorphism and receive an isomorphism in turn -- you'll need to use Category -- --
-- from (from l) ≡ l ---- -- If you imported . from Control.Category, then: -- --
-- from l . from r ≡ from (r . l) --from :: Isomorphic k => Isomorphism a b -> k b a -- | Convert from an Isomorphism back to any Isomorphic -- value. -- -- This is useful when you need to store an isomoprhism as a data type -- inside a container and later reconstitute it as an overloaded -- function. via :: Isomorphic k => Isomorphism a b -> k a b -- | A concrete data type for isomorphisms. -- -- This lets you place an isomorphism inside a container without using -- ImpredicativeTypes. data Isomorphism a b Isomorphism :: (a -> b) -> (b -> a) -> Isomorphism a b -- | Used to provide overloading of isomorphism application -- -- This is a Category with a canonical mapping to it from the -- category of isomorphisms over Haskell types. class Category k => Isomorphic k isomorphic :: Isomorphic k => (a -> b) -> (b -> a) -> k a b isomap :: Isomorphic k => ((a -> b) -> c -> d) -> ((b -> a) -> d -> c) -> k a b -> k c d -- | This isomorphism can be used to wrap or unwrap a value in Const -- --
-- x ^. _const ≡ Const x -- Const x ^. from _const ≡ x --_const :: Iso a b (Const a c) (Const b d) -- | This isomorphism can be used to wrap or unwrap a value in -- Identity. -- --
-- x^.identity ≡ Identity x -- Identity x ^. from identity ≡ x --identity :: Iso a b (Identity a) (Identity b) -- | Useful for storing isomorphisms in containers. newtype ReifiedIso s t a b ReifyIso :: Iso s t a b -> ReifiedIso s t a b reflectIso :: ReifiedIso s t a b -> Iso s t a b -- |
-- type SimpleIso = Simple Iso --type SimpleIso s a = Iso s s a a -- |
-- type SimpleReifiedIso = Simple ReifiedIso --type SimpleReifiedIso s a = ReifiedIso s s a a module Data.Set.Lens -- | This Setter can be used to change the type of a Set by -- mapping the elements to new values. -- -- Sadly, you can't create a valid Traversal for a Set, but -- you can manipulate it by reading using folded and reindexing it -- via setmapped. -- --
-- >>> over setmapped (+1) (fromList [1,2,3,4]) -- fromList [2,3,4,5] --setmapped :: (Ord i, Ord j) => Setter (Set i) (Set j) i j -- | Construct a set from a Getter, Fold, Traversal, -- Lens or Iso. -- --
-- >>> setOf (folded._2) [("hello",1),("world",2),("!!!",3)]
-- fromList [1,2,3]
--
--
-- -- setOf :: Getter s a -> s -> Set a -- setOf :: Ord a => Fold s a -> s -> Set a -- setOf :: Simple Iso s a -> s -> Set a -- setOf :: Simple Lens s a -> s -> Set a -- setOf :: Ord a => Simple Traversal s a -> s -> Set a --setOf :: Getting (Set a) s t a b -> s -> Set a module Control.Lens.Projection -- | A Projection is a Traversal that can also be turned -- around with by to obtain a Getter type Projection s t a b = forall k f. (Projective k s b, Applicative f) => k (a -> f b) (s -> f s) -- | Used to provide overloading of projections. class Projective k a d projective :: Projective k a d => (d -> a) -> (x -> y) -> k x y -- | Reflect a Projection. project :: Projective k s b => Overloaded (Project s b) f s s a b -> Overloaded k f s s a b -- | Turn a Projection around to get an embedding by :: Project s b (b -> Identity b) (s -> Identity s) -> Getter b s -- | A concrete Projection, suitable for storing in a container or -- extracting an embedding. data Project s b x y Project :: (b -> s) -> (x -> y) -> Project s b x y -- | Build a Projection projection :: (b -> s) -> (s -> Maybe a) -> Projection s t a b -- | Compose projections. stereo :: Projective k s a => Project t a y z -> Project s t x y -> k x z -- | Convert an Iso to a Projection. -- -- Ideally we would be able to use an Iso directly as a -- Projection, but this opens a can of worms. mirror :: Projective k s a => Simple Iso s a -> Simple Projection s a -- |
-- type SimpleProjection = Simple Projection --type SimpleProjection s a = Projection s s a a instance (s ~ s', b ~ b') => Projective (Project s b) s' b' instance Projective (->) a d -- | Corepresentable endofunctors represented by their polymorphic lenses -- -- The polymorphic lenses of the form (forall x. Lens (f x) -- x) each represent a distinct path into a functor f. If -- the functor is entirely characterized by assigning values to these -- paths, then the functor is representable. -- -- Consider the following example. -- --
-- import Control.Lens -- import Data.Distributive ---- --
-- data Pair a = Pair { _x :: a, _y :: a }
--
--
-- -- makeLenses ''Pair ---- --
-- instance Representable Pair where -- rep f = Pair (f x) (f y) ---- -- From there, you can get definitions for a number of instances for -- free. -- --
-- instance Applicative Pair where -- pure = pureRep -- (<*>) = apRep ---- --
-- instance Monad Pair where -- return = pureRep -- (>>=) = bindRep ---- --
-- instance Distributive Pair where -- distribute = distributeRep --module Control.Lens.Representable -- | Representable Functors. -- -- A Functor f is Representable if it is -- isomorphic to (x -> a) for some x. Nearly all such -- functors can be represented by choosing x to be the set of -- lenses that are polymorphic in the contents of the Functor, -- that is to say x = Rep f is a valid choice of -- x for (nearly) every Representable Functor. -- -- Note: Some sources refer to covariant representable functors as -- corepresentable functors, and leave the "representable" name to -- contravariant functors (those are isomorphic to (a -> x) -- for some x). -- -- As the covariant case is vastly more common, and both are often -- referred to as representable functors, we choose to call these -- functors Representable here. class Functor f => Representable f rep :: Representable f => (Rep f -> a) -> f a -- | The representation of a Representable Functor as Lenses type Rep f = forall a. Simple Lens (f a) a -- | fmapRep is a valid default definition for fmap for a -- Representable functor. -- --
-- fmapRep f m = rep $ i -> f (m ^. i) ---- -- Usage for a Representable Foo: -- --
-- instance Functor Foo where -- fmap = fmapRep --fmapRep :: Representable f => (a -> b) -> f a -> f b -- | pureRep is a valid default definition for pure and -- return for a Representable functor. -- --
-- pureRep = rep . const ---- -- Usage for a Representable Foo: -- --
-- instance Applicative Foo where -- pure = pureRep -- ... ---- --
-- instance Monad Foo where -- return = pureRep -- ... --pureRep :: Representable f => a -> f a -- | apRep is a valid default definition for (<*>) for -- a Representable functor. -- --
-- apRep mf ma = rep $ i -> mf ^. i $ ma ^. i ---- -- Usage for a Representable Foo: -- --
-- instance Applicative Foo where -- pure = pureRep -- (<*>) = apRep --apRep :: Representable f => f (a -> b) -> f a -> f b -- | bindRep is a valid default default definition for '(>>=)' -- for a representable functor. -- --
-- bindRep m f = rep $ i -> f (m ^. i) ^. i ---- -- Usage for a Representable Foo: -- --
-- instance Monad Foo where -- return = pureRep -- (>>=) = bindRep --bindRep :: Representable f => f a -> (a -> f b) -> f b -- | A default definition for distribute for a Representable -- Functor -- --
-- distributeRep wf = rep $ i -> fmap (^. i) wf ---- -- Usage for a Representable Foo: -- --
-- instance Distributive Foo where -- distribute = distributeRep --distributeRep :: (Representable f, Functor w) => w (f a) -> f (w a) -- | Sometimes you need to store a path lens into a container, but at least -- at this time, ImpredicativePolymorphism in GHC is somewhat -- lacking. -- -- This type provides a way to, say, store a [] of paths. newtype Path f Path :: Rep f -> Path f walk :: Path f -> Rep f -- | A Representable Functor has a fixed shape. This fills -- each position in it with a Path paths :: Representable f => f (Path f) -- | A version of rep that is an isomorphism. Predicativity requires -- that we wrap the Rep as a Key, however. tabulated :: Representable f => (Path f -> a) -> f a -- | Map over a Representable functor with access to the Lens -- for the current position -- --
-- rmap f m = rep $ i -> f i (m ^. i) --rmap :: Representable f => (Rep f -> a -> b) -> f a -> f b -- | Fold over a Representable functor with access to the current -- path as a Lens, yielding a Monoid rfoldMap :: (Representable f, Foldable f, Monoid m) => (Rep f -> a -> m) -> f a -> m -- | Fold over a Representable functor with access to the current -- path as a Lens. rfoldr :: (Representable f, Foldable f) => (Rep f -> a -> b -> b) -> b -> f a -> b -- | Traverse a Representable functor with access to the current -- path rtraverse :: (Representable f, Traversable f, Applicative g) => (Rep f -> a -> g b) -> f a -> g (f b) -- | Traverse a Representable functor with access to the current -- path as a Lens, discarding the result rtraverse_ :: (Representable f, Foldable f, Applicative g) => (Rep f -> a -> g b) -> f a -> g () -- | Traverse a Representable functor with access to the current -- path and a Lens (and the arguments flipped) rfor :: (Representable f, Traversable f, Applicative g) => f a -> (Rep f -> a -> g b) -> g (f b) -- | mapM over a Representable functor with access to the -- current path as a Lens rmapM :: (Representable f, Traversable f, Monad m) => (Rep f -> a -> m b) -> f a -> m (f b) -- | mapM over a Representable functor with access to the -- current path as a Lens, discarding the result rmapM_ :: (Representable f, Foldable f, Monad m) => (Rep f -> a -> m b) -> f a -> m () -- | mapM over a Representable functor with access to the -- current path as a Lens (with the arguments flipped) rforM :: (Representable f, Traversable f, Monad m) => f a -> (Rep f -> a -> m b) -> m (f b) -- | An IndexedSetter that walks an Representable -- Functor using a Path for an index. rmapped :: Representable f => IndexedSetter (Path f) (f a) (f b) a b -- | An IndexedFold that walks an Foldable -- Representable Functor using a Path for an index. rfolded :: (Representable f, Foldable f) => IndexedFold (Path f) (f a) a -- | An IndexedTraversal for a Traversable -- Representable Functor. rtraversed :: (Representable f, Traversable f) => IndexedTraversal (Path f) (f a) (f b) a b instance Eq e => Representable ((->) e) instance Representable Identity module Control.Lens.Zoom -- | This class allows us to use magnify part of the environment, -- changing the environment supplied by many different monad -- transformers. Unlike zoom this can change the environment of a -- deeply nested monad transformer. -- -- Also, unlike zoom, this can be used with any valid -- Getter, but cannot be used with a Traversal or -- Fold. class (MonadReader b m, MonadReader a n) => Magnify m n k b a | m -> b, n -> a, m a -> n, n b -> m magnify :: Magnify m n k b a => ((b -> k c b) -> a -> k c a) -> m c -> n c -- | This class allows us to use zoom in, changing the State -- supplied by many different monad transformers, potentially quite deep -- in a monad transformer stack. class (MonadState s m, MonadState t n) => Zoom m n k s t | m -> s k, n -> t k, m t -> n, n s -> m zoom :: (Zoom m n k s t, Monad m) => SimpleLensLike (k c) t s -> m c -> n c instance Magnify m n k b a => Magnify (IdentityT m) (IdentityT n) k b a instance (Monad m, Monoid w) => Magnify (RWST b w s m) (RWST a w s m) (EffectRWS w s m) b a instance (Monad m, Monoid w) => Magnify (RWST b w s m) (RWST a w s m) (EffectRWS w s m) b a instance Magnify ((->) b) ((->) a) Accessor b a instance Monad m => Magnify (ReaderT b m) (ReaderT a m) (Effect m) b a instance (Error e, Zoom m n k s t) => Zoom (ErrorT e m) (ErrorT e n) (FocusingErr e k) s t instance Zoom m n k s t => Zoom (MaybeT m) (MaybeT n) (FocusingMay k) s t instance Zoom m n k s t => Zoom (ListT m) (ListT n) (FocusingOn [] k) s t instance (Monoid w, Zoom m n k s t) => Zoom (WriterT w m) (WriterT w n) (FocusingPlus w k) s t instance (Monoid w, Zoom m n k s t) => Zoom (WriterT w m) (WriterT w n) (FocusingPlus w k) s t instance (Monoid w, Monad z) => Zoom (RWST r w s z) (RWST r w t z) (FocusingWith w z) s t instance (Monoid w, Monad z) => Zoom (RWST r w s z) (RWST r w t z) (FocusingWith w z) s t instance Zoom m n k s t => Zoom (IdentityT m) (IdentityT n) k s t instance Zoom m n k s t => Zoom (ReaderT e m) (ReaderT e n) k s t instance Monad z => Zoom (StateT s z) (StateT t z) (Focusing z) s t instance Monad z => Zoom (StateT s z) (StateT t z) (Focusing z) s t module Data.HashSet.Lens -- | This Setter can be used to change the type of a HashSet -- by mapping the elements to new values. -- -- Sadly, you can't create a valid Traversal for a Set, -- but you can manipulate it by reading using folded and -- reindexing it via setmapped. -- --
-- >>> over setmapped (+1) (fromList [1,2,3,4]) -- fromList [2,3,4,5] --setmapped :: (Eq i, Hashable i, Eq j, Hashable j) => Setter (HashSet i) (HashSet j) i j -- | Construct a set from a Getter, Fold, Traversal, -- Lens or Iso. -- --
-- >>> setOf (folded._2) [("hello",1),("world",2),("!!!",3)]
-- fromList [1,2,3]
--
--
-- -- setOf :: Hashable a => Getter s a -> s -> HashSet a -- setOf :: (Eq a, Hashable a) => Fold s a -> s -> HashSet a -- setOf :: Hashable a => Simple Iso s a -> s -> HashSet a -- setOf :: Hashable a => Simple Lens s a -> s -> HashSet a -- setOf :: (Eq a, Hashable a) => Simple Traversal s a -> s -> HashSet a --setOf :: Hashable a => Getting (HashSet a) s t a b -> s -> HashSet a -- | A Fold s a is a generalization of something -- Foldable. It allows you to extract multiple results from a -- container. A Foldable container can be characterized by the -- behavior of foldMap :: (Foldable t, Monoid m) => -- (a -> m) -> t a -> m. Since we want to be able to work -- with monomorphic containers, we could generalize this signature to -- forall m. Monoid m => (a -> m) -> s -> m, -- and then decorate it with Accessor to obtain -- --
-- type Fold s a = forall m. Monoid m => Getting m s s a a ---- -- Every Getter is a valid Fold that simply doesn't use the -- Monoid it is passed. -- -- In practice the type we use is slightly more complicated to allow for -- better error messages and for it to be transformed by certain -- Applicative transformers. -- -- Everything you can do with a Foldable container, you can with -- with a Fold and there are combinators that generalize the usual -- Foldable operations here. module Control.Lens.Fold -- | A Fold describes how to retrieve multiple values in a way that -- can be composed with other lens-like constructions. -- -- A Fold s a provides a structure with operations very -- similar to those of the Foldable typeclass, see -- foldMapOf and the other Fold combinators. -- -- By convention, if there exists a foo method that expects a -- Foldable (f a), then there should be a fooOf -- method that takes a Fold s a and a value of type -- s. -- -- A Getter is a legal Fold that just ignores the supplied -- Monoid -- -- Unlike a Traversal a Fold is read-only. Since a -- Fold cannot be used to write back there are no lens laws that -- apply. type Fold s a = forall f. (Gettable f, Applicative f) => (a -> f a) -> s -> f s -- | Perform a safe head of a Fold or Traversal or -- retrieve Just the result from a Getter or Lens. -- -- When using a Traversal as a partial Lens, or a -- Fold as a partial Getter this can be a convenient way to -- extract the optional value. -- --
-- (^?) ≡ flip headOf ---- --
-- (^?) :: s -> Getter s a -> Maybe a -- (^?) :: s -> Fold s a -> Maybe a -- (^?) :: s -> Simple Lens s a -> Maybe a -- (^?) :: s -> Simple Iso s a -> Maybe a -- (^?) :: s -> Simple Traversal s a -> Maybe a --(^?) :: s -> Getting (First a) s t a b -> Maybe a -- | A convenient infix (flipped) version of toListOf. -- --
-- >>> [[1,2],[3]]^..traverse.traverse -- [1,2,3] ---- --
-- >>> (1,2)^..both -- [1,2] ---- --
-- toList xs ≡ xs ^.. folded -- (^..) ≡ flip toListOf ---- --
-- (^..) :: s -> Getter s a -> [a] -- (^..) :: s -> Fold s a -> [a] -- (^..) :: s -> Simple Lens s a -> [a] -- (^..) :: s -> Simple Iso s a -> [a] -- (^..) :: s -> Simple Traversal s a -> [a] --(^..) :: s -> Getting [a] s t a b -> [a] -- | Obtain a Fold by lifting an operation that returns a foldable -- result. -- -- This can be useful to lift operations from Data.List and -- elsewhere into a Fold. folding :: (Foldable f, Applicative g, Gettable g) => (s -> f a) -> LensLike g s t a b -- | Obtain a Fold from any Foldable. folded :: Foldable f => Fold (f a) a -- | Build a fold that unfolds its values from a seed. -- --
-- unfoldr ≡ toListOf . unfolded --unfolded :: (b -> Maybe (a, b)) -> Fold b a -- | x ^. iterated f Return an infinite fold of repeated -- applications of f to x. -- --
-- toListOf (iterated f) a ≡ iterate f a --iterated :: (a -> a) -> Fold a a -- | Obtain a Fold that can be composed with to filter another -- Lens, Iso, Getter, Fold (or -- Traversal) -- -- Note: This is not a legal Traversal, unless you are very -- careful not to invalidate the predicate on the target. -- -- As a counter example, consider that given evens = filtered -- even the second Traversal law is violated: -- --
-- over evens succ . over evens succ /= over evens (succ . succ) ---- -- So, in order for this to qualify as a legal Traversal you can -- only use it for actions that preserve the result of the predicate! -- --
-- filtered :: (a -> Bool) -> Fold a a --filtered :: Applicative f => (a -> Bool) -> SimpleLensLike f a a -- | This allows you to traverse the elements of a Traversal or -- Fold in the opposite order. This will demote an -- IndexedTraversal or IndexedFold to a regular -- Traversal or Fold; to preserve the indices, use -- ibackwards instead. -- -- Note: backwards should have no impact on a Getter, -- Setter, Lens or Iso. -- -- To change the direction of an Iso, use from. backwards :: LensLike (Backwards f) s t a b -> LensLike f s t a b -- | Fold by repeating the input forever. -- --
-- repeat ≡ toListOf repeated --repeated :: Fold a a -- | A fold that replicates its input n times. -- --
-- replicate n ≡ toListOf (replicated n) --replicated :: Int -> Fold a a -- | Transform a fold into a fold that loops over its elements over and -- over. -- --
-- >>> take 6 $ toListOf (cycled traverse) [1,2,3] -- [1,2,3,1,2,3] --cycled :: (Applicative f, Gettable f) => LensLike f s t a b -> LensLike f s t a b -- | Obtain a Fold by taking elements from another Fold, -- Lens, Iso, Getter or Traversal while a -- predicate holds. -- --
-- takeWhile p ≡ toListOf (takingWhile p folded) ---- --
-- >>> toListOf (takingWhile (<=3) folded) [1..] -- [1,2,3] --takingWhile :: (Gettable f, Applicative f) => (a -> Bool) -> Getting (Endo (f s)) s s a a -> LensLike f s s a a -- | Obtain a Fold by dropping elements from another Fold, -- Lens, Iso, Getter or Traversal while a -- predicate holds. -- --
-- dropWhile p ≡ toListOf (droppingWhile p folded) ---- --
-- >>> toListOf (droppingWhile (<=3) folded) [1..6] -- [4,5,6] ---- --
-- >>> toListOf (droppingWhile (<=3) folded) [1,6,1] -- [6,1] --droppingWhile :: (Gettable f, Applicative f) => (a -> Bool) -> Getting (Endo (f s, f s)) s s a a -> LensLike f s s a a -- |
-- foldMap = foldMapOf folded ---- --
-- foldMapOf ≡ views ---- --
-- foldMapOf :: Getter s a -> (a -> r) -> s -> r -- foldMapOf :: Monoid r => Fold s a -> (a -> r) -> s -> r -- foldMapOf :: Simple Lens s a -> (a -> r) -> s -> r -- foldMapOf :: Simple Iso s a -> (a -> r) -> s -> r -- foldMapOf :: Monoid r => Simple Traversal s a -> (a -> r) -> s -> r --foldMapOf :: Getting r s t a b -> (a -> r) -> s -> r -- |
-- fold = foldOf folded ---- --
-- foldOf ≡ view ---- --
-- foldOf :: Getter s m -> s -> m -- foldOf :: Monoid m => Fold s m -> s -> m -- foldOf :: Simple Lens s m -> s -> m -- foldOf :: Simple Iso s m -> s -> m -- foldOf :: Monoid m => Simple Traversal s m -> s -> m --foldOf :: Getting a s t a b -> s -> a -- | Right-associative fold of parts of a structure that are viewed through -- a Lens, Getter, Fold or Traversal. -- --
-- foldr ≡ foldrOf folded ---- --
-- foldrOf :: Getter s a -> (a -> r -> r) -> r -> s -> r -- foldrOf :: Fold s a -> (a -> r -> r) -> r -> s -> r -- foldrOf :: Simple Lens s a -> (a -> r -> r) -> r -> s -> r -- foldrOf :: Simple Iso s a -> (a -> r -> r) -> r -> s -> r -- foldrOf :: Simple Traversal s a -> (a -> r -> r) -> r -> s -> r --foldrOf :: Getting (Endo r) s t a b -> (a -> r -> r) -> r -> s -> r -- | Left-associative fold of the parts of a structure that are viewed -- through a Lens, Getter, Fold or Traversal. -- --
-- foldl ≡ foldlOf folded ---- --
-- foldlOf :: Getter s a -> (r -> a -> r) -> r -> s -> r -- foldlOf :: Fold s a -> (r -> a -> r) -> r -> s -> r -- foldlOf :: Simple Lens s a -> (r -> a -> r) -> r -> s -> r -- foldlOf :: Simple Iso s a -> (r -> a -> r) -> r -> s -> r -- foldlOf :: Simple Traversal s a -> (r -> a -> r) -> r -> s -> r --foldlOf :: Getting (Dual (Endo r)) s t a b -> (r -> a -> r) -> r -> s -> r -- | Extract a list of the targets of a Fold. See also (^..). -- --
-- toList ≡ toListOf folded -- (^..) ≡ flip toListOf --toListOf :: Getting [a] s t a b -> s -> [a] -- | Returns True if any target of a Fold satisfies a -- predicate. -- --
-- >>> anyOf both (=='x') ('x','y')
-- True
--
-- >>> import Data.Data.Lens
--
-- >>> anyOf biplate (== "world") (((),2::Int),"hello",("world",11))
-- True
--
--
-- -- any ≡ anyOf folded ---- --
-- anyOf :: Getter s a -> (a -> Bool) -> s -> Bool -- anyOf :: Fold s a -> (a -> Bool) -> s -> Bool -- anyOf :: Simple Lens s a -> (a -> Bool) -> s -> Bool -- anyOf :: Simple Iso s a -> (a -> Bool) -> s -> Bool -- anyOf :: Simple Traversal s a -> (a -> Bool) -> s -> Bool --anyOf :: Getting Any s t a b -> (a -> Bool) -> s -> Bool -- | Returns True if every target of a Fold satisfies a -- predicate. -- --
-- >>> allOf both (>=3) (4,5) -- True -- -- >>> allOf folded (>=2) [1..10] -- False ---- --
-- all ≡ allOf folded ---- --
-- allOf :: Getter s a -> (a -> Bool) -> s -> Bool -- allOf :: Fold s a -> (a -> Bool) -> s -> Bool -- allOf :: Simple Lens s a -> (a -> Bool) -> s -> Bool -- allOf :: Simple Iso s a -> (a -> Bool) -> s -> Bool -- allOf :: Simple Traversal s a -> (a -> Bool) -> s -> Bool --allOf :: Getting All s t a b -> (a -> Bool) -> s -> Bool -- | Returns True if every target of a Fold is True. -- --
-- >>> andOf both (True,False) -- False -- -- >>> andOf both (True,True) -- True ---- --
-- and ≡ andOf folded ---- --
-- andOf :: Getter s Bool -> s -> Bool -- andOf :: Fold s Bool -> s -> Bool -- andOf :: Simple Lens s Bool -> s -> Bool -- andOf :: Simple Iso s Bool -> s -> Bool -- andOf :: Simple Traversal s Bool -> s -> Bool --andOf :: Getting All s t Bool b -> s -> Bool -- | Returns True if any target of a Fold is True. -- --
-- >>> orOf both (True,False) -- True -- -- >>> orOf both (False,False) -- False ---- --
-- or ≡ orOf folded ---- --
-- orOf :: Getter s Bool -> s -> Bool -- orOf :: Fold s Bool -> s -> Bool -- orOf :: Simple Lens s Bool -> s -> Bool -- orOf :: Simple Iso s Bool -> s -> Bool -- orOf :: Simple Traversal s Bool -> s -> Bool --orOf :: Getting Any s t Bool b -> s -> Bool -- | Calculate the product of every number targeted by a Fold -- --
-- >>> productOf both (4,5) -- 20 -- -- >>> productOf folded [1,2,3,4,5] -- 120 ---- --
-- product ≡ productOf folded ---- --
-- productOf :: Getter s a -> s -> a -- productOf :: Num a => Fold s a -> s -> a -- productOf :: Simple Lens s a -> s -> a -- productOf :: Simple Iso s a -> s -> a -- productOf :: Num a => Simple Traversal s a -> s -> a --productOf :: Getting (Product a) s t a b -> s -> a -- | Calculate the sum of every number targeted by a Fold. -- --
-- >>> sumOf both (5,6) -- 11 -- -- >>> sumOf folded [1,2,3,4] -- 10 -- -- >>> sumOf (folded.both) [(1,2),(3,4)] -- 10 -- -- >>> import Data.Data.Lens -- -- >>> sumOf biplate [(1::Int,[]),(2,[(3::Int,4::Int)])] :: Int -- 10 ---- --
-- sum ≡ sumOf folded ---- --
-- sumOf _1 :: (a, b) -> a -- sumOf (folded . _1) :: (Foldable f, Num a) => f (a, b) -> a ---- --
-- sumOf :: Getter s a -> s -> a -- sumOf :: Num a => Fold s a -> s -> a -- sumOf :: Simple Lens s a -> s -> a -- sumOf :: Simple Iso s a -> s -> a -- sumOf :: Num a => Simple Traversal s a -> s -> a --sumOf :: Getting (Sum a) s t a b -> s -> a -- | Traverse over all of the targets of a Fold (or Getter), -- computing an Applicative (or Functor) -based answer, but -- unlike traverseOf do not construct a new structure. -- traverseOf_ generalizes traverse_ to work over any -- Fold. -- -- When passed a Getter, traverseOf_ can work over any -- Functor, but when passed a Fold, traverseOf_ -- requires an Applicative. -- --
-- >>> traverseOf_ both putStrLn ("hello","world")
-- hello
-- world
--
--
-- -- traverse_ ≡ traverseOf_ folded ---- --
-- traverseOf_ _2 :: Functor f => (c -> f r) -> (d, c) -> f () -- traverseOf_ traverseLeft :: Applicative f => (a -> f b) -> Either a c -> f () ---- -- The rather specific signature of traverseOf_ allows it to be -- used as if the signature was any of: -- --
-- traverseOf_ :: Functor f => Getter s a -> (a -> f r) -> s -> f () -- traverseOf_ :: Applicative f => Fold s a -> (a -> f r) -> s -> f () -- traverseOf_ :: Functor f => Simple Lens s a -> (a -> f r) -> s -> f () -- traverseOf_ :: Functor f => Simple Iso s a -> (a -> f r) -> s -> f () -- traverseOf_ :: Applicative f => Simple Traversal s a -> (a -> f r) -> s -> f () --traverseOf_ :: Functor f => Getting (Traversed f) s t a b -> (a -> f r) -> s -> f () -- | Traverse over all of the targets of a Fold (or Getter), -- computing an Applicative (or Functor) -based answer, but -- unlike forOf do not construct a new structure. forOf_ -- generalizes for_ to work over any Fold. -- -- When passed a Getter, forOf_ can work over any -- Functor, but when passed a Fold, forOf_ requires -- an Applicative. -- --
-- for_ ≡ forOf_ folded ---- -- The rather specific signature of forOf_ allows it to be used as -- if the signature was any of: -- --
-- forOf_ :: Functor f => Getter s a -> s -> (a -> f r) -> f () -- forOf_ :: Applicative f => Fold s a -> s -> (a -> f r) -> f () -- forOf_ :: Functor f => Simple Lens s a -> s -> (a -> f r) -> f () -- forOf_ :: Functor f => Simple Iso s a -> s -> (a -> f r) -> f () -- forOf_ :: Applicative f => Simple Traversal s a -> s -> (a -> f r) -> f () --forOf_ :: Functor f => Getting (Traversed f) s t a b -> s -> (a -> f r) -> f () -- | Evaluate each action in observed by a Fold on a structure from -- left to right, ignoring the results. -- --
-- sequenceA_ ≡ sequenceAOf_ folded ---- --
-- sequenceAOf_ :: Functor f => Getter s (f ()) -> s -> f () -- sequenceAOf_ :: Applicative f => Fold s (f ()) -> s -> f () -- sequenceAOf_ :: Functor f => Simple Lens s (f ()) -> s -> f () -- sequenceAOf_ :: Functor f => Simple Iso s (f ()) -> s -> f () -- sequenceAOf_ :: Applicative f => Simple Traversal s (f ()) -> s -> f () --sequenceAOf_ :: Functor f => Getting (Traversed f) s t (f ()) b -> s -> f () -- | Map each target of a Fold on a structure to a monadic action, -- evaluate these actions from left to right, and ignore the results. -- --
-- mapM_ ≡ mapMOf_ folded ---- --
-- mapMOf_ :: Monad m => Getter s a -> (a -> m r) -> s -> m () -- mapMOf_ :: Monad m => Fold s a -> (a -> m r) -> s -> m () -- mapMOf_ :: Monad m => Simple Lens s a -> (a -> m r) -> s -> m () -- mapMOf_ :: Monad m => Simple Iso s a -> (a -> m r) -> s -> m () -- mapMOf_ :: Monad m => Simple Traversal s a -> (a -> m r) -> s -> m () --mapMOf_ :: Monad m => Getting (Sequenced m) s t a b -> (a -> m r) -> s -> m () -- | forMOf_ is mapMOf_ with two of its arguments flipped. -- --
-- forM_ ≡ forMOf_ folded ---- --
-- forMOf_ :: Monad m => Getter s a -> s -> (a -> m r) -> m () -- forMOf_ :: Monad m => Fold s a -> s -> (a -> m r) -> m () -- forMOf_ :: Monad m => Simple Lens s a -> s -> (a -> m r) -> m () -- forMOf_ :: Monad m => Simple Iso s a -> s -> (a -> m r) -> m () -- forMOf_ :: Monad m => Simple Traversal s a -> s -> (a -> m r) -> m () --forMOf_ :: Monad m => Getting (Sequenced m) s t a b -> s -> (a -> m r) -> m () -- | Evaluate each monadic action referenced by a Fold on the -- structure from left to right, and ignore the results. -- --
-- sequence_ ≡ sequenceOf_ folded ---- --
-- sequenceOf_ :: Monad m => Getter s (m a) -> s -> m () -- sequenceOf_ :: Monad m => Fold s (m a) -> s -> m () -- sequenceOf_ :: Monad m => Simple Lens s (m a) -> s -> m () -- sequenceOf_ :: Monad m => Simple Iso s (m a) -> s -> m () -- sequenceOf_ :: Monad m => Simple Traversal s (m a) -> s -> m () --sequenceOf_ :: Monad m => Getting (Sequenced m) s t (m a) b -> s -> m () -- | The sum of a collection of actions, generalizing concatOf. -- --
-- asum ≡ asumOf folded ---- --
-- asumOf :: Alternative f => Getter s a -> s -> f a -- asumOf :: Alternative f => Fold s a -> s -> f a -- asumOf :: Alternative f => Simple Lens s a -> s -> f a -- asumOf :: Alternative f => Simple Iso s a -> s -> f a -- asumOf :: Alternative f => Simple Traversal s a -> s -> f a --asumOf :: Alternative f => Getting (Endo (f a)) s t (f a) b -> s -> f a -- | The sum of a collection of actions, generalizing concatOf. -- --
-- msum ≡ msumOf folded ---- --
-- msumOf :: MonadPlus m => Getter s a -> s -> m a -- msumOf :: MonadPlus m => Fold s a -> s -> m a -- msumOf :: MonadPlus m => Simple Lens s a -> s -> m a -- msumOf :: MonadPlus m => Simple Iso s a -> s -> m a -- msumOf :: MonadPlus m => Simple Traversal s a -> s -> m a --msumOf :: MonadPlus m => Getting (Endo (m a)) s t (m a) b -> s -> m a -- | Map a function over all the targets of a Fold of a container -- and concatenate the resulting lists. -- --
-- concatMap ≡ concatMapOf folded ---- --
-- concatMapOf :: Getter s a -> (a -> [r]) -> s -> [r] -- concatMapOf :: Fold s a -> (a -> [r]) -> s -> [r] -- concatMapOf :: Simple Lens s a -> (a -> [r]) -> s -> [r] -- concatMapOf :: Simple Iso s a -> (a -> [r]) -> s -> [r] -- concatMapOf :: Simple Traversal s a -> (a -> [r]) -> s -> [r] --concatMapOf :: Getting [r] s t a b -> (a -> [r]) -> s -> [r] -- | Concatenate all of the lists targeted by a Fold into a longer -- list. -- --
-- >>> concatOf both ("pan","ama")
-- "panama"
--
--
-- -- concat ≡ concatOf folded -- concatOf ≡ view ---- --
-- concatOf :: Getter s [r] -> s -> [r] -- concatOf :: Fold s [r] -> s -> [r] -- concatOf :: Simple Iso s [r] -> s -> [r] -- concatOf :: Simple Lens s [r] -> s -> [r] -- concatOf :: Simple Traversal s [r] -> s -> [r] --concatOf :: Getting [r] s t [r] b -> s -> [r] -- | Does the element occur anywhere within a given Fold of the -- structure? -- --
-- >>> elemOf both "hello" ("hello","world")
-- True
--
--
-- -- elem ≡ elemOf folded ---- --
-- elemOf :: Eq a => Getter s a -> a -> s -> Bool -- elemOf :: Eq a => Fold s a -> a -> s -> Bool -- elemOf :: Eq a => Simple Lens s a -> a -> s -> Bool -- elemOf :: Eq a => Simple Iso s a -> a -> s -> Bool -- elemOf :: Eq a => Simple Traversal s a -> a -> s -> Bool --elemOf :: Eq a => Getting Any s t a b -> a -> s -> Bool -- | Does the element not occur anywhere within a given Fold of the -- structure? -- --
-- notElem ≡ notElemOf folded ---- --
-- notElemOf :: Eq a => Getter s a -> a -> s -> Bool -- notElemOf :: Eq a => Fold s a -> a -> s -> Bool -- notElemOf :: Eq a => Simple Iso s a -> a -> s -> Bool -- notElemOf :: Eq a => Simple Lens s a -> a -> s -> Bool -- notElemOf :: Eq a => Simple Traversal s a -> a -> s -> Bool --notElemOf :: Eq a => Getting All s t a b -> a -> s -> Bool -- | Note: this can be rather inefficient for large containers. -- --
-- length ≡ lengthOf folded ---- --
-- >>> lengthOf _1 ("hello",())
-- 1
--
--
-- -- lengthOf (folded . folded) :: Foldable f => f (g a) -> Int ---- --
-- lengthOf :: Getter s a -> s -> Int -- lengthOf :: Fold s a -> s -> Int -- lengthOf :: Simple Lens s a -> s -> Int -- lengthOf :: Simple Iso s a -> s -> Int -- lengthOf :: Simple Traversal s a -> s -> Int --lengthOf :: Getting (Sum Int) s t a b -> s -> Int -- | Returns True if this Fold or Traversal has no -- targets in the given container. -- -- Note: nullOf on a valid Iso, Lens or -- Getter should always return False -- --
-- null ≡ nullOf folded ---- -- This may be rather inefficient compared to the null check of -- many containers. -- --
-- >>> nullOf _1 (1,2) -- False ---- --
-- nullOf (folded . _1 . folded) :: Foldable f => f (g a, b) -> Bool ---- --
-- nullOf :: Getter s a -> s -> Bool -- nullOf :: Fold s a -> s -> Bool -- nullOf :: Simple Iso s a -> s -> Bool -- nullOf :: Simple Lens s a -> s -> Bool -- nullOf :: Simple Traversal s a -> s -> Bool --nullOf :: Getting All s t a b -> s -> Bool -- | Perform a safe head of a Fold or Traversal or -- retrieve Just the result from a Getter or Lens. -- See also (^?). -- --
-- listToMaybe . toList ≡ headOf folded ---- --
-- headOf :: Getter s a -> s -> Maybe a -- headOf :: Fold s a -> s -> Maybe a -- headOf :: Simple Lens s a -> s -> Maybe a -- headOf :: Simple Iso s a -> s -> Maybe a -- headOf :: Simple Traversal s a -> s -> Maybe a --headOf :: Getting (First a) s t a b -> s -> Maybe a -- | Perform a safe last of a Fold or Traversal or -- retrieve Just the result from a Getter or Lens. -- --
-- lastOf :: Getter s a -> s -> Maybe a -- lastOf :: Fold s a -> s -> Maybe a -- lastOf :: Simple Lens s a -> s -> Maybe a -- lastOf :: Simple Iso s a -> s -> Maybe a -- lastOf :: Simple Traversal s a -> s -> Maybe a --lastOf :: Getting (Last a) s t a b -> s -> Maybe a -- | Obtain the maximum element (if any) targeted by a Fold or -- Traversal -- -- Note: maximumOf on a valid Iso, Lens or Getter -- will always return Just a value. -- --
-- maximum ≡ fromMaybe (error empty) . maximumOf folded ---- --
-- maximumOf :: Getter s a -> s -> Maybe a -- maximumOf :: Ord a => Fold s a -> s -> Maybe a -- maximumOf :: Simple Iso s a -> s -> Maybe a -- maximumOf :: Simple Lens s a -> s -> Maybe a -- maximumOf :: Ord a => Simple Traversal s a -> s -> Maybe a --maximumOf :: Getting (Max a) s t a b -> s -> Maybe a -- | Obtain the minimum element (if any) targeted by a Fold or -- Traversal -- -- Note: minimumOf on a valid Iso, Lens or Getter -- will always return Just a value. -- --
-- minimum ≡ fromMaybe (error empty) . minimumOf folded ---- --
-- minimumOf :: Getter s a -> s -> Maybe a -- minimumOf :: Ord a => Fold s a -> s -> Maybe a -- minimumOf :: Simple Iso s a -> s -> Maybe a -- minimumOf :: Simple Lens s a -> s -> Maybe a -- minimumOf :: Ord a => Simple Traversal s a -> s -> Maybe a --minimumOf :: Getting (Min a) s t a b -> s -> Maybe a -- | Obtain the maximum element (if any) targeted by a Fold, -- Traversal, Lens, Iso, or Getter according -- to a user supplied ordering. -- --
-- maximumBy cmp ≡ fromMaybe (error empty) . maximumByOf folded cmp ---- --
-- maximumByOf :: Getter s a -> (a -> a -> Ordering) -> s -> Maybe a -- maximumByOf :: Fold s a -> (a -> a -> Ordering) -> s -> Maybe a -- maximumByOf :: Simple Iso s a -> (a -> a -> Ordering) -> s -> Maybe a -- maximumByOf :: Simple Lens s a -> (a -> a -> Ordering) -> s -> Maybe a -- maximumByOf :: Simple Traversal s a -> (a -> a -> Ordering) -> s -> Maybe a --maximumByOf :: Getting (Endo (Maybe a)) s t a b -> (a -> a -> Ordering) -> s -> Maybe a -- | Obtain the minimum element (if any) targeted by a Fold, -- Traversal, Lens, Iso or Getter according -- to a user supplied ordering. -- --
-- minimumBy cmp ≡ fromMaybe (error empty) . minimumByOf folded cmp ---- --
-- minimumByOf :: Getter s a -> (a -> a -> Ordering) -> s -> Maybe a -- minimumByOf :: Fold s a -> (a -> a -> Ordering) -> s -> Maybe a -- minimumByOf :: Simple Iso s a -> (a -> a -> Ordering) -> s -> Maybe a -- minimumByOf :: Simple Lens s a -> (a -> a -> Ordering) -> s -> Maybe a -- minimumByOf :: Simple Traversal s a -> (a -> a -> Ordering) -> s -> Maybe a --minimumByOf :: Getting (Endo (Maybe a)) s t a b -> (a -> a -> Ordering) -> s -> Maybe a -- | The findOf function takes a Lens (or Getter, -- Iso, Fold, or Traversal), a predicate and a -- structure and returns the leftmost element of the structure matching -- the predicate, or Nothing if there is no such element. -- --
-- findOf :: Getter s a -> (a -> Bool) -> s -> Maybe a -- findOf :: Fold s a -> (a -> Bool) -> s -> Maybe a -- findOf :: Simple Iso s a -> (a -> Bool) -> s -> Maybe a -- findOf :: Simple Lens s a -> (a -> Bool) -> s -> Maybe a -- findOf :: Simple Traversal s a -> (a -> Bool) -> s -> Maybe a --findOf :: Getting (First a) s t a b -> (a -> Bool) -> s -> Maybe a -- | Strictly fold right over the elements of a structure. -- --
-- foldr' ≡ foldrOf' folded ---- --
-- foldrOf' :: Getter s a -> (a -> r -> r) -> r -> s -> r -- foldrOf' :: Fold s a -> (a -> r -> r) -> r -> s -> r -- foldrOf' :: Simple Iso s a -> (a -> r -> r) -> r -> s -> r -- foldrOf' :: Simple Lens s a -> (a -> r -> r) -> r -> s -> r -- foldrOf' :: Simple Traversal s a -> (a -> r -> r) -> r -> s -> r --foldrOf' :: Getting (Dual (Endo (r -> r))) s t a b -> (a -> r -> r) -> r -> s -> r -- | Fold over the elements of a structure, associating to the left, but -- strictly. -- --
-- foldl' ≡ foldlOf' folded ---- --
-- foldlOf' :: Getter s a -> (r -> a -> r) -> r -> s -> r -- foldlOf' :: Fold s a -> (r -> a -> r) -> r -> s -> r -- foldlOf' :: Simple Iso s a -> (r -> a -> r) -> r -> s -> r -- foldlOf' :: Simple Lens s a -> (r -> a -> r) -> r -> s -> r -- foldlOf' :: Simple Traversal s a -> (r -> a -> r) -> r -> s -> r --foldlOf' :: Getting (Endo (r -> r)) s t a b -> (r -> a -> r) -> r -> s -> r -- | A variant of foldrOf that has no base case and thus may only be -- applied to lenses and structures such that the lens views at least one -- element of the structure. -- --
-- foldr1Of l f ≡ foldr1 f . toListOf l -- foldr1 ≡ foldr1Of folded ---- --
-- foldr1Of :: Getter s a -> (a -> a -> a) -> s -> a -- foldr1Of :: Fold s a -> (a -> a -> a) -> s -> a -- foldr1Of :: Simple Iso s a -> (a -> a -> a) -> s -> a -- foldr1Of :: Simple Lens s a -> (a -> a -> a) -> s -> a -- foldr1Of :: Simple Traversal s a -> (a -> a -> a) -> s -> a --foldr1Of :: Getting (Endo (Maybe a)) s t a b -> (a -> a -> a) -> s -> a -- | A variant of foldlOf that has no base case and thus may only be -- applied to lenses and strutures such that the lens views at least one -- element of the structure. -- --
-- foldl1Of l f ≡ foldl1Of l f . toList -- foldl1 ≡ foldl1Of folded ---- --
-- foldl1Of :: Getter s a -> (a -> a -> a) -> s -> a -- foldl1Of :: Fold s a -> (a -> a -> a) -> s -> a -- foldl1Of :: Simple Iso s a -> (a -> a -> a) -> s -> a -- foldl1Of :: Simple Lens s a -> (a -> a -> a) -> s -> a -- foldl1Of :: Simple Traversal s a -> (a -> a -> a) -> s -> a --foldl1Of :: Getting (Dual (Endo (Maybe a))) s t a b -> (a -> a -> a) -> s -> a -- | Monadic fold over the elements of a structure, associating to the -- right, i.e. from right to left. -- --
-- foldrM ≡ foldrMOf folded ---- --
-- foldrMOf :: Monad m => Getter s a -> (a -> r -> m r) -> r -> s -> m r -- foldrMOf :: Monad m => Fold s a -> (a -> r -> m r) -> r -> s -> m r -- foldrMOf :: Monad m => Simple Iso s a -> (a -> r -> m r) -> r -> s -> m r -- foldrMOf :: Monad m => Simple Lens s a -> (a -> r -> m r) -> r -> s -> m r -- foldrMOf :: Monad m => Simple Traversal s a -> (a -> r -> m r) -> r -> s -> m r --foldrMOf :: Monad m => Getting (Dual (Endo (r -> m r))) s t a b -> (a -> r -> m r) -> r -> s -> m r -- | Monadic fold over the elements of a structure, associating to the -- left, i.e. from left to right. -- --
-- foldlM ≡ foldlMOf folded ---- --
-- foldlMOf :: Monad m => Getter s a -> (r -> a -> m r) -> r -> s -> m r -- foldlMOf :: Monad m => Fold s a -> (r -> a -> m r) -> r -> s -> m r -- foldlMOf :: Monad m => Simple Iso s a -> (r -> a -> m r) -> r -> s -> m r -- foldlMOf :: Monad m => Simple Lens s a -> (r -> a -> m r) -> r -> s -> m r -- foldlMOf :: Monad m => Simple Traversal s a -> (r -> a -> m r) -> r -> s -> m r --foldlMOf :: Monad m => Getting (Endo (r -> m r)) s t a b -> (r -> a -> m r) -> r -> s -> m r -- | Useful for storing folds in containers. newtype ReifiedFold s a ReifyFold :: Fold s a -> ReifiedFold s a reflectFold :: ReifiedFold s a -> Fold s a -- | A Traversal s t a b is a generalization of -- traverse from Traversable. It allows you to traverse -- over a structure and change out its contents with monadic or -- applicative side-effects. Starting from -- -- traverse :: (Traversable t, Applicative f) -- => (a -> f b) -> t a -> f (t b), -- -- we monomorphize the contents and result to obtain -- --
-- type Traversal s t a b = forall f. Applicative f => (a -> f b) -> s -> f t ---- -- While a Traversal isn't quite a Fold, it _can_ be used -- for Getting like a Fold, because given a -- Monoid m, we have an Applicative for -- (Const m). Everything you know how to do with a -- Traversable container, you can with with a Traversal, -- and here we provide combinators that generalize the usual -- Traversable operations. module Control.Lens.Traversal -- | A Traversal can be used directly as a Setter or a -- Fold (but not as a Lens) and provides the ability to -- both read and update multiple fields, subject to some relatively weak -- Traversal laws. -- -- These have also been known as multilenses, but they have the signature -- and spirit of -- --
-- traverse :: Traversable f => Traversal (f a) (f b) a b ---- -- and the more evocative name suggests their application. -- -- Most of the time the Traversal you will want to use is just -- traverse, but you can also pass any Lens or Iso -- as a Traversal, and composition of a Traversal (or -- Lens or Iso) with a Traversal (or Lens or -- Iso) using (.) forms a valid Traversal. -- -- The laws for a Traversal t follow from the laws for -- Traversable as stated in "The Essence of the Iterator Pattern". -- --
-- t pure ≡ pure -- fmap (t f) . t g ≡ getCompose . t (Compose . fmap f . g) ---- -- One consequence of this requirement is that a Traversal needs -- to leave the same number of elements as a candidate for subsequent -- Traversal that it started with. Another testament to the -- strength of these laws is that the caveat expressed in section 5.5 of -- the "Essence of the Iterator Pattern" about exotic Traversable -- instances that traverse the same entry multiple times was -- actually already ruled out by the second law in that same paper! type Traversal s t a b = forall f. Applicative f => (a -> f b) -> s -> f t -- | Map each element of a structure targeted by a Lens or Traversal, -- evaluate these actions from left to right, and collect the results. -- -- This function is only provided for consistency, id is strictly -- more general. -- --
-- traverseOf ≡ id ---- -- This yields the obvious law: -- --
-- traverse ≡ traverseOf traverse ---- --
-- traverseOf :: Iso s t a b -> (a -> f b) -> s -> f t -- traverseOf :: Lens s t a b -> (a -> f b) -> s -> f t -- traverseOf :: Traversal s t a b -> (a -> f b) -> s -> f t --traverseOf :: LensLike f s t a b -> (a -> f b) -> s -> f t -- | A version of traverseOf with the arguments flipped, such that: -- --
-- forOf l ≡ flip (traverseOf l) ---- --
-- for ≡ forOf traverse ---- -- This function is only provided for consistency, flip is -- strictly more general. -- --
-- forOf ≡ flip ---- --
-- forOf :: Iso s t a b -> s -> (a -> f b) -> f t -- forOf :: Lens s t a b -> s -> (a -> f b) -> f t -- forOf :: Traversal s t a b -> s -> (a -> f b) -> f t --forOf :: LensLike f s t a b -> s -> (a -> f b) -> f t -- | Evaluate each action in the structure from left to right, and collect -- the results. -- --
-- sequenceA ≡ sequenceAOf traverse ≡ traverse id -- sequenceAOf l ≡ traverseOf l id ≡ l id ---- --
-- sequenceAOf :: Iso s t (f b) b -> s -> f t -- sequenceAOf :: Lens s t (f b) b -> s -> f t -- sequenceAOf :: Applicative f => Traversal s t (f b) b -> s -> f t --sequenceAOf :: LensLike f s t (f b) b -> s -> f t -- | Map each element of a structure targeted by a lens to a monadic -- action, evaluate these actions from left to right, and collect the -- results. -- --
-- mapM ≡ mapMOf traverse ---- --
-- mapMOf :: Iso s t a b -> (a -> m b) -> s -> m t -- mapMOf :: Lens s t a b -> (a -> m b) -> s -> m t -- mapMOf :: Monad m => Traversal s t a b -> (a -> m b) -> s -> m t --mapMOf :: LensLike (WrappedMonad m) s t a b -> (a -> m b) -> s -> m t -- | forMOf is a flipped version of mapMOf, consistent with -- the definition of forM. forM ≡ forMOf -- traverse forMOf l ≡ flip (mapMOf l) -- --
-- forMOf :: Iso s t a b -> s -> (a -> m b) -> m t -- forMOf :: Lens s t a b -> s -> (a -> m b) -> m t -- forMOf :: Monad m => Traversal s t a b -> s -> (a -> m b) -> m t --forMOf :: LensLike (WrappedMonad m) s t a b -> s -> (a -> m b) -> m t -- | Sequence the (monadic) effects targeted by a lens in a container from -- left to right. -- --
-- sequence ≡ sequenceOf traverse -- sequenceOf l ≡ mapMOf l id -- sequenceOf l ≡ unwrapMonad . l WrapMonad ---- --
-- sequenceOf :: Iso s t (m b) b -> s -> m t -- sequenceOf :: Lens s t (m b) b -> s -> m t -- sequenceOf :: Monad m => Traversal s t (m b) b -> s -> m t --sequenceOf :: LensLike (WrappedMonad m) s t (m b) b -> s -> m t -- | This generalizes transpose to an arbitrary Traversal. -- -- Note: transpose handles ragged inputs more intelligently, but -- for non-ragged inputs: -- --
-- transpose ≡ transposeOf traverse ---- --
-- >>> transposeOf traverse [[1,2,3],[4,5,6]] -- [[1,4],[2,5],[3,6]] ---- -- Since every Lens is a Traversal, we can use this as a -- form of monadic strength as well: -- --
-- transposeOf _2 :: (b, [a]) -> [(b, a)] --transposeOf :: LensLike ZipList s t [a] a -> s -> [t] -- | This generalizes mapAccumL to an arbitrary Traversal. -- --
-- mapAccumL ≡ mapAccumLOf traverse ---- -- mapAccumLOf accumulates state from left to right. -- --
-- mapAccumLOf :: Iso s t a b -> (acc -> a -> (acc, b)) -> acc -> s -> (acc, t) -- mapAccumLOf :: Lens s t a b -> (acc -> a -> (acc, b)) -> acc -> s -> (acc, t) -- mapAccumLOf :: Traversal s t a b -> (acc -> a -> (acc, b)) -> acc -> s -> (acc, t) --mapAccumLOf :: LensLike (Backwards (State acc)) s t a b -> (acc -> a -> (acc, b)) -> acc -> s -> (acc, t) -- | This generalizes mapAccumR to an arbitrary Traversal. -- --
-- mapAccumR ≡ mapAccumROf traverse ---- -- mapAccumROf accumulates state from right to left. -- --
-- mapAccumROf :: Iso s t a b -> (acc -> a -> (acc, b)) -> acc -> s -> (acc, t) -- mapAccumROf :: Lens s t a b -> (acc -> a -> (acc, b)) -> acc -> s -> (acc, t) -- mapAccumROf :: Traversal s t a b -> (acc -> a -> (acc, b)) -> acc -> s -> (acc, t) --mapAccumROf :: LensLike (State acc) s t a b -> (acc -> a -> (acc, b)) -> acc -> s -> (acc, t) -- | This permits the use of scanr1 over an arbitrary -- Traversal or Lens. -- --
-- scanr1 ≡ scanr1Of traverse ---- --
-- scanr1Of :: Iso s t a a -> (a -> a -> a) -> s -> t -- scanr1Of :: Lens s t a a -> (a -> a -> a) -> s -> t -- scanr1Of :: Traversal s t a a -> (a -> a -> a) -> s -> t --scanr1Of :: LensLike (State (Maybe a)) s t a a -> (a -> a -> a) -> s -> t -- | This permits the use of scanl1 over an arbitrary -- Traversal or Lens. -- --
-- scanl1 ≡ scanl1Of traverse ---- --
-- scanr1Of :: Iso s t a a -> (a -> a -> a) -> s -> t -- scanr1Of :: Lens s t a a -> (a -> a -> a) -> s -> t -- scanr1Of :: Traversal s t a a -> (a -> a -> a) -> s -> t --scanl1Of :: LensLike (Backwards (State (Maybe a))) s t a a -> (a -> a -> a) -> s -> t -- | Functors representing data structures that can be traversed from left -- to right. -- -- Minimal complete definition: traverse or sequenceA. -- -- Instances are similar to Functor, e.g. given a data type -- --
-- data Tree a = Empty | Leaf a | Node (Tree a) a (Tree a) ---- -- a suitable instance would be -- --
-- instance Traversable Tree where -- traverse f Empty = pure Empty -- traverse f (Leaf x) = Leaf <$> f x -- traverse f (Node l k r) = Node <$> traverse f l <*> f k <*> traverse f r ---- -- This is suitable even for abstract types, as the laws for -- <*> imply a form of associativity. -- -- The superclass instances should satisfy the following: -- --
-- ignored :: Applicative f => (a -> f b) -> s -> f s ---- --
-- ignored ≡ const pure --ignored :: Traversal s s a b -- | A traversal for tweaking the left-hand value of an Either: -- --
-- >>> over traverseLeft (+1) (Left 2) -- Left 3 -- -- >>> over traverseLeft (+1) (Right 2) -- Right 2 -- -- >>> Right 42 ^.traverseLeft :: String -- "" -- -- >>> Left "hello" ^.traverseLeft -- "hello" ---- --
-- traverseLeft :: Applicative f => (a -> f b) -> Either a c -> f (Either b c) --traverseLeft :: Traversal (Either a c) (Either b c) a b -- | traverse the right-hand value of an Either: -- --
-- traverseRight ≡ traverse ---- -- Unfortunately the instance for Traversable (Either -- c) is still missing from base, so this can't just be -- traverse -- --
-- >>> over traverseRight (+1) (Left 2) -- Left 2 -- -- >>> over traverseRight (+1) (Right 2) -- Right 3 -- -- >>> Right "hello" ^.traverseRight -- "hello" -- -- >>> Left "hello" ^.traverseRight :: [Double] -- [] ---- --
-- traverseRight :: Applicative f => (a -> f b) -> Either c a -> f (Either c a) --traverseRight :: Traversal (Either c a) (Either c b) a b -- | Traverse both parts of a tuple with matching types. -- --
-- >>> both *~ 10 $ (1,2)
-- (10,20)
--
-- >>> over both length ("hello","world")
-- (5,5)
--
-- >>> ("hello","world")^.both
-- "helloworld"
--
both :: Traversal (a, a) (b, b) a b
-- | Visit the first n targets of a Traversal, Fold,
-- Getter or Lens.
taking :: Applicative f => Int -> SimpleLensLike (Indexing f) s a -> SimpleLensLike f s a
-- | Visit all but the first n targets of a Traversal,
-- Fold, Getter or Lens.
dropping :: Applicative f => Int -> SimpleLensLike (Indexing f) s a -> SimpleLensLike f s a
-- | A Traversal is completely characterized by its behavior on a
-- Bazaar.
--
-- Cloning a Traversal is one way to make sure you aren't given
-- something weaker, such as a Fold and can be used as a way to
-- pass around traversals that have to be monomorphic in f.
--
-- Note: This only accepts a proper Traversal (or Lens). To
-- clone a Lens as such, use cloneLens
--
-- Note: It is usually better to ReifyTraversal and use
-- reflectTraversal than to cloneTraversal. The former can
-- execute at full speed, while the latter needs to round trip through
-- the Bazaar.
--
--
-- >>> let foo l a = (view (cloneTraversal l) a, set (cloneTraversal l) 10 a)
--
-- >>> foo both ("hello","world")
-- ("helloworld",(10,10))
--
--
-- -- cloneTraversal :: LensLike (Bazaar a b) s t a b -> Traversal s t a b --cloneTraversal :: Applicative f => ((a -> Bazaar a b b) -> s -> Bazaar a b t) -> (a -> f b) -> s -> f t -- | A form of Traversal that can be stored monomorphically in a -- container. data ReifiedTraversal s t a b ReifyTraversal :: Traversal s t a b -> ReifiedTraversal s t a b reflectTraversal :: ReifiedTraversal s t a b -> Traversal s t a b -- |
-- type SimpleTraversal = Simple Traversal --type SimpleTraversal s a = Traversal s s a a -- |
-- type SimpleReifiedTraversal = Simple ReifiedTraversal --type SimpleReifiedTraversal s a = ReifiedTraversal s s a a -- | Lenses and Traversals for working with Template Haskell module Language.Haskell.TH.Lens -- | Has a Name class HasName t name :: HasName t => Simple Lens t Name -- | Provides for the extraction of free type variables, and alpha -- renaming. class HasTypeVars t typeVarsEx :: HasTypeVars t => Set Name -> Simple Traversal t Name -- | Provides substitution for types class SubstType t substType :: SubstType t => Map Name Type -> t -> t -- | Traverse free type variables typeVars :: HasTypeVars t => Simple Traversal t Name -- | Substitute using a map of names in for free type variables substTypeVars :: HasTypeVars t => Map Name Name -> t -> t -- | Provides a Traversal of the types of each field of a -- constructor. conFields :: Simple Traversal Con StrictType -- | Traversal of the types of the named fields of a -- constructor. conNamedFields :: Simple Traversal Con VarStrictType instance SubstType Pred instance SubstType t => SubstType [t] instance SubstType Type instance HasTypeVars t => HasTypeVars [t] instance HasTypeVars Pred instance HasTypeVars Type instance HasTypeVars Name instance HasTypeVars TyVarBndr instance HasName Con instance HasName Name instance HasName TyVarBndr -- | The name "plate" stems originally from "boilerplate", which was the -- term used by the "Scrap Your Boilerplate" papers, and later inherited -- by Neil Mitchell's "Uniplate". -- -- http://community.haskell.org/~ndm/uniplate/ -- -- The combinators in here are designed to be compatible with and subsume -- the uniplate API with the notion of a Traversal -- replacing a uniplate or biplate. -- -- By implementing these combinators in terms of plate instead of -- uniplate additional type safety is gained, as the user is no -- longer responsible for maintaining invariants such as the number of -- children he received. -- -- Note: The Biplate is deliberately excluded from the -- API here, with the intention that you replace them with either -- explicit traversals, or by using the On variants of the -- combinators below with biplate from Data.Data.Lens. As -- a design, it forced the user into too many situations where they had -- to choose between correctness and ease of use, and it was brittle in -- the face of competing imports. -- -- The sensible use of these combinators makes some simple assumptions. -- Notably, any of the On combinators are expecting a -- Traversal, Setter or Fold to play the role of the -- biplate combinator, and so when the types of the contents and -- the container match, they should be the id Traversal, -- Setter or Fold. -- -- It is often beneficial to use the combinators in this module with the -- combinators from Data.Data.Lens or GHC.Generics.Lens -- to make it easier to automatically derive definitions for -- plate, or to derive custom traversals. module Control.Lens.Plated -- | A Plated type is one where we know how to extract its immediate -- self-similar children. -- -- Example 1: -- --
-- import Control.Applicative -- import Control.Lens -- import Control.Plated -- import Data.Data -- import Data.Data.Lens (uniplate) ---- --
-- data Expr -- = Val Int -- | Neg Expr -- | Add Expr Expr -- deriving (Eq,Ord,Show,Read,Data,Typeable) ---- --
-- instance Plated Expr where -- plate f (Neg e) = Neg <$> f e -- plate f (Add a b) = Add <$> f a <*> f b -- plate _ a = pure a ---- -- or -- --
-- instance Plated Expr where -- plate = uniplate ---- -- Example 2: -- --
-- import Control.Applicative -- import Control.Lens -- import Control.Plated -- import Data.Data -- import Data.Data.Lens (uniplate) ---- --
-- data Tree a -- = Bin (Tree a) (Tree a) -- | Tip a -- deriving (Eq,Ord,Show,Read,Data,Typeable) ---- --
-- instance Plated (Tree a) where -- plate f (Bin l r) = Bin <$> f l <*> f r -- plate _ t = pure t ---- -- or -- --
-- instance Data a => Plated (Tree a) where -- plate = uniplate ---- -- Note the big distinction between these two implementations. -- -- The former will only treat children directly in this tree as -- descendents, the latter will treat trees contained in the values under -- the tips also as descendants! -- -- When in doubt, pick a Traversal and just use the various -- ...Of combinators rather than pollute Plated with -- orphan instances! -- -- If you want to find something unplated and non-recursive with -- biplate use the ...OnOf variant with ignored, -- though those usecases are much better served in most cases by using -- the existing lens combinators! e.g. -- -- toListOf biplate ≡ universeOnOf -- biplate ignored. -- -- This same ability to explicitly pass the Traversal in question -- is why there is no analogue to uniplate's Biplate. -- -- Moreover, since we can allow custom traversals, we implement -- reasonable defaults for polymorphic data types, that only traverse -- into themselves, and not their polymorphic arguments. class Plated a where plate = ignored plate :: Plated a => Simple Traversal a a -- | Extract the immediate descendants of a Plated container. -- --
-- children ≡ toListOf plate --children :: Plated a => a -> [a] -- | Provided for compatibility with uniplate. -- --
-- childrenOn ≡ toListOf ---- --
-- childrenOn :: Fold s a -> s -> [a] --childrenOn :: Getting [a] s t a b -> s -> [a] -- | Rewrite by applying a rule everywhere you can. Ensures that the rule -- cannot be applied anywhere in the result: -- --
-- propRewrite r x = all (isNothing . r) (universe (rewrite r x)) ---- -- Usually transform is more appropriate, but rewrite can -- give better compositionality. Given two single transformations -- f and g, you can construct a -> f a -- mplus g a which performs both rewrites until a fixed -- point. rewrite :: Plated a => (a -> Maybe a) -> a -> a -- | Rewrite by applying a rule everywhere you can. Ensures that the rule -- cannot be applied anywhere in the result: -- --
-- propRewriteOf l r x = all (isNothing . r) (universeOf l (rewriteOf l r x)) ---- -- Usually transformOf is more appropriate, but rewriteOf -- can give better compositionality. Given two single transformations -- f and g, you can construct a -> f a -- mplus g a which performs both rewrites until a fixed -- point. -- --
-- rewriteOf :: Simple Iso a a -> (a -> Maybe a) -> a -> a -- rewriteOf :: Simple Lens a a -> (a -> Maybe a) -> a -> a -- rewriteOf :: Simple Traversal a a -> (a -> Maybe a) -> a -> a -- rewriteOf :: Simple Setter a a -> (a -> Maybe a) -> a -> a --rewriteOf :: SimpleSetting a a -> (a -> Maybe a) -> a -> a -- | Rewrite recursively over part of a larger structure. -- --
-- rewriteOn :: Plated a => Simple Iso s a -> (a -> Maybe a) -> s -> s -- rewriteOn :: Plated a => Simple Lens s a -> (a -> Maybe a) -> s -> s -- rewriteOn :: Plated a => Simple Traversal s a -> (a -> Maybe a) -> s -> s -- rewriteOn :: Plated a => Simple Setting s a -> (a -> Maybe a) -> s -> s --rewriteOn :: Plated a => Setting s t a a -> (a -> Maybe a) -> s -> t -- | Rewrite recursively over part of a larger structure using a specified -- setter. -- --
-- rewriteOnOf :: Plated a => Simple Iso s a -> Simple Iso a a -> (a -> Maybe a) -> s -> s -- rewriteOnOf :: Plated a => Simple Lens s a -> Simple Lens a a -> (a -> Maybe a) -> s -> s -- rewriteOnOf :: Plated a => Simple Traversal s a -> Simple Traversal a a -> (a -> Maybe a) -> s -> s -- rewriteOnOf :: Plated a => Simple Setter s a -> Simple Setter a a -> (a -> Maybe a) -> s -> s --rewriteOnOf :: Setting s t a a -> SimpleSetting a a -> (a -> Maybe a) -> s -> t -- | Rewrite by applying a monadic rule everywhere you can. Ensures that -- the rule cannot be applied anywhere in the result. rewriteM :: (Monad m, Plated a) => (a -> m (Maybe a)) -> a -> m a -- | Rewrite by applying a monadic rule everywhere you recursing with a -- user-specified Traversal. Ensures that the rule cannot be -- applied anywhere in the result. rewriteMOf :: Monad m => SimpleLensLike (WrappedMonad m) a a -> (a -> m (Maybe a)) -> a -> m a -- | Rewrite by applying a monadic rule everywhere inside of a structure -- located by a user-specified Traversal. Ensures that the rule -- cannot be applied anywhere in the result. rewriteMOn :: (Monad m, Plated a) => LensLike (WrappedMonad m) s t a a -> (a -> m (Maybe a)) -> s -> m t -- | Rewrite by applying a monadic rule everywhere inside of a structure -- located by a user-specified Traversal, using a user-specified -- Traversal for recursion. Ensures that the rule cannot be -- applied anywhere in the result. rewriteMOnOf :: Monad m => LensLike (WrappedMonad m) s t a a -> SimpleLensLike (WrappedMonad m) a a -> (a -> m (Maybe a)) -> s -> m t -- | Retrieve all of the transitive descendants of a Plated -- container, including itself. universe :: Plated a => a -> [a] -- | Given a fold that knows how to locate immediate children, retrieve all -- of the transitive descendants of a node, including itself. -- --
-- universeOf :: Fold a a -> a -> [a] --universeOf :: Getting [a] a b a b -> a -> [a] -- | Given a Fold that knows how to find Plated parts of a -- container retrieve them and all of their descendants, recursively. universeOn :: Plated a => Getting [a] s t a a -> s -> [a] -- | Given a Fold that knows how to locate immediate children, -- retrieve all of the transitive descendants of a node, including itself -- that lie in a region indicated by another Fold. -- --
-- toListOf l ≡ universeOnOf l ignored --universeOnOf :: Getting [a] s t a b -> Getting [a] a b a b -> s -> [a] -- | Transform every element in the tree, in a bottom-up manner. -- -- For example, replacing negative literals with literals: -- --
-- negLits = transform $ x -> case x of -- Neg (Lit i) -> Lit (negate i) -- _ -> x --transform :: Plated a => (a -> a) -> a -> a -- | Transform every element by recursively applying a given Setter -- in a bottom-up manner. -- --
-- transformOf :: Simple Traversal a a -> (a -> a) -> a -> a -- transformOf :: Simple Setter a a -> (a -> a) -> a -> a --transformOf :: SimpleSetting a a -> (a -> a) -> a -> a -- | Transform every element in the tree in a bottom-up manner over a -- region indicated by a Setter. -- --
-- transformOn :: Plated a => Simple Traversal s a -> (a -> a) -> s -> s -- transformOn :: Plated a => Simple Setter s a -> (a -> a) -> s -> s --transformOn :: Plated a => Setting s t a a -> (a -> a) -> s -> t -- | Transform every element in a region indicated by a Setter by -- recursively applying another Setter in a bottom-up manner. -- --
-- transformOnOf :: Simple Setter s a -> Simple Traversal a a -> (a -> a) -> s -> s -- transformOnOf :: Simple Setter s a -> Simple Setter a a -> (a -> a) -> s -> s --transformOnOf :: Setting s t a a -> SimpleSetting a a -> (a -> a) -> s -> t -- | Transform every element in the tree, in a bottom-up manner, -- monadically. transformM :: (Monad m, Plated a) => (a -> m a) -> a -> m a -- | Transform every element in a tree using a user supplied -- Traversal in a bottom-up manner with a monadic effect. -- --
-- transformMOf :: Monad m => 'Simple Traversal a a -> (a -> m a) -> a -> m a --transformMOf :: Monad m => SimpleLensLike (WrappedMonad m) a a -> (a -> m a) -> a -> m a -- | Transform every element in the tree in a region indicated by a -- supplied Traversal, in a bottom-up manner, monadically. -- --
-- transformMOn :: (Monad m, Plated a) => Simple Traversal s a -> (a -> m a) -> s -> m s --transformMOn :: (Monad m, Plated a) => LensLike (WrappedMonad m) s t a a -> (a -> m a) -> s -> m t -- | Transform every element in a tree that lies in a region indicated by a -- supplied Traversal, walking with a user supplied -- Traversal in a bottom-up manner with a monadic effect. -- --
-- transformMOnOf :: Monad m => Simple Traversal s a -> Simple Traversal a a -> (a -> m a) -> s -> m s --transformMOnOf :: Monad m => LensLike (WrappedMonad m) s a a a -> SimpleLensLike (WrappedMonad m) a a -> (a -> m a) -> s -> m a -- | Recurse one level into a structure. (a.k.a composOp from -- Björn Bringert's compos) -- --
-- descend ≡ over plate --descend :: Plated a => (a -> a) -> a -> a -- | Recurse one level into a structure using a user specified recursion -- scheme. This is over, but it is supplied here for consistency -- with the uniplate API. -- --
-- descendOf ≡ over ---- --
-- descendOf :: Simple Setter s a -> (a -> a) -> s -> s -- descendOf :: Simple Traversal s a -> (a -> a) -> s -> s --descendOf :: Setting s t a b -> (a -> b) -> s -> t -- | Recurse one level into the parts of the structure delimited by a -- Setter. -- --
-- descendOn b ≡ over (b . plate) ---- --
-- descendOn :: Plated a => Setter s t -> (t -> t) -> s -> s --descendOn :: Plated a => Setting s t a a -> (a -> a) -> s -> t -- | Recurse one level into the parts delimited by one Setter, using -- another. -- --
-- descendOnOf b l ≡ over (b . l) ---- --
-- descendOnOf :: Simple Setter s a -> Simple Setter a a -> (a -> a) -> s -> s -- descendOnOf :: Simple Traversal s a -> Simple Traversal a a -> (a -> a) -> s -> s --descendOnOf :: Setting s t a b -> Setting a b u v -> (u -> v) -> s -> t -- | Recurse one level into a structure with an Applicative effect, -- this is plate, but it is supplied for consistency with the -- uniplate API. -- --
-- descendA ≡ plate --descendA :: (Applicative f, Plated a) => (a -> f a) -> a -> f a -- | Recurse one level into a structure using a user specified recursion -- scheme and Applicative effects. This is id, but it is -- supplied for consistency with the uniplate API. -- --
-- descendAOf ≡ id ---- --
-- descendAOf :: Applicative m => Simple Traversal s a => (a -> m a) -> s -> m s --descendAOf :: Applicative f => LensLike f s t a b -> (a -> f b) -> s -> f t -- | Recurse one level into the parts of the structure delimited by a -- Traversal with Applicative effects. -- --
-- descendAOn b ≡ b . plate ---- --
-- descendAOn :: (Applicative f, Plated' a) => Simple Traversal s a -> (a -> f a) -> s -> f s --descendAOn :: (Applicative f, Plated a) => LensLike f s t a a -> (a -> f a) -> s -> f t -- | Recurse one level into the parts delimited by one Traversal, -- using another with Applicative effects. -- --
-- descendAOnOf ≡ (.) ---- --
-- descendAOnOf :: Applicative f => Simple Traversal s a -> Simple Traversal a a -> (a -> f a) -> s -> f s --descendAOnOf :: Applicative f => LensLike f u v s t -> LensLike f s t a b -> (a -> f b) -> u -> f v -- |
-- descendA_ ≡ traverseOf_' plate --descendA_ :: (Applicative f, Plated a) => (a -> f b) -> a -> f () -- | Recurse one level into a structure using a user specified recursion -- scheme and Applicative effects, without reconstructing the -- structure behind you. -- -- This is just traverseOf_, but is provided for consistency. -- --
-- descendAOf_ ≡ traverseOf_ ---- --
-- descendAOf_ :: Applicative f => Fold s a => (a -> f r) -> s -> f () --descendAOf_ :: Applicative f => Getting (Traversed f) s t a b -> (a -> f r) -> s -> f () -- | Recurse one level into the parts of the structure delimited by a -- Traversal with monadic effects. -- --
-- descendAOn_ b ≡ traverseOf_ (b . plate) ---- --
-- descendAOn_ :: (Applicative f, Plated a) => Simple Traversal s a -> (a -> f r) -> s -> f () --descendAOn_ :: (Applicative f, Plated a) => Getting (Traversed f) s t a a -> (a -> f r) -> s -> f () -- | Recurse one level into the parts delimited by one Fold, using -- another with Applicative effects, without reconstructing the -- structure behind you. -- --
-- descendAOnOf_ b l ≡ traverseOf_ (b . l) ---- --
-- descendAOnOf_ :: Applicative f => Fold s a -> Fold a a -> (a -> f r) -> s -> f () --descendAOnOf_ :: Applicative f => Getting (Traversed f) s t a b -> Getting (Traversed f) a b a b -> (a -> f r) -> s -> f () -- | Recurse one level into a structure with a monadic effect. (a.k.a -- composOpM from Björn Bringert's compos) -- --
-- descendM ≡ mapMOf plate --descendM :: (Monad m, Plated a) => (a -> m a) -> a -> m a -- | Recurse one level into a structure using a user specified recursion -- scheme and monadic effects. This is id, but it is supplied for -- consistency with the uniplate API. -- --
-- descendMOf ≡ mapMOf ---- --
-- descendMOf :: Monad m => Simple Traversal s a => (a -> m a) -> s -> m s --descendMOf :: Monad m => LensLike (WrappedMonad m) s t a b -> (a -> m b) -> s -> m t -- | Recurse one level into the parts of the structure delimited by a -- Traversal with monadic effects. -- --
-- descendMOn b ≡ mapMOf (b . plate) ---- --
-- descendMOn :: (Monad m, Plated a) => Simple Traversal s a -> (a -> m a) -> s -> m s --descendMOn :: (Monad m, Plated a) => LensLike (WrappedMonad m) s t a a -> (a -> m a) -> s -> m t -- | Recurse one level into the parts delimited by one Traversal, -- using another with monadic effects. -- --
-- descendMOnOf b l ≡ mapMOf (b . l) ---- --
-- descendMOnOf :: Monad m => Simple Traversal s a -> Simple Traversal a a -> (a -> m a) -> s -> m s --descendMOnOf :: Monad m => LensLike (WrappedMonad m) s t a a -> SimpleLensLike (WrappedMonad m) a a -> (a -> m a) -> s -> m t -- | Descend one level into a structure with monadic effects (a.k.a -- composOpM from Björn Bringert's compos) -- --
-- descendM_ ≡ mapMOf_' plate --descendM_ :: (Monad m, Plated a) => (a -> m b) -> a -> m () -- | Recurse one level into a structure using a user specified recursion -- scheme and monadic effects. This is just mapMOf_, but is -- provided for consistency. -- --
-- descendMOf_ ≡ mapMOf_ ---- --
-- descendMOf_ :: Monad m => Fold s a => (a -> m a) -> s -> m () --descendMOf_ :: Monad m => Getting (Sequenced m) s t a b -> (a -> m r) -> s -> m () -- | Recurse one level into the parts of the structure delimited by a -- Traversal with monadic effects. -- --
-- descendMOn_ b ≡ mapMOf_ (b . plate) ---- --
-- descendMOn_ :: (Monad m, Plated a) => Simple Traversal s a -> (a -> m r) -> b -> m () --descendMOn_ :: (Monad m, Plated a) => Getting (Sequenced m) s t a a -> (a -> m r) -> s -> m () -- | Recurse one level into the parts delimited by one Traversal, -- using another with monadic effects. -- --
-- descendMOnOf_ b l ≡ mapMOf_ (b . l) ---- --
-- descendMOnOf_ :: Monad m => Fold s a -> Fold a a -> (a -> m a) -> s -> m () --descendMOnOf_ :: Monad m => Getting (Sequenced m) s t a b -> Getting (Sequenced m) a b a b -> (a -> m r) -> s -> m () -- | Return a list of all of the editable contexts for every location in -- the structure, recursively. -- --
-- propUniverse x = universe x == map pos (contexts x) -- propId x = all (== x) [extract w | w <- contexts x] ---- --
-- contexts ≡ contextsOf plate --contexts :: Plated a => a -> [Context a a a] -- | Return a list of all of the editable contexts for every location in -- the structure, recursively, using a user-specified Traversal to -- walk each layer. -- --
-- propUniverse l x = universeOf l x == map pos (contextsOf l x) -- propId l x = all (== x) [extract w | w <- contextsOf l x] ---- --
-- contextsOf :: Simple Traversal a a -> a -> [Context a a] --contextsOf :: SimpleLensLike (Bazaar a a) a a -> a -> [Context a a a] -- | Return a list of all of the editable contexts for every location in -- the structure in an areas indicated by a user supplied -- Traversal, recursively using plate. -- --
-- contextsOn b ≡ contextsOnOf b plate ---- --
-- contextsOn :: Plated a => Simple Traversal s a -> s -> [Context a a s] --contextsOn :: Plated a => LensLike (Bazaar a a) s t a a -> s -> [Context a a t] -- | Return a list of all of the editable contexts for every location in -- the structure in an areas indicated by a user supplied -- Traversal, recursively using another user-supplied -- Traversal to walk each layer. -- --
-- contextsOnOf :: Simple Traversal s a -> Simple Traversal a a -> s -> [Context a a s] --contextsOnOf :: LensLike (Bazaar a a) s t a a -> SimpleLensLike (Bazaar a a) a a -> s -> [Context a a t] -- | The one-level version of context. This extracts a list of the -- immediate children as editable contexts. -- -- Given a context you can use pos to see the values, -- peek at what the structure would be like with an edited -- result, or simply extract the original structure. -- --
-- propChildren x = children l x == map pos (holes l x) -- propId x = all (== x) [extract w | w <- holes l x] ---- --
-- holes = holesOf plate --holes :: Plated a => a -> [Context a a a] -- | The one-level version of contextsOf. This extracts a list of -- the immediate children according to a given Traversal as -- editable contexts. -- -- Given a context you can use pos to see the values, -- peek at what the structure would be like with an edited -- result, or simply extract the original structure. -- --
-- propChildren l x = childrenOf l x == map pos (holesOf l x) -- propId l x = all (== x) [extract w | w <- holesOf l x] ---- --
-- holesOf :: Simple Iso s a -> s -> [Context a a s] -- holesOf :: Simple Lens s a -> s -> [Context a a s] -- holesOf :: Simple Traversal s a -> s -> [Context a a s] --holesOf :: LensLike (Bazaar a a) s t a a -> s -> [Context a a t] -- | An alias for holesOf, provided for consistency with the other -- combinators. -- --
-- holesOn ≡ holesOf ---- --
-- holesOn :: Simple Iso s a -> s -> [Context a a s] -- holesOn :: Simple Lens s a -> s -> [Context a a s] -- holesOn :: Simple Traversal s a -> s -> [Context a a s] --holesOn :: LensLike (Bazaar a a) s t a a -> s -> [Context a a t] -- | Extract one level of holes from a container in a region specified by -- one Traversal, using another. -- --
-- holesOnOf b l ≡ holesOf (b . l) ---- --
-- holesOnOf :: Simple Iso s a -> Simple Iso a a -> s -> [Context a a s] -- holesOnOf :: Simple Lens s a -> Simple Lens a a -> s -> [Context a a s] -- holesOnOf :: Simple Traversal s a -> Simple Traversal a a -> s -> [Context a a s] --holesOnOf :: LensLike (Bazaar r r) s t a b -> LensLike (Bazaar r r) a b r r -> s -> [Context r r t] -- | Perform a fold-like computation on each value, technically a -- paramorphism. -- --
-- para ≡ paraOf plate --para :: Plated a => (a -> [r] -> r) -> a -> r -- | Perform a fold-like computation on each value, technically a -- paramorphism. -- --
-- paraOf :: Fold a a -> (a -> [r] -> r) -> a -> r --paraOf :: Getting [a] a b a b -> (a -> [r] -> r) -> a -> r -- | Fold the immediate children of a Plated container. -- --
-- composOpFold z c f = foldrOf plate (c . f) z --composOpFold :: Plated a => b -> (b -> b -> b) -> (a -> b) -> a -> b -- | Access the nth element of a Traversable container. -- -- Attempts to access beyond the range of the Traversal will cause -- an error. -- --
-- element ≡ elementOf traverse --element :: Traversable t => Int -> Simple Lens (t a) a -- | A Lens to 'Control.Lens.Getter.view'/'Control.Lens.Setter.set' -- the nth element elementOf a Traversal, Lens or -- Iso. -- -- Attempts to access beyond the range of the Traversal will cause -- an error. -- --
-- >>> [[1],[3,4]]^.elementOf (traverse.traverse) 1 -- 3 --elementOf :: Functor f => LensLike (Bazaar a a) s t a a -> Int -> LensLike f s t a a -- | The original uniplate combinator, implemented in terms of -- Plated as a Lens. -- --
-- parts ≡ partsOf plate ---- -- The resulting lens is safer to use as it ignores 'over-application' -- and deals gracefully with under-application, but it is only a proper -- lens if you don't change the list length! parts :: Plated a => Simple Lens a [a] -- | partsOf turns a Traversal into a lens that resembles an -- early version of the uniplate (or biplate) type. -- -- Note: You should really try to maintain the invariant of the -- number of children in the list. -- -- Any extras will be lost. If you do not supply enough, then the -- remainder will come from the original structure. -- -- So technically, this is only a lens if you do not change the number of -- results it returns. -- --
-- partsOf :: Simple Iso s a -> Simple Lens s [a] -- partsOf :: Simple Lens s a -> Simple Lens s [a] -- partsOf :: Simple Traversal s a -> Simple Lens s [a] --partsOf :: LensLike (Bazaar a a) s t a a -> Lens s t [a] [a] -- | unsafePartsOf turns a Traversal into a uniplate -- (or biplate) family. -- -- If you do not need the types of s and t to be -- different, it is recommended that you use partsOf -- -- It is generally safer to traverse with the Bazaar rather than -- use this combinator. However, it is sometimes convenient. -- -- This is unsafe because if you don't supply at least as many -- b's as you were given a's, then the reconstruction -- of t will result in an error! -- --
-- unsafePartsOf :: Iso s t a b -> Lens s t [a] [b] -- unsafePartsOf :: Lens s t a b -> Lens s t [a] [b] -- unsafePartsOf :: Traversal s t a b -> Lens s t [a] [b] --unsafePartsOf :: LensLike (Bazaar a b) s t a b -> Lens s t [a] [b] instance Plated (Tree a) instance Plated [a] -- | This module provides a Zipper with fairly strong type checking -- guarantees. -- -- The code here is inspired by Brandon Simmons' zippo package, -- but uses a slightly different approach to represent the Zipper -- that makes the whole thing look like his breadcrumb trail, and can -- move side-to-side through traversals. -- -- Some examples types: -- --
-- >>> zipper ("hello","world") % down _1 % fromWithin traverse % focus .~ 'J' % rightmost % focus .~ 'y' % rezip
-- ("Jelly","world")
--
--
-- This is particularly powerful when compiled with plate,
-- uniplate or biplate for walking down into self-similar
-- children in syntax trees and other structures.
module Control.Lens.Zipper
-- | This is used to represent the Top of the Zipper.
--
-- Every Zipper starts with Top.
--
-- e.g. Top :> a is the trivial zipper.
data Top
-- | This is the type of a Zipper. It visually resembes a
-- 'breadcrumb trail' as used in website navigation. Each breadcrumb in
-- the trail represents a level you can move up to.
--
-- This type operator associates to the right, so you can use a type like
--
-- -- Top :> (String,Double) :> String :> Char ---- -- to represent a zipper from (String,Double) down -- to Char that has an intermediate crumb for the String -- containing the Char. data (:>) p a -- | Construct a zipper that can explore anything. zipper :: a -> Top :> a -- | This Lens views the current target of the zipper. focus :: SimpleIndexedLens (Tape (h :> a)) (h :> a) a -- | Move the zipper up, closing the current level and -- focusing on the parent element. up :: ((a :> b) :> c) -> a :> b -- | Step down into a Lens. This is a constrained form of -- fromWithin for when you know there is precisely one target. -- --
-- down :: Simple Lens b c -> (a :> b) -> a :> b :> c -- down :: Simple Iso b c -> (a :> b) -> a :> b :> c --down :: SimpleLensLike (Context c c) b c -> (a :> b) -> (a :> b) :> c -- | Step down into the leftmost entry of a Traversal. -- --
-- within :: Simple Traversal b c -> (a :> b) -> Maybe (a :> b :> c) -- within :: Simple Lens b c -> (a :> b) -> Maybe (a :> b :> c) -- within :: Simple Iso b c -> (a :> b) -> Maybe (a :> b :> c) --within :: SimpleLensLike (Bazaar c c) b c -> (a :> b) -> Maybe ((a :> b) :> c) -- | Unsafely step down into a Traversal that is assumed to -- be non-empty. -- -- If this invariant is not met then this will usually result in an -- error! -- --
-- fromWithin :: Simple Traversal b c -> (a :> b) -> a :> b :> c -- fromWithin :: Simple Lens b c -> (a :> b) -> a :> b :> c -- fromWithin :: Simple Iso b c -> (a :> b) -> a :> b :> c ---- -- You can reason about this function as if the definition was: -- --
-- fromWithin l ≡ 'fromJust ' . within l ---- -- but it is lazier in such a way that if this invariant is violated, -- some code can still succeed if it is lazy enough in the use of the -- focused value. fromWithin :: SimpleLensLike (Bazaar c c) b c -> (a :> b) -> (a :> b) :> c -- | Pull the zipper left within the current -- Traversal. left :: (a :> b) -> Maybe (a :> b) -- | Try to pull the zipper one entry to the left. -- -- If the entry to the left doesn't exist, then stay still. left1 :: (a :> b) -> a :> b -- | Try to pull the zipper n entries to the left, -- returning Nothing if you pull too far and run out of entries. lefts :: Int -> (h :> a) -> Maybe (h :> a) -- | Try to pull the zipper n entries to the left. -- Stopping at the first entry if you run out of entries. -- -- Passing a negative n will move to -n entries the -- right, and will return the last entry if you run out of entries. lefts1 :: Int -> (h :> a) -> h :> a -- | Move to the left-most position of the current Traversal. leftmost :: (a :> b) -> a :> b -- | Pull the entry one entry to the right right :: (a :> b) -> Maybe (a :> b) -- | Try to pull the zipper one entry to the right. -- -- If the entry doesn't exist, then stay still. right1 :: (a :> b) -> a :> b -- | Try to pull the zipper n entries to the right, -- returning Nothing if you pull too far and run out of entries. -- -- Passing a negative n will move -n entries to the -- left. rights :: Int -> (h :> a) -> Maybe (h :> a) -- | Try to pull the zipper n entries to the right. -- Stopping at the last entry if you run out of entries. -- -- Passing a negative number will move to the left and will return the -- first entry if you run out of entries. rights1 :: Int -> (h :> a) -> h :> a -- | Move to the right-most position of the current Traversal. rightmost :: (a :> b) -> a :> b -- | Move the zipper horizontally to the element in the nth -- position in the current level. (absolutely indexed, starting with the -- leftmost as 0) -- -- This returns Nothing if the target element doesn't exist. -- --
-- goto n = rights n . leftmost --goto :: Int -> (a :> b) -> Maybe (a :> b) -- | Move the zipper horizontally to the element in the nth -- position of the current level. (absolutely indexed, starting with the -- leftmost as 0) -- -- If the element at that position doesn't exist, then this will clamp to -- the range 0 <= n < width z and return the -- element there. goto1 :: Int -> (a :> b) -> a :> b -- | Return the index into the current Traversal. -- --
-- goto (coordinate l) l = Just' --coordinate :: (a :> b) -> Int -- | Returns the number of siblings at the current level in the -- zipper. -- --
-- width z >= 1 ---- -- NB: If the current Traversal targets an infinite -- number of elements then this may not terminate. width :: (a :> b) -> Int -- | Close something back up that you opened as a zipper. rezip :: Zipper h a => (h :> a) -> Zipped h a -- | This represents the type a zipper will have when it is fully -- Zipped back up. -- | This enables us to pull the zipper back up to the Top. class Zipper h a -- | A Tape is a recorded path through the Traversal chain -- of a Zipper. data Tape k -- | Save the current path as as a Tape we can play back later. save :: (a :> b) -> Tape (a :> b) -- | Restore ourselves to a previously recorded position precisely. -- -- If the position does not exist, then fail. restore :: Tape (h :> a) -> Zipped h a -> Maybe (h :> a) -- | Restore ourselves to a previously recorded position. -- -- When moving left to right through a Traversal, if this will -- clamp at each level to the range 0 <= k < width, so the -- only failures will occur when one of the sequence of downward -- traversals find no targets. restore1 :: Tape (h :> a) -> Zipped h a -> Maybe (h :> a) -- | Restore ourselves to a previously recorded position. -- -- This assumes that nothing has been done in the meantime to affect the -- existence of anything on the entire path. -- -- Motions left or right are clamped, but all traversals included on the -- Tape are assumed to be non-empty. -- -- Violate these assumptions at your own risk. unsafelyRestore :: Tape (h :> a) -> Zipped h a -> h :> a instance Zipper h b => Zipper (h :> b) c instance Zipper Top a -- | (These need to be defined together for DefaultSignatures to -- work.) module Control.Lens.WithIndex -- | A Functor with an additional index. -- -- Instances must satisfy a modified form of the Functor laws: -- --
-- imap f . imap g ≡ imap (i -> f i . g i) -- imap (_ a -> a) ≡ id --class Functor f => FunctorWithIndex i f | f -> i imap :: FunctorWithIndex i f => (i -> a -> b) -> f a -> f b -- | The IndexedSetter for a FunctorWithIndex. -- -- If you don't need access to the index, then mapped is more -- flexible in what it accepts. imapped :: FunctorWithIndex i f => IndexedSetter i (f a) (f b) a b -- | A container that supports folding with an additional index. class Foldable f => FoldableWithIndex i f | f -> i where ifoldr f z t = appEndo (ifoldMap (\ i -> Endo . f i) t) z ifoldl f z t = appEndo (getDual (ifoldMap (\ i -> Dual . Endo . flip (f i)) t)) z ifoldr' f z0 xs = ifoldl f' id xs z0 where f' i k x z = k $! f i x z ifoldl' f z0 xs = ifoldr f' id xs z0 where f' i x k z = k $! f i z x ifoldMap :: (FoldableWithIndex i f, Monoid m) => (i -> a -> m) -> f a -> m ifoldr :: FoldableWithIndex i f => (i -> a -> b -> b) -> b -> f a -> b ifoldl :: FoldableWithIndex i f => (i -> b -> a -> b) -> b -> f a -> b ifoldr' :: FoldableWithIndex i f => (i -> a -> b -> b) -> b -> f a -> b ifoldl' :: FoldableWithIndex i f => (i -> b -> a -> b) -> b -> f a -> b -- | The IndexedFold of a FoldableWithIndex container. ifolded :: FoldableWithIndex i f => IndexedFold i (f a) a -- | Obtain a Fold by lifting an operation that returns a foldable -- result. -- -- This can be useful to lift operations from Data.List and -- elsewhere into a Fold. ifolding :: FoldableWithIndex i f => (a -> f c) -> IndexedFold i a c -- | Return whether or not any element in a container satisfies a -- predicate, with access to the index i. -- -- When you don't need access to the index then any is more -- flexible in what it accepts. -- --
-- any = iany . const --iany :: FoldableWithIndex i f => (i -> a -> Bool) -> f a -> Bool -- | Return whether or not all elements in a container satisfy a predicate, -- with access to the index i. -- -- When you don't need access to the index then all is more -- flexible in what it accepts. -- --
-- all ≡ iall . const --iall :: FoldableWithIndex i f => (i -> a -> Bool) -> f a -> Bool -- | Traverse elements with access to the index i, discarding the -- results. -- -- When you don't need access to the index then traverse_ is more -- flexible in what it accepts. -- --
-- traverse_ l = itraverse . const --itraverse_ :: (FoldableWithIndex i t, Applicative f) => (i -> a -> f b) -> t a -> f () -- | Traverse elements with access to the index i, discarding the -- results (with the arguments flipped). -- --
-- ifor_ ≡ flip itraverse_ ---- -- When you don't need access to the index then for_ is more -- flexible in what it accepts. -- --
-- for_ a ≡ ifor_ a . const --ifor_ :: (FoldableWithIndex i t, Applicative f) => t a -> (i -> a -> f b) -> f () -- | Run monadic actions for each target of an IndexedFold or -- IndexedTraversal with access to the index, discarding the -- results. -- -- When you don't need access to the index then mapMOf_ is more -- flexible in what it accepts. -- --
-- mapM_ ≡ imapM . const --imapM_ :: (FoldableWithIndex i t, Monad m) => (i -> a -> m b) -> t a -> m () -- | Run monadic actions for each target of an IndexedFold or -- IndexedTraversal with access to the index, discarding the -- results (with the arguments flipped). -- --
-- iforM_ ≡ flip imapM_ ---- -- When you don't need access to the index then forMOf_ is more -- flexible in what it accepts. -- --
-- forMOf_ l a ≡ iforMOf l a . const --iforM_ :: (FoldableWithIndex i t, Monad m) => t a -> (i -> a -> m b) -> m () -- | Concatenate the results of a function of the elements of an indexed -- container with access to the index. -- -- When you don't need access to the index then concatMap is more -- flexible in what it accepts. -- --
-- concatMap ≡ iconcatMap . const -- iconcatMap ≡ ifoldMap --iconcatMap :: FoldableWithIndex i f => (i -> a -> [b]) -> f a -> [b] -- | Searches a container with a predicate that is also supplied the index, -- returning the left-most element of the structure matching the -- predicate, or Nothing if there is no such element. -- -- When you don't need access to the index then find is more -- flexible in what it accepts. -- --
-- find ≡ ifind . const --ifind :: FoldableWithIndex i f => (i -> a -> Bool) -> f a -> Maybe (i, a) -- | Monadic fold right over the elements of a structure with an index. -- -- When you don't need access to the index then foldrM is more -- flexible in what it accepts. -- --
-- foldrM ≡ ifoldrM . const --ifoldrM :: (FoldableWithIndex i f, Monad m) => (i -> a -> b -> m b) -> b -> f a -> m b -- | Monadic fold over the elements of a structure with an index, -- associating to the left. -- -- When you don't need access to the index then foldlM is more -- flexible in what it accepts. -- --
-- foldlM ≡ ifoldlM . const --ifoldlM :: (FoldableWithIndex i f, Monad m) => (i -> b -> a -> m b) -> b -> f a -> m b -- | Extract the key-value pairs from a structure. -- -- When you don't need access to the indices in the result, then -- toList is more flexible in what it accepts. -- --
-- toList ≡ map fst . itoList --itoList :: FoldableWithIndex i f => f a -> [(i, a)] -- | Fold a container with indices returning both the indices and the -- values. withIndices :: FoldableWithIndex i f => Fold (f a) (i, a) -- | Fold a container with indices returning only the indices. indices :: FoldableWithIndex i f => Fold (f a) i -- | A Traversable with an additional index. -- -- An instance must satisfy a (modified) form of the Traversable -- laws: -- --
-- itraverse (const Identity) ≡ Identity -- fmap (itraverse f) . itraverse g ≡ getCompose . itraverse (i -> Compose . fmap (f i) . g i) --class (FunctorWithIndex i t, FoldableWithIndex i t, Traversable t) => TraversableWithIndex i t | t -> i itraverse :: (TraversableWithIndex i t, Applicative f) => (i -> a -> f b) -> t a -> f (t b) -- | The IndexedTraversal of a TraversableWithIndex -- container. itraversed :: TraversableWithIndex i f => IndexedTraversal i (f a) (f b) a b -- | Traverse with an index (and the arguments flipped) -- --
-- for a ≡ ifor a . const -- ifor ≡ flip itraverse --ifor :: (TraversableWithIndex i t, Applicative f) => t a -> (i -> a -> f b) -> f (t b) -- | Map each element of a structure to a monadic action, evaluate these -- actions from left to right, and collect the results, with access the -- index. -- -- When you don't need access to the index mapM is more liberal in -- what it can accept. -- --
-- mapM ≡ imapM . const --imapM :: (TraversableWithIndex i t, Monad m) => (i -> a -> m b) -> t a -> m (t b) -- | Map each element of a structure to a monadic action, evaluate these -- actions from left to right, and collect the results, with access its -- position (and the arguments flipped). -- --
-- forM a ≡ iforM a . const -- iforM ≡ flip imapM --iforM :: (TraversableWithIndex i t, Monad m) => t a -> (i -> a -> m b) -> m (t b) -- | Generalizes mapAccumR to add access to the index. -- -- imapAccumROf accumulates state from right to left. -- --
-- mapAccumR ≡ imapAccumR . const --imapAccumR :: TraversableWithIndex i t => (i -> s -> a -> (s, b)) -> s -> t a -> (s, t b) -- | Generalizes mapAccumL to add access to the index. -- -- imapAccumLOf accumulates state from left to right. -- --
-- mapAccumLOf ≡ imapAccumL . const --imapAccumL :: TraversableWithIndex i t => (i -> s -> a -> (s, b)) -> s -> t a -> (s, t b) -- | Access the element of an indexed container where the index matches a -- predicate. -- --
-- >>> over (iwhere (>0)) Prelude.reverse $ ["He","was","stressed","o_O"] -- ["He","saw","desserts","O_o"] --iwhere :: TraversableWithIndex i t => (i -> Bool) -> SimpleIndexedTraversal i (t a) a instance (Eq k, Hashable k) => TraversableWithIndex k (HashMap k) instance (Eq k, Hashable k) => FoldableWithIndex k (HashMap k) instance (Eq k, Hashable k) => FunctorWithIndex k (HashMap k) instance Ord k => TraversableWithIndex k (Map k) instance Ord k => FoldableWithIndex k (Map k) instance Ord k => FunctorWithIndex k (Map k) instance TraversableWithIndex Int IntMap instance FoldableWithIndex Int IntMap instance FunctorWithIndex Int IntMap instance TraversableWithIndex Int Seq instance FoldableWithIndex Int Seq instance FunctorWithIndex Int Seq instance TraversableWithIndex Int [] instance FoldableWithIndex Int [] instance FunctorWithIndex Int [] module Control.Lens.TH -- | Build lenses (and traversals) with a sensible default configuration. -- --
-- makeLenses = makeLensesWith lensRules --makeLenses :: Name -> Q [Dec] -- | Derive lenses and traversals, specifying explicit pairings of -- (fieldName, lensName). -- -- If you map multiple names to the same label, and it is present in the -- same constructor then this will generate a Traversal. -- -- e.g. -- --
-- makeLensesFor [("_foo", "fooLens"), ("baz", "lbaz")] ''Foo
-- makeLensesFor [("_barX", "bar"), ("_barY", "bar)] ''Bar
--
makeLensesFor :: [(String, String)] -> Name -> Q [Dec]
-- | Make lenses and traversals for a type, and create a class when the
-- type has no arguments.
--
-- e.g.
--
--
-- data Foo = Foo { _fooX, _fooY :: Int }
-- makeClassy ''Foo
--
--
-- will create
--
-- -- class HasFoo t where -- foo :: Simple Lens t Foo -- instance HasFoo Foo where foo = id -- fooX, fooY :: HasFoo t => Simple Lens t Int ---- --
-- makeClassy = makeLensesWith classyRules --makeClassy :: Name -> Q [Dec] -- | Derive lenses and traversals, using a named wrapper class, and -- specifying explicit pairings of (fieldName, traversalName). -- -- Example usage: -- --
-- makeClassyFor "HasFoo" "foo" [("_foo", "fooLens"), ("bar", "lbar")] ''Foo
--
makeClassyFor :: String -> String -> [(String, String)] -> Name -> Q [Dec]
-- | Make a top level isomorphism injecting into the type.
--
-- The supplied name is required to be for a type with a single
-- constructor that has a single argument
--
-- e.g.
--
-- -- newtype List a = List [a] -- makeIso ''List ---- -- will create -- --
-- list :: Iso [a] [b] (List a) (List b) ---- --
-- makeIso = makeLensesWith isoRules --makeIso :: Name -> Q [Dec] -- | Build lenses with a custom configuration. makeLensesWith :: LensRules -> Name -> Q [Dec] -- | Default lens rules defaultRules :: LensRules -- | This configuration describes the options we'll be using to make -- isomorphisms or lenses. data LensRules LensRules :: (String -> Maybe String) -> (String -> Maybe String) -> (String -> Maybe (String, String)) -> Set LensFlag -> LensRules -- | Rules for making fairly simple partial lenses, ignoring the special -- cases for isomorphisms and traversals, and not making any classes. lensRules :: LensRules -- | Rules for making lenses and traversals that precompose another lens. classyRules :: LensRules -- | Rules for making an isomorphism from a data type isoRules :: LensRules -- | Lens to access the convention for naming top level isomorphisms in our -- lens rules. -- -- Defaults to lowercasing the first letter of the constructor. lensIso :: Simple Lens LensRules (String -> Maybe String) -- | Lens to access the convention for naming fields in our lens rules. -- -- Defaults to stripping the _ off of the field name, lowercasing the -- name, and rejecting the field if it doesn't start with an '_'. lensField :: Simple Lens LensRules (String -> Maybe String) -- | Retrieve options such as the name of the class and method to put in it -- to build a class around monomorphic data types. lensClass :: Simple Lens LensRules (String -> Maybe (String, String)) -- | Retrieve options such as the name of the class and method to put in it -- to build a class around monomorphic data types. lensFlags :: Simple Lens LensRules (Set LensFlag) -- | Flags for lens construction data LensFlag SimpleLenses :: LensFlag PartialLenses :: LensFlag BuildTraversals :: LensFlag SingletonAndField :: LensFlag SingletonIso :: LensFlag HandleSingletons :: LensFlag SingletonRequired :: LensFlag CreateClass :: LensFlag CreateInstance :: LensFlag ClassRequired :: LensFlag GenerateSignatures :: LensFlag -- | Only Generate valid Simple Lens lenses. simpleLenses :: Simple Lens LensRules Bool -- | Enables the generation of partial lenses, generating runtime errors -- for every constructor that does not have a valid definition for the -- lens. This occurs when the constructor lacks the field, or has -- multiple fields mapped to the same lens. partialLenses :: Simple Lens LensRules Bool -- | In the situations that a lens would be partial, when -- partialLenses is used, this flag instead causes traversals to -- be generated. Only one can be used, and if neither are, then -- compile-time errors are generated. buildTraversals :: Simple Lens LensRules Bool -- | Handle singleton constructors specially. handleSingletons :: Simple Lens LensRules Bool -- | Use Iso for singleton constructors. singletonIso :: Simple Lens LensRules Bool -- | Expect a single constructor, single field newtype or data type. singletonRequired :: Simple Lens LensRules Bool -- | Create the class if the constructor is simple and the lensClass -- rule matches. createClass :: Simple Lens LensRules Bool -- | Create the instance if the constructor is simple and the -- lensClass rule matches. createInstance :: Simple Lens LensRules Bool -- | Die if the lensClass fails to match. classRequired :: Simple Lens LensRules Bool -- | When building a singleton Iso (or Lens) for a record -- constructor, build both the Iso (or Lens) for the record -- and the one for the field. singletonAndField :: Simple Lens LensRules Bool -- | Indicate whether or not to supply the signatures for the generated -- lenses. -- -- Disabling this can be useful if you want to provide a more restricted -- type signature or if you want to supply hand-written haddocks. generateSignatures :: Simple Lens LensRules Bool instance Eq LensFlag instance Ord LensFlag instance Show LensFlag instance Read LensFlag -- | Usage: -- -- You can derive lenses automatically for many data types: -- --
-- import Control.Lens
-- data Foo a = Foo { _fooArgs :: [String], _fooValue :: a }
-- makeLenses ''Foo
--
--
-- This defines the following lenses:
--
-- -- fooArgs :: Simple Lens (Foo a) [String] -- fooValue :: Lens (Foo a) (Foo b) a b ---- -- You can then access the value with (^.) and set the value of -- the field with (.~) and can use almost any other combinator -- that is re-exported here on those fields. -- -- The combinators here have unusually specific type signatures, so for -- particularly tricky ones, the simpler type signatures you might want -- to pretend the combinators have are specified as well. -- -- More information on how to use lenses is available on the lens wiki: -- -- http://github.com/ekmett/lens/wiki -- module Control.Lens -- | Note: GHC.Generics exports a number of names that collide -- with Control.Lens. -- -- You can use hiding or imports to mitigate this to an extent, and the -- following imports, represent a fair compromise for user code: -- --
-- import Control.Lens hiding (Rep) -- import GHC.Generics hiding (from, to) ---- -- You can use generic to replace from and to from -- GHC.Generics, and probably won't be explicitly referencing -- Rep from Control.Lens in code that uses generics. module GHC.Generics.Lens -- | Convert from the data type to its representation (or back) -- --
-- >>> "hello"^.generic.from generic :: String -- "hello" --generic :: Generic a => Simple Iso a (Rep a b) -- | Convert from the data type to its representation (or back) generic1 :: Generic1 f => Simple Iso (f a) (Rep1 f a) -- | A Generic Traversal that visits every occurence of -- something Typeable anywhere in a container. -- --
-- >>> allOf tinplate (=="Hello") (1::Int,2::Double,(),"Hello",["Hello"]) -- True ---- --
-- >>> mapMOf_ tinplate putStrLn ("hello",[(2 :: Int, "world!")])
-- hello
-- world!
--
tinplate :: (Generic a, GTraversal (Rep a), Typeable b) => Simple Traversal a b
-- | Used to traverse Generic data by uniplate.
class GTraversal f
instance (Traversable f, GTraversal g) => GTraversal (f :.: g)
instance GTraversal a => GTraversal (M1 i c a)
instance (GTraversal f, GTraversal g) => GTraversal (f :+: g)
instance (GTraversal f, GTraversal g) => GTraversal (f :*: g)
instance GTraversal U1
instance (Generic a, GTraversal (Rep a), Typeable a) => GTraversal (K1 i a)
module Control.Exception.Lens
-- | Traverse the strongly typed Exception contained in
-- SomeException where the type of your function matches the
-- desired Exception.
--
-- -- exception :: (Applicative f, Exception a, Exception b) -- => (a -> f b) -> SomeException -> f SomeException --exception :: (Exception a, Exception b) => Projection SomeException SomeException a b module Data.Bits.Lens -- | Bitwise .|. the target(s) of a Lens or Setter -- --
-- >>> _2 |~ 6 $ ("hello",3)
-- ("hello",7)
--
--
--
-- (|~) :: Bits a => Setter s t a a -> a -> s -> t
-- (|~) :: Bits a => Iso s t a a -> a -> s -> t
-- (|~) :: Bits a => Lens s t a a -> a -> s -> t
-- (|~) :: ('Monoid a', Bits a) => Traversal s t a a -> a -> s -> t
--
(|~) :: Bits a => Setting s t a a -> a -> s -> t
-- | Bitwise .&. the target(s) of a Lens or Setter
--
--
-- >>> _2 &~ 7 $ ("hello",254)
-- ("hello",6)
--
--
--
-- (&~) :: Bits a => Setter s t a a -> a -> s -> t
-- (&~) :: Bits a => Iso s t a a -> a -> s -> t
-- (&~) :: Bits a => Lens s t a a -> a -> s -> t
-- (&~) :: ('Monoid a', Bits a) => Traversal s t a a -> a -> s -> t
--
(&~) :: Bits a => Setting s t a a -> a -> s -> t
-- | Bitwise .|. the target(s) of a Lens (or
-- Traversal), returning the result (or a monoidal summary of all
-- of the results).
--
--
-- >>> _2 <|~ 6 $ ("hello",3)
-- (7,("hello",7))
--
--
-- -- (<|~) :: Bits a => Iso s t a a -> a -> s -> (a, t) -- (<|~) :: Bits a => Lens s t a a -> a -> s -> (a, t) -- (<|~) :: (Bits a, 'Monoid a) => Traversal s t a a -> a -> s -> (a, t) --(<|~) :: Bits a => LensLike ((,) a) s t a a -> a -> s -> (a, t) -- | Bitwise .&. the target(s) of a Lens or -- Traversal, returning the result (or a monoidal summary of all -- of the results). -- --
-- >>> _2 <&~ 7 $ ("hello",254)
-- (6,("hello",6))
--
--
-- -- (<&~) :: Bits a => Iso s t a a -> a -> s -> (a, t) -- (<&~) :: Bits a => Lens s t a a -> a -> s -> (a, t) -- (<&~) :: (Bits a, 'Monoid a) => Traversal s t a a -> a -> s -> (a, t) --(<&~) :: Bits a => LensLike ((,) a) s t a a -> a -> s -> (a, t) -- | Modify the target(s) of a Simple Lens, Setter or -- Traversal by computing its bitwise .|. with another -- value. -- --
-- (|=) :: (MonadState s m, Bits a) => Simple Setter s a -> a -> m () -- (|=) :: (MonadState s m, Bits a) => Simple Iso s a -> a -> m () -- (|=) :: (MonadState s m, Bits a) => Simple Lens s a -> a -> m () -- (|=) :: (MonadState s m, Bits a) => Simple Traversal s a -> a -> m () --(|=) :: (MonadState s m, Bits a) => Simple Setting s a -> a -> m () -- | Modify the target(s) of a Simple Lens, Setter or -- Traversal by computing its bitwise .&. with another -- value. -- --
-- (&=) :: (MonadState s m, Bits a) => Simple Setter s a -> a -> m () -- (&=) :: (MonadState s m, Bits a) => Simple Iso s a -> a -> m () -- (&=) :: (MonadState s m, Bits a) => Simple Lens s a -> a -> m () -- (&=) :: (MonadState s m, Bits a) => Simple Traversal s a -> a -> m () --(&=) :: (MonadState s m, Bits a) => Simple Setting s a -> a -> m () -- | Modify the target(s) of a Simple Lens, (or -- Traversal) by computing its bitwise .|. with another -- value, returning the result (or a monoidal summary of all of the -- results traversed) -- --
-- (<|=) :: (MonadState s m, Bits a) => Simple Lens s a -> a -> m a -- (<|=) :: (MonadState s m, Bits a, Monoid a) => Simple Traversal s a -> a -> m a --(<|=) :: (MonadState s m, Bits a) => SimpleLensLike ((,) a) s a -> a -> m a -- | Modify the target(s) of a Simple Lens (or -- Traversal) by computing its bitwise .&. with another -- value, returning the result (or a monoidal summary of all of the -- results traversed) -- --
-- (<&=) :: (MonadState s m, Bits a) => Simple Lens s a -> a -> m a -- (<&=) :: (MonadState s m, Bits a, Monoid a) => Simple Traversal s a -> a -> m a --(<&=) :: (MonadState s m, Bits a) => SimpleLensLike ((,) a) s a -> a -> m a -- | This lens can be used to access the value of the nth bit in a number. -- -- bitAt n is only a legal Lens into b if -- 0 <= n < bitSize (undefined :: b) -- --
-- >>> 16^.bitAt 4 -- True ---- --
-- >>> 15^.bitAt 4 -- False --bitAt :: Bits b => Int -> SimpleIndexedLens Int b Bool -- | Traverse over all bits in a numeric type. -- -- The bit position is available as the index. -- --
-- >>> import Data.Word -- -- >>> toListOf traverseBits (5 :: Word8) -- [True,False,True,False,False,False,False,False] ---- -- If you supply this an Integer, the result will be an infinite -- Traversal that can be productively consumed. traverseBits :: (Num b, Bits b) => SimpleIndexedTraversal Int b Bool -- | Traversals for manipulating parts of a list. module Data.List.Lens -- | A lens reading and writing to the head of a non-empty list. -- -- Attempting to read or write to the head of an empty list will -- result in an error. -- --
-- >>> [1,2,3]^._head -- 1 --_head :: Simple Lens [a] a -- | A lens reading and writing to the tail of a non-empty list -- -- Attempting to read or write to the tail of an empty list will -- result in an error. -- --
-- >>> _tail .~ [3,4,5] $ [1,2] -- [1,3,4,5] --_tail :: Simple Lens [a] [a] -- | A lens reading and writing to the last element of a non-empty -- list -- -- Attempting to read or write to the last element of an empty -- list will result in an error. -- --
-- >>> [1,2]^._last -- 2 --_last :: Simple Lens [a] a -- | A lens reading and replacing all but the a last element of a -- non-empty list -- -- Attempting to read or write to all but the last element of an -- empty list will result in an error. -- --
-- >>> [1,2,3,4]^._init -- [1,2,3] --_init :: Simple Lens [a] [a] -- | Obtain a version of the list with the supplied value interspersed. -- --
-- >>> "abcde"^.interspersed ',' -- "a,b,c,d,e" ---- --
-- xs^.interspersed a = intersperse a xs --interspersed :: a -> Getter [a] [a] -- | Obtain a version of the list with the supplied value intercalated. intercalated :: [a] -> Getter [[a]] [a] -- | Indexed traversal of a list. The position in the list is available as -- the index. traverseList :: IndexedTraversal Int [a] [b] a b -- | A traversal for reading and writing to the head of a list -- -- The position of the head in the original list (0) is available as the -- index. -- --
-- >>> traverseHead +~ 1 $ [1,2,3] -- [2,2,3] ---- --
-- traverseHead :: Applicative f => (a -> f a) -> [a] -> f [a] --traverseHead :: SimpleIndexedTraversal Int [a] a -- | A traversal for editing the tail of a list -- -- The position of each element in the original list is available -- as the index. -- --
-- >>> traverseTail +~ 1 $ [1,2,3] -- [1,3,4] ---- --
-- traverseTail :: Applicative f => (a -> f a) -> [a] -> f [a] --traverseTail :: SimpleIndexedTraversal Int [a] a -- | A traversal of all but the last element of a list -- -- The position of each element is available as the index. -- --
-- >>> traverseInit +~ 1 $ [1,2,3] -- [2,3,3] ---- --
-- traverseInit :: Applicative f => (a -> f a) -> [a] -> f [a] --traverseInit :: SimpleIndexedTraversal Int [a] a -- | A traversal the last element in a list -- -- The position of the last element in the original list is available as -- the index. -- --
-- >>> traverseLast +~ 1 $ [1,2,3] -- [1,2,4] ---- --
-- traverseLast :: Applicative f => (a -> f a) -> [a] -> f [a] --traverseLast :: SimpleIndexedTraversal Int [a] a -- | Cons onto the list(s) referenced by a Setter. -- --
-- >>> 'h' ~: _1 $ ("ello","world")
-- ("hello","world")
--
--
-- -- (~:) :: b -> Simple Setter s [a] -> s -> s -- (~:) :: b -> Simple Traversal s [a] -> s -> s -- (~:) :: b -> Simple Lens s [a] -> s -> s -- (~:) :: b -> Simple Iso s [a] -> s -> s --(~:) :: a -> Setting s t [a] [a] -> s -> t -- | Cons onto the list(s) referenced by a Setter in your monad -- state -- --
-- (=:) :: MonadState s m => a -> Simple Setter s [a] -> m () -- (=:) :: MonadState s m => a -> Simple Traversal s [a] -> m () -- (=:) :: MonadState s m => a -> Simple Lens s [a] -> m () -- (=:) :: MonadState s m => a -> Simple Iso s [a] -> m () --(=:) :: MonadState s m => a -> SimpleSetting s [a] -> m () -- | Cons onto the list(s) referenced by a Lens (or -- Traversal), returning the result. -- -- If you use this with a Traversal you will receive back the -- concatenation of all of the resulting lists instead of an individual -- result. -- --
-- >>> 'h' <~: _1 $ ("ello","world")
-- ("hello",("hello","world"))
--
--
-- -- (<~:) :: b -> Simple Lens s [a] -> s -> ([a], s) -- (<~:) :: b -> Simple Iso s [a] -> s -> ([a], s) -- (<~:) :: b -> Simple Traversal s [a] -> s -> ([a], s) --(<~:) :: a -> LensLike ((,) [a]) s t [a] [a] -> s -> ([a], t) -- | Cons onto the list(s) referenced by a Lens (or -- Traversal) into your monad state, returning the result. -- -- If you use this with a Traversal, you will receive back the -- concatenation of all of the resulting lists instead of an individual -- result. -- --
-- (<=:) :: MonadState s m => Simple Lens s [a] -> a -> m [a] -- (<=:) :: MonadState s m => Simple Iso s [a] -> a -> m [a] -- (<=:) :: MonadState s m => Simple Traversal s [a] -> a -> m [a] --(<=:) :: MonadState s m => a -> SimpleLensLike ((,) [a]) s [a] -> m [a] -- | Append to the target of a list-valued setter by appending to it with -- (++). -- -- (<>~) generalizes this operation to an arbitrary -- Monoid. -- --
-- >>> :m + Control.Lens
--
-- >>> both ++~ "!!!" $ ("hello","world")
-- ("hello!!!","world!!!")
--
--
-- -- (++~) :: Simple Setter s [a] -> [a] -> s -> s -- (++~) :: Simple Iso s [a] -> [a] -> s -> s -- (++~) :: Simple Lens s [a] -> [a] -> s -> s -- (++~) :: Simple Traversal s [a] -> [a] -> s -> s --(++~) :: Setting s t [a] [a] -> [a] -> s -> t -- | Append onto the end of the list targeted by a Lens and return -- the result. -- -- (<<>~) generalizes this operation to an arbitrary -- Monoid. -- -- When using a Traversal, the result returned is actually the -- concatenation of all of the results. -- -- When you do not need the result of the operation, (++~) is more -- flexible. (<++~) :: LensLike ((,) [a]) s t [a] [a] -> [a] -> s -> ([a], t) -- | Append to the target(s) of a Simple Lens, Iso, -- Setter or Traversal with (++) in the current -- state. -- -- (<>=) generalizes this operation to an arbitrary -- Monoid. -- --
-- (++=) :: MonadState s m => Simple Setter s [a] -> [a] -> m () -- (++=) :: MonadState s m => Simple Iso s [a] -> [a] -> m () -- (++=) :: MonadState s m => Simple Lens s [a] -> [a] -> m () -- (++=) :: MonadState s m => Simple Traversal s [a] -> [a] -> m () --(++=) :: MonadState s m => SimpleSetting s [a] -> [a] -> m () -- | Append onto the end of the list targeted by a Lens into the -- current monadic state, and return the result. -- -- (<<>=) generalizes this operation to an arbitrary -- Monoid. -- -- When using a Traversal, the result returned is actually the -- concatenation of all of the results. -- -- When you do not need the result of the operation, (++=) is more -- flexible. (<++=) :: MonadState s m => SimpleLensLike ((,) [a]) s [a] -> [a] -> m [a] module Data.ByteString.Strict.Lens -- | pack (or unpack) a list of bytes into a -- ByteString -- --
-- pack x = x ^. packedBytes ---- --
-- unpack x = x ^. from packedBytes --packedBytes :: Simple Iso [Word8] ByteString -- | Traverse each Word8 in a ByteString -- --
-- bytes = from packedBytes .> traverseList ---- --
-- anyOf bytes (== 0x80) :: ByteString -> Bool --bytes :: SimpleIndexedTraversal Int ByteString Word8 -- | pack (or unpack) a list of characters into a -- ByteString -- -- When writing back to the ByteString it is assumed that every -- Char lies between '\x00' and '\xff'. -- --
-- pack x = x ^. packedChars ---- --
-- unpack x = x ^. from packedChars --packedChars :: Simple Iso String ByteString -- | Traverse the individual bytes in a ByteString as characters. -- -- When writing back to the ByteString it is assumed that every -- Char lies between '\x00' and '\xff'. -- --
-- chars = from packedChars . traverse ---- --
-- anyOf chars (== 'c') :: ByteString -> Bool --chars :: SimpleIndexedTraversal Int ByteString Char -- | Lenses for lazy bytestrings module Data.ByteString.Lazy.Lens -- | pack (or unpack) a list of bytes into a -- ByteString -- --
-- pack x = x ^. packedBytes ---- --
-- unpack x = x ^. from packedBytes --packedBytes :: Simple Iso [Word8] ByteString -- | Traverse the individual bytes in a ByteString -- --
-- bytes = from packedBytes . traverseList ---- --
-- anyOf bytes (== 0x80) :: ByteString -> Bool --bytes :: SimpleIndexedTraversal Int ByteString Word8 -- | pack (or unpack) a list of characters into a -- ByteString -- -- When writing back to the ByteString it is assumed that every -- Char lies between '\x00' and '\xff'. -- --
-- pack x = x ^. packedChars ---- --
-- unpack x = x ^. from packedChars --packedChars :: Simple Iso String ByteString -- | Traverse the individual bytes in a ByteString as characters. -- -- When writing back to the ByteString it is assumed that every -- Char lies between '\x00' and '\xff'. -- --
-- chars = from packedChars .> traverseList ---- --
-- anyOf chars (== 'c') :: ByteString -> Bool --chars :: SimpleIndexedTraversal Int ByteString Char module Data.ByteString.Lens -- | Traversals for ByteStrings. class IsByteString t where bytes = from packedBytes .> itraversed chars = from packedChars .> itraversed packedBytes :: IsByteString t => Simple Iso [Word8] t packedChars :: IsByteString t => Simple Iso String t bytes :: IsByteString t => SimpleIndexedTraversal Int t Word8 chars :: IsByteString t => SimpleIndexedTraversal Int t Char instance IsByteString ByteString instance IsByteString ByteString module Data.Complex.Lens -- | Access the realPart of a Complex number -- --
-- >>> (1.0 :+ 0.0)^.real -- 1.0 ---- --
-- real :: Functor f => (a -> f a) -> Complex a -> f (Complex a) --real :: Simple Lens (Complex a) a -- | Access the imaginaryPart of a Complex number -- --
-- >>> (0.0 :+ 1.0)^.imaginary -- 1.0 ---- --
-- imaginary :: Functor f => (a -> f a) -> Complex a -> f (Complex a) --imaginary :: Simple Lens (Complex a) a -- | This isn't quite a legal lens. Notably the -- --
-- view l (set l b a) = b ---- -- law is violated when you set a polar value with 0 -- magnitude and non-zero phase as the phase -- information is lost. So don't do that! -- -- Otherwise, this is a perfectly cromulent Lens. polarize :: (RealFloat a, RealFloat b) => Iso (Complex a) (Complex b) (a, a) (b, b) -- | Traverse both the real and imaginary parts of a Complex number. -- --
-- traverseComplex :: Applicative f => (a -> f b) -> Complex a -> f (Complex b) --traverseComplex :: Traversal (Complex a) (Complex b) a b -- | Smart and naïve generic traversals given Data instances. -- -- template, uniplate, and biplate each build up -- information about what types can be contained within another type to -- speed up Traversal. module Data.Data.Lens -- | Find every occurence of a given type a recursively that -- doesn't require passing through something of type a using -- Data, while avoiding traversal of areas that cannot contain a -- value of type a. -- -- This is uniplate with a more liberal signature. template :: (Data s, Typeable a) => Simple Traversal s a -- | Naïve Traversal using Data. This does not attempt to -- optimize the traversal. -- -- This is primarily useful when the children are immediately obvious, -- and for benchmarking. tinplate :: (Data s, Typeable a) => Simple Traversal s a -- | Find descendants of type a non-transitively, while avoiding -- computation of areas that cannot contain values of type a -- using Data. -- -- uniplate is a useful default definition for plate uniplate :: Data a => Simple Traversal a a -- | biplate performs like template, except when s ~ -- a, it returns itself and nothing else. biplate :: (Data s, Typeable a) => Simple Traversal s a -- | A generic applicative transformation that maps over the immediate -- subterms. -- -- gtraverse is to traverse what gmapM is to -- mapM -- -- This really belongs in Data.Data. gtraverse :: (Applicative f, Data a) => (forall d. Data d => d -> f d) -> a -> f a instance Eq a => Eq (Answer a) instance Ord a => Ord (Answer a) instance Show a => Show (Answer a) instance Read a => Read (Answer a) instance Functor Oracle instance Functor Answer module Data.Dynamic.Lens -- | Traverse the typed value contained in a Dynamic where the type -- required by your function matches that of the contents of the -- Dynamic. -- --
-- >>> ()^.by dynamic -- <<()>> --dynamic :: (Typeable a, Typeable b) => Projection Dynamic Dynamic a b module Data.IntSet.Lens -- | IntSet isn't Foldable, but this Fold can be used to access the -- members of an IntSet. -- --
-- >>> sumOf members $ setOf folded [1,2,3,4] -- 10 --members :: Fold IntSet Int -- | This Setter can be used to change the contents of an -- IntSet by mapping the elements to new values. -- -- Sadly, you can't create a valid Traversal for a Set, -- because the number of elements might change but you can manipulate it -- by reading using folded and reindexing it via setmapped. -- --
-- >>> over setmapped (+1) (fromList [1,2,3,4]) -- fromList [2,3,4,5] --setmapped :: Simple Setter IntSet Int -- | Construct an IntSet from a Getter, Fold, -- Traversal, Lens or Iso. -- --
-- >>> setOf (folded._2) [("hello",1),("world",2),("!!!",3)]
-- fromList [1,2,3]
--
--
-- -- setOf :: Getter s Int -> s -> IntSet -- setOf :: Fold s Int -> s -> IntSet -- setOf :: Simple Iso s Int -> s -> IntSet -- setOf :: Simple Lens s Int -> s -> IntSet -- setOf :: Simple Traversal s Int -> s -> IntSet --setOf :: Getting IntSet s t Int b -> s -> IntSet module Data.Monoid.Lens -- | Modify the target of a monoidally valued by mappending another -- value. -- --
-- >>> :m + Control.Lens
--
-- >>> both <>~ "!!!" $ ("hello","world")
-- ("hello!!!","world!!!")
--
--
-- -- (<>~) :: Monoid a => Setter s t a a -> a -> s -> t -- (<>~) :: Monoid a => Iso s t a a -> a -> s -> t -- (<>~) :: Monoid a => Lens s t a a -> a -> s -> t -- (<>~) :: Monoid a => Traversal s t a a -> a -> s -> t --(<>~) :: Monoid a => Setting s t a a -> a -> s -> t -- | mappend a monoidal value onto the end of the target of a -- Lens and return the result -- -- When you do not need the result of the operation, (<>~) -- is more flexible. (<<>~) :: Monoid m => LensLike ((,) m) s t m m -> m -> s -> (m, t) -- | Modify the target(s) of a Simple Lens, Iso, -- Setter or Traversal by mappending a value. -- --
-- (<>=) :: (MonadState s m, Monoid a) => Simple Setter s a -> a -> m () -- (<>=) :: (MonadState s m, Monoid a) => Simple Iso s a -> a -> m () -- (<>=) :: (MonadState s m, Monoid a) => Simple Lens s a -> a -> m () -- (<>=) :: (MonadState s m, Monoid a) => Simple Traversal s a -> a -> m () --(<>=) :: (MonadState s m, Monoid a) => SimpleSetting s a -> a -> m () -- | mappend a monoidal value onto the end of the target of a -- Lens into your monad's state and return the result. -- -- When you do not need the result of the operation, (<>=) -- is more flexible. (<<>=) :: (MonadState s m, Monoid r) => SimpleLensLike ((,) r) s r -> r -> m r -- | Isomorphism for Dual _dual :: Iso a b (Dual a) (Dual b) -- | Isomorphism for Endo _endo :: Iso (a -> a) (b -> b) (Endo a) (Endo b) -- | Isomorphism for All -- --
-- >>> :m + Control.Lens Data.Monoid.Lens Data.Foldable -- -- >>> ala _all foldMap [True,True] -- True ---- --
-- >>> :m + Control.Lens Data.Monoid.Lens Data.Foldable -- -- >>> ala _all foldMap [True,False] -- False --_all :: Simple Iso Bool All -- | Isomorphism for Any -- --
-- >>> :m + Control.Lens Data.Monoid.Lens Data.Foldable -- -- >>> ala _any foldMap [False,False] -- False ---- --
-- >>> :m + Control.Lens Data.Monoid.Lens Data.Foldable -- -- >>> ala _any foldMap [True,False] -- True --_any :: Simple Iso Bool Any -- | Isomorphism for Sum -- --
-- >>> :m + Control.Lens Data.Monoid.Lens Data.Foldable -- -- >>> ala _sum foldMap [1,2,3,4] -- 10 --_sum :: Iso a b (Sum a) (Sum b) -- | Isomorphism for Product -- --
-- >>> :m + Control.Lens Data.Monoid.Lens Data.Foldable -- -- >>> ala _product foldMap [1,2,3,4] -- 24 --_product :: Iso a b (Product a) (Product b) -- | Isomorphism for First _first :: Iso (Maybe a) (Maybe b) (First a) (First b) -- | Isomorphism for Last _last :: Iso (Maybe a) (Maybe b) (Last a) (Last b) module Data.Sequence.Lens -- | A Lens that can access the nth element of a -- Seq. -- -- Note: This is only a legal lens if there is already such an element! ordinal :: Int -> SimpleIndexedLens Int (Seq a) a -- | A Seq is isomorphic to a ViewL -- --
-- viewl m = m ^. viewL --viewL :: Iso (Seq a) (Seq b) (ViewL a) (ViewL b) -- | A Seq is isomorphic to a ViewR -- --
-- viewr m = m ^. viewR --viewR :: Iso (Seq a) (Seq b) (ViewR a) (ViewR b) -- | Traverse the head of a Seq traverseHead :: SimpleIndexedTraversal Int (Seq a) a -- | Traverse the tail of a Seq traverseTail :: SimpleIndexedTraversal Int (Seq a) a -- | Traverse the last element of a Seq traverseLast :: SimpleIndexedTraversal Int (Seq a) a -- | Traverse all but the last element of a Seq traverseInit :: SimpleIndexedTraversal Int (Seq a) a -- | Traverse the first n elements of a Seq traverseTo :: Int -> SimpleIndexedTraversal Int (Seq a) a -- | Traverse all but the first n elements of a Seq traverseFrom :: Int -> SimpleIndexedTraversal Int (Seq a) a -- | Travere all the elements numbered from i to j of a -- Seq traverseSlice :: Int -> Int -> SimpleIndexedTraversal Int (Seq a) a module Data.Text.Strict.Lens -- | pack (or unpack) strict Text. -- --
-- pack x = x ^. packed -- unpack x = x ^. from packed --packed :: Simple Iso String Text -- | Traverse the individual characters in strict Text. -- --
-- >>> anyOf text (=='o') $ "hello"^.packed -- True --text :: SimpleIndexedTraversal Int Text Char module Data.Text.Lazy.Lens -- | Pack (or unpack) lazy Text. -- --
-- pack x = x ^. packed -- unpack x = x ^. from packed --packed :: Simple Iso String Text -- | Traverse the individual characters in a Text. -- --
-- anyOf text (=='c') :: Text -> Bool --text :: SimpleIndexedTraversal Int Text Char module Data.Text.Lens -- | Traversals for strict or lazy Text class IsText t where text = from packed .> itraversed packed :: IsText t => Simple Iso String t text :: IsText t => SimpleIndexedTraversal Int t Char instance IsText Text instance IsText Text module Data.Tree.Lens -- | A Lens that focuses on the root of a Tree. -- --
-- >>> view root $ Node 42 [] -- 42 --root :: Simple Lens (Tree a) a -- | A Traversal of the direct descendants of the root of a -- Tree indexed by its position in the list of children -- --
-- toListOf branches ≡ subForest --branches :: SimpleIndexedTraversal Int (Tree a) (Tree a) module Data.Typeable.Lens -- | A Simple Traversal for working with a cast of a -- Typeable value. _cast :: (Typeable s, Typeable a) => Simple Traversal s a -- | A Simple Traversal for working with a gcast of a -- Typeable value. _gcast :: (Typeable s, Typeable a) => Simple Traversal (c s) (c a) module Data.Array.Lens -- | Access an element of an array. -- -- Note: The indexed element is assumed to exist in the target -- IArray. -- --
-- arr ! i ≡ arr ^. ix i -- arr // [(i,e)] ≡ ix i .~ e $ arr ---- --
-- >>> ix 2 .~ 9 $ (listArray (1,5) [4,5,6,7,8] :: Array Int Int) -- array (1,5) [(1,4),(2,9),(3,6),(4,7),(5,8)] --ix :: (IArray a e, Ix i) => i -> Simple Lens (a i e) e -- | This setter can be used to derive a new IArray from an old -- array by applying a function to each of the indices to look it up in -- the old IArray. -- -- This is a contravariant Setter. -- --
-- ixmap ≡ over . ixmapped -- ixmapped ≡ sets . ixmap -- over (ixmapped b) f arr ! i ≡ arr ! f i -- bounds (over (ixmapped b) f arr) ≡ b --ixmapped :: (IArray a e, Ix i, Ix j) => (i, i) -> Setter (a j e) (a i e) i j -- | An IndexedTraversal of the elements of an IArray, using -- the index into the array as the index of the traversal. -- --
-- amap ≡ over traverseArray --traverseArray :: (IArray arr a, IArray arr b, Ix i) => IndexedTraversal i (arr i a) (arr i b) a b module System.FilePath.Lens -- | Modify the path by adding another path. -- --
-- >>> :m + Control.Lens
--
-- >>> both </>~ "!!!" $ ("hello","world")
-- ("hello/!!!","world/!!!")
--
--
-- -- (</>~) :: Setter s a FilePath FilePath -> FilePath -> s -> a -- (</>~) :: Iso s a FilePath FilePath -> FilePath -> s -> a -- (</>~) :: Lens s a FilePath FilePath -> FilePath -> s -> a -- (</>~) :: Traversal s a FilePath FilePath -> FilePath -> s -> a --(>~) :: Setting s t FilePath FilePath -> FilePath -> s -> t -- | Add a path onto the end of the target of a Lens and return the -- result -- -- When you do not need the result of the operation, (</>~) -- is more flexible. (<>~) :: LensLike ((,) FilePath) s a FilePath FilePath -> FilePath -> s -> (FilePath, a) -- | Modify the path by adding extension. -- --
-- >>> :m + Control.Lens
--
-- >>> both <.>~ "!!!" $ ("hello","world")
-- ("hello.!!!","world.!!!")
--
--
-- -- (<.>~) :: Setter s a FilePath FilePath -> String -> s -> a -- (<.>~) :: Iso s a FilePath FilePath -> String -> s -> a -- (<.>~) :: Lens s a FilePath FilePath -> String -> s -> a -- (<.>~) :: Traversal s a FilePath FilePath -> String -> s -> a --(<.>~) :: Setting s a FilePath FilePath -> String -> s -> a -- | Add an extension onto the end of the target of a Lens and -- return the result -- -- When you do not need the result of the operation, (<.>~) -- is more flexible. (<<.>~) :: LensLike ((,) FilePath) s a FilePath FilePath -> String -> s -> (FilePath, a) -- | Modify the target(s) of a Simple Lens, Iso, -- Setter or Traversal by adding a path. -- --
-- (</>=) :: MonadState s m => Simple Setter s FilePath -> FilePath -> m () -- (</>=) :: MonadState s m => Simple Iso s FilePath -> FilePath -> m () -- (</>=) :: MonadState s m => Simple Lens s FilePath -> FilePath -> m () -- (</>=) :: MonadState s m => Simple Traversal s FilePath -> FilePath -> m () --(>=) :: MonadState s m => SimpleSetting s FilePath -> FilePath -> m () -- | Add a path onto the end of the target of a Lens into your -- monad's state and return the result. -- -- When you do not need the result of the operation, (</>=) -- is more flexible. (<>=) :: MonadState s m => SimpleLensLike ((,) FilePath) s FilePath -> FilePath -> m FilePath -- | Modify the target(s) of a Simple Lens, Iso, -- Setter or Traversal by adding an extension. -- --
-- (<.>=) :: MonadState s m => Simple Setter s FilePath -> String -> m () -- (<.>=) :: MonadState s m => Simple Iso s FilePath -> String -> m () -- (<.>=) :: MonadState s m => Simple Lens s FilePath -> String -> m () -- (<.>=) :: MonadState s m => Simple Traversal s FilePath -> String -> m () --(<.>=) :: MonadState s m => SimpleSetting s FilePath -> String -> m () -- | Add an extension onto the end of the target of a Lens into your -- monad's state and return the result. -- -- When you do not need the result of the operation, (<.>=) -- is more flexible. (<<.>=) :: MonadState s m => SimpleLensLike ((,) FilePath) s FilePath -> String -> m FilePath -- | A lens reading and writing to the basename. -- --
-- >>> basename .~ "filename" $ "path/name.png" -- "path/filename.png" --basename :: Simple Lens FilePath FilePath -- | A lens reading and writing to the directory. -- --
-- >>> "long/path/name.txt" ^. directory -- "long/path" --directory :: Simple Lens FilePath FilePath -- | A lens reading and writing to the extension. -- --
-- >>> extension .~ ".png" $ "path/name.txt" -- "path/name.png" --extension :: Simple Lens FilePath FilePath -- | A lens reading and writing to the full filename. -- --
-- >>> filename .~ "name.txt" $ "path/name.png" -- "path/name.txt" --filename :: Simple Lens FilePath FilePath -- | A Lens or Traversal can be used to take the role of -- Traversable in Control.Parallel.Strategies, enabling -- those combinators to work with monomorphic containers. module Control.Parallel.Strategies.Lens -- | Evaluate the targets of a Lens or Traversal into a data -- structure according to the given Strategy. -- --
-- evalTraversable = evalOf traverse = traverse -- evalOf = id ---- --
-- evalOf :: Simple Lens s a -> Strategy a -> Strategy s -- evalOf :: Simple Traversal s a -> Strategy a -> Strategy s -- evalOf :: (a -> Eval a) -> s -> Eval s) -> Strategy a -> Strategy s --evalOf :: SimpleLensLike Eval s a -> Strategy a -> Strategy s -- | Evaluate the targets of a Lens or Traversal according -- into a data structure according to a given Strategy in -- parallel. -- --
-- parTraversable = parOf traverse ---- --
-- parOf :: Simple Lens s a -> Strategy a -> Strategy s -- parOf :: Simple Traversal s a -> Strategy a -> Strategy s -- parOf :: ((a -> Eval a) -> s -> Eval s) -> Strategy a -> Strategy s --parOf :: SimpleLensLike Eval s a -> Strategy a -> Strategy s -- | Transform a Lens, Fold, Getter, Setter or -- Traversal to first evaluates its argument according to a given -- Strategy before proceeding. -- --
-- after rdeepseq traverse :: Traversable t => Strategy a -> Strategy [a] --after :: Strategy s -> LensLike f s t a b -> LensLike f s t a b -- | Transform a Lens, Fold, Getter, Setter or -- Traversal to evaluate its argument according to a given -- Strategy in parallel with evaluating. -- --
-- throughout rdeepseq traverse :: Traversable t => Strategy a -> Strategy [a] --throughout :: Strategy s -> LensLike f s t a b -> LensLike f s t a b -- | A Fold can be used to take the role of Foldable in -- Control.Seq module Control.Seq.Lens -- | Evaluate the elements targeted by a Lens, Traversal, -- Iso, Getter or Fold according to the given -- strategy. -- --
-- seqFoldable = seqOf folded --seqOf :: Getting [a] s t a b -> Strategy a -> Strategy s