profunctor-optics-0.0.2: A compact optics library compatible with the typeclasses in profunctors.

Safe HaskellNone
LanguageHaskell2010

Data.Profunctor.Optic.Iso

Contents

Synopsis

Types

type Equality s t a b = forall p. Optic p s t a b Source #

\( \mathsf{Equality}\;A = A \cong A \)

type Equality' s a = Equality s s a a Source #

type Iso s t a b = forall p. Profunctor p => Optic p s t a b Source #

\( \mathsf{Iso}\;S\;A = S \cong A \)

type Iso' s a = Iso s s a a Source #

iso :: (s -> a) -> (b -> t) -> Iso s t a b Source #

Obtain an Iso from two inverses.

o . re o ≡ id
re o . o ≡ id
view o (review o b) ≡ b
review o (view o s) ≡ s

Caution: In order for the generated optic to be well-defined, you must ensure that the input functions satisfy the following properties:

  • sa . bt ≡ id
  • bt . sa ≡ id

More generally, a profunctor optic must be monoidal as a natural transformation:

See Property.

isoVl :: (forall f g. Functor f => Functor g => (g a -> f b) -> g s -> f t) -> Iso s t a b Source #

Transform a Van Laarhoven Iso into a profunctor Iso.

fmapping :: Functor f => Functor g => AIso s t a b -> Iso (f s) (g t) (f a) (g b) Source #

Lift an Iso into a pair of functors.

contramapping :: Contravariant f => Contravariant g => AIso s t a b -> Iso (f a) (g b) (f s) (g t) Source #

Lift an Iso into a pair of Contravariant functors.

contramapping :: Contravariant f => Iso s t a b -> Iso (f a) (f b) (f s) (f t)

dimapping :: Profunctor p => Profunctor q => AIso s1 t1 a1 b1 -> AIso s2 t2 a2 b2 -> Iso (p a1 s2) (q b1 t2) (p s1 a2) (q t1 b2) Source #

Lift a pair of Isos into a pair of profunctors.

toYoneda :: Profunctor p => Iso s t a b -> p a b -> Yoneda p s t Source #

Lift an Iso into a Yoneda.

toCoyoneda :: Iso s t a b -> p a b -> Coyoneda p s t Source #

Lift an Iso into a Coyoneda.

cloneIso :: AIso s t a b -> Iso s t a b Source #

Convert from AIso back to any Iso.

Optics

equaled :: s ~ a => t ~ b => Iso s t a b Source #

Obtain an Iso' directly from type equality constraints.

>>> :t (^. equaled)
(^. equaled) :: b -> b

coerced :: Coercible s a => Coercible t b => Iso s t a b Source #

Obtain an Iso from data types that are representationally equal.

>>> view coerced 'x' :: Identity Char
Identity 'x'

wrapped :: Newtype s => Iso' s (O s) Source #

Obtain an Iso from a newtype.

view wrapped f . f ≡ id
f . view wrapped f ≡ id
>>> view wrapped $ Identity 'x'
'x'
>>> view wrapped (Const "hello")
"hello"

rewrapped :: Newtype s => Newtype t => Iso s t (O s) (O t) Source #

Work between newtype wrappers.

>>> Const "hello" & rewrapped ..~ Prelude.length & getConst
5

rewrapped' :: Newtype s => Newtype t => (O s -> s) -> Iso s t (O s) (O t) Source #

Variant of rewrapped that ignores its argument.

generic :: Generic a => Generic b => Iso a b (Rep a c) (Rep b c) Source #

Obtain an Iso from a Generic representation.

>>> view (generic . re generic) "hello" :: String
"hello"

generic1 :: Generic1 f => Generic1 g => Iso (f a) (g b) (Rep1 f a) (Rep1 g b) Source #

Obtain an Iso from a Generic1 representation.

adjuncted :: Adjunction f u => Iso (f a -> b) (f s -> t) (a -> u b) (s -> u t) Source #

Obtain an Iso from a functor and its adjoint.

Useful for converting between lens-like optics and grate-like optics:

over adjuncted :: Adjunction f u => ((a -> u b) -> s -> u t) -> (f a -> b) -> f s -> t

tabulated :: Representable f => Representable g => Iso (f a) (g b) (Rep f -> a) (Rep g -> b) Source #

Obtain an Iso from a functor and its function representation.

transposed :: Functor f => Distributive g => Iso (f (g a)) (g (f a)) (g (f a)) (f (g a)) Source #

TODO: Document

flipped :: Iso (a -> b -> c) (d -> e -> f) (b -> a -> c) (e -> d -> f) Source #

Flip two arguments of a function.

>>> (view flipped (,)) 1 2
(2,1)

curried :: Iso (a -> b -> c) (d -> e -> f) ((a, b) -> c) ((d, e) -> f) Source #

Curry a function.

>>> (fst ^. invert curried) 3 4
3

unzipped :: Adjunction f u => Iso (u a, u b) (u c, u d) (u (a, b)) (u (c, d)) Source #

A right adjoint admits an intrinsic notion of zipping.

cozipped :: Adjunction f u => Iso (f a + f b) (f c + f d) (f (a + b)) (f (c + d)) Source #

A left adjoint must be inhabited by exactly one element.

swapped :: Iso (a, b) (c, d) (b, a) (d, c) Source #

Swap sides of a product.

coswapped :: Iso (a + b) (c + d) (b + a) (d + c) Source #

Swap sides of a sum.

associated :: Iso (a, (b, c)) (d, (e, f)) ((a, b), c) ((d, e), f) Source #

