Anforderungen  |   Konzepte  |   Entwurf  |   Entwicklung  |   Qualitätssicherung  |   Lebenszyklus  |   Steuerung
 
 
 
 


Quelle  AB.thy

  Sprache: Isabelle
 

(*<*)theory AB imports Main begin(*>*)

sectionCase Study: A Context Free Grammar

text\label{sec:CFG}
 \index{grammars!defining inductively|(}%
 Grammars are nothing but shorthands for inductive definitions of nonterminals
 which represent sets of strings. For example, the production
 $A \to B c$ is short for
 \[ w \in B \Longrightarrow wc \in A \]
 This section demonstrates this idea with an example
 due to Hopcroft and Ullman, a grammar for generating all words with an
 equal number of $a$'s and~$b$'s:
 \begin{eqnarray}
 S &\to& \epsilon \mid b A \mid a B \nonumber\\
 A &\to& a S \mid b A A \nonumber\\
 B &\to& b S \mid a B B \nonumber
 \end{eqnarray}
 At the end we say a few words about the relationship between
 the original proof 🍋p.\ts81 in HopcroftUllman a

We start by fixing the alphabet, which consists only of 🍋a's
and~🍋b's:


datatype alfa = a | b

text\noindent
 For convenience we include the following easy lemmas as simplification rules:
 

lemma [simp]: "(x a) = (x = b) (x b) = (x = a)"
by (case_tac x, auto)

text\noindent
 Words over this alphabet are of type 🍋alfa list,
the three nonterminals are declared as sets of such words.
The productions above are recast as a \emph{mutual} inductive
definition\index{inductive definition!simultaneous}
of 🍋S🍋A and~🍋B:


inductive_set
  S :: "alfa list set" and
  A :: "alfa list set" and
  B :: "alfa list set"
where
  "[] S"
"w A ==> b#w S"
"w B ==> a#w S"

"w S ==> a#w A"
"[ vA; wA ] ==> b#v@w A"

"w S ==> b#w B"
"[ v B; w B ] ==> a#v@w B"

text\noindent
 First we show that all words in 🍋S c
induction, so is the proof: we show at the same time that all words in
🍋A contain one more 🍋a than 🍋b and all words in 🍋B contain one more 🍋b than ??a.


lemma correctness:
  "(w S size[xw. x=a] = size[xw. x=b])
   (w A size[xw. x=a] = size[xw. x=b] + 1)
   (w B size[xw. x=b] = size[xw. x=a] + 1)"

txt\noindent
 These propositions are expressed with the help of the predefined 🍋filter f
x]
, the list of all elements 🍋x in 🍋xs such that 🍋P x
holds. Remember that on lists size and length are synonymous.

The proof itself is by rule induction and afterwards automatic:


by (rule S_A_B.induct, auto)

text\noindent
 This may seem surprising at first, and is indeed an indication of the power
 of inductive definitions. But it is also quite straightforward. For example,
 consider the production $A \to b A A$: if $v,w \in A$ and the elements of $A$
 contain one more $a$ than~$b$'s, then $bvw$ must again contain one more $a$
 than~$b$'s.
 
 As usual, the correctness of syntactic descriptions is easy, but completeness
 is hard: does 🍋S c
🍋a's and 🍋b's? It turns out that this proof requires the
following lemma: every string with two more 🍋a's than 🍋b's can be cut somewhere such that each half has one more 🍋a than
🍋b. This is best seen by imagining counting the difference between the
number of 🍋a's and 🍋b's starting at the left end of the
word. We start with 0 and end (at the right endwith 2. Since each move to the
right increases or decreases the difference by 1, we must have passed through
1 on our way from 0 to 2. Formally, we appeal to the following discrete
intermediate value theorem @{thm[source]nat0_intermed_int_val}
@{thm[display,margin=60]nat0_intermed_int_val[no_vars]}
where 🍋f is of type 🍋nat ==> int🍋int are the integers,
. is the absolute value function\footnote{See
Table~\ref{tab:ascii} in the Appendix for the correct \textsc{ascii}
syntax.}, and 🍋1::int is the integer 1 (see \S\ref{sec:numbers}).

First we show that our specific function, the difference between the
numbers of 🍋a's and 🍋b's, does indeed only change by 1 in every
move to the right. At this point we also start generalizing from 🍋a's
and 🍋b's to an arbitrary property 🍋POtherwise we would have
to prove the desired lemma twice, once as stated above and once with the
roles of 🍋a's and 🍋b's interchanged.


lemma step1: "i < size w.
  (int(size[xtake (i+1) w. P x])-int(size[xtake (i+1) w. ¬P x]))
   - (int(size[xtake i w. P x])-int(size[xtake i w. ¬P x])) 1"

txt\noindent
 The lemma is a bit hard to read because of the coercion function
 int :: nat ==> int.
a natural number, but subtraction on type~🍋nat will do the wrong thing.
Function 🍋take is predefined and 🍋take i xs is the prefix of
length 🍋i of 🍋xs; below we also need 🍋drop i xs, which
is what remains after that prefix has been dropped from 🍋xs.

The proof is by induction on 🍋wwith a trivial base caseand a not
so trivial induction step. Since it is essentially just arithmetic, we do not
discuss it.


apply(induct_tac w)
apply(auto simp add: abs_if take_Cons split: nat.split)
done

text
 Finally we come to the above-mentioned lemma about cutting in half a word with two more elements of one sort than of the other sort:
 

lemma part1:
 "size[xw. P x] = size[xw. ¬P x]+2 ==>
  isize w. size[xtake i w. P x] = size[xtake i w. ¬P x]+1"

txt\noindent
 This is proved by force w
instantiated appropriately and with its first premise disposed of by lemma
@{thm[source]step1}:


apply(insert nat0_intermed_int_val[OF step1, of "P" "w" "1"])
by force

text\noindent
 
 Lemma @{thm[source]part1} tells us only about the prefix 🍋take i w.
An easy lemma deals with the suffix 🍋drop i w:



lemma part2:
  "[size[xtake i w @ drop i w. P x] =
    size[xtake i w @ drop i w. ¬P x]+2;
    size[xtake i w. P x] = size[xtake i w. ¬P x]+1]
   ==> size[xdrop i w. P x] = size[xdrop i w. ¬P x]+1"
by(simp del: append_take_drop_id)

text\noindent
 In the proof we have disabled the normally useful lemma
 \begin{isabelle}
 @{thm append_take_drop_id[no_vars]}
 \rulename{append_take_drop_id}
 \end{isabelle}
 to allow the simplifier to apply the following lemma instead:
 @{text[display]"[xxs@ys. P x] = [xxs. P x] @ [xys. P x]"}
 
 To dispose of trivial cases automatically, the rules of the inductive
 definition are declared simplification rules:
 

declare S_A_B.intros[simp]

text\noindent
 This could have been done earlier but was not necessary so far.
 
 The completeness theorem tells us that if a word has the same number of
 🍋a's
for 🍋A and 🍋B:


theorem completeness:
  "(size[xw. x=a] = size[xw. x=b] w S)
   (size[xw. x=a] = size[xw. x=b] + 1 w A)
   (size[xw. x=b] = size[xw. x=a] + 1 w B)"

txt\noindent
 The proof is by induction on 🍋w.
because, as we can see from the grammar, we need to make bigger steps than
merely appending a single letter at the front. Hence we induct on the length
of 🍋wusing the induction rule @{thm[source]length_induct}:


apply(induct_tac w rule: length_induct)
apply(rename_tac w)

txt\noindent
 The rule p
rule to useFor details see \S\ref{sec:complete-ind} below.
In this case the result is that we may assume the lemma already
holds for all words shorter than 🍋w. Because the induction step renames
the induction variable we rename it back to w.

The proof continues with a case distinction on 🍋w,
on whether 🍋w is empty or not.


apply(case_tac w)
 apply(simp_all)
(*<*)apply(rename_tac x v)(*>*)

txt\noindent
 Simplification disposes of the base case and leaves only a conjunction
 of two step cases to be proved:
 if 🍋w = a#v a
