From 2ac50103672df10af130d3b1003980f087bc17dd Mon Sep 17 00:00:00 2001 From: Tom Harley Date: Wed, 4 Jul 2018 21:08:05 +0100 Subject: Add nondeterminism. Add the ability to specify non-deterministic TMs. Listing multiple outcomes for a particular state-input combo will create a non-deterministic split instead of using only the latest one in the list. Deterministic TMs should use a `.dtm` suffix, and non-deterministic TMs should use `.ntm`. TMs with the extension `.tm` will be assumed to be non-deterministic for compatability. --- USAGE.txt | 5 +- machines/addition/addition.dtm | 66 +++++++++++++++++++++++ machines/addition/addition.tm | 66 ----------------------- machines/addition/testgen.sh | 4 +- machines/endsina/endsina.ntm | 9 ++++ machines/endsina/testgen.sh | 18 +++++++ machines/palindrome/palindrome.dtm | 37 +++++++++++++ machines/palindrome/palindrome.tm | 37 ------------- machines/palindrome/testgen.sh | 4 +- machines/simple/simple.dtm | 10 ++++ machines/simple/simple.tm | 10 ---- src/tmachines.nim | 37 +++++++++++-- src/tmachinespkg/ntm.nim | 104 +++++++++++++++++++++++++++++++++++++ src/tmachinespkg/tm.nim | 14 ++--- src/tmachinespkg/tmlib.nim | 39 ++++++++------ tests/test.sh | 4 +- 16 files changed, 317 insertions(+), 147 deletions(-) create mode 100644 machines/addition/addition.dtm delete mode 100644 machines/addition/addition.tm create mode 100644 machines/endsina/endsina.ntm create mode 100755 machines/endsina/testgen.sh create mode 100644 machines/palindrome/palindrome.dtm delete mode 100644 machines/palindrome/palindrome.tm create mode 100644 machines/simple/simple.dtm delete mode 100644 machines/simple/simple.tm create mode 100644 src/tmachinespkg/ntm.nim diff --git a/USAGE.txt b/USAGE.txt index d8fb8e0..bedcaa2 100644 --- a/USAGE.txt +++ b/USAGE.txt @@ -3,5 +3,6 @@ TMachine: a Turing machine emulator. Usage: tmachines [options] Options: - -q --quiet Only output "true" or "false" - -h --help Show this help message. + -q --quiet Only output "true" or "false" + -h --help Show this help message. + -t --type= Specify type of machine (inferred from extension otherwise). diff --git a/machines/addition/addition.dtm b/machines/addition/addition.dtm new file mode 100644 index 0000000..a00455b --- /dev/null +++ b/machines/addition/addition.dtm @@ -0,0 +1,66 @@ +states 15 +S +0A +0B +0C +0D +1A +1B +1C +1D +R1 +R2 +Ca +_1 +_2 +Ac + +alphabet 3 # 0 1 +S # S # R +S 0 0A # R +S 1 1A # R +S _ _1 _ R +0A # 0B _ R +0A 0 0A 0 R +0A 1 0A 1 R +0A _ 0B _ R +0B # 0D # R +0B 0 0C _ R +0B 1 1C _ R +0B _ 0B _ R +0C # 0D # R +0C 0 0C 0 R +0C 1 0C 1 R +0D # 0D # R +0D 0 R1 # L +0D _ R1 _ L +1A # 1B _ R +1A 0 1A 0 R +1A 1 1A 1 R +1A _ 1B _ R +1B # 1D # R +1B 0 1C _ R +1B 1 Ca _ R +1B _ 1B _ R +1C # 1D # R +1C 0 1C 0 R +1C 1 1C 1 R +1D # 1D # R +1D 1 R1 # L +R1 # R1 # L +R1 0 R1 0 L +R1 1 R1 1 L +R1 _ R2 _ L +R2 # S # R +R2 0 R2 0 L +R2 1 R2 1 L +R2 _ R2 _ L +Ca # 0D 1 R +Ca 0 0C 1 R +Ca 1 Ca 0 R +_1 # _2 # R +_1 0 0C _ R +_1 1 1C _ R +_1 _ _1 _ R +_2 # _2 # R +_2 0 _2 0 R +_2 _ Ac _ R diff --git a/machines/addition/addition.tm b/machines/addition/addition.tm deleted file mode 100644 index a00455b..0000000 --- a/machines/addition/addition.tm +++ /dev/null @@ -1,66 +0,0 @@ -states 15 -S -0A -0B -0C -0D -1A -1B -1C -1D -R1 -R2 -Ca -_1 -_2 -Ac + -alphabet 3 # 0 1 -S # S # R -S 0 0A # R -S 1 1A # R -S _ _1 _ R -0A # 0B _ R -0A 0 0A 0 R -0A 1 0A 1 R -0A _ 0B _ R -0B # 0D # R -0B 0 0C _ R -0B 1 1C _ R -0B _ 0B _ R -0C # 0D # R -0C 0 0C 0 R -0C 1 0C 1 R -0D # 0D # R -0D 0 R1 # L -0D _ R1 _ L -1A # 1B _ R -1A 0 1A 0 R -1A 1 1A 1 R -1A _ 1B _ R -1B # 1D # R -1B 0 1C _ R -1B 1 Ca _ R -1B _ 1B _ R -1C # 1D # R -1C 0 1C 0 R -1C 1 1C 1 R -1D # 1D # R -1D 1 R1 # L -R1 # R1 # L -R1 0 R1 0 L -R1 1 R1 1 L -R1 _ R2 _ L -R2 # S # R -R2 0 R2 0 L -R2 1 R2 1 L -R2 _ R2 _ L -Ca # 0D 1 R -Ca 0 0C 1 R -Ca 1 Ca 0 R -_1 # _2 # R -_1 0 0C _ R -_1 1 1C _ R -_1 _ _1 _ R -_2 # _2 # R -_2 0 _2 0 R -_2 _ Ac _ R diff --git a/machines/addition/testgen.sh b/machines/addition/testgen.sh index ba7b6d3..10b4f61 100755 --- a/machines/addition/testgen.sh +++ b/machines/addition/testgen.sh @@ -7,8 +7,8 @@ function dec2bin { k=$1 k=${k:="1"} until [ "$k" -lt 1 ] ; do - a=$(($RANDOM * $RANDOM)) - b=$(($RANDOM * $RANDOM)) + a=$RANDOM + b=$RANDOM c=$(($a + $b)) echo "`dec2bin $a`#`dec2bin $b`#`dec2bin $c` true" let k-=1 diff --git a/machines/endsina/endsina.ntm b/machines/endsina/endsina.ntm new file mode 100644 index 0000000..786313f --- /dev/null +++ b/machines/endsina/endsina.ntm @@ -0,0 +1,9 @@ +states 3 +S +E +A + +alphabet 2 a b +S a S a R +S b S b R +S a E a R +E _ A _ R diff --git a/machines/endsina/testgen.sh b/machines/endsina/testgen.sh new file mode 100755 index 0000000..16c4604 --- /dev/null +++ b/machines/endsina/testgen.sh @@ -0,0 +1,18 @@ +#!/bin/bash + +k=$1 +k=${k:="1"} +until [ "$k" -lt 1 ] ; do + o="" + for i in `seq 1 20` ; do + if [ $(($RANDOM%2)) -lt 1 ] ; then + o="${o}a" + b="true" + else + o="${o}b" + b="false" + fi + done + echo "$o $b" + let k-=1 +done diff --git a/machines/palindrome/palindrome.dtm b/machines/palindrome/palindrome.dtm new file mode 100644 index 0000000..bd6143a --- /dev/null +++ b/machines/palindrome/palindrome.dtm @@ -0,0 +1,37 @@ +states 9 +A +B0 +B1 +B2 +C0 +C1 +C2 +D +Ac + +alphabet 3 0 1 2 +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 +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 +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 diff --git a/machines/palindrome/palindrome.tm b/machines/palindrome/palindrome.tm deleted file mode 100644 index bd6143a..0000000 --- a/machines/palindrome/palindrome.tm +++ /dev/null @@ -1,37 +0,0 @@ -states 9 -A -B0 -B1 -B2 -C0 -C1 -C2 -D -Ac + -alphabet 3 0 1 2 -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 -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 -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 diff --git a/machines/palindrome/testgen.sh b/machines/palindrome/testgen.sh index 926ff9f..d27588d 100755 --- a/machines/palindrome/testgen.sh +++ b/machines/palindrome/testgen.sh @@ -1,8 +1,8 @@ #!/bin/sh function rndm { - for i in {1..100} ; do - echo -n $(($RANDOM % 3 )) + for i in {1..20} ; do + echo -n $(($RANDOM % 3)) done } diff --git a/machines/simple/simple.dtm b/machines/simple/simple.dtm new file mode 100644 index 0000000..da79405 --- /dev/null +++ b/machines/simple/simple.dtm @@ -0,0 +1,10 @@ +states 3 +s0 +s1 +s2 + +alphabet 2 a b +s0 a s0 a R +s0 b s1 b R +s0 _ s2 _ S +s1 b s1 b R +s1 _ s2 _ S diff --git a/machines/simple/simple.tm b/machines/simple/simple.tm deleted file mode 100644 index da79405..0000000 --- a/machines/simple/simple.tm +++ /dev/null @@ -1,10 +0,0 @@ -states 3 -s0 -s1 -s2 + -alphabet 2 a b -s0 a s0 a R -s0 b s1 b R -s0 _ s2 _ S -s1 b s1 b R -s1 _ s2 _ S diff --git a/src/tmachines.nim b/src/tmachines.nim index 2d1d190..aae5401 100644 --- a/src/tmachines.nim +++ b/src/tmachines.nim @@ -3,6 +3,27 @@ import os, strutils import docopt from tmachinespkg/tm import nil +from tmachinespkg/ntm import nil + +type Machine = enum + DTM, NTM + +proc tm_type_from_cli(value: string): Machine = + case value + of "dtm": + return DTM + of "ntm": + return NTM + else: + return NTM + +proc tm_type_from_filename(filename: string): Machine = + if filename.ends_with(".dtm"): + return DTM + elif filename.ends_with(".ntm"): + return NTM + elif filename.ends_with(".tm"): + return NTM const tmname {.strdefine.}: string = "" @@ -18,6 +39,16 @@ else: let args = docopt(doc) let file = $args[""] - if file.ends_with(".tm") or file.ends_with(".dtm"): - let tmd = tm.parse(readFile($args[""])) - echo tm.test(tm.makeDTM(tmd, $args[""]), not args["--quiet"]) + var m_type: Machine + if $args["--type"] != "nil": + m_type = tm_type_from_cli($args["--type"]) + else: + m_type = tm_type_from_filename(file) + + case m_type: + of DTM: + let tmd = tm.parse(readFile($args[""])) + echo tm.test(tm.makeDTM(tmd, $args[""]), not args["--quiet"]) + of NTM: + let tmd = ntm.parse(readFile($args[""])) + echo ntm.test(ntm.makeNTM(tmd, $args[""]), not args["--quiet"]) diff --git a/src/tmachinespkg/ntm.nim b/src/tmachinespkg/ntm.nim new file mode 100644 index 0000000..c207029 --- /dev/null +++ b/src/tmachinespkg/ntm.nim @@ -0,0 +1,104 @@ +import sets, tables, strutils + +import tmlib + +type ttable = Table[input, HashSet[transition]] + +type description = object + initial_state: state + transitions: ttable + accepting_states: HashSet[state] + +type NTM* = ref object + acceptingStates*: HashSet[state] + transitions*: ttable + configurations*: HashSet[configuration] + +proc print_configurations*(this: NTM): string = + result = "" + for config in this.configurations: + result &= $config & "\n" + result &= "\n" + +proc move_copy(this: config, trans: transition): config = + result = this + result.move(trans) + +proc parse*(description: string): description = + ## Construct an NTM description from an iterator of rules from a .ntm file + var noStates = -1 + var statesSeen = -1 + var transitions = init_table[input, HashSet[transition]]() + var acceptingStates = init_set[state]() + var current_state = cast[state]("") + var alphabet = false + 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 no_states < 0: + no_states = line.split_white_space()[1].parseInt() + states_seen = 0 + # If we're partway through seeing states + 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]) + # If it's the first state mentioned + if statesSeen == 0: + current_state = (split[0]) + statesSeen += 1 + # If we haven't seen the alphabet yet + elif not alphabet: + alphabet = true + # If we're past all that and are at transitions + else: + let s = line.splitWhiteSpace() + let + input = (s[0], s[1][0]) + outs = s[2] + outl = s[3][0] + outd = to_direction(s[4]) + let transition = ( + newState: outs, + toWrite: outl, + moveDir: outd, + ) + if not transitions.has_key(input): + transitions[input] = init_set[transition]() + transitions[input].incl transition + # Next line + return description( + initial_state: current_state, + acceptingStates: acceptingStates, + transitions: transitions + ) + +proc make_configuration*(desc: description, input: string): configuration = + return make_configuration(desc.initial_state, input) + +proc makeNTM*(desc: description, input: string): NTM = + var configs = init_set[configuration]() + configs.incl(make_configuration(desc, input)) + return NTM( + configurations: configs, + accepting_states: desc.accepting_states, + transitions: desc.transitions, + ) + +proc test*(this: NTM, verbose: bool = true): bool = + while this.configurations.card != 0: + var new_configs = init_set[config]() + for config in this.configurations: + let input = config.get_input() + if this.transitions.has_key(input): + for trans in this.transitions[input]: + if trans.new_state in this.accepting_states: + return true + new_configs.incl(config.move_copy(trans)) + this.configurations = new_configs + if verbose: + echo this.print_configurations() + return false diff --git a/src/tmachinespkg/tm.nim b/src/tmachinespkg/tm.nim index c009a60..7849054 100644 --- a/src/tmachinespkg/tm.nim +++ b/src/tmachinespkg/tm.nim @@ -12,25 +12,25 @@ type DTM* = ref object transitions*: Table[(state, letter), transition] configuration*: configuration -proc `[]`(this: DTM, i: int): letter = +proc `[]`*(this: DTM, i: int): letter = return this.configuration[i] -proc `[]=`(this: DTM, i: int, l: letter) = +proc `[]=`*(this: DTM, i: int, l: letter) = this.configuration[i] = l -proc `$`(this: DTM): string = +proc `$`*(this: DTM): string = return $this.configuration proc make_configuration*(desc: description, input: string): configuration = return make_configuration(desc.initial_state, input) -## Construct a TM description from an iterator of rules from a description file proc parse*(description: string): description = + ## Construct a TM description from an iterator of rules from a .dtm file var noStates = -1 var statesSeen = -1 var transitions = init_table[input, transition]() var acceptingStates = init_set[state]() - var currentState = cast[state]("") + var current_state = cast[state]("") var alphabet = false for line in description.split_lines: # If the line is empty @@ -48,7 +48,7 @@ proc parse*(description: string): description = acceptingStates.incl(split[0]) # If it's the first state mentioned if statesSeen == 0: - currentState = (split[0]) + current_state = (split[0]) statesSeen += 1 # If we haven't seen the alphabet yet elif not alphabet: @@ -69,7 +69,7 @@ proc parse*(description: string): description = transitions[(ins, inl)] = transition # Next line return description( - initial_state: currentState, + initial_state: current_state, acceptingStates: acceptingStates, transitions: transitions ) diff --git a/src/tmachinespkg/tmlib.nim b/src/tmachinespkg/tmlib.nim index dfa815f..1924845 100644 --- a/src/tmachinespkg/tmlib.nim +++ b/src/tmachinespkg/tmlib.nim @@ -5,7 +5,7 @@ type letter* = char input* = tuple[state: state, letter: letter] -proc `$`(input: input): string = +proc `$`*(input: input): string = "(" & $input[0] & " " & $input[1] & ")" type Direction* = enum @@ -27,17 +27,29 @@ type transition* = tuple[ toWrite: letter, moveDir: Direction] -proc `$`(t: transition): string = +proc `$`*(t: transition): string = "[" & $t.newState & " " & $t.toWrite & " " & $t.moveDir & "]" type tape* = Table[int, letter] + +proc hash*(this: tape): Hash = + return ($this).hash() + type ttable* = Table[input, transition] type configuration* = object - currentState: state - tape: tape - position: int -type config = configuration + current_state*: state + tape*: tape + position*: int +type config* = configuration + +proc hash*(this: config): Hash = + ## Hashing configurations + var h: Hash = 0 + h = h !& this.current_state.hash() + h = h !& this.tape.hash() + h = h !& this.position.hash() + return !$h # Parses a Direction from a string proc to_direction*(token: string): Direction = @@ -61,12 +73,12 @@ proc `[]=`*(this: var configuration, i: int, l: letter) = this.tape[i] = l proc get_input*(this: configuration): input = - return (this.currentState, this[this.position]) + return (this.current_state, 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 + this.current_state = trans.newState return this # Tape string conversion @@ -90,7 +102,7 @@ proc tapeOf(input: string): tape = result = initTable[int, letter]() result.insert(input) -proc load( +proc load*( this: var configuration, input: string, reset: bool = true @@ -101,21 +113,16 @@ proc load( return this ## Iterator over every line in a file -proc lineIterator(f: File): (iterator(): TaintedString {.closure.}) = +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, + current_state: start, tape: tapeOf(input), position: 0, ) diff --git a/tests/test.sh b/tests/test.sh index b16f4fc..672ff01 100755 --- a/tests/test.sh +++ b/tests/test.sh @@ -7,8 +7,8 @@ for file in `ls -1 $1` ; do if [ -e "$1/$file/testgen.sh" ] ; then "$1/$file/testgen.sh" 50 | while read line ; do split=($line) - echo ./tmachines $1/$file/$file.tm $line - test "./tmachines --quiet $1/$file/$file.tm ${split[0]}" ${split[1]} + echo ./tmachines $1/$file/$file'.*tm' $line + test "./tmachines --quiet $1/$file/$file"'.*tm'" ${split[0]}" ${split[1]} done fi done -- cgit v1.2.1