drop table if exists statemachine; create table statemachine ( state integer not null, value integer not null, newstate integer not null, newvalue integer not null, direction integer not null, primary key (state,value) ); # 2 state busy beaver #insert into statemachine values (0,0,1,1,1), (0,1,1,1,-1), (1,0,0,1,-1), (1,1,-1,1,0); # 3 state busy beaver insert into statemachine values (0,0,1,1,1), (0,1,2,1,-1), (1,0,0,1,-1), (1,1,1,1,1), (2,0,1,1,-1), (2,1,-1,1,0); drop table if exists tape; create table tape ( location integer not null, type enum ('tape','state','finger') not null default 'tape', value integer not null, primary key (type,location) ); insert into tape values (0,'state',0), (0,'finger',0), (0,'tape',0); drop procedure if exists TURING; delimiter | CREATE PROCEDURE TURING() BEGIN SELECT value INTO @state FROM tape WHERE type = 'state'; SELECT value INTO @finger FROM tape WHERE type = 'finger'; SET @value = 0; SELECT value INTO @value FROM tape WHERE type = 'tape' AND location = @finger; REPLACE INTO tape SELECT 0 AS location, 'state' AS type, newstate AS value FROM statemachine WHERE state = @state AND value = @value UNION SELECT 0 AS location, 'finger' AS type, @finger+direction AS value FROM statemachine WHERE state = @state AND value = @value UNION SELECT @finger AS location, 'tape' AS type, newvalue AS value FROM statemachine WHERE state = @state AND value = @value; END; | delimiter ;