Admit duplicate lambdas in lambda sets when their captures don't unify

This commit is contained in:
Ayaz Hafiz 2022-06-27 12:51:54 -04:00 committed by ayazhafiz
parent b2c094ca07
commit cecb6987e7
No known key found for this signature in database
GPG key ID: B443F7A3030C9AED
4 changed files with 154 additions and 82 deletions

View file

@ -6953,6 +6953,68 @@ mod solve_expr {
"# "#
), ),
"Str", "Str",
)
}
#[test]
fn lambda_sets_collide_with_captured_var() {
infer_queries!(
indoc!(
r#"
capture : a -> ({} -> Str)
capture = \val ->
thunk =
\{} ->
when val is
_ -> ""
thunk
x : [True, False]
fun =
when x is
True -> capture ""
False -> capture {}
fun
#^^^{-1}
"#
),
&["fun : {} -[[thunk(5) {}, thunk(5) Str]]-> Str"],
);
}
#[test]
fn lambda_sets_collide_with_captured_function() {
infer_queries!(
indoc!(
r#"
Lazy a : {} -> a
after : Lazy a, (a -> Lazy b) -> Lazy b
after = \effect, map ->
thunk = \{} ->
when map (effect {}) is
b -> b {}
thunk
f = \_ -> \_ -> ""
g = \{ s1 } -> \_ -> s1
x : [True, False]
fun =
when x is
True -> after (\{} -> "") f
False -> after (\{} -> {s1: "s1"}) g
fun
#^^^{-1}
"#
),
&[
"fun : {} -[[thunk(9) (({} -[[15(15)]]-> { s1 : Str })) ({ s1 : Str } -[[g(4)]]-> ({} -[[13(13) Str]]-> Str)), \
thunk(9) (({} -[[14(14)]]-> Str)) (Str -[[f(3)]]-> ({} -[[11(11)]]-> Str))]]-> Str",
],
print_only_under_alias = true,
); );
} }
} }

View file

@ -1872,6 +1872,10 @@ impl Subs {
self.utable.unioned(left, right) self.utable.unioned(left, right)
} }
pub fn equivalent_without_compacting(&self, left: Variable, right: Variable) -> bool {
self.utable.unioned_without_compacting(left, right)
}
pub fn redundant(&self, var: Variable) -> bool { pub fn redundant(&self, var: Variable) -> bool {
self.utable.is_redirect(var) self.utable.is_redirect(var)
} }

View file

