Skip to content

Commit de16b05

Browse files
chengluyuLPTK
authored andcommitted
Reduce the abuse of UCS debug commands
1 parent 1cf5a60 commit de16b05

File tree

6 files changed

+2
-343
lines changed

6 files changed

+2
-343
lines changed

hkmc2/shared/src/test/mlscript/ucs/general/CardSuits.mls

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -13,28 +13,12 @@ fun suit(x) = if x is Card(a) then a
1313
let card = Card(Heart)
1414
suit of card
1515

16-
:ucs desugared normalized
16+
// :ucs desugared normalized
1717
fun foo(x) = if x is
1818
Card(Heart) then 0
1919
Card(Diamond) then 1
2020
Card(Spade) then 2
2121
Card(Club) then 3
2222
else 4
23-
//│ Desugared:
24-
//│ > if
25-
//│ > x@22 is Card($param0@23) $param0@23 is Heart then 0
26-
//│ > x@22 is Card($param0@23) $param0@23 is Diamond then 1
27-
//│ > x@22 is Card($param0@23) $param0@23 is Spade then 2
28-
//│ > x@22 is Card($param0@23) $param0@23 is Club then 3
29-
//│ > else 4
30-
//│ Normalized:
31-
//│ > if
32-
//│ > x@22 is Card($param0@23) and
33-
//│ > $param0@23 is Heart then 0
34-
//│ > $param0@23 is Diamond then 1
35-
//│ > $param0@23 is Spade then 2
36-
//│ > $param0@23 is Club then 3
37-
//│ > else 4
38-
//│ > else 4
3923

4024

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

Lines changed: 0 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -4,99 +4,23 @@ import "../../../mlscript-compile/Option.mls"
44
open Stack
55
open Option
66

7-
:global
8-
:ucs desugared normalized
9-
107
class Pair[A, B](x: A, y: B)
118

129
fun foo(xs) =
1310
if xs is
1411
"abc" :: "xyz" :: xs then 0
1512
"abc" :: "uvw" :: xs then 1
1613
Nil then 3
17-
//│ Desugared:
18-
//│ > if
19-
//│ > xs@16 is Cons($param0@17, $param1@18) and $param0@17 is "abc" and $param1@18 is Cons($param0@19, $param1@20) and $param0@19 is "xyz" and
20-
//│ > let xs@22 = $param1@20#1
21-
//│ > else 0
22-
//│ > xs@16 is Cons($param0@17, $param1@18) and $param0@17 is "abc" and $param1@18 is Cons($param0@19, $param1@20) and $param0@19 is "uvw" and
23-
//│ > let xs@21 = $param1@20#0
24-
//│ > else 1
25-
//│ > xs@16 is Nil then 3
26-
//│ Normalized:
27-
//│ > if
28-
//│ > xs@16 is Cons($param0@17, $param1@18) and $param0@17 is "abc" and $param1@18 is Cons($param0@19, $param1@20) and
29-
//│ > $param0@19 is "xyz" and
30-
//│ > let xs@22 = $param1@20#1
31-
//│ > else 0
32-
//│ > $param0@19 is "uvw" and
33-
//│ > let xs@21 = $param1@20#0
34-
//│ > else 1
35-
//│ > xs@16 is Nil then 3
3614

3715
fun sum(acc, xs) =
3816
if xs is
3917
x :: xs then sum(acc + x, xs)
4018
Nil then acc
41-
//│ Desugared:
42-
//│ > if
43-
//│ > xs@26 is Cons($param0@27, $param1@28) and
44-
//│ > let x@29 = $param0@27#0
45-
//│ > let xs@30 = $param1@28#0
46-
//│ > else globalThis:block#5#666.sum‹member:sum›(builtin:+#0(acc@25#666, x@29#666), xs@30#666)
47-
//│ > xs@26 is Nil then acc@25#666
48-
//│ Normalized:
49-
//│ > if
50-
//│ > xs@26 is Cons($param0@27, $param1@28) and
51-
//│ > let x@29 = $param0@27#0
52-
//│ > let xs@30 = $param1@28#0
53-
//│ > else globalThis:block#5#666.sum‹member:sum›(builtin:+#0(acc@25#666, x@29#666), xs@30#666)
54-
//│ > xs@26 is Nil then acc@25#666
5519

