@@ -2,7 +2,7 @@
22 PatternGuards #-}
33
44module Core.Evaluate(normalise, normaliseTrace, normaliseC, normaliseAll,
5− simplify, specialise, hnf, convEq, convEq',
5+ rt_simplify, simplify, specialise, hnf, convEq, convEq',
66 Def(..), CaseInfo(..), CaseDefs(..),
77 Accessibility(..), Totality(..), PReason(..),
88 Context, initContext, ctxtAlist, uconstraints, next_tvar,
@@ -45,7 +45,9 @@ data Value = VP NameType Name Value
4545 | VApp Value Value
4646 | VType UExp
4747 | VErased
48+ | VImpossible
4849 | VConstant Const
50+ | VProj Value Int
4951-- | VLazy Env [Value] Term
5052 | VTmp Int
5153
@@ -104,6 +106,18 @@ simplify ctxt env t
104106 [Simplify]
105107 quote 0 val) initEval
106108
109+-- | Simplify for run-time (i.e. basic inlining)
110+rt_simplify :: Context -> Env -> TT Name -> TT Name
111+rt_simplify ctxt env t
112+ = evalState (do val <- eval False ctxt [(UN "lazy", 0),
113+ (UN "assert_smaller", 0),
114+ (UN "par", 0),
115+ (UN "prim__syntactic_eq", 0),
116+ (UN "prim_fork", 0)]
117+ (map finalEntry env) (finalise t)
118+ [RunTT]
119+ quote 0 val) initEval
120+
107121-- | Reduce a term to head normal form
108122hnf :: Context -> Env -> TT Name -> TT Name
109123hnf ctxt env t
@@ -170,6 +184,8 @@ eval traceon ctxt ntimes genv tm opts = ev ntimes [] True [] tm where
170184 -- returns 'True' if the function should block
171185 -- normal evaluation should return false
172186 blockSimplify (CaseInfo inl dict) n stk
187+ | RunTT `elem` opts
188+ = not inl || elem n stk
173189 | Simplify `elem` opts
174190 = (not (inl || dict) || elem n stk)
175191 || (n == UN "prim__syntactic_eq")
@@ -195,10 +211,11 @@ eval traceon ctxt ntimes genv tm opts = ev ntimes [] True [] tm where
195211 [(CaseOp ci _ _ _ cd, acc)]
196212 | acc == Public &&
197213 null (fst (cases_totcheck cd)) -> -- unoptimised version
198− let (_, tree) = getCases cd in
214+ let (ns, tree) = getCases cd in
199215 if blockSimplify ci n stk
200216 then liftM (VP Ref n) (ev ntimes stk top env ty)
201− else do c <- evCase ntimes n (n:stk) top env [] [] tree
217+ else -- traceWhen runtime (show (n, ns, tree)) $
218+ do c <- evCase ntimes n (n:stk) top env ns [] tree
202219 case c of
203220 (Nothing, _) -> liftM (VP Ref n) (ev ntimes stk top env ty)
204221 (Just v, _) -> return v
@@ -242,8 +259,15 @@ eval traceon ctxt ntimes genv tm opts = ev ntimes [] True [] tm where
242259 = do f' <- ev ntimes stk False env f
243260 a' <- ev ntimes stk False env a
244261 evApply ntimes stk top env [a'] f'
262+ ev ntimes stk top env (Proj t i)
263+ = do t' <- ev ntimes stk top env t
264+ return (doProj t' (getValArgs t'))
265+ where doProj t' (VP (DCon _ _) _ _, args) | i < length args = args!!i
266+ doProj t' _ = VProj t' i
267+
245268 ev ntimes stk top env (Constant c) = return $ VConstant c
246269 ev ntimes stk top env Erased = return VErased
270+ ev ntimes stk top env Impossible = return VImpossible
247271 ev ntimes stk top env (TType i) = return $ VType i
248272
249273 evApply ntimes stk top env args (VApp f a)
@@ -269,7 +293,8 @@ eval traceon ctxt ntimes genv tm opts = ev ntimes [] True [] tm where
269293 let (ns, tree) = getCases cd in
270294 if blockSimplify ci n stk
271295 then return $ unload env (VP Ref n ty) args
272− else do c <- evCase ntimes n (n:stk) top env ns args tree
296+ else -- traceWhen runtime (show (n, ns, tree)) $
297+ do c <- evCase ntimes n (n:stk) top env ns args tree
273298 case c of
274299 (Nothing, _) -> return $ unload env (VP Ref n ty) args
275300 (Just v, rest) -> evApply ntimes stk top env rest v
@@ -443,6 +468,9 @@ instance Quote Value where
443468 quote i (VApp f a) = liftM2 App (quote i f) (quote i a)
444469 quote i (VType u) = return $ TType u
445470 quote i VErased = return $ Erased
471+ quote i VImpossible = return $ Impossible
472+ quote i (VProj v j) = do v' <- quote i v
473+ return (Proj v' j)
446474 quote i (VConstant c) = return $ Constant c
447475 quote i (VTmp x) = return $ V (i - x - 1)
448476