{-# LANGUAGE TemplateHaskell #-}

-- | Generate 'HFunctor' instances via @TemplateHaskell@
module Hyper.TH.Functor
    ( makeHFunctor
    ) where

import qualified Control.Lens as Lens
import Hyper.Class.Functor (HFunctor (..))
import Hyper.TH.Internal.Utils
import Language.Haskell.TH
import Language.Haskell.TH.Datatype (ConstructorVariant)

import Hyper.Internal.Prelude

-- | Generate a 'HFunctor' instance
makeHFunctor :: Name -> DecsQ
makeHFunctor :: Name -> DecsQ
makeHFunctor Name
typeName = Name -> Q TypeInfo
makeTypeInfo Name
typeName forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= TypeInfo -> DecsQ
makeHFunctorForType

makeHFunctorForType :: TypeInfo -> DecsQ
makeHFunctorForType :: TypeInfo -> DecsQ
makeHFunctorForType TypeInfo
info =
    forall (m :: * -> *).
Quote m =>
m [Type] -> m Type -> [m Dec] -> m Dec
instanceD
        (TypeInfo -> Q [Type]
makeContext TypeInfo
info forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= [Type] -> Q [Type]
simplifyContext)
        [t|HFunctor $(pure (tiInstance info))|]
        [ Name -> Inline -> RuleMatch -> Phases -> Pragma
InlineP 'hmap Inline
Inline RuleMatch
FunLike Phases
AllPhases forall a b. a -> (a -> b) -> b
& Pragma -> Dec
PragmaD forall a b. a -> (a -> b) -> b
& forall (f :: * -> *) a. Applicative f => a -> f a
pure
        , forall (m :: * -> *). Quote m => Name -> [m Clause] -> m Dec
funD 'hmap (TypeInfo
-> [(Name, ConstructorVariant, [Either Type CtrTypePattern])]
tiConstructors TypeInfo
info forall (f :: * -> *) a b. Functor f => f a -> (a -> b) -> f b
<&> (Name, ConstructorVariant, [Either Type CtrTypePattern])
-> Q Clause
makeCtr)
        ]
        forall (f :: * -> *) a b. Functor f => f a -> (a -> b) -> f b
<&> (forall a. a -> [a] -> [a]
: [])
    where
        ([Type -> Q Con]
_, NodeWitnesses
wit) = TypeInfo -> ([Type -> Q Con], NodeWitnesses)
makeNodeOf TypeInfo
info
        makeCtr :: (Name, ConstructorVariant, [Either Type CtrTypePattern])
-> Q Clause
makeCtr (Name, ConstructorVariant, [Either Type CtrTypePattern])
ctr =
            forall (m :: * -> *).
Quote m =>
[m Pat] -> m Body -> [m Dec] -> m Clause
clause [forall (m :: * -> *). Quote m => Name -> m Pat
varP Name
varF, Q Pat
pat] Q Body
body []
            where
                (Q Pat
pat, Q Body
body) = Int
-> NodeWitnesses
-> (Name, ConstructorVariant, [Either Type CtrTypePattern])
-> (Q Pat, Q Body)
makeHMapCtr Int
0 NodeWitnesses
wit (Name, ConstructorVariant, [Either Type CtrTypePattern])
ctr

varF :: Name
varF :: Name
varF = String -> Name
mkName String
"_f"

makeContext :: TypeInfo -> Q [Pred]
makeContext :: TypeInfo -> Q [Type]
makeContext TypeInfo
info =
    TypeInfo
-> [(Name, ConstructorVariant, [Either Type CtrTypePattern])]
tiConstructors TypeInfo
info forall s a. s -> Getting (Endo [a]) s a -> [a]
^.. forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
traverse forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall s t a b. Field3 s t a b => Lens s t a b
Lens._3 forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
traverse forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall c a b. Prism (Either c a) (Either c b) a b
Lens._Right
        forall a b. a -> (a -> b) -> b
& forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
traverse CtrTypePattern -> Q [Type]
ctxForPat
        forall (f :: * -> *) a b. Functor f => f a -> (a -> b) -> f b
<&> forall a. Monoid a => [a] -> a
mconcat
    where
        ctxForPat :: CtrTypePattern -> Q [Type]
