doc-src/TutorialI/Advanced/simp.thy
author nipkow
Thu, 14 Sep 2000 17:46:00 +0200
changeset 9958 67f2920862c7
child 10186 499637e8f2c6
permissions -rw-r--r--
*** empty log message ***
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
9958
67f2920862c7 *** empty log message ***
nipkow
parents:
diff changeset
     1
(*<*)
67f2920862c7 *** empty log message ***
nipkow
parents:
diff changeset
     2
theory simp = Main:
67f2920862c7 *** empty log message ***
nipkow
parents:
diff changeset
     3
(*>*)
67f2920862c7 *** empty log message ***
nipkow
parents:
diff changeset
     4
67f2920862c7 *** empty log message ***
nipkow
parents:
diff changeset
     5
section{*Simplification*}
67f2920862c7 *** empty log message ***
nipkow
parents:
diff changeset
     6
67f2920862c7 *** empty log message ***
nipkow
parents:
diff changeset
     7
text{*\label{sec:simplification-II}\index{simplification|(}
67f2920862c7 *** empty log message ***
nipkow
parents:
diff changeset
     8
This section discusses some additional nifty features not covered so far and
67f2920862c7 *** empty log message ***
nipkow
parents:
diff changeset
     9
gives a short introduction to the simplification process itself. The latter
67f2920862c7 *** empty log message ***
nipkow
parents:
diff changeset
    10
is helpful to understand why a particular rule does or does not apply in some
67f2920862c7 *** empty log message ***
nipkow
parents:
diff changeset
    11
situation.
67f2920862c7 *** empty log message ***
nipkow
parents:
diff changeset
    12
*}
67f2920862c7 *** empty log message ***
nipkow
parents:
diff changeset
    13
67f2920862c7 *** empty log message ***
nipkow
parents:
diff changeset
    14
subsection{*Advanced features*}
67f2920862c7 *** empty log message ***
nipkow
parents:
diff changeset
    15
67f2920862c7 *** empty log message ***
nipkow
parents:
diff changeset
    16
subsubsection{*Congruence rules*}
67f2920862c7 *** empty log message ***
nipkow
parents:
diff changeset
    17
67f2920862c7 *** empty log message ***
nipkow
parents:
diff changeset
    18
text{*\label{sec:simp-cong}
67f2920862c7 *** empty log message ***
nipkow
parents:
diff changeset
    19
It is hardwired into the simplifier that while simplifying the conclusion $Q$
67f2920862c7 *** empty log message ***
nipkow
parents:
diff changeset
    20
of $P \isasymImp Q$ it is legal to make uses of the assumptions $P$. This
67f2920862c7 *** empty log message ***
nipkow
parents:
diff changeset
    21
kind of contextual information can also be made available for other
67f2920862c7 *** empty log message ***
nipkow
parents:
diff changeset
    22
operators. For example, @{prop"xs = [] --> xs@xs = xs"} simplifies to @{term
67f2920862c7 *** empty log message ***
nipkow
parents:
diff changeset
    23
True} because we may use @{prop"xs = []"} when simplifying @{prop"xs@xs =
67f2920862c7 *** empty log message ***
nipkow
parents:
diff changeset
    24
xs"}. The generation of contextual information during simplification is
67f2920862c7 *** empty log message ***
nipkow
parents:
diff changeset
    25
controlled by so-called \bfindex{congruence rules}. This is the one for
67f2920862c7 *** empty log message ***
nipkow
parents:
diff changeset
    26
@{text"\<longrightarrow>"}:
67f2920862c7 *** empty log message ***
nipkow
parents:
diff changeset
    27
@{thm[display]imp_cong[no_vars]}
67f2920862c7 *** empty log message ***
nipkow
parents:
diff changeset
    28
It should be read as follows:
67f2920862c7 *** empty log message ***
nipkow
parents:
diff changeset
    29
In order to simplify @{prop"P-->Q"} to @{prop"P'-->Q'"},
67f2920862c7 *** empty log message ***
nipkow
parents:
diff changeset
    30
simplify @{prop P} to @{prop P'}
67f2920862c7 *** empty log message ***
nipkow
parents:
diff changeset
    31
and assume @{prop"P'"} when simplifying @{prop Q} to @{prop"Q'"}.
67f2920862c7 *** empty log message ***
nipkow
parents:
diff changeset
    32
67f2920862c7 *** empty log message ***
nipkow
parents:
diff changeset
    33
Here are some more examples.  The congruence rules for bounded
67f2920862c7 *** empty log message ***
nipkow
parents:
diff changeset
    34
quantifiers supply contextual information about the bound variable:
67f2920862c7 *** empty log message ***
nipkow
parents:
diff changeset
    35
@{thm[display,eta_contract=false,margin=60]ball_cong[no_vars]}
67f2920862c7 *** empty log message ***
nipkow
parents:
diff changeset
    36
The congruence rule for conditional expressions supply contextual
67f2920862c7 *** empty log message ***
nipkow
parents:
diff changeset
    37
information for simplifying the arms:
67f2920862c7 *** empty log message ***
nipkow
parents:
diff changeset
    38
@{thm[display]if_cong[no_vars]}
67f2920862c7 *** empty log message ***
nipkow
parents:
diff changeset
    39
A congruence rule can also \emph{prevent} simplification of some arguments.
67f2920862c7 *** empty log message ***
nipkow
parents:
diff changeset
    40
Here is an alternative congruence rule for conditional expressions:
67f2920862c7 *** empty log message ***
nipkow
parents:
diff changeset
    41
@{thm[display]if_weak_cong[no_vars]}
67f2920862c7 *** empty log message ***
nipkow
parents:
diff changeset
    42
Only the first argument is simplified; the others remain unchanged.
67f2920862c7 *** empty log message ***
nipkow
parents:
diff changeset
    43
This makes simplification much faster and is faithful to the evaluation
67f2920862c7 *** empty log message ***
nipkow
parents:
diff changeset
    44
strategy in programming languages, which is why this is the default
67f2920862c7 *** empty log message ***
nipkow
parents:
diff changeset
    45
congruence rule for @{text if}. Analogous rules control the evaluaton of
67f2920862c7 *** empty log message ***
nipkow
parents:
diff changeset
    46
@{text case} expressions.
67f2920862c7 *** empty log message ***
nipkow
parents:
diff changeset
    47
67f2920862c7 *** empty log message ***
nipkow
parents:
diff changeset
    48
You can delare your own congruence rules with the attribute @{text cong},
67f2920862c7 *** empty log message ***
nipkow
parents:
diff changeset
    49
either globally, in the usual manner,
67f2920862c7 *** empty log message ***
nipkow
parents:
diff changeset
    50
\begin{quote}
67f2920862c7 *** empty log message ***
nipkow
parents:
diff changeset
    51
\isacommand{declare} \textit{theorem-name} @{text"[cong]"}
67f2920862c7 *** empty log message ***
nipkow
parents:
diff changeset
    52
\end{quote}
67f2920862c7 *** empty log message ***
nipkow
parents:
diff changeset
    53
or locally in a @{text"simp"} call by adding the modifier
67f2920862c7 *** empty log message ***
nipkow
parents:
diff changeset
    54
\begin{quote}
67f2920862c7 *** empty log message ***
nipkow
parents:
diff changeset
    55
@{text"cong:"} \textit{list of theorem names}
67f2920862c7 *** empty log message ***
nipkow
parents:
diff changeset
    56
\end{quote}
67f2920862c7 *** empty log message ***
nipkow
parents:
diff changeset
    57
The effect is reversed by @{text"cong del"} instead of @{text cong}.
67f2920862c7 *** empty log message ***
nipkow
parents:
diff changeset
    58
67f2920862c7 *** empty log message ***
nipkow
parents:
diff changeset
    59
\begin{warn}
67f2920862c7 *** empty log message ***
nipkow
parents:
diff changeset
    60
The congruence rule @{thm[source]conj_cong}
67f2920862c7 *** empty log message ***
nipkow
parents:
diff changeset
    61
@{thm[display]conj_cong[no_vars]}
67f2920862c7 *** empty log message ***
nipkow
parents:
diff changeset
    62
is occasionally useful but not a default rule; you have to use it explicitly.
67f2920862c7 *** empty log message ***
nipkow
parents:
diff changeset
    63
\end{warn}
67f2920862c7 *** empty log message ***
nipkow
parents:
diff changeset
    64
*}
67f2920862c7 *** empty log message ***
nipkow
parents:
diff changeset
    65
67f2920862c7 *** empty log message ***
nipkow
parents:
diff changeset
    66
subsubsection{*Permutative rewrite rules*}
67f2920862c7 *** empty log message ***
nipkow
parents:
diff changeset
    67
67f2920862c7 *** empty log message ***
nipkow
parents:
diff changeset
    68
text{*
67f2920862c7 *** empty log message ***
nipkow
parents:
diff changeset
    69
\index{rewrite rule!permutative|bold}
67f2920862c7 *** empty log message ***
nipkow
parents:
diff changeset
    70
\index{rewriting!ordered|bold}
67f2920862c7 *** empty log message ***
nipkow
parents:
diff changeset
    71
\index{ordered rewriting|bold}
67f2920862c7 *** empty log message ***
nipkow
parents:
diff changeset
    72
\index{simplification!ordered|bold}
67f2920862c7 *** empty log message ***
nipkow
parents:
diff changeset
    73
An equation is a \bfindex{permutative rewrite rule} if the left-hand
67f2920862c7 *** empty log message ***
nipkow
parents:
diff changeset
    74
side and right-hand side are the same up to renaming of variables.  The most
67f2920862c7 *** empty log message ***
nipkow
parents:
diff changeset
    75
common permutative rule is commutativity: @{prop"x+y = y+x"}.  Other examples
67f2920862c7 *** empty log message ***
nipkow
parents:
diff changeset
    76
include @{prop"(x-y)-z = (x-z)-y"} in arithmetic and @{prop"insert x (insert
67f2920862c7 *** empty log message ***
nipkow
parents:
diff changeset
    77
y A) = insert y (insert x A)"} for sets. Such rules are problematic because
67f2920862c7 *** empty log message ***
nipkow
parents:
diff changeset
    78
once they apply, they can be used forever. The simplifier is aware of this
67f2920862c7 *** empty log message ***
nipkow
parents:
diff changeset
    79
danger and treats permutative rules by means of a special strategy, called
67f2920862c7 *** empty log message ***
nipkow
parents:
diff changeset
    80
\bfindex{ordered rewriting}: a permutative rewrite
67f2920862c7 *** empty log message ***
nipkow
parents:
diff changeset
    81
rule is only applied if the term becomes ``smaller'' (w.r.t.\ some fixed
67f2920862c7 *** empty log message ***
nipkow
parents:
diff changeset
    82
lexicographic ordering on terms). For example, commutativity rewrites
67f2920862c7 *** empty log message ***
nipkow
parents:
diff changeset
    83
@{term"b+a"} to @{term"a+b"}, but then stops because @{term"a+b"} is strictly
67f2920862c7 *** empty log message ***
nipkow
parents:
diff changeset
    84
smaller than @{term"b+a"}.  Permutative rewrite rules can be turned into
67f2920862c7 *** empty log message ***
nipkow
parents:
diff changeset
    85
simplification rules in the usual manner via the @{text simp} attribute; the
67f2920862c7 *** empty log message ***
nipkow
parents:
diff changeset
    86
simplifier recognizes their special status automatically.
67f2920862c7 *** empty log message ***
nipkow
parents:
diff changeset
    87
67f2920862c7 *** empty log message ***
nipkow
parents:
diff changeset
    88
Permutative rewrite rules are most effective in the case of
67f2920862c7 *** empty log message ***
nipkow
parents:
diff changeset
    89
associative-commutative operators.  (Associativity by itself is not
67f2920862c7 *** empty log message ***
nipkow
parents:
diff changeset
    90
permutative.)  When dealing with an AC-operator~$f$, keep the
67f2920862c7 *** empty log message ***
nipkow
parents:
diff changeset
    91
following points in mind:
67f2920862c7 *** empty log message ***
nipkow
parents:
diff changeset
    92
\begin{itemize}\index{associative-commutative operators}
67f2920862c7 *** empty log message ***
nipkow
parents:
diff changeset
    93
  
67f2920862c7 *** empty log message ***
nipkow
parents:
diff changeset
    94
\item The associative law must always be oriented from left to right,
67f2920862c7 *** empty log message ***
nipkow
parents:
diff changeset
    95
  namely $f(f(x,y),z) = f(x,f(y,z))$.  The opposite orientation, if
67f2920862c7 *** empty log message ***
nipkow
parents:
diff changeset
    96
  used with commutativity, can lead to nontermination.
67f2920862c7 *** empty log message ***
nipkow
parents:
diff changeset
    97
67f2920862c7 *** empty log message ***
nipkow
parents:
diff changeset
    98
\item To complete your set of rewrite rules, you must add not just
67f2920862c7 *** empty log message ***
nipkow
parents:
diff changeset
    99
  associativity~(A) and commutativity~(C) but also a derived rule, {\bf
67f2920862c7 *** empty log message ***
nipkow
parents:
diff changeset
   100
    left-com\-mut\-ativ\-ity} (LC): $f(x,f(y,z)) = f(y,f(x,z))$.
67f2920862c7 *** empty log message ***
nipkow
parents:
diff changeset
   101
\end{itemize}
67f2920862c7 *** empty log message ***
nipkow
parents:
diff changeset
   102
Ordered rewriting with the combination of A, C, and LC sorts a term
67f2920862c7 *** empty log message ***
nipkow
parents:
diff changeset
   103
lexicographically:
67f2920862c7 *** empty log message ***
nipkow
parents:
diff changeset
   104
\[\def\maps#1{~\stackrel{#1}{\leadsto}~}
67f2920862c7 *** empty log message ***
nipkow
parents:
diff changeset
   105
 f(f(b,c),a) \maps{A} f(b,f(c,a)) \maps{C} f(b,f(a,c)) \maps{LC} f(a,f(b,c)) \]
67f2920862c7 *** empty log message ***
nipkow
parents:
diff changeset
   106
67f2920862c7 *** empty log message ***
nipkow
parents:
diff changeset
   107
Note that ordered rewriting for @{text"+"} and @{text"*"} on numbers is rarely
67f2920862c7 *** empty log message ***
nipkow
parents:
diff changeset
   108
necessary because the builtin arithmetic capabilities often take care of
67f2920862c7 *** empty log message ***
nipkow
parents:
diff changeset
   109
this.
67f2920862c7 *** empty log message ***
nipkow
parents:
diff changeset
   110
*}
67f2920862c7 *** empty log message ***
nipkow
parents:
diff changeset
   111
67f2920862c7 *** empty log message ***
nipkow
parents:
diff changeset
   112
subsection{*How it works*}
67f2920862c7 *** empty log message ***
nipkow
parents:
diff changeset
   113
67f2920862c7 *** empty log message ***
nipkow
parents:
diff changeset
   114
text{*\label{sec:SimpHow}
67f2920862c7 *** empty log message ***
nipkow
parents:
diff changeset
   115
Roughly speaking, the simplifier proceeds bottom-up (subterms are simplified
67f2920862c7 *** empty log message ***
nipkow
parents:
diff changeset
   116
first) and a conditional equation is only applied if its condition could be
67f2920862c7 *** empty log message ***
nipkow
parents:
diff changeset
   117
proved (again by simplification). Below we explain some special 
67f2920862c7 *** empty log message ***
nipkow
parents:
diff changeset
   118
*}
67f2920862c7 *** empty log message ***
nipkow
parents:
diff changeset
   119
67f2920862c7 *** empty log message ***
nipkow
parents:
diff changeset
   120
subsubsection{*Higher-order patterns*}
67f2920862c7 *** empty log message ***
nipkow
parents:
diff changeset
   121
67f2920862c7 *** empty log message ***
nipkow
parents:
diff changeset
   122
subsubsection{*Local assumptions*}
67f2920862c7 *** empty log message ***
nipkow
parents:
diff changeset
   123
67f2920862c7 *** empty log message ***
nipkow
parents:
diff changeset
   124
subsubsection{*The preprocessor*}
67f2920862c7 *** empty log message ***
nipkow
parents:
diff changeset
   125
67f2920862c7 *** empty log message ***
nipkow
parents:
diff changeset
   126
text{*
67f2920862c7 *** empty log message ***
nipkow
parents:
diff changeset
   127
\index{simplification|)}
67f2920862c7 *** empty log message ***
nipkow
parents:
diff changeset
   128
*}
67f2920862c7 *** empty log message ***
nipkow
parents:
diff changeset
   129
(*<*)
67f2920862c7 *** empty log message ***
nipkow
parents:
diff changeset
   130
end
67f2920862c7 *** empty log message ***
nipkow
parents:
diff changeset
   131
(*>*)