|     34 | fresh_lam1: "fresh a (Lam a t)" |     34 | fresh_lam1: "fresh a (Lam a t)" | 
|     35 | fresh_lam2: "\<lbrakk>a\<noteq>b; fresh a t\<rbrakk> \<Longrightarrow> fresh a (Lam b t)" |     35 | fresh_lam2: "\<lbrakk>a\<noteq>b; fresh a t\<rbrakk> \<Longrightarrow> fresh a (Lam b t)" | 
|     36 (*>*) |     36 (*>*) | 
|     37  |     37  | 
|     38  |     38  | 
|     39 section {* The Code in a Nutshell\label{sec:nutshell} *} |     39 section \<open>The Code in a Nutshell\label{sec:nutshell}\<close> | 
|     40  |     40  | 
|     41  |     41  | 
|     42 text {* |     42 text \<open> | 
|     43   The inductive package will generate the reasoning infrastructure for |     43   The inductive package will generate the reasoning infrastructure for | 
|     44   mutually recursive predicates, say @{text "pred\<^sub>1\<dots>pred\<^sub>n"}. In what |     44   mutually recursive predicates, say \<open>pred\<^sub>1\<dots>pred\<^sub>n\<close>. In what | 
|     45   follows we will have the convention that various, possibly empty collections |     45   follows we will have the convention that various, possibly empty collections | 
|     46   of ``things'' (lists, terms, nested implications and so on) are indicated either by |     46   of ``things'' (lists, terms, nested implications and so on) are indicated either by | 
|     47   adding an @{text [quotes] "s"} or by adding a superscript @{text [quotes] |     47   adding an @{text [quotes] "s"} or by adding a superscript @{text [quotes] | 
|     48   "\<^sup>*"}. The shorthand for the predicates will therefore be @{text |     48   "\<^sup>*"}. The shorthand for the predicates will therefore be \<open>preds\<close> or \<open>pred\<^sup>*\<close>. In the case of the predicates there must | 
|     49   "preds"} or @{text "pred\<^sup>*"}. In the case of the predicates there must |         | 
|     50   be, of course, at least a single one in order to obtain a meaningful |     49   be, of course, at least a single one in order to obtain a meaningful | 
|     51   definition. |     50   definition. | 
|     52  |     51  | 
|     53   The input for the inductive package will be some @{text "preds"} with possible  |     52   The input for the inductive package will be some \<open>preds\<close> with possible  | 
|     54   typing and syntax annotations, and also some introduction rules. We call below the  |     53   typing and syntax annotations, and also some introduction rules. We call below the  | 
|     55   introduction rules short as @{text "rules"}. Borrowing some idealised Isabelle  |     54   introduction rules short as \<open>rules\<close>. Borrowing some idealised Isabelle  | 
|     56   notation, one such @{text "rule"} is assumed to be of the form |     55   notation, one such \<open>rule\<close> is assumed to be of the form | 
|     57  |     56  | 
|     58   \begin{isabelle} |     57   \begin{isabelle} | 
|     59   @{text "rule ::=  |     58   \<open>rule ::=  | 
|     60   \<And>xs. \<^latex>\<open>$\\underbrace{\\mbox{\<close>As\<^latex>\<open>}}_{\\text{\\makebox[0mm]{\\rm non-recursive premises}}}$\<close> \<Longrightarrow>  |     59   \<And>xs. \<^latex>\<open>$\underbrace{\mbox{\<close>As\<^latex>\<open>}}_{\text{\makebox[0mm]{\rm non-recursive premises}}}$\<close> \<Longrightarrow>  | 
|     61   \<^latex>\<open>$\\underbrace{\\mbox{\<close>(\<And>ys. Bs \<Longrightarrow> pred ss)\<^sup>*\<^latex>\<open>}}_{\\text{\\rm recursive premises}}$>\<close>  |     60   \<^latex>\<open>$\underbrace{\mbox{\<close>(\<And>ys. Bs \<Longrightarrow> pred ss)\<^sup>*\<^latex>\<open>}}_{\text{\rm recursive premises}}$>\<close>  | 
|     62   \<Longrightarrow> pred ts"} |     61   \<Longrightarrow> pred ts\<close> | 
|     63   \end{isabelle} |     62   \end{isabelle} | 
|     64    |     63    | 
|     65   For the purposes here, we will assume the @{text rules} have this format and |     64   For the purposes here, we will assume the \<open>rules\<close> have this format and | 
|     66   omit any code that actually tests this. Therefore ``things'' can go horribly |     65   omit any code that actually tests this. Therefore ``things'' can go horribly | 
|     67   wrong, if the @{text "rules"} are not of this form.  The @{text As} and |     66   wrong, if the \<open>rules\<close> are not of this form.  The \<open>As\<close> and | 
|     68   @{text Bs} in a @{text "rule"} stand for formulae not involving the |     67   \<open>Bs\<close> in a \<open>rule\<close> stand for formulae not involving the | 
|     69   inductive predicates @{text "preds"}; the instances @{text "pred ss"} and |     68   inductive predicates \<open>preds\<close>; the instances \<open>pred ss\<close> and | 
|     70   @{text "pred ts"} can stand for different predicates, like @{text |     69   \<open>pred ts\<close> can stand for different predicates, like \<open>pred\<^sub>1 ss\<close> and \<open>pred\<^sub>2 ts\<close>, in case mutual recursive | 
|     71   "pred\<^sub>1 ss"} and @{text "pred\<^sub>2 ts"}, in case mutual recursive |     70   predicates are defined; the terms \<open>ss\<close> and \<open>ts\<close> are the | 
|     72   predicates are defined; the terms @{text ss} and @{text ts} are the |         | 
|     73   arguments of these predicates. Every formula left of @{text [quotes] "\<Longrightarrow> pred |     71   arguments of these predicates. Every formula left of @{text [quotes] "\<Longrightarrow> pred | 
|     74   ts"} is a premise of the rule. The outermost quantified variables @{text |     72   ts"} is a premise of the rule. The outermost quantified variables \<open>xs\<close> are usually omitted in the user's input. The quantification for the | 
|     75   "xs"} are usually omitted in the user's input. The quantification for the |     73   variables \<open>ys\<close> is local with respect to one recursive premise and | 
|     76   variables @{text "ys"} is local with respect to one recursive premise and |     74   must be given. Some examples of \<open>rule\<close>s are | 
|     77   must be given. Some examples of @{text "rule"}s are |         | 
|     78  |     75  | 
|     79  |     76  | 
|     80   @{thm [display] fresh_var[no_vars]} |     77   @{thm [display] fresh_var[no_vars]} | 
|     81  |     78  | 
|     82   which has only a single non-recursive premise, whereas |     79   which has only a single non-recursive premise, whereas | 
|     86   has a single recursive premise; the rule |     83   has a single recursive premise; the rule | 
|     87  |     84  | 
|     88   @{thm [display] accpartI[no_vars]} |     85   @{thm [display] accpartI[no_vars]} | 
|     89  |     86  | 
|     90   has a single recursive premise that has a precondition. As is custom all  |     87   has a single recursive premise that has a precondition. As is custom all  | 
|     91   rules are stated without the leading meta-quantification @{text "\<And>xs"}. |     88   rules are stated without the leading meta-quantification \<open>\<And>xs\<close>. | 
|     92  |     89  | 
|     93   The output of the inductive package will be definitions for the predicates,  |     90   The output of the inductive package will be definitions for the predicates,  | 
|     94   induction principles and introduction rules.  For the definitions we need to have the |     91   induction principles and introduction rules.  For the definitions we need to have the | 
|     95   @{text rules} in a form where the meta-quantifiers and meta-implications are |     92   \<open>rules\<close> in a form where the meta-quantifiers and meta-implications are | 
|     96   replaced by their object logic equivalents. Therefore an @{text "orule"} is |     93   replaced by their object logic equivalents. Therefore an \<open>orule\<close> is | 
|     97   of the form |     94   of the form | 
|     98  |     95  | 
|     99   @{text [display] "orule ::= \<forall>xs. As \<longrightarrow> (\<forall>ys. Bs \<longrightarrow> pred ss)\<^sup>* \<longrightarrow> pred ts"} |     96   @{text [display] "orule ::= \<forall>xs. As \<longrightarrow> (\<forall>ys. Bs \<longrightarrow> pred ss)\<^sup>* \<longrightarrow> pred ts"} | 
|    100  |     97  | 
|    101   A definition for the predicate @{text "pred"} has then the form |     98   A definition for the predicate \<open>pred\<close> has then the form | 
|    102  |     99  | 
|    103   @{text [display] "def ::= pred \<equiv> \<lambda>zs. \<forall>preds. orules \<longrightarrow> pred zs"} |    100   @{text [display] "def ::= pred \<equiv> \<lambda>zs. \<forall>preds. orules \<longrightarrow> pred zs"} | 
|    104  |    101  | 
|    105   The induction principles for every predicate @{text "pred"} are of the |    102   The induction principles for every predicate \<open>pred\<close> are of the | 
|    106   form |    103   form | 
|    107  |    104  | 
|    108   @{text [display] "ind ::= pred ?zs \<Longrightarrow> rules[preds := ?Ps] \<Longrightarrow> ?P ?zs"} |    105   @{text [display] "ind ::= pred ?zs \<Longrightarrow> rules[preds := ?Ps] \<Longrightarrow> ?P ?zs"} | 
|    109  |    106  | 
|    110   where in the @{text "rules"}-part every @{text pred} is replaced by a fresh |    107   where in the \<open>rules\<close>-part every \<open>pred\<close> is replaced by a fresh | 
|    111   schematic variable @{text "?P"}. |    108   schematic variable \<open>?P\<close>. | 
|    112  |    109  | 
|    113   In order to derive an induction principle for the predicate @{text "pred"}, |    110   In order to derive an induction principle for the predicate \<open>pred\<close>, | 
|    114   we first transform @{text ind} into the object logic and fix the schematic variables.  |    111   we first transform \<open>ind\<close> into the object logic and fix the schematic variables.  | 
|    115   Hence we have to prove a formula of the form |    112   Hence we have to prove a formula of the form | 
|    116  |    113  | 
|    117   @{text [display] "pred zs \<longrightarrow> orules[preds := Ps] \<longrightarrow> P zs"} |    114   @{text [display] "pred zs \<longrightarrow> orules[preds := Ps] \<longrightarrow> P zs"} | 
|    118  |    115  | 
|    119   If we assume @{text "pred zs"} and unfold its definition, then we have an |    116   If we assume \<open>pred zs\<close> and unfold its definition, then we have an | 
|    120   assumption |    117   assumption | 
|    121    |    118    | 
|    122   @{text [display] "\<forall>preds. orules \<longrightarrow> pred zs"}  |    119   @{text [display] "\<forall>preds. orules \<longrightarrow> pred zs"}  | 
|    123  |    120  | 
|    124   and must prove the goal |    121   and must prove the goal | 
|    125  |    122  | 
|    126   @{text [display] "orules[preds := Ps] \<longrightarrow> P zs"} |    123   @{text [display] "orules[preds := Ps] \<longrightarrow> P zs"} | 
|    127  |    124  | 
|    128   This can be done by instantiating the @{text "\<forall>preds"}-quantification  |    125   This can be done by instantiating the \<open>\<forall>preds\<close>-quantification  | 
|    129   with the @{text "Ps"}. Then we are done since we are left with a simple  |    126   with the \<open>Ps\<close>. Then we are done since we are left with a simple  | 
|    130   identity. |    127   identity. | 
|    131    |    128    | 
|    132   Although the user declares the introduction rules @{text rules}, they must  |    129   Although the user declares the introduction rules \<open>rules\<close>, they must  | 
|    133   also be derived from the @{text defs}. These derivations are a bit involved.  |    130   also be derived from the \<open>defs\<close>. These derivations are a bit involved.  | 
|    134   Assuming we want to prove the introduction rule  |    131   Assuming we want to prove the introduction rule  | 
|    135  |    132  | 
|    136   @{text [display] "\<And>xs. As \<Longrightarrow> (\<And>ys. Bs \<Longrightarrow> pred ss)\<^sup>* \<Longrightarrow> pred ts"} |    133   @{text [display] "\<And>xs. As \<Longrightarrow> (\<And>ys. Bs \<Longrightarrow> pred ss)\<^sup>* \<Longrightarrow> pred ts"} | 
|    137  |    134  | 
|    138   then we have assumptions of the form |    135   then we have assumptions of the form | 
|    139  |    136  | 
|    140   \begin{isabelle} |    137   \begin{isabelle} | 
|    141   (i)~~@{text "As"}\\ |    138   (i)~~\<open>As\<close>\\ | 
|    142   (ii)~@{text "(\<And>ys. Bs \<Longrightarrow> pred ss)\<^sup>*"} |    139   (ii)~\<open>(\<And>ys. Bs \<Longrightarrow> pred ss)\<^sup>*\<close> | 
|    143   \end{isabelle} |    140   \end{isabelle} | 
|    144  |    141  | 
|    145   and must show the goal |    142   and must show the goal | 
|    146  |    143  | 
|    147   @{text [display] "pred ts"} |    144   @{text [display] "pred ts"} | 
|    148    |    145    | 
|    149   If we now unfold the definitions for the @{text preds}, we have assumptions |    146   If we now unfold the definitions for the \<open>preds\<close>, we have assumptions | 
|    150  |    147  | 
|    151   \begin{isabelle} |    148   \begin{isabelle} | 
|    152   (i)~~~@{text "As"}\\ |    149   (i)~~~\<open>As\<close>\\ | 
|    153   (ii)~~@{text "(\<And>ys. Bs \<Longrightarrow> \<forall>preds. orules \<longrightarrow> pred ss)\<^sup>*"}\\ |    150   (ii)~~\<open>(\<And>ys. Bs \<Longrightarrow> \<forall>preds. orules \<longrightarrow> pred ss)\<^sup>*\<close>\\ | 
|    154   (iii)~@{text "orules"} |    151   (iii)~\<open>orules\<close> | 
|    155   \end{isabelle} |    152   \end{isabelle} | 
|    156  |    153  | 
|    157   and need to show |    154   and need to show | 
|    158  |    155  | 
|    159   @{text [display] "pred ts"} |    156   @{text [display] "pred ts"} | 
|    160  |    157  | 
|    161   In the last step we removed some quantifiers and moved the precondition @{text "orules"}   |    158   In the last step we removed some quantifiers and moved the precondition \<open>orules\<close>   | 
|    162   into the assumption. The @{text "orules"} stand for all introduction rules that are given  |    159   into the assumption. The \<open>orules\<close> stand for all introduction rules that are given  | 
|    163   by the user. We apply the @{text orule} that corresponds to introduction rule we are  |    160   by the user. We apply the \<open>orule\<close> that corresponds to introduction rule we are  | 
|    164   proving. After transforming the object connectives into meta-connectives, this introduction  |    161   proving. After transforming the object connectives into meta-connectives, this introduction  | 
|    165   rule must necessarily be of the form  |    162   rule must necessarily be of the form  | 
|    166  |    163  | 
|    167   @{text [display] "As \<Longrightarrow> (\<And>ys. Bs \<Longrightarrow> pred ss)\<^sup>* \<Longrightarrow> pred ts"} |    164   @{text [display] "As \<Longrightarrow> (\<And>ys. Bs \<Longrightarrow> pred ss)\<^sup>* \<Longrightarrow> pred ts"} | 
|    168  |    165  | 
|    169   When we apply this rule we end up in the goal state where we have to prove |    166   When we apply this rule we end up in the goal state where we have to prove | 
|    170   goals of the form |    167   goals of the form | 
|    171  |    168  | 
|    172   \begin{isabelle} |    169   \begin{isabelle} | 
|    173   (a)~@{text "As"}\\ |    170   (a)~\<open>As\<close>\\ | 
|    174   (b)~@{text "(\<And>ys. Bs \<Longrightarrow> pred ss)\<^sup>*"} |    171   (b)~\<open>(\<And>ys. Bs \<Longrightarrow> pred ss)\<^sup>*\<close> | 
|    175   \end{isabelle} |    172   \end{isabelle} | 
|    176  |    173  | 
|    177   We can immediately discharge the goals @{text "As"} using the assumptions in  |    174   We can immediately discharge the goals \<open>As\<close> using the assumptions in  | 
|    178   @{text "(i)"}. The goals in @{text "(b)"} can be discharged as follows: we  |    175   \<open>(i)\<close>. The goals in \<open>(b)\<close> can be discharged as follows: we  | 
|    179   assume the @{text "Bs"} and prove @{text "pred ss"}. For this we resolve the  |    176   assume the \<open>Bs\<close> and prove \<open>pred ss\<close>. For this we resolve the  | 
|    180   @{text "Bs"}  with the assumptions in @{text "(ii)"}. This gives us the  |    177   \<open>Bs\<close>  with the assumptions in \<open>(ii)\<close>. This gives us the  | 
|    181   assumptions |    178   assumptions | 
|    182  |    179  | 
|    183   @{text [display] "(\<forall>preds. orules \<longrightarrow> pred ss)\<^sup>*"} |    180   @{text [display] "(\<forall>preds. orules \<longrightarrow> pred ss)\<^sup>*"} | 
|    184  |    181  | 
|    185   Instantiating the universal quantifiers and then resolving with the assumptions  |    182   Instantiating the universal quantifiers and then resolving with the assumptions  | 
|    186   in @{text "(iii)"} gives us @{text "pred ss"}, which is the goal we are after. |    183   in \<open>(iii)\<close> gives us \<open>pred ss\<close>, which is the goal we are after. | 
|    187   This completes the proof for introduction rules. |    184   This completes the proof for introduction rules. | 
|    188  |    185  | 
|    189   What remains is to implement in Isabelle the reasoning outlined in this section.  |    186   What remains is to implement in Isabelle the reasoning outlined in this section.  | 
|    190   We will describe the code in the next section. For building testcases, we use the shorthands for  |    187   We will describe the code in the next section. For building testcases, we use the shorthands for  | 
|    191   @{text "even/odd"}, @{term "fresh"} and @{term "accpart"} |    188   \<open>even/odd\<close>, @{term "fresh"} and @{term "accpart"} | 
|    192   defined in Figure~\ref{fig:shorthands}. |    189   defined in Figure~\ref{fig:shorthands}. | 
|    193 *} |    190 \<close> | 
|    194  |    191  | 
|    195  |    192  | 
|    196 text_raw{* |    193 text_raw\<open> | 
|    197 \begin{figure}[p] |    194 \begin{figure}[p] | 
|    198 \begin{minipage}{\textwidth} |    195 \begin{minipage}{\textwidth} | 
|    199 \begin{isabelle}*}   |    196 \begin{isabelle}\<close>   | 
|    200 ML %grayML{*(* even-odd example *) |    197 ML %grayML\<open>(* even-odd example *) | 
|    201 val eo_defs = [@{thm even_def}, @{thm odd_def}] |    198 val eo_defs = [@{thm even_def}, @{thm odd_def}] | 
|    202  |    199  | 
|    203 val eo_rules =   |    200 val eo_rules =   | 
|    204   [@{prop "even 0"}, |    201   [@{prop "even 0"}, | 
|    205    @{prop "\<And>n. odd n \<Longrightarrow> even (Suc n)"}, |    202    @{prop "\<And>n. odd n \<Longrightarrow> even (Suc n)"}, |