{-# LANGUAGE CPP #-}
{-# LANGUAGE TemplateHaskell #-}
module Data.Comp.Derive.Utils where
import Control.Monad
import Data.Proxy
import Language.Haskell.TH
import Language.Haskell.TH.ExpandSyns
import Language.Haskell.TH.Syntax
#if __GLASGOW_HASKELL__ < 706
reportError :: String -> Q ()
reportError = report True
#endif
#if __GLASGOW_HASKELL__ < 800
data DataInfo = DataInfo Cxt Name [TyVarBndr] [Con] [Name]
#elif __GLASGOW_HASKELL__ >= 802
data DataInfo = DataInfo Cxt Name [TyVarBndr] [Con] [DerivClause]
#else
data DataInfo = DataInfo Cxt Name [TyVarBndr] [Con] Cxt
#endif
abstractNewtypeQ :: Q Info -> Q (Maybe DataInfo)
abstractNewtypeQ :: Q Info -> Q (Maybe DataInfo)
abstractNewtypeQ = (Info -> Maybe DataInfo) -> Q Info -> Q (Maybe DataInfo)
forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
liftM Info -> Maybe DataInfo
abstractNewtype
abstractNewtype :: Info -> Maybe DataInfo
#if __GLASGOW_HASKELL__ < 800
abstractNewtype (TyConI (NewtypeD cxt name args constr derive))
= Just (DataInfo cxt name args [constr] derive)
abstractNewtype (TyConI (DataD cxt name args constrs derive))
= Just (DataInfo cxt name args constrs derive)
#else
abstractNewtype :: Info -> Maybe DataInfo
abstractNewtype (TyConI (NewtypeD cxt :: Cxt
cxt name :: Name
name args :: [TyVarBndr]
args _ constr :: Con
constr derive :: [DerivClause]
derive))
= DataInfo -> Maybe DataInfo
forall a. a -> Maybe a
Just (Cxt -> Name -> [TyVarBndr] -> [Con] -> [DerivClause] -> DataInfo
DataInfo Cxt
cxt Name
name [TyVarBndr]
args [Con
constr] [DerivClause]
derive)
abstractNewtype (TyConI (DataD cxt :: Cxt
cxt name :: Name
name args :: [TyVarBndr]
args _ constrs :: [Con]
constrs derive :: [DerivClause]
derive))
= DataInfo -> Maybe DataInfo
forall a. a -> Maybe a
Just (Cxt -> Name -> [TyVarBndr] -> [Con] -> [DerivClause] -> DataInfo
DataInfo Cxt
cxt Name
name [TyVarBndr]
args [Con]
constrs [DerivClause]
derive)
#endif
abstractNewtype _ = Maybe DataInfo
forall a. Maybe a
Nothing
normalCon :: Con -> (Name,[StrictType], Maybe Type)
normalCon :: Con -> (Name, [StrictType], Maybe Type)
normalCon (NormalC constr :: Name
constr args :: [StrictType]
args) = (Name
constr, [StrictType]
args, Maybe Type
forall a. Maybe a
Nothing)
normalCon (RecC constr :: Name
constr args :: [VarBangType]
args) = (Name
constr, (VarBangType -> StrictType) -> [VarBangType] -> [StrictType]
forall a b. (a -> b) -> [a] -> [b]
map (\(_,s :: Bang
s,t :: Type
t) -> (Bang
s,Type
t)) [VarBangType]
args, Maybe Type
forall a. Maybe a
Nothing)
normalCon (InfixC a :: StrictType
a constr :: Name
constr b :: StrictType
b) = (Name
constr, [StrictType
a,StrictType
b], Maybe Type
forall a. Maybe a
Nothing)
normalCon (ForallC _ _ constr :: Con
constr) = Con -> (Name, [StrictType], Maybe Type)
normalCon Con
constr
#if __GLASGOW_HASKELL__ >= 800
normalCon (GadtC (constr :: Name
constr:_constrs :: [Name]
_constrs) args :: [StrictType]
args typ :: Type
typ) = (Name
constr,[StrictType]
args,Type -> Maybe Type
forall a. a -> Maybe a
Just Type
typ)
#endif
normalCon' :: Con -> (Name,[Type], Maybe Type)
normalCon' :: Con -> (Name, Cxt, Maybe Type)
normalCon' con :: Con
con = (Name
n, (StrictType -> Type) -> [StrictType] -> Cxt
forall a b. (a -> b) -> [a] -> [b]
map StrictType -> Type
forall a b. (a, b) -> b
snd [StrictType]
ts, Maybe Type
t)
where (n :: Name
n, ts :: [StrictType]
ts, t :: Maybe Type
t) = Con -> (Name, [StrictType], Maybe Type)
normalCon Con
con
normalConExp :: Con -> Q (Name,[Type], Maybe Type)
normalConExp :: Con -> Q (Name, Cxt, Maybe Type)
normalConExp c :: Con
c = do
let (n :: Name
n,ts :: Cxt
ts,t :: Maybe Type
t) = Con -> (Name, Cxt, Maybe Type)
normalCon' Con
c
Cxt
ts' <- (Type -> Q Type) -> Cxt -> Q Cxt
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM Type -> Q Type
expandSyns Cxt
ts
(Name, Cxt, Maybe Type) -> Q (Name, Cxt, Maybe Type)
forall (m :: * -> *) a. Monad m => a -> m a
return (Name
n, Cxt
ts',Maybe Type
t)
normalConStrExp :: Con -> Q (Name,[StrictType], Maybe Type)
normalConStrExp :: Con -> Q (Name, [StrictType], Maybe Type)
normalConStrExp c :: Con
c = do
let (n :: Name
n,ts :: [StrictType]
ts,t :: Maybe Type
t) = Con -> (Name, [StrictType], Maybe Type)
normalCon Con
c
[StrictType]
ts' <- (StrictType -> Q StrictType) -> [StrictType] -> Q [StrictType]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM (\ (st :: Bang
st,ty :: Type
ty) -> do Type
ty' <- Type -> Q Type
expandSyns Type
ty; StrictType -> Q StrictType
forall (m :: * -> *) a. Monad m => a -> m a
return (Bang
st,Type
ty')) [StrictType]
ts
(Name, [StrictType], Maybe Type)
-> Q (Name, [StrictType], Maybe Type)
forall (m :: * -> *) a. Monad m => a -> m a
return (Name
n, [StrictType]
ts',Maybe Type
t)
getBinaryFArg :: Type -> Maybe Type -> Type
getBinaryFArg :: Type -> Maybe Type -> Type
getBinaryFArg _ (Just (AppT (AppT _ t :: Type
t) _)) = Type
t
getBinaryFArg def :: Type
def _ = Type
def
getUnaryFArg :: Type -> Maybe Type -> Type
getUnaryFArg :: Type -> Maybe Type -> Type
getUnaryFArg _ (Just (AppT _ t :: Type
t)) = Type
t
getUnaryFArg def :: Type
def _ = Type
def
abstractConType :: Con -> (Name,Int)
abstractConType :: Con -> (Name, Int)
abstractConType (NormalC constr :: Name
constr args :: [StrictType]
args) = (Name
constr, [StrictType] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [StrictType]
args)
abstractConType (RecC constr :: Name
constr args :: [VarBangType]
args) = (Name
constr, [VarBangType] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [VarBangType]
args)
abstractConType (InfixC _ constr :: Name
constr _) = (Name
constr, 2)
abstractConType (ForallC _ _ constr :: Con
constr) = Con -> (Name, Int)
abstractConType Con
constr
#if __GLASGOW_HASKELL__ >= 800
abstractConType (GadtC (constr :: Name
constr:_) args :: [StrictType]
args _typ :: Type
_typ) = (Name
constr,[StrictType] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [StrictType]
args)
#endif
tyVarBndrName :: TyVarBndr -> Name
tyVarBndrName (PlainTV n :: Name
n) = Name
n
tyVarBndrName (KindedTV n :: Name
n _) = Name
n
containsType :: Type -> Type -> Bool
containsType :: Type -> Type -> Bool
containsType s :: Type
s t :: Type
t
| Type
s Type -> Type -> Bool
forall a. Eq a => a -> a -> Bool
== Type
t = Bool
True
| Bool
otherwise = case Type
s of
ForallT _ _ s' :: Type
s' -> Type -> Type -> Bool
containsType Type
s' Type
t
AppT s1 :: Type
s1 s2 :: Type
s2 -> Type -> Type -> Bool
containsType Type
s1 Type
t Bool -> Bool -> Bool
|| Type -> Type -> Bool
containsType Type
s2 Type
t
SigT s' :: Type
s' _ -> Type -> Type -> Bool
containsType Type
s' Type
t
_ -> Bool
False
containsType' :: Type -> Type -> [Int]
containsType' :: Type -> Type -> [Int]
containsType' = Int -> Type -> Type -> [Int]
forall a. Num a => a -> Type -> Type -> [a]
run 0
where run :: a -> Type -> Type -> [a]
run n :: a
n s :: Type
s t :: Type
t
| Type
s Type -> Type -> Bool
forall a. Eq a => a -> a -> Bool
== Type
t = [a
n]
| Bool
otherwise = case Type
s of
ForallT _ _ s' :: Type
s' -> a -> Type -> Type -> [a]
run a
n Type
s' Type
t
AppT s1 :: Type
s1 s2 :: Type
s2 -> a -> Type -> Type -> [a]
run a
n Type
s1 Type
t [a] -> [a] -> [a]
forall a. [a] -> [a] -> [a]
++ a -> Type -> Type -> [a]
run (a
na -> a -> a
forall a. Num a => a -> a -> a
+1) Type
s2 Type
t
SigT s' :: Type
s' _ -> a -> Type -> Type -> [a]
run a
n Type
s' Type
t
_ -> []
newNames :: Int -> String -> Q [Name]
newNames :: Int -> String -> Q [Name]
newNames n :: Int
n name :: String
name = Int -> Q Name -> Q [Name]
forall (m :: * -> *) a. Applicative m => Int -> m a -> m [a]
replicateM Int
n (String -> Q Name
newName String
name)
tupleTypes :: Int -> Int -> [Name]
tupleTypes n :: Int
n m :: Int
m = (Int -> Name) -> [Int] -> [Name]
forall a b. (a -> b) -> [a] -> [b]
map Int -> Name
tupleTypeName [Int
n..Int
m]
derive :: [Name -> Q [Dec]] -> [Name] -> Q [Dec]
derive :: [Name -> Q [Dec]] -> [Name] -> Q [Dec]
derive ders :: [Name -> Q [Dec]]
ders names :: [Name]
names = ([[Dec]] -> [Dec]) -> Q [[Dec]] -> Q [Dec]
forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
liftM [[Dec]] -> [Dec]
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat (Q [[Dec]] -> Q [Dec]) -> Q [[Dec]] -> Q [Dec]
forall a b. (a -> b) -> a -> b
$ [Q [Dec]] -> Q [[Dec]]
forall (t :: * -> *) (m :: * -> *) a.
(Traversable t, Monad m) =>
t (m a) -> m (t a)
sequence [Name -> Q [Dec]
der Name
name | Name -> Q [Dec]
der <- [Name -> Q [Dec]]
ders, Name
name <- [Name]
names]
#if __GLASGOW_HASKELL__ < 710
mkClassP :: Name -> [Type] -> Pred
mkClassP = ClassP
#else
mkClassP :: Name -> [Type] -> Type
mkClassP :: Name -> Cxt -> Type
mkClassP name :: Name
name = (Type -> Type -> Type) -> Type -> Cxt -> Type
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl Type -> Type -> Type
AppT (Name -> Type
ConT Name
name)
#endif
#if __GLASGOW_HASKELL__ < 710
isEqualP :: Pred -> Maybe (Type, Type)
isEqualP (EqualP x y) = Just (x, y)
isEqualP _ = Nothing
#else
isEqualP :: Type -> Maybe (Type, Type)
isEqualP :: Type -> Maybe (Type, Type)
isEqualP (AppT (AppT EqualityT x :: Type
x) y :: Type
y) = (Type, Type) -> Maybe (Type, Type)
forall a. a -> Maybe a
Just (Type
x, Type
y)
isEqualP _ = Maybe (Type, Type)
forall a. Maybe a
Nothing
#endif
mkInstanceD :: Cxt -> Type -> [Dec] -> Dec
#if __GLASGOW_HASKELL__ < 800
mkInstanceD cxt ty decs = InstanceD cxt ty decs
#else
mkInstanceD :: Cxt -> Type -> [Dec] -> Dec
mkInstanceD cxt :: Cxt
cxt ty :: Type
ty decs :: [Dec]
decs = Maybe Overlap -> Cxt -> Type -> [Dec] -> Dec
InstanceD Maybe Overlap
forall a. Maybe a
Nothing Cxt
cxt Type
ty [Dec]
decs
#endif
liftSumGen :: Name -> Name -> Name -> Name -> Q [Dec]
liftSumGen :: Name -> Name -> Name -> Name -> Q [Dec]
liftSumGen caseName :: Name
caseName sumName :: Name
sumName allName :: Name
allName fname :: Name
fname = do
ClassI (ClassD _ name :: Name
name targs_ :: [TyVarBndr]
targs_ _ decs :: [Dec]
decs) _ <- Name -> Q Info
reify Name
fname
let targs :: [Name]
targs = (TyVarBndr -> Name) -> [TyVarBndr] -> [Name]
forall a b. (a -> b) -> [a] -> [b]
map TyVarBndr -> Name
tyVarBndrName [TyVarBndr]
targs_
ts :: Cxt
ts = (Name -> Type) -> [Name] -> Cxt
forall a b. (a -> b) -> [a] -> [b]
map Name -> Type
VarT ([Name] -> [Name]
forall a. [a] -> [a]
init [Name]
targs)
Name
fs <- String -> Q Name
newName "fs"
case [Name] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [Name]
targs of
True -> do
String -> Q ()
reportError (String -> Q ()) -> String -> Q ()
forall a b. (a -> b) -> a -> b
$ "Class " String -> String -> String
forall a. [a] -> [a] -> [a]
++ Name -> String
forall a. Show a => a -> String
show Name
name String -> String -> String
forall a. [a] -> [a] -> [a]
++ " cannot be lifted to sums!"
[Dec] -> Q [Dec]
forall (m :: * -> *) a. Monad m => a -> m a
return []
False -> do
Type
allCxt <- Name -> Q Type
conT Name
allName Q Type -> Q Type -> Q Type
`appT` Q Type
clsType Q Type -> Q Type -> Q Type
`appT` Name -> Q Type
varT Name
fs
let cxt :: Cxt
cxt = [ Type
allCxt ]
let tp :: Type
tp = Name -> Type
ConT Name
sumName Type -> Type -> Type
`AppT` Name -> Type
VarT Name
fs
let complType :: Type
complType = (Type -> Type -> Type) -> Type -> Cxt -> Type
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl Type -> Type -> Type
AppT (Name -> Type
ConT Name
name) Cxt
ts Type -> Type -> Type
`AppT` Type
tp
[Dec]
decs' <- [Q Dec] -> Q [Dec]
forall (t :: * -> *) (m :: * -> *) a.
(Traversable t, Monad m) =>
t (m a) -> m (t a)
sequence ([Q Dec] -> Q [Dec]) -> [Q Dec] -> Q [Dec]
forall a b. (a -> b) -> a -> b
$ (Dec -> [Q Dec]) -> [Dec] -> [Q Dec]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap Dec -> [Q Dec]
decl [Dec]
decs
[Dec] -> Q [Dec]
forall (m :: * -> *) a. Monad m => a -> m a
return [Cxt -> Type -> [Dec] -> Dec
mkInstanceD Cxt
cxt Type
complType [Dec]
decs']
where decl :: Dec -> [DecQ]
decl :: Dec -> [Q Dec]
decl (SigD f :: Name
f _) = [Name -> [ClauseQ] -> Q Dec
funD Name
f [Name -> ClauseQ
clause Name
f]]
decl _ = []
clause :: Name -> ClauseQ
clause :: Name -> ClauseQ
clause f :: Name
f = do Name
x <- String -> Q Name
newName "x"
Exp
pclsName <- ExpQ
pclsNameM
let b :: Body
b = Exp -> Body
NormalB (Name -> Exp
VarE Name
caseName Exp -> Exp -> Exp
`AppE` Exp
pclsName Exp -> Exp -> Exp
`AppE` Name -> Exp
VarE Name
f Exp -> Exp -> Exp
`AppE` Name -> Exp
VarE Name
x)
Clause -> ClauseQ
forall (m :: * -> *) a. Monad m => a -> m a
return (Clause -> ClauseQ) -> Clause -> ClauseQ
forall a b. (a -> b) -> a -> b
$ [Pat] -> Body -> [Dec] -> Clause
Clause [Name -> Pat
VarP Name
x] Body
b []
pclsNameM :: ExpQ
pclsNameM = [e| Proxy :: Proxy $clsType |]
clsType :: Q Type
clsType = (Q Type -> Type -> Q Type) -> Q Type -> Cxt -> Q Type
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl (\acc :: Q Type
acc a :: Type
a -> Q Type
acc Q Type -> Q Type -> Q Type
`appT` Type -> Q Type
forall (f :: * -> *) a. Applicative f => a -> f a
pure Type
a) (Name -> Q Type
conT Name
name) Cxt
ts
findSig :: [Name] -> [Dec] -> Q (Maybe ([Name],[Name]))
findSig :: [Name] -> [Dec] -> Q (Maybe ([Name], [Name]))
findSig targs :: [Name]
targs decs :: [Dec]
decs = case (Dec -> Q (Maybe Name)) -> [Dec] -> [Q (Maybe Name)]
forall a b. (a -> b) -> [a] -> [b]
map Dec -> Q (Maybe Name)
run [Dec]
decs of
[] -> Maybe ([Name], [Name]) -> Q (Maybe ([Name], [Name]))
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe ([Name], [Name])
forall a. Maybe a
Nothing
mx :: Q (Maybe Name)
mx:_ -> do Maybe Name
x <- Q (Maybe Name)
mx
case Maybe Name
x of
Nothing -> Maybe ([Name], [Name]) -> Q (Maybe ([Name], [Name]))
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe ([Name], [Name])
forall a. Maybe a
Nothing
Just n :: Name
n -> Maybe ([Name], [Name]) -> Q (Maybe ([Name], [Name]))
forall (m :: * -> *) a. Monad m => a -> m a
return (Maybe ([Name], [Name]) -> Q (Maybe ([Name], [Name])))
-> Maybe ([Name], [Name]) -> Q (Maybe ([Name], [Name]))
forall a b. (a -> b) -> a -> b
$ Name -> [Name] -> Maybe ([Name], [Name])
forall a. Eq a => a -> [a] -> Maybe ([a], [a])
splitNames Name
n [Name]
targs
where run :: Dec -> Q (Maybe Name)
run :: Dec -> Q (Maybe Name)
run (SigD _ ty :: Type
ty) = do
Type
ty' <- Type -> Q Type
expandSyns Type
ty
Maybe Name -> Q (Maybe Name)
forall (m :: * -> *) a. Monad m => a -> m a
return (Maybe Name -> Q (Maybe Name)) -> Maybe Name -> Q (Maybe Name)
forall a b. (a -> b) -> a -> b
$ Bool -> Type -> Maybe Name
getSig Bool
False Type
ty'
run _ = Maybe Name -> Q (Maybe Name)
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe Name
forall a. Maybe a
Nothing
getSig :: Bool -> Type -> Maybe Name
getSig t :: Bool
t (ForallT _ _ ty :: Type
ty) = Bool -> Type -> Maybe Name
getSig Bool
t Type
ty
getSig False (AppT (AppT ArrowT ty :: Type
ty) _) = Bool -> Type -> Maybe Name
getSig Bool
True Type
ty
getSig True (AppT ty :: Type
ty _) = Bool -> Type -> Maybe Name
getSig Bool
True Type
ty
getSig True (VarT n :: Name
n) = Name -> Maybe Name
forall a. a -> Maybe a
Just Name
n
getSig _ _ = Maybe Name
forall a. Maybe a
Nothing
splitNames :: a -> [a] -> Maybe ([a], [a])
splitNames y :: a
y (x :: a
x:xs :: [a]
xs)
| a
y a -> a -> Bool
forall a. Eq a => a -> a -> Bool
== a
x = ([a], [a]) -> Maybe ([a], [a])
forall a. a -> Maybe a
Just ([],[a]
xs)
| Bool
otherwise = do (xs1 :: [a]
xs1,xs2 :: [a]
xs2) <- a -> [a] -> Maybe ([a], [a])
splitNames a
y [a]
xs
([a], [a]) -> Maybe ([a], [a])
forall (m :: * -> *) a. Monad m => a -> m a
return (a
xa -> [a] -> [a]
forall a. a -> [a] -> [a]
:[a]
xs1,[a]
xs2)
splitNames _ [] = Maybe ([a], [a])
forall a. Maybe a
Nothing