on master help sign in
Improve optimizations a bit. The compiler now performs beta reduction in some simple cases.
commit8ac0c81c1c8f64b995716fedfec670d3dabe5bdb authorevancz <info@elm-lang.org> dateThu 13 Sep 2012, 21:31 (14 years ago) parent8a79164
3 files changed+19−6
elm/src/Gen/Cases.hs+1−1 elm/src/Optimize.hs+14−1 elm/src/Substitute.hs+4−4
elm/src/Gen/Cases.hs+1−1
@@ -45,7 +45,7 @@ match vs cs def
4545
4646matchVar :: [String] -> [([Pattern],Expr)] -> Match -> GuidCounter Match
4747matchVar (v:vs) cs def = match vs (map subVar cs) def
48− where subVar (PVar x : ps, e) = (ps, subst v x e)
48+ where subVar (PVar x : ps, e) = (ps, subst x (Var v) e)
4949 subVar (PAnything : ps, e) = (ps, e)
5050
5151matchCon :: [String] -> [([Pattern],Expr)] -> Match -> GuidCounter Match
elm/src/Optimize.hs+14−1
@@ -3,7 +3,7 @@ module Optimize (optimize) where
33import Ast
44import Control.Arrow (second)
55import Data.Char (isAlpha)
6−
6+import Substitute
77
88optimize (Module name ims exs stmts) =
99 Module name ims exs (map optimizeStmt stmts)
@@ -21,6 +21,9 @@ simp expr =
2121 Range e1 e2 -> Range (f e1) (f e2)
2222 Binop op e1 e2 -> simp_binop op (f e1) (f e2)
2323 Lambda x e -> Lambda x (f e)
24+ App (Lambda x e1) e2 ->
25+ if isValue e2' then subst x e2' e1' else App (Lambda x e1') e2'
26+ where { e1' = f e1 ; e2' = f e2 }
2427 App e1 e2 -> App (f e1) (f e2)
2528 If e1 e2 e3 -> simp_if (f e1) (f e2) (f e3)
2629 Let defs e -> Let (map simpDef defs) (f e)
@@ -32,6 +35,15 @@ simp expr =
3235simp_if (Boolean b) e2 e3 = if b then e2 else e3
3336simp_if a b c = If a b c
3437
38+isValue e = case e of { IntNum _ -> True
39+ ; FloatNum _ -> True
40+ ; Chr _ -> True
41+ ; Str _ -> True
42+ ; Boolean _ -> True
43+ ; Var _ -> True
44+ ; Data _ _ -> True
45+ ; _ -> False }
46+
3547simp_binop = binop
3648
3749binop op (IntNum n) (IntNum m) = f n m
@@ -87,6 +99,7 @@ binop "++" e (Data "Nil" []) = e
8799binop "++" (Data "Cons" [h,t]) e = Data "Cons" [h, binop "++" t e]
88100
89101binop "$" e1 e2 = App e1 e2
102+binop "." e1 e2 = Lambda "x" (App e1 (App e2 (Var "x")))
90103
91104binop op e1 e2
92105 | isAlpha (head op) || '_' == head op = App (App (Var op) e1) e2
elm/src/Substitute.hs+4−4
@@ -4,9 +4,9 @@ import Control.Arrow (second)
44
55import Ast
66
7−subst :: String -> String -> Expr -> Expr
8−subst new old expr =
9− let f = subst new old in
7+subst :: String -> Expr -> Expr -> Expr
8+subst old new expr =
9+ let f = subst old new in
1010 case expr of
1111 Range e1 e2 -> Range (f e1) (f e2)
1212 Access e x -> Access (f e) x
@@ -20,7 +20,7 @@ subst new old expr =
2020 Input _ -> expr
2121 Let defs e -> Let (map substDef defs) (f e)
2222 where substDef (Definition name vs e) = Definition name vs (f e)
23− Var x -> if x == old then Var new else expr
23+ Var x -> if x == old then new else expr
2424 Case e cases -> Case (f e) $ map (second f) cases
2525 Data name es -> Data name (map f es)
2626 _ -> expr