@@ -1,7 +1,7 @@
1{-# LANGUAGE MultiParamTypeClasses, FlexibleInstances,
1{-# LANGUAGE MultiParamTypeClasses, FlexibleInstances,
2 PatternGuards #-}
2 PatternGuards #-}
4−module Core.Evaluate(normalise, normaliseC, normaliseAll,
4+module Core.Evaluate(normalise, normaliseTrace, normaliseC, normaliseAll,
5 simplify, specialise, hnf, convEq, convEq',
5 simplify, specialise, hnf, convEq, convEq',
6 Def(..), Accessibility(..), Totality(..), PReason(..),
6 Def(..), Accessibility(..), Totality(..), PReason(..),
7 Context, initContext, ctxtAlist, uconstraints, next_tvar,
7 Context, initContext, ctxtAlist, uconstraints, next_tvar,
@@ -83,22 +83,25 @@ threshold = 1000 -- boredom threshold for evaluation, to prevent infinite typech
83-- Normalise fully type checked terms (so, assume all names/let bindings resolved)
83-- Normalise fully type checked terms (so, assume all names/let bindings resolved)
84normaliseC :: Context -> Env -> TT Name -> TT Name
84normaliseC :: Context -> Env -> TT Name -> TT Name
85normaliseC ctxt env t
85normaliseC ctxt env t
86− = evalState (do val <- eval ctxt threshold [] env t []
86+ = evalState (do val <- eval False ctxt threshold [] env t []
87 quote 0 val) initEval
87 quote 0 val) initEval
89normaliseAll :: Context -> Env -> TT Name -> TT Name
89normaliseAll :: Context -> Env -> TT Name -> TT Name
90normaliseAll ctxt env t
90normaliseAll ctxt env t
91− = evalState (do val <- eval ctxt threshold [] env t [AtREPL]
91+ = evalState (do val <- eval False ctxt threshold [] env t [AtREPL]
92 quote 0 val) initEval
92 quote 0 val) initEval
94normalise :: Context -> Env -> TT Name -> TT Name
94normalise :: Context -> Env -> TT Name -> TT Name
95−normalise ctxt env t
95+normalise = normaliseTrace False
97+normaliseTrace :: Bool -> Context -> Env -> TT Name -> TT Name
98+normaliseTrace tr ctxt env t
96− = evalState (do val <- eval ctxt threshold [] (map finalEntry env) (finalise t) []
99+ = evalState (do val <- eval tr ctxt threshold [] (map finalEntry env) (finalise t) []
97 quote 0 val) initEval
100 quote 0 val) initEval
99specialise :: Context -> Env -> [(Name, Int)] -> TT Name -> TT Name
102specialise :: Context -> Env -> [(Name, Int)] -> TT Name -> TT Name
100specialise ctxt env limits t
103specialise ctxt env limits t
101− = evalState (do val <- eval ctxt threshold limits (map finalEntry env) (finalise t) []
104+ = evalState (do val <- eval False ctxt threshold limits (map finalEntry env) (finalise t) []
102 quote 0 val) (initEval { limited = limits })
105 quote 0 val) (initEval { limited = limits })
104-- Like normalise, but we only reduce functions that are marked as okay to
107-- Like normalise, but we only reduce functions that are marked as okay to
@@ -106,13 +109,13 @@ specialise ctxt env limits t
107simplify :: Context -> Env -> TT Name -> TT Name
110simplify :: Context -> Env -> TT Name -> TT Name
108simplify ctxt env t
111simplify ctxt env t
109− = evalState (do val <- eval ctxt threshold []
112+ = evalState (do val <- eval False ctxt threshold []
110 (map finalEntry env) (finalise t) [Simplify]
113 (map finalEntry env) (finalise t) [Simplify]
111 quote 0 val) initEval
114 quote 0 val) initEval
113hnf :: Context -> Env -> TT Name -> TT Name
116hnf :: Context -> Env -> TT Name -> TT Name
114hnf ctxt env t
117hnf ctxt env t
115− = evalState (do val <- eval ctxt threshold [] (map finalEntry env) (finalise t) [HNF]
118+ = evalState (do val <- eval False ctxt threshold [] (map finalEntry env) (finalise t) [HNF]
116 quote 0 val) initEval
119 quote 0 val) initEval
@@ -144,8 +147,9 @@ reduction = do ES ns s <- get
144-- Evaluate in a context of locally named things (i.e. not de Bruijn indexed,
147-- Evaluate in a context of locally named things (i.e. not de Bruijn indexed,
145-- such as we might have during construction of a proof)
148-- such as we might have during construction of a proof)
147−eval :: Context -> Int -> [(Name, Int)] -> Env -> TT Name -> [EvalOpt] -> Eval Value
150+eval :: Bool -> Context -> Int -> [(Name, Int)] -> Env -> TT Name ->
151+ [EvalOpt] -> Eval Value
148−eval ctxt maxred ntimes genv tm opts = ev ntimes [] True [] tm where
152+eval traceon ctxt maxred ntimes genv tm opts = ev ntimes [] True [] tm where
149 spec = Spec `elem` opts
153 spec = Spec `elem` opts
150 simpl = Simplify `elem` opts
154 simpl = Simplify `elem` opts
151 atRepl = AtREPL `elem` opts
155 atRepl = AtREPL `elem` opts
@@ -216,7 +220,7 @@ eval ctxt maxred ntimes genv tm opts = ev ntimes [] True [] tm where
216-- | spec = specApply ntimes stk env f args
220-- | spec = specApply ntimes stk env f args
217 apply ntimes_in stk top env f@(VP Ref n ty) args
221 apply ntimes_in stk top env f@(VP Ref n ty) args
218 | (True, ntimes) <- usable n ntimes_in
222 | (True, ntimes) <- usable n ntimes_in
219− = -- trace (show n) $
223+ = traceWhen traceon (show stk) $
220 do let val = lookupDefAcc Nothing n atRepl ctxt
224 do let val = lookupDefAcc Nothing n atRepl ctxt
221 case val of
225 case val of
222 [(CaseOp inl _ _ ns tree _ _, Public)] ->
226 [(CaseOp inl _ _ ns tree _ _, Public)] ->
@@ -629,7 +633,7 @@ initContext = MkContext [] 0 emptyContext
629ctxtAlist :: Context -> [(Name, Def)]
633ctxtAlist :: Context -> [(Name, Def)]
630ctxtAlist ctxt = map (\(n, (d, a, t)) -> (n, d)) $ toAlist (definitions ctxt)
634ctxtAlist ctxt = map (\(n, (d, a, t)) -> (n, d)) $ toAlist (definitions ctxt)
632−veval ctxt env t = evalState (eval ctxt threshold [] env t []) initEval
636+veval ctxt env t = evalState (eval False ctxt threshold [] env t []) initEval
634addToCtxt :: Name -> Term -> Type -> Context -> Context
638addToCtxt :: Name -> Term -> Type -> Context -> Context
635addToCtxt n tm ty uctxt
639addToCtxt n tm ty uctxt