git.iko.soy/rowtype-yoga/purescript-native
on golang help sign in
Optimise (f <<< g $ x) as (f (g x))

When writing PureScript code like: not <<< not $ id true We see a few function calls: Prelude["<<<"](Prelude.semigroupoidArr) (Prelude.not(Prelude.boolLikeBoolean)) (Prelude.not(Prelude.boolLikeBoolean)) (Prelude.id(Prelude.categoryArr)( true)); We now see something very straight forward: !!Prelude.id(Prelude.categoryArr)(true); The implementation of this optimisation contains a lot of similarities to the common binary operator inlining. We should extract some functions out in another commit.

commitd8d96cf6bc1e335bbe18f90ddbccc83480cbbb50 authorBrian McKenna <brian@brianmckenna.org> dateFri 17 Apr 2015, 23:16 (12 years ago) parentfc4325e
3 files changed+21−1
src/Language/PureScript/CodeGen/JS/Optimizer.hs+2−1 src/Language/PureScript/CodeGen/JS/Optimizer/Inliner.hs+16 src/Language/PureScript/Constants.hs+3
src/Language/PureScript/CodeGen/JS/Optimizer.hs+2−1
@@ -77,7 +77,8 @@ optimize' js = do
7777 , inlineOperator (C.prelude, (C.$)) $ \f x -> JSApp f [x]
7878 , inlineOperator (C.prelude, (C.#)) $ \x f -> JSApp f [x]
7979 , inlineOperator (C.preludeUnsafe, C.unsafeIndex) $ flip JSIndexer
80− , inlineCommonOperators ]) js
80+ , inlineCommonOperators
81+ , inlineAppliedArrComposition ]) js
8182
8283untilFixedPoint :: (Eq a) => (a -> a) -> a -> a
8384untilFixedPoint f = go
src/Language/PureScript/CodeGen/JS/Optimizer/Inliner.hs+16
@@ -18,6 +18,7 @@ module Language.PureScript.CodeGen.JS.Optimizer.Inliner (
1818 inlineValues,
1919 inlineOperator,
2020 inlineCommonOperators,
21+ inlineAppliedArrComposition,
2122 etaConvert,
2223 unThunk,
2324 evaluateIifes
@@ -31,6 +32,10 @@ import Language.PureScript.Names
3132import Language.PureScript.CodeGen.JS.Optimizer.Common
3233import qualified Language.PureScript.Constants as C
3334
35+-- TODO: Potential bug:
36+-- Shouldn't just inline this case: { var x = 0; x.toFixed(10); }
37+-- Needs to be: { 0..toFixed(10); }
38+-- Probably needs to be fixed in pretty-printer instead.
3439shouldInline :: JS -> Bool
3540shouldInline (JSVar _) = True
3641shouldInline (JSNumericLiteral _) = True
@@ -233,6 +238,14 @@ inlineCommonOperators = applyAll $
233238 go m acc (JSApp lhs [arg]) = go (m - 1) (arg : acc) lhs
234239 go _ _ _ = Nothing
235240
241+-- (f << g $ x) = f (g x)
242+inlineAppliedArrComposition :: JS -> JS
243+inlineAppliedArrComposition = everywhereOnJS convert
244+ where
245+ convert :: JS -> JS
246+ convert (JSApp (JSApp (JSApp (JSApp fn [dict']) [x]) [y]) [z]) | isDict semigroupoidArr dict' && isPreludeFn (C.<<<) fn = JSApp x [JSApp y [z]]
247+ convert other = other
248+
236249isDict :: (String, String) -> JS -> Bool
237250isDict (moduleName, dictName) (JSAccessor x (JSVar y)) = x == dictName && y == moduleName
238251isDict _ _ = False
@@ -292,3 +305,6 @@ latticeBoolean = (C.prelude, C.latticeBoolean)
292305
293306complementedLatticeBoolean :: (String, String)
294307complementedLatticeBoolean = (C.prelude, C.complementedLatticeBoolean)
308+
309+semigroupoidArr :: (String, String)
310+semigroupoidArr = (C.prelude, C.semigroupoidArr)
src/Language/PureScript/Constants.hs+3
@@ -83,6 +83,9 @@ unsafeIndex = "unsafeIndex"
8383(.^.) :: String
8484(.^.) = ".^."
8585
86+(<<<) :: String
87+(<<<) = "<<<"
88+
8689-- Functions
8790
8891negate :: String