|
4 | 4 | :import ../Prelude/Option.mls
|
5 | 5 | //│ Imported 3 member(s)
|
6 | 6 |
|
7 |
| -fun add_6(x, y) = |
| 7 | +:import ../Prelude/Tuple.mls |
| 8 | +//│ Imported 2 member(s) |
| 9 | + |
| 10 | +// Normalization should unify fixed-length tuple patterns. |
| 11 | +fun sum_options(x, y) = |
8 | 12 | if [x, y] is
|
9 | 13 | [Some(xv), Some(yv)] then xv + yv
|
10 | 14 | [Some(xv), None] then xv
|
11 | 15 | [None, Some(yv)] then yv
|
12 | 16 | [None, None] then 0
|
13 | 17 | //│ Desugared:
|
14 | 18 | //│ > if
|
15 |
| -//│ > let $scrut@34 = [x@32#666, y@33#666] |
16 |
| -//│ > $scrut@34 is []=2 and |
17 |
| -//│ > let $first0@36 = $scrut@34#10.0 |
18 |
| -//│ > let $first1@35 = $scrut@34#11.1 |
19 |
| -//│ > $first0@36 is Some($param0@39) and |
20 |
| -//│ > let xv@41 = $param0@39#1 |
21 |
| -//│ > $first1@35 is Some($param0@37) and |
22 |
| -//│ > let yv@42 = $param0@37#1 |
23 |
| -//│ > else globalThis:import#Prelude#666.+‹member:+›(xv@41#666, yv@42#666) |
24 |
| -//│ > $scrut@34 is []=2 and |
25 |
| -//│ > let $first0@36 = $scrut@34#7.0 |
26 |
| -//│ > let $first1@35 = $scrut@34#8.1 |
27 |
| -//│ > $first0@36 is Some($param0@39) and |
28 |
| -//│ > let xv@40 = $param0@39#0 |
29 |
| -//│ > $first1@35 is None then xv@40#666 |
30 |
| -//│ > $scrut@34 is []=2 and |
31 |
| -//│ > let $first0@36 = $scrut@34#4.0 |
32 |
| -//│ > let $first1@35 = $scrut@34#5.1 |
33 |
| -//│ > $first0@36 is None and $first1@35 is Some($param0@37) and |
34 |
| -//│ > let yv@38 = $param0@37#0 |
35 |
| -//│ > else yv@38#666 |
36 |
| -//│ > $scrut@34 is []=2 and |
37 |
| -//│ > let $first0@36 = $scrut@34#1.0 |
38 |
| -//│ > let $first1@35 = $scrut@34#2.1 |
39 |
| -//│ > $first0@36 is None $first1@35 is None then 0 |
| 19 | +//│ > let $scrut@49 = [x@47#666, y@48#666] |
| 20 | +//│ > $scrut@49 is []=2 and |
| 21 | +//│ > let $first0@51 = $scrut@49#10.0 |
| 22 | +//│ > let $first1@50 = $scrut@49#11.1 |
| 23 | +//│ > $first0@51 is Some($param0@54) and |
| 24 | +//│ > let xv@56 = $param0@54#1 |
| 25 | +//│ > $first1@50 is Some($param0@52) and |
| 26 | +//│ > let yv@57 = $param0@52#1 |
| 27 | +//│ > else globalThis:import#Prelude#666.+‹member:+›(xv@56#666, yv@57#666) |
| 28 | +//│ > $scrut@49 is []=2 and |
| 29 | +//│ > let $first0@51 = $scrut@49#7.0 |
| 30 | +//│ > let $first1@50 = $scrut@49#8.1 |
| 31 | +//│ > $first0@51 is Some($param0@54) and |
| 32 | +//│ > let xv@55 = $param0@54#0 |
| 33 | +//│ > $first1@50 is None then xv@55#666 |
| 34 | +//│ > $scrut@49 is []=2 and |
| 35 | +//│ > let $first0@51 = $scrut@49#4.0 |
| 36 | +//│ > let $first1@50 = $scrut@49#5.1 |
| 37 | +//│ > $first0@51 is None and $first1@50 is Some($param0@52) and |
| 38 | +//│ > let yv@53 = $param0@52#0 |
| 39 | +//│ > else yv@53#666 |
| 40 | +//│ > $scrut@49 is []=2 and |
| 41 | +//│ > let $first0@51 = $scrut@49#1.0 |
| 42 | +//│ > let $first1@50 = $scrut@49#2.1 |
| 43 | +//│ > $first0@51 is None $first1@50 is None then 0 |
| 44 | +//│ Normalized: |
| 45 | +//│ > if |
| 46 | +//│ > let $scrut@49 = [x@47#666, y@48#666] |
| 47 | +//│ > $scrut@49 is []=2 and |
| 48 | +//│ > let $first0@51 = $scrut@49#10.0 |
| 49 | +//│ > let $first1@50 = $scrut@49#11.1 |
| 50 | +//│ > $first0@51 is Some($param0@54) and |
| 51 | +//│ > let xv@56 = $param0@54#1 |
| 52 | +//│ > $first1@50 is Some($param0@52) and |
| 53 | +//│ > let yv@57 = $param0@52#1 |
| 54 | +//│ > else globalThis:import#Prelude#666.+‹member:+›(xv@56#666, yv@57#666) |
| 55 | +//│ > let xv@55 = $param0@54#0 |
| 56 | +//│ > $first1@50 is None then xv@55#666 |
| 57 | +//│ > $first0@51 is None and |
| 58 | +//│ > $first1@50 is Some($param0@52) and |
| 59 | +//│ > let yv@53 = $param0@52#0 |
| 60 | +//│ > else yv@53#666 |
| 61 | +//│ > $first1@50 is None then 0 |
| 62 | + |
| 63 | + |
| 64 | + |
| 65 | + |
| 66 | +// ========================================================================== // |
| 67 | + |
| 68 | + |
| 69 | +// Normalization should also unify the sub-scrutinees at the same position of |
| 70 | +// tuple patterns of different lengths. |
| 71 | + |
| 72 | + |
| 73 | +fun foo(xs) = if xs is |
| 74 | + [Some(x)] then x |
| 75 | + [Some(y), ..rest] then y |
| 76 | +//│ Desugared: |
| 77 | +//│ > if |
| 78 | +//│ > xs@62 is []=1 and |
| 79 | +//│ > let $first0@64 = xs@62#4.0 |
| 80 | +//│ > $first0@64 is Some($param0@65) and |
| 81 | +//│ > let x@68 = $param0@65#1 |
| 82 | +//│ > else x@68#666 |
| 83 | +//│ > xs@62 is []>=1 and |
| 84 | +//│ > let $first0@64 = xs@62#1.0 |
| 85 | +//│ > let $rest@63 = globalThis:import#Tuple#666.tupleSlice‹member:tupleSlice›(xs@62#2, 1, 0) |
| 86 | +//│ > $first0@64 is Some($param0@65) and |
| 87 | +//│ > let y@66 = $param0@65#0 |
| 88 | +//│ > let rest@67 = $rest@63#0 |
| 89 | +//│ > else y@66#666 |
40 | 90 | //│ Normalized:
|
41 | 91 | //│ > if
|
42 |
| -//│ > let $scrut@34 = [x@32#666, y@33#666] |
43 |
| -//│ > $scrut@34 is []=2 and |
44 |
| -//│ > let $first0@36 = $scrut@34#10.0 |
45 |
| -//│ > let $first1@35 = $scrut@34#11.1 |
46 |
| -//│ > $first0@36 is Some($param0@39) and |
47 |
| -//│ > let xv@41 = $param0@39#1 |
48 |
| -//│ > $first1@35 is Some($param0@37) and |
49 |
| -//│ > let yv@42 = $param0@37#1 |
50 |
| -//│ > else globalThis:import#Prelude#666.+‹member:+›(xv@41#666, yv@42#666) |
51 |
| -//│ > let xv@40 = $param0@39#0 |
52 |
| -//│ > $first1@35 is None then xv@40#666 |
53 |
| -//│ > $first0@36 is None and |
54 |
| -//│ > $first1@35 is Some($param0@37) and |
55 |
| -//│ > let yv@38 = $param0@37#0 |
56 |
| -//│ > else yv@38#666 |
57 |
| -//│ > $first1@35 is None then 0 |
| 92 | +//│ > xs@62 is []=1 and |
| 93 | +//│ > let $first0@64 = xs@62#4.0 |
| 94 | +//│ > $first0@64 is Some($param0@65) and |
| 95 | +//│ > let x@68 = $param0@65#1 |
| 96 | +//│ > else x@68#666 |
| 97 | +//│ > let $rest@63 = globalThis:import#Tuple#666.tupleSlice‹member:tupleSlice›(xs@62#2, 1, 0) |
| 98 | +//│ > xs@62 is []>=1 and |
| 99 | +//│ > let $first0@64 = xs@62#1.0 |
| 100 | +//│ > let $rest@63 = globalThis:import#Tuple#666.tupleSlice‹member:tupleSlice›(xs@62#2, 1, 0) |
| 101 | +//│ > $first0@64 is Some($param0@65) and |
| 102 | +//│ > let y@66 = $param0@65#0 |
| 103 | +//│ > let rest@67 = $rest@63#0 |
| 104 | +//│ > else y@66#666 |
| 105 | + |
| 106 | +fun do_something(x) = "done" |
| 107 | + |
| 108 | + |
| 109 | +fun foo(zs) = if zs is |
| 110 | + [Some(x), Some(y)] then x + y |
| 111 | + [None, ..rest] then do_something(rest) |
| 112 | +//│ Desugared: |
| 113 | +//│ > if |
| 114 | +//│ > zs@76 is []=2 and |
| 115 | +//│ > let $first0@78 = zs@76#4.0 |
| 116 | +//│ > let $first1@81 = zs@76#5.1 |
| 117 | +//│ > $first0@78 is Some($param0@82) and |
| 118 | +//│ > let x@83 = $param0@82#0 |
| 119 | +//│ > $first1@81 is Some($param0@84) and |
| 120 | +//│ > let y@85 = $param0@84#0 |
| 121 | +//│ > else globalThis:import#Prelude#666.+‹member:+›(x@83#666, y@85#666) |
| 122 | +//│ > zs@76 is []>=1 and |
| 123 | +//│ > let $first0@78 = zs@76#1.0 |
| 124 | +//│ > let $rest@77 = globalThis:import#Tuple#666.tupleSlice‹member:tupleSlice›(zs@76#2, 1, 0) |
| 125 | +//│ > $first0@78 is None and |
| 126 | +//│ > let rest@79 = $rest@77#0 |
| 127 | +//│ > else globalThis:block#3#666.do_something‹member:do_something›(rest@79#666) |
| 128 | +//│ Normalized: |
| 129 | +//│ > if |
| 130 | +//│ > zs@76 is []=2 and |
| 131 | +//│ > let $first0@78 = zs@76#4.0 |
| 132 | +//│ > let $first1@81 = zs@76#5.1 |
| 133 | +//│ > $first0@78 is Some($param0@82) and |
| 134 | +//│ > let x@83 = $param0@82#0 |
| 135 | +//│ > $first1@81 is Some($param0@84) and |
| 136 | +//│ > let y@85 = $param0@84#0 |
| 137 | +//│ > else globalThis:import#Prelude#666.+‹member:+›(x@83#666, y@85#666) |
| 138 | +//│ > let $rest@77 = globalThis:import#Tuple#666.tupleSlice‹member:tupleSlice›(zs@76#2, 1, 0) |
| 139 | +//│ > let $rest@77 = globalThis:import#Tuple#666.tupleSlice‹member:tupleSlice›(zs@76#2, 1, 0) |
| 140 | +//│ > $first0@78 is None and |
| 141 | +//│ > let rest@79 = $rest@77#0 |
| 142 | +//│ > else globalThis:block#3#666.do_something‹member:do_something›(rest@79#666) |
| 143 | +//│ > zs@76 is []>=1 and |
| 144 | +//│ > let $first0@78 = zs@76#1.0 |
| 145 | +//│ > let $rest@77 = globalThis:import#Tuple#666.tupleSlice‹member:tupleSlice›(zs@76#2, 1, 0) |
| 146 | +//│ > $first0@78 is None and |
| 147 | +//│ > let rest@79 = $rest@77#0 |
| 148 | +//│ > else globalThis:block#3#666.do_something‹member:do_something›(rest@79#666) |
| 149 | + |
| 150 | +:expect 4 |
| 151 | +foo([Some(1), Some(3)]) |
| 152 | +//│ = 4 |
| 153 | + |
| 154 | +:expect 'done' |
| 155 | +foo([None, Some(1), Some(2)]) |
| 156 | +//│ = 'done' |
| 157 | + |
| 158 | +:re |
| 159 | +foo([Some(0)]) |
| 160 | +//│ ═══[RUNTIME ERROR] Error: match error |
| 161 | + |
| 162 | +:re |
| 163 | +foo([Some(0), Some(1), Some(2)]) |
| 164 | +//│ ═══[RUNTIME ERROR] Error: match error |
| 165 | + |
| 166 | +:re |
| 167 | +foo([]) |
| 168 | +//│ ═══[RUNTIME ERROR] Error: match error |
0 commit comments