Grammar(unrestricted grammar)
- Context-free grammar: A→wA\to wA→w
- Grammar: uAv→wuAv\to wuAv→w
Definition
- A grammar is a 4-tuple G=(V,Σ,S,R)G=(V,\Sigma,S,R)G=(V,Σ,S,R)
- VVV is an alphabet
- Σ⊆V\Sigma\subseteq VΣ⊆V is the set of terminals
- V−ΣV-\SigmaV−Σ is the set of non-terminals
- S∈V−ΣS\in V-\SigmaS∈V−Σ is the start symbol
- RRR, the set of rules, is a finite subset of (V∗(V−Σ)V∗)×V∗(V^*(V-\Sigma)V^*)\times V^*(V∗(V−Σ)V∗)×V∗
- (u,v)∈R,u→v(u,v)\in R,u\to v(u,v)∈R,u→v
- derives in one step, u⇒Gvu\xRightarrow[G]{}vuGv
- derives ,u⇒G∗vu\xRightarrow[G]{}^*vuG∗v
- GGG generates w∈Σ∗w\in\Sigma^*w∈Σ∗ if S⇒GwS\xRightarrow[G]{}wSGw
- L(G)={w∈Σ∗L(G)=\{w\in\Sigma^*L(G)={w∈Σ∗: S⇒Gw∗}S\xRightarrow[G]{}w^*\}SGw∗}, GGG generates L(G)L(G)L(G)
Example
- The following grammar generates {anbncn:n≥1}\{a^nb^nc^n:n\ge1\}{anbncn:n≥1}
- S→ABCSS\to ABCSS→ABCS
- BA→ABBA\to ABBA→AB, CA→ACCA\to ACCA→AC, CB→BCCB\to BCCB→BC
- S→TcS\to T_cS→Tc, CTc→TccCT_c\to T_ccCTc→Tcc
- BTc→BTbBT_c\to BT_bBTc→BTb, BTb→TbbBT_b\to T_bbBTb→Tbb
- ATb→ATaAT_b\to AT_aATb→ATa, ATa→TaaAT_a\to T_aaATa→Taa
- Ta→eT_a\to eTa→e
Theorem
- A language is generated by a grammar iff it is semidecided by a TM
- G→MG\to MG→M
- M=M=M= on input www
- layer 000 is {S}\{S\}{S}
- for i=1,2,3,…i=1,2,3,\dotsi=1,2,3,…
- construct layer iii from layer i−1i-1i−1
- if www in layer iii, halts
- M=M=M= on input www
- M→GM\to GM→G
- WLOG, assume that
- K∩Σ=∅K\cap\Sigma=\varnothingK∩Σ=∅
- H={h}H=\{h\}H={h}, and if MMM halts, it is always in configuration (h,⊳⊔‾)(h,\rhd\underline{\sqcup})(h,⊳⊔)
- G=(V,Σ′,S,R)G=(V,\Sigma',S,R)G=(V,Σ′,S,R)
- V=K∪Σ∪{S,⊲}V=K\cup\Sigma\cup\{S,\lhd\}V=K∪Σ∪{S,⊲}
- Σ′=Σ−{⊳,⊔}\Sigma'=\Sigma-\{\rhd,\sqcup\}Σ′=Σ−{⊳,⊔}
- R=R=R=
- S→⊳⊔h⊲S\to\rhd\sqcup h\lhdS→⊳⊔h⊲
- ⊳⊔S→e\rhd\sqcup S\to e⊳⊔S→e, ⊲→e\lhd\to e⊲→e
- writing: if MMM has δ(q,a)=(p,b)\delta(q,a)=(p,b)δ(q,a)=(p,b), GGG has bp→aqbp\to aqbp→aq
- moving right: if MMM has δ(q,a)=(p,→)\delta(q,a)=(p,\rightarrow)δ(q,a)=(p,→), GGG has abp→aqbabp\to aqbabp→aqb
- moving left: if MMM has δ(q,a)=(p,←)\delta(q,a)=(p,\leftarrow)δ(q,a)=(p,←), GGG has pa→aqpa\to aqpa→aq
- WLOG, assume that
Undecidable problems about grammar
- G1 Problem
- AG={A_G=\{AG={“GGG”“www”: GGG is a grammar that generates w}w\}w}
- f(f(f(“MMM”“www”)=)=)=“GGG”“www”
- H⪯AGH\preceq A_GH⪯AG
- AG={A_G=\{AG={“GGG”“www”: GGG is a grammar that generates w}w\}w}
Rice’s Theorem
- Suppose L\mathcal{L}L is a proper, non-empty subset of all R.E. languages, the following is undecidable: given a grammar GGG, is L(G)∈LL(G)\in\mathcal{L}L(G)∈L?
Theorem
- Language: NDCFG={ND_\mathrm{CFG}=\{NDCFG={"G1G_1G1"“G2G_2G2”: G1G_1G1 and G2G_2G2 are two CFGs with L(G1)∩L(G2)≠∅}L(G_1)\cap L(G_2)\ne\varnothing\}L(G1)∩L(G2)=∅}
- Reduction by computation history
Numerical function
- We say a TM MMM computes a numerical function f:Nk→Nf:\mathbb{N}^k\to\mathbb{N}f:Nk→N if for any n1,n2,…,nk∈Nn_1,n_2,\dots,n_k\in\mathbb{N}n1,n2,…,nk∈N, M(bin(n1),bin(n2),…,bin(nk))=bin(f(n1,n2,…,nk))M(\mathrm{bin}(n_1),\mathrm{bin}(n_2),\dots,\mathrm{bin}(n_k))=\mathrm{bin}(f(n_1,n_2,\dots,n_k))M(bin(n1),bin(n2),…,bin(nk))=bin(f(n1,n2,…,nk))
Basic functions
- for any k≥0k\ge0k≥0, the kkk-ary zero function is zero(n1,…,nk)=0\mathrm{zero}(n_1,\dots,n_k)=0zero(n1,…,nk)=0
- for any k,j≥0k,j\ge0k,j≥0, the jjj-th kkk-ary identity function is idk,j(n1,…,nk)=nj\mathrm{id}_{k,j}(n_1,\dots,n_k)=n_jidk,j(n1,…,nk)=nj
- for any n∈Nn\in\mathbb{N}n∈N, the successor function succ(n)=n+1\mathrm{succ}(n)=n+1succ(n)=n+1
Operations
- Composition: given g:Nk→Ng:\mathbb{N}^k\to\mathbb{N}g:Nk→N and h1,h2,…,hk:Nt→Nh_1,h_2,\dots,h_k:\mathbb{N}^t\to\mathbb{N}h1,h2,…,hk:Nt→N, the composition of ggg with h1,h2,…,hkh_1,h_2,\dots,h_kh1,h2,…,hk is the ttt-ary function f(n1,n2,…,nt)=g(h1(n1,n2,…,nt),h2(n1,n2,…,nt),…,hk(n1,n2,…,nt))f(n_1,n_2,\dots,n_t)=g(h_1(n_1,n_2,\dots,n_t),h_2(n_1,n_2,\dots,n_t),\dots,h_k(n_1,n_2,\dots,n_t))f(n1,n2,…,nt)=g(h1(n1,n2,…,nt),h2(n1,n2,…,nt),…,hk(n1,n2,…,nt))
- Recursive definition: given g:Nk→Ng:\mathbb{N}^k\to\mathbb{N}g:Nk→N and h:Nk+2→Nh:\mathbb{N}^{k+2}\to\mathbb{N}h:Nk+2→N, the function defined recursively by ggg and hhh is the (k+1)(k+1)(k+1)-ary function f(n1,n2,…,nk,0)=g(n1,n2,…,nk),f(n1,n2,…,nk,m+1)=h(n1,n2,…,nk,m,f(n1,n2,…,nk,m))f(n_1,n_2,\dots,n_k,0)=g(n_1,n_2,\dots,n_k),f(n_1,n_2,\dots,n_k,m+1)=h(n_1,n_2,\dots,n_k,m,f(n_1,n_2,\dots,n_k,m))f(n1,n2,…,nk,0)=g(n1,n2,…,nk),f(n1,n2,…,nk,m+1)=h(n1,n2,…,nk,m,f(n1,n2,…,nk,m))
- Given f:Nk→Nf:\mathbb{N}^k\to\mathbb{N}f:Nk→N, there exists g:Nt+k→Ng:\mathbb{N}^{t+k}\to\mathbb{N}g:Nt+k→N such that g(m1,…,mt,n1,…,nk)=f(n1,…,nk)g(m_1,\dots,m_t,n_1,\dots,n_k)=f(n_1,\dots,n_k)g(m1,…,mt,n1,…,nk)=f(n1,…,nk)
Example: plus(m,n)=m+n\mathrm{plus}(m,n)=m+nplus(m,n)=m+n
- plus(m,0)=id2,1(m,0)=m\mathrm{plus}(m,0)=\mathrm{id}_{2,1}(m,0)=mplus(m,0)=id2,1(m,0)=m
- plus(m,n+1)=succ(id3,3(m,n,plus(m,n)))\mathrm{plus}(m,n+1)=\mathrm{succ}(\mathrm{id}_{3,3}(m,n,\mathrm{plus}(m,n)))plus(m,n+1)=succ(id3,3(m,n,plus(m,n)))
Definition
- The primitive recursive functions are all the basic functions and all the functions obtained from the basic function by composition and recursive definition
- Collary
- Compositions of primitive functions are primitive recursive
- Functions defined recursively by primitive recursive functions are primitive recursive
Example
- plus2(n)=n+2\mathrm{plus_2}(n)=n+2plus2(n)=n+2
- plus(m,n)=m+n\mathrm{plus}(m,n)=m+nplus(m,n)=m+n
- mul(m,n)=m⋅n\mathrm{mul}(m,n)=m\cdot nmul(m,n)=m⋅n
- exp(m,n)=mn\exp(m,n)=m^nexp(m,n)=mn
- Constant function: f(n1,…,nk)=cf(n_1,\dots,n_k)=cf(n1,…,nk)=c
- Sign function: sgn(0)=0,sgn(n+1)=1\mathrm{sgn}(0)=0,\mathrm{sgn}(n+1)=1sgn(0)=0,sgn(n+1)=1
- Predessor function: pred(0)=0,pred(n)=n−1\mathrm{pred}(0)=0,\mathrm{pred}(n)=n-1pred(0)=0,pred(n)=n−1
- pred(0)=0\mathrm{pred}(0)=0pred(0)=0
- pred(n+1)=succ(id2,1(n,pred(n)))\mathrm{pred}(n+1)=\mathrm{succ}(\mathrm{id}_{2,1}(n,\mathrm{pred}(n)))pred(n+1)=succ(id2,1(n,pred(n)))
- Non-negative substraction: m∼n=max{m−n,0}m\sim n=\max\{m-n,0\}m∼n=max{m−n,0}
Definition
- A primitive recursive function is a primitive recursive predicate if it only takes values of 000 and 111
Example
- Is-zero function: iszero(0)=1,iszero(n+1)=0\mathrm{iszero}(0)=1,\mathrm{iszero}(n+1)=0iszero(0)=1,iszero(n+1)=0
- Is-positive function: ispos(n)=sgn(n)\mathrm{ispos}(n)=\mathrm{sgn}(n)ispos(n)=sgn(n)
- Great-or-equal function: geq(m,n)=iszero(n∼m)\mathrm{geq}(m,n)=\mathrm{iszero}(n\sim m)geq(m,n)=iszero(n∼m)
Theorem
- The negation of a primitive recursive predicate is primitive recursive
- ¬p(m)=1∼p(m)\neg p(m)=1\sim p(m)¬p(m)=1∼p(m)
- The disjunction and conjunction of primitive recursive predicates are primitive recursive
- p(m)∨q(m)=1∼iszero(p(m)+q(m))p(m)\lor q(m)=1\sim\mathrm{iszero}(p(m)+q(m))p(m)∨q(m)=1∼iszero(p(m)+q(m))
- p(m)∧q(m)=1∼iszero(p(m)⋅q(m))p(m)\land q(m)=1\sim\mathrm{iszero}(p(m)\cdot q(m))p(m)∧q(m)=1∼iszero(p(m)⋅q(m))
- Let g,h:Nk→Ng,h:\mathbb{N}^k\to\mathbb{N}g,h:Nk→N be primitive recursive functions and p:Nk→Np:\mathbb{N}^k\to\mathbb{N}p:Nk→N be a primitive recursive predicate, the following function is primitive recursive
- f(n1,…,nk)={g(n1,…,nk),if p(n1,…,nk)h(n1,…,nk),otherwisef(n_1,\dots,n_k)=\begin{cases}g(n_1,\dots,n_k),\mathrm{if} \ p(n_1,\dots,n_k) \\ h(n_1,\dots,n_k),\mathrm{otherwise}\end{cases}f(n1,…,nk)={g(n1,…,nk),if p(n1,…,nk)h(n1,…,nk),otherwise
- f=p⋅g+(1∼p)⋅hf=p\cdot g+(1\sim p)\cdot hf=p⋅g+(1∼p)⋅h
Example
- Equal function: eq(m,n)=geq(m,n)∧geq(n,m)\mathrm{eq}(m,n)=\mathrm{geq}(m,n)\land\mathrm{geq}(n,m)eq(m,n)=geq(m,n)∧geq(n,m)
- Remainder function: rem(m,n)\mathrm{rem}(m,n)rem(m,n) is the remainder of mmm divided by nnn
- rem(0,n)=0\mathrm{rem}(0,n)=0rem(0,n)=0
- rem(m+1,n)={0,if eq(rem(m,n),pred(n))rem(m,n)+1,otherwise\mathrm{rem}(m+1,n)=\begin{cases}0,\mathrm{if} \ \mathrm{eq}(\mathrm{rem}(m,n),\mathrm{pred}(n)) \\ \mathrm{rem}(m,n)+1,\mathrm{otherwise}\end{cases}rem(m+1,n)={0,if eq(rem(m,n),pred(n))rem(m,n)+1,otherwise
- Division function: div(m,n)\mathrm{div}(m,n)div(m,n) is the quotient of mmm divided by nnn
- div(0,n)=0\mathrm{div}(0,n)=0div(0,n)=0
- div(m+1,n)={div(m,n)+1,if eq(rem(m,n),pred(n))div(m,n),otherwise\mathrm{div}(m+1,n)=\begin{cases}\mathrm{div}(m,n)+1,\mathrm{if} \ \mathrm{eq}(\mathrm{rem}(m,n),\mathrm{pred}(n)) \\ \mathrm{div}(m,n),\mathrm{otherwise}\end{cases}div(m+1,n)={div(m,n)+1,if eq(rem(m,n),pred(n))div(m,n),otherwise
- Digit function: digit(m,n,p)\mathrm{digit}(m,n,p)digit(m,n,p) is the mmm-th digit of the base-ppp representation of nnn
- digit(m,n,p)=div(rem(n,pm),pm−1)\mathrm{digit}(m,n,p)=\mathrm{div}(\mathrm{rem}(n,p^m),p^{m-1})digit(m,n,p)=div(rem(n,pm),pm−1)
- If f(m,n)f(m,n)f(m,n) is primitive recursive, so is sumf(m,n)=∑k=0nf(m,k)\mathrm{sum}_f(m,n)=\sum_{k=0}^n{f(m,k)}sumf(m,n)=∑k=0nf(m,k)
- sumf(m,0)=f(m,0)\mathrm{sum}_f(m,0)=f(m,0)sumf(m,0)=f(m,0)
- sumf(m,n+1)=sumf(m,n)+f(m,n+1)\mathrm{sum}_f(m,n+1)=\mathrm{sum}_f(m,n)+f(m,n+1)sumf(m,n+1)=sumf(m,n)+f(m,n+1)
Theorem
- If a function is primitive recursive, it is computable
Theorem
- There exists a function that is computable but not primitive recursive
- Claim: The set of all unary primitive recursive functions can be lexicographically enumerated by a TM
- M=M=M=
- enumerate all strings in Σ∗\Sigma^*Σ∗ in lexico order
- every time a string is enumerated, output it if it is a valid encoding of a unary primitive recursive function
- M=M=M=
- Name all the unary primitive recursive functions as f0,f1,f2,…f_0,f_1,f_2,\dotsf0,f1,f2,…
- Define f∗(n)=fn(n)+1f^*(n)=f_n(n)+1f∗(n)=fn(n)+1, for ∀n∈N\forall n\in\mathbb{N}∀n∈N
- f∗≠fnf^*\ne f_nf∗=fn, therefore f∗f^*f∗ is not primitive recursive
- f∗f^*f∗ is computable
- M=M=M= on input nnn
- enumerat all the unary primitive recursive functions until it output fn(n)f_n(n)fn(n)
- output fn(n)+1f_n(n)+1fn(n)+1
- M=M=M= on input nnn