Skip to content

Commit

Permalink
Reduce the abuse of UCS debug commands
Browse files Browse the repository at this point in the history
  • Loading branch information
chengluyu authored and LPTK committed Nov 15, 2024
1 parent 1cf5a60 commit de16b05
Show file tree
Hide file tree
Showing 6 changed files with 2 additions and 343 deletions.
18 changes: 1 addition & 17 deletions hkmc2/shared/src/test/mlscript/ucs/general/CardSuits.mls
Original file line number Diff line number Diff line change
Expand Up @@ -13,28 +13,12 @@ fun suit(x) = if x is Card(a) then a
let card = Card(Heart)
suit of card

:ucs desugared normalized
// :ucs desugared normalized
fun foo(x) = if x is
Card(Heart) then 0
Card(Diamond) then 1
Card(Spade) then 2
Card(Club) then 3
else 4
//│ Desugared:
//│ > if
//│ > x@22 is Card($param0@23) $param0@23 is Heart then 0
//│ > x@22 is Card($param0@23) $param0@23 is Diamond then 1
//│ > x@22 is Card($param0@23) $param0@23 is Spade then 2
//│ > x@22 is Card($param0@23) $param0@23 is Club then 3
//│ > else 4
//│ Normalized:
//│ > if
//│ > x@22 is Card($param0@23) and
//│ > $param0@23 is Heart then 0
//│ > $param0@23 is Diamond then 1
//│ > $param0@23 is Spade then 2
//│ > $param0@23 is Club then 3
//│ > else 4
//│ > else 4


Original file line number Diff line number Diff line change
Expand Up @@ -4,99 +4,23 @@ import "../../../mlscript-compile/Option.mls"
open Stack
open Option

:global
:ucs desugared normalized

class Pair[A, B](x: A, y: B)

fun foo(xs) =
if xs is
"abc" :: "xyz" :: xs then 0
"abc" :: "uvw" :: xs then 1
Nil then 3
//│ Desugared:
//│ > if
//│ > 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
//│ > let xs@22 = $param1@20#1
//│ > else 0
//│ > 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
//│ > let xs@21 = $param1@20#0
//│ > else 1
//│ > xs@16 is Nil then 3
//│ Normalized:
//│ > if
//│ > 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
//│ > let xs@22 = $param1@20#1
//│ > else 0
//│ > $param0@19 is "uvw" and
//│ > let xs@21 = $param1@20#0
//│ > else 1
//│ > xs@16 is Nil then 3

fun sum(acc, xs) =
if xs is
x :: xs then sum(acc + x, xs)
Nil then acc
//│ Desugared:
//│ > if
//│ > xs@26 is Cons($param0@27, $param1@28) and
//│ > let x@29 = $param0@27#0
//│ > let xs@30 = $param1@28#0
//│ > else globalThis:block#5#666.sum‹member:sum›(builtin:+#0(acc@25#666, x@29#666), xs@30#666)
//│ > xs@26 is Nil then acc@25#666
//│ Normalized:
//│ > if
//│ > xs@26 is Cons($param0@27, $param1@28) and
//│ > let x@29 = $param0@27#0
//│ > let xs@30 = $param1@28#0
//│ > else globalThis:block#5#666.sum‹member:sum›(builtin:+#0(acc@25#666, x@29#666), xs@30#666)
//│ > xs@26 is Nil then acc@25#666

