From 00faff0a2014e767091ad719733ab03671288ee3 Mon Sep 17 00:00:00 2001 From: Luyu Cheng Date: Wed, 13 Nov 2024 11:27:25 +0800 Subject: [PATCH] Use meaningful function names in tuple pattern tests --- .../test/mlscript/ucs/patterns/RestTuple.mls | 21 ++- .../mlscript/ucs/patterns/SimpleTuple.mls | 168 +++++++++--------- 2 files changed, 96 insertions(+), 93 deletions(-) diff --git a/hkmc2/shared/src/test/mlscript/ucs/patterns/RestTuple.mls b/hkmc2/shared/src/test/mlscript/ucs/patterns/RestTuple.mls index 6dbe70535..10b778747 100644 --- a/hkmc2/shared/src/test/mlscript/ucs/patterns/RestTuple.mls +++ b/hkmc2/shared/src/test/mlscript/ucs/patterns/RestTuple.mls @@ -17,7 +17,7 @@ tupleGet([1, 2, 3, 4], -1) //│ = 4 :sjs -fun stupid(xs) = if xs is +fun nonsense(xs) = if xs is [..ys] then ys [] then "empty" //│ Desugared: @@ -28,7 +28,7 @@ fun stupid(xs) = if xs is //│ > else ys@61#666 //│ > xs@59 is []=0 then "empty" //│ JS: -//│ function stupid(xs) { +//│ function nonsense(xs) { //│ let rest, ys; //│ if (Array.isArray(xs) && xs.length >= 0) { //│ rest = globalThis.tupleSlice(xs, 0, 0); @@ -40,14 +40,14 @@ fun stupid(xs) = if xs is //│ }; //│ undefined -stupid([]) +nonsense([]) //│ = [] -stupid([1, 2, 3, 4]) +nonsense([1, 2, 3, 4]) //│ = [ 1, 2, 3, 4 ] :sjs -fun foo(xs) = if xs is +fun lead_and_last(xs) = if xs is [x, ..ys, y] then x + y [] then 0 //│ Desugared: @@ -62,7 +62,7 @@ fun foo(xs) = if xs is //│ > else globalThis:import#Prelude#666.+‹member:+›(x@75#666, y@77#666) //│ > xs@71 is []=0 then 0 //│ JS: -//│ function foo(xs) { +//│ function lead_and_last(xs) { //│ let last0, rest, first0, x, ys, y; //│ if (Array.isArray(xs) && xs.length >= 2) { //│ first0 = xs[0]; @@ -82,21 +82,20 @@ fun foo(xs) = if xs is //│ }; //│ undefined -foo(["foo", "bar"]) +lead_and_last(["foo", "bar"]) //│ = 'foobar' -foo([1, 2, 3, 4]) +lead_and_last([1, 2, 3, 4]) //│ = 5 -foo([]) +lead_and_last([]) //│ = 0 :re -foo(["boom"]) +lead_and_last(["boom"]) //│ ═══[RUNTIME ERROR] Error: match error :sjs -:todo // Better using ... instead of .. for the rest pattern. fun nested_tuple_patterns(xs) = if xs is [x, ..[y, z], w] then x + y + z + w [] then 0 diff --git a/hkmc2/shared/src/test/mlscript/ucs/patterns/SimpleTuple.mls b/hkmc2/shared/src/test/mlscript/ucs/patterns/SimpleTuple.mls index f70365abb..06bc9e23a 100644 --- a/hkmc2/shared/src/test/mlscript/ucs/patterns/SimpleTuple.mls +++ b/hkmc2/shared/src/test/mlscript/ucs/patterns/SimpleTuple.mls @@ -39,73 +39,77 @@ test("") test(12) //│ ═══[RUNTIME ERROR] Error: match error -:ucs desugared class Point(x: Int, y: Int) -fun discarded_cases(thing) = + +:ucs desugared +fun with_other_constructors(thing) = if thing is - [x, y] then x + y + [x, y] then x * y Point(x, y) then x + y //│ Desugared: //│ > if -//│ > thing@58 is []=2 and -//│ > let $first0@65 = thing@58#2.0 -//│ > let $first1@64 = thing@58#3.1 -//│ > let x@66 = $first0@65#0 -//│ > let y@67 = $first1@64#0 -//│ > else globalThis:import#Prelude#666.+‹member:+›(x@66#666, y@67#666) -//│ > thing@58 is Point($param0@59, $param1@60) and -//│ > let x@61 = $param0@59#0 -//│ > let y@62 = $param1@60#0 -//│ > else globalThis:import#Prelude#666.+‹member:+›(x@61#666, y@62#666) - -:e -:todo -discarded_cases(Point(0, 0)) -//│ = 0 +//│ > thing@59 is []=2 and +//│ > let $first0@66 = thing@59#2.0 +//│ > let $first1@65 = thing@59#3.1 +//│ > let x@67 = $first0@66#0 +//│ > let y@68 = $first1@65#0 +//│ > else globalThis:import#Prelude#666.*‹member:*›(x@67#666, y@68#666) +//│ > thing@59 is Point($param0@60, $param1@61) and +//│ > let x@62 = $param0@60#0 +//│ > let y@63 = $param1@61#0 +//│ > else globalThis:import#Prelude#666.+‹member:+›(x@62#666, y@63#666) + +:expect 7 +with_other_constructors(Point(3, 4)) +//│ = 7 + +:expect 12 +with_other_constructors([3, 4]) +//│ = 12 // A workaround is to move the tuple pattern to the last case. :ucs desugared -fun working_cases(thing) = +fun with_other_constructors(thing) = if thing is Point(x, y) then x + y - [x, y] then x + y + [x, y] then x * y //│ Desugared: //│ > if -//│ > thing@79 is Point($param0@85, $param1@86) and -//│ > let x@87 = $param0@85#0 -//│ > let y@88 = $param1@86#0 -//│ > else globalThis:import#Prelude#666.+‹member:+›(x@87#666, y@88#666) -//│ > thing@79 is []=2 and -//│ > let $first0@81 = thing@79#1.0 -//│ > let $first1@80 = thing@79#2.1 -//│ > let x@82 = $first0@81#0 -//│ > let y@83 = $first1@80#0 -//│ > else globalThis:import#Prelude#666.+‹member:+›(x@82#666, y@83#666) - -working_cases(Point(0, 0)) -//│ = 0 - -// However, the `Object` type forbids tuples to be used. -:todo -working_cases([0, 0]) -//│ = 0 - - -fun not_working(x) = +//│ > thing@83 is Point($param0@89, $param1@90) and +//│ > let x@91 = $param0@89#0 +//│ > let y@92 = $param1@90#0 +//│ > else globalThis:import#Prelude#666.+‹member:+›(x@91#666, y@92#666) +//│ > thing@83 is []=2 and +//│ > let $first0@85 = thing@83#1.0 +//│ > let $first1@84 = thing@83#2.1 +//│ > let x@86 = $first0@85#0 +//│ > let y@87 = $first1@84#0 +//│ > else globalThis:import#Prelude#666.*‹member:*›(x@86#666, y@87#666) + +:expect 7 +with_other_constructors(Point(3, 4)) +//│ = 7 + +:expect 12 +with_other_constructors([3, 4]) +//│ = 12 + + +fun with_else(x) = if x is [a, b, c] then a + b + c else 0 -not_working([1, 2, 3]) +with_else([1, 2, 3]) //│ = 6 -not_working([1, 2]) +with_else([1, 2]) //│ = 0 :ucs desugared -fun multiple_checks(xs) = +fun match_against_different_length(xs) = if xs is [] then 0 [x] then x + 1 @@ -113,40 +117,40 @@ fun multiple_checks(xs) = [x, y, z] then x + y + z + 3 //│ Desugared: //│ > if -//│ > xs@123 is []=0 then 0 -//│ > xs@123 is []=1 and -//│ > let $first0@126 = xs@123#8.0 -//│ > let x@137 = $first0@126#2 -//│ > else globalThis:import#Prelude#666.+‹member:+›(x@137#666, 1) -//│ > xs@123 is []=2 and -//│ > let $first0@126 = xs@123#5.0 -//│ > let $first1@125 = xs@123#6.1 -//│ > let x@133 = $first0@126#1 -//│ > let y@134 = $first1@125#1 -//│ > else globalThis:import#Prelude#666.+‹member:+›(globalThis:import#Prelude#666.+‹member:+›(x@133#666, y@134#666), 2) -//│ > xs@123 is []=3 and -//│ > let $first0@126 = xs@123#1.0 -//│ > let $first1@125 = xs@123#2.1 -//│ > let $first2@124 = xs@123#3.2 -//│ > let x@127 = $first0@126#0 -//│ > let y@128 = $first1@125#0 -//│ > let z@129 = $first2@124#0 -//│ > 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) +//│ > xs@127 is []=0 then 0 +//│ > xs@127 is []=1 and +//│ > let $first0@130 = xs@127#8.0 +//│ > let x@141 = $first0@130#2 +//│ > else globalThis:import#Prelude#666.+‹member:+›(x@141#666, 1) +//│ > xs@127 is []=2 and +//│ > let $first0@130 = xs@127#5.0 +//│ > let $first1@129 = xs@127#6.1 +//│ > let x@137 = $first0@130#1 +//│ > let y@138 = $first1@129#1 +//│ > else globalThis:import#Prelude#666.+‹member:+›(globalThis:import#Prelude#666.+‹member:+›(x@137#666, y@138#666), 2) +//│ > xs@127 is []=3 and +//│ > let $first0@130 = xs@127#1.0 +//│ > let $first1@129 = xs@127#2.1 +//│ > let $first2@128 = xs@127#3.2 +//│ > let x@131 = $first0@130#0 +//│ > let y@132 = $first1@129#0 +//│ > let z@133 = $first2@128#0 +//│ > 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) :expect 0 -multiple_checks([]) +match_against_different_length([]) //│ = 0 :expect 18 -multiple_checks([17]) +match_against_different_length([17]) //│ = 18 :expect 22 -multiple_checks([9, 11]) +match_against_different_length([9, 11]) //│ = 22 :expect 42 -multiple_checks([13, 13, 13]) +match_against_different_length([13, 13, 13]) //│ = 42 :import ../Prelude/Option.mls @@ -154,24 +158,24 @@ multiple_checks([13, 13, 13]) :ucs desugared normalized :todo // Sub-scrutinees are not memorized thus the normalization is not correct. -fun multiple_checks(xs) = +fun with_the_common_prefix(xs) = if xs is [Some(x)] then x + 1 [None] then 0 //│ Desugared: //│ > if -//│ > xs@165 is []=1 and -//│ > let $first0@166 = xs@165#3.0 -//│ > $first0@166 is Some($param0@167) and -//│ > let x@168 = $param0@167#0 -//│ > else globalThis:import#Prelude#666.+‹member:+›(x@168#666, 1) -//│ > xs@165 is []=1 and -//│ > let $first0@166 = xs@165#1.0 -//│ > $first0@166 is None then 0 +//│ > xs@169 is []=1 and +//│ > let $first0@170 = xs@169#3.0 +//│ > $first0@170 is Some($param0@171) and +//│ > let x@172 = $param0@171#0 +//│ > else globalThis:import#Prelude#666.+‹member:+›(x@172#666, 1) +//│ > xs@169 is []=1 and +//│ > let $first0@170 = xs@169#1.0 +//│ > $first0@170 is None then 0 //│ Normalized: -//│ > if xs@165 is []=1 and -//│ > let $first0@166 = xs@165#3.0 -//│ > $first0@166 is Some($param0@167) and -//│ > let x@168 = $param0@167#0 -//│ > else globalThis:import#Prelude#666.+‹member:+›(x@168#666, 1) -//│ > $first0@166 is None then 0 +//│ > if xs@169 is []=1 and +//│ > let $first0@170 = xs@169#3.0 +//│ > $first0@170 is Some($param0@171) and +//│ > let x@172 = $param0@171#0 +//│ > else globalThis:import#Prelude#666.+‹member:+›(x@172#666, 1) +//│ > $first0@170 is None then 0