A circuit is a standard one of among many ways of representing a propositional logic formula. This module provides a flexible circuit type class and various representations that admit efficient conversion to funsat CNF.
The implementation for this module was adapted from http://okmij.org/ftp/Haskell/DSLSharing.hs.
- class Circuit repr where
- true :: (Ord var, Show var) => repr var
- false :: (Ord var, Show var) => repr var
- input :: (Ord var, Show var) => var -> repr var
- not :: (Ord var, Show var) => repr var -> repr var
- and :: (Ord var, Show var) => repr var -> repr var -> repr var
- or :: (Ord var, Show var) => repr var -> repr var -> repr var
- ite :: (Ord var, Show var) => repr var -> repr var -> repr var -> repr var
- onlyif :: (Ord var, Show var) => repr var -> repr var -> repr var
- iff :: (Ord var, Show var) => repr var -> repr var -> repr var
- xor :: (Ord var, Show var) => repr var -> repr var -> repr var
- class CastCircuit c where
- castCircuit :: (Circuit cOut, Ord var, Show var) => c var -> cOut var
- newtype Shared v = Shared {}
- data FrozenShared v = FrozenShared !CCode !(CMaps v)
- runShared :: Shared v -> FrozenShared v
- type CircuitHash = Int
- falseHash :: CircuitHash
- trueHash :: CircuitHash
- data CCode
- data CMaps v = CMaps {
- hashCount :: [CircuitHash]
- varMap :: Bimap CircuitHash v
- andMap :: Bimap CircuitHash (CCode, CCode)
- orMap :: Bimap CircuitHash (CCode, CCode)
- notMap :: Bimap CircuitHash CCode
- xorMap :: Bimap CircuitHash (CCode, CCode)
- onlyifMap :: Bimap CircuitHash (CCode, CCode)
- iffMap :: Bimap CircuitHash (CCode, CCode)
- iteMap :: Bimap CircuitHash (CCode, CCode, CCode)
- emptyCMaps :: CMaps v
- data Tree v
- foldTree :: (t -> v -> t) -> t -> Tree v -> t
- simplifyTree :: Tree v -> Tree v
- data Graph v
- runGraph :: DynGraph gr => Graph v -> gr (NodeType v) EdgeType
- shareGraph :: (DynGraph gr, Eq v, Show v) => FrozenShared v -> gr (FrozenShared v) (FrozenShared v)
- data NodeType v
- data EdgeType
- type BEnv v = Map v Bool
- newtype Eval v = Eval {}
- runEval :: BEnv v -> Eval v -> Bool
- data CircuitProblem v = CircuitProblem {}
- toCNF :: (Ord v, Show v) => FrozenShared v -> CircuitProblem v
- projectCircuitSolution :: Ord v => Solution -> CircuitProblem v -> BEnv v
Circuit type class
class Circuit repr whereSource
A class representing a grammar for logical circuits. Default implemenations are indicated.
true :: (Ord var, Show var) => repr varSource
false :: (Ord var, Show var) => repr varSource
input :: (Ord var, Show var) => var -> repr varSource
not :: (Ord var, Show var) => repr var -> repr varSource
and :: (Ord var, Show var) => repr var -> repr var -> repr varSource
or :: (Ord var, Show var) => repr var -> repr var -> repr varSource
ite :: (Ord var, Show var) => repr var -> repr var -> repr var -> repr varSource
If-then-else circuit. ite c t e
returns a circuit that evaluates to
t
when c
evaluates to true, and e
otherwise.
onlyif :: (Ord var, Show var) => repr var -> repr var -> repr varSource
iff :: (Ord var, Show var) => repr var -> repr var -> repr varSource
xor :: (Ord var, Show var) => repr var -> repr var -> repr varSource
class CastCircuit c whereSource
Instances of CastCircuit
admit converting one circuit representation to
another.
castCircuit :: (Circuit cOut, Ord var, Show var) => c var -> cOut varSource
Explicit sharing circuit
A Circuit
constructed using common-subexpression elimination. This is a
compact representation that facilitates converting to CNF. See runShared
.
data FrozenShared v Source
A shared circuit that has already been constructed.
FrozenShared !CCode !(CMaps v) |
runShared :: Shared v -> FrozenShared vSource
Reify a sharing circuit.
type CircuitHash = IntSource
0 is false, 1 is true. Any positive value labels a logical circuit node.
A CCode
represents a circuit element for Shared
circuits. A CCode
is
a flattened tree node which has been assigned a unique number in the
corresponding map inside CMaps
, which indicates children, if any.
For example, CAnd i
has the two children of the tuple lookup i (andMap
cmaps)
assuming cmaps :: CMaps v
.
Maps used to implement the common-subexpression sharing implementation of
the Circuit
class. See Shared
.
CMaps | |
|
emptyCMaps :: CMaps vSource
Explicit tree circuit
Explicit tree representation, which is a generic description of a circuit. This representation enables a conversion operation to any other type of circuit. Trees evaluate from variable values at the leaves to the root.
Circuit simplification
simplifyTree :: Tree v -> Tree vSource
Performs obvious constant propagations.
Explicit graph circuit
A circuit type that constructs a Graph
representation. This is useful
for visualising circuits, for example using the graphviz
package.
shareGraph :: (DynGraph gr, Eq v, Show v) => FrozenShared v -> gr (FrozenShared v) (FrozenShared v)Source
Node type labels for graphs.
Circuit evaluator
A circuit evaluator, that is, a circuit represented as a function from variable values to booleans.
Convert circuit to CNF
data CircuitProblem v Source
A circuit problem packages up the CNF corresponding to a given
FrozenShared
circuit, and the mapping between the variables in the CNF and
the circuit elements of the circuit.
toCNF :: (Ord v, Show v) => FrozenShared v -> CircuitProblem vSource
Produces a CNF formula that is satisfiable if and only if the input circuit is satisfiable. Note that it does not produce an equivalent CNF formula. It is not equivalent in general because the transformation introduces variables into the CNF which were not present as circuit inputs. (Variables in the CNF correspond to circuit elements.) Returns equisatisfiable CNF along with the frozen input circuit, and the mapping between the variables of the CNF and the circuit elements.
The implementation uses the Tseitin transformation, to guarantee that the output CNF formula is linear in the size of the circuit. Contrast this with the naive DeMorgan-laws transformation which produces an exponential-size CNF formula.
projectCircuitSolution :: Ord v => Solution -> CircuitProblem v -> BEnv vSource
Projects a funsat Solution
back into the original circuit space,
returning a boolean environment containing an assignment of all circuit
inputs to true and false.