Skip to content

Commit 943bf21

Browse files
committed
Use meaningful function names in tuple pattern tests
1 parent 0c437de commit 943bf21

File tree

4 files changed

+108
-105
lines changed

4 files changed

+108
-105
lines changed

hkmc2/shared/src/test/mlscript/ucs/normalization/UnifySubScrutinees.mls

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,14 @@ fun sum(acc, xs) =
4343
//│ > xs@30 is Cons($param0@31, $param1@32) and
4444
//│ > let x@33 = $param0@31#0
4545
//│ > let xs@34 = $param1@32#0
46-
//│ > else globalThis:block#3#666.sum‹member:sum›(builtin:+#100(acc@29#666, x@33#666), xs@34#666)
46+
//│ > else globalThis:block#3#666.sum‹member:sum›(builtin:+#98(acc@29#666, x@33#666), xs@34#666)
4747
//│ > xs@30 is Nil then acc@29#666
4848
//│ Normalized:
4949
//│ > if
5050
//│ > xs@30 is Cons($param0@31, $param1@32) and
5151
//│ > let x@33 = $param0@31#0
5252
//│ > let xs@34 = $param1@32#0
53-
//│ > else globalThis:block#3#666.sum‹member:sum›(builtin:+#100(acc@29#666, x@33#666), xs@34#666)
53+
//│ > else globalThis:block#3#666.sum‹member:sum›(builtin:+#98(acc@29#666, x@33#666), xs@34#666)
5454
//│ > xs@30 is Nil then acc@29#666
5555

5656
:todo
@@ -70,12 +70,12 @@ fun test(xs) =
7070
//│ > let x@52 = $param0@45#1
7171
//│ > $param1@46 is Cons($param0@48, $param1@49) and
7272
//│ > let y@53 = $param0@48#1
73-
//│ > $param1@49 is Nil then builtin:+#101(x@52#666, y@53#666)
73+
//│ > $param1@49 is Nil then builtin:+#99(x@52#666, y@53#666)
7474
//│ > xs@39 is Some($param0@40) and $param0@40 is Cons($param0@41, $param1@42) and $param0@41 is "mul" and $param1@42 is Cons($param0@45, $param1@46) and
7575
//│ > let x@47 = $param0@45#0
7676
//│ > $param1@46 is Cons($param0@48, $param1@49) and
7777
//│ > let y@50 = $param0@48#0
78-
//│ > $param1@49 is Nil then builtin:*#23(x@47#666, y@50#666)
78+
//│ > $param1@49 is Nil then builtin:*#25(x@47#666, y@50#666)
7979
//│ > xs@39 is Some($param0@40) and $param0@40 is Cons($param0@41, $param1@42) and $param0@41 is "sum" and
8080
//│ > let xs@43 = $param1@42#0
8181
//│ > else globalThis:block#3#666.sum‹member:sum›(0, xs@43#666)
@@ -89,12 +89,12 @@ fun test(xs) =
8989
//│ > let x@52 = $param0@45#1
9090
//│ > $param1@46 is Cons($param0@48, $param1@49) and
9191
//│ > let y@53 = $param0@48#1
92-
//│ > $param1@49 is Nil then builtin:+#101(x@52#666, y@53#666)
92+
//│ > $param1@49 is Nil then builtin:+#99(x@52#666, y@53#666)
9393
//│ > $param0@41 is "mul" and $param1@42 is Cons($param0@45, $param1@46) and
9494
//│ > let x@47 = $param0@45#0
9595
//│ > $param1@46 is Cons($param0@48, $param1@49) and
9696
//│ > let y@50 = $param0@48#0
97-
//│ > $param1@49 is Nil then builtin:*#23(x@47#666, y@50#666)
97+
//│ > $param1@49 is Nil then builtin:*#25(x@47#666, y@50#666)
9898
//│ > $param0@41 is "sum" and
9999
//│ > let xs@43 = $param1@42#0
100100
//│ > else globalThis:block#3#666.sum‹member:sum›(0, xs@43#666)

