⚛️

Theory of Computation

Requisites

Resources

Lenguajes Formales y Autómatas. (2020, January 19). Retrieved from https://turing.iimas.unam.mx/~ivanvladimir/page/curso_lfya_2022i

Welcome to maquinas’s documentation! — ⚙️ maquinas 0.1.5.14 documentation. (2023, January 23). Retrieved from https://maquinas.readthedocs.io/en/stable

History

NameTags
Machines, Languages, and Computation at MITPeter J. DenningTheoretical Models of Computationanecdotes
Lectures on computationRichard P. Feynman


Introduction

What is the Theory of computation?

mathematical cybernetics

Why does the Theory of computation matter to you?

https://twitter.com/Plinz/status/1541937546006933504

Research

ACM SIGACT

Ecosystem

Standards, jobs, industry, roles, …

Story

FAQ

Worked examples

Models of computation

https://gist.github.com/sanchezcarlosjr/333b9a774c0fa57f55532d772b13c946

https://cs.brown.edu/people/jsavage/book/pdfs/ModelsOfComputation_Chapter4.pdf

Model of computation

Automaton (Mathematics model)Paradigm
Turing MachineImperative (procedural) programming
Lambda-calculusFunctional programming
Pi-calculusEvent-based and distributed programming
OOP
Logic

Mind and computation

https://en.wikipedia.org/wiki/Talk:Theory_of_computation

Strings and Languages

Definitions.

Alphabet. Any non-empty finite set. We generally use either capital Greek letters Σ,Γ\Sigma ,\Gamma , or uppercase letters A,B,...A,B,... to designate alphabets.

Symbol. The members of an Alphabet.

Examples: Σ1={0,1};Σ2={a,b,c,d,e,f,g,h};Γ={0,1,x,y,z}\Sigma_1=\{0,1\};\Sigma_2=\{a,b,c,d,e,f,g,h\};\Gamma=\{0,1,x,y,z\}

String over an alphabet. A finite sequence of symbols from that alphabet.

Length of a string. If ww is a string over Σ\Sigma, the length of ww, written w|w|, is the number of symbols that it contains. Some authors prefer to use na(w)n_a(w) for indicating some aa characteristic applied on ww.

Empty string. w=ϵ=λ=0|w|=\epsilon=\lambda=0

Language. Set of strings. We generally use upper-case letters A,B,...A,B,... to designate languages.

Lexicographic ordering.

1. Shorter strings precede longer strings.

2. The first symbol in the alphabet precedes the next symbol.

Regular operations.

Let A,BA,B be languages:

Union: AB={wwA or wB}A\cup B=\{w|w\in A\text{ or }w\in B\}

Concatenation: AB={xyxA or yB}=ABA\cdot B=\{xy|x\in A\text{ or }y\in B\}=AB

Ak=AA...Ak times,A1=A,A0={ϵ}A^k=A \overset{\text{k times}}{ A...A},A^1=A,A^0=\{\epsilon\}

Kleene star: A={x1...xk each xiA for k0}=A0A1A2A3...A^*=\{x_1...x_k|\text{ each }x_i\in A \text{ for } k\ge 0\}=A^0\cup A^1\cup A^2 \cup A^3 \cup..., note ϵA\epsilon\in A^* always.

Kleene plus: A+=A1A2A3...=VVA^+=A^1\cup A^2\cup A^3\cup...=V^*V

Example. Let A={good,bad}A=\{good,bad\} and B={boy,girl}B=\{boy,girl\}

AB={good,bad,boy,girl}A\cup B=\{good,bad,boy,girl\}

AB={goodboy,goodgirl,badboy,badgirl}AB=\{goodboy,goodgirl,badboy,badgirl\}

A2={goodgood,goodbad,badgood,badbad}A^2=\{goodgood,goodbad,badgood,badbad\}

A={ϵ,good,bad,goodgood,bgoodbad,badgood,badbad,goodgood,good,goodgoodbad,...}A^*=\{\epsilon,good,bad,goodgood,bgoodbad,badgood,badbad,goodgood,good,goodgoodbad,...\}

Chomsky hierarchy

Regular languages

Operator precedence

OperatorPrecedence
()6
*+?5
concatenation3
|2
aSigmaa\in Sigma1

