diff -r 6e2479089226 -r cecd7a941885 ProgTutorial/Package/Ind_Prelims.thy --- a/ProgTutorial/Package/Ind_Prelims.thy Tue May 14 16:59:53 2019 +0200 +++ b/ProgTutorial/Package/Ind_Prelims.thy Tue May 14 17:10:47 2019 +0200 @@ -2,9 +2,9 @@ imports Ind_Intro begin -section{* Preliminaries *} +section\Preliminaries\ -text {* +text \ The user will just give a specification of inductive predicate(s) and expects from the package to produce a convenient reasoning infrastructure. This infrastructure needs to be derived from the definition @@ -18,7 +18,7 @@ the ML-implementation we will develop in later sections. - We first consider the transitive closure of a relation @{text R}. The + We first consider the transitive closure of a relation \R\. The ``pencil-and-paper'' specification for the transitive closure is: \begin{center}\small @@ -29,18 +29,17 @@ As mentioned before, the package has to make an appropriate definition for @{term "trcl"}. Since an inductively defined predicate is the least predicate closed under a collection of introduction rules, the predicate - @{text "trcl R x y"} can be defined so that it holds if and only if @{text - "P x y"} holds for every predicate @{text P} closed under the rules + \trcl R x y\ can be defined so that it holds if and only if \P x y\ holds for every predicate \P\ closed under the rules above. This gives rise to the definition -*} +\ definition "trcl \ \R x y. \P. (\x. P x x) \ (\x y z. R x y \ P y z \ P x z) \ P x y" -text {* - Note we have to use the object implication @{text "\"} and object - quantification @{text "\"} for stating this definition (there is no other +text \ + Note we have to use the object implication \\\ and object + quantification \\\ for stating this definition (there is no other way for definitions in HOL). However, the introduction rules and induction principles associated with the transitive closure should use the meta-connectives, since they simplify the reasoning for the user. @@ -49,11 +48,11 @@ With this definition, the proof of the induction principle for @{term trcl} is almost immediate. It suffices to convert all the meta-level connectives in the lemma to object-level connectives using the - proof method @{text atomize} (Line 4 below), expand the definition of @{term trcl} + proof method \atomize\ (Line 4 below), expand the definition of @{term trcl} (Line 5 and 6), eliminate the universal quantifier contained in it (Line~7), - and then solve the goal by @{text assumption} (Line 8). + and then solve the goal by \assumption\ (Line 8). -*} +\ lemma %linenos trcl_induct: assumes "trcl R x y" @@ -65,10 +64,10 @@ apply(assumption) done -text {* +text \ The proofs for the introduction rules are slightly more complicated. For the first one, we need to prove the following lemma: -*} +\ lemma %linenos trcl_base: shows "trcl R x x" @@ -78,45 +77,45 @@ apply(assumption) done -text {* +text \ We again unfold first the definition and apply introduction rules - for @{text "\"} and @{text "\"} as often as possible (Lines 3 and 4). + for \\\ and \\\ as often as possible (Lines 3 and 4). We then end up in the goal state: -*} +\ (*<*)lemma "trcl R x x" apply (unfold trcl_def) apply (rule allI impI)+(*>*) -txt {* @{subgoals [display]} *} +txt \@{subgoals [display]}\ (*<*)oops(*>*) -text {* +text \ The two assumptions come from the definition of @{term trcl} and correspond to the introduction rules. Thus, all we have to do is to eliminate the universal quantifier in front of the first assumption (Line 5), and then - solve the goal by @{text assumption} (Line 6). -*} + solve the goal by \assumption\ (Line 6). +\ -text {* +text \ Next we have to show that the second introduction rule also follows from the definition. Since this rule has premises, the proof is a bit more involved. After unfolding the definitions and applying the introduction - rules for @{text "\"} and @{text "\"} -*} + rules for \\\ and \\\ +\ lemma trcl_step: shows "R x y \ trcl R y z \ trcl R x z" apply (unfold trcl_def) apply (rule allI impI)+ -txt {* +txt \ we obtain the goal state @{subgoals [display]} To see better where we are, let us explicitly name the assumptions by starting a subproof. -*} +\ proof - case (goal1 P) @@ -127,42 +126,40 @@ have r2: "\x y z. R x y \ P y z \ P x z" by fact show "P x z" -txt {* - The assumptions @{text "p1"} and @{text "p2"} correspond to the premises of - the second introduction rule (unfolded); the assumptions @{text "r1"} and @{text "r2"} - come from the definition of @{term trcl}. We apply @{text "r2"} to the goal +txt \ + The assumptions \p1\ and \p2\ correspond to the premises of + the second introduction rule (unfolded); the assumptions \r1\ and \r2\ + come from the definition of @{term trcl}. We apply \r2\ to the goal @{term "P x z"}. In order for this assumption to be applicable as a rule, we have to eliminate the universal quantifier and turn the object-level - implications into meta-level ones. This can be accomplished using the @{text - rule_format} attribute. So we continue the proof with: + implications into meta-level ones. This can be accomplished using the \rule_format\ attribute. So we continue the proof with: -*} +\ apply (rule r2[rule_format]) -txt {* +txt \ This gives us two new subgoals @{subgoals [display]} - which can be solved using assumptions @{text p1} and @{text p2}. The latter + which can be solved using assumptions \p1\ and \p2\. The latter involves a quantifier and implications that have to be eliminated before it can be applied. To avoid potential problems with higher-order unification, - we explicitly instantiate the quantifier to @{text "P"} and also match - explicitly the implications with @{text "r1"} and @{text "r2"}. This gives + we explicitly instantiate the quantifier to \P\ and also match + explicitly the implications with \r1\ and \r2\. This gives the proof: -*} +\ apply(rule p1) apply(rule p2[THEN spec[where x=P], THEN mp, THEN mp, OF r1, OF r2]) done qed -text {* +text \ Now we are done. It might be surprising that we are not using the automatic - tactics available in Isabelle/HOL for proving this lemmas. After all @{text - "blast"} would easily dispense of it. -*} + tactics available in Isabelle/HOL for proving this lemmas. After all \blast\ would easily dispense of it. +\ lemma trcl_step_blast: shows "R x y \ trcl R y z \ trcl R x z" @@ -170,9 +167,9 @@ apply(blast) done term "even" -text {* +text \ Experience has shown that it is generally a bad idea to rely heavily on - @{text blast}, @{text auto} and the like in automated proofs. The reason is + \blast\, \auto\ and the like in automated proofs. The reason is that you do not have precise control over them (the user can, for example, declare new intro- or simplification rules that can throw automatic tactics off course) and also it is very hard to debug proofs involving automatic @@ -181,7 +178,7 @@ The method of defining inductive predicates by impredicative quantification also generalises to mutually inductive predicates. The next example defines - the predicates @{text even} and @{text odd} given by + the predicates \even\ and \odd\ given by \begin{center}\small @{prop[mode=Axiom] "even (0::nat)"} \hspace{5mm} @@ -191,8 +188,8 @@ Since the predicates @{term even} and @{term odd} are mutually inductive, each corresponding definition must quantify over both predicates (we name them - below @{text "P"} and @{text "Q"}). -*} + below \P\ and \Q\). +\ hide_const even hide_const odd @@ -205,10 +202,10 @@ \n. \P Q. P 0 \ (\m. Q m \ P (Suc m)) \ (\m. P m \ Q (Suc m)) \ Q n" -text {* +text \ For proving the induction principles, we use exactly the same technique as in the transitive closure example, namely: -*} +\ lemma even_induct: assumes "even n" @@ -221,14 +218,14 @@ apply(assumption) done -text {* - The only difference with the proof @{text "trcl_induct"} is that we have to +text \ + The only difference with the proof \trcl_induct\ is that we have to instantiate here two universal quantifiers. We omit the other induction principle that has @{prop "even n"} as premise and @{term "Q n"} as conclusion. The proofs of the introduction rules are also very similar to the ones in - the @{text "trcl"}-example. We only show the proof of the second introduction + the \trcl\-example. We only show the proof of the second introduction rule. -*} +\ lemma %linenos evenS: shows "odd m \ even (Suc m)" @@ -248,18 +245,18 @@ done qed -text {* +text \ The interesting lines are 7 to 15. Again, the assumptions fall into two categories: - @{text p1} corresponds to the premise of the introduction rule; @{text "r1"} - to @{text "r3"} come from the definition of @{text "even"}. - In Line 13, we apply the assumption @{text "r2"} (since we prove the second - introduction rule). In Lines 14 and 15 we apply assumption @{text "p1"} (if + \p1\ corresponds to the premise of the introduction rule; \r1\ + to \r3\ come from the definition of \even\. + In Line 13, we apply the assumption \r2\ (since we prove the second + introduction rule). In Lines 14 and 15 we apply assumption \p1\ (if the second introduction rule had more premises we have to do that for all of them). In order for this assumption to be applicable, the quantifiers need to be instantiated and then also the implications need to be resolved with the other rules. - Next we define the accessible part of a relation @{text R} given by + Next we define the accessible part of a relation \R\ given by the single rule: \begin{center}\small @@ -268,16 +265,16 @@ The interesting point of this definition is that it contains a quantification that ranges only over the premise and the premise has also a precondition. - The definition of @{text "accpart"} is: -*} + The definition of \accpart\ is: +\ definition "accpart \ \R x. \P. (\x. (\y. R y x \ P y) \ P x) \ P x" -text {* +text \ The proof of the induction principle is again straightforward and omitted. Proving the introduction rule is a little more complicated, because the quantifier and the implication in the premise. The proof is as follows. -*} +\ lemma %linenos accpartI: shows "(\y. R y x \ accpart R y) \ accpart R x" @@ -299,13 +296,11 @@ qed qed -text {* +text \ As you can see, there are now two subproofs. The assumptions fall as usual into - two categories (Lines 7 to 9). In Line 11, applying the assumption @{text - "r1"} generates a goal state with the new local assumption @{term "R y x"}, - named @{text "r1_prem"} in the second subproof (Line 14). This local assumption is - used to solve the goal @{term "P y"} with the help of assumption @{text - "p1"}. + two categories (Lines 7 to 9). In Line 11, applying the assumption \r1\ generates a goal state with the new local assumption @{term "R y x"}, + named \r1_prem\ in the second subproof (Line 14). This local assumption is + used to solve the goal @{term "P y"} with the help of assumption \p1\. \begin{exercise} @@ -328,5 +323,5 @@ them. This is usually the first step in writing a package. We next explain the parsing and typing part of the package. -*} +\ (*<*)end(*>*)