56-
:todo
57-
// The normalization does not distinguish between bindings and their aliases.
58-
// We may still need to substitute variables when merging branches or reuse
59-
// bindings at the same location.
6020
fun test(xs) =
6121
if xs is
6222
Some(Cons("add", Cons(x, Cons(y, Nil)))) then x + y
6323
Some(Cons("mul", Cons(x, Cons(y, Nil)))) then x * y
6424
Some(Cons("sum", xs)) then sum(0, xs)
6525
Some(Nil) then "nothing"
6626
None then "nothing"
67-
//│ Desugared:
68-
//│ > if
69-
//│ > xs@35 is Some($param0@36) and $param0@36 is Cons($param0@37, $param1@38) and $param0@37 is "add" and $param1@38 is Cons($param0@41, $param1@42) and
70-
//│ > let x@48 = $param0@41#1
71-
//│ > $param1@42 is Cons($param0@44, $param1@45) and
72-
//│ > let y@49 = $param0@44#1
73-
//│ > $param1@45 is Nil then builtin:+#0(x@48#666, y@49#666)
74-
//│ > xs@35 is Some($param0@36) and $param0@36 is Cons($param0@37, $param1@38) and $param0@37 is "mul" and $param1@38 is Cons($param0@41, $param1@42) and
75-
//│ > let x@43 = $param0@41#0
76-
//│ > $param1@42 is Cons($param0@44, $param1@45) and
77-
//│ > let y@46 = $param0@44#0
78-
//│ > $param1@45 is Nil then builtin:*#0(x@43#666, y@46#666)
79-
//│ > xs@35 is Some($param0@36) and $param0@36 is Cons($param0@37, $param1@38) and $param0@37 is "sum" and
80-
//│ > let xs@39 = $param1@38#0
81-
//│ > else globalThis:block#5#666.sum‹member:sum›(0, xs@39#666)
82-
//│ > xs@35 is Some($param0@36) $param0@36 is Nil then "nothing"
83-
//│ > xs@35 is None then "nothing"
84-
//│ Normalized:
85-
//│ > if
86-
//│ > xs@35 is Some($param0@36) and
87-
//│ > $param0@36 is Cons($param0@37, $param1@38) and
88-
//│ > $param0@37 is "add" and $param1@38 is Cons($param0@41, $param1@42) and
89-
//│ > let x@48 = $param0@41#1
90-
//│ > $param1@42 is Cons($param0@44, $param1@45) and
91-
//│ > let y@49 = $param0@44#1
92-
//│ > $param1@45 is Nil then builtin:+#0(x@48#666, y@49#666)
93-
//│ > $param0@37 is "mul" and $param1@38 is Cons($param0@41, $param1@42) and
94-
//│ > let x@43 = $param0@41#0
95-
//│ > $param1@42 is Cons($param0@44, $param1@45) and
96-
//│ > let y@46 = $param0@44#0
97-
//│ > $param1@45 is Nil then builtin:*#0(x@43#666, y@46#666)
98-
//│ > $param0@37 is "sum" and
99-
//│ > let xs@39 = $param1@38#0
100-
//│ > else globalThis:block#5#666.sum‹member:sum›(0, xs@39#666)
101-
//│ > $param0@36 is Nil then "nothing"
102-
//│ > xs@35 is None then "nothing"

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

Lines changed: 0 additions & 112 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
:js
2-
:ucs desugared normalized
32

43
import "../../../mlscript-compile/Option.mls"
54

