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

An AffineFold is a Fold that contains at most one element, or a Getter where the function may be partial.

Contents
  1. Formation
  2. Introduction
  3. Elimination
  4. Computation
  5. Additional introduction forms
  6. Additional elimination forms
  7. Monoid structure
  8. Subtyping
Synopsis
  • Packageoptics-core-0.4.2
  • Exports10
  • LanguageHaskell2010
  • LicenceBSD-3-Clause
  • SourceAffineFold.hs

Formation

Introduction

Elimination

valuepreview :: Is k An_AffineFold => Optic' k is s a -> s -> Maybe a

Retrieve the value targeted by an AffineFold.

Example1 expression
let _Right = prism Right $ either (Left . Left) Right
Example1 expression
preview _Right (Right 'x')Just 'x'
Example1 expression
preview _Right (Left 'y')Nothing

Computation

preview (afolding f) ≡ f

Additional introduction forms

Additional elimination forms

Monoid structure

AffineFold admits a monoid structure where afailing combines folds (returning a result from the second fold only if the first returns none) and the identity element is ignored (which returns no results).

Note: There is no summing equivalent that returns an AffineFold, because it would not need to return more than one result.

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

valueafailing
  1. :: (Is k An_AffineFold, Is l An_AffineFold)
  2. => Optic' k is s a
  3. -> Optic' l js s a
  4. -> AffineFold s a

Try the first AffineFold. If it returns no entry, try the second one.

Example1 expression
preview (ix 1 % re _Left `afailing` ix 2 % re _Right) [0,1,2,3]Just (Left 1)
Example1 expression
preview (ix 42 % re _Left `afailing` ix 2 % re _Right) [0,1,2,3]Just (Right 2)

Subtyping

datadata An_AffineFold

Tag for an affine fold.

Instances27Is, JoinKinds, IxOptic, ToReadOnly, ReadOnlyOptic, …