Documentation

Complexitylib.Languages.Balanced

{x : #false x = #true x}: equal-count / "balanced" language #

The balanced language contains every string with as many falses as trues — any interleaving, not just the blocked form 0ⁿ 1ⁿ. The TM generalizes anbnTM by tracking which bit is currently in excess in the control state, while using the single work tape as a unary counter for the size of the excess.

Invariant: after consuming a prefix p, the work head position equals |#false(p) - #true(p)|, and the control state records whether #false > #true (scanExcess0), #true > #false (scanExcess1), or equality (either state with head at cell 0).

Emptiness (h = 0) is detected structurally by the work head reading at cell 0 — the same trick as in anbnTM.

Main definitions #

Main results #

Control states of balancedTM.

  • start : initial state. Moves all heads from cell 0 (▷) to cell 1.
  • initWork : rewinds the work head back to cell 0.
  • scanExcess0 : scanning; the stack (unary counter) holds #false - #true. h = 0 (work reads ) means equality so far.
  • scanExcess1 : scanning; the stack holds #true - #false.
  • done : halted.
Instances For
    @[implicit_reducible]
    Equations
    • One or more equations did not get rendered due to their size.

    Push-down TM deciding the balanced language. One work tape is used as a unary counter. The sign (which bit is in excess) is encoded in the control state; the stack height is the absolute difference.

    Equations
    • One or more equations did not get rendered due to their size.
    Instances For

      The output that balancedTM produces when run from scan state s with stack height h and remaining input rest. Structurally recursive on rest.

      Equations
      Instances For
        structure Complexity.TM.BalancedScanInv (c : Cfg 1 balancedTM.Q) (x : List Bool) (k h : ) :

        Invariants for a configuration during the scan phase. Identical shape to AnBn.BalancedScanInv: the input is a suffix of x.map Γ.ofBool starting at position k + 1, the work head equals the stack height h, and the output tape is pristine at head position 1.

        Instances For

          balancedTM halts in |x| + 3 steps on every input, writing the correct answer (Γ.one iff x.count false = x.count true, else Γ.zero) to output cell 1.

          The balanced language — strings with equal numbers of falses and trues. Generalizes anbn to arbitrary interleavings.

          Equations
          Instances For

            balanced ∈ DTIME(n + 3).