HORIZON HASKELLDocslts/ghc-10.0.x8fef28d2026-11-02Search packagesCtrl K
Theme: |

GHC 10.0.0.20260917 · lts/ghc-10.0.x · 8fef28d · 2026-11-02

Moduleoptics-core-0.4.2Haskell2010

Optics.Prism

A Prism generalises the notion of a constructor (just as a Lens generalises the notion of a field).

Contents
  1. Formation
  2. Introduction
  3. Elimination
  4. Computation
  5. Well-formedness
  6. Additional introduction forms
  7. Additional elimination forms
  8. Combinators
  9. Subtyping
Synopsis
  • Packageoptics-core-0.4.2
  • Exports11
  • LanguageHaskell2010
  • LicenceBSD-3-Clause
  • SourcePrism.hs

Formation

Introduction

valueprism :: (b -> t) -> (s -> Either t a) -> Prism s t a b

Build a prism from a constructor and a matcher, which must respect the well-formedness laws.

If you want to build a Prism from the van Laarhoven representation, use prismVL from the optics-vl package.

Elimination

A Prism is in particular an AffineFold, an AffineTraversal, a Review and a Setter, therefore you can specialise types to obtain:

preview  :: Prism' s a -> s -> Maybe a
review   :: Prism' s a -> a -> s
over     :: Prism s t a b -> (a -> b) -> s -> t
set      :: Prism s t a b ->       b  -> s -> t
matching :: Prism s t a b             -> s -> Either t a

If you want to preview a type-modifying Prism that is insufficiently polymorphic to be used as a type-preserving Prism', use getting:

preview . getting :: Prism s t a b -> s -> Maybe a

Computation

review   (prism f g) ≡ f
matching (prism f g) ≡ g

Well-formedness

matching o (review o b) ≡ Right b
matching o s ≡ Right a  =>  review o a ≡ s

Additional introduction forms

See Data.Maybe.Optics and Data.Either.Optics for Prisms for the corresponding types, and _Cons, _Snoc and _Empty for Prisms for container types.

valueonly :: Eq a => a -> Prism' a ()

This Prism compares for exact equality with a given value.

Example1 expression
only 4 # ()4
Example1 expression
5 ^? only 4Nothing
valuenearly :: a -> (a -> Bool) -> Prism' a ()

This Prism compares for approximate equality with a given value and a predicate for testing, an example where the value is the empty list and the predicate checks that a list is empty (same as Optics.Empty._Empty with the Optics.Empty.AsEmpty list instance):

Example2 expressions
nearly [] null # ()[][1,2,3,4] ^? nearly [] nullNothing
nearly [] null :: Prism' [a] ()

To comply with the Prism laws the arguments you supply to nearly a p are somewhat constrained.

We assume p x holds iff x ≡ a. Under that assumption then this is a valid Prism.

This is useful when working with a type where you can test equality for only a subset of its values, and the prism selects such a value.

Additional elimination forms

Combinators

Subtyping

datadata A_Prism

Tag for a prism.

Instances38ReversibleOptic, Is, ArrowOptic, JoinKinds, ToReadOnly, MappingOptic, …