0 | module Deriving.SpecialiseData
2 | import Control.Monad.Either
3 | import Control.Monad.Trans
4 | import public Data.DPair
8 | import public Data.List.Map
9 | import Data.List.Quantifiers
12 | import Data.SnocList
13 | import Data.SnocList.Quantifiers
14 | import Data.SortedMap
15 | import Data.SortedMap.Dependent
16 | import Data.SortedSet
18 | import Data.Vect.Quantifiers
19 | import public Decidable.Decidable
20 | import public Decidable.Equality
21 | import Deriving.Show
22 | import public Deriving.SpecialiseData.Codegen
23 | import public Deriving.SpecialiseData.Common
24 | import public Deriving.SpecialiseData.Helpers
25 | import public Deriving.SpecialiseData.Task
26 | import public Deriving.SpecialiseData.TaskFormation
27 | import public Deriving.SpecialiseData.Unification
28 | import public Deriving.SpecialiseData.Utils.Quantifiers
29 | import public Deriving.SpecialiseData.Utils.TypeInfo
30 | import public Language.Mk
31 | import Language.Reflection.Compat
32 | import Language.Reflection.Compat.Constr
33 | import public Language.Reflection.Compat.TypeInfo
34 | import public Language.Reflection.Expr
35 | import public Language.Reflection.Expr.VarSubst
36 | import Language.Reflection.Syntax
37 | import Language.Reflection.Logging
38 | import public Language.Reflection.Unify.Interface
40 | %language ElabReflection
62 | (nsProvider : NamespaceProvider m) =>
63 | (unifier : CanUnify m) =>
65 | MonadError SpecialisationError m =>
66 | (namesInfo : NamesInfoInTypes) =>
67 | SpecialisationParams =>
68 | (resultName : Name) ->
69 | (resultKind : TTImp) ->
70 | (resultContent : TTImp) ->
71 | m (TypeInfo, List Decl)
72 | specialiseDataRaw resultName resultKind resultContent = do
73 | task <- getTask resultName (cleanupNamedHoles resultKind) (cleanupNamedHoles resultContent)
74 | logPoint DetailedDebug "specialiseData" [task] "Specialising as \{show resultName}"
75 | logPoint DetailedDebug "specialiseData.task" [task] "Specialisation task: \{show task}"
76 | uniResults <- sequence $
mapCons task $
unifyCon task
77 | (Element specTy specTyNamed, specMeta) <- mkSpecTy task uniResults
78 | decls <- specDecls task uniResults specTy specMeta
79 | pure (specTy, decls)
81 | typeDPair : List Arg -> TTImp
82 | typeDPair [] = `(Type)
83 | typeDPair (x :: xs) = do
84 | let aName = fromMaybe "" x.name
85 | let aTyName = fromString "\{aName}^ty"
86 | let tyArg = MkArg MW ExplicitArg (Just aTyName) `(Type)
87 | let tyVar = var aTyName
88 | let valArg = MkArg MW ExplicitArg (Just aName) tyVar
89 | `(DPair
Type ~(lam tyArg `(DPair
~tyVar ~(lam valArg $
typeDPair xs))))
91 | valDPair : SortedMap Name String -> List Arg -> TTImp -> TTImp
92 | valDPair n2s [] x = x
93 | valDPair n2s (x :: xs) y = do
94 | let aName = fromMaybe "" x.name
95 | let aTyName = fromString "\{aName}^ty"
96 | let tyVar = var aTyName
97 | let valVar = var aName
98 | let valHole = fromMaybe `(?
) $
hole <$> lookup aName n2s
99 | `(MkDPair
~(x.type) ~(iLet MW aName x.type valHole `(MkDPair
~valVar ~(valDPair n2s xs y))))
101 | unholeImpl : SortedMap String Name -> TTImp -> TTImp
102 | unholeImpl s2n (IHole fc holeName) =
103 | case lookup holeName s2n of
105 | Nothing => IHole fc holeName
106 | unholeImpl s2n t = t
108 | unhole : SortedMap String Name -> TTImp -> TTImp
109 | unhole s2n = mapTTImp (unholeImpl s2n)
111 | unBadHoleImpl : TTImp -> TTImp
112 | unBadHoleImpl (IHole fc "_") = Implicit fc False
113 | unBadHoleImpl t = t
115 | unBadHole : TTImp -> TTImp
116 | unBadHole = mapTTImp unBadHoleImpl
118 | unMkDPair : TTImp -> List TTImp
119 | unMkDPair (IApp _ (IApp _ (INamedApp _ (INamedApp _ (IVar _ "Builtin.DPair.MkDPair") _ _) _ _) dl) dr) =
123 | decodeDPair : Elaboration m => List Arg -> List TTImp -> m (List Arg)
124 | decodeDPair [] _ = pure []
125 | decodeDPair (a :: as) (aT :: _ :: ts) = pure $
({type := aT} a) :: !(decodeDPair as ts)
126 | decodeDPair _ _ = fail "INTERNAL ERROR: Failed during lambda normalisation"
128 | genAliases : Elaboration m => List Arg -> m (SortedMap Name String, SortedMap String Name)
129 | genAliases = foldlM genAImpl (empty, empty)
132 | (SortedMap Name String, SortedMap String Name) ->
134 | m (SortedMap Name String, SortedMap String Name)
135 | genAImpl (n2s, s2n) a = do
136 | randN <- genSym "lamArg"
138 | let n = fromMaybe "" a.name
139 | pure (insert n s n2s, insert s n s2n)
142 | normaliseTask : Elaboration m => List Arg -> TTImp -> m (TTImp, TTImp)
143 | normaliseTask lamArgs lamRhs = do
144 | (n2s, s2n) <- genAliases lamArgs
145 | nT : Type <- check $
unBadHole $
typeDPair lamArgs
146 | nV : nT <- check $
unBadHole $
valDPair n2s lamArgs lamRhs
148 | newArgs <- decodeDPair lamArgs $
unMkDPair $
unBadHole $
unhole s2n nVQ
149 | let newLamTy = piAll `(Type) newArgs
150 | let newLam = foldr lam lamRhs newArgs
151 | pure (newLamTy, newLam)
154 | specialiseDataArgs :
156 | (nsProvider : NamespaceProvider m) =>
157 | (unifier : CanUnify m) =>
159 | MonadError SpecialisationError m =>
160 | (namesInfo : NamesInfoInTypes) =>
161 | SpecialisationParams =>
162 | (resultName : Name) ->
163 | (lambdaArgs : List Arg) ->
164 | (lambdaRHS : TTImp) ->
165 | m (TypeInfo, List Decl)
166 | specialiseDataArgs resultName fvArgs lambdaRHS =
167 | uncurry (specialiseDataRaw resultName) =<< normaliseTask fvArgs lambdaRHS
181 | specialiseDataLam :
185 | (nsProvider : NamespaceProvider m) =>
186 | (unifier : CanUnify m) =>
187 | MonadError SpecialisationError m =>
188 | (namesInfo : NamesInfoInTypes) =>
189 | SpecialisationParams =>
190 | (resultName : Name) ->
191 | (0 task : taskT) ->
192 | m (TypeInfo, List Decl)
193 | specialiseDataLam resultName task = do
195 | resultKind <- quote taskT
197 | resultContent <- quote task
198 | specialiseDataRaw resultName resultKind resultContent
214 | specialiseDataLam'' :
216 | (nsProvider : NamespaceProvider m) =>
217 | (unifier : CanUnify m) =>
218 | SpecialisationParams =>
223 | specialiseDataLam'' resultName task = do
225 | nit <- getNamesInfoInTypes' tq
226 | Right (specTy, decls) <-
227 | runEitherT {m} {e=SpecialisationError} $
228 | specialiseDataLam resultName task
229 | | Left err => fail "Specialisation error: \{show err}"
245 | specialiseDataLam' :
247 | (nsProvider : NamespaceProvider m) =>
248 | (unifier : CanUnify m) =>
249 | SpecialisationParams =>
254 | specialiseDataLam' resultName task =
255 | specialiseDataLam'' resultName task >>= declare