Fix merge_definitions stack overflow

This commit is contained in:
LunaAmora 2024-01-31 12:57:55 -03:00
parent 262be81a3f
commit 24f64d4c15
2 changed files with 8 additions and 13 deletions

View File

@ -7,16 +7,14 @@ impl Book {
/// Expects variables to be linear.
pub fn merge_definitions(&mut self, main: DefId) {
let ids: Vec<_> = self.defs.keys().copied().collect();
let filtered_ids = ids.into_iter().filter(|&id| id != main);
self.merge(main, filtered_ids);
self.merge(main, ids.into_iter());
}
fn merge(&mut self, main: DefId, defs: impl Iterator<Item = DefId>) {
let mut term_map: HashMap<Term, DefId> = HashMap::new();
let mut def_id_map: HashMap<DefId, DefId> = HashMap::new();
self.collect_terms(defs, &mut term_map, &mut def_id_map);
self.collect_terms(defs.filter(|&id| id != main), &mut term_map, &mut def_id_map);
self.merge_terms(main, term_map, def_id_map);
}
@ -32,10 +30,10 @@ impl Book {
def.assert_no_pattern_matching_rules();
let term = std::mem::take(&mut def.rules[0].body);
if let Some(new) = term_map.get(&term) {
def_id_map.insert(id, *new);
if let Some(&new) = term_map.get(&term) {
def_id_map.insert(new, new);
def_id_map.insert(id, new);
} else {
def_id_map.insert(id, id);
term_map.insert(term, id);
}
}
@ -73,9 +71,7 @@ impl Book {
for def in self.defs.values_mut() {
if subst_ref_to_ref(&mut def.rules[0].body, &def_id_map) {
if def.def_id != main {
updated_defs.push(def.def_id);
}
updated_defs.push(def.def_id);
}
}

View File

@ -9,7 +9,7 @@ input_file: tests/golden_tests/compile_file_o_all/list_merge_sort.hvm
@Nil = {4 * {4 a a}}
@P = {8 a {6 {4 @P {4 @N (b c)}} ({3 (a d) b} {4 {8 d {6 c e}} {4 * e}})}}
@Pure = (a {4 {8 a {6 @Nil b}} {4 * b}})
@U = {8 a {6 {4 @m {4 @n (b (a c))}} (b c)}}
@U = {8 a {6 {4 @m {4 @i (b (a c))}} (b c)}}
@V = (* @Nil)
@W = (a ({4 @U {4 @V (a b)}} b))
@Z = {8 a {6 {4 @s {4 @t (b (a c))}} (b c)}}
@ -18,11 +18,10 @@ input_file: tests/golden_tests/compile_file_o_all/list_merge_sort.hvm
@g = (* @w)
@h = {8 a {6 b (c ({4 @f {4 @g (c (a (b d)))}} d))}}
@i = (* (a a))
@m = {8 a {6 {4 @Z {4 @a (b {4 @m {4 @n (c (d e))}})}} ({21 c {23 f b}} ({4 @h {4 @i (f (a d))}} e))}}
@m = {8 a {6 {4 @Z {4 @a (b {4 @m {4 @i (c (d e))}})}} ({21 c {23 f b}} ({4 @h {4 @i (f (a d))}} e))}}
@main = (a (b c))
& @W ~ (a (d c))
& @Map ~ (b (@Pure d))
@n = (* (a a))
@s = {8 a {6 {4 @Z {4 @a (b c)}} ({23 d b} ({4 @h {4 @i (d (a e))}} {4 {8 e {6 c f}} {4 * f}}))}}
@t = (* @x)
@w = (a (b {4 {8 a {6 b c}} {4 * c}}))