@@ -39,139 +39,143 @@ test("")
39
39
test(12)
40
40
//│ ═══[RUNTIME ERROR] Error: match error
41
41
42
- :ucs desugared
43
42
class Point(x: Int, y: Int)
44
- fun discarded_cases(thing) =
43
+
44
+ :ucs desugared
45
+ fun with_other_constructors(thing) =
45
46
if thing is
46
- [x, y] then x + y
47
+ [x, y] then x * y
47
48
Point(x, y) then x + y
48
49
//│ Desugared:
49
50
//│ > if
50
- //│ > thing@58 is []=2 and
51
- //│ > let $first0@65 = thing@58#2.0
52
- //│ > let $first1@64 = thing@58#3.1
53
- //│ > let x@66 = $first0@65#0
54
- //│ > let y@67 = $first1@64#0
55
- //│ > else globalThis:import#Prelude#666.+‹member:+›(x@66#666, y@67#666)
56
- //│ > thing@58 is Point($param0@59, $param1@60) and
57
- //│ > let x@61 = $param0@59#0
58
- //│ > let y@62 = $param1@60#0
59
- //│ > else globalThis:import#Prelude#666.+‹member:+›(x@61#666, y@62#666)
60
-
61
- :e
62
- :todo
63
- discarded_cases(Point(0, 0))
64
- //│ = 0
51
+ //│ > thing@59 is []=2 and
52
+ //│ > let $first0@66 = thing@59#2.0
53
+ //│ > let $first1@65 = thing@59#3.1
54
+ //│ > let x@67 = $first0@66#0
55
+ //│ > let y@68 = $first1@65#0
56
+ //│ > else globalThis:import#Prelude#666.*‹member:*›(x@67#666, y@68#666)
57
+ //│ > thing@59 is Point($param0@60, $param1@61) and
58
+ //│ > let x@62 = $param0@60#0
59
+ //│ > let y@63 = $param1@61#0
60
+ //│ > else globalThis:import#Prelude#666.+‹member:+›(x@62#666, y@63#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
65
69
66
70
// A workaround is to move the tuple pattern to the last case.
67
71
:ucs desugared
68
- fun working_cases (thing) =
72
+ fun with_other_constructors (thing) =
69
73
if thing is
70
74
Point(x, y) then x + y
71
- [x, y] then x + y
75
+ [x, y] then x * y
72
76
//│ Desugared:
73
77
//│ > if
74
- //│ > thing@79 is Point($param0@85 , $param1@86 ) and
75
- //│ > let x@87 = $param0@85 #0
76
- //│ > let y@88 = $param1@86 #0
77
- //│ > else globalThis:import#Prelude#666.+‹member:+›(x@87 #666, y@88 #666)
78
- //│ > thing@79 is []=2 and
79
- //│ > let $first0@81 = thing@79 #1.0
80
- //│ > let $first1@80 = thing@79 #2.1
81
- //│ > let x@82 = $first0@81 #0
82
- //│ > let y@83 = $first1@80 #0
83
- //│ > else globalThis:import#Prelude#666.+ ‹member:+ ›(x@82 #666, y@83 #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@83 is Point($param0@89 , $param1@90 ) and
79
+ //│ > let x@91 = $param0@89 #0
80
+ //│ > let y@92 = $param1@90 #0
81
+ //│ > else globalThis:import#Prelude#666.+‹member:+›(x@91 #666, y@92 #666)
82
+ //│ > thing@83 is []=2 and
83
+ //│ > let $first0@85 = thing@83 #1.0
84
+ //│ > let $first1@84 = thing@83 #2.1
85
+ //│ > let x@86 = $first0@85 #0
86
+ //│ > let y@87 = $first1@84 #0
87
+ //│ > else globalThis:import#Prelude#666.* ‹member:* ›(x@86 #666, y@87 #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) =
95
99
if x is
96
100
[a, b, c] then
97
101
a + b + c
98
102
else
99
103
0
100
104
101
- not_working ([1, 2, 3])
105
+ with_else ([1, 2, 3])
102
106
//│ = 6
103
107
104
- not_working ([1, 2])
108
+ with_else ([1, 2])
105
109
//│ = 0
106
110
107
111
:ucs desugared
108
- fun multiple_checks (xs) =
112
+ fun match_against_different_length (xs) =
109
113
if xs is
110
114
[] then 0
111
115
[x] then x + 1
112
116
[x, y] then x + y + 2
113
117
[x, y, z] then x + y + z + 3
114
118
//│ Desugared:
115
119
//│ > if
116
- //│ > xs@123 is []=0 then 0
117
- //│ > xs@123 is []=1 and
118
- //│ > let $first0@126 = xs@123 #8.0
119
- //│ > let x@137 = $first0@126 #2
120
- //│ > else globalThis:import#Prelude#666.+‹member:+›(x@137 #666, 1)
121
- //│ > xs@123 is []=2 and
122
- //│ > let $first0@126 = xs@123 #5.0
123
- //│ > let $first1@125 = xs@123 #6.1
124
- //│ > let x@133 = $first0@126 #1
125
- //│ > let y@134 = $first1@125 #1
126
- //│ > else globalThis:import#Prelude#666.+‹member:+›(globalThis:import#Prelude#666.+‹member:+›(x@133 #666, y@134 #666), 2)
127
- //│ > xs@123 is []=3 and
128
- //│ > let $first0@126 = xs@123 #1.0
129
- //│ > let $first1@125 = xs@123 #2.1
130
- //│ > let $first2@124 = xs@123 #3.2
131
- //│ > let x@127 = $first0@126 #0
132
- //│ > let y@128 = $first1@125 #0
133
- //│ > let z@129 = $first2@124 #0
134
- //│ > else globalThis:import#Prelude#666.+‹member:+›(globalThis:import#Prelude#666.+‹member:+›(globalThis:import#Prelude#666.+‹member:+›(x@127 #666, y@128 #666), z@129 #666), 3)
120
+ //│ > xs@127 is []=0 then 0
121
+ //│ > xs@127 is []=1 and
122
+ //│ > let $first0@130 = xs@127 #8.0
123
+ //│ > let x@141 = $first0@130 #2
124
+ //│ > else globalThis:import#Prelude#666.+‹member:+›(x@141 #666, 1)
125
+ //│ > xs@127 is []=2 and
126
+ //│ > let $first0@130 = xs@127 #5.0
127
+ //│ > let $first1@129 = xs@127 #6.1
128
+ //│ > let x@137 = $first0@130 #1
129
+ //│ > let y@138 = $first1@129 #1
130
+ //│ > else globalThis:import#Prelude#666.+‹member:+›(globalThis:import#Prelude#666.+‹member:+›(x@137 #666, y@138 #666), 2)
131
+ //│ > xs@127 is []=3 and
132
+ //│ > let $first0@130 = xs@127 #1.0
133
+ //│ > let $first1@129 = xs@127 #2.1
134
+ //│ > let $first2@128 = xs@127 #3.2
135
+ //│ > let x@131 = $first0@130 #0
136
+ //│ > let y@132 = $first1@129 #0
137
+ //│ > let z@133 = $first2@128 #0
138
+ //│ > else globalThis:import#Prelude#666.+‹member:+›(globalThis:import#Prelude#666.+‹member:+›(globalThis:import#Prelude#666.+‹member:+›(x@131 #666, y@132 #666), z@133 #666), 3)
135
139
136
140
:expect 0
137
- multiple_checks ([])
141
+ match_against_different_length ([])
138
142
//│ = 0
139
143
140
144
:expect 18
141
- multiple_checks ([17])
145
+ match_against_different_length ([17])
142
146
//│ = 18
143
147
144
148
:expect 22
145
- multiple_checks ([9, 11])
149
+ match_against_different_length ([9, 11])
146
150
//│ = 22
147
151
148
152
:expect 42
149
- multiple_checks ([13, 13, 13])
153
+ match_against_different_length ([13, 13, 13])
150
154
//│ = 42
151
155
152
156
:import ../Prelude/Option.mls
153
157
//│ Imported 3 member(s)
154
158
155
159
:ucs desugared normalized
156
160
:todo // Sub-scrutinees are not memorized thus the normalization is not correct.
157
- fun multiple_checks (xs) =
161
+ fun with_the_common_prefix (xs) =
158
162
if xs is
159
163
[Some(x)] then x + 1
160
164
[None] then 0
161
165
//│ Desugared:
162
166
//│ > if
163
- //│ > xs@165 is []=1 and
164
- //│ > let $first0@166 = xs@165 #3.0
165
- //│ > $first0@166 is Some($param0@167 ) and
166
- //│ > let x@168 = $param0@167 #0
167
- //│ > else globalThis:import#Prelude#666.+‹member:+›(x@168 #666, 1)
168
- //│ > xs@165 is []=1 and
169
- //│ > let $first0@166 = xs@165 #1.0
170
- //│ > $first0@166 is None then 0
167
+ //│ > xs@169 is []=1 and
168
+ //│ > let $first0@170 = xs@169 #3.0
169
+ //│ > $first0@170 is Some($param0@171 ) and
170
+ //│ > let x@172 = $param0@171 #0
171
+ //│ > else globalThis:import#Prelude#666.+‹member:+›(x@172 #666, 1)
172
+ //│ > xs@169 is []=1 and
173
+ //│ > let $first0@170 = xs@169 #1.0
174
+ //│ > $first0@170 is None then 0
171
175
//│ Normalized:
172
- //│ > if xs@165 is []=1 and
173
- //│ > let $first0@166 = xs@165 #3.0
174
- //│ > $first0@166 is Some($param0@167 ) and
175
- //│ > let x@168 = $param0@167 #0
176
- //│ > else globalThis:import#Prelude#666.+‹member:+›(x@168 #666, 1)
177
- //│ > $first0@166 is None then 0
176
+ //│ > if xs@169 is []=1 and
177
+ //│ > let $first0@170 = xs@169 #3.0
178
+ //│ > $first0@170 is Some($param0@171 ) and
179
+ //│ > let x@172 = $param0@171 #0
180
+ //│ > else globalThis:import#Prelude#666.+‹member:+›(x@172 #666, 1)
181
+ //│ > $first0@170 is None then 0
0 commit comments