Make defs use "let rec" style; drop Variable

This commit is contained in:
Richard Feldman 2019-12-22 10:44:29 -08:00
parent 760de2f328
commit 302b03cb20
6 changed files with 32 additions and 33 deletions

View file

@ -918,37 +918,32 @@ pub fn can_defs_with_return<'a>(
output.rigids = output.rigids.union(found_rigids); output.rigids = output.rigids.union(found_rigids);
// Rigid constraint for the def expr as a whole // Rigid constraint for the def expr as a whole.
// This is a "LetRec" constraint; it supports recursion.
// (The only advantage of "Let" over "LetRec" is if you want to
// shadow things, and Roc disallows shadowing anyway.)
let constraint = Let(Box::new(LetConstraint { let constraint = Let(Box::new(LetConstraint {
rigid_vars: rigid_info.vars, rigid_vars: rigid_info.vars,
flex_vars: Vec::new(), flex_vars: Vec::new(),
def_types: rigid_info.def_types, def_types: rigid_info.def_types,
defs_constraint: defs_constraint: True,
// Flex constraint ret_constraint: Let(Box::new(LetConstraint {
Let(Box::new(LetConstraint { rigid_vars: Vec::new(),
flex_vars: flex_info.vars,
def_types: flex_info.def_types.clone(),
defs_constraint: Let(Box::new(LetConstraint {
flex_vars: Vec::new(),
rigid_vars: Vec::new(), rigid_vars: Vec::new(),
flex_vars: flex_info.vars, def_types: flex_info.def_types,
def_types: flex_info.def_types.clone(), defs_constraint: True,
defs_constraint: ret_constraint: And(flex_info.constraints),
// Final flex constraints
Let(Box::new(LetConstraint {
rigid_vars: Vec::new(),
flex_vars: Vec::new(),
def_types: flex_info.def_types,
defs_constraint: True,
ret_constraint: And(flex_info.constraints)
})),
ret_constraint: And(vec![And(rigid_info.constraints), ret_con])
})), })),
ret_constraint: True, ret_constraint: And(vec![And(rigid_info.constraints), ret_con]),
})),
})); }));
match can_defs { match can_defs {
Ok(defs) => ( Ok(defs) => (Defs(defs, Box::new(ret_expr)), output, constraint),
Defs(var_store.fresh(), defs, Box::new(ret_expr)),
output,
constraint,
),
Err(err) => (RuntimeError(err), output, constraint), Err(err) => (RuntimeError(err), output, constraint),
} }
} }

View file

@ -74,7 +74,7 @@ pub enum Expr {
Box<Located<Expr>>, Box<Located<Expr>>,
Vec<(Located<Pattern>, Located<Expr>)>, Vec<(Located<Pattern>, Located<Expr>)>,
), ),
Defs(Variable, Vec<Def>, Box<Located<Expr>>), Defs(Vec<Def>, Box<Located<Expr>>),
/// This is *only* for calling functions, not for tag application. /// This is *only* for calling functions, not for tag application.
/// The Tag variant contains any applied values inside it. /// The Tag variant contains any applied values inside it.

View file

@ -565,7 +565,7 @@ fn adjust_rank(
} else if mark == visit_mark { } else if mark == visit_mark {
desc.rank desc.rank
} else { } else {
let min_rank = desc.rank.min(group_rank); let min_rank = group_rank.min(desc.rank);
// TODO from elm-compiler: how can min_rank ever be group_rank? // TODO from elm-compiler: how can min_rank ever be group_rank?
desc.rank = min_rank; desc.rank = min_rank;

View file

@ -45,6 +45,8 @@ impl fmt::Debug for Type {
match self { match self {
Type::EmptyRec => write!(f, "{{}}"), Type::EmptyRec => write!(f, "{{}}"),
Type::Function(args, ret) => { Type::Function(args, ret) => {
write!(f, "Fn(")?;
for (index, arg) in args.iter().enumerate() { for (index, arg) in args.iter().enumerate() {
if index > 0 { if index > 0 {
", ".fmt(f)?; ", ".fmt(f)?;
@ -55,7 +57,9 @@ impl fmt::Debug for Type {
write!(f, " -> ")?; write!(f, " -> ")?;
ret.fmt(f) ret.fmt(f)?;
write!(f, ")")
} }
Type::Variable(var) => write!(f, "<{:?}>", var), Type::Variable(var) => write!(f, "<{:?}>", var),

View file

@ -385,7 +385,7 @@ pub fn canonicalize_expr(
) )
} }
Defs(_, defs, loc_ret) => { Defs(defs, loc_ret) => {
// The body expression gets a new scope for canonicalization, // The body expression gets a new scope for canonicalization,
// so clone it. // so clone it.

View file

@ -232,7 +232,7 @@ mod test_canonicalize {
fn get_closure(expr: &Expr, i: usize) -> roc::can::expr::Recursive { fn get_closure(expr: &Expr, i: usize) -> roc::can::expr::Recursive {
match expr { match expr {
Defs(_, assignments, _) => match &assignments.get(i).map(|def| &def.expr.value) { Defs(assignments, _) => match &assignments.get(i).map(|def| &def.expr.value) {
Some(Closure(_, recursion, _, _)) => recursion.clone(), Some(Closure(_, recursion, _, _)) => recursion.clone(),
Some(other @ _) => { Some(other @ _) => {
panic!("assignment at {} is not a closure, but a {:?}", i, other) panic!("assignment at {} is not a closure, but a {:?}", i, other)