• No results found

In this section we present a formalisation of Chomsky Normal Form, assuming the grammar has already gone through the previously described simplifications.

Theorem 2.5.1 (H&U Theorem 4.5) [Chomsky Normal Form] Any CFL withoutǫcan be generated by a grammar in which all productions are of the formA BC orA a. HereA, B, Care variables andais a terminal.

Broadly, the algorithm is divided into two stages. In the first stage, all rules in the grammar are converted so that they are of the form A → B1. . . Bn orA → a. This is theweak Chomsky Normal Form(Rozenberg and Salomaa [66]).

In the second stage, they are transformed to be of the form A → BC. Algorithm 1 shows the steps in the transformation. This process is the same as described in Hopcroft and Ullman. The two ‘for’ loops represent the two stages respectively. What we need to prove is that the grammarg′, result of this process, is in Chomsky Normal Form. The way we proceed in HOL is as follows. First, we implement the two stages in HOL. The Algorithm 1 shows the corresponding transformation relations in HOL. The boxes show the steps that form part of these two relations. The for-loops are replicated by taking the reflexive, transitive closure (RTC) of the relations.

input : Grammargwhereǫ /L(g)

output: Grammarg′ such thatL(g) =L(g)andgis in CNF

begin

transform all rules to be of the formAaorAX1X2. . . Xm, whereXi is a nonterminal

whileAX1X2. . . Xm ∈rules g do

ifm2then

forisTerminalXi do

trans1Tmnl

introduce new variableCi introduce ruleCi→Xi replaceXi byCi

transform all rules to be of the formA aorAX1X2, nonterminalsX1, X2

whileAX1X2. . . Xm ∈rules g do

trans2NT ifm≥3then

introduce new variableDi introduce ruleDi→X2X3 replaceX2X3byDi

Algorithm 1:Algorithm for transforming a grammar into Chomsky Normal Form. The next step is to ensure the transformations affect the grammar in the correct way. This is established by proving properties at different stages of the transformations. These are summarised in Algorithm 2.

Proof Let g1 = (V, T, P, S) be a context-free grammar. We can assumeP contains no useless symbols, unit productions orǫ-productions using the above simplifications. If a production has a single symbol on the right-hand side, that symbol must be a terminal. Thus, that production is already in an acceptable form. The remaining productions in

g1are converted into CNF in two steps.

The first step is called trans1Tmnl, wherein a terminal occurring on the right side of a production gets replaced by a nonterminal in the following manner. We replace the productions of the form l pts (p or s is nonempty and t is a terminal) with

productionsA→tandl→pAs.

HOL Definition 2.5.1 (trans1Tmnl)

trans1Tmnl nt t g g′ ⇐⇒ ∃ℓ r p s. rule ℓ r rules g r = p ++ [t] ++ s (p 6= [] s 6= []) isTmnlSym t NTS nt / nonTerminals g rules g′ =

delete (rule ℓ r) (rules g) ++

[rule nt [t]; rule ℓ (p ++ [NTS nt] ++ s)] startSym g′ = startSym g

(Functiondeleteremoves an element from a list. The;is used to separate elements in a list.)

We prove that multiple applications of the above transformation preserve the language.

HOL Theorem 2.5.1

(λx y. nt t. trans1Tmnl nt t x y)∗ g g L g = L g

We want to obtain a grammar g2 = (V′, T, P′, S) which only contains productions of the form A → a, where a is a terminal symbol or A → A1...An where Ai is a nonterminal symbol. We prove that such a g2 can be obtained by repeated applications of trans1Tmnl. The multiple applications are denoted by taking the reflexive transitive closure oftrans1Tmnl.

Idiosyncrasies of formalisation Algorithm 2 shows the two steps and the properties that need to be established at each stage.

The process for transformation of a grammar into Chomsky Normal Form requires introducing new variables in to the grammar. In a text proof one would just express it by simply stating ‘pick a symbol not in the grammar’. Mechanisation is too rigorous for one to be able to use such a statement straight away. Instead, we have to show that the nonterminals in a grammar are finite and if the universal set for the type is infinitethenwe can pick a nonterminal that does not belong in the grammar. The first condition (Property 0 in Algorithm 2) follows from the fact the number of rules in a grammar are finite. Since the nonterminals are derived from the rules, the number of nonterminals must be finite as well. The second condition needs to be asserted as a premise for any theorems asserting that the transformation relation holds between two grammars (e.g.HOL Theorem 2.5.2).

We have chosen to model the transformations as relations rather than functions in HOL. Step 1 in Algorithm 2 corresponds to the first for-loop in Algorithm 1. Since relations

are partial in HOL (compared to functions which are total), we need to show that zero or more applications oftrans1Tmnlwill result in a grammar satisfying the properties 1.1 and 1.2. Hence the termination assertion in Step 1.

In addition, since we are interested in the grammar preserving certain properties, we also have to show that such multiple applications preserve the language (Property 1.1) and all the rules are of the formAB1. . . BnandA→a(Property 1.2).

