Skip to content

Commit

Permalink
wip: implement not(x) ~> w-lit(x,0)
Browse files Browse the repository at this point in the history
  • Loading branch information
niklasdewally committed Oct 11, 2024
1 parent d5d2116 commit 28be2b7
Showing 1 changed file with 25 additions and 7 deletions.
32 changes: 25 additions & 7 deletions crates/conjure_core/src/rules/minion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@
/* Rules for translating to Minion-supported constraints */
/************************************************************************/


use crate::ast::{
Constant as Const, DecisionVariable, Domain, Expression as Expr, SymbolTable,
};
use crate::ast::{Constant as Const, DecisionVariable, Domain, Expression as Expr, SymbolTable};
use crate::metadata::Metadata;
use crate::rule_engine::{
register_rule, register_rule_set, ApplicationError, ApplicationResult, Reduction,
Expand Down Expand Up @@ -397,6 +394,7 @@ fn negated_eq_to_neq(expr: &Expr, _: &Model) -> ApplicationResult {
/// ```text
/// and([x,...]) ~> and([w-literal(x,1),..])
/// or([x,...]) ~> or([w-literal(x,1),..])
/// not(x) ~> w-literal(x,0)
/// ```
///
/// ## Rationale
Expand All @@ -407,16 +405,18 @@ fn negated_eq_to_neq(expr: &Expr, _: &Model) -> ApplicationResult {
///
#[register_rule(("Minion", 50))]
fn boolean_literal_to_wliteral(expr: &Expr, mdl: &Model) -> ApplicationResult {
use Expr::*;
use Domain::BoolDomain;
use Expr::*;
match expr {
Or(m, vec) => {
let mut changed = false;
let mut new_vec = Vec::new();
for expr in vec {
new_vec.push(match expr {
Reference(m, name)
if mdl.get_domain(name).is_some_and(|x| matches!(x,BoolDomain)) =>
if mdl
.get_domain(name)
.is_some_and(|x| matches!(x, BoolDomain)) =>
{
changed = true;
WatchedLiteral(m.clone_dirty(), name.clone(), Const::Bool(true))
Expand All @@ -437,7 +437,9 @@ fn boolean_literal_to_wliteral(expr: &Expr, mdl: &Model) -> ApplicationResult {
for expr in vec {
new_vec.push(match expr {
Reference(m, name)
if mdl.get_domain(name).is_some_and(|x| matches!(x,BoolDomain)) =>
if mdl
.get_domain(name)
.is_some_and(|x| matches!(x, BoolDomain)) =>
{
changed = true;
WatchedLiteral(m.clone_dirty(), name.clone(), Const::Bool(true))
Expand All @@ -452,6 +454,22 @@ fn boolean_literal_to_wliteral(expr: &Expr, mdl: &Model) -> ApplicationResult {

Ok(Reduction::pure(And(m.clone_dirty(), new_vec)))
}

Not(m, expr) => {
if let Reference(_, name) = (**expr).clone() {
if mdl
.get_domain(&name)
.is_some_and(|x| matches!(x, BoolDomain))
{
return Ok(Reduction::pure(WatchedLiteral(
m.clone_dirty(),
name.clone(),
Const::Bool(false),
)));
}
}
Err(RuleNotApplicable)
}
_ => Err(RuleNotApplicable),
}
}

0 comments on commit 28be2b7

Please sign in to comment.