Machine-facing encoding of fan-in-two AND/OR circuits #
This file defines a proof-free, on-tape representation of the library's
Basis.andOr2 circuits. A raw circuit is an ordered list of gates. For an
input arity N, wires 0, ..., N - 1 are primary inputs and gate i produces
wire N + i. Thus topological well-formedness says that both references of
gate i are strictly less than N + i. The last gate is the sole output.
The evaluator is intentionally iterative. It stores each wire value in an
array exactly once, so sharing in the circuit DAG does not cause the recursive
recomputation performed by the proof-oriented Circuit.wireValue definition.
The bit format is deterministic and self-delimiting. Naturals use terminated
unary (n one-bits followed by a zero-bit), a gate stores three fixed bits and
two unary references, and a circuit starts with its unary gate count. Exact
decoding rejects truncation and trailing garbage. Unary references make the
format polynomially long in the input arity and number of gates; a binary
format can be added later without changing the raw circuit semantics.
A proof-free fan-in-two AND/OR gate.
input₀ and input₁ are absolute wire indices. Negation flags are applied
before the AND/OR operation, matching Gate.negated in the typed circuit
model.
- op : AndOrOp
Whether the gate computes AND or OR of its (possibly negated) inputs.
- input₀ : ℕ
Absolute wire index of the first input.
- input₁ : ℕ
Absolute wire index of the second input.
- negated₀ : Bool
Whether the first input value is negated before applying
op. - negated₁ : Bool
Whether the second input value is negated before applying
op.
Instances For
Equations
- One or more equations did not get rendered due to their size.
Instances For
A raw single-output circuit. The output is the value of the last gate.
Instances For
Equations
- g.instDecidableWellFormedAt available = id inferInstance
Boolean checker corresponding to WellFormedAt.
Equations
- g.isWellFormedAt available = decide (g.WellFormedAt available)
Instances For
Evaluate a gate once its two (unnegated) input values are known.
Equations
Instances For
Every gate reference points to a primary input or an earlier gate.
Equations
- Complexity.CircuitCode.RawCircuit.TopologicallyWellFormed N c = ∀ (i : Fin (List.length c)), (List.get c i).WellFormedAt (N + ↑i)
Instances For
A valid single-output raw circuit is nonempty and topologically ordered.
Equations
Instances For
Boolean checker for the explicit well-formedness predicate.
Equations
Instances For
Evaluate gates in order, appending each result to the memo array.
Failure means that a gate contains a forward or out-of-range reference.
Equations
- One or more equations did not get rendered due to their size.
- Complexity.CircuitCode.RawCircuit.evalAux? [] x✝ = some x✝
Instances For
Evaluate a raw circuit on a list of primary-input values.
The empty gate list has no designated output and is rejected.
Equations
Instances For
Terminated unary fields #
Self-delimiting unary: n one-bits followed by a zero terminator.
Equations
Instances For
Parse one terminated-unary prefix, returning the unconsumed suffix.
Equations
- Complexity.CircuitCode.NatCode.decodeAux? [] x✝ = none
- Complexity.CircuitCode.NatCode.decodeAux? (false :: rest) x✝ = some (x✝, rest)
- Complexity.CircuitCode.NatCode.decodeAux? (true :: rest) x✝ = Complexity.CircuitCode.NatCode.decodeAux? rest (x✝ + 1)
Instances For
Parse one terminated-unary prefix.
Equations
Instances For
Gate serialization #
Operation bit: one is AND and zero is OR.
Equations
- g.opBit = match g.op with | Complexity.AndOrOp.and => true | Complexity.AndOrOp.or => false
Instances For
Encode a gate as operation, negation flags, then two unary references.
Equations
Instances For
Circuit serialization #
Parse exactly count gate prefixes and return the remaining suffix.
Equations
Instances For
Serialize a circuit as its unary gate count followed by its gates.
Equations
Instances For
Decode one circuit prefix and return the unconsumed suffix.
Equations
- One or more equations did not get rendered due to their size.
Instances For
Decode exactly one circuit. Any trailing bits are rejected.
Equations
Instances For
Translate a typed fan-in-two gate to the proof-free wire format.
Equations
Instances For
Translate a typed single-output circuit to an ordered raw circuit.
Internal gates retain their order and the typed output gate is appended as the last raw gate.
Equations
- Complexity.CircuitCode.RawCircuit.ofCircuit c = (List.ofFn fun (i : Fin G) => Complexity.CircuitCode.RawGate.ofGate (c.gates i)) ++ [Complexity.CircuitCode.RawGate.ofGate (c.outputs 0)]
Instances For
Serialize a typed fan-in-two circuit as machine-facing bits.
Equations
Instances For
Decode and evaluate a circuit code against an input of exactly N bits.
Equations
- Complexity.CircuitCode.evalCode N code input = if input.length = N then do let circuit ← Complexity.CircuitCode.RawCircuit.decode? code circuit.eval? input else none