doc-src/TutorialI/Ifexpr/document/Ifexpr.tex
author nipkow
Wed, 24 Jan 2001 12:29:10 +0100
changeset 10971 6852682eaf16
parent 10878 b254d5ad6dd4
child 10978 5eebea8f359f
permissions -rw-r--r--
*** empty log message ***
nipkow@9722
     1
%
nipkow@9722
     2
\begin{isabellebody}%
wenzelm@9924
     3
\def\isabellecontext{Ifexpr}%
nipkow@8749
     4
%
paulson@10878
     5
\isamarkupsubsection{Case Study: Boolean Expressions%
wenzelm@10395
     6
}
nipkow@9933
     7
%
nipkow@8749
     8
\begin{isamarkuptext}%
nipkow@9933
     9
\label{sec:boolex}
nipkow@9933
    10
The aim of this case study is twofold: it shows how to model boolean
nipkow@9933
    11
expressions and some algorithms for manipulating them, and it demonstrates
nipkow@9933
    12
the constructs introduced above.%
nipkow@9933
    13
\end{isamarkuptext}%
nipkow@9933
    14
%
paulson@10878
    15
\isamarkupsubsubsection{How Can We Model Boolean Expressions?%
wenzelm@10395
    16
}
nipkow@9933
    17
%
nipkow@9933
    18
\begin{isamarkuptext}%
nipkow@8749
    19
We want to represent boolean expressions built up from variables and
nipkow@8749
    20
constants by negation and conjunction. The following datatype serves exactly
nipkow@8749
    21
that purpose:%
nipkow@8749
    22
\end{isamarkuptext}%
wenzelm@9673
    23
\isacommand{datatype}\ boolex\ {\isacharequal}\ Const\ bool\ {\isacharbar}\ Var\ nat\ {\isacharbar}\ Neg\ boolex\isanewline
wenzelm@9673
    24
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ {\isacharbar}\ And\ boolex\ boolex%
nipkow@8749
    25
\begin{isamarkuptext}%
nipkow@8749
    26
\noindent
nipkow@9541
    27
The two constants are represented by \isa{Const\ True} and
nipkow@9541
    28
\isa{Const\ False}. Variables are represented by terms of the form
nipkow@9792
    29
\isa{Var\ n}, where \isa{n} is a natural number (type \isa{nat}).
nipkow@8749
    30
For example, the formula $P@0 \land \neg P@1$ is represented by the term
nipkow@10187
    31
\isa{And\ {\isacharparenleft}Var\ {\isadigit{0}}{\isacharparenright}\ {\isacharparenleft}Neg\ {\isacharparenleft}Var\ {\isadigit{1}}{\isacharparenright}{\isacharparenright}}.
nipkow@8749
    32
paulson@10878
    33
\subsubsection{What is the Value of a Boolean Expression?}
nipkow@8749
    34
nipkow@8749
    35
The value of a boolean expression depends on the value of its variables.
nipkow@9792
    36
Hence the function \isa{value} takes an additional parameter, an
nipkow@9792
    37
\emph{environment} of type \isa{nat\ {\isasymRightarrow}\ bool}, which maps variables to their
nipkow@9792
    38
values:%
nipkow@8749
    39
\end{isamarkuptext}%
wenzelm@9673
    40
\isacommand{consts}\ value\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequote}boolex\ {\isasymRightarrow}\ {\isacharparenleft}nat\ {\isasymRightarrow}\ bool{\isacharparenright}\ {\isasymRightarrow}\ bool{\isachardoublequote}\isanewline
nipkow@8749
    41
