@@ -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,
@@ -22,6 +22,7 @@ import Core.CaseTree
2222
2323data EvalState = ES { limited :: [(Name, Int)],
2424 nexthole :: Int }
25+ deriving Show
2526
2627type Eval a = State EvalState a
2728
@@ -29,6 +30,7 @@ data EvalOpt = Spec
2930 | HNF
3031 | Simplify
3132 | AtREPL
33+ | RunTT
3234 deriving (Show, Eq)
3335
3436initEval = ES [] 0
@@ -44,7 +46,9 @@ data Value = VP NameType Name Value
4446 | VApp Value Value
4547 | VType UExp
4648 | VErased
49+ | VImpossible
4750 | VConstant Const
51+ | VProj Value Int
4852-- | VLazy Env [Value] Term
4953 | VTmp Int
5054
@@ -103,6 +107,18 @@ simplify ctxt env t
103107 [Simplify]
104108 quote 0 val) initEval
105109
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),
115+ (UN "par", 0),
116+ (UN "prim__syntactic_eq", 0),
117+ (UN "prim_fork", 0)]
118+ (map finalEntry env) (finalise t)
119+ [RunTT]
120+ quote 0 val) initEval
121+
106122-- | Reduce a term to head normal form
107123hnf :: Context -> Env -> TT Name -> TT Name
108124hnf ctxt env t
@@ -162,17 +178,24 @@ eval :: Bool -> Context -> [(Name, Int)] -> Env -> TT Name ->
162178eval traceon ctxt ntimes genv tm opts = ev ntimes [] True [] tm where
163179 spec = Spec `elem` opts
164180 simpl = Simplify `elem` opts
181+ runtime = RunTT `elem` opts
165182 atRepl = AtREPL `elem` opts
166183 hnf = HNF `elem` opts
167184
168185 -- returns 'True' if the function should block
169186 -- normal evaluation should return false
170187 blockSimplify (CaseInfo inl dict) n stk
188+ | RunTT `elem` opts
189+ = not (inl || dict) || elem n stk
171190 | Simplify `elem` opts
172191 = (not (inl || dict) || elem n stk)
173192 || (n == UN "prim__syntactic_eq")
174193 | otherwise = False
175194
195+ getCases cd | simpl = cases_totcheck cd
196+ | runtime = cases_runtime cd
197+ | otherwise = cases_compiletime cd
198+
176199 ev ntimes stk top env (P _ n ty)
177200 | Just (Let t v) <- lookup n genv = ev ntimes stk top env v
178201 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
189212 [(CaseOp ci _ _ _ cd, acc)]
190213 | acc == Public &&
191214 null (fst (cases_totcheck cd)) -> -- unoptimised version
192− let (_, tree) = if simpl then cases_totcheck cd
193− else cases_compiletime cd in
215+ let (ns, tree) = getCases cd in
194216 if blockSimplify ci n stk
195217 then liftM (VP Ref n) (ev ntimes stk top env ty)
196− else do c <- evCase ntimes n (n:stk) top env [] [] tree
218+ else -- traceWhen runtime (show (n, ns, tree)) $
219+ do c <- evCase ntimes n (n:stk) top env ns [] tree
197220 case c of
198221 (Nothing, _) -> liftM (VP Ref n) (ev ntimes stk top env ty)
199222 (Just v, _) -> return v
@@ -237,8 +260,17 @@ eval traceon ctxt ntimes genv tm opts = ev ntimes [] True [] tm where
237260 = do f' <- ev ntimes stk False env f
238261 a' <- ev ntimes stk False env a
239262 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
270+
240271 ev ntimes stk top env (Constant c) = return $ VConstant c
241272 ev ntimes stk top env Erased = return VErased
273+ ev ntimes stk top env Impossible = return VImpossible
242274 ev ntimes stk top env (TType i) = return $ VType i
243275
244276 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
246278 evApply ntimes stk top env args f
247279 = apply ntimes stk top env f args
248280
281+ reapply ntimes stk top env f@(VP Ref n ty) args
282+ = let val = lookupDefAcc n atRepl ctxt in
283+ case val of
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
287+ case c of
288+ (Nothing, _) -> return $ unload env (VP Ref n ty) args
289+ (Just v, rest) -> evApply ntimes stk top env rest v
290+ _ -> case args of
291+ (a : as) -> return $ unload env f (a : as)
292+ [] -> return f
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
296+
249297 apply ntimes stk top env (VBind True n (Lam t) sc) (a:as)
250298 = do a' <- sc a
251299 app <- apply ntimes stk top env a' as
@@ -261,11 +309,11 @@ eval traceon ctxt ntimes genv tm opts = ev ntimes [] True [] tm where
261309 case val of
262310 [(CaseOp ci _ _ _ cd, acc)]
263311 | acc == Public -> -- unoptimised version
264− let (ns, tree) = if simpl then cases_totcheck cd
265− else cases_compiletime cd in
312+ let (ns, tree) = getCases cd in
266313 if blockSimplify ci n stk
267314 then return $ unload env (VP Ref n ty) args
268− else do c <- evCase ntimes n (n:stk) top env ns args tree
315+ else -- traceWhen runtime (show (n, ns, tree)) $
316+ do c <- evCase ntimes n (n:stk) top env ns args tree
269317 case c of
270318 (Nothing, _) -> return $ unload env (VP Ref n ty) args
271319 (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
315363 etm' <- ev ntimes stk (not (conHeaded tm))
316364 (map snd amap ++ env) etm
317365 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
318369 evTree ntimes stk top env amap (Case n alts)
319370 = case lookup n amap of
320− Just v -> do c <- chooseAlt env v (getValArgs v) alts amap
321− case c of
322− Just (altmap, sc) -> evTree ntimes stk top env altmap sc
323− _ -> do c' <- chooseAlt' ntimes stk env v (getValArgs v) alts amap
324− case c' of
325− Just (altmap, sc) -> evTree ntimes stk top env altmap sc
326− _ -> return Nothing
371+ Just v -> doCase ntimes stk top env amap v alts
327372 _ -> return Nothing
328373 evTree ntimes stk top env amap ImpossibleCase = return Nothing
329374
375+ doCase ntimes stk top env amap v alts =
376+ do c <- chooseAlt env v (getValArgs v) alts amap
377+ case c of
378+ Just (altmap, sc) -> evTree ntimes stk top env altmap sc
379+ _ -> do c' <- chooseAlt' ntimes stk env v (getValArgs v) alts amap
380+ case c' of
381+ Just (altmap, sc) -> evTree ntimes stk top env altmap sc
382+ _ -> return Nothing
383+
330384 conHeaded tm@(App _ _)
331385 | (P (DCon _ _) _ _, args) <- unApply tm = True
332386 conHeaded t = False
@@ -439,6 +493,9 @@ instance Quote Value where
439493 quote i (VApp f a) = liftM2 App (quote i f) (quote i a)
440494 quote i (VType u) = return $ TType u
441495 quote i VErased = return $ Erased
496+ quote i VImpossible = return $ Impossible
497+ quote i (VProj v j) = do v' <- quote i v
498+ return (Proj v' j)
442499 quote i (VConstant c) = return $ Constant c
443500 quote i (VTmp x) = return $ V (i - x - 1)
444501