{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 #
TM.balancedTM— 5-state 1-work-tape push-down machine.Language.balanced—{x | x.count false = x.count true}.
Main results #
TM.balancedTM_reachesIn— halts in|x| + 3steps on every input.balanced_in_DTIME,balanced_mem_P.
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.
- start : BalancedPhase
- initWork : BalancedPhase
- scanExcess0 : BalancedPhase
- scanExcess1 : BalancedPhase
- done : BalancedPhase
Instances For
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
- Complexity.TM.balancedExpected Complexity.TM.BalancedPhase.scanExcess0 0 [] = Complexity.Γw.one
- Complexity.TM.balancedExpected Complexity.TM.BalancedPhase.scanExcess0 n.succ [] = Complexity.Γw.zero
- Complexity.TM.balancedExpected Complexity.TM.BalancedPhase.scanExcess1 0 [] = Complexity.Γw.one
- Complexity.TM.balancedExpected Complexity.TM.BalancedPhase.scanExcess1 n.succ [] = Complexity.Γw.zero
- Complexity.TM.balancedExpected Complexity.TM.BalancedPhase.scanExcess0 x✝ (false :: rest) = Complexity.TM.balancedExpected Complexity.TM.BalancedPhase.scanExcess0 (x✝ + 1) rest
- Complexity.TM.balancedExpected Complexity.TM.BalancedPhase.scanExcess0 0 (true :: rest) = Complexity.TM.balancedExpected Complexity.TM.BalancedPhase.scanExcess1 1 rest
- Complexity.TM.balancedExpected Complexity.TM.BalancedPhase.scanExcess0 h.succ (true :: rest) = Complexity.TM.balancedExpected Complexity.TM.BalancedPhase.scanExcess0 h rest
- Complexity.TM.balancedExpected Complexity.TM.BalancedPhase.scanExcess1 x✝ (true :: rest) = Complexity.TM.balancedExpected Complexity.TM.BalancedPhase.scanExcess1 (x✝ + 1) rest
- Complexity.TM.balancedExpected Complexity.TM.BalancedPhase.scanExcess1 0 (false :: rest) = Complexity.TM.balancedExpected Complexity.TM.BalancedPhase.scanExcess0 1 rest
- Complexity.TM.balancedExpected Complexity.TM.BalancedPhase.scanExcess1 h.succ (false :: rest) = Complexity.TM.balancedExpected Complexity.TM.BalancedPhase.scanExcess1 h rest
- Complexity.TM.balancedExpected Complexity.TM.BalancedPhase.start x✝¹ x✝ = Complexity.Γw.blank
- Complexity.TM.balancedExpected Complexity.TM.BalancedPhase.initWork x✝¹ x✝ = Complexity.Γw.blank
- Complexity.TM.balancedExpected Complexity.TM.BalancedPhase.done x✝¹ x✝ = Complexity.Γw.blank
Instances For
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
balancedTM decides Language.balanced in time |x| + 3.
balanced ∈ DTIME(n + 3).