From 3143763fc098ce2c46b84e38c6cedabbf4622fa2 Mon Sep 17 00:00:00 2001 From: Tom Harley Date: Sun, 13 May 2018 23:53:42 +0100 Subject: Reorganise project structure --- addition.tm | 65 ----------- bingen.sh | 12 --- get-nim.sh | 5 - lib.nim | 222 -------------------------------------- machines/addition/addition.tm | 65 +++++++++++ machines/addition/bingen.sh | 12 +++ machines/palindrome/palgen.sh | 12 +++ machines/palindrome/palindrome.tm | 35 ++++++ machines/simple/test.tm | 10 ++ mt.nim | 41 ------- palgen.sh | 12 --- palindrome.tm | 35 ------ src/lib.nim | 222 ++++++++++++++++++++++++++++++++++++++ src/mt.nim | 41 +++++++ src/tm.nim | 18 ++++ test.tm | 10 -- tm.nim | 18 ---- 17 files changed, 415 insertions(+), 420 deletions(-) delete mode 100644 addition.tm delete mode 100755 bingen.sh delete mode 100755 get-nim.sh delete mode 100644 lib.nim create mode 100644 machines/addition/addition.tm create mode 100755 machines/addition/bingen.sh create mode 100755 machines/palindrome/palgen.sh create mode 100644 machines/palindrome/palindrome.tm create mode 100644 machines/simple/test.tm delete mode 100644 mt.nim delete mode 100755 palgen.sh delete mode 100644 palindrome.tm create mode 100644 src/lib.nim create mode 100644 src/mt.nim create mode 100644 src/tm.nim delete mode 100644 test.tm delete mode 100644 tm.nim diff --git a/addition.tm b/addition.tm deleted file mode 100644 index f2b61f8..0000000 --- a/addition.tm +++ /dev/null @@ -1,65 +0,0 @@ -states 14 -S -0A -0B -0C -0D -1A -1B -1C -1D -R1 -R2 -Ca -_1 -_2 + -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 1 0D 1 S diff --git a/bingen.sh b/bingen.sh deleted file mode 100755 index b9a0717..0000000 --- a/bingen.sh +++ /dev/null @@ -1,12 +0,0 @@ -#!/bin/bash - -function dec2bin { - echo "obase=2;$1" | bc | tr -d '\n' | rev -} - -for i in {1..1000} ; do - a=$RANDOM - b=$RANDOM - c=$(($a + $b)) - echo "`dec2bin $a`#`dec2bin $b`#`dec2bin $c` true" -done diff --git a/get-nim.sh b/get-nim.sh deleted file mode 100755 index 3ec78c6..0000000 --- a/get-nim.sh +++ /dev/null @@ -1,5 +0,0 @@ -#!/bin/sh -# Copied from https://nim-lang.org/install_unix.html: -curl https://nim-lang.org/choosenim/init.sh -sSf | sh - -export PATH=$HOME/.nimble/bin:"$PATH" diff --git a/lib.nim b/lib.nim deleted file mode 100644 index b909478..0000000 --- a/lib.nim +++ /dev/null @@ -1,222 +0,0 @@ -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 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 - -proc `[]`(this: DTM, i: int): letter = - if this.tape.hasKey(i): - return this.tape[i] - else: - return '_' - -proc `[]=`(this: DTM, i: int, l: letter) = - this.tape[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) - -## 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 - -## Construct a DTM 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 currentState = cast[state]("") - var alphabet = false - for line in description.splitLines: - # 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 we're partway through seeing states - elif noStates > statesSeen: - let split = line.splitWhiteSpace() - # If it's an accepting state - if split.len == 2: - acceptingStates.incl(split[0]) - # If it's the first state mentioned - if statesSeen == 0: - currentState = (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 - ins = s[0] - inl = s[1][0] - outs = s[2] - outl = s[3][0] - outd = toDirection(s[4]) - let transition = ( - newState: outs, - toWrite: outl, - moveDir: outd) - transitions[(ins, inl)] = transition - # Next line - return ( - currentState: 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] = {} - for k, v in desc.transitions: - states.incl k.state - states.incl v.newState - alphabet.incl k.letter - alphabet.incl v.toWrite - result = "states " & $states.len & "\n" - for v in states: - result &= $v & "\n" - result &= "alphabet " & $alphabet.card() - for v in alphabet: - result &= " " & $v - result &= "\n" - for k, v in desc.transitions: - result &= $k.state & " " & $k.letter & " " & $v.newState & " " & $v.toWrite - 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) - -## Does this DTM accept this string as input? -proc `<= 1: - echo makeDTM(desc, paramStr(1)).test(true) -else: - echo desc.toFile() diff --git a/palgen.sh b/palgen.sh deleted file mode 100755 index d5daddc..0000000 --- a/palgen.sh +++ /dev/null @@ -1,12 +0,0 @@ -#!/bin/sh - -function rndm { - for i in {1..100} ; do - echo -n $(($RANDOM % 3 )) - done -} - -for i in {1..1000} ; do - woa=`rndm` - echo "$woa`echo $woa | rev` true" -done diff --git a/palindrome.tm b/palindrome.tm deleted file mode 100644 index 692b70d..0000000 --- a/palindrome.tm +++ /dev/null @@ -1,35 +0,0 @@ -states 8 -A + -B0 -B1 -B2 -C0 -C1 -C2 -D -alphabet 3 0 1 2 -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/src/lib.nim b/src/lib.nim new file mode 100644 index 0000000..b909478 --- /dev/null +++ b/src/lib.nim @@ -0,0 +1,222 @@ +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 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 + +proc `[]`(this: DTM, i: int): letter = + if this.tape.hasKey(i): + return this.tape[i] + else: + return '_' + +proc `[]=`(this: DTM, i: int, l: letter) = + this.tape[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) + +## 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 + +## Construct a DTM 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 currentState = cast[state]("") + var alphabet = false + for line in description.splitLines: + # 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 we're partway through seeing states + elif noStates > statesSeen: + let split = line.splitWhiteSpace() + # If it's an accepting state + if split.len == 2: + acceptingStates.incl(split[0]) + # If it's the first state mentioned + if statesSeen == 0: + currentState = (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 + ins = s[0] + inl = s[1][0] + outs = s[2] + outl = s[3][0] + outd = toDirection(s[4]) + let transition = ( + newState: outs, + toWrite: outl, + moveDir: outd) + transitions[(ins, inl)] = transition + # Next line + return ( + currentState: 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] = {} + for k, v in desc.transitions: + states.incl k.state + states.incl v.newState + alphabet.incl k.letter + alphabet.incl v.toWrite + result = "states " & $states.len & "\n" + for v in states: + result &= $v & "\n" + result &= "alphabet " & $alphabet.card() + for v in alphabet: + result &= " " & $v + result &= "\n" + for k, v in desc.transitions: + result &= $k.state & " " & $k.letter & " " & $v.newState & " " & $v.toWrite + 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) + +## Does this DTM accept this string as input? +proc `<= 1: + echo makeDTM(desc, paramStr(1)).test(true) +else: + echo desc.toFile() diff --git a/src/tm.nim b/src/tm.nim new file mode 100644 index 0000000..26c97f2 --- /dev/null +++ b/src/tm.nim @@ -0,0 +1,18 @@ +import os + +import lib + +const tmname {.strdefine.}: string = "" + +when tmname.len > 0: + const tmd = parse(slurp(tmname & ".tm")) + if paramCount() >= 1: + echo makeDTM(tmd, paramStr(1)).test(false) + else: + echo "please specify a single string argument" +else: + if paramCount() >= 2: + let tmd = parse(readFile(paramStr(1) & ".tm")) + echo makeDTM(tmd, paramStr(2)).test(true) + else: + echo "please specify a description and string input" diff --git a/test.tm b/test.tm deleted file mode 100644 index da79405..0000000 --- a/test.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/tm.nim b/tm.nim deleted file mode 100644 index 26c97f2..0000000 --- a/tm.nim +++ /dev/null @@ -1,18 +0,0 @@ -import os - -import lib - -const tmname {.strdefine.}: string = "" - -when tmname.len > 0: - const tmd = parse(slurp(tmname & ".tm")) - if paramCount() >= 1: - echo makeDTM(tmd, paramStr(1)).test(false) - else: - echo "please specify a single string argument" -else: - if paramCount() >= 2: - let tmd = parse(readFile(paramStr(1) & ".tm")) - echo makeDTM(tmd, paramStr(2)).test(true) - else: - echo "please specify a description and string input" -- cgit v1.2.1