@@ -3,7 +3,7 @@
4module Core.Evaluate(normalise, normaliseTrace, normaliseC, normaliseAll,
4module Core.Evaluate(normalise, normaliseTrace, normaliseC, normaliseAll,
5 simplify, specialise, hnf, convEq, convEq',
5 simplify, specialise, hnf, convEq, convEq',
6− Def(..), CaseInfo(..),
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,
9 addToCtxt, setAccess, setTotal, addCtxtDef, addTyDecl,
9 addToCtxt, setAccess, setTotal, addCtxtDef, addTyDecl,
@@ -186,9 +186,12 @@ eval traceon ctxt ntimes genv tm opts = ev ntimes [] True [] tm where
186 ev ntimes (n:stk) True env tm
186 ev ntimes (n:stk) True env tm
187 [(TyDecl nt ty, _)] -> do vty <- ev ntimes stk True env ty
187 [(TyDecl nt ty, _)] -> do vty <- ev ntimes stk True env ty
188 return $ VP nt n vty
188 return $ VP nt n vty
189− [(CaseOp ci _ _ _ [] tree _ _, acc)]
189+ [(CaseOp ci _ _ _ cd, acc)]
190− | acc == Public -> -- unoptimised version
191+ null (fst (cases_totcheck cd)) -> -- unoptimised version
192+ let (_, tree) = if simpl then cases_totcheck cd
193+ else cases_compiletime cd in
191− if blockSimplify ci n stk
194+ if blockSimplify ci n stk
192 then liftM (VP Ref n) (ev ntimes stk top env ty)
195 then liftM (VP Ref n) (ev ntimes stk top env ty)
193 else do c <- evCase ntimes n (n:stk) top env [] [] tree
196 else do c <- evCase ntimes n (n:stk) top env [] [] tree
194 case c of
197 case c of
@@ -256,9 +259,11 @@ eval traceon ctxt ntimes genv tm opts = ev ntimes [] True [] tm where
256 if u then
259 if u then
257 do let val = lookupDefAcc n atRepl ctxt
260 do let val = lookupDefAcc n atRepl ctxt
258 case val of
261 case val of
259− [(CaseOp ci _ _ _ ns tree _ _, acc)]
262+ [(CaseOp ci _ _ _ cd, acc)]
260 | acc == Public -> -- unoptimised version
263 | acc == Public -> -- unoptimised version
264+ let (ns, tree) = if simpl then cases_totcheck cd
265+ else cases_compiletime cd in
261− if blockSimplify ci n stk
266+ if blockSimplify ci n stk
262 then return $ unload env (VP Ref n ty) args
267 then return $ unload env (VP Ref n ty) args
263 else do c <- evCase ntimes n (n:stk) top env ns args tree
268 else do c <- evCase ntimes n (n:stk) top env ns args tree
264 case c of
269 case c of
@@ -488,9 +493,11 @@ convEq ctxt = ceq [] where
488 sameDefs ps x y = case (lookupDef x ctxt, lookupDef y ctxt) of
493 sameDefs ps x y = case (lookupDef x ctxt, lookupDef y ctxt) of
489 ([Function _ xdef], [Function _ ydef])
494 ([Function _ xdef], [Function _ ydef])
490 -> ceq ((x,y):ps) xdef ydef
495 -> ceq ((x,y):ps) xdef ydef
491− ([CaseOp _ _ _ _ _ xdef _ _],
496+ ([CaseOp _ _ _ _ xd],
492− [CaseOp _ _ _ _ _ ydef _ _])
497+ [CaseOp _ _ _ _ yd])
498+ -> let (_, xdef) = cases_compiletime xd
499+ (_, ydef) = cases_compiletime yd in
493− -> caseeq ((x,y):ps) xdef ydef
500+ caseeq ((x,y):ps) xdef ydef
494 _ -> return False
501 _ -> return False
496-- SPECIALISATION -----------------------------------------------------------
503-- SPECIALISATION -----------------------------------------------------------
@@ -514,9 +521,16 @@ data Def = Function Type Term
514 | CaseOp CaseInfo
521 | CaseOp CaseInfo
516 [Either Term (Term, Term)] -- original definition
523 [Either Term (Term, Term)] -- original definition
517− [([Name], Term, Term)] -- simplified definition
524+ [([Name], Term, Term)] -- simplified for totality check definition
518− [Name] SC -- Compile time case definition
526+-- [Name] SC -- Compile time case definition
519− [Name] SC -- Run time cae definitions
527+-- [Name] SC -- Run time cae definitions
529+data CaseDefs = CaseDefs {
530+ cases_totcheck :: ([Name], SC),
531+ cases_compiletime :: ([Name], SC),
532+ cases_runtime :: ([Name], SC)
521data CaseInfo = CaseInfo {
535data CaseInfo = CaseInfo {
522 case_inlinable :: Bool,
536 case_inlinable :: Bool,
@@ -526,13 +540,21 @@ data CaseInfo = CaseInfo {
527deriving instance Binary Def
541deriving instance Binary Def
544+deriving instance Binary CaseInfo
547+deriving instance Binary CaseDefs
530instance Show Def where
550instance Show Def where
531 show (Function ty tm) = "Function: " ++ show (ty, tm)
551 show (Function ty tm) = "Function: " ++ show (ty, tm)
532 show (TyDecl nt ty) = "TyDecl: " ++ show nt ++ " " ++ show ty
552 show (TyDecl nt ty) = "TyDecl: " ++ show nt ++ " " ++ show ty
533 show (Operator ty _ _) = "Operator: " ++ show ty
553 show (Operator ty _ _) = "Operator: " ++ show ty
534− show (CaseOp (CaseInfo inlc inlr) ty ps_in ps ns sc ns' sc')
554+ show (CaseOp (CaseInfo inlc inlr) ty ps_in ps cd)
555+ = let (ns, sc) = cases_compiletime cd
556+ (ns', sc') = cases_runtime cd in
535− = "Case: " ++ show ty ++ " " ++ show ps ++ "\n" ++
557+ "Case: " ++ show ty ++ " " ++ show ps ++ "\n" ++
536 show ns ++ " " ++ show sc ++ "\n" ++
558 show ns ++ " " ++ show sc ++ "\n" ++
537 show ns' ++ " " ++ show sc' ++ "\n" ++
559 show ns' ++ " " ++ show sc' ++ "\n" ++
538 if inlc then "Inlinable\n" else "Not inlinable\n"
560 if inlc then "Inlinable\n" else "Not inlinable\n"
@@ -666,9 +688,10 @@ addCasedef n ci@(CaseInfo alwaysInline tcdict)
666 (OK (CaseDef args sc _), OK (CaseDef args' sc' _)) ->
688 (OK (CaseDef args sc _), OK (CaseDef args' sc' _)) ->
667 let inl = alwaysInline -- || tcdict
689 let inl = alwaysInline -- || tcdict
668 inlc = (inl || small n args sc') && (not asserted)
690 inlc = (inl || small n args sc') && (not asserted)
669− inlr = inl || small n args sc' in
691+ inlr = inl || small n args sc'
692+ cdef = CaseDefs (args, sc) (args, sc) (args', sc') in
670 addDef n (CaseOp (ci { case_inlinable = inlc })
693 addDef n (CaseOp (ci { case_inlinable = inlc })
671− ty ps_in ps args sc args' sc',
694+ ty ps_in ps cdef,
672 access, Unchecked) ctxt in
695 access, Unchecked) ctxt in
673 uctxt { definitions = ctxt' }
696 uctxt { definitions = ctxt' }
@@ -676,15 +699,15 @@ simplifyCasedef :: Name -> Context -> Context
676simplifyCasedef n uctxt
699simplifyCasedef n uctxt
677 = let ctxt = definitions uctxt
700 = let ctxt = definitions uctxt
678 ctxt' = case lookupCtxt n ctxt of
701 ctxt' = case lookupCtxt n ctxt of
679− [(CaseOp ci ty [] ps args sc args' sc', acc, tot)] ->
702+ [(CaseOp ci ty [] ps _, acc, tot)] ->
680 ctxt -- nothing to simplify (or already done...)
703 ctxt -- nothing to simplify (or already done...)
681− [(CaseOp ci ty ps_in ps args sc args' sc', acc, tot)] ->
704+ [(CaseOp ci ty ps_in ps cd, acc, tot)] ->
682 let ps_in' = map simpl ps_in
705 let ps_in' = map simpl ps_in
683 pdef = map debind ps_in' in
706 pdef = map debind ps_in' in
684 case simpleCase False True False CompileTime (FC "" 0) pdef of
707 case simpleCase False True False CompileTime (FC "" 0) pdef of
685 OK (CaseDef args sc _) ->
708 OK (CaseDef args sc _) ->
686 addDef n (CaseOp ci
709 addDef n (CaseOp ci
687− ty ps_in' ps args sc args' sc',
710+ ty ps_in' ps (cd { cases_totcheck = (args, sc) }),
688 acc, tot) ctxt
711 acc, tot) ctxt
689 Error err -> error (show err)
712 Error err -> error (show err)
690 _ -> ctxt in
713 _ -> ctxt in
@@ -722,7 +745,7 @@ lookupTy n ctxt
722 (Function ty _) -> return ty
745 (Function ty _) -> return ty
723 (TyDecl _ ty) -> return ty
746 (TyDecl _ ty) -> return ty
724 (Operator ty _ _) -> return ty
747 (Operator ty _ _) -> return ty
725− (CaseOp _ ty _ _ _ _ _ _) -> return ty
748+ (CaseOp _ ty _ _ _) -> return ty
727isConName :: Name -> Context -> Bool
750isConName :: Name -> Context -> Bool
728isConName n ctxt
751isConName n ctxt
@@ -738,7 +761,7 @@ isFnName n ctxt
738 case tfst def of
761 case tfst def of
739 (Function _ _) -> return True
762 (Function _ _) -> return True
740 (Operator _ _ _) -> return True
763 (Operator _ _ _) -> return True
741− (CaseOp _ _ _ _ _ _ _ _) -> return True
764+ (CaseOp _ _ _ _ _) -> return True
742 _ -> return False
765 _ -> return False
744lookupP :: Name -> Context -> [Term]
767lookupP :: Name -> Context -> [Term]
@@ -747,7 +770,7 @@ lookupP n ctxt
747 p <- case def of
770 p <- case def of
748 (Function ty tm, a, _) -> return (P Ref n ty, a)
771 (Function ty tm, a, _) -> return (P Ref n ty, a)
749 (TyDecl nt ty, a, _) -> return (P nt n ty, a)
772 (TyDecl nt ty, a, _) -> return (P nt n ty, a)
750− (CaseOp _ ty _ _ _ _ _ _, a, _) -> return (P Ref n ty, a)
773+ (CaseOp _ ty _ _ _, a, _) -> return (P Ref n ty, a)
751 (Operator ty _ _, a, _) -> return (P Ref n ty, a)
774 (Operator ty _ _, a, _) -> return (P Ref n ty, a)
752 case snd p of
775 case snd p of
753 Hidden -> []
776 Hidden -> []