🍋b#v Aand similarly for 🍋w = b#v.
We only consider the first case in detail.

After breaking the conjunction up into two cases, we can apply
@{thm[source]part1} to the assumption that 🍋w contains two more 🍋a's than 🍋b's.


apply(rule conjI)
 apply(clarify)
 apply(frule part1[of "λx. x=a", simplified])
 apply(clarify)
txt\noindent
 This yields an index 🍋i length v s
@{prop[display]"length [xtake i v . x = a] = length [xtake i v . x = b] + 1"}
With the help of @{thm[source]part2} it follows that
@{prop[display]"length [xdrop i v . x = a] = length [xdrop i v . x = b] + 1"}


 apply(drule part2[of "λx. x=a", simplified])
  apply(assumption)

txt\noindent
 Now it is time to decompose 🍋v i
into 🍋take i v @ drop i v,


 apply(rule_tac n1=i and t=v in subst[OF append_take_drop_id])

txt\noindent
 (the variables 🍋n1 a
theorems @{thm[source]subst} and @{thm[source]append_take_drop_id})
after which the appropriate rule of the grammar reduces the goal
to the two subgoals 🍋take i v A and 🍋drop i v A:


 apply(rule S_A_B.intros)

txt
 Both subgoals follow from the induction hypothesis because both 🍋take i
 v


  apply(force simp add: min_less_iff_disj)
 apply(force split: nat_diff_split)

txt
 The case 🍋w = b#v i


apply(clarify)
apply(frule part1[of "λx. x=b", simplified])
apply(clarify)
apply(drule part2[of "λx. x=b", simplified])
 apply(assumption)
apply(rule_tac n1=i and t=v in subst[OF append_take_drop_id])
apply(rule S_A_B.intros)
 apply(force simp add: min_less_iff_disj)
by(force simp add: min_less_iff_disj split: nat_diff_split)

text
 We conclude this section with a comparison of our proof with
 Hopcroft\index{Hopcroft, J. E.} and Ullman's\index{Ullman, J. D.}
 🍋p.\ts81 in
For a start, the textbook
grammar, for no good reason, excludes the empty word, thus complicating
matters just a little bit: they have 8 instead of our 7 productions.

More importantly, the proof itself is different: rather than
separating the two directions, they perform one induction on the
length of a word. This deprives them of the beauty of rule induction,
and in the easy direction (correctness) their reasoning is more
detailed than our autoFor the hard part (completeness), they
consider just one of the cases that our simp_all disposes of
automatically. Then they conclude the proof by saying about the
remaining cases: ``We do this in a manner similar to our method of
proof for part (1); this part is left to the reader''. But this is
precisely the part that requires the intermediate value theorem and
thus is not at all similar to the other cases (which are automatic in
Isabelle). The authors are at least cavalier about this point and may
even have overlooked the slight difficulty lurking in the omitted
cases.  Such errors are found in many pen-and-paper proofs when they
are scrutinized formally.%
\index{grammars!defining inductively|)}


(*<*)end(*>*)

Messung V0.5 in Prozent
C=44 H=42 G=42

¤ Dauer der Verarbeitung: 0.16 Sekunden  (vorverarbeitet am  2026-05-03) ¤

*© Formatika GbR, Deutschland






Wurzel

Suchen

Beweissystem der NASA

Beweissystem Isabelle

NIST Cobol Testsuite

Cephes Mathematical Library

Wiener Entwicklungsmethode

Haftungshinweis

Die Informationen auf dieser Webseite wurden nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit, noch Qualität der bereit gestellten Informationen zugesichert.

Bemerkung:

Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.






                                                                                                                                                                                                                                                                                                                                                                                                     


Neuigkeiten

     Aktuelles
     Motto des Tages

Software

     Produkte
     Quellcodebibliothek

Aktivitäten

     Artikel über Sicherheit
     Anleitung zur Aktivierung von SSL

Muße

     Gedichte
     Musik
     Bilder

Jenseits des Üblichen ....

Besucherstatistik

Besucherstatistik

Monitoring

Montastic status badge