ProgTutorial/Package/Ind_General_Scheme.thy
changeset 211 d5accbc67e1b
parent 210 db8e302f44c8
child 212 ac01ddb285f6
equal deleted inserted replaced
210:db8e302f44c8 211:d5accbc67e1b
    15 | fresh_app: "\<lbrakk>fresh a t; fresh a s\<rbrakk> \<Longrightarrow> fresh a (App t s)"
    15 | fresh_app: "\<lbrakk>fresh a t; fresh a s\<rbrakk> \<Longrightarrow> fresh a (App t s)"
    16 | fresh_lam1: "fresh a (Lam a t)"
    16 | fresh_lam1: "fresh a (Lam a t)"
    17 | fresh_lam2: "\<lbrakk>a\<noteq>b; fresh a t\<rbrakk> \<Longrightarrow> fresh a (Lam b t)"
    17 | fresh_lam2: "\<lbrakk>a\<noteq>b; fresh a t\<rbrakk> \<Longrightarrow> fresh a (Lam b t)"
    18 (*>*)
    18 (*>*)
    19 
    19 
    20 section {* The Code in a Nutshell *}
    20 section {* The Code in a Nutshell\label{sec:nutshell} *}
    21 
    21 
    22 text {*
    22 text {*
    23   (FIXME: perhaps move somewhere else)
    23   (FIXME: perhaps move somewhere else)
    24 
    24 
    25   The point of these examples is to get a feeling what the automatic proofs 
    25   The point of these examples is to get a feeling what the automatic proofs 
    32   The inductive package will generate the reasoning infrastructure
    32   The inductive package will generate the reasoning infrastructure
    33   for mutually recursive predicates @{text "pred\<^isub>1\<dots>pred\<^isub>n"}. In what
    33   for mutually recursive predicates @{text "pred\<^isub>1\<dots>pred\<^isub>n"}. In what
    34   follows we will have the convention that various, possibly empty collections of 
    34   follows we will have the convention that various, possibly empty collections of 
    35   ``things'' are indicated either by adding an @{text "s"} or by adding 
    35   ``things'' are indicated either by adding an @{text "s"} or by adding 
    36   a superscript @{text [quotes] "\<^isup>*"}. The shorthand for the predicates 
    36   a superscript @{text [quotes] "\<^isup>*"}. The shorthand for the predicates 
    37   will therefore be @{text "preds"} or @{text "pred\<^sup>*"}. In this case of the
    37   will therefore be @{text "preds"} or @{text "pred\<^sup>*"}. In the case of the
    38   predicates there must be at least a single one in order to obtain a meaningful
    38   predicates there must be, of course, at least a single one in order to obtain a 
    39   definition.
    39   meaningful definition.
    40 
    40 
    41   The input for the inductive predicate will be some @{text "preds"} with possible 
    41   The input for the inductive package will be some @{text "preds"} with possible 
    42   typing and syntax annotations, and also some introduction rules. We call below the 
    42   typing and syntax annotations, and also some introduction rules. We call below the 
    43   introduction rules short as @{text "rules"}. Borrowing some idealised Isabelle 
    43   introduction rules short as @{text "rules"}. Borrowing some idealised Isabelle 
    44   notation, one such @{text "rule"} is of the form
    44   notation, one such @{text "rule"} is assumed to be of the form
    45 
    45 
    46   \begin{isabelle}
    46   \begin{isabelle}
    47   @{text "rule ::= 
    47   @{text "rule ::= 
    48   \<And>xs. \<^raw:$\underbrace{\mbox{>As\<^raw:}}_{\text{\makebox[0mm]{\rm non-recursive premises}}}$> \<Longrightarrow> 
    48   \<And>xs. \<^raw:$\underbrace{\mbox{>As\<^raw:}}_{\text{\makebox[0mm]{\rm non-recursive premises}}}$> \<Longrightarrow> 
    49   \<^raw:$\underbrace{\mbox{>(\<And>ys. Bs \<Longrightarrow> pred ss)\<^isup>*\<^raw:}}_{\text{\rm recursive premises}}$> 
    49   \<^raw:$\underbrace{\mbox{>(\<And>ys. Bs \<Longrightarrow> pred ss)\<^isup>*\<^raw:}}_{\text{\rm recursive premises}}$> 
    50   \<Longrightarrow> pred ts"}
    50   \<Longrightarrow> pred ts"}
    51   \end{isabelle}
    51   \end{isabelle}
    52   
    52   
    53   We actually assume the user will always state rules of this form and we omit
    53   For the purposes here, we will assume the @{text rules} have
    54   any code that test this format. So things can go horribly wrong if the
    54   this format and omit any code that actually tests this. Therefore ``things'' 
    55   @{text "rules"} are not of this form.\footnote{FIXME: Exercise to test this
    55   can go horribly wrong, if the @{text "rules"} are not of this
    56   format.} The @{text As} and @{text Bs} in a @{text "rule"} are formulae not
    56   form.\footnote{FIXME: Exercise to test this format.} The @{text As} and
    57   involving the inductive predicates @{text "preds"}; the instances @{text
    57   @{text Bs} in a @{text "rule"} are formulae not involving the inductive
    58   "pred ss"} and @{text "pred ts"} can stand for different predicates.
    58   predicates @{text "preds"}; the instances @{text "pred ss"} and @{text "pred
    59   Everything left of @{text [quotes] "\<Longrightarrow> pred ts"} are called the premises of
    59   ts"} can stand for different predicates, like @{text "pred\<^isub>1 ss"} and 
    60   the rule. The variables @{text "xs"} are usually omitted in the user's
    60   @{text "pred\<^isub>2 ts"}; @{text ss} and @{text ts} are the
    61   input. The variables @{text "ys"} are local with respect to on recursive
    61   arguments of the predicates. Every formula left of @{text [quotes]
    62   premise. Some examples of @{text "rule"}s the user might write are:
    62   "\<Longrightarrow> pred ts"} is a premise of the rule. The outermost quantified 
    63 
    63   variables @{text "xs"} are usually omitted in the user's input. The 
       
    64   quantification for the variables @{text "ys"} is local with respect to 
       
    65   one recursive premise and must be given. Some examples of @{text "rule"}s 
       
    66   are
    64 
    67 
    65   @{thm [display] fresh_var[no_vars]}
    68   @{thm [display] fresh_var[no_vars]}
    66 
    69 
    67   which has only a single non-recursive premise, whereas
    70   which has only a single non-recursive premise, whereas
    68 
    71 
    70 
    73 
    71   has a single recursive premise; the rule
    74   has a single recursive premise; the rule
    72 
    75 
    73   @{thm [display] accpartI[no_vars]}
    76   @{thm [display] accpartI[no_vars]}
    74 
    77 
    75   has a recursive premise which has a precondition. In the examples, all 
    78   has a recursive premise that has a precondition. As usual all 
    76   rules are stated without the leading meta-quantification @{text "\<And>xs"}.
    79   rules are stated without the leading meta-quantification @{text "\<And>xs"}.
    77 
    80 
    78   The code of the inductive package falls roughly in tree parts involving
    81   The code of the inductive package falls roughly in tree parts: the first
    79   the definitions, the induction principles and the introduction rules, 
    82   deals with the definitions, the second with the induction principles and 
    80   respectively. For the definitions we need to have the @{text rules}
    83   the third with the introduction rules. 
    81   in a form where the meta-quantifiers and meta-implications are replaced
    84   
    82   by their object logic equivalent. Therefore an @{text "orule"} is of the
    85   For the definitions we need to have the @{text rules} in a form where 
    83   form 
    86   the meta-quantifiers and meta-implications are replaced by their object 
       
    87   logic equivalents. Therefore an @{text "orule"} is of the form 
    84 
    88 
    85   @{text [display] "orule ::= \<forall>xs. As \<longrightarrow> (\<forall>ys. Bs \<longrightarrow> pred ss)\<^isup>* \<longrightarrow> pred ts"}
    89   @{text [display] "orule ::= \<forall>xs. As \<longrightarrow> (\<forall>ys. Bs \<longrightarrow> pred ss)\<^isup>* \<longrightarrow> pred ts"}
    86 
    90 
    87   A definition for the predicate @{text "pred"} has then the form
    91   A definition for the predicate @{text "pred"} has then the form
    88 
    92 
    89   @{text [display] "def ::= pred \<equiv> \<lambda>zs. \<forall>preds. orules \<longrightarrow> pred zs"}
    93   @{text [display] "def ::= pred \<equiv> \<lambda>zs. \<forall>preds. orules \<longrightarrow> pred zs"}
    90 
    94 
    91   The induction principles for the predicate @{text "pred"} are of the
    95   The induction principles for every predicate @{text "pred"} are of the
    92   form
    96   form
    93 
    97 
    94   @{text [display] "ind ::= pred ?zs \<Longrightarrow> rules[preds := ?Ps] \<Longrightarrow> ?P ?zs"}
    98   @{text [display] "ind ::= pred ?zs \<Longrightarrow> rules[preds := ?Ps] \<Longrightarrow> ?P ?zs"}
    95 
    99 
    96   where in the @{text "rules"} every @{text pred} is replaced by a new
   100   where in the @{text "rules"} every @{text pred} is replaced by a fresh
    97   (meta)variable @{text "?P"}.
   101   meta-variable @{text "?P"}.
    98 
   102 
    99   In order to derive an induction principle for the predicate @{text "pred"}
   103   In order to derive an induction principle for the predicate @{text "pred"},
   100   we first transform it into the object logic and fix the meta-variables. Hence 
   104   we first transform @{text ind} into the object logic and fix the meta-variables. 
   101   we have to prove a formula of the form
   105   Hence we have to prove a formula of the form
   102 
   106 
   103   @{text [display] "pred zs \<longrightarrow> orules[preds := Ps] \<longrightarrow> P zs"}
   107   @{text [display] "pred zs \<longrightarrow> orules[preds := Ps] \<longrightarrow> P zs"}
   104 
   108 
   105   If we assume @{text "pred zs"} and unfold its definition, then we have
   109   If we assume @{text "pred zs"} and unfold its definition, then we have
   106   
   110   
   108 
   112 
   109   and must prove
   113   and must prove
   110 
   114 
   111   @{text [display] "orules[preds := Ps] \<longrightarrow> P zs"}
   115   @{text [display] "orules[preds := Ps] \<longrightarrow> P zs"}
   112 
   116 
   113   This can be done by instantiating the @{text "\<forall>preds"} with the @{text "Ps"}. 
   117   This can be done by instantiating the @{text "\<forall>preds"}-quantification 
   114   Then we are done since we are left with a simple identity.
   118   with the @{text "Ps"}. Then we are done since we are left with a simple 
   115   
   119   identity.
   116   The proofs for the introduction rules are more involved. Assuming we want to
   120   
   117   prove the introduction rule 
   121   Although the user declares introduction rules @{text rules}, they must 
       
   122   be derived from the @{text defs}. These derivations are a bit involved. 
       
   123   Assuming we want to prove the introduction rule 
   118 
   124 
   119   @{text [display] "\<And>xs. As \<Longrightarrow> (\<And>ys. Bs \<Longrightarrow> pred ss)\<^isup>* \<Longrightarrow> pred ts"}
   125   @{text [display] "\<And>xs. As \<Longrightarrow> (\<And>ys. Bs \<Longrightarrow> pred ss)\<^isup>* \<Longrightarrow> pred ts"}
   120 
   126 
   121   then we can assume
   127   then we can assume
   122 
   128 
   141 
   147 
   142   @{text [display] "pred ts"}
   148   @{text [display] "pred ts"}
   143 
   149 
   144   In the last step we removed some quantifiers and moved the precondition @{text "orules"}  
   150   In the last step we removed some quantifiers and moved the precondition @{text "orules"}  
   145   into the assumtion. The @{text "orules"} stand for all introduction rules that are given 
   151   into the assumtion. The @{text "orules"} stand for all introduction rules that are given 
   146   by the user. We apply the one which corresponds to introduction rule we are proving.
   152   by the user. We apply the @{text orule} that corresponds to introduction rule we are 
   147   This introduction rule must be of the form 
   153   proving. This introduction rule must necessarily be of the form 
   148 
   154 
   149   @{text [display] "As \<Longrightarrow> (\<And>ys. Bs \<Longrightarrow> pred ss)\<^isup>* \<Longrightarrow> pred ts"}
   155   @{text [display] "As \<Longrightarrow> (\<And>ys. Bs \<Longrightarrow> pred ss)\<^isup>* \<Longrightarrow> pred ts"}
   150 
   156 
   151   When we apply this rule we end up in the goal state where we have to prove
   157   When we apply this rule we end up in the goal state where we have to prove
   152 
   158 
   153   \begin{isabelle}
   159   \begin{isabelle}
   154   (a)~@{text "As"}\\
   160   (a)~@{text "As"}\\
   155   (b)~@{text "(\<And>ys. Bs \<Longrightarrow> pred ss)\<^isup>*"}
   161   (b)~@{text "(\<And>ys. Bs \<Longrightarrow> pred ss)\<^isup>*"}
   156   \end{isabelle}
   162   \end{isabelle}
   157 
   163 
   158   The goals @{text "As"} we can immediately discharge with the assumption in @{text "(i)"}.
   164   We can immediately discharge the goals @{text "As"} using the assumption in 
   159   The goals in @{text "(b)"} we discharge as follows: we assume the @{text "(iv)"} 
   165   @{text "(i)"}. The goals in @{text "(b)"} can be discharged as follows: we 
   160   @{text "Bs"} and prove @{text "(c)"} @{text "pred ss"}. We then resolve the 
   166   assume the @{text "Bs"} and prove @{text "pred ss"}. For this we resolve the 
   161   @{text "Bs"}  with the assumptions in @{text "(ii)"}. This gives us
   167   @{text "Bs"}  with the assumptions in @{text "(ii)"}. This gives us the 
       
   168   assumptions
   162 
   169 
   163   @{text [display] "(\<forall>preds. orules \<longrightarrow> pred ss)\<^isup>*"}
   170   @{text [display] "(\<forall>preds. orules \<longrightarrow> pred ss)\<^isup>*"}
   164 
   171 
   165   Instantiating the universal quantifiers and then resolving with the assumptions 
   172   Instantiating the universal quantifiers and then resolving with the assumptions 
   166   in @{text "(iii)"} gives us @{text "pred ss"}, which is the goal we are after.
   173   in @{text "(iii)"} gives us @{text "pred ss"}, which is the goal we are after.
   167 
   174   This completes the proof for introduction rules.
   168   
   175 
   169   What remains is to implement the reasoning outlined above. 
   176   What remains is to implement the reasoning outlined above. We do this in
   170   For building testcases, we use some shorthands for the definitions 
   177   the next section. For building testcases, we use the shorthands for 
   171   of @{text "even/odd"}, @{term "fresh"} and @{term "accpart"}. 
   178   @{text "even/odd"}, @{term "fresh"} and @{term "accpart"}
       
   179   given in Figure~\ref{fig:shorthands}.
   172 *}
   180 *}
   173 
   181 
   174 
   182 
   175 text_raw{*
   183 text_raw{*
   176 \begin{figure}[p]
   184 \begin{figure}[p]
   217 
   225 
   218 
   226 
   219 (* accessible-part example *)
   227 (* accessible-part example *)
   220 val acc_rules = 
   228 val acc_rules = 
   221      [@{prop "\<And>R x. (\<And>y. R y x \<Longrightarrow> accpart R y) \<Longrightarrow> accpart R x"}]
   229      [@{prop "\<And>R x. (\<And>y. R y x \<Longrightarrow> accpart R y) \<Longrightarrow> accpart R x"}]
   222 val acc_pred = @{term "accpart:: ('a \<Rightarrow>'a\<Rightarrow>bool)\<Rightarrow>'a \<Rightarrow>bool"}*}
   230 val acc_pred = @{term "accpart::('a \<Rightarrow>'a\<Rightarrow>bool)\<Rightarrow>'a \<Rightarrow>bool"}*}
   223 text_raw{*
   231 text_raw{*
   224 \end{isabelle}
   232 \end{isabelle}
   225 \end{minipage}
   233 \end{minipage}
   226 \caption{Shorthands for the inductive predicates of @{text "even"}-@{text "odd"}, 
   234 \caption{Shorthands for the inductive predicates @{text "even"}-@{text "odd"}, 
   227   @{text "fresh"} and @{text "accpart"}. The follow the convention @{text "rules"}, 
   235   @{text "fresh"} and @{text "accpart"}. The names of these shorthands follow 
   228   @{text "orules"}, @{text "preds"} and so on as used in Section ???. The purpose 
   236   the convention @{text "rules"}, @{text "orules"}, @{text "preds"} and so on. 
   229   of these shorthands is to simplify the construction of testcases.}
   237   The purpose of these shorthands is to simplify the construction of testcases
       
   238   in Section~\ref{sec:code}.\label{fig:shorthands}}
   230 \end{figure}
   239 \end{figure}
   231 *}
   240 *}
   232 
   241 
   233 
   242 
   234 
   243