Skip to content

Commit d8d96cf

Browse files
committed
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.
1 parent fc4325e commit d8d96cf

File tree

3 files changed

+21
-1
lines changed

3 files changed

+21
-1
lines changed

src/Language/PureScript/CodeGen/JS/Optimizer.hs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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

8283
untilFixedPoint :: (Eq a) => (a -> a) -> a -> a
8384
untilFixedPoint f = go

src/Language/PureScript/CodeGen/JS/Optimizer/Inliner.hs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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
3132
import Language.PureScript.CodeGen.JS.Optimizer.Common
3233
import 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.
3439
shouldInline :: JS -> Bool
3540
shouldInline (JSVar _) = True
3641
shouldInline (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+
236249
isDict :: (String, String) -> JS -> Bool
237250
isDict (moduleName, dictName) (JSAccessor x (JSVar y)) = x == dictName && y == moduleName
238251
isDict _ _ = False
@@ -292,3 +305,6 @@ latticeBoolean = (C.prelude, C.latticeBoolean)
292305

293306
complementedLatticeBoolean :: (String, String)
294307
complementedLatticeBoolean = (C.prelude, C.complementedLatticeBoolean)
308+
309+
semigroupoidArr :: (String, String)
310+
semigroupoidArr = (C.prelude, C.semigroupoidArr)

src/Language/PureScript/Constants.hs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,9 @@ unsafeIndex = "unsafeIndex"
8383
(.^.) :: String
8484
(.^.) = ".^."
8585

86+
(<<<) :: String
87+
(<<<) = "<<<"
88+
8689
-- Functions
8790

8891
negate :: String

0 commit comments

Comments
 (0)