From cd388784e90a22d69ef1a3e50721e5ef0a72bb7c Mon Sep 17 00:00:00 2001 From: agentelement Date: Sat, 18 May 2024 20:26:44 -0700 Subject: [PATCH 1/2] feat: Term::is_isomorphic_to and associated tests --- src/term.rs | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/src/term.rs b/src/term.rs index c9304c3..8999d75 100644 --- a/src/term.rs +++ b/src/term.rs @@ -418,6 +418,48 @@ 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 { + Var(x) => { + if let Var(y) = other { + x == y + } else { + false + } + } + Abs(p) => { + if let Abs(q) = other { + p.is_isomorphic_to(q) + } else { + false + } + } + App(p_boxed) => { + if let App(q_boxed) = other { + let (ref fp, ref ap) = **p_boxed; + let (ref fq, ref aq) = **q_boxed; + fp.is_isomorphic_to(fq) && ap.is_isomorphic_to(aq) + } else { + false + } + } + } + } } /// Wraps a `Term` in an `Abs`traction. Consumes its argument. @@ -677,4 +719,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))))); + } } From 75cbfd97b75c633c7b200db2a27a3207d8477400 Mon Sep 17 00:00:00 2001 From: AgentElement <38045210+AgentElement@users.noreply.github.com> Date: Wed, 29 May 2024 02:50:04 -0700 Subject: [PATCH 2/2] refactor: change match expression in is_isomorphic_to Co-authored-by: ljedrz --- src/term.rs | 31 ++++++++----------------------- 1 file changed, 8 insertions(+), 23 deletions(-) diff --git a/src/term.rs b/src/term.rs index 8999d75..823d83a 100644 --- a/src/term.rs +++ b/src/term.rs @@ -434,30 +434,15 @@ impl Term { /// /// ``` pub fn is_isomorphic_to(&self, other: &Term) -> bool { - match self { - Var(x) => { - if let Var(y) = other { - x == y - } else { - false - } - } - Abs(p) => { - if let Abs(q) = other { - p.is_isomorphic_to(q) - } else { - false - } - } - App(p_boxed) => { - if let App(q_boxed) = other { - let (ref fp, ref ap) = **p_boxed; - let (ref fq, ref aq) = **q_boxed; - fp.is_isomorphic_to(fq) && ap.is_isomorphic_to(aq) - } else { - false - } + 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, } } }