Skip to content

Commit

Permalink
Integrate the gamma translation into the command-line interface
Browse files Browse the repository at this point in the history
  • Loading branch information
teiesti authored and ZachJHansen committed Feb 16, 2024
1 parent bf19493 commit d3e525e
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 10 deletions.
1 change: 1 addition & 0 deletions src/command_line/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ pub enum Command {

#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, ValueEnum)]
pub enum Translation {
Gamma,
TauStar,
}

Expand Down
13 changes: 12 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,13 @@ pub mod translating;
use {
crate::{
command_line::{Arguments, Command, Translation},
syntax_tree::asp,
syntax_tree::{asp, fol},
translating::tau_star::tau_star,
},
anyhow::{Context, Result},
clap::Parser as _,
std::fs::read_to_string,
translating::gamma::gamma,
};

fn main() -> Result<()> {
Expand All @@ -24,6 +25,16 @@ fn main() -> Result<()> {
.with_context(|| format!("could not read file `{}`", input.display()))?;

match with {
Translation::Gamma => {
let theory: fol::Theory = content
.parse()
.with_context(|| format!("could not parse file `{}`", input.display()))?;

let theory = gamma(theory);

println!("{theory}")
}

Translation::TauStar => {
let program: asp::Program = content
.parse()
Expand Down
27 changes: 18 additions & 9 deletions src/translating/gamma.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
use crate::{
convenience::apply::Apply as _,
syntax_tree::fol::{AtomicFormula, BinaryConnective, Formula, UnaryConnective},
syntax_tree::fol::{AtomicFormula, BinaryConnective, Formula, Theory, UnaryConnective},
};

pub fn gamma(formula: Formula) -> Formula {
pub fn gamma(theory: Theory) -> Theory {
let mut formulas = Vec::new();
for formula in theory.formulas {
formulas.push(gamma_formula(formula));
}

Theory { formulas }
}

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

Expand All @@ -22,8 +31,8 @@ pub fn gamma(formula: Formula) -> Formula {
rhs,
} => Formula::BinaryFormula {
connective,
lhs: gamma(*lhs).into(),
rhs: gamma(*rhs).into(),
lhs: gamma_formula(*lhs).into(),
rhs: gamma_formula(*rhs).into(),
},

Formula::BinaryFormula {
Expand All @@ -37,8 +46,8 @@ pub fn gamma(formula: Formula) -> Formula {
connective: BinaryConnective::Conjunction,
lhs: Formula::BinaryFormula {
connective: connective.clone(),
lhs: gamma(*lhs.clone()).into(),
rhs: gamma(*rhs.clone()).into(),
lhs: gamma_formula(*lhs.clone()).into(),
rhs: gamma_formula(*rhs.clone()).into(),
}
.into(),
rhs: Formula::BinaryFormula {
Expand All @@ -54,7 +63,7 @@ pub fn gamma(formula: Formula) -> Formula {
formula,
} => Formula::QuantifiedFormula {
quantification,
formula: gamma(*formula).into(),
formula: gamma_formula(*formula).into(),
},
}
}
Expand All @@ -79,7 +88,7 @@ fn prepend_predicate(formula: Formula, prefix: &'static str) -> Formula {

#[cfg(test)]
mod tests {
use super::gamma;
use super::gamma_formula;

#[test]
fn test_simplify() {
Expand All @@ -98,7 +107,7 @@ mod tests {
("forall X p(X)", "forall X hp(X)"),
("exists X p(X)", "exists X hp(X)"),
] {
assert_eq!(gamma(src.parse().unwrap()), target.parse().unwrap())
assert_eq!(gamma_formula(src.parse().unwrap()), target.parse().unwrap())
}
}
}

0 comments on commit d3e525e

Please sign in to comment.