src/Doc/ProgProve/Types_and_funs.thy
changeset 52361 7d5ad23b8245
parent 52045 90cd3c53a887
child 52593 aedf7b01c6e4
equal deleted inserted replaced
52357:a5d3730043c2 52361:7d5ad23b8245
     3 imports Main
     3 imports Main
     4 begin
     4 begin
     5 (*>*)
     5 (*>*)
     6 text{*
     6 text{*
     7 \vspace{-5ex}
     7 \vspace{-5ex}
     8 \section{Type and function definitions}
     8 \section{Type and Function Definitions}
     9 
     9 
    10 Type synonyms are abbreviations for existing types, for example
    10 Type synonyms are abbreviations for existing types, for example
    11 *}
    11 *}
    12 
    12 
    13 type_synonym string = "char list"
    13 type_synonym string = "char list"
   127 if abused, they can lead to a confusing discrepancy between the internal and
   127 if abused, they can lead to a confusing discrepancy between the internal and
   128 external view of a term.
   128 external view of a term.
   129 
   129 
   130 The ASCII representation of @{text"\<equiv>"} is \texttt{==} or \xsymbol{equiv}.
   130 The ASCII representation of @{text"\<equiv>"} is \texttt{==} or \xsymbol{equiv}.
   131 
   131 
   132 \subsection{Recursive functions}
   132 \subsection{Recursive Functions}
   133 \label{sec:recursive-funs}
   133 \label{sec:recursive-funs}
   134 
   134 
   135 Recursive functions are defined with \isacom{fun} by pattern matching
   135 Recursive functions are defined with \isacom{fun} by pattern matching
   136 over datatype constructors. The order of equations matters. Just as in
   136 over datatype constructors. The order of equations matters. Just as in
   137 functional programming languages. However, all HOL functions must be
   137 functional programming languages. However, all HOL functions must be
   198 \end{quote}
   198 \end{quote}
   199 where typically there is a call @{text"f x\<^isub>1 \<dots> x\<^isub>n"} in the goal.
   199 where typically there is a call @{text"f x\<^isub>1 \<dots> x\<^isub>n"} in the goal.
   200 But note that the induction rule does not mention @{text f} at all,
   200 But note that the induction rule does not mention @{text f} at all,
   201 except in its name, and is applicable independently of @{text f}.
   201 except in its name, and is applicable independently of @{text f}.
   202 
   202 
   203 \section{Induction heuristics}
   203 \section{Induction Heuristics}
   204 
   204 
   205 We have already noted that theorems about recursive functions are proved by
   205 We have already noted that theorems about recursive functions are proved by
   206 induction. In case the function has more than one argument, we have followed
   206 induction. In case the function has more than one argument, we have followed
   207 the following heuristic in the proofs about the append function:
   207 the following heuristic in the proofs about the append function:
   208 \begin{quote}
   208 \begin{quote}
   343 \end{array}
   343 \end{array}
   344 \]
   344 \]
   345 Simplification is often also called \concept{rewriting}
   345 Simplification is often also called \concept{rewriting}
   346 and simplification rules \concept{rewrite rules}.
   346 and simplification rules \concept{rewrite rules}.
   347 
   347 
   348 \subsection{Simplification rules}
   348 \subsection{Simplification Rules}
   349 
   349 
   350 The attribute @{text"simp"} declares theorems to be simplification rules,
   350 The attribute @{text"simp"} declares theorems to be simplification rules,
   351 which the simplifier will use automatically. In addition,
   351 which the simplifier will use automatically. In addition,
   352 \isacom{datatype} and \isacom{fun} commands implicitly declare some
   352 \isacom{datatype} and \isacom{fun} commands implicitly declare some
   353 simplification rules: \isacom{datatype} the distinctness and injectivity
   353 simplification rules: \isacom{datatype} the distinctness and injectivity
   361 rules. Equations that may be counterproductive as simplification rules
   361 rules. Equations that may be counterproductive as simplification rules
   362 should only be used in specific proof steps (see \S\ref{sec:simp} below).
   362 should only be used in specific proof steps (see \S\ref{sec:simp} below).
   363 Distributivity laws, for example, alter the structure of terms
   363 Distributivity laws, for example, alter the structure of terms
   364 and can produce an exponential blow-up.
   364 and can produce an exponential blow-up.
   365 
   365 
   366 \subsection{Conditional simplification rules}
   366 \subsection{Conditional Simplification Rules}
   367 
   367 
   368 Simplification rules can be conditional.  Before applying such a rule, the
   368 Simplification rules can be conditional.  Before applying such a rule, the
   369 simplifier will first try to prove the preconditions, again by
   369 simplifier will first try to prove the preconditions, again by
   370 simplification. For example, given the simplification rules
   370 simplification. For example, given the simplification rules
   371 \begin{quote}
   371 \begin{quote}
   399 @{prop "Suc n < m \<Longrightarrow> (n < m) = True"}
   399 @{prop "Suc n < m \<Longrightarrow> (n < m) = True"}
   400 \end{quote}
   400 \end{quote}
   401 leads to nontermination: when trying to rewrite @{prop"n<m"} to @{const True}
   401 leads to nontermination: when trying to rewrite @{prop"n<m"} to @{const True}
   402 one first has to prove \mbox{@{prop"Suc n < m"}}, which can be rewritten to @{const True} provided @{prop"Suc(Suc n) < m"}, \emph{ad infinitum}.
   402 one first has to prove \mbox{@{prop"Suc n < m"}}, which can be rewritten to @{const True} provided @{prop"Suc(Suc n) < m"}, \emph{ad infinitum}.
   403 
   403 
   404 \subsection{The @{text simp} proof method}
   404 \subsection{The @{text simp} Proof Method}
   405 \label{sec:simp}
   405 \label{sec:simp}
   406 
   406 
   407 So far we have only used the proof method @{text auto}.  Method @{text simp}
   407 So far we have only used the proof method @{text auto}.  Method @{text simp}
   408 is the key component of @{text auto}, but @{text auto} can do much more. In
   408 is the key component of @{text auto}, but @{text auto} can do much more. In
   409 some cases, @{text auto} is overeager and modifies the proof state too much.
   409 some cases, @{text auto} is overeager and modifies the proof state too much.
   434 
   434 
   435 Note that @{text simp} acts only on subgoal 1, @{text auto} acts on all
   435 Note that @{text simp} acts only on subgoal 1, @{text auto} acts on all
   436 subgoals. There is also @{text simp_all}, which applies @{text simp} to
   436 subgoals. There is also @{text simp_all}, which applies @{text simp} to
   437 all subgoals.
   437 all subgoals.
   438 
   438 
   439 \subsection{Rewriting with definitions}
   439 \subsection{Rewriting With Definitions}
   440 \label{sec:rewr-defs}
   440 \label{sec:rewr-defs}
   441 
   441 
   442 Definitions introduced by the command \isacom{definition}
   442 Definitions introduced by the command \isacom{definition}
   443 can also be used as simplification rules,
   443 can also be used as simplification rules,
   444 but by default they are not: the simplifier does not expand them
   444 but by default they are not: the simplifier does not expand them
   455 \isacom{apply}@{text"(simp add: f_def)"}
   455 \isacom{apply}@{text"(simp add: f_def)"}
   456 \end{quote}
   456 \end{quote}
   457 In particular, let-expressions can be unfolded by
   457 In particular, let-expressions can be unfolded by
   458 making @{thm[source] Let_def} a simplification rule.
   458 making @{thm[source] Let_def} a simplification rule.
   459 
   459 
   460 \subsection{Case splitting with @{text simp}}
   460 \subsection{Case Splitting With @{text simp}}
   461 
   461 
   462 Goals containing if-expressions are automatically split into two cases by
   462 Goals containing if-expressions are automatically split into two cases by
   463 @{text simp} using the rule
   463 @{text simp} using the rule
   464 \begin{quote}
   464 \begin{quote}
   465 @{prop"P(if A then s else t) = ((A \<longrightarrow> P(s)) \<and> (~A \<longrightarrow> P(t)))"}
   465 @{prop"P(if A then s else t) = ((A \<longrightarrow> P(s)) \<and> (~A \<longrightarrow> P(t)))"}