CookBook/Package/Ind_Examples.thy
author Christian Urban <urbanc@in.tum.de>
Fri, 13 Feb 2009 01:05:31 +0000
changeset 113 9b6c9d172378
parent 102 5e309df58557
child 114 13fd0a83d3c3
permissions -rw-r--r--
slightly updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
32
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
     1
theory Ind_Examples
88
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 38
diff changeset
     2
imports Main LaTeXsugar
32
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
     3
begin
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
     4
88
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 38
diff changeset
     5
section{* Examples of Inductive Definitions \label{sec:ind-examples} *}
32
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
     6
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
     7
text {*
113
9b6c9d172378 slightly updated
Christian Urban <urbanc@in.tum.de>
parents: 102
diff changeset
     8
  Let us first give three examples showing how to define inductive predicates
9b6c9d172378 slightly updated
Christian Urban <urbanc@in.tum.de>
parents: 102
diff changeset
     9
  by hand and prove characteristic properties such as introduction rules and
9b6c9d172378 slightly updated
Christian Urban <urbanc@in.tum.de>
parents: 102
diff changeset
    10
  an induction rule. From these examples, we will then figure out a general
9b6c9d172378 slightly updated
Christian Urban <urbanc@in.tum.de>
parents: 102
diff changeset
    11
  method for defining inductive predicates.  The aim in this section is not to
9b6c9d172378 slightly updated
Christian Urban <urbanc@in.tum.de>
parents: 102
diff changeset
    12
  write proofs that are as beautiful as possible, but as close as possible to
9b6c9d172378 slightly updated
Christian Urban <urbanc@in.tum.de>
parents: 102
diff changeset
    13
  the ML-code producing the proofs that we will develop later.
88
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 38
diff changeset
    14
113
9b6c9d172378 slightly updated
Christian Urban <urbanc@in.tum.de>
parents: 102
diff changeset
    15
  As a first example, we consider the transitive closure of a relation @{text
9b6c9d172378 slightly updated
Christian Urban <urbanc@in.tum.de>
parents: 102
diff changeset
    16
  R}. It is an inductive predicate characterized by the two introduction rules
88
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 38
diff changeset
    17
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 38
diff changeset
    18
  \begin{center}
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 38
diff changeset
    19
  @{term[mode=Axiom] "trcl R x x"} \hspace{5mm}
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 38
diff changeset
    20
  @{term[mode=Rule] "R x y \<Longrightarrow> trcl R y z \<Longrightarrow> trcl R x z"}
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 38
diff changeset
    21
  \end{center}
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 38
diff changeset
    22
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 38
diff changeset
    23
  (FIXME first rule should be an ``axiom'')
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 38
diff changeset
    24
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 38
diff changeset
    25
  Note that the @{text trcl} predicate has two different kinds of parameters: the
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 38
diff changeset
    26
  first parameter @{text R} stays \emph{fixed} throughout the definition, whereas
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 38
diff changeset
    27
  the second and third parameter changes in the ``recursive call''.
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 38
diff changeset
    28
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 38
diff changeset
    29
  Since an inductively defined predicate is the least predicate closed under
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 38
diff changeset
    30
  a collection of introduction rules, we define the predicate @{text "trcl R x y"} in
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 38
diff changeset
    31
  such a way that it holds if and only if @{text "P x y"} holds for every predicate
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 38
diff changeset
    32
  @{text P} closed under the rules above. This gives rise to the definition 
32
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
    33
*}
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
    34
88
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 38
diff changeset
    35
definition "trcl R x y \<equiv> 
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 38
diff changeset
    36
      \<forall>P. (\<forall>x. P x x) \<longrightarrow> (\<forall>x y z. R x y \<longrightarrow> P y z \<longrightarrow> P x z) \<longrightarrow> P x y"
32
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
    37
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
    38
