/* Turing Completeness: Loops and State Management */ /* Demonstrates: Indefinite iteration, state mutation, complex control flow */ ..out "=== Loops and State Management Test ==="; /* Test 1: Counter with state - simulates while loop */ counter_state : {count: 0, target: 5, result: {}}; /* Function to increment counter and collect results */ increment : state -> when state.count < state.target is true then { count: state.count + 1, target: state.target, result: t.append state.result state.count } false then state; /* Simulate loop by repeated application */ step1 : increment counter_state; step2 : increment step1; step3 : increment step2; step4 : increment step3; step5 : increment step4; final_state : increment step5; ..assert final_state.count = 5; ..assert final_state.result = {1: 0, 2: 1, 3: 2, 4: 3, 5: 4}; ..out "1. Counter/Loop simulation: PASS"; /* Test 2: Fibonacci sequence with state */ fib_state : {a: 0, b: 1, sequence: {1: 0, 2: 1}, count: 2, limit: 8}; fib_step : state -> when state.count < state.limit is true then { a: state.b, b: state.a + state.b, sequence: t.append state.sequence (state.a + state.b), count: state.count + 1, limit: state.limit } false then state; /* Generate Fibonacci sequence */ f1 : fib_step fib_state; f2 : fib_step f1; f3 : fib_step f2; f4 : fib_step f3; f5 : fib_step f4; f6 : fib_step f5; final_fib : fib_step f6; ..assert final_fib.sequence = {1: 0, 2: 1, 3: 1, 4: 2, 5: 3, 6: 5, 7: 8, 8: 13}; ..out "2. Fibonacci with state: PASS"; /* Test 3: Game of Life cell simulation */ cell_state : {alive: true, neighbors: 3, generation: 0}; life_rule : state -> when state.neighbors is 2 then {alive: state.alive, neighbors: state.neighbors, generation: state.generation + 1} 3 then {alive: true, neighbors: state.neighbors, generation: state.generation + 1} _ then {alive: false, neighbors: state.neighbors, generation: state.generation + 1}; next_gen1 : life_rule cell_state; next_gen2 : life_rule {alive: next_gen1.alive, neighbors: 1, generation: next_gen1.generation}; ..assert next_gen1.alive = true; ..assert next_gen2.alive = false; ..out "3. Game of Life rules: PASS"; ..out "✅ All loop and state tests passed"; ..out "Demonstrates: Indefinite computation, state evolution, complex control";