doc-src/TutorialI/Advanced/document/simp.tex
changeset 11494 23a118849801
parent 11458 09a6c44a48ea
child 11866 fbd097aec213
equal deleted inserted replaced
11493:f3ff2549cdc8 11494:23a118849801
     5 \isamarkupsection{Simplification%
     5 \isamarkupsection{Simplification%
     6 }
     6 }
     7 %
     7 %
     8 \begin{isamarkuptext}%
     8 \begin{isamarkuptext}%
     9 \label{sec:simplification-II}\index{simplification|(}
     9 \label{sec:simplification-II}\index{simplification|(}
    10 This section discusses some additional nifty features not covered so far and
    10 This section describes features not covered until now.  It also
    11 gives a short introduction to the simplification process itself. The latter
    11 outlines the simplification process itself, which can be helpful
    12 is helpful to understand why a particular rule does or does not apply in some
    12 when the simplifier does not do what you expect of it.%
    13 situation.%
       
    14 \end{isamarkuptext}%
    13 \end{isamarkuptext}%
    15 %
    14 %
    16 \isamarkupsubsection{Advanced Features%
    15 \isamarkupsubsection{Advanced Features%
    17 }
    16 }
    18 %
    17 %
    19 \isamarkupsubsubsection{Congruence Rules%
    18 \isamarkupsubsubsection{Congruence Rules%
    20 }
    19 }
    21 %
    20 %
    22 \begin{isamarkuptext}%
    21 \begin{isamarkuptext}%
    23 \label{sec:simp-cong}
    22 \label{sec:simp-cong}
    24 It is hardwired into the simplifier that while simplifying the conclusion $Q$
    23 While simplifying the conclusion $Q$
    25 of $P \Imp Q$ it is legal to make uses of the assumption $P$. This
    24 of $P \Imp Q$, it is legal use the assumption $P$.
    26 kind of contextual information can also be made available for other
    25 For $\Imp$ this policy is hardwired, but 
       
    26 contextual information can also be made available for other
    27 operators. For example, \isa{xs\ {\isacharequal}\ {\isacharbrackleft}{\isacharbrackright}\ {\isasymlongrightarrow}\ xs\ {\isacharat}\ xs\ {\isacharequal}\ xs} simplifies to \isa{True} because we may use \isa{xs\ {\isacharequal}\ {\isacharbrackleft}{\isacharbrackright}} when simplifying \isa{xs\ {\isacharat}\ xs\ {\isacharequal}\ xs}. The generation of contextual information during simplification is
    27 operators. For example, \isa{xs\ {\isacharequal}\ {\isacharbrackleft}{\isacharbrackright}\ {\isasymlongrightarrow}\ xs\ {\isacharat}\ xs\ {\isacharequal}\ xs} simplifies to \isa{True} because we may use \isa{xs\ {\isacharequal}\ {\isacharbrackleft}{\isacharbrackright}} when simplifying \isa{xs\ {\isacharat}\ xs\ {\isacharequal}\ xs}. The generation of contextual information during simplification is
    28 controlled by so-called \bfindex{congruence rules}. This is the one for
    28 controlled by so-called \bfindex{congruence rules}. This is the one for
    29 \isa{{\isasymlongrightarrow}}:
    29 \isa{{\isasymlongrightarrow}}:
    30 \begin{isabelle}%
    30 \begin{isabelle}%
    31 \ \ \ \ \ {\isasymlbrakk}P\ {\isacharequal}\ P{\isacharprime}{\isacharsemicolon}\ P{\isacharprime}\ {\isasymLongrightarrow}\ Q\ {\isacharequal}\ Q{\isacharprime}{\isasymrbrakk}\ {\isasymLongrightarrow}\ {\isacharparenleft}P\ {\isasymlongrightarrow}\ Q{\isacharparenright}\ {\isacharequal}\ {\isacharparenleft}P{\isacharprime}\ {\isasymlongrightarrow}\ Q{\isacharprime}{\isacharparenright}%
    31 \ \ \ \ \ {\isasymlbrakk}P\ {\isacharequal}\ P{\isacharprime}{\isacharsemicolon}\ P{\isacharprime}\ {\isasymLongrightarrow}\ Q\ {\isacharequal}\ Q{\isacharprime}{\isasymrbrakk}\ {\isasymLongrightarrow}\ {\isacharparenleft}P\ {\isasymlongrightarrow}\ Q{\isacharparenright}\ {\isacharequal}\ {\isacharparenleft}P{\isacharprime}\ {\isasymlongrightarrow}\ Q{\isacharprime}{\isacharparenright}%
    39 quantifiers supply contextual information about the bound variable:
    39 quantifiers supply contextual information about the bound variable:
    40 \begin{isabelle}%
    40 \begin{isabelle}%
    41 \ \ \ \ \ {\isasymlbrakk}A\ {\isacharequal}\ B{\isacharsemicolon}\ {\isasymAnd}x{\isachardot}\ x\ {\isasymin}\ B\ {\isasymLongrightarrow}\ P\ x\ {\isacharequal}\ Q\ x{\isasymrbrakk}\isanewline
    41 \ \ \ \ \ {\isasymlbrakk}A\ {\isacharequal}\ B{\isacharsemicolon}\ {\isasymAnd}x{\isachardot}\ x\ {\isasymin}\ B\ {\isasymLongrightarrow}\ P\ x\ {\isacharequal}\ Q\ x{\isasymrbrakk}\isanewline
    42 \isaindent{\ \ \ \ \ }{\isasymLongrightarrow}\ {\isacharparenleft}{\isasymforall}x{\isasymin}A{\isachardot}\ P\ x{\isacharparenright}\ {\isacharequal}\ {\isacharparenleft}{\isasymforall}x{\isasymin}B{\isachardot}\ Q\ x{\isacharparenright}%
    42 \isaindent{\ \ \ \ \ }{\isasymLongrightarrow}\ {\isacharparenleft}{\isasymforall}x{\isasymin}A{\isachardot}\ P\ x{\isacharparenright}\ {\isacharequal}\ {\isacharparenleft}{\isasymforall}x{\isasymin}B{\isachardot}\ Q\ x{\isacharparenright}%
    43 \end{isabelle}
    43 \end{isabelle}
    44 The congruence rule for conditional expressions supplies contextual
    44 One congruence rule for conditional expressions supplies contextual
    45 information for simplifying the \isa{then} and \isa{else} cases:
    45 information for simplifying the \isa{then} and \isa{else} cases:
    46 \begin{isabelle}%
    46 \begin{isabelle}%
    47 \ \ \ \ \ {\isasymlbrakk}b\ {\isacharequal}\ c{\isacharsemicolon}\ c\ {\isasymLongrightarrow}\ x\ {\isacharequal}\ u{\isacharsemicolon}\ {\isasymnot}\ c\ {\isasymLongrightarrow}\ y\ {\isacharequal}\ v{\isasymrbrakk}\isanewline
    47 \ \ \ \ \ {\isasymlbrakk}b\ {\isacharequal}\ c{\isacharsemicolon}\ c\ {\isasymLongrightarrow}\ x\ {\isacharequal}\ u{\isacharsemicolon}\ {\isasymnot}\ c\ {\isasymLongrightarrow}\ y\ {\isacharequal}\ v{\isasymrbrakk}\isanewline
    48 \isaindent{\ \ \ \ \ }{\isasymLongrightarrow}\ {\isacharparenleft}if\ b\ then\ x\ else\ y{\isacharparenright}\ {\isacharequal}\ {\isacharparenleft}if\ c\ then\ u\ else\ v{\isacharparenright}%
    48 \isaindent{\ \ \ \ \ }{\isasymLongrightarrow}\ {\isacharparenleft}if\ b\ then\ x\ else\ y{\isacharparenright}\ {\isacharequal}\ {\isacharparenleft}if\ c\ then\ u\ else\ v{\isacharparenright}%
    49 \end{isabelle}
    49 \end{isabelle}
    50 A congruence rule can also \emph{prevent} simplification of some arguments.
    50 An alternative congruence rule for conditional expressions
    51 Here is an alternative congruence rule for conditional expressions:
    51 actually \emph{prevents} simplification of some arguments:
    52 \begin{isabelle}%
    52 \begin{isabelle}%
    53 \ \ \ \ \ b\ {\isacharequal}\ c\ {\isasymLongrightarrow}\ {\isacharparenleft}if\ b\ then\ x\ else\ y{\isacharparenright}\ {\isacharequal}\ {\isacharparenleft}if\ c\ then\ x\ else\ y{\isacharparenright}%
    53 \ \ \ \ \ b\ {\isacharequal}\ c\ {\isasymLongrightarrow}\ {\isacharparenleft}if\ b\ then\ x\ else\ y{\isacharparenright}\ {\isacharequal}\ {\isacharparenleft}if\ c\ then\ x\ else\ y{\isacharparenright}%
    54 \end{isabelle}
    54 \end{isabelle}
    55 Only the first argument is simplified; the others remain unchanged.
    55 Only the first argument is simplified; the others remain unchanged.
    56 This makes simplification much faster and is faithful to the evaluation
    56 This makes simplification much faster and is faithful to the evaluation
    81 %
    81 %
    82 \isamarkupsubsubsection{Permutative Rewrite Rules%
    82 \isamarkupsubsubsection{Permutative Rewrite Rules%
    83 }
    83 }
    84 %
    84 %
    85 \begin{isamarkuptext}%
    85 \begin{isamarkuptext}%
    86 \index{rewrite rule!permutative|bold}
    86 \index{rewrite rules!permutative|bold}%
    87 \index{rewriting!ordered|bold}
    87 An equation is a \textbf{permutative rewrite rule} if the left-hand
    88 \index{ordered rewriting|bold}
       
    89 \index{simplification!ordered|bold}
       
    90 An equation is a \bfindex{permutative rewrite rule} if the left-hand
       
    91 side and right-hand side are the same up to renaming of variables.  The most
    88 side and right-hand side are the same up to renaming of variables.  The most
    92 common permutative rule is commutativity: \isa{x\ {\isacharplus}\ y\ {\isacharequal}\ y\ {\isacharplus}\ x}.  Other examples
    89 common permutative rule is commutativity: \isa{x\ {\isacharplus}\ y\ {\isacharequal}\ y\ {\isacharplus}\ x}.  Other examples
    93 include \isa{x\ {\isacharminus}\ y\ {\isacharminus}\ z\ {\isacharequal}\ x\ {\isacharminus}\ z\ {\isacharminus}\ y} in arithmetic and \isa{insert\ x\ {\isacharparenleft}insert\ y\ A{\isacharparenright}\ {\isacharequal}\ insert\ y\ {\isacharparenleft}insert\ x\ A{\isacharparenright}} for sets. Such rules are problematic because
    90 include \isa{x\ {\isacharminus}\ y\ {\isacharminus}\ z\ {\isacharequal}\ x\ {\isacharminus}\ z\ {\isacharminus}\ y} in arithmetic and \isa{insert\ x\ {\isacharparenleft}insert\ y\ A{\isacharparenright}\ {\isacharequal}\ insert\ y\ {\isacharparenleft}insert\ x\ A{\isacharparenright}} for sets. Such rules are problematic because
    94 once they apply, they can be used forever. The simplifier is aware of this
    91 once they apply, they can be used forever. The simplifier is aware of this
    95 danger and treats permutative rules by means of a special strategy, called
    92 danger and treats permutative rules by means of a special strategy, called
   123 Note that ordered rewriting for \isa{{\isacharplus}} and \isa{{\isacharasterisk}} on numbers is rarely
   120 Note that ordered rewriting for \isa{{\isacharplus}} and \isa{{\isacharasterisk}} on numbers is rarely
   124 necessary because the built-in arithmetic prover often succeeds without
   121 necessary because the built-in arithmetic prover often succeeds without
   125 such tricks.%
   122 such tricks.%
   126 \end{isamarkuptext}%
   123 \end{isamarkuptext}%
   127 %
   124 %
   128 \isamarkupsubsection{How it Works%
   125 \isamarkupsubsection{How the Simplifier Works%
   129 }
   126 }
   130 %
   127 %
   131 \begin{isamarkuptext}%
   128 \begin{isamarkuptext}%
   132 \label{sec:SimpHow}
   129 \label{sec:SimpHow}
   133 Roughly speaking, the simplifier proceeds bottom-up (subterms are simplified
   130 Roughly speaking, the simplifier proceeds bottom-up: subterms are simplified
   134 first) and a conditional equation is only applied if its condition could be
   131 first.  A conditional equation is only applied if its condition can be
   135 proved (again by simplification). Below we explain some special features of the rewriting process.%
   132 proved, again by simplification.  Below we explain some special features of
       
   133 the rewriting process.%
   136 \end{isamarkuptext}%
   134 \end{isamarkuptext}%
   137 %
   135 %
   138 \isamarkupsubsubsection{Higher-Order Patterns%
   136 \isamarkupsubsubsection{Higher-Order Patterns%
   139 }
   137 }
   140 %
   138 %
   141 \begin{isamarkuptext}%
   139 \begin{isamarkuptext}%
   142 \index{simplification rule|(}
   140 \index{simplification rule|(}
   143 So far we have pretended the simplifier can deal with arbitrary
   141 So far we have pretended the simplifier can deal with arbitrary
   144 rewrite rules. This is not quite true.  Due to efficiency (and
   142 rewrite rules. This is not quite true.  For reasons of feasibility,
   145 potentially also computability) reasons, the simplifier expects the
   143 the simplifier expects the
   146 left-hand side of each rule to be a so-called \emph{higher-order
   144 left-hand side of each rule to be a so-called \emph{higher-order
   147 pattern}~\cite{nipkow-patterns}\indexbold{higher-order
   145 pattern}~\cite{nipkow-patterns}\indexbold{patterns!higher-order}. 
   148 pattern}\indexbold{pattern, higher-order}. This restricts where
   146 This restricts where
   149 unknowns may occur.  Higher-order patterns are terms in $\beta$-normal
   147 unknowns may occur.  Higher-order patterns are terms in $\beta$-normal
   150 form (this will always be the case unless you have done something
   148 form.  (This means there are no subterms of the form $(\lambda x. M)(N)$.)  
   151 strange) where each occurrence of an unknown is of the form
   149 Each occurrence of an unknown is of the form
   152 $\Var{f}~x@1~\dots~x@n$, where the $x@i$ are distinct bound
   150 $\Var{f}~x@1~\dots~x@n$, where the $x@i$ are distinct bound
   153 variables. Thus all ordinary rewrite rules, where all unknowns are
   151 variables. Thus all ordinary rewrite rules, where all unknowns are
   154 of base type, for example \isa{{\isacharquery}m\ {\isacharplus}\ {\isacharquery}n\ {\isacharplus}\ {\isacharquery}k\ {\isacharequal}\ {\isacharquery}m\ {\isacharplus}\ {\isacharparenleft}{\isacharquery}n\ {\isacharplus}\ {\isacharquery}k{\isacharparenright}}, are OK: if an unknown is
   152 of base type, for example \isa{{\isacharquery}m\ {\isacharplus}\ {\isacharquery}n\ {\isacharplus}\ {\isacharquery}k\ {\isacharequal}\ {\isacharquery}m\ {\isacharplus}\ {\isacharparenleft}{\isacharquery}n\ {\isacharplus}\ {\isacharquery}k{\isacharparenright}}, are acceptable: if an unknown is
   155 of base type, it cannot have any arguments. Additionally, the rule
   153 of base type, it cannot have any arguments. Additionally, the rule
   156 \isa{{\isacharparenleft}{\isasymforall}x{\isachardot}\ {\isacharquery}P\ x\ {\isasymand}\ {\isacharquery}Q\ x{\isacharparenright}\ {\isacharequal}\ {\isacharparenleft}{\isacharparenleft}{\isasymforall}x{\isachardot}\ {\isacharquery}P\ x{\isacharparenright}\ {\isasymand}\ {\isacharparenleft}{\isasymforall}x{\isachardot}\ {\isacharquery}Q\ x{\isacharparenright}{\isacharparenright}} is also OK, in
   154 \isa{{\isacharparenleft}{\isasymforall}x{\isachardot}\ {\isacharquery}P\ x\ {\isasymand}\ {\isacharquery}Q\ x{\isacharparenright}\ {\isacharequal}\ {\isacharparenleft}{\isacharparenleft}{\isasymforall}x{\isachardot}\ {\isacharquery}P\ x{\isacharparenright}\ {\isasymand}\ {\isacharparenleft}{\isasymforall}x{\isachardot}\ {\isacharquery}Q\ x{\isacharparenright}{\isacharparenright}} is also acceptable, in
   157 both directions: all arguments of the unknowns \isa{{\isacharquery}P} and
   155 both directions: all arguments of the unknowns \isa{{\isacharquery}P} and
   158 \isa{{\isacharquery}Q} are distinct bound variables.
   156 \isa{{\isacharquery}Q} are distinct bound variables.
   159 
   157 
   160 If the left-hand side is not a higher-order pattern, not all is lost
   158 If the left-hand side is not a higher-order pattern, all is not lost.
   161 and the simplifier will still try to apply the rule, but only if it
   159 The simplifier will still try to apply the rule provided it
   162 matches \emph{directly}, i.e.\ without much $\lambda$-calculus hocus
   160 matches directly: without much $\lambda$-calculus hocus
   163 pocus. For example, \isa{{\isacharquery}f\ {\isacharquery}x\ {\isasymin}\ range\ {\isacharquery}f\ {\isacharequal}\ True} rewrites
   161 pocus.  For example, \isa{{\isacharparenleft}{\isacharquery}f\ {\isacharquery}x\ {\isasymin}\ range\ {\isacharquery}f{\isacharparenright}\ {\isacharequal}\ True} rewrites
   164 \isa{g\ a\ {\isasymin}\ range\ g} to \isa{True}, but will fail to match
   162 \isa{g\ a\ {\isasymin}\ range\ g} to \isa{True}, but will fail to match
   165 \isa{g{\isacharparenleft}h\ b{\isacharparenright}\ {\isasymin}\ range{\isacharparenleft}{\isasymlambda}x{\isachardot}\ g{\isacharparenleft}h\ x{\isacharparenright}{\isacharparenright}}.  However, you can
   163 \isa{g{\isacharparenleft}h\ b{\isacharparenright}\ {\isasymin}\ range{\isacharparenleft}{\isasymlambda}x{\isachardot}\ g{\isacharparenleft}h\ x{\isacharparenright}{\isacharparenright}}.  However, you can
   166 replace the offending subterms (in our case \isa{{\isacharquery}f\ {\isacharquery}x}, which
   164 eliminate the offending subterms --- those that are not patterns ---
   167 is not a pattern) by adding new variables and conditions: \isa{{\isacharquery}y\ {\isacharequal}\ {\isacharquery}f\ {\isacharquery}x\ {\isasymLongrightarrow}\ {\isacharquery}y\ {\isasymin}\ range\ {\isacharquery}f\ {\isacharequal}\ True} is fine
   165 by adding new variables and conditions.
       
   166 In our example, we eliminate \isa{{\isacharquery}f\ {\isacharquery}x} and obtain
       
   167  \isa{{\isacharquery}y\ {\isacharequal}\ {\isacharquery}f\ {\isacharquery}x\ {\isasymLongrightarrow}\ {\isacharparenleft}{\isacharquery}y\ {\isasymin}\ range\ {\isacharquery}f{\isacharparenright}\ {\isacharequal}\ True}, which is fine
   168 as a conditional rewrite rule since conditions can be arbitrary
   168 as a conditional rewrite rule since conditions can be arbitrary
   169 terms. However, this trick is not a panacea because the newly
   169 terms.  However, this trick is not a panacea because the newly
   170 introduced conditions may be hard to solve.
   170 introduced conditions may be hard to solve.
   171   
   171   
   172 There is no restriction on the form of the right-hand
   172 There is no restriction on the form of the right-hand
   173 sides.  They may not contain extraneous term or type variables, though.%
   173 sides.  They may not contain extraneous term or type variables, though.%
   174 \end{isamarkuptext}%
   174 \end{isamarkuptext}%