Iso defined by left-association of nested tuples.

coassociated :: Iso (a + (b + c)) (d + (e + f)) ((a + b) + c) ((d + e) + f) Source #

Iso defined by left-association of nested tuples.

involuted :: (s -> a) -> Iso s a a s Source #

Obtain an Iso from a function that is its own inverse.

involutedjoin iso
>>> "live" ^. involuted reverse
"evil"
>>> involuted reverse ..~ ('d':) $ "live"
"lived"

anon :: a -> (a -> Bool) -> Iso' (Maybe a) a Source #

Generalize non a to take any value and a predicate.

Assumes that p a holds True and generates an isomorphism between Maybe (a | not (p a)) and a.

non :: Eq a => a -> Iso' (Maybe a) a Source #

Remove a single value from a type.

>>> review (non "foo") "foo"
Nothing
>>> review (non "foo") "foobar"
Just "foobar"

Primitive operators

withIso :: AIso s t a b -> ((s -> a) -> (b -> t) -> r) -> r Source #

Extract the two functions that characterize an Iso.

Operators

invert :: AIso s t a b -> Iso b a t s Source #

Invert an isomorphism.

invert (invert o) ≡ o

reover :: AIso s t a b -> (t -> s) -> b -> a Source #

Given a conversion on one side of an Iso, recover the other.

reoverover . re

Compare over.

op :: (Newtype n, o ~ O n) => (o -> n) -> n -> o #

This function serves two purposes:

  1. Giving you the unpack of a newtype without you needing to remember the name.
  2. Showing that the first parameter is completely ignored on the value level, meaning the only reason you pass in the constructor is to provide type information. Typeclasses sure are neat.
>>> op Identity (Identity 3)
3

au :: Functor f => AIso s t a b -> ((b -> t) -> f s) -> f a Source #

Based on ala from Conor McBride's work on Epigram.

This version is generalized to accept any Iso, not just a newtype.

>>> au (rewrapped' Sum) foldMap [1,2,3,4]
10

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

au :: AIso s t a b -> ((b -> t) -> e -> s) -> e -> a

aup :: Profunctor p => Functor f => AIso s t a b -> (p c a -> f b) -> p c s -> f t Source #

Variant of au for profunctors.

flip aup runStar :: Functor f => AIso s t a (f a) -> Star f c s -> c -> t

ala :: Newtype s => Newtype t => Functor f => (O s -> s) -> ((O t -> t) -> f s) -> f (O s) Source #

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

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

>>> ala Sum foldMap [1,2,3,4]
10
>>> ala All foldMap [True,True]
True
>>> ala All foldMap [True,False]
False
>>> ala Any foldMap [False,False]
False
>>> ala Any foldMap [True,False]
True
>>> ala Product foldMap [1,2,3,4]
24
ala :: Newtype s => Newtype t => (O s -> s) -> ((O t -> t) -> e -> s) -> e -> O s

Auxilliary Types

newtype Re p s t a b Source #

The Re type and its instances witness the symmetry between the parameters of a Profunctor.

Constructors

Re 

Fields

  • runRe :: p b a -> p t s
     
Instances
(Profunctor p, forall x. Contravariant (p x)) => Bifunctor (Re p s t) Source # 
Instance details

Defined in Data.Profunctor.Optic.Types

Methods

bimap :: (a -> b) -> (c -> d) -> Re p s t a c -> Re p s t b d #

first :: (a -> b) -> Re p s t a c -> Re p s t b c #

second :: (b -> c) -> Re p s t a b -> Re p s t a c #

Cochoice p => Choice (Re p s t) Source # 
Instance details

Defined in Data.Profunctor.Optic.Types

Methods

left' :: Re p s t a b -> Re p s t (Either a c) (Either b c) #

right' :: Re p s t a b -> Re p s t (Either c a) (Either c b) #

Choice p => Cochoice (Re p s t) Source # 
Instance details

Defined in Data.Profunctor.Optic.Types

Methods

unleft :: Re p s t (Either a d) (Either b d) -> Re p s t a b #

unright :: Re p s t (Either d a) (Either d b) -> Re p s t a b #

Costrong p => Strong (Re p s t) Source # 
Instance details

Defined in Data.Profunctor.Optic.Types

Methods

first' :: Re p s t a b -> Re p s t (a, c) (b, c) #

second' :: Re p s t a b -> Re p s t (c, a) (c, b) #

Strong p => Costrong (Re p s t) Source # 
Instance details

Defined in Data.Profunctor.Optic.Types

Methods

unfirst :: Re p s t (a, d) (b, d) -> Re p s t a b #

unsecond :: Re p s t (d, a) (d, b) -> Re p s t a b #

Profunctor p => Profunctor (Re p s t) Source # 
Instance details

Defined in Data.Profunctor.Optic.Types

Methods

dimap :: (a -> b) -> (c -> d) -> Re p s t b c -> Re p s t a d #

lmap :: (a -> b) -> Re p s t b c -> Re p s t a c #

rmap :: (b -> c) -> Re p s t a b -> Re p s t a c #

(#.) :: Coercible c b => q b c -> Re p s t a b -> Re p s t a c #

(.#) :: Coercible b a => Re p s t b c -> q a b -> Re p s t a c #

Bifunctor p => Contravariant (Re p s t a) Source # 
Instance details

Defined in Data.Profunctor.Optic.Types

Methods

contramap :: (a0 -> b) -> Re p s t a b -> Re p s t a a0 #

(>$) :: b -> Re p s t a b -> Re p s t a a0 #