Newtype-based optics via Coercible.
Provides wrapped and ala for working with newtypes
through profunctor optics, using GHC’s Coercible
constraint for zero-cost conversions.
Every newtype N is isomorphic to its underlying type A.
The Coercible constraint witnesses this at zero runtime
cost. wrapped lifts this into an Iso:
wrapped :: (Coercible s a, Coercible t b) => Iso s t a b
import Data.Profunctor.Optic.Newtype
import Data.Profunctor.Optic
import Data.Monoid (Sum(..))
>>> view wrapped (Sum 42)
42
>>> review wrapped 42 :: Sum Int
Sum {getSum = 42}
import Data.Profunctor.Optic.Newtype
import Data.Monoid
-- ala lifts a function to work "under" a newtype:
-- ala Sum foldMap [1, 2, 3] ≡ getSum (foldMap Sum [1, 2, 3])
>>> ala @(Sum Int) foldMap [1, 2, 3]
6
>>> ala @(Product Int) foldMap [1, 2, 3]
6
rewrapped converts between two newtypes that wrap the
same underlying type:
>>> review (rewrapped @(Sum Int)) (Product 42) :: Sum Int
Sum {getSum = 42}
| Module | Contents |
|---|---|
Data.Profunctor.Optic.Newtype |
wrapped, rewrapped, rewrapped', ala |
base, profunctor-optics