git.iko.soy/rowtype-yoga/purescript-native
on golang help sign in
Inlining multiple function arguments
commit1301ee679fc90c6e4f3ba42440a01d8849d90c5e authorPhil Freeman <paf31@cantab.net> dateThu 26 Dec 2013, 14:00 (13 years ago) parent8e56f00
4 files changed+52−47
examples/passing/TopLevelCase.purs+9−5 libraries/prelude/prelude.purs+28−38 src/Language/PureScript/Optimize.hs+9−1 src/Language/PureScript/TypeChecker/Types.hs+6−3
examples/passing/TopLevelCase.purs+9−5
@@ -1,8 +1,12 @@
1−_1 :: (Number, Number) -> Number
2−_1 (0, x) = x
3−_1 (x, 0) = x
4−_1 (x, y) | x > y = _1 (x % y, y)
5−_1 (x, y) = _1 (y % x, x)
1+gcd :: (Number, Number) -> Number
2+gcd (0, x) = x
3+gcd (x, 0) = x
4+gcd (x, y) | x > y = gcd (x % y, y)
5+gcd (x, y) = gcd (y % x, x)
66
77guardsTest (x:xs) | x > 0 = guardsTest xs
88guardsTest xs = xs
9+
10+data A = A
11+
12+parseTest A 0 = 0
libraries/prelude/prelude.purs+28−38
@@ -18,57 +18,50 @@ konst = \a -> \b -> a
1818data Maybe a = Nothing | Just a
1919
2020maybe :: forall a b. b -> (a -> b) -> Maybe a -> b
21−maybe = \b -> \f -> \m -> case m of
22− Nothing -> b
23− Just a -> f a
21+maybe b _ Nothing = b
22+maybe _ f (Just a) = f a
2423
2524fromMaybe :: forall a. a -> Maybe a -> a
26−fromMaybe = \a -> maybe a id
25+fromMaybe a = maybe a id
2726
2827bindMaybe :: forall a b. Maybe a -> (a -> Maybe b) -> Maybe b
29−bindMaybe = \m -> \f -> maybe Nothing f m
28+bindMaybe m f = maybe Nothing f m
3029
3130-- Either
3231
3332data Either a b = Left a | Right b
3433
3534either :: forall a b c. (a -> c) -> (b -> c) -> Either a b -> c
36−either = \f -> \g -> \e -> case e of
37− Left a -> f a
38− Right b -> g b
35+either f _ (Left a) = f a
36+either _ g (Right b) = g b
3937
4038bindEither :: forall e a b. Either e a -> (a -> Either e b) -> Either e b
41−bindEither = either (\e -> \_ -> Left e) (\a -> \f -> f a)
39+bindEither = either (\e _ -> Left e) (\a f -> f a)
4240
4341-- Arrays
4442
4543head :: forall a. [a] -> a
46−head = \xs -> case xs of
47− x : _ -> x
44+head (x : _) = x
4845
4946headSafe :: forall a. [a] -> Maybe a
50−headSafe = \xs -> case xs of
51− x : _ -> Just x
52− _ -> Nothing
47+headSafe (x : _) = Just x
48+headSafe _ = Nothing
5349
5450tail :: forall a. [a] -> [a]
55−tail = \xs -> case xs of
56− _ : xs -> xs
51+tail (_ : xs) = xs
5752
5853tailSafe :: forall a. [a] -> Maybe [a]
59−tailSafe = \xs -> case xs of
60− _ : xs -> Just xs
61− _ -> Nothing
54+tailSafe (_ : xs) = Just xs
55+tailSafe _ = Nothing
6256
6357foreign import map :: forall a b. (a -> b) -> [a] -> [b]
6458
6559foldr :: forall a b. (a -> b -> a) -> a -> [b] -> a
66−foldr = \f -> \a -> \bs -> case bs of
67− b : bs -> f (foldr f a bs) b
68− [] -> a
60+foldr f a (b : bs) = f (foldr f a bs) b
61+foldr _ a [] = a
6962
7063foldl :: forall a b. (a -> b -> b) -> b -> [a] -> b
71−foldl = \f -> \b -> \as -> {
64+foldl f b as = {
7265 var result = b;
7366 foreach (a in as) {
7467 result = f a result;
@@ -104,7 +97,7 @@ cons :: forall a. a -> [a] -> [a]
10497cons = \a -> concat([a])
10598
10699concatMap :: forall a b. [a] -> (a -> [b]) -> [b]
107−concatMap = \as -> \f -> {
100+concatMap as f = {
108101 var result = [];
109102 foreach (a in as) {
110103 result = result `concat` f a;
@@ -115,12 +108,11 @@ concatMap = \as -> \f -> {
115108foreign import filter :: forall a. [a] -> (a -> Boolean) -> [a]
116109
117110empty :: forall a. [a] -> Boolean
118−empty = \as -> case as of
119− [] -> true
120− _ -> false
111+empty [] = true
112+empty _ = false
121113
122114range :: Number -> Number -> [Number]
123−range = \lo -> \hi -> {
115+range lo hi = {
124116 var ns = [];
125117 for (n <- lo until hi) {
126118 ns = push ns n;
@@ -129,12 +121,11 @@ range = \lo -> \hi -> {
129121 }
130122
131123zipWith :: forall a b c. (a -> b -> c) -> [a] -> [b] -> [c]
132−zipWith = \f -> \as -> \bs -> case { as: as, bs: bs } of
133− { as = a : as1, bs = b : bs1 } -> cons (f a b) (zipWith f as1 bs1)
134− _ -> []
124+zipWith f (a:as) (b:bs) = cons (f a b) (zipWith f as bs)
125+zipWith _ _ _ = []
135126
136127any :: forall a. (a -> Boolean) -> [a] -> Boolean
137−any = \p -> \as -> {
128+any p as = {
138129 for (i <- 0 until length as) {
139130 if (p (as !! i)) {
140131 return true;
@@ -144,7 +135,7 @@ any = \p -> \as -> {
144135 }
145136
146137all :: forall a. (a -> Boolean) -> [a] -> Boolean
147−all = \p -> \as -> {
138+all p as = {
148139 for (i <- 0 until length as) {
149140 if (!(p (as !! i))) {
150141 return false;
@@ -158,10 +149,10 @@ all = \p -> \as -> {
158149type Tuple a b = { fst :: a, snd :: b }
159150
160151curry :: forall a b c. (Tuple a b -> c) -> a -> b -> c
161−curry = \f -> \a -> \b -> f { fst: a, snd: b }
152+curry f a b = f { fst: a, snd: b }
162153
163154uncurry :: forall a b c. (a -> b -> c) -> Tuple a b -> c
164−uncurry = \f -> \t -> f t.fst t.snd
155+uncurry f t = f t.fst t.snd
165156
166157tuple :: forall a b. a -> b -> Tuple a b
167158tuple = curry id
@@ -170,10 +161,9 @@ zip :: forall a b. [a] -> [b] -> [Tuple a b]
170161zip = zipWith tuple
171162
172163unzip :: forall a b. [Tuple a b] -> Tuple [a] [b]
173−unzip = \ts -> case ts of
174− t : ts1 -> case unzip ts1 of
164+unzip (t:ts) = case unzip ts of
175165 { fst = as, snd = bs } -> tuple (cons t.fst as) (cons t.snd bs)
176− [] -> tuple [] []
166+unzip [] = tuple [] []
177167
178168-- Strings
179169
src/Language/PureScript/Optimize.hs+9−1
@@ -17,6 +17,7 @@ module Language.PureScript.Optimize (
1717) where
1818
1919import Data.Data
20+import Data.Maybe (fromMaybe)
2021import Data.Generics
2122
2223import Language.PureScript.Names
@@ -31,6 +32,12 @@ replaceIdent var1 js = everywhere (mkT replace)
3132 replace (JSVar var2) | var1 == var2 = js
3233 replace other = other
3334
35+replaceIdents :: (Data d) => [(Ident, JS)] -> d -> d
36+replaceIdents vars = everywhere (mkT replace)
37+ where
38+ replace v@(JSVar var) = fromMaybe v $ lookup var vars
39+ replace other = other
40+
3441isReassigned :: (Data d) => Ident -> d -> Bool
3542isReassigned var1 = everything (||) (mkQ False check)
3643 where
@@ -84,7 +91,8 @@ etaConvert :: JS -> JS
8491etaConvert = everywhere (mkT convert)
8592 where
8693 convert :: JS -> JS
87− convert (JSBlock [JSReturn (JSApp (JSFunction Nothing [ident] (JSBlock body)) [arg])]) | shouldInline arg = JSBlock (replaceIdent ident arg body)
94+ convert (JSBlock [JSReturn (JSApp (JSFunction Nothing idents (JSBlock body)) args)])
95+ | all shouldInline args = JSBlock (replaceIdents (zip idents args) body)
8896 convert js = js
8997
9098unThunk :: JS -> JS
src/Language/PureScript/TypeChecker/Types.hs+6−3
@@ -451,9 +451,12 @@ inferBinder val (UnaryBinder ctor binder) = do
451451 modulePath <- checkModulePath <$> get
452452 case M.lookup (qualify modulePath ctor) (dataConstructors env) of
453453 Just ty -> do
454− Function [obj] ret <- replaceAllVarsWithUnknowns ty
455− val `subsumes` ret
456− inferBinder obj binder
454+ fn <- replaceAllVarsWithUnknowns ty
455+ case fn of
456+ Function [obj] ret -> do
457+ val `subsumes` ret
458+ inferBinder obj binder
459+ _ -> throwError $ "Constructor " ++ show ctor ++ " is not a unary constructor"
457460 _ -> throwError $ "Constructor " ++ show ctor ++ " is not defined"
458461inferBinder val (ObjectBinder props) = do
459462 row <- fresh