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

Modulelens-5.3.6Haskell2010

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.

Contents
  1. Wrapping and Unwrapping monomorphically
  2. Wrapping and unwrapping polymorphically
  3. Operations
  4. Pattern Synonyms
  5. Generics
Synopsis
  • Packagelens-5.3.6
  • Exports16
  • LanguageHaskell2010
  • LicenceBSD-2-Clause
  • SourceWrapped.hs

Wrapping and Unwrapping monomorphically

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

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

Operations

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

Generics