Skip to content

Commit

Permalink
Refactor compare_one_to_many() to use recursion
Browse files Browse the repository at this point in the history
  • Loading branch information
turboladen committed Aug 28, 2024
1 parent 4717d16 commit 3edf401
Showing 1 changed file with 61 additions and 24 deletions.
85 changes: 61 additions & 24 deletions crates/api/src/unit/term_reducing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,22 +100,33 @@ pub(super) fn simplify_one_to_many(lhs: &Term, rhs_terms: &[Term]) -> WhatToKeep
for (i, rhs) in rhs_terms.iter().enumerate() {
match lhs.term_reduce(rhs) {
ReducedTerm::ReducedToTerm(new_term) => {
what_to_keep.new_terms.push(new_term);
what_to_keep.keep_lhs = false;

if let Some(things) = rhs_terms.get((i + 1)..) {
what_to_keep.kept_terms.extend_from_slice(things);
match rhs_terms.get((i + 1)..) {
Some(things) if !things.is_empty() => {
let wtk = simplify_one_to_many(&new_term, things);

if wtk.keep_lhs {
what_to_keep.new_terms.push(new_term);
}

what_to_keep.new_terms.extend(wtk.new_terms);
what_to_keep.kept_terms.extend(wtk.kept_terms);
}
_ => {
what_to_keep.new_terms.push(new_term);
}
}

break;
return what_to_keep;
}
ReducedTerm::ReducedAway => {
what_to_keep.keep_lhs = false;

if let Some(things) = rhs_terms.get((i + 1)..) {
what_to_keep.kept_terms.extend_from_slice(things);
}
break;
return what_to_keep;
}
ReducedTerm::NotReducible => {
what_to_keep.kept_terms.push(rhs.clone());
Expand All @@ -128,68 +139,94 @@ pub(super) fn simplify_one_to_many(lhs: &Term, rhs_terms: &[Term]) -> WhatToKeep

#[cfg(test)]
mod tests {
use crate::term::UNITY;

use super::*;

#[test]
fn compare_one_to_many_test() {
const METER: Term = Term::Atom(crate::atom::Atom::Meter);
const METER: Term = Term::Atom(crate::atom::Atom::Meter);

mod compare_one_to_many {
use super::*;

assert_eq!(
simplify_one_to_many(&crate::term::UNITY, &[]),
macro_rules! test_compare_one_to_many {
($test_name:ident, $lhs:expr, $rhs:expr, $expected:expr) => {
#[test]
fn $test_name() {
pretty_assertions::assert_eq!(simplify_one_to_many($lhs, $rhs), $expected);
}
};
}

test_compare_one_to_many!(
test_unity_vs_empty_slice,
&UNITY,
&[],
WhatToKeep::default()
);

assert_eq!(
simplify_one_to_many(&METER, &[METER]),
test_compare_one_to_many!(
test_meter_vs_meter,
&METER,
&[METER],
WhatToKeep {
keep_lhs: false,
new_terms: vec![term!(Meter, exponent: 2)],
kept_terms: vec![]
}
);

assert_eq!(
simplify_one_to_many(&METER, &[term!(Meter, exponent: -1)]),
test_compare_one_to_many!(
test_meter_vs_meter_minus_1,
&METER,
&[term!(Meter, exponent: -1)],
WhatToKeep {
keep_lhs: false,
new_terms: vec![],
kept_terms: vec![]
}
);

assert_eq!(
simplify_one_to_many(&METER, &[term!(Meter, exponent: -2)]),
test_compare_one_to_many!(
test_meter_vs_meter_minus_2,
&METER,
&[term!(Meter, exponent: -2)],
WhatToKeep {
keep_lhs: false,
new_terms: vec![term!(Meter, exponent: -1)],
kept_terms: vec![]
}
);

assert_eq!(
simplify_one_to_many(&crate::term::UNITY, &[METER]),
test_compare_one_to_many!(
test_unity_vs_meter,
&UNITY,
&[METER],
WhatToKeep {
keep_lhs: true,
new_terms: vec![],
kept_terms: vec![METER]
}
);

assert_eq!(
simplify_one_to_many(&METER, &[term!(Gram)]),
test_compare_one_to_many!(
test_meter_vs_gram,
&METER,
&[term!(Gram)],
WhatToKeep {
keep_lhs: true,
new_terms: vec![],
kept_terms: vec![term!(Gram)]
}
);

assert_eq!(
simplify_one_to_many(&METER, &[METER, METER, METER]),
test_compare_one_to_many!(
test_meter_vs_meter_meter_meter,
&METER,
&[METER, METER, METER],
WhatToKeep {
keep_lhs: false,
new_terms: vec![term!(Meter, exponent: 2)],
kept_terms: vec![METER, METER]
new_terms: vec![term!(Meter, exponent: 4)],
kept_terms: vec![]
}
);
}
Expand Down

0 comments on commit 3edf401

Please sign in to comment.