Examples:

ax|b → (ax)|b

ab*|c → (a(b*))|c

ab?* → a((b?)*)

ab*? → a((b*)?)

Note: L(a((b?)*)) = L(a((b*)?))

If two operators have the same precedence, then they generate the same language. ??

https://webcache.googleusercontent.com/search?q=cache:UnlM5CsShGAJ:www.cs.columbia.edu/~aho/cs3261/Lectures/L3-Regular_Expressions.html&cd=1&hl=en&ct=clnk&gl=mx&client=firefox-b-d

https://www.oreilly.com/library/view/learning-awk-programming/9781788391030/ceafcbc7-fe9e-4c33-b899-ed2f48e7e659.xhtml

https://www.boost.org/doc/libs/1_56_0/libs/regex/doc/html/boost_regex/syntax/basic_extended.html#boost_regex.syntax.basic_extended.operator_precedence

TODO: 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaac'.match(/^(a+)+b/)

Regular Expressions

Pathological regular expressions can execute in geometric time. For example 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaac'.match(/^(a+)+b/) will effectively crash any JavaScript runtime. JS-Interpreter has a variety of methods for safely dealing with regular expressions depending on the platform. https://neil.fraser.name/software/JS-Interpreter/docs.html

Worked examples

Homework

Finite state machine

Representations.

Moore and Mealy. Responses are all kinds of values.

DFA and NFA. Responses are 0 or 1.

Programming.

Image from Domain Specific Languages by Martin Fowler, with Rebecca Parsons

Worked examples

  1. Make a program in C++ that verify emails by argument (Hint: use the library regex).

Moore and Mealy automata

Boolean Algebra and Digital Logic. Circuit conversion. From Moore to Circuit.

https://www-igm.univ-mlv.fr/~berstel/Exposes/2009-06-08MinimisationLiege.pdf

Finite automata

Finite automata and their probabilistic counterpart Markov chains. Finite Automaton (FA) or Finite-State Machine. An automaton (Automata in plural).

State diagram of a finite automaton M1. It has two states.
🔥
OOP uses a State Pattern, which is a design pattern.

We feed the input string 0101 to the machine M1M_1, the processing proceeds as follows:

  1. Start in state q1q_1.
  1. Read 1, follow the transition from q1q_1 to q2q_2.
  1. Read 1, follow the transition from q2q_2 to q1q_1.
  1. Accept because M1M_1 is in an accept state q2q_2 at the end of the input.

The formal definition of a deterministic finite automaton (DFA)

A finite automaton is a 5-tuple (Q,Σ,δ,q0,F)(Q,\Sigma,\delta,q_0,F) where

  1. QQ is a finite set called the states,
  1. Σ\Sigma is a finite set called the alphabet,
  1. δ:Q×ΣQ\delta:Q\times \Sigma\rarr Q is the transition function,
  1. q0Qq_0 \in Q is the start state, and
  1. FQF\subseteq Q is the set accept states, they are sometimes called final states.

The formal definition of a nondeterministic finite automaton (NFA)

A finite automaton is a 5-tuple (Q,Σ,δ,q0,F)(Q,\Sigma,\delta,q_0,F) where

  1. QQ is a finite set called the states,
  1. Σ\Sigma is a finite set called the alphabet,
  1. δ:Q×(Σ{λ})P(Q)={RRQ}\delta:Q\times (\Sigma\cup\{\lambda\})\rarr P(Q)=\{R|R \subseteq Q\} is the transition function (P is a power set),
  1. q0Qq_0 \in Q is the start state, and
  1. FQF\subseteq Q is the set accept states, they are sometimes called final states.

Converting NFAs to DFAs

Rabin-Scott Theorem

https://twitter.com/getjonwithit/status/1720832283026854098

delta = {
  'state1': {
     'symbol1': {'state1', 'state2',...}
   }
}
# BFS
NFAToDFA(NFA)
   
         

Classification

https://static.googleusercontent.com/media/research.google.com/en//pubs/archive/40342.pdf

Worked examples

Cellular automaton

FAQ

What is the difference between finite automata and finite state machines?

Pattern Matching

Extended finite-state machine

https://www.seas.upenn.edu/~lee/10cis541/lecs/EFSM-Code-Generation-v4-1x2.pdf