ctxForPat (InContainer Type
t CtrTypePattern
pat) = (:) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [t|Functor $(pure t)|] forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> CtrTypePattern -> Q [Type]
ctxForPat CtrTypePattern
pat
        ctxForPat (GenEmbed Type
t) = [t|HFunctor $(pure t)|] forall (f :: * -> *) a b. Functor f => f a -> (a -> b) -> f b
<&> (forall a. a -> [a] -> [a]
: [])
        ctxForPat (FlatEmbed TypeInfo
t) = TypeInfo -> Q [Type]
makeContext TypeInfo
t
        ctxForPat CtrTypePattern
_ = forall (f :: * -> *) a. Applicative f => a -> f a
pure []

makeHMapCtr :: Int -> NodeWitnesses -> (Name, ConstructorVariant, [Either Type CtrTypePattern]) -> (Q Pat, Q Body)
makeHMapCtr :: Int
-> NodeWitnesses
-> (Name, ConstructorVariant, [Either Type CtrTypePattern])
-> (Q Pat, Q Body)
makeHMapCtr Int
i NodeWitnesses
wit (Name
cName, ConstructorVariant
_, [Either Type CtrTypePattern]
cFields) =
    (forall (m :: * -> *). Quote m => Name -> [m Pat] -> m Pat
conP Name
cName ([Name]
cVars forall (f :: * -> *) a b. Functor f => f a -> (a -> b) -> f b
<&> forall (m :: * -> *). Quote m => Name -> m Pat
varP), Q Body
body)
    where
        cVars :: [Name]
cVars =
            [Int
i ..]
                forall (f :: * -> *) a b. Functor f => f a -> (a -> b) -> f b
<&> String -> Name
mkName forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Char
'x' forall a. a -> [a] -> [a]
:) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Show a => a -> String
show
                forall a b. a -> (a -> b) -> b
& forall a. Int -> [a] -> [a]
take (forall (t :: * -> *) a. Foldable t => t a -> Int
length [Either Type CtrTypePattern]
cFields)
        body :: Q Body
body =
            forall a b c. (a -> b -> c) -> [a] -> [b] -> [c]
zipWith Either Type CtrTypePattern -> Name -> Q Exp
bodyFor [Either Type CtrTypePattern]
cFields [Name]
cVars
                forall a b. a -> (a -> b) -> b
& forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl forall (m :: * -> *). Quote m => m Exp -> m Exp -> m Exp
appE (forall (m :: * -> *). Quote m => Name -> m Exp
conE Name
cName)
                forall a b. a -> (a -> b) -> b
& forall (m :: * -> *). Quote m => m Exp -> m Body
normalB
        bodyFor :: Either Type CtrTypePattern -> Name -> Q Exp
bodyFor (Right CtrTypePattern
x) Name
v = CtrTypePattern -> Q Exp
bodyForPat CtrTypePattern
x forall (m :: * -> *). Quote m => m Exp -> m Exp -> m Exp
`appE` forall (m :: * -> *). Quote m => Name -> m Exp
varE Name
v
        bodyFor Left{} Name
v = forall (m :: * -> *). Quote m => Name -> m Exp
varE Name
v
        f :: Q Exp
f = forall (m :: * -> *). Quote m => Name -> m Exp
varE Name
varF
        bodyForPat :: CtrTypePattern -> Q Exp
bodyForPat (Node Type
t) = [|$f $(nodeWit wit t)|]
        bodyForPat (GenEmbed Type
t) = [|hmap ($f . $(embedWit wit t))|]
        bodyForPat (InContainer Type
_ CtrTypePattern
pat) = [|fmap $(bodyForPat pat)|]
        bodyForPat (FlatEmbed TypeInfo
x) =
            forall (m :: * -> *). Quote m => [m Match] -> m Exp
lamCaseE
                ( TypeInfo
-> [(Name, ConstructorVariant, [Either Type CtrTypePattern])]
tiConstructors TypeInfo
x
                    forall (f :: * -> *) a b. Functor f => f a -> (a -> b) -> f b
<&> forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry forall (m :: * -> *).
Quote m =>
m Pat -> m Body -> [m Dec] -> m Match
match
                        forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int
-> NodeWitnesses
-> (Name, ConstructorVariant, [Either Type CtrTypePattern])
-> (Q Pat, Q Body)
makeHMapCtr (Int
i forall a. Num a => a -> a -> a
+ forall (t :: * -> *) a. Foldable t => t a -> Int
length [Name]
cVars) NodeWitnesses
wit
                        forall (f :: * -> *) a b. Functor f => f (a -> b) -> a -> f b
?? []
                )