-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
150 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
pub mod tests { | ||
use chompy::{ | ||
chomper::{Chomper, MathChomper, Rule}, | ||
language::{ChompyLanguage, MathLang}, | ||
}; | ||
use ruler::enumo::Sexp; | ||
use std::str::FromStr; | ||
|
||
use super::super::evaluate_ruleset; | ||
|
||
#[test] | ||
fn check_math_rules() { | ||
let chomper = MathChomper; | ||
let predicates = chomper.get_language().get_predicates(); | ||
let values = chomper.get_language().get_vals(); | ||
for v in values { | ||
println!("Value: {}", chomper.get_language().make_val(v)); | ||
} | ||
for p in predicates.force() { | ||
println!("Predicate: {}", p); | ||
} | ||
let rules = chomper.run_chompy(5); | ||
let hand_picked_rules = vec![ | ||
Rule { | ||
condition: Sexp::from_str("(Neq ?x (Const 0))").ok(), | ||
lhs: Sexp::from_str("(Div ?x ?x)").unwrap(), | ||
rhs: Sexp::from_str("(Const 1)").unwrap(), | ||
}, | ||
Rule { | ||
condition: Sexp::from_str("(Gt ?x (Const -1))").ok(), | ||
lhs: Sexp::from_str("(Abs ?x)").unwrap(), | ||
rhs: Sexp::from_str("?x").unwrap(), | ||
}, | ||
]; | ||
evaluate_ruleset::<MathChomper, MathLang>( | ||
&rules, | ||
&hand_picked_rules, | ||
chomper, | ||
MathLang::Var("dummy".into()), | ||
); | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,57 @@ | ||
mod halide; | ||
use chompy::chomper::{Chomper, Rule}; | ||
use chompy::language::ChompyLanguage; | ||
use ruler::enumo::Sexp; | ||
|
||
// mod halide; | ||
mod math; | ||
|
||
// This is a hack. `rule_is_derivable` calls | ||
// `concretize_rule`, which expects the rules which are | ||
// discovered via observational equivalence, i.e., rules | ||
// before they have been generalized. Going down a level, this | ||
// means `concretize_rule` expects the rules to have variables | ||
// as `(Var blah)` vs. what gets output by Chompy, which is | ||
// `?blah`. This function transforms the rules from the latter | ||
// format to the former. | ||
pub fn transform_rule(rule: &Rule, language: &Box<impl ChompyLanguage>) -> Rule { | ||
fn transform_sexp(sexp: &Sexp, language: &Box<impl ChompyLanguage>) -> Sexp { | ||
match sexp { | ||
Sexp::Atom(a) => { | ||
if a.starts_with("?") { | ||
language.make_var(&a[1..]) | ||
} else { | ||
sexp.clone() | ||
} | ||
} | ||
Sexp::List(l) => Sexp::List(l.iter().map(|s| transform_sexp(s, language)).collect()), | ||
} | ||
} | ||
Rule { | ||
condition: if let Some(cond) = &rule.condition { | ||
Some(transform_sexp(cond, language)) | ||
} else { | ||
None | ||
}, | ||
lhs: transform_sexp(&rule.lhs, language), | ||
rhs: transform_sexp(&rule.rhs, language), | ||
} | ||
} | ||
|
||
pub fn evaluate_ruleset<C: Chomper + Sized, L: ChompyLanguage + Sized>( | ||
chompy_rules: &Vec<Rule>, | ||
other_rules: &Vec<Rule>, | ||
chomper: C, | ||
language: L, | ||
) { | ||
let egraph = chomper.get_initial_egraph(); | ||
let b = Box::new(language); | ||
for rule in other_rules { | ||
let rule = transform_rule(rule, &b); | ||
let result = chomper.rule_is_derivable(&egraph, &chompy_rules, &rule); | ||
if result { | ||
println!("Rule is derivable: {:?}", rule); | ||
} else { | ||
println!("Rule is not derivable: {:?}", rule); | ||
} | ||
} | ||
} |