@@ -12,51 +11,6 @@ fun sum_options(x, y) =
1211
[Some(xv), None] then xv
1312
[None, Some(yv)] then yv
1413
[None, None] then 0
15-
//│ Desugared:
16-
//│ > if
17-
//│ > let $scrut@15 = [x@13#666, y@14#666]
18-
//│ > $scrut@15 is []=2 and
19-
//│ > let $first0@17 = $scrut@15#10.0
20-
//│ > let $first1@16 = $scrut@15#11.1
21-
//│ > $first0@17 is Some($param0@20) and
22-
//│ > let xv@22 = $param0@20#1
23-
//│ > $first1@16 is Some($param0@18) and
24-
//│ > let yv@23 = $param0@18#1
25-
//│ > else builtin:+#0(xv@22#666, yv@23#666)
26-
//│ > $scrut@15 is []=2 and
27-
//│ > let $first0@17 = $scrut@15#7.0
28-
//│ > let $first1@16 = $scrut@15#8.1
29-
//│ > $first0@17 is Some($param0@20) and
30-
//│ > let xv@21 = $param0@20#0
31-
//│ > $first1@16 is None then xv@21#666
32-
//│ > $scrut@15 is []=2 and
33-
//│ > let $first0@17 = $scrut@15#4.0
34-
//│ > let $first1@16 = $scrut@15#5.1
35-
//│ > $first0@17 is None and $first1@16 is Some($param0@18) and
36-
//│ > let yv@19 = $param0@18#0
37-
//│ > else yv@19#666
38-
//│ > $scrut@15 is []=2 and
39-
//│ > let $first0@17 = $scrut@15#1.0
40-
//│ > let $first1@16 = $scrut@15#2.1
41-
//│ > $first0@17 is None $first1@16 is None then 0
42-
//│ Normalized:
43-
//│ > if
44-
//│ > let $scrut@15 = [x@13#666, y@14#666]
45-
//│ > $scrut@15 is []=2 and
46-
//│ > let $first0@17 = $scrut@15#10.0
47-
//│ > let $first1@16 = $scrut@15#11.1
48-
//│ > $first0@17 is Some($param0@20) and
49-
//│ > let xv@22 = $param0@20#1
50-
//│ > $first1@16 is Some($param0@18) and
51-
//│ > let yv@23 = $param0@18#1
52-
//│ > else builtin:+#0(xv@22#666, yv@23#666)
53-
//│ > let xv@21 = $param0@20#0
54-
//│ > $first1@16 is None then xv@21#666
55-
//│ > $first0@17 is None and
56-
//│ > $first1@16 is Some($param0@18) and
57-
//│ > let yv@19 = $param0@18#0
58-
//│ > else yv@19#666
59-
//│ > $first1@16 is None then 0
6014

6115

6216

@@ -71,79 +25,13 @@ fun sum_options(x, y) =
7125
fun foo(xs) = if xs is
7226
[Some(x)] then x
7327
[Some(y), ..rest] then y
74-
//│ Desugared:
75-
//│ > if
76-
//│ > xs@28 is []=1 and
77-
//│ > let $first0@30 = xs@28#4.0
78-
//│ > $first0@30 is Some($param0@31) and
79-
//│ > let x@34 = $param0@31#1
80-
//│ > else x@34#666
81-
//│ > xs@28 is []>=1 and
82-
//│ > let $first0@30 = xs@28#1.0
83-
//│ > let $rest@29 = globalThis:globalThis#666.Predef.tupleSlice(xs@28#2, 1, 0)
84-
//│ > $first0@30 is Some($param0@31) and
85-
//│ > let y@32 = $param0@31#0
86-
//│ > let rest@33 = $rest@29#0
87-
//│ > else y@32#666
88-
//│ Normalized:
89-
//│ > if
90-
//│ > xs@28 is []=1 and
91-
//│ > let $first0@30 = xs@28#4.0
92-
//│ > $first0@30 is Some($param0@31) and
93-
//│ > let x@34 = $param0@31#1
94-
//│ > else x@34#666
95-
//│ > let $rest@29 = globalThis:globalThis#666.Predef.tupleSlice(xs@28#2, 1, 0)
96-
//│ > xs@28 is []>=1 and
97-
//│ > let $first0@30 = xs@28#1.0
98-
//│ > let $rest@29 = globalThis:globalThis#666.Predef.tupleSlice(xs@28#2, 1, 0)
99-
//│ > $first0@30 is Some($param0@31) and
100-
//│ > let y@32 = $param0@31#0
101-
//│ > let rest@33 = $rest@29#0
102-
//│ > else y@32#666
10328

