Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix the free variable names #51

Merged
merged 4 commits into from
Sep 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -381,10 +381,7 @@ mod tests {

#[test]
fn alternative_lambda_parsing() {
assert_eq!(
parse(r#"\\\2(321)"#, DeBruijn),
parse("λλλ2(321)", DeBruijn)
)
assert_eq!(parse(r"\\\2(321)", DeBruijn), parse("λλλ2(321)", DeBruijn))
}

#[test]
Expand Down
74 changes: 68 additions & 6 deletions src/term.rs
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,27 @@ impl Term {
}
true
}

/// Returns the maximum depth of lambda abstractions
/// in the given `Term`.
///
/// # Example
/// ```
/// use lambda_calculus::*;
///
/// assert_eq!(abs(Var(1)).max_depth(), 1);
/// ```
pub fn max_depth(&self) -> u32 {
match self {
Var(_) => 0,
Abs(t) => t.max_depth() + 1,
App(boxed) => {
let d0 = boxed.0.max_depth();
let d1 = boxed.1.max_depth();
d0.max(d1)
}
}
}
}

/// Wraps a `Term` in an `Abs`traction. Consumes its argument.
Expand Down Expand Up @@ -426,11 +447,16 @@ pub fn app(lhs: Term, rhs: Term) -> Term {

impl fmt::Display for Term {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", show_precedence_cla(self, 0, 0))
write!(f, "{}", show_precedence_cla(self, 0, self.max_depth(), 0))
}
}

fn show_precedence_cla(term: &Term, context_precedence: usize, depth: u32) -> String {
fn show_precedence_cla(
term: &Term,
context_precedence: usize,
max_depth: u32,
depth: u32,
) -> String {
match term {
Var(0) => "undefined".to_owned(),
Var(i) => {
Expand All @@ -439,7 +465,8 @@ fn show_precedence_cla(term: &Term, context_precedence: usize, depth: u32) -> St
.expect("error while printing term")
.to_string()
} else {
from_u32(96 + *i as u32)
// use a different name than bound variables
from_u32(max_depth + 96 + *i as u32 - depth)
Comment on lines +468 to +469
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Refer to the free variable list while considering depth. Also, avoid using names that might be used for bound variables by using max_depth.

.expect("error while printing term")
.to_string()
}
Expand All @@ -450,7 +477,7 @@ fn show_precedence_cla(term: &Term, context_precedence: usize, depth: u32) -> St
"{}{}.{}",
LAMBDA,
from_u32(depth + 97).expect("error while printing term"),
show_precedence_cla(t, 0, depth + 1)
show_precedence_cla(t, 0, max_depth, depth + 1)
)
};
parenthesize_if(&ret, context_precedence > 1).into()
Expand All @@ -459,8 +486,8 @@ fn show_precedence_cla(term: &Term, context_precedence: usize, depth: u32) -> St
let (ref t1, ref t2) = **boxed;
let ret = format!(
"{} {}",
show_precedence_cla(t1, 2, depth),
show_precedence_cla(t2, 3, depth)
show_precedence_cla(t1, 2, max_depth, depth),
show_precedence_cla(t2, 3, max_depth, depth)
);
parenthesize_if(&ret, context_precedence == 3).into()
}
Expand Down Expand Up @@ -574,6 +601,30 @@ mod tests {
assert_eq!(&abs(Var(3)).to_string(), "λa.c");
assert_eq!(&abs!(2, Var(3)).to_string(), "λa.λb.c");
assert_eq!(&abs!(2, Var(4)).to_string(), "λa.λb.d");
assert_eq!(
app!(
Var(3),
Var(4),
abs(app(Var(4), Var(5))),
abs!(2, app(Var(5), Var(6)))
)
.to_string(),
"e f (λa.e f) (λa.λb.e f)"
);
assert_eq!(
app!(
abs!(2, app(Var(3), Var(4))),
Var(1),
Var(2),
abs(app(Var(2), Var(3)))
)
.to_string(),
"(λa.λb.c d) c d (λa.c d)"
);
assert_eq!(
&app(abs(Var(1)), app(abs(app(Var(10), Var(1))), Var(10))).to_string(),
"(λa.a) ((λa.j a) k)"
);
}

#[test]
Expand Down Expand Up @@ -615,4 +666,15 @@ mod tests {
assert!(!abs!(10, Var(11)).is_supercombinator());
assert!(!abs!(10, app(Var(10), Var(11))).is_supercombinator());
}

#[test]
fn max_depth() {
assert_eq!(Var(1).max_depth(), 0);
assert_eq!(abs(Var(1)).max_depth(), 1);
assert_eq!(abs!(10, Var(5)).max_depth(), 10);
assert_eq!(
app!(abs!(5, Var(2)), abs!(9, Var(4)), abs!(7, Var(6))).max_depth(),
9
);
}
}
Loading