Type synonym for a type-modifying traversal.
Moduleoptics-core-0.4.2Haskell2010
Optics.Traversal
A Traversal lifts an effectful operation on elements to act on structures containing those elements.
That is, given a function op :: A -> F B where F is Applicative, a
Traversal S T A B can produce a function S -> F T that applies op to
all the As contained in the S.
This can be seen as a generalisation of traverse, where the type S does
not need to be a type constructor with A as the last parameter.
A Lens is a Traversal that acts on a single value.
A close relative is the AffineTraversal, which is a Traversal that acts on at most one value.
Contents
Synopsis
- type Traversal
- type Traversal'
- value traversalVL
- value traverseOf
- value traversed
- value both
- value forOf
- value sequenceOf
- value transposeOf
- value mapAccumROf
- value mapAccumLOf
- value scanr1Of
- value scanl1Of
- value rewriteMOf
- value transformMOf
- value failover
- value failover'
- value backwards
- value partsOf
- value unsafePartsOf
- value singular
- value adjoin
- value disjoin
- data A_Traversal
- type TraversalVL
- type TraversalVL'
- Packageoptics-core-0.4.2
- Exports26
- LanguageHaskell2010
- LicenceBSD-3-Clause
- SourceTraversal.hs
Formation
Type synonym for a type-preserving traversal.
Introduction
Build a traversal from the van Laarhoven representation.
traversalVL . traverseOf ≡ id
traverseOf . traversalVL ≡ id
Elimination
Map each element of a structure targeted by a Traversal, evaluate these actions from left to right, and collect the results.
Computation
traverseOf (traversalVL f) ≡ f
Well-formedness
traverseOf o pure ≡ pure
fmap (traverseOf o f) . traverseOf o g ≡ getCompose . traverseOf o (Compose . fmap f . g)
Additional introduction forms
Construct a Traversal via the Traversable class.
traverseOf traversed = traverse
Traverse both parts of a Bitraversable container with matching types.
Note: for traversing a pair or an Either it's better to use each and chosen respectively to reduce potential for bugs due to too much polymorphism.
(1,2) & both %~ (*10)(10,20)
over both length ("hello","world")(5,5)
foldOf both ("hello","world")"helloworld"
Additional elimination forms
A version of traverseOf with the arguments flipped.
Evaluate each action in the structure from left to right, and collect the results.
sequenceOf each ([1,2],[3,4])[(1,3),(1,4),(2,3),(2,4)]
sequence ≡ sequenceOf traversed ≡ traverse id
sequenceOf o ≡ traverseOf o id
This generalizes Data.List.transpose to an arbitrary Traversal.
Note: Data.List.transpose handles ragged inputs more intelligently, but for
non-ragged inputs:
transposeOf traversed [[1,2,3],[4,5,6]][[1,4],[2,5],[3,6]]
Data.List.transpose ≡ transposeOf traverse
This generalizes mapAccumR to an arbitrary Traversal.
mapAccumR ≡ mapAccumROf traversed
mapAccumROf accumulates State from right to left.
This generalizes mapAccumL to an arbitrary Traversal.
mapAccumL ≡ mapAccumLOf traverse
mapAccumLOf accumulates State from left to right.
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.
Transform every element in a tree using a user supplied Traversal in a bottom-up manner with a monadic effect.
Try to map a function over this Traversal, returning Nothing if the traversal has no targets.
failover (element 3) (*2) [1,2]Nothing
failover _Left (*2) (Right 4)Nothing
failover _Right (*2) (Right 4)Just (Right 8)
Version of failover strict in the application of f.
Combinators
This allows you to traverse the elements of a traversal in the opposite order.
partsOf turns a Traversal into a Lens.
Note: You should really try to maintain the invariant of the number of children in the list.
('a','b','c') & partsOf each .~ ['x','y','z']('x','y','z')
Any extras will be lost. If you do not supply enough, then the remainder will come from the original structure.
('a','b','c') & partsOf each .~ ['w','x','y','z']('w','x','y')
('a','b','c') & partsOf each .~ ['x','y']('x','y','c')
('b', 'a', 'd', 'c') & partsOf each %~ sort('a','b','c','d')
So technically, this is only a Lens if you do not change the number of results it returns.
A variant of partsOf that allows changing the type of elements.
Warning: if you don't supply at least as many b's as you were given a's,
the reconstruction of t will result in an error.
Convert a traversal to an AffineTraversal that visits the first element of the original traversal.
For the fold version see pre.
"foo" & singular traversed .~ 'z'"zoo"
Monoid structures
Traversal admits two monoid structures:
adjoin combines non-overlapping traversals.
disjoin returns results from the second traversal only if the first returns no results.
In both cases, the identity element is ignored (which traverses no elements).
If you merely need a Fold, you can use traversals as folds and combine
them with one of the monoid structures on folds (see
Optics.Fold#monoids). In particular, summing can be used to concatenate
results from two traversals, and failing will returns results from the
second traversal only if the first returns no results.
There is no Semigroup or Monoid instance for Traversal, because there is not a unique choice of monoid to use that works for all optics, and the (<>) operator could not be used to combine optics of different kinds.
Combine two disjoint traversals into one.
over (_1 % _Just `adjoin` _2 % _Right) not (Just True, Right False)(Just False,Right True)
Note: if the argument traversals are not disjoint, the result will not respect the Traversal laws, because it will visit the same element multiple times. See section 7 of Understanding Idiomatic Traversals Backwards and Forwards by Bird et al. for why this is illegal.
view (partsOf (each `adjoin` _1)) ('x','y')"xyx"set (partsOf (each `adjoin` _1)) "abc" ('x','y')('c','b')
Try the first Traversal. If it returns no entries, try the second one.
over (_1 `disjoin` _2) succ (0, 0)(1,0)over (ignored `disjoin` _2) succ (0, 0)(0,1)
Subtyping
Tag for a traversal.
Instances28Is, JoinKinds, IxOptic, ToReadOnly, ReadOnlyOptic, …
Is A_Lens A_TraversalDefined in optics-core-0.4.2 · Optics.Internal.Optic.SubtypingIs A_Prism A_TraversalDefined in optics-core-0.4.2 · Optics.Internal.Optic.SubtypingIs A_Traversal A_FoldDefined in optics-core-0.4.2 · Optics.Internal.Optic.SubtypingIs A_Traversal A_SetterDefined in optics-core-0.4.2 · Optics.Internal.Optic.SubtypingIs An_AffineTraversal A_TraversalDefined in optics-core-0.4.2 · Optics.Internal.Optic.SubtypingIs An_Iso A_TraversalDefined in optics-core-0.4.2 · Optics.Internal.Optic.Subtypingk ~ A_Fold => JoinKinds A_Fold A_Traversal kDefined in optics-core-0.4.2 · Optics.Internal.Optic.Subtypingk ~ A_Fold => JoinKinds A_Getter A_Traversal kDefined in optics-core-0.4.2 · Optics.Internal.Optic.Subtypingk ~ A_Fold => JoinKinds A_ReversedPrism A_Traversal kDefined in optics-core-0.4.2 · Optics.Internal.Optic.Subtypingk ~ A_Fold => JoinKinds A_Traversal A_Fold kDefined in optics-core-0.4.2 · Optics.Internal.Optic.Subtypingk ~ A_Fold => JoinKinds A_Traversal A_Getter kDefined in optics-core-0.4.2 · Optics.Internal.Optic.Subtypingk ~ A_Fold => JoinKinds A_Traversal A_ReversedPrism kDefined in optics-core-0.4.2 · Optics.Internal.Optic.Subtypingk ~ A_Fold => JoinKinds A_Traversal An_AffineFold kDefined in optics-core-0.4.2 · Optics.Internal.Optic.Subtypingk ~ A_Fold => JoinKinds An_AffineFold A_Traversal kDefined in optics-core-0.4.2 · Optics.Internal.Optic.Subtypingk ~ A_Setter => JoinKinds A_Setter A_Traversal kDefined in optics-core-0.4.2 · Optics.Internal.Optic.Subtypingk ~ A_Setter => JoinKinds A_Traversal A_Setter kDefined in optics-core-0.4.2 · Optics.Internal.Optic.Subtypingk ~ A_Traversal => JoinKinds A_Lens A_Traversal kDefined in optics-core-0.4.2 · Optics.Internal.Optic.Subtypingk ~ A_Traversal => JoinKinds A_Prism A_Traversal kDefined in optics-core-0.4.2 · Optics.Internal.Optic.Subtypingk ~ A_Traversal => JoinKinds A_Traversal A_Lens kDefined in optics-core-0.4.2 · Optics.Internal.Optic.Subtypingk ~ A_Traversal => JoinKinds A_Traversal A_Prism kDefined in optics-core-0.4.2 · Optics.Internal.Optic.Subtypingk ~ A_Traversal => JoinKinds A_Traversal A_Traversal kDefined in optics-core-0.4.2 · Optics.Internal.Optic.Subtypingk ~ A_Traversal => JoinKinds A_Traversal An_AffineTraversal kDefined in optics-core-0.4.2 · Optics.Internal.Optic.Subtypingk ~ A_Traversal => JoinKinds A_Traversal An_Iso kDefined in optics-core-0.4.2 · Optics.Internal.Optic.Subtypingk ~ A_Traversal => JoinKinds An_AffineTraversal A_Traversal kDefined in optics-core-0.4.2 · Optics.Internal.Optic.Subtypingk ~ A_Traversal => JoinKinds An_Iso A_Traversal kDefined in optics-core-0.4.2 · Optics.Internal.Optic.SubtypingIxOptic A_Traversal s t a bDefined in optics-core-0.4.2 · Optics.Indexed.CoreToReadOnly A_Traversal s t a bDefined in optics-core-0.4.2 · Optics.ReadOnlytype ReadOnlyOptic A_Traversal = A_FoldDefined in optics-core-0.4.2 · Optics.ReadOnly
van Laarhoven encoding
The van Laarhoven representation of a Traversal directly expresses how
it lifts an effectful operation A -> F B on elements to act on structures
S -> F T. Thus traverseOf converts a Traversal to a TraversalVL.
type TraversalVL s t a b = forall (f :: Type -> Type). Applicative f => (a -> f b) -> s -> f tType synonym for a type-modifying van Laarhoven traversal.
Type synonym for a type-preserving van Laarhoven traversal.