\isacommand{primrec}\isanewline
wenzelm@9673
    42
{\isachardoublequote}value\ {\isacharparenleft}Const\ b{\isacharparenright}\ env\ {\isacharequal}\ b{\isachardoublequote}\isanewline
wenzelm@9673
    43
{\isachardoublequote}value\ {\isacharparenleft}Var\ x{\isacharparenright}\ \ \ env\ {\isacharequal}\ env\ x{\isachardoublequote}\isanewline
wenzelm@9673
    44
{\isachardoublequote}value\ {\isacharparenleft}Neg\ b{\isacharparenright}\ \ \ env\ {\isacharequal}\ {\isacharparenleft}{\isasymnot}\ value\ b\ env{\isacharparenright}{\isachardoublequote}\isanewline
wenzelm@9673
    45
{\isachardoublequote}value\ {\isacharparenleft}And\ b\ c{\isacharparenright}\ env\ {\isacharequal}\ {\isacharparenleft}value\ b\ env\ {\isasymand}\ value\ c\ env{\isacharparenright}{\isachardoublequote}%
nipkow@8749
    46
\begin{isamarkuptext}%
nipkow@8749
    47
\noindent
paulson@10878
    48
\subsubsection{If-Expressions}
nipkow@8749
    49
nipkow@8749
    50
An alternative and often more efficient (because in a certain sense
nipkow@8749
    51
canonical) representation are so-called \emph{If-expressions} built up
nipkow@8749
    52
from constants (\isa{CIF}), variables (\isa{VIF}) and conditionals
nipkow@8749
    53
(\isa{IF}):%
nipkow@8749
    54
\end{isamarkuptext}%
wenzelm@9673
    55
\isacommand{datatype}\ ifex\ {\isacharequal}\ CIF\ bool\ {\isacharbar}\ VIF\ nat\ {\isacharbar}\ IF\ ifex\ ifex\ ifex%
nipkow@8749
    56
\begin{isamarkuptext}%
nipkow@8749
    57
\noindent
nipkow@10971
    58
The evaluation of If-expressions proceeds as for \isa{boolex}:%
nipkow@8749
    59
\end{isamarkuptext}%
wenzelm@9673
    60
\isacommand{consts}\ valif\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequote}ifex\ {\isasymRightarrow}\ {\isacharparenleft}nat\ {\isasymRightarrow}\ bool{\isacharparenright}\ {\isasymRightarrow}\ bool{\isachardoublequote}\isanewline
nipkow@8749
    61
\isacommand{primrec}\isanewline
wenzelm@9673
    62
{\isachardoublequote}valif\ {\isacharparenleft}CIF\ b{\isacharparenright}\ \ \ \ env\ {\isacharequal}\ b{\isachardoublequote}\isanewline
wenzelm@9673
    63
{\isachardoublequote}valif\ {\isacharparenleft}VIF\ x{\isacharparenright}\ \ \ \ env\ {\isacharequal}\ env\ x{\isachardoublequote}\isanewline
wenzelm@9673
    64
{\isachardoublequote}valif\ {\isacharparenleft}IF\ b\ t\ e{\isacharparenright}\ env\ {\isacharequal}\ {\isacharparenleft}if\ valif\ b\ env\ then\ valif\ t\ env\isanewline
wenzelm@9673
    65
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ else\ valif\ e\ env{\isacharparenright}{\isachardoublequote}%
nipkow@8749
    66
\begin{isamarkuptext}%
paulson@10878
    67
\subsubsection{Transformation Into and of If-Expressions}
nipkow@8749
    68
paulson@10878
    69
\REMARK{is this the title you wanted?}
nipkow@8749
    70
The type \isa{boolex} is close to the customary representation of logical
nipkow@8771
    71
formulae, whereas \isa{ifex} is designed for efficiency. It is easy to
nipkow@8749
    72
translate from \isa{boolex} into \isa{ifex}:%
nipkow@8749
    73
\end{isamarkuptext}%
nipkow@10187
    74
\isacommand{consts}\ bool{\isadigit{2}}if\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequote}boolex\ {\isasymRightarrow}\ ifex{\isachardoublequote}\isanewline
nipkow@8749
    75