:todo
// The normalization does not distinguish between bindings and their aliases.
// We may still need to substitute variables when merging branches or reuse
// bindings at the same location.
fun test(xs) =
if xs is
Some(Cons("add", Cons(x, Cons(y, Nil)))) then x + y
Some(Cons("mul", Cons(x, Cons(y, Nil)))) then x * y
Some(Cons("sum", xs)) then sum(0, xs)
Some(Nil) then "nothing"
None then "nothing"
//│ Desugared:
//│ > if
//│ > 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
//│ > let x@48 = $param0@41#1
//│ > $param1@42 is Cons($param0@44, $param1@45) and
//│ > let y@49 = $param0@44#1
//│ > $param1@45 is Nil then builtin:+#0(x@48#666, y@49#666)
//│ > 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
//│ > let x@43 = $param0@41#0
//│ > $param1@42 is Cons($param0@44, $param1@45) and
//│ > let y@46 = $param0@44#0
//│ > $param1@45 is Nil then builtin:*#0(x@43#666, y@46#666)
//│ > xs@35 is Some($param0@36) and $param0@36 is Cons($param0@37, $param1@38) and $param0@37 is "sum" and
//│ > let xs@39 = $param1@38#0
//│ > else globalThis:block#5#666.sum‹member:sum›(0, xs@39#666)
//│ > xs@35 is Some($param0@36) $param0@36 is Nil then "nothing"
//│ > xs@35 is None then "nothing"
//│ Normalized:
//│ > if
//│ > 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
//│ > let x@48 = $param0@41#1
//│ > $param1@42 is Cons($param0@44, $param1@45) and
//│ > let y@49 = $param0@44#1
//│ > $param1@45 is Nil then builtin:+#0(x@48#666, y@49#666)
//│ > $param0@37 is "mul" and $param1@38 is Cons($param0@41, $param1@42) and
//│ > let x@43 = $param0@41#0
//│ > $param1@42 is Cons($param0@44, $param1@45) and
//│ > let y@46 = $param0@44#0
//│ > $param1@45 is Nil then builtin:*#0(x@43#666, y@46#666)
//│ > $param0@37 is "sum" and
//│ > let xs@39 = $param1@38#0
//│ > else globalThis:block#5#666.sum‹member:sum›(0, xs@39#666)
//│ > $param0@36 is Nil then "nothing"
//│ > xs@35 is None then "nothing"
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
:js
:ucs desugared normalized

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

Expand All @@ -12,51 +11,6 @@ fun sum_options(x, y) =
[Some(xv), None] then xv
[None, Some(yv)] then yv
[None, None] then 0
//│ Desugared:
//│ > if
//│ > let $scrut@15 = [x@13#666, y@14#666]
//│ > $scrut@15 is []=2 and
//│ > let $first0@17 = $scrut@15#10.0
//│ > let $first1@16 = $scrut@15#11.1
//│ > $first0@17 is Some($param0@20) and
//│ > let xv@22 = $param0@20#1
//│ > $first1@16 is Some($param0@18) and
//│ > let yv@23 = $param0@18#1
//│ > else builtin:+#0(xv@22#666, yv@23#666)
//│ > $scrut@15 is []=2 and
//│ > let $first0@17 = $scrut@15#7.0
//│ > let $first1@16 = $scrut@15#8.1
//│ > $first0@17 is Some($param0@20) and
//│ > let xv@21 = $param0@20#0
//│ > $first1@16 is None then xv@21#666
//│ > $scrut@15 is []=2 and
//│ > let $first0@17 = $scrut@15#4.0
//│ > let $first1@16 = $scrut@15#5.1
//│ > $first0@17 is None and $first1@16 is Some($param0@18) and
//│ > let yv@19 = $param0@18#0
//│ > else yv@19#666
//│ > $scrut@15 is []=2 and
//│ > let $first0@17 = $scrut@15#1.0
//│ > let $first1@16 = $scrut@15#2.1
//│ > $first0@17 is None $first1@16 is None then 0
//│ Normalized:
//│ > if
//│ > let $scrut@15 = [x@13#666, y@14#666]
//│ > $scrut@15 is []=2 and
//│ > let $first0@17 = $scrut@15#10.0
//│ > let $first1@16 = $scrut@15#11.1
//│ > $first0@17 is Some($param0@20) and
//│ > let xv@22 = $param0@20#1
//│ > $first1@16 is Some($param0@18) and
//│ > let yv@23 = $param0@18#1
//│ > else builtin:+#0(xv@22#666, yv@23#666)
//│ > let xv@21 = $param0@20#0
//│ > $first1@16 is None then xv@21#666
//│ > $first0@17 is None and
//│ > $first1@16 is Some($param0@18) and
//│ > let yv@19 = $param0@18#0
//│ > else yv@19#666
//│ > $first1@16 is None then 0



