/
decider.go
389 lines (367 loc) · 14.5 KB
/
decider.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
package decider
import (
"fmt"
ATS "github.com/Iijil1/Bruteforce-CTL/abstracttapestack"
TM "github.com/Iijil1/Bruteforce-CTL/turingmachines"
)
type Status int
const TODO Status = 0
const DONE Status = 1
const HALTSATANYDEPTH Status = 2
const ABORTED Status = 3
const HALTSBECAUSEDEPTHLIMIT Status = 4
type Configuration struct {
State TM.State
Direction TM.Direction
StackList ATS.StackCollection
TM TM.TuringMachine
LeftTape ATS.AbstractStack
RightTape ATS.AbstractStack
Status Status
Predecessors []*Configuration
Successors []*Configuration
PrintHelper int
Depth int
DepthLimitCausingHalt int
}
func (c *Configuration) String() string {
stateString := c.State.String()
if c.Direction == TM.RIGHT {
stateString = stateString + ">"
} else {
stateString = "<" + stateString
}
return c.LeftTape.NeatString(false) + stateString + c.RightTape.NeatString(true)
}
type ConfigurationKey struct {
State TM.State
Direction TM.Direction
LeftTape ATS.AbstractStack
RightTape ATS.AbstractStack
}
type RunInfo struct {
Steps int
ConfigurationMap map[ConfigurationKey]*Configuration
StartConfig *Configuration
MaxDepth int
}
func BruteforceCTL(tm TM.TuringMachine, options DeciderOptions, printOptions PrintOptions) (bool, RunInfo) {
stackList := ATS.NewStackCollection(tm, options.StackHeuristics)
startConfiguration := &Configuration{State: tm.StartState(), TM: tm, Direction: TM.RIGHT, StackList: stackList, LeftTape: stackList.EmptyStack(), RightTape: stackList.EmptyStack(), Status: TODO, Depth: 0}
todoStack := fifoStack{}
todoStack.push(startConfiguration)
redoStack := []*Configuration{}
forcedLines := []*Configuration{}
configurationMap := make(map[ConfigurationKey]*Configuration)
configurationMap[ConfigurationKey{tm.StartState(), TM.RIGHT, stackList.EmptyStack(), stackList.EmptyStack()}] = startConfiguration
depthlimit := options.InitialDepth
maxDepth := 0
// specialSights := 0
for steps := 0; steps < options.StepLimit; steps += 1 {
var currentConfiguration *Configuration
if len(redoStack) > 0 {
currentConfiguration = redoStack[len(redoStack)-1]
redoStack = redoStack[:len(redoStack)-1]
} else if len(forcedLines) > 0 {
currentConfiguration = forcedLines[len(forcedLines)-1]
forcedLines = forcedLines[:len(forcedLines)-1]
} else if todoStack.length > 0 {
currentConfiguration = todoStack.pop()
} else {
if printOptions.Success {
fmt.Printf("After %v steps with maximum depth %v, we found a closed set of configurations. This machine never halts.\n", steps, maxDepth)
if printOptions.StatusCountInfo {
todo, done, halts, aborted, haltsDepth := statusCounting(configurationMap)
fmt.Printf("TODO: %v, DONE: %v, HALTS: %v, ABORTED: %v, HALTSBECAUSEDEPTH: %v\n", todo, done, halts, aborted, haltsDepth)
}
}
return true, RunInfo{Steps: steps, ConfigurationMap: configurationMap, StartConfig: startConfiguration, MaxDepth: maxDepth}
}
if currentConfiguration.Status == DONE {
steps -= 1
continue
}
if currentConfiguration.Status == HALTSATANYDEPTH || currentConfiguration.Status == ABORTED || (currentConfiguration.Status == HALTSBECAUSEDEPTHLIMIT && currentConfiguration.DepthLimitCausingHalt == depthlimit) {
steps -= 1
for _, successor := range currentConfiguration.Successors {
if (successor.Status == DONE || successor.Status == TODO) && successor != startConfiguration {
abort := true
for _, predessor := range successor.Predecessors {
if predessor.Status == DONE {
abort = false
break
}
}
if abort {
successor.Status = ABORTED
redoStack = append(redoStack, successor)
if printOptions.SettingAborted {
fmt.Printf("\t\tIt is now unnecessary to check %v as it has no predecessors among DONE configurations\n", successor)
}
}
}
}
continue
}
if printOptions.CurrentConfiguration {
fmt.Printf("Checking %v\n", currentConfiguration)
}
currentConfiguration.Status = DONE
forcedLine := false
if options.ForcedLines {
forcedLine = true
}
var possibleSymbols []TM.Symbol
var popStackOptions []ATS.AbstractStack
if currentConfiguration.Direction == TM.RIGHT {
possibleSymbols, popStackOptions = currentConfiguration.RightTape.Pop()
} else {
possibleSymbols, popStackOptions = currentConfiguration.LeftTape.Pop()
}
if len(possibleSymbols) > 1 {
forcedLine = false
}
possibleSuccessors := make([]ConfigurationKey, len(possibleSymbols))
successorPriority := make([]int, len(possibleSymbols))
overallFailure := false
overallFailureBecauseOfDepthLimit := true
for i := range possibleSymbols {
popOptionFailure := false
popOptionFailureBecauseOfDepthLimit := false
var nextRightStack, nextLeftStack ATS.AbstractStack
if currentConfiguration.Direction == TM.RIGHT {
nextRightStack = popStackOptions[i]
nextLeftStack = currentConfiguration.LeftTape
} else {
nextRightStack = currentConfiguration.RightTape
nextLeftStack = popStackOptions[i]
}
if printOptions.DetailedHaltDecision {
fmt.Printf("\tOption %v%v%v%v", nextLeftStack.NeatString(false), currentConfiguration.State, possibleSymbols[i], nextRightStack.NeatString(true))
}
nextSymbol, nextDirection, nextState := tm.Transition(currentConfiguration.State, possibleSymbols[i])
if nextState.IsHalt() {
if printOptions.DetailedHaltDecision {
fmt.Printf(" halts immediatly\n")
}
popOptionFailure = true
} else if nextState.IsLoop() {
if printOptions.DetailedHaltDecision {
possibleSuccessors[i] = ConfigurationKey{}
fmt.Printf(" loops in place forever\n")
}
} else {
if printOptions.DetailedHaltDecision {
fmt.Printf(" can turn into:\n")
}
var pushStackOptions []ATS.AbstractStack
if nextDirection == TM.RIGHT {
pushStackOptions = nextLeftStack.Push(nextSymbol)
} else {
pushStackOptions = nextRightStack.Push(nextSymbol)
}
if len(pushStackOptions) > 1 {
forcedLine = false
}
popOptionFailure = true
for _, stack := range pushStackOptions {
if nextDirection == TM.RIGHT {
nextLeftStack = stack
} else {
nextRightStack = stack
}
if printOptions.DetailedHaltDecision {
stateString := nextState.String()
if nextDirection == TM.RIGHT {
stateString = stateString + ">"
} else {
stateString = "<" + stateString
}
fmt.Printf("\t\tOption %v%v%v", nextLeftStack.NeatString(false), stateString, nextRightStack.NeatString(true))
}
successorKey := ConfigurationKey{nextState, nextDirection, nextLeftStack, nextRightStack}
if successor, exists := configurationMap[ConfigurationKey{nextState, nextDirection, nextLeftStack, nextRightStack}]; !exists {
if printOptions.DetailedHaltDecision {
fmt.Printf(" which can work because it was never seen before\n")
}
if currentConfiguration.Depth < depthlimit {
if successorPriority[i] < 2 {
possibleSuccessors[i] = successorKey
successorPriority[i] = 2
}
popOptionFailure = false
continue
} else {
if printOptions.DetailedHaltDecision {
fmt.Printf("\t\tBut the depth is too big, so we will not consider that yet\n")
}
popOptionFailureBecauseOfDepthLimit = true
}
} else {
if printOptions.DetailedHaltDecision {
fmt.Printf(" which was seen before")
}
if successor.Status != HALTSATANYDEPTH && successor.Status != HALTSBECAUSEDEPTHLIMIT {
if printOptions.DetailedHaltDecision {
fmt.Printf(" but isn't known to halt yet, so we'll take it\n")
}
if successorPriority[i] < 3 {
possibleSuccessors[i] = successorKey
successorPriority[i] = 3
}
popOptionFailure = false
break
}
if successor.Status == HALTSBECAUSEDEPTHLIMIT {
if printOptions.DetailedHaltDecision {
fmt.Printf(" but was found to halt because of the depthlimit\n")
}
if successor.DepthLimitCausingHalt < depthlimit {
if printOptions.DetailedHaltDecision {
fmt.Printf("\t\tHowever, that was at a lower depthlimit, so we might try it again\n")
}
if successorPriority[i] < 1 ||
(successorPriority[i] == 1 && successor.DepthLimitCausingHalt < configurationMap[possibleSuccessors[i]].DepthLimitCausingHalt) {
possibleSuccessors[i] = successorKey
successorPriority[i] = 1
}
popOptionFailure = false
continue
}
popOptionFailureBecauseOfDepthLimit = true
}
if printOptions.DetailedHaltDecision {
fmt.Printf(" and been found to halt\n")
}
}
}
}
if popOptionFailure {
overallFailure = true
if !popOptionFailureBecauseOfDepthLimit {
overallFailureBecauseOfDepthLimit = false
break
}
}
}
if overallFailure {
if overallFailureBecauseOfDepthLimit == false {
currentConfiguration.Status = HALTSATANYDEPTH
if currentConfiguration == startConfiguration {
if printOptions.FoundStartHalting {
fmt.Printf("\tWe found that the starting configuration has the possibility to halt. We can't prove this machine doesn't halt. That took %v steps and %v configurations.\n", steps, len(configurationMap))
}
return false, RunInfo{Steps: steps, StartConfig: startConfiguration, MaxDepth: maxDepth}
}
if printOptions.FoundHalting {
fmt.Println("\tWe found that this configuration has the possibility to halt. We will mark that and try the predecessors again.")
}
} else {
currentConfiguration.Status = HALTSBECAUSEDEPTHLIMIT
currentConfiguration.DepthLimitCausingHalt = depthlimit
if currentConfiguration == startConfiguration {
if options.DepthIncrease > 0 {
if printOptions.DepthLimitIncrease {
fmt.Printf("\tWe found that the starting configuration has the possibility to halt with depthlimit %v. That took %v steps. Increasing the limit to %v.\n", depthlimit, steps, depthlimit+options.DepthIncrease)
if printOptions.StatusCountInfo {
todo, done, halts, aborted, haltsDepth := statusCounting(configurationMap)
fmt.Printf("TODO: %v, DONE: %v, HALTS: %v, ABORTED: %v, HALTSBECAUSEDEPTH: %v\n", todo, done, halts, aborted, haltsDepth)
}
}
depthlimit += options.DepthIncrease
currentConfiguration.Status = TODO
todoStack.push(currentConfiguration)
continue
} else {
if printOptions.FoundStartHalting {
fmt.Printf("\tWe found that the starting configuration has the possibility to halt with the given depth limit. We can't prove this machine doesn't halt. That took %v steps.\n", steps)
if printOptions.StatusCountInfo {
todo, done, halts, aborted, haltsDepth := statusCounting(configurationMap)
fmt.Printf("TODO: %v, DONE: %v, HALTS: %v, ABORTED: %v, HALTSBECAUSEDEPTH: %v\n", todo, done, halts, aborted, haltsDepth)
}
}
return false, RunInfo{Steps: steps, StartConfig: startConfiguration, MaxDepth: maxDepth}
}
}
if printOptions.FoundHalting {
fmt.Printf("\tWe found that this configuration has the possibility to halt at depthlimit %v. We will mark that and try the predecessors again.\n", depthlimit)
}
}
for _, predecessor := range currentConfiguration.Predecessors {
if predecessor.Status == DONE || predecessor.Status == TODO {
predecessor.Status = TODO
redoStack = append(redoStack, predecessor)
if printOptions.FoundHalting && printOptions.AddedConfiguration {
fmt.Printf("\t\tAdding %v\n", predecessor)
}
} else {
if printOptions.FoundHalting && printOptions.AddedConfiguration {
fmt.Printf("\t\t%v is a predecessor, but is not added again because of the status %v\n", predecessor, predecessor.Status)
}
}
}
redoStack = append(redoStack, currentConfiguration)
continue
}
currentConfiguration.Successors = []*Configuration{}
for _, successorKey := range possibleSuccessors {
if successorKey == (ConfigurationKey{}) {
continue
}
if successor, exists := configurationMap[successorKey]; !exists {
nextConfiguration := &Configuration{State: successorKey.State, Direction: successorKey.Direction, StackList: currentConfiguration.StackList, TM: currentConfiguration.TM, LeftTape: successorKey.LeftTape, RightTape: successorKey.RightTape, Status: TODO, Depth: currentConfiguration.Depth + 1, Predecessors: []*Configuration{currentConfiguration}, Successors: []*Configuration{}}
if forcedLine {
nextConfiguration.Depth = currentConfiguration.Depth
forcedLines = append(forcedLines, nextConfiguration)
} else {
todoStack.push(nextConfiguration)
}
configurationMap[successorKey] = nextConfiguration
currentConfiguration.Successors = append(currentConfiguration.Successors, nextConfiguration)
if nextConfiguration.Depth > maxDepth {
maxDepth = nextConfiguration.Depth
}
if printOptions.AddedConfiguration {
fmt.Printf("\tAdding %v\n", nextConfiguration)
}
} else {
if successor.Status == ABORTED || successor.Status == HALTSBECAUSEDEPTHLIMIT {
successor.Status = TODO
todoStack.push(successor)
if printOptions.AddedConfiguration {
fmt.Printf("\tAdding %v, checking it is now necessary again\n", successor)
}
} else if printOptions.AddedConfiguration {
fmt.Printf("\tWe reach %v and it was already seen\n", successor)
}
successor.Predecessors = append(successor.Predecessors, currentConfiguration)
currentConfiguration.Successors = append(currentConfiguration.Successors, successor)
}
}
}
if printOptions.OutOfSteps {
fmt.Printf("Even after %v steps, we keep finding new configurations. We can't decide this machine.\n", options.StepLimit)
if printOptions.StatusCountInfo {
todo, done, halts, aborted, haltsDepth := statusCounting(configurationMap)
fmt.Printf("TODO: %v, DONE: %v, HALTS: %v, ABORTED: %v, HALTSBECAUSEDEPTH: %v\n", todo, done, halts, aborted, haltsDepth)
}
}
return false, RunInfo{Steps: options.StepLimit, ConfigurationMap: configurationMap, StartConfig: startConfiguration, MaxDepth: maxDepth}
}
func statusCounting(configurationMap map[ConfigurationKey]*Configuration) (todo int, done int, halts int, aborted int, haltsDepth int) {
for _, conf := range configurationMap {
switch conf.Status {
case TODO:
todo += 1
case DONE:
done += 1
case HALTSATANYDEPTH:
halts += 1
case ABORTED:
aborted += 1
case HALTSBECAUSEDEPTHLIMIT:
haltsDepth += 1
}
}
return
}