\isacommand{primrec}\isanewline
nipkow@10187
    76
{\isachardoublequote}bool{\isadigit{2}}if\ {\isacharparenleft}Const\ b{\isacharparenright}\ {\isacharequal}\ CIF\ b{\isachardoublequote}\isanewline
nipkow@10187
    77
{\isachardoublequote}bool{\isadigit{2}}if\ {\isacharparenleft}Var\ x{\isacharparenright}\ \ \ {\isacharequal}\ VIF\ x{\isachardoublequote}\isanewline
nipkow@10187
    78
{\isachardoublequote}bool{\isadigit{2}}if\ {\isacharparenleft}Neg\ b{\isacharparenright}\ \ \ {\isacharequal}\ IF\ {\isacharparenleft}bool{\isadigit{2}}if\ b{\isacharparenright}\ {\isacharparenleft}CIF\ False{\isacharparenright}\ {\isacharparenleft}CIF\ True{\isacharparenright}{\isachardoublequote}\isanewline
nipkow@10187
    79
{\isachardoublequote}bool{\isadigit{2}}if\ {\isacharparenleft}And\ b\ c{\isacharparenright}\ {\isacharequal}\ IF\ {\isacharparenleft}bool{\isadigit{2}}if\ b{\isacharparenright}\ {\isacharparenleft}bool{\isadigit{2}}if\ c{\isacharparenright}\ {\isacharparenleft}CIF\ False{\isacharparenright}{\isachardoublequote}%
nipkow@8749
    80
\begin{isamarkuptext}%
nipkow@8749
    81
\noindent
nipkow@10187
    82
At last, we have something we can verify: that \isa{bool{\isadigit{2}}if} preserves the
nipkow@8749
    83
value of its argument:%
nipkow@8749
    84
\end{isamarkuptext}%
nipkow@10187
    85
\isacommand{lemma}\ {\isachardoublequote}valif\ {\isacharparenleft}bool{\isadigit{2}}if\ b{\isacharparenright}\ env\ {\isacharequal}\ value\ b\ env{\isachardoublequote}%
nipkow@8749
    86
\begin{isamarkuptxt}%
nipkow@8749
    87
\noindent
nipkow@8749
    88
The proof is canonical:%
nipkow@8749
    89
\end{isamarkuptxt}%
wenzelm@9673
    90
\isacommand{apply}{\isacharparenleft}induct{\isacharunderscore}tac\ b{\isacharparenright}\isanewline
nipkow@10171
    91
\isacommand{apply}{\isacharparenleft}auto{\isacharparenright}\isanewline
nipkow@10171
    92
\isacommand{done}%
nipkow@8749
    93
\begin{isamarkuptext}%
nipkow@8749
    94
\noindent
nipkow@8749
    95
In fact, all proofs in this case study look exactly like this. Hence we do
nipkow@8749
    96
not show them below.
nipkow@8749
    97
nipkow@8749
    98
More interesting is the transformation of If-expressions into a normal form
nipkow@8749
    99
where the first argument of \isa{IF} cannot be another \isa{IF} but
nipkow@8749
   100
must be a constant or variable. Such a normal form can be computed by
nipkow@9792
   101
repeatedly replacing a subterm of the form \isa{IF\ {\isacharparenleft}IF\ b\ x\ y{\isacharparenright}\ z\ u} by
nipkow@9792
   102
\isa{IF\ b\ {\isacharparenleft}IF\ x\ z\ u{\isacharparenright}\ {\isacharparenleft}IF\ y\ z\ u{\isacharparenright}}, which has the same value. The following
nipkow@8749
   103
primitive recursive functions perform this task:%
nipkow@8749
   104
\end{isamarkuptext}%
wenzelm@9673
   105
\isacommand{consts}\ normif\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequote}ifex\ {\isasymRightarrow}\ ifex\ {\isasymRightarrow}\ ifex\ {\isasymRightarrow}\ ifex{\isachardoublequote}\isanewline
nipkow@8749
   106
