0 | module Core.Normalise
2 | import public Core.Normalise.Convert
3 | import public Core.Normalise.Eval
4 | import public Core.Normalise.Quote
6 | import Core.Context.Log
15 | normalisePis : {auto c : Ref Ctxt Defs} ->
17 | Defs -> Env Term vars -> Term vars -> Core (Term vars)
18 | normalisePis defs env tm
19 | = do tmnf <- nf defs env tm
21 | NBind _ _ (Pi {}) _ => quoteWithPi defs env tmnf
25 | glueBack : {auto c : Ref Ctxt Defs} ->
27 | Defs -> Env Term vars -> NF vars -> Glued vars
28 | glueBack defs env nf
30 | (do empty <- clearDefs defs
35 | glueClosure : {auto c : Ref Ctxt Defs} ->
37 | Defs -> Env Term vars -> Closure vars -> Glued vars
38 | glueClosure defs env clos
40 | (do empty <- clearDefs defs
41 | quote empty env clos)
42 | (const (evalClosure defs clos))
45 | normalise : {auto c : Ref Ctxt Defs} ->
47 | Defs -> Env Term free -> Term free -> Core (Term free)
48 | normalise defs env tm = quote defs env !(nf defs env tm)
51 | normaliseOpts : {auto c : Ref Ctxt Defs} ->
53 | EvalOpts -> Defs -> Env Term free -> Term free -> Core (Term free)
54 | normaliseOpts opts defs env tm
55 | = quote defs env !(nfOpts opts defs env tm)
58 | normaliseHoles : {auto c : Ref Ctxt Defs} ->
60 | Defs -> Env Term free -> Term free -> Core (Term free)
61 | normaliseHoles defs env tm
62 | = quote defs env !(nfOpts withHoles defs env tm)
65 | normaliseLHS : {auto c : Ref Ctxt Defs} ->
67 | Defs -> Env Term free -> Term free -> Core (Term free)
68 | normaliseLHS defs env (Bind fc n b sc)
69 | = pure $
Bind fc n b !(normaliseLHS defs (b :: env) sc)
70 | normaliseLHS defs env tm
71 | = quote defs env !(nfOpts onLHS defs env tm)
74 | tryNormaliseSizeLimit : {auto c : Ref Ctxt Defs} ->
77 | Env Term free -> Term free -> Core (Term free)
78 | tryNormaliseSizeLimit defs limit env tm
79 | = do tm' <- nf defs env tm
80 | quoteOpts (MkQuoteOpts False False (Just limit)) defs env tm'
85 | normaliseSizeLimit : {auto c : Ref Ctxt Defs} ->
88 | Env Term free -> Term free -> Core (Term free)
89 | normaliseSizeLimit defs limit env tm
90 | = catch (do tm' <- nf defs env tm
91 | quoteOpts (MkQuoteOpts False False (Just limit)) defs env tm')
95 | normaliseArgHoles : {auto c : Ref Ctxt Defs} ->
97 | Defs -> Env Term free -> Term free -> Core (Term free)
98 | normaliseArgHoles defs env tm
99 | = quote defs env !(nfOpts withArgHoles defs env tm)
102 | normaliseAll : {auto c : Ref Ctxt Defs} ->
104 | Defs -> Env Term free -> Term free -> Core (Term free)
105 | normaliseAll defs env tm
106 | = quote defs env !(nfOpts withAll defs env tm)
112 | normaliseScope : {auto c : Ref Ctxt Defs} ->
114 | Defs -> Env Term free -> Term free -> Core (Term free)
115 | normaliseScope defs env (Bind fc n b sc)
116 | = pure $
Bind fc n b !(normaliseScope defs (b :: env) sc)
117 | normaliseScope defs env tm = normalise defs env tm
120 | etaContract : {auto _ : Ref Ctxt Defs} ->
121 | {vars : _} -> Term vars -> Core (Term vars)
122 | etaContract tm = do
124 | logTerm "eval.eta" 5 "Attempting to eta contract subterms of" tm
125 | nf <- normalise defs (mkEnv EmptyFC _) tm
126 | logTerm "eval.eta" 5 "Evaluated to" nf
127 | res <- mapTermM act tm
128 | logTerm "eval.eta" 5 "Result of eta-contraction" res
133 | act : {vars : _} -> Term vars -> Core (Term vars)
135 | logTerm "eval.eta" 10 " Considering" tm
137 | (Bind _ x (Lam {}) (App _ fn (Local _ _ Z _))) => do
138 | logTerm "eval.eta" 10 " Shrinking candidate" fn
139 | let shrunk = shrink fn (Drop Refl)
142 | log "eval.eta" 10 " Failure!"
145 | logTerm "eval.eta" 10 " Success!" tm'
150 | getValArity : Defs -> Env Term vars -> NF vars -> Core Nat
151 | getValArity defs env (NBind fc x (Pi {}) sc)
152 | = pure (S !(getValArity defs env !(sc defs (toClosure defaultOpts env (Erased fc Placeholder)))))
153 | getValArity defs env val = pure 0
156 | getArity : {auto c : Ref Ctxt Defs} ->
158 | Defs -> Env Term vars -> Term vars -> Core Nat
159 | getArity defs env tm = getValArity defs env !(nf defs env tm)
163 | logNF : {vars : _} ->
164 | {auto c : Ref Ctxt Defs} ->
165 | LogTopic -> Nat -> Lazy String -> Env Term vars -> NF vars -> Core ()
166 | logNF s n msg env tmnf
167 | = when !(logging s n) $
168 | do defs <- get Ctxt
169 | tm <- quote defs env tmnf
170 | tm' <- toFullNames tm
171 | logString s.topic n (msg ++ ": " ++ show tm')
176 | logTermNF' : {vars : _} ->
177 | {auto c : Ref Ctxt Defs} ->
179 | Nat -> Lazy String -> Env Term vars -> Term vars -> Core ()
180 | logTermNF' s n msg env tm
181 | = do defs <- get Ctxt
182 | tmnf <- normaliseHoles defs env tm
183 | tm' <- toFullNames tmnf
184 | logString s.topic n (msg ++ ": " ++ show tm')
187 | logTermNF : {vars : _} ->
188 | {auto c : Ref Ctxt Defs} ->
190 | Nat -> Lazy String -> Env Term vars -> Term vars -> Core ()
191 | logTermNF s n msg env tm
192 | = when !(logging s n) $
logTermNF' s n msg env tm
195 | logGlue : {vars : _} ->
196 | {auto c : Ref Ctxt Defs} ->
198 | Nat -> Lazy String -> Glued vars -> Core ()
199 | logGlue s n msg gtm
200 | = when !(logging s n) $
201 | do defs <- get Ctxt
203 | tm' <- toFullNames tm
204 | logString s.topic n (msg ++ ": " ++ show tm')
207 | logGlueNF : {vars : _} ->
208 | {auto c : Ref Ctxt Defs} ->
210 | Nat -> Lazy String -> Env Term vars -> Glued vars -> Core ()
211 | logGlueNF s n msg env gtm
212 | = when !(logging s n) $
213 | do defs <- get Ctxt
215 | tmnf <- normaliseHoles defs env tm
216 | tm' <- toFullNames tmnf
217 | logString s.topic n (msg ++ ": " ++ show tm')
220 | logEnv : {vars : _} ->
221 | {auto c : Ref Ctxt Defs} ->
223 | Nat -> String -> Env Term vars -> Core ()
225 | = when !(logging s n) $
226 | do logString s.topic n msg
231 | dumpEnv : {vs : Scope} -> Env Term vs -> Core ()
232 | dumpEnv [] = pure ()
233 | dumpEnv {vs = x :: _} (Let _ c val ty :: bs)
234 | = do logTermNF' s n (msg ++ ": let " ++ show x) bs val
235 | logTermNF' s n (msg ++ ":" ++ show c ++ " " ++ show x) bs ty
237 | dumpEnv {vs = x :: _} (b :: bs)
238 | = do logTermNF' s n (msg ++ ":" ++ show (multiplicity b) ++ " " ++
239 | show (piInfo b) ++ " " ++
240 | show x) bs (binderType b)
242 | replace' : {auto c : Ref Ctxt Defs} ->
244 | Int -> Defs -> Env Term vars ->
245 | (lhs : NF vars) -> (parg : Term vars) -> (exp : NF vars) ->
247 | replace' {vars} tmpi defs env lhs parg tm
248 | = if !(convert defs env lhs tm)
252 | repArg : (Closure vars) -> Core (Term vars)
254 | = do tmnf <- evalClosure defs c
255 | replace' tmpi defs env lhs parg tmnf
257 | repSub : NF vars -> Core (Term vars)
258 | repSub (NBind fc x b scfn)
259 | = do b' <- traverse (\c => repSub !(evalClosure defs c)) b
260 | let x' = MN "tmp" tmpi
261 | sc' <- replace' (tmpi + 1) defs env lhs parg
262 | !(scfn defs (toClosure defaultOpts env (Ref fc Bound x')))
263 | pure (Bind fc x b' (refsToLocals (Add x x' None) sc'))
264 | repSub (NApp fc hd [])
265 | = do empty <- clearDefs defs
266 | quote empty env (NApp fc hd [])
267 | repSub (NApp fc hd args)
268 | = do args' <- traverse (traversePair repArg) args
269 | pure $
applyStackWithFC
270 | !(replace' tmpi defs env lhs parg (NApp fc hd []))
272 | repSub (NDCon fc n t a args)
273 | = do args' <- traverse (traversePair repArg) args
274 | empty <- clearDefs defs
275 | pure $
applyStackWithFC
276 | !(quote empty env (NDCon fc n t a []))
278 | repSub (NTCon fc n a args)
279 | = do args' <- traverse (traversePair repArg) args
280 | empty <- clearDefs defs
281 | pure $
applyStackWithFC
282 | !(quote empty env (NTCon fc n a []))
284 | repSub (NAs fc s a p)
285 | = do a' <- repSub a
287 | pure (As fc s a' p')
288 | repSub (NDelayed fc r tm)
289 | = do tm' <- repSub tm
290 | pure (TDelayed fc r tm')
291 | repSub (NDelay fc r ty tm)
292 | = do ty' <- replace' tmpi defs env lhs parg !(evalClosure defs ty)
293 | tm' <- replace' tmpi defs env lhs parg !(evalClosure defs tm)
294 | pure (TDelay fc r ty' tm')
295 | repSub (NForce fc r tm args)
296 | = do args' <- traverse (traversePair repArg) args
298 | pure $
applyStackWithFC (TForce fc r tm') args'
299 | repSub (NErased fc (Dotted t))
300 | = do t' <- repSub t
301 | pure (Erased fc (Dotted t'))
302 | repSub tm = do empty <- clearDefs defs
306 | replace : {auto c : Ref Ctxt Defs} ->
308 | Defs -> Env Term vars ->
309 | (orig : NF vars) -> (new : Term vars) -> (tm : NF vars) ->
311 | replace = replace' 0
316 | normalisePrims : {auto c : Ref Ctxt Defs} -> {vs : _} ->
318 | (Constant -> Bool) ->
320 | (arg -> Maybe Constant) ->
327 | (args : List arg) ->
332 | Core (Maybe (Term vs))
333 | normalisePrims boundSafe viewConstant all prims n args tm env
334 | = do let True = isPrimName prims !(getFullName n)
335 | | _ => pure Nothing
336 | let (mc :: _) = reverse args
337 | | _ => pure Nothing
338 | let (Just c) = viewConstant mc
339 | | _ => pure Nothing
340 | let True = boundSafe c
341 | | _ => pure Nothing
344 | then normaliseAll defs env tm
345 | else normalise defs env tm