Merge pull request #591 from HigherOrderCO/call-desugar-use-before-linearize-matches

Call desugar_use before linearize matches
This commit is contained in:
Nicolas Abril 2024-06-17 18:08:01 +00:00 committed by GitHub
commit 0381f8a109
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 102 additions and 31 deletions

View File

@ -114,6 +114,7 @@ pub fn desugar_book(
// Auto match linearization
ctx.book.make_var_names_unique();
ctx.book.desugar_use();
match opts.linearize_matches {
OptLevel::Disabled => (),
OptLevel::Alt => ctx.book.linearize_match_binds(),

View File

@ -239,6 +239,7 @@ fn simplify_matches() {
ctx.desugar_with_blocks()?;
ctx.check_unbound_vars()?;
ctx.book.make_var_names_unique();
ctx.book.desugar_use();
ctx.book.linearize_match_binds();
ctx.book.linearize_match_with();
ctx.check_unbound_vars()?;
@ -288,6 +289,7 @@ fn encode_pattern_match() {
ctx.desugar_with_blocks()?;
ctx.check_unbound_vars()?;
ctx.book.make_var_names_unique();
ctx.book.desugar_use();
ctx.book.linearize_match_binds();
ctx.book.linearize_match_with();
ctx.book.encode_matches(adt_encoding);

View File

@ -0,0 +1,23 @@
def prng(state):
state = state ^ (state << 13)
state = state ^ (state >> 17)
state = state ^ (state << 5)
return state
def fullMap:
bend i = 14:
when i > 0:
return Map/Node(1, fork(i - 1), fork(i - 1))
else:
return Map/Leaf
def test(map):
bend i = 0, map:
when i < 1000:
(curr, map) = Map/get(map, prng(i) % 4096)
return curr+fork(i + 1, map)
else:
return 0
def main:
return test(fullMap)

View File

@ -128,14 +128,14 @@ input_file: tests/golden_tests/cli/no_check_net_size.bend
@Sum__C2 = (?((0 @Sum__C1) a) a)
@Swap = (?((@Swap__C0 @Swap__C1) (a (b c))) (b (a c)))
@Swap = (?((@Swap__C0 @Swap__C1) a) a)
@Swap__C0 = (b (a c))
& @Map_/Both ~ (a (b c))
@Swap__C1 = (* a)
@Swap__C0 = a
& @Map_/Both ~ a
@Swap__C1 = (* (b (a c)))
& @Map_/Both ~ (a (b c))
@ToArr = (a ((@ToArr__C3 (a b)) b))
@ToArr__C0 = a

View File

@ -12,12 +12,12 @@ input_file: tests/golden_tests/compile_file/redex_order_recursive.bend
@List.concat__C1 = (?(((a a) @List.concat__C0) b) b)
@List.fold = ((@List.fold__C1 (a (b c))) (b (a c)))
@List.fold = ((@List.fold__C1 a) a)
@List.fold__C0 = (* (a (b (d ({(a (e f)) c} f)))))
@List.fold__C0 = (* (a (b ({(a (e f)) c} (d f)))))
& @List.fold ~ (b (c (d e)))
@List.fold__C1 = (?(((a (* a)) @List.fold__C0) b) b)
@List.fold__C1 = (?(((* (a a)) @List.fold__C0) b) b)
@List.len = ((@List.len__C1 a) a)
@ -43,12 +43,12 @@ input_file: tests/golden_tests/compile_file/redex_order_recursive.bend
@List.map__C1 = (?(((* @List/Nil) @List.map__C0) a) a)
@List.reduce = ((@List.reduce__C1 (a (b c))) (b (a c)))
@List.reduce = ((@List.reduce__C1 a) a)
@List.reduce__C0 = (* (d (a (c ({b (c (d e))} f)))))
@List.reduce__C0 = (* (d (a ({b (c (d e))} (c f)))))
& @List.reduce ~ (a (b (e f)))
@List.reduce__C1 = (?(((a (* a)) @List.reduce__C0) b) b)
@List.reduce__C1 = (?(((* (a a)) @List.reduce__C0) b) b)
@List.reverse_bad = ((@List.reverse_bad__C1 a) a)
@ -164,7 +164,7 @@ input_file: tests/golden_tests/compile_file/redex_order_recursive.bend
@main = *
@max = ({$([>] $(a ?(((b (* b)) (* (* (c c)))) (d (e f))))) e} ({a d} f))
@max = ({$([>] $(a ?(((* (b b)) (* (c (* c)))) (d (e f))))) d} ({a e} f))
@tail_recursive = ((@tail_recursive__C0 ((* 0) a)) a)

View File

@ -6,7 +6,7 @@ input_file: tests/golden_tests/desugar_file/mapper_syntax.bend
(Map/get) = λa λb (a Map/get__C5 b)
(Map/set) = λa λb λc (a Map/set__C10 c b)
(Map/set) = λa λb λc (a Map/set__C10 b c)
(Map/map) = λa λb λc (a Map/map__C5 b c)
@ -24,44 +24,44 @@ input_file: tests/golden_tests/desugar_file/mapper_syntax.bend
(Map/get__C1) = λ* λa λb λc λd let (e, f) = (Map/get d (/ a 2)); (e, (Map/Node b c f))
(Map/get__C2) = λa let {b c} = a; λd λe λf λ* (switch (% b 2) { 0: Map/get__C0; _: Map/get__C1; } c d e f)
(Map/get__C2) = λa let {b c} = a; λd λe λf (switch (% b 2) { 0: Map/get__C0; _: Map/get__C1; } c d e f)
(Map/get__C3) = λ* λ* λa λ* λ* λb (a, b)
(Map/get__C3) = λ* λ* λa let {b c} = a; λd λe (b, (Map/Node c d e))
(Map/get__C4) = λa let {b c} = a; λd let {e f} = d; λg let {h i} = g; λj let {k l} = j; (switch (== 0 k) { 0: Map/get__C2; _: Map/get__C3; } l b e h (Map/Node c f i))
(Map/get__C4) = λa λb λc λd let {e f} = d; (switch (== 0 e) { 0: Map/get__C2; _: Map/get__C3; } f a b c)
(Map/get__C5) = λa switch a { 0: Map/get__C4; _: λ* λ* (*, Map/Leaf); }
(Map/map__C0) = λa λb λc λd λe (Map/Node e (Map/map d (/ b 2) a) c)
(Map/map__C0) = λa λb λc λd λe (Map/Node c (Map/map d (/ a 2) b) e)
(Map/map__C1) = λ* λa λb λc λd λe (Map/Node e d (Map/map c (/ b 2) a))
(Map/map__C1) = λ* λa λb λc λd λe (Map/Node c d (Map/map e (/ a 2) b))
(Map/map__C2) = λa λb let {c d} = b; λe λf λg (switch (% c 2) { 0: Map/map__C0; _: Map/map__C1; } a d e f g)
(Map/map__C2) = λa let {b c} = a; λd λe λf λg (switch (% b 2) { 0: Map/map__C0; _: Map/map__C1; } c d e f g)
(Map/map__C3) = λ* λa λ* λb λc λd (Map/Node (a d) c b)
(Map/map__C3) = λ* λ* λa λb λc λd (Map/Node (a b) c d)
(Map/map__C4) = λa λb λc λd let {e f} = d; λg (switch (== 0 e) { 0: Map/map__C2; _: Map/map__C3; } g f c b a)
(Map/map__C4) = λa λb λc λd let {e f} = d; λg (switch (== 0 e) { 0: Map/map__C2; _: Map/map__C3; } f g a b c)
(Map/map__C5) = λa switch a { 0: Map/map__C4; _: λ* λ* λ* Map/Leaf; }
(Map/set__C0) = λa λb λc λd λe (Map/Node c (Map/set d (/ b 2) a) e)
(Map/set__C0) = λa λb λc λd λe (Map/Node c (Map/set d (/ a 2) b) e)
(Map/set__C1) = λ* λa λb λc λd λe (Map/Node c d (Map/set e (/ b 2) a))
(Map/set__C1) = λ* λa λb λc λd λe (Map/Node c d (Map/set e (/ a 2) b))
(Map/set__C10) = λa switch a { 0: Map/set__C8; _: Map/set__C9; }
(Map/set__C2) = λa λb let {c d} = b; λe λf λg (switch (% c 2) { 0: Map/set__C0; _: Map/set__C1; } a d e f g)
(Map/set__C2) = λa let {b c} = a; λd λe λf λg (switch (% b 2) { 0: Map/set__C0; _: Map/set__C1; } c d e f g)
(Map/set__C3) = λ* λa λ* λ* λb λc (Map/Node a b c)
(Map/set__C3) = λ* λ* λa λ* λb λc (Map/Node a b c)
(Map/set__C4) = λa λb (Map/Node * (Map/set Map/Leaf (/ b 2) a) Map/Leaf)
(Map/set__C4) = λa λb (Map/Node * (Map/set Map/Leaf (/ a 2) b) Map/Leaf)
(Map/set__C5) = λ* λa λb (Map/Node * Map/Leaf (Map/set Map/Leaf (/ b 2) a))
(Map/set__C5) = λ* λa λb (Map/Node * Map/Leaf (Map/set Map/Leaf (/ a 2) b))
(Map/set__C6) = λa λb let {c d} = b; (switch (% c 2) { 0: Map/set__C4; _: Map/set__C5; } a d)
(Map/set__C6) = λa let {b c} = a; λd (switch (% b 2) { 0: Map/set__C4; _: Map/set__C5; } c d)
(Map/set__C7) = λ* λa λ* (Map/Node a Map/Leaf Map/Leaf)
(Map/set__C7) = λ* λ* λa (Map/Node a Map/Leaf Map/Leaf)
(Map/set__C8) = λa λb λc λd λe let {f g} = e; (switch (== 0 f) { 0: Map/set__C2; _: Map/set__C3; } d g a b c)
(Map/set__C8) = λa λb λc λd let {e f} = d; λg (switch (== 0 e) { 0: Map/set__C2; _: Map/set__C3; } f g a b c)
(Map/set__C9) = λ* λa λb let {c d} = b; (switch (== 0 c) { 0: Map/set__C6; _: Map/set__C7; } a d)
(Map/set__C9) = λ* λa let {b c} = a; λd (switch (== 0 b) { 0: Map/set__C6; _: Map/set__C7; } c d)

View File

@ -0,0 +1,45 @@
---
source: tests/golden_tests.rs
input_file: tests/golden_tests/encode_pattern_match/full_map.bend
---
Scott
(Map/get) = λa (a λb let {b b_2 b_3 b_4} = b; λc let {c c_2 c_3} = c; λd let {d d_2 d_3} = d; λe let {e e_2 e_3 e_4} = e; switch (== 0 e) { 0: switch (% e_2 2) { 0: let (f, g) = (Map/get c (/ e_3 2)); (f, (Map/Node b g d)); _: λ* let (i, j) = (Map/get d_2 (/ e_4 2)); (i, (Map/Node b_2 c_2 j)); }; _: λ* (b_3, (Map/Node b_4 c_3 d_3)); } λ* (*, Map/Leaf))
(prng) = λa let {a a_2} = a; let {b b_2} = (^ a (<< a_2 13)); let {c c_2} = (^ b (>> b_2 17)); (^ c (<< c_2 5))
(fullMap) = (fullMap__bend0 14)
(test) = λa (test__bend0 0 a)
(main) = (test fullMap)
(Map/Node) = λa λb λc λd λ* (d a b c)
(Map/Leaf) = λ* λb b
(fullMap__bend0) = λa let {a a_2 a_3} = a; switch (> a 0) { 0: Map/Leaf; _: λ* (Map/Node 1 (fullMap__bend0 (- a_2 1)) (fullMap__bend0 (- a_3 1))); }
(test__bend0) = λa let {a a_2 a_3} = a; switch (< a 1000) { 0: λ* 0; _: λ* λd let (e, f) = (Map/get d (% (prng a_2) 4096)); (+ e (test__bend0 (+ a_3 1) f)); }
NumScott
(Map/get) = λa (a λb switch b { 0: λc let {c c_2 c_3 c_4} = c; λd let {d d_2 d_3} = d; λe let {e e_2 e_3} = e; λf let {f f_2 f_3 f_4} = f; switch (== 0 f) { 0: switch (% f_2 2) { 0: let (g, h) = (Map/get d (/ f_3 2)); (g, (Map/Node c h e)); _: λ* let (j, k) = (Map/get e_2 (/ f_4 2)); (j, (Map/Node c_2 d_2 k)); }; _: λ* (c_3, (Map/Node c_4 d_3 e_3)); }; _: λ* λ* (*, Map/Leaf); })
(prng) = λa let {a a_2} = a; let {b b_2} = (^ a (<< a_2 13)); let {c c_2} = (^ b (>> b_2 17)); (^ c (<< c_2 5))
(fullMap) = (fullMap__bend0 14)
(test) = λa (test__bend0 0 a)
(main) = (test fullMap)
(Map/Node) = λa λb λc λd (d Map/Node/tag a b c)
(Map/Leaf) = λa (a Map/Leaf/tag)
(Map/Node/tag) = 0
(Map/Leaf/tag) = 1
(fullMap__bend0) = λa let {a a_2 a_3} = a; switch (> a 0) { 0: Map/Leaf; _: λ* (Map/Node 1 (fullMap__bend0 (- a_2 1)) (fullMap__bend0 (- a_3 1))); }
(test__bend0) = λa let {a a_2 a_3} = a; switch (< a 1000) { 0: λ* 0; _: λ* λd let (e, f) = (Map/get d (% (prng a_2) 4096)); (+ e (test__bend0 (+ a_3 1) f)); }