{-# LANGUAGE DataKinds #-}
{-# LANGUAGE TypeFamilies #-}
module Data.Singletons.TH.Syntax
( module Data.Singletons.TH.Syntax
, module Data.Singletons.TH.Syntax.LocalVar
) where
import Prelude hiding ( exp )
import Data.Kind (Constraint, Type)
import Language.Haskell.TH.Syntax hiding (Type)
import Language.Haskell.TH.Desugar
import qualified Language.Haskell.TH.Desugar.OMap.Strict as OMap
import Language.Haskell.TH.Desugar.OMap.Strict (OMap)
import Language.Haskell.TH.Desugar.OSet (OSet)
import Data.Singletons.TH.Syntax.LocalVar
type VarPromotions = [(Name, LocalVar)]
data PromDPatInfos = PromDPatInfos
{ PromDPatInfos -> VarPromotions
prom_dpat_vars :: VarPromotions
, PromDPatInfos -> OSet LocalVar
prom_dpat_sig_kvs :: OSet LocalVar
}
instance Semigroup PromDPatInfos where
PromDPatInfos VarPromotions
vars1 OSet LocalVar
sig_kvs1 <> :: PromDPatInfos -> PromDPatInfos -> PromDPatInfos
<> PromDPatInfos VarPromotions
vars2 OSet LocalVar
sig_kvs2
= VarPromotions -> OSet LocalVar -> PromDPatInfos
PromDPatInfos (VarPromotions
vars1 VarPromotions -> VarPromotions -> VarPromotions
forall a. Semigroup a => a -> a -> a
<> VarPromotions
vars2) (OSet LocalVar
sig_kvs1 OSet LocalVar -> OSet LocalVar -> OSet LocalVar
forall a. Semigroup a => a -> a -> a
<> OSet LocalVar
sig_kvs2)
instance Monoid PromDPatInfos where
mempty :: PromDPatInfos
mempty = VarPromotions -> OSet LocalVar -> PromDPatInfos
PromDPatInfos VarPromotions
forall a. Monoid a => a
mempty OSet LocalVar
forall a. Monoid a => a
mempty
type SingDSigPaInfos = [(DExp, DType)]
data DataDecl = DataDecl DataFlavor Name [DTyVarBndrVis] [DCon]
data TySynDecl = TySynDecl Name [DTyVarBndrVis] DType
type OpenTypeFamilyDecl = TypeFamilyDecl 'Open
type ClosedTypeFamilyDecl = TypeFamilyDecl 'Closed
newtype TypeFamilyDecl (info :: FamilyInfo)
= TypeFamilyDecl { forall (info :: FamilyInfo). TypeFamilyDecl info -> DTypeFamilyHead
getTypeFamilyDecl :: DTypeFamilyHead }
data FamilyInfo = Open | Closed
data ClassDecl ann
= ClassDecl { forall (ann :: AnnotationFlag). ClassDecl ann -> DCxt
cd_cxt :: DCxt
, forall (ann :: AnnotationFlag). ClassDecl ann -> Name
cd_name :: Name
, forall (ann :: AnnotationFlag). ClassDecl ann -> [DTyVarBndrVis]
cd_tvbs :: [DTyVarBndrVis]
, forall (ann :: AnnotationFlag). ClassDecl ann -> [FunDep]
cd_fds :: [FunDep]
, forall (ann :: AnnotationFlag). ClassDecl ann -> LetDecEnv ann
cd_lde :: LetDecEnv ann
, forall (ann :: AnnotationFlag).
ClassDecl ann -> [OpenTypeFamilyDecl]
cd_atfs :: [OpenTypeFamilyDecl]
}
data InstDecl ann = InstDecl { forall (ann :: AnnotationFlag). InstDecl ann -> DCxt
id_cxt :: DCxt
, forall (ann :: AnnotationFlag). InstDecl ann -> Name
id_name :: Name
, forall (ann :: AnnotationFlag). InstDecl ann -> DCxt
id_arg_tys :: [DType]
, forall (ann :: AnnotationFlag). InstDecl ann -> OMap Name DType
id_sigs :: OMap Name DType
, forall (ann :: AnnotationFlag).
InstDecl ann -> [(Name, LetDecRHS ann)]
id_meths :: [(Name, LetDecRHS ann)] }
type UClassDecl = ClassDecl Unannotated
type UInstDecl = InstDecl Unannotated
type AClassDecl = ClassDecl Annotated
type AInstDecl = InstDecl Annotated
data ADExp = ADVarE Name
| ADConE Name
| ADLitE Lit
| ADAppE ADExp ADExp
| ADLamCasesE
Int
DType
[ADClause]
| ADLetE ALetDecEnv ADExp
| ADSigE DType
ADExp DType
data ADPat = ADLitP Lit
| ADVarP Name
| ADConP Name [DType] [ADPat]
| ADTildeP ADPat
| ADBangP ADPat
| ADSigP DType
ADPat DType
| ADWildP
data ADClause = ADClause VarPromotions
[ADPat] ADExp
data AnnotationFlag = Annotated | Unannotated
type Annotated = 'Annotated
type Unannotated = 'Unannotated
type family IfAnn (ann :: AnnotationFlag) (yes :: k) (no :: k) :: k where
IfAnn Annotated yes no = yes
IfAnn Unannotated yes no = no
data family LetDecRHS :: AnnotationFlag -> Type
data instance LetDecRHS Annotated
=
AFunction
Int
[ADClause]
|
AValue
ADExp
data instance LetDecRHS Unannotated = UFunction [DClause]
| UValue DExp
type ALetDecRHS = LetDecRHS Annotated
type ULetDecRHS = LetDecRHS Unannotated
type LetDecProm = (Name, [LocalVar])
data LetDecEnv ann = LetDecEnv
{ forall (ann :: AnnotationFlag).
LetDecEnv ann -> OMap Name (LetDecRHS ann)
lde_defns :: OMap Name (LetDecRHS ann)
, forall (ann :: AnnotationFlag). LetDecEnv ann -> OMap Name DType
lde_types :: OMap Name DType
, forall (ann :: AnnotationFlag).
LetDecEnv ann -> OMap Name (Fixity, NamespaceSpecifier)
lde_infix :: OMap Name (Fixity, NamespaceSpecifier)
, forall (ann :: AnnotationFlag).
LetDecEnv ann -> IfAnn ann (OMap Name LetDecProm) ()
lde_proms :: IfAnn ann (OMap Name LetDecProm) ()
}
type ALetDecEnv = LetDecEnv Annotated
type ULetDecEnv = LetDecEnv Unannotated
instance Semigroup ULetDecEnv where
LetDecEnv OMap Name (LetDecRHS Unannotated)
defns1 OMap Name DType
types1 OMap Name (Fixity, NamespaceSpecifier)
infx1 IfAnn Unannotated (OMap Name LetDecProm) ()
_ <> :: ULetDecEnv -> ULetDecEnv -> ULetDecEnv
<> LetDecEnv OMap Name (LetDecRHS Unannotated)
defns2 OMap Name DType
types2 OMap Name (Fixity, NamespaceSpecifier)
infx2 IfAnn Unannotated (OMap Name LetDecProm) ()
_ =
OMap Name (LetDecRHS Unannotated)
-> OMap Name DType
-> OMap Name (Fixity, NamespaceSpecifier)
-> IfAnn Unannotated (OMap Name LetDecProm) ()
-> ULetDecEnv
forall (ann :: AnnotationFlag).
OMap Name (LetDecRHS ann)
-> OMap Name DType
-> OMap Name (Fixity, NamespaceSpecifier)
-> IfAnn ann (OMap Name LetDecProm) ()
-> LetDecEnv ann
LetDecEnv (OMap Name (LetDecRHS Unannotated)
defns1 OMap Name (LetDecRHS Unannotated)
-> OMap Name (LetDecRHS Unannotated)
-> OMap Name (LetDecRHS Unannotated)
forall a. Semigroup a => a -> a -> a
<> OMap Name (LetDecRHS Unannotated)
defns2) (OMap Name DType
types1 OMap Name DType -> OMap Name DType -> OMap Name DType
forall a. Semigroup a => a -> a -> a
<> OMap Name DType
types2) (OMap Name (Fixity, NamespaceSpecifier)
infx1 OMap Name (Fixity, NamespaceSpecifier)
-> OMap Name (Fixity, NamespaceSpecifier)
-> OMap Name (Fixity, NamespaceSpecifier)
forall a. Semigroup a => a -> a -> a
<> OMap Name (Fixity, NamespaceSpecifier)
infx2) ()
instance Monoid ULetDecEnv where
mempty :: ULetDecEnv
mempty = OMap Name (LetDecRHS Unannotated)
-> OMap Name DType
-> OMap Name (Fixity, NamespaceSpecifier)
-> IfAnn Unannotated (OMap Name LetDecProm) ()
-> ULetDecEnv
forall (ann :: AnnotationFlag).
OMap Name (LetDecRHS ann)
-> OMap Name DType
-> OMap Name (Fixity, NamespaceSpecifier)
-> IfAnn ann (OMap Name LetDecProm) ()
-> LetDecEnv ann
LetDecEnv OMap Name (LetDecRHS Unannotated)
forall k v. OMap k v
OMap.empty OMap Name DType
forall k v. OMap k v
OMap.empty OMap Name (Fixity, NamespaceSpecifier)
forall k v. OMap k v
OMap.empty ()
valueBinding :: Name -> ULetDecRHS -> ULetDecEnv
valueBinding :: Name -> LetDecRHS Unannotated -> ULetDecEnv
valueBinding Name
n LetDecRHS Unannotated
v = ULetDecEnv
emptyLetDecEnv { lde_defns = OMap.singleton n v }
typeBinding :: Name -> DType -> ULetDecEnv
typeBinding :: Name -> DType -> ULetDecEnv
typeBinding Name
n DType
t = ULetDecEnv
emptyLetDecEnv { lde_types = OMap.singleton n t }
infixDecl :: Fixity -> NamespaceSpecifier -> Name -> ULetDecEnv
infixDecl :: Fixity -> NamespaceSpecifier -> Name -> ULetDecEnv
infixDecl Fixity
f NamespaceSpecifier
ns Name
n = ULetDecEnv
emptyLetDecEnv { lde_infix = OMap.singleton n (f, ns) }
emptyLetDecEnv :: ULetDecEnv
emptyLetDecEnv :: ULetDecEnv
emptyLetDecEnv = ULetDecEnv
forall a. Monoid a => a
mempty
buildLetDecEnv :: Quasi q => [DLetDec] -> q ULetDecEnv
buildLetDecEnv :: forall (q :: * -> *). Quasi q => [DLetDec] -> q ULetDecEnv
buildLetDecEnv = ULetDecEnv -> [DLetDec] -> q ULetDecEnv
forall {m :: * -> *}.
Quasi m =>
ULetDecEnv -> [DLetDec] -> m ULetDecEnv
go ULetDecEnv
emptyLetDecEnv
where
go :: ULetDecEnv -> [DLetDec] -> m ULetDecEnv
go ULetDecEnv
acc [] = ULetDecEnv -> m ULetDecEnv
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return ULetDecEnv
acc
go ULetDecEnv
acc (DFunD Name
name [DClause]
clauses : [DLetDec]
rest) =
ULetDecEnv -> [DLetDec] -> m ULetDecEnv
go (Name -> LetDecRHS Unannotated -> ULetDecEnv
valueBinding Name
name ([DClause] -> LetDecRHS Unannotated
UFunction [DClause]
clauses) ULetDecEnv -> ULetDecEnv -> ULetDecEnv
forall a. Semigroup a => a -> a -> a
<> ULetDecEnv
acc) [DLetDec]
rest
go ULetDecEnv
acc (DValD (DVarP Name
name) DExp
exp : [DLetDec]
rest) =
ULetDecEnv -> [DLetDec] -> m ULetDecEnv
go (Name -> LetDecRHS Unannotated -> ULetDecEnv
valueBinding Name
name (DExp -> LetDecRHS Unannotated
UValue DExp
exp) ULetDecEnv -> ULetDecEnv -> ULetDecEnv
forall a. Semigroup a => a -> a -> a
<> ULetDecEnv
acc) [DLetDec]
rest
go ULetDecEnv
acc (dec :: DLetDec
dec@(DValD {}) : [DLetDec]
rest) = do
flattened <- DLetDec -> m [DLetDec]
forall (q :: * -> *). Quasi q => DLetDec -> q [DLetDec]
flattenDValD DLetDec
dec
go acc (flattened ++ rest)
go ULetDecEnv
acc (DSigD Name
name DType
ty : [DLetDec]
rest) =
ULetDecEnv -> [DLetDec] -> m ULetDecEnv
go (Name -> DType -> ULetDecEnv
typeBinding Name
name DType
ty ULetDecEnv -> ULetDecEnv -> ULetDecEnv
forall a. Semigroup a => a -> a -> a
<> ULetDecEnv
acc) [DLetDec]
rest
go ULetDecEnv
acc (DInfixD Fixity
f NamespaceSpecifier
ns Name
n : [DLetDec]
rest) =
ULetDecEnv -> [DLetDec] -> m ULetDecEnv
go (Fixity -> NamespaceSpecifier -> Name -> ULetDecEnv
infixDecl Fixity
f NamespaceSpecifier
ns Name
n ULetDecEnv -> ULetDecEnv -> ULetDecEnv
forall a. Semigroup a => a -> a -> a
<> ULetDecEnv
acc) [DLetDec]
rest
go ULetDecEnv
acc (DPragmaD{} : [DLetDec]
rest) = ULetDecEnv -> [DLetDec] -> m ULetDecEnv
go ULetDecEnv
acc [DLetDec]
rest
data DerivedDecl (cls :: Type -> Constraint) = DerivedDecl
{ forall (cls :: * -> Constraint). DerivedDecl cls -> Maybe DCxt
ded_mb_cxt :: Maybe DCxt
, forall (cls :: * -> Constraint). DerivedDecl cls -> DType
ded_type :: DType
, forall (cls :: * -> Constraint). DerivedDecl cls -> Name
ded_type_tycon :: Name
, forall (cls :: * -> Constraint). DerivedDecl cls -> DataDecl
ded_decl :: DataDecl
}
type DerivedEqDecl = DerivedDecl Eq
type DerivedOrdDecl = DerivedDecl Ord
type DerivedShowDecl = DerivedDecl Show