@@ -3,7 +3,6 @@ module Transform.Optimize (optimize) where
33import SourceSyntax.Everything
44import Control.Arrow (second, (***))
55import Data.Char (isAlpha)
6−import Transform.Substitute
76
87optimize (Module name ims exs stmts) =
98 Module name ims exs (map optimizeStmt stmts)
@@ -20,8 +19,7 @@ instance Simplify (Declaration t v) where
2019 simp stmt = stmt
2120
2221instance Simplify (Def t v) where
23− simp (FnDef func args e) = FnDef func args (simp e)
24− simp (OpDef op a1 a2 e) = OpDef op a1 a2 (simp e)
22+ simp (Def name e) = Def name (simp e)
2523 simp x = x
2624
2725instance Simplify e => Simplify (Located e) where
@@ -34,11 +32,7 @@ instance Simplify (Expr t v) where
3432 Range e1 e2 -> Range (f e1) (f e2)
3533 Binop op e1 e2 -> binop op (f e1) (f e2)
3634 Lambda x e -> Lambda x (f e)
37− Record fs -> Record (map (\(f,as,e) -> (f, as, simp e)) fs)
38− App (L t s (Lambda x e1)) e2 ->
39− if isValue e2' then subst x e2' e1' else App (L t s (Lambda x ce1')) ce2'
40− where ce1'@(L _ _ e1') = f e1
41− ce2'@(L _ _ e2') = f e2
35+ Record fs -> Record (map (second simp) fs)
4236 App e1 e2 -> App (f e1) (f e2)
4337 Let defs e -> Let (map simp defs) (f e)
4438 Data name es -> Data name (map f es)
@@ -133,12 +127,6 @@ binop op ce1@(L t1 s1 e1) ce2@(L t2 s2 e2) =
133127 ("++", _, Data "[]" []) -> e1
134128 ("++", Data "::" [h,t], _) -> Data "::" [h, none $ binop "++" t ce2]
135129
136− ("|>", _, _) -> App ce2 ce1
137− ("<|", _, _) -> App ce1 ce2
138− (".", _, _) ->
139− Lambda "x" (none $
140− App ce1 (none $ App ce2 (none $ Var "x")))
141−
142130 _ | isAlpha (head op) || '_' == head op ->
143131 App (none $ App (none $ Var op) ce1) ce2
144132 | otherwise -> Binop op ce1 ce2