Data update
This commit is contained in:
parent
4bb20c9b71
commit
cbaf4c4b64
12390 changed files with 318560 additions and 27248 deletions
|
|
@ -0,0 +1,54 @@
|
|||
private with Ada.Containers.Doubly_Linked_Lists;
|
||||
|
||||
generic
|
||||
type State is (<>); -- State'First is starting state
|
||||
type Symbol is (<>); -- Symbol'First is blank
|
||||
package Turing is
|
||||
|
||||
Start: constant State := State'First;
|
||||
Halt: constant State := State'Last;
|
||||
subtype Action_State is State range Start .. State'Pred(Halt);
|
||||
|
||||
Blank: constant Symbol := Symbol'First;
|
||||
|
||||
type Movement is (Left, Stay, Right);
|
||||
|
||||
type Action is record
|
||||
New_State: State;
|
||||
Move_To: Movement;
|
||||
New_Symbol: Symbol;
|
||||
end record;
|
||||
|
||||
type Rules_Type is array(Action_State, Symbol) of Action;
|
||||
|
||||
type Tape_Type is limited private;
|
||||
|
||||
type Symbol_Map is array(Symbol) of Character;
|
||||
|
||||
function To_String(Tape: Tape_Type; Map: Symbol_Map) return String;
|
||||
function Position_To_String(Tape: Tape_Type; Marker: Character := '^')
|
||||
return String;
|
||||
function To_Tape(Str: String; Map: Symbol_Map) return Tape_Type;
|
||||
|
||||
procedure Single_Step(Current: in out State;
|
||||
Tape: in out Tape_Type;
|
||||
Rules: Rules_Type);
|
||||
|
||||
procedure Run(The_Tape: in out Tape_Type;
|
||||
Rules: Rules_Type;
|
||||
Max_Steps: Natural := Natural'Last;
|
||||
Print: access procedure(Tape: Tape_Type; Current: State));
|
||||
-- runs from Start State until either Halt or # Steps exceeds Max_Steps
|
||||
-- if # of steps exceeds Max_Steps, Constrained_Error is raised;
|
||||
-- if Print is not null, Print is called at the beginning of each step
|
||||
|
||||
private
|
||||
package Symbol_Lists is new Ada.Containers.Doubly_Linked_Lists(Symbol);
|
||||
subtype List is Symbol_Lists.List;
|
||||
|
||||
type Tape_Type is record
|
||||
Left: List;
|
||||
Here: Symbol;
|
||||
Right: List;
|
||||
end record;
|
||||
end Turing;
|
||||
|
|
@ -0,0 +1,98 @@
|
|||
package body Turing is
|
||||
|
||||
function List_To_String(L: List; Map: Symbol_Map) return String is
|
||||
LL: List := L;
|
||||
use type List;
|
||||
begin
|
||||
if L = Symbol_Lists.Empty_List then
|
||||
return "";
|
||||
else
|
||||
LL.Delete_First;
|
||||
return Map(L.First_Element) & List_To_String(LL, Map);
|
||||
end if;
|
||||
end List_To_String;
|
||||
|
||||
function To_String(Tape: Tape_Type; Map: Symbol_Map) return String is
|
||||
|
||||
begin
|
||||
return List_To_String(Tape.Left, Map) & Map(Tape.Here) &
|
||||
List_To_String(Tape.Right, Map);
|
||||
end To_String;
|
||||
|
||||
function Position_To_String(Tape: Tape_Type; Marker: Character := '^')
|
||||
return String is
|
||||
Blank_Map: Symbol_Map := (others => ' ');
|
||||
begin
|
||||
return List_To_String(Tape.Left, Blank_Map) & Marker &
|
||||
List_To_String(Tape.Right, Blank_Map);
|
||||
end Position_To_String;
|
||||
|
||||
function To_Tape(Str: String; Map: Symbol_Map) return Tape_Type is
|
||||
Char_Map: array(Character) of Symbol := (others => Blank);
|
||||
Tape: Tape_Type;
|
||||
begin
|
||||
if Str = "" then
|
||||
Tape.Here := Blank;
|
||||
else
|
||||
for S in Symbol loop
|
||||
Char_Map(Map(S)) := S;
|
||||
end loop;
|
||||
Tape.Here := Char_Map(Str(Str'First));
|
||||
for I in Str'First+1 .. Str'Last loop
|
||||
Tape.Right.Append(Char_Map(Str(I)));
|
||||
end loop;
|
||||
end if;
|
||||
return Tape;
|
||||
end To_Tape;
|
||||
|
||||
procedure Single_Step(Current: in out State;
|
||||
Tape: in out Tape_Type;
|
||||
Rules: Rules_Type) is
|
||||
Act: Action := Rules(Current, Tape.Here);
|
||||
use type List; -- needed to compare Tape.Left/Right to the Empty_List
|
||||
begin
|
||||
Current := Act.New_State; -- 1. update State
|
||||
Tape.Here := Act.New_Symbol; -- 2. write Symbol to Tape
|
||||
case Act.Move_To is -- 3. move Tape to the Left/Right or Stay
|
||||
when Left =>
|
||||
Tape.Right.Prepend(Tape.Here);
|
||||
if Tape.Left /= Symbol_Lists.Empty_List then
|
||||
Tape.Here := Tape.Left.Last_Element;
|
||||
Tape.Left.Delete_Last;
|
||||
else
|
||||
Tape.Here := Blank;
|
||||
end if;
|
||||
when Stay =>
|
||||
null; -- Stay where you are!
|
||||
when Right =>
|
||||
Tape.Left.Append(Tape.Here);
|
||||
if Tape.Right /= Symbol_Lists.Empty_List then
|
||||
Tape.Here := Tape.Right.First_Element;
|
||||
Tape.Right.Delete_First;
|
||||
else
|
||||
Tape.Here := Blank;
|
||||
end if;
|
||||
end case;
|
||||
end Single_Step;
|
||||
|
||||
procedure Run(The_Tape: in out Tape_Type;
|
||||
Rules: Rules_Type;
|
||||
Max_Steps: Natural := Natural'Last;
|
||||
Print: access procedure (Tape: Tape_Type; Current: State)) is
|
||||
The_State: State := Start;
|
||||
Steps: Natural := 0;
|
||||
begin
|
||||
Steps := 0;
|
||||
while (Steps <= Max_Steps) and (The_State /= Halt) loop
|
||||
if Print /= null then
|
||||
Print(The_Tape, The_State);
|
||||
end if;
|
||||
Steps := Steps + 1;
|
||||
Single_Step(The_State, The_Tape, Rules);
|
||||
end loop;
|
||||
if The_State /= Halt then
|
||||
raise Constraint_Error;
|
||||
end if;
|
||||
end Run;
|
||||
|
||||
end Turing;
|
||||
|
|
@ -0,0 +1,27 @@
|
|||
with Ada.Text_IO, Turing;
|
||||
|
||||
procedure Simple_Incrementer is
|
||||
|
||||
type States is (Start, Stop);
|
||||
type Symbols is (Blank, One);
|
||||
|
||||
package UTM is new Turing(States, Symbols);
|
||||
use UTM;
|
||||
|
||||
Map: Symbol_Map := (One => '1', Blank => '_');
|
||||
|
||||
Rules: Rules_Type :=
|
||||
(Start => (One => (Start, Right, One),
|
||||
Blank => (Stop, Stay, One)));
|
||||
Tape: Tape_Type := To_Tape("111", Map);
|
||||
|
||||
procedure Put_Tape(Tape: Tape_Type; Current: States) is
|
||||
begin
|
||||
Ada.Text_IO.Put_Line(To_String(Tape, Map) & " " & States'Image(Current));
|
||||
Ada.Text_IO.Put_Line(Position_To_String(Tape));
|
||||
end Put_Tape;
|
||||
|
||||
begin
|
||||
Run(Tape, Rules, 20, null); -- don't print the configuration during running
|
||||
Put_Tape(Tape, Stop); -- print the final configuration
|
||||
end Simple_Incrementer;
|
||||
|
|
@ -0,0 +1,31 @@
|
|||
with Ada.Text_IO, Turing;
|
||||
|
||||
procedure Busy_Beaver_3 is
|
||||
|
||||
type States is (A, B, C, Stop);
|
||||
type Symbols is range 0 .. 1;
|
||||
package UTM is new Turing(States, Symbols); use UTM;
|
||||
|
||||
Map: Symbol_Map := (1 => '1', 0 => '0');
|
||||
|
||||
Rules: Rules_Type :=
|
||||
(A => (0 => (New_State => B, Move_To => Right, New_Symbol => 1),
|
||||
1 => (New_State => C, Move_To => Left, New_Symbol => 1)),
|
||||
B => (0 => (New_State => A, Move_To => Left, New_Symbol => 1),
|
||||
1 => (New_State => B, Move_To => Right, New_Symbol => 1)),
|
||||
C => (0 => (New_State => B, Move_To => Left, New_Symbol => 1),
|
||||
1 => (New_State => Stop, Move_To => Stay, New_Symbol => 1)));
|
||||
|
||||
Tape: Tape_Type := To_Tape("", Map);
|
||||
|
||||
procedure Put_Tape(Tape: Tape_Type; Current: States) is
|
||||
begin
|
||||
Ada.Text_IO.Put_Line(To_String(Tape, Map) & " " &
|
||||
States'Image(Current));
|
||||
Ada.Text_IO.Put_Line(Position_To_String(Tape));
|
||||
end Put_Tape;
|
||||
|
||||
begin
|
||||
Run(Tape, Rules, 20, Put_Tape'Access); -- print configuration before each step
|
||||
Put_Tape(Tape, Stop); -- and print the final configuration
|
||||
end Busy_Beaver_3;
|
||||
192
Task/Universal-Turing-machine/Ring/universal-turing-machine.ring
Normal file
192
Task/Universal-Turing-machine/Ring/universal-turing-machine.ring
Normal file
|
|
@ -0,0 +1,192 @@
|
|||
# Author: Gal Zsolt (CalmoSoft)
|
||||
load "guilib.ring"
|
||||
load "stdlib.ring"
|
||||
|
||||
# --- Global Variables ---
|
||||
cTape = "110100"
|
||||
nHeadPos = 1
|
||||
cCurrentState = "q0"
|
||||
nSpeed = 500
|
||||
# Rules format: [State, Read, [NewState, Write, Move]]
|
||||
aRules = [
|
||||
["q0", "1", ["q0", "0", "R"]],
|
||||
["q0", "0", ["q1", "1", "L"]],
|
||||
["q1", "1", ["q1", "1", "R"]],
|
||||
["q1", "0", ["halt", "1", "N"]]
|
||||
]
|
||||
|
||||
# GUI Objects
|
||||
winEdit = NULL
|
||||
txtEdit = NULL
|
||||
|
||||
app = new qApp {
|
||||
win = new qWidget() {
|
||||
setWindowTitle("Turing Machine - CalmoSoft")
|
||||
resize(800, 650)
|
||||
setStyleSheet("QWidget { background-color: #1a1a1a; color: white; font-family: Arial; }")
|
||||
|
||||
lblTape = new qLabel(win) {
|
||||
move(50, 30) resize(700, 120) setAlignment(132)
|
||||
setStyleSheet("border: 3px solid #00ffcc; border-radius: 15px; background: #262626;")
|
||||
setText(updateTapeView())
|
||||
}
|
||||
|
||||
lblSpeed = new qLabel(win) { move(50, 160) setText("Speed (ms):") }
|
||||
|
||||
sldSpeed = new qSlider(win) {
|
||||
move(50, 180) resize(250, 30) setOrientation(1)
|
||||
setRange(50, 2000) setValue(nSpeed)
|
||||
setValueChangedEvent("updateSpeed()")
|
||||
}
|
||||
|
||||
timer = new qTimer(win) { setInterval(nSpeed) setTimeoutEvent("stepTuring()") }
|
||||
|
||||
cmbRules = new qComboBox(win) {
|
||||
move(50, 230) resize(700, 45)
|
||||
# Item size and font increase via StyleSheet
|
||||
setStyleSheet("
|
||||
QComboBox {
|
||||
background-color: #333; color: #00ffcc; border: 1px solid #444; font-size: 16px; padding-left: 10px;
|
||||
}
|
||||
QComboBox QAbstractItemView {
|
||||
background-color: #222; color: #00ffcc; selection-background-color: #444; font-size: 16px;
|
||||
}
|
||||
QComboBox QAbstractItemView::item {
|
||||
min-height: 40px;
|
||||
}
|
||||
")
|
||||
}
|
||||
fillCombo()
|
||||
|
||||
btnStep = new qPushButton(win) {
|
||||
move(50, 300) resize(170, 50) setText("STEP")
|
||||
setStyleSheet("background-color: #00ffcc; color: black; font-weight: bold;")
|
||||
setClickEvent("stepTuring()")
|
||||
}
|
||||
|
||||
btnAuto = new qPushButton(win) {
|
||||
move(230, 300) resize(170, 50) setText("START / STOP")
|
||||
setStyleSheet("background-color: #f1c40f; color: black; font-weight: bold;")
|
||||
setClickEvent("toggleAuto()")
|
||||
}
|
||||
|
||||
btnEdit = new qPushButton(win) {
|
||||
move(410, 300) resize(170, 50) setText("EDIT RULES")
|
||||
setStyleSheet("background-color: #3498db; color: white; font-weight: bold;")
|
||||
setClickEvent("openEditor()")
|
||||
}
|
||||
|
||||
btnReset = new qPushButton(win) {
|
||||
move(590, 300) resize(160, 50) setText("RESET")
|
||||
setStyleSheet("background-color: #e74c3c; color: white;")
|
||||
setClickEvent("resetMachine()")
|
||||
}
|
||||
|
||||
lblStatus = new qLabel(win) {
|
||||
move(50, 400) resize(700, 50) setAlignment(132)
|
||||
setStyleSheet("font-size: 22px; color: #00ffcc; font-weight: bold;")
|
||||
setText("State: " + cCurrentState)
|
||||
}
|
||||
win.show()
|
||||
}
|
||||
exec()
|
||||
}
|
||||
|
||||
func updateTapeView
|
||||
cHTML = "<html><body style='font-family: Courier New; font-size: 40px;'><center>"
|
||||
for i = 1 to len(cTape)
|
||||
char = cTape[i]
|
||||
if i = nHeadPos
|
||||
cHTML += "<span style='color: #00ffcc; background-color: #444; border: 2px solid #00ffcc; padding: 5px;'> " + char + " </span>"
|
||||
else
|
||||
cHTML += "<span style='color: #ffffff; padding: 5px;'> " + char + " </span>"
|
||||
ok
|
||||
next
|
||||
return cHTML + "</center></body></html>"
|
||||
|
||||
func fillCombo
|
||||
cmbRules.clear()
|
||||
for r in aRules
|
||||
cmbRules.addItem("IF "+r[1]+","+r[2]+" -> GO "+r[3][1]+", WRITE "+r[3][2]+", MOVE "+r[3][3], 0)
|
||||
next
|
||||
|
||||
func toggleAuto
|
||||
if timer.isActive() timer.stop() else timer.start() ok
|
||||
|
||||
func updateSpeed
|
||||
nSpeed = sldSpeed.value()
|
||||
timer.setInterval(nSpeed)
|
||||
|
||||
func stepTuring
|
||||
if cCurrentState = "halt" timer.stop() return ok
|
||||
char = "" + cTape[nHeadPos]
|
||||
found = 0
|
||||
for nIdx = 1 to len(aRules)
|
||||
r = aRules[nIdx]
|
||||
if r[1] = cCurrentState AND r[2] = char
|
||||
found = 1
|
||||
cmbRules.setCurrentIndex(nIdx-1)
|
||||
cCurrentState = r[3][1]
|
||||
cTape[nHeadPos] = "" + r[3][2]
|
||||
if r[3][3] = "R" nHeadPos++ elseif r[3][3] = "L" nHeadPos-- ok
|
||||
exit
|
||||
ok
|
||||
next
|
||||
# Infinite tape handling
|
||||
if nHeadPos < 1
|
||||
cTape = "0" + cTape nHeadPos = 1
|
||||
elseif nHeadPos > len(cTape)
|
||||
cTape = cTape + "0"
|
||||
ok
|
||||
|
||||
if found = 0 or cCurrentState = "halt"
|
||||
cCurrentState = "halt" timer.stop()
|
||||
lblStatus.setText("<span style='color:#ff4444;'>FINISHED: HALTED</span>")
|
||||
else
|
||||
lblStatus.setText("State: " + cCurrentState + " | Position: " + nHeadPos)
|
||||
ok
|
||||
lblTape.setText(updateTapeView())
|
||||
|
||||
func openEditor
|
||||
winEdit = new qWidget() {
|
||||
setWindowTitle("Rule Editor")
|
||||
resize(450, 450)
|
||||
setStyleSheet("background-color: #222; color: white;")
|
||||
txtEdit = new qTextEdit(winEdit) {
|
||||
move(10, 10) resize(430, 340)
|
||||
setStyleSheet("background-color: #333; color: white; font-family: monospace; font-size: 14px;")
|
||||
cContent = ""
|
||||
for r in aRules
|
||||
cContent += r[1] + "," + r[2] + "," + r[3][1] + "," + r[3][2] + "," + r[3][3] + nl
|
||||
next
|
||||
setText(cContent)
|
||||
}
|
||||
btnSave = new qPushButton(winEdit) {
|
||||
move(10, 370) resize(430, 50) setText("SAVE & APPLY")
|
||||
setStyleSheet("background-color: #00ffcc; color: black; font-weight: bold;")
|
||||
setClickEvent("saveRules()")
|
||||
}
|
||||
winEdit.show()
|
||||
}
|
||||
|
||||
func saveRules
|
||||
cRaw = txtEdit.toPlainText()
|
||||
aLines = split(cRaw, nl)
|
||||
aNewRules = []
|
||||
for line in aLines
|
||||
line = trim(line)
|
||||
if line = "" loop ok
|
||||
parts = split(line, ",")
|
||||
if len(parts) < 5 loop ok
|
||||
add(aNewRules, [ parts[1], parts[2], [parts[3], parts[4], parts[5]] ])
|
||||
next
|
||||
aRules = aNewRules
|
||||
fillCombo()
|
||||
resetMachine()
|
||||
winEdit.close()
|
||||
|
||||
func resetMachine
|
||||
cTape = "110100" nHeadPos = 1 cCurrentState = "q0"
|
||||
timer.stop()
|
||||
lblStatus.setText("State: q0")
|
||||
lblTape.setText(updateTapeView())
|
||||
Loading…
Add table
Add a link
Reference in a new issue