Regular expressions. Regex.

💡
Stephen Cole Kleene invented regex.

Regular expressions, in short regex, are expressions that generate regular languages.

Although some modern regex engines support regular expressions, they support nonregular languages (Regular Expression Recursion) too. It can be misleading, but we use the above definition forward.

POSIX regular expressions. BRE (Basic Regular Expressions), ERE (Extended Regular Expressions), and SRE

Flavors. Perl and PCRE.

https://gist.github.com/CMCDragonkai/6c933f4a7d713ef712145c5eb94a1816

Regex engine.

perl vs awk

https://unix.stackexchange.com/questions/114591/is-perl-ever-a-better-tool-than-awk-for-text-processing

Accepters are equivalent to regular expressions

Are traducers equivalent to regular expressions?

/^1?$|^(11+?)\1+$/

Worked example

Tools

https://regex101.com/

https://regexr.com/

AWK

Roman numbers: https://wiki.imperivm-romanvm.com/wiki/Roman_Numerals

RegEq

Check equivalence of regular expressions

https://bakkot.github.io/dfa-lib/regeq.html

https://regex-equality.herokuapp.com/

Goyvaerts, J. (2022, December 02). Regular Expressions Reference. Retrieved from https://www.regular-expressions.info/reference.html

Regular expressions to Automata. Generalized nondeterministic finite automaton. Arden’s Rule.

Regular expressions to Automata

  1. Generalized nondeterministic finite automaton.
def convert_to_regular_rxpression(G):
   if G.number_of_states == 2:
      return 
  1. Solving right-linear equation by Arden’s rule.

https://assets.press.princeton.edu/chapters/i11348.pdf

https://www.gwern.net/docs/math/1973-knuth.pdf

Steps to convert regular expressions directly to regular grammars and vice versa

https://cs.stackexchange.com/questions/68575/steps-to-convert-regular-expressions-directly-to-regular-grammars-and-vice-versa

Free Grammars. LL.

Deterministic context-free languages.

FAQ

Are axioms in math equivalent to production rules in unrestricted grammars?

how could a CFG hold an unrestricted grammar (UG), which is turing complete?

https://stackoverflow.com/questions/61952877/chomsky-hierarchy-examples-with-real-languages?rq=1

Notes and references

[1] M. Johnson, “Formal Grammars Handout written,” 2012 [Online]. Available: https://web.stanford.edu/class/archive/cs/cs143/cs143.1128/handouts/080 Formal Grammars.pdf. [Accessed: 28-Feb-2022]

Grammar

We describe the syntax of language as grammar but not semantics.

Definition

A grammar is a 4-tuple (V,T,S,P),(V,T,S,P) ,

where VV is a finite set of objects called variables or nonterminals,

TT is a finite set of objects, disjoint from VV, called terminal symbols, in short terminals, and sometimes tokens because it is a terminal identifier.

SVS\in V is a special symbol called the start variable,

PP is a finite set of rules, called production rules, with each rule being a variable a string of variables and terminals.

If production rules are of the form

xyx\to y

where x(VT)+x \in (V \cup T)^+, y(VT)y \in (V \cup T)^*. Some authors call x and y Head and Body respectively. Others call x and y left-hand side (LHS) and right-hand side (RHS).

The production rules are applied in the following manner: Given a string w1=uxvw_1=uxv

RuleApplicationResult
xyx \to yw1=uxvw_1=u\bold{x}vw2=uyvw_2=u\bold{y}v

We say that w1w_1 derives w2w_2 o that w2w_2 is derived from w1w_1.

A rule can be used whenever it’s applicable.

If w1w2...wn,w_1\to w_2\to ...\to w_n,

we say that w1w_1 derives wnw_n and writes

w1wnw_1 \to^* w_n . The * indicates an unspecified number of steps (including zero).

Informally, grammar consists of terminals, nonterminals, a start symbol, and productions.

  1. Terminals.
classDiagram
    class TerminalSymbol
    TerminalSymbol : token // id or name
    TerminalSymbol : lexeme // regular expression
  1. Nonterminals are syntactic variables that can be replaced, so they denote a set of strings.
  1. Start symbol.
  1. Productions. We may follow a notation for production rules, which is a particular form.

