use std::{
ops::{BitAnd, Not},
rc::Rc,
};
pub trait AsAigerCircuit<'a> {
type Inner: AsAigerCircuit<'a>;
fn as_aiger_circuit(&'a self) -> AigerCircuit<'a, Self::Inner>;
}
#[derive(Debug)]
pub enum AigerCircuit<'a, T> {
And(AndOperand<'a, T>, AndOperand<'a, T>),
Not(&'a T),
Variable(usize),
Literal(bool),
}
#[derive(Debug)]
pub struct AndOperand<'a, T> {
pub id: usize,
pub value: &'a T,
}
#[repr(transparent)]
#[derive(Clone, Debug)]
pub struct SimpleCircuit(Rc<SimpleConstraint>);
impl BitAnd for SimpleCircuit {
type Output = Self;
fn bitand(self, rhs: Self) -> Self::Output {
Self(Rc::new(SimpleConstraint::And(self.0, rhs.0)))
}
}
impl Not for SimpleCircuit {
type Output = Self;
fn not(self) -> Self::Output {
Self(Rc::new(SimpleConstraint::Not(self.0)))
}
}
impl SimpleCircuit {
pub fn var(id: usize) -> Self {
Self(Rc::new(SimpleConstraint::Variable(id)))
}
pub fn lit(val: bool) -> Self {
Self(Rc::new(SimpleConstraint::Literal(val)))
}
}
#[derive(Clone, Debug)]
pub enum SimpleConstraint {
And(Rc<Self>, Rc<Self>),
Not(Rc<Self>),
Variable(usize),
Literal(bool),
}
impl<'a> AsAigerCircuit<'a> for SimpleCircuit {
type Inner = SimpleConstraint;
fn as_aiger_circuit(&'a self) -> AigerCircuit<'a, Self::Inner> {
match self.0.as_ref() {
SimpleConstraint::And(x, y) => AigerCircuit::And(
AndOperand {
id: Rc::as_ptr(x) as usize,
value: x.as_ref(),
},
AndOperand {
id: Rc::as_ptr(y) as usize,
value: y.as_ref(),
},
),
SimpleConstraint::Not(x) => AigerCircuit::Not(x.as_ref()),
SimpleConstraint::Variable(id) => AigerCircuit::Variable(*id),
SimpleConstraint::Literal(v) => AigerCircuit::Literal(*v),
}
}
}
impl<'a> AsAigerCircuit<'a> for SimpleConstraint {
type Inner = SimpleConstraint;
fn as_aiger_circuit(&'a self) -> AigerCircuit<'a, Self::Inner> {
match self {
SimpleConstraint::And(x, y) => AigerCircuit::And(
AndOperand {
id: Rc::as_ptr(x) as usize,
value: x.as_ref(),
},
AndOperand {
id: Rc::as_ptr(y) as usize,
value: y.as_ref(),
},
),
SimpleConstraint::Not(x) => AigerCircuit::Not(x.as_ref()),
SimpleConstraint::Variable(id) => AigerCircuit::Variable(*id),
SimpleConstraint::Literal(v) => AigerCircuit::Literal(*v),
}
}
}