profunctor-optics-0.0.0.2: An 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 #

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

type As a = Equality' a a Source #

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

Iso

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

For any valid Iso o we have: o . re o ≡ id re o . o ≡ id view o (review o b) ≡ b review o (view o s) ≡ s

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

Constructors

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

Obtain an Iso from two inverses.

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.

ixmapping :: Profunctor p => AIso s t a b -> IndexedOptic p i s t a b Source #

Lift an Iso into an indexed version.

>>> ixlists (ixtraversed . ixmapping swapped) [(40,'f'),(41,'o'),(42,'o')]
[(0,('f',40)),(1,('o',41)),(2,('o',42))]

cxmapping :: Profunctor p => AIso s t a b -> CoindexedOptic p k s t a b Source #

Lift an Iso into a coindexed version.

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

TODO: Document

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 Contravariant functor.

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 #

TODO: Document

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 #

Capture type constraints as an Iso'.

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

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

Data types that are representationally equal.

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

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

Work under a newtype wrapper.

'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

rewrapping :: 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 #

Convert between a data type and its 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 #

Convert between a data type and its Generic1 representation.

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 #

TODO: Document

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

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

TODO: Document

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

TODO: Document

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

Iso defined by left-association of nested tuples.

eassociated :: 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"

added :: Group a => a -> Iso' a a Source #

The group isomorphism defined by an element's action.

subtracted :: Group a => a -> Iso' a a Source #

The group isomorphism defined by an element's inverse action.

subtracted n = re (added n)

viewedl :: Iso (Seq a) (Seq b) (ViewL a) (ViewL b) Source #

A Seq is isomorphic to a ViewL

viewl m ≡ m ^. viewedl
>>> Seq.fromList [1,2,3] ^. viewedl
1 :< fromList [2,3]
>>> Seq.empty ^. viewedl
EmptyL
>>> EmptyL ^. re viewedl
fromList []
>>> review viewedl $ 1 Seq.:< fromList [2,3]
fromList [1,2,3]

viewedr :: Iso (Seq a) (Seq b) (ViewR a) (ViewR b) Source #

A Seq is isomorphic to a ViewR

viewr m ≡ m ^. viewedr
>>> Seq.fromList [1,2,3] ^. viewedr
fromList [1,2] :> 3
>>> Seq.empty ^. viewedr
EmptyR
>>> EmptyR ^. re viewedr
fromList []
>>> review viewedr $ fromList [1,2] Seq.:> 3
fromList [1,2,3]

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

Remove a single value from a type.

nonnon' . only
>>> non 0 #^ rem 10 4
Just 2
>>> non 0 #^ rem 10 5
Nothing

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.

u1 :: Iso (U1 p) (U1 q) () () Source #

par1 :: Iso (Par1 p) (Par1 q) p q Source #

rec1 :: Iso (Rec1 f p) (Rec1 g q) (f p) (g q) Source #

k1 :: Iso (K1 i c p) (K1 j d q) c d Source #

m1 :: Iso (M1 i c f p) (M1 j d g q) (f p) (g q) Source #

Primitive operators

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

Extract the two functions that characterize an Iso.

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, reover the other.

reoverover . re

Compare reover.

reixed :: Profunctor p => AIso' i j -> IndexedOptic p i s t a b -> IndexedOptic p j s t a b Source #

Remap the indices of an indexed optic.

recxed :: Profunctor p => AIso' k l -> CoindexedOptic p k s t a b -> CoindexedOptic p l s t a b Source #

Remap the indices of a coindexed optic.

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 (rewrapping Sum) foldMap [1,2,3,4]
10

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

au :: AnIso 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.

>>> :t flip aup runStar
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 _Wrapping, 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.Type

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

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

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

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

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

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

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 #

Carriers

type AIso s t a b = Optic (IsoRep a b) s t a b Source #

When you see this as an argument to a function, it expects an Iso.

type AIso' s a = AIso s s a a Source #

data IsoRep a b s t Source #

The IsoRep profunctor precisely characterizes an Iso.

Constructors

IsoRep (s -> a) (b -> t) 
Instances
Profunctor (IsoRep a b) Source # 
Instance details

Defined in Data.Profunctor.Optic.Iso

Methods

dimap :: (a0 -> b0) -> (c -> d) -> IsoRep a b b0 c -> IsoRep a b a0 d #

lmap :: (a0 -> b0) -> IsoRep a b b0 c -> IsoRep a b a0 c #

rmap :: (b0 -> c) -> IsoRep a b a0 b0 -> IsoRep a b a0 c #

(#.) :: Coercible c b0 => q b0 c -> IsoRep a b a0 b0 -> IsoRep a b a0 c #

(.#) :: Coercible b0 a0 => IsoRep a b b0 c -> q a0 b0 -> IsoRep a b a0 c #

Sieve (IsoRep a b) (Index a b) Source # 
Instance details

Defined in Data.Profunctor.Optic.Iso

Methods

sieve :: IsoRep a b a0 b0 -> a0 -> Index a b b0 #

Cosieve (IsoRep a b) (Coindex a b) Source # 
Instance details

Defined in Data.Profunctor.Optic.Iso

Methods

cosieve :: IsoRep a b a0 b0 -> Coindex a b a0 -> b0 #

Functor (IsoRep a b s) Source # 
Instance details

Defined in Data.Profunctor.Optic.Iso

Methods

fmap :: (a0 -> b0) -> IsoRep a b s a0 -> IsoRep a b s b0 #

(<$) :: a0 -> IsoRep a b s b0 -> IsoRep a b s a0 #