From 8fdedb754ed0d12a961696198feee096e31cac0e Mon Sep 17 00:00:00 2001 From: microproofs Date: Mon, 4 Dec 2023 14:56:31 -0500 Subject: [PATCH] chore: continue more on curry optimizations --- crates/uplc/src/optimize/shrinker.rs | 47 +++++++++++++++++++--------- 1 file changed, 32 insertions(+), 15 deletions(-) diff --git a/crates/uplc/src/optimize/shrinker.rs b/crates/uplc/src/optimize/shrinker.rs index 25b2ef2b..a0e1ce6c 100644 --- a/crates/uplc/src/optimize/shrinker.rs +++ b/crates/uplc/src/optimize/shrinker.rs @@ -597,24 +597,17 @@ impl Program { // WIP pub fn builtin_curry_reducer(self) -> Program { let mut curried_terms = vec![]; - let mut applied_ids = vec![]; + let mut curry_applied_ids: Vec = vec![]; - self.traverse_uplc_with(&mut |id, term, mut arg_stack, scope| match term { - Term::Apply { function, argument } => { - // We are apply some arg so now we unwrap the id of the applied arg - let id = id.unwrap(); - - if applied_ids.contains(&id) { - let func = Rc::make_mut(function); - // we inlined the arg so now remove the apply and arg from the program - *term = func.clone(); - } - } + self.traverse_uplc_with(&mut |_id, term, mut arg_stack, scope| match term { Term::Builtin(func) => { - if can_curry_builtin(*func) { - let mut scope = scope.pop(); + if can_curry_builtin(*func) && arg_stack.len() == func.arity() { + let mut scope = scope.clone(); - let arg_number = func.arity(); + // Get upper scope of the function plus args + for _ in 0..func.arity() { + scope = scope.pop(); + } let is_order_agnostic = is_order_agnostic_builtin(*func); @@ -623,6 +616,30 @@ impl Program { .find(|curried_term: &&mut CurriedBuiltin| curried_term.func == *func) { let mut current_children = &mut curried_builtin.children; + + let ordered_args = + arg_stack + .into_iter() + .map(|(_, arg)| arg) + .sorted_by(|arg1, arg2| { + if is_order_agnostic { + if matches!(arg1, Term::Constant(_)) + && matches!(arg2, Term::Constant(_)) + { + std::cmp::Ordering::Equal + } else if matches!(arg1, Term::Constant(_)) { + std::cmp::Ordering::Greater + } else if matches!(arg2, Term::Constant(_)) { + std::cmp::Ordering::Less + } else { + std::cmp::Ordering::Equal + } + } else { + std::cmp::Ordering::Equal + } + }); + + todo!("Finish this") } else { let Some(curried_tree) = arg_stack .into_iter()