We establish similarly properties for Step 2. Step 2 corresponds to second for-loop over the rules of a grammar. At the end of the two transformations and if the stated properties are preserved then we have established that grammarg can be transformed intog′ such thatgis in CNF and the language forgandgis the same.

Another set of tedious proofs corresponds to showing that the transformation for CNF do not affect the original assumptions we made about useless symbols, unit and ǫ- productions.

input : Grammargwhereǫ /L(g)

output: Grammarg′ such thatL(g) =L(g)andgis in CNF

transform all rules to be of the formA→aorA→X1X2. . . Xm, whereXi is a nonterminal

Property 0 FINITE (nonTerminals g)

Step 1g2. (λx y. ∃nt t. trans1Tmnl nt t x y)∗ g g2 Property 1.1 L g = L g2

Property 1.2 badTmnlsCount g2 = 0

transform all rules to be of the formAaorAX1X2, for nonterminalsX1, X2

Step 2g3. (λx y. ∃nt t. trans2NT nt nt1 nt2 x y)∗ g2 g3 Property 2.1 L g2 = L g3

Property 2.2 badTmnlsCount g3 = 0 Property 2.3 badNtmsCount g3 = 0 Property 2.4 isCnf g3

Algorithm 2: Steps for transforming a grammar into CNF in HOL and the key properties to be established at each stage.

Coming back to the proof, we now want to show termination corresponding to Step 1. For this, we define the constant badTmnlsCount, which counts the terminals in the RHSs of all productions which have more than one terminal symbol present in their RHS.

HOL Definition 2.5.2 (badTmnlsCount)

(SUMadds the count over all the productions,MAP f ℓappliesf to each element in.) The auxiliaryruleTmnlsis characterised:

HOL Definition 2.5.3 (ruleTmnls)

ruleTmnls (rule ℓ r) =

if |r| ≤ 1 then 0 else |FILTER isTmnlSym r| (Notation|r|represents the size of r.)

Each application of the process should decrease the number ofruleTmnlsunless there are none to change (i.e.the grammar is already in the desired form). By induction on badTmnlsCountwe prove that by a finite number of applications oftrans1Tmnlwe can get grammarg2. This follows from the fact that the set of symbols in a grammar are finite. HOL Theorem 2.5.2 INFINITE U(:’nts) ∃g′. (λx y. ∃nt t. trans1Tmnl nt t x y)∗ g g badTmnlsCount g′ = 0

(Note the use of the assumption INFINITE U(:’nts). HereU(:’nts) represents the universal set for the type of nonterminals (nts) in the grammar g and g′. As mentioned before, the transformation process involves introducing a new nonterminal symbol. To be able to pick a fresh symbol, the set of possible nonterminals has to be infinite.)

The above process gives us a simplified grammar g2 such that badTmnlsCount g2 = 0. By HOL Theorem 2.5.1 we have that L(g1) = L(g2). We now apply another transformation on g2 which gives us our final CNF. The final transformation is calledtrans2NTand works by replacing two adjacent nonterminals in the right-hand side of a rule by a single nonterminal symbol. Repeated application ong2give us a grammar where all productions conform to the CNF criteria.

HOL Definition 2.5.4 (trans2NT)

trans2NT nt nt1 nt2 g g′ ⇐⇒ ∃ℓ r p s. rule ℓ r rules g r = p ++ [nt1; nt2] ++ s ∧ (p 6= [] s 6= []) isNonTmnlSym nt1 ∧ isNonTmnlSym nt2 ∧ NTS nt / nonTerminals g rules g′ =

delete (rule ℓ r) (rules g) ++

[rule nt [nt1; nt2]; rule ℓ (p ++ [NTS nt] ++ s)] ∧

We prove that the language remains the same after zero or more such transformations (Property 2.1 in Algorithm 2).

We follow a similar strategy as with trans1Tmnl to show that applications of trans2NTwill result in grammar (g3) where all rules with nonterminals on the RHS have exactly two nonterminals,i.e. rules are of the formA A1A2 (Property 2.3 in Algorithm 2).

To wrap up the proof we show two results. First, that applications of trans1Tmnl followed by applications oftrans2NT, leaves the language of the grammar untouched, i.e. L(g1) = L(g3). Second, that the transformation trans2NT does not introduce productions to change badTmnlsCount (Property 2.2 in Algorithm 2). We can then apply the two transformations to obtain our grammar in CNF (Property 2.4 in Algorithm 2) where all rules are of the form A → a or A → A1A2. This is in HOL asserted by theisCnfpredicate.

HOL Definition 2.5.5 (isCnf)

isCnf g ⇐⇒ ∀ℓ r.

rule ℓ r rules g

|r| = 2 EVERY isNonTmnlSym r ∨ |r| = 1 isWord r

(PredicateisNonTmnlSymis true of a symbol if it is of the formNTS s for some string

s.EVERYchecks that every element of a list satisfies the given predicate.) The HOL theorem corresponding to Theorem 2.5.1 is:

HOL Theorem 2.5.3

INFINITE U(:’nts) [] / L g ⇒ ∃g′. isCnf g L g = L g