The semiring of signed minima, optimal multiplicities and secondary keys
DefinitionBinaryTreeAutomata_Optimumautomatacomputational-complexity
Infeasibility is the additive zero. A feasible triple records the minimum signed cost, its multiplicity and its largest secondary key. Addition compares costs and handles ties. Multiplication adds costs and keys and multiplies multiplicities.
Definition code
import Mathlib
namespace BinaryTreeAutomata
/-- A feasible minimum, its multiplicity, and the largest secondary key among all minimizers.
Secondary keys add when independent choices are joined. -/
inductive Optimum
| impossible
| value (cost : ℤ) (ways key : ℕ)
deriving Repr, DecidableEq
namespace Optimum
def choose : Optimum → Optimum → Optimum
| .impossible, y => y
| x, .impossible => x
| .value c n a, .value d m b =>
if c < d then .value c n a else if d < c then .value d m b
else .value c (n+m) (max a b)
def combine : Optimum → Optimum → Optimum
| .value c n a, .value d m b => .value (c+d) (n*m) (a+b)
| _, _ => .impossible
private theorem choose_comm (x y : Optimum) : choose x y = choose y x := by
cases x <;> cases y <;> simp [choose] <;> split_ifs <;> simp_all <;> omega
private theorem choose_impossible (x : Optimum) : choose x .impossible = x := by cases x <;> rfl
private theorem choose_lt {c d : ℤ} (h : c < d) (n a m b : ℕ) :
choose (.value c n a) (.value d m b) = .value c n a := by simp [choose,h]
private theorem choose_gt {c d : ℤ} (h : d < c) (n a m b : ℕ) :
choose (.value c n a) (.value d m b) = .value d m b := by simp [choose,h,not_lt_of_gt h]
private theorem choose_eq (c : ℤ) (n a m b : ℕ) :
choose (.value c n a) (.value c m b) = .value c (n+m) (max a b) := by simp [choose]
private theorem choose_assoc (x y z : Optimum) : choose (choose x y) z = choose x (choose y z) := by
cases x with
| impossible => rfl
| value c n a =>
cases y with
| impossible => cases z <;> rfl
| value d m b =>
cases z with
| impossible => rw [choose_impossible,choose_impossible]
| value e p k =>
rcases lt_trichotomy c d with h₁|h₁|h₁ <;> rcases lt_trichotomy d e with h₂|h₂|h₂ <;>
rcases lt_trichotomy c e with h₃|h₃|h₃ <;> subst_vars <;> first | omega | (simp (disch := omega) only [choose_eq,choose_lt,choose_gt,Nat.add_assoc,max_assoc])
private theorem combine_comm (x y : Optimum) : combine x y = combine y x := by
cases x <;> cases y <;> simp [combine, add_comm, mul_comm]
private theorem distribute (x y z : Optimum) :
combine x (choose y z) = choose (combine x y) (combine x z) := by
cases x with
| impossible => cases y <;> cases z <;> rfl
| value c n a =>
cases y with
| impossible => rfl
| value d m b =>
cases z with
| impossible => rfl
| value e p k =>
rcases lt_trichotomy d e with h|rfl|h <;>
simp (disch := omega) only [choose_eq,choose_lt,choose_gt,combine,Nat.mul_add, Nat.add_max_add_left]
instance : Zero Optimum := ⟨.impossible⟩
instance : One Optimum := ⟨.value 0 1 0⟩
instance : Add Optimum := ⟨choose⟩
instance : Mul Optimum := ⟨combine⟩
instance : CommSemiring Optimum where
zero_add _ := rfl
add_zero x := by cases x <;> rfl
add_assoc := choose_assoc
add_comm := choose_comm
zero_mul x := by cases x <;> rfl
mul_zero x := by cases x <;> rfl
one_mul x := by change combine (.value 0 1 0) x = x; cases x <;> simp [combine]
mul_one x := by change combine x (.value 0 1 0) = x; cases x <;> simp [combine]
mul_comm := combine_comm
mul_assoc x y z := by
change combine (combine x y) z = combine x (combine y z)
cases x <;> cases y <;> cases z <;> simp [combine,add_assoc,mul_assoc]
left_distrib := distribute
right_distrib x y z := by
change combine (choose x y) z = choose (combine x z) (combine y z)
rw [combine_comm, distribute, combine_comm z x, combine_comm z y]
nsmul := nsmulRec
npow := npowRec
end Optimum
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.