10429
fun do_something(x) = "done"
10530

10631

10732
fun foo(zs) = if zs is
10833
[Some(x), Some(y)] then x + y
10934
[None, ..rest] then do_something(rest)
110-
//│ Desugared:
111-
//│ > if
112-
//│ > zs@42 is []=2 and
113-
//│ > let $first0@44 = zs@42#4.0
114-
//│ > let $first1@47 = zs@42#5.1
115-
//│ > $first0@44 is Some($param0@48) and
116-
//│ > let x@49 = $param0@48#0
117-
//│ > $first1@47 is Some($param0@50) and
118-
//│ > let y@51 = $param0@50#0
119-
//│ > else builtin:+#0(x@49#666, y@51#666)
120-
//│ > zs@42 is []>=1 and
121-
//│ > let $first0@44 = zs@42#1.0
122-
//│ > let $rest@43 = globalThis:globalThis#666.Predef.tupleSlice(zs@42#2, 1, 0)
123-
//│ > $first0@44 is None and
124-
//│ > let rest@45 = $rest@43#0
125-
//│ > else globalThis:block#5#666.do_something‹member:do_something›(rest@45#666)
126-
//│ Normalized:
127-
//│ > if
128-
//│ > zs@42 is []=2 and
129-
//│ > let $first0@44 = zs@42#4.0
130-
//│ > let $first1@47 = zs@42#5.1
131-
//│ > $first0@44 is Some($param0@48) and
132-
//│ > let x@49 = $param0@48#0
133-
//│ > $first1@47 is Some($param0@50) and
134-
//│ > let y@51 = $param0@50#0
135-
//│ > else builtin:+#0(x@49#666, y@51#666)
136-
//│ > let $rest@43 = globalThis:globalThis#666.Predef.tupleSlice(zs@42#2, 1, 0)
137-
//│ > let $rest@43 = globalThis:globalThis#666.Predef.tupleSlice(zs@42#2, 1, 0)
138-
//│ > $first0@44 is None and
139-
//│ > let rest@45 = $rest@43#0
140-
//│ > else globalThis:block#5#666.do_something‹member:do_something›(rest@45#666)
141-
//│ > zs@42 is []>=1 and
142-
//│ > let $first0@44 = zs@42#1.0
143-
//│ > let $rest@43 = globalThis:globalThis#666.Predef.tupleSlice(zs@42#2, 1, 0)
144-
//│ > $first0@44 is None and
145-
//│ > let rest@45 = $rest@43#0
146-
//│ > else globalThis:block#5#666.do_something‹member:do_something›(rest@45#666)
14735

14836
:expect 4
14937
foo([Some(1), Some(3)])

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

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,16 @@
1-
:ucs desugared
2-
31
import "../../../mlscript-compile/Option.mls"
42

53
open Option
64

75
fun map(f) = case
86
Some(x) then Some(f(x))
97
None as n then n
10-
//│ Desugared:
11-
//│ > if
12-
//│ > caseScrut@14 is Some($param0@16) and
13-
//│ > let x@17 = $param0@16#0
14-
//│ > else member:Option#666.Some‹member:Some›(f@13#666(x@17#666))
15-
//│ > caseScrut@14 is None and
16-
//│ > let n@15 = caseScrut@14#1
17-
//│ > else n@15#666
188

199
fun map(f) = case
2010
Some(x as n) then Some of f(n)
2111
None as n then n
22-
//│ Desugared:
23-
//│ > if
24-
//│ > caseScrut@23 is Some($param0@25) and
25-
//│ > let x@27 = $param0@25#0
26-
//│ > let n@26 = $param0@25#1
27-
//│ > else member:Option#666.Some‹member:Some›(f@22#666(n@26#666))
28-
//│ > caseScrut@23 is None and
29-
//│ > let n@24 = caseScrut@23#1
30-
//│ > else n@24#666
3112

13+
:ucs desugared
3214
fun foo = case
3315
Some(Some(a as b) as c) as d then [a, b, c, d]
3416
//│ Desugared:

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

Lines changed: 0 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
:js
2-
:ucs desugared
32

