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

Add a convenience feature that applies a function recursively (in post-order) to a tree #41

Merged
merged 3 commits into from
Dec 1, 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
45 changes: 45 additions & 0 deletions src/convenience/apply/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
use crate::syntax_tree::fol::Formula;

pub trait Apply {
fn apply(self, f: &mut impl FnMut(Self) -> Self) -> Self
where
Self: Sized;
}

impl Apply for Formula {
fn apply(self, f: &mut impl FnMut(Self) -> Self) -> Self
where
Self: Sized,
{
let inner = match self {
x @ Formula::AtomicFormula(_) => x,

Formula::UnaryFormula {
connective,
formula,
} => Formula::UnaryFormula {
connective,
formula: formula.apply(f).into(),
},

Formula::BinaryFormula {
connective,
lhs,
rhs,
} => Formula::BinaryFormula {
connective,
lhs: lhs.apply(f).into(),
rhs: rhs.apply(f).into(),
},

Formula::QuantifiedFormula {
quantification,
formula,
} => Formula::QuantifiedFormula {
quantification,
formula: formula.apply(f).into(),
},
};
f(inner)
}
}
1 change: 1 addition & 0 deletions src/convenience/mod.rs
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
pub mod apply;
pub mod unbox;
35 changes: 5 additions & 30 deletions src/simplifying/fol/ht.rs
Original file line number Diff line number Diff line change
@@ -1,38 +1,13 @@
use crate::{
convenience::unbox::{fol::UnboxedFormula, Unbox as _},
convenience::{
apply::Apply as _,
unbox::{fol::UnboxedFormula, Unbox as _},
},
syntax_tree::fol::{AtomicFormula, BinaryConnective, Formula},
};

pub fn simplify(formula: Formula) -> Formula {
simplify_outer(match formula {
x @ Formula::AtomicFormula(_) => x,

Formula::UnaryFormula {
connective,
formula,
} => Formula::UnaryFormula {
connective,
formula: Box::new(simplify(*formula)),
},

Formula::BinaryFormula {
connective,
lhs,
rhs,
} => Formula::BinaryFormula {
connective,
lhs: Box::new(simplify(*lhs)),
rhs: Box::new(simplify(*rhs)),
},

Formula::QuantifiedFormula {
quantification,
formula,
} => Formula::QuantifiedFormula {
quantification,
formula: Box::new(simplify(*formula)),
},
})
formula.apply(&mut simplify_outer)
}

pub fn simplify_outer(formula: Formula) -> Formula {
Expand Down
Loading