|
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@16 = [x@14#666, y@15#666] |
16 |
| -//│ > $scrut@16 is []=2 and |
17 |
| -//│ > let $first0@18 = $scrut@16#10.0 |
18 |
| -//│ > let $first1@17 = $scrut@16#11.1 |
19 |
| -//│ > $first0@18 is Some($param0@21) and |
20 |
| -//│ > let xv@23 = $param0@21#1 |
21 |
| -//│ > $first1@17 is Some($param0@19) and |
22 |
| -//│ > let yv@24 = $param0@19#1 |
23 |
| -//│ > else builtin:+#98(xv@23#666, yv@24#666) |
24 |
| -//│ > $scrut@16 is []=2 and |
25 |
| -//│ > let $first0@18 = $scrut@16#7.0 |
26 |
| -//│ > let $first1@17 = $scrut@16#8.1 |
27 |
| -//│ > $first0@18 is Some($param0@21) and |
28 |
| -//│ > let xv@22 = $param0@21#0 |
29 |
| -//│ > $first1@17 is None then xv@22#666 |
30 |
| -//│ > $scrut@16 is []=2 and |
31 |
| -//│ > let $first0@18 = $scrut@16#4.0 |
32 |
| -//│ > let $first1@17 = $scrut@16#5.1 |
33 |
| -//│ > $first0@18 is None and $first1@17 is Some($param0@19) and |
34 |
| -//│ > let yv@20 = $param0@19#0 |
35 |
| -//│ > else yv@20#666 |
36 |
| -//│ > $scrut@16 is []=2 and |
37 |
| -//│ > let $first0@18 = $scrut@16#1.0 |
38 |
| -//│ > let $first1@17 = $scrut@16#2.1 |
39 |
| -//│ > $first0@18 is None $first1@17 is None then 0 |
| 19 | +//│ > let $scrut@31 = [x@29#666, y@30#666] |
| 20 | +//│ > $scrut@31 is []=2 and |
| 21 | +//│ > let $first0@33 = $scrut@31#10.0 |
| 22 | +//│ > let $first1@32 = $scrut@31#11.1 |
| 23 | +//│ > $first0@33 is Some($param0@36) and |
| 24 | +//│ > let xv@38 = $param0@36#1 |
| 25 | +//│ > $first1@32 is Some($param0@34) and |
| 26 | +//│ > let yv@39 = $param0@34#1 |
| 27 | +//│ > else builtin:+#98(xv@38#666, yv@39#666) |
| 28 | +//│ > $scrut@31 is []=2 and |
| 29 | +//│ > let $first0@33 = $scrut@31#7.0 |
| 30 | +//│ > let $first1@32 = $scrut@31#8.1 |
| 31 | +//│ > $first0@33 is Some($param0@36) and |
| 32 | +//│ > let xv@37 = $param0@36#0 |
| 33 | +//│ > $first1@32 is None then xv@37#666 |
| 34 | +//│ > $scrut@31 is []=2 and |
| 35 | +//│ > let $first0@33 = $scrut@31#4.0 |
| 36 | +//│ > let $first1@32 = $scrut@31#5.1 |
| 37 | +//│ > $first0@33 is None and $first1@32 is Some($param0@34) and |
| 38 | +//│ > let yv@35 = $param0@34#0 |
| 39 | +//│ > else yv@35#666 |
| 40 | +//│ > $scrut@31 is []=2 and |
| 41 | +//│ > let $first0@33 = $scrut@31#1.0 |
| 42 | +//│ > let $first1@32 = $scrut@31#2.1 |
| 43 | +//│ > $first0@33 is None $first1@32 is None then 0 |
| 44 | +//│ Normalized: |
| 45 | +//│ > if |
| 46 | +//│ > let $scrut@31 = [x@29#666, y@30#666] |
| 47 | +//│ > $scrut@31 is []=2 and |
| 48 | +//│ > let $first0@33 = $scrut@31#10.0 |
| 49 | +//│ > let $first1@32 = $scrut@31#11.1 |
| 50 | +//│ > $first0@33 is Some($param0@36) and |
| 51 | +//│ > let xv@38 = $param0@36#1 |
| 52 | +//│ > $first1@32 is Some($param0@34) and |
| 53 | +//│ > let yv@39 = $param0@34#1 |
| 54 | +//│ > else builtin:+#98(xv@38#666, yv@39#666) |
| 55 | +//│ > let xv@37 = $param0@36#0 |
| 56 | +//│ > $first1@32 is None then xv@37#666 |
| 57 | +//│ > $first0@33 is None and |
| 58 | +//│ > $first1@32 is Some($param0@34) and |
| 59 | +//│ > let yv@35 = $param0@34#0 |
| 60 | +//│ > else yv@35#666 |
| 61 | +//│ > $first1@32 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@44 is []=1 and |
| 79 | +//│ > let $first0@46 = xs@44#4.0 |
| 80 | +//│ > $first0@46 is Some($param0@47) and |
| 81 | +//│ > let x@50 = $param0@47#1 |
| 82 | +//│ > else x@50#666 |
| 83 | +//│ > xs@44 is []>=1 and |
| 84 | +//│ > let $first0@46 = xs@44#1.0 |
| 85 | +//│ > let $rest@45 = globalThis:import#Tuple#666.tupleSlice‹member:tupleSlice›(xs@44#2, 1, 0) |
| 86 | +//│ > $first0@46 is Some($param0@47) and |
| 87 | +//│ > let y@48 = $param0@47#0 |
| 88 | +//│ > let rest@49 = $rest@45#0 |
| 89 | +//│ > else y@48#666 |
40 | 90 | //│ Normalized:
|
41 | 91 | //│ > if
|
42 |
| -//│ > let $scrut@16 = [x@14#666, y@15#666] |
43 |
| -//│ > $scrut@16 is []=2 and |
44 |
| -//│ > let $first0@18 = $scrut@16#10.0 |
45 |
| -//│ > let $first1@17 = $scrut@16#11.1 |
46 |
| -//│ > $first0@18 is Some($param0@21) and |
47 |
| -//│ > let xv@23 = $param0@21#1 |
48 |
| -//│ > $first1@17 is Some($param0@19) and |
49 |
| -//│ > let yv@24 = $param0@19#1 |
50 |
| -//│ > else builtin:+#98(xv@23#666, yv@24#666) |
51 |
| -//│ > let xv@22 = $param0@21#0 |
52 |
| -//│ > $first1@17 is None then xv@22#666 |
53 |
| -//│ > $first0@18 is None and |
54 |
| -//│ > $first1@17 is Some($param0@19) and |
55 |
| -//│ > let yv@20 = $param0@19#0 |
56 |
| -//│ > else yv@20#666 |
57 |
| -//│ > $first1@17 is None then 0 |
| 92 | +//│ > xs@44 is []=1 and |
| 93 | +//│ > let $first0@46 = xs@44#4.0 |
| 94 | +//│ > $first0@46 is Some($param0@47) and |
| 95 | +//│ > let x@50 = $param0@47#1 |
| 96 | +//│ > else x@50#666 |
| 97 | +//│ > let $rest@45 = globalThis:import#Tuple#666.tupleSlice‹member:tupleSlice›(xs@44#2, 1, 0) |
| 98 | +//│ > xs@44 is []>=1 and |
| 99 | +//│ > let $first0@46 = xs@44#1.0 |
| 100 | +//│ > let $rest@45 = globalThis:import#Tuple#666.tupleSlice‹member:tupleSlice›(xs@44#2, 1, 0) |
| 101 | +//│ > $first0@46 is Some($param0@47) and |
| 102 | +//│ > let y@48 = $param0@47#0 |
| 103 | +//│ > let rest@49 = $rest@45#0 |
| 104 | +//│ > else y@48#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@58 is []=2 and |
| 115 | +//│ > let $first0@60 = zs@58#4.0 |
| 116 | +//│ > let $first1@63 = zs@58#5.1 |
| 117 | +//│ > $first0@60 is Some($param0@64) and |
| 118 | +//│ > let x@65 = $param0@64#0 |
| 119 | +//│ > $first1@63 is Some($param0@66) and |
| 120 | +//│ > let y@67 = $param0@66#0 |
| 121 | +//│ > else builtin:+#99(x@65#666, y@67#666) |
| 122 | +//│ > zs@58 is []>=1 and |
| 123 | +//│ > let $first0@60 = zs@58#1.0 |
| 124 | +//│ > let $rest@59 = globalThis:import#Tuple#666.tupleSlice‹member:tupleSlice›(zs@58#2, 1, 0) |
| 125 | +//│ > $first0@60 is None and |
| 126 | +//│ > let rest@61 = $rest@59#0 |
| 127 | +//│ > else globalThis:block#3#666.do_something‹member:do_something›(rest@61#666) |
| 128 | +//│ Normalized: |
| 129 | +//│ > if |
| 130 | +//│ > zs@58 is []=2 and |
| 131 | +//│ > let $first0@60 = zs@58#4.0 |
| 132 | +//│ > let $first1@63 = zs@58#5.1 |
| 133 | +//│ > $first0@60 is Some($param0@64) and |
| 134 | +//│ > let x@65 = $param0@64#0 |
| 135 | +//│ > $first1@63 is Some($param0@66) and |
| 136 | +//│ > let y@67 = $param0@66#0 |
| 137 | +//│ > else builtin:+#99(x@65#666, y@67#666) |
| 138 | +//│ > let $rest@59 = globalThis:import#Tuple#666.tupleSlice‹member:tupleSlice›(zs@58#2, 1, 0) |
| 139 | +//│ > let $rest@59 = globalThis:import#Tuple#666.tupleSlice‹member:tupleSlice›(zs@58#2, 1, 0) |
| 140 | +//│ > $first0@60 is None and |
| 141 | +//│ > let rest@61 = $rest@59#0 |
| 142 | +//│ > else globalThis:block#3#666.do_something‹member:do_something›(rest@61#666) |
| 143 | +//│ > zs@58 is []>=1 and |
| 144 | +//│ > let $first0@60 = zs@58#1.0 |
| 145 | +//│ > let $rest@59 = globalThis:import#Tuple#666.tupleSlice‹member:tupleSlice›(zs@58#2, 1, 0) |
| 146 | +//│ > $first0@60 is None and |
| 147 | +//│ > let rest@61 = $rest@59#0 |
| 148 | +//│ > else globalThis:block#3#666.do_something‹member:do_something›(rest@61#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