-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlec-computation.typ
More file actions
1690 lines (1333 loc) · 56.6 KB
/
lec-computation.typ
File metadata and controls
1690 lines (1333 loc) · 56.6 KB
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
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#import "theme2.typ": *
#show: slides.with(
title: [Formal Methods in Software Engineering],
subtitle: "Theory of Computation",
date: "Spring 2026",
authors: "Konstantin Chukharev",
)
#import "common-lec.typ": *
#let yields = $scripts(arrow.double)$
#let tapestart = box(baseline: 1pt)[$triangle.small.r$]
#let Blank = math.class("normal", sym.square.stroked)
= Languages
== Why Theory of Computation?
We have studied _propositional logic_, _SAT_, and _first-order logic_. \
A natural question recurs: *"Which problems can be solved automatically?"*
#Block(color: yellow)[
*The central question of this lecture:*
Given a decision problem (e.g., "is this FOL formula valid?"), does there exist an _algorithm_ that always answers correctly in _finite time_?
]
#grid(
columns: 2,
column-gutter: 1em,
[
*We have already seen:*
- SAT is NP-complete --- hard, but _decidable_ \
_(an answer always exists, just slowly)_
- FOL validity is only _semi-decidable_ \
_(provability but not refutability)_
- Some SMT theories are _decidable_ \
_(e.g., linear arithmetic over $RR$)_
- FOL over $NN$ (Peano Arithmetic) is undecidable
],
[
*This lecture provides:*
- Formal definition of "computation"
- Precise meaning of _decidability_
- Turing machines as a model of computation
- Rice's theorem: why program verification is #box[_hard in general_]
- Why SMT solvers restrict to specific _theories_
],
)
// #Block[
// You will understand precisely _why_ automated verification requires carefully chosen decidable fragments --- and why general program verification is fundamentally undecidable.
// ]
== Formal Languages
#definition[
An _alphabet_ $Sigma$ is a finite non-empty set of symbols.
A _word_ (or _string_) over $Sigma$ is a finite sequence of symbols from $Sigma$.
The _empty word_ is $epsilon$.
The set of all finite words over $Sigma$ is $Sigma^* = limits(union.big)_(k=0)^infinity Sigma^k$.
A _formal language_ $L subset.eq Sigma^*$ is any set of finite words over $Sigma$.
]
#example[
- $Sigma = {0, 1}$, $L_1 = {0^n 1^n mid(|) n geq 0} = {epsilon, 01, 0011, 000111, dots}$
- $Sigma = {a, b}$, $L_2 = {w mid(|) w "has equal number of" a"s and" b"s"}$
- $L_3 = {"SAT", "HALT", "VALID", dots}$ --- languages encoding decision problems
]
#note[
Every decision problem is a formal language: the set of _yes-instances_.
Solving the problem = deciding membership in the language.
]
== Chomsky Hierarchy
Formal languages are classified into four nested levels:
#align(center)[
#table(
columns: 4,
align: (center, left, left, left),
stroke: (x, y) => if y == 0 { (bottom: 0.8pt) },
table.header([*Type*], [*Class*], [*Recognizing Machine*], [*Example Language*]),
[3], [Regular], [DFA / NFA], [$a^* b^*$, ${ a^n mid(|) n "even" }$],
[2], [Context-Free], [Pushdown Automaton], [${ a^n b^n mid(|) n geq 0 }$],
[1], [Context-Sensitive], [Linear-Bounded TM], [${ a^n b^n c^n mid(|) n geq 0 }$],
[0], [Recursively Enumerable], [Turing Machine], [${ angle.l M, w angle.r mid(|) M "halts on" w }$],
)
]
#Block(color: blue)[
Each level adds _more memory_:
finite states $arrow.r$ unlimited stack $arrow.r$ bounded tape $arrow.r$ infinite tape.
More expressive = harder algorithmic questions about the language class.
]
#note[
The classes are _nested_: every regular language is context-free, every context-free is context-sensitive, etc.
The containments are strict --- each level is strictly more powerful than the one below.
]
== Decision Problems as Languages
#definition[
A _decision problem_ is a question with a "yes" or "no" answer depending on the input.
Formally, the set of inputs for which the answer is "yes" forms a language $L subset.eq Sigma^*$.
_Deciding_ the problem = _recognizing_ the language $L$.
]
#grid(
columns: 1,
gutter: 0.4em,
block(width: 100%)[
*SAT:* Given a CNF formula $phi$, is it satisfiable?
$ "SAT" = { phi mid(|) phi "is a satisfiable Boolean formula" } $
*FOL Validity:* Given a first-order formula $phi$, is it valid?
$ "VALID" = { phi mid(|) phi "is a valid (universally true) FOL formula" } $
*Halting Problem:* Given a TM $M$ and input $w$, does $M$ halt on $w$?
$ "HALT" = { angle.l M, w angle.r mid(|) "TM" M "halts on input" w } $
],
)
#place[
#v(1em)
#Block(color: yellow)[
"Is $w in L$?" and "does the algorithm say yes on input $w$?" are _the same question_.
// Formal language theory gives us the mathematics to study the _limits of computation_.
]
]
== Language Complexity Classes
#v(-1em)
#align(center)[
#cetz.canvas({
import cetz.draw: *
scale(95%)
circle((0, 0), radius: (0.8, 0.4))
circle((0, 0.4), radius: (1.4, 0.8))
circle((0, 0.8), radius: (2, 1.2))
circle((0, 1.2), radius: (2.6, 1.6))
circle((0, 2.4), radius: (4, 2.8), stroke: blue)
circle((0, 1.2), radius: (4, 2.8), stroke: red)
content((0, 0))[Finite]
content((0, .7))[Regular]
content((0, 1.55))[Context-Free]
content((0, 2.3))[Context-Sensitive]
content((0, 3.2))[#set text(fill: purple); Decidable = $"RE" inter "co-RE"$]
content((0, 4.4))[#set text(fill: blue); Recursively Enumerable (RE)]
content((0, -1))[#set text(fill: red); co-RE]
circle((2.5, 2.5), radius: 3pt, fill: yellow)
content((2.5, 2.5), anchor: "north-west", padding: 5pt)[SAT]
circle((3.2, 3.8), radius: 3pt, fill: yellow)
content((3.2, 3.8), anchor: "south-west", padding: 5pt)[HALT]
circle((2.8, 5), radius: 3pt, fill: yellow)
content((2.8, 5), anchor: "south-west", padding: 5pt)[$"REGULAR"_"TM"$]
})
]
#place[
#v(1em)
#note[
*SAT* is decidable (NP-complete).
*HALT* is recognizable but _not_ decidable: a TM can confirm halting by simulation, but cannot confirm non-halting.
$"REGULAR"_"TM"$ = "does TM $M$ recognize a regular language?" --- in neither RE nor co-RE.
]
]
= Machines
== Finite Automata
#definition[
A _Deterministic Finite Automaton_ (DFA) is a 5-tuple $(Q, Sigma, delta, q_0, F)$:
- $Q$ --- finite set of _states_
- $Sigma$ --- _input alphabet_
- $delta: Q times Sigma to Q$ --- _transition function_
- $q_0 in Q$ --- _start state_
- $F subset.eq Q$ --- set of _accepting states_
A DFA processes input left-to-right, one symbol at a time, and accepts if it ends in an accepting state.
DFAs recognize exactly the _regular_ languages (Type 3 in the Chomsky hierarchy).
]
== DFA Example: Even Number of 0s
#example[
Automaton $cal(A)$ over $Sigma = {0, 1}$ recognizing $cal(L)(cal(A)) = { w mid(|) w "has an even number of 0s" }$.
States: $q_0$ = "seen even many 0s" (start, accepting), $q_1$ = "seen odd many 0s".
- Reading a *0*: flip parity (switch state).
- Reading a *1*: parity unchanged (stay in state).
#let aut = (
q0: (q1: 0, q0: 1),
q1: (q0: 0, q1: 1),
)
#grid(
columns: 2,
column-gutter: 3em,
align: horizon,
finite.transition-table(aut),
finite.automaton(
aut,
final: ("q0",),
style: (
state: (radius: 0.5, extrude: 0.8),
transition: (curve: 0.4),
q0-q0: (anchor: top + left),
q1-q1: (anchor: top + right),
),
),
)
]
== Turing Machines
Informally, a Turing machine is a _finite-state_ machine with an _infinite tape_ and a _head_ that can read and write symbols.
Initially, the tape contains the _input_ string, the rest are blanks, and the machine is in the _start_ state.
At each step, the machine reads the symbol under the head, changes the state, writes a new symbol, and moves the head left or right.
When the machine reaches the _accept_ or _reject_ state, it immediately halts.
#note[
If the machine never reaches the _accept_ or _reject_ state, it _loops_ forever.
]
#v(1em)
#align(center)[
#import fletcher: diagram, edge, node
#diagram(
node-corner-radius: 2pt,
edge-stroke: 1pt,
mark-scale: 70%,
blob((0, 0), [Input], tint: blue, name: <input>),
blob((1, 0), [Turing \ machine], tint: purple, name: <tm>),
blob((2, -0.5), [Accept], tint: green, name: <accept>),
blob((2, 0), [Loop], tint: yellow, name: <loop>),
blob((2, 0.5), [Reject], tint: red, name: <reject>),
edge(<input>, <tm>, "-|>"),
edge(<tm>, <accept>, "-|>"),
edge(<tm>, <loop>, "-|>"),
edge(<tm>, <reject>, "-|>"),
render: (grid, nodes, edges, options) => {
import fletcher: cetz
cetz.canvas({
// this is the default code to render the diagram
fletcher.draw-diagram(grid, nodes, edges, debug: options.debug)
let n-accept = fletcher.find-node(nodes, <accept>)
let n-loop = fletcher.find-node(nodes, <loop>)
let n-reject = fletcher.find-node(nodes, <reject>)
fletcher.get-node-anchor(
n-accept,
0deg,
pa => {
fletcher.get-node-anchor(
n-loop,
0deg,
pl => {
fletcher.get-node-anchor(
n-reject,
0deg,
pr => {
cetz.decorations.brace((rel: (1pt, 5pt), to: pa), (rel: (1pt, 5pt), to: pl), name: "b1")
cetz.decorations.brace((rel: (1pt, -5pt), to: pl), (rel: (1pt, -5pt), to: pr), name: "b2")
},
)
},
)
},
)
cetz.draw.content("b1.content", anchor: "west")[Does not reject]
cetz.draw.content("b2.content", anchor: "west")[Does not accept]
})
},
)
]
== TM Formal Definition
#definition[
Turing Machine (TM) is a 7-tuple $(Q, Sigma, Gamma, delta, q_0, q_"acc", q_"rej")$ where:
- $Gamma$ is a _tape alphabet_ (including blank symbol $square in Gamma$),
- $Sigma subset.eq Gamma$ is a _input alphabet_,
- $delta: Q times Gamma to Q times Gamma times {L, R}$ is a transition function,
- $q_"acc"$ and $q_"rej"$ are the _accept_ and _reject_ states.
TM recognizes _recursively enumerable_ languages (Type 0).
]
== TM Language and Acceptance
#definition[
The language _recognized_ by $M$, written $cal(L)(M)$, is the set of inputs $M$ accepts:
$ cal(L)(M) = { w in Sigma^* mid(|) M "halts in state" q_"acc" "on input" w } $
For inputs _not_ in $cal(L)(M)$, the machine either _rejects_ (halts in $q_"rej"$) or _loops forever_.
]
#Block(color: yellow)[
- A _recognizer_ only needs to accept members of $L$. It may loop forever on non-members.
- A _decider_ must always halt --- it accepts members and _rejects_ non-members.
$ "Decider" = "Recognizer that never loops" $
]
#definition[
A TM is a _decider_ for $L$ if it halts on _every_ input (accepting $L$ and rejecting $overline(L)$).
- A language is _decidable_ (recursive) if it has a decider.
- A language is _recognizable_ (RE) if it has a recognizer.
]
== TM Configuration
// Helper: draw TM read/write head pointing at a tape cell
#let tm-head(pos, state, name: none, ..style) = {
import cetz.draw: *
group(
name: name,
{
let lu = (rel: (-1, -0.5), to: pos)
let ld = (rel: (0, -0.8), to: lu)
let ru = (rel: (1, -0.5), to: pos)
let rd = (rel: (0, -0.8), to: ru)
anchor("state", (rel: (0, -0.7), to: pos))
content("state", state)
line(pos, lu, ld, rd, ru, ..style, close: true)
},
)
}
#definition[
A _configuration_ describes the complete state of a TM at a given moment:
$ (u ; q ; v) quad u, v in Gamma^*, quad q in Q $
- $u$ --- tape contents to the _left_ of the head
- $q$ --- current _state_
- $v$ --- tape contents from the head _rightward_ (head reads $v[0]$)
]
#example[
Configuration $(u ; q ; a v)$ is visualized as:
#align(center)[
#cetz.canvas({
import cetz.draw: *
scale(55%)
content((-0.5, 0.5))[$tapestart$]
rect((0, 0), (rel: (2, 1)), name: "u")
content("u.center")[$u$]
rect((2, 0), (rel: (1, 1)), name: "a", fill: orange.lighten(80%))
content("a.center")[$a$]
rect((3, 0), (rel: (2, 1)), name: "v")
content("v.center")[$v$]
for-each-anchor("a", name => {}, exclude: ("center",))
line((0, 0), (5.5, 0))
line((0, 1), (5.5, 1))
line((5.5, 0), (6.2, 0), stroke: (dash: "dashed"))
line((5.5, 1), (6.2, 1), stroke: (dash: "dashed"))
tm-head((rel: (0, -1pt), to: "a.south"))[$q$]
})
]
The head reads $a$; the next transition depends on $(q, a)$.
]
== TM Computation
#definition[
A _computation_ of TM $M$ on input $w$ is a sequence of configurations:
$ C_1 yields C_2 yields dots.c yields C_n $
- $C_1 = (#tapestart ; q_0 ; w)$ --- _start configuration_
- $C_i yields C_{i+1}$ --- "$C_i$ yields $C_{i+1}$ in one step"
- $C_n$ is a _halting configuration_ (state is $q_"acc"$ or $q_"rej"$)
]
The relation $yields^*$ (yields in any number of steps) is the reflexive-transitive closure of $yields$.
#Block(color: yellow)[
*Intuition:* Think of a computation as a "snapshot sequence" of the machine.
Each snapshot captures the tape contents, the current state, and the head position.
The machine moves from snapshot to snapshot by applying one transition.
]
#note[
A _terminating_ computation always reaches $q_"acc"$ or $q_"rej"$.
A _looping_ computation produces an infinite sequence $C_1 yields C_2 yields dots.c$ that never halts.
]
== TM Yields Relation
How does one configuration yield the next?
#definition[
Let $u, v in Gamma^*$, $a, b, c in Gamma$, $q_i, q_j in Q$.
*Move left* ($L$): $(u a ; q_i ; b v) yields (u ; q_j ; a c v)$ when $delta(q_i, b) = (q_j, c, L)$
*Move right* ($R$): $(u ; q_i ; b a v) yields (u c ; q_j ; a v)$ when $delta(q_i, b) = (q_j, c, R)$
In both cases: overwrite $b$ with $c$, move the head, change to state $q_j$.
]
#align(center)[
#cetz.canvas({
import cetz.draw: *
scale(50%)
// Left move: before
rect((0, 0), (rel: (2, 1)), name: "u")
content("u.center")[$u$]
rect((2, 0), (rel: (1, 1)), name: "a")
content("a.center")[$a$]
rect((3, 0), (rel: (1, 1)), name: "b", fill: orange.lighten(80%))
content("b.center")[$b$]
rect((4, 0), (rel: (2, 1)), name: "v")
content("v.center")[$v$]
line((-0.3, 0), (6.3, 0))
line((-0.3, 1), (6.3, 1))
tm-head((rel: (0, -1pt), to: "b.south"))[$q_i$]
translate(x: 8)
content((-1, -0.4))[$limits(yields)_(delta(q_i, b) = (q_j, c, L))$]
// Left move: after
rect((0, 0), (rel: (2, 1)), name: "u")
content("u.center")[$u$]
rect((2, 0), (rel: (1, 1)), name: "a", fill: orange.lighten(80%))
content("a.center")[$a$]
rect((3, 0), (rel: (1, 1)), name: "c")
content("c.center")[$c$]
rect((4, 0), (rel: (2, 1)), name: "v")
content("v.center")[$v$]
line((-0.3, 0), (6.3, 0))
line((-0.3, 1), (6.3, 1))
tm-head((rel: (0, -1pt), to: "a.south"))[$q_j$]
translate(x: 9)
// Right move: before
rect((0, 0), (rel: (2, 1)), name: "u")
content("u.center")[$u$]
rect((2, 0), (rel: (1, 1)), name: "b", fill: orange.lighten(80%))
content("b.center")[$b$]
rect((3, 0), (rel: (1, 1)), name: "a")
content("a.center")[$a$]
rect((4, 0), (rel: (2, 1)), name: "v")
content("v.center")[$v$]
line((-0.3, 0), (6.3, 0))
line((-0.3, 1), (6.3, 1))
tm-head((rel: (0, -1pt), to: "b.south"))[$q_i$]
translate(x: 8)
content((-1, -0.4))[$limits(yields)_(delta(q_i, b) = (q_j, c, R))$]
// Right move: after
rect((0, 0), (rel: (2, 1)), name: "u")
content("u.center")[$u$]
rect((2, 0), (rel: (1, 1)), name: "c")
content("c.center")[$c$]
rect((3, 0), (rel: (1, 1)), name: "a", fill: orange.lighten(80%))
content("a.center")[$a$]
rect((4, 0), (rel: (2, 1)), name: "v")
content("v.center")[$v$]
line((-0.3, 0), (6.3, 0))
line((-0.3, 1), (6.3, 1))
tm-head((rel: (0, -1pt), to: "a.south"))[$q_j$]
})
]
#note[
*Left-end special case:* If the head is at the tape start and the transition says "move left", the head stays in place:
$( #tapestart ; q_i ; b v) yields (#tapestart ; q_j ; c v)$ when $delta(q_i, b) = (q_j, c, L)$.
]
== TM Tape Visualization
#example[
Initial tape state for ${ 0^n 1^n }$ TM on input $w = 0011$:
#align(center)[
#cetz.canvas({
import cetz.draw: *
scale(90%)
let cells = ("", "0", "0", "1", "1", " ", " ", " ")
for (i, c) in cells.enumerate() {
let fill-color = if c == "0" or c == "1" { blue.lighten(80%) } else { white }
rect((i, 0), (i + 1, 1), fill: fill-color, stroke: 0.6pt)
content((i + 0.5, 0.5))[#c]
}
content((-0.3, 0.5))[$tapestart$]
// Draw head pointer
line(
(1.5, -0.15),
(1.1, -0.6),
(1.9, -0.6),
close: true,
fill: orange.lighten(60%),
stroke: 0.6pt,
)
content((1.5, -1.0), anchor: "north")[$q_0$]
})
]
Blue cells = input.
The head (orange triangle) points at the first cell. State is $q_0$.
]
#note[
The machine strategy: repeatedly find the leftmost `0`, mark it `X`, find the matching `1`, mark it `Y`.
Accept when all 0s and 1s are paired.
Reject if counts mismatch.
]
== TM Example: Recognizing *$0^n 1^n$*
Step-by-step configuration trace for input $0011$ ($n = 2$).
// The _underlined_ symbol is at the head.
#v(-0.5em)
#align(center)[
#table(
columns: 3,
align: left,
stroke: (x, y) => if y == 0 { (bottom: 0.8pt) },
table.header([*Step*], [*Configuration $(u ; q ; v)$*], [*Action*]),
[Start], [$(#tapestart ; q_0 ; 0011)$], [Read `0` → write `X`, move right],
[Mark first 0], [$(#tapestart X ; q_1 ; 011)$], [Scan right past 0s to find `1`],
[Found first 1], [$(#tapestart X 0 ; q_2 ; 11)$], [Write `Y`, move left],
[Mark first 1], [$(#tapestart X 0 Y ; q_3 ; 1)$], [Move back to start],
[Back at start], [$(#tapestart ; q_0 ; X 0 Y 1)$], [Read `X` → skip, find next `0`],
[Mark second 0], [$(#tapestart X X ; q_1 ; Y 1)$], [Scan right past Y to find `1`],
[Found second 1], [$(#tapestart X X Y ; q_2 ; 1)$], [Write `Y`, move left],
[All matched], [$(#tapestart X X Y Y ; q_"acc" ; #Blank)$], [Tape is all X/Y --- *Accept!*],
)
]
#v(-0.5em)
#Block(color: yellow)[
*Key insight:*
The tape acts as scratch memory.
At each round, one `0`--`1` pair is matched and "consumed" by overwriting with `X` and `Y`.
This requires $O(n^2)$ steps for input length $2n$.
]
== Machine Comparison
#align(center)[
#table(
columns: 4,
align: (left, center, center, center),
stroke: (x, y) => if y == 0 { (bottom: 0.8pt) },
table.header([*Property*], [*DFA*], [*PDA*], [*TM*]),
[Memory], [None (finite states)], [Stack (LIFO)], [Infinite R/W tape],
[Reading], [Left-to-right,\ each symbol once], [Left-to-right,\ each symbol once], [Arbitrary R/W movement],
[Language class], [Regular], [Context-Free], [RE (or R if decider)],
[Determinism], [Equivalent to NDFA], [NDPDA more powerful], [NTM = DTM],
[Emptiness check], [Decidable], [Decidable], [Undecidable],
[Equality check], [Decidable], [Undecidable], [Undecidable],
[Example language], [$a^n (n "even")$], [$a^n b^n$], [$a^n b^n c^n$],
)
]
#note[
NTM $=$ DTM is the _Church--Turing thesis_ in action: non-determinism does not add power, only speed.
Compare with "NP vs P"!
]
== TM Variants
All the following are equivalent in computational power (they recognize the same class of languages):
#columns(2)[
*Standard TM*
- Single infinite tape, one head
- Alphabet $Gamma$, states $Q$, transition $delta$
*Multi-tape TM*
- $k$ tapes, $k$ heads moving independently
- Easier to program, same power
*Non-deterministic TM (NTM)*
- At each step, choose from multiple transitions
- Accepts if _any_ branch accepts
- Non-determinism $=>$ exponential simulation
#colbreak()
*Two-way infinite tape*
- Tape extends in both directions
- Simulated by storing two tapes on one
*TM with stay*
- Head can stay in place ($S$ move)
- Easier to define, trivially equivalent
*Random Access Machine (RAM)*
- Memory indexed by address
- Polynomially equivalent to standard TM (relevant for complexity!)
]
#Block(color: yellow)[
*Church--Turing thesis:* Any reasonable model of computation computes exactly the same class of functions as a Turing machine.
The thesis is supported by the equivalence of all known models.
]
== Recognizing vs Deciding
There are _two_ types of Turing machines:
+ *Decider* (total TM): always halts on every input.
+ *Recognizer* (general TM): may loop forever on some inputs.
#definition[Recognition][
A TM _recognizes_ language $L$ if it accepts every $w in L$ and does not accept any $w notin L$ (but may loop on non-members).
Such a language is called _recognizable_ (also: Turing-recognizable, recursively enumerable, semi-decidable --- all equivalent). The class of all recognizable languages is *RE*.
]
#definition[Decision][
A TM _decides_ language $L$ if it accepts every $w in L$ and _rejects_ every $w notin L$; it always halts.
Such a language is called _decidable_ (also called: recursive, computable).
The class of all decidable languages is *R* ($subset.neq$~RE).
]
#Block(color: yellow)[
*Key distinction:*
- A recognizer is allowed to loop on non-members.
- A decider _must_ halt and give an answer for every input.
]
== MIU. MU?
#definition[MIU system][
The _MIU system_ is a "formal system" consisting of:
- an alphabet $Sigma = { #`M`, #`I`, #`U` }$,
- a single axiom: `MI`,
- a set of inference rules:
#table(
columns: 3,
column-gutter: 1em,
stroke: (x, y) => if y == 0 { (bottom: .4pt) },
table.header[*Rule*][*Description*][*Example*],
[$x#`I` entails x#`IU`$], [add `U` to the end of any string ending with `I`], [`MI` to `MIU`],
[$#`M`x entails #`M`x x$], [double the string after $M$], [`MIU` to `MIUIU`],
[$x#`III`y entails x#`U`y$], [replace any `III` with `U`], [`MUIIIU` to `MUUU`],
[$x#`UU`y entails x y$], [remove any $U U$], [`MUUU` to `MU`],
)
*Question*: Is `MU` a theorem of the MIU system?
]
= Complexity
== P and NP
#definition[
Class $P$ consists of problems that can be _solved_ in _polynomial time_.
Equivalently, $L in P$ iff $L$ is _decidable_ in polynomial time by a _deterministic_ TM.
]
#examples[
Shortest path, primality testing (AKS algorithm), linear programming.
]
#definition[
Class NP consists of problems where a _certificate_ of a solution ("yes" answer) can be _verified_ in polynomial time.
Equivalently, $L in "NP"$ iff $L$ is _decidable_ in polynomial time by a _non-deterministic_ TM.
Equivalently, $L in "NP"$ iff $L$ is _recognizable_ in polynomial time by a _deterministic_ TM.
]
#examples[
SAT, graph coloring, graph isomorphism, subset sum, knapsack, vertex cover, clique.
]
== NP-Hard and NP-Complete
#definition[
A problem $H$ is _NP-hard_ if every problem $L in "NP"$ is polynomial-time _reducible_ to $H$.
]
#examples[
Halting problem (undecidable), Traveling Salesman Problem (TSP).
]
#definition[
A problem $H$ is _NP-complete_ if:
1. $H in "NP"$
2. $H$ is NP-hard
]
#examples[
SAT, 3-SAT, Hamiltonian path...
Actually, almost all NP problems are NP-complete!
]
#theorem[Cook--Levin][
SAT is NP-complete.
]
== co-NP
#definition[
Complexity class $"co-NP"$ contains problems where _"no"_ instances can be _verified_ in _polynomial time_.
Equivalently, $L in "co-NP"$ iff the complement of $L$ is in $"NP"$:
$ "co-NP" = { L | overline(L) in "NP" } $
]
_Open question_: $"NP" eq.quest "co-NP"$? Implies $"P" neq "NP"$ if false.
#examples[
- *VALID*: Check if a Boolean formula is always true (tautology).
- *UNSAT*: Check if a formula has no satisfying assignment.
]
== Computational Hierarchy
$"P" subset.eq "NP" subset.eq "PSPACE" subset.eq "EXP" subset "R" subset "RE"$
- *RE* --- Languages _accepted_ by any TM. Not all RE languages are decidable.
- *R* = RE $inter$ co-RE --- Languages _decided_ by a halting TM. Closed under complement.
- *EXP* --- Decided in _exponential time_ by a deterministic TM. Closed, proper superset of NP.
- *PSPACE* --- Decided in _polynomial space_. Contains NP and co-NP. QBF is PSPACE-complete.
- *NP* --- Accepted by a _non-deterministic_ TM in polynomial time. SAT, graph coloring, etc.
- *P* --- Decided in _polynomial time_ by a deterministic TM. Primality, BFS/DFS, LP.
#note[
All containments are known, but many strict separations are open: $"P" eq.quest "NP"$, $"NP" eq.quest "co-NP"$, $"NP" eq.quest "PSPACE"$, etc.
]
#place(center)[
#cetz.canvas({
import cetz.draw: *
circle((0, 0), radius: (0.5, 0.5))
circle((0.5, 0), radius: (1, 0.7))
circle((1.5, 0), radius: (2, 1.1))
circle((2, 0), radius: (2.5, 1.3))
circle((2.5, 0), radius: (3, 1.5))
circle((3, 0), radius: (3.5, 1.7))
content((0, 0))[P]
content((1, 0))[NP]
content((2.5, 0))[PSPACE]
content((4, 0))[EXP]
content((5, 0))[R]
content((6, 0))[RE]
})
]
== Polynomial Hierarchy
The _polynomial hierarchy_ PH refines the NP/co-NP picture using alternating quantifiers:
$
Sigma_0^P = Pi_0^P = "P" \
Sigma_(k+1)^P = "NP"^(Sigma_k^P), quad Pi_(k+1)^P = "co-NP"^(Sigma_k^P)
$
- $Sigma_1^P = "NP"$: $exists$ witness, polynomial verifier
- $Pi_1^P = "co-NP"$: $forall$ witnesses, polynomial verifier
- $Sigma_2^P$: $exists forall$ witnesses (e.g., "does $phi$ have an assignment that satisfies all clauses for every setting of some variables?")
- $Pi_2^P$: $forall exists$ witnesses
#v(-0.3em)
#Block(color: yellow)[
#v(-0.3em)
- Model checking for $mu$-calculus is PSPACE-complete.
- Bounded model checking for quantified Boolean formulas (QBF) is PSPACE-complete.
- SMT with quantifiers ($forall/exists$) stratifies across the polynomial hierarchy.
#v(-0.3em)
]
#v(-0.3em)
#note[
If PH collapses to any level (i.e., $Sigma_k^P = Sigma_(k+1)^P$ for some $k$), it implies strong consequences about the structure of NP. In particular, P $=$ NP would collapse the entire hierarchy to P.
]
== Complexity Zoo
#align(center)[
#table(
columns: 3,
align: left,
stroke: (x, y) => if y == 0 { (bottom: 0.8pt) },
table.header([*Problem*], [*Complexity*], [*Practical Approach*]),
[Propositional SAT], [NP-complete], [CDCL SAT solvers (CaDiCaL, MiniSat)],
[QBF (SMT over $2^"nd"$ order)], [PSPACE-complete], [QBF solvers (DepQBF)],
[Linear arith. ($RR$)], [P (LP), NP in SMT], [Simplex + DPLL(T) (Z3, CVC5)],
[Linear arith. ($ZZ$)], [NP-hard], [Branch-and-bound + cutting planes],
[Non-linear arith. ($RR$)], [Decidable (Tarski)], [Cylindrical Algebraic Decomp.],
[Non-linear arith. ($ZZ$)], [Undecidable (Hilbert 10)], [Semi-decidable fragments only],
[FOL validity], [Undecidable (semi-decidable)], [Tableau / resolution (incomplete)],
[Program verification], [Undecidable (Rice)], [Require invariants, bounded checking],
)
]
#Block(color: blue)[
*Why decidable fragments matter:* SMT works precisely because it uses _restricted but decidable_ theories.
Once you add full integer multiplication or quantified arithmetic, the theories become undecidable --- and no complete solver can exist.
#h(1fr)
#text(0.7em)[
See also: #link("https://complexityzoo.net/Petting_Zoo")[Complexity Zoo Petting Zoo]
]
]
= Computability
== The Church--Turing Thesis
#theorem(title: [Church--Turing Thesis], numbering: none)[
Every _effectively computable_ function is computable by a Turing machine.
]
#note[
"Effectively computable" means: can be carried out by a _finite_, _deterministic_, _mechanical_ step-by-step _procedure_, with no creativity or _luck_ required.
]
#note[
This is a *thesis*, not a theorem, since the notion "effectively computable" is informal and cannot be precisely defined.
We cannot formally _prove_ the thesis, but no counterexample has ever been found.
]
#Block(color: teal)[
*Historical note:* In 1936, Alonzo Church ($lambda$-calculus) and Alan Turing (Turing machines) independently formalized computability and proved these models equivalent.
Every other general-purpose model proposed since --- register machines, $mu$-recursive functions, Post systems, Markov normal algorithms --- computes exactly the same class of functions.
]
== Computable Functions
#definition[Computable function][
A partial function $f : NN^k arrow.hook NN$ is _computable_ if there exists a TM $M$ such that:
- If $f(arrow(x))$ is defined: $M$ halts on input $arrow(x)$ with output $f(arrow(x))$.
- If $f(arrow(x))$ is undefined: $M$ loops forever on input $arrow(x)$.
]
_Computable functions:_
- $f(x) = x^2$, $f(x) = x!$, $f(x) = x mod 2$ --- basic arithmetic
- $f(n) =$ the $n$-th prime --- search computable
- $f(n) =$ the $n$-th digit of $pi$ --- BBP formula
- The Ackermann function $A(m, n)$ --- computable but not primitive recursive
_Non-computable functions:_
#definition[Busy Beaver][
$"BB"(n)$ = the maximum number of 1s a _halting_ $n$-state TM over ${0, 1}$ can write on an initially blank tape.
]
== The Busy Beaver
$"BB"$ grows faster than _any_ computable function:
#example[
$"BB"(1) = 1$, $"BB"(2) = 4$, $"BB"(3) = 6$, $"BB"(4) = 13$, $"BB"(5) geq 47{,}176{,}870$.
$"BB"(6)$ is astronomically large (on the order of $10^{10^{10^{10^{18705352}}}}$).
]
#Block(color: orange)[
*BB is not computable.* Suppose we could compute $"BB"(n)$.
Then to check whether an $n$-state TM~$M$ halts on blank tape: run $M$ for $"BB"(n)$ steps.
If $M$ hasn't halted by then, it never will.
But this solves the Halting Problem --- a contradiction.
]
= Decidability
== Decidable Sets
#definition[
Given a universal set $cal(U)$, a set $S subset.eq cal(U)$ is _decidable_ (or _computable_, or~_recursive_) if there exists a computable function $f : cal(U) to {0,1}$ such that $f(x) = 1$ iff $x in S$.
]
#example(title: [Examples])[
- The set of all WFFs is decidable.
- _We can check if a given string is well-formed by recursively verifying the syntax rules._
- For a given finite set $Gamma$ of WFFs, the set ${alpha | Gamma models alpha}$ of all tautological consequences of $Gamma$ is decidable.
- _We can decide $Gamma models alpha$ using a truth table algorithm by enumerating all possible interpretations (at~most~$2^abs(Gamma)$) and checking if each satisfies all formulas in $Gamma$._
- The set of all tautologies is decidable. \
- _It is the set of all tautological consequences of the empty set._
]
== Undecidable Sets
#definition[
A set $S$ is _undecidable_ if it is not decidable.
]
#example[
The existence of _undecidable_ sets can be shown as follows.
An algorithm is completely determined by its _finite_ description.
Thus, there are only _countably many_ effective procedures.
But there are _uncountably many_ subsets of $NN$ (by Cantor's theorem).
Hence, _most_ sets of natural numbers are undecidable --- decidable sets are the exception, not the rule.
]
#Block(color: blue)[
*FM implication:* The set of _valid FOL formulas_ is semi-decidable but not decidable (Church--Turing, 1936). This is why automated theorem provers for full FOL cannot be _complete deciders_ --- they can confirm validity but cannot always confirm invalidity.
Restricted theories (linear arithmetic, equality + uninterpreted functions) _are_ decidable, which is exactly why SMT solvers work!
]
= Undecidability
== Halting Problem
#definition[Halting problem][
Given a program $P$ and an input $x$, determine whether $P$ _halts_ on $x$ (stops after finite time) or _loops_ forever.
]
#theorem[Turing, 1936][
The halting problem is _undecidable_.
]
#proof[sketch][
Suppose there exists a procedure $H$ that decides the halting problem.
We can construct a program $P$ that takes itself as input and runs $H$ on it.
If $H$ says that $P$ halts, then $P$ enters an infinite loop.
If $H$ says that $P$ does not halt, then $P$ halts.
This leads to a contradiction, proving that $H$ cannot exist.
]
== Halting Problem Pseudocode
#shadowed.shadowed(inset: 5pt, radius: 5pt)[
```py
def halts(P, x) -> bool:
"""
Returns True if program P halts on input x.
Returns False if P loops forever.
"""
def self_halts(P):
if halts(P, P):
while True: # loop forever
else:
return # halt
```
]
Observe that ```py halts(self_halts, self_halts)``` cannot return neither ```py True``` nor ```py False```. *Contradiction!*
Thus, the `halts` function _does not exist_ (cannot be implemented), and the halting problem is _undecidable_.
#Block(color: orange)[
*Common confusion:* The halting problem is _undecidable_ for TMs (and all equivalent models).
It does _not_ mean we cannot detect simple loops in practice --- a static analyzer can catch obvious infinite loops.
It means there is _no algorithm_ that correctly decides all possible programs.
]
== Many-One Reductions
#definition[
Language $A$ is _many-one reducible_ to language $B$, written $A scripts(leq)_m B$, if there exists a _total computable_ function $f : Sigma^* to Sigma^*$ such that for all $w in Sigma^*$:
$ w in A iff f(w) in B $
The function $f$ is called the _reduction function_.
]
#theorem[
If $A scripts(leq)_m B$ and $B$ is decidable, then $A$ is decidable.
]
#Block(color: yellow)[
*Contrapositive (more useful):* If $A scripts(leq)_m B$ and $A$ is _undecidable_, then $B$ is _undecidable_.
*Strategy:* To prove $B$ is undecidable, show $"HALT" scripts(leq)_m B$ (or reduce from another problem $A$ known to be undecidable).
]
#pagebreak()
#example[
To prove $E_"TM" = { angle.l M angle.r mid(|) cal(L)(M) = emptyset }$ is undecidable:
Reduce $"HALT"$ to the _complement_ of $E_"TM"$: from $angle.l M, w angle.r$, construct $M'$ that ignores its own input and simulates $M$ on $w$.
Then $M'$ accepts something iff $M$ halts on $w$.
Since $"HALT"$ is undecidable and the reduction is computable, $E_"TM"$ is undecidable.
]
== Rice's Theorem
#definition[Semantic property][
A property $P$ of TMs is _semantic_ if it depends only on the _language_ recognized by the TM, not on the implementation.
Formally: if $cal(L)(M_1) = cal(L)(M_2)$ then $P(M_1) = P(M_2)$.
A property is _non-trivial_ if _some_ TMs satisfy it and _some_ TMs do not.