Expand All @@ -71,79 +25,13 @@ fun sum_options(x, y) =
fun foo(xs) = if xs is
[Some(x)] then x
[Some(y), ..rest] then y
//│ Desugared:
//│ > if
//│ > xs@28 is []=1 and
//│ > let $first0@30 = xs@28#4.0
//│ > $first0@30 is Some($param0@31) and
//│ > let x@34 = $param0@31#1
//│ > else x@34#666
//│ > xs@28 is []>=1 and
//│ > let $first0@30 = xs@28#1.0
//│ > let $rest@29 = globalThis:globalThis#666.Predef.tupleSlice(xs@28#2, 1, 0)
//│ > $first0@30 is Some($param0@31) and
//│ > let y@32 = $param0@31#0
//│ > let rest@33 = $rest@29#0
//│ > else y@32#666
//│ Normalized:
//│ > if
//│ > xs@28 is []=1 and
//│ > let $first0@30 = xs@28#4.0
//│ > $first0@30 is Some($param0@31) and
//│ > let x@34 = $param0@31#1
//│ > else x@34#666
//│ > let $rest@29 = globalThis:globalThis#666.Predef.tupleSlice(xs@28#2, 1, 0)
//│ > xs@28 is []>=1 and
//│ > let $first0@30 = xs@28#1.0
//│ > let $rest@29 = globalThis:globalThis#666.Predef.tupleSlice(xs@28#2, 1, 0)
//│ > $first0@30 is Some($param0@31) and
//│ > let y@32 = $param0@31#0
//│ > let rest@33 = $rest@29#0
//│ > else y@32#666

fun do_something(x) = "done"


fun foo(zs) = if zs is
[Some(x), Some(y)] then x + y
[None, ..rest] then do_something(rest)
//│ Desugared:
//│ > if
//│ > zs@42 is []=2 and
//│ > let $first0@44 = zs@42#4.0
//│ > let $first1@47 = zs@42#5.1
//│ > $first0@44 is Some($param0@48) and
//│ > let x@49 = $param0@48#0
//│ > $first1@47 is Some($param0@50) and
//│ > let y@51 = $param0@50#0
//│ > else builtin:+#0(x@49#666, y@51#666)
//│ > zs@42 is []>=1 and
//│ > let $first0@44 = zs@42#1.0
//│ > let $rest@43 = globalThis:globalThis#666.Predef.tupleSlice(zs@42#2, 1, 0)
//│ > $first0@44 is None and
//│ > let rest@45 = $rest@43#0
//│ > else globalThis:block#5#666.do_something‹member:do_something›(rest@45#666)
//│ Normalized:
//│ > if
//│ > zs@42 is []=2 and
//│ > let $first0@44 = zs@42#4.0
//│ > let $first1@47 = zs@42#5.1
//│ > $first0@44 is Some($param0@48) and
//│ > let x@49 = $param0@48#0
//│ > $first1@47 is Some($param0@50) and
//│ > let y@51 = $param0@50#0
//│ > else builtin:+#0(x@49#666, y@51#666)
//│ > let $rest@43 = globalThis:globalThis#666.Predef.tupleSlice(zs@42#2, 1, 0)
//│ > let $rest@43 = globalThis:globalThis#666.Predef.tupleSlice(zs@42#2, 1, 0)
//│ > $first0@44 is None and
//│ > let rest@45 = $rest@43#0
//│ > else globalThis:block#5#666.do_something‹member:do_something›(rest@45#666)
//│ > zs@42 is []>=1 and
//│ > let $first0@44 = zs@42#1.0
//│ > let $rest@43 = globalThis:globalThis#666.Predef.tupleSlice(zs@42#2, 1, 0)
//│ > $first0@44 is None and
//│ > let rest@45 = $rest@43#0
//│ > else globalThis:block#5#666.do_something‹member:do_something›(rest@45#666)