BNF. A particular form of notation for grammar (Backus-Naur form).

CNF. A particular form of notation for grammar (Chomsky normal form).

Bottom-up

graph LR
    subgraph scanning
        W -. find the body X that match w=uXv .-> Body
    end
    subgraph substitution
        Body -. substitute body X to head Y, w=uYv.-> Head
        Head -- search if it is not StartSymbol --> W
    end

Backtracking.

Chomsky normal form (CNF) example

V={P,S,M}V=\{P,S,M\}, T={+,,1,2,3,4}T=\{+,*,1,2,3,4\}, SVS\in V

PSSS+MMMMTTT1234P \to S \\ S \to S+M|M\\ M \to M*T|T\\ T \to 1|2|3|4

Backus–Naur form (BNF) example

V={P,S,M}V=\{P,S,M\}, T={+,,1,2,3,4}T=\{+,*,1,2,3,4\}, SVS\in V

<P>::=<S><S>::=<S>+<M><M><M>::=<M><T><T><T>::=1234<P> ::= <S> \\ <S> ::= <S> + <M> | <M>\\ <M> ::= <M> * <T> | <T>\\ <T> ::= 1 | 2 | 3 | 4

Derivations

<P>a<Q>::=1Q1<P>::=b<Q>::=2<P>a<Q> ::= 1Q1\\ <P> ::= b \\ <Q> ::= 2

Parse trees and derivations

Ambiguity

Writing a grammar

Verifying the language generated by a grammar

The language generated by GG. Let G=(V,T,S,P),G=(V,T,S,P) , be a grammar. Then the set

L(G)={wT:Sw}L(G)=\{w\in T^*:S\to^* w\}

is the language developed by G.

Grammar Hierarchy

Set view

https://www.geeksforgeeks.org/chomsky-hierarchy-in-theory-of-computation/

Table view

Grammar typeGrammar acceptedLanguage AcceptedAutomatonProduction rule form
Type 0.
These are the most general.
Free or unrestricted grammarRecursively enumerable languageTuring Machineuvu\to v,
where both u and v are arbitrary strings of symbols in V, with u non-null.
Type 1Context-sensitive grammarsContext-sensitive languageLinear-bounded automatonuXwuvw,uXw\to uvw, where uu,vv, and ww are arbitrary strings of symbols in VV, with vv non-null, and XX a single nonterminal. In other words, in a particular context.
Type 2Context-free grammarsContext-free languagePushdown automatonXvX\to v,
where
vv is an arbitrary string of symbols in VV, and XX is a single nonterminal. i.e. regardless of context.
Type 3.
These grammars are the most limited in terms of expressive power.
Regular grammarsRegular languageFinite-state automatonXaX \to a,

XaYX \to aY (right-regular grammar),

XYaX \to Ya (left-regular grammar),

XϵX \to \epsilon,
where
XX and YY are nonterminals and aa is a terminal.

Regular grammar is either right-linear grammar or left-linear grammar, but not both.

Mixing left-regular and right-regular rules are the context-free grammar.

https://www.cs.utexas.edu/users/novak/cs343283.html

Augmented Grammar

Definite clause grammars

Worked examples

Automata and Grammar

Context-free grammars

TODO: Context-Free Grammars Versus Regular expressions

Classification

From Berkley Prof. Sen CS 164 Lecture 8-9

Notional conventions

Derivations

Ambiguity

  1. Conversión de gramáticas ambiguas a no ambiguas.
  1. Estrategias para el manejo de la ambigüedad en los lenguajes.
  1. Problemas equivalentes a determinar si una gramática es ambigua.
  1. Complejidad de determinar si una gramática es ambigua.

Writing a grammar

Eliminating Ambiguity

Elimination of Left Recursion

A left recursive grammar has a nonterminal AA such that there is a

derivation for some string aa

A+AaA\to^+ Aa

Top-down parsing methods cannot handle left-recursive, so

Method.

Given a grammar, you replace each left-recursive pair of productions AAaβA\to Aa|\beta

AAaβA\to Aa|\beta

by

AβAAaAϵA\to \beta A'\\ A'\to aA'|\epsilon

Example.

EE+TTE\to E+T|T

We replace recursive pair of productions following the method by

ETEE+TEϵE\to TE'\\ E'\to +TE'|\epsilon

