mirror of
https://github.com/roc-lang/roc.git
synced 2025-10-03 08:34:33 +00:00
wip2
This commit is contained in:
parent
04d3f68192
commit
3fc3059915
7 changed files with 42 additions and 14 deletions
|
@ -1166,7 +1166,7 @@ fn canonicalize_pattern(
|
||||||
|
|
||||||
// This is a fresh identifier that wasn't already in scope.
|
// This is a fresh identifier that wasn't already in scope.
|
||||||
// Add it to scope!
|
// Add it to scope!
|
||||||
let symbol_and_region = (symbol.clone(), region);
|
let symbol_and_region = (symbol.clone(), region.clone());
|
||||||
|
|
||||||
// Add this to both scope.idents *and* shadowable_idents.
|
// Add this to both scope.idents *and* shadowable_idents.
|
||||||
// The latter is relevant when recursively canonicalizing Variant patterns,
|
// The latter is relevant when recursively canonicalizing Variant patterns,
|
||||||
|
|
|
@ -13,7 +13,6 @@ use types::Constraint::{self, *};
|
||||||
/// blah mapping =
|
/// blah mapping =
|
||||||
/// nested : Map k v # <-- the same k and v from the top-level annotation
|
/// nested : Map k v # <-- the same k and v from the top-level annotation
|
||||||
/// nested = mapping
|
/// nested = mapping
|
||||||
///
|
|
||||||
/// 42
|
/// 42
|
||||||
///
|
///
|
||||||
/// In elm/compiler this is called RTV - the "Rigid Type Variables" dictionary.
|
/// In elm/compiler this is called RTV - the "Rigid Type Variables" dictionary.
|
||||||
|
@ -21,6 +20,7 @@ type BoundTypeVars = ImMap<String, Type>;
|
||||||
|
|
||||||
pub fn constrain(
|
pub fn constrain(
|
||||||
bound_vars: BoundTypeVars,
|
bound_vars: BoundTypeVars,
|
||||||
|
subs: &mut Subs,
|
||||||
loc_expr: Located<Expr>,
|
loc_expr: Located<Expr>,
|
||||||
expected: Expected<Type>,
|
expected: Expected<Type>,
|
||||||
) -> Constraint {
|
) -> Constraint {
|
||||||
|
@ -63,7 +63,7 @@ pub fn constrain_def(
|
||||||
flex_vars: state.vars,
|
flex_vars: state.vars,
|
||||||
header: state.headers,
|
header: state.headers,
|
||||||
header_constraint: And(state.reversed_constraints),
|
header_constraint: And(state.reversed_constraints),
|
||||||
body_constraint: constrain(bound_vars, loc_expr, NoExpectation(args.ret_type))
|
body_constraint: constrain(bound_vars, subs, loc_expr, NoExpectation(args.ret_type))
|
||||||
})),
|
})),
|
||||||
body_constraint,
|
body_constraint,
|
||||||
header: panic!("TODO Map.singleton name (A.At region tipe)"),
|
header: panic!("TODO Map.singleton name (A.At region tipe)"),
|
||||||
|
@ -84,7 +84,7 @@ pub fn constrain_procedure(
|
||||||
};
|
};
|
||||||
let args = constrain_args(proc.args.into_iter(), subs, &mut state);
|
let args = constrain_args(proc.args.into_iter(), subs, &mut state);
|
||||||
let body_type = NoExpectation(args.ret_type);
|
let body_type = NoExpectation(args.ret_type);
|
||||||
let body_constraint = constrain(bound_vars, proc.body, body_type);
|
let body_constraint = constrain(bound_vars, subs, proc.body, body_type);
|
||||||
|
|
||||||
state.reversed_constraints.reverse();
|
state.reversed_constraints.reverse();
|
||||||
|
|
||||||
|
|
21
src/infer.rs
Normal file
21
src/infer.rs
Normal file
|
@ -0,0 +1,21 @@
|
||||||
|
use canonicalize::Expr;
|
||||||
|
use region::Located;
|
||||||
|
use subs::{Subs, Content};
|
||||||
|
use types::Expected::*;
|
||||||
|
use types::Type::*;
|
||||||
|
use collections::ImMap;
|
||||||
|
use solve::solve;
|
||||||
|
use constrain::constrain;
|
||||||
|
|
||||||
|
pub fn infer(loc_expr: Located<Expr>) -> Content {
|
||||||
|
let mut subs = Subs::new();
|
||||||
|
let bound_vars = ImMap::default();
|
||||||
|
let variable = subs.mk_flex_var();
|
||||||
|
let expected = NoExpectation(Variable(variable));
|
||||||
|
let constraint = constrain(bound_vars, &mut subs, loc_expr, expected);
|
||||||
|
|
||||||
|
solve(&mut subs, constraint);
|
||||||
|
|
||||||
|
subs.get(variable).content
|
||||||
|
}
|
||||||
|
|
|
@ -15,6 +15,7 @@ pub mod subs;
|
||||||
pub mod constrain;
|
pub mod constrain;
|
||||||
pub mod solve;
|
pub mod solve;
|
||||||
pub mod unify;
|
pub mod unify;
|
||||||
|
pub mod infer;
|
||||||
|
|
||||||
extern crate im_rc;
|
extern crate im_rc;
|
||||||
extern crate fraction;
|
extern crate fraction;
|
||||||
|
|
16
src/solve.rs
16
src/solve.rs
|
@ -16,14 +16,6 @@
|
||||||
use subs::{Subs, Variable};
|
use subs::{Subs, Variable};
|
||||||
use types::Constraint::{self, *};
|
use types::Constraint::{self, *};
|
||||||
use types::Type::{self, *};
|
use types::Type::{self, *};
|
||||||
use unify::unify;
|
|
||||||
|
|
||||||
pub fn type_to_variable(subs: &mut Subs, typ: Type) -> Variable {
|
|
||||||
match typ {
|
|
||||||
Variable(var) => var,
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn solve(subs: &mut Subs, constraint: Constraint) {
|
pub fn solve(subs: &mut Subs, constraint: Constraint) {
|
||||||
match constraint {
|
match constraint {
|
||||||
|
@ -96,3 +88,11 @@ pub fn solve(subs: &mut Subs, constraint: Constraint) {
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn type_to_variable(subs: &mut Subs, typ: Type) -> Variable {
|
||||||
|
match typ {
|
||||||
|
Variable(var) => var,
|
||||||
|
_ => panic!("TODO type_to_var")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
|
@ -26,6 +26,12 @@ impl UnifyKey for Variable {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Subs {
|
impl Subs {
|
||||||
|
pub fn new() -> Self {
|
||||||
|
Subs {
|
||||||
|
utable: UnificationTable::default()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub fn fresh(&mut self, value: Descriptor) -> Variable {
|
pub fn fresh(&mut self, value: Descriptor) -> Variable {
|
||||||
self.utable.new_key(value)
|
self.utable.new_key(value)
|
||||||
}
|
}
|
||||||
|
|
|
@ -116,7 +116,7 @@ mod test_canonicalize {
|
||||||
name: Some("func".to_string()),
|
name: Some("func".to_string()),
|
||||||
is_self_tail_recursive: false,
|
is_self_tail_recursive: false,
|
||||||
definition: empty_region(),
|
definition: empty_region(),
|
||||||
args: vec![Pattern::Identifier(sym("arg"))],
|
args: vec![loc(Pattern::Identifier(sym("arg")))],
|
||||||
body: loc(Expr::Operator(
|
body: loc(Expr::Operator(
|
||||||
loc_box(Expr::Var(sym("arg"))),
|
loc_box(Expr::Var(sym("arg"))),
|
||||||
loc(Operator::Plus),
|
loc(Operator::Plus),
|
||||||
|
@ -401,7 +401,7 @@ mod test_canonicalize {
|
||||||
match expr {
|
match expr {
|
||||||
Assign(assignments, _) => {
|
Assign(assignments, _) => {
|
||||||
assignments.into_iter().map(|(pattern, _)| {
|
assignments.into_iter().map(|(pattern, _)| {
|
||||||
match pattern {
|
match pattern.value {
|
||||||
Identifier(symbol) => {
|
Identifier(symbol) => {
|
||||||
symbol
|
symbol
|
||||||
},
|
},
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue