{-# LANGUAGE Rank2Types #-}
{-# LANGUAGE TemplateHaskell #-}
module Control.Lens.Format where
import Control.Monad ((>=>))
import Control.Lens
import Data.Functor.Invariant.TH
data Format a b = Format
{ getMaybe :: a -> Maybe b
, reverseGet :: b -> a
}
$(deriveInvariant ''Format)
normalize :: Format a b -> a -> Maybe a
normalize (Format f g) x = g <$> f x
composePrism :: Format a b -> Prism' b c -> Format a c
composePrism (Format x y) p =
Format (x >=> (^? p)) (y . review p)
composeIso :: Format a b -> Iso' b c -> Format a c
composeIso (Format x y) i =
Format (fmap (^. i) . x) (y . review i)
fromPrism :: Prism' a b -> Format a b
fromPrism p = Format (^? p) (review p)
fromIso :: Iso' a b -> Format a b
fromIso i = Format (^? i) (review i)