@@ -28,7 +28,7 @@ data EvalState = ES { limited :: [(Name, Int)],
29type Eval a = State EvalState a
29type Eval a = State EvalState a
31−data EvalOpt = Spec | HNF | Simplify | AtREPL
31+data EvalOpt = Spec | HNF | Simplify Bool | AtREPL
32 deriving (Show, Eq)
32 deriving (Show, Eq)
34initEval = ES [] 0
34initEval = ES [] 0
@@ -107,11 +107,13 @@ specialise ctxt env limits t
107-- Like normalise, but we only reduce functions that are marked as okay to
107-- Like normalise, but we only reduce functions that are marked as okay to
108-- inline (and probably shouldn't reduce lets?)
108-- inline (and probably shouldn't reduce lets?)
110−simplify :: Context -> Env -> TT Name -> TT Name
110+simplify :: Context -> Bool -> Env -> TT Name -> TT Name
111−simplify ctxt env t
111+simplify ctxt runtime env t
112 = evalState (do val <- eval False ctxt threshold [(UN "lazy", 0),
112 = evalState (do val <- eval False ctxt threshold [(UN "lazy", 0),
113− (UN "par", 0)]
113+ (UN "par", 0),
114− (map finalEntry env) (finalise t) [Simplify]
115+ (map finalEntry env) (finalise t)
115 quote 0 val) initEval
117 quote 0 val) initEval
117hnf :: Context -> Env -> TT Name -> TT Name
119hnf :: Context -> Env -> TT Name -> TT Name
@@ -153,9 +155,13 @@ eval :: Bool -> Context -> Int -> [(Name, Int)] -> Env -> TT Name ->
153 [EvalOpt] -> Eval Value
155 [EvalOpt] -> Eval Value
154eval traceon ctxt maxred ntimes genv tm opts = ev ntimes [] True [] tm where
156eval traceon ctxt maxred ntimes genv tm opts = ev ntimes [] True [] tm where
155 spec = Spec `elem` opts
157 spec = Spec `elem` opts
156− simpl = Simplify `elem` opts
158+ simpl = Simplify True `elem` opts
157 atRepl = AtREPL `elem` opts
159 atRepl = AtREPL `elem` opts
161+ canSimplify inl inr n stk
162+ = (Simplify False `elem` opts && (not inl || elem n stk))
163+ || (Simplify True `elem` opts && (not inr || elem n stk))
159 ev ntimes stk top env (P _ n ty)
165 ev ntimes stk top env (P _ n ty)
160 | Just (Let t v) <- lookup n genv = do when (not atRepl) $ step maxred
166 | Just (Let t v) <- lookup n genv = do when (not atRepl) $ step maxred
161 ev ntimes stk top env v
167 ev ntimes stk top env v
@@ -168,8 +174,8 @@ eval traceon ctxt maxred ntimes genv tm opts = ev ntimes [] True [] tm where
168 ev ntimes (n:stk) True env tm
174 ev ntimes (n:stk) True env tm
169 [(TyDecl nt ty, _)] -> do vty <- ev ntimes stk True env ty
175 [(TyDecl nt ty, _)] -> do vty <- ev ntimes stk True env ty
170 return $ VP nt n vty
176 return $ VP nt n vty
171− [(CaseOp inl _ _ _ [] tree _ _, Public)] -> -- unoptimised version
177+ [(CaseOp inl inr _ _ _ [] tree _ _, Public)] -> -- unoptimised version
172− if simpl && (not inl || elem n stk)
178+ if canSimplify inl inr n stk
173 then liftM (VP Ref n) (ev ntimes stk top env ty)
179 then liftM (VP Ref n) (ev ntimes stk top env ty)
174 else do c <- evCase ntimes (n:stk) top env [] [] tree
180 else do c <- evCase ntimes (n:stk) top env [] [] tree
175 case c of
181 case c of
@@ -232,8 +238,8 @@ eval traceon ctxt maxred ntimes genv tm opts = ev ntimes [] True [] tm where
232 = traceWhen traceon (show stk) $
238 = traceWhen traceon (show stk) $
233 do let val = lookupDefAcc Nothing n atRepl ctxt
239 do let val = lookupDefAcc Nothing n atRepl ctxt
234 case val of
240 case val of
235− [(CaseOp inl _ _ _ ns tree _ _, Public)] ->
241+ [(CaseOp inl inr _ _ _ ns tree _ _, Public)] ->
236− if simpl && (not inl || elem n stk)
242+ if canSimplify inl inr n stk
237 then return $ unload env (VP Ref n ty) args
243 then return $ unload env (VP Ref n ty) args
238 else do c <- evCase ntimes (n:stk) top env ns args tree
244 else do c <- evCase ntimes (n:stk) top env ns args tree
239 case c of
245 case c of
@@ -392,7 +398,7 @@ eval_hnf ctxt statics genv tm = ev [] tm where
392 ev env (P Ref n ty) = case lookupDef Nothing n ctxt of
398 ev env (P Ref n ty) = case lookupDef Nothing n ctxt of
393 [Function _ t] -> ev env t
399 [Function _ t] -> ev env t
394 [TyDecl nt ty] -> return $ HP nt n ty
400 [TyDecl nt ty] -> return $ HP nt n ty
395− [CaseOp inl _ _ _ [] tree _ _] ->
401+ [CaseOp inl _ _ _ _ [] tree _ _] ->
396 do c <- evCase env [] [] tree
402 do c <- evCase env [] [] tree
397 case c of
403 case c of
398 (Nothing, _, _) -> return $ HP Ref n ty
404 (Nothing, _, _) -> return $ HP Ref n ty
@@ -422,7 +428,7 @@ eval_hnf ctxt statics genv tm = ev [] tm where
422 app <- apply env sc' as
428 app <- apply env sc' as
423 wknH (-1) app
429 wknH (-1) app
424 apply env (HP Ref n ty) args
430 apply env (HP Ref n ty) args
425− | [CaseOp _ _ _ _ ns tree _ _] <- lookupDef Nothing n ctxt
431+ | [CaseOp _ _ _ _ _ ns tree _ _] <- lookupDef Nothing n ctxt
426 = do c <- evCase env ns args tree
432 = do c <- evCase env ns args tree
427 case c of
433 case c of
428 (Nothing, _, env') -> return $ unload env' (HP Ref n ty) args
434 (Nothing, _, env') -> return $ unload env' (HP Ref n ty) args
@@ -549,8 +555,8 @@ convEq ctxt = ceq [] where
549 sameDefs ps x y = case (lookupDef Nothing x ctxt, lookupDef Nothing y ctxt) of
555 sameDefs ps x y = case (lookupDef Nothing x ctxt, lookupDef Nothing y ctxt) of
550 ([Function _ xdef], [Function _ ydef])
556 ([Function _ xdef], [Function _ ydef])
551 -> ceq ((x,y):ps) xdef ydef
557 -> ceq ((x,y):ps) xdef ydef
552− ([CaseOp _ _ _ _ _ xdef _ _],
558+ ([CaseOp _ _ _ _ _ _ xdef _ _],
553− [CaseOp _ _ _ _ _ ydef _ _])
559+ [CaseOp _ _ _ _ _ _ ydef _ _])
554 -> caseeq ((x,y):ps) xdef ydef
560 -> caseeq ((x,y):ps) xdef ydef
555 _ -> return False
561 _ -> return False
@@ -572,7 +578,8 @@ spec ctxt statics genv tm = error "spec undefined"
572data Def = Function Type Term
578data Def = Function Type Term
573 | TyDecl NameType Type
579 | TyDecl NameType Type
574 | Operator Type Int ([Value] -> Maybe Value)
580 | Operator Type Int ([Value] -> Maybe Value)
575− | CaseOp Bool Type -- bool for inlinable
581+ | CaseOp Bool Bool -- compile-time/run-time inlinable
576 [Either Term (Term, Term)] -- original definition
583 [Either Term (Term, Term)] -- original definition
577 [([Name], Term, Term)] -- simplified definition
584 [([Name], Term, Term)] -- simplified definition
578 [Name] SC -- Compile time case definition
585 [Name] SC -- Compile time case definition
@@ -585,7 +592,7 @@ instance Show Def where
585 show (Function ty tm) = "Function: " ++ show (ty, tm)
592 show (Function ty tm) = "Function: " ++ show (ty, tm)
586 show (TyDecl nt ty) = "TyDecl: " ++ show nt ++ " " ++ show ty
593 show (TyDecl nt ty) = "TyDecl: " ++ show nt ++ " " ++ show ty
587 show (Operator ty _ _) = "Operator: " ++ show ty
594 show (Operator ty _ _) = "Operator: " ++ show ty
588− show (CaseOp _ ty ps_in ps ns sc ns' sc')
595+ show (CaseOp _ _ ty ps_in ps ns sc ns' sc')
589 = "Case: " ++ show ty ++ " " ++ show ps ++ "\n" ++
596 = "Case: " ++ show ty ++ " " ++ show ps ++ "\n" ++
590 show ns ++ " " ++ show sc ++ "\n" ++
597 show ns ++ " " ++ show sc ++ "\n" ++
591 show ns' ++ " " ++ show sc'
598 show ns' ++ " " ++ show sc'
@@ -707,8 +714,9 @@ addCasedef n alwaysInline tcase covering ps_in ps psrt ty uctxt
707 ctxt' = case (simpleCase tcase covering CompileTime (FC "" 0) ps,
714 ctxt' = case (simpleCase tcase covering CompileTime (FC "" 0) ps,
708 simpleCase tcase covering RunTime (FC "" 0) psrt) of
715 simpleCase tcase covering RunTime (FC "" 0) psrt) of
709 (OK (CaseDef args sc _), OK (CaseDef args' sc' _)) ->
716 (OK (CaseDef args sc _), OK (CaseDef args' sc' _)) ->
710− let inl = alwaysInline || small n sc' in
717+ let inl = alwaysInline in
718+ addDef n (CaseOp inl (inl || small n sc')
711− addDef n (CaseOp inl ty ps_in ps args sc args' sc',
719+ ty ps_in ps args sc args' sc',
712 access, Unchecked) ctxt in
720 access, Unchecked) ctxt in
713 uctxt { definitions = ctxt' }
721 uctxt { definitions = ctxt' }
@@ -716,15 +724,14 @@ simplifyCasedef :: Name -> Context -> Context
716simplifyCasedef n uctxt
724simplifyCasedef n uctxt
717 = let ctxt = definitions uctxt
725 = let ctxt = definitions uctxt
718 ctxt' = case lookupCtxt Nothing n ctxt of
726 ctxt' = case lookupCtxt Nothing n ctxt of
719− [(CaseOp inl ty [] ps args sc args' sc', acc, tot)] ->
727+ [(CaseOp inl inr ty [] ps args sc args' sc', acc, tot)] ->
720 ctxt -- nothing to simplify (or already done...)
728 ctxt -- nothing to simplify (or already done...)
721− [(CaseOp inl ty ps_in ps args sc args' sc', acc, tot)] ->
729+ [(CaseOp inl inr ty ps_in ps args sc args' sc', acc, tot)] ->
722 let pdef = map debind $ map simpl ps_in in
730 let pdef = map debind $ map simpl ps_in in
723 case simpleCase False True CompileTime (FC "" 0) pdef of
731 case simpleCase False True CompileTime (FC "" 0) pdef of
724 OK (CaseDef args sc _) ->
732 OK (CaseDef args sc _) ->
725−-- Erase the original patterns, since we won't use them again and it
726−-- only clutters the .ibc
727− addDef n (CaseOp inl ty [] ps args sc args' sc',
733+ addDef n (CaseOp inl inr
734+ ty ps_in ps args sc args' sc',
728 acc, tot) ctxt
735 acc, tot) ctxt
729 _ -> ctxt in
736 _ -> ctxt in
730 uctxt { definitions = ctxt' }
737 uctxt { definitions = ctxt' }
@@ -737,7 +744,7 @@ simplifyCasedef n uctxt
737 (vs, x', y')
744 (vs, x', y')
738 debind (Left x) = let (vs, x') = depat [] x in
745 debind (Left x) = let (vs, x') = depat [] x in
739 (vs, x', Impossible)
746 (vs, x', Impossible)
740− simpl (Right (x, y)) = Right (x, simplify uctxt [] y)
747+ simpl (Right (x, y)) = Right (x, simplify uctxt False [] y)
741 simpl t = t
748 simpl t = t
743addOperator :: Name -> Type -> Int -> ([Value] -> Maybe Value) ->
750addOperator :: Name -> Type -> Int -> ([Value] -> Maybe Value) ->
@@ -761,7 +768,7 @@ lookupTy root n ctxt
761 (Function ty _) -> return ty
768 (Function ty _) -> return ty
762 (TyDecl _ ty) -> return ty
769 (TyDecl _ ty) -> return ty
763 (Operator ty _ _) -> return ty
770 (Operator ty _ _) -> return ty
764− (CaseOp _ ty _ _ _ _ _ _) -> return ty
771+ (CaseOp _ _ ty _ _ _ _ _ _) -> return ty
766isConName :: Maybe [String] -> Name -> Context -> Bool
773isConName :: Maybe [String] -> Name -> Context -> Bool
767isConName root n ctxt
774isConName root n ctxt
@@ -777,7 +784,7 @@ isFnName root n ctxt
777 case tfst def of
784 case tfst def of
778 (Function _ _) -> return True
785 (Function _ _) -> return True
779 (Operator _ _ _) -> return True
786 (Operator _ _ _) -> return True
780− (CaseOp _ _ _ _ _ _ _ _) -> return True
787+ (CaseOp _ _ _ _ _ _ _ _ _) -> return True
781 _ -> return False
788 _ -> return False
783lookupP :: Maybe [String] -> Name -> Context -> [Term]
790lookupP :: Maybe [String] -> Name -> Context -> [Term]
@@ -786,7 +793,7 @@ lookupP root n ctxt
786 p <- case def of
793 p <- case def of
787 (Function ty tm, a, _) -> return (P Ref n ty, a)
794 (Function ty tm, a, _) -> return (P Ref n ty, a)
788 (TyDecl nt ty, a, _) -> return (P nt n ty, a)
795 (TyDecl nt ty, a, _) -> return (P nt n ty, a)
789− (CaseOp _ ty _ _ _ _ _ _, a, _) -> return (P Ref n ty, a)
796+ (CaseOp _ _ ty _ _ _ _ _ _, a, _) -> return (P Ref n ty, a)
790 (Operator ty _ _, a, _) -> return (P Ref n ty, a)
797 (Operator ty _ _, a, _) -> return (P Ref n ty, a)
791 case snd p of
798 case snd p of
792 Hidden -> []
799 Hidden -> []