make solve that uses new constraint

This commit is contained in:
Folkert 2022-03-02 19:43:03 +01:00
parent 2598e8fe8c
commit fe48bdf5b1
No known key found for this signature in database
GPG key ID: 1F17F6FFD112B97C
3 changed files with 577 additions and 16 deletions

View file

@ -1,6 +1,5 @@
use bumpalo::Bump;
use roc_can::constraint::Constraint::{self, *};
use roc_can::constraint::PresenceConstraint;
use roc_can::constraint::Constraint;
use roc_can::expected::{Expected, PExpected};
use roc_collections::all::MutMap;
use roc_module::ident::TagName;
@ -226,6 +225,16 @@ enum Work<'a> {
After(After),
}
enum WorkSoa<'a> {
Constraint {
env: &'a Env,
rank: Rank,
constraint: &'a roc_can::constraint_soa::Constraint,
},
/// Something to be done after a constraint and all its dependencies are fully solved.
After(After),
}
#[allow(clippy::too_many_arguments)]
fn solve(
arena: &Bump,
@ -236,8 +245,11 @@ fn solve(
problems: &mut Vec<TypeError>,
cached_aliases: &mut MutMap<Symbol, Variable>,
subs: &mut Subs,
constraint: &Constraint,
constraint: &roc_can::constraint::Constraint,
) -> State {
use roc_can::constraint::PresenceConstraint;
use Constraint::*;
let mut stack = vec![Work::Constraint {
env,
rank,
@ -745,6 +757,555 @@ fn solve(
state
}
#[allow(clippy::too_many_arguments)]
fn solve_soa(
arena: &Bump,
constraints: &roc_can::constraint_soa::Constraints,
env: &Env,
mut state: State,
rank: Rank,
pools: &mut Pools,
problems: &mut Vec<TypeError>,
cached_aliases: &mut MutMap<Symbol, Variable>,
subs: &mut Subs,
constraint: &roc_can::constraint_soa::Constraint,
) -> State {
use roc_can::constraint_soa::Constraint::*;
let initial = WorkSoa::Constraint {
env,
rank,
constraint,
};
let mut stack = vec![initial];
while let Some(work_item) = stack.pop() {
let (env, rank, constraint) = match work_item {
WorkSoa::After(After::CheckForInfiniteTypes(def_vars)) => {
for (symbol, loc_var) in def_vars.iter() {
check_for_infinite_type(subs, problems, *symbol, *loc_var);
}
// No constraint to be solved
continue;
}
WorkSoa::Constraint {
env,
rank,
constraint,
} => (env, rank, constraint),
};
state = match constraint {
True => state,
SaveTheEnvironment => {
// NOTE deviation: elm only copies the env into the state on SaveTheEnvironment
let mut copy = state;
copy.env = env.clone();
copy
}
Eq(type_index, expectation_index, category_index, region) => {
let typ = &constraints.types[type_index.index()];
let expectation = &constraints.expectations[expectation_index.index()];
let category = &constraints.categories[category_index.index()];
let actual = type_to_var(subs, rank, pools, cached_aliases, typ);
let expected = type_to_var(
subs,
rank,
pools,
cached_aliases,
expectation.get_type_ref(),
);
match unify(subs, actual, expected, Mode::EQ) {
Success(vars) => {
introduce(subs, rank, pools, &vars);
state
}
Failure(vars, actual_type, expected_type) => {
introduce(subs, rank, pools, &vars);
let problem = TypeError::BadExpr(
*region,
category.clone(),
actual_type,
expectation.clone().replace(expected_type),
);
problems.push(problem);
state
}
BadType(vars, problem) => {
introduce(subs, rank, pools, &vars);
problems.push(TypeError::BadType(problem));
state
}
}
}
Store(source_index, target, _filename, _linenr) => {
let source = &constraints.types[source_index.index()];
// a special version of Eq that is used to store types in the AST.
// IT DOES NOT REPORT ERRORS!
let actual = type_to_var(subs, rank, pools, cached_aliases, source);
let target = *target;
match unify(subs, actual, target, Mode::EQ) {
Success(vars) => {
introduce(subs, rank, pools, &vars);
state
}
Failure(vars, _actual_type, _expected_type) => {
introduce(subs, rank, pools, &vars);
// ERROR NOT REPORTED
state
}
BadType(vars, _) => {
introduce(subs, rank, pools, &vars);
// ERROR NOT REPORTED
state
}
}
}
Lookup(symbol, expectation_index, region) => {
match env.get_var_by_symbol(symbol) {
Some(var) => {
// Deep copy the vars associated with this symbol before unifying them.
// Otherwise, suppose we have this:
//
// identity = \a -> a
//
// x = identity 5
//
// When we call (identity 5), it's important that we not unify
// on identity's original vars. If we do, the type of `identity` will be
// mutated to be `Int -> Int` instead of `a -> `, which would be incorrect;
// the type of `identity` is more general than that!
//
// Instead, we want to unify on a *copy* of its vars. If the copy unifies
// successfully (in this case, to `Int -> Int`), we can use that to
// infer the type of this lookup (in this case, `Int`) without ever
// having mutated the original.
//
// If this Lookup is targeting a value in another module,
// then we copy from that module's Subs into our own. If the value
// is being looked up in this module, then we use our Subs as both
// the source and destination.
let actual = deep_copy_var(subs, rank, pools, var);
let expectation = &constraints.expectations[expectation_index.index()];
let expected = type_to_var(
subs,
rank,
pools,
cached_aliases,
expectation.get_type_ref(),
);
match unify(subs, actual, expected, Mode::EQ) {
Success(vars) => {
introduce(subs, rank, pools, &vars);
state
}
Failure(vars, actual_type, expected_type) => {
introduce(subs, rank, pools, &vars);
let problem = TypeError::BadExpr(
*region,
Category::Lookup(*symbol),
actual_type,
expectation.clone().replace(expected_type),
);
problems.push(problem);
state
}
BadType(vars, problem) => {
introduce(subs, rank, pools, &vars);
problems.push(TypeError::BadType(problem));
state
}
}
}
None => {
problems.push(TypeError::UnexposedLookup(*symbol));
state
}
}
}
And(slice) => {
let it = constraints.constraints[slice.indices()].iter().rev();
for sub_constraint in it {
stack.push(WorkSoa::Constraint {
env,
rank,
constraint: sub_constraint,
})
}
state
}
Pattern(type_index, expectation_index, category_index, region)
| PatternPresence(type_index, expectation_index, category_index, region) => {
let typ = &constraints.types[type_index.index()];
let expectation = &constraints.pattern_expectations[expectation_index.index()];
let category = &constraints.pattern_categories[category_index.index()];
let actual = type_to_var(subs, rank, pools, cached_aliases, typ);
let expected = type_to_var(
subs,
rank,
pools,
cached_aliases,
expectation.get_type_ref(),
);
let mode = match constraint {
PatternPresence(..) => Mode::PRESENT,
_ => Mode::EQ,
};
match unify(subs, actual, expected, mode) {
Success(vars) => {
introduce(subs, rank, pools, &vars);
state
}
Failure(vars, actual_type, expected_type) => {
introduce(subs, rank, pools, &vars);
let problem = TypeError::BadPattern(
*region,
category.clone(),
actual_type,
expectation.clone().replace(expected_type),
);
problems.push(problem);
state
}
BadType(vars, problem) => {
introduce(subs, rank, pools, &vars);
problems.push(TypeError::BadType(problem));
state
}
}
}
Let(index) => {
let let_con = &constraints.let_constraints[index.index()];
let offset = let_con.defs_and_ret_constraint.index();
let defs_constraint = &constraints.constraints[offset];
let ret_constraint = &constraints.constraints[offset + 1];
let flex_vars = &constraints.variables[let_con.flex_vars.indices()];
let rigid_vars = &constraints.variables[let_con.rigid_vars.indices()];
let def_types = &constraints.def_types[let_con.def_types.indices()];
match &ret_constraint {
True if let_con.rigid_vars.is_empty() => {
introduce(subs, rank, pools, flex_vars);
// If the return expression is guaranteed to solve,
// solve the assignments themselves and move on.
stack.push(WorkSoa::Constraint {
env,
rank,
constraint: defs_constraint,
});
state
}
ret_con if let_con.rigid_vars.is_empty() && let_con.flex_vars.is_empty() => {
// TODO: make into `WorkItem` with `After`
let state = solve_soa(
arena,
constraints,
env,
state,
rank,
pools,
problems,
cached_aliases,
subs,
defs_constraint,
);
// Add a variable for each def to new_vars_by_env.
let mut local_def_vars =
LocalDefVarsVec::with_length(let_con.def_types.len());
for (symbol, loc_type_index) in def_types.iter() {
let typ = &constraints.types[loc_type_index.value.index()];
let var = type_to_var(subs, rank, pools, cached_aliases, typ);
local_def_vars.push((
*symbol,
Loc {
value: var,
region: loc_type_index.region,
},
));
}
let mut new_env = env.clone();
for (symbol, loc_var) in local_def_vars.iter() {
new_env.insert_symbol_var_if_vacant(*symbol, loc_var.value);
}
stack.push(WorkSoa::After(After::CheckForInfiniteTypes(local_def_vars)));
stack.push(WorkSoa::Constraint {
env: arena.alloc(new_env),
rank,
constraint: ret_con,
});
state
}
ret_con => {
// work in the next pool to localize header
let next_rank = rank.next();
// introduce variables
for &var in rigid_vars.iter().chain(flex_vars.iter()) {
subs.set_rank(var, next_rank);
}
// determine the next pool
if next_rank.into_usize() < pools.len() {
// Nothing to do, we already accounted for the next rank, no need to
// adjust the 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());
}
let pool: &mut Vec<Variable> = pools.get_mut(next_rank);
// 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());
// run solver in next pool
// Add a variable for each def to local_def_vars.
let mut local_def_vars =
LocalDefVarsVec::with_length(let_con.def_types.len());
for (symbol, loc_type) in def_types.iter() {
let def_type = &constraints.types[loc_type.value.index()];
let var = type_to_var(subs, next_rank, pools, cached_aliases, def_type);
local_def_vars.push((
*symbol,
Loc {
value: var,
region: loc_type.region,
},
));
}
// Solve the assignments' constraints first.
// TODO: make into `WorkItem` with `After`
let State {
env: saved_env,
mark,
} = solve_soa(
arena,
constraints,
env,
state,
next_rank,
pools,
problems,
cached_aliases,
subs,
defs_constraint,
);
let young_mark = mark;
let visit_mark = young_mark.next();
let final_mark = visit_mark.next();
debug_assert_eq!(
{
let offenders = pools
.get(next_rank)
.iter()
.filter(|var| {
let current_rank =
subs.get_rank(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);
}
result
},
0
);
// pop pool
generalize(subs, young_mark, visit_mark, next_rank, pools);
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_rank(*var) != Rank::NONE
})
.collect();
if !failing.is_empty() {
println!("Rigids {:?}", &rigid_vars);
println!("Failing {:?}", failing);
}
failing.is_empty()
});
let mut new_env = env.clone();
for (symbol, loc_var) in local_def_vars.iter() {
new_env.insert_symbol_var_if_vacant(*symbol, loc_var.value);
}
// Note that this vars_by_symbol is the one returned by the
// previous call to solve()
let state_for_ret_con = State {
env: saved_env,
mark: final_mark,
};
// Now solve the body, using the new vars_by_symbol which includes
// the assignments' name-to-variable mappings.
stack.push(WorkSoa::After(After::CheckForInfiniteTypes(local_def_vars)));
stack.push(WorkSoa::Constraint {
env: arena.alloc(new_env),
rank,
constraint: ret_con,
});
state_for_ret_con
}
}
}
IsOpenType(type_index) => {
let typ = &constraints.types[type_index.index()];
let actual = type_to_var(subs, rank, pools, cached_aliases, typ);
let mut new_desc = subs.get(actual);
match new_desc.content {
Content::Structure(FlatType::TagUnion(tags, _)) => {
let new_ext = subs.fresh_unnamed_flex_var();
let new_union = Content::Structure(FlatType::TagUnion(tags, new_ext));
new_desc.content = new_union;
subs.set(actual, new_desc);
state
}
_ => {
// Today, an "open" constraint doesn't affect any types
// other than tag unions. Recursive tag unions are constructed
// at a later time (during occurs checks after tag unions are
// resolved), so that's not handled here either.
// NB: Handle record types here if we add presence constraints
// to their type inference as well.
state
}
}
}
IncludesTag(index) => {
let includes_tag = &constraints.includes_tags[index.index()];
let roc_can::constraint_soa::IncludesTag {
type_index,
tag_name,
types,
pattern_category,
region,
} = includes_tag;
let typ = &constraints.types[type_index.index()];
let tys = &constraints.types[types.indices()];
let pattern_category = &constraints.pattern_categories[pattern_category.index()];
let actual = type_to_var(subs, rank, pools, cached_aliases, typ);
let tag_ty = Type::TagUnion(
vec![(tag_name.clone(), tys.to_vec())],
Box::new(Type::EmptyTagUnion),
);
let includes = type_to_var(subs, rank, pools, cached_aliases, &tag_ty);
match unify(subs, actual, includes, Mode::PRESENT) {
Success(vars) => {
introduce(subs, rank, pools, &vars);
state
}
Failure(vars, actual_type, expected_to_include_type) => {
introduce(subs, rank, pools, &vars);
let problem = TypeError::BadPattern(
*region,
pattern_category.clone(),
expected_to_include_type,
PExpected::NoExpectation(actual_type),
);
problems.push(problem);
state
}
BadType(vars, problem) => {
introduce(subs, rank, pools, &vars);
problems.push(TypeError::BadType(problem));
state
}
}
}
};
}
state
}
#[derive(Debug)]
enum LocalDefVarsVec<T> {
Stack(arrayvec::ArrayVec<T, 32>),