mirror of
https://github.com/roc-lang/roc.git
synced 2025-09-29 06:44:46 +00:00
refactor how we solve LetCon
This commit is contained in:
parent
6d866ff58d
commit
48d13a7b12
1 changed files with 123 additions and 116 deletions
|
@ -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,148 +451,149 @@ 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 pool: &mut Vec<Variable> = next_pools.get_mut(next_rank);
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
// Replace the contents of this pool with rigid_vars and flex_vars
|
let pool: &mut Vec<Variable> = next_pools.get_mut(next_rank);
|
||||||
pool.clear();
|
|
||||||
pool.reserve(rigid_vars.len() + flex_vars.len());
|
|
||||||
pool.extend(rigid_vars.iter());
|
|
||||||
pool.extend(flex_vars.iter());
|
|
||||||
|
|
||||||
let mut new_env = env.clone();
|
// Replace the contents of this pool with rigid_vars and flex_vars
|
||||||
|
pool.clear();
|
||||||
|
pool.reserve(rigid_vars.len() + flex_vars.len());
|
||||||
|
pool.extend(rigid_vars.iter());
|
||||||
|
pool.extend(flex_vars.iter());
|
||||||
|
|
||||||
// Add a variable for each def to local_def_vars.
|
// run solver in next pool
|
||||||
let mut local_def_vars = ImMap::default();
|
|
||||||
|
|
||||||
for (symbol, loc_type) in let_con.def_types.iter() {
|
// Add a variable for each def to local_def_vars.
|
||||||
let def_type = loc_type.value.clone();
|
let mut local_def_vars = ImMap::default();
|
||||||
|
|
||||||
let var =
|
for (symbol, loc_type) in let_con.def_types.iter() {
|
||||||
type_to_var(subs, next_rank, next_pools, cached_aliases, &def_type);
|
let def_type = &loc_type.value;
|
||||||
|
|
||||||
local_def_vars.insert(
|
let var =
|
||||||
*symbol,
|
type_to_var(subs, next_rank, next_pools, cached_aliases, def_type);
|
||||||
Located {
|
|
||||||
value: var,
|
|
||||||
region: loc_type.region,
|
|
||||||
},
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
// run solver in next pool
|
local_def_vars.insert(
|
||||||
|
*symbol,
|
||||||
// Solve the assignments' constraints first.
|
Located {
|
||||||
let new_state = solve(
|
value: var,
|
||||||
&new_env,
|
region: loc_type.region,
|
||||||
state,
|
|
||||||
next_rank,
|
|
||||||
next_pools,
|
|
||||||
problems,
|
|
||||||
cached_aliases,
|
|
||||||
subs,
|
|
||||||
&let_con.defs_constraint,
|
|
||||||
);
|
|
||||||
let young_mark = new_state.mark;
|
|
||||||
let visit_mark = young_mark.next();
|
|
||||||
let final_mark = visit_mark.next();
|
|
||||||
|
|
||||||
debug_assert_eq!(
|
|
||||||
{
|
|
||||||
let offenders = next_pools
|
|
||||||
.get(next_rank)
|
|
||||||
.iter()
|
|
||||||
.filter(|var| {
|
|
||||||
let current = subs.get_without_compacting(
|
|
||||||
roc_types::subs::Variable::clone(var),
|
|
||||||
);
|
|
||||||
|
|
||||||
current.rank.into_usize() > next_rank.into_usize()
|
|
||||||
})
|
|
||||||
.collect::<Vec<_>>();
|
|
||||||
|
|
||||||
let result = offenders.len();
|
|
||||||
|
|
||||||
if result > 0 {
|
|
||||||
dbg!(
|
|
||||||
&subs,
|
|
||||||
&offenders,
|
|
||||||
&let_con.def_types,
|
|
||||||
&let_con.def_aliases
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
result
|
|
||||||
},
|
},
|
||||||
0
|
|
||||||
);
|
);
|
||||||
|
}
|
||||||
|
|
||||||
// pop pool
|
// Solve the assignments' constraints first.
|
||||||
generalize(subs, young_mark, visit_mark, next_rank, next_pools);
|
let State {
|
||||||
|
env: saved_env,
|
||||||
|
mark,
|
||||||
|
} = solve(
|
||||||
|
&env,
|
||||||
|
state,
|
||||||
|
next_rank,
|
||||||
|
next_pools,
|
||||||
|
problems,
|
||||||
|
cached_aliases,
|
||||||
|
subs,
|
||||||
|
&let_con.defs_constraint,
|
||||||
|
);
|
||||||
|
|
||||||
next_pools.get_mut(next_rank).clear();
|
let young_mark = mark;
|
||||||
|
let visit_mark = young_mark.next();
|
||||||
|
let final_mark = visit_mark.next();
|
||||||
|
|
||||||
// check that things went well
|
debug_assert_eq!(
|
||||||
debug_assert!({
|
{
|
||||||
// NOTE the `subs.redundant` check is added for the uniqueness
|
let offenders = next_pools
|
||||||
// inference, and does not come from elm. It's unclear whether this is
|
.get(next_rank)
|
||||||
// a bug with uniqueness inference (something is redundant that
|
|
||||||
// shouldn't be) or that it just never came up in elm.
|
|
||||||
let failing: Vec<_> = rigid_vars
|
|
||||||
.iter()
|
.iter()
|
||||||
.filter(|&var| {
|
.filter(|var| {
|
||||||
!subs.redundant(*var)
|
let current = subs.get_without_compacting(
|
||||||
&& subs.get_without_compacting(*var).rank != Rank::NONE
|
roc_types::subs::Variable::clone(var),
|
||||||
|
);
|
||||||
|
|
||||||
|
current.rank.into_usize() > next_rank.into_usize()
|
||||||
})
|
})
|
||||||
.collect();
|
.collect::<Vec<_>>();
|
||||||
|
|
||||||
if !failing.is_empty() {
|
let result = offenders.len();
|
||||||
println!("Rigids {:?}", &rigid_vars);
|
|
||||||
println!("Failing {:?}", failing);
|
if result > 0 {
|
||||||
|
dbg!(&subs, &offenders, &let_con.def_types, &let_con.def_aliases);
|
||||||
}
|
}
|
||||||
|
|
||||||
failing.is_empty()
|
result
|
||||||
});
|
},
|
||||||
|
0
|
||||||
|
);
|
||||||
|
|
||||||
for (symbol, loc_var) in local_def_vars.iter() {
|
// pop pool
|
||||||
if !new_env.vars_by_symbol.contains_key(&symbol) {
|
generalize(subs, young_mark, visit_mark, next_rank, next_pools);
|
||||||
new_env.vars_by_symbol.insert(*symbol, loc_var.value);
|
|
||||||
}
|
next_pools.get_mut(next_rank).clear();
|
||||||
|
|
||||||
|
// check that things went well
|
||||||
|
debug_assert!({
|
||||||
|
// NOTE the `subs.redundant` check is added for the uniqueness
|
||||||
|
// inference, and does not come from elm. It's unclear whether this is
|
||||||
|
// a bug with uniqueness inference (something is redundant that
|
||||||
|
// shouldn't be) or that it just never came up in elm.
|
||||||
|
let failing: Vec<_> = rigid_vars
|
||||||
|
.iter()
|
||||||
|
.filter(|&var| {
|
||||||
|
!subs.redundant(*var)
|
||||||
|
&& subs.get_without_compacting(*var).rank != Rank::NONE
|
||||||
|
})
|
||||||
|
.collect();
|
||||||
|
|
||||||
|
if !failing.is_empty() {
|
||||||
|
println!("Rigids {:?}", &rigid_vars);
|
||||||
|
println!("Failing {:?}", failing);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Note that this vars_by_symbol is the one returned by the
|
failing.is_empty()
|
||||||
// previous call to solve()
|
});
|
||||||
let temp_state = State {
|
|
||||||
env: new_state.env,
|
|
||||||
mark: final_mark,
|
|
||||||
};
|
|
||||||
|
|
||||||
// Now solve the body, using the new vars_by_symbol which includes
|
let mut new_env = env.clone();
|
||||||
// the assignments' name-to-variable mappings.
|
for (symbol, loc_var) in local_def_vars.iter() {
|
||||||
let new_state = solve(
|
// when there are duplicates, keep the one from `env`
|
||||||
&new_env,
|
if !new_env.vars_by_symbol.contains_key(&symbol) {
|
||||||
temp_state,
|
new_env.vars_by_symbol.insert(*symbol, loc_var.value);
|
||||||
rank,
|
|
||||||
next_pools,
|
|
||||||
problems,
|
|
||||||
cached_aliases,
|
|
||||||
subs,
|
|
||||||
&ret_con,
|
|
||||||
);
|
|
||||||
|
|
||||||
for (symbol, loc_var) in local_def_vars {
|
|
||||||
check_for_infinite_type(subs, problems, symbol, loc_var);
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
new_state
|
// Note that this vars_by_symbol is the one returned by the
|
||||||
|
// previous call to solve()
|
||||||
|
let temp_state = State {
|
||||||
|
env: saved_env,
|
||||||
|
mark: final_mark,
|
||||||
};
|
};
|
||||||
|
|
||||||
if next_rank.into_usize() < pools.len() {
|
// Now solve the body, using the new vars_by_symbol which includes
|
||||||
work_in_next_pools(pools)
|
// the assignments' name-to-variable mappings.
|
||||||
} else {
|
let new_state = solve(
|
||||||
// TODO shouldn't this grow the pool, it does in the elm source
|
&new_env,
|
||||||
work_in_next_pools(&mut pools.clone())
|
temp_state,
|
||||||
|
rank,
|
||||||
|
next_pools,
|
||||||
|
problems,
|
||||||
|
cached_aliases,
|
||||||
|
subs,
|
||||||
|
&ret_con,
|
||||||
|
);
|
||||||
|
|
||||||
|
for (symbol, loc_var) in local_def_vars {
|
||||||
|
check_for_infinite_type(subs, problems, symbol, loc_var);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
new_state
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue