module Data.Generics.Internal.Lens where
import Control.Applicative ( Const (..) )
import GHC.Generics ( (:*:) (..), Generic (..), M1 (..), Rep, (:+:) (..) )
newtype Identity a
= Identity { runIdentity :: a }
instance Functor Identity where
fmap f (Identity a)
= Identity (f a)
type Lens' s a
= forall f. Functor f => (a -> f a) -> s -> f s
(^.) :: s -> ((a -> Const a a) -> s -> Const a s) -> a
s ^. l = getConst (l Const s)
infixl 8 ^.
set :: ((a -> Identity b) -> s -> Identity t) -> b -> s -> t
set l b
= runIdentity . l (\_ -> Identity b)
first :: Lens' ((a :*: b) x) (a x)
first f (a :*: b)
= fmap (:*: b) (f a)
second :: Lens' ((a :*: b) x) (b x)
second f (a :*: b)
= fmap (a :*:) (f b)
combine :: Lens' (s x) a -> Lens' (t x) a -> Lens' ((:+:) s t x) a
combine sa _ f (L1 s) = fmap (\a -> L1 (set sa a s)) (f (s ^. sa))
combine _ ta f (R1 t) = fmap (\a -> R1 (set ta a t)) (f (t ^. ta))
repIso :: Generic a => Lens' a (Rep a x)
repIso a = fmap to . a . from
lensM :: Lens' (M1 i c f p) (f p)
lensM f (M1 x) = fmap M1 (f x)