HORIZON HASKELLDocslts/ghc-10.0.x77a86622026-10-31Search packagesCtrl K
Theme: |

GHC 10.0.0.20260917 · lts/ghc-10.0.x · 77a8662 · 2026-10-31

Moduleoptics-core-0.4.2Haskell2010

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.

Contents
  1. Formation
  2. Introduction
  3. Elimination
  4. Computation
  5. Well-formedness
  6. Additional introduction forms
  7. Additional elimination forms
  8. Combinators
  9. Monoid structures
  10. Subtyping
  11. van Laarhoven encoding
  12. Re-exports
Synopsis
  • Packageoptics-core-0.4.2
  • Exports27
  • LanguageHaskell2010
  • LicenceBSD-3-Clause
  • SourceIxTraversal.hs

Formation

Introduction

Elimination

Computation

Well-formedness

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

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

Additional elimination forms

Combinators

Monoid structures

IxTraversal admits two monoid structures:

  • iadjoin combines non-overlapping indexed traversals.

  • idisjoin returns results from the second indexed traversal only if the first returns no results.

In both cases, 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

datadata A_Traversal

Tag for a traversal.

Instances28Is, JoinKinds, IxOptic, ToReadOnly, ReadOnlyOptic, …

van Laarhoven encoding

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.

Re-exports

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, …