Last active
January 21, 2017 21:09
-
-
Save deque-blog/757fe47a3474915efaf6c8b7cc2c7562 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
optimize :: Expr -> Expr | |
optimize op@(Op Add ys) = optimizeOp Add (map optimize ys) 0 (+) | |
optimize op@(Op Mul ys) | |
| not (null (dropWhile (/= cst 0) xs)) = cst 0 | |
| otherwise = optimizeOp Mul xs 1 (*) | |
where xs = map optimize ys | |
optimize e = e | |
optimizeOp :: OpType -> [Expr] -> Int -> (Int -> Int -> Int) -> Expr | |
optimizeOp opType xs neutral combine = | |
let (constants, vars) = partition isCst xs | |
constantsVal = map (\(Cst x) -> x) constants | |
sumCst = foldl' combine neutral constantsVal | |
in case vars of | |
[] -> cst sumCst | |
[y] | sumCst == neutral -> y | |
ys | sumCst == neutral -> Op opType ys | |
ys -> Op opType (cst sumCst : ys) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment