# Parikh's theorem

Parikh's theorem in theoretical computer science says that if one looks only at the number of occurrences of each terminal symbol in a context-free language, without regard to their order, then the language is indistinguishable from a regular language.[1] It is useful for deciding that strings with a given number of terminals are not accepted by a context-free grammar.[2] It was first proved by Rohit Parikh in 1961[3] and republished in 1966.[4]

## Definitions and formal statement

Let ${\displaystyle \Sigma =\{a_{1},a_{2},\ldots ,a_{k}\}}$ be an alphabet. The Parikh vector of a word is defined as the function ${\textstyle p:\Sigma ^{*}\to \mathbb {N} ^{k}}$, given by[1]

${\displaystyle p(w)=(|w|_{a_{1}},|w|_{a_{2}},\ldots ,|w|_{a_{k}})}$
where ${\displaystyle |w|_{a_{i}}}$ denotes the number of occurrences of the letter ${\displaystyle a_{i}}$ in the word ${\displaystyle w}$.

A subset of ${\displaystyle \mathbb {N} ^{k}}$ is said to be linear if it is of the form

${\displaystyle u_{0}+\mathbb {N} u_{1}+\dots +\mathbb {N} u_{m}=\{u_{0}+t_{1}u_{1}+\dots +t_{m}u_{m}\mid t_{1},\ldots ,t_{m}\in \mathbb {N} \}}$
for some vectors ${\textstyle u_{0},\ldots ,u_{m}}$. A subset of ${\displaystyle \mathbb {N} ^{k}}$ is said to be semi-linear if it is a union of finitely many linear subsets.

Theorem — Let ${\displaystyle L}$ be a context-free language or a regular language, let ${\displaystyle P(L)}$ be the set of Parikh vectors of words in ${\displaystyle L}$, that is, ${\textstyle P(L)=\{p(w)\mid w\in L\}}$. Then ${\displaystyle P(L)}$ is a semi-linear set.

If ${\displaystyle S}$ is any semi-linear set, then there exists a regular language (which a fortiori is context-free) whose Parikh vectors is ${\displaystyle S}$.

In short, the image under ${\displaystyle p}$ of context-free languages and of regular languages is the same, and it is equal to the set of semilinear sets.

Two languages are said to be commutatively equivalent if they have the same set of Parikh vectors. Thus, every context-free language is commutatively equivalent to some regular language.

## Proof

The second part is easy to prove.

Proof

Given semi-linear set ${\displaystyle S}$, to construct a regular language whose set of Parikh vectors is ${\displaystyle S}$.

${\displaystyle S}$ is a union of 0 or more linear sets. Since the empty language is regular, and union of regular languages is regular, it suffices to prove that any linear set is the set of Parikh vectors of a regular language.

Let ${\displaystyle S=\{u_{0}+t_{1}u_{1}+\dots +t_{m}u_{m}\mid t_{1},\ldots ,t_{m}\in \mathbb {N} \}}$, then it is the set of Parikh vectors of ${\displaystyle \{z_{0}\}\cdot (\cup _{i=1}^{m}\{z_{i}\})^{*}}$, where each ${\displaystyle z_{i}}$ has Parikh vector ${\displaystyle u_{i}}$.

The first part is less easy. The following proof is credited to Goldstine.[5]

First we need a small strengthening of the pumping lemma for context-free languages:

Lemma — If ${\displaystyle L}$ is generated by a Chomsky normal form grammar, then ${\displaystyle \exists N\geq 1}$, such that

For any ${\displaystyle k\geq 1}$, and for any ${\displaystyle w\in L}$ with ${\displaystyle |w|\geq N^{k}}$, there exists a way to split ${\displaystyle w}$ into segments ${\displaystyle ux_{1}\cdots x_{k}zy_{k}\cdots y_{1}v}$, and a nonterminal symbol ${\displaystyle A}$, such that

${\displaystyle |x_{i}y_{i}|\geq 1}$ for all ${\displaystyle i}$, and ${\displaystyle |x_{1}\cdots x_{k}zy_{k}\cdots y_{1}|\leq N^{k}}$

${\displaystyle S\Rightarrow ^{*}uAv\quad A\Rightarrow ^{*}z\quad \forall i,A\Rightarrow ^{*}x_{i}Ay_{i}}$

The proof is essentially the same as the standard pumping lemma: use the pigeonhole principle to find ${\displaystyle k}$ copies of some nonterminal symbol ${\displaystyle A}$ in the longest path in the shortest derivation tree.

Now we prove the first part of Parikh's theorem, making use of the above lemma.

Proof

First, construct a Chomsky normal form grammar for ${\displaystyle L}$.

For each finite nonempty subset of nonterminals ${\displaystyle U}$, define ${\displaystyle L_{U}}$ to be the set of sentences in ${\displaystyle L}$ such that there exists a derivation that uses every nonterminal in ${\displaystyle U}$, no more and no less. It is clear that ${\displaystyle L=\cup _{U}L_{U}}$, so it suffices to prove that each ${\displaystyle p(L_{U})}$ is a semilinear set.

Now fix some ${\displaystyle U}$, and let ${\displaystyle k=|U|}$. We construct two finite sets ${\displaystyle F,G}$, such that ${\displaystyle p(L_{U})=p(F\cdot G^{*})}$, which is obviously semilinear.

For notational clarity, write ${\displaystyle \Rightarrow _{U}^{*}}$ to mean "there exists a derivation using no more (but possibly less) than nonterminals in ${\displaystyle U}$. With that, we define ${\displaystyle F,G}$ as follows:

${\displaystyle F=\{w\in L_{U}:|w|
${\displaystyle G=\{xy:1\leq |xy|\leq N^{k}{\text{ and there exists }}A\in U{\text{ such that }}A\Rightarrow _{U}^{*}xAy\}}$

To prove ${\displaystyle p(L_{U})\subset p(F\cdot G^{*})}$, we induct on the length of ${\displaystyle w\in L_{U}}$.

If ${\displaystyle |w|, then ${\displaystyle w\in F}$, so ${\displaystyle p(w)\in p(F\cdot G^{*})}$. Otherwise, by the strengthened pumping lemma, there exists a derivation of ${\displaystyle w}$ using precisely the elements of ${\displaystyle U}$, and is of the form

${\displaystyle S{\underset {d_{0}}{\stackrel {*}{\Rightarrow }}}uAv{\underset {d_{1}}{\stackrel {*}{\Rightarrow }}}ux_{1}Ay_{1}v{\underset {d_{2}}{\stackrel {*}{\Rightarrow }}}\cdots {\underset {d_{k}}{\stackrel {*}{\Rightarrow }}}ux_{1}\cdots x_{k}Ay_{k}\cdots y_{1}v{\underset {d_{k+1}}{\stackrel {*}{\Rightarrow }}}ux_{1}\cdots x_{k}zy_{k}\cdots y_{1}v}$

where ${\displaystyle A\in U}$, ${\displaystyle 1\leq |x_{i}y_{i}|}$, and ${\displaystyle |x_{1}\cdots x_{k}zy_{k}\cdots y_{1}|\leq N^{k}}$.
Since there are only ${\displaystyle k-1}$ elements in ${\displaystyle U\setminus \{A\}}$, but there are ${\displaystyle k}$ sub-derivations ${\displaystyle d_{1},...,d_{k}}$ in the middle, we may delete one sub-derivation ${\displaystyle d_{i}}$, and obtain a shorter ${\displaystyle w'}$ that is still in ${\displaystyle L_{U}}$, with

${\displaystyle p(w)=p(uzv)+p(x_{1}y_{1})+\cdots +p(x_{k}y_{k})=p(w')+p(x_{i}y_{i})}$

By induction, ${\displaystyle p(w')\in p(F\cdot G^{*})}$, and by construction, ${\displaystyle x_{i}y_{i}\in G}$, so ${\displaystyle p(w)\in p(F\cdot G^{*})}$.

To prove ${\displaystyle p(L_{U})\supset p(F\cdot G^{*})}$, consider an element ${\displaystyle w\in F\cdot G^{*}}$. We need to show that ${\displaystyle p(w)\in p(L_{U})}$. We induct on the minimal number of factors from ${\displaystyle G}$ that is needed to identify ${\displaystyle w}$ as an element of ${\displaystyle F\cdot G^{*}}$.

If no factor from ${\displaystyle G}$ is needed, then ${\displaystyle w\in F\subset L_{U}}$.
Otherwise, ${\displaystyle w=w'xy}$ for some ${\displaystyle w'\in F\cdot G^{*}}$ and ${\displaystyle xy\in G}$, where ${\displaystyle w'}$ requires less factors from ${\displaystyle G}$ than ${\displaystyle w}$.
By induction, ${\displaystyle p(w')=p(w'')}$ for some ${\displaystyle w''\in L_{U}}$. By construction of ${\displaystyle G}$, there exists some ${\displaystyle A\in U}$ such that ${\displaystyle A\Rightarrow _{U}^{*}xAy}$.
By construction of ${\displaystyle L_{U}}$, the symbol ${\displaystyle A}$ appears in a derivation of ${\displaystyle w''}$ using exactly all of ${\displaystyle U}$. Then we can interpolate ${\displaystyle A\Rightarrow _{U}^{*}xAy}$ into that derivation to obtain some ${\displaystyle w'''\in L_{U}}$ such that

${\displaystyle p(w''')=p(w'')+p(xy)=p(w')+p(xy)=p(w)}$

## Strengthening for bounded languages

A language ${\displaystyle L}$ is bounded if ${\displaystyle L\subset w_{1}^{*}\ldots w_{k}^{*}}$ for some fixed words ${\displaystyle w_{1},\ldots ,w_{k}}$. Ginsburg and Spanier [6] gave a necessary and sufficient condition, similar to Parikh's theorem, for bounded languages.

Call a linear set stratified, if in its definition for each ${\displaystyle i\geq 1}$ the vector ${\displaystyle u_{i}}$ has the property that it has at most two non-zero coordinates, and for each ${\displaystyle i,j\geq 1}$ if each of the vectors ${\displaystyle u_{i},u_{j}}$ has two non-zero coordinates, ${\displaystyle i_{1} and ${\displaystyle j_{1}, respectively, then their order is not ${\displaystyle i_{1}. A semi-linear set is stratified if it is a union of finitely many stratified linear subsets.

Ginsburg-Spanier — A bounded language ${\displaystyle L}$ is context-free if and only if ${\displaystyle \{(n_{1},\ldots ,n_{k})\mid w_{1}^{n_{1}}\ldots w_{k}^{n_{k}}\in L\}}$ is a stratified semi-linear set.

## Significance

The theorem has multiple interpretations. It shows that a context-free language over a singleton alphabet must be a regular language and that some context-free languages can only have ambiguous grammars[further explanation needed]. Such languages are called inherently ambiguous languages. From a formal grammar perspective, this means that some ambiguous context-free grammars cannot be converted to equivalent unambiguous context-free grammars.

## References

1. ^ a b Kozen, Dexter (1997). Automata and Computability. New York: Springer-Verlag. ISBN 3-540-78105-6.
2. ^ Håkan Lindqvist. "Parikh's theorem" (PDF). Umeå Universitet.
3. ^ Parikh, Rohit (1961). "Language Generating Devices". Quarterly Progress Report, Research Laboratory of Electronics, MIT.
4. ^ Parikh, Rohit (1966). "On Context-Free Languages". Journal of the Association for Computing Machinery. 13 (4): 570–581. doi:10.1145/321356.321364. S2CID 12263468.
5. ^ Goldstine, J. (1977-01-01). "A simplified proof of parikh's theorem". Discrete Mathematics. 19 (3): 235–239. doi:10.1016/0012-365X(77)90103-0. ISSN 0012-365X.
6. ^ Ginsburg, Seymour; Spanier, Edwin H. (1966). "Presburger formulas, and languages". Pacific Journal of Mathematics. 16 (2): 285–296. doi:10.2140/pjm.1966.16.285.