case where removing a lam with 0 occurrences is not safe

Since a function call can have arbitrary other calls that lead to error and that is difficult to detect.
This commit is contained in:
Kasey White 2023-02-10 19:24:47 -05:00
parent 21fbd48b8d
commit 0269409fa1
1 changed files with 23 additions and 37 deletions

View File

@ -151,22 +151,8 @@ fn inline_basic_reduce(term: &mut Term<Name>) {
| Term::Delay(_) | Term::Delay(_)
| Term::Lambda { .. }) = argument.as_ref() | Term::Lambda { .. }) = argument.as_ref()
{ {
if occurrences == 1 { *term =
*term = substitute_term( substitute_term(body.as_ref(), parameter_name.clone(), replace_term);
body.as_ref(),
parameter_name.clone(),
replace_term,
);
}
}
} else if occurrences == 0 {
error_occurrences(argument.as_ref(), &mut occurrences);
if occurrences == 0 {
*term = substitute_term(
body.as_ref(),
parameter_name.clone(),
argument.as_ref(),
);
} }
} }
} }
@ -208,27 +194,27 @@ fn var_occurrences(term: &Term<Name>, search_for: Rc<Name>, occurrences: &mut us
} }
} }
fn error_occurrences(term: &Term<Name>, occurrences: &mut usize) { // fn error_occurrences(term: &Term<Name>, occurrences: &mut usize) {
match term { // match term {
Term::Delay(body) => { // Term::Delay(body) => {
error_occurrences(body.as_ref(), occurrences); // error_occurrences(body.as_ref(), occurrences);
} // }
Term::Lambda { body, .. } => { // Term::Lambda { body, .. } => {
error_occurrences(body.as_ref(), occurrences); // error_occurrences(body.as_ref(), occurrences);
} // }
Term::Apply { function, argument } => { // Term::Apply { function, argument } => {
error_occurrences(function.as_ref(), occurrences); // error_occurrences(function.as_ref(), occurrences);
error_occurrences(argument.as_ref(), occurrences); // error_occurrences(argument.as_ref(), occurrences);
} // }
Term::Force(x) => { // Term::Force(x) => {
error_occurrences(x.as_ref(), occurrences); // error_occurrences(x.as_ref(), occurrences);
} // }
Term::Error => { // Term::Error => {
*occurrences += 1; // *occurrences += 1;
} // }
_ => {} // _ => {}
} // }
} // }
fn lambda_reduce(term: &mut Term<Name>) { fn lambda_reduce(term: &mut Term<Name>) {
match term { match term {