hkmc2/shared/src/test/mlscript/ucs/normalization/UnifyTupleElements.mls

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ fun sum_options(x, y) =
2424
//│ > let xv@38 = $param0@36#1
2525
//│ > $first1@32 is Some($param0@34) and
2626
//│ > let yv@39 = $param0@34#1
27-
//│ > else builtin:+#98(xv@38#666, yv@39#666)
27+
//│ > else builtin:+#96(xv@38#666, yv@39#666)
2828
//│ > $scrut@31 is []=2 and
2929
//│ > let $first0@33 = $scrut@31#7.0
3030
//│ > let $first1@32 = $scrut@31#8.1
@@ -51,7 +51,7 @@ fun sum_options(x, y) =
5151
//│ > let xv@38 = $param0@36#1
5252
//│ > $first1@32 is Some($param0@34) and
5353
//│ > let yv@39 = $param0@34#1
54-
//│ > else builtin:+#98(xv@38#666, yv@39#666)
54+
//│ > else builtin:+#96(xv@38#666, yv@39#666)
5555
//│ > let xv@37 = $param0@36#0
5656
//│ > $first1@32 is None then xv@37#666
5757
//│ > $first0@33 is None and
@@ -118,7 +118,7 @@ fun foo(zs) = if zs is
118118
//│ > let x@65 = $param0@64#0
119119
//│ > $first1@63 is Some($param0@66) and
120120
//│ > let y@67 = $param0@66#0
121-
//│ > else builtin:+#99(x@65#666, y@67#666)
121+
//│ > else builtin:+#97(x@65#666, y@67#666)
122122
//│ > zs@58 is []>=1 and
123123
//│ > let $first0@60 = zs@58#1.0
124124
//│ > let $rest@59 = globalThis:import#Tuple#666.tupleSlice‹member:tupleSlice›(zs@58#2, 1, 0)
@@ -134,7 +134,7 @@ fun foo(zs) = if zs is
134134
//│ > let x@65 = $param0@64#0
135135
//│ > $first1@63 is Some($param0@66) and
136136
//│ > let y@67 = $param0@66#0
137-
//│ > else builtin:+#99(x@65#666, y@67#666)
137+
//│ > else builtin:+#97(x@65#666, y@67#666)
138138
//│ > let $rest@59 = globalThis:import#Tuple#666.tupleSlice‹member:tupleSlice›(zs@58#2, 1, 0)
139139
//│ > let $rest@59 = globalThis:import#Tuple#666.tupleSlice‹member:tupleSlice›(zs@58#2, 1, 0)
140140
//│ > $first0@60 is None and

hkmc2/shared/src/test/mlscript/ucs/patterns/RestTuple.mls

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ tupleGet([1, 2, 3, 4], -1)
1717
//│ = 4
1818

1919
:sjs
20-
fun stupid(xs) = if xs is
20+
fun nonsense(xs) = if xs is
2121
[..ys] then ys
2222
[] then "empty"
2323
//│ Desugared:
@@ -28,7 +28,7 @@ fun stupid(xs) = if xs is
2828
//│ > else ys@43#666
2929
//│ > xs@41 is []=0 then "empty"
3030
//│ JS:
31-
//│ function stupid(xs) {
31+
//│ function nonsense(xs) {
3232
//│ let rest, ys;
3333
//│ if (Array.isArray(xs) && xs.length >= 0) {
3434
//│ rest = globalThis.tupleSlice(xs, 0, 0);
@@ -40,14 +40,14 @@ fun stupid(xs) = if xs is
4040
//│ };
4141
//│ undefined
4242

43-
stupid([])
43+
nonsense([])
4444
//│ = []
4545

46-
stupid([1, 2, 3, 4])
46+
nonsense([1, 2, 3, 4])
4747
//│ = [ 1, 2, 3, 4 ]
4848

4949
:sjs
50-
fun foo(xs) = if xs is
50+
fun lead_and_last(xs) = if xs is
5151
[x, ..ys, y] then x + y
5252
[] then 0
5353
//│ Desugared:
@@ -59,10 +59,10 @@ fun foo(xs) = if xs is
5959
//│ > let x@57 = $first0@56#0
6060
//│ > let ys@58 = $rest@55#0
6161
//│ > let y@59 = $last0@54#0
62-
//│ > else builtin:+#94(x@57#666, y@59#666)
62+
//│ > else builtin:+#92(x@57#666, y@59#666)
6363
//│ > xs@53 is []=0 then 0
6464
//│ JS:
65-
//│ function foo(xs) {
65+
//│ function lead_and_last(xs) {
6666
//│ let last0, rest, first0, x, ys, y;
6767
//│ if (Array.isArray(xs) && xs.length >= 2) {
6868
//│ first0 = xs[0];
@@ -82,21 +82,20 @@ fun foo(xs) = if xs is
8282
//│ };
8383
//│ undefined
8484

85-
foo(["foo", "bar"])
85+
lead_and_last(["foo", "bar"])
8686
//│ = 'foobar'
8787

88-
foo([1, 2, 3, 4])
88+
lead_and_last([1, 2, 3, 4])
8989
//│ = 5
9090

91-
foo([])
91+
lead_and_last([])
9292
//│ = 0
9393

9494
:re
95-
foo(["boom"])
95+
lead_and_last(["boom"])
9696
//│ ═══[RUNTIME ERROR] Error: match error
9797

9898
:sjs
99-
:todo // Better using ... instead of .. for the rest pattern.
10099
fun nested_tuple_patterns(xs) = if xs is
101100
[x, ..[y, z], w] then x + y + z + w
102101
[] then 0
@@ -113,7 +112,7 @@ fun nested_tuple_patterns(xs) = if xs is
113112
//│ > let y@85 = $first0@84#0
114113
//│ > let z@86 = $first1@83#0
115114
//│ > let w@87 = $last0@79#0
116-
//│ > else builtin:+#95(builtin:+#96(builtin:+#97(x@82#666, y@85#666), z@86#666), w@87#666)
115+
//│ > else builtin:+#93(builtin:+#94(builtin:+#95(x@82#666, y@85#666), z@86#666), w@87#666)
117116
//│ > xs@78 is []=0 then 0
118117
//│ JS:
119118
//│ function nested_tuple_patterns(xs) {

hkmc2/shared/src/test/mlscript/ucs/patterns/SimpleTuple.mls

Lines changed: 86 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -39,139 +39,143 @@ test("")
3939
test(12)
4040
//│ ═══[RUNTIME ERROR] Error: match error
4141

42-
:ucs desugared
4342
class Point(x: Int, y: Int)
44-
fun discarded_cases(thing) =
43+
44+
:ucs desugared
45+
fun with_other_constructors(thing) =
4546
if thing is
46-
[x, y] then x + y
47+
[x, y] then x * y
4748
Point(x, y) then x + y
4849
//│ Desugared:
4950
//│ > if
50-
//│ > thing@40 is []=2 and
51-
//│ > let $first0@47 = thing@40#2.0
52-
//│ > let $first1@46 = thing@40#3.1
53-
//│ > let x@48 = $first0@47#0
54-
//│ > let y@49 = $first1@46#0
55-
//│ > else builtin:+#82(x@48#666, y@49#666)
56-
//│ > thing@40 is Point($param0@41, $param1@42) and
57-
//│ > let x@43 = $param0@41#0
58-
//│ > let y@44 = $param1@42#0
59-
//│ > else builtin:+#81(x@43#666, y@44#666)
60-
61-
:e
62-
:todo
63-
discarded_cases(Point(0, 0))
64-
//│ = 0
51+
//│ > thing@41 is []=2 and
52+
//│ > let $first0@48 = thing@41#2.0
53+
//│ > let $first1@47 = thing@41#3.1
54+
//│ > let x@49 = $first0@48#0
55+
//│ > let y@50 = $first1@47#0
56+
//│ > else builtin:*#23(x@49#666, y@50#666)
57+
//│ > thing@41 is Point($param0@42, $param1@43) and
58+
//│ > let x@44 = $param0@42#0
59+
//│ > let y@45 = $param1@43#0
60+
//│ > else builtin:+#81(x@44#666, y@45#666)
61+
62+
:expect 7
63+
with_other_constructors(Point(3, 4))
64+
//│ = 7
65+
66+
:expect 12
67+
with_other_constructors([3, 4])
68+
//│ = 12
6569

