HORIZON HASKELLDocslts/ghc-9.10.x248f8f02026-10-05Search names, modules, packages, or :: a typeCtrl K

GHC 9.10.3 · lts/ghc-9.10.x · 248f8f0 · 2026-10-05

Moduleoptics-core-0.4.1.1Haskell2010

Optics.IxTraversal

An IxTraversal is an indexed version of a Traversal. See the "Indexed optics" section of the overview documentation in the Optics module of the main optics package for more details on indexed optics.

  • 5 types
  • 1 class
  • 20 values

Formation

2 declarations

Introduction

1 declaration

Elimination

1 declaration
valueitraverseOf
  1. :: (Is k A_Traversal, Applicative f, HasSingleIndex is i)
  2. => Optic k is s t a b
  3. -> i -> a -> f b
  4. -> s
  5. -> f t
#

Map each element of a structure targeted by an IxTraversal (supplying the index), evaluate these actions from left to right, and collect the results.

This yields the van Laarhoven representation of an indexed traversal.

Computation

0 declarations

Well-formedness

0 declarations
itraverseOf o (const pure) ≡ pure
fmap (itraverseOf o f) . itraverseOf o g ≡ getCompose . itraverseOf o (\ i -> Compose . fmap (f i) . g i)

Additional introduction forms

6 declarations

See also each, which is an IxTraversal over each element of a (potentially monomorphic) container.

Additional elimination forms

7 declarations

Combinators

4 declarations

Monoid structure

1 declaration

IxTraversal admits a (partial) monoid structure where iadjoin combines non-overlapping indexed traversals, and the identity element is ignored (which traverses no elements).

If you merely need an IxFold, you can use indexed traversals as indexed folds and combine them with one of the monoid structures on indexed folds (see Optics.IxFold#monoids). In particular, isumming can be used to concatenate results from two traversals, and ifailing will returns results from the second traversal only if the first returns no results.

There is no Semigroup or Monoid instance for IxTraversal, 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.

valueiadjoin
  1. :: (Is k A_Traversal, Is l A_Traversal, HasSingleIndex is i)
  2. => Optic' k is s a
  3. -> Optic' l is s a
  4. -> IxTraversal' i s a
#

Combine two disjoint indexed traversals into one.

Example1 expression
iover (_1 % itraversed `iadjoin` _2 % itraversed) (+) ([0, 0, 0], (3, 5))([0,1,2],(3,8))

Note: if the argument traversals are not disjoint, the result will not respect the IxTraversal 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.

Example2 expressions
iview (ipartsOf (each `iadjoin` each)) ("x","y")([0,1,0,1],["x","y","x","y"])iset (ipartsOf (each `iadjoin` each)) (const ["a","b","c","d"]) ("x","y")("c","d")

For the IxFold version see isumming.

Subtyping

1 declaration
datadata A_Traversal
#

Tag for a traversal.

Instances28Is, JoinKinds, IxOptic, ToReadOnly, ReadOnlyOptic, …

van Laarhoven encoding

2 declarations

The van Laarhoven representation of an IxTraversal directly expresses how it lifts an effectful operation I -> A -> F B on elements and their indices to act on structures S -> F T. Thus itraverseOf converts an IxTraversal to an IxTraversalVL.

typetype IxTraversalVL i s t a b = forall (f :: Type -> Type). Applicative f => (i -> a -> f b) -> s -> f t
#

Type synonym for a type-modifying van Laarhoven indexed traversal.

Re-exports

1 declaration
classclass (FunctorWithIndex i t, FoldableWithIndex i t, Traversable t) => TraversableWithIndex i (t :: Type -> Type) | t -> i where
#

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)

Methods

Instances28TraversableWithIndex, …