43
// Drop the first and last elements.
54
tupleSlice([1, 2, 3, 4], 1, -1)
@@ -17,13 +16,6 @@ tupleGet([1, 2, 3, 4], -1)
1716
fun nonsense(xs) = if xs is
1817
[..ys] then ys
1918
[] then "empty"
20-
//│ Desugared:
21-
//│ > if
22-
//│ > xs@26 is []>=0 and
23-
//│ > let $rest@27 = globalThis:globalThis#666.Predef.tupleSlice(xs@26#2, 0, 0)
24-
//│ > let ys@28 = $rest@27#0
25-
//│ > else ys@28#666
26-
//│ > xs@26 is []=0 then "empty"
2719
//│ JS:
2820
//│ function nonsense(xs) {
2921
//│ let rest, ys;
@@ -47,17 +39,6 @@ nonsense([1, 2, 3, 4])
4739
fun lead_and_last(xs) = if xs is
4840
[x, ..ys, y] then x + y
4941
[] then 0
50-
//│ Desugared:
51-
//│ > if
52-
//│ > xs@38 is []>=2 and
53-
//│ > let $first0@41 = xs@38#2.0
54-
//│ > let $rest@40 = globalThis:globalThis#666.Predef.tupleSlice(xs@38#3, 1, 1)
55-
//│ > let $last0@39 = globalThis:globalThis#666.Predef.tupleGet(xs@38#4, -1)
56-
//│ > let x@42 = $first0@41#0
57-
//│ > let ys@43 = $rest@40#0
58-
//│ > let y@44 = $last0@39#0
59-
//│ > else builtin:+#0(x@42#666, y@44#666)
60-
//│ > xs@38 is []=0 then 0
6142
//│ JS:
6243
//│ function lead_and_last(xs) {
6344
//│ let last0, rest, first0, x, ys, y;
@@ -96,21 +77,6 @@ lead_and_last(["boom"])
9677
fun nested_tuple_patterns(xs) = if xs is
9778
[x, ..[y, z], w] then x + y + z + w
9879
[] then 0
99-
//│ Desugared:
100-
//│ > if
101-
//│ > xs@63 is []>=2 and
102-
//│ > let $first0@66 = xs@63#2.0
103-
//│ > let $rest@65 = globalThis:globalThis#666.Predef.tupleSlice(xs@63#3, 1, 1)
104-
//│ > let $last0@64 = globalThis:globalThis#666.Predef.tupleGet(xs@63#4, -1)
105-
//│ > let x@67 = $first0@66#0
106-
//│ > $rest@65 is []=2 and
107-
//│ > let $first0@69 = $rest@65#1.0
108-
//│ > let $first1@68 = $rest@65#2.1
109-
//│ > let y@70 = $first0@69#0
110-
//│ > let z@71 = $first1@68#0
111-
//│ > let w@72 = $last0@64#0
112-
//│ > else builtin:+#0(builtin:+#1(builtin:+#2(x@67#666, y@70#666), z@71#666), w@72#666)
113-
//│ > xs@63 is []=0 then 0
11480
//│ JS:
11581
//│ function nested_tuple_patterns(xs) {
11682
//│ let last0, rest, first0, x, first1, first01, y, z, w, tmp, tmp1;
@@ -143,13 +109,6 @@ fun nested_tuple_patterns(xs) = if xs is
143109

144110
fun hack(tupleSlice) = if tupleSlice is
145111
[..tupleGet, x] then x
146-
//│ Desugared:
147-
//│ > if tupleSlice@85 is []>=1 and
148-
//│ > let $rest@87 = globalThis:globalThis#666.Predef.tupleSlice(tupleSlice@85#1, 0, 1)
149-
//│ > let $last0@86 = globalThis:globalThis#666.Predef.tupleGet(tupleSlice@85#2, -1)
150-
//│ > let tupleGet@88 = $rest@87#0
151-
//│ > let x@89 = $last0@86#0
152-
//│ > else x@89#666
153112

154113
:expect 4
155114
hack([1, 2, 3, 4])

0 commit comments

Comments
 (0)