@@ -3,7 +3,8 @@
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(..), Accessibility(..), Totality(..), PReason(..),
7+ Accessibility(..), Totality(..), PReason(..),
7 Context, initContext, ctxtAlist, uconstraints, next_tvar,
8 Context, initContext, ctxtAlist, uconstraints, next_tvar,
8 addToCtxt, setAccess, setTotal, addCtxtDef, addTyDecl,
9 addToCtxt, setAccess, setTotal, addCtxtDef, addTyDecl,
9 addDatatype, addCasedef, simplifyCasedef, addOperator,
10 addDatatype, addCasedef, simplifyCasedef, addOperator,
@@ -24,7 +25,11 @@ data EvalState = ES { limited :: [(Name, Int)],
25type Eval a = State EvalState a
26type Eval a = State EvalState a
27−data EvalOpt = Spec | HNF | Simplify Bool | AtREPL
28+data EvalOpt = Spec
30+ | Simplify Bool -- simplifying - if True, we're doing it to help
31+ -- totality checking so reduce dictionaries too
28 deriving (Show, Eq)
33 deriving (Show, Eq)
30initEval = ES [] 0
35initEval = ES [] 0
@@ -95,14 +100,14 @@ specialise ctxt env limits t
95-- | Like normalise, but we only reduce functions that are marked as okay to
100-- | Like normalise, but we only reduce functions that are marked as okay to
96-- inline (and probably shouldn't reduce lets?)
101-- inline (and probably shouldn't reduce lets?)
97simplify :: Context -> Bool -> Env -> TT Name -> TT Name
102simplify :: Context -> Bool -> Env -> TT Name -> TT Name
98−simplify ctxt runtime env t
103+simplify ctxt alldicts env t
99 = evalState (do val <- eval False ctxt [(UN "lazy", 0),
104 = evalState (do val <- eval False ctxt [(UN "lazy", 0),
100 (UN "assert_smaller", 0),
105 (UN "assert_smaller", 0),
101 (UN "par", 0),
106 (UN "par", 0),
102 (UN "prim__syntactic_eq", 0),
107 (UN "prim__syntactic_eq", 0),
103 (UN "fork", 0)]
108 (UN "fork", 0)]
104 (map finalEntry env) (finalise t)
109 (map finalEntry env) (finalise t)
105− [Simplify runtime]
110+ [Simplify alldicts]
106 quote 0 val) initEval
111 quote 0 val) initEval
108-- | Reduce a term to head normal form
113-- | Reduce a term to head normal form
@@ -168,9 +173,11 @@ eval traceon ctxt ntimes genv tm opts = ev ntimes [] True [] tm where
168 atRepl = AtREPL `elem` opts
173 atRepl = AtREPL `elem` opts
169 hnf = HNF `elem` opts
174 hnf = HNF `elem` opts
171− canSimplify inl inr n stk
176+ -- returns 'True' if the function should block
177+ -- normal evaluation should return false
178+ blockSimplify (CaseInfo inl dict) n stk
172− = (Simplify False `elem` opts && (not inl || elem n stk))
179+ = (Simplify False `elem` opts && (not (inl || dict) || elem n stk))
173− || (Simplify True `elem` opts && (not inr || elem n stk))
180+ || (Simplify True `elem` opts && (not inl || elem n stk))
175 ev ntimes stk top env (P _ n ty)
182 ev ntimes stk top env (P _ n ty)
176 | Just (Let t v) <- lookup n genv = ev ntimes stk top env v
183 | Just (Let t v) <- lookup n genv = ev ntimes stk top env v
@@ -185,9 +192,9 @@ eval traceon ctxt ntimes genv tm opts = ev ntimes [] True [] tm where
185 ev ntimes (n:stk) True env tm
192 ev ntimes (n:stk) True env tm
186 [(TyDecl nt ty, _)] -> do vty <- ev ntimes stk True env ty
193 [(TyDecl nt ty, _)] -> do vty <- ev ntimes stk True env ty
187 return $ VP nt n vty
194 return $ VP nt n vty
188− [(CaseOp inl inr _ _ _ [] tree _ _, acc)]
195+ [(CaseOp ci _ _ _ [] tree _ _, acc)]
189 | acc == Public || simpl -> -- unoptimised version
196 | acc == Public || simpl -> -- unoptimised version
190− if canSimplify inl inr n stk
197+ if blockSimplify ci n stk
191 then liftM (VP Ref n) (ev ntimes stk top env ty)
198 then liftM (VP Ref n) (ev ntimes stk top env ty)
192 else do c <- evCase ntimes n (n:stk) top env [] [] tree
199 else do c <- evCase ntimes n (n:stk) top env [] [] tree
193 case c of
200 case c of
@@ -260,9 +267,9 @@ eval traceon ctxt ntimes genv tm opts = ev ntimes [] True [] tm where
260 if u then
267 if u then
261 do let val = lookupDefAcc n atRepl ctxt
268 do let val = lookupDefAcc n atRepl ctxt
262 case val of
269 case val of
263− [(CaseOp inl inr _ _ _ ns tree _ _, acc)]
270+ [(CaseOp ci _ _ _ ns tree _ _, acc)]
264 | acc == Public -> -- unoptimised version
271 | acc == Public -> -- unoptimised version
265− if canSimplify inl inr n stk
272+ if blockSimplify ci n stk
266 then return $ unload env (VP Ref n ty) args
273 then return $ unload env (VP Ref n ty) args
267 else do c <- evCase ntimes n (n:stk) top env ns args tree
274 else do c <- evCase ntimes n (n:stk) top env ns args tree
268 case c of
275 case c of
@@ -470,7 +477,7 @@ eval_hnf ctxt statics genv tm = ev [] tm where
470 ev env (P Ref n ty) = case lookupDef n ctxt of
477 ev env (P Ref n ty) = case lookupDef n ctxt of
471 [Function _ t] -> ev env t
478 [Function _ t] -> ev env t
472 [TyDecl nt ty] -> return $ HP nt n ty
479 [TyDecl nt ty] -> return $ HP nt n ty
473− [CaseOp inl _ _ _ _ [] tree _ _] ->
480+ [CaseOp _ _ _ _ [] tree _ _] ->
474 do c <- evCase env [] [] tree
481 do c <- evCase env [] [] tree
475 case c of
482 case c of
476 (Nothing, _, _) -> return $ HP Ref n ty
483 (Nothing, _, _) -> return $ HP Ref n ty
@@ -500,7 +507,7 @@ eval_hnf ctxt statics genv tm = ev [] tm where
500 app <- apply env sc' as
507 app <- apply env sc' as
501 wknH (-1) app
508 wknH (-1) app
502 apply env (HP Ref n ty) args
509 apply env (HP Ref n ty) args
503− | [CaseOp _ _ _ _ _ ns tree _ _] <- lookupDef n ctxt
510+ | [CaseOp _ _ _ _ ns tree _ _] <- lookupDef n ctxt
504 = do c <- evCase env ns args tree
511 = do c <- evCase env ns args tree
505 case c of
512 case c of
506 (Nothing, _, env') -> return $ unload env' (HP Ref n ty) args
513 (Nothing, _, env') -> return $ unload env' (HP Ref n ty) args
@@ -638,8 +645,8 @@ convEq ctxt = ceq [] where
638 sameDefs ps x y = case (lookupDef x ctxt, lookupDef y ctxt) of
645 sameDefs ps x y = case (lookupDef x ctxt, lookupDef y ctxt) of
639 ([Function _ xdef], [Function _ ydef])
646 ([Function _ xdef], [Function _ ydef])
640 -> ceq ((x,y):ps) xdef ydef
647 -> ceq ((x,y):ps) xdef ydef
641− ([CaseOp _ _ _ _ _ _ xdef _ _],
648+ ([CaseOp _ _ _ _ _ xdef _ _],
642− [CaseOp _ _ _ _ _ _ ydef _ _])
649+ [CaseOp _ _ _ _ _ ydef _ _])
643 -> caseeq ((x,y):ps) xdef ydef
650 -> caseeq ((x,y):ps) xdef ydef
644 _ -> return False
651 _ -> return False
@@ -661,12 +668,18 @@ spec ctxt statics genv tm = error "spec undefined"
661data Def = Function Type Term
668data Def = Function Type Term
662 | TyDecl NameType Type
669 | TyDecl NameType Type
663 | Operator Type Int ([Value] -> Maybe Value)
670 | Operator Type Int ([Value] -> Maybe Value)
664− | CaseOp Bool Bool -- compile-time/run-time inlinable
671+ | CaseOp CaseInfo
666 [Either Term (Term, Term)] -- original definition
673 [Either Term (Term, Term)] -- original definition
667 [([Name], Term, Term)] -- simplified definition
674 [([Name], Term, Term)] -- simplified definition
668 [Name] SC -- Compile time case definition
675 [Name] SC -- Compile time case definition
669 [Name] SC -- Run time cae definitions
676 [Name] SC -- Run time cae definitions
678+data CaseInfo = CaseInfo {
679+ case_inlinable :: Bool,
680+ tc_dictionary :: Bool
671deriving instance Binary Def
684deriving instance Binary Def
@@ -675,7 +688,7 @@ instance Show Def where
675 show (Function ty tm) = "Function: " ++ show (ty, tm)
688 show (Function ty tm) = "Function: " ++ show (ty, tm)
676 show (TyDecl nt ty) = "TyDecl: " ++ show nt ++ " " ++ show ty
689 show (TyDecl nt ty) = "TyDecl: " ++ show nt ++ " " ++ show ty
677 show (Operator ty _ _) = "Operator: " ++ show ty
690 show (Operator ty _ _) = "Operator: " ++ show ty
678− show (CaseOp inlc inlr ty ps_in ps ns sc ns' sc')
691+ show (CaseOp (CaseInfo inlc inlr) ty ps_in ps ns sc ns' sc')
679 = "Case: " ++ show ty ++ " " ++ show ps ++ "\n" ++
692 = "Case: " ++ show ty ++ " " ++ show ps ++ "\n" ++
680 show ns ++ " " ++ show sc ++ "\n" ++
693 show ns ++ " " ++ show sc ++ "\n" ++
681 show ns' ++ " " ++ show sc' ++ "\n" ++
694 show ns' ++ " " ++ show sc' ++ "\n" ++
@@ -793,12 +806,14 @@ addDatatype (Data n tag ty cons) uctxt
793 addCons (tag+1) cons (addDef n
806 addCons (tag+1) cons (addDef n
794 (TyDecl (DCon tag (arity ty')) ty, Public, Unchecked) ctxt)
807 (TyDecl (DCon tag (arity ty')) ty, Public, Unchecked) ctxt)
809+-- FIXME: Too many arguments! Refactor all these Bools.
796−addCasedef :: Name -> Bool -> Bool -> Bool -> Bool -> Bool ->
810+addCasedef :: Name -> CaseInfo -> Bool -> Bool -> Bool -> Bool ->
797 [Either Term (Term, Term)] ->
811 [Either Term (Term, Term)] ->
798 [([Name], Term, Term)] ->
812 [([Name], Term, Term)] ->
799 [([Name], Term, Term)] ->
813 [([Name], Term, Term)] ->
800 Type -> Context -> Context
814 Type -> Context -> Context
815+addCasedef n ci@(CaseInfo alwaysInline tcdict)
801−addCasedef n alwaysInline tcase covering reflect asserted ps_in ps psrt ty uctxt
816+ tcase covering reflect asserted ps_in ps psrt ty uctxt
802 = let ctxt = definitions uctxt
817 = let ctxt = definitions uctxt
803 access = case lookupDefAcc n False uctxt of
818 access = case lookupDefAcc n False uctxt of
804 [(_, acc)] -> acc
819 [(_, acc)] -> acc
@@ -806,10 +821,10 @@ addCasedef n alwaysInline tcase covering reflect asserted ps_in ps psrt ty uctxt
806 ctxt' = case (simpleCase tcase covering reflect CompileTime (FC "" 0) ps,
821 ctxt' = case (simpleCase tcase covering reflect CompileTime (FC "" 0) ps,
807 simpleCase tcase covering reflect RunTime (FC "" 0) psrt) of
822 simpleCase tcase covering reflect RunTime (FC "" 0) psrt) of
808 (OK (CaseDef args sc _), OK (CaseDef args' sc' _)) ->
823 (OK (CaseDef args sc _), OK (CaseDef args' sc' _)) ->
809− let inl = alwaysInline
824+ let inl = alwaysInline || tcdict
810 inlc = (inl || small n args sc') && (not asserted)
825 inlc = (inl || small n args sc') && (not asserted)
811 inlr = inl || small n args sc' in
826 inlr = inl || small n args sc' in
812− addDef n (CaseOp inlc inlr
827+ addDef n (CaseOp (ci { case_inlinable = inlc })
813 ty ps_in ps args sc args' sc',
828 ty ps_in ps args sc args' sc',
814 access, Unchecked) ctxt in
829 access, Unchecked) ctxt in
815 uctxt { definitions = ctxt' }
830 uctxt { definitions = ctxt' }
@@ -818,14 +833,14 @@ simplifyCasedef :: Name -> Context -> Context
818simplifyCasedef n uctxt
833simplifyCasedef n uctxt
819 = let ctxt = definitions uctxt
834 = let ctxt = definitions uctxt
820 ctxt' = case lookupCtxt n ctxt of
835 ctxt' = case lookupCtxt n ctxt of
821− [(CaseOp inl inr ty [] ps args sc args' sc', acc, tot)] ->
836+ [(CaseOp ci ty [] ps args sc args' sc', acc, tot)] ->
822 ctxt -- nothing to simplify (or already done...)
837 ctxt -- nothing to simplify (or already done...)
823− [(CaseOp inl inr ty ps_in ps args sc args' sc', acc, tot)] ->
838+ [(CaseOp ci ty ps_in ps args sc args' sc', acc, tot)] ->
824 let ps_in' = map simpl ps_in
839 let ps_in' = map simpl ps_in
825 pdef = map debind ps_in' in
840 pdef = map debind ps_in' in
826 case simpleCase False True False CompileTime (FC "" 0) pdef of
841 case simpleCase False True False CompileTime (FC "" 0) pdef of
827 OK (CaseDef args sc _) ->
842 OK (CaseDef args sc _) ->
828− addDef n (CaseOp inl inr
843+ addDef n (CaseOp ci
829 ty ps_in' ps args sc args' sc',
844 ty ps_in' ps args sc args' sc',
830 acc, tot) ctxt
845 acc, tot) ctxt
831 _ -> ctxt in
846 _ -> ctxt in
@@ -863,7 +878,7 @@ lookupTy n ctxt
863 (Function ty _) -> return ty
878 (Function ty _) -> return ty
864 (TyDecl _ ty) -> return ty
879 (TyDecl _ ty) -> return ty
865 (Operator ty _ _) -> return ty
880 (Operator ty _ _) -> return ty
866− (CaseOp _ _ ty _ _ _ _ _ _) -> return ty
881+ (CaseOp _ ty _ _ _ _ _ _) -> return ty
868isConName :: Name -> Context -> Bool
883isConName :: Name -> Context -> Bool
869isConName n ctxt
884isConName n ctxt
@@ -879,7 +894,7 @@ isFnName n ctxt
879 case tfst def of
894 case tfst def of
880 (Function _ _) -> return True
895 (Function _ _) -> return True
881 (Operator _ _ _) -> return True
896 (Operator _ _ _) -> return True
882− (CaseOp _ _ _ _ _ _ _ _ _) -> return True
897+ (CaseOp _ _ _ _ _ _ _ _) -> return True
883 _ -> return False
898 _ -> return False
885lookupP :: Name -> Context -> [Term]
900lookupP :: Name -> Context -> [Term]
@@ -888,7 +903,7 @@ lookupP n ctxt
888 p <- case def of
903 p <- case def of
889 (Function ty tm, a, _) -> return (P Ref n ty, a)
904 (Function ty tm, a, _) -> return (P Ref n ty, a)
890 (TyDecl nt ty, a, _) -> return (P nt n ty, a)
905 (TyDecl nt ty, a, _) -> return (P nt n ty, a)
891− (CaseOp _ _ ty _ _ _ _ _ _, a, _) -> return (P Ref n ty, a)
906+ (CaseOp _ ty _ _ _ _ _ _, a, _) -> return (P Ref n ty, a)
892 (Operator ty _ _, a, _) -> return (P Ref n ty, a)
907 (Operator ty _ _, a, _) -> return (P Ref n ty, a)
893 case snd p of
908 case snd p of
894 Hidden -> []
909 Hidden -> []