II Automata and Formal Langauges
Here the natural numbers are $\N = \set{0, 1, 2, \ldots}$ and we represent $n\in \N$ as $n=\set{0,1,\ldots,n-1}$.
Let $X$ be a set.
-
$X^n = \set{\text{functions }n\to X}$ is the set of $X$-strings of length $n$.
-
$X^0 = \set{ε}$ contains only the empty sequence $ε$.
-
$X^\ast = \bigcup_{n\in \N} X^n$ is the set of all $X$-strings.
-
$X^+ = X^\ast \setminus \set{0}$ is the set of non-empty strings.
-
For $α\in X^\ast$, its length $|α| = n$ if $α\in X^n$.
-
For $k\leq n$, $α\in X^n$, the unique initial segment of $α$ of length $k$ is denoted $α|_k$.
-
For $α,β\in X^\ast$, their concatenation $αβ \in X^{|α| + |β|}$ is defined as $αβ(k) = α(k)$ if $k\leq |α|$ and $αβ(k) = β(k-|α|)$ if $k>|α|$.
-
For $α\in X^\ast$, define $α^n$ as the concatenation of $n$ copies of $α$ (recursively as $α^0 = ε$, $α^{n+1} = α^n α$).
-
$β$ is a substring or subword of $α$ if $α = γβδ$ for some strings $γ, δ$ (possibly empty).
Let $Σ$ and $Π$ be alphabets.
A homomorphism $h:{}$$Σ^\ast$$\to Π^\ast$ is a function satisfying $h(αβ) = h(α)h(β)$ for all $α,β\in Σ^\ast$.
Given any function $f: Σ\to Π^\ast$, there is a unique homomorphism $\hat f: Σ^\ast\to Π^\ast$ extending $f$, given by $\hat f(ε) = ε$ and $\hat f(αx) = \hat f(α) f(x)$ for $α\in Σ^\ast$ and $x\in Σ$.
A set $X$ is infinite if there exists an injection $\N \to X$. Otherwise it is finite.
A set $X$ is countable if there exists a surjection $\N \to X$. Otherwise it is uncountable.
A rewrite system $R$ is a pair $(Ω, P)$ where
-
$Ω$ is a non-empty finite set (of symbols); and
-
$P\subseteq$ $Ω^+$ $\times$ $Ω^\ast$ is a finite set (of rewrite rules or production rules).
We write $α\to β$ for an element $(α,β)\in P$.
For $σ,τ\in Ω^\ast$, we write $σ \xrightarrow{R}_1 τ$ if there exists $α,β,δ,γ\in Ω^\ast$ such that $σ = αβγ$, $τ = αδγ$ and $β\toδ\in P$. We say "$R$ produces $τ$ from $σ$ in one step", "$R$ rewrites $σ$ into $τ$ in one step", or "$β$ is derivable from $α$ in one step".
The relation $\xrightarrow{R}$ is the transitive and reflexive closure of $\xrightarrow{R}_1$, i.e. $σ\xrightarrow{R} τ$ if and only if $σ=τ$ or there exists a sequence $(σ_0, σ_1, \ldots, σ_n)$ such that $σ = σ_0 \xrightarrow{R}_1 σ_1 \xrightarrow{R}_1 \ldots\xrightarrow{R}_1 σ_n = τ$. We say "$R$ produces $τ$ from $σ$", "$R$ rewrites $σ$ into $τ$", or "$τ$ can be derived from $σ$ in $R$". We call such a sequence an "$R$-derivation of $τ$ from $σ$ using $R$ of length $n$".
If $α\in Ω^\times$, write $\mathcal{D}(R,α) = \set{β\in Ω^\times \; | \; α \xrightarrow{R} β}$ for the set of strings that can be derived from $α$.
A (variable-based) grammar $G$ over $Σ$ is a tuple $(Σ, N, P, S)$ where
-
$Σ$ is a non-empty finite set (the alphabet, containing terminal symbols or letters);
-
$N$ is a non-empty finite set (the non-terminal symbols or variables), disjoint from $Σ$;
-
$(Ω, P)$ is a rewrite system, where $Ω = Σ\cup N$,
-
every rewrite rule $α\to β\in P$ has $α\in N^+$;
-
$S\in N$ (the start symbol).
Any subset $L\subseteq Σ^\ast$ is a language over $Σ$.
We say a word $w\in Ω^\ast$ is in sentential form if $w$ can be derived from the start symbol $S$.
A sentence is a word $w\in Σ^\ast$ in sentential form.
The language generated by $G$ is the set of all sentences: $$ \begin{align*} \mathcal{L}(G) = Σ^\ast \cap \mathcal{D}(G, S) = \set{w\in Σ^\ast \; | \; S\xrightarrow{G} w}. \end{align*} $$
Two grammars $G$, $G'$ are said to be equivalent if they produce the same language: $\mathcal{L}(G) = \mathcal{L}(G')$.
Let $G = (Σ, N, P, S)$ be a grammar. A rewrite rule $α\to β$ is of
-
type 0 if $α\in N^+$;
-
type 1 (non-contracting) if $|β| \geq |α|$ and $α\in N^+$;
-
type 2 if $α\in N$ and $|β|\geq 1$;
-
type 3 if it is a terminal rule $A \to a$ or a nonterminal rule $A \to aB$ (for some $A,B\in N$ and $a\in Σ$).
Let $\mathsf{Q}$ be one of these properties.
A grammar is called $\mathsf{Q}$ if all of its rewrite rules are $\mathsf{Q}$.
A language is called $\mathsf{Q}$ if it is produced by a grammar that is $\mathsf{Q}$. This is the Chomsky hierarchy.
Let $G, G'$ be grammars and $w\in \mathbb{W}$ be a word.
Is there an algorithm to determine whether $w\in \mathcal{L}(G)$?
Is there an algorithm to determine whether $\mathcal{L}(G) = \emptyset$?
Is there an algorithm to determine whether $\mathcal{L}(G) = \mathcal{L}(G')$?
Turns out all three general problems are unsolvable.
The word problem for noncontracting grammars is solvable.
A deterministic finite automaton (DFA) is a tuple $D = (Q, Σ, δ, q_0, F)$ where
-
$Q$ is a non-empty finite set (of states);
-
$Σ$ is a non-empty finite set (the alphabet);
-
$δ: Q\times Σ\to Q$ (the transition function);
-
$q_0 \in Q$ (the start state); and
-
$F\subseteq Q$ a subset (of accepting states).
Extend $δ$ to $\hat δ: Q\times{}$$Σ^\ast$${}\to Q$ by $$ \begin{align*} \hat δ(q, ε) = q, \qquad \hat δ(q, wa) = δ(\hat δ(q, w), a). \end{align*} $$
For every word $w=a_0 a_1 \ldots a_n\in \mathbb{W}$, the state sequence of the computation is $$ \begin{alignat}{1} q_0 & &&= \hat δ(q_0, ε), \\ q_1 &= δ(q_0, a_0) &&= \hat δ(q_0, a_0), \\ q_2 &= δ(q_1, a_1) &&= \hat δ(q_0, a_0 a_1), \\ \vdots \\ q_{n+1} &= δ(q_n, a_n) &&= \hat δ(q_0, a_0 a_1 \ldots a_n) = \hat δ(q_0, w). \end{alignat} $$
The language of $D$ is $$ \begin{align*} \mathcal{L}(D) = \sb{w\in Σ^\ast}{\hat δ(q_0, w) \in F}. \end{align*} $$
A language $L\subseteq Σ^\ast$ is regular if it is accepted by a DFA.
We can represent a DFA using a transition diagram
or a transition table
![]()
A nondeterministic finite automaton (NFA) is a tuple $N = (Q, Σ, δ, q_0, F)$ where
-
$Q$ is a non-empty finite set (of states);
-
$Σ$ is a non-empty finite set (the alphabet);
-
$δ: Q\times Σ\to \mathcal{P}(Q)$ (the transition function);
-
$q_0 \in Q$ (the start state); and
-
$F\subseteq Q$ a subset (of accepting states).
Extend $δ$ to $\hat δ: Q\times Σ^\ast\to \mathcal{P}(Q)$ by $$ \begin{align*} \hat δ(q,ε) = \set{q}, \qquad \hat δ(q, wa) = \bigcup \sb{δ(p,a)}{p\in \hat δ(q,w)}. \end{align*} $$
For every word $w=a_0 a_1 \ldots a_n\in Σ^\ast$, the state set sequence is $$ \begin{align*} X_0 &= \set{q_0}, \qquad X_{i+1} = \bigcup \sb{δ(p,a_i)}{p\in X_i}. \end{align*} $$
The language accepted by $N$ is $$ \begin{align*} \mathcal{L}(N) = \sb{w\in Σ^\ast}{\hat δ(q_0, w) \cap F \neq \emptyset}. \end{align*} $$
An $ε$-nondeterministic finite automaton ($ε$-NFA) is a tuple $E = (Q, Σ, δ, q_0, F)$ where
-
$Q$ is a non-empty finite set (of states);
-
$Σ$ is a non-empty finite set (the alphabet);
-
$δ: Q\times (Σ\times \set{ε})\to \mathcal{P}(Q)$ (the transition function);
-
$q_0 \in Q$ (the start state); and
-
$F\subseteq Q$ a subset (of accepting states).
Let the $ε$-closure of a set $S\subseteq Q$ be $$ \begin{align*} \op{ecl}(S) := \bigcup_{n=0}^{\infty} S_n \qquad \text{where }S_0 := S,\; S_{n+1} = S_n \cup \bigcup_{q\in S_{n}} δ(q, ε). \end{align*} $$
Extend $δ$ to $\hat δ: Q \times Σ^\ast\to \mathcal{P}(Q)$ by $$ \begin{align*} \hat δ(q, ε) := \op{ecl}(q), \qquad \hat δ(q, wa) := \op{ecl}\left(\bigcup \sb{δ(p, a)}{p\in \hat δ(q, w)}\right). \end{align*} $$ The language accepted by $E$ is $$ \begin{align*} \mathcal{L}(N) := \sb{w\in Σ^\ast}{\hat δ(q_0, w)\cap F \neq \emptyset}. \end{align*} $$
Let $D = (Σ, Q, δ, q_0, F)$ and $D' = (Σ, Q', δ', q_0', F')$ be deterministic automata over the same alphabet $Σ$.
A map $f: Q \to Q'$ is a homomorphism from $D$ to $D'$ if
-
$f(δ(q, a)) = δ'(f(q), a)$ for all $q\in Q$ and $a\in Σ$;
-
$f(q_0) = q_0'$; and
-
for any $q\in Q,$ we have $q\in F$ iff $f(q) \in F'$.
$f: Q\to Q'$ is an isomorphism if $f$ is a bijective homomorphism.
By induction, we also have $f(\hat δ(q), w) = \hat δ'(f(q), w)$ for any $q\in Q$, $w\in Σ^\ast$.
Let $D$ and $D'$ be deterministic automata.
If $D$ and $D'$ are homomorphic, then $\mathcal{L}(D) = \mathcal{L}(D')$.
Let $L\subseteq Σ^\ast$ be a language. We say $L$ _satisfies the (regular) pumping lemma with pumping number $n$_ if for every word $w\in L$ with length $|w|\geq n$, there exists words $x,y,z\in Σ^\ast$ such that
-
$w = xyz$;
-
$|y|>0$;
-
$|xy|\leq n$;
-
$xy^k z\in L$ for all $k=0,1,2,\ldots$.
For every regular language $L$, there exists some $n$ such that $L$ satisfies the regular pumping lemma with pumping number $n$.
Let $L\subseteq Σ^\ast$ be a language.
The Kleene plus operation is $$ \begin{align*} L^+ = \sb{w\in Σ^\ast}{\exists w_0, \ldots, w_n\in L \text{ such that } w = w_0 \ldots w_n}, \end{align*} $$ i.e. finite concatenation of one or more elements of $L$.
The Kleene star operation is $$ \begin{align*} L^\ast = L^+ \cup \set{ε}. \end{align*} $$
Let $Σ$ be an alphabet.
Among the finite strings over the set of symbols $Σ \sqcup \set{\emptyset, ε, (, ), +, {}^* }$, we define the notion of regular expression by recursion:
-
$\emptyset$ is a regular expression;
-
$ε$ is a regular expression;
-
$(R + S)$ is a regular expression if $R,S$ are regular expressions;
-
$(RS)$ is a regular expression if $R,S$ are regular expressions;
-
$R^\ast$ is a regular expression if $R$ is a regular expression.
We usually omit unnecessary parentheses.
We associate languages to regular expressions by recursion:
-
$\mathcal{L}(\emptyset) = \emptyset$;
-
$\mathcal{L}(ε) = \set{ε}$;
-
$\mathcal{L}(a) = \set{a}$ if $a\in Σ$;
-
$\mathcal{L}(R+S) = \mathcal{L}(R) \cup \mathcal{L}(S)$;
-
$\mathcal{L}(RS) = \mathcal{L}(R) \, \mathcal{L}(S)$;
-
$\mathcal{L}(R^*) = \mathcal{L}(R)$$^*$;
For any regular expression $R$, there is a DFA $D$ such that $\mathcal{L}(R) = \mathcal{L}(D)$, and vice versa.
Let $L$ and $M$ be regular languages over an alphabet $Σ$. The following are also regular:
Let $D = (Σ, Q, δ, q_0, F)$ be a deterministic automaton.
A state $q\in Q$ is accessible if there exists a word $w\in Σ^\ast$ such that $\hat δ(q_0, w) = q$. Otherwise it is inaccessible.
-
Note if $f: Q \to Q'$ is an homomorphism between automata $D$ and $D'$, then $q'\in Q'$ being accessible implies $q' \in \mathrm{range}(f)$.
Let $D = (Σ, Q, δ, q_0, F)$ be a deterministic automaton.
Two states $q, q'\in Q$ are indistinguishable if $\hat δ(q, w)\in F \iff \hat δ(q', w)\in F$ for all words $w\in \mathbb{W}$.
A word $w\in Σ^\ast$ is said to distinguish $q$ and $q'$ if $\hat δ(q,w) \in F$ and $\hat δ(q', w)\not\in F$ or vice versa.
Define an equivalence relation $\sim$ on $Q$ where $q\sim q'$ if $q,q'$ are indistinguishable, and write $[q]$ for the equivalence class of $q$. The quotient automaton is $$ \begin{align*} D/{\sim} = (Σ, Q/{\sim}, [δ], [q_0], [F]) \end{align*} $$ where $[δ]([q], a) = [δ(q,a)]$ and $[F] = \sb{[q]}{q\in F}$.
Note if $f: Q \to Q'$ is an homomorphism between automata $D$ and $D'$, then for any distinguishable $p,q\in Q$, we have $f(p) \neq f(q)$.
The quotient automaton is well-defined. (Derivation)
A deterministic automaton is said to be irreducible if it has neither inaccessible states nor indistinguishable distinct states.
No two distinct states of a quotient automaton are indistinguishable.
For any deterministic automaton $D$, we have $\mathcal{L}(D) = \mathcal{L}($$D/{\sim}$$)$.
Let $f$ be a homomorphism between deterministic automata $D$ and $D'$.
-
If $D$ is irreducible, then $f$ is injective.
-
If $D'$ is irreducible, then $f$ is surjective.
Every deterministic automaton $D$ has a minimal automaton $I$, unique up to isomorphism. That is, there exists an irreducible automaton $I$ with $\mathcal{L}(D) = \mathcal{L}(I)$ and at most as many states as $D$. Moreover, if $I$ and $I'$ are irreducible automata, then $\mathcal{L}(I) = \mathcal{L}(I')$ iff they are isomorphic.
The emptiness problem for regular grammars is solvable.
The equivalence problem for regular grammars is solvable.
A grammar $G = (Σ, N, P, S)$ is said to be in Chomsky normal form if all of its rewrite rules are either of the form $A \to AB$ (for $A,B,C \in N$) or of the form $A \to a$ (for $A\in N$ and $a\in Σ$).
$G$ is context-free.
If $G = (Σ, N, P, S)$ is a grammar in Chomsky normal form and $w\in \mathcal{L}(G)$ with $|w| = n$, then any $G$-derivation of $w$ has length $2n-1$.
Let $G = (Σ, N, P, S)$ be a context-free grammar.
There exists a context-free grammar $\mathring G = (Σ, N, \mathring P, S)$ where $\mathring P$ has no $ε$ production (i.e. rules of the form $A\to ε$) and unit production (i.e. rules of the form $A\to B$) and $\mathcal{L}(\mathring G) = \mathcal{L}(G) \setminus \set{ε}$.
For every context-free grammar without $ε$-production (i.e. rules of the form $A\to ε$), there exists an equivalent context-free variable-targeted grammar, i.e. where all rewrite rules are either $A\to a$ or $A \to B_1 B_2 \ldots B_k$ (where $A,B_1, \ldots, B_k\in N$ and $a\in Σ$).
For any context-free grammar $G$, there exists a grammar $\bar G$ in Chomsky normal form such that $\mathcal{L}(\bar G) = \mathcal{L}(G) \setminus \set{ε}$.
A subset $T\subseteq \mathbb{N}$$^\ast$ is said to be a finitely branching tree if
-
$T$ is closed under initial segments, i.e. if $t\in T$ and $s\subseteq t$, then $s\in T$; and
-
for each $t\in T$, there exists some $n\in \N$ such that $tk\in T$ iff $k<n$.
-
We say $t$ has $n$ successors or is $n$-branching.
-
An element $t\in T$ is a leaf or terminal node if it has no successors.
-
$ε$ is contained in every non-empty tree, and is called the root.
-
The $k$-th level of $T$ is all elements of length $k$.
-
The height of $T$ is the maximal $k\in \N$ such that $T$ has an element on the $k$-th level. This exists if $T$ is a finite tree.
-
For $t\in T$ on the $k$-th level, the corresponding branch through $T$ is the sequence $\sb{t|_m}{m\leq k}$, which is a sequence of nodes of $T$ of length $k+1$.
Define the left-to-right order $<$ as $$ \begin{align*} s<t\iff s\neq t \text{ and if $k$ is least such that $s(k)\neq t(k)$, then $s(k) < t(k)$}. \end{align*} $$
-
This defines a partial order on $T$.
-
$<$ is a total order on the set of nodes on the same level.
-
$<$ is a total order on the set of leaves of $T$.
Let $G = (Σ, V, P, S)$ be a context-free grammar, and $A\in V$ be a variable.
A pair $\mathbf{T} = (T,l)$ is a $G$-parse tree starting from $A$ if $T$ is a finite finitely branching tree and $l: T \to Ω$ satisfying
-
$l(ε) = A$;
-
if $l(t) \in Σ$, then $t$ is a leaf in $T$; and
-
if $l(t) \in V$ and $t$ is $n+1$-branching ($n\in \N$, i.e. $t$ is not a leaf), then there is a rule $l(t) \to x_0 x_1 \ldots x_n \in P$ such that $l(tk) = x_k$ for all $k=0,1,\ldots,n$.
Write $σ_\mathbf{T} = l(t_0) \ldots l(t_m)$ for the string parsed by $\mathbf{T}$, where $l_0 < t_1 < \ldots < t_m$ are the leaves of $\mathbf{T}$ totally-ordered by the left-to-right order.
For $t\in T$ not a leaf, define the subtree at $t$ by $\mathbf{T}_t = (T_t, l_t)$ where $T_t = \sb{s\in \N^\ast}{ts\in T}$ and $l_t(s) = l(ts)$.
-
$\mathbf{T}_t$ is a $G$-parse tree starting from $l(t).$
-
Under the left-to-right ordering, the leaves of $\mathbf{T}_t$ are a consecutive sequence of the leaves of $\mathbf{T}$. Therefore $σ_\mathbf{T} = u σ_{\mathbf{T}_t} v$, where $u\in \mathbb{W}$ corresponds to all leaves of $\mathbf{T}$ to the left of leaves of $\mathbf{T}_t$, and $w\in \mathbb{W}$ corresponds to all leaves of $\mathbf{T}$ to the right of leaves of $\mathbf{T}_t$.
If $\mathbf{T}$ and $\mathbf{T}'$ are $G$-parse trees, $t\in T$ with $l(t) = A$ and $\mathbf{T}'$ starts from $A$, then we can graft $\mathbf{T}'$ into $\mathbf{T}$: define $\mathrm{graft}(\mathbf{T}, t, \mathbf{T}') = (T^\star, l^\star)$ where $T^\star = \sb{s\in T}{t\not\subseteq s} \cup \sb{ts}{s\in T'}$ and $$ \begin{align*} l^\star(s) = \begin{cases} l(s), &\text{if } t\not\subseteq s, \\ l'(u), &\text{if } s = tu \text{ for some $u\in T'$}. \end{cases} \end{align*} $$
-
Grafting corresponds to removing the subword $σ_{\mathbf{T}_t}$ from $σ_{\mathbf{T}}$ and replacing it with $σ_{\mathbf{T}'}$.
Let $G = (Σ, V, P, S)$ be a context-free grammar.
$w\in \mathcal{L}(G)$ iff there exists a $G$-parse tree $\mathbf{T}$ starting from $S$ such that $w = σ_\v T$.
If $G$ is a grammar in Chomsky normal form, $\mathbf{T}$ is a $G$-parse tree of height $h+1$, and $σ_\mathbf{T} = w\in Σ^\ast$, then $|w|\leq 2^h$.
For any context-free language $L\subseteq Σ^\ast$, there exists $n\in \N$ such that for all $u\in L$, if $|u|\geq n$, then there are subwords $v,w,x,y,z\in Σ^\ast$ such that
-
$u = vwxyz$;
-
$w$ and $y$ are not both $ε$;
-
$|wxy|\leq n$;
-
$vw^k xy^k z\in L$ for all $k=0,1,2,\ldots$.
Write $f: X \dashrightarrow Y$ for a partial function $f$ from $X$ to $Y$, i.e. a (total) function with domain $\op{dom}(f)\subseteq X$ and range $\mathrm{ran}(f)\subseteq Y$.
Write $f(x)\downarrow$ ("$f$ converges on input $x$") when $x\in \op{dom}(f)$ and $f(x)\uparrow$ ("$f$ diverges on input $x$") otherwise.
A register machine is a pair $M = (Q, P)$ where
-
$Q = \set{S_0, S_1, \ldots, S_n}$ is a finite set (of states) with two special elements $S_0 \neq S_1$ (the start state and the halt state); and
-
$P$ is a function with domain $Q$ (the program), such that each $P(S_i)$ is a $(Σ, Q)$-instruction, i.e. tuple of one of the following forms: ($j\in \Z_{\geq 1}$, $k, l\in \N$)
Instruction |
Interpretation |
|---|---|
$S_i: (j, +, k)$ |
Add 1 to the content of register $k$; |
$S_i: (j, -, k, l)$ |
If register $j$ is $0$: |
Let $M = (Q, P)$ be a register machine.
-
The upper register index $m=\op{upper}(P)$ of $M$ is the maximal register number that appears in any of the (finite) instructions in the range of $P$.
-
Given an input $(n_1, \ldots, n_k)\in \N^k$, initialise the content of the registers $R_1,\ldots, R_k$ to be $(n_1, \ldots, n_k)$. Start at state $S_1$ and keep following the instructions. Stop if we reach $S_0$.
-
Say $M$ halts on input $(n1, \ldots, nk)\in \N^k$ we reach $S_0$ (at some finite time). We write $P(n_1, \ldots, n_k)\downarrow$. If it does not halt, we write $P(n_1, \ldots, n_k)\uparrow$.
For a register machine $M$ and some $k\in \N$, define a partial function $f_{M,k}: \N^k \dashrightarrow \N$ by
-
$f_{M,k}(\v w)\uparrow$ if $M$ does not halt on input $\v w$; and
-
$f_{M,k}(\v w)=n$ if $M$ halts on input $\v w$ with register content $n$ in $R_0$ at the time of halting.
Write $\mathrm{W}_M = \op{dom}(f_{M,1})$. Two machines $M,M'$ are called weakly equivalent if $\mathrm{W}_M = \mathrm{W}_{M'}$.
A partial function $f: \N^k \dashrightarrow \N$ is called partial computable if there exists a register machine such that $f_{M,k} = f$.
The basic functions are
-
projection functions $π_{k,i}: \N^k \to \N: (n_1, \ldots, n_k)\mapsto n_i$;
-
the zero function $\N \to \N: n\mapsto 0$; and
-
the successor function $\N\to \N: n\mapsto n+1$.
Given $f: \mathbb{N}^k\dashrightarrow \N$ and $g_1, \ldots, g_k: \N^l \dashrightarrow \N$, the composition of $f$ with $(g_1, \ldots, g_k)$ is $$ \begin{align*} \N^l &\dashrightarrow \N \\ \v n &\mapsto f(g_1(\v n), \ldots, g_k(\v n)). \end{align*} $$
Given $f: \N^k \dashrightarrow \N$ and $g: \N^{k+2}\dashrightarrow \N$, the recursion result of $f$ and $g$ is $$ \begin{align*} h: \N^{k+1} &\dashrightarrow \N \\ (\v n, 0) &\mapsto f(\v n), \\ (\v n, \;n_{k+1}+ 1) &\mapsto g(\v n, \; n_{k+1}, \; h(\v n, n_{k+1})). \end{align*} $$
Given $f: \N^{k+1} \dashrightarrow \N$, the minimisation result of $f$ is $$ \begin{align*} \N^k &\dashrightarrow \N \\ \v n &\mapsto \begin{cases} m, &\text{minimal $m$ such that $f(\v n, m) = 0$}, \\ \uparrow, &\text{if $f(\v n, m) > 0$ for all $m$}. \end{cases} \end{align*} $$
The class of primitive recursive functions is the smallest class of partial functions containing all basic functions that is closed under composition and recursion.
The class of (partial) recursive functions is the smallest class of partial functions containing all basic functions that is closed under composition, recursion and minimisation.
Since addition, multiplication etc are recursive, they are computable. Thus we have access to arithmetics.
Then we encode $k$-tuples using (writing $p_i$ for the $i$-th prime) $$ \begin{align*} \N^k &\to \N \\ (n_1, \ldots, n_k) &\mapsto 2^{n_1} 3^{n_2} 5^{n_3} \ldots p_k^{n_k}. \end{align*} $$ Decode using $n \mapsto (n)_i$ the highest power of $p_i$ in $n$.
Alternatively, consider the Cantor zigzag bijection $$ \begin{align*} z: (i,j) \mapsto \frac{(i+j)(i+j+1)}{2} + i. \end{align*} $$ Its inverse is computable and lists $\N^2$ in shortlex ordering.
Every partial recursive function is partial computable, and vice versa.
The shortlex ordering on $\N^m$ is the total order defined by $(n_1, \ldots, n_m) < (n_1', \ldots, n_{m}')$ iff either
-
$\sum_{i=1}^{m} n_i < \sum_{i=1}^{m} n_i'$, or
-
$\sum_{i=1}^{m} n_i = \sum_{i=1}^{m} n_i'$ and there exists $j$ such that $n_1 = n_1', \ldots, n_{j-1} = n_{j-1}'$ and $n_j < n_j'$.
We devise a coding: (write $\op{code}(k)$ for the encoding of $k$)
-
Instruction: $\op{code}((j, +, k)) = 2^j 5^k$ and $\op{code}((j, -, k, l) ) = 2^j35^k7^l$.
-
Program: $\op{code}((i_1, \ldots, i_n)) = 2^n 3^{\op{code}(i_1)} 5^{\op{code}(i_2)} \ldots p_{n+1}^{i_n}$.
Write $f_{n,k}: \N^k \dashrightarrow \N$ for the partial function computed by machine encoded by $n$. If $n$ does not code a program, define $\op{dom}(f_{n,k}) = \emptyset$.
The software principle: there is a universal register machine $U$ such that $$ \begin{align*} f_{U,3}(n, k, m) = f_{n,k}((m)_1, \ldots, (m)_k). \end{align*} $$ (Proof)
We can then extend notation: for arbitrary $v\in \mathbb{W}$, define $f_{v,k}(\v w) = f_{U,2}(v,\op{code}(\v w))$.
Let $m,k\in \Z_{\geq 1}$, and write $\N^{m+k} = \N^{m} \times \N^{k}$.
A function $h: \N^{m+k}\dashrightarrow \N$ is partial computable iff there exists a total computable function $τ: \N^{m}\to \N$ such that $$ \begin{align*} h(\v a, \v x) = f_{τ(\v a), k}(\v x) \end{align*} $$ where $f_{n,k}$ is defined here.
Let $X\subseteq \N^k$.
The characteristic function of $X$ is $$ \begin{align*} χ_X(\v n) = \begin{cases} 1, &\v n\in X, \\ 0, &\v n\not\in X. \end{cases} \end{align*} $$ $X$ is called computable if $χ_X$ is computable.
The pseudo-characteristic function of $X$ is $$ \begin{align*} ψ_X(\v n) = \begin{cases} 1, &\v n\in X, \\ \uparrow, &\v n\not\in X. \end{cases} \end{align*} $$ $X$ is called computably enumerable if $ψ_X$ is computable.
If $X$ is computable, then so is $\N^k \setminus X$.
If $X$ is computable, then $X$ is computably enumerable. (Proof)
If $X\subseteq \N$, the following are equivalent:
The halting set and its two-variable variant are the sets $$ \begin{align*} \mathbf{K} &= \sb{w\in \N}{f_{w,1}(w)\downarrow}, \\ \mathbf{K}_0 &= \sb{(w,v)\in \N^2}{f_{w,1}(v)\downarrow}, \end{align*} $$ where $f_{w,1}$ is defined here.
The sets $\mathbf{K}$ and $\mathbf{K}_0$ are computably enumerable but not computable.
Let $g: \N\to \N$ be any total computable function.
There exists some $n_0\in \N$ such that $$ \begin{align*} f_{n_0, 1} = f_{g(n_0), 1}. \end{align*} $$ ($f_{n,k}$)
Let $A, B\subseteq \N$ be subsets.
A total computable function $f: \N\to \N$ is called a reduction of $A$ to $B$ if for all $w\in \N$, $$ \begin{align*} w\in A \iff f(w) \in B. \end{align*} $$
We say $A$ is many-one reducible to $B$ (denoted as $A\leq_m B$) if there is a reduction from $A$ to $B$.
We say $A$ is many-one equivalent to $B$ (denoted as $A\equiv_m B$) if $A\leq_m B$ and $B\leq_m A$.
$\leq_m$ is a partial order, i.e. it is reflexive ($A\leq_m A$ via $f=\id_\N$) and transitive (if $A_1 \leq_m A_2$ and $A_2 \leq_m A_3$, then $A_1 \leq_m A_3$ by concatenation of reductions).
$\equiv_m$ is an equivalence relation.
Note $A\leq_m B\iff \N\setminus A \leq_m \N\setminus B$. Thus $A$ and $\N\setminus A$ is either many-one equivalent or incomparable by $\leq_m$.
Let $L,L'\subseteq \mathbb{W}$. Suppose $L$${}\leq_m{}$$L'$.
-
If $L'$ is computable, then so is $L$.
-
If $L'$ is computably enumerable, then so is $L$.
Interpret $L\leq_m L'$ as "$L$ is at most as complicated as $L'$".
$\mathbf{K}$ and $\mathbb{W}\setminus \mathbf{K}$ are incomparable in $\leq_m$: otherwise $\mathbb{W}\setminus \mathbf{K}\equiv_m \mathbf{K}$ and in particular $\mathbb{W}\setminus \mathbf{K}\leq_m \mathbf{K}$, contradicting with $\mathbf{K}$ being $Σ_1$ but not $Π_1$.
Let $\mathcal{C}\subseteq \mathcal{P}(\N)$ be a collection of subsets and $A\subseteq \N$ be a subset.
Say $A$ is $\mathcal{C}$-hard if $X$${}\leq_m{}$$A$ for all $X\in \mathcal{C}$.
Say $A$ is $\mathcal{C}$-complete if $A$ is $\mathcal{C}$-hard and $A\in \mathcal{C}$.
A set $X\subseteq \N^k$ is called $Σ_n$ if there exists a computable set $Y\subseteq \N^{k+n}$ such that for all $\v x\in \N^k$, $$ \begin{align*} \v x\in X \iff \exists y_1 \forall y_2 \exists y_3 \forall y_4 \ldots m_n: (\v x, y_1, \ldots, y_m)\in Y. \end{align*} $$
$X$ is called $Π_n$ if $\mathbb{N}^k \setminus X$ is $Σ_n$, i.e. there exists a computable set $Y\subseteq \N^{k+n}$ such that $$ \begin{align*} \v x\in X \iff \forall y_1 \exists y_2 \forall y_3 \exists y_4 \ldots m_n: (\v x, y_1, \ldots, y_m)\in Y. \end{align*} $$
$X$ is called $Δ_1$ if $X$ is both $Σ_1$ and $Π_1$.
A set is computably enumerable iff it is $Σ_1$.
A set is computable iff it is $Δ_1$.
If $L\subseteq \mathbb{W}$ is a computable language, and $L\neq \emptyset$ and $L\neq \mathbb{W}$, then $L$ is $Δ_1$-complete.
The halting problem $\mathbf{K}$ is $Σ_1$-complete.
A set $I\subseteq \N$ is called an index set if it is closed under weak equivalence, i.e. if $w\in I, v\in \N$ and $\mathrm{W}_w = \mathrm{W}_v$, then $v\in I$.
-
Any non-empty index set is infinite by the padding lemma: adding arbitrarily many unreachable instructions results in a distinct program but same partial function.
If an index set $I$ is not trivial ($I\neq \emptyset$ and $I\neq \N$), then $I$ is not computable.
In particular, let $e\in \N$ such that $\mathrm{W}_e = \emptyset$.
-
If $e\in I$, then $\N\setminus{}$$\mathbf{K}$${}\leq_m I$;
-
If $e\not\in I$, then $\mathbf{K}\leq_m I$.
Consider an encoding of grammars: for every $n\in \N$, there is a grammar $G_n$, and all grammars are of this form.
Then we can recast the decision problems:
-
the word problem: $\sb{(u,v)}{u\in \mathcal{L}(G_v)}$;
-
the emptiness problem: $\sb{u}{\mathcal{L}(G_u) = \emptyset}$;
-
the equivalence problem: $\sb{(u,v)}{\mathcal{L}(G_u) = \mathcal{L}(G_v)}$,
and we say a decision problem is solvable if the set is computable.
It can be shown that there are total computable functions that translate between codes of grammars and register machines and vice versa. They then witness the many-one equivalences $$ \begin{alignat}{1} \mathbf{K}_0 &= \sb{(w,v)}{w\in \mathrm{W}_v} &&\equiv_m \sb{(u,v)}{u\in \mathcal{L}(G_v)}, \\ \mathbf{Emp} &:= \sb{u}{\mathrm{W}_u = \emptyset} &&\equiv_m \sb{u}{\mathcal{L}(G_u) = \emptyset}, \\ \mathbf{Fin} &:= \sb{u}{\mathrm{W}_u \text{ is finite}} &&\equiv_m \sb{u}{\mathcal{L}(G_u) \text{ is finite}}, \\ \mathbf{Inf} &:= \sb{u}{\mathrm{W}_u \text{ is infinite}} &&\equiv_m \sb{u}{\mathcal{L}(G_u) \text{ is infinite}}, \\ \mathbf{Cof} &:= \sb{u}{\N \setminus \mathrm{W}_u \text{ is finite}} &&\equiv_m \sb{u}{Σ^\ast \setminus \mathcal{L}(G_u) \text{ is finite}}, \\ \mathbf{Tot} &:= \sb{u}{\mathrm{W}_u = \N} &&\equiv_m \sb{u}{\mathcal{L}(G_u) = Σ^\ast}, \\ \end{alignat} $$
$\mathbf{Emp}, \mathbf{Fin}, \mathbf{Inf}, \mathbf{Tot}$ are non-trivial index sets. By Rice's theorem, we obtain $\mathbb{N}\setminus \mathbf{K} \leq_m \mathbf{Emp}, \mathbf{Fin}$ and $\mathbf{K}\leq_m \mathbf{Inf}, \mathbf{Tot}$. This shows the emptiness problem for type 0 languages is not solvable.
$\mathbf{Fin}$ is neither $Σ_1$ nor $Π_1$.
Consequently $\mathbf{Inf}$ $= \N\setminus \mathbf{Fin}$ is neither $Σ_1$ nor $Π_1$.
By describing using the total computable set $T = \sb{(m,x,n)}{\text{program }m\text{ with input }x\text{ terminates in }n\text{ steps}}$, we see
-
$\mathbf{Emp} = \sb{m}{\forall x \forall n: (m,x,n)\not\in T}$ is $Π_1$;
-
$\mathbf{Fin} = \sb{m}{\exists X\forall x\forall n: x\leq X \text{ or } (m,x,n)\not\in T}$ is $Σ_2$ so $\mathbf{Inf}$ is $Π_2$;
-
$\mathbf{Cof} = \sb{m}{\exists X\forall x\exists n: x\leq X \text{ or } (m,x,n)\in T}$ is $Σ_3$;
-
$\mathbf{Tot} = \sb{m}{\forall x\exists n: (m,x,n)\in T}$ is $Π_2$.
or a transition table