0 | module Deriving.SpecialiseData
  1 |
  2 | import Control.Monad.Either
  3 | import Control.Monad.Trans
  4 | import public Data.DPair -- workaround for idris-lang/Idris2#2439
  5 | import Data.Either
  6 | import Data.Fin.Set
  7 | import Data.List
  8 | import public Data.List.Map -- workaround for idris-lang/Idris2#2439
  9 | import Data.List.Quantifiers
 10 | import Data.List1
 11 | import Data.Maybe
 12 | import Data.SnocList
 13 | import Data.SnocList.Quantifiers
 14 | import Data.SortedMap
 15 | import Data.SortedMap.Dependent
 16 | import Data.SortedSet
 17 | import Data.Vect
 18 | import Data.Vect.Quantifiers
 19 | import public Decidable.Decidable -- workaround for idris-lang/Idris2#2439
 20 | import public Decidable.Equality -- workaround for idris-lang/Idris2#2439
 21 | import Deriving.Show
 22 | import public Deriving.SpecialiseData.Codegen -- workaround for idris-lang/Idris2#2439
 23 | import public Deriving.SpecialiseData.Common
 24 | import public Deriving.SpecialiseData.Helpers -- workaround for idris-lang/Idris2#2439
 25 | import public Deriving.SpecialiseData.Task -- workaround for idris-lang/Idris2#2439
 26 | import public Deriving.SpecialiseData.TaskFormation -- workaround for idris-lang/Idris2#2439
 27 | import public Deriving.SpecialiseData.Unification -- workaround for idris-lang/Idris2#2439
 28 | import public Deriving.SpecialiseData.Utils.Quantifiers -- workaround for idris-lang/Idris2#2439
 29 | import public Deriving.SpecialiseData.Utils.TypeInfo -- workaround for idris-lang/Idris2#2439
 30 | import public Language.Mk -- used in generated code
 31 | import Language.Reflection.Compat
 32 | import Language.Reflection.Compat.Constr
 33 | import public Language.Reflection.Compat.TypeInfo -- workaround for idris-lang/Idris2#2439
 34 | import public Language.Reflection.Expr
 35 | import public Language.Reflection.Expr.VarSubst -- workaround for idris-lang/Idris2#2439
 36 | import Language.Reflection.Syntax
 37 | import Language.Reflection.Logging
 38 | import public Language.Reflection.Unify.Interface
 39 |
 40 | %language ElabReflection
 41 |
 42 | %default total
 43 |
 44 | ---------------------------
 45 | --- DATA SPECIALISATION ---
 46 | ---------------------------
 47 |
 48 | ||| Perform a specialisation for a given type name, kind and content expressions
 49 | |||
 50 | ||| In order to generate a specialised type declaration equivalent to the following type alias:
 51 | ||| ```
 52 | ||| VF : Nat -> Type
 53 | ||| VF n = Fin n
 54 | ||| ```
 55 | ||| ...you may use this function as follows:
 56 | ||| ```
 57 | ||| specialiseDataRaw `{VF} `(Nat -> Type) `(\n => Fin n)
 58 | ||| ```
 59 | export
 60 | specialiseDataRaw :
 61 |   Monad m =>
 62 |   (nsProvider : NamespaceProvider m) =>
 63 |   (unifier : CanUnify m) =>
 64 |   MonadLog 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)
 80 |
 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))))
 90 |
 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))))
100 |
101 | unholeImpl : SortedMap String Name -> TTImp -> TTImp
102 | unholeImpl s2n (IHole fc holeName) =
103 |   case lookup holeName s2n of
104 |       Just vn => var vn
105 |       Nothing => IHole fc holeName
106 | unholeImpl s2n t = t
107 |
108 | unhole : SortedMap String Name -> TTImp -> TTImp
109 | unhole s2n = mapTTImp (unholeImpl s2n)
110 |
111 | unBadHoleImpl : TTImp -> TTImp
112 | unBadHoleImpl (IHole fc "_") = Implicit fc False
113 | unBadHoleImpl t = t
114 |
115 | unBadHole : TTImp -> TTImp
116 | unBadHole = mapTTImp unBadHoleImpl
117 |
118 | unMkDPair : TTImp -> List TTImp
119 | unMkDPair (IApp _ (IApp _ (INamedApp _ (INamedApp _ (IVar _ "Builtin.DPair.MkDPair") _ _) _ _) dl) dr) =
120 |   dl :: unMkDPair dr
121 | unMkDPair _ = []
122 |
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"
127 |
128 | genAliases : Elaboration m => List Arg -> m (SortedMap Name String, SortedMap String Name)
129 | genAliases = foldlM genAImpl (empty, empty)
130 |   where
131 |     genAImpl :
132 |       (SortedMap Name String, SortedMap String Name) ->
133 |       Arg ->
134 |       m (SortedMap Name String, SortedMap String Name)
135 |     genAImpl (n2s, s2n) a = do
136 |       randN <- genSym "lamArg"
137 |       let s = show randN
138 |       let n = fromMaybe "" a.name
139 |       pure (insert n s n2s, insert s n s2n)
140 |
141 | export
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
147 |   nVQ <- quote nV
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)
152 |
153 | export
154 | specialiseDataArgs :
155 |   Elaboration m =>
156 |   (nsProvider : NamespaceProvider m) =>
157 |   (unifier : CanUnify m) =>
158 |   MonadLog 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
168 |
169 | ||| Perform a specialisation for a given type name and content lambda
170 | |||
171 | ||| In order to generate a specialised type declaration equivalent to the following type alias:
172 | ||| ```
173 | ||| VF : Nat -> Type
174 | ||| VF n = Fin n
175 | ||| ```
176 | ||| ...you may use this function as follows:
177 | ||| ```
178 | ||| specialiseData `{VF} $ \n => Fin n
179 | ||| ```
180 | export
181 | specialiseDataLam :
182 |   -- TaskLambda taskT =>
183 |   Monad m =>
184 |   Elaboration m =>
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
194 |   -- Quote spec lambda type
195 |   resultKind <- quote taskT
196 |   -- Quote spec lambda
197 |   resultContent <- quote task
198 |   specialiseDataRaw resultName resultKind resultContent
199 |
200 |
201 | ||| Perform a specialisation for a given type name and content lambda,
202 | ||| returning a list of declarations and failing on error
203 | |||
204 | ||| In order to generate a specialised type declaration equivalent to the following type alias:
205 | ||| ```
206 | ||| VF : Nat -> Type
207 | ||| VF n = Fin n
208 | ||| ```
209 | ||| ...you may use this function as follows:
210 | ||| ```
211 | ||| specialiseDataLam'' `{VF} $ \n => Fin n
212 | ||| ```
213 | export
214 | specialiseDataLam'' :
215 |   Elaboration m =>
216 |   (nsProvider : NamespaceProvider m) =>
217 |   (unifier : CanUnify m) =>
218 |   SpecialisationParams =>
219 |   -- TaskLambda taskT =>
220 |   Name ->
221 |   (0 task: taskT) ->
222 |   m $ List Decl
223 | specialiseDataLam'' resultName task = do
224 |   tq <- quote task
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}"
230 |   pure decls
231 |
232 | ||| Perform a specialisation for a given type name and content lambda,
233 | ||| declaring the results and failing on error
234 | |||
235 | ||| In order to declare a specialised type declaration equivalent to the following type alias:
236 | ||| ```
237 | ||| VF : Nat -> Type
238 | ||| VF n = Fin n
239 | ||| ```
240 | ||| ...you may use this function as follows:
241 | ||| ```
242 | ||| %runElab specialiseDataLam' `{VF} $ \n => Fin n
243 | ||| ```
244 | export
245 | specialiseDataLam' :
246 |   Elaboration m =>
247 |   (nsProvider : NamespaceProvider m) =>
248 |   (unifier : CanUnify m) =>
249 |   SpecialisationParams =>
250 |   -- TaskLambda taskT =>
251 |   Name ->
252 |   (0 task: taskT) ->
253 |   m ()
254 | specialiseDataLam' resultName task =
255 |   specialiseDataLam'' resultName task >>= declare
256 |