Skip to content

Commit

Permalink
Fix assigns sometimes having free uniques
Browse files Browse the repository at this point in the history
  • Loading branch information
MicroProofs committed Oct 29, 2024
1 parent 1be28be commit e33bd2a
Showing 1 changed file with 70 additions and 56 deletions.
126 changes: 70 additions & 56 deletions crates/aiken-lang/src/gen_uplc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2516,7 +2516,6 @@ impl<'a> CodeGenerator<'a> {
tail_cases,
default,
} => {
println!("PATH IS {:#?}", path);
//Current path to test
let current_tipo = get_tipo_by_path(subject_tipo.clone(), &path);
let builtins_path = Builtins::new_from_path(subject_tipo.clone(), path);
Expand All @@ -2526,21 +2525,15 @@ impl<'a> CodeGenerator<'a> {
format!("{}_{}", subject_name, builtins_path.to_string())
};

println!("Current IS {:#?}", builtins_path);

// Transition process from previous to current
let builtins_to_add = stick_set.diff_union_builtins(builtins_path.clone());

println!("TO ADD IS {:#?}", builtins_to_add);

// Previous path to apply the transition process too
let prev_builtins = Builtins {
vec: builtins_path.vec[0..(builtins_path.len() - builtins_to_add.len())]
.to_vec(),
};

println!("PREV IS {:#?}", prev_builtins);

let prev_subject_name = if prev_builtins.is_empty() {
subject_name.clone()
} else {
Expand All @@ -2554,23 +2547,30 @@ impl<'a> CodeGenerator<'a> {
let longest_pattern = cases.iter().chain(tail_cases.iter()).fold(
0,
|longest, (case, _)| match case {
CaseTest::List(i) | CaseTest::ListWithTail(i) => {
CaseTest::List(i) => {
if longest < *i {
*i
} else {
longest
}
}
CaseTest::ListWithTail(i) => {
if longest < *i {
*i - 1
} else {
longest
}
}
_ => unreachable!(),
},
);

let last_pattern = if tail_cases.is_empty() {
*default.as_ref().unwrap().clone()
} else {
let (_case, tree) = tail_cases.first().unwrap();
let tree = tail_cases.last().unwrap();

tree.clone()
tree.1.clone()
};

let last_case = cases.last().unwrap().0.clone();
Expand All @@ -2589,7 +2589,7 @@ impl<'a> CodeGenerator<'a> {
stick_set.clone(),
);

let list_clauses = (0..longest_pattern).rev().with_position().fold(
let list_clauses = (0..=longest_pattern).rev().with_position().fold(
(builtins_for_pattern, last_pattern),
|(mut builtins_for_pattern, acc), list_item| match list_item {
itertools::Position::First(index) | itertools::Position::Only(index) => {
Expand Down Expand Up @@ -2724,51 +2724,7 @@ impl<'a> CodeGenerator<'a> {
air_args.into_iter().map(|i| i.1).collect_vec(),
);

args.into_iter().rfold(then, |acc, assign| {
let Assigned { path, assigned } = assign;

let current_tipo = get_tipo_by_path(subject_tipo.clone(), &path);
let builtins_path = Builtins::new_from_path(subject_tipo.clone(), path);
let current_subject_name = if builtins_path.is_empty() {
subject_name.clone()
} else {
format!("{}_{}", subject_name, builtins_path.to_string())
};

// Transition process from previous to current
let builtins_to_add = stick_set.diff_union_builtins(builtins_path.clone());

// Previous path to apply the transition process too
let prev_builtins = Builtins {
vec: builtins_path.vec[0..(builtins_path.len() - builtins_to_add.len())]
.to_vec(),
};

let prev_subject_name = if prev_builtins.is_empty() {
subject_name.clone()
} else {
format!("{}_{}", subject_name, prev_builtins.to_string())
};
let prev_tipo = prev_builtins
.vec
.last()
.map_or(subject_tipo.clone(), |last| last.tipo());

let assignment = AirTree::let_assignment(
assigned,
AirTree::local_var(current_subject_name, current_tipo),
acc,
);

let thing = builtins_to_add.to_air(
&mut self.special_functions,
prev_subject_name,
prev_tipo,
assignment,
);

thing
})
self.handle_assigns(subject_name, subject_tipo, &args, &mut stick_set, then)
}
DecisionTree::HoistThen {
name,
Expand Down Expand Up @@ -2808,6 +2764,64 @@ impl<'a> CodeGenerator<'a> {
}
}

fn handle_assigns(
&mut self,
subject_name: &String,
subject_tipo: Rc<Type>,
assigns: &[Assigned],
stick_set: &mut TreeSet,
then: AirTree,
) -> AirTree {
match assigns {
[] => then,
[assign, rest @ ..] => {
let Assigned { path, assigned } = assign;

let current_tipo = get_tipo_by_path(subject_tipo.clone(), path);
let builtins_path = Builtins::new_from_path(subject_tipo.clone(), path.clone());
let current_subject_name = if builtins_path.is_empty() {
subject_name.clone()
} else {
format!("{}_{}", subject_name, builtins_path.to_string())
};

// Transition process from previous to current
let builtins_to_add = stick_set.diff_union_builtins(builtins_path.clone());

// Previous path to apply the transition process too
let prev_builtins = Builtins {
vec: builtins_path.vec[0..(builtins_path.len() - builtins_to_add.len())]
.to_vec(),
};

let prev_subject_name = if prev_builtins.is_empty() {
subject_name.clone()
} else {
format!("{}_{}", subject_name, prev_builtins.to_string())
};
let prev_tipo = prev_builtins
.vec
.last()
.map_or(subject_tipo.clone(), |last| last.tipo());

let assignment = AirTree::let_assignment(
assigned,
AirTree::local_var(current_subject_name, current_tipo),
self.handle_assigns(subject_name, subject_tipo, rest, stick_set, then),
);

let thing = builtins_to_add.to_air(
&mut self.special_functions,
prev_subject_name,
prev_tipo,
assignment,
);

thing
}
}
}

fn hoist_functions_to_validator(&mut self, mut air_tree: AirTree) -> AirTree {
let mut functions_to_hoist = IndexMap::new();
let mut used_functions = vec![];
Expand Down

0 comments on commit e33bd2a

Please sign in to comment.