{-# LANGUAGE CPP #-}
{-# LANGUAGE Rank2Types #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE FlexibleContexts #-}
module Generics.Deriving.Lens
(
generic
, generic1
, tinplate
, GTraversal
) where
import Control.Applicative
import Control.Lens
import Data.Maybe (fromJust)
import Data.Typeable
import qualified GHC.Generics as Generic
import GHC.Generics hiding (from, to)
generic :: Generic a => Iso' a (Generic.Rep a b)
generic = iso Generic.from Generic.to
{-# INLINE generic #-}
generic1 :: Generic1 f => Iso' (f a) (Rep1 f a)
generic1 = iso from1 to1
{-# INLINE generic1 #-}
tinplate :: (Generic a, GTraversal (Generic.Rep a), Typeable b) => Traversal' a b
tinplate = generic . tinplated True
{-# INLINE tinplate #-}
maybeArg1Of :: Maybe c -> (c -> d) -> Maybe c
maybeArg1Of = const
{-# INLINE maybeArg1Of #-}
class GTraversal f where
tinplated :: Typeable b => Bool -> Traversal' (f a) b
instance (Generic a, GTraversal (Generic.Rep a), Typeable a) => GTraversal (K1 i a) where
tinplated rec f (K1 a) = case cast a `maybeArg1Of` f of
Just b -> K1 . fromJust . cast <$> f b
Nothing | rec -> K1 <$> fmap generic (tinplated False) f a
| otherwise -> pure $ K1 a
{-# INLINE tinplated #-}
instance GTraversal U1 where
tinplated _ _ U1 = pure U1
{-# INLINE tinplated #-}
instance (GTraversal f, GTraversal g) => GTraversal (f :*: g) where
tinplated _ f (x :*: y) = (:*:) <$> tinplated True f x <*> tinplated True f y
{-# INLINE tinplated #-}
instance (GTraversal f, GTraversal g) => GTraversal (f :+: g) where
tinplated _ f (L1 x) = L1 <$> tinplated True f x
tinplated _ f (R1 x) = R1 <$> tinplated True f x
{-# INLINE tinplated #-}
instance GTraversal a => GTraversal (M1 i c a) where
tinplated rec f (M1 x) = M1 <$> tinplated rec f x
{-# INLINE tinplated #-}
instance (Traversable f, GTraversal g) => GTraversal (f :.: g) where
tinplated _ f (Comp1 fgp) = Comp1 <$> traverse (tinplated True f) fgp
{-# INLINE tinplated #-}