Iso
Overview
An iso is both a lens and a prism there for you can combine lens and prism if iso.
In iso, s and a are isormorphic
Because an iso is a lens, we have:
code:view.hs
view :: Iso s a -> s -> a
Because an iso is also a prism, we also have:
code:review.hs
review :: Iso s a -> a -> s
Iso laws
An Iso s a zooms from an s into an ”isomorphic” part of type a, where the isomorphism is witnessed by view and
review.
Therefore an iso i :: Iso s a should obey the following two laws:
review view: review i (view i s) = s
view review: view i (review i a) = a
https://gyazo.com/a83f54535bd9a0c0311eee9daf4698bc
code:iso.hs
iso :: (s -> a) -> (a -> s) -> Iso s a
iso v r = dimap v (fmap r)
re:: Iso s a -> Iso a s
re i = iso (review i) (view i)
curried :: Iso ((a,b) -> c) (a -> b -> c)
curried = iso curry uncurry
flipped :: Iso (a -> b-> c) (b -> a -> c)
flipped = iso flip flip
swapped :: Iso (a, b) (b, a)
swapped = iso swap swap
where
swap(a, b) = (b, a)
swapped' :: Iso (Either a b) (Either b a)
swapped' = iso swap swap
where
swap = either Right Left