text {*
113
9b6c9d172378 slightly updated
Christian Urban <urbanc@in.tum.de>
parents: 102
diff changeset
    39
  where we quantify over the predicate @{text P}. Note that we have to use the
9b6c9d172378 slightly updated
Christian Urban <urbanc@in.tum.de>
parents: 102
diff changeset
    40
  object implication @{text "\<longrightarrow>"} and object quantification @{text "\<forall>"} for stating
9b6c9d172378 slightly updated
Christian Urban <urbanc@in.tum.de>
parents: 102
diff changeset
    41
  this definition. 
32
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
    42
113
9b6c9d172378 slightly updated
Christian Urban <urbanc@in.tum.de>
parents: 102
diff changeset
    43
  With this definition of the transitive closure, the proof of the induction
88
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 38
diff changeset
    44
  theorem is almost immediate. It suffices to convert all the meta-level connectives
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 38
diff changeset
    45
  in the induction rule to object-level connectives using the @{text atomize} proof
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 38
diff changeset
    46
  method, expand the definition of @{text trcl}, eliminate the universal quantifier
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 38
diff changeset
    47
  contained in it, and then solve the goal by assumption.
113
9b6c9d172378 slightly updated
Christian Urban <urbanc@in.tum.de>
parents: 102
diff changeset
    48
9b6c9d172378 slightly updated
Christian Urban <urbanc@in.tum.de>
parents: 102
diff changeset
    49
  (FIXME: add linenumbers to the proof below and the text above)
32
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
    50
*}
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
    51
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
    52
lemma trcl_induct:
113
9b6c9d172378 slightly updated
Christian Urban <urbanc@in.tum.de>
parents: 102
diff changeset
    53
  assumes asm: "trcl R x y"
32
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
    54
  shows "(\<And>x. P x x) \<Longrightarrow> (\<And>x y z. R x y \<Longrightarrow> P y z \<Longrightarrow> P x z) \<Longrightarrow> P x y"
113
9b6c9d172378 slightly updated
Christian Urban <urbanc@in.tum.de>
parents: 102
diff changeset
    55
apply(atomize (full))
9b6c9d172378 slightly updated
Christian Urban <urbanc@in.tum.de>
parents: 102
diff changeset
    56
apply(cut_tac asm)
9b6c9d172378 slightly updated
Christian Urban <urbanc@in.tum.de>
parents: 102
diff changeset
    57
apply(unfold trcl_def)
9b6c9d172378 slightly updated
Christian Urban <urbanc@in.tum.de>
parents: 102
diff changeset
    58
apply(drule spec[where x=P])
9b6c9d172378 slightly updated
Christian Urban <urbanc@in.tum.de>
parents: 102
diff changeset
    59
apply(assumption)
9b6c9d172378 slightly updated
Christian Urban <urbanc@in.tum.de>
parents: 102
diff changeset
    60
done
88
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 38
diff changeset
    61
113
9b6c9d172378 slightly updated
Christian Urban <urbanc@in.tum.de>
parents: 102
diff changeset
    62
text {*
88
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 38
diff changeset
    63
  We now turn to the proofs of the introduction rules, which are slightly more complicated.
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 38
diff changeset
    64
  In order to prove the first introduction rule, we again unfold the definition and
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 38
diff changeset
    65
  then apply the introdution rules for @{text "\<forall>"} and @{text "\<longrightarrow>"} as often as possible.
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 38
diff changeset
    66
  We then end up in a proof state of the following form:
113
9b6c9d172378 slightly updated
Christian Urban <urbanc@in.tum.de>
parents: 102
diff changeset
    67
9b6c9d172378 slightly updated
Christian Urban <urbanc@in.tum.de>
parents: 102
diff changeset
    68
*}
88
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 38
diff changeset
    69
113
9b6c9d172378 slightly updated
Christian Urban <urbanc@in.tum.de>
parents: 102
diff changeset
    70
(*<*)lemma "trcl R x x"
9b6c9d172378 slightly updated
Christian Urban <urbanc@in.tum.de>
parents: 102
diff changeset
    71
  apply (unfold trcl_def)
9b6c9d172378 slightly updated
Christian Urban <urbanc@in.tum.de>
parents: 102
diff changeset
    72
  apply (rule allI impI)+(*>*)
9b6c9d172378 slightly updated
Christian Urban <urbanc@in.tum.de>
parents: 102
diff changeset
    73
txt {* @{subgoals [display]} *}
32
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
    74
