aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--machines/addition/addition.tm7
-rw-r--r--machines/palindrome/palindrome.tm32
-rw-r--r--src/tmachinespkg/tm.nim192
-rw-r--r--src/tmachinespkg/tmlib.nim121
4 files changed, 184 insertions, 168 deletions
diff --git a/machines/addition/addition.tm b/machines/addition/addition.tm
index f2b61f8..a00455b 100644
--- a/machines/addition/addition.tm
+++ b/machines/addition/addition.tm
@@ -1,4 +1,4 @@
-states 14
+states 15
S
0A
0B
@@ -12,7 +12,8 @@ R1
R2
Ca
_1
-_2 +
+_2
+Ac +
alphabet 3 # 0 1
S # S # R
S 0 0A # R
@@ -62,4 +63,4 @@ _1 1 1C _ R
_1 _ _1 _ R
_2 # _2 # R
_2 0 _2 0 R
-_2 1 0D 1 S
+_2 _ Ac _ R
diff --git a/machines/palindrome/palindrome.tm b/machines/palindrome/palindrome.tm
index 692b70d..bd6143a 100644
--- a/machines/palindrome/palindrome.tm
+++ b/machines/palindrome/palindrome.tm
@@ -1,5 +1,5 @@
-states 8
-A +
+states 9
+A
B0
B1
B2
@@ -7,29 +7,31 @@ C0
C1
C2
D
+Ac +
alphabet 3 0 1 2
-A 0 B0 _ R
-A 1 B1 _ R
-A 2 B2 _ R
+A _ Ac _ R
+A 0 B0 _ R
+A 1 B1 _ R
+A 2 B2 _ R
B0 0 B0 0 R
B0 1 B0 1 R
B0 2 B0 2 R
B0 _ C0 _ L
-C0 0 D _ L
-C0 _ A _ S
+C0 0 D _ L
+C0 _ A _ S
B1 0 B1 0 R
B1 1 B1 1 R
B1 2 B1 2 R
B1 _ C1 _ L
-C1 1 D _ L
-C1 _ A _ S
+C1 1 D _ L
+C1 _ A _ S
B2 0 B2 0 R
B2 1 B2 1 R
B2 2 B2 2 R
B2 _ C2 _ L
-C2 2 D _ L
-C2 _ A _ S
-D 0 D 0 L
-D 1 D 1 L
-D 2 D 2 L
-D _ A _ R
+C2 2 D _ L
+C2 _ A _ S
+D 0 D 0 L
+D 1 D 1 L
+D 2 D 2 L
+D _ A _ R
diff --git a/src/tmachinespkg/tm.nim b/src/tmachinespkg/tm.nim
index b909478..c009a60 100644
--- a/src/tmachinespkg/tm.nim
+++ b/src/tmachinespkg/tm.nim
@@ -1,151 +1,48 @@
-import hashes, tables, strutils, sets
+import sets, tables, strutils
-type
- state* = string
- letter* = char
- input* = tuple[state: state, letter: letter]
+import tmlib
-proc `$`(input: input): string =
- "(" & $input[0] & " " & $input[1] & ")"
-
-type Direction* = enum
- Dec = -1,
- None = 0,
- Inc = 1
-
-proc `$`*(this: Direction): string =
- case this
- of Dec:
- return "L"
- of None:
- return "S"
- of Inc:
- return "R"
-
-type transition* = tuple[
- newState: state,
- toWrite: letter,
- moveDir: Direction]
-
-proc `$`(t: transition): string =
- "[" & $t.newState & " " & $t.toWrite & " " & $t.moveDir & "]"
-
-type tape* = Table[int, letter]
-type ttable* = Table[input, transition]
+type description = object
+ initial_state: state
+ transitions: ttable
+ accepting_states: HashSet[state]
type DTM* = ref object
- currentState*: state
acceptingStates*: HashSet[state]
- tape*: tape
- position*: int
transitions*: Table[(state, letter), transition]
-
-type description* = tuple [
- currentState: state,
- acceptingStates: HashSet[state],
- transitions: Table[input, transition]
- ]
-
-# Parses a Direction from a string
-proc toDirection(token: string): Direction =
- case token[0]
- of 'R':
- return Inc
- of 'L':
- return Dec
- of 'S':
- return None
- else:
- return None
+ configuration*: configuration
proc `[]`(this: DTM, i: int): letter =
- if this.tape.hasKey(i):
- return this.tape[i]
- else:
- return '_'
+ return this.configuration[i]
proc `[]=`(this: DTM, i: int, l: letter) =
- this.tape[i] = l
+ this.configuration[i] = l
-# Tape string conversion
proc `$`(this: DTM): string =
- result = ""
- for i in -150..150:
- if i == 0:
- result &= '['
- result &= $this[this.position + i]
- if i == 0:
- result &= ']'
-
-proc write(this: DTM, c: letter): DTM {.discardable.} =
- this[this.position] = c
- return this
-
-## Write to and move DTM
-proc move(this: DTM, move: transition): DTM {.discardable.} =
- # Change state:
- this.currentState = move.newState
- # Write to tape:
- this[this.position] = move.toWrite
- # Move position on tape:
- this.position += cast[int](move.moveDir)
- return this
-
-proc insert(this: var tape, input: string, p = 0): tape {.discardable.} =
- for i, c in input:
- this[i + p] = cast[letter](c)
- return this
-
-# Makes a Tape containing this string
-proc tapeOf(input: string): tape =
- result = initTable[int, letter]()
- result.insert(input)
-
-## Fits this DTM with a Tape containing this string
-proc load(tm: DTM, input: string): DTM =
- return DTM(
- tape: tapeOf(input),
- acceptingStates: tm.acceptingStates,
- currentState: tm.currentState,
- transitions: tm.transitions
- )
-
-## Fits this DTM with a Tape containing this string
-proc `<<`(tm: DTM, input: string): DTM =
- return tm.load(input)
+ return $this.configuration
-## Iterator over every line in a file
-proc lineIterator(f: File): (iterator(): TaintedString {.closure.}) =
- iterator yes(): TaintedString {.closure.} =
- var line: TaintedString
- while readLine(f, line):
- yield line
- return yes
+proc make_configuration*(desc: description, input: string): configuration =
+ return make_configuration(desc.initial_state, input)
-iterator characters(filename: string): char =
- for line in lines filename:
- for letter in line:
- yield letter
-
-## Construct a DTM from an iterator of rules from a description file
+## Construct a TM description from an iterator of rules from a description file
proc parse*(description: string): description =
var noStates = -1
var statesSeen = -1
- var transitions = initTable[input, transition]()
- var acceptingStates = initSet[state]()
+ var transitions = init_table[input, transition]()
+ var acceptingStates = init_set[state]()
var currentState = cast[state]("")
var alphabet = false
- for line in description.splitLines:
+ for line in description.split_lines:
# If the line is empty
if line.len == 0:
continue
# If we haven't seen any states yet
- if noStates < 0:
- noStates = line.splitWhiteSpace()[1].parseInt()
- statesSeen = 0
+ if no_states < 0:
+ no_states = line.split_white_space()[1].parseInt()
+ states_seen = 0
# If we're partway through seeing states
- elif noStates > statesSeen:
- let split = line.splitWhiteSpace()
+ elif no_states > states_seen:
+ let split = line.split_white_space()
# If it's an accepting state
if split.len == 2:
acceptingStates.incl(split[0])
@@ -171,19 +68,12 @@ proc parse*(description: string): description =
moveDir: outd)
transitions[(ins, inl)] = transition
# Next line
- return (
- currentState: currentState,
+ return description(
+ initial_state: currentState,
acceptingStates: acceptingStates,
transitions: transitions
)
-proc makeDTM*(desc: description, input: string): DTM =
- return DTM(
- currentState: desc[0],
- acceptingStates: desc[1],
- transitions: desc.transitions,
- tape: tapeOf(input))
-
proc toFile*(desc: description): string =
var states = initSet[state]()
var alphabet: set[letter] = {}
@@ -200,23 +90,25 @@ proc toFile*(desc: description): string =
result &= " " & $v
result &= "\n"
for k, v in desc.transitions:
- result &= $k.state & " " & $k.letter & " " & $v.newState & " " & $v.toWrite
+ result &= $k.state & " " & $k.letter
+ result &= " " & $v.new_state & " " & $v.to_write
result &= " " & $v.moveDir & "\n"
-## Step through DTM emulation until it would halt
-proc test*(tm: DTM, speak: bool): bool =
- var m: transition
- var i = 0
- try:
- while true:
- i += 1
- if speak:
- echo(tm, " ", tm.currentState, " ", i)
- m = tm.transitions[(tm.currentState, tm[tm.position])]
- tm.move(m)
- except KeyError:
- return tm.acceptingStates.contains(tm.currentState)
+proc makeDTM*(desc: description, input: string): DTM =
+ return DTM(
+ configuration: make_configuration(desc, input),
+ accepting_states: desc.accepting_states,
+ transitions: desc.transitions,
+ )
-## Does this DTM accept this string as input?
-proc `<<?`*(tm: DTM, input: string): bool =
- return tm.load(input).test(false)
+proc test*(this: DTM, verbose: bool = true): bool =
+ while true:
+ let input = this.configuration.get_input()
+ if not this.transitions.has_key(input):
+ return false
+ let trans = this.transitions[input]
+ if trans.new_state in this.accepting_states:
+ return true
+ this.configuration.move(trans)
+ if verbose:
+ echo this.configuration
diff --git a/src/tmachinespkg/tmlib.nim b/src/tmachinespkg/tmlib.nim
new file mode 100644
index 0000000..dfa815f
--- /dev/null
+++ b/src/tmachinespkg/tmlib.nim
@@ -0,0 +1,121 @@
+import hashes, tables, strutils, sets
+
+type
+ state* = string
+ letter* = char
+ input* = tuple[state: state, letter: letter]
+
+proc `$`(input: input): string =
+ "(" & $input[0] & " " & $input[1] & ")"
+
+type Direction* = enum
+ Dec = -1,
+ None = 0,
+ Inc = 1
+
+proc `$`*(this: Direction): string =
+ case this
+ of Dec:
+ return "L"
+ of None:
+ return "S"
+ of Inc:
+ return "R"
+
+type transition* = tuple[
+ newState: state,
+ toWrite: letter,
+ moveDir: Direction]
+
+proc `$`(t: transition): string =
+ "[" & $t.newState & " " & $t.toWrite & " " & $t.moveDir & "]"
+
+type tape* = Table[int, letter]
+type ttable* = Table[input, transition]
+
+type configuration* = object
+ currentState: state
+ tape: tape
+ position: int
+type config = configuration
+
+# Parses a Direction from a string
+proc to_direction*(token: string): Direction =
+ case token[0]
+ of 'R':
+ return Inc
+ of 'L':
+ return Dec
+ of 'S':
+ return None
+ else:
+ return None
+
+proc `[]`*(this: configuration, i: int): letter =
+ if this.tape.hasKey(i):
+ return this.tape[i]
+ else:
+ return '_'
+
+proc `[]=`*(this: var configuration, i: int, l: letter) =
+ this.tape[i] = l
+
+proc get_input*(this: configuration): input =
+ return (this.currentState, this[this.position])
+
+proc move*(this: var config, trans: transition): config {.discardable.} =
+ this.tape[this.position] = trans.toWrite
+ this.position += cast[int](trans.moveDir)
+ this.currentState = trans.newState
+ return this
+
+# Tape string conversion
+proc `$`*(this: configuration): string =
+ result = ""
+ for i in -30..30:
+ if i == 0:
+ result &= '['
+ result &= $this[this.position + i]
+ if i == 0:
+ result &= ']'
+ result &= " " & $this.current_state
+
+proc insert(this: var tape, input: string, p = 0): tape {.discardable.} =
+ for i, c in input:
+ this[i + p] = cast[letter](c)
+ return this
+
+# Makes a Tape containing this string
+proc tapeOf(input: string): tape =
+ result = initTable[int, letter]()
+ result.insert(input)
+
+proc load(
+ this: var configuration,
+ input: string,
+ reset: bool = true
+): configuration =
+ this.tape = tapeOf(input)
+ if reset:
+ this.position = 0
+ return this
+
+## Iterator over every line in a file
+proc lineIterator(f: File): (iterator(): TaintedString {.closure.}) =
+ iterator yes(): TaintedString {.closure.} =
+ var line: TaintedString
+ while readLine(f, line):
+ yield line
+ return yes
+
+iterator characters(filename: string): char =
+ for line in lines filename:
+ for letter in line:
+ yield letter
+
+proc make_configuration*(start: state, input: string): configuration =
+ return configuration(
+ currentState: start,
+ tape: tapeOf(input),
+ position: 0,
+ )