1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
mod bitvec;
mod external;
mod monster;

#[cfg(feature = "boolector-solver")]
mod boolector;

#[cfg(feature = "z3-solver")]
mod z3;

#[cfg(feature = "boolector-solver")]
pub use self::boolector::*;

#[cfg(feature = "z3-solver")]
pub use self::z3::*;

pub use self::{bitvec::*, external::*, monster::*};

use log::debug;
use std::marker::Sync;
use std::{collections::HashMap, convert::From, fmt, io, ops::Index};
use thiserror::Error;

pub type Assignment = HashMap<SymbolId, BitVector>;

pub trait Solver: Default + Sync {
    fn name() -> &'static str;

    fn solve<F: Formula>(&self, formula: &F) -> Result<Option<Assignment>, SolverError> {
        debug!("try to solve with {} solver", Self::name());

        time_debug!("finished solving formula", { self.solve_impl(formula) })
    }

    fn solve_impl<F: Formula>(&self, formula: &F) -> Result<Option<Assignment>, SolverError>;
}

#[derive(Debug, Error, Clone)]
pub enum SolverError {
    #[error("failed to compute satisfiability within the given limits")]
    SatUnknown,

    #[error("could not find a satisfiable assignment before timing out")]
    Timeout,

    #[error("solver failed with IO error")]
    IoError(String),
}

impl From<io::Error> for SolverError {
    fn from(err: io::Error) -> Self {
        SolverError::IoError(err.to_string())
    }
}

#[derive(Clone, Debug, Copy, Eq, Hash, PartialEq)]
pub enum OperandSide {
    Lhs,
    Rhs,
}

impl OperandSide {
    #[allow(dead_code)]
    pub fn other(&self) -> Self {
        match self {
            OperandSide::Lhs => OperandSide::Rhs,
            OperandSide::Rhs => OperandSide::Lhs,
        }
    }
}

#[derive(Debug, Copy, Clone, Hash, Eq, PartialEq)]
pub enum BVOperator {
    Add,
    Sub,
    Mul,
    Divu,
    Sltu,
    Remu,
    Not,
    Equals,
    BitwiseAnd,
}

impl BVOperator {
    pub fn is_unary(&self) -> bool {
        *self == BVOperator::Not
    }
    pub fn is_binary(&self) -> bool {
        !self.is_unary()
    }
}

impl fmt::Display for BVOperator {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(
            f,
            "{}",
            match self {
                BVOperator::Add => "+",
                BVOperator::Sub => "-",
                BVOperator::Mul => "*",
                BVOperator::Divu => "/",
                BVOperator::Not => "!",
                BVOperator::Remu => "%",
                BVOperator::Equals => "=",
                BVOperator::BitwiseAnd => "&",
                BVOperator::Sltu => "<",
            }
        )
    }
}

#[derive(Clone, Debug, Eq, Hash, PartialEq)]
pub enum Symbol {
    Constant(BitVector),
    Input(String),
    Operator(BVOperator),
}

pub type SymbolId = usize;

pub trait Formula: Index<SymbolId, Output = Symbol> {
    type DependencyIter: Iterator<Item = SymbolId>;
    type SymbolIdsIter: Iterator<Item = SymbolId>;

    fn root(&self) -> SymbolId;

    fn operands(&self, sym: SymbolId) -> (SymbolId, Option<SymbolId>);

    fn operand(&self, sym: SymbolId) -> SymbolId;

    fn dependencies(&self, sym: SymbolId) -> Self::DependencyIter;
    //where
    //Iter: Iterator<Item = SymbolId>;

    fn symbol_ids(&self) -> Self::SymbolIdsIter;
    //where
    //Iter: Iterator<Item = SymbolId>;

    fn is_operand(&self, sym: SymbolId) -> bool;

    fn traverse<V, R>(&self, n: SymbolId, visit_map: &mut HashMap<SymbolId, R>, v: &mut V) -> R
    where
        V: FormulaVisitor<R>,
        R: Clone;
}

pub trait FormulaVisitor<T>: Sized {
    fn input(&mut self, idx: SymbolId, name: &str) -> T;
    fn constant(&mut self, idx: SymbolId, v: BitVector) -> T;
    fn unary(&mut self, idx: SymbolId, op: BVOperator, v: T) -> T;
    fn binary(&mut self, idx: SymbolId, op: BVOperator, lhs: T, rhs: T) -> T;
}