mirror of
https://github.com/roc-lang/roc.git
synced 2024-11-13 09:49:11 +03:00
refactor solve_module
This commit is contained in:
parent
de8ad07f2e
commit
ed592d3d10
@ -2,6 +2,7 @@ use crate::solve;
|
||||
use roc_can::constraint::Constraint;
|
||||
use roc_can::module::Module;
|
||||
use roc_collections::all::{MutMap, SendMap};
|
||||
use roc_module::ident::Lowercase;
|
||||
use roc_module::symbol::Symbol;
|
||||
use roc_types::solved_types::{Solved, SolvedType};
|
||||
use roc_types::subs::{Subs, VarStore, Variable};
|
||||
@ -20,15 +21,42 @@ pub fn solve_module(
|
||||
constraint: Constraint,
|
||||
var_store: VarStore,
|
||||
) -> (Solved<Subs>, SolvedModule) {
|
||||
let exposed_vars_by_symbol: Vec<(Symbol, Variable)> = module.exposed_vars_by_symbol;
|
||||
let Module {
|
||||
exposed_vars_by_symbol,
|
||||
aliases,
|
||||
rigid_variables,
|
||||
..
|
||||
} = module;
|
||||
|
||||
let (solved_subs, solved_env, problems) =
|
||||
run_solve(aliases, rigid_variables, constraint, var_store);
|
||||
|
||||
let solved_types = make_solved_types(&solved_env, &solved_subs, &exposed_vars_by_symbol);
|
||||
|
||||
let solved_module = SolvedModule {
|
||||
exposed_vars_by_symbol,
|
||||
solved_types,
|
||||
problems,
|
||||
aliases: solved_env.aliases,
|
||||
};
|
||||
|
||||
(solved_subs, solved_module)
|
||||
}
|
||||
|
||||
pub fn run_solve(
|
||||
aliases: MutMap<Symbol, 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: SendMap::default(),
|
||||
aliases: module.aliases,
|
||||
aliases,
|
||||
};
|
||||
|
||||
let mut subs = Subs::new(var_store.into());
|
||||
|
||||
for (var, name) in module.rigid_variables {
|
||||
for (var, name) in rigid_variables {
|
||||
subs.rigid_var(var, name);
|
||||
}
|
||||
|
||||
@ -38,21 +66,30 @@ pub fn solve_module(
|
||||
|
||||
// Run the solver to populate Subs.
|
||||
let (solved_subs, solved_env) = solve::run(&env, &mut problems, subs, &constraint);
|
||||
|
||||
(solved_subs, solved_env, problems)
|
||||
}
|
||||
|
||||
fn make_solved_types(
|
||||
solved_env: &solve::Env,
|
||||
solved_subs: &Solved<Subs>,
|
||||
exposed_vars_by_symbol: &[(Symbol, Variable)],
|
||||
) -> MutMap<Symbol, SolvedType> {
|
||||
let mut solved_types = MutMap::default();
|
||||
|
||||
for (symbol, alias) in solved_env.aliases {
|
||||
for (symbol, alias) in solved_env.aliases.iter() {
|
||||
let mut args = Vec::with_capacity(alias.vars.len());
|
||||
|
||||
for loc_named_var in alias.vars {
|
||||
let (name, var) = loc_named_var.value;
|
||||
for loc_named_var in alias.vars.iter() {
|
||||
let (name, var) = &loc_named_var.value;
|
||||
|
||||
args.push((name.clone(), SolvedType::new(&solved_subs, var)));
|
||||
args.push((name.clone(), SolvedType::new(&solved_subs, *var)));
|
||||
}
|
||||
|
||||
let solved_type = SolvedType::from_type(&solved_subs, alias.typ);
|
||||
let solved_alias = SolvedType::Alias(symbol, args, Box::new(solved_type));
|
||||
let solved_type = SolvedType::from_type(&solved_subs, &alias.typ);
|
||||
let solved_alias = SolvedType::Alias(*symbol, args, Box::new(solved_type));
|
||||
|
||||
solved_types.insert(symbol, solved_alias);
|
||||
solved_types.insert(*symbol, solved_alias);
|
||||
}
|
||||
|
||||
// exposed_vars_by_symbol contains the Variables for all the Symbols
|
||||
@ -66,12 +103,5 @@ pub fn solve_module(
|
||||
solved_types.insert(*symbol, solved_type);
|
||||
}
|
||||
|
||||
let solved_module = SolvedModule {
|
||||
exposed_vars_by_symbol,
|
||||
solved_types,
|
||||
problems,
|
||||
aliases: env.aliases,
|
||||
};
|
||||
|
||||
(solved_subs, solved_module)
|
||||
solved_types
|
||||
}
|
||||
|
@ -98,7 +98,7 @@ impl SolvedType {
|
||||
Self::from_var(solved_subs.inner(), var)
|
||||
}
|
||||
|
||||
pub fn from_type(solved_subs: &Solved<Subs>, typ: Type) -> Self {
|
||||
pub fn from_type(solved_subs: &Solved<Subs>, typ: &Type) -> Self {
|
||||
use crate::types::Type::*;
|
||||
|
||||
match typ {
|
||||
@ -113,11 +113,11 @@ impl SolvedType {
|
||||
solved_types.push(solved_type);
|
||||
}
|
||||
|
||||
SolvedType::Apply(symbol, solved_types)
|
||||
SolvedType::Apply(*symbol, solved_types)
|
||||
}
|
||||
Function(args, box_closure, box_ret) => {
|
||||
let solved_ret = Self::from_type(solved_subs, *box_ret);
|
||||
let solved_closure = Self::from_type(solved_subs, *box_closure);
|
||||
let solved_ret = Self::from_type(solved_subs, box_ret);
|
||||
let solved_closure = Self::from_type(solved_subs, box_closure);
|
||||
let mut solved_args = Vec::with_capacity(args.len());
|
||||
|
||||
for arg in args.into_iter() {
|
||||
@ -129,7 +129,7 @@ impl SolvedType {
|
||||
SolvedType::Func(solved_args, Box::new(solved_closure), Box::new(solved_ret))
|
||||
}
|
||||
Record(fields, box_ext) => {
|
||||
let solved_ext = Self::from_type(solved_subs, *box_ext);
|
||||
let solved_ext = Self::from_type(solved_subs, box_ext);
|
||||
let mut solved_fields = Vec::with_capacity(fields.len());
|
||||
|
||||
for (label, field) in fields {
|
||||
@ -150,7 +150,7 @@ impl SolvedType {
|
||||
}
|
||||
}
|
||||
TagUnion(tags, box_ext) => {
|
||||
let solved_ext = Self::from_type(solved_subs, *box_ext);
|
||||
let solved_ext = Self::from_type(solved_subs, box_ext);
|
||||
let mut solved_tags = Vec::with_capacity(tags.len());
|
||||
for (tag_name, types) in tags {
|
||||
let mut solved_types = Vec::with_capacity(types.len());
|
||||
@ -166,7 +166,7 @@ impl SolvedType {
|
||||
SolvedType::TagUnion(solved_tags, Box::new(solved_ext))
|
||||
}
|
||||
RecursiveTagUnion(rec_var, tags, box_ext) => {
|
||||
let solved_ext = Self::from_type(solved_subs, *box_ext);
|
||||
let solved_ext = Self::from_type(solved_subs, box_ext);
|
||||
let mut solved_tags = Vec::with_capacity(tags.len());
|
||||
for (tag_name, types) in tags {
|
||||
let mut solved_types = Vec::with_capacity(types.len());
|
||||
@ -180,24 +180,24 @@ impl SolvedType {
|
||||
}
|
||||
|
||||
SolvedType::RecursiveTagUnion(
|
||||
VarId::from_var(rec_var, solved_subs.inner()),
|
||||
VarId::from_var(*rec_var, solved_subs.inner()),
|
||||
solved_tags,
|
||||
Box::new(solved_ext),
|
||||
)
|
||||
}
|
||||
Erroneous(problem) => SolvedType::Erroneous(problem),
|
||||
Erroneous(problem) => SolvedType::Erroneous(problem.clone()),
|
||||
Alias(symbol, args, box_type) => {
|
||||
let solved_type = Self::from_type(solved_subs, *box_type);
|
||||
let solved_type = Self::from_type(solved_subs, box_type);
|
||||
let mut solved_args = Vec::with_capacity(args.len());
|
||||
|
||||
for (name, var) in args {
|
||||
solved_args.push((name.clone(), Self::from_type(solved_subs, var)));
|
||||
}
|
||||
|
||||
SolvedType::Alias(symbol, solved_args, Box::new(solved_type))
|
||||
SolvedType::Alias(*symbol, solved_args, Box::new(solved_type))
|
||||
}
|
||||
Boolean(val) => SolvedType::Boolean(SolvedBool::from_bool(&val, solved_subs.inner())),
|
||||
Variable(var) => Self::from_var(solved_subs.inner(), var),
|
||||
Variable(var) => Self::from_var(solved_subs.inner(), *var),
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user