Skip to content

Commit

Permalink
New tag for recursive variables
Browse files Browse the repository at this point in the history
  • Loading branch information
gabriel-barrett committed Jan 6, 2024
1 parent 271be4a commit 3a5c7e7
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 7 deletions.
11 changes: 5 additions & 6 deletions src/lem/eval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -831,8 +831,7 @@ fn reduce(cprocs: &[(&Symbol, usize)]) -> Func {
}
return (expr, smaller_env, not_found, binding)
}
// This is a hack to signal that the binding came from a letrec
Expr::Thunk => {
Expr::RecVar => {
let eq_val = eq_val(var, expr);
if eq_val {
return (val, env, rec, binding)
Expand Down Expand Up @@ -1299,8 +1298,8 @@ fn apply_cont(cprocs: &[(&Symbol, usize)], ivc: bool) -> Func {
}
Cont::LetRec => {
let (var, saved_env, body, cont) = decons4(cont);
// This is a hack to signal that the binding came from a letrec
let var = cast(var, Expr::Thunk);
// Since the variable came from a letrec, it is a recursive variable
let var = cast(var, Expr::RecVar);
let binding: Expr::Cons = cons2(var, result);
let extended_env: Expr::Cons = cons2(binding, saved_env);
return (body, extended_env, cont, ret)
Expand Down Expand Up @@ -1746,8 +1745,8 @@ mod tests {
expected.assert_eq(&computed.to_string());
};
expect_eq(cs.num_inputs(), expect!["1"]);
expect_eq(cs.aux().len(), expect!["8883"]);
expect_eq(cs.num_constraints(), expect!["10830"]);
expect_eq(cs.aux().len(), expect!["8884"]);
expect_eq(cs.num_constraints(), expect!["10831"]);
assert_eq!(func.num_constraints(&store), cs.num_constraints());
}
}
9 changes: 8 additions & 1 deletion src/lem/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use crate::{
self, Binop, Binop2, Call, Call0, Call2, Dummy, Emit, If, Let, LetRec, Lookup, Outermost,
Tail, Terminal, Unop,
},
tag::ExprTag::{Char, Comm, Cons, Cproc, Fun, Key, Nil, Num, Str, Sym, Thunk, U64},
tag::ExprTag::{Char, Comm, Cons, Cproc, Fun, Key, Nil, Num, RecVar, Str, Sym, Thunk, U64},
};

use super::pointers::{Ptr, RawPtr, ZPtr};
Expand Down Expand Up @@ -1004,6 +1004,13 @@ impl Ptr {
"<Opaque Sym>".into()
}
}
RecVar => {
if let Some(sym) = store.fetch_sym(&self.cast(Tag::Expr(Sym))) {
state.fmt_to_string(&sym.into())
} else {
"<Opaque RecVar>".into()
}
}
Key => {
if let Some(key) = store.fetch_key(self) {
state.fmt_to_string(&key.into())
Expand Down
2 changes: 2 additions & 0 deletions src/tag.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ pub enum ExprTag {
U64,
Key,
Cproc,
RecVar,
}

impl From<ExprTag> for u16 {
Expand Down Expand Up @@ -80,6 +81,7 @@ impl fmt::Display for ExprTag {
ExprTag::Comm => write!(f, "comm#"),
ExprTag::U64 => write!(f, "u64#"),
ExprTag::Cproc => write!(f, "cproc#"),
ExprTag::RecVar => write!(f, "rec_var#"),
}
}
}
Expand Down

0 comments on commit 3a5c7e7

Please sign in to comment.