| 1 |
# ------------------------ |
| 2 |
# Universal Turing machine |
| 3 |
# ------------------------ |
| 4 |
# |
| 5 |
# See https://en.wikipedia.org/wiki/Universal_Turing_machine |
| 6 |
|
| 7 |
@prefix log: <http://www.w3.org/2000/10/swap/log#>. |
| 8 |
@prefix list: <http://www.w3.org/2000/10/swap/list#>. |
| 9 |
@prefix : <urn:example:>. |
| 10 |
|
| 11 |
# declare components |
| 12 |
:compute a log:Component. |
| 13 |
:find a log:Component. |
| 14 |
:continue a log:Component. |
| 15 |
:move a log:Component. |
| 16 |
:reverse a log:Component. |
| 17 |
|
| 18 |
# interpreter for Univeral Turing Machine |
| 19 |
(_:OutTape _:Machine _:I) log:onNegativeSurface { |
| 20 |
() log:onNegativeSurface { |
| 21 |
() :compute _:OutTape. |
| 22 |
}. |
| 23 |
_:Machine :start _:I. |
| 24 |
(_:I () "#" ()) :find _:OutTape. |
| 25 |
}. |
| 26 |
|
| 27 |
(_:List _:OutTape _:Head _:Tail _:Machine _:I) log:onNegativeSurface { |
| 28 |
() log:onNegativeSurface { |
| 29 |
_:List :compute _:OutTape. |
| 30 |
}. |
| 31 |
_:List list:firstRest (_:Head _:Tail). |
| 32 |
_:Machine :start _:I. |
| 33 |
(_:I () _:Head _:Tail) :find _:OutTape. |
| 34 |
}. |
| 35 |
|
| 36 |
(_:State _:Left _:Cell _:Right _:OutTape _:Write _:Move _:Next _:A _:B _:C) log:onNegativeSurface { |
| 37 |
() log:onNegativeSurface { |
| 38 |
(_:State _:Left _:Cell _:Right) :find _:OutTape. |
| 39 |
}. |
| 40 |
(_:State _:Cell _:Write _:Move) :tape _:Next. |
| 41 |
(_:Move _:Left _:Write _:Right _:A _:B _:C) :move true. |
| 42 |
(_:Next _:A _:B _:C) :continue _:OutTape. |
| 43 |
}. |
| 44 |
|
| 45 |
(_:Left _:Cell _:Right _:OutTape _:R _:List) log:onNegativeSurface { |
| 46 |
() log:onNegativeSurface { |
| 47 |
(:halt _:Left _:Cell _:Right) :continue _:OutTape. |
| 48 |
}. |
| 49 |
_:Left :reverse _:R. |
| 50 |
_:List list:firstRest (_:Cell _:Right). |
| 51 |
(_:R _:List) list:append _:OutTape. |
| 52 |
}. |
| 53 |
|
| 54 |
(_:State _:Left _:Cell _:Right _:OutTape) log:onNegativeSurface { |
| 55 |
() log:onNegativeSurface { |
| 56 |
(_:State _:Left _:Cell _:Right) :continue _:OutTape. |
| 57 |
}. |
| 58 |
(_:State _:Left _:Cell _:Right) :find _:OutTape. |
| 59 |
}. |
| 60 |
|
| 61 |
(_:Cell _:Right _:L) log:onNegativeSurface { |
| 62 |
() log:onNegativeSurface { |
| 63 |
(:left () _:Cell _:Right () "#" _:L) :move true. |
| 64 |
}. |
| 65 |
_:L list:firstRest (_:Cell _:Right). |
| 66 |
}. |
| 67 |
|
| 68 |
(_:List _:Cell _:Right _:Tail _:Head _:L) log:onNegativeSurface { |
| 69 |
() log:onNegativeSurface { |
| 70 |
(:left _:List _:Cell _:Right _:Tail _:Head _:L) :move true. |
| 71 |
}. |
| 72 |
_:List list:firstRest (_:Head _:Tail). |
| 73 |
_:L list:firstRest (_:Cell _:Right). |
| 74 |
}. |
| 75 |
|
| 76 |
(_:Left _:Cell _:Right) log:onNegativeSurface { |
| 77 |
() log:onNegativeSurface { |
| 78 |
(:stop _:Left _:Cell _:Right _:Left _:Cell _:Right) :move true. |
| 79 |
}. |
| 80 |
}. |
| 81 |
|
| 82 |
(_:Left _:Cell _:L) log:onNegativeSurface { |
| 83 |
() log:onNegativeSurface { |
| 84 |
(:right _:Left _:Cell () _:L "#" ()) :move true. |
| 85 |
}. |
| 86 |
_:L list:firstRest (_:Cell _:Left). |
| 87 |
}. |
| 88 |
|
| 89 |
(_:Left _:Cell _:List _:L _:Head _:Tail) log:onNegativeSurface { |
| 90 |
() log:onNegativeSurface { |
| 91 |
(:right _:Left _:Cell _:List _:L _:Head _:Tail) :move true. |
| 92 |
}. |
| 93 |
_:List list:firstRest (_:Head _:Tail). |
| 94 |
_:L list:firstRest (_:Cell _:Left). |
| 95 |
}. |
| 96 |
|
| 97 |
() log:onNegativeSurface { |
| 98 |
() log:onNegativeSurface { |
| 99 |
() :reverse (). |
| 100 |
}. |
| 101 |
}. |
| 102 |
|
| 103 |
(_:List _:Reverse _:Head _:Tail _:R) log:onNegativeSurface { |
| 104 |
() log:onNegativeSurface { |
| 105 |
_:List :reverse _:Reverse. |
| 106 |
}. |
| 107 |
_:List list:firstRest (_:Head _:Tail). |
| 108 |
_:Tail :reverse _:R. |
| 109 |
(_:R (_:Head)) list:append _:Reverse. |
| 110 |
}. |
| 111 |
|
| 112 |
# a Turing machine to add 1 to a binary number |
| 113 |
:add1 :start 0. |
| 114 |
|
| 115 |
(0 0 0 :right) :tape 0. |
| 116 |
(0 1 1 :right) :tape 0. |
| 117 |
(0 "#" "#" :left) :tape 1. |
| 118 |
(1 0 1 :stop) :tape :halt. |
| 119 |
(1 1 0 :left) :tape 1. |
| 120 |
(1 "#" 1 :stop) :tape :halt. |
| 121 |
|
| 122 |
# query |
| 123 |
(_:A1 _:A2 _:A3 _:A4) log:onNegativeSurface { |
| 124 |
(1 0 1 0 0 1) :compute _:A1. |
| 125 |
(1 0 1 1 1 1) :compute _:A2. |
| 126 |
(1 1 1 1 1 1) :compute _:A3. |
| 127 |
() :compute _:A4. |
| 128 |
() log:onNegativeAnswerSurface { |
| 129 |
(1 0 1 0 0 1) :compute _:A1. |
| 130 |
(1 0 1 1 1 1) :compute _:A2. |
| 131 |
(1 1 1 1 1 1) :compute _:A3. |
| 132 |
() :compute _:A4. |
| 133 |
}. |
| 134 |
}. |