refactor how we solve LetCon

This commit is contained in:
Folkert 2020-10-19 22:18:20 +02:00
parent 6d866ff58d
commit 48d13a7b12

View file

@ -117,6 +117,12 @@ impl Pools {
.split_last() .split_last()
.unwrap_or_else(|| panic!("Attempted to split_last() on non-empy Pools")) .unwrap_or_else(|| panic!("Attempted to split_last() on non-empy Pools"))
} }
pub fn extend_to(&mut self, n: usize) {
for _ in self.len()..n {
self.0.push(Vec::new());
}
}
} }
#[derive(Clone)] #[derive(Clone)]
@ -445,7 +451,17 @@ fn solve(
subs.set_rank(var, next_rank); subs.set_rank(var, next_rank);
} }
let work_in_next_pools = |next_pools: &mut Pools| { // determine the next pool
let next_pools;
if next_rank.into_usize() < pools.len() {
next_pools = pools
} else {
// we should be off by one at this point
debug_assert_eq!(next_rank.into_usize(), 1 + pools.len());
pools.extend_to(next_rank.into_usize());
next_pools = pools;
}
let pool: &mut Vec<Variable> = next_pools.get_mut(next_rank); let pool: &mut Vec<Variable> = next_pools.get_mut(next_rank);
// Replace the contents of this pool with rigid_vars and flex_vars // Replace the contents of this pool with rigid_vars and flex_vars
@ -454,16 +470,16 @@ fn solve(
pool.extend(rigid_vars.iter()); pool.extend(rigid_vars.iter());
pool.extend(flex_vars.iter()); pool.extend(flex_vars.iter());
let mut new_env = env.clone(); // run solver in next pool
// Add a variable for each def to local_def_vars. // Add a variable for each def to local_def_vars.
let mut local_def_vars = ImMap::default(); let mut local_def_vars = ImMap::default();
for (symbol, loc_type) in let_con.def_types.iter() { for (symbol, loc_type) in let_con.def_types.iter() {
let def_type = loc_type.value.clone(); let def_type = &loc_type.value;
let var = let var =
type_to_var(subs, next_rank, next_pools, cached_aliases, &def_type); type_to_var(subs, next_rank, next_pools, cached_aliases, def_type);
local_def_vars.insert( local_def_vars.insert(
*symbol, *symbol,
@ -474,11 +490,12 @@ fn solve(
); );
} }
// run solver in next pool
// Solve the assignments' constraints first. // Solve the assignments' constraints first.
let new_state = solve( let State {
&new_env, env: saved_env,
mark,
} = solve(
&env,
state, state,
next_rank, next_rank,
next_pools, next_pools,
@ -487,7 +504,8 @@ fn solve(
subs, subs,
&let_con.defs_constraint, &let_con.defs_constraint,
); );
let young_mark = new_state.mark;
let young_mark = mark;
let visit_mark = young_mark.next(); let visit_mark = young_mark.next();
let final_mark = visit_mark.next(); let final_mark = visit_mark.next();
@ -508,12 +526,7 @@ fn solve(
let result = offenders.len(); let result = offenders.len();
if result > 0 { if result > 0 {
dbg!( dbg!(&subs, &offenders, &let_con.def_types, &let_con.def_aliases);
&subs,
&offenders,
&let_con.def_types,
&let_con.def_aliases
);
} }
result result
@ -548,7 +561,9 @@ fn solve(
failing.is_empty() failing.is_empty()
}); });
let mut new_env = env.clone();
for (symbol, loc_var) in local_def_vars.iter() { for (symbol, loc_var) in local_def_vars.iter() {
// when there are duplicates, keep the one from `env`
if !new_env.vars_by_symbol.contains_key(&symbol) { if !new_env.vars_by_symbol.contains_key(&symbol) {
new_env.vars_by_symbol.insert(*symbol, loc_var.value); new_env.vars_by_symbol.insert(*symbol, loc_var.value);
} }
@ -557,7 +572,7 @@ fn solve(
// Note that this vars_by_symbol is the one returned by the // Note that this vars_by_symbol is the one returned by the
// previous call to solve() // previous call to solve()
let temp_state = State { let temp_state = State {
env: new_state.env, env: saved_env,
mark: final_mark, mark: final_mark,
}; };
@ -579,14 +594,6 @@ fn solve(
} }
new_state new_state
};
if next_rank.into_usize() < pools.len() {
work_in_next_pools(pools)
} else {
// TODO shouldn't this grow the pool, it does in the elm source
work_in_next_pools(&mut pools.clone())
}
} }
} }
} }