@@ -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,
@@ -22,6 +22,7 @@ import Core.CaseTree
23data EvalState = ES { limited :: [(Name, Int)],
23data EvalState = ES { limited :: [(Name, Int)],
24 nexthole :: Int }
24 nexthole :: Int }
26type Eval a = State EvalState a
27type Eval a = State EvalState a
@@ -29,6 +30,7 @@ data EvalOpt = Spec
30 | Simplify
31 | Simplify
32 deriving (Show, Eq)
34 deriving (Show, Eq)
34initEval = ES [] 0
36initEval = ES [] 0
@@ -44,7 +46,9 @@ data Value = VP NameType Name Value
44 | VApp Value Value
46 | VApp Value Value
45 | VType UExp
47 | VType UExp
47 | VConstant Const
50 | VConstant Const
48-- | VLazy Env [Value] Term
52-- | VLazy Env [Value] Term
49 | VTmp Int
53 | VTmp Int
@@ -103,6 +107,18 @@ simplify ctxt env t
103 [Simplify]
107 [Simplify]
104 quote 0 val) initEval
108 quote 0 val) initEval
110+-- | Simplify for run-time (i.e. basic inlining)
111+rt_simplify :: Context -> Env -> TT Name -> TT Name
112+rt_simplify ctxt env t
113+ = evalState (do val <- eval False ctxt [(UN "lazy", 0),
114+ (UN "assert_smaller", 0),
116+ (UN "prim__syntactic_eq", 0),
117+ (UN "prim_fork", 0)]
118+ (map finalEntry env) (finalise t)
120+ quote 0 val) initEval
106-- | Reduce a term to head normal form
122-- | Reduce a term to head normal form
107hnf :: Context -> Env -> TT Name -> TT Name
123hnf :: Context -> Env -> TT Name -> TT Name
108hnf ctxt env t
124hnf ctxt env t
@@ -162,17 +178,24 @@ eval :: Bool -> Context -> [(Name, Int)] -> Env -> TT Name ->
162eval traceon ctxt ntimes genv tm opts = ev ntimes [] True [] tm where
178eval traceon ctxt ntimes genv tm opts = ev ntimes [] True [] tm where
163 spec = Spec `elem` opts
179 spec = Spec `elem` opts
164 simpl = Simplify `elem` opts
180 simpl = Simplify `elem` opts
181+ runtime = RunTT `elem` opts
165 atRepl = AtREPL `elem` opts
182 atRepl = AtREPL `elem` opts
166 hnf = HNF `elem` opts
183 hnf = HNF `elem` opts
168 -- returns 'True' if the function should block
185 -- returns 'True' if the function should block
169 -- normal evaluation should return false
186 -- normal evaluation should return false
170 blockSimplify (CaseInfo inl dict) n stk
187 blockSimplify (CaseInfo inl dict) n stk
189+ = not (inl || dict) || elem n stk
171 | Simplify `elem` opts
190 | Simplify `elem` opts
172 = (not (inl || dict) || elem n stk)
191 = (not (inl || dict) || elem n stk)
173 || (n == UN "prim__syntactic_eq")
192 || (n == UN "prim__syntactic_eq")
174 | otherwise = False
193 | otherwise = False
195+ getCases cd | simpl = cases_totcheck cd
196+ | runtime = cases_runtime cd
197+ | otherwise = cases_compiletime cd
176 ev ntimes stk top env (P _ n ty)
199 ev ntimes stk top env (P _ n ty)
177 | Just (Let t v) <- lookup n genv = ev ntimes stk top env v
200 | Just (Let t v) <- lookup n genv = ev ntimes stk top env v
178 ev ntimes_in stk top env (P Ref n ty)
201 ev ntimes_in stk top env (P Ref n ty)
@@ -189,11 +212,11 @@ eval traceon ctxt ntimes genv tm opts = ev ntimes [] True [] tm where
189 [(CaseOp ci _ _ _ cd, acc)]
212 [(CaseOp ci _ _ _ cd, acc)]
190 | acc == Public &&
213 | acc == Public &&
191 null (fst (cases_totcheck cd)) -> -- unoptimised version
214 null (fst (cases_totcheck cd)) -> -- unoptimised version
192− let (_, tree) = if simpl then cases_totcheck cd
215+ let (ns, tree) = getCases cd in
193− else cases_compiletime cd in
194 if blockSimplify ci n stk
216 if blockSimplify ci n stk
195 then liftM (VP Ref n) (ev ntimes stk top env ty)
217 then liftM (VP Ref n) (ev ntimes stk top env ty)
218+ else -- traceWhen runtime (show (n, ns, tree)) $
196− else do c <- evCase ntimes n (n:stk) top env [] [] tree
219+ do c <- evCase ntimes n (n:stk) top env ns [] tree
197 case c of
220 case c of
198 (Nothing, _) -> liftM (VP Ref n) (ev ntimes stk top env ty)
221 (Nothing, _) -> liftM (VP Ref n) (ev ntimes stk top env ty)
199 (Just v, _) -> return v
222 (Just v, _) -> return v
@@ -237,8 +260,17 @@ eval traceon ctxt ntimes genv tm opts = ev ntimes [] True [] tm where
237 = do f' <- ev ntimes stk False env f
260 = do f' <- ev ntimes stk False env f
238 a' <- ev ntimes stk False env a
261 a' <- ev ntimes stk False env a
239 evApply ntimes stk top env [a'] f'
262 evApply ntimes stk top env [a'] f'
263+ ev ntimes stk top env (Proj t i)
264+ = do -- evaluate dictionaries if it means the projection works
265+ t' <- ev ntimes stk top env t
266+-- tfull' <- reapply ntimes stk top env t' []
267+ return (doProj t' (getValArgs t'))
268+ where doProj t' (VP (DCon _ _) _ _, args) | i < length args = args!!i
269+ doProj t' _ = VProj t' i
240 ev ntimes stk top env (Constant c) = return $ VConstant c
271 ev ntimes stk top env (Constant c) = return $ VConstant c
241 ev ntimes stk top env Erased = return VErased
272 ev ntimes stk top env Erased = return VErased
273+ ev ntimes stk top env Impossible = return VImpossible
242 ev ntimes stk top env (TType i) = return $ VType i
274 ev ntimes stk top env (TType i) = return $ VType i
244 evApply ntimes stk top env args (VApp f a)
276 evApply ntimes stk top env args (VApp f a)
@@ -246,6 +278,22 @@ eval traceon ctxt ntimes genv tm opts = ev ntimes [] True [] tm where
246 evApply ntimes stk top env args f
278 evApply ntimes stk top env args f
247 = apply ntimes stk top env f args
279 = apply ntimes stk top env f args
281+ reapply ntimes stk top env f@(VP Ref n ty) args
282+ = let val = lookupDefAcc n atRepl ctxt in
284+ [(CaseOp ci _ _ _ cd, acc)] ->
285+ let (ns, tree) = getCases cd in
286+ do c <- evCase ntimes n (n:stk) top env ns args tree
288+ (Nothing, _) -> return $ unload env (VP Ref n ty) args
289+ (Just v, rest) -> evApply ntimes stk top env rest v
291+ (a : as) -> return $ unload env f (a : as)
293+ reapply ntimes stk top env (VApp f a) args
294+ = reapply ntimes stk top env f (a : args)
295+ reapply ntimes stk top env v args = return v
249 apply ntimes stk top env (VBind True n (Lam t) sc) (a:as)
297 apply ntimes stk top env (VBind True n (Lam t) sc) (a:as)
250 = do a' <- sc a
298 = do a' <- sc a
251 app <- apply ntimes stk top env a' as
299 app <- apply ntimes stk top env a' as
@@ -261,11 +309,11 @@ eval traceon ctxt ntimes genv tm opts = ev ntimes [] True [] tm where
261 case val of
309 case val of
262 [(CaseOp ci _ _ _ cd, acc)]
310 [(CaseOp ci _ _ _ cd, acc)]
263 | acc == Public -> -- unoptimised version
311 | acc == Public -> -- unoptimised version
264− let (ns, tree) = if simpl then cases_totcheck cd
312+ let (ns, tree) = getCases cd in
265− else cases_compiletime cd in
266 if blockSimplify ci n stk
313 if blockSimplify ci n stk
267 then return $ unload env (VP Ref n ty) args
314 then return $ unload env (VP Ref n ty) args
315+ else -- traceWhen runtime (show (n, ns, tree)) $
268− else do c <- evCase ntimes n (n:stk) top env ns args tree
316+ do c <- evCase ntimes n (n:stk) top env ns args tree
269 case c of
317 case c of
270 (Nothing, _) -> return $ unload env (VP Ref n ty) args
318 (Nothing, _) -> return $ unload env (VP Ref n ty) args
271 (Just v, rest) -> evApply ntimes stk top env rest v
319 (Just v, rest) -> evApply ntimes stk top env rest v
@@ -315,18 +363,24 @@ eval traceon ctxt ntimes genv tm opts = ev ntimes [] True [] tm where
315 etm' <- ev ntimes stk (not (conHeaded tm))
363 etm' <- ev ntimes stk (not (conHeaded tm))
316 (map snd amap ++ env) etm
364 (map snd amap ++ env) etm
317 return $ Just etm'
365 return $ Just etm'
366+ evTree ntimes stk top env amap (ProjCase t alts)
367+ = do t' <- ev ntimes stk top env t
368+ doCase ntimes stk top env amap t' alts
318 evTree ntimes stk top env amap (Case n alts)
369 evTree ntimes stk top env amap (Case n alts)
319 = case lookup n amap of
370 = case lookup n amap of
320− Just v -> do c <- chooseAlt env v (getValArgs v) alts amap
371+ Just v -> doCase ntimes stk top env amap v alts
322− Just (altmap, sc) -> evTree ntimes stk top env altmap sc
323− _ -> do c' <- chooseAlt' ntimes stk env v (getValArgs v) alts amap
325− Just (altmap, sc) -> evTree ntimes stk top env altmap sc
327 _ -> return Nothing
372 _ -> return Nothing
328 evTree ntimes stk top env amap ImpossibleCase = return Nothing
373 evTree ntimes stk top env amap ImpossibleCase = return Nothing
375+ doCase ntimes stk top env amap v alts =
376+ do c <- chooseAlt env v (getValArgs v) alts amap
378+ Just (altmap, sc) -> evTree ntimes stk top env altmap sc
379+ _ -> do c' <- chooseAlt' ntimes stk env v (getValArgs v) alts amap
381+ Just (altmap, sc) -> evTree ntimes stk top env altmap sc
330 conHeaded tm@(App _ _)
384 conHeaded tm@(App _ _)
331 | (P (DCon _ _) _ _, args) <- unApply tm = True
385 | (P (DCon _ _) _ _, args) <- unApply tm = True
332 conHeaded t = False
386 conHeaded t = False
@@ -439,6 +493,9 @@ instance Quote Value where
439 quote i (VApp f a) = liftM2 App (quote i f) (quote i a)
493 quote i (VApp f a) = liftM2 App (quote i f) (quote i a)
440 quote i (VType u) = return $ TType u
494 quote i (VType u) = return $ TType u
441 quote i VErased = return $ Erased
495 quote i VErased = return $ Erased
496+ quote i VImpossible = return $ Impossible
497+ quote i (VProj v j) = do v' <- quote i v
442 quote i (VConstant c) = return $ Constant c
499 quote i (VConstant c) = return $ Constant c
443 quote i (VTmp x) = return $ V (i - x - 1)
500 quote i (VTmp x) = return $ V (i - x - 1)