initial solver for the editor's type constraints

This commit is contained in:
Folkert 2021-04-12 21:26:20 +02:00
parent f16d1619ea
commit bffb9ab6fe
7 changed files with 1625 additions and 4 deletions

1
Cargo.lock generated
View File

@ -3060,6 +3060,7 @@ dependencies = [
"roc_reporting",
"roc_solve",
"roc_types",
"roc_unify",
"ropey",
"serde",
"snafu",

View File

@ -89,4 +89,16 @@ impl<T> Expected<T> {
}
}
}
pub fn replace_ref<U>(&self, new: U) -> Expected<U> {
match self {
Expected::NoExpectation(_val) => Expected::NoExpectation(new),
Expected::ForReason(reason, _val, region) => {
Expected::ForReason(reason.clone(), new, *region)
}
Expected::FromAnnotation(pattern, size, source, _val) => {
Expected::FromAnnotation(pattern.clone(), *size, *source, new)
}
}
}
}

View File

@ -858,7 +858,7 @@ pub enum PReason {
OptionalField,
}
#[derive(Debug, Clone, PartialEq, Eq)]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum AnnotationSource {
TypedIfBranch { index: Index, num_branches: usize },
TypedWhenBranch { index: Index },

View File

@ -15,6 +15,7 @@ roc_region = { path = "../compiler/region" }
roc_module = { path = "../compiler/module" }
roc_problem = { path = "../compiler/problem" }
roc_types = { path = "../compiler/types" }
roc_unify = { path = "../compiler/unify" }
roc_fmt = { path = "../compiler/fmt" }
roc_reporting = { path = "../compiler/reporting" }
roc_solve = { path = "../compiler/solve" }

View File

@ -7,4 +7,5 @@ mod pattern;
pub mod pool;
pub mod roc_file;
pub mod scope;
pub mod solve;
pub mod types;

1575
editor/src/lang/solve.rs Normal file

File diff suppressed because it is too large Load Diff

View File

@ -5,16 +5,22 @@ extern crate indoc;
use bumpalo::Bump;
use roc_can::expected::Expected;
use roc_collections::all::MutMap;
use roc_editor::lang::solve;
use roc_editor::lang::{
constrain::constrain_expr,
constrain::Constraint,
expr::{str_to_expr2, Env},
pool::Pool,
scope::Scope,
types::Type2,
};
use roc_module::ident::{Lowercase, TagName};
use roc_module::symbol::Symbol;
use roc_module::symbol::{IdentIds, ModuleIds};
use roc_region::all::Region;
use roc_solve::module::run_solve;
use roc_types::solved_types::Solved;
use roc_types::subs::{Subs, Variable};
use roc_types::{pretty_print::content_to_string, subs::VarStore, types::Type};
fn ed_constraint_to_can_constraint(
@ -46,6 +52,33 @@ fn type2_to_type(typ: &Type2) -> Type {
}
}
fn run_solve(
aliases: MutMap<Symbol, roc_types::types::Alias>,
rigid_variables: MutMap<Variable, Lowercase>,
constraint: Constraint,
var_store: VarStore,
) -> (Solved<Subs>, solve::Env, Vec<solve::TypeError>) {
let env = solve::Env {
vars_by_symbol: MutMap::default(),
aliases,
};
let mut subs = Subs::new(var_store.into());
for (var, name) in rigid_variables {
subs.rigid_var(var, name);
}
// Now that the module is parsed, canonicalized, and constrained,
// we need to type check it.
let mut problems = Vec::new();
// Run the solver to populate Subs.
let (solved_subs, solved_env) = solve::run(&env, &mut problems, subs, &constraint);
(solved_subs, solved_env, problems)
}
fn infer_eq(actual: &str, expected_str: &str) {
let mut env_pool = Pool::with_capacity(1024);
let env_arena = Bump::new();
@ -83,8 +116,6 @@ fn infer_eq(actual: &str, expected_str: &str) {
Expected::NoExpectation(Type2::Variable(var)),
);
let constraint = ed_constraint_to_can_constraint(constraint);
let (mut solved, _, _) = run_solve(
Default::default(),
Default::default(),