git.iko.soy/rowtype-yoga/purescript-native
on golang help sign in
Inlining multiple function arguments
1301ee6Phil Freeman
examples/passing/TopLevelCase.purs+9−5
@@ -1,8 +1,12 @@
1−_1 :: (Number, Number) -> Number
1+gcd :: (Number, Number) -> Number
2−_1 (0, x) = x
2+gcd (0, x) = x
3−_1 (x, 0) = x
3+gcd (x, 0) = x
4−_1 (x, y) | x > y = _1 (x % y, y)
4+gcd (x, y) | x > y = gcd (x % y, y)
5−_1 (x, y) = _1 (y % x, x)
5+gcd (x, y) = gcd (y % x, x)
6
6
7guardsTest (x:xs) | x > 0 = guardsTest xs
7guardsTest (x:xs) | x > 0 = guardsTest xs
8guardsTest xs = xs
8guardsTest 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
18data Maybe a = Nothing | Just a
18data Maybe a = Nothing | Just a
19
19
20maybe :: forall a b. b -> (a -> b) -> Maybe a -> b
20maybe :: forall a b. b -> (a -> b) -> Maybe a -> b
21−maybe = \b -> \f -> \m -> case m of
22− Nothing -> b
21+maybe b _ Nothing = b
23− Just a -> f a
22+maybe _ f (Just a) = f a
24
23
25fromMaybe :: forall a. a -> Maybe a -> a
24fromMaybe :: forall a. a -> Maybe a -> a
26−fromMaybe = \a -> maybe a id
25+fromMaybe a = maybe a id
27
26
28bindMaybe :: forall a b. Maybe a -> (a -> Maybe b) -> Maybe b
27bindMaybe :: 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
30
29
31-- Either
30-- Either
32
31
33data Either a b = Left a | Right b
32data Either a b = Left a | Right b
34
33
35either :: forall a b c. (a -> c) -> (b -> c) -> Either a b -> c
34either :: 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
35+either f _ (Left a) = f a
38− Right b -> g b
36+either _ g (Right b) = g b
39
37
40bindEither :: forall e a b. Either e a -> (a -> Either e b) -> Either e b
38bindEither :: 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)
42
40
43-- Arrays
41-- Arrays
44
42
45head :: forall a. [a] -> a
43head :: forall a. [a] -> a
46−head = \xs -> case xs of
47− x : _ -> x
44+head (x : _) = x
48
45
49headSafe :: forall a. [a] -> Maybe a
46headSafe :: forall a. [a] -> Maybe a
50−headSafe = \xs -> case xs of
51− x : _ -> Just x
47+headSafe (x : _) = Just x
52− _ -> Nothing
48+headSafe _ = Nothing
53
49
54tail :: forall a. [a] -> [a]
50tail :: forall a. [a] -> [a]
55−tail = \xs -> case xs of
56− _ : xs -> xs
51+tail (_ : xs) = xs
57
52
58tailSafe :: forall a. [a] -> Maybe [a]
53tailSafe :: forall a. [a] -> Maybe [a]
59−tailSafe = \xs -> case xs of
60− _ : xs -> Just xs
54+tailSafe (_ : xs) = Just xs
61− _ -> Nothing
55+tailSafe _ = Nothing
62
56
63foreign import map :: forall a b. (a -> b) -> [a] -> [b]
57foreign import map :: forall a b. (a -> b) -> [a] -> [b]
64
58
65foldr :: forall a b. (a -> b -> a) -> a -> [b] -> a
59foldr :: 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
60+foldr f a (b : bs) = f (foldr f a bs) b
68− [] -> a
61+foldr _ a [] = a
69
62
70foldl :: forall a b. (a -> b -> b) -> b -> [a] -> b
63foldl :: forall a b. (a -> b -> b) -> b -> [a] -> b
71−foldl = \f -> \b -> \as -> {
64+foldl f b as = {
72 var result = b;
65 var result = b;
73 foreach (a in as) {
66 foreach (a in as) {
74 result = f a result;
67 result = f a result;
@@ -104,7 +97,7 @@ cons :: forall a. a -> [a] -> [a]
104cons = \a -> concat([a])
97cons = \a -> concat([a])
105
98
106concatMap :: forall a b. [a] -> (a -> [b]) -> [b]
99concatMap :: forall a b. [a] -> (a -> [b]) -> [b]
107−concatMap = \as -> \f -> {
100+concatMap as f = {
108 var result = [];
101 var result = [];
109 foreach (a in as) {
102 foreach (a in as) {
110 result = result `concat` f a;
103 result = result `concat` f a;
@@ -115,12 +108,11 @@ concatMap = \as -> \f -> {
115foreign import filter :: forall a. [a] -> (a -> Boolean) -> [a]
108foreign import filter :: forall a. [a] -> (a -> Boolean) -> [a]
116
109
117empty :: forall a. [a] -> Boolean
110empty :: forall a. [a] -> Boolean
118−empty = \as -> case as of
119− [] -> true
111+empty [] = true
120− _ -> false
112+empty _ = false
121
113
122range :: Number -> Number -> [Number]
114range :: Number -> Number -> [Number]
123−range = \lo -> \hi -> {
115+range lo hi = {
124 var ns = [];
116 var ns = [];
125 for (n <- lo until hi) {
117 for (n <- lo until hi) {
126 ns = push ns n;
118 ns = push ns n;
@@ -129,12 +121,11 @@ range = \lo -> \hi -> {
129 }
121 }
130
122
131zipWith :: forall a b c. (a -> b -> c) -> [a] -> [b] -> [c]
123zipWith :: 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)
124+zipWith f (a:as) (b:bs) = cons (f a b) (zipWith f as bs)
134− _ -> []
125+zipWith _ _ _ = []
135
126
136any :: forall a. (a -> Boolean) -> [a] -> Boolean
127any :: forall a. (a -> Boolean) -> [a] -> Boolean
137−any = \p -> \as -> {
128+any p as = {
138 for (i <- 0 until length as) {
129 for (i <- 0 until length as) {
139 if (p (as !! i)) {
130 if (p (as !! i)) {
140 return true;
131 return true;
@@ -144,7 +135,7 @@ any = \p -> \as -> {
144 }
135 }
145
136
146all :: forall a. (a -> Boolean) -> [a] -> Boolean
137all :: forall a. (a -> Boolean) -> [a] -> Boolean
147−all = \p -> \as -> {
138+all p as = {
148 for (i <- 0 until length as) {
139 for (i <- 0 until length as) {
149 if (!(p (as !! i))) {
140 if (!(p (as !! i))) {
150 return false;
141 return false;
@@ -158,10 +149,10 @@ all = \p -> \as -> {
158type Tuple a b = { fst :: a, snd :: b }
149type Tuple a b = { fst :: a, snd :: b }
159
150
160curry :: forall a b c. (Tuple a b -> c) -> a -> b -> c
151curry :: 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 }
162
153
163uncurry :: forall a b c. (a -> b -> c) -> Tuple a b -> c
154uncurry :: 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
165
156
166tuple :: forall a b. a -> b -> Tuple a b
157tuple :: forall a b. a -> b -> Tuple a b
167tuple = curry id
158tuple = curry id
@@ -170,10 +161,9 @@ zip :: forall a b. [a] -> [b] -> [Tuple a b]
170zip = zipWith tuple
161zip = zipWith tuple
171
162
172unzip :: forall a b. [Tuple a b] -> Tuple [a] [b]
163unzip :: 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
175 { fst = as, snd = bs } -> tuple (cons t.fst as) (cons t.snd bs)
165 { fst = as, snd = bs } -> tuple (cons t.fst as) (cons t.snd bs)
176− [] -> tuple [] []
166+unzip [] = tuple [] []
177
167
178-- Strings
168-- Strings
179
169
src/Language/PureScript/Optimize.hs+9−1
@@ -17,6 +17,7 @@ module Language.PureScript.Optimize (
17) where
17) where
18
18
19import Data.Data
19import Data.Data
20+import Data.Maybe (fromMaybe)
20import Data.Generics
21import Data.Generics
21
22
22import Language.PureScript.Names
23import Language.PureScript.Names
@@ -31,6 +32,12 @@ replaceIdent var1 js = everywhere (mkT replace)
31 replace (JSVar var2) | var1 == var2 = js
32 replace (JSVar var2) | var1 == var2 = js
32 replace other = other
33 replace other = other
33
34
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+
34isReassigned :: (Data d) => Ident -> d -> Bool
41isReassigned :: (Data d) => Ident -> d -> Bool
35isReassigned var1 = everything (||) (mkQ False check)
42isReassigned var1 = everything (||) (mkQ False check)
36 where
43 where
@@ -84,7 +91,8 @@ etaConvert :: JS -> JS
84etaConvert = everywhere (mkT convert)
91etaConvert = everywhere (mkT convert)
85 where
92 where
86 convert :: JS -> JS
93 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)
88 convert js = js
96 convert js = js
89
97
90unThunk :: JS -> JS
98unThunk :: JS -> JS
src/Language/PureScript/TypeChecker/Types.hs+6−3
@@ -451,9 +451,12 @@ inferBinder val (UnaryBinder ctor binder) = do
451 modulePath <- checkModulePath <$> get
451 modulePath <- checkModulePath <$> get
452 case M.lookup (qualify modulePath ctor) (dataConstructors env) of
452 case M.lookup (qualify modulePath ctor) (dataConstructors env) of
453 Just ty -> do
453 Just ty -> do
454+ fn <- replaceAllVarsWithUnknowns ty
455+ case fn of
454− Function [obj] ret <- replaceAllVarsWithUnknowns ty
456+ Function [obj] ret -> do
455− val `subsumes` ret
457+ val `subsumes` ret
456− inferBinder obj binder
458+ inferBinder obj binder
459+ _ -> throwError $ "Constructor " ++ show ctor ++ " is not a unary constructor"
457 _ -> throwError $ "Constructor " ++ show ctor ++ " is not defined"
460 _ -> throwError $ "Constructor " ++ show ctor ++ " is not defined"
458inferBinder val (ObjectBinder props) = do
461inferBinder val (ObjectBinder props) = do
459 row <- fresh
462 row <- fresh