{-# LANGUAGE Rank2Types #-}
{-# LANGUAGE TemplateHaskell #-}
module Control.Lens.Wedge where
import Control.Lens
import Data.Functor.Invariant.TH
data Wedge a b = Wedge
{ get :: a -> b
, reverseGet :: b -> a
}
$(deriveInvariant ''Wedge)
normalizeA :: Wedge a b -> a -> a
normalizeA w = reverseGet w . get w
normalizeB :: Wedge a b -> b -> b
normalizeB w = get w . reverseGet w
reverse :: Wedge a b -> Wedge b a
reverse (Wedge f g) = Wedge g f
composeWedge :: Wedge a b -> Wedge b c -> Wedge a c
composeWedge (Wedge x y) (Wedge q w) =
Wedge (q . x) (y . w)
composeIso :: Wedge a b -> Iso' b c -> Wedge a c
composeIso (Wedge x y) i =
Wedge ((^. i) . x) (y . review i)
fromIso :: Iso' a b -> Wedge a b
fromIso i = Wedge (^. i) (review i)