@@ -19,9 +19,7 @@ import Control.Monad.Supply.Class (MonadSupply, freshName)
1919import Data.Maybe (fromMaybe)
2020
2121import Language.PureScript.CodeGen.JS.AST
22−import Language.PureScript.CodeGen.JS.Common
2322import Language.PureScript.CodeGen.JS.Optimizer.Common
24−import Language.PureScript.Names
2523import qualified Language.PureScript.Constants as C
2624
2725-- TODO: Potential bug:
@@ -82,24 +80,24 @@ inlineCommonValues = everywhereOnJS convert
8280 where
8381 convert :: JS -> JS
8482 convert (JSApp ss fn [dict])
85− | isDict' (semiringNumber ++ semiringInt) dict && isFn' fnZero fn = JSNumericLiteral ss (Left 0)
86− | isDict' (semiringNumber ++ semiringInt) dict && isFn' fnOne fn = JSNumericLiteral ss (Left 1)
87− | isDict' boundedBoolean dict && isFn' fnBottom fn = JSBooleanLiteral ss False
88− | isDict' boundedBoolean dict && isFn' fnTop fn = JSBooleanLiteral ss True
83+ | isDict' [semiringNumber, semiringInt] dict && isFn fnZero fn = JSNumericLiteral ss (Left 0)
84+ | isDict' [semiringNumber, semiringInt] dict && isFn fnOne fn = JSNumericLiteral ss (Left 1)
85+ | isDict boundedBoolean dict && isFn fnBottom fn = JSBooleanLiteral ss False
86+ | isDict boundedBoolean dict && isFn fnTop fn = JSBooleanLiteral ss True
8987 convert (JSApp ss (JSApp _ (JSApp _ fn [dict]) [x]) [y])
90− | isDict' semiringInt dict && isFn' fnAdd fn = intOp ss Add x y
91− | isDict' semiringInt dict && isFn' fnMultiply fn = intOp ss Multiply x y
92− | isDict' euclideanRingInt dict && isFn' fnDivide fn = intOp ss Divide x y
93− | isDict' ringInt dict && isFn' fnSubtract fn = intOp ss Subtract x y
88+ | isDict semiringInt dict && isFn fnAdd fn = intOp ss Add x y
89+ | isDict semiringInt dict && isFn fnMultiply fn = intOp ss Multiply x y
90+ | isDict euclideanRingInt dict && isFn fnDivide fn = intOp ss Divide x y
91+ | isDict ringInt dict && isFn fnSubtract fn = intOp ss Subtract x y
9492 convert other = other
95− fnZero = [(C.prelude, C.zero), (C.dataSemiring, C.zero)]
96− fnOne = [(C.prelude, C.one), (C.dataSemiring, C.one)]
97− fnBottom = [(C.prelude, C.bottom), (C.dataBounded, C.bottom)]
98− fnTop = [(C.prelude, C.top), (C.dataBounded, C.top)]
99− fnAdd = [(C.prelude, (C.+)), (C.prelude, (C.add)), (C.dataSemiring, (C.+)), (C.dataSemiring, (C.add))]
100− fnDivide = [(C.prelude, (C./)), (C.prelude, (C.div)), (C.dataEuclideanRing, C.div)]
101− fnMultiply = [(C.prelude, (C.*)), (C.prelude, (C.mul)), (C.dataSemiring, (C.*)), (C.dataSemiring, (C.mul))]
102− fnSubtract = [(C.prelude, (C.-)), (C.prelude, C.sub), (C.dataRing, C.sub)]
93+ fnZero = (C.dataSemiring, C.zero)
94+ fnOne = (C.dataSemiring, C.one)
95+ fnBottom = (C.dataBounded, C.bottom)
96+ fnTop = (C.dataBounded, C.top)
97+ fnAdd = (C.dataSemiring, C.add)
98+ fnDivide = (C.dataEuclideanRing, C.div)
99+ fnMultiply = (C.dataSemiring, C.mul)
100+ fnSubtract = (C.dataRing, C.sub)
103101 intOp ss op x y = JSBinary ss BitwiseOr (JSBinary ss op x y) (JSNumericLiteral ss (Left 0))
104102
105103inlineOperator :: (String, String) -> (JS -> JS -> JS) -> JS -> JS
@@ -108,7 +106,6 @@ inlineOperator (m, op) f = everywhereOnJS convert
108106 convert :: JS -> JS
109107 convert (JSApp _ (JSApp _ op' [x]) [y]) | isOp op' = f x y
110108 convert other = other
111− isOp (JSAccessor _ longForm (JSVar _ m')) = m == m' && longForm == identToJs (Op op)
112109 isOp (JSIndexer _ (JSStringLiteral _ op') (JSVar _ m')) = m == m' && op == op'
113110 isOp _ = False
114111
@@ -173,11 +170,11 @@ inlineCommonOperators = applyAll $
173170 ] ++
174171 [ fn | i <- [0..10], fn <- [ mkFn i, runFn i ] ]
175172 where
176− binary :: [(String, String)] -> [(String, String)] -> BinaryOperator -> JS -> JS
173+ binary :: (String, String) -> (String, String) -> BinaryOperator -> JS -> JS
177174 binary dict fns op = everywhereOnJS convert
178175 where
179176 convert :: JS -> JS
180− convert (JSApp ss (JSApp _ (JSApp _ fn [dict']) [x]) [y]) | isDict' dict dict' && isFn' fns fn = JSBinary ss op x y
177+ convert (JSApp ss (JSApp _ (JSApp _ fn [dict']) [x]) [y]) | isDict dict dict' && isFn fns fn = JSBinary ss op x y
181178 convert other = other
182179 binary' :: String -> String -> BinaryOperator -> JS -> JS
183180 binary' moduleName opString op = everywhereOnJS convert
@@ -185,11 +182,11 @@ inlineCommonOperators = applyAll $
185182 convert :: JS -> JS
186183 convert (JSApp ss (JSApp _ fn [x]) [y]) | isFn (moduleName, opString) fn = JSBinary ss op x y
187184 convert other = other
188− unary :: [(String, String)] -> [(String, String)] -> UnaryOperator -> JS -> JS
185+ unary :: (String, String) -> (String, String) -> UnaryOperator -> JS -> JS
189186 unary dicts fns op = everywhereOnJS convert
190187 where
191188 convert :: JS -> JS
192− convert (JSApp ss (JSApp _ fn [dict']) [x]) | isDict' dicts dict' && isFn' fns fn = JSUnary ss op x
189+ convert (JSApp ss (JSApp _ fn [dict']) [x]) | isDict dicts dict' && isFn fns fn = JSUnary ss op x
193190 convert other = other
194191 unary' :: String -> String -> UnaryOperator -> JS -> JS
195192 unary' moduleName fnName op = everywhereOnJS convert
@@ -251,118 +248,118 @@ inlineFnComposition = everywhereOnJSTopDownM convert
251248 return $ JSFunction ss Nothing [arg] (JSBlock ss [JSReturn Nothing $ JSApp Nothing y [JSApp Nothing x [JSVar Nothing arg]]])
252249 convert other = return other
253250 isFnCompose :: JS -> JS -> Bool
254− isFnCompose dict' fn = isDict' semigroupoidFn dict' && isFn' fnCompose fn
251+ isFnCompose dict' fn = isDict semigroupoidFn dict' && isFn fnCompose fn
255252 isFnComposeFlipped :: JS -> JS -> Bool
256− isFnComposeFlipped dict' fn = isDict' semigroupoidFn dict' && isFn' fnComposeFlipped fn
257− fnCompose :: [(String, String)]
258− fnCompose = [(C.prelude, C.compose), (C.prelude, (C.<<<)), (C.controlSemigroupoid, C.compose)]
259− fnComposeFlipped :: [(String, String)]
260− fnComposeFlipped = [(C.prelude, (C.>>>)), (C.controlSemigroupoid, C.composeFlipped)]
253+ isFnComposeFlipped dict' fn = isDict semigroupoidFn dict' && isFn fnComposeFlipped fn
254+ fnCompose :: (String, String)
255+ fnCompose = (C.controlSemigroupoid, C.compose)
256+ fnComposeFlipped :: (String, String)
257+ fnComposeFlipped = (C.controlSemigroupoid, C.composeFlipped)
261258
262−semiringNumber :: [(String, String)]
263−semiringNumber = [(C.prelude, C.semiringNumber), (C.dataSemiring, C.semiringNumber)]
259+semiringNumber :: (String, String)
260+semiringNumber = (C.dataSemiring, C.semiringNumber)
264261
265−semiringInt :: [(String, String)]
266−semiringInt = [(C.prelude, C.semiringInt), (C.dataSemiring, C.semiringInt)]
262+semiringInt :: (String, String)
263+semiringInt = (C.dataSemiring, C.semiringInt)
267264
268−ringNumber :: [(String, String)]
269−ringNumber = [(C.prelude, C.ringNumber), (C.dataRing, C.ringNumber)]
265+ringNumber :: (String, String)
266+ringNumber = (C.dataRing, C.ringNumber)
270267
271−ringInt :: [(String, String)]
272−ringInt = [(C.prelude, C.ringInt), (C.dataRing, C.ringInt)]
268+ringInt :: (String, String)
269+ringInt = (C.dataRing, C.ringInt)
273270
274−euclideanRingNumber :: [(String, String)]
275−euclideanRingNumber = [(C.prelude, C.moduloSemiringNumber), (C.dataEuclideanRing, C.euclideanRingNumber)]
271+euclideanRingNumber :: (String, String)
272+euclideanRingNumber = (C.dataEuclideanRing, C.euclideanRingNumber)
276273
277−euclideanRingInt :: [(String, String)]
278−euclideanRingInt = [(C.prelude, C.moduloSemiringInt), (C.dataEuclideanRing, C.euclideanRingInt)]
274+euclideanRingInt :: (String, String)
275+euclideanRingInt = (C.dataEuclideanRing, C.euclideanRingInt)
279276
280−eqNumber :: [(String, String)]
281−eqNumber = [(C.prelude, C.eqNumber), (C.dataEq, C.eqNumber)]
277+eqNumber :: (String, String)
278+eqNumber = (C.dataEq, C.eqNumber)
282279
283−eqInt :: [(String, String)]
284−eqInt = [(C.prelude, C.eqInt), (C.dataEq, C.eqInt)]
280+eqInt :: (String, String)
281+eqInt = (C.dataEq, C.eqInt)
285282
286−eqString :: [(String, String)]
287−eqString = [(C.prelude, C.eqString), (C.dataEq, C.eqString)]
283+eqString :: (String, String)
284+eqString = (C.dataEq, C.eqString)
288285
289−eqChar :: [(String, String)]
290−eqChar = [(C.prelude, C.eqChar), (C.dataEq, C.eqChar)]
286+eqChar :: (String, String)
287+eqChar = (C.dataEq, C.eqChar)
291288
292−eqBoolean :: [(String, String)]
293−eqBoolean = [(C.prelude, C.eqBoolean), (C.dataEq, C.eqBoolean)]
289+eqBoolean :: (String, String)
290+eqBoolean = (C.dataEq, C.eqBoolean)
294291
295−ordBoolean :: [(String, String)]
296−ordBoolean = [(C.prelude, C.ordBoolean), (C.dataOrd, C.ordBoolean)]
292+ordBoolean :: (String, String)
293+ordBoolean = (C.dataOrd, C.ordBoolean)
297294
298−ordNumber :: [(String, String)]
299−ordNumber = [(C.prelude, C.ordNumber), (C.dataOrd, C.ordNumber)]
295+ordNumber :: (String, String)
296+ordNumber = (C.dataOrd, C.ordNumber)
300297
301−ordInt :: [(String, String)]
302−ordInt = [(C.prelude, C.ordInt), (C.dataOrd, C.ordInt)]
298+ordInt :: (String, String)
299+ordInt = (C.dataOrd, C.ordInt)
303300
304−ordString :: [(String, String)]
305−ordString = [(C.prelude, C.ordString), (C.dataOrd, C.ordString)]
301+ordString :: (String, String)
302+ordString = (C.dataOrd, C.ordString)
306303
307−ordChar :: [(String, String)]
308−ordChar = [(C.prelude, C.ordChar), (C.dataOrd, C.ordChar)]
304+ordChar :: (String, String)
305+ordChar = (C.dataOrd, C.ordChar)
309306
310−semigroupString :: [(String, String)]
311−semigroupString = [(C.prelude, C.semigroupString), (C.dataSemigroup, C.semigroupString)]
307+semigroupString :: (String, String)
308+semigroupString = (C.dataSemigroup, C.semigroupString)
312309
313−boundedBoolean :: [(String, String)]
314−boundedBoolean = [(C.prelude, C.boundedBoolean), (C.dataBounded, C.boundedBoolean)]
310+boundedBoolean :: (String, String)
311+boundedBoolean = (C.dataBounded, C.boundedBoolean)
315312
316−heytingAlgebraBoolean :: [(String, String)]
317−heytingAlgebraBoolean = [(C.prelude, C.booleanAlgebraBoolean), (C.dataHeytingAlgebra, C.heytingAlgebraBoolean)]
313+heytingAlgebraBoolean :: (String, String)
314+heytingAlgebraBoolean = (C.dataHeytingAlgebra, C.heytingAlgebraBoolean)
318315
319−semigroupoidFn :: [(String, String)]
320−semigroupoidFn = [(C.prelude, C.semigroupoidFn), (C.controlSemigroupoid, C.semigroupoidFn)]
316+semigroupoidFn :: (String, String)
317+semigroupoidFn = (C.controlSemigroupoid, C.semigroupoidFn)
321318
322−opAdd :: [(String, String)]
323−opAdd = [(C.prelude, (C.+)), (C.prelude, C.add), (C.dataSemiring, C.add)]
319+opAdd :: (String, String)
320+opAdd = (C.dataSemiring, C.add)
324321
325−opMul :: [(String, String)]
326−opMul = [(C.prelude, (C.*)), (C.prelude, C.mul), (C.dataSemiring, C.mul)]
322+opMul :: (String, String)
323+opMul = (C.dataSemiring, C.mul)
327324
328−opEq :: [(String, String)]
329−opEq = [(C.prelude, (C.==)), (C.prelude, C.eq), (C.dataEq, C.eq)]
325+opEq :: (String, String)
326+opEq = (C.dataEq, C.eq)
330327
331−opNotEq :: [(String, String)]
332−opNotEq = [(C.prelude, (C./=)), (C.dataEq, C.notEq)]
328+opNotEq :: (String, String)
329+opNotEq = (C.dataEq, C.notEq)
333330
334−opLessThan :: [(String, String)]
335−opLessThan = [(C.prelude, (C.<)), (C.dataOrd, C.lessThan)]
331+opLessThan :: (String, String)
332+opLessThan = (C.dataOrd, C.lessThan)
336333
337−opLessThanOrEq :: [(String, String)]
338−opLessThanOrEq = [(C.prelude, (C.<=)), (C.dataOrd, C.lessThanOrEq)]
334+opLessThanOrEq :: (String, String)
335+opLessThanOrEq = (C.dataOrd, C.lessThanOrEq)
339336
340−opGreaterThan :: [(String, String)]
341−opGreaterThan = [(C.prelude, (C.>)), (C.dataOrd, C.greaterThan)]
337+opGreaterThan :: (String, String)
338+opGreaterThan = (C.dataOrd, C.greaterThan)
342339
343−opGreaterThanOrEq :: [(String, String)]
344−opGreaterThanOrEq = [(C.prelude, (C.>=)), (C.dataOrd, C.greaterThanOrEq)]
340+opGreaterThanOrEq :: (String, String)
341+opGreaterThanOrEq = (C.dataOrd, C.greaterThanOrEq)
345342
346−opAppend :: [(String, String)]
347−opAppend = [(C.prelude, (C.<>)), (C.prelude, (C.++)), (C.prelude, C.append), (C.dataSemigroup, C.append)]
343+opAppend :: (String, String)
344+opAppend = (C.dataSemigroup, C.append)
348345
349−opSub :: [(String, String)]
350−opSub = [(C.prelude, (C.-)), (C.prelude, C.sub), (C.dataRing, C.sub)]
346+opSub :: (String, String)
347+opSub = (C.dataRing, C.sub)
351348
352−opNegate :: [(String, String)]
353−opNegate = [(C.prelude, C.negate), (C.dataRing, C.negate)]
349+opNegate :: (String, String)
350+opNegate = (C.dataRing, C.negate)
354351
355−opDiv :: [(String, String)]
356−opDiv = [(C.prelude, (C./)), (C.prelude, C.div), (C.dataEuclideanRing, C.div)]
352+opDiv :: (String, String)
353+opDiv = (C.dataEuclideanRing, C.div)
357354
358−opMod :: [(String, String)]
359−opMod = [(C.prelude, C.mod), (C.dataEuclideanRing, C.mod)]
355+opMod :: (String, String)
356+opMod = (C.dataEuclideanRing, C.mod)
360357
361−opConj :: [(String, String)]
362−opConj = [(C.prelude, (C.&&)), (C.prelude, C.conj), (C.dataHeytingAlgebra, C.conj)]
358+opConj :: (String, String)
359+opConj = (C.dataHeytingAlgebra, C.conj)
363360
364−opDisj :: [(String, String)]
365−opDisj = [(C.prelude, (C.||)), (C.prelude, C.disj), (C.dataHeytingAlgebra, C.disj)]
361+opDisj :: (String, String)
362+opDisj = (C.dataHeytingAlgebra, C.disj)
366363
367−opNot :: [(String, String)]
368−opNot = [(C.prelude, C.not), (C.dataHeytingAlgebra, C.not)]
364+opNot :: (String, String)
365+opNot = (C.dataHeytingAlgebra, C.not)