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 first batch of simplifications #35

Merged
merged 3 commits into from
Nov 23, 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
1 change: 1 addition & 0 deletions src/convenience/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod unbox;
51 changes: 51 additions & 0 deletions src/convenience/unbox/fol.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
use crate::syntax_tree::fol::{
AtomicFormula, BinaryConnective, Formula, Quantification, UnaryConnective,
};

pub enum UnboxedFormula {
AtomicFormula(AtomicFormula),
UnaryFormula {
connective: UnaryConnective,
formula: Formula,
},
BinaryFormula {
connective: BinaryConnective,
lhs: Formula,
rhs: Formula,
},
QuantifiedFormula {
quantification: Quantification,
formula: Formula,
},
}

impl UnboxedFormula {
pub fn rebox(self) -> Formula {
match self {
Self::AtomicFormula(f) => Formula::AtomicFormula(f),
Self::UnaryFormula {
connective,
formula,
} => Formula::UnaryFormula {
connective,
formula: Box::new(formula),
},
Self::BinaryFormula {
connective,
lhs,
rhs,
} => Formula::BinaryFormula {
connective,
lhs: Box::new(lhs),
rhs: Box::new(rhs),
},
Self::QuantifiedFormula {
quantification,
formula,
} => Formula::QuantifiedFormula {
quantification,
formula: Box::new(formula),
},
}
}
}
44 changes: 44 additions & 0 deletions src/convenience/unbox/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
use crate::syntax_tree::fol::Formula;

use self::fol::UnboxedFormula;

pub mod fol;

pub trait Unbox {
type Unboxed;

fn unbox(self) -> Self::Unboxed;
}

impl Unbox for Formula {
type Unboxed = UnboxedFormula;

fn unbox(self) -> UnboxedFormula {
match self {
Self::AtomicFormula(f) => UnboxedFormula::AtomicFormula(f),
Self::UnaryFormula {
connective,
formula,
} => UnboxedFormula::UnaryFormula {
connective,
formula: *formula,
},
Self::BinaryFormula {
connective,
lhs,
rhs,
} => UnboxedFormula::BinaryFormula {
connective,
lhs: *lhs,
rhs: *rhs,
},
Self::QuantifiedFormula {
quantification,
formula,
} => UnboxedFormula::QuantifiedFormula {
quantification,
formula: *formula,
},
}
}
}
2 changes: 2 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
pub mod convenience;
pub mod formatting;
pub mod parsing;
pub mod simplifying;
pub mod syntax_tree;

fn main() {
Expand Down
165 changes: 165 additions & 0 deletions src/simplifying/fol/ht.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
use crate::{
convenience::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)),
},
})
}

pub fn simplify_outer(formula: Formula) -> Formula {
// TODO: Split simplifications into multiple functions?

match formula.unbox() {
// Remove identities
// e.g. F op E => F

// F and #true => F
UnboxedFormula::BinaryFormula {
connective: BinaryConnective::Conjunction,
lhs,
rhs: Formula::AtomicFormula(AtomicFormula::Truth),
} => lhs,

// #true and F => F
UnboxedFormula::BinaryFormula {
connective: BinaryConnective::Conjunction,
lhs: Formula::AtomicFormula(AtomicFormula::Truth),
rhs,
} => rhs,

// F or #false => F
UnboxedFormula::BinaryFormula {
connective: BinaryConnective::Disjunction,
lhs,
rhs: Formula::AtomicFormula(AtomicFormula::Falsity),
} => lhs,

// #false or F => F
UnboxedFormula::BinaryFormula {
connective: BinaryConnective::Disjunction,
lhs: Formula::AtomicFormula(AtomicFormula::Falsity),
rhs,
} => rhs,

// Remove annihilations
// e.g. F op E => E

// F or #true => #true
UnboxedFormula::BinaryFormula {
connective: BinaryConnective::Disjunction,
lhs: _,
rhs: rhs @ Formula::AtomicFormula(AtomicFormula::Truth),
} => rhs,

// #true or F => #true
UnboxedFormula::BinaryFormula {
connective: BinaryConnective::Disjunction,
lhs: lhs @ Formula::AtomicFormula(AtomicFormula::Truth),
rhs: _,
} => lhs,

// F and #false => false
UnboxedFormula::BinaryFormula {
connective: BinaryConnective::Conjunction,
lhs: _,
rhs: rhs @ Formula::AtomicFormula(AtomicFormula::Falsity),
} => rhs,

// #false and F => #false
UnboxedFormula::BinaryFormula {
connective: BinaryConnective::Conjunction,
lhs: lhs @ Formula::AtomicFormula(AtomicFormula::Falsity),
rhs: _,
} => lhs,

// Remove idempotences
// e.g. F op F => F

// F and F => F
// F or F => F
UnboxedFormula::BinaryFormula {
connective: BinaryConnective::Conjunction | BinaryConnective::Disjunction,
lhs,
rhs,
} if lhs == rhs => lhs,

x => x.rebox(),
}
}

#[cfg(test)]
mod tests {
use super::{simplify, simplify_outer};

#[test]
fn test_simplify() {
for (src, target) in [
("#true and a", "a"),
("a and #true", "a"),
("#false or a", "a"),
("a or #false", "a"),
("#true or a", "#true"),
("a or #true", "#true"),
("#false and a", "#false"),
("a and #false", "#false"),
("a and a", "a"),
("a or a", "a"),
("#true and #true and a", "a"),
("#true and (#true and a)", "a"),
] {
assert_eq!(simplify(src.parse().unwrap()), target.parse().unwrap())
}
}

#[test]
fn test_simplify_outer() {
for (src, target) in [
("#true and a", "a"),
("a and #true", "a"),
("#false or a", "a"),
("a or #false", "a"),
("#true or a", "#true"),
("a or #true", "#true"),
("#false and a", "#false"),
("a and #false", "#false"),
("a and a", "a"),
("a or a", "a"),
("#true and (#true and a)", "#true and a"),
("(#true and #true) and a", "(#true and #true) and a"),
] {
assert_eq!(
simplify_outer(src.parse().unwrap()),
target.parse().unwrap()
)
}
}
}
1 change: 1 addition & 0 deletions src/simplifying/fol/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod ht;
1 change: 1 addition & 0 deletions src/simplifying/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod fol;
Loading