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

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

  • 3 types
  • 8 values

Formation

2 declarations

Introduction

1 declaration
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

0 declarations

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

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

Well-formedness

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

Additional introduction forms

3 declarations

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

valueprism' :: (b -> s) -> (s -> Maybe a) -> Prism s s a b
#

This is usually used to build a Prism', when you have to use an operation like cast which already returns a Maybe.

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

1 declaration

Combinators

3 declarations

Subtyping

1 declaration
datadata A_Prism
#

Tag for a prism.

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