├── .gitignore ├── LICENSE ├── README.md ├── project.clj ├── src └── halting │ ├── analysis.clj │ ├── machines.clj │ └── tapes.clj └── test └── halting └── machines_test.clj /.gitignore: -------------------------------------------------------------------------------- 1 | /target 2 | /classes 3 | /checkouts 4 | pom.xml 5 | pom.xml.asc 6 | *.jar 7 | *.class 8 | /.lein-* 9 | /.nrepl-port 10 | .hgignore 11 | .hg/ 12 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | THE ACCOMPANYING PROGRAM IS PROVIDED UNDER THE TERMS OF THIS ECLIPSE PUBLIC 2 | LICENSE ("AGREEMENT"). ANY USE, REPRODUCTION OR DISTRIBUTION OF THE PROGRAM 3 | CONSTITUTES RECIPIENT'S ACCEPTANCE OF THIS AGREEMENT. 4 | 5 | 1. DEFINITIONS 6 | 7 | "Contribution" means: 8 | 9 | a) in the case of the initial Contributor, the initial code and 10 | documentation distributed under this Agreement, and 11 | 12 | b) in the case of each subsequent Contributor: 13 | 14 | i) changes to the Program, and 15 | 16 | ii) additions to the Program; 17 | 18 | where such changes and/or additions to the Program originate from and are 19 | distributed by that particular Contributor. A Contribution 'originates' from 20 | a Contributor if it was added to the Program by such Contributor itself or 21 | anyone acting on such Contributor's behalf. Contributions do not include 22 | additions to the Program which: (i) are separate modules of software 23 | distributed in conjunction with the Program under their own license 24 | agreement, and (ii) are not derivative works of the Program. 25 | 26 | "Contributor" means any person or entity that distributes the Program. 27 | 28 | "Licensed Patents" mean patent claims licensable by a Contributor which are 29 | necessarily infringed by the use or sale of its Contribution alone or when 30 | combined with the Program. 31 | 32 | "Program" means the Contributions distributed in accordance with this 33 | Agreement. 34 | 35 | "Recipient" means anyone who receives the Program under this Agreement, 36 | including all Contributors. 37 | 38 | 2. GRANT OF RIGHTS 39 | 40 | a) Subject to the terms of this Agreement, each Contributor hereby grants 41 | Recipient a non-exclusive, worldwide, royalty-free copyright license to 42 | reproduce, prepare derivative works of, publicly display, publicly perform, 43 | distribute and sublicense the Contribution of such Contributor, if any, and 44 | such derivative works, in source code and object code form. 45 | 46 | b) Subject to the terms of this Agreement, each Contributor hereby grants 47 | Recipient a non-exclusive, worldwide, royalty-free patent license under 48 | Licensed Patents to make, use, sell, offer to sell, import and otherwise 49 | transfer the Contribution of such Contributor, if any, in source code and 50 | object code form. This patent license shall apply to the combination of the 51 | Contribution and the Program if, at the time the Contribution is added by the 52 | Contributor, such addition of the Contribution causes such combination to be 53 | covered by the Licensed Patents. The patent license shall not apply to any 54 | other combinations which include the Contribution. No hardware per se is 55 | licensed hereunder. 56 | 57 | c) Recipient understands that although each Contributor grants the licenses 58 | to its Contributions set forth herein, no assurances are provided by any 59 | Contributor that the Program does not infringe the patent or other 60 | intellectual property rights of any other entity. Each Contributor disclaims 61 | any liability to Recipient for claims brought by any other entity based on 62 | infringement of intellectual property rights or otherwise. As a condition to 63 | exercising the rights and licenses granted hereunder, each Recipient hereby 64 | assumes sole responsibility to secure any other intellectual property rights 65 | needed, if any. For example, if a third party patent license is required to 66 | allow Recipient to distribute the Program, it is Recipient's responsibility 67 | to acquire that license before distributing the Program. 68 | 69 | d) Each Contributor represents that to its knowledge it has sufficient 70 | copyright rights in its Contribution, if any, to grant the copyright license 71 | set forth in this Agreement. 72 | 73 | 3. REQUIREMENTS 74 | 75 | A Contributor may choose to distribute the Program in object code form under 76 | its own license agreement, provided that: 77 | 78 | a) it complies with the terms and conditions of this Agreement; and 79 | 80 | b) its license agreement: 81 | 82 | i) effectively disclaims on behalf of all Contributors all warranties and 83 | conditions, express and implied, including warranties or conditions of title 84 | and non-infringement, and implied warranties or conditions of merchantability 85 | and fitness for a particular purpose; 86 | 87 | ii) effectively excludes on behalf of all Contributors all liability for 88 | damages, including direct, indirect, special, incidental and consequential 89 | damages, such as lost profits; 90 | 91 | iii) states that any provisions which differ from this Agreement are offered 92 | by that Contributor alone and not by any other party; and 93 | 94 | iv) states that source code for the Program is available from such 95 | Contributor, and informs licensees how to obtain it in a reasonable manner on 96 | or through a medium customarily used for software exchange. 97 | 98 | When the Program is made available in source code form: 99 | 100 | a) it must be made available under this Agreement; and 101 | 102 | b) a copy of this Agreement must be included with each copy of the Program. 103 | 104 | Contributors may not remove or alter any copyright notices contained within 105 | the Program. 106 | 107 | Each Contributor must identify itself as the originator of its Contribution, 108 | if any, in a manner that reasonably allows subsequent Recipients to identify 109 | the originator of the Contribution. 110 | 111 | 4. COMMERCIAL DISTRIBUTION 112 | 113 | Commercial distributors of software may accept certain responsibilities with 114 | respect to end users, business partners and the like. While this license is 115 | intended to facilitate the commercial use of the Program, the Contributor who 116 | includes the Program in a commercial product offering should do so in a 117 | manner which does not create potential liability for other Contributors. 118 | Therefore, if a Contributor includes the Program in a commercial product 119 | offering, such Contributor ("Commercial Contributor") hereby agrees to defend 120 | and indemnify every other Contributor ("Indemnified Contributor") against any 121 | losses, damages and costs (collectively "Losses") arising from claims, 122 | lawsuits and other legal actions brought by a third party against the 123 | Indemnified Contributor to the extent caused by the acts or omissions of such 124 | Commercial Contributor in connection with its distribution of the Program in 125 | a commercial product offering. The obligations in this section do not apply 126 | to any claims or Losses relating to any actual or alleged intellectual 127 | property infringement. In order to qualify, an Indemnified Contributor must: 128 | a) promptly notify the Commercial Contributor in writing of such claim, and 129 | b) allow the Commercial Contributor tocontrol, and cooperate with the 130 | Commercial Contributor in, the defense and any related settlement 131 | negotiations. The Indemnified Contributor may participate in any such claim 132 | at its own expense. 133 | 134 | For example, a Contributor might include the Program in a commercial product 135 | offering, Product X. That Contributor is then a Commercial Contributor. If 136 | that Commercial Contributor then makes performance claims, or offers 137 | warranties related to Product X, those performance claims and warranties are 138 | such Commercial Contributor's responsibility alone. Under this section, the 139 | Commercial Contributor would have to defend claims against the other 140 | Contributors related to those performance claims and warranties, and if a 141 | court requires any other Contributor to pay any damages as a result, the 142 | Commercial Contributor must pay those damages. 143 | 144 | 5. NO WARRANTY 145 | 146 | EXCEPT AS EXPRESSLY SET FORTH IN THIS AGREEMENT, THE PROGRAM IS PROVIDED ON 147 | AN "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, EITHER 148 | EXPRESS OR IMPLIED INCLUDING, WITHOUT LIMITATION, ANY WARRANTIES OR 149 | CONDITIONS OF TITLE, NON-INFRINGEMENT, MERCHANTABILITY OR FITNESS FOR A 150 | PARTICULAR PURPOSE. Each Recipient is solely responsible for determining the 151 | appropriateness of using and distributing the Program and assumes all risks 152 | associated with its exercise of rights under this Agreement , including but 153 | not limited to the risks and costs of program errors, compliance with 154 | applicable laws, damage to or loss of data, programs or equipment, and 155 | unavailability or interruption of operations. 156 | 157 | 6. DISCLAIMER OF LIABILITY 158 | 159 | EXCEPT AS EXPRESSLY SET FORTH IN THIS AGREEMENT, NEITHER RECIPIENT NOR ANY 160 | CONTRIBUTORS SHALL HAVE ANY LIABILITY FOR ANY DIRECT, INDIRECT, INCIDENTAL, 161 | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING WITHOUT LIMITATION 162 | LOST PROFITS), HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 163 | CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 164 | ARISING IN ANY WAY OUT OF THE USE OR DISTRIBUTION OF THE PROGRAM OR THE 165 | EXERCISE OF ANY RIGHTS GRANTED HEREUNDER, EVEN IF ADVISED OF THE POSSIBILITY 166 | OF SUCH DAMAGES. 167 | 168 | 7. GENERAL 169 | 170 | If any provision of this Agreement is invalid or unenforceable under 171 | applicable law, it shall not affect the validity or enforceability of the 172 | remainder of the terms of this Agreement, and without further action by the 173 | parties hereto, such provision shall be reformed to the minimum extent 174 | necessary to make such provision valid and enforceable. 175 | 176 | If Recipient institutes patent litigation against any entity (including a 177 | cross-claim or counterclaim in a lawsuit) alleging that the Program itself 178 | (excluding combinations of the Program with other software or hardware) 179 | infringes such Recipient's patent(s), then such Recipient's rights granted 180 | under Section 2(b) shall terminate as of the date such litigation is filed. 181 | 182 | All Recipient's rights under this Agreement shall terminate if it fails to 183 | comply with any of the material terms or conditions of this Agreement and 184 | does not cure such failure in a reasonable period of time after becoming 185 | aware of such noncompliance. If all Recipient's rights under this Agreement 186 | terminate, Recipient agrees to cease use and distribution of the Program as 187 | soon as reasonably practicable. However, Recipient's obligations under this 188 | Agreement and any licenses granted by Recipient relating to the Program shall 189 | continue and survive. 190 | 191 | Everyone is permitted to copy and distribute copies of this Agreement, but in 192 | order to avoid inconsistency the Agreement is copyrighted and may only be 193 | modified in the following manner. The Agreement Steward reserves the right to 194 | publish new versions (including revisions) of this Agreement from time to 195 | time. No one other than the Agreement Steward has the right to modify this 196 | Agreement. The Eclipse Foundation is the initial Agreement Steward. The 197 | Eclipse Foundation may assign the responsibility to serve as the Agreement 198 | Steward to a suitable separate entity. Each new version of the Agreement will 199 | be given a distinguishing version number. The Program (including 200 | Contributions) may always be distributed subject to the version of the 201 | Agreement under which it was received. In addition, after a new version of 202 | the Agreement is published, Contributor may elect to distribute the Program 203 | (including its Contributions) under the new version. Except as expressly 204 | stated in Sections 2(a) and 2(b) above, Recipient receives no rights or 205 | licenses to the intellectual property of any Contributor under this 206 | Agreement, whether expressly, by implication, estoppel or otherwise. All 207 | rights in the Program not expressly granted under this Agreement are 208 | reserved. 209 | 210 | This Agreement is governed by the laws of the State of New York and the 211 | intellectual property laws of the United States of America. No party to this 212 | Agreement will bring a legal action under this Agreement more than one year 213 | after the cause of action arose. Each party waives its rights to a jury trial 214 | in any resulting litigation. 215 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # The Halting Project 2 | 3 | What's the simplest Turing Machine with unknown behavior? 4 | 5 | ## What? 6 | 7 | The 8 | [undecidability of The Halting Problem](https://en.wikipedia.org/wiki/Halting_problem#Sketch_of_proof) 9 | implies that we can't write a program that can analyze any program to 10 | determine if it ever terminates. That doesn't stop us from analyzing 11 | individual programs using ad-hoc methods (some programs obviously 12 | terminate or obviously don't), but ad-hoc methods will hit a wall 13 | somewhere. I'm interested in finding that wall. 14 | 15 | ## Why? 16 | 17 | I thought it would be fun. 18 | 19 | ## Is this a reasonable goal? 20 | 21 | I'm not sure yet. 22 | 23 | ## What's do you consider a Turing Machine to be exactly for these purposes? 24 | 25 | That's a good question. 26 | 27 | It seemed useful to choose a definition that simplifies this project. 28 | 29 | A turing machine operates on an infinite two-way binary tape, where 30 | each square of the tape can only be `0` or `1`. 31 | 32 | The tape is always initialized with all `0`s (there is no notion of 33 | the "input" to a program). 34 | 35 | A machine has `N` states (excluding the halt state, which isn't a 36 | proper state), numbered `0` to `N-1` (state `0` is the start state and 37 | state `N` is the halt state). 38 | 39 | Each state specifies the machine's behavior based on the value read 40 | from the current position in the tape. E.g., `1L1` means "write a `1`, 41 | move left, and transition to state `1`. 42 | 43 | An example (which only halts when reading a `0` in state `4`): 44 | 45 | | State | Read 0 | Read 1 | 46 | |------:|:-------|:-------| 47 | | 0|1L1 |1R2 | 48 | | 1|1L2 |1L1 | 49 | | 2|1L3 |0R4 | 50 | | 3|1R0 |1R3 | 51 | | 4|1L5 |0R0 | 52 | 53 | We can identify turing machines with natural numbers by first ordering 54 | them by size and then by the natural lexicographic ordering of the 55 | above representation. So machine `#0` is the empty turing machine (which 56 | has already halted), machine `#1` is: 57 | 58 | | State | Read 0 | Read 1 | 59 | |------:|:-------|:-------| 60 | | 0|0L0 |0L0 | 61 | 62 | machine `#2` is: 63 | 64 | | State | Read 0 | Read 1 | 65 | |------:|:-------|:-------| 66 | | 0|0L0 |0L1 | 67 | 68 | and so on or something. 69 | 70 | ### Would a different definition be better? 71 | 72 | I think the above definition could be refined to exclude some 73 | redundancy, but then counting would be harder. I dunno. 74 | 75 | ## Related 76 | 77 | [The Busy Beaver Game](https://en.wikipedia.org/wiki/Busy_beaver). 78 | 79 | ## Results So Far 80 | 81 | Here I'll list different techniques for proving halts-or-not, in order 82 | of the smallest turing machine that requires that technique. 83 | 84 | ### By Definition 85 | 86 | Machine `#0` (the 0-state machine) halts by definition, so there's nothing further 87 | to prove about it. 88 | 89 | ### Immediate Loop 90 | 91 | Machine `#1` cannot halt because its first transition is back to state 92 | `0` and so it will always read `0`s and is trivially in an infinite 93 | loop. 94 | 95 | | State | Read 0 | Read 1 | 96 | |------:|:------------|:------------| 97 | | **α** | 0, ←, **α** | 0, ←, **α** | 98 | 99 | (Machines `#2` and `#3` both have immediate loops) 100 | 101 | ### Uncanonical 102 | 103 | Machine `#4` can be ignored, since it is equivalent to machine `#2` 104 | (which, after reading a 1, moves left before halting instead of 105 | right). 106 | 107 | | State | Read 0 | Read 1 | 108 | |------:|:------------|:------------| 109 | | **α** | 0, ←, **α** | 0, →, **H** | 110 | 111 | (Machines `#5` through `#8` are half uncanonical and half immediate 112 | loops) 113 | 114 | ### Halts Quickly 115 | 116 | Machine `#9` halts after 1 step, which can be verified by simulating 117 | it. 118 | 119 | | State | Read 0 | Read 1 | 120 | |------:|:------------|:------------| 121 | | **α** | 0, ←, **H** | 0, ←, **α** | 122 | 123 | (Machines `#10` through `#1792` consist of 1035 uncanonical machines, 124 | 744 immediate loops, and 4 quick halters) 125 | 126 | ### Unreachable Halt State 127 | 128 | Machine `#1793` cannot halt because there is no transition path from 129 | its start state (`0`) to its halt state (`2`). 130 | 131 | | State | Read 0 | Read 1 | 132 | |------:|:------------|:------------| 133 | | **α** | 0, ←, **β** | 0, ←, **α** | 134 | | **β** | 0, ←, **α** | 0, ←, **α** | 135 | 136 | (Machine `#1794` also has an unreachable halt state) 137 | 138 | ### Onelessness 139 | 140 | Machine `#1795` cannot halt because it only halts when reading a `1`, 141 | but it never writes a `1`. 142 | 143 | | State | Read 0 | Read 1 | 144 | |------:|:-----------:|:------------| 145 | | **α** | 0, ←, **β** | 0, ←, **α** | 146 | | **β** | 0, ←, **α** | 0, ←, **H** | 147 | 148 | (Machines `#1796` through `#1866` consist of 32 machines with an 149 | unreachable halt state, 27 uncanonical machines, 9 quick halters, 150 | and 3 oneless machines) 151 | 152 | ### Unidirectionality 153 | 154 | Machine `#1867` should not halt because it is shown by simulation to 155 | not halt within `N` steps, and its transition table shows that it 156 | always moves in the same direction when reading a `0`, so it will only 157 | keep reading `0`s. 158 | 159 | | State | Read 0 | Read 1 | 160 | |------:|:------------|:------------| 161 | | **α** | 0, ←, **β** | 0, ←, **α** | 162 | | **β** | 1, ←, **α** | 0, ←, **H** | 163 | 164 | (Machines `#1868` through `#1914` consist of 24 machines with an 165 | unreachable halt state, 21 uncanonical machines, 1 quick halter 166 | and 1 unidirectional machine) 167 | 168 | ### Eventual Loop 169 | 170 | Machine `#1915` quickly transitions to an infinite loop of writing 171 | `1`s and moving right, but it has an extra state transition at first 172 | which prevents the "Immediate Loop" check above from catching it. 173 | 174 | At this point we may as well also look for loops that have a period of 175 | multiple steps, which can be done by tracking the last time the 176 | machine was in the current state and comparing the old and new tapes 177 | for relevant similarities. 178 | 179 | | State | Read 0 | Read 1 | 180 | |------:|:------------|:------------| 181 | | **α** | 0, ←, **β** | 0, ←, **α** | 182 | | **β** | 1, →, **β** | 0, ←, **H** | 183 | 184 | (Machines `#1916` through `#1251965` consist of 977730 uncanonical 185 | machines, 188256 immediate loopers, 63258 machines with an unreachable 186 | halt state, 10831 quick halters, 4392 eventual loopers, 3135 187 | unidirectional machines, and 2448 oneless machines) 188 | 189 | ### Unknown 190 | 191 | Machine `#1251966` seems to have an increasing loop of some sort. Oh 192 | boy! 193 | 194 | | State | Read 0 | Read 1 | 195 | |------:|:------------|:------------| 196 | | **α** | 0, ←, **β** | 0, ←, **γ** | 197 | | **β** | 1, →, **α** | 1, ←, **β** | 198 | | **γ** | 0, ←, **H** | 1, →, **β** | 199 | 200 | ## License 201 | 202 | Copyright © 2016 Gary Fredericks 203 | 204 | Distributed under the Eclipse Public License either version 1.0 or (at 205 | your option) any later version. 206 | -------------------------------------------------------------------------------- /project.clj: -------------------------------------------------------------------------------- 1 | (defproject com.gfredericks/the-halting-project "0.1.0-SNAPSHOT" 2 | :description "What's the simplest Turing Machine with unknown behavior?" 3 | :url "https://github.com/gfredericks/the-halting-project" 4 | :license {:name "Eclipse Public License" 5 | :url "http://www.eclipse.org/legal/epl-v10.html"} 6 | :dependencies [[org.clojure/clojure "1.7.0"]] 7 | :profiles {:dev {:dependencies [[org.clojure/test.check "0.9.0"]]}}) 8 | -------------------------------------------------------------------------------- /src/halting/analysis.clj: -------------------------------------------------------------------------------- 1 | (ns halting.analysis 2 | (:require [halting.machines :as machines] 3 | [halting.tapes :as tapes])) 4 | 5 | (defn reachable-states 6 | [TM] 7 | (loop [to-visit [0] 8 | visited #{}] 9 | (if (empty? to-visit) 10 | visited 11 | (let [to-states (-> TM (get (peek to-visit)) (->> (map last))) 12 | visited' (conj visited (peek to-visit))] 13 | (recur (-> to-visit pop (into (remove visited' to-states))) 14 | visited'))))) 15 | 16 | (defn degenerate? 17 | [TM] 18 | (= (inc (count TM)) 19 | (count (reachable-states TM)))) 20 | 21 | (defn reachable-halt-state? 22 | [TM] 23 | (contains? (reachable-states TM) (count TM))) 24 | 25 | (defn all-TMs 26 | "Returns a naive list of all TMs with N states (excluding halt 27 | state)." 28 | [N] 29 | (letfn [(TMs [state-count] 30 | (if (zero? state-count) 31 | [[]] 32 | (for [TM (TMs (dec state-count)) 33 | write0 [0 1] 34 | dir0 [:left :right] 35 | state0 (range (inc N)) 36 | write1 [0 1] 37 | dir1 [:left :right] 38 | state1 (range (inc N))] 39 | (conj TM [[write0 dir0 state0] 40 | [write1 dir1 state1]]))))] 41 | (TMs N))) 42 | 43 | (defn run-on-empty-tape 44 | "Returns the number of transitions to the halt state, or nil if 45 | max-steps was reached without halting." 46 | [TM max-steps] 47 | (let [halt-state (count TM)] 48 | (loop [tape tapes/zero-tape 49 | state 0 50 | steps 0] 51 | (cond (= halt-state state) 52 | steps 53 | 54 | (= steps max-steps) 55 | nil 56 | 57 | :else 58 | (let [[write dir state'] (get-in TM [state (tapes/read tape)]) 59 | steps' (inc steps)] 60 | (recur (-> tape 61 | (tapes/write write) 62 | ((case dir 63 | :left tapes/move-left 64 | :right tapes/move-right))) 65 | state' 66 | steps')))))) 67 | 68 | (defn eventual-loop? 69 | "Returns true if a loop can be detected in max-steps by 70 | comparing the semi-local state of the tape with the last 71 | time the machine was in the current state." 72 | [TM max-steps window-size] 73 | (let [halt-state (count TM)] 74 | (loop [tape tapes/zero-tape 75 | state 0 76 | step 0 77 | pos 0 78 | states-last-seen {}] 79 | (cond (= halt-state state) 80 | false 81 | 82 | (= step max-steps) 83 | false 84 | 85 | (if-let [{last-tape :tape, last-pos :pos, last-step :step} 86 | (states-last-seen [state (tapes/context tape window-size window-size)])] 87 | (if (= pos last-pos) 88 | (= last-tape tape) 89 | (let [Δstep (- step last-step)] 90 | (if (< pos last-pos) 91 | ;; moving left 92 | (= (tapes/context tape nil Δstep) 93 | (tapes/context last-tape nil Δstep)) 94 | ;; moving right 95 | (= (tapes/context tape Δstep nil) 96 | (tapes/context last-tape Δstep nil)))))) 97 | true 98 | 99 | :else 100 | (let [[write dir state'] (get-in TM [state (tapes/read tape)]) 101 | step' (inc step)] 102 | (recur (-> tape 103 | (tapes/write write) 104 | ((case dir 105 | :left tapes/move-left 106 | :right tapes/move-right))) 107 | state' 108 | step' 109 | ((case dir :left dec :right inc) pos) 110 | (assoc states-last-seen [state (tapes/context tape window-size window-size)] 111 | {:tape tape :pos pos :step step}))))))) 112 | 113 | (defn unidirectional? 114 | [TM] 115 | (->> TM 116 | (map first) 117 | (map second) 118 | (apply =))) 119 | 120 | (defn oneless? 121 | "Returns true if the machine never writes a 1 and can only 122 | halt if it reads a 1." 123 | [TM] 124 | (and (->> TM 125 | (map ffirst) 126 | (every? zero?)) 127 | (->> TM 128 | (map first) 129 | (map last) 130 | (not-any? #{(count TM)})))) 131 | 132 | (defn trivial-loop? 133 | [TM] 134 | (-> TM first first last zero?)) 135 | 136 | (defn classify 137 | [TM] 138 | (cond (empty? TM) 139 | :halts-by-definition 140 | 141 | (not (machines/canonical? TM)) 142 | :uncanonical 143 | 144 | (trivial-loop? TM) 145 | :infinite-by-immediate-loop 146 | 147 | (not (reachable-halt-state? TM)) 148 | :unreachable-halt-state 149 | 150 | (run-on-empty-tape TM (* 10 (count TM))) 151 | :halts-quickly 152 | 153 | (oneless? TM) 154 | :infinite-by-onelessness 155 | 156 | (unidirectional? TM) 157 | :infinite-by-unidirectionality 158 | 159 | (eventual-loop? TM (* 100 (count TM)) 1) 160 | :eventual-loop 161 | 162 | :else 163 | :unknown)) 164 | 165 | (defn classification-summary 166 | [] 167 | (->> (range) 168 | (map (juxt identity machines/nat->machine)) 169 | (reduce (fn [{:keys [result seen] :as state} [n machine]] 170 | (let [class (classify machine)] 171 | (if (seen class) 172 | (if (-> result peek first (= :old)) 173 | (update-in state [:result (dec (count result)) 1 class] 174 | (fnil inc 0)) 175 | (update state :result conj [:old {class 1}])) 176 | (cond-> {:result (conj result [:new class n machine]) 177 | :seen (conj seen class)} 178 | (= :unknown class) 179 | (reduced))))) 180 | {:result [] 181 | :seen #{}}) 182 | (:result))) 183 | -------------------------------------------------------------------------------- /src/halting/machines.clj: -------------------------------------------------------------------------------- 1 | (ns halting.machines) 2 | 3 | 4 | 5 | (def the-empty-machine 6 | []) 7 | 8 | (defn number-of-state-specs 9 | "The number of possible specifications of a single state, 10 | given the total number of states in the machine." 11 | [state-count] 12 | (let [x (inc state-count)] (* x x 16))) 13 | 14 | (defn total-machines-for-state-count 15 | [state-count] 16 | (apply *' 17 | (repeat state-count (number-of-state-specs state-count)))) 18 | 19 | ;; ordering is the natural lexicographic ordering on the 20 | ;; representation we have 21 | (defn nat->machine 22 | [n] 23 | {:pre [(integer? n) (not (neg? n))]} 24 | (loop [state-count 0 25 | n n] 26 | (let [machine-count (total-machines-for-state-count state-count)] 27 | (if (< n machine-count) 28 | (let [base (number-of-state-specs state-count) 29 | 30 | state-indices 31 | (loop [n n 32 | indices ()] 33 | (if (= (count indices) state-count) 34 | indices 35 | (recur (quot n base) 36 | (cons (mod n base) indices)))) 37 | 38 | to-states (inc state-count)] 39 | (mapv (fn [index] 40 | ;; shut up 41 | (let [state1 (mod index to-states) 42 | index (quot index to-states) 43 | dir1 (mod index 2) 44 | index (quot index 2) 45 | write1 (mod index 2) 46 | index (quot index 2) 47 | state0 (mod index to-states) 48 | index (quot index to-states) 49 | dir0 (mod index 2) 50 | index (quot index 2) 51 | write0 (mod index 2)] 52 | [[(long write0) ([:left :right] dir0) (long state0)] 53 | [(long write1) ([:left :right] dir1) (long state1)]])) 54 | state-indices)) 55 | (recur (inc state-count) (- n machine-count)))))) 56 | 57 | (defn machine->nat 58 | [machine] 59 | (->> (range (count machine)) 60 | (map total-machines-for-state-count) 61 | (reduce +') 62 | (+ (let [base (number-of-state-specs (count machine)) 63 | to-states (inc (count machine))] 64 | (->> machine 65 | (map (fn [[[write0 dir0 state0] 66 | [write1 dir1 state1]]] 67 | (-> write0 68 | (* 2) 69 | (+ ({:left 0 :right 1} dir0)) 70 | (* to-states) 71 | (+ state0) 72 | (* 2) 73 | (+ write1) 74 | (* 2) 75 | (+ ({:left 0 :right 1} dir1)) 76 | (* to-states) 77 | (+ state1)))) 78 | (reduce #(-> %1 (*' base) (+' %2)) 0)))))) 79 | 80 | (defn inc-machine 81 | [machine] 82 | ;; TODO 83 | ) 84 | 85 | (defn permutations 86 | [coll] 87 | (if (empty? coll) 88 | [()] 89 | (for [x coll 90 | xs (permutations (remove #{x} coll))] 91 | (cons x xs)))) 92 | 93 | (defn TM-permutations 94 | [TM] 95 | (let [normal-states (dec (count TM))] 96 | (if (<= normal-states 1) 97 | [TM] 98 | (for [perm (permutations (range 1 (count TM)))] 99 | (mapv (fn [reads] 100 | (mapv (fn [[write dir state]] 101 | [write dir 102 | (if (or (zero? state) 103 | (= (count TM) state)) 104 | state 105 | (inc (.indexOf ^java.util.List perm state)))]) 106 | reads)) 107 | (cons (first TM) 108 | (map TM perm))))))) 109 | 110 | (defn canonize 111 | [TM] 112 | (let [halt-state (count TM)] 113 | (->> TM 114 | (mapv (fn [reads] 115 | (mapv (fn [[write dir state :as spec]] 116 | (if (= halt-state state) 117 | [0 :left state] 118 | spec)) 119 | reads))) 120 | (TM-permutations) 121 | (reduce (fn [tm1 tm2] 122 | (if (neg? (compare tm1 tm2)) 123 | tm1 124 | tm2)))))) 125 | 126 | (defn canonical? 127 | "Returns true if the states are labeled canonically, and halting 128 | actions always write 0 and move left." 129 | [TM] 130 | (= TM (canonize TM))) 131 | 132 | (def greek 133 | "αβγδεζηθικλμνξπρστυφχψω") 134 | 135 | (defn print-markdown 136 | [machine] 137 | (let [state-name (if (<= (count machine) (count greek)) 138 | (mapv str greek) 139 | str) 140 | state-name (fn [state] (if (= state (count machine)) 141 | "H" 142 | (state-name state)))] 143 | (println "| State | Read 0 | | Read 1 | |") 144 | (println "|------:|:-------|:------|:-------|-------|") 145 | (doseq [[[[write0 dir0 state0] 146 | [write1 dir1 state1]] 147 | state] 148 | (map vector machine (range))] 149 | (printf "| **%s** | %s | **%s** | %s | **%s** |\n" 150 | (state-name state) 151 | (case dir0 152 | :left (str \← write0) 153 | :right (str write0 \→)) 154 | (state-name state0) 155 | (case dir1 156 | :left (str \← write1) 157 | :right (str write1 \→)) 158 | (state-name state1))))) 159 | 160 | (defn print-morphett 161 | "Prints the given machine in the syntax use at 162 | http://morphett.info/turing/turing.html." 163 | [machine] 164 | (let [symbol ["_" "1"]] 165 | (dotimes [state (count machine)] 166 | (let [[[write0 dir0 state0] 167 | [write1 dir1 state1]] (machine state)] 168 | (printf "%d %s %s %s %s\n" 169 | state (symbol 0) (symbol write0) 170 | ({:left "l" :right "r"} dir0) (if (= (count machine) state0) "halt" state0)) 171 | (printf "%d %s %s %s %s\n" 172 | state (symbol 1) (symbol write1) 173 | ({:left "l" :right "r"} dir1) (if (= (count machine) state1) "halt" state1)))))) 174 | -------------------------------------------------------------------------------- /src/halting/tapes.clj: -------------------------------------------------------------------------------- 1 | (ns halting.tapes 2 | (:refer-clojure :exclude [read])) 3 | 4 | ;; 5 | ;; run length encoding of infinite (eventually homogeneous) sequences 6 | ;; 7 | 8 | (defn rle-repeat 9 | [x] 10 | (list [nil x])) 11 | 12 | (defn rle-first 13 | [rle] 14 | (-> rle first second)) 15 | 16 | (defn rle-cons 17 | [x rle] 18 | (if (= (rle-first rle) x) 19 | (->> rle rest (cons (update (first rle) 0 #(some-> % inc)))) 20 | (cons [1 x] rle))) 21 | 22 | (defn rle-rest 23 | [rle] 24 | (if (= 1 (ffirst rle)) 25 | (rest rle) 26 | (->> rle rest (cons (update (first rle) 0 #(some-> % dec)))))) 27 | 28 | (defn rle-take 29 | [n rle] 30 | (when (pos? n) 31 | (lazy-seq 32 | (cons (rle-first rle) 33 | (rle-take (dec n) 34 | (rle-rest rle)))))) 35 | 36 | ;; 37 | ;; Infinite tape; a zipper using the run-length encoding 38 | ;; 39 | 40 | (def zero-tape 41 | "The tape filled with 0's." 42 | [(rle-repeat 0) 0 (rle-repeat 0)]) 43 | 44 | (defn read 45 | "Reads a 0 or a 1 from the current location on the tape." 46 | [[_ at]] 47 | at) 48 | 49 | (defn write 50 | "Writes a 0 or a 1 to the current location on the tape." 51 | [[left _ right] value] 52 | [left value right]) 53 | 54 | (defn move-left 55 | [[left at right]] 56 | [(rle-rest left) (rle-first left) (rle-cons at right)]) 57 | 58 | (defn move-right 59 | [[left at right]] 60 | [(rle-cons at left) (rle-first right) (rle-rest right)]) 61 | 62 | (defn context 63 | "Returns an opaque value representing a subsection of the given 64 | tape. Only useful for comparing to others for equality. 65 | 66 | If steps-left or steps-right is nil, the entire infinite half 67 | of the tape will be included." 68 | [[left at right] steps-left steps-right] 69 | [(if (nil? steps-left) 70 | left 71 | (rle-take steps-left left)) 72 | at 73 | (if (nil? steps-right) 74 | right 75 | (rle-take steps-right right))]) 76 | -------------------------------------------------------------------------------- /test/halting/machines_test.clj: -------------------------------------------------------------------------------- 1 | (ns halting.machines-test 2 | (:require [clojure.test.check.clojure-test :refer [defspec]] 3 | [clojure.test.check.generators :as gen] 4 | [clojure.test.check.properties :as prop] 5 | [halting.analysis :as analysis] 6 | [halting.machines :as machines])) 7 | 8 | (defn gen-TM-state 9 | [total-states] 10 | (gen/vector (gen/tuple (gen/choose 0 1) 11 | (gen/elements [:left :right]) 12 | (gen/choose 0 total-states)) 13 | 2)) 14 | 15 | (defn gen-TM* 16 | [state-count] 17 | (gen/vector (gen-TM-state state-count) state-count)) 18 | 19 | (def gen-TM 20 | (gen/bind gen/nat gen-TM*)) 21 | 22 | (def gen-small-TM 23 | (gen/bind (gen/large-integer* {:min 0 :max 6}) 24 | gen-TM*)) 25 | 26 | (defspec bijection-spec-1 27 | (prop/for-all [nat (gen/large-integer* {:min 0})] 28 | (-> nat machines/nat->machine machines/machine->nat 29 | (= nat)))) 30 | 31 | (defspec bijection-spec-2 32 | (prop/for-all [machine gen-TM] 33 | (-> machine machines/machine->nat machines/nat->machine 34 | (= machine)))) 35 | 36 | (defspec canonization-spec 37 | (prop/for-all [machine gen-small-TM] 38 | (<= (-> machine machines/canonize machines/machine->nat) 39 | (-> machine machines/machine->nat)))) 40 | 41 | (defspec canonize-preserves-behavior-spec 42 | (prop/for-all [machine gen-small-TM] 43 | (= (analysis/run-on-empty-tape machine 1000) 44 | (analysis/run-on-empty-tape (machines/canonize machine) 1000)))) 45 | --------------------------------------------------------------------------------