src/Doc/Prog_Prove/Logic.thy
changeset 69505 cc2d676d5395
parent 68800 d4bad1efa268
child 69597 ff784d5a5bfb
equal deleted inserted replaced
69504:bda7527ccf05 69505:cc2d676d5395
    11 provides the standard logical constructs, in decreasing order of precedence:
    11 provides the standard logical constructs, in decreasing order of precedence:
    12 \[
    12 \[
    13 \begin{array}{rcl}
    13 \begin{array}{rcl}
    14 
    14 
    15 \mathit{form} & ::= &
    15 \mathit{form} & ::= &
    16   @{text"(form)"} ~\mid~
    16   \<open>(form)\<close> ~\mid~
    17   @{const True} ~\mid~
    17   @{const True} ~\mid~
    18   @{const False} ~\mid~
    18   @{const False} ~\mid~
    19   @{prop "term = term"}\\
    19   @{prop "term = term"}\\
    20  &\mid& @{prop"\<not> form"}\index{$HOL4@\isasymnot} ~\mid~
    20  &\mid& @{prop"\<not> form"}\index{$HOL4@\isasymnot} ~\mid~
    21   @{prop "form \<and> form"}\index{$HOL0@\isasymand} ~\mid~
    21   @{prop "form \<and> form"}\index{$HOL0@\isasymand} ~\mid~
    23   @{prop "form \<longrightarrow> form"}\index{$HOL2@\isasymlongrightarrow}\\
    23   @{prop "form \<longrightarrow> form"}\index{$HOL2@\isasymlongrightarrow}\\
    24  &\mid& @{prop"\<forall>x. form"}\index{$HOL6@\isasymforall} ~\mid~  @{prop"\<exists>x. form"}\index{$HOL7@\isasymexists}
    24  &\mid& @{prop"\<forall>x. form"}\index{$HOL6@\isasymforall} ~\mid~  @{prop"\<exists>x. form"}\index{$HOL7@\isasymexists}
    25 \end{array}
    25 \end{array}
    26 \]
    26 \]
    27 Terms are the ones we have seen all along, built from constants, variables,
    27 Terms are the ones we have seen all along, built from constants, variables,
    28 function application and @{text"\<lambda>"}-abstraction, including all the syntactic
    28 function application and \<open>\<lambda>\<close>-abstraction, including all the syntactic
    29 sugar like infix symbols, @{text "if"}, @{text "case"}, etc.
    29 sugar like infix symbols, \<open>if\<close>, \<open>case\<close>, etc.
    30 \begin{warn}
    30 \begin{warn}
    31 Remember that formulas are simply terms of type @{text bool}. Hence
    31 Remember that formulas are simply terms of type \<open>bool\<close>. Hence
    32 @{text "="} also works for formulas. Beware that @{text"="} has a higher
    32 \<open>=\<close> also works for formulas. Beware that \<open>=\<close> has a higher
    33 precedence than the other logical operators. Hence @{prop"s = t \<and> A"} means
    33 precedence than the other logical operators. Hence @{prop"s = t \<and> A"} means
    34 @{text"(s = t) \<and> A"}, and @{prop"A\<and>B = B\<and>A"} means @{text"A \<and> (B = B) \<and> A"}.
    34 \<open>(s = t) \<and> A\<close>, and @{prop"A\<and>B = B\<and>A"} means \<open>A \<and> (B = B) \<and> A\<close>.
    35 Logical equivalence can also be written with
    35 Logical equivalence can also be written with
    36 @{text "\<longleftrightarrow>"} instead of @{text"="}, where @{text"\<longleftrightarrow>"} has the same low
    36 \<open>\<longleftrightarrow>\<close> instead of \<open>=\<close>, where \<open>\<longleftrightarrow>\<close> has the same low
    37 precedence as @{text"\<longrightarrow>"}. Hence @{text"A \<and> B \<longleftrightarrow> B \<and> A"} really means
    37 precedence as \<open>\<longrightarrow>\<close>. Hence \<open>A \<and> B \<longleftrightarrow> B \<and> A\<close> really means
    38 @{text"(A \<and> B) \<longleftrightarrow> (B \<and> A)"}.
    38 \<open>(A \<and> B) \<longleftrightarrow> (B \<and> A)\<close>.
    39 \end{warn}
    39 \end{warn}
    40 \begin{warn}
    40 \begin{warn}
    41 Quantifiers need to be enclosed in parentheses if they are nested within
    41 Quantifiers need to be enclosed in parentheses if they are nested within
    42 other constructs (just like @{text "if"}, @{text case} and @{text let}).
    42 other constructs (just like \<open>if\<close>, \<open>case\<close> and \<open>let\<close>).
    43 \end{warn}
    43 \end{warn}
    44 The most frequent logical symbols and their ASCII representations are shown
    44 The most frequent logical symbols and their ASCII representations are shown
    45 in Fig.~\ref{fig:log-symbols}.
    45 in Fig.~\ref{fig:log-symbols}.
    46 \begin{figure}
    46 \begin{figure}
    47 \begin{center}
    47 \begin{center}
    48 \begin{tabular}{l@ {\qquad}l@ {\qquad}l}
    48 \begin{tabular}{l@ {\qquad}l@ {\qquad}l}
    49 @{text "\<forall>"} & \xsymbol{forall} & \texttt{ALL}\\
    49 \<open>\<forall>\<close> & \xsymbol{forall} & \texttt{ALL}\\
    50 @{text "\<exists>"} & \xsymbol{exists} & \texttt{EX}\\
    50 \<open>\<exists>\<close> & \xsymbol{exists} & \texttt{EX}\\
    51 @{text "\<lambda>"} & \xsymbol{lambda} & \texttt{\%}\\
    51 \<open>\<lambda>\<close> & \xsymbol{lambda} & \texttt{\%}\\
    52 @{text "\<longrightarrow>"} & \texttt{-{}->}\\
    52 \<open>\<longrightarrow>\<close> & \texttt{-{}->}\\
    53 @{text "\<longleftrightarrow>"} & \texttt{<->}\\
    53 \<open>\<longleftrightarrow>\<close> & \texttt{<->}\\
    54 @{text "\<and>"} & \texttt{/\char`\\} & \texttt{\&}\\
    54 \<open>\<and>\<close> & \texttt{/\char`\\} & \texttt{\&}\\
    55 @{text "\<or>"} & \texttt{\char`\\/} & \texttt{|}\\
    55 \<open>\<or>\<close> & \texttt{\char`\\/} & \texttt{|}\\
    56 @{text "\<not>"} & \xsymbol{not} & \texttt{\char`~}\\
    56 \<open>\<not>\<close> & \xsymbol{not} & \texttt{\char`~}\\
    57 @{text "\<noteq>"} & \xsymbol{noteq} & \texttt{\char`~=}
    57 \<open>\<noteq>\<close> & \xsymbol{noteq} & \texttt{\char`~=}
    58 \end{tabular}
    58 \end{tabular}
    59 \end{center}
    59 \end{center}
    60 \caption{Logical symbols and their ASCII forms}
    60 \caption{Logical symbols and their ASCII forms}
    61 \label{fig:log-symbols}
    61 \label{fig:log-symbols}
    62 \end{figure}
    62 \end{figure}
    66 depends on the interface. The ASCII forms \texttt{/\char`\\} and
    66 depends on the interface. The ASCII forms \texttt{/\char`\\} and
    67 \texttt{\char`\\/}
    67 \texttt{\char`\\/}
    68 are special in that they are merely keyboard shortcuts for the interface and
    68 are special in that they are merely keyboard shortcuts for the interface and
    69 not logical symbols by themselves.
    69 not logical symbols by themselves.
    70 \begin{warn}
    70 \begin{warn}
    71 The implication @{text"\<Longrightarrow>"} is part of the Isabelle framework. It structures
    71 The implication \<open>\<Longrightarrow>\<close> is part of the Isabelle framework. It structures
    72 theorems and proof states, separating assumptions from conclusions.
    72 theorems and proof states, separating assumptions from conclusions.
    73 The implication @{text"\<longrightarrow>"} is part of the logic HOL and can occur inside the
    73 The implication \<open>\<longrightarrow>\<close> is part of the logic HOL and can occur inside the
    74 formulas that make up the assumptions and conclusion.
    74 formulas that make up the assumptions and conclusion.
    75 Theorems should be of the form @{text"\<lbrakk> A\<^sub>1; \<dots>; A\<^sub>n \<rbrakk> \<Longrightarrow> A"},
    75 Theorems should be of the form \<open>\<lbrakk> A\<^sub>1; \<dots>; A\<^sub>n \<rbrakk> \<Longrightarrow> A\<close>,
    76 not @{text"A\<^sub>1 \<and> \<dots> \<and> A\<^sub>n \<longrightarrow> A"}. Both are logically equivalent
    76 not \<open>A\<^sub>1 \<and> \<dots> \<and> A\<^sub>n \<longrightarrow> A\<close>. Both are logically equivalent
    77 but the first one works better when using the theorem in further proofs.
    77 but the first one works better when using the theorem in further proofs.
    78 \end{warn}
    78 \end{warn}
    79 
    79 
    80 \section{Sets}
    80 \section{Sets}
    81 \label{sec:Sets}
    81 \label{sec:Sets}
    82 
    82 
    83 Sets of elements of type @{typ 'a} have type @{typ"'a set"}\index{set@@{text set}}.
    83 Sets of elements of type @{typ 'a} have type @{typ"'a set"}\index{set@\<open>set\<close>}.
    84 They can be finite or infinite. Sets come with the usual notation:
    84 They can be finite or infinite. Sets come with the usual notation:
    85 \begin{itemize}
    85 \begin{itemize}
    86 \item \indexed{@{term"{}"}}{$IMP042},\quad @{text"{e\<^sub>1,\<dots>,e\<^sub>n}"}
    86 \item \indexed{@{term"{}"}}{$IMP042},\quad \<open>{e\<^sub>1,\<dots>,e\<^sub>n}\<close>
    87 \item @{prop"e \<in> A"}\index{$HOLSet0@\isasymin},\quad @{prop"A \<subseteq> B"}\index{$HOLSet2@\isasymsubseteq}
    87 \item @{prop"e \<in> A"}\index{$HOLSet0@\isasymin},\quad @{prop"A \<subseteq> B"}\index{$HOLSet2@\isasymsubseteq}
    88 \item @{term"A \<union> B"}\index{$HOLSet4@\isasymunion},\quad @{term"A \<inter> B"}\index{$HOLSet5@\isasyminter},\quad @{term"A - B"},\quad @{term"-A"}
    88 \item @{term"A \<union> B"}\index{$HOLSet4@\isasymunion},\quad @{term"A \<inter> B"}\index{$HOLSet5@\isasyminter},\quad @{term"A - B"},\quad @{term"-A"}
    89 \end{itemize}
    89 \end{itemize}
    90 (where @{term"A-B"} and @{text"-A"} are set difference and complement)
    90 (where @{term"A-B"} and \<open>-A\<close> are set difference and complement)
    91 and much more. @{const UNIV} is the set of all elements of some type.
    91 and much more. @{const UNIV} is the set of all elements of some type.
    92 Set comprehension\index{set comprehension} is written
    92 Set comprehension\index{set comprehension} is written
    93 @{term"{x. P}"}\index{$IMP042@@{term"{x. P}"}} rather than @{text"{x | P}"}.
    93 @{term"{x. P}"}\index{$IMP042@@{term"{x. P}"}} rather than \<open>{x | P}\<close>.
    94 \begin{warn}
    94 \begin{warn}
    95 In @{term"{x. P}"} the @{text x} must be a variable. Set comprehension
    95 In @{term"{x. P}"} the \<open>x\<close> must be a variable. Set comprehension
    96 involving a proper term @{text t} must be written
    96 involving a proper term \<open>t\<close> must be written
    97 \noquotes{@{term[source] "{t | x y. P}"}}\index{$IMP042@@{text"{t |x. P}"}},
    97 \noquotes{@{term[source] "{t | x y. P}"}}\index{$IMP042@\<open>{t |x. P}\<close>},
    98 where @{text "x y"} are those free variables in @{text t}
    98 where \<open>x y\<close> are those free variables in \<open>t\<close>
    99 that occur in @{text P}.
    99 that occur in \<open>P\<close>.
   100 This is just a shorthand for @{term"{v. \<exists>x y. v = t \<and> P}"}, where
   100 This is just a shorthand for @{term"{v. \<exists>x y. v = t \<and> P}"}, where
   101 @{text v} is a new variable. For example, @{term"{x+y|x. x \<in> A}"}
   101 \<open>v\<close> is a new variable. For example, @{term"{x+y|x. x \<in> A}"}
   102 is short for \noquotes{@{term[source]"{v. \<exists>x. v = x+y \<and> x \<in> A}"}}.
   102 is short for \noquotes{@{term[source]"{v. \<exists>x. v = x+y \<and> x \<in> A}"}}.
   103 \end{warn}
   103 \end{warn}
   104 
   104 
   105 Here are the ASCII representations of the mathematical symbols:
   105 Here are the ASCII representations of the mathematical symbols:
   106 \begin{center}
   106 \begin{center}
   107 \begin{tabular}{l@ {\quad}l@ {\quad}l}
   107 \begin{tabular}{l@ {\quad}l@ {\quad}l}
   108 @{text "\<in>"} & \texttt{\char`\\\char`\<in>} & \texttt{:}\\
   108 \<open>\<in>\<close> & \texttt{\char`\\\char`\<in>} & \texttt{:}\\
   109 @{text "\<subseteq>"} & \texttt{\char`\\\char`\<subseteq>} & \texttt{<=}\\
   109 \<open>\<subseteq>\<close> & \texttt{\char`\\\char`\<subseteq>} & \texttt{<=}\\
   110 @{text "\<union>"} & \texttt{\char`\\\char`\<union>} & \texttt{Un}\\
   110 \<open>\<union>\<close> & \texttt{\char`\\\char`\<union>} & \texttt{Un}\\
   111 @{text "\<inter>"} & \texttt{\char`\\\char`\<inter>} & \texttt{Int}
   111 \<open>\<inter>\<close> & \texttt{\char`\\\char`\<inter>} & \texttt{Int}
   112 \end{tabular}
   112 \end{tabular}
   113 \end{center}
   113 \end{center}
   114 Sets also allow bounded quantifications @{prop"\<forall>x \<in> A. P"} and
   114 Sets also allow bounded quantifications @{prop"\<forall>x \<in> A. P"} and
   115 @{prop"\<exists>x \<in> A. P"}.
   115 @{prop"\<exists>x \<in> A. P"}.
   116 
   116 
   117 For the more ambitious, there are also @{text"\<Union>"}\index{$HOLSet6@\isasymUnion}
   117 For the more ambitious, there are also \<open>\<Union>\<close>\index{$HOLSet6@\isasymUnion}
   118 and @{text"\<Inter>"}\index{$HOLSet7@\isasymInter}:
   118 and \<open>\<Inter>\<close>\index{$HOLSet7@\isasymInter}:
   119 \begin{center}
   119 \begin{center}
   120 @{thm Union_eq} \qquad @{thm Inter_eq}
   120 @{thm Union_eq} \qquad @{thm Inter_eq}
   121 \end{center}
   121 \end{center}
   122 The ASCII forms of @{text"\<Union>"} are \texttt{\char`\\\char`\<Union>} and \texttt{Union},
   122 The ASCII forms of \<open>\<Union>\<close> are \texttt{\char`\\\char`\<Union>} and \texttt{Union},
   123 those of @{text"\<Inter>"} are \texttt{\char`\\\char`\<Inter>} and \texttt{Inter}.
   123 those of \<open>\<Inter>\<close> are \texttt{\char`\\\char`\<Inter>} and \texttt{Inter}.
   124 There are also indexed unions and intersections:
   124 There are also indexed unions and intersections:
   125 \begin{center}
   125 \begin{center}
   126 @{thm[eta_contract=false] UNION_eq} \\ @{thm[eta_contract=false] INTER_eq}
   126 @{thm[eta_contract=false] UNION_eq} \\ @{thm[eta_contract=false] INTER_eq}
   127 \end{center}
   127 \end{center}
   128 The ASCII forms are \ \texttt{UN x:A.~B} \ and \ \texttt{INT x:A. B} \
   128 The ASCII forms are \ \texttt{UN x:A.~B} \ and \ \texttt{INT x:A. B} \
   133 \begin{center}
   133 \begin{center}
   134 \begin{tabular}{l@ {\quad}l}
   134 \begin{tabular}{l@ {\quad}l}
   135 @{const_typ set}\index{set@@{const set}} & converts a list to the set of its elements\\
   135 @{const_typ set}\index{set@@{const set}} & converts a list to the set of its elements\\
   136 @{const_typ finite}\index{finite@@{const finite}} & is true iff its argument is finite\\
   136 @{const_typ finite}\index{finite@@{const finite}} & is true iff its argument is finite\\
   137 \noquotes{@{term[source] "card :: 'a set \<Rightarrow> nat"}}\index{card@@{const card}} & is the cardinality of a finite set\\
   137 \noquotes{@{term[source] "card :: 'a set \<Rightarrow> nat"}}\index{card@@{const card}} & is the cardinality of a finite set\\
   138  & and is @{text 0} for all infinite sets\\
   138  & and is \<open>0\<close> for all infinite sets\\
   139 @{thm image_def}\index{$IMP042@@{term"f ` A"}} & is the image of a function over a set
   139 @{thm image_def}\index{$IMP042@@{term"f ` A"}} & is the image of a function over a set
   140 \end{tabular}
   140 \end{tabular}
   141 \end{center}
   141 \end{center}
   142 See @{cite "Nipkow-Main"} for the wealth of further predefined functions in theory
   142 See @{cite "Nipkow-Main"} for the wealth of further predefined functions in theory
   143 @{theory Main}.
   143 @{theory Main}.
   150 \<close>
   150 \<close>
   151 
   151 
   152 datatype 'a tree = Tip | Node "'a tree" 'a "'a tree"
   152 datatype 'a tree = Tip | Node "'a tree" 'a "'a tree"
   153 
   153 
   154 text\<open>
   154 text\<open>
   155 Define a function @{text "set ::"} @{typ "'a tree \<Rightarrow> 'a set"}
   155 Define a function \<open>set ::\<close> @{typ "'a tree \<Rightarrow> 'a set"}
   156 that returns the elements in a tree and a function
   156 that returns the elements in a tree and a function
   157 @{text "ord ::"} @{typ "int tree \<Rightarrow> bool"}
   157 \<open>ord ::\<close> @{typ "int tree \<Rightarrow> bool"}
   158 that tests if an @{typ "int tree"} is ordered.
   158 that tests if an @{typ "int tree"} is ordered.
   159 
   159 
   160 Define a function @{text ins} that inserts an element into an ordered @{typ "int tree"}
   160 Define a function \<open>ins\<close> that inserts an element into an ordered @{typ "int tree"}
   161 while maintaining the order of the tree. If the element is already in the tree, the
   161 while maintaining the order of the tree. If the element is already in the tree, the
   162 same tree should be returned. Prove correctness of @{text ins}:
   162 same tree should be returned. Prove correctness of \<open>ins\<close>:
   163 @{prop "set(ins x t) = {x} \<union> set t"} and @{prop "ord t \<Longrightarrow> ord(ins i t)"}.
   163 @{prop "set(ins x t) = {x} \<union> set t"} and @{prop "ord t \<Longrightarrow> ord(ins i t)"}.
   164 \endexercise
   164 \endexercise
   165 
   165 
   166 
   166 
   167 \section{Proof Automation}
   167 \section{Proof Automation}
   168 
   168 
   169 So far we have only seen @{text simp} and \indexed{@{text auto}}{auto}: Both perform
   169 So far we have only seen \<open>simp\<close> and \indexed{\<open>auto\<close>}{auto}: Both perform
   170 rewriting, both can also prove linear arithmetic facts (no multiplication),
   170 rewriting, both can also prove linear arithmetic facts (no multiplication),
   171 and @{text auto} is also able to prove simple logical or set-theoretic goals:
   171 and \<open>auto\<close> is also able to prove simple logical or set-theoretic goals:
   172 \<close>
   172 \<close>
   173 
   173 
   174 lemma "\<forall>x. \<exists>y. x = y"
   174 lemma "\<forall>x. \<exists>y. x = y"
   175 by auto
   175 by auto
   176 
   176 
   184 is short for
   184 is short for
   185 \begin{quote}
   185 \begin{quote}
   186 \isacom{apply} \textit{proof-method}\\
   186 \isacom{apply} \textit{proof-method}\\
   187 \isacom{done}
   187 \isacom{done}
   188 \end{quote}
   188 \end{quote}
   189 The key characteristics of both @{text simp} and @{text auto} are
   189 The key characteristics of both \<open>simp\<close> and \<open>auto\<close> are
   190 \begin{itemize}
   190 \begin{itemize}
   191 \item They show you where they got stuck, giving you an idea how to continue.
   191 \item They show you where they got stuck, giving you an idea how to continue.
   192 \item They perform the obvious steps but are highly incomplete.
   192 \item They perform the obvious steps but are highly incomplete.
   193 \end{itemize}
   193 \end{itemize}
   194 A proof method is \conceptnoidx{complete} if it can prove all true formulas.
   194 A proof method is \conceptnoidx{complete} if it can prove all true formulas.
   195 There is no complete proof method for HOL, not even in theory.
   195 There is no complete proof method for HOL, not even in theory.
   196 Hence all our proof methods only differ in how incomplete they are.
   196 Hence all our proof methods only differ in how incomplete they are.
   197 
   197 
   198 A proof method that is still incomplete but tries harder than @{text auto} is
   198 A proof method that is still incomplete but tries harder than \<open>auto\<close> is
   199 \indexed{@{text fastforce}}{fastforce}.  It either succeeds or fails, it acts on the first
   199 \indexed{\<open>fastforce\<close>}{fastforce}.  It either succeeds or fails, it acts on the first
   200 subgoal only, and it can be modified like @{text auto}, e.g.,
   200 subgoal only, and it can be modified like \<open>auto\<close>, e.g.,
   201 with @{text "simp add"}. Here is a typical example of what @{text fastforce}
   201 with \<open>simp add\<close>. Here is a typical example of what \<open>fastforce\<close>
   202 can do:
   202 can do:
   203 \<close>
   203 \<close>
   204 
   204 
   205 lemma "\<lbrakk> \<forall>xs \<in> A. \<exists>ys. xs = ys @ ys;  us \<in> A \<rbrakk>
   205 lemma "\<lbrakk> \<forall>xs \<in> A. \<exists>ys. xs = ys @ ys;  us \<in> A \<rbrakk>
   206    \<Longrightarrow> \<exists>n. length us = n+n"
   206    \<Longrightarrow> \<exists>n. length us = n+n"
   207 by fastforce
   207 by fastforce
   208 
   208 
   209 text\<open>This lemma is out of reach for @{text auto} because of the
   209 text\<open>This lemma is out of reach for \<open>auto\<close> because of the
   210 quantifiers.  Even @{text fastforce} fails when the quantifier structure
   210 quantifiers.  Even \<open>fastforce\<close> fails when the quantifier structure
   211 becomes more complicated. In a few cases, its slow version @{text force}
   211 becomes more complicated. In a few cases, its slow version \<open>force\<close>
   212 succeeds where @{text fastforce} fails.
   212 succeeds where \<open>fastforce\<close> fails.
   213 
   213 
   214 The method of choice for complex logical goals is \indexed{@{text blast}}{blast}. In the
   214 The method of choice for complex logical goals is \indexed{\<open>blast\<close>}{blast}. In the
   215 following example, @{text T} and @{text A} are two binary predicates. It
   215 following example, \<open>T\<close> and \<open>A\<close> are two binary predicates. It
   216 is shown that if @{text T} is total, @{text A} is antisymmetric and @{text T} is
   216 is shown that if \<open>T\<close> is total, \<open>A\<close> is antisymmetric and \<open>T\<close> is
   217 a subset of @{text A}, then @{text A} is a subset of @{text T}:
   217 a subset of \<open>A\<close>, then \<open>A\<close> is a subset of \<open>T\<close>:
   218 \<close>
   218 \<close>
   219 
   219 
   220 lemma
   220 lemma
   221   "\<lbrakk> \<forall>x y. T x y \<or> T y x;
   221   "\<lbrakk> \<forall>x y. T x y \<or> T y x;
   222      \<forall>x y. A x y \<and> A y x \<longrightarrow> x = y;
   222      \<forall>x y. A x y \<and> A y x \<longrightarrow> x = y;
   224    \<Longrightarrow> \<forall>x y. A x y \<longrightarrow> T x y"
   224    \<Longrightarrow> \<forall>x y. A x y \<longrightarrow> T x y"
   225 by blast
   225 by blast
   226 
   226 
   227 text\<open>
   227 text\<open>
   228 We leave it to the reader to figure out why this lemma is true.
   228 We leave it to the reader to figure out why this lemma is true.
   229 Method @{text blast}
   229 Method \<open>blast\<close>
   230 \begin{itemize}
   230 \begin{itemize}
   231 \item is (in principle) a complete proof procedure for first-order formulas,
   231 \item is (in principle) a complete proof procedure for first-order formulas,
   232   a fragment of HOL. In practice there is a search bound.
   232   a fragment of HOL. In practice there is a search bound.
   233 \item does no rewriting and knows very little about equality.
   233 \item does no rewriting and knows very little about equality.
   234 \item covers logic, sets and relations.
   234 \item covers logic, sets and relations.
   255 by (metis append_eq_conv_conj)
   255 by (metis append_eq_conv_conj)
   256 
   256 
   257 text\<open>We do not explain how the proof was found but what this command
   257 text\<open>We do not explain how the proof was found but what this command
   258 means. For a start, Isabelle does not trust external tools (and in particular
   258 means. For a start, Isabelle does not trust external tools (and in particular
   259 not the translations from Isabelle's logic to those tools!)
   259 not the translations from Isabelle's logic to those tools!)
   260 and insists on a proof that it can check. This is what \indexed{@{text metis}}{metis} does.
   260 and insists on a proof that it can check. This is what \indexed{\<open>metis\<close>}{metis} does.
   261 It is given a list of lemmas and tries to find a proof using just those lemmas
   261 It is given a list of lemmas and tries to find a proof using just those lemmas
   262 (and pure logic). In contrast to using @{text simp} and friends who know a lot of
   262 (and pure logic). In contrast to using \<open>simp\<close> and friends who know a lot of
   263 lemmas already, using @{text metis} manually is tedious because one has
   263 lemmas already, using \<open>metis\<close> manually is tedious because one has
   264 to find all the relevant lemmas first. But that is precisely what
   264 to find all the relevant lemmas first. But that is precisely what
   265 \isacom{sledgehammer} does for us.
   265 \isacom{sledgehammer} does for us.
   266 In this case lemma @{thm[source]append_eq_conv_conj} alone suffices:
   266 In this case lemma @{thm[source]append_eq_conv_conj} alone suffices:
   267 @{thm[display] append_eq_conv_conj}
   267 @{thm[display] append_eq_conv_conj}
   268 We leave it to the reader to figure out why this lemma suffices to prove
   268 We leave it to the reader to figure out why this lemma suffices to prove
   272 substitute arbitrary values for the free variables in a lemma.
   272 substitute arbitrary values for the free variables in a lemma.
   273 
   273 
   274 Just as for the other proof methods we have seen, there is no guarantee that
   274 Just as for the other proof methods we have seen, there is no guarantee that
   275 \isacom{sledgehammer} will find a proof if it exists. Nor is
   275 \isacom{sledgehammer} will find a proof if it exists. Nor is
   276 \isacom{sledgehammer} superior to the other proof methods.  They are
   276 \isacom{sledgehammer} superior to the other proof methods.  They are
   277 incomparable. Therefore it is recommended to apply @{text simp} or @{text
   277 incomparable. Therefore it is recommended to apply \<open>simp\<close> or \<open>auto\<close> before invoking \isacom{sledgehammer} on what is left.
   278 auto} before invoking \isacom{sledgehammer} on what is left.
       
   279 
   278 
   280 \subsection{Arithmetic}
   279 \subsection{Arithmetic}
   281 
   280 
   282 By arithmetic formulas we mean formulas involving variables, numbers, @{text
   281 By arithmetic formulas we mean formulas involving variables, numbers, \<open>+\<close>, \<open>-\<close>, \<open>=\<close>, \<open><\<close>, \<open>\<le>\<close> and the usual logical
   283 "+"}, @{text"-"}, @{text "="}, @{text "<"}, @{text "\<le>"} and the usual logical
   282 connectives \<open>\<not>\<close>, \<open>\<and>\<close>, \<open>\<or>\<close>, \<open>\<longrightarrow>\<close>,
   284 connectives @{text"\<not>"}, @{text"\<and>"}, @{text"\<or>"}, @{text"\<longrightarrow>"},
   283 \<open>\<longleftrightarrow>\<close>. Strictly speaking, this is known as \concept{linear arithmetic}
   285 @{text"\<longleftrightarrow>"}. Strictly speaking, this is known as \concept{linear arithmetic}
       
   286 because it does not involve multiplication, although multiplication with
   284 because it does not involve multiplication, although multiplication with
   287 numbers, e.g., @{text"2*n"}, is allowed. Such formulas can be proved by
   285 numbers, e.g., \<open>2*n\<close>, is allowed. Such formulas can be proved by
   288 \indexed{@{text arith}}{arith}:
   286 \indexed{\<open>arith\<close>}{arith}:
   289 \<close>
   287 \<close>
   290 
   288 
   291 lemma "\<lbrakk> (a::nat) \<le> x + b; 2*x < c \<rbrakk> \<Longrightarrow> 2*a + 1 \<le> 2*b + c"
   289 lemma "\<lbrakk> (a::nat) \<le> x + b; 2*x < c \<rbrakk> \<Longrightarrow> 2*a + 1 \<le> 2*b + c"
   292 by arith
   290 by arith
   293 
   291 
   294 text\<open>In fact, @{text auto} and @{text simp} can prove many linear
   292 text\<open>In fact, \<open>auto\<close> and \<open>simp\<close> can prove many linear
   295 arithmetic formulas already, like the one above, by calling a weak but fast
   293 arithmetic formulas already, like the one above, by calling a weak but fast
   296 version of @{text arith}. Hence it is usually not necessary to invoke
   294 version of \<open>arith\<close>. Hence it is usually not necessary to invoke
   297 @{text arith} explicitly.
   295 \<open>arith\<close> explicitly.
   298 
   296 
   299 The above example involves natural numbers, but integers (type @{typ int})
   297 The above example involves natural numbers, but integers (type @{typ int})
   300 and real numbers (type @{text real}) are supported as well. As are a number
   298 and real numbers (type \<open>real\<close>) are supported as well. As are a number
   301 of further operators like @{const min} and @{const max}. On @{typ nat} and
   299 of further operators like @{const min} and @{const max}. On @{typ nat} and
   302 @{typ int}, @{text arith} can even prove theorems with quantifiers in them,
   300 @{typ int}, \<open>arith\<close> can even prove theorems with quantifiers in them,
   303 but we will not enlarge on that here.
   301 but we will not enlarge on that here.
   304 
   302 
   305 
   303 
   306 \subsection{Trying Them All}
   304 \subsection{Trying Them All}
   307 
   305 
   311 \end{isabelle}
   309 \end{isabelle}
   312 There is also a lightweight variant \isacom{try0} that does not call
   310 There is also a lightweight variant \isacom{try0} that does not call
   313 sledgehammer. If desired, specific simplification and introduction rules
   311 sledgehammer. If desired, specific simplification and introduction rules
   314 can be added:
   312 can be added:
   315 \begin{isabelle}
   313 \begin{isabelle}
   316 \isacom{try0} @{text"simp: \<dots> intro: \<dots>"}
   314 \isacom{try0} \<open>simp: \<dots> intro: \<dots>\<close>
   317 \end{isabelle}
   315 \end{isabelle}
   318 
   316 
   319 \section{Single Step Proofs}
   317 \section{Single Step Proofs}
   320 
   318 
   321 Although automation is nice, it often fails, at least initially, and you need
   319 Although automation is nice, it often fails, at least initially, and you need
   322 to find out why. When @{text fastforce} or @{text blast} simply fail, you have
   320 to find out why. When \<open>fastforce\<close> or \<open>blast\<close> simply fail, you have
   323 no clue why. At this point, the stepwise
   321 no clue why. At this point, the stepwise
   324 application of proof rules may be necessary. For example, if @{text blast}
   322 application of proof rules may be necessary. For example, if \<open>blast\<close>
   325 fails on @{prop"A \<and> B"}, you want to attack the two
   323 fails on @{prop"A \<and> B"}, you want to attack the two
   326 conjuncts @{text A} and @{text B} separately. This can
   324 conjuncts \<open>A\<close> and \<open>B\<close> separately. This can
   327 be achieved by applying \emph{conjunction introduction}
   325 be achieved by applying \emph{conjunction introduction}
   328 \[ @{thm[mode=Rule,show_question_marks]conjI}\ @{text conjI}
   326 \[ @{thm[mode=Rule,show_question_marks]conjI}\ \<open>conjI\<close>
   329 \]
   327 \]
   330 to the proof state. We will now examine the details of this process.
   328 to the proof state. We will now examine the details of this process.
   331 
   329 
   332 \subsection{Instantiating Unknowns}
   330 \subsection{Instantiating Unknowns}
   333 
   331 
   334 We had briefly mentioned earlier that after proving some theorem,
   332 We had briefly mentioned earlier that after proving some theorem,
   335 Isabelle replaces all free variables @{text x} by so called \conceptidx{unknowns}{unknown}
   333 Isabelle replaces all free variables \<open>x\<close> by so called \conceptidx{unknowns}{unknown}
   336 @{text "?x"}. We can see this clearly in rule @{thm[source] conjI}.
   334 \<open>?x\<close>. We can see this clearly in rule @{thm[source] conjI}.
   337 These unknowns can later be instantiated explicitly or implicitly:
   335 These unknowns can later be instantiated explicitly or implicitly:
   338 \begin{itemize}
   336 \begin{itemize}
   339 \item By hand, using \indexed{@{text of}}{of}.
   337 \item By hand, using \indexed{\<open>of\<close>}{of}.
   340 The expression @{text"conjI[of \"a=b\" \"False\"]"}
   338 The expression \<open>conjI[of "a=b" "False"]\<close>
   341 instantiates the unknowns in @{thm[source] conjI} from left to right with the
   339 instantiates the unknowns in @{thm[source] conjI} from left to right with the
   342 two formulas @{text"a=b"} and @{text False}, yielding the rule
   340 two formulas \<open>a=b\<close> and \<open>False\<close>, yielding the rule
   343 @{thm[display,mode=Rule,margin=100]conjI[of "a=b" False]}
   341 @{thm[display,mode=Rule,margin=100]conjI[of "a=b" False]}
   344 
   342 
   345 In general, @{text"th[of string\<^sub>1 \<dots> string\<^sub>n]"} instantiates
   343 In general, \<open>th[of string\<^sub>1 \<dots> string\<^sub>n]\<close> instantiates
   346 the unknowns in the theorem @{text th} from left to right with the terms
   344 the unknowns in the theorem \<open>th\<close> from left to right with the terms
   347 @{text string\<^sub>1} to @{text string\<^sub>n}.
   345 \<open>string\<^sub>1\<close> to \<open>string\<^sub>n\<close>.
   348 
   346 
   349 \item By unification. \conceptidx{Unification}{unification} is the process of making two
   347 \item By unification. \conceptidx{Unification}{unification} is the process of making two
   350 terms syntactically equal by suitable instantiations of unknowns. For example,
   348 terms syntactically equal by suitable instantiations of unknowns. For example,
   351 unifying @{text"?P \<and> ?Q"} with \mbox{@{prop"a=b \<and> False"}} instantiates
   349 unifying \<open>?P \<and> ?Q\<close> with \mbox{@{prop"a=b \<and> False"}} instantiates
   352 @{text "?P"} with @{prop "a=b"} and @{text "?Q"} with @{prop False}.
   350 \<open>?P\<close> with @{prop "a=b"} and \<open>?Q\<close> with @{prop False}.
   353 \end{itemize}
   351 \end{itemize}
   354 We need not instantiate all unknowns. If we want to skip a particular one we
   352 We need not instantiate all unknowns. If we want to skip a particular one we
   355 can write @{text"_"} instead, for example @{text "conjI[of _ \"False\"]"}.
   353 can write \<open>_\<close> instead, for example \<open>conjI[of _ "False"]\<close>.
   356 Unknowns can also be instantiated by name using \indexed{@{text "where"}}{where}, for example
   354 Unknowns can also be instantiated by name using \indexed{\<open>where\<close>}{where}, for example
   357 @{text "conjI[where ?P = \"a=b\""} \isacom{and} @{text"?Q = \"False\"]"}.
   355 \<open>conjI[where ?P = "a=b"\<close> \isacom{and} \<open>?Q = "False"]\<close>.
   358 
   356 
   359 
   357 
   360 \subsection{Rule Application}
   358 \subsection{Rule Application}
   361 
   359 
   362 \conceptidx{Rule application}{rule application} means applying a rule backwards to a proof state.
   360 \conceptidx{Rule application}{rule application} means applying a rule backwards to a proof state.
   363 For example, applying rule @{thm[source]conjI} to a proof state
   361 For example, applying rule @{thm[source]conjI} to a proof state
   364 \begin{quote}
   362 \begin{quote}
   365 @{text"1.  \<dots>  \<Longrightarrow> A \<and> B"}
   363 \<open>1.  \<dots>  \<Longrightarrow> A \<and> B\<close>
   366 \end{quote}
   364 \end{quote}
   367 results in two subgoals, one for each premise of @{thm[source]conjI}:
   365 results in two subgoals, one for each premise of @{thm[source]conjI}:
   368 \begin{quote}
   366 \begin{quote}
   369 @{text"1.  \<dots>  \<Longrightarrow> A"}\\
   367 \<open>1.  \<dots>  \<Longrightarrow> A\<close>\\
   370 @{text"2.  \<dots>  \<Longrightarrow> B"}
   368 \<open>2.  \<dots>  \<Longrightarrow> B\<close>
   371 \end{quote}
   369 \end{quote}
   372 In general, the application of a rule @{text"\<lbrakk> A\<^sub>1; \<dots>; A\<^sub>n \<rbrakk> \<Longrightarrow> A"}
   370 In general, the application of a rule \<open>\<lbrakk> A\<^sub>1; \<dots>; A\<^sub>n \<rbrakk> \<Longrightarrow> A\<close>
   373 to a subgoal \mbox{@{text"\<dots> \<Longrightarrow> C"}} proceeds in two steps:
   371 to a subgoal \mbox{\<open>\<dots> \<Longrightarrow> C\<close>} proceeds in two steps:
   374 \begin{enumerate}
   372 \begin{enumerate}
   375 \item
   373 \item
   376 Unify @{text A} and @{text C}, thus instantiating the unknowns in the rule.
   374 Unify \<open>A\<close> and \<open>C\<close>, thus instantiating the unknowns in the rule.
   377 \item
   375 \item
   378 Replace the subgoal @{text C} with @{text n} new subgoals @{text"A\<^sub>1"} to @{text"A\<^sub>n"}.
   376 Replace the subgoal \<open>C\<close> with \<open>n\<close> new subgoals \<open>A\<^sub>1\<close> to \<open>A\<^sub>n\<close>.
   379 \end{enumerate}
   377 \end{enumerate}
   380 This is the command to apply rule @{text xyz}:
   378 This is the command to apply rule \<open>xyz\<close>:
   381 \begin{quote}
   379 \begin{quote}
   382 \isacom{apply}@{text"(rule xyz)"}\index{rule@@{text rule}}
   380 \isacom{apply}\<open>(rule xyz)\<close>\index{rule@\<open>rule\<close>}
   383 \end{quote}
   381 \end{quote}
   384 This is also called \concept{backchaining} with rule @{text xyz}.
   382 This is also called \concept{backchaining} with rule \<open>xyz\<close>.
   385 
   383 
   386 \subsection{Introduction Rules}
   384 \subsection{Introduction Rules}
   387 
   385 
   388 Conjunction introduction (@{thm[source] conjI}) is one example of a whole
   386 Conjunction introduction (@{thm[source] conjI}) is one example of a whole
   389 class of rules known as \conceptidx{introduction rules}{introduction rule}. They explain under which
   387 class of rules known as \conceptidx{introduction rules}{introduction rule}. They explain under which
   390 premises some logical construct can be introduced. Here are some further
   388 premises some logical construct can be introduced. Here are some further
   391 useful introduction rules:
   389 useful introduction rules:
   392 \[
   390 \[
   393 \inferrule*[right=\mbox{@{text impI}}]{\mbox{@{text"?P \<Longrightarrow> ?Q"}}}{\mbox{@{text"?P \<longrightarrow> ?Q"}}}
   391 \inferrule*[right=\mbox{\<open>impI\<close>}]{\mbox{\<open>?P \<Longrightarrow> ?Q\<close>}}{\mbox{\<open>?P \<longrightarrow> ?Q\<close>}}
   394 \qquad
   392 \qquad
   395 \inferrule*[right=\mbox{@{text allI}}]{\mbox{@{text"\<And>x. ?P x"}}}{\mbox{@{text"\<forall>x. ?P x"}}}
   393 \inferrule*[right=\mbox{\<open>allI\<close>}]{\mbox{\<open>\<And>x. ?P x\<close>}}{\mbox{\<open>\<forall>x. ?P x\<close>}}
   396 \]
   394 \]
   397 \[
   395 \[
   398 \inferrule*[right=\mbox{@{text iffI}}]{\mbox{@{text"?P \<Longrightarrow> ?Q"}} \\ \mbox{@{text"?Q \<Longrightarrow> ?P"}}}
   396 \inferrule*[right=\mbox{\<open>iffI\<close>}]{\mbox{\<open>?P \<Longrightarrow> ?Q\<close>} \\ \mbox{\<open>?Q \<Longrightarrow> ?P\<close>}}
   399   {\mbox{@{text"?P = ?Q"}}}
   397   {\mbox{\<open>?P = ?Q\<close>}}
   400 \]
   398 \]
   401 These rules are part of the logical system of \concept{natural deduction}
   399 These rules are part of the logical system of \concept{natural deduction}
   402 (e.g., @{cite HuthRyan}). Although we intentionally de-emphasize the basic rules
   400 (e.g., @{cite HuthRyan}). Although we intentionally de-emphasize the basic rules
   403 of logic in favour of automatic proof methods that allow you to take bigger
   401 of logic in favour of automatic proof methods that allow you to take bigger
   404 steps, these rules are helpful in locating where and why automation fails.
   402 steps, these rules are helpful in locating where and why automation fails.
   405 When applied backwards, these rules decompose the goal:
   403 When applied backwards, these rules decompose the goal:
   406 \begin{itemize}
   404 \begin{itemize}
   407 \item @{thm[source] conjI} and @{thm[source]iffI} split the goal into two subgoals,
   405 \item @{thm[source] conjI} and @{thm[source]iffI} split the goal into two subgoals,
   408 \item @{thm[source] impI} moves the left-hand side of a HOL implication into the list of assumptions,
   406 \item @{thm[source] impI} moves the left-hand side of a HOL implication into the list of assumptions,
   409 \item and @{thm[source] allI} removes a @{text "\<forall>"} by turning the quantified variable into a fixed local variable of the subgoal.
   407 \item and @{thm[source] allI} removes a \<open>\<forall>\<close> by turning the quantified variable into a fixed local variable of the subgoal.
   410 \end{itemize}
   408 \end{itemize}
   411 Isabelle knows about these and a number of other introduction rules.
   409 Isabelle knows about these and a number of other introduction rules.
   412 The command
   410 The command
   413 \begin{quote}
   411 \begin{quote}
   414 \isacom{apply} @{text rule}\index{rule@@{text rule}}
   412 \isacom{apply} \<open>rule\<close>\index{rule@\<open>rule\<close>}
   415 \end{quote}
   413 \end{quote}
   416 automatically selects the appropriate rule for the current subgoal.
   414 automatically selects the appropriate rule for the current subgoal.
   417 
   415 
   418 You can also turn your own theorems into introduction rules by giving them
   416 You can also turn your own theorems into introduction rules by giving them
   419 the \indexed{@{text"intro"}}{intro} attribute, analogous to the @{text simp} attribute.  In
   417 the \indexed{\<open>intro\<close>}{intro} attribute, analogous to the \<open>simp\<close> attribute.  In
   420 that case @{text blast}, @{text fastforce} and (to a limited extent) @{text
   418 that case \<open>blast\<close>, \<open>fastforce\<close> and (to a limited extent) \<open>auto\<close> will automatically backchain with those theorems. The \<open>intro\<close>
   421 auto} will automatically backchain with those theorems. The @{text intro}
       
   422 attribute should be used with care because it increases the search space and
   419 attribute should be used with care because it increases the search space and
   423 can lead to nontermination.  Sometimes it is better to use it only in
   420 can lead to nontermination.  Sometimes it is better to use it only in
   424 specific calls of @{text blast} and friends. For example,
   421 specific calls of \<open>blast\<close> and friends. For example,
   425 @{thm[source] le_trans}, transitivity of @{text"\<le>"} on type @{typ nat},
   422 @{thm[source] le_trans}, transitivity of \<open>\<le>\<close> on type @{typ nat},
   426 is not an introduction rule by default because of the disastrous effect
   423 is not an introduction rule by default because of the disastrous effect
   427 on the search space, but can be useful in specific situations:
   424 on the search space, but can be useful in specific situations:
   428 \<close>
   425 \<close>
   429 
   426 
   430 lemma "\<lbrakk> (a::nat) \<le> b; b \<le> c; c \<le> d; d \<le> e \<rbrakk> \<Longrightarrow> a \<le> e"
   427 lemma "\<lbrakk> (a::nat) \<le> b; b \<le> c; c \<le> d; d \<le> e \<rbrakk> \<Longrightarrow> a \<le> e"
   431 by(blast intro: le_trans)
   428 by(blast intro: le_trans)
   432 
   429 
   433 text\<open>
   430 text\<open>
   434 Of course this is just an example and could be proved by @{text arith}, too.
   431 Of course this is just an example and could be proved by \<open>arith\<close>, too.
   435 
   432 
   436 \subsection{Forward Proof}
   433 \subsection{Forward Proof}
   437 \label{sec:forward-proof}
   434 \label{sec:forward-proof}
   438 
   435 
   439 Forward proof means deriving new theorems from old theorems. We have already
   436 Forward proof means deriving new theorems from old theorems. We have already
   440 seen a very simple form of forward proof: the @{text of} operator for
   437 seen a very simple form of forward proof: the \<open>of\<close> operator for
   441 instantiating unknowns in a theorem. The big brother of @{text of} is
   438 instantiating unknowns in a theorem. The big brother of \<open>of\<close> is
   442 \indexed{@{text OF}}{OF} for applying one theorem to others. Given a theorem @{prop"A \<Longrightarrow> B"} called
   439 \indexed{\<open>OF\<close>}{OF} for applying one theorem to others. Given a theorem @{prop"A \<Longrightarrow> B"} called
   443 @{text r} and a theorem @{text A'} called @{text r'}, the theorem @{text
   440 \<open>r\<close> and a theorem \<open>A'\<close> called \<open>r'\<close>, the theorem \<open>r[OF r']\<close> is the result of applying \<open>r\<close> to \<open>r'\<close>, where \<open>r\<close> should be viewed as a function taking a theorem \<open>A\<close> and returning
   444 "r[OF r']"} is the result of applying @{text r} to @{text r'}, where @{text
   441 \<open>B\<close>.  More precisely, \<open>A\<close> and \<open>A'\<close> are unified, thus
   445 r} should be viewed as a function taking a theorem @{text A} and returning
   442 instantiating the unknowns in \<open>B\<close>, and the result is the instantiated
   446 @{text B}.  More precisely, @{text A} and @{text A'} are unified, thus
   443 \<open>B\<close>. Of course, unification may also fail.
   447 instantiating the unknowns in @{text B}, and the result is the instantiated
       
   448 @{text B}. Of course, unification may also fail.
       
   449 \begin{warn}
   444 \begin{warn}
   450 Application of rules to other rules operates in the forward direction: from
   445 Application of rules to other rules operates in the forward direction: from
   451 the premises to the conclusion of the rule; application of rules to proof
   446 the premises to the conclusion of the rule; application of rules to proof
   452 states operates in the backward direction, from the conclusion to the
   447 states operates in the backward direction, from the conclusion to the
   453 premises.
   448 premises.
   454 \end{warn}
   449 \end{warn}
   455 
   450 
   456 In general @{text r} can be of the form @{text"\<lbrakk> A\<^sub>1; \<dots>; A\<^sub>n \<rbrakk> \<Longrightarrow> A"}
   451 In general \<open>r\<close> can be of the form \<open>\<lbrakk> A\<^sub>1; \<dots>; A\<^sub>n \<rbrakk> \<Longrightarrow> A\<close>
   457 and there can be multiple argument theorems @{text r\<^sub>1} to @{text r\<^sub>m}
   452 and there can be multiple argument theorems \<open>r\<^sub>1\<close> to \<open>r\<^sub>m\<close>
   458 (with @{text"m \<le> n"}), in which case @{text "r[OF r\<^sub>1 \<dots> r\<^sub>m]"} is obtained
   453 (with \<open>m \<le> n\<close>), in which case \<open>r[OF r\<^sub>1 \<dots> r\<^sub>m]\<close> is obtained
   459 by unifying and thus proving @{text "A\<^sub>i"} with @{text "r\<^sub>i"}, @{text"i = 1\<dots>m"}.
   454 by unifying and thus proving \<open>A\<^sub>i\<close> with \<open>r\<^sub>i\<close>, \<open>i = 1\<dots>m\<close>.
   460 Here is an example, where @{thm[source]refl} is the theorem
   455 Here is an example, where @{thm[source]refl} is the theorem
   461 @{thm[show_question_marks] refl}:
   456 @{thm[show_question_marks] refl}:
   462 \<close>
   457 \<close>
   463 
   458 
   464 thm conjI[OF refl[of "a"] refl[of "b"]]
   459 thm conjI[OF refl[of "a"] refl[of "b"]]
   465 
   460 
   466 text\<open>yields the theorem @{thm conjI[OF refl[of "a"] refl[of "b"]]}.
   461 text\<open>yields the theorem @{thm conjI[OF refl[of "a"] refl[of "b"]]}.
   467 The command \isacom{thm} merely displays the result.
   462 The command \isacom{thm} merely displays the result.
   468 
   463 
   469 Forward reasoning also makes sense in connection with proof states.
   464 Forward reasoning also makes sense in connection with proof states.
   470 Therefore @{text blast}, @{text fastforce} and @{text auto} support a modifier
   465 Therefore \<open>blast\<close>, \<open>fastforce\<close> and \<open>auto\<close> support a modifier
   471 @{text dest} which instructs the proof method to use certain rules in a
   466 \<open>dest\<close> which instructs the proof method to use certain rules in a
   472 forward fashion. If @{text r} is of the form \mbox{@{text "A \<Longrightarrow> B"}}, the modifier
   467 forward fashion. If \<open>r\<close> is of the form \mbox{\<open>A \<Longrightarrow> B\<close>}, the modifier
   473 \mbox{@{text"dest: r"}}\index{dest@@{text"dest:"}}
   468 \mbox{\<open>dest: r\<close>}\index{dest@\<open>dest:\<close>}
   474 allows proof search to reason forward with @{text r}, i.e.,
   469 allows proof search to reason forward with \<open>r\<close>, i.e.,
   475 to replace an assumption @{text A'}, where @{text A'} unifies with @{text A},
   470 to replace an assumption \<open>A'\<close>, where \<open>A'\<close> unifies with \<open>A\<close>,
   476 with the correspondingly instantiated @{text B}. For example, @{thm[source,show_question_marks] Suc_leD} is the theorem \mbox{@{thm Suc_leD}}, which works well for forward reasoning:
   471 with the correspondingly instantiated \<open>B\<close>. For example, @{thm[source,show_question_marks] Suc_leD} is the theorem \mbox{@{thm Suc_leD}}, which works well for forward reasoning:
   477 \<close>
   472 \<close>
   478 
   473 
   479 lemma "Suc(Suc(Suc a)) \<le> b \<Longrightarrow> a \<le> b"
   474 lemma "Suc(Suc(Suc a)) \<le> b \<Longrightarrow> a \<le> b"
   480 by(blast dest: Suc_leD)
   475 by(blast dest: Suc_leD)
   481 
   476 
   512 \begin{itemize}
   507 \begin{itemize}
   513 \item 0 is even
   508 \item 0 is even
   514 \item If $n$ is even, so is $n+2$.
   509 \item If $n$ is even, so is $n+2$.
   515 \end{itemize}
   510 \end{itemize}
   516 The operative word ``inductive'' means that these are the only even numbers.
   511 The operative word ``inductive'' means that these are the only even numbers.
   517 In Isabelle we give the two rules the names @{text ev0} and @{text evSS}
   512 In Isabelle we give the two rules the names \<open>ev0\<close> and \<open>evSS\<close>
   518 and write
   513 and write
   519 \<close>
   514 \<close>
   520 
   515 
   521 inductive ev :: "nat \<Rightarrow> bool" where
   516 inductive ev :: "nat \<Rightarrow> bool" where
   522 ev0:    "ev 0" |
   517 ev0:    "ev 0" |
   526 text\<open>To get used to inductive definitions, we will first prove a few
   521 text\<open>To get used to inductive definitions, we will first prove a few
   527 properties of @{const ev} informally before we descend to the Isabelle level.
   522 properties of @{const ev} informally before we descend to the Isabelle level.
   528 
   523 
   529 How do we prove that some number is even, e.g., @{prop "ev 4"}? Simply by combining the defining rules for @{const ev}:
   524 How do we prove that some number is even, e.g., @{prop "ev 4"}? Simply by combining the defining rules for @{const ev}:
   530 \begin{quote}
   525 \begin{quote}
   531 @{text "ev 0 \<Longrightarrow> ev (0 + 2) \<Longrightarrow> ev((0 + 2) + 2) = ev 4"}
   526 \<open>ev 0 \<Longrightarrow> ev (0 + 2) \<Longrightarrow> ev((0 + 2) + 2) = ev 4\<close>
   532 \end{quote}
   527 \end{quote}
   533 
   528 
   534 \subsubsection{Rule Induction}\index{rule induction|(}
   529 \subsubsection{Rule Induction}\index{rule induction|(}
   535 
   530 
   536 Showing that all even numbers have some property is more complicated.  For
   531 Showing that all even numbers have some property is more complicated.  For
   547 prove @{prop"evn m"}. There are two cases corresponding to the two rules
   542 prove @{prop"evn m"}. There are two cases corresponding to the two rules
   548 for @{const ev}:
   543 for @{const ev}:
   549 \begin{description}
   544 \begin{description}
   550 \item[Case @{thm[source]ev0}:]
   545 \item[Case @{thm[source]ev0}:]
   551  @{prop"ev m"} was derived by rule @{prop "ev 0"}: \\
   546  @{prop"ev m"} was derived by rule @{prop "ev 0"}: \\
   552  @{text"\<Longrightarrow>"} @{prop"m=(0::nat)"} @{text"\<Longrightarrow>"} @{text "evn m = evn 0 = True"}
   547  \<open>\<Longrightarrow>\<close> @{prop"m=(0::nat)"} \<open>\<Longrightarrow>\<close> \<open>evn m = evn 0 = True\<close>
   553 \item[Case @{thm[source]evSS}:]
   548 \item[Case @{thm[source]evSS}:]
   554  @{prop"ev m"} was derived by rule @{prop "ev n \<Longrightarrow> ev(n+2)"}: \\
   549  @{prop"ev m"} was derived by rule @{prop "ev n \<Longrightarrow> ev(n+2)"}: \\
   555 @{text"\<Longrightarrow>"} @{prop"m=n+(2::nat)"} and by induction hypothesis @{prop"evn n"}\\
   550 \<open>\<Longrightarrow>\<close> @{prop"m=n+(2::nat)"} and by induction hypothesis @{prop"evn n"}\\
   556 @{text"\<Longrightarrow>"} @{text"evn m = evn(n + 2) = evn n = True"}
   551 \<open>\<Longrightarrow>\<close> \<open>evn m = evn(n + 2) = evn n = True\<close>
   557 \end{description}
   552 \end{description}
   558 
   553 
   559 What we have just seen is a special case of \concept{rule induction}.
   554 What we have just seen is a special case of \concept{rule induction}.
   560 Rule induction applies to propositions of this form
   555 Rule induction applies to propositions of this form
   561 \begin{quote}
   556 \begin{quote}
   562 @{prop "ev n \<Longrightarrow> P n"}
   557 @{prop "ev n \<Longrightarrow> P n"}
   563 \end{quote}
   558 \end{quote}
   564 That is, we want to prove a property @{prop"P n"}
   559 That is, we want to prove a property @{prop"P n"}
   565 for all even @{text n}. But if we assume @{prop"ev n"}, then there must be
   560 for all even \<open>n\<close>. But if we assume @{prop"ev n"}, then there must be
   566 some derivation of this assumption using the two defining rules for
   561 some derivation of this assumption using the two defining rules for
   567 @{const ev}. That is, we must prove
   562 @{const ev}. That is, we must prove
   568 \begin{description}
   563 \begin{description}
   569 \item[Case @{thm[source]ev0}:] @{prop"P(0::nat)"}
   564 \item[Case @{thm[source]ev0}:] @{prop"P(0::nat)"}
   570 \item[Case @{thm[source]evSS}:] @{prop"\<lbrakk> ev n; P n \<rbrakk> \<Longrightarrow> P(n + 2::nat)"}
   565 \item[Case @{thm[source]evSS}:] @{prop"\<lbrakk> ev n; P n \<rbrakk> \<Longrightarrow> P(n + 2::nat)"}
   576 \mbox{@{thm (prem 2) ev.induct}}\\
   571 \mbox{@{thm (prem 2) ev.induct}}\\
   577 \mbox{@{prop"!!n. \<lbrakk> ev n; P n \<rbrakk> \<Longrightarrow> P(n+2)"}}}
   572 \mbox{@{prop"!!n. \<lbrakk> ev n; P n \<rbrakk> \<Longrightarrow> P(n+2)"}}}
   578 {\mbox{@{thm (concl) ev.induct[of "n"]}}}
   573 {\mbox{@{thm (concl) ev.induct[of "n"]}}}
   579 \]
   574 \]
   580 The first premise @{prop"ev n"} enforces that this rule can only be applied
   575 The first premise @{prop"ev n"} enforces that this rule can only be applied
   581 in situations where we know that @{text n} is even.
   576 in situations where we know that \<open>n\<close> is even.
   582 
   577 
   583 Note that in the induction step we may not just assume @{prop"P n"} but also
   578 Note that in the induction step we may not just assume @{prop"P n"} but also
   584 \mbox{@{prop"ev n"}}, which is simply the premise of rule @{thm[source]
   579 \mbox{@{prop"ev n"}}, which is simply the premise of rule @{thm[source]
   585 evSS}.  Here is an example where the local assumption @{prop"ev n"} comes in
   580 evSS}.  Here is an example where the local assumption @{prop"ev n"} comes in
   586 handy: we prove @{prop"ev m \<Longrightarrow> ev(m - 2)"} by induction on @{prop"ev m"}.
   581 handy: we prove @{prop"ev m \<Longrightarrow> ev(m - 2)"} by induction on @{prop"ev m"}.
   587 Case @{thm[source]ev0} requires us to prove @{prop"ev(0 - 2)"}, which follows
   582 Case @{thm[source]ev0} requires us to prove @{prop"ev(0 - 2)"}, which follows
   588 from @{prop"ev 0"} because @{prop"0 - 2 = (0::nat)"} on type @{typ nat}. In
   583 from @{prop"ev 0"} because @{prop"0 - 2 = (0::nat)"} on type @{typ nat}. In
   589 case @{thm[source]evSS} we have \mbox{@{prop"m = n+(2::nat)"}} and may assume
   584 case @{thm[source]evSS} we have \mbox{@{prop"m = n+(2::nat)"}} and may assume
   590 @{prop"ev n"}, which implies @{prop"ev (m - 2)"} because @{text"m - 2 = (n +
   585 @{prop"ev n"}, which implies @{prop"ev (m - 2)"} because \<open>m - 2 = (n +
   591 2) - 2 = n"}. We did not need the induction hypothesis at all for this proof (it
   586 2) - 2 = n\<close>. We did not need the induction hypothesis at all for this proof (it
   592 is just a case analysis of which rule was used) but having @{prop"ev n"}
   587 is just a case analysis of which rule was used) but having @{prop"ev n"}
   593 at our disposal in case @{thm[source]evSS} was essential.
   588 at our disposal in case @{thm[source]evSS} was essential.
   594 This case analysis of rules is also called ``rule inversion''
   589 This case analysis of rules is also called ``rule inversion''
   595 and is discussed in more detail in \autoref{ch:Isar}.
   590 and is discussed in more detail in \autoref{ch:Isar}.
   596 
   591 
   597 \subsubsection{In Isabelle}
   592 \subsubsection{In Isabelle}
   598 
   593 
   599 Let us now recast the above informal proofs in Isabelle. For a start,
   594 Let us now recast the above informal proofs in Isabelle. For a start,
   600 we use @{const Suc} terms instead of numerals in rule @{thm[source]evSS}:
   595 we use @{const Suc} terms instead of numerals in rule @{thm[source]evSS}:
   601 @{thm[display] evSS}
   596 @{thm[display] evSS}
   602 This avoids the difficulty of unifying @{text"n+2"} with some numeral,
   597 This avoids the difficulty of unifying \<open>n+2\<close> with some numeral,
   603 which is not automatic.
   598 which is not automatic.
   604 
   599 
   605 The simplest way to prove @{prop"ev(Suc(Suc(Suc(Suc 0))))"} is in a forward
   600 The simplest way to prove @{prop"ev(Suc(Suc(Suc(Suc 0))))"} is in a forward
   606 direction: @{text "evSS[OF evSS[OF ev0]]"} yields the theorem @{thm evSS[OF
   601 direction: \<open>evSS[OF evSS[OF ev0]]\<close> yields the theorem @{thm evSS[OF
   607 evSS[OF ev0]]}. Alternatively, you can also prove it as a lemma in backwards
   602 evSS[OF ev0]]}. Alternatively, you can also prove it as a lemma in backwards
   608 fashion. Although this is more verbose, it allows us to demonstrate how each
   603 fashion. Although this is more verbose, it allows us to demonstrate how each
   609 rule application changes the proof state:\<close>
   604 rule application changes the proof state:\<close>
   610 
   605 
   611 lemma "ev(Suc(Suc(Suc(Suc 0))))"
   606 lemma "ev(Suc(Suc(Suc(Suc 0))))"
   623 apply(rule ev0)
   618 apply(rule ev0)
   624 done
   619 done
   625 
   620 
   626 text\<open>\indent
   621 text\<open>\indent
   627 Rule induction is applied by giving the induction rule explicitly via the
   622 Rule induction is applied by giving the induction rule explicitly via the
   628 @{text"rule:"} modifier:\index{inductionrule@@{text"induction ... rule:"}}\<close>
   623 \<open>rule:\<close> modifier:\index{inductionrule@\<open>induction ... rule:\<close>}\<close>
   629 
   624 
   630 lemma "ev m \<Longrightarrow> evn m"
   625 lemma "ev m \<Longrightarrow> evn m"
   631 apply(induction rule: ev.induct)
   626 apply(induction rule: ev.induct)
   632 by(simp_all)
   627 by(simp_all)
   633 
   628 
   634 text\<open>Both cases are automatic. Note that if there are multiple assumptions
   629 text\<open>Both cases are automatic. Note that if there are multiple assumptions
   635 of the form @{prop"ev t"}, method @{text induction} will induct on the leftmost
   630 of the form @{prop"ev t"}, method \<open>induction\<close> will induct on the leftmost
   636 one.
   631 one.
   637 
   632 
   638 As a bonus, we also prove the remaining direction of the equivalence of
   633 As a bonus, we also prove the remaining direction of the equivalence of
   639 @{const ev} and @{const evn}:
   634 @{const ev} and @{const evn}:
   640 \<close>
   635 \<close>
   641 
   636 
   642 lemma "evn n \<Longrightarrow> ev n"
   637 lemma "evn n \<Longrightarrow> ev n"
   643 apply(induction n rule: evn.induct)
   638 apply(induction n rule: evn.induct)
   644 
   639 
   645 txt\<open>This is a proof by computation induction on @{text n} (see
   640 txt\<open>This is a proof by computation induction on \<open>n\<close> (see
   646 \autoref{sec:recursive-funs}) that sets up three subgoals corresponding to
   641 \autoref{sec:recursive-funs}) that sets up three subgoals corresponding to
   647 the three equations for @{const evn}:
   642 the three equations for @{const evn}:
   648 @{subgoals[display,indent=0]}
   643 @{subgoals[display,indent=0]}
   649 The first and third subgoals follow with @{thm[source]ev0} and @{thm[source]evSS}, and the second subgoal is trivially true because @{prop"evn(Suc 0)"} is @{const False}:
   644 The first and third subgoals follow with @{thm[source]ev0} and @{thm[source]evSS}, and the second subgoal is trivially true because @{prop"evn(Suc 0)"} is @{const False}:
   650 \<close>
   645 \<close>
   653 
   648 
   654 text\<open>The rules for @{const ev} make perfect simplification and introduction
   649 text\<open>The rules for @{const ev} make perfect simplification and introduction
   655 rules because their premises are always smaller than the conclusion. It
   650 rules because their premises are always smaller than the conclusion. It
   656 makes sense to turn them into simplification and introduction rules
   651 makes sense to turn them into simplification and introduction rules
   657 permanently, to enhance proof automation. They are named @{thm[source] ev.intros}
   652 permanently, to enhance proof automation. They are named @{thm[source] ev.intros}
   658 \index{intros@@{text".intros"}} by Isabelle:\<close>
   653 \index{intros@\<open>.intros\<close>} by Isabelle:\<close>
   659 
   654 
   660 declare ev.intros[simp,intro]
   655 declare ev.intros[simp,intro]
   661 
   656 
   662 text\<open>The rules of an inductive definition are not simplification rules by
   657 text\<open>The rules of an inductive definition are not simplification rules by
   663 default because, in contrast to recursive functions, there is no termination
   658 default because, in contrast to recursive functions, there is no termination
   669 recursive one. Which one is better? Much of the time, the recursive one is
   664 recursive one. Which one is better? Much of the time, the recursive one is
   670 more convenient: it allows us to do rewriting in the middle of terms, and it
   665 more convenient: it allows us to do rewriting in the middle of terms, and it
   671 expresses both the positive information (which numbers are even) and the
   666 expresses both the positive information (which numbers are even) and the
   672 negative information (which numbers are not even) directly. An inductive
   667 negative information (which numbers are not even) directly. An inductive
   673 definition only expresses the positive information directly. The negative
   668 definition only expresses the positive information directly. The negative
   674 information, for example, that @{text 1} is not even, has to be proved from
   669 information, for example, that \<open>1\<close> is not even, has to be proved from
   675 it (by induction or rule inversion). On the other hand, rule induction is
   670 it (by induction or rule inversion). On the other hand, rule induction is
   676 tailor-made for proving \mbox{@{prop"ev n \<Longrightarrow> P n"}} because it only asks you
   671 tailor-made for proving \mbox{@{prop"ev n \<Longrightarrow> P n"}} because it only asks you
   677 to prove the positive cases. In the proof of @{prop"evn n \<Longrightarrow> P n"} by
   672 to prove the positive cases. In the proof of @{prop"evn n \<Longrightarrow> P n"} by
   678 computation induction via @{thm[source]evn.induct}, we are also presented
   673 computation induction via @{thm[source]evn.induct}, we are also presented
   679 with the trivial negative cases. If you want the convenience of both
   674 with the trivial negative cases. If you want the convenience of both
   697 \ifsem
   692 \ifsem
   698 It will also be an important building block for
   693 It will also be an important building block for
   699 some of the semantics considered in the second part of the book.
   694 some of the semantics considered in the second part of the book.
   700 \fi
   695 \fi
   701 
   696 
   702 The reflexive transitive closure, called @{text star} below, is a function
   697 The reflexive transitive closure, called \<open>star\<close> below, is a function
   703 that maps a binary predicate to another binary predicate: if @{text r} is of
   698 that maps a binary predicate to another binary predicate: if \<open>r\<close> is of
   704 type @{text"\<tau> \<Rightarrow> \<tau> \<Rightarrow> bool"} then @{term "star r"} is again of type @{text"\<tau> \<Rightarrow>
   699 type \<open>\<tau> \<Rightarrow> \<tau> \<Rightarrow> bool\<close> then @{term "star r"} is again of type \<open>\<tau> \<Rightarrow>
   705 \<tau> \<Rightarrow> bool"}, and @{prop"star r x y"} means that @{text x} and @{text y} are in
   700 \<tau> \<Rightarrow> bool\<close>, and @{prop"star r x y"} means that \<open>x\<close> and \<open>y\<close> are in
   706 the relation @{term"star r"}. Think @{term"r\<^sup>*"} when you see @{term"star
   701 the relation @{term"star r"}. Think @{term"r\<^sup>*"} when you see @{term"star
   707 r"}, because @{text"star r"} is meant to be the reflexive transitive closure.
   702 r"}, because \<open>star r\<close> is meant to be the reflexive transitive closure.
   708 That is, @{prop"star r x y"} is meant to be true if from @{text x} we can
   703 That is, @{prop"star r x y"} is meant to be true if from \<open>x\<close> we can
   709 reach @{text y} in finitely many @{text r} steps. This concept is naturally
   704 reach \<open>y\<close> in finitely many \<open>r\<close> steps. This concept is naturally
   710 defined inductively:\<close>
   705 defined inductively:\<close>
   711 
   706 
   712 inductive star :: "('a \<Rightarrow> 'a \<Rightarrow> bool) \<Rightarrow> 'a \<Rightarrow> 'a \<Rightarrow> bool"  for r where
   707 inductive star :: "('a \<Rightarrow> 'a \<Rightarrow> bool) \<Rightarrow> 'a \<Rightarrow> 'a \<Rightarrow> bool"  for r where
   713 refl:  "star r x x" |
   708 refl:  "star r x x" |
   714 step:  "r x y \<Longrightarrow> star r y z \<Longrightarrow> star r x z"
   709 step:  "r x y \<Longrightarrow> star r y z \<Longrightarrow> star r x z"
   715 
   710 
   716 text\<open>The base case @{thm[source] refl} is reflexivity: @{term "x=y"}. The
   711 text\<open>The base case @{thm[source] refl} is reflexivity: @{term "x=y"}. The
   717 step case @{thm[source]step} combines an @{text r} step (from @{text x} to
   712 step case @{thm[source]step} combines an \<open>r\<close> step (from \<open>x\<close> to
   718 @{text y}) and a @{term"star r"} step (from @{text y} to @{text z}) into a
   713 \<open>y\<close>) and a @{term"star r"} step (from \<open>y\<close> to \<open>z\<close>) into a
   719 @{term"star r"} step (from @{text x} to @{text z}).
   714 @{term"star r"} step (from \<open>x\<close> to \<open>z\<close>).
   720 The ``\isacom{for}~@{text r}'' in the header is merely a hint to Isabelle
   715 The ``\isacom{for}~\<open>r\<close>'' in the header is merely a hint to Isabelle
   721 that @{text r} is a fixed parameter of @{const star}, in contrast to the
   716 that \<open>r\<close> is a fixed parameter of @{const star}, in contrast to the
   722 further parameters of @{const star}, which change. As a result, Isabelle
   717 further parameters of @{const star}, which change. As a result, Isabelle
   723 generates a simpler induction rule.
   718 generates a simpler induction rule.
   724 
   719 
   725 By definition @{term"star r"} is reflexive. It is also transitive, but we
   720 By definition @{term"star r"} is reflexive. It is also transitive, but we
   726 need rule induction to prove that:\<close>
   721 need rule induction to prove that:\<close>
   735 txt\<open>The induction is over @{prop"star r x y"} (the first matching assumption)
   730 txt\<open>The induction is over @{prop"star r x y"} (the first matching assumption)
   736 and we try to prove \mbox{@{prop"star r y z \<Longrightarrow> star r x z"}},
   731 and we try to prove \mbox{@{prop"star r y z \<Longrightarrow> star r x z"}},
   737 which we abbreviate by @{prop"P x y"}. These are our two subgoals:
   732 which we abbreviate by @{prop"P x y"}. These are our two subgoals:
   738 @{subgoals[display,indent=0]}
   733 @{subgoals[display,indent=0]}
   739 The first one is @{prop"P x x"}, the result of case @{thm[source]refl},
   734 The first one is @{prop"P x x"}, the result of case @{thm[source]refl},
   740 and it is trivial:\index{assumption@@{text assumption}}
   735 and it is trivial:\index{assumption@\<open>assumption\<close>}
   741 \<close>
   736 \<close>
   742 apply(assumption)
   737 apply(assumption)
   743 txt\<open>Let us examine subgoal @{text 2}, case @{thm[source] step}.
   738 txt\<open>Let us examine subgoal \<open>2\<close>, case @{thm[source] step}.
   744 Assumptions @{prop"r u x"} and \mbox{@{prop"star r x y"}}
   739 Assumptions @{prop"r u x"} and \mbox{@{prop"star r x y"}}
   745 are the premises of rule @{thm[source]step}.
   740 are the premises of rule @{thm[source]step}.
   746 Assumption @{prop"star r y z \<Longrightarrow> star r x z"} is \mbox{@{prop"P x y"}},
   741 Assumption @{prop"star r y z \<Longrightarrow> star r x z"} is \mbox{@{prop"P x y"}},
   747 the IH coming from @{prop"star r x y"}. We have to prove @{prop"P u y"},
   742 the IH coming from @{prop"star r x y"}. We have to prove @{prop"P u y"},
   748 which we do by assuming @{prop"star r y z"} and proving @{prop"star r u z"}.
   743 which we do by assuming @{prop"star r y z"} and proving @{prop"star r u z"}.
   757 
   752 
   758 \subsection{The General Case}
   753 \subsection{The General Case}
   759 
   754 
   760 Inductive definitions have approximately the following general form:
   755 Inductive definitions have approximately the following general form:
   761 \begin{quote}
   756 \begin{quote}
   762 \isacom{inductive} @{text"I :: \"\<tau> \<Rightarrow> bool\""} \isacom{where}
   757 \isacom{inductive} \<open>I :: "\<tau> \<Rightarrow> bool"\<close> \isacom{where}
   763 \end{quote}
   758 \end{quote}
   764 followed by a sequence of (possibly named) rules of the form
   759 followed by a sequence of (possibly named) rules of the form
   765 \begin{quote}
   760 \begin{quote}
   766 @{text "\<lbrakk> I a\<^sub>1; \<dots>; I a\<^sub>n \<rbrakk> \<Longrightarrow> I a"}
   761 \<open>\<lbrakk> I a\<^sub>1; \<dots>; I a\<^sub>n \<rbrakk> \<Longrightarrow> I a\<close>
   767 \end{quote}
   762 \end{quote}
   768 separated by @{text"|"}. As usual, @{text n} can be 0.
   763 separated by \<open>|\<close>. As usual, \<open>n\<close> can be 0.
   769 The corresponding rule induction principle
   764 The corresponding rule induction principle
   770 @{text I.induct} applies to propositions of the form
   765 \<open>I.induct\<close> applies to propositions of the form
   771 \begin{quote}
   766 \begin{quote}
   772 @{prop "I x \<Longrightarrow> P x"}
   767 @{prop "I x \<Longrightarrow> P x"}
   773 \end{quote}
   768 \end{quote}
   774 where @{text P} may itself be a chain of implications.
   769 where \<open>P\<close> may itself be a chain of implications.
   775 \begin{warn}
   770 \begin{warn}
   776 Rule induction is always on the leftmost premise of the goal.
   771 Rule induction is always on the leftmost premise of the goal.
   777 Hence @{text "I x"} must be the first premise.
   772 Hence \<open>I x\<close> must be the first premise.
   778 \end{warn}
   773 \end{warn}
   779 Proving @{prop "I x \<Longrightarrow> P x"} by rule induction means proving
   774 Proving @{prop "I x \<Longrightarrow> P x"} by rule induction means proving
   780 for every rule of @{text I} that @{text P} is invariant:
   775 for every rule of \<open>I\<close> that \<open>P\<close> is invariant:
   781 \begin{quote}
   776 \begin{quote}
   782 @{text "\<lbrakk> I a\<^sub>1; P a\<^sub>1; \<dots>; I a\<^sub>n; P a\<^sub>n \<rbrakk> \<Longrightarrow> P a"}
   777 \<open>\<lbrakk> I a\<^sub>1; P a\<^sub>1; \<dots>; I a\<^sub>n; P a\<^sub>n \<rbrakk> \<Longrightarrow> P a\<close>
   783 \end{quote}
   778 \end{quote}
   784 
   779 
   785 The above format for inductive definitions is simplified in a number of
   780 The above format for inductive definitions is simplified in a number of
   786 respects. @{text I} can have any number of arguments and each rule can have
   781 respects. \<open>I\<close> can have any number of arguments and each rule can have
   787 additional premises not involving @{text I}, so-called \conceptidx{side
   782 additional premises not involving \<open>I\<close>, so-called \conceptidx{side
   788 conditions}{side condition}. In rule inductions, these side conditions appear as additional
   783 conditions}{side condition}. In rule inductions, these side conditions appear as additional
   789 assumptions. The \isacom{for} clause seen in the definition of the reflexive
   784 assumptions. The \isacom{for} clause seen in the definition of the reflexive
   790 transitive closure simplifies the induction rule.
   785 transitive closure simplifies the induction rule.
   791 \index{inductive definition|)}
   786 \index{inductive definition|)}
   792 
   787 
   794 
   789 
   795 \begin{exercise}
   790 \begin{exercise}
   796 Formalize the following definition of palindromes
   791 Formalize the following definition of palindromes
   797 \begin{itemize}
   792 \begin{itemize}
   798 \item The empty list and a singleton list are palindromes.
   793 \item The empty list and a singleton list are palindromes.
   799 \item If @{text xs} is a palindrome, so is @{term "a # xs @ [a]"}.
   794 \item If \<open>xs\<close> is a palindrome, so is @{term "a # xs @ [a]"}.
   800 \end{itemize}
   795 \end{itemize}
   801 as an inductive predicate @{text "palindrome ::"} @{typ "'a list \<Rightarrow> bool"}
   796 as an inductive predicate \<open>palindrome ::\<close> @{typ "'a list \<Rightarrow> bool"}
   802 and prove that @{prop "rev xs = xs"} if @{text xs} is a palindrome.
   797 and prove that @{prop "rev xs = xs"} if \<open>xs\<close> is a palindrome.
   803 \end{exercise}
   798 \end{exercise}
   804 
   799 
   805 \exercise
   800 \exercise
   806 We could also have defined @{const star} as follows:
   801 We could also have defined @{const star} as follows:
   807 \<close>
   802 \<close>
   809 inductive star' :: "('a \<Rightarrow> 'a \<Rightarrow> bool) \<Rightarrow> 'a \<Rightarrow> 'a \<Rightarrow> bool" for r where
   804 inductive star' :: "('a \<Rightarrow> 'a \<Rightarrow> bool) \<Rightarrow> 'a \<Rightarrow> 'a \<Rightarrow> bool" for r where
   810 refl': "star' r x x" |
   805 refl': "star' r x x" |
   811 step': "star' r x y \<Longrightarrow> r y z \<Longrightarrow> star' r x z"
   806 step': "star' r x y \<Longrightarrow> r y z \<Longrightarrow> star' r x z"
   812 
   807 
   813 text\<open>
   808 text\<open>
   814 The single @{text r} step is performed after rather than before the @{text star'}
   809 The single \<open>r\<close> step is performed after rather than before the \<open>star'\<close>
   815 steps. Prove @{prop "star' r x y \<Longrightarrow> star r x y"} and
   810 steps. Prove @{prop "star' r x y \<Longrightarrow> star r x y"} and
   816 @{prop "star r x y \<Longrightarrow> star' r x y"}. You may need lemmas.
   811 @{prop "star r x y \<Longrightarrow> star' r x y"}. You may need lemmas.
   817 Note that rule induction fails
   812 Note that rule induction fails
   818 if the assumption about the inductive predicate is not the first assumption.
   813 if the assumption about the inductive predicate is not the first assumption.
   819 \endexercise
   814 \endexercise
   820 
   815 
   821 \begin{exercise}\label{exe:iter}
   816 \begin{exercise}\label{exe:iter}
   822 Analogous to @{const star}, give an inductive definition of the @{text n}-fold iteration
   817 Analogous to @{const star}, give an inductive definition of the \<open>n\<close>-fold iteration
   823 of a relation @{text r}: @{term "iter r n x y"} should hold if there are @{text x\<^sub>0}, \dots, @{text x\<^sub>n}
   818 of a relation \<open>r\<close>: @{term "iter r n x y"} should hold if there are \<open>x\<^sub>0\<close>, \dots, \<open>x\<^sub>n\<close>
   824 such that @{prop"x = x\<^sub>0"}, @{prop"x\<^sub>n = y"} and @{text"r x\<^bsub>i\<^esub> x\<^bsub>i+1\<^esub>"} for
   819 such that @{prop"x = x\<^sub>0"}, @{prop"x\<^sub>n = y"} and \<open>r x\<^bsub>i\<^esub> x\<^bsub>i+1\<^esub>\<close> for
   825 all @{prop"i < n"}. Correct and prove the following claim:
   820 all @{prop"i < n"}. Correct and prove the following claim:
   826 @{prop"star r x y \<Longrightarrow> iter r n x y"}.
   821 @{prop"star r x y \<Longrightarrow> iter r n x y"}.
   827 \end{exercise}
   822 \end{exercise}
   828 
   823 
   829 \begin{exercise}\label{exe:cfg}
   824 \begin{exercise}\label{exe:cfg}
   830 A context-free grammar can be seen as an inductive definition where each
   825 A context-free grammar can be seen as an inductive definition where each
   831 nonterminal $A$ is an inductively defined predicate on lists of terminal
   826 nonterminal $A$ is an inductively defined predicate on lists of terminal
   832 symbols: $A(w)$ means that $w$ is in the language generated by $A$.
   827 symbols: $A(w)$ means that $w$ is in the language generated by $A$.
   833 For example, the production $S \to a S b$ can be viewed as the implication
   828 For example, the production $S \to a S b$ can be viewed as the implication
   834 @{prop"S w \<Longrightarrow> S (a # w @ [b])"} where @{text a} and @{text b} are terminal symbols,
   829 @{prop"S w \<Longrightarrow> S (a # w @ [b])"} where \<open>a\<close> and \<open>b\<close> are terminal symbols,
   835 i.e., elements of some alphabet. The alphabet can be defined like this:
   830 i.e., elements of some alphabet. The alphabet can be defined like this:
   836 \isacom{datatype} @{text"alpha = a | b | \<dots>"}
   831 \isacom{datatype} \<open>alpha = a | b | \<dots>\<close>
   837 
   832 
   838 Define the two grammars (where $\varepsilon$ is the empty word)
   833 Define the two grammars (where $\varepsilon$ is the empty word)
   839 \[
   834 \[
   840 \begin{array}{r@ {\quad}c@ {\quad}l}
   835 \begin{array}{r@ {\quad}c@ {\quad}l}
   841 S &\to& \varepsilon \quad\mid\quad aSb \quad\mid\quad SS \\
   836 S &\to& \varepsilon \quad\mid\quad aSb \quad\mid\quad SS \\
   842 T &\to& \varepsilon \quad\mid\quad TaTb
   837 T &\to& \varepsilon \quad\mid\quad TaTb
   843 \end{array}
   838 \end{array}
   844 \]
   839 \]
   845 as two inductive predicates.
   840 as two inductive predicates.
   846 If you think of @{text a} and @{text b} as ``@{text "("}'' and  ``@{text ")"}'',
   841 If you think of \<open>a\<close> and \<open>b\<close> as ``\<open>(\<close>'' and  ``\<open>)\<close>'',
   847 the grammar defines strings of balanced parentheses.
   842 the grammar defines strings of balanced parentheses.
   848 Prove @{prop"T w \<Longrightarrow> S w"} and \mbox{@{prop "S w \<Longrightarrow> T w"}} separately and conclude
   843 Prove @{prop"T w \<Longrightarrow> S w"} and \mbox{@{prop "S w \<Longrightarrow> T w"}} separately and conclude
   849 @{prop "S w = T w"}.
   844 @{prop "S w = T w"}.
   850 \end{exercise}
   845 \end{exercise}
   851 
   846 
   852 \ifsem
   847 \ifsem
   853 \begin{exercise}
   848 \begin{exercise}
   854 In \autoref{sec:AExp} we defined a recursive evaluation function
   849 In \autoref{sec:AExp} we defined a recursive evaluation function
   855 @{text "aval :: aexp \<Rightarrow> state \<Rightarrow> val"}.
   850 \<open>aval :: aexp \<Rightarrow> state \<Rightarrow> val\<close>.
   856 Define an inductive evaluation predicate
   851 Define an inductive evaluation predicate
   857 @{text "aval_rel :: aexp \<Rightarrow> state \<Rightarrow> val \<Rightarrow> bool"}
   852 \<open>aval_rel :: aexp \<Rightarrow> state \<Rightarrow> val \<Rightarrow> bool\<close>
   858 and prove that it agrees with the recursive function:
   853 and prove that it agrees with the recursive function:
   859 @{prop "aval_rel a s v \<Longrightarrow> aval a s = v"}, 
   854 @{prop "aval_rel a s v \<Longrightarrow> aval a s = v"}, 
   860 @{prop "aval a s = v \<Longrightarrow> aval_rel a s v"} and thus
   855 @{prop "aval a s = v \<Longrightarrow> aval_rel a s v"} and thus
   861 \noquotes{@{prop [source] "aval_rel a s v \<longleftrightarrow> aval a s = v"}}.
   856 \noquotes{@{prop [source] "aval_rel a s v \<longleftrightarrow> aval a s = v"}}.
   862 \end{exercise}
   857 \end{exercise}
   864 \begin{exercise}
   859 \begin{exercise}
   865 Consider the stack machine from Chapter~3
   860 Consider the stack machine from Chapter~3
   866 and recall the concept of \concept{stack underflow}
   861 and recall the concept of \concept{stack underflow}
   867 from Exercise~\ref{exe:stack-underflow}.
   862 from Exercise~\ref{exe:stack-underflow}.
   868 Define an inductive predicate
   863 Define an inductive predicate
   869 @{text "ok :: nat \<Rightarrow> instr list \<Rightarrow> nat \<Rightarrow> bool"}
   864 \<open>ok :: nat \<Rightarrow> instr list \<Rightarrow> nat \<Rightarrow> bool\<close>
   870 such that @{text "ok n is n'"} means that with any initial stack of length
   865 such that \<open>ok n is n'\<close> means that with any initial stack of length
   871 @{text n} the instructions @{text "is"} can be executed
   866 \<open>n\<close> the instructions \<open>is\<close> can be executed
   872 without stack underflow and that the final stack has length @{text n'}.
   867 without stack underflow and that the final stack has length \<open>n'\<close>.
   873 Prove that @{text ok} correctly computes the final stack size
   868 Prove that \<open>ok\<close> correctly computes the final stack size
   874 @{prop[display] "\<lbrakk>ok n is n'; length stk = n\<rbrakk> \<Longrightarrow> length (exec is s stk) = n'"}
   869 @{prop[display] "\<lbrakk>ok n is n'; length stk = n\<rbrakk> \<Longrightarrow> length (exec is s stk) = n'"}
   875 and that instruction sequences generated by @{text comp}
   870 and that instruction sequences generated by \<open>comp\<close>
   876 cannot cause stack underflow: \ @{text "ok n (comp a) ?"} \ for
   871 cannot cause stack underflow: \ \<open>ok n (comp a) ?\<close> \ for
   877 some suitable value of @{text "?"}.
   872 some suitable value of \<open>?\<close>.
   878 \end{exercise}
   873 \end{exercise}
   879 \fi
   874 \fi
   880 \<close>
   875 \<close>
   881 (*<*)
   876 (*<*)
   882 end
   877 end