@@ -2,7 +2,7 @@
2 PatternGuards #-}
2 PatternGuards #-}
4module Core.Evaluate(normalise, normaliseTrace, normaliseC, normaliseAll,
4module Core.Evaluate(normalise, normaliseTrace, normaliseC, normaliseAll,
5− simplify, specialise, hnf, convEq, convEq',
5+ rt_simplify, simplify, specialise, hnf, convEq, convEq',
6 Def(..), CaseInfo(..), CaseDefs(..),
6 Def(..), CaseInfo(..), CaseDefs(..),
7 Accessibility(..), Totality(..), PReason(..),
7 Accessibility(..), Totality(..), PReason(..),
8 Context, initContext, ctxtAlist, uconstraints, next_tvar,
8 Context, initContext, ctxtAlist, uconstraints, next_tvar,
@@ -45,7 +45,9 @@ data Value = VP NameType Name Value
45 | VApp Value Value
45 | VApp Value Value
46 | VType UExp
46 | VType UExp
48 | VConstant Const
49 | VConstant Const
49-- | VLazy Env [Value] Term
51-- | VLazy Env [Value] Term
50 | VTmp Int
52 | VTmp Int
@@ -104,6 +106,18 @@ simplify ctxt env t
104 [Simplify]
106 [Simplify]
105 quote 0 val) initEval
107 quote 0 val) initEval
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),
115+ (UN "prim__syntactic_eq", 0),
116+ (UN "prim_fork", 0)]
117+ (map finalEntry env) (finalise t)
119+ quote 0 val) initEval
107-- | Reduce a term to head normal form
121-- | Reduce a term to head normal form
108hnf :: Context -> Env -> TT Name -> TT Name
122hnf :: Context -> Env -> TT Name -> TT Name
109hnf ctxt env t
123hnf ctxt env t
@@ -170,6 +184,8 @@ eval traceon ctxt ntimes genv tm opts = ev ntimes [] True [] tm where
170 -- returns 'True' if the function should block
184 -- returns 'True' if the function should block
171 -- normal evaluation should return false
185 -- normal evaluation should return false
172 blockSimplify (CaseInfo inl dict) n stk
186 blockSimplify (CaseInfo inl dict) n stk
188+ = not inl || elem n stk
173 | Simplify `elem` opts
189 | Simplify `elem` opts
174 = (not (inl || dict) || elem n stk)
190 = (not (inl || dict) || elem n stk)
175 || (n == UN "prim__syntactic_eq")
191 || (n == UN "prim__syntactic_eq")
@@ -195,10 +211,11 @@ eval traceon ctxt ntimes genv tm opts = ev ntimes [] True [] tm where
195 [(CaseOp ci _ _ _ cd, acc)]
211 [(CaseOp ci _ _ _ cd, acc)]
196 | acc == Public &&
212 | acc == Public &&
197 null (fst (cases_totcheck cd)) -> -- unoptimised version
213 null (fst (cases_totcheck cd)) -> -- unoptimised version
198− let (_, tree) = getCases cd in
214+ let (ns, tree) = getCases cd in
199 if blockSimplify ci n stk
215 if blockSimplify ci n stk
200 then liftM (VP Ref n) (ev ntimes stk top env ty)
216 then liftM (VP Ref n) (ev ntimes stk top env ty)
217+ else -- traceWhen runtime (show (n, ns, tree)) $
201− else do c <- evCase ntimes n (n:stk) top env [] [] tree
218+ do c <- evCase ntimes n (n:stk) top env ns [] tree
202 case c of
219 case c of
203 (Nothing, _) -> liftM (VP Ref n) (ev ntimes stk top env ty)
220 (Nothing, _) -> liftM (VP Ref n) (ev ntimes stk top env ty)
204 (Just v, _) -> return v
221 (Just v, _) -> return v
@@ -242,8 +259,15 @@ eval traceon ctxt ntimes genv tm opts = ev ntimes [] True [] tm where
242 = do f' <- ev ntimes stk False env f
259 = do f' <- ev ntimes stk False env f
243 a' <- ev ntimes stk False env a
260 a' <- ev ntimes stk False env a
244 evApply ntimes stk top env [a'] f'
261 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
245 ev ntimes stk top env (Constant c) = return $ VConstant c
268 ev ntimes stk top env (Constant c) = return $ VConstant c
246 ev ntimes stk top env Erased = return VErased
269 ev ntimes stk top env Erased = return VErased
270+ ev ntimes stk top env Impossible = return VImpossible
247 ev ntimes stk top env (TType i) = return $ VType i
271 ev ntimes stk top env (TType i) = return $ VType i
249 evApply ntimes stk top env args (VApp f a)
273 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
269 let (ns, tree) = getCases cd in
293 let (ns, tree) = getCases cd in
270 if blockSimplify ci n stk
294 if blockSimplify ci n stk
271 then return $ unload env (VP Ref n ty) args
295 then return $ unload env (VP Ref n ty) args
296+ else -- traceWhen runtime (show (n, ns, tree)) $
272− else do c <- evCase ntimes n (n:stk) top env ns args tree
297+ do c <- evCase ntimes n (n:stk) top env ns args tree
273 case c of
298 case c of
274 (Nothing, _) -> return $ unload env (VP Ref n ty) args
299 (Nothing, _) -> return $ unload env (VP Ref n ty) args
275 (Just v, rest) -> evApply ntimes stk top env rest v
300 (Just v, rest) -> evApply ntimes stk top env rest v
@@ -443,6 +468,9 @@ instance Quote Value where
443 quote i (VApp f a) = liftM2 App (quote i f) (quote i a)
468 quote i (VApp f a) = liftM2 App (quote i f) (quote i a)
444 quote i (VType u) = return $ TType u
469 quote i (VType u) = return $ TType u
445 quote i VErased = return $ Erased
470 quote i VErased = return $ Erased
471+ quote i VImpossible = return $ Impossible
472+ quote i (VProj v j) = do v' <- quote i v
446 quote i (VConstant c) = return $ Constant c
474 quote i (VConstant c) = return $ Constant c
447 quote i (VTmp x) = return $ V (i - x - 1)
475 quote i (VTmp x) = return $ V (i - x - 1)