6670
// A workaround is to move the tuple pattern to the last case.
6771
:ucs desugared
68-
fun working_cases(thing) =
72+
fun with_other_constructors(thing) =
6973
if thing is
7074
Point(x, y) then x + y
71-
[x, y] then x + y
75+
[x, y] then x * y
7276
//│ Desugared:
7377
//│ > if
74-
//│ > thing@61 is Point($param0@67, $param1@68) and
75-
//│ > let x@69 = $param0@67#0
76-
//│ > let y@70 = $param1@68#0
77-
//│ > else builtin:+#84(x@69#666, y@70#666)
78-
//│ > thing@61 is []=2 and
79-
//│ > let $first0@63 = thing@61#1.0
80-
//│ > let $first1@62 = thing@61#2.1
81-
//│ > let x@64 = $first0@63#0
82-
//│ > let y@65 = $first1@62#0
83-
//│ > else builtin:+#83(x@64#666, y@65#666)
84-
85-
working_cases(Point(0, 0))
86-
//│ = 0
87-
88-
// However, the `Object` type forbids tuples to be used.
89-
:todo
90-
working_cases([0, 0])
91-
//│ = 0
92-
93-
94-
fun not_working(x) =
78+
//│ > thing@65 is Point($param0@71, $param1@72) and
79+
//│ > let x@73 = $param0@71#0
80+
//│ > let y@74 = $param1@72#0
81+
//│ > else builtin:+#82(x@73#666, y@74#666)
82+
//│ > thing@65 is []=2 and
83+
//│ > let $first0@67 = thing@65#1.0
84+
//│ > let $first1@66 = thing@65#2.1
85+
//│ > let x@68 = $first0@67#0
86+
//│ > let y@69 = $first1@66#0
87+
//│ > else builtin:*#24(x@68#666, y@69#666)
88+
89+
:expect 7
90+
with_other_constructors(Point(3, 4))
91+
//│ = 7
92+
93+
:expect 12
94+
with_other_constructors([3, 4])
95+
//│ = 12
96+
97+
98+
fun with_else(x) =
9599
if x is
96100
[a, b, c] then
97101
a + b + c
98102
else
99103
0
100104

101-
not_working([1, 2, 3])
105+
with_else([1, 2, 3])
102106
//│ = 6
103107

104-
not_working([1, 2])
108+
with_else([1, 2])
105109
//│ = 0
106110

