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

Modulelens-5.3.5Haskell2010

Control.Lens.Wrapped

The Wrapped class provides similar functionality as Control.Newtype, from the newtype package, but in a more convenient and efficient form.

There are a few functions from newtype that are not provided here, because they can be done with the Iso directly:

Control.Newtype.over Sum f ≡ _Unwrapping Sum %~ f
Control.Newtype.under Sum f ≡ _Wrapping Sum %~ f
Control.Newtype.overF Sum f ≡ mapping (_Unwrapping Sum) %~ f
Control.Newtype.underF Sum f ≡ mapping (_Wrapping Sum) %~ f

under can also be used with _Unwrapping to provide the equivalent of Control.Newtype.under. Also, most use cases don't need full polymorphism, so only the single constructor _Wrapping functions would be needed.

These equivalences aren't 100% honest, because newtype's operators need to rely on two Newtype constraints. This means that the wrapper used for the output is not necessarily the same as the input.

  • 3 classes
  • 11 values
  • Packagelens-5.3.5
  • Exports16
  • LanguageHaskell2010
  • LicenceBSD-2-Clause
  • SourceWrapped.hs

Wrapping and Unwrapping monomorphically

4 declarations
classclass Wrapped s where
#

Wrapped provides isomorphisms to wrap and unwrap newtypes or data types with one constructor.

Associated types

Methods

Instances149Wrapped, …

Wrapping and unwrapping polymorphically

6 declarations
classclass Wrapped s => Rewrapped s t
#
Instances149Rewrapped, …

Operations

3 declarations
valueop :: Wrapped s => (Unwrapped s -> s) -> s -> Unwrapped s
#

Given the constructor for a Wrapped type, return a deconstructor that is its inverse.

Assuming the Wrapped instance is legal, these laws hold:

op f . f ≡ id
f . op f ≡ id
Example1 expression
op Identity (Identity 4)4
Example1 expression
op Const (Const "hello")"hello"
valueala
  1. :: (Functor f, Rewrapping s t)
  2. => Unwrapped s -> s
  3. -> (Unwrapped t -> t) -> f s
  4. -> f (Unwrapped s)
#

This combinator is based on ala from Conor McBride's work on Epigram.

As with _Wrapping, the user supplied function for the newtype is ignored.

Example1 expression
ala Sum foldMap [1,2,3,4]10
Example1 expression
ala All foldMap [True,True]True
Example1 expression
ala All foldMap [True,False]False
Example1 expression
ala Any foldMap [False,False]False
Example1 expression
ala Any foldMap [True,False]True
Example1 expression
ala Product foldMap [1,2,3,4]24

You may want to think of this combinator as having the following, simpler, type.

ala :: Rewrapping s t => (Unwrapped s -> s) -> ((Unwrapped t -> t) -> e -> s) -> e -> Unwrapped s
valuealaf
  1. :: (Functor f, Functor g, Rewrapping s t)
  2. => Unwrapped s -> s
  3. -> f t -> g s
  4. -> f (Unwrapped t)
  5. -> g (Unwrapped s)
#

This combinator is based on ala' from Conor McBride's work on Epigram.

As with _Wrapping, the user supplied function for the newtype is ignored.

alaf :: Rewrapping s t => (Unwrapped s -> s) -> ((r -> t) -> e -> s) -> (r -> Unwrapped t) -> e -> Unwrapped s
Example1 expression
alaf Sum foldMap Prelude.length ["hello","world"]10

Pattern Synonyms

2 declarations

Generics

1 declaration