0 | module Control.Lens.Iso
3 | import Data.Contravariant
5 | import Data.Profunctor
6 | import Control.Monad.Identity
7 | import Control.Applicative.Const
8 | import Control.Lens.Optic
9 | import Control.Lens.Equality
20 | record IsIso p where
22 | runIsIso : Profunctor p
28 | 0 Iso : (s,t,a,b : Type) -> Type
35 | 0 Iso' : (s,a : Type) -> Type
48 | getIso : Iso s t a b -> (s -> a, b -> t)
49 | getIso l = l @{MkIsIso coexp} (id, id)
51 | [coexp] Profunctor (\x,y => (x -> a, b -> y)) where
52 | dimap f g (f', g') = (f' . f, g . g')
56 | withIso : Iso s t a b -> ((s -> a) -> (b -> t) -> r) -> r
57 | withIso l f = uncurry f (getIso l)
65 | iso : (s -> a) -> (b -> t) -> Iso s t a b
66 | iso f g @{MkIsIso _} = dimap f g
70 | fromEqv : s <=> a -> Iso' s a
71 | fromEqv (MkEquivalence l r) = iso l r
75 | from : Iso s t a b -> Iso b a t s
76 | from l @{MkIsIso _} = withIso l (flip dimap)
83 | au : Functor f => Iso s t a b -> ((b -> t) -> f s) -> f a
84 | au l f = withIso l $
\g,h => g <$> f h
88 | auf : (Functor f, Functor g) => Iso s t a b -> (f t -> g s) -> f b -> g a
89 | auf l f x = withIso l $
\g,h => g <$> f (h <$> x)
93 | xplat : Functor f => Iso s t a b -> ((s -> a) -> f b) -> f t
94 | xplat l f = withIso l $
\g,h => h <$> f g
98 | xplatf : (Functor f, Functor g) => Iso s t a b -> (f a -> g b) -> f s -> g t
99 | xplatf l f x = withIso l $
\g,h => h <$> f (g <$> x)
105 | under : Iso s t a b -> (t -> s) -> (b -> a)
106 | under l = let (f,g) = getIso l in (f .) . (. g)
114 | constant : a -> Iso (a -> b) (a' -> b') b b'
115 | constant x = iso ($
x) const
119 | involuted : (a -> a) -> Iso' a a
120 | involuted f = iso f f
124 | flipped : Iso (a -> b -> c) (a' -> b' -> c') (b -> a -> c) (b' -> a' -> c')
125 | flipped = iso flip flip
129 | swapped : Symmetric f => Iso (f a b) (f a' b') (f b a) (f b' a')
130 | swapped = iso swap' swap'
136 | casted : (Cast s a, Cast b t) => Iso s t a b
137 | casted = iso cast cast
145 | non : Eq a => a -> Iso' (Maybe a) a
146 | non x = iso (fromMaybe x) (\y => guard (x /= y) $> y)
150 | Id_ : Iso (Identity a) (Identity b) a b
151 | Id_ = iso runIdentity Id
154 | Const_ : Iso (Const a b) (Const c d) a c
155 | Const_ = iso runConst MkConst
162 | mapping : (Functor f, Functor g) => Iso s t a b -> Iso (f s) (g t) (f a) (g b)
163 | mapping l = withIso l $
\f,g => iso (map f) (map g)
167 | contramapping : (Contravariant f, Contravariant g) => Iso s t a b -> Iso (f a) (g b) (f s) (g t)
168 | contramapping l = withIso l $
\f,g => iso (contramap f) (contramap g)
172 | bimapping : (Bifunctor f, Bifunctor g) => Iso s t a b -> Iso s' t' a' b' ->
173 | Iso (f s s') (g t t') (f a a') (g b b')
174 | bimapping l r = withIso l $
\f,g => withIso r $
\f',g' =>
175 | iso (bimap f f') (bimap g g')
179 | mappingFst : (Bifunctor f, Bifunctor g) => Iso s t a b ->
180 | Iso (f s x) (g t y) (f a x) (g b y)
181 | mappingFst l = withIso l $
\f,g => iso (mapFst f) (mapFst g)
185 | mappingSnd : (Bifunctor f, Bifunctor g) => Iso s t a b ->
186 | Iso (f x s) (g y t) (f x a) (g y b)
187 | mappingSnd l = withIso l $
\f,g => iso (mapSnd f) (mapSnd g)
191 | dimapping : (Profunctor f, Profunctor g) => Iso s t a b -> Iso s' t' a' b' ->
192 | Iso (f a s') (g b t') (f s a') (g t b')
193 | dimapping l r = withIso l $
\f,g => withIso r $
\f',g' =>
194 | iso (dimap f f') (dimap g g')
198 | lmapping : (Profunctor f, Profunctor g) => Iso s t a b ->
199 | Iso (f a x) (g b y) (f s x) (g t y)
200 | lmapping l = withIso l $
\f,g => iso (lmap f) (lmap g)
204 | rmapping : (Profunctor f, Profunctor g) => Iso s t a b ->
205 | Iso (f x s) (g y t) (f x a) (g y b)
206 | rmapping l = withIso l $
\f,g => iso (rmap f) (rmap g)