General Algorithm.

References

Elimination of Left Recursion. (2021, October 31). Retrieved from https://cyberzhg.github.io/toolbox/left_rec

L-system or Lindenmayer system

https://runestone.academy/ns/books/published/thinkcspy/Strings/TurtlesandStringsandLSystems.html

Left Factoring

Non-Context-Free Language Context

Computability theory

Turing Machine

Undecidable problems

Reductions

FAQ

Continuous "recursive iteration"

Self-replicating programs. Quine functions (computing).

intron functions computing

function twice(x) {
   console.log(x); // Action 
   console.log("'" + x + "'"); // Template
}
// If language allows read function code
twice(twice.toString())
// If language doesn't allow read function code
twice("function twice(x) {console.log(x);console.log(\"'\" + x + \"'\")}")

// Full Self replication
function self_reply() {
   function twice(x) {
      console.log(x); // Action
      console.log("'" + x + "'") // Template
   }
   twice(twice.toString()+"\ntwice(twice.toString())") // Execution
}

// Another option
function self_reply() {
    A = f.toString() // Template
    function f() {
      console.log("MY Instructions"); // Payload 
      console.log(A+"f()"); // Reflection, Reproduction, and mutation
    }
    f()
}

// Another option
(function quine() {
  console.log(quine.toString());
})();

// Other option
$=_=>`$=${$};$();$()`

Conferences, N. (2020, February 26). The Art of Code - Dylan Beattie. Youtube. Retrieved from https://www.youtube.com/watch?v=6avJHaC3C2U&ab_channel=NDCConferences

Fridman, L. (2020, July 11). Self-replicating Python code | Quine. Youtube. Retrieved from https://www.youtube.com/watch?v=a-zEbokJAgY&ab_channel=LexFridman

Computational complexity theory

A high-level overview of NP

NP

NP are all languages where one can verify membership quickly.

NP are all languages where one can test membership quickly.

Certificate concept.

Does NP=P?NP=P?

Cook-Levin Theorem: SATP    P=NPSAT \in P \implies P=NP. Proof later.

Def. AA

Theorem.

Not always “good” algorithms to solve problems. But many problems we think about can be checked in polynomial time or solved by brute force in exponential time.

NP-Completeness

Schema of reductions.

TODO: A Boolean formula ϕ\phi is in CNF that consists of literals (a variable or a negated variable) and clauses (an OR of literals).

3SAT={<ϕ>ϕ is a satisfiable 3CNF formula}3SAT=\{<\phi>|\phi \text{ is a satisfiable 3CNF formula} \}, a special case of SAT.

A clique in a graph is a collection of points that are all pairwise connected by lines. k-clique in a graph is a subset of k nodes all directly connected by edges.

Theorem. 3SATPCLIQUE3SAT\le_P CLIQUE

We’re going to reduce the 3SAT problem into CLIQUE.

Reduction.

Nodes are literals.

Edges. G has all non-forben deges.

Algorithms

WalkSAT

Cook-Levin Theorem

References

https://www.claymath.org/sites/default/files/pvsnp.pdf

References

https://www.cs.toronto.edu/~sacook/math_teachers.pdf

FAQ

If we have an input has a size S(n), does our algorithm has to be at least the size, formally S(n)=O(T(n))S(n)=O(T(n))? Suppose the permutations of a string with nn, it have S(n)=n!S(n)=n! permutations, so our algorithm checks the relation S(n)=n!=O(T(n))S(n)=n!=O(T(n)).

No. Sometimes you can write an algorithm T(n)=O(S(n))T(n)=O(S(n)).

Graph grammars

Introduction to graph grammars with applications to semantic networks

P system

The Rough P System: Simulation of Logic Gates and Basic DB Tasks using Rough P System

TODO

“We tend to forget that every problem we solve is a special case of some recursively unsolvable problem!” Knuth, D. E. (1973). The dangers of computer-science theory. In Studies in Logic and the Foundations of Mathematics (Vol. 74, pp. 189-195). Elsevier.

21] D.E. Knuth, On the translation of languages from left to right, Information and Control 8 (1965) 607–639.

[22] D.E. Knuth, A characterization of parenthesis languages, Information and Control 11 (1967) 269–289.

Rice’s theorem