\isacommand{primrec}\isanewline
wenzelm@9673
   107
{\isachardoublequote}normif\ {\isacharparenleft}CIF\ b{\isacharparenright}\ \ \ \ t\ e\ {\isacharequal}\ IF\ {\isacharparenleft}CIF\ b{\isacharparenright}\ t\ e{\isachardoublequote}\isanewline
wenzelm@9673
   108
{\isachardoublequote}normif\ {\isacharparenleft}VIF\ x{\isacharparenright}\ \ \ \ t\ e\ {\isacharequal}\ IF\ {\isacharparenleft}VIF\ x{\isacharparenright}\ t\ e{\isachardoublequote}\isanewline
wenzelm@9673
   109
{\isachardoublequote}normif\ {\isacharparenleft}IF\ b\ t\ e{\isacharparenright}\ u\ f\ {\isacharequal}\ normif\ b\ {\isacharparenleft}normif\ t\ u\ f{\isacharparenright}\ {\isacharparenleft}normif\ e\ u\ f{\isacharparenright}{\isachardoublequote}\isanewline
nipkow@8749
   110
\isanewline
wenzelm@9673
   111
\isacommand{consts}\ norm\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequote}ifex\ {\isasymRightarrow}\ ifex{\isachardoublequote}\isanewline
nipkow@8749
   112
\isacommand{primrec}\isanewline
wenzelm@9673
   113
{\isachardoublequote}norm\ {\isacharparenleft}CIF\ b{\isacharparenright}\ \ \ \ {\isacharequal}\ CIF\ b{\isachardoublequote}\isanewline
wenzelm@9673
   114
{\isachardoublequote}norm\ {\isacharparenleft}VIF\ x{\isacharparenright}\ \ \ \ {\isacharequal}\ VIF\ x{\isachardoublequote}\isanewline
wenzelm@9673
   115
{\isachardoublequote}norm\ {\isacharparenleft}IF\ b\ t\ e{\isacharparenright}\ {\isacharequal}\ normif\ b\ {\isacharparenleft}norm\ t{\isacharparenright}\ {\isacharparenleft}norm\ e{\isacharparenright}{\isachardoublequote}%
nipkow@8749
   116
\begin{isamarkuptext}%
nipkow@8749
   117
\noindent
nipkow@8749
   118
Their interplay is a bit tricky, and we leave it to the reader to develop an
nipkow@8749
   119
intuitive understanding. Fortunately, Isabelle can help us to verify that the
nipkow@8749
   120
transformation preserves the value of the expression:%
nipkow@8749
   121
\end{isamarkuptext}%
wenzelm@9673
   122
\isacommand{theorem}\ {\isachardoublequote}valif\ {\isacharparenleft}norm\ b{\isacharparenright}\ env\ {\isacharequal}\ valif\ b\ env{\isachardoublequote}%
nipkow@8749
   123
\begin{isamarkuptext}%
nipkow@8749
   124
\noindent
nipkow@8749
   125
The proof is canonical, provided we first show the following simplification
nipkow@8749
   126
lemma (which also helps to understand what \isa{normif} does):%
nipkow@8749
   127
\end{isamarkuptext}%
wenzelm@9673
   128
\isacommand{lemma}\ {\isacharbrackleft}simp{\isacharbrackright}{\isacharcolon}\isanewline
wenzelm@9673
   129
\ \ {\isachardoublequote}{\isasymforall}t\ e{\isachardot}\ valif\ {\isacharparenleft}normif\ b\ t\ e{\isacharparenright}\ env\ {\isacharequal}\ valif\ {\isacharparenleft}IF\ b\ t\ e{\isacharparenright}\ env{\isachardoublequote}%
nipkow@8749
   130
\begin{isamarkuptext}%
nipkow@8749
   131
\noindent
nipkow@8749
   132
Note that the lemma does not have a name, but is implicitly used in the proof
nipkow@9792
   133
of the theorem shown above because of the \isa{{\isacharbrackleft}simp{\isacharbrackright}} attribute.
nipkow@8749
   134