107111
:ucs desugared
108-
fun multiple_checks(xs) =
112+
fun match_against_different_length(xs) =
109113
if xs is
110114
[] then 0
111115
[x] then x + 1
112116
[x, y] then x + y + 2
113117
[x, y, z] then x + y + z + 3
114118
//│ Desugared:
115119
//│ > if
116-
//│ > xs@105 is []=0 then 0
117-
//│ > xs@105 is []=1 and
118-
//│ > let $first0@108 = xs@105#8.0
119-
//│ > let x@119 = $first0@108#2
120-
//│ > else builtin:+#92(x@119#666, 1)
121-
//│ > xs@105 is []=2 and
122-
//│ > let $first0@108 = xs@105#5.0
123-
//│ > let $first1@107 = xs@105#6.1
124-
//│ > let x@115 = $first0@108#1
125-
//│ > let y@116 = $first1@107#1
126-
//│ > else builtin:+#90(builtin:+#91(x@115#666, y@116#666), 2)
127-
//│ > xs@105 is []=3 and
128-
//│ > let $first0@108 = xs@105#1.0
129-
//│ > let $first1@107 = xs@105#2.1
130-
//│ > let $first2@106 = xs@105#3.2
131-
//│ > let x@109 = $first0@108#0
132-
//│ > let y@110 = $first1@107#0
133-
//│ > let z@111 = $first2@106#0
134-
//│ > else builtin:+#87(builtin:+#88(builtin:+#89(x@109#666, y@110#666), z@111#666), 3)
120+
//│ > xs@109 is []=0 then 0
121+
//│ > xs@109 is []=1 and
122+
//│ > let $first0@112 = xs@109#8.0
123+
//│ > let x@123 = $first0@112#2
124+
//│ > else builtin:+#90(x@123#666, 1)
125+
//│ > xs@109 is []=2 and
126+
//│ > let $first0@112 = xs@109#5.0
127+
//│ > let $first1@111 = xs@109#6.1
128+
//│ > let x@119 = $first0@112#1
129+
//│ > let y@120 = $first1@111#1
130+
//│ > else builtin:+#88(builtin:+#89(x@119#666, y@120#666), 2)
131+
//│ > xs@109 is []=3 and
132+
//│ > let $first0@112 = xs@109#1.0
133+
//│ > let $first1@111 = xs@109#2.1
134+
//│ > let $first2@110 = xs@109#3.2
135+
//│ > let x@113 = $first0@112#0
136+
//│ > let y@114 = $first1@111#0
137+
//│ > let z@115 = $first2@110#0
138+
//│ > else builtin:+#85(builtin:+#86(builtin:+#87(x@113#666, y@114#666), z@115#666), 3)
135139

136140
:expect 0
137-
multiple_checks([])
141+
match_against_different_length([])
138142
//│ = 0
139143

140144
:expect 18
141-
multiple_checks([17])
145+
match_against_different_length([17])
142146
//│ = 18
143147

144148
:expect 22
145-
multiple_checks([9, 11])
149+
match_against_different_length([9, 11])
146150
//│ = 22
147151

148152
:expect 42
149-
multiple_checks([13, 13, 13])
153+
match_against_different_length([13, 13, 13])
150154
//│ = 42
151155

152156
:import ../Prelude/Option.mls
153157
//│ Imported 3 member(s)
154158

155159
:ucs desugared normalized
156160
:todo // Sub-scrutinees are not memorized thus the normalization is not correct.
157-
fun multiple_checks(xs) =
161+
fun with_the_common_prefix(xs) =
158162
if xs is
159163
[Some(x)] then x + 1
160164
[None] then 0
161165
//│ Desugared:
162166
//│ > if
163-
//│ > xs@147 is []=1 and
164-
//│ > let $first0@148 = xs@147#3.0
165-
//│ > $first0@148 is Some($param0@149) and
166-
//│ > let x@150 = $param0@149#0
167-
//│ > else builtin:+#93(x@150#666, 1)
168-
//│ > xs@147 is []=1 and
169-
//│ > let $first0@148 = xs@147#1.0
170-
//│ > $first0@148 is None then 0
167+
//│ > xs@151 is []=1 and
168+
//│ > let $first0@152 = xs@151#3.0
169+
//│ > $first0@152 is Some($param0@153) and
170+
//│ > let x@154 = $param0@153#0
171+
//│ > else builtin:+#91(x@154#666, 1)
172+
//│ > xs@151 is []=1 and
173+
//│ > let $first0@152 = xs@151#1.0
174+
//│ > $first0@152 is None then 0
171175
//│ Normalized:
172-
//│ > if xs@147 is []=1 and
173-
//│ > let $first0@148 = xs@147#3.0
174-
//│ > $first0@148 is Some($param0@149) and
175-
//│ > let x@150 = $param0@149#0
176-
//│ > else builtin:+#93(x@150#666, 1)
177-
//│ > $first0@148 is None then 0
176+
//│ > if xs@151 is []=1 and
177+
//│ > let $first0@152 = xs@151#3.0
178+
//│ > $first0@152 is Some($param0@153) and
179+
//│ > let x@154 = $param0@153#0
180+
//│ > else builtin:+#91(x@154#666, 1)
181+
//│ > $first0@152 is None then 0

0 commit comments

Comments
 (0)