HORIZON HASKELLDocslts/ghc-9.10.xc74966e2026-09-27Search names, modules, packages, or :: a typeCtrl K

GHC 9.10.3 · lts/ghc-9.10.x · c74966e · 2026-09-27

Moduleoptics-core-0.4.1.1Haskell2010

Optics.AffineFold

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

  • 2 types
  • 8 values

Formation

1 declaration

Introduction

1 declaration

Elimination

2 declarations
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

0 declarations
preview (afolding f) ≡ f

Additional introduction forms

2 declarations

Additional elimination forms

2 declarations

Monoid structure

1 declaration

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

1 declaration
datadata An_AffineFold
#

Tag for an affine fold.

Instances27Is, JoinKinds, IxOptic, ToReadOnly, ReadOnlyOptic, …