/* Turing Completeness: Loops and State Management - Compatibility Version */ /* Modified to work without array literals and concat operations */ ..out "=== Loops and State Management Test ==="; /* Test 1: Counter with state - simulates while loop */ /* Using table to simulate array */ counter_state : {count: 0, target: 5, result: {size: 0}}; /* 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: { size: state.result.size + 1, val1: when state.result.size >= 1 then state.result.val1 _ then state.count, val2: when state.result.size >= 2 then state.result.val2 _ then when state.result.size = 1 then state.count _ then null, val3: when state.result.size >= 3 then state.result.val3 _ then when state.result.size = 2 then state.count _ then null, val4: when state.result.size >= 4 then state.result.val4 _ then when state.result.size = 3 then state.count _ then null, val5: when state.result.size >= 5 then state.result.val5 _ then when state.result.size = 4 then state.count _ then null } } 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.size = 5; ..out "1. Counter/Loop simulation: PASS"; /* Test 2: Fibonacci sequence with state */ fib_state : {a: 0, b: 1, count: 2, limit: 8, fib3: 1, fib4: 2, fib5: 3, fib6: 5, fib7: 8, fib8: 13}; fib_step : state -> when state.count < state.limit is true then { a: state.b, b: state.a + state.b, count: state.count + 1, limit: state.limit, fib3: state.fib3, fib4: state.fib4, fib5: state.fib5, fib6: state.fib6, fib7: state.fib7, fib8: state.fib8 } 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.b = 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";