conjure_core/ast/
domains.rsuse serde::{Deserialize, Serialize};
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub enum Range<A>
where
A: Ord,
{
Single(A),
Bounded(A, A),
}
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub enum Domain {
BoolDomain,
IntDomain(Vec<Range<i32>>),
}
impl Domain {
pub fn values_i32(&self) -> Option<Vec<i32>> {
match self {
Domain::IntDomain(ranges) => Some(
ranges
.iter()
.flat_map(|r| match r {
Range::Single(i) => vec![*i],
Range::Bounded(i, j) => (*i..=*j).collect(),
})
.collect(),
),
_ => None,
}
}
pub fn apply_i32(&self, op: fn(i32, i32) -> Option<i32>, other: &Domain) -> Option<Domain> {
if let (Some(vs1), Some(vs2)) = (self.values_i32(), other.values_i32()) {
let mut new_ranges = vec![];
for (v1, v2) in itertools::iproduct!(vs1, vs2) {
if let Some(v) = op(v1, v2) {
new_ranges.push(Range::Single(v))
}
}
return Some(Domain::IntDomain(new_ranges));
}
None
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_negative_product() {
let d1 = Domain::IntDomain(vec![Range::Bounded(-2, 1)]);
let d2 = Domain::IntDomain(vec![Range::Bounded(-2, 1)]);
let res = d1.apply_i32(|a, b| Some(a * b), &d2).unwrap();
assert!(matches!(res, Domain::IntDomain(_)));
if let Domain::IntDomain(ranges) = res {
assert!(!ranges.contains(&Range::Single(-4)));
assert!(!ranges.contains(&Range::Single(-3)));
assert!(ranges.contains(&Range::Single(-2)));
assert!(ranges.contains(&Range::Single(-1)));
assert!(ranges.contains(&Range::Single(0)));
assert!(ranges.contains(&Range::Single(1)));
assert!(ranges.contains(&Range::Single(2)));
assert!(!ranges.contains(&Range::Single(3)));
assert!(ranges.contains(&Range::Single(4)));
}
}
#[test]
fn test_negative_div() {
let d1 = Domain::IntDomain(vec![Range::Bounded(-2, 1)]);
let d2 = Domain::IntDomain(vec![Range::Bounded(-2, 1)]);
let res = d1
.apply_i32(|a, b| if b != 0 { Some(a / b) } else { None }, &d2)
.unwrap();
assert!(matches!(res, Domain::IntDomain(_)));
if let Domain::IntDomain(ranges) = res {
assert!(!ranges.contains(&Range::Single(-4)));
assert!(!ranges.contains(&Range::Single(-3)));
assert!(ranges.contains(&Range::Single(-2)));
assert!(ranges.contains(&Range::Single(-1)));
assert!(ranges.contains(&Range::Single(0)));
assert!(ranges.contains(&Range::Single(1)));
assert!(ranges.contains(&Range::Single(2)));
assert!(!ranges.contains(&Range::Single(3)));
assert!(!ranges.contains(&Range::Single(4)));
}
}
}