chore: continue more on curry optimizations

This commit is contained in:
microproofs 2023-12-04 14:56:31 -05:00 committed by Kasey
parent 88e21449c5
commit 8fdedb754e
1 changed files with 32 additions and 15 deletions

View File

@ -597,24 +597,17 @@ impl Program<Name> {
// WIP // WIP
pub fn builtin_curry_reducer(self) -> Program<Name> { pub fn builtin_curry_reducer(self) -> Program<Name> {
let mut curried_terms = vec![]; let mut curried_terms = vec![];
let mut applied_ids = vec![]; let mut curry_applied_ids: Vec<usize> = vec![];
self.traverse_uplc_with(&mut |id, term, mut arg_stack, scope| match term { 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();
}
}
Term::Builtin(func) => { Term::Builtin(func) => {
if can_curry_builtin(*func) { if can_curry_builtin(*func) && arg_stack.len() == func.arity() {
let mut scope = scope.pop(); 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); let is_order_agnostic = is_order_agnostic_builtin(*func);
@ -623,6 +616,30 @@ impl Program<Name> {
.find(|curried_term: &&mut CurriedBuiltin| curried_term.func == *func) .find(|curried_term: &&mut CurriedBuiltin| curried_term.func == *func)
{ {
let mut current_children = &mut curried_builtin.children; 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 { } else {
let Some(curried_tree) = arg_stack let Some(curried_tree) = arg_stack
.into_iter() .into_iter()