:expect 4
foo([Some(1), Some(3)])
Expand Down
20 changes: 1 addition & 19 deletions hkmc2/shared/src/test/mlscript/ucs/patterns/AliasPattern.mls
Original file line number Diff line number Diff line change
@@ -1,34 +1,16 @@
:ucs desugared

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

open Option

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

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

:ucs desugared
fun foo = case
Some(Some(a as b) as c) as d then [a, b, c, d]
//│ Desugared:
Expand Down
41 changes: 0 additions & 41 deletions hkmc2/shared/src/test/mlscript/ucs/patterns/RestTuple.mls
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
:js
:ucs desugared

// Drop the first and last elements.
tupleSlice([1, 2, 3, 4], 1, -1)
Expand All @@ -17,13 +16,6 @@ tupleGet([1, 2, 3, 4], -1)
fun nonsense(xs) = if xs is
[..ys] then ys
[] then "empty"
//│ Desugared:
//│ > if
//│ > xs@26 is []>=0 and
//│ > let $rest@27 = globalThis:globalThis#666.Predef.tupleSlice(xs@26#2, 0, 0)
//│ > let ys@28 = $rest@27#0
//│ > else ys@28#666
//│ > xs@26 is []=0 then "empty"
//│ JS:
//│ function nonsense(xs) {
//│ let rest, ys;
Expand All @@ -47,17 +39,6 @@ nonsense([1, 2, 3, 4])
fun lead_and_last(xs) = if xs is
[x, ..ys, y] then x + y
[] then 0
//│ Desugared:
//│ > if
//│ > xs@38 is []>=2 and
//│ > let $first0@41 = xs@38#2.0
//│ > let $rest@40 = globalThis:globalThis#666.Predef.tupleSlice(xs@38#3, 1, 1)
//│ > let $last0@39 = globalThis:globalThis#666.Predef.tupleGet(xs@38#4, -1)
//│ > let x@42 = $first0@41#0
//│ > let ys@43 = $rest@40#0
//│ > let y@44 = $last0@39#0
//│ > else builtin:+#0(x@42#666, y@44#666)
//│ > xs@38 is []=0 then 0
//│ JS:
//│ function lead_and_last(xs) {
//│ let last0, rest, first0, x, ys, y;
Expand Down Expand Up @@ -96,21 +77,6 @@ lead_and_last(["boom"])
fun nested_tuple_patterns(xs) = if xs is
[x, ..[y, z], w] then x + y + z + w
[] then 0
//│ Desugared:
//│ > if
//│ > xs@63 is []>=2 and
//│ > let $first0@66 = xs@63#2.0
//│ > let $rest@65 = globalThis:globalThis#666.Predef.tupleSlice(xs@63#3, 1, 1)
//│ > let $last0@64 = globalThis:globalThis#666.Predef.tupleGet(xs@63#4, -1)
//│ > let x@67 = $first0@66#0
//│ > $rest@65 is []=2 and
//│ > let $first0@69 = $rest@65#1.0
//│ > let $first1@68 = $rest@65#2.1
//│ > let y@70 = $first0@69#0
//│ > let z@71 = $first1@68#0
//│ > let w@72 = $last0@64#0
//│ > else builtin:+#0(builtin:+#1(builtin:+#2(x@67#666, y@70#666), z@71#666), w@72#666)
//│ > xs@63 is []=0 then 0
//│ JS:
//│ function nested_tuple_patterns(xs) {
//│ let last0, rest, first0, x, first1, first01, y, z, w, tmp, tmp1;
Expand Down Expand Up @@ -143,13 +109,6 @@ fun nested_tuple_patterns(xs) = if xs is

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

:expect 4
hack([1, 2, 3, 4])
Expand Down
Loading

0 comments on commit de16b05

Please sign in to comment.