Skip to content

Commit ff9725b

Browse files
committed
Tidy up applyToStack/continueNF
It's best if they don't use 'quote' (which actually is a potential problem anyway, if quotes somehow end up nested). applyToStack actually only worked properly for NTCon/NDCon/NPrim, although in practice that was fine since it was only ever called in those contexts. This fixes it so it properly continues evaluation, which means we have a chance of caching results in more cases.
1 parent a3a9b5c commit ff9725b

File tree

1 file changed

+54
-30
lines changed

1 file changed

+54
-30
lines changed

src/Core/Normalise.idr

Lines changed: 54 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,58 @@ parameters (defs : Defs, topopts : EvalOpts)
140140
eval env locs (Erased fc i) stk = pure $ NErased fc i
141141
eval env locs (TType fc) stk = pure $ NType fc
142142

143+
-- Apply an evaluated argument (perhaps cached from an earlier evaluation)
144+
-- to a stack
145+
applyToStack : {auto c : Ref Ctxt Defs} ->
146+
{free : _} ->
147+
Env Term free -> NF free -> Stack free -> Core (NF free)
148+
applyToStack env (NBind fc _ (Lam _ _ _ _) sc) (arg :: stk)
149+
= do defs' <- get Ctxt
150+
arg' <- sc defs' $ snd arg
151+
applyToStack env arg' stk
152+
applyToStack env (NBind fc x b@(Let _ r val ty) sc) stk
153+
= if (holesOnly topopts || argHolesOnly topopts) && not (tcInline topopts)
154+
then do b' <- traverse (\t => applyToStack env t []) b
155+
pure (NBind fc x b'
156+
(\defs', arg => applyToStack env !(sc defs' arg) stk))
157+
else do val' <- applyToStack env val []
158+
applyToStack env !(sc defs (MkNFClosure val')) stk
159+
applyToStack env (NBind fc x b sc) stk
160+
= do b' <- traverse (\t => applyToStack env t []) b
161+
pure (NBind fc x b'
162+
(\defs', arg => applyToStack env !(sc defs' arg) stk))
163+
applyToStack env (NApp fc (NRef nt fn) args) stk
164+
= evalRef env False fc nt fn (args ++ stk)
165+
(NApp fc (NRef nt fn) (args ++ stk))
166+
applyToStack env (NApp fc (NLocal mrig idx p) args) stk
167+
= evalLocal env fc mrig _ p (args ++ stk) []
168+
applyToStack env (NApp fc (NMeta n i args) args') stk
169+
= evalMeta env fc n i args (args' ++ stk)
170+
applyToStack env (NDCon fc n t a args) stk
171+
= pure $ NDCon fc n t a (args ++ stk)
172+
applyToStack env (NTCon fc n t a args) stk
173+
= pure $ NTCon fc n t a (args ++ stk)
174+
applyToStack env (NAs fc s p t) stk
175+
= if removeAs topopts
176+
then applyToStack env t stk
177+
else do p' <- applyToStack env p []
178+
t' <- applyToStack env t stk
179+
pure (NAs fc s p' t')
180+
applyToStack env (NDelayed fc r tm) stk
181+
= do tm' <- applyToStack env tm stk
182+
pure (NDelayed fc r tm')
183+
applyToStack env nf@(NDelay fc r ty tm) stk
184+
= pure nf -- stack should always be empty here!
185+
applyToStack env (NForce fc r tm args) stk
186+
= do tm' <- applyToStack env tm []
187+
case tm' of
188+
NDelay fc r _ arg =>
189+
eval env [arg] (Local {name = UN "fvar"} fc Nothing _ First) stk
190+
_ => pure (NForce fc r tm' (args ++ stk))
191+
applyToStack env nf@(NPrimVal fc _) _ = pure nf
192+
applyToStack env nf@(NErased fc _) _ = pure nf
193+
applyToStack env nf@(NType fc) _ = pure nf
194+
143195
evalLocClosure : {auto c : Ref Ctxt Defs} ->
144196
{free : _} ->
145197
Env Term free ->
@@ -150,22 +202,7 @@ parameters (defs : Defs, topopts : EvalOpts)
150202
evalLocClosure env fc mrig stk (MkClosure opts locs' env' tm')
151203
= evalWithOpts defs opts env' locs' tm' stk
152204
evalLocClosure {free} env fc mrig stk (MkNFClosure nf)
153-
= applyToStack nf stk
154-
where
155-
applyToStack : NF free -> Stack free -> Core (NF free)
156-
applyToStack (NBind fc _ (Lam _ _ _ _) sc) (arg :: stk)
157-
= do arg' <- sc defs $ snd arg
158-
applyToStack arg' stk
159-
applyToStack (NApp fc (NRef nt fn) args) stk
160-
= evalRef env False fc nt fn (args ++ stk)
161-
(NApp fc (NRef nt fn) args)
162-
applyToStack (NApp fc (NLocal mrig idx p) args) stk
163-
= evalLocal env fc mrig _ p (args ++ stk) []
164-
applyToStack (NDCon fc n t a args) stk
165-
= pure $ NDCon fc n t a (args ++ stk)
166-
applyToStack (NTCon fc n t a args) stk
167-
= pure $ NTCon fc n t a (args ++ stk)
168-
applyToStack nf _ = pure nf
205+
= applyToStack env nf stk
169206

170207
evalLocal : {auto c : Ref Ctxt Defs} ->
171208
{free : _} ->
@@ -703,20 +740,7 @@ export
703740
continueNF : {auto c : Ref Ctxt Defs} ->
704741
{vars : _} ->
705742
Defs -> Env Term vars -> NF vars -> Core (NF vars)
706-
continueNF defs env stuck@(NApp fc (NRef nt fn) stk)
707-
= evalRef defs defaultOpts env False fc nt fn stk stuck
708-
continueNF defs env (NApp fc (NMeta name idx args) stk)
709-
= evalMeta defs defaultOpts env fc name idx args stk
710-
-- Next batch are already in normal form
711-
continueNF defs env nf@(NDCon fc x tag arity xs) = pure nf
712-
continueNF defs env nf@(NTCon fc x tag arity xs) = pure nf
713-
continueNF defs env nf@(NPrimVal fc x) = pure nf
714-
continueNF defs env nf@(NErased fc imp) = pure nf
715-
continueNF defs env nf@(NType fc) = pure nf
716-
-- For the rest, easiest just to quote and reevaluate
717-
continueNF defs env tm
718-
= do empty <- clearDefs defs
719-
nf defs env !(quote empty env tm)
743+
continueNF defs env stuck = applyToStack defs defaultOpts env stuck []
720744

721745
export
722746
glueBack : {auto c : Ref Ctxt Defs} ->

0 commit comments

Comments
 (0)