nipkow@8749
   135
But how can we be sure that \isa{norm} really produces a normal form in
nipkow@8749
   136
the above sense? We define a function that tests If-expressions for normality%
nipkow@8749
   137
\end{isamarkuptext}%
wenzelm@9673
   138
\isacommand{consts}\ normal\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequote}ifex\ {\isasymRightarrow}\ bool{\isachardoublequote}\isanewline
nipkow@8749
   139
\isacommand{primrec}\isanewline
wenzelm@9673
   140
{\isachardoublequote}normal{\isacharparenleft}CIF\ b{\isacharparenright}\ {\isacharequal}\ True{\isachardoublequote}\isanewline
wenzelm@9673
   141
{\isachardoublequote}normal{\isacharparenleft}VIF\ x{\isacharparenright}\ {\isacharequal}\ True{\isachardoublequote}\isanewline
wenzelm@9673
   142
{\isachardoublequote}normal{\isacharparenleft}IF\ b\ t\ e{\isacharparenright}\ {\isacharequal}\ {\isacharparenleft}normal\ t\ {\isasymand}\ normal\ e\ {\isasymand}\isanewline
wenzelm@9673
   143
\ \ \ \ \ {\isacharparenleft}case\ b\ of\ CIF\ b\ {\isasymRightarrow}\ True\ {\isacharbar}\ VIF\ x\ {\isasymRightarrow}\ True\ {\isacharbar}\ IF\ x\ y\ z\ {\isasymRightarrow}\ False{\isacharparenright}{\isacharparenright}{\isachardoublequote}%
nipkow@8749
   144
\begin{isamarkuptext}%
nipkow@8749
   145
\noindent
nipkow@9792
   146
and prove \isa{normal\ {\isacharparenleft}norm\ b{\isacharparenright}}. Of course, this requires a lemma about
nipkow@8749
   147
normality of \isa{normif}:%
nipkow@8749
   148
\end{isamarkuptext}%
nipkow@9933
   149
\isacommand{lemma}{\isacharbrackleft}simp{\isacharbrackright}{\isacharcolon}\ {\isachardoublequote}{\isasymforall}t\ e{\isachardot}\ normal{\isacharparenleft}normif\ b\ t\ e{\isacharparenright}\ {\isacharequal}\ {\isacharparenleft}normal\ t\ {\isasymand}\ normal\ e{\isacharparenright}{\isachardoublequote}%
nipkow@9933
   150
\begin{isamarkuptext}%
nipkow@9933
   151
\medskip
paulson@10795
   152
How do we come up with the required lemmas? Try to prove the main theorems
paulson@10795
   153
without them and study carefully what \isa{auto} leaves unproved. This 
paulson@10795
   154
can provide the clue.  The necessity of universal quantification
nipkow@9933
   155
(\isa{{\isasymforall}t\ e}) in the two lemmas is explained in
nipkow@9933
   156
\S\ref{sec:InductionHeuristics}
nipkow@9933
   157
nipkow@9933
   158
\begin{exercise}
nipkow@9933
   159
  We strengthen the definition of a \isa{normal} If-expression as follows:
nipkow@9933
   160
  the first argument of all \isa{IF}s must be a variable. Adapt the above
nipkow@9933
   161
  development to this changed requirement. (Hint: you may need to formulate
nipkow@9933
   162
  some of the goals as implications (\isa{{\isasymlongrightarrow}}) rather than
nipkow@9933
   163
  equalities (\isa{{\isacharequal}}).)
nipkow@9933
   164
\end{exercise}%
nipkow@9933
   165
\end{isamarkuptext}%
nipkow@9933
   166
\end{isabellebody}%
wenzelm@9145
   167
%%% Local Variables:
wenzelm@9145
   168
%%% mode: latex
wenzelm@9145
   169
%%% TeX-master: "root"
wenzelm@9145
   170
%%% End: