You are on page 1of 53

Automata and Computability: A

Programmer’s Perspective Ganesh


Gopalakrishnan
Visit to download the full and correct content document:
https://textbookfull.com/product/automata-and-computability-a-programmers-perspect
ive-ganesh-gopalakrishnan/
More products digital (pdf, epub, mobi) instant
download maybe you interests ...

Notes on formal languages automata computability and


complexity Gallier J

https://textbookfull.com/product/notes-on-formal-languages-
automata-computability-and-complexity-gallier-j/

Computability Nigel Cutland

https://textbookfull.com/product/computability-nigel-cutland/

Oracle Certified Professional Java SE 8 Programmer Exam


1Z0 809 A Comprehensive OCPJP 8 Certification Guide A
Comprehensive OCPJP 8 Certification Guide Ganesh Sg
Kumar Hari Kiran Sharma Tushar
https://textbookfull.com/product/oracle-certified-professional-
java-se-8-programmer-exam-1z0-809-a-comprehensive-
ocpjp-8-certification-guide-a-comprehensive-
ocpjp-8-certification-guide-ganesh-sg-kumar-hari-kiran-sharma-
tushar/
Oracle Certified Professional Java SE 7 Programmer
Exams 1Z0 804 and 1Z0 805 A Comprehensive OCPJP 7
Certification Guide Expert s Voice in Java S G Ganesh
Tushar Sharma
https://textbookfull.com/product/oracle-certified-professional-
java-se-7-programmer-exams-1z0-804-and-1z0-805-a-comprehensive-
ocpjp-7-certification-guide-expert-s-voice-in-java-s-g-ganesh-
The ShortTube 80 Telescope A User s Guide Neil T.
English

https://textbookfull.com/product/the-shorttube-80-telescope-a-
user-s-guide-neil-t-english/

Becoming A Programmer A Beginner s Guide 1st Edition


Paul Mabry

https://textbookfull.com/product/becoming-a-programmer-a-
beginner-s-guide-1st-edition-paul-mabry/

Cellular Learning Automata: Theory and Applications


Reza Vafashoar

https://textbookfull.com/product/cellular-learning-automata-
theory-and-applications-reza-vafashoar/

Language and Automata Theory and Applications Shmuel


Tomi Klein

https://textbookfull.com/product/language-and-automata-theory-
and-applications-shmuel-tomi-klein/

Groups Languages and Automata 1st Edition Derek F. Holt

https://textbookfull.com/product/groups-languages-and-
automata-1st-edition-derek-f-holt/
Automata and Computability
A Programmer’s Perspective
Automata and Computability
A Programmer’s Perspective

Ganesh Lalitha Gopalakrishnan


Cover Photo Credit: NASA, ESA

This image combines an image taken with Hubble Space Telescope in the optical (taken in spring 2014) and observations of its auroras in the ultraviolet,
taken in 2016. URL: https://www.spacetelescope.org/images/heic1613a/

CRC Press
Taylor & Francis Group
6000 Broken Sound Parkway NW, Suite 300
Boca Raton, FL 33487-2742
© 2019 by Taylor & Francis Group, LLC
CRC Press is an imprint of Taylor & Francis Group, an Informa business
No claim to original U.S. Government works
Printed on acid-free paper
Version Date: 20190206

International Standard Book Number-13: 978-1-138-55242-5 (Hardback)


This book contains information obtained from authentic and highly regarded sources. Reasonable efforts have been made to publish reliable data and
information, but the author and publisher cannot assume responsibility for the validity of all materials or the consequences of their use. The authors and
publishers have attempted to trace the copyright holders of all material reproduced in this publication and apologize to copyright holders if permission
to publish in this form has not been obtained. If any copyright material has not been acknowledged please write and let us know so we may rectify in any
future reprint.
Except as permitted under U.S. Copyright Law, no part of this book may be reprinted, reproduced, transmitted, or utilized in any form by any electronic,
mechanical, or other means, now known or hereafter invented, including photocopying, microfilming, and recording, or in any information storage or
retrieval system, without written permission from the publishers.
For permission to photocopy or use material electronically from this work, please access www.copyright.com (http://www.copyright.com/) or contact
the Copyright Clearance Center, Inc. (CCC), 222 Rosewood Drive, Danvers, MA 01923, 978-750-8400. CCC is a not-for-profit organization that provides
licenses and registration for a variety of users. For organizations that have been granted a photocopy license by the CCC, a separate system of payment
has been arranged.
Trademark Notice: Product or corporate names may be trademarks or registered trademarks, and are used only for identification and explanation
without intent to infringe.
Visit the Taylor & Francis Web site at
http://www.taylorandfrancis.com
and the CRC Press Web site at
http://www.crcpress.com
Contents

Foreword xiii

Preface xv

Acknowledgments xix

1 What Machines Think 3


1.1 Problems Without Algorithms . . . . . . . . . . . . . . . . . . 4
1.2 How to Define a Computer? . . . . . . . . . . . . . . . . . . . . 5
1.3 Practical Application: Syntax Definition/Checking . . . . . . 7
1.4 Simplified Turing Machines as Parsers . . . . . . . . . . . . . 11
1.5 Automata and Computability for Lifelong Learning . . . . . 13

2 Defining Languages: Patterns in Sets of Strings 15


2.1 Symbol, Alphabet, String, Language . . . . . . . . . . . . . . 15
2.1.1 Symbol . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
2.1.2 Alphabet . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
2.1.3 String or Word . . . . . . . . . . . . . . . . . . . . . . . 16
2.1.4 Various Notions of Zero and One . . . . . . . . . . . . 17
2.2 Language . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
2.2.1 Language Concatenation . . . . . . . . . . . . . . . . . 20
2.2.2 The Zero and One for Language Concatenation . . . 21
2.2.3 Zero and One of Language Concatenation in Python 21
2.2.4 Exponentiation of a Language . . . . . . . . . . . . . . 22
2.2.5 Python Encoding of Language Exponentiation . . . . 23
2.2.6 Union and Intersection of Languages . . . . . . . . . 24
2.3 Useful Results, Slippery Roads . . . . . . . . . . . . . . . . . 25

3 Kleene Star: Basic Method of Defining Repetitious Patterns 27


3.1 Three Ways to Describe Star . . . . . . . . . . . . . . . . . . . 27
3.2 Additional Definitions and Properties of Star . . . . . . . . . 28
3.3 Language Complementation . . . . . . . . . . . . . . . . . . . 31
3.4 Other Language Operations . . . . . . . . . . . . . . . . . . . 32
3.4.1 Symmetric Difference, Subtraction . . . . . . . . . . . 32
3.4.2 Reverse of a Language . . . . . . . . . . . . . . . . . . 32
vi

3.5 String/Language Homomorphisms . . . . . . . . . . . . . . . 33


3.5.1 Taking Star Repeatedly . . . . . . . . . . . . . . . . . . 35
3.6 Enumerating Strings in a Language . . . . . . . . . . . . . . 35

4 Basics of DFA 41
4.1 DFA Everywhere . . . . . . . . . . . . . . . . . . . . . . . . . . 41
4.2 Elements of a DFA . . . . . . . . . . . . . . . . . . . . . . . . . 42
4.3 Formal Structure of DFA . . . . . . . . . . . . . . . . . . . . . 43
4.4 The Language of a DFA . . . . . . . . . . . . . . . . . . . . . . 44
4.4.1 DFA as String Classifers . . . . . . . . . . . . . . . . . 44
4.4.2 Basics of Designing a DFA . . . . . . . . . . . . . . . . 45
4.5 Formal Definition of DFA Language Acceptance . . . . . . . 45
4.6 “Lasso” Shape of DFA and the Pumping Lemma . . . . . . . 47
4.6.1 General Statement of the Pumping Lemma . . . . . . 48
4.7 Proving a Language to Be Non-Regular . . . . . . . . . . . . 50
4.7.1 Why All Splits of x, y, z? . . . . . . . . . . . . . . . . . . 51
4.8 Grossly Abusing the Pumping Lemma . . . . . . . . . . . . . 52
4.8.1 Inability to Prove with this Pumping Lemma . . . . . 53
4.9 Regularity-Preserving Transformations Aid Proofs . . . . . 54

5 Designing DFA 57
5.1 Understanding the Language to Be Realized . . . . . . . . . 57
5.1.1 The Language of Equal Changes . . . . . . . . . . . . 57
5.1.2 Best Practices to Correct DFA Design and Verification 58
5.2 Examples of Designing DFA . . . . . . . . . . . . . . . . . . . 59
5.2.1 The Language of Blocks of 3 . . . . . . . . . . . . . . . 59
5.2.2 DFA for “Ends with 0101” . . . . . . . . . . . . . . . . 60
5.2.3 DFA for “MSB/LSB-first Binary Number is Divisible
by 3” . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60
5.2.4 DFA for “Third-last bit is a 1” . . . . . . . . . . . . . . 62
5.3 Automd: A Markdown Language for All Machines . . . . . . 64
5.3.1 Markdown for DFA . . . . . . . . . . . . . . . . . . . . 64

6 Operations on DFA 67
6.1 Complementation of DFA . . . . . . . . . . . . . . . . . . . . . 67
6.2 Union and Intersection of DFA . . . . . . . . . . . . . . . . . 67
6.3 Language Equivalence and Isomorphism . . . . . . . . . . . 71
6.4 DFA Minimization and Myhill-Nerode Theorem . . . . . . . 72
6.4.1 Fully Worked-out Example of DFA Minimization . . 72
6.4.2 Salient Code Excerpts . . . . . . . . . . . . . . . . . . . 75
6.5 Examples of Language Design and Manipulation . . . . . . 76
6.5.1 Use of Union, Minimization, and Language
Equivalence . . . . . . . . . . . . . . . . . . . . . . . . . 77
6.5.2 Use of DeMorgan’s Law . . . . . . . . . . . . . . . . . . 78
vii

7 Nondeterministic Finite Automata 81


7.1 Overview of NFA . . . . . . . . . . . . . . . . . . . . . . . . . . 81
7.2 Formal Description of NFA . . . . . . . . . . . . . . . . . . . . 83
7.3 Language of an NFA: Example Driven . . . . . . . . . . . . . 84
7.3.1 Simulations of the NFA of Figure 7.5 . . . . . . . . . 84
7.3.2 Simulations of the NFA of Figure 7.3 . . . . . . . . . 85
7.4 Language of an NFA: via Eclosure . . . . . . . . . . . . . . . 85
7.4.1 Defining Eclosure . . . . . . . . . . . . . . . . . . . . . 86
7.4.2 Definition of δ and δ̂ . . . . . . . . . . . . . . . . . . . . 86
7.5 NFA to DFA Conversion through Subset Construction . . . 87
7.6 Brzozowski’s DFA Minimization Algorithm . . . . . . . . . . 90
7.6.1 Reversal of a DFA Yields an NFA . . . . . . . . . . . . 90
7.7 A Complete Illustration of Brzozowski’s Minimization . . . 92

8 Regular Expressions and NFA 95


8.1 Regular Expressions . . . . . . . . . . . . . . . . . . . . . . . . 95
8.2 RE to NFA Conversion: Examples, Algorithm Sketch . . . . 96
8.3 A More Extensive Example . . . . . . . . . . . . . . . . . . . . 100
8.4 Regular Expressions: Ubiquitous, yet Error-Prone . . . . . . 101
8.5 Anatomy of the RE to NFA Converter . . . . . . . . . . . . . 103
8.6 Example: Designing an Error-Correcting DFA . . . . . . . . 106
8.6.1 Error-correcting RE for “within Hamming Distance
of 2” . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106
8.6.2 NFA-based Design of “within Hamming Distance of 2”108
8.7 Minimal DFA and Isomorphism . . . . . . . . . . . . . . . . . 108
8.8 DFA Ultimate Periodicity to Solve the Postage Stamp
Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111
8.8.1 Ultimately periodic sets and lengths of members of
a regular language . . . . . . . . . . . . . . . . . . . . . 111
8.8.2 Stamp Problem and Ultimate Periodicity via Jove . . 112
8.8.3 Applying to numbers that are not relatively prime . 113
8.8.4 Solving for three stamps . . . . . . . . . . . . . . . . . 113
8.8.5 Lengths of strings accepted by DFA . . . . . . . . . . 113

9 NFA to RE Conversion 115


9.1 NFA to RE Conversion Algorithm . . . . . . . . . . . . . . . . 115
9.2 Illustration on Pedagogical Example . . . . . . . . . . . . . . 116
9.3 Illustration on Non-trivial Example . . . . . . . . . . . . . . 118
9.4 Checking the Conversion . . . . . . . . . . . . . . . . . . . . . 121
9.5 DFA, NFA, and RE Are Equally Powerful . . . . . . . . . . . 122
9.6 Implementation of NFA to RE . . . . . . . . . . . . . . . . . . 123
9.7 Closure Results Pertaining to Regular Languages . . . . . . 123

10 Derivative-Based Regular Expression Matching 127


10.1 Introduction to RE Derivatives . . . . . . . . . . . . . . . . . 127
viii

10.2 Definitions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129


10.2.1 Derivative Rules . . . . . . . . . . . . . . . . . . . . . . 129
10.2.2 Nullability Rules . . . . . . . . . . . . . . . . . . . . . . 132
10.3 Implementation of Derivative-Based String Matching . . . . 134
10.3.1 Derivatives: Closing Thoughts . . . . . . . . . . . . . 134

11 Context-Free Languages
and Grammars 137
11.1 Context-Free Language Examples . . . . . . . . . . . . . . . 137
11.2 Context-Free Grammars and Parse Trees . . . . . . . . . . . 138
11.2.1 Elements of Context-Free Grammars . . . . . . . . . 139
11.2.2 Parse Trees, Language of a CFG . . . . . . . . . . . . 139
11.3 Avoiding Mistakes in Designing CFGs . . . . . . . . . . . . . 140
11.3.1 Completeness and Consistency . . . . . . . . . . . . . 141
11.4 The Design of CFG, and the Hill/Valley Plot
for Arguing Consistency and Completeness . . . . . . . . . . 142
11.5 Ambiguous Grammars, and Disambiguation . . . . . . . . . 144
11.5.1 Disambiguation . . . . . . . . . . . . . . . . . . . . . . 145
11.5.2 Disambiguation Is Crucial! . . . . . . . . . . . . . . . . 147
11.5.3 Impossibility Results . . . . . . . . . . . . . . . . . . . 147
11.6 Inherently Ambiguous Languages . . . . . . . . . . . . . . . . 147
11.7 Expressing DFA via CFGs . . . . . . . . . . . . . . . . . . . . 149
11.7.1 Purely Right Linear Grammars . . . . . . . . . . . . . 150
11.7.2 Closure, Purely Left Linear, and Mixed Linearity . . 151
11.8 Historical Importance of the Theory of Parsing . . . . . . . . 152
11.8.1 Combating Inherent Ambiguity . . . . . . . . . . . . . 153
11.9 A Pumping Lemma for CFLs . . . . . . . . . . . . . . . . . . . 154
11.9.1 Application of the CFL Pumping Lemma . . . . . . . 157
11.10 The Complement of a Non-CFL Can Be a CFL . . . . . . . 157
11.10.1 Growing “Inside-Out” . . . . . . . . . . . . . . . . . . 158

12 Pushdown Automata 161


12.1 Pushdown Automaton Basics . . . . . . . . . . . . . . . . . . 161
12.2 Formal Description of PDA . . . . . . . . . . . . . . . . . . . . 164
12.2.1 Acceptance, Deterministic PDA . . . . . . . . . . . . . 165
12.3 Exploring the PDA for L D yck Using Jove . . . . . . . . . . . . 165
12.4 PDA Behavior Through Examples . . . . . . . . . . . . . . . . 167
12.4.1 Rerunning pda6 with Larger Stack Allowed . . . . . 171
12.5 Toward More Practical PDA . . . . . . . . . . . . . . . . . . . 173
12.6 CFG to PDA Conversion . . . . . . . . . . . . . . . . . . . . . 175
12.6.1 Disambiguation . . . . . . . . . . . . . . . . . . . . . . 179
12.7 Practical Knowledge Imparted by Jove: Three Parsers . . . 181

13 Turing Machines 183


13.1 Brief History of Turing Machines . . . . . . . . . . . . . . . . 183
ix

13.2 Universal Computing Devices . . . . . . . . . . . . . . . . . . 184


13.3 Formal Definition of Turing Machines . . . . . . . . . . . . . 186
13.4 Examples of Simple TMs . . . . . . . . . . . . . . . . . . . . . 189
13.4.1 A Simple DTM that Flips Bits . . . . . . . . . . . . . . 189
13.4.2 TMs that check if a string contains 101 . . . . . . . . 189
13.5 A DTM for w#w and an NDTM for ww . . . . . . . . . . . . . 193
13.5.1 A DTM Recognizing w#w . . . . . . . . . . . . . . . . . 193
13.5.2 A Nondeterministic TM Recognizing ww . . . . . . . 193
13.5.3 Nondeterminism does not increase a TM’s
Expressive Power . . . . . . . . . . . . . . . . . . . . . 194
13.6 Example: A TM that Works on the Collatz Problem . . . . . 200
13.6.1 Markdown for the Collatz Problem TM with
Comments . . . . . . . . . . . . . . . . . . . . . . . . . . 200
13.7 The Chomsky Hierarchy . . . . . . . . . . . . . . . . . . . . . 203
13.7.1 Recursively Enumerable and Recursive Languages . 204
13.8 An Alternate Notation for Instantaneous Descriptions . . . 205

14 Interplay between Formal Languages 209


14.1 Why Study Impossibility Results? . . . . . . . . . . . . . . . . 209
14.1.1 Definitions: Procedure and Algorithm . . . . . . . . . 209
14.1.2 Formal Languages Associated with Turing Machines 210
14.1.3 Allaying Confusion: Language vs. Language Family 210
14.2 One Example of a Recursive and an RE Language . . . . . . 211
14.2.1 A Recursive Language . . . . . . . . . . . . . . . . . . 211
14.2.2 Prerequisites to Defining the Notion of RE . . . . . . 212
14.2.3 Combining Semi-deciders for L and L . . . . . . . . . 213
14.2.4 The “no wimp” Clause . . . . . . . . . . . . . . . . . . . 213
14.2.5 A Recursively Enumerable Language . . . . . . . . . 213
14.3 Other Examples of Recursive and RE Languages . . . . . . 215
14.3.1 RE Languages that are not Recursive . . . . . . . . . 216
14.3.2 Why is it called Recursively Enumerable? . . . . . . 217
14.3.3 Alternate proof of A T M being RE . . . . . . . . . . . . 218
14.3.4 Some More RE Languages . . . . . . . . . . . . . . . . 218
14.4 Summary of Decidability/Semi-Decidability Results . . . . . 219
14.4.1 Existence of Non-RE languages . . . . . . . . . . . . . 220
14.5 RE and Recursive Sets: More High-Level Proof Sketches . . 221
14.5.1 Language of DFA D where L(D) = Σ∗ is Recursive . . 221
14.5.2 Language of CFG G where L(G) = ; is Recursive . . 222
14.5.3 Language of LBA that halt on input w is Recursive . 223
14.5.4 Language of Turing machines whose first output is ‘3’ 224

15 Post Correspondence, and


Other Undecidability Proofs 227
15.1 Post Correspondence: “Drosophila” for Decidability . . . . . 227
x

15.2 Proof Sketch of the Undecidability of PCP . . . . . . . . . . . 229


15.2.1 Tile Construction Basics . . . . . . . . . . . . . . . . . 230
15.2.2 Proving Grammar Ambiguity by Reduction from PCP 231
15.2.3 PCP in Jove . . . . . . . . . . . . . . . . . . . . . . . . . 231
15.3 Undecidability of the Acceptance Problem . . . . . . . . . . . 233
15.4 Halting (Halt T M ) is Undecidable . . . . . . . . . . . . . . . . 234
15.5 Mapping Reductions . . . . . . . . . . . . . . . . . . . . . . . . 236
15.5.1 Undecidable problems are “A T M in disguise” . . . . . 239

16 NP-Completeness 241
16.1 What Does NP-Complete Mean? . . . . . . . . . . . . . . . . . 241
16.1.1 Grouping Problems: Solving One Implies Solving All 242
16.1.2 Some Historical Notes . . . . . . . . . . . . . . . . . . 243
16.2 NPC Notions Defined Based on NDTMs . . . . . . . . . . . . 244
16.2.1 P-time . . . . . . . . . . . . . . . . . . . . . . . . . . . . 244
16.2.2 NP-time . . . . . . . . . . . . . . . . . . . . . . . . . . . 246
16.2.3 NP Verifier . . . . . . . . . . . . . . . . . . . . . . . . . 246
16.2.4 Examples of P-time and NP-time Deciders . . . . . . 247
16.2.5 Decider versus Verifier Views . . . . . . . . . . . . . . 247
16.3 Introducing SAT Problems . . . . . . . . . . . . . . . . . . . . 248
16.3.1 A Warmup: 2-SAT . . . . . . . . . . . . . . . . . . . . . 249
16.3.2 2-SAT: Examples and Algorithm . . . . . . . . . . . . 251
16.4 3-SAT and Its NP-Completeness . . . . . . . . . . . . . . . . . 253
16.5 3-SAT Is NP-Complete . . . . . . . . . . . . . . . . . . . . . . 254
16.5.1 3-SAT is in NP . . . . . . . . . . . . . . . . . . . . . . . 255
16.5.2 Every Language in NP Reduces to 3-SAT . . . . . . . 255
16.5.3 How P=NP is Obtained if 3-SAT ∈ P? . . . . . . . . . 256
16.6 Show that Clique Is NPC: Reduction from 3-SAT . . . . . . 257
16.6.1 Clique is in NP . . . . . . . . . . . . . . . . . . . . . . . 257
16.6.2 Some Language in NPC Reduces to Clique . . . . . . 257
16.7 Complexity Classes, Closing Caveats . . . . . . . . . . . . . . 258
16.7.1 NP-Hard Problems can be Undecidable (Pitfall in
Proofs) . . . . . . . . . . . . . . . . . . . . . . . . . . . . 258
16.7.2 The CoNP and CoNPC Complexity Classes . . . . . . 260
16.8 SAT in Practice . . . . . . . . . . . . . . . . . . . . . . . . . . . 261

17 Binary Decision Diagrams as


Minimal DFA 267
17.1 Boolean Functions in Computing Theory and Practice . . . 267
17.2 Boolean Functions as Minimal DFA of Their On-Sets . . . . 268
17.3 The Importance of Variable Ordering . . . . . . . . . . . . . . 271
17.3.1 Finding a better input variable order . . . . . . . . . 272
17.3.2 Functions with linearly sized BDDs . . . . . . . . . . 273
17.4 From Minimal DFA to BDD: Intuitive Presentation . . . . . 275
xi

17.5 On BDD Sizes . . . . . . . . . . . . . . . . . . . . . . . . . . . . 277

18 Computability Using Lambdas 279


18.1 The History of Lambda Calculus . . . . . . . . . . . . . . . . 279
18.2 Lambdas from a Programmer’s Perspective . . . . . . . . . . 280
18.3 Syntax and Semantics of the Lambda Calculus . . . . . . . . 282
18.4 Illustration: Church Numerals in Python . . . . . . . . . . . 284
18.5 Illustration: Booleans, Pairs, Other Functions . . . . . . . . 284
18.6 Handling Recursion . . . . . . . . . . . . . . . . . . . . . . . . 287
18.7 Obtaining Fixpoints from Fixpoint Equations . . . . . . . . . 288
18.7.1 Y : A Fixpoint Finder . . . . . . . . . . . . . . . . . . . 288
18.7.2 The Y Combinator . . . . . . . . . . . . . . . . . . . . . 288
18.7.3 Expression Recursion using Y . . . . . . . . . . . . . . 289
18.7.4 Reason for an alternate fixpoint finder Ye . . . . . . . 290
18.8 Illustrating the Use of Fixpoint Combinators . . . . . . . . . 292
18.9 Combinators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 292

Appendices

Appendix A A Recap of Discrete Math 297


A.1 Sets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 297
A.1.1 Set Builder . . . . . . . . . . . . . . . . . . . . . . . . . 297
A.1.2 Powerset . . . . . . . . . . . . . . . . . . . . . . . . . . . 298
A.1.3 Complement . . . . . . . . . . . . . . . . . . . . . . . . 299
A.1.4 Equivalence Classes, Partitioning . . . . . . . . . . . 299
A.2 Mathematical Logic . . . . . . . . . . . . . . . . . . . . . . . . 299
A.3 Proof Methods: Using Contrapositive, by Contradiction . . . 300
A.4 Cartesian Product, Binary Relations, Functions . . . . . . . 301
A.5 Functions: Signature, Onto, Into, Total . . . . . . . . . . . . 301
A.6 Trees . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 302

Appendix B Catalog of Jove Functions 303


B.1 Jove’s Top-Level Functions . . . . . . . . . . . . . . . . . . . . 303
B.1.1 Chapters 2 and 3 . . . . . . . . . . . . . . . . . . . . . . 304
B.1.2 Chapters 4 through 6 . . . . . . . . . . . . . . . . . . . 305
B.1.3 Chapters 7 through 9 . . . . . . . . . . . . . . . . . . . 305
B.1.4 Chapter 10 . . . . . . . . . . . . . . . . . . . . . . . . . 306
B.1.5 Chapter 11 . . . . . . . . . . . . . . . . . . . . . . . . . 307
B.1.6 Chapter 12 . . . . . . . . . . . . . . . . . . . . . . . . . 307
B.1.7 Chapter 13 . . . . . . . . . . . . . . . . . . . . . . . . . 308
B.1.8 Chapter 15 . . . . . . . . . . . . . . . . . . . . . . . . . 308
B.1.9 Chapters 16 and 17 . . . . . . . . . . . . . . . . . . . . 308
B.1.10 Chapter 18 . . . . . . . . . . . . . . . . . . . . . . . . . 309
B.2 Jove’s Use of Python, Including Lambda Basics . . . . . . . 310
xii

Appendix C There Are More Languages than RE Languages 313


C.1 Gödel Hash . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 313
C.2 Cantor-Schröder-Bernstein (CSB) Theorem . . . . . . . . . . 314
C.3 Cantor’s Diagonalization Proof about Languages . . . . . . . 315
C.4 | R eal | Is Higher than | Nat | . . . . . . . . . . . . . . . . . . 316

Selected References 319

Index 323
xiii

FOREWORD
Compared to other disciplines, theoretical computer science is a relatively
young field. Nevertheless, more than seventy years have already passed since
Alan Turing introduced formal definitions of a computer and an algorithm in
1936; for his fundamental contributions Turing is generally considered to be
the father of not only theoretical computer science, but also of artificial intelli-
gence. The late 1950s and the 1960s saw a great increase in the activities re-
lated to theoretical computer science, and this activity continues to the present
day. Many books have been written on this subject: texts suitable for a first
course as well as advanced monographs. The reader may well ask: Is another
introductory text really needed? The answer is a clear yes, because of the spe-
cial features of this book.
As the book’s title states, this text is written from the programmer’s per-
spective. Along with the printed book, Gopalakrishnan provides a suite of
Jupyter notebooks for dealing with automata and languages, packaged under
the name Jove. This, and the frequent relations the author establishes between
theoretical concepts and their applications should make the theory attractive
to software engineering students, as well as to the more mathematically ori-
ented computer science students.
Many texts on theoretical computer science begin with a preliminary chap-
ter on mathematical notions such as sets, relations and functions. In contrast
to this, the present book begins with a brief history of computer science, and
makes the case that automata and computability are at its very core. This
provides a much stronger motivation for the study of these theoretical topics
than do sets, relations and functions. Incidentally, the discrete mathematics
background is provided in the book, but as an appendix.
The book is very readable. New concepts are illustrated by numer-
ous examples and further clarified by sets of exercises, both theoretical and
programming-oriented. The students have an opportunity to verify their un-
derstanding by using Jove programs. Numerous footnotes, often humorous,
provide references to further reading in publications and websites.
The coverage of topics in theoretical computer science is quite comprehen-
sive. After the short history of computing, there is a gentle introduction to for-
mal languages and operations on them. Deterministic and non-deterministic
finite automata, regular expressions, context-free languages, pushdown au-
tomata, and Turing machines then follow. The book concludes with the much
more challenging concepts of undecidability, NP-completeness and lambda cal-
culus. Altogether, this is a very valuable up-to-date addition to the literature.

Janusz A. Brzozowski
University of Waterloo
Waterloo, Ontario, Canada
xv

PREFACE

This is a book on the central theoretical pillars of computer sci-


ence! We begin with the theory of syntax processing based on machines
and grammars and finish with a study of the absolute limits of computing,
asking questions such as “what can and cannot be algorithmically solved
using computers?” We assume that the reader already has basic working
knowledge of sets, relations, functions, and predicate logic (Appendix A
provides a refresher). 1
Jove is a synonym of Jupiter, the Roman
god of the sky. According to Wikipedia, the
Half our educational material comes in the form of Jupyter notebooks
Roman practice of swearing by Jove to wit-
(software) that accompany this book (being called Jove).1 Jupyter is a ness an oath in law courts is the origin of
compendium of runnable code in Python2 documentation, and may also the expression “by Jove!”

include web references and Youtube videos.3 Jupyter notebooks are the
2
Jupyter also supports other languages.
modern equivalent of Engineers’ Notebooks, and find use in real-world
We use Python exclusively, but employing
projects of immense importance. For instance, some of the scientists only a very small subset that is quite easy
who successfully detected gravitational waves recently employed Jupyter to learn.

notebooks in their research to perform some of their calculations.4 Some


of my Jove notebooks employ Jupyter widgets that provide sliders and
3
Many of our notebooks start with videos
pull-down menu selections to incrementally expand language construc- that describe the rest of the notebook.
tions, nondeterministic machine executions, etc. “By Jove,” please take
advantage of these interactive facilities!
I recommend that you first luxuriate by simply running the already 4
A 2017 Physics Nobel prize winning dis-
provided notebooks (especially those that begin with Youtube videos) and covery.

execute their commands one by one. After that, try to peek into Ap-
pendix B that summarizes all of Jove’s functions, and imitate some of
my examples. Then, read the Python code that powers these notebooks. At
that point, you will have a firm grasp of the covered topics.
5
In Appendix B, we also provide an overview of how to use lambda func- In fact, imparting the requisite mathe-
matical maturity to understand such pre-
tions in Python. Jove’s Python functions are written largely using a sub- sentations is one of the goals of this
set of Python based on functional programming. In our codes, we employ course.
recursion and also higher order functions such as map, reduce, fold, and 6
Not to mention automata-theory book
filter. Given that all modern programming languages (even C++) nowa- authors such as myself! A good example
days include some elements of functional programming, these are valu- is the notion of acceptance of a string by
a nondeterministic push-down automaton
able concepts to carry with you beyond this course.
(PDA, Section 12.2.1). By interacting with
In the study of computability, it is common practice to define machines, a PDA using Jove, one can minutely study
grammars and languages solely in mathematics.5 Unfortunately, such the execution history and understand ev-
ery associated detail.
definitions possess subtle nuances that can often stump students.6 Aug-
menting the definitions with actual runnable code can greatly facilitate 7
Knuth once famously wrote, “Beware of
one’s ability to grasp such slippery topics. bugs in the above code; I have only
Providing alternate definitions is also vastly safer practice in real life. proved it correct, not tried it.”

Mathematical definitions are like code that has never been run even once,7 8
“Running” mathematical definitions
and unfortunately can be incomplete or contain bugs.8 Having alternate means examining them within a theorem-
prover—something that very few have the
definitions increases the chances of spotting mistakes by noticing dis- time or skill-set to properly carry out.
agreements.
xvi

Book Highlights: This book serves “not just the standard fare of au-
tomata theory” but also topics that help with your overall CS education,
and also some that are more modern, and are transitioning to practice.
Part I of this book begins with a historical perspective in Chapter 1,
“What Machines Think.” Discussing the history of computer science is
supremely important because ours is an area that, despite providing the
society countless bounties, still struggles to safeguard the everyday cit-
izen from the perils of software bugs. In his Turing award lecture, Di-
9
Google EWD340 to locate this article. jkstra’s closing paragraph goes as follows:9 We shall do a much better
programming job, provided that we approach the task with a full appre-
ciation of its tremendous difficulty, provided that we stick to modest and
elegant programming languages, provided that we respect the intrinsic
limitations of the human mind and approach the task as Very Humble
Programmers.
In fact, it is very easy to feel humble, as Dijkstra espouses. Just
60 years ago, people did not know how to write even simple lexers and
parsers. They used crude methods that were so terrible to look at and
analyze, and were untrustworthy. The CS community conquered this
complexity through formal methods. Formal methods are central to all
advances in computer science. They include just about any method that
systematically guides us toward the construction of precision software
10
By way of analogy, think of precision components.10 Viewed this way, the very first formal methods in com-
machining. If one can build ball-bearings
puter science indeed were the theory of regular expressions, context-free
with a tolerance of 0.1mm, they are per-
haps good for putting into bullock-cart grammars and recursively enumerable sets!
wheels, but not into automobile wheels. Some may think that syntax processing is “well-understood old stuff ”—
If one can achieve micron-level precision,
one can put them into automobiles or
but this is simply not true! Today, we are in a fast-changing world where
even jet-engine turbines. Initially, pre- syntax processing is ever more important. Today’s markdown languages,
cision machining increases product costs
JSON files, web forms, and countless other tools all employ elements of
and does not immediately benefit the soci-
ety. But eventually, through mass produc- lexing and parsing. To design them robustly and reliably, one needs to
tion, we curb the costs. truly understand the subtle nuances of language processing. Bugs intro-
duced while reading-in and processing someone’s command inputs or web
forms are difficult to track down, and may cause severe loss of money and
may even threaten life (for example if one evaluates a formula gathered
11
Regular-expression processing bugs do through a web form pertaining to medical drug proportions).11 The for-
occur in the field.
mal methods introduced in this course are thus very relevant to modern
software design.
We cover syntax-processing beginning in Chapter 2, “Defining Lan-
guages: Patterns in Sets of Strings” and Chapter 3, “Kleene Star: Basic
Method of Defining Repetitious Patterns.” Part II then takes over, dis-
cussing the crucially important topic of DFA over three chapters: “Ba-
sics of DFA” (Chapter 4), “Designing DFA” (Chapter 5), and “Operations
on DFA” (Chapter 6). Nondeterministic automata and regular expres-
sions are then discussed over three chapters: “Nondeterministic Finite
Automata” (Chapter 7), “Regular Expressions and NFA” (Chapter 8), and
xvii

“NFA to RE Conversion” (Chapter 9).


Chapter 10 discusses “Derivative-Based Regular Expression Match-
ing.” This approach allows us to handle the complementation of regular
expressions without paying the exponential cost of determinization. This
is an elegant and important algorithm lost in the mist of time, but pub-
lications on this topic have appeared in top conferences even as recently
as 2016 [2]. Our presentation of derivative-based parsing rules also re-
sembles how one writes operational semantic definitions. This is another
example of how formal methods help quality conscious companies develop
specifications of complex pieces of software.
We provide plenty of material that increases the students’ understand-
ing of how lexers and parsers are used together in full applications. This
is achieved as follows. First, in Chapter 11 (“Context-Free Languages and
Grammars”) and Chapter 12 (“Pushdown Automata”), we cover context-
free grammars and parsing. Then we provide the definition of a sim-
ple lexer (based on regular expressions) and parser (based on context-free
grammars) using the widely used tool, “PLY.”12 Our first lexer/parser pair 12
PLY stands for “Python Lex and Yacc.”
Lex and Yacc were celebrated lexers and
fits under half a page of Python code (Figure 10.4)! We then follow it up
parsers created in Bell Labs in the 1970s.
with another lexer and parser that turn regular expressions into NFA Written by Dr. David Beazley, PLY is
using PLY (again, in about a page, Figure 8.1). available from http://www.dabeaz.com/
ply/.
We finally provide a more realistic lexer and parser for a markdown
language that we design (see Section B.1.5) for writing clear descriptions
of NFA, DFA, PDA, and Turing machines accompanied by comments.13 13
We call this markdown “automd” stand-
ing for “Automaton Markdown.”
Chapter 13 discusses Turing machines. We make the study of Turing
machines (TM) enjoyable by equipping them with “fuel tanks” that users
can load with some number of “gallons” of fuel. This gives us a chance
to concretely talk about time-complexity (the amount of fuel consumed
before termination). This sets the stage for Part III that begins with In-
terplay between Formal Languages (Chapter 14) and proceeds to give you
a domino game in Chapter 15 (“Post Correspondence, and
Other Undecidability Proofs”) that you can play within Jove using the
PCP solver written by Ling Zhao. This deceptively simple game actu-
ally encompasses the essence of Turing undecidability—a fact that is at
the same time satisfying and bewildering! Chapter 16 introduces NP-
Completeness—a foundational aspect of anyone’s CS education.
Chapter 17 (“Binary Decision Diagrams as
Minimal DFA”) gives you the ability to play with the PBDD tool written
by Tyler Sorensen, launching it from within Jove. Chapter 18 (“Com-
putability Using Lambdas”) gives you the ability to play with lambda
calculus within Jove.
Exercises are included within chapters as well as towards their end.14 14
We number the exercises and theorems
the same as the section (or subsection)
Proofs of the stated theorems are either sketched or cited. A solution
containing them. This idea (proposed by
manual for instructors will be made available as supplementary mate- my late colleague Kris Sikorski) makes lo-
rial. Solutions to selected problems will be included as supplementary cating them easier, we hope.
xviii

material on the book’s website https://bit.ly/Automata_Jove. Jove


notebooks will be kept updated on the github page of Jove, namely https:
//github.com/ganeshutah/Jove.
We provide many examples of formal methods and functional program-
ming. For example, we encourage you to construct DFA using two dis-
tinct approaches and check whether their minimized forms are isomor-
phic (Chapter 5). Equivalence checking is a powerful formal methods
idea, and is more comprehensive than “plain old testing.” During equiva-
lence checking, all behaviors are covered – not just those for which a user
might care to write tests for.
We present formal methods for Boolean reasoning using Binary De-
cision Diagrams (BDD, Chapter 17). Knuth calls BDDs one of the only
really fundamental data structures that came out in the last twenty-five
years. We show how BDDs and minimal DFA are, essentially, one and the
same. BDDs are routinely used by the hardware industry to detect and
eliminate serious bugs from microprocessors.
Most books on automata theory deal with Turing machines but do not
discuss lambda calculus. However, both these perspectives are crucially
important in computer science. Besides, ideas from functional program-
ming are powering many industries, increasing programmer productivity
and helping reduce bugs. Functional programming can be quickly and
elegantly introduced by demonstrating how to encode computability con-
cepts using lambdas. We provide a glimpse of the power of the lambda
notation by showing how familiar recursive functions such as factorial
and fibonacci can be written using lambdas and a fixpoint finder, namely
the “Y combinator.” Essentially, we provide a Jove notebook that tells you
how to “compute everything” using lambdas.

This is for those who love to code: Please make an attempt to not
just use Jove, but actually extend it! Please stick to the extremely simple
subset of Python that I’ve chosen, so that this material remains accessible
to the widest possible audience.
You may find my use of lambdas and higher order functions to be a
departure from simplicity—but in fact, they are there to make the code re-
semble mathematics a bit more, and also they help with lambda calculus.
Hence please don’t convert these codes to imperative for-loops (loops may
be more readily understood, but such a conversion can be a step back from
the intended pedagogy). To remain self-contained, I provide tutorials on
lambdas and higher order functions in Section B.2.
Last but not the least, this is largely a book on programming and in-
teracting with abstract machines. Section B.1 documents almost all the
Jove functions, as if Automata Theory is just “an advanced math library.”
That is the Programmer’s Perspective that the book’s title boasts.
xix

Acknowledgments

I wish to acknowledge all those who selflessly give to others. Stories


such as that of scientist and professor Gordon Hamilton https://www.
nytimes.com/2016/10/25/science/gordon-hamilton.html make one in-
stantly feel humble, giving one a different perspective on life. Here are
some excerpts from this article:

Many climate scientists spend most of their days behind a desk.


... Yet for a whole cadre of climate scientists, the work entails
real physical risks. Thousands of specialists – glaciologists, geol-
ogists, geodetic engineers, wildlife biologists and many others –
must travel to remote regions to better understand the effects of
warming on the natural world. Gordon Stuart Hamilton, 50, a
glaciologist at the University of Maine, was killed when on a sci-
entific expedition to Antarctica. He was surveying a trail to find
the crevasses that can make working on glacial ice so dangerous,
and his snowmobile plunged into one of them. He died doing a job
whose urgency and importance, whose implications for the fate of
all humanity, he understood as well as anyone. Yet he had carried
out his work with a sense of wonder.

Coming to computer science, clearly, one feels an immense sense of


gratitude for scientists who pursued the true meaning of computability
despite their personal poverty and lack of recognition. You will be reading
about many of these scientists in this book. The name of Moses Schön-
finkel comes to mind: please read about him. https://en.wikipedia.
org/wiki/Moses_Schonfinkel Here are some excerpts from this article:

In 1929, Schönfinkel had one other paper published, on special


cases of the decision problem (Entscheidungsproblem), that was
prepared by Paul Bernays. After he left Göttingen, Schönfinkel
returned to Moscow. By 1927 he was reported to be mentally ill
and in a sanatorium. His later life was spent in poverty, and he
died in Moscow some time in 1942. His papers were burned by his
neighbors for heating.

Alan Turing’s, Marie Curie’s and Stephen Hawking’s personal lives


also evoke a similar sense of awe and respect. Our scientific fields are
replete with stories of such struggles. Please be sure to look up the per-
sonal lives of scientists whose names you hear about! It often heightens
the power of their inventions, revealing the dedication of the woman or
man behind these inventions we may otherwise take for granted.
Specific Acknowledgements: My special thanks to all those who gave
me input on this book, read it minutely and found typos, contributed var-
ious items to this book, and the authors of existing books in this area.
xx

My most heartfelt thanks to a few select individuals who I admire deeply.


The first is Prof. John Brzozowski for his very careful proof-reading, his
writing of a foreword for me, and last but not least, his immensely im-
pactful contributions to computer science! Second in my list is Professor
Rick Neff of BYU-Idaho. Rick is the most astute reader I have encoun-
tered in my life! As I’ve once heard, “the ability to spot ugliness is the
beginning of wisdom!” Rick literally X-rays into typos and marks them
15
While I’ve never been to the Red Sea, beautifully in red ink.15 Third in my list is Professor Geoffrey Draper
thanks to Rick, I did traverse it! Any re-
of BYU-Hawaii for drawing all the cartoons in this book as well as the
maining typos are those that I carelessly
missed despite Rick’s comments. back-cover cartoon! Geoff impressed me as a student in my CS 6100 class
during Fall 2005 when, while taking my class, he also drew delightful
cartoons that were the highlight of my 2006 Springer book “Computation
Engineering.” Geoff’s cartoon of Alan Turing stumbling upon his inven-
tion has gone far and wide around the world!
Next, I would like to specifically thank these individuals: Shriram Kr-
ishnamoorthy for his astute comments; David Dill, Ching-Tsun Chou and
Vivek Sarkar for contributing to the back-cover quotes of this book; Randi
Cohen for proposing that I write this book; both Randi and Karen Simon
who gave considerable feedback; Callum Frazer for his help with this
project; Ian Briggs for testing Jove, creating many of the interesting ma-
chines that can be executed under Jove, and writing the Lambda Calcu-
lus code used in Chapter 18; Heath French for testing Jove; Steve Siegel
for his encouraging feedback; Michael Bentley for setting up Jupyter hub
for my class, and proof-reading; Paul Carlson and Arnab Das for their
extensive feedback; and all my TAs and students who suffered through
the early versions of this book that got used in the CS 3100 classes I’ve
taught. I thank Mark Van der Merwe for integrating Ling Zhao’s PCP
solver to support the experiments in Chapter 15, Mate Soos for the web-
based SAT solver mentioned in Chapter 16, and Tyler Sorensen for con-
tributing the BDD package used in Chapter 17. I thank Dr. David Beazley
for creating the excellent PLY lexer and parser package, and Prof. Fer-
nando Perez, creator of IPython and co-founder of Project Jupyter for
creating Jupyter notes and also encouraging me to post my work on the
Jupyter Gallery of Notebooks. I also thank the students who took my
class Models of Computation (CS 3100) at Utah during Fall 2017 and
2018 for their feedback. I apologize if I left out anyone inadvertently, and
wish to be gently reminded.
Last but not least, I wish to thank my amazing family (whose initials
are captured by the regular expression (k g)∗ ∩ ([a − z][a − z])3 ) for their
love and support. I thank my late parents (whose names I have taken on
as my middle and last names) who saw to it that I lacked nothing at all,
and my amazing brothers and sister-in-laws who took care of my parents.
I also dedicate this work to the many inspiring books that brought me
into science, and to the noble souls who gifted them to me.
Part I: Foundations

Pascal’s calculator
(Photographed by this author, Aug 2012
Musée des Arts et Métiers in Paris France)

Biography of Pascal
(Musée des Arts et Métiers)
1
What Machines Think

Computers are everywhere and their power seldom ceases to amaze even
those deeply embedded within the field! Computers seem to do (or be
involved with) everything: from processing payroll to making telephone
calls and even brushing our teeth (hiding within electric toothbrushes).
They have also beaten humans at (the quiz show called) Jeopardy! One
may therefore ask in all honesty: “can computers do everything?!”
There are obviously things computers can’t do.1 But what all can they 1
Like screw up something as badly as hu-
mans alone can do!
do, and what does “can do” mean in the context of a machine? Remember
that these questions were raised during the early part of the 20th cen-
tury when actual computers weren’t around; therefore, it isn’t clear what
answers flew around. It is quite possible that people relied on their im-
mediate experiences to provide answers—much the same way as people
imagined airplanes to be before the Wright brothers flew one.2 2
Some early models of airplanes actually
flapped their wings (imitating birds) and
Coming back to the subject of computing, early computing successes
invariably crashed (unlike most birds).
were marvels of engineering. Blaise Pascal created an impressive ma-
chine around 1642 to assist his father with his work (Page 1). According
to Wikipedia, Pascal conceived the idea while trying to help his father
in his job as a tax collector. Pascal’s calculators could add and subtract
two numbers directly and multiply and divide by repetition. About 200
years later, Babbage designed the Analytical Engine (trial design of 1837
shown in Figure 1.1). He even hired the first ever programmer—-Ada
Lovelace—who was introduced to Babbage when she was just 17 years of
age, and got “hooked onto computing!” 3 3
Ms. Lovelace described the first algo-
rithm to compute Bernoulli numbers us-
At the turn of the 20th century, people started pondering the question
ing the Analytical Engine of Babbage.
of the fundamental limits of computing. They began formulating this
question in terms of the types of problems in mathematics and logic that
can be “solved” using a machine. It wasn’t clear why there should be a
fundamental limit to the power of computers. After all, authors such as
Jules Verne were at this time writing science fiction pieces pertaining to
going to the moon in spaceships (even before planes arrived).
4 AU T O M A T A , C O M P U T A B I L I T Y : P R O G R A M M E R ’ S P E R S P E C T I V E

1.1 Problems Without Algorithms

One can always state problems without knowing how to solve them.
For instance, Fermat posed the problem of finding a, b, c that are natu-
ral numbers greater than 1 such that a n + b n = c n for n > 2. This problem
remained open for over 300 years till 1995 when it was conclusively set-
tled by Andrew Wiles in the negative: no such a, b, c can exist [46]. Until
that year, one could have defined a procedure that could have searched
for every possible a, b, c in a systematic way for a given n (say 3). For
Figure 1.1: Babbage’s Analytical En- instance, the procedure could list all a, b, c that add up to 6, then list all
gine. By Science Museum London a, b, c that add up to 7, etc., till such an a, b, c, were found that satisfied
/ Science and Society Picture Library.
Reproduced under Creative Commons this equation. However, since 1995, thanks to the proof by Andrew Wiles
Attribution-Share Alike 2.0 Generic li- of a theorem called Fermat’s last theorem, we know that this procedure
cense. http://creativecommons.org/
licenses/by-sa/2.0.
would go on forever, not finding any a, b, c. In fact, we now have an algo-
rithm: just print “impossible to find such a, b, c” and halt.
In the same vein, people in the 20th century were in hot pursuit of
algorithms for pretty much every problem that can be posed in mathe-
matics! Most notable in this quest was a German mathematician named
David Hilbert who challenged the mathematics and logic community with
23 problems pertaining to logic, mathematics and computability. One of
Hilbert’s challenges was to prove the following:4

There should be an algorithm—a systematic and mechanical pro-


cedure that also terminates on any input—to decide the truth of
any logical statement in mathematics.
Figure 1.2: The website http:
//aturingmachine.com and the Youtube
This was such a bold quest: Hilbert wanted any mathematical question
Video https://youtu.be/E3keLeMwfHY
of a Mechanical Turing Machine by Mike to be algorithmically solved (always halt with “here is the solution” or
Davey. Please search for An Interview “you can’t have a solution.”) A long line of famous mathematicians and
with Mike Davey about his Homemade
Turing machine also.
logicians that includes Gödel, Church, and Turing showed that this goal
was impossible to realize! They showed that many mathematical systems
4
The distinction between a procedure and are undecidable: there isn’t an algorithm to decide the truth or falsity of
an algorithm is taken up in Chapters 13 statements made in them! They also showed that many logical systems
through 15. Basically, a procedure is any-
thing that is mechanizable, and it is also
are incomplete: there are logical systems that are so powerful that one
an algorithm if it halts on all of its ex- cannot prove known truths in them.5
pected inputs. We already gave you an ex-
ample of these ideas in the context of Fer- For concreteness, Hilbert’s tenth problem was to devise an al-
mat’s problem.
gorithm for finding solutions (over integers) for Diophantine
5
equations—equations of the form
For a detailed discussion (that tends
to be rather technical), kindly see
https://plato.stanford.edu/entries/
3x2 − 2x y − y2 z − 7 = 0.
goedel-incompleteness/. This, how-
ever, does not mean that humans cannot
Another Diophantine equation is
guide the creation of useful proofs. See an
early paper that proves facts about a tool x2 + y2 + 1 = 0.
that synthesizes logic circuits [1].
It turns out that the former has the solution x = 1, y = 2, z = −2
WHAT MACHINES THINK 5

while the latter has no solution.

A series of results developed in the 1940s through 1970 by Julia Robin-


son.6 The joint work by Yuri Matiyasevich, Julia Robinson, Martin Davis, 6
We are using a system called Jupyter
to illustrate ideas underlying this book.
and Hilary Putnam helped settle Hilbert’s 10th problem in the negative
“Jupyter” stands for Julia and Python,
through the so-called MRDP theorem (see a nice historical account at where the language Julia is named after
[34]). Their work showed that alas, we cannot write a single computer the same Julia Robinson who also helped
settle Hilbert’s 10th problem. She is con-
program that always halts and either prints out the solution for such an sidered one of the preeminent computa-
equation (if one exists) or prints out that such a solution does not exist. A tional theory researchers.
third possibility is to be admitted in case the equation has no solution:
any such program must necessarily loop and never halt!
In this book, we shall be building some of the foundations toward ap-
preciating all this momentous work. Some of the later chapters will also
detail the topic of the fundamental limits of computing.
The vast majority of this book, however, discusses the serendipitous
outcomes of these early pursuits seeking the fundamental limits of com-
puting. In the end, these pursuits did help settle many open theoreti-
cal questions, but along the way, they spun off many fundamental ideas
that form the bedrock of modern Computer Science. Fundamental de-
velopments in this young field are led by a brand new community of re-
searchers who go by the name computer scientist (and not “mathemati-
cian” or “logician”).7 7
It is not a big stretch to say that your
iPhone exists due to the aforesaid scien-
tists, plus of course countless excellent en-
1.2 How to Define a Computer? gineers who followed!

Even to get started on Hilbert’s program, one had to clearly define “a


computer.” Remarkably, within a few decades of the early 20th century,
mathematicians managed to arrive at the definition of a computer (i.
e., an ultimate computing device). This work was spearheaded by many
famous scientists including Alan Turing of England as well as Alonzo
Church, Emil Post, Stephen Kleene and John von Neumann of the United
States.8 8
Turing as well as von Neumann were
also involved in the construction of some
Alan Turing’s approach was to assume basic representations for num-
of the early computers.
bers (in terms of 0’s and 1’s) and describe how one performs operations
on numbers. His recipe for expressing an algorithm went something like
this:

• At each step of the algorithm, if a character c (say 0) is read un-


der the Turing machine head (or “cursor”), change the character
to a different one (say 1). Then move the head one step left, one
step right, or stay at the same spot. Then advance to the next
step of the algorithm.
• The algorithm is deemed to have halted when it reaches one of
the previously selected “halting steps.” In that case, the compu-
6 AU T O M A T A , C O M P U T A B I L I T Y : P R O G R A M M E R ’ S P E R S P E C T I V E

9
It does not matter how long it takes to tation ends, leaving behind the contents of the tape as the final
compute something; all that matters is
“can it be done at all, in a finite amount of result.
time?” For instance, in Mike Davey’s Tur-
ing machine, about 3 Turing machine op- It was soon shown that anything that is mechanically computable can
erations such as moving left or erasing can
be done in one second. Saving a megabyte be described in elementary terms such as this.9 See a modern Turing ma-
of data takes 870 of the 1000-foot tape chine detailed in Figure 1.2 and a cartoonist’s view of the historical mo-
rolls shown in Figure 1.2.
ment in Figure 1.3. An excerpt from Sir John Dermot Turing’s book [44]
recounts a conversation between Aspray and Church [4]:

Aspray: Can you tell me something about his (Turing’s) personality?


Church: I did not have enough contact with him to know. He had the
reputation of being a loner and rather odd.
(Aspray notes in [19] “Of course, the same often was said of Church.”)

Turing was not alone in the process of trying to define the concept
of computation. His contemporary, Church,10 meanwhile proposed the
Lambda Calculus. Another contemporary, Post, also proposed Semi-Thue
Figure 1.3: Prof. Geoff Draper’s imagi- Systems around the same time. It was also shown around this time that
nation of how Alan stumbled upon his in- things written in the Lambda Calculus of Church or Semi-Thue Systems
vention. This is one of many cartoons do-
nated by Professor Draper for my 2006 of Post could also do no more and no less than a Turing machine!11 This
book [21] (for all of Geoff’s cartoons in- convergence12 was taken as further evidence that the notion of what is
cluded in that book, kindly see http://
mechanically computable is unique. It also happens to be one of the most
draperg.cis.byuh.edu/cartoons).
fundamental of insights that humanity has achieved in terms of under-
standing what “computation” means.
Paraphrasing Prof. Phil Wadler (now of the University of Edinburgh),
10
Alonzo Church served as a PhD advisor
“when researchers come up with the same powerful idea even
for Alan Turing at Princeton University in
1938. when starting from multiple alternative perspectives, the idea it-
self is fundamental—“existed out there all the time”, and happens
to have been discovered—not invented.”

11
In this book, we shall adhere to Turing
Turing Machines Define a Computer
machines as our notion of a computer (and
“computation”).
From a practical perspective, it is clear that a supercomputer is more
12
powerful than the computer in your cellphone. It may take a second to
Metaphorically, all “roads of thought”
lead to “the Rome of universal computabil- do on a supercomputer what might take years to solve on your cellphone.
ity.” Yet (if you allow enough time and provide enough memory), anything
that can be solved on a supercomputer can be solved on your cellphone.
More specifically, the same types of problems can be solved on these ma-
13
One can bail a swimming pool with a chines.13 The reason we do not consider absolute time in our study of
teaspoon.
computation is that it is an entirely ephemeral notion; computers keep
getting faster as well as different from each other all the time. However,
an entire area called complexity theory is in fact devoted to the study of
“how much time computations take.” We will be studying one of the most
WHAT MACHINES THINK 7

central of approaches for measuring complexity—namely, the notion of


NP-completeness—in Chapter 16.

1.3 Practical Application: Syntax Definition/Checking

One of the main applications of the material we are going to study in this
book is in defining program syntax and in checking adherence to program
syntax definitions.
Beginning in the late 1950s, computer scientists began seeking practi-
cal ways of writing computer programs. They began developing rich and
expressive syntactic constructs, making programs easier to create as well
as readable. As soon as programming languages having formal syntactic
rules were created, it became clear that one had to parse these programs
(i. e., check for syntactic errors).14 Now, parsing is the act of checking 14
If there are no syntactic errors, the code
generator would typically be called next to
for “allowed patterns,” and flagging errors when disallowed patterns are
generate the object (machine) code.
seen. For instance,
• The identifier of a program variable is comprised of letters followed by
digits or special characters such as _; for example, Head1_ptr.
• Multiple variables can be declared in one line, separated by commas:

int a, b, c, d, e;

• A real number in standard fractional form has an optional sign fol-


lowed by the whole part, a period, and the fractional part; for example,
-1.03.
• A nested block is a collection of matching braces with things between
braces. Matched braces themselves are as follows: { }, {{ }}, {{{ }}}.
• A prototype declaration and a function definition match as follows (ex-
ample):

char func(int, float);

char func(int a, float b) { body }

As soon as one defines a syntax, one has to entertain the notion of


syntactic errors. A compiler relegates the low-level work of checking for
syntactic errors in the declaration of identifiers and numbers to what is
called the scanner, which is nothing but a highly simplified parser. The
results of scanning (when successful) are then fed to the main parser
which checks for the overall program well-formedness, and generates
code.
As an example, a scanner turns something like
8 AU T O M A T A , C O M P U T A B I L I T Y : P R O G R A M M E R ’ S P E R S P E C T I V E

int My_var1, My_Var2, My_Var3;


int My_var4 = -1.03;
int main(){{}}

15
It stashes away information regarding into (respectively) the following simpler patterns;15 a parser must now
which exact identifier is being declared,
recognize these patterns as being legal:
which keyword is being processed, etc., in
a data structure called a symbol table for
later reference.
keyword id, id, id;
keyword id = number;
keyword keyword lpar rpar lbrace lbrace rbrace rbrace

It is now clear that


• A scanner must reject a number of the form 30.-1 and accept a num-
ber of the form -1.03;
• A parser must reject a variable declaration statement of the form
keyword ; id id id„
and must accept something of the form
keyword id, id, id;
• There are usually no a priori limits on the lengths of identifiers or
numbers, nor limits on how many declarations one can have, or even
the level of nesting allowed using braces. Thus,
-12222244343434343.4343434343434343566689991
must be accepted as a legitimate number, and
{{{{{{{{}}}}}}}}
must be allowed to be a legitimate nested-brace structure.
In fact, you can check that the following is a syntactically correct C
program:

int main(){{{{{{{{}}}}}}}}

whereas if you omitted one right-brace, you will get a syntactic error,
essentially saying this:

error: expected ’}’


