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)
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)
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)