Documentation

CslibFrontier.Computability.BranchingProgram.Nondeterministic

Nondeterministic branching programs #

A nondeterministic branching program over inputs x : ι → α is a directed acyclic multigraph with a source and a target, in which each wire is either unlabelled (a rectifier) or labelled by a test x i = a (a contact). An input switches on the rectifiers and the contacts whose tests it passes, and the program accepts it when some path from the source to the target uses only wires that are switched on. The size of a program is its number of contacts Jukna, Section 1.3. Over Bool the tests are the literals; tests over a larger alphabet are those of Jukna, Section 15.2.

NBP ι α n lists n nodes in a topological order, as BranchingProgram.Nodes does for deterministic programs: the first node is the target, each later node is given by the list of wires leaving it for earlier nodes, and the last node is the source. The number of paths from a node to the target that an input switches on is defined by structural recursion (NBP.paths, NBP.pathCount), so it computes by decide on a concrete program. This one count gives the modes of acceptance: a nondeterministic program accepts when the count is nonzero (NBP.Accepts), and a parity branching program when it is odd (NBP.AcceptsParity).

The recursion for NBP.paths recomputes the counts of earlier nodes at every use. Two compiler replacements avoid this. NBP.pathsVector stores the count of every node in a vector, in topological order, and the compiler uses it for NBP.paths (NBP.paths_eq_pathsImpl). For NBP.pathCount (NBP.pathCount_eq_pathCountImpl), the compiler counts the switched-on wires of the source directly when they all lead to the target, and otherwise pushes the number of paths from the source down the program in an array ending at the highest head of those wires, with a private helper. A node that no path from the source reaches is stepped past without reading its wires, and the push stops below the lowest node other than the target that a path reaches. The kernel definitions, their simplification rules and kernel reduction (decide, rfl) are unchanged. Separate calls of NBP.paths may recompute the vector. Compiler replacements apply to callers compiled with this module imported, not previously compiled callers. This module does not certify a runtime bound.

A deterministic branching program is a nondeterministic one in which every input switches on exactly one way out of each query node: a query node has a contact for each letter and an accepting sink has a rectifier to the target (BranchingProgram.toNBP). Its path count is 1 on the inputs it accepts and 0 on the others (BranchingProgram.pathCount_toNBP), and it has at most Fintype.card α contacts for each node of the deterministic program (BranchingProgram.size_toNBP_le).

Main definitions #

Main results #

References #

structure Cslib.BranchingPrograms.NBP.Wire (ι : Type u_1) (α : Type u_2) (n : ℕ) :
Type (max u_1 u_2)

A wire leaving a node of a nondeterministic branching program for one of the n earlier nodes: a rectifier, or a contact testing one coordinate of the input.

  • head : Fin n

    The earlier node the wire leads to.

  • label : Option (ι × α)

    none for a rectifier, and some (i, a) for a contact testing x i = a.

