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

Term::is_isomorphic_to: A function to determine if two term objects describe identical expressions. #53

Merged
merged 2 commits into from
May 29, 2024
Merged
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
36 changes: 36 additions & 0 deletions src/term.rs
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,33 @@ impl Term {
}
}
}

/// Returns `true` if `self` is structurally isomorphic to `other`.
///
/// # Example
/// ```
/// use lambda_calculus::*;
///
/// let term1 = abs(Var(1)); // λ 1
/// let term2 = abs(Var(2)); // λ 2
/// let term3 = abs(Var(1)); // λ 1
///
/// assert_eq!(term1.is_isomorphic_to(&term2), false);
/// assert_eq!(term1.is_isomorphic_to(&term3), true);
///
/// ```
pub fn is_isomorphic_to(&self, other: &Term) -> bool {
match (self, other) {
(Var(x), Var(y)) => x == y,
(Abs(p), Abs(q)) => p.is_isomorphic_to(q),
(App(p_boxed), App(q_boxed)) => {
let (ref fp, ref ap) = **p_boxed;
let (ref fq, ref aq) = **q_boxed;
fp.is_isomorphic_to(fq) && ap.is_isomorphic_to(aq)
}
_ => false,
}
}
}

/// Wraps a `Term` in an `Abs`traction. Consumes its argument.
Expand Down Expand Up @@ -677,4 +704,13 @@ mod tests {
9
);
}

#[test]
fn is_isomorphic_to() {
assert!(abs(Var(1)).is_isomorphic_to(&abs(Var(1))));
assert!(!abs(Var(1)).is_isomorphic_to(&abs(Var(2))));
assert!(!app(abs(Var(1)), Var(1)).is_isomorphic_to(&app(abs(Var(1)), Var(2))));
assert!(app(abs(Var(1)), Var(1)).is_isomorphic_to(&app(abs(Var(1)), Var(1))));
assert!(!app(abs(Var(1)), Var(1)).is_isomorphic_to(&app(Var(2), abs(Var(1)))));
}
}
Loading