Copyright | (c) Masahiro Sakai 2021 |
---|---|
License | BSD-style |
Maintainer | masahiro.sakai@gmail.com |
Stability | unstable |
Portability | non-portable |
Safe Haskell | None |
Language | Haskell2010 |
Reduced Ordered Binary Decision Diagrams (ROBDD).
References:
- Bryant, "Graph-Based Algorithms for Boolean Function Manipulation," in IEEE Transactions on Computers, vol. C-35, no. 8, pp. 677-691, Aug. 1986, doi: 10.1109/TC.1986.1676819. https://www.cs.cmu.edu/~bryant/pubdir/ieeetc86.pdf
Synopsis
- data BDD a where
- class ItemOrder a where
- compareItem :: proxy a -> Int -> Int -> Ordering
- data AscOrder
- data DescOrder
- withDefaultOrder :: forall r. (forall a. ItemOrder a => Proxy a -> r) -> r
- withAscOrder :: forall r. (Proxy AscOrder -> r) -> r
- withDescOrder :: forall r. (Proxy DescOrder -> r) -> r
- withCustomOrder :: forall r. (Int -> Int -> Ordering) -> (forall a. ItemOrder a => Proxy a -> r) -> r
- true :: BDD a
- false :: BDD a
- var :: Int -> BDD a
- notB :: BDD a -> BDD a
- (.&&.) :: forall a. ItemOrder a => BDD a -> BDD a -> BDD a
- (.||.) :: forall a. ItemOrder a => BDD a -> BDD a -> BDD a
- xor :: forall a. ItemOrder a => BDD a -> BDD a -> BDD a
- (.=>.) :: forall a. ItemOrder a => BDD a -> BDD a -> BDD a
- (.<=>.) :: forall a. ItemOrder a => BDD a -> BDD a -> BDD a
- ite :: forall a. ItemOrder a => BDD a -> BDD a -> BDD a -> BDD a
- andB :: forall f a. (Foldable f, ItemOrder a) => f (BDD a) -> BDD a
- orB :: forall f a. (Foldable f, ItemOrder a) => f (BDD a) -> BDD a
- pbAtLeast :: forall a w. (ItemOrder a, Real w) => IntMap w -> w -> BDD a
- pbAtMost :: forall a w. (ItemOrder a, Real w) => IntMap w -> w -> BDD a
- pbExactly :: forall a w. (ItemOrder a, Real w) => IntMap w -> w -> BDD a
- pbExactlyIntegral :: forall a w. (ItemOrder a, Real w, Integral w) => IntMap w -> w -> BDD a
- forAll :: forall a. ItemOrder a => Int -> BDD a -> BDD a
- exists :: forall a. ItemOrder a => Int -> BDD a -> BDD a
- existsUnique :: forall a. ItemOrder a => Int -> BDD a -> BDD a
- forAllSet :: forall a. ItemOrder a => IntSet -> BDD a -> BDD a
- existsSet :: forall a. ItemOrder a => IntSet -> BDD a -> BDD a
- existsUniqueSet :: forall a. ItemOrder a => IntSet -> BDD a -> BDD a
- support :: BDD a -> IntSet
- evaluate :: (Int -> Bool) -> BDD a -> Bool
- numNodes :: BDD a -> Int
- restrict :: forall a. ItemOrder a => Int -> Bool -> BDD a -> BDD a
- restrictSet :: forall a. ItemOrder a => IntMap Bool -> BDD a -> BDD a
- restrictLaw :: forall a. ItemOrder a => BDD a -> BDD a -> BDD a
- subst :: forall a. ItemOrder a => Int -> BDD a -> BDD a -> BDD a
- substSet :: forall a. ItemOrder a => IntMap (BDD a) -> BDD a -> BDD a
- anySat :: BDD a -> Maybe (IntMap Bool)
- allSat :: BDD a -> [IntMap Bool]
- anySatComplete :: ItemOrder a => IntSet -> BDD a -> Maybe (IntMap Bool)
- allSatComplete :: ItemOrder a => IntSet -> BDD a -> [IntMap Bool]
- countSat :: forall a b. (ItemOrder a, Num b, Bits b, HasCallStack) => IntSet -> BDD a -> b
- uniformSatM :: forall a g m. (ItemOrder a, StatefulGen g m, HasCallStack) => IntSet -> BDD a -> g -> m (IntMap Bool)
- data Sig a
- inSig :: Sig (BDD a) -> BDD a
- outSig :: BDD a -> Sig (BDD a)
- fold :: (Int -> b -> b -> b) -> (Bool -> b) -> BDD a -> b
- fold' :: (Int -> b -> b -> b) -> (Bool -> b) -> BDD a -> b
- unfoldHashable :: forall a b. (ItemOrder a, Eq b, Hashable b) => (b -> Sig b) -> b -> BDD a
- unfoldOrd :: forall a b. (ItemOrder a, Ord b) => (b -> Sig b) -> b -> BDD a
- lfp :: ItemOrder a => (BDD a -> BDD a) -> BDD a
- gfp :: ItemOrder a => (BDD a -> BDD a) -> BDD a
- type Graph f = IntMap (f Int)
- toGraph :: BDD a -> (Graph Sig, Int)
- toGraph' :: Traversable t => t (BDD a) -> (Graph Sig, t Int)
- fromGraph :: HasCallStack => (Graph Sig, Int) -> BDD a
- fromGraph' :: HasCallStack => Graph Sig -> IntMap (BDD a)
The BDD type
Reduced ordered binary decision diagram representing boolean function
pattern Leaf :: Bool -> BDD a | |
pattern Branch :: Int -> BDD a -> BDD a -> BDD a | Smart constructor that takes the BDD reduction rules into account |
Item ordering
withDefaultOrder :: forall r. (forall a. ItemOrder a => Proxy a -> r) -> r Source #
Currently the default order is AscOrder
withAscOrder :: forall r. (Proxy AscOrder -> r) -> r Source #
withDescOrder :: forall r. (Proxy DescOrder -> r) -> r Source #
withCustomOrder :: forall r. (Int -> Int -> Ordering) -> (forall a. ItemOrder a => Proxy a -> r) -> r Source #
Boolean operations
(.&&.) :: forall a. ItemOrder a => BDD a -> BDD a -> BDD a infixr 3 Source #
Conjunction of two boolean function
(.||.) :: forall a. ItemOrder a => BDD a -> BDD a -> BDD a infixr 2 Source #
Disjunction of two boolean function
andB :: forall f a. (Foldable f, ItemOrder a) => f (BDD a) -> BDD a Source #
Conjunction of a list of BDDs.
orB :: forall f a. (Foldable f, ItemOrder a) => f (BDD a) -> BDD a Source #
Disjunction of a list of BDDs.
Pseudo-boolean constraints
pbAtLeast :: forall a w. (ItemOrder a, Real w) => IntMap w -> w -> BDD a Source #
Pseudo-boolean constraint, w1*x1 + w2*x2 + … ≥ k.
pbAtMost :: forall a w. (ItemOrder a, Real w) => IntMap w -> w -> BDD a Source #
Pseudo-boolean constraint, w1*x1 + w2*x2 + … ≤ k.
pbExactly :: forall a w. (ItemOrder a, Real w) => IntMap w -> w -> BDD a Source #
Pseudo-boolean constraint, w1*x1 + w2*x2 + … = k.
If weight type is Integral
, pbExactlyIntegral
is more efficient.
pbExactlyIntegral :: forall a w. (ItemOrder a, Real w, Integral w) => IntMap w -> w -> BDD a Source #
Similar to pbExactly
but more efficient.
Quantification
existsUnique :: forall a. ItemOrder a => Int -> BDD a -> BDD a Source #
Unique existential quantification (∃!)
forAllSet :: forall a. ItemOrder a => IntSet -> BDD a -> BDD a Source #
Universal quantification (∀) over a set of variables
existsSet :: forall a. ItemOrder a => IntSet -> BDD a -> BDD a Source #
Existential quantification (∃) over a set of variables
existsUniqueSet :: forall a. ItemOrder a => IntSet -> BDD a -> BDD a Source #
Unique existential quantification (∃!) over a set of variables
Query
evaluate :: (Int -> Bool) -> BDD a -> Bool Source #
Evaluate a boolean function represented as BDD under the valuation
given by (Int -> Bool)
, i.e. it lifts a valuation function from
variables to BDDs.
numNodes :: BDD a -> Int Source #
Count the number of nodes in a BDD viewed as a rooted directed acyclic graph.
See also toGraph
.
Restriction / Cofactor
restrict :: forall a. ItemOrder a => Int -> Bool -> BDD a -> BDD a Source #
Compute \(F|_{x_i} \) or \(F|_{\neg x_i} \).
\[ F|_{x_i}(\ldots, x_{i-1}, x_{i+1}, \ldots) = F(\ldots, x_{i-1}, \mathrm{True}, x_{i+1}, \ldots) \] \[ F|_{\neg x_i}(\ldots, x_{i-1}, x_{i+1}, \ldots) = F(\ldots, x_{i-1}, \mathrm{False}, x_{i+1}, \ldots) \]
restrictSet :: forall a. ItemOrder a => IntMap Bool -> BDD a -> BDD a Source #
Compute \(F|_{\{x_i = v_i\}_i} \).
restrictLaw :: forall a. ItemOrder a => BDD a -> BDD a -> BDD a Source #
Compute generalized cofactor of F with respect to C.
Note that C is the first argument.
Substition / Composition
subst :: forall a. ItemOrder a => Int -> BDD a -> BDD a -> BDD a Source #
subst x N M
computes substitution M[x ↦ N].
Note the order of the arguments. This operation is also known as Composition.
substSet :: forall a. ItemOrder a => IntMap (BDD a) -> BDD a -> BDD a Source #
Simultaneous substitution.
Note that this is not the same as repeated application of subst
.
Satisfiability
anySatComplete :: ItemOrder a => IntSet -> BDD a -> Maybe (IntMap Bool) Source #
Find one satisfying (complete) assignment over a given set of variables
The set of variables must be a superset of support
.
allSatComplete :: ItemOrder a => IntSet -> BDD a -> [IntMap Bool] Source #
Enumerate all satisfying (complete) assignment over a given set of variables
The set of variables must be a superset of support
.
countSat :: forall a b. (ItemOrder a, Num b, Bits b, HasCallStack) => IntSet -> BDD a -> b Source #
Count the number of satisfying (complete) assignment over a given set of variables.
The set of variables must be a superset of support
.
It is polymorphic in return type, but it is recommended to use Integer
or Natural
because the size can be larger than fixed integer types such as Int64
.
>>>
countSat (IntSet.fromList [1..128]) (true :: BDD AscOrder)
340282366920938463463374607431768211456>>>
import Data.Int
>>>
maxBound :: Int64
9223372036854775807
uniformSatM :: forall a g m. (ItemOrder a, StatefulGen g m, HasCallStack) => IntSet -> BDD a -> g -> m (IntMap Bool) Source #
Sample an assignment from uniform distribution over complete satisfiable assignments (allSatComplete
) of the BDD.
The function constructs a table internally and the table is shared across
multiple use of the resulting action (m IntSet
).
Therefore, the code
let g = uniformSatM xs bdd gen s1 <- g s2 <- g
is more efficient than
s1 <- uniformSatM xs bdd gen s2 <- uniformSatM xs bdd gen
.
(Co)algebraic structure
Signature functor of binary decision trees, BDD, and ZDD.
Instances
Fold
Unfold
unfoldHashable :: forall a b. (ItemOrder a, Eq b, Hashable b) => (b -> Sig b) -> b -> BDD a Source #
Top-down construction of BDD, memoising internal states using Hashable
instance.
unfoldOrd :: forall a b. (ItemOrder a, Ord b) => (b -> Sig b) -> b -> BDD a Source #
Top-down construction of BDD, memoising internal states using Ord
instance.
Fixpoints
lfp :: ItemOrder a => (BDD a -> BDD a) -> BDD a Source #
Least fixed point.
Monotonicity of the operator is assumed but not checked.
gfp :: ItemOrder a => (BDD a -> BDD a) -> BDD a Source #
Greatest fixed point.
Monotonicity of the operator is assumed but not checked.
Conversion from/to graphs
type Graph f = IntMap (f Int) Source #
Graph where nodes are decorated using a functor f
.
The occurrences of the parameter of f
represent out-going edges.
toGraph :: BDD a -> (Graph Sig, Int) Source #
Convert a BDD into a pointed graph
Nodes 0
and 1
are reserved for SLeaf False
and SLeaf True
even if they are not actually used. Therefore the result may be
larger than numNodes
if the leaf nodes are not used.
toGraph' :: Traversable t => t (BDD a) -> (Graph Sig, t Int) Source #
Convert multiple BDDs into a graph
fromGraph' :: HasCallStack => Graph Sig -> IntMap (BDD a) Source #
Convert nodes of a graph into BDDs