@ -323,6 +323,10 @@ impl UnificationTable {
self.root_key(a) == self.root_key(b) self.root_key(a) == self.root_key(b)
} }
pub fn unioned_without_compacting(&self, a: Variable, b: Variable) -> bool {
self.root_key_without_compacting(a) == self.root_key_without_compacting(b)
}
// custom very specific helpers // custom very specific helpers
#[inline(always)] #[inline(always)]
pub fn get_rank_set_mark(&mut self, key: Variable, mark: Mark) -> Rank { pub fn get_rank_set_mark(&mut self, key: Variable, mark: Mark) -> Rank {

View file

@ -1000,103 +1000,105 @@ fn unify_lambda_set_help<M: MetaCollector>(
in_both, in_both,
} = separate_union_lambdas(subs, solved1, solved2); } = separate_union_lambdas(subs, solved1, solved2);
let num_shared = in_both.len(); let mut new_lambdas = vec![];
for (lambda_name, (vars1, vars2)) in in_both {
let mut captures_unify = vars1.len() == vars2.len();
let mut joined_lambdas = vec![]; if captures_unify {
for (tag_name, (vars1, vars2)) in in_both { for (var1, var2) in (vars1.into_iter()).zip(vars2.into_iter()) {
let mut matching_vars = vec![]; let (var1, var2) = (subs[var1], subs[var2]);
if vars1.len() != vars2.len() { // Lambda sets are effectively tags under another name, and their usage can also result
continue; // this is a type mismatch; not adding the tag will trigger it below. // in the arguments of a lambda name being recursive. It very well may happen that
} // during unification, a lambda set previously marked as not recursive becomes
// recursive. See the docs of [LambdaSet] for one example, or https://github.com/rtfeldman/roc/pull/2307.
//
// Like with tag unions, if it has, we'll always pass through this branch. So, take
// this opportunity to promote the lambda set to recursive if need be.
maybe_mark_union_recursive(subs, var1);
maybe_mark_union_recursive(subs, var2);
let num_vars = vars1.len(); let snapshot = subs.snapshot();
for (var1, var2) in (vars1.into_iter()).zip(vars2.into_iter()) { let outcome = unify_pool::<M>(subs, pool, var1, var2, ctx.mode);
let (var1, var2) = (subs[var1], subs[var2]);
// Lambda sets are effectively tags under another name, and their usage can also result if !outcome.mismatches.is_empty() {
// in the arguments of a lambda name being recursive. It very well may happen that captures_unify = false;
// during unification, a lambda set previously marked as not recursive becomes subs.rollback_to(snapshot);
// recursive. See the docs of [LambdaSet] for one example, or https://github.com/rtfeldman/roc/pull/2307. // Continue so the other variables can unify if possible, allowing us to re-use
// // shared variables.
// Like with tag unions, if it has, we'll always pass through this branch. So, take }
// this opportunity to promote the lambda set to recursive if need be.
maybe_mark_union_recursive(subs, var1);
maybe_mark_union_recursive(subs, var2);
let outcome = unify_pool::<M>(subs, pool, var1, var2, ctx.mode);
if outcome.mismatches.is_empty() {
matching_vars.push(var1);
} }
} }
if matching_vars.len() == num_vars { if captures_unify {
joined_lambdas.push((tag_name, matching_vars)); debug_assert!((subs.get_subs_slice(vars1).iter())
.zip(subs.get_subs_slice(vars2).iter())
.all(|(v1, v2)| subs.equivalent_without_compacting(*v1, *v2)));
new_lambdas.push((lambda_name, subs.get_subs_slice(vars1).to_vec()));
} else {
debug_assert!((subs.get_subs_slice(vars1).iter())
.zip(subs.get_subs_slice(vars2).iter())
.any(|(v1, v2)| !subs.equivalent_without_compacting(*v1, *v2)));
new_lambdas.push((lambda_name, subs.get_subs_slice(vars1).to_vec()));
new_lambdas.push((lambda_name, subs.get_subs_slice(vars2).to_vec()));
} }
} }
if joined_lambdas.len() == num_shared { let all_lambdas = new_lambdas;
let all_lambdas = joined_lambdas; let all_lambdas = merge_sorted(
let all_lambdas = merge_sorted( all_lambdas,
all_lambdas, only_in_1.into_iter().map(|(name, subs_slice)| {
only_in_1.into_iter().map(|(name, subs_slice)| { let vec = subs.get_subs_slice(subs_slice).to_vec();
let vec = subs.get_subs_slice(subs_slice).to_vec(); (name, vec)
(name, vec) }),
}), );
); let all_lambdas = merge_sorted(
let all_lambdas = merge_sorted( all_lambdas,
all_lambdas, only_in_2.into_iter().map(|(name, subs_slice)| {
only_in_2.into_iter().map(|(name, subs_slice)| { let vec = subs.get_subs_slice(subs_slice).to_vec();
let vec = subs.get_subs_slice(subs_slice).to_vec(); (name, vec)
(name, vec) }),
}), );
);
let recursion_var = match (rec1.into_variable(), rec2.into_variable()) { let recursion_var = match (rec1.into_variable(), rec2.into_variable()) {
// Prefer left when it's available. // Prefer left when it's available.
(Some(rec), _) | (_, Some(rec)) => OptVariable::from(rec), (Some(rec), _) | (_, Some(rec)) => OptVariable::from(rec),
(None, None) => OptVariable::NONE, (None, None) => OptVariable::NONE,
}; };
// Combine the unspecialized lambda sets as needed. Note that we don't need to update the // Combine the unspecialized lambda sets as needed. Note that we don't need to update the
// bookkeeping of variable -> lambda set to be resolved, because if we had v1 -> lset1, and // bookkeeping of variable -> lambda set to be resolved, because if we had v1 -> lset1, and
// now lset1 ~ lset2, then afterward either lset1 still resolves to itself or re-points to // now lset1 ~ lset2, then afterward either lset1 still resolves to itself or re-points to
// lset2. In either case the merged unspecialized lambda sets will be there. // lset2. In either case the merged unspecialized lambda sets will be there.
let merged_unspecialized = match (uls1.is_empty(), uls2.is_empty()) { let merged_unspecialized = match (uls1.is_empty(), uls2.is_empty()) {
(true, true) => SubsSlice::default(), (true, true) => SubsSlice::default(),
(false, true) => uls1, (false, true) => uls1,
(true, false) => uls2, (true, false) => uls2,
(false, false) => { (false, false) => {
let mut all_uls = (subs.get_subs_slice(uls1).iter()) let mut all_uls = (subs.get_subs_slice(uls1).iter())
.chain(subs.get_subs_slice(uls2)) .chain(subs.get_subs_slice(uls2))
.map(|&Uls(var, sym, region)| { .map(|&Uls(var, sym, region)| {
// Take the root key to deduplicate // Take the root key to deduplicate
Uls(subs.get_root_key_without_compacting(var), sym, region) Uls(subs.get_root_key_without_compacting(var), sym, region)
}) })
.collect::<Vec<_>>(); .collect::<Vec<_>>();
all_uls.sort(); all_uls.sort();
all_uls.dedup(); all_uls.dedup();
SubsSlice::extend_new(&mut subs.unspecialized_lambda_sets, all_uls) SubsSlice::extend_new(&mut subs.unspecialized_lambda_sets, all_uls)
} }
}; };
let new_solved = UnionLabels::insert_into_subs(subs, all_lambdas); let new_solved = UnionLabels::insert_into_subs(subs, all_lambdas);
let new_lambda_set = Content::LambdaSet(LambdaSet { let new_lambda_set = Content::LambdaSet(LambdaSet {
solved: new_solved, solved: new_solved,
recursion_var, recursion_var,
unspecialized: merged_unspecialized, unspecialized: merged_unspecialized,
}); });
merge(subs, ctx, new_lambda_set) merge(subs, ctx, new_lambda_set)
} else {
mismatch!(
"Problem with lambda sets: there should be {:?} matching lambda, but only found {:?}",
num_shared,
&joined_lambdas
)
}
} }
/// Ensures that a non-recursive tag union, when unified with a recursion var to become a recursive /// Ensures that a non-recursive tag union, when unified with a recursion var to become a recursive