16
This is akin to how we define in- note: to match this ’{’
finite sets such as integers, but in int main(){{{{{{{{}}}}}}}
practice may not go beyond a cer-
^
tain size. The known universe it-
self has only about 1082 atoms, accord- 1 error generated.
ing to https://www.universetoday.com/
36302/atoms-in-the-universe/.
• Even if a system likes to impose limits, it is best to design scanners
and parsers as if they had no such limits.16
Another random document with
no related content on Scribd:
Night and Day therein for ten Days and for
Intelligence of the Motions of the Rebells and other
Expences

The Rebells having upon the 13th of Septemr.


crossed the Forth at a Ford some miles above Sterling;
Mr. Grosett by Direction of Lord Justice Clarke removed
the Ships and Vessells from the Harbours of
Borristounness and Queen’s Ferry, on the south side of
the River, to prevent the Cannon, Arms and Amunition
on Board of these Sloops and Vessells from falling into
the hands of the Rebells to prevent their having any
communication with the North side of the River Forth
otherwise than by going round the Way they came, and
which Mr. Grosett did, though the Rebells had at this
time by an advance party taken Possession of the Town
of Borristouness, about 12 miles to the West of
Edinburgh; Upon Mr. Grosett’s return to Edinburgh, upon
Sunday the 15th in the Evening he found General
Fowke,[591] who was just arrived from England with Lord
Justice Clarke, together with General Guest, and who
approved of what Mr. Grosett had done, and sent him
with Orders to Colonel Gardiner, to remain that Night
with the Troops at Coltbridge, about a Mile to the West of
the Town.

No. Expended in this service


2
7 12 0

Upon the 16th of September Mr. Grosett was sent out


twice in the morning to get Intelligence of the Motions of
the Rebells, and to let General Fowkes, who was then
posted at Coltbridge, know their Motions, and the last
Account he brought was that the Rebells had lain down
upon their Arms, about Twelve that Day near Kirklisten,
about 6 Miles to the West of Edinburgh, that the
Dragoons having soon after this upon the motion of the
Rebells towards them quit their Post at Coltbridge and
retired in some haste by the North side of the Town
about 3 that afternoon, without sending the Party of
Dragoons into the Town as had been conserted in the
morning of that Day, and Lord Justice Clarke observing
that this might give a Handle for justifying the Provest to
give up the Town to the Rebells, he sent Mr. Grosett to
the Provest, to press the Defence of the Town, and to
assure him, that as many of the Dragoons as he pleased
to desire should forthwith be sent in, to assist in the
Defence thereof, till Sir John Cope, who was then hourly
expected by sea from Aberdeen, should come with the
Troops to their Relief. But the Provost, declining to
defend the Town upon Pretence of the Uncertainty of Sir
John Cope’s coming in time to their Assistance, Mr.
Grosett returned and acquainted Lord Chief Justice
Clarke thereof; and as by this time an Express was
arrived from Dunbar with Letters for Lord Justice Clarke,
giving an Account of the Arrival of the Troops under Sir
John Cope off Dunbar; Mr. Grosett was the only Person
who would undertake to go back into the Town with these
Letters; but not being able to prevail with the Provost to
agree to the Defence thereof, he left it about One in the
Morning, and brought Lord Justice Clarke an Account of
what had past [narrowly escaped falling into the Hands
of the Rebells, who by 5 in the Morning were in full
Possession of the Town, the Gates having been opened
to them; But the Particulars of Mr. Grosetts Transactions,
and the Provosts Behaviour at this Juncture, will more
fully appear from a Narrative relating thereto, formerly
delivered by Mr. Grosett to His Grace the Duke of
Newcastle.[592]]

No. 16 17 0
Expended and Lost in this Service, Mr.
3 Grosett in his speedy Return to Edinburgh having
lost both his Hatt and Wig and killed one of his
Horses

Sep Mr. Grosett having retired with Lord Justice


t. Clarke to Dunbar, and his Lordship having upon
174
5
the 18th Septemr. received Information that the
young Pretender was to be that Night with some
of the Chiefs of his Party at the Dutchess of
Gordon’s house,[593] about 7 miles to the South-East of
Edinburgh on their Road to England, and that the First
Column of the Rebell Army was to march that way, Mr.
Grosett was sent by Lord Justice Clarke with proper
Guides and Assistants in the Night to reconnoitre and
send certain Intelligence of their Motions and number;
But receiving Information near the Dutchess of Gordon’s
House, that they had changed their Resolution he
returned and acquainted Lord Justice Clarke thereof;
From this time to the Battle of Preston, Mr. Grosett was
employed in reconnoitering and procuring Intelligence of
the Motions and Designs of the Rebells [narrowly
escaped being killed the Night before the Battle, by a
Party of the Rebells who lay in ambuscade in a Thicket
of Wood on the side of a hollow way, Mr. Grosett had to
pass through in going by the Directions of Sir John Cope
to observe their motions and numbers, as they were
drawing up in front of our army from whence they fired
close upon him, as he went along, and from which Place
they were drove by our Cañon after Mr. Grosett’s return
with an Account of their Situation]; and the Day of the
Battle he Lost a Horse and all his Baggage, the servant
who had charge thereof being made Prisoner by the
Rebells.

No. Expended and lost in this Service


4
33 6 0
Sep After the Battle of Preston Mr. Grosett went
t. from place to place to receive Information and
and
Oct
Intelligence of the Motions and Proceedings of
. the Rebells and so soon as he heard of General
174 Handasyde’s commanding at Berwick sent his
5 brother Captain Grosett, who was Aid-de-Camp
to the General, the Intelligence he from time to
time procured of the Motions, and Designs of the
Rebells, and amongst other services brought Prisoner to
Edinburgh Castle with the assistance of Mr. Brown and
some Farmers, Spalding of Whitefield[594] one of the
Chiefs of the Rebells, with Two others, who were
secured as they were returning, thro’ the West of
Scotland from the Rebell Army near Carlisle to the North,
in order to bring up about 3000 men more from that
Country, and for which purpose he had written Orders
from Mr. Murray, the young Pretenders Secretary, Duke
of Perth, Lord George Murray, and others of the Rebell
chiefs: As also the particular Route they were to take
with these men until they should join the Rebell Army,
who were to halt for them at Carlisle. All which Orders,
Route and other Letters and Papers found upon
Whitefield and his servant were delivered over with them
to General Guest, then in the Castle of Edinburgh.

No. Expended in this Service


5
24 8 0

Nov Upon the 13th Novemr. Mr. Grosett met Lord


. Justice Clarke at Musselburrow, and returned
with his Lordship to Edinburgh,[595] and the Day
after General Handasyde[596] arrived there with two
Regiments of Foot and the Remains of Hamilton’s and
Gardiner’s Dragoons; That the Castle of Edinburgh being
at this time in great want of Provisions of all sorts Mr.
Grosett by order of General Handasyde of the 16th
Novemr. procured and laid in a sufficient supply thereof
not only for the Garrison but for the Troops, that should
be employd in the Defence of the Town.

No. Expended in this Service


6
5 11 0

About this time the Rebells at Perth who were about


3000 in number being reinforced by the Landing of
Troops in the North, with Cannon and Stores from
France; Therefore in order to prevent the Kings Troops
from being surprized, and the Town of Edinburgh falling
again into the hands of the Rebells, Mr. Grosett by
Direction of Lord Justice Clarke, and Orders from
General Handasyde of the 26th and 27th November
1745,[597] went with proper Assistance and removed all
the Boats and Vessells that were at that time to be found
at the different Ports and Creeks on the Northside of the
River Forth, between Kinghorn and Aloa, to the
Southside thereof.

No. Expended in this Service


7
11 10 0

[Mr. Grosett at this time recovered seized Goods to


the value of £1800 which the Rebells had carry’d off from
the Kings Warehouse at Leith, and in which he was
greatly assisted by Genl. Handasyde.]
When the Rebells came to Edinburgh there were of
seized and condemned Goods, in the King’s Warehouse
at Leith, to the Value of about Ten Thousand Pounds,
and as these Goods were all carry’d off by the
assistance of the Rebells, and Mr. Legrand Collector at
Leith having upon their approach left Scotland, Mr.
Grosett at this time saved no Labour or Expense to get
Information by whom these Goods had been carried off,
and where lodged, found out and secured with the
assistance of a Company of the Military which he
procured from General Handasyde, as many of these
Goods as were sold for about £1500 and procured
Information of the Names of the Persons who with the
assistance of the Rebells carried these and the greatest
part of the other Goods from the Kings Warehouse, and
who are now under Prosecution for that offence, as well
as for the value of the Goods, that Mr. Grosett did not
recover.

No. Expended in this Service


8
68 14 0

Dec That after the Removal of the Boats and


. Vessells as above others having arrived, and
174
5
some of those that had been removed gone back,
and that it was found absolutely necessary for the
Service, that all Boats, Ships, and Vessells
whatsoever on the North Coast betwixt Kinghorn and St.
Andrews down the River as well as those formerly
ordered to be removed betwixt Kinghorn and Alloa up the
River should be removed to prevent the Rebells from
getting across with their Cannon, and as this cou’d not
be done without the assistance of some Ship of Force to
secure a Retreat in case of being surprized by the
Rebells who were in possession of that Part of the
Country; and as the Captain of the Milford Man of War
was ordered to concert proper measures with and assist
Mr. Grosett therein; Mr. Grosett by Order of Lord Justice
Clarke and General Guest the 8th Decemr. 1745 went
with the Custom House Boats and other proper
Assistance and performed what was required above.
No. Expended in this Service for Intelligence
9
6 10 0

Dec Mr. Grosett by Warrant of Lord Justice Clarke


.
174 and General Guest[598] of the 16th Decemr. 1745
5 removed with the Assistance of the Kings Boats
and Happy Janet armed Vessell, all the Boats
and Vessells at the Creeks on the South side of
the Forth betwixt Borristouness and Sterling to the
Harbour of Borristouness to prevent their being forced
from these Places by the Rebells. As also from Alloa a
Quantity of large Loggs of Wood and long Planks to
prevent the Rebells making Floats thereof to cross with
their Cannon at Alloa, with which they proposed to attack
Sterling Castle.

No. Expended in this Service


10
22 9 0

Dec Lord Justice Clarke having received


. Intelligence that the Rebells from the North were
174
5
to force their Passage across the River Forth at
Haigens and Carsy Nooks in flat bottomed Boats
they were bringing over Land from the Water of
Earn, to prevent which Mr. Grosett by Directions of his
Lordship the 19th Decemr. 1745[599] went and got that
Part of Sterling Shire which lies next to the River put in
arms to defend the Banks thereof, and by Order of
General Guest of the same Date engaged and stationed
the Pretty Janet armed Vessell with proper Hands, which
he procured at Borristouness to defend the Passage at
Haigen’s Nook and the Jean armed Sloop that at Carsey
Nook which effectually prevented the Designs of the
Rebells at this Juncture.

No. Expended in this Service


11
17 4 0

Dec To prevent the Rebells, upon their crossing


. the River Esk in their Return from England into
174
5
Scotland, having it in their Power to secure a
ready communication between them and their
friends in the North, and to get from thence the
Cannon and other Military Stores that had been landed
there by the French; Mr. Grosett by order of Lord Justice
Clarke and General Guest of the 21st December
1745[600] was directed instantly to remove all Vessells
and Boats of whatever Size out of the Harbours of
Borristouness, Queens Ferry or any where else upon the
South Coast of the Forth to such Places as he should
think most proper for his Majesty’s Service at so critical a
juncture and who accordingly removed all Boats and
Vessells whatever that could be floated [to the Roads of
Queens Ferry and Borristouness under the command of
the armed vessells lying there, which effectually
frustrated the Designs of the Rebells.]

Expended in this Service 860

Expended in forwarding from Borristouness Powder,


Ball, Grape Shot, and other ammunition sent thither from
the Castle of Edinburgh for the use of the Castle of
Sterling: Pretty Janet armed Vessell stationed at Haigens
Nook and the Jean armed Sloop at Carsy Nook to
prevent the Rebells crossing at these Ferries by order of
General Guest of 22nd Decemr 1745[601] 3 12 0
Upon the Return of the Rebells from England to
Scotland Orders being given to the two Regiments of
Foot Prices and Ligoniers, and to the Glasgow Regiment
and three Paisly Companies,[602] to march from Sterling
to Edinburgh for the Defence of that Place, Intelligence
being then received that the Rebells were at Moffat, in
their Road to Edinburgh which City was at this time (by
the Retreat of the two Regiments of Dragoons from
thence to Haddington in their way to Berwick) left without
any other Force but the Edinburgh Regiment[603] and as
it was probable, that the Rebells might in this situation
attempt to intercept the Troops in their March from
Sterling to Edinburgh; Mr. Grosett was Dispatched, with
Orders from Lord Justice Clarke and General Guest[604]
for hireing and pressing a number of Boats and Ships,
for embarking if necessary the Troops at Borristouness,
at which place they were expected from Sterling that
night; that Provisions should be laid in for Berwick but
that they should stop at Leith Road for Orders. Mr.
Grosett having agreeable to these Directions saved no
Expense and Labour in a service of so much Importance
had Ships and Boats at Borristouness for embarking and
transporting the Troops by the time they came there
which was about Twelve at Night. By these means the
Troops and the Glasgow and Paisly Militia, about a
Thousand in Number, who unable to continue their
march being embarked at Borristouness with the
Baggage belonging to the whole. They arrived on the
morning of the 24th Decemr. in the Road of Leith much
about the Time that the other Troops, who were
forwarded by Horses from Linlithgow, arrived at
Edinburgh; and who upon their Arrival at Leith were
ordered to disembark there, and by that Means and the
Number of Volunteers One Thousand and upwards
whom Lord Justice Clarke got to take up Arms in the
Neighbourhood of Edinburgh, and march immediately
into the Town. The Rebells being deterred from coming
forward to Edinburgh took the Road to Glasgow, where
they arrived the 25th Decemr. 1745.

To forward this Service Mr. Grosett got from General


Guest 100 gunners and expended therein 74 8 0

To Charges sending from Borrostownness and


destroying two Boats by Order of Lord Home 23d
Decemr. 1745[605] which the Friends of the Rebells had
got privately concealed, and made use of for carrying
Intelligence, and Dispatches to and from their friends on
the opposite side of the Forth the one at Newton Pow
and the other at Carron Water 2 16 0

Dec The Castle of Edinburgh being at this time


. crowded with a great Number of Prisoners, and
174
5
particularly with those taken by Captain Hanway
of the Milford Man of War, near Montrose, on
Board the Lewis a French Transport from
Dunkirk;[606] and as the keeping them there was looked
upon to be at this time dangerous Mr. Grosett by Order
of Lord Justice Clarke and General Guest hired to
Transports and shipt off these with other Prisoners at
Leith for Berwick the 26th Decemr. 1745 with a Company
of Foot on Board each Transport as a Guard.

No. Expended in this Service


16
640

Jan Lord Justice Clarke having received


. 7, Intelligence that the Rebells at Glasgow, being
174
5-6
reinforced by their Friends from the North were
preparing to March from that to attack Edinburgh
Mr. Grosett was thereupon sent with a Warrant
from his Lordship and from General Guest of the 1st
Janry.[607] to take and bring from Borristowness or from
on Board the Shipping lying in the Road of that Place all
the Cannon he could meet with to be placed upon the
Walls of the City of Edinburgh for the Defence thereof.
The Rebells upon their going into England, having
carried off or destroyed all the Cannon that were formerly
placed there for that purpose: Mr. Grosett went
accordingly and provided them, and brought them in a
vessell to Leith; But the Troops under General
Hawley[608] arriving by this time at Edinburgh, it was
then not thought necessary to bring them to that Place.

No. Expended in this Service


17
12 13 0

Jan Lord Justice Clarke having received certain


.
174 Intelligence,[609] that the Rebells were erecting
5-6 considerable Magazines at Alloa and that they
proposed bringing their Cannon to that Place, as
the most convenient for getting them across the
River Forth, in order to their laying siege to Sterling
Castle; His Lordship acquainted Generals Hawley and
Husk thereof and Scheme was thereupon formed for
getting Possession of their Cannon or at least retarding
their getting them across the River, till the General
should be ready to march with the Army to the Relief of
Sterling; In Order thereto two Sloops of War were sent
up the River, and Transports got ready by Mr. Grosett, to
take on Board Troops at Leith, [where a Regiment lay
ready to be embarked upon an hours warning]. Matters
having been thus prepared, and Lord Justice Clarke
having saved no expence in procuring Intelligence had
twice a day at least certain Accounts from Alloa and
other Places in that Neighbourhood giving the whole
Proceedings of the Rebells and having early in the
Morning upon the 8th of Janry. ‘received sure Information
that Lord John Drummond and Lord George Murray with
the whole of the Cannon, with which the Rebells
proposed to attack Sterling Castle were to be that Night
at Alloa escorted only by about 200[610] of the Rebells
they being under no apprehensions of meeting with any
Disturbance on that side of the River, the Bridge of
Sterling being Cut and the Rebell Army betwixt them and
the Kings Troops; His Lordship and General Husk came
thereupon to Leith and got 300 of the Troops that lay
there immediately embarked on this Expedition to be
commanded by Colonel Leighton,[611] and conducted by
Mr. Grosett. As the Rebells at Alloa could have no
Intelligence of their Designs, the Passage across the
Forth, being for some time before this stopt everywhere,
and as the Wind when they sailed proved favourable
they thought themselves sure of surprizing the Rebells
that Night at Alloa. But as by the time they had got one
third of the way thither, the Wind turned flat against them
it was one o’clock next Day before they got to Hegins
Nook three Miles below Alloa and where they were stopt
from proceeding farther by the Ebbing of the Tide. Upon
their Arrival there Mr. Grosett having received
Intelligence from Alloa that the Rebells were shipping
their cannon on Board of a Vessell, with a Design to
proceed in the Night, and to land them on the opposite
side of the River about two Miles above that Place so
soon as the Tide would admitt of the Vessell’s floating
and Sailing from that Harbour; and as Mr. Grosett was
perfectly well acquainted with the River and every Corner
of the Country, to prevent this, Fifty Soldiers and as
many armed Sailors were put on board one large and
two smaller Boats with orders to pass privately in the
Night to the Rebells Batteries at Elphinstone and Alloa
and lye at a Place appointed about a mile above Alloa,
where the Vessell with the Cannon was to pass, and
where they could not be observed, either from Alloa, or
even from the Vessells on Board of which they had
shipped their Cannon ’till they were just upon them, by
reason of the windings and Turnings of that River and as
Mr. Grosett had procured proper Pilots for that purpose,
they got to the Place appointed, without being observed
either from Elphingstone or from Alloa; and as the same
Flood Tide and Depth of Water, that would have carry’d
the Vessell with the Cannon from Alloa could have
carried the Sloops of War there, they could not in this
situation have failed, in securing the Vessell and
Cannon: But the Commander of the largest Boat, being
seized with an unreasonable Pannick, could not be
prevailed with to stay at the place appointed tho’ there
was much less Danger in remaining there than in
returning: For as in their Return they alarmed the Rebells
at Alloa by one of the Boats taking the Ground near that
Place, they had a continued Fire to get thro’ as they past
Alloa and Elphingstone Batteries; But Mr. Grosett having
by order of Lord Justice Clarke taken 200 Matts of Flax
from on Board a Dutch ship in the Road of Leith,[612] and
placed these along the sides of the Boats there was only
one man killed and another wounded on this Expedition,
and which answered the end so far as to keep the
Vessell from Sailing, and the Rebells from getting their
Cannon at this time across the River, as they had
projected; Mr. Grosett receiving at this Juncture
Information from Alloa that the Rebells there were not
even at this time above 200 strong: they upon the Return
of the Boats landed the Troops at Kincairdin about three
miles below Alloa, with a Design to attack them by Land.
But Mr. Grosett being soon after acquainted by different
Expresses from Alloa that the Rebells had just received a
Reinforcement of 300 men they returned and reimbarked
the Troops without any Loss. That Day the Vessell with
the Cannon sailed from Alloa; But Mr. Grosett having
fallen upon a method to get one of the Sailors who knew
the River, and whom they had forced out to assist in the
conducting of this Vessell persuaded to run her on
Ground on some of the Banks in her Passage up the
River; Upon receiving Information that the Vessell was
accordingly grounded, and that the Rebells had
thereupon dismounted their Battery at Alloa, and were
transporting all the Cannon they had there by Land two
miles above Alloa in order to cross them there. It was
thereupon resolved to attack their Battery at
Elphingstone, and after silencing the Cannon there to
proceed with small Vessells and Boats, and set Fire to
the Vessell that lay a Ground, with the Cannon and other
Military Stores: The Battery at Elphingstone was
accordingly attacked with great Resolution and Bravery
by the Captains of the Vulture and Pearl Sloops of War,
assisted by the Pretty Janet armed Vessell and Jean
armed sloop: And after about three hours close
cannonading within less than Musquet Shot of the
Battery, all their Cannon but one were silenced: But the
Pearl having her cable cut asunder by a Cannon ball,
she was forced from her Station by the strength of the
Ebb-Tide, and the Two Pilots of the Vulture (one of them
a Shipmaster at Elphinstone who at Mr Grosett’s
Request came to his assistance in this Expedition)
having each of them at this Time Lost a leg by another
Cannon ball, and by which Accident they both Dyed;
They were obliged to quit the Battery; This attack
however so far answered the End as to prevent the
Rebells from crossing with their Cannon; For upon
making this Attack they brought back their Cannon to
Alloa, and placed them upon the Battery, there to prevent
the Kings Ships and Troops from procedeing farther up
the River, and getting Possession of the Vessell that lay
aground above that Place with the Cannon and
Ammunition. In this whole affair Two men were killed,
Two lost a leg each by which they died and one an Arm
by the Cannon from the Batteries, Ten others were
wounded, but not disabled; the Chief Engineer and
several others of the Rebells were killed and many
wounded. Mr. Grosett having on the 11th January
received a letter from Lord Justice Clarke with an Order
inclosed from General Hawley to Colonel Layton to
return with the Troops to join the Army who were then
ready to march to the Relief of Sterling Castle, they
thereupon returned accordingly; But Mr. Grosett having
before he left that country concerted Measures for
getting the Vessell burnt that was grounded as above in
which the Cannon had been transported from Alloa it
was done accordingly without the Rebells knowing how it
came about.
Lord Justice Clarke in his letter, dated the 10th Janry.
1745/6,[613] having sent Mr. Grosett a Letter from
General Hawley to General Blackeney[614] who at that
time commanded in the Castle of Sterling with Directions
to use all methods possible to get it delivered and
answered, as on it depended Matters of the greatest
Consequence; Mr. Grosett not only got the Letter safely
delivered, but an answer thereto, tho’ the Rebells were at
that time in Possession of Sterling and suffered none
they could observe to go near the Castle and upon which
they kept the strictest Guard.

No. Expended in all these Services, for Provisions,


18 Intelligence, Boats, Pilots, etc.
39 4 0

Upon the 12th January 1745/6 Mr. Grosett returned to


Edinburgh from this Expedition, and upon the 13th was
sent early next morning by Lord Justice Clarke to
procure Intelligence of the motions of the Rebells for
General Husk, who was that Day to march with the First
Division of the Army from Edinburgh for Linlithgow;[615]
Upon Mr. Grosett’s coming near that place, being
informed by some Country People he had sent into the
Town, for Intelligence that about Twelve hundred of the
Rebells had that Morning taken Possession thereof and
given out that they were resolved to dispute their
Quarters with the Kings Troops. Mr. Grosett returned and
acquainted the General thereof who thereupon made the
proper Dispositions. The Town of Linlithgow lying in a
Hollow upon the South side of a large Lake which cuts
off all Communication with or access to the Town from
the North and Mr. Grosett having acquainted the General
of this and of the situation of the country The General in
order to surprize and cut off the retreat of the Rebells to
Falkirk where the Main body of their Army lay sent a
strong advance Party forward with orders to halt, and
remain upon a rising Ground upon the Road about a mile
from the East Gate of the Town, and within Sight thereof,
and marched the Main Body round another way; which
Mr. Grosett conducted them by the South side of the
Town where they could not be observed by the Rebells,
till they came near the West Gate thereof; and the
Rebells having no suspicion of the Main Body’s
advancing upon them, while the Advance Guard stood
still in sight, their communication with the Main Body of
their Army, would by these means have been cut off, if
one of their Friends upon accidentally seeing the King’s
Troops marching under cover of a Rising Ground by the
South side of the Town, had not rode into the Place and
given the Alarm to the Rebells who thereupon fled with
such Precipitation that the Troops could not come up
with them.[616] Lord [George Murray, Lord Elcho, and
others of their Chiefs left their Dinner just as it was going
to be set upon the Table. The Dragoons pursued, but not
being able to come up with them they took Possession of
the Bridge of Linlithgow, over the water Avon, about a
mile to the West of that Place, left a sufficient Guard
there, and then went into the Town, and eat the Dinner
the Rebells had provided, and got ready for themselves.]
The Person who gave the alarm was apprehended
together with one of the Rebells, and sent Prisoners to
Edinburgh, and Mr. Grosett after General Husk
appointed proper Places for Out Guards returned to Lord
Justice Clarke and General Hawley with an Account of
these proceedings.

No. Expended in this Service


19
450

The Army being in great want of Gunners and other


proper persons for the Artillery which was to march the
15th; but could not get forward ’till these were provided
Mr. Grosett by order of Lord Justice Clarke and General
Hawley went to Borristownness, and brought from
thence nine or Ten Sailors for that purpose who had
been Gunners on Board of Men of War.

No. Expended in this Service being obliged to


20 advance money to each of them to subsist their
Families in their Absence before they would
agree to go upon this service 16 8 0

Lord Justice Clarke having on Thursday the 17th


January in the morning received Intelligence of
Importance relating to the Motions and Designs of the
Rebells; Mr. Grosett was immediately dispatched by his
Lordship, to acquaint General Hawley thereof. After the
Battle which happened that day, Mr. Grosett was sent
back to acquaint Lord Justice Clarke what had happened
but more especially to let his Lordship know that the
Kings Troops had at last beat the Rebells from and kept
the Field of Battle ’till obliged to leave it for want of
Provision, and leave Seven of their Cannon on the Field
for want of Horses to carry them off. This Account gave
the greater Joy to Lord Justice Clarke, and the other
good Friends of the Government at Edinburgh, as before
that time they had, by the Flight of the Troops to that
Place, been much alarmed with the Accounts they gave
of the Defeat of the Kings Army:[617] As the Troops in
their Flight to Edinburgh occasioned those that had been
sent from theme with Provisions for the Army to return
towards that place. Mr. Grosett therefore by Order of
Lord Justice Clarke returned immediately on fresh
Horses to force them all back to Linlithgow there not
being a Morsel of Provision to be had on any
Consideration there for the Troops, who had all of them
been obliged to retire to that Place for want thereof, and
who by that means were well supply’d. But as the
General did not think it advisable to remain there with the
Troops, he sent Mr. Grosett back to Lord Justice Clarke
to get Quarters provided for the whole Army against their
Arrival at Edinburgh and which was done accordingly by
the time they got there which was about Four in the
afternoon.

No. Expended in this Service and for Assistance to


21 get forward the Provisions to Linlithgow
5 14 0

Jan Lord Justice Clarke having formed a Scheme


. to release the Officers of the Kings Troops, who
174
5/6
had been made Prisoners at the Battle of
Preston, and bring them by force from the
respective places to which they were upon their
Parole confined in that Part of the Country of which the
Rebells had at that time Possession. In order thereto
(after those at Glames were brought to Edinburgh) a
Company of the Argyleshire Highlanders were (by Order
of Lord Justice Clarke and General Hawley [to Colonel
Campbell][618] of the 20th January) put under Mr.
Grosett’s Directions who crossed the Forth with them in
the Night at Queens Ferry, and with their assistance
secured and brought safely to Edinburgh from different
Parts of the Shires of Fyfe and Perth, Colonel Halket,
Captains Stewart, Cochrane, and Dundas who at that
time were prisoners with the Rebells in these Countries.
[619]

No. Expended in this Service for Boats, Freight Horse


22 hire
6 11 0

Jan Lord Justice Clarke having on the 25th


. January about Eight at Night received Information
174
5/6
that the Rebells had formed a Design of
surprizing the Kings Troops that night at
Edinburgh, and in particular the Argyleshire
Highlanders, who were the Advance Guard to the Kings
Army, Mr. Grosett accompanied by Lieutenant Campbell
of the Edinburgh Regiment, went by His Lordships
Orders, and acquainted Colonel Campbell, and the other
officers thereof who commanded the advance parties
some miles from the Town, to put them upon their Guard,
and at the same time employed proper Persons to
patrole the Country the whole of that Night, to give timely
Notice of the Motions and Approach of the Rebells, and
who finding out had got Information of their Design came
no farther than Linlithgow.

No. Expended in this Service


23
1 10 0

Upon the 29th January 1745/6 Mr. Grosett was sent


by Lord Justice Clarke and General Hawley to meet the
Duke and acquaint His Royal Highness that the Troops
were ready to march and that the Guards, Coaches, and
Relays of Horses were at the proper stages for
forwarding His Royal Highness in the most expeditious
manner to Edinburgh.

No. ... and in getting Coach


24
140

Jan
The Duke upon his arrival into Scotland[620]
.
174 found it necessary for the Service to send two
5/6 armed Vessells and 100 of the Troops along the
Coast of Fife with Sundry Warrants which were
only to be shewn and put in execution as things
Cast up; These Troops were accordingly embarked by
Mr. Grosett, and as His Royal Highness was upon the
Recommendation of Lord Justice Clarke pleased to
direct that he should have the conducting of this
Expedition, and executing of these warrants as occasion
should require: The following Order to Captain Coren[621]
the Commanding Officer of that Party was with the
Warrants therein referred to delivered to Mr. Grosett, viz:

‘You are to be assisting to Walter Grosett Esq. one of
His Majesty’s Justices of the Peace in executing divers
Warrants which he will shew you when proper.’[622]
In consequence thereof Mr. Grosett upon the 30th
January sailed in the evening from Leith, with the said
Vessell and Party, and proceeded to different places on
the North Coast betwixt St. Andrews and Alloa, secured
all the Magazines of Provisions, which the Rebells had
on that Coast, returned the Provisions (being Oatmeal)
to the Persons from whom it had been taken by the
Rebells, as they were all of them well affected to the
Government, and secured nine of the Rebells and sent
them by Order of the Duke prisoners to Sterling, His
Royal Highness being at this time there with the Army.

You might also like