(*<*)oops(*>*)
88
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 38
diff changeset
    75
113
9b6c9d172378 slightly updated
Christian Urban <urbanc@in.tum.de>
parents: 102
diff changeset
    76
text {*
9b6c9d172378 slightly updated
Christian Urban <urbanc@in.tum.de>
parents: 102
diff changeset
    77
  The two assumptions correspond to the introduction rules, where @{text "trcl
9b6c9d172378 slightly updated
Christian Urban <urbanc@in.tum.de>
parents: 102
diff changeset
    78
  R"} has been replaced by P. Thus, all we have to do is to eliminate the
9b6c9d172378 slightly updated
Christian Urban <urbanc@in.tum.de>
parents: 102
diff changeset
    79
  universal quantifier in front of the first assumption, and then solve the
9b6c9d172378 slightly updated
Christian Urban <urbanc@in.tum.de>
parents: 102
diff changeset
    80
  goal by assumption:
9b6c9d172378 slightly updated
Christian Urban <urbanc@in.tum.de>
parents: 102
diff changeset
    81
*}
9b6c9d172378 slightly updated
Christian Urban <urbanc@in.tum.de>
parents: 102
diff changeset
    82
9b6c9d172378 slightly updated
Christian Urban <urbanc@in.tum.de>
parents: 102
diff changeset
    83
32
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
    84
lemma trcl_base: "trcl R x x"
113
9b6c9d172378 slightly updated
Christian Urban <urbanc@in.tum.de>
parents: 102
diff changeset
    85
apply(unfold trcl_def)
9b6c9d172378 slightly updated
Christian Urban <urbanc@in.tum.de>
parents: 102
diff changeset
    86
apply(rule allI impI)+
9b6c9d172378 slightly updated
Christian Urban <urbanc@in.tum.de>
parents: 102
diff changeset
    87
apply(drule spec)
9b6c9d172378 slightly updated
Christian Urban <urbanc@in.tum.de>
parents: 102
diff changeset
    88
apply(assumption)
9b6c9d172378 slightly updated
Christian Urban <urbanc@in.tum.de>
parents: 102
diff changeset
    89
done
88
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 38
diff changeset
    90
113
9b6c9d172378 slightly updated
Christian Urban <urbanc@in.tum.de>
parents: 102
diff changeset
    91
text {*
88
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 38
diff changeset
    92
  Since the second introduction rule has premises, its proof is not as easy as the previous
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 38
diff changeset
    93
  one. After unfolding the definitions and applying the introduction rules for @{text "\<forall>"}
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 38
diff changeset
    94
  and @{text "\<longrightarrow>"}, we get the proof state
113
9b6c9d172378 slightly updated
Christian Urban <urbanc@in.tum.de>
parents: 102
diff changeset
    95
*}
9b6c9d172378 slightly updated
Christian Urban <urbanc@in.tum.de>
parents: 102
diff changeset
    96
9b6c9d172378 slightly updated
Christian Urban <urbanc@in.tum.de>
parents: 102
diff changeset
    97
9b6c9d172378 slightly updated
Christian Urban <urbanc@in.tum.de>
parents: 102
diff changeset
    98
(*<*)lemma "R x y \<Longrightarrow> trcl R y z \<Longrightarrow> trcl R x z"
9b6c9d172378 slightly updated
Christian Urban <urbanc@in.tum.de>
parents: 102
diff changeset
    99
  apply (unfold trcl_def)
9b6c9d172378 slightly updated
Christian Urban <urbanc@in.tum.de>
parents: 102
diff changeset
   100
  apply (rule allI impI)+(*>*)
9b6c9d172378 slightly updated
Christian Urban <urbanc@in.tum.de>
parents: 102
diff changeset
   101
txt {*@{subgoals [display]} *}
9b6c9d172378 slightly updated
Christian Urban <urbanc@in.tum.de>
parents: 102
diff changeset
   102
(*<*)oops(*>*)
9b6c9d172378 slightly updated
Christian Urban <urbanc@in.tum.de>
parents: 102
diff changeset
   103
9b6c9d172378 slightly updated
Christian Urban <urbanc@in.tum.de>
parents: 102
diff changeset
   104
text {*
88
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 38
diff changeset
   105
  The third and fourth assumption corresponds to the first and second introduction rule,
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 38
diff changeset
   106
  respectively, whereas the first and second assumption corresponds to the premises of
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 38
diff changeset
   107
  the introduction rule. Since we want to prove the second introduction rule, we apply
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 38
diff changeset
   108
  the fourth assumption to the goal @{term "P x z"}. In order for the assumption to
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 38
diff changeset
   109
  be applicable, we have to eliminate the universal quantifiers and turn the object-level
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 38
diff changeset
   110
  implications into meta-level ones. This can be accomplished using the @{text rule_format}
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 38
diff changeset
   111
  attribute. Applying the assumption produces two new subgoals, which can be solved using
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 38
diff changeset
   112
  the first and second assumption. The second assumption again involves a quantifier and
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 38
diff changeset
   113
  implications that have to be eliminated before it can be applied. To avoid problems
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 38
diff changeset
   114
  with higher order unification, it is advisable to provide an instantiation for the
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 38
diff changeset
   115
  universally quantified predicate variable in the assumption.
32
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   116
*}
113
9b6c9d172378 slightly updated
Christian Urban <urbanc@in.tum.de>
parents: 102
diff changeset
   117
88
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 38
diff changeset
   118
32
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   119
lemma trcl_step: "R x y \<Longrightarrow> trcl R y z \<Longrightarrow> trcl R x z"
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   120
  apply (unfold trcl_def)
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   121
  apply (rule allI impI)+
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   122
  proof -
102
5e309df58557 general cleaning up; deleted antiquotation ML_text; adjusted pathnames of various files in the distribution
Christian Urban <urbanc@in.tum.de>
parents: 88
diff changeset
   123
    case (goal1 P)
5e309df58557 general cleaning up; deleted antiquotation ML_text; adjusted pathnames of various files in the distribution
Christian Urban <urbanc@in.tum.de>
parents: 88
diff changeset
   124
    have g1: "R x y" by fact
5e309df58557 general cleaning up; deleted antiquotation ML_text; adjusted pathnames of various files in the distribution
Christian Urban <urbanc@in.tum.de>
parents: 88
diff changeset
   125
    have g2: "\<forall>P. (\<forall>x. P x x) \<longrightarrow> (\<forall>x y z. R x y \<longrightarrow> P y z \<longrightarrow> P x z) \<longrightarrow> P y z" by fact
5e309df58557 general cleaning up; deleted antiquotation ML_text; adjusted pathnames of various files in the distribution
Christian Urban <urbanc@in.tum.de>
parents: 88
diff changeset
   126
    have g3: "\<forall>x. P x x" by fact
5e309df58557 general cleaning up; deleted antiquotation ML_text; adjusted pathnames of various files in the distribution
Christian Urban <urbanc@in.tum.de>
parents: 88
diff changeset
   127
    have g4: "\<forall>x y z. R x y \<longrightarrow> P y z \<longrightarrow> P x z" by fact
32
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   128
    show ?case
102
5e309df58557 general cleaning up; deleted antiquotation ML_text; adjusted pathnames of various files in the distribution
Christian Urban <urbanc@in.tum.de>
parents: 88
diff changeset
   129
      apply (rule g4 [rule_format])
5e309df58557 general cleaning up; deleted antiquotation ML_text; adjusted pathnames of various files in the distribution
Christian Urban <urbanc@in.tum.de>
parents: 88
diff changeset
   130
      apply (rule g1)
5e309df58557 general cleaning up; deleted antiquotation ML_text; adjusted pathnames of various files in the distribution
Christian Urban <urbanc@in.tum.de>
parents: 88
diff changeset
   131
      apply (rule g2 [THEN spec [where x=P], THEN mp, THEN mp, OF g3, OF g4])
32
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   132
      done
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   133
  qed
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   134
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   135
text {*
88
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 38
diff changeset
   136
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 38
diff changeset
   137
  This method of defining inductive predicates easily generalizes to mutually inductive
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 38
diff changeset
   138
  predicates, like the predicates @{text even} and @{text odd} characterized by the
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 38
diff changeset
   139
  following introduction rules:
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 38
diff changeset
   140
 
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 38
diff changeset
   141
  \begin{center}
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 38
diff changeset
   142
  @{term[mode=Axiom] "even (0::nat)"} \hspace{5mm}
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 38
diff changeset
   143
  @{term[mode=Rule] "odd m \<Longrightarrow> even (Suc m)"} \hspace{5mm}
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 38
diff changeset
   144
  @{term[mode=Rule] "even m \<Longrightarrow> odd (Suc m)"}
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 38
diff changeset
   145
  \end{center}
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 38
diff changeset
   146
  
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 38
diff changeset
   147
  Since the predicates are mutually inductive, each of the definitions contain two
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 38
diff changeset
   148
  quantifiers over the predicates @{text P} and @{text Q}.
32
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   149
*}
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   150
88
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 38
diff changeset
   151
definition "even n \<equiv> 
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 38
diff changeset
   152
  \<forall>P Q. P 0 \<longrightarrow> (\<forall>m. Q m \<longrightarrow> P (Suc m)) 
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 38
diff changeset
   153
             \<longrightarrow> (\<forall>m. P m \<longrightarrow> Q (Suc m)) \<longrightarrow> P n"
32
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   154
88
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 38
diff changeset
   155
definition "odd n \<equiv>
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 38
diff changeset
   156
  \<forall>P Q. P 0 \<longrightarrow> (\<forall>m. Q m \<longrightarrow> P (Suc m)) 
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 38
diff changeset
   157
             \<longrightarrow> (\<forall>m. P m \<longrightarrow> Q (Suc m)) \<longrightarrow> Q n"
32
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   158
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   159
text {*
88
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 38
diff changeset
   160
  For proving the induction rule, we use exactly the same technique as in the transitive
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 38
diff changeset
   161
  closure example:
32
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   162
*}
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   163
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   164
lemma even_induct:
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   165
  assumes even: "even n"
38
e21b2f888fa2 added a preliminary section about parsing
Christian Urban <urbanc@in.tum.de>
parents: 32
diff changeset
   166
  shows "P 0 \<Longrightarrow> 
e21b2f888fa2 added a preliminary section about parsing
Christian Urban <urbanc@in.tum.de>
parents: 32
diff changeset
   167
             (\<And>m. Q m \<Longrightarrow> P (Suc m)) \<Longrightarrow> (\<And>m. P m \<Longrightarrow> Q (Suc m)) \<Longrightarrow> P n"
32
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   168
  apply (atomize (full))
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   169
  apply (cut_tac even)
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   170
  apply (unfold even_def)
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   171
  apply (drule spec [where x=P])
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   172
  apply (drule spec [where x=Q])
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   173
  apply assumption
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   174
  done
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   175
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   176
text {*
88
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 38
diff changeset
   177
  A similar induction rule having @{term "Q n"} as a conclusion can be proved for
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 38
diff changeset
   178
  the @{text odd} predicate.
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 38
diff changeset
   179
  The proofs of the introduction rules are also very similar to the ones in the
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 38
diff changeset
   180
  previous example. We only show the proof of the second introduction rule,
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 38
diff changeset
   181
  since it is almost the same as the one for the third introduction rule,
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 38
diff changeset
   182
  and the proof of the first rule is trivial.
32
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   183
*}
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   184
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   185
lemma evenS: "odd m \<Longrightarrow> even (Suc m)"
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   186
  apply (unfold odd_def even_def)
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   187
  apply (rule allI impI)+
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   188
  proof -
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   189
    case goal1
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   190
    show ?case
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   191
      apply (rule goal1(3) [rule_format])
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   192
      apply (rule goal1(1) [THEN spec [where x=P], THEN spec [where x=Q],
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   193
	THEN mp, THEN mp, THEN mp, OF goal1(2-4)])
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   194
      done
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   195
  qed
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   196
(*<*)
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   197
lemma even0: "even 0"
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   198
  apply (unfold even_def)
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   199
  apply (rule allI impI)+
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   200
  apply assumption
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   201
  done
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   202
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   203
lemma oddS: "even m \<Longrightarrow> odd (Suc m)"
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   204
  apply (unfold odd_def even_def)
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   205
  apply (rule allI impI)+
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   206
  proof -
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   207
    case goal1
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   208
    show ?case
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   209
      apply (rule goal1(4) [rule_format])
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   210
      apply (rule goal1(1) [THEN spec [where x=P], THEN spec [where x=Q],
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   211
	THEN mp, THEN mp, THEN mp, OF goal1(2-4)])
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   212
      done
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   213
  qed
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   214
(*>*)
88
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 38
diff changeset
   215
32
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   216
text {*
88
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 38
diff changeset
   217
  As a final example, we will consider the definition of the accessible
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 38
diff changeset
   218
  part of a relation @{text R} characterized by the introduction rule
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 38
diff changeset
   219
  
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 38
diff changeset
   220
  \begin{center}
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 38
diff changeset
   221
  @{term[mode=Rule] "(\<forall>y. R y x \<longrightarrow> accpart R y) \<Longrightarrow> accpart R x"}
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 38
diff changeset
   222
  \end{center}
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 38
diff changeset
   223
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 38
diff changeset
   224
  whose premise involves a universal quantifier and an implication. The
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 38
diff changeset
   225
  definition of @{text accpart} is as follows:
32
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   226
*}
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   227
88
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 38
diff changeset
   228
definition "accpart R x \<equiv> \<forall>P. (\<forall>x. (\<forall>y. R y x \<longrightarrow> P y) \<longrightarrow> P x) \<longrightarrow> P x"
32
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   229
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   230
text {*
88
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 38
diff changeset
   231
  The proof of the induction theorem is again straightforward:
32
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   232
*}
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   233
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   234
lemma accpart_induct:
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   235
  assumes acc: "accpart R x"
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   236
  shows "(\<And>x. (\<And>y. R y x \<Longrightarrow> P y) \<Longrightarrow> P x) \<Longrightarrow> P x"
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   237
  apply (atomize (full))
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   238
  apply (cut_tac acc)
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   239
  apply (unfold accpart_def)
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   240
  apply (drule spec [where x=P])
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   241
  apply assumption
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   242
  done
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   243
(*<*)
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   244
lemma accpartI: "(\<And>y. R y x \<Longrightarrow> accpart R y) \<Longrightarrow> accpart R x"
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   245
  apply (unfold accpart_def)
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   246
  apply (rule allI impI)+
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   247
(*>*)
88
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 38
diff changeset
   248
32
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   249
txt {*
88
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 38
diff changeset
   250
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 38
diff changeset
   251
  Proving the introduction rule is a little more complicated, due to the quantifier
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 38
diff changeset
   252
  and the implication in the premise. We first convert the meta-level universal quantifier
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 38
diff changeset
   253
  and implication to their object-level counterparts. Unfolding the definition of
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 38
diff changeset
   254
  @{text accpart} and applying the introduction rules for @{text "\<forall>"} and @{text "\<longrightarrow>"}
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 38
diff changeset
   255
  yields the following proof state:
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 38
diff changeset
   256
  @{subgoals [display]}
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 38
diff changeset
   257
  Applying the second assumption produces a proof state with the new local assumption
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 38
diff changeset
   258
  @{term "R y x"}, which will then be used to solve the goal @{term "P y"} using the
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 38
diff changeset
   259
  first assumption.
32
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   260
*}
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   261
(*<*)oops(*>*)
88
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 38
diff changeset
   262
32
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   263
lemma accpartI: "(\<And>y. R y x \<Longrightarrow> accpart R y) \<Longrightarrow> accpart R x"
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   264
  apply (unfold accpart_def)
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   265
  apply (rule allI impI)+
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   266
  proof -
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   267
    case goal1
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   268
    note goal1' = this
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   269
    show ?case
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   270
      apply (rule goal1'(2) [rule_format])
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   271
      proof -
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   272
        case goal1
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   273
        show ?case
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   274
	  apply (rule goal1'(1) [OF goal1, THEN spec [where x=P],
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   275
            THEN mp, OF goal1'(2)])
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   276
	  done
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   277
      qed
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   278
    qed
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   279
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   280
end