Problem: bracket_matching

Your task is to implement a linear scan algorithm for Bracket Matching and verify its correctness in Dafny. The algorithm determines whether a sequence of characters $s$ (represented as integers) 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 '(' and decrement $c$ by $1$ for a closed bracket ')'. (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.

// <preamble>
// Assigns a mathematical weight to characters:
// '(' (ASCII 40) adds 1 (opening a scope)
// ')' (ASCII 41) subtracts 1 (closing a scope)
// All other characters are neutral (0).
function char_weight(c: int): int {
    if c == 40 then      // ASCII for '('
        1
    else if c == 41 then // ASCII for ')'
        -1
    else
        0
}

// Recursively calculates the total balance of the sequence.
// A balanced string must result in a total weight of 0.
function total_weight(s: seq<int>): int
    decreases |s|
{
    if |s| == 0 then
        0
    else
        char_weight(s[0]) + total_weight(s[1..])
}

// Checks the Prefix Property:
// At no point in the string (index i) should the closing brackets exceed opening brackets.
// If this is violated (weight < 0), the string is invalid immediately.
predicate valid_prefix_weights(s: seq<int>) {
    forall i :: 0 <= i <= |s| ==> total_weight(s[0..i]) >= 0
}

// The high-level correctness predicate for bracket matching.
predicate is_matched(s: seq<int>) {
    && total_weight(s) == 0       // Must end with closed pairs
    && valid_prefix_weights(s)    // Must never close a non-existent pair
}
// </preamble>

// <helpers>

// </helpers>

// <proofs>

// </proofs>

// <spec>
method bracket_match(s: seq<int>) returns (res: bool)
    requires
        // CONSTRAINT: Hard limit of 1,000,000 ensures no integer overflow
        // when counting or indexing.
        |s| <= 1000000
    ensures
        res == is_matched(s)
// </spec>
// <code>
{
    // TODO: Implement and verify the bracket matching logic here.
}
// </code>