Weighted markings and materialized state tables of a binary tree automaton
DefinitionBinaryTreeAutomata_WeightedMarkingsautomatacomputational-complexity
A marking makes an independent Boolean choice at every leaf. Its weight is the ordered product of the leaf weights in a semiring. At each node the materialized state table aggregates the products of the two child entries over transitions to each state.
Definition code
import Mathlib
import Definitions.Def_BinaryTreeAutomata_ExplicitAutomata
namespace BinaryTreeAutomata
namespace Term
variable {L B : Type}
/-- Independent Boolean choices at the leaf occurrences of a fixed tree shape. -/
def Marking : Term L B → Type
| .leaf _ => Bool
| .node _ l r => l.Marking × r.Marking
instance markingFintype : (t : Term L B) → Fintype t.Marking
| .leaf _ => inferInstanceAs (Fintype Bool)
| .node _ l r => by
letI := markingFintype l
letI := markingFintype r
exact inferInstanceAs (Fintype (l.Marking × r.Marking))
def marked : (t : Term L B) → t.Marking → Term (L × Bool) B
| .leaf a, v => .leaf (a,v)
| .node b l r, v => .node b (l.marked v.1) (r.marked v.2)
def markingWeight {R : Type} [Mul R] (w : L → Bool → R) : (t : Term L B) → t.Marking → R
| .leaf a, v => w a v
| .node _ l r, v => markingWeight w l v.1 * markingWeight w r v.2
/-- Primary and secondary scores add independently along a marking. -/
def scores (w : L → Bool → ℤ × ℕ) : (t : Term L B) → t.Marking → ℤ × ℕ
| .leaf a,z => w a z
| .node _ l r,z => scores w l z.1 + scores w r z.2
end Term
namespace Automaton
variable {L B R : Type} [Semiring R]
/-- A materialized row for every state. Each child table is built once before the parent
sums over pairs of child states. The enumeration may contain duplicate indices. -/
def weightTable (A : Automaton (L × Bool) B) (w : L → Bool → R) : Term L B → List R
| .leaf a => A.states.map fun q =>
(if A.atLeaf (a,false) = q then w a false else 0) +
(if A.atLeaf (a,true) = q then w a true else 0)
| .node b l r =>
let left := weightTable A w l
let right := weightTable A w r
A.states.map fun q => ∑ x ∈ A.states.toFinset, ∑ y ∈ A.states.toFinset,
if A.atNode b x y = q then
(left[A.states.idxOf x]?).getD 0 * (right[A.states.idxOf y]?).getD 0 else 0
def weightedTotal (A : Automaton (L × Bool) B) (w : L → Bool → R) (t : Term L B) : R :=
let table := A.weightTable w t
∑ q ∈ A.states.toFinset, if A.accept q then (table[A.states.idxOf q]?).getD 0 else 0
end Automaton
end BinaryTreeAutomata
Source
Auxiliary constructions for Chakraborty, Ordyniak and Siebertz, Model Checking for Metric Extensions of Monadic Second-Order Logic, arXiv:2610.41974, Theorem 2.8, pp. 16-17.