Instances For
    @[instance_reducible]
    instance Cslib.BranchingPrograms.NBP.instDecidableEqWire {ι✝ : Type u_1} {α✝ : Type u_2} {n✝ : ℕ} [DecidableEq ι✝] [DecidableEq α✝] :
    DecidableEq (Wire ι✝ α✝ n✝)
    def Cslib.BranchingPrograms.NBP.instDecidableEqWire.decEq {ι✝ : Type u_1} {α✝ : Type u_2} {n✝ : ℕ} [DecidableEq ι✝] [DecidableEq α✝] (x✝ x✝¹ : Wire ι✝ α✝ n✝) :
    Decidable (x✝ = x✝¹)
    def Cslib.BranchingPrograms.NBP.Wire.equivProd {ι : Type u_1} {α : Type u_2} {n : ℕ} :
    Wire ι α n ≃ Fin n × Option (ι × α)

    A wire is its head and its label.

    @[instance_reducible]
    instance Cslib.BranchingPrograms.NBP.Wire.instFintype {ι : Type u_1} {α : Type u_2} {n : ℕ} [Fintype ι] [Fintype α] :
    Fintype (Wire ι α n)
    theorem Cslib.BranchingPrograms.NBP.Wire.card {ι : Type u_1} {α : Type u_2} {n : ℕ} [Fintype ι] [Fintype α] :

    There are n * (|ι| |α| + 1) wires to n earlier nodes: a rectifier and a contact for each test, to each node.

    inductive Cslib.BranchingPrograms.NBP (ι : Type u_1) (α : Type u_2) :
    ℕ → Type (max u_1 u_2)

    A nondeterministic branching program over inputs ι → α, with n nodes in topological order: the first node is the target, each later node has a list of wires to earlier nodes, and the last node is the source Jukna, Section 1.3.

    • target {ι : Type u_1} {α : Type u_2} : NBP ι α 1

      The program whose only node is the target.

    • node {ι : Type u_1} {α : Type u_2} {n : ℕ} (P : NBP ι α n) (wires : List (Wire ι α n)) : NBP ι α (n + 1)

      The nodes of P followed by a node with the given wires to them.

    def Cslib.BranchingPrograms.NBP.size {ι : Type u_1} {α : Type u_2} {n : ℕ} :
    NBP ι α n → ℕ

    The size of a program: its number of contacts.

    def Cslib.BranchingPrograms.NBP.Wire.on {ι : Type u_1} {α : Type u_2} [DecidableEq α] {n : ℕ} (w : Wire ι α n) (x : ι → α) :

    Whether the input x switches the wire on: a rectifier always, and a contact when x passes its test.

    def Cslib.BranchingPrograms.NBP.paths {ι : Type u_1} {α : Type u_2} [DecidableEq α] (x : ι → α) {n : ℕ} :
    NBP ι α n → Fin n → ℕ

    The number of paths from each node to the target on which x switches every wire on.

    @[simp]
    theorem Cslib.BranchingPrograms.NBP.paths_node_castSucc {ι : Type u_1} {α : Type u_2} [DecidableEq α] {n : ℕ} (P : NBP ι α n) (wires : List (Wire ι α n)) (x : ι → α) (v : Fin n) :
    paths x (P.node wires) v.castSucc = paths x P v

    An earlier node has the same paths in a longer program.

    @[simp]
    theorem Cslib.BranchingPrograms.NBP.paths_node_last {ι : Type u_1} {α : Type u_2} [DecidableEq α] {n : ℕ} (P : NBP ι α n) (wires : List (Wire ι α n)) (x : ι → α) :
    paths x (P.node wires) (Fin.last n) = (List.map (fun (w : Wire ι α n) => paths x P w.head) (List.filter (fun (x_1 : Wire ι α n) => x_1.on x) wires)).sum

    The paths from the last node go through its wires that are switched on.

    @[simp]
    theorem Cslib.BranchingPrograms.NBP.paths_zero {ι : Type u_1} {α : Type u_2} [DecidableEq α] (x : ι → α) {n : ℕ} (P : NBP ι α (n + 1)) :
    paths x P 0 = 1

    The target has one path to itself, the empty one.

    def Cslib.BranchingPrograms.NBP.pathCount {ι : Type u_1} {α : Type u_2} [DecidableEq α] {n : ℕ} :
    NBP ι α n → (ι → α) → ℕ

    The number of paths from the source to the target on which x switches every wire on.

    def Cslib.BranchingPrograms.NBP.pathsVector {ι : Type u_1} {α : Type u_2} [DecidableEq α] (x : ι → α) {n : ℕ} :
    NBP ι α n → Vector ℕ n

    The number of paths from each node to the target on which x switches every wire on, computed node by node in topological order and stored in a vector.

    @[simp]
    theorem Cslib.BranchingPrograms.NBP.get_pathsVector {ι : Type u_1} {α : Type u_2} [DecidableEq α] (x : ι → α) {n : ℕ} (P : NBP ι α n) :

    The stored counts agree with NBP.paths.

    def Cslib.BranchingPrograms.NBP.pathsImpl {ι : Type u_1} {α : Type u_2} [DecidableEq α] (x : ι → α) {n : ℕ} (P : NBP ι α n) :
    Fin n → ℕ

    Compiler implementation of NBP.paths reading the stored counts.

    def Cslib.BranchingPrograms.NBP.pathCountImpl {ι : Type u_1} {α : Type u_2} [DecidableEq α] {n : ℕ} :
    NBP ι α n → (ι → α) → ℕ

    Compiler implementation of NBP.pathCount. When every switched-on wire of the source leads to the target, it counts those wires; otherwise it pushes the source's paths down the program with the private pathCountFrom, in an array that ends at the highest head of those wires.

    def Cslib.BranchingPrograms.NBP.Accepts {ι : Type u_1} {α : Type u_2} [DecidableEq α] {n : ℕ} (P : NBP ι α n) (x : ι → α) :

    A nondeterministic branching program accepts an input that switches on some path from the source to the target.

    Instances For
      @[instance_reducible]
      instance Cslib.BranchingPrograms.NBP.instDecidableAccepts {ι : Type u_1} {α : Type u_2} [DecidableEq α] {n : ℕ} (P : NBP ι α n) (x : ι → α) :
      def Cslib.BranchingPrograms.NBP.AcceptsParity {ι : Type u_1} {α : Type u_2} [DecidableEq α] {n : ℕ} (P : NBP ι α n) (x : ι → α) :

      A parity branching program accepts an input that switches on an odd number of paths from the source to the target Jukna, Section 1.3.

      Instances For
        @[instance_reducible]
        instance Cslib.BranchingPrograms.NBP.instDecidableAcceptsParity {ι : Type u_1} {α : Type u_2} [DecidableEq α] {n : ℕ} (P : NBP ι α n) (x : ι → α) :
        noncomputable def Cslib.BranchingPrograms.BranchingProgram.Step.wires {ι : Type u_1} {α : Type u_2} [Fintype α] {n : ℕ} :
        Step ι α (Fin n) → List (NBP.Wire ι α (n + 1))

        The wires of a node of a deterministic program: a contact for each letter at a query node, a rectifier to the target at an accepting sink, and no wire at a rejecting sink.

        noncomputable def Cslib.BranchingPrograms.BranchingProgram.Nodes.toNBP {ι : Type u_1} {α : Type u_2} [Fintype α] {n : ℕ} :
        Nodes ι α n → NBP ι α (n + 1)

        A sequence of deterministic nodes as a nondeterministic program: a new target, then the same nodes with the wires Step.wires.

        noncomputable def Cslib.BranchingPrograms.BranchingProgram.toNBP {ι : Type u_1} {α : Type u_2} [Fintype α] (P : BranchingProgram ι α) :
        NBP ι α (P.size + 2)

        A deterministic program as a nondeterministic one: a new source with a rectifier to the start node.

        theorem Cslib.BranchingPrograms.BranchingProgram.Nodes.size_toNBP_le {ι : Type u_1} {α : Type u_2} [Fintype α] {n : ℕ} (P : Nodes ι α n) :

        The nondeterministic program of a sequence of n deterministic nodes has at most Fintype.card α contacts for each node.

        The nondeterministic program of a deterministic one has at most Fintype.card α contacts for each node: its size, counting contacts, is at most Fintype.card α times the size, counting nodes, of the deterministic program.

        theorem Cslib.BranchingPrograms.BranchingProgram.Nodes.paths_toNBP {ι : Type u_1} {α : Type u_2} [Fintype α] [DecidableEq α] (x : ι → α) {n : ℕ} (P : Nodes ι α n) (v : Fin n) :

        From each node, a deterministic program switches on one path to the target when it reaches an accepting sink, and none otherwise.

        theorem Cslib.BranchingPrograms.BranchingProgram.pathCount_toNBP {ι : Type u_1} {α : Type u_2} [Fintype α] [DecidableEq α] (P : BranchingProgram ι α) (x : ι → α) :

        A deterministic branching program is an unambiguous nondeterministic one: it switches on one path from the source to the target on the inputs it accepts, and none on the others.