Problem: bracket_matching

Your task is to implement a linear scan algorithm for Bracket Matching and verify its correctness in Lean using Mathlib. The algorithm determines whether a sequence of characters $s$ (represented as UInt8 arrays) has perfectly balanced parentheses by maintaining a running balance counter. The operation proceeds by iterating through the string index $i$: (1) Initialization: Start with a balance counter variable $c$ set to $0$. (2) Iteration: Traverse the string character by character. Increment $c$ by $1$ for an open bracket '(' (40) and decrement $c$ by $1$ for a closed bracket ')' (41). (3) Early Failure: If $c$ drops below $0$ at any point, immediately return false. This enforces the constraint that a closing bracket must never appear before its corresponding opening bracket. (4) Final Check: After processing the entire string, return true only if $c$ is exactly $0$. Specifically, you must prove the following: (1) Functional Correctness: The executable boolean result must strictly match the formal definition of a valid bracket sequence, which requires that the sum of weights for the entire string is $0$ and the sum of weights for every prefix is non-negative (where '(' is $1$ and ')' is $-1$). (2) Loop Invariants: You must demonstrate that at every step $i$, the current counter value $c$ equals the mathematical summation of weights of the substring processed so far ($s[0..i]$), and that the non-negative prefix property holds for all prefixes within that range.


import Mathlib

-- Precondition definitions
@[reducible, simp]
def bracket_match_precond (s : Array UInt8) : Prop :=
  -- !benchmark @start precond
  s.size ≤ 1000000
  -- !benchmark @end precond

-- !benchmark @start auxcode
-- !benchmark @end auxcode

-- Main function definition
def bracket_match (s : Array UInt8) (_ : bracket_match_precond s) : Bool :=
  -- !benchmark @start code
  sorry
  -- !benchmark @end code

-- Helper definitions
def char_weight (c : UInt8) : Int :=
  if c == 40 then 1      -- '('
  else if c == 41 then -1 -- ')'
  else 0

-- Calculates total weight of a list of characters
def total_weight (s : List UInt8) : Int :=
  (s.map char_weight).sum

-- Prefix property: all prefixes have non-negative total weight
def valid_prefix_weights (s : List UInt8) : Prop :=
  ∀ i, i ≤ s.length → total_weight (s.take i) ≥ 0

-- Correctness predicate
def is_matched (s : List UInt8) : Prop :=
  total_weight s = 0 ∧ valid_prefix_weights s

-- Postcondition definitions
@[reducible, simp]
def bracket_match_postcond (s : Array UInt8) (result : Bool) (_ : bracket_match_precond s) : Prop :=
  -- !benchmark @start postcond
  result = is_matched s.toList
  -- !benchmark @end postcond

-- !benchmark @start lemma
-- !benchmark @end lemma

-- Proof content
theorem bracket_match_postcond_satisfied
    (s : Array UInt8) (h_precond : bracket_match_precond s) :
    bracket_match_postcond s (bracket_match s h_precond) h_precond := by
  -- !benchmark @start proof
  sorry
  -- !benchmark @end proof