doc-src/TutorialI/Ifexpr/Ifexpr.thy
author nipkow
Tue, 05 Sep 2000 13:53:39 +0200
changeset 9844 8016321c7de1
parent 9792 bbefb6ce5cb2
child 9933 9feb1e0c4cb3
permissions -rw-r--r--
*** empty log message ***
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
8745
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
     1
(*<*)
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
     2
theory Ifexpr = Main:;
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
     3
(*>*)
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
     4
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
     5
text{*
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
     6
\subsubsection{How can we model boolean expressions?}
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
     7
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
     8
We want to represent boolean expressions built up from variables and
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
     9
constants by negation and conjunction. The following datatype serves exactly
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    10
that purpose:
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    11
*}
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    12
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    13
datatype boolex = Const bool | Var nat | Neg boolex
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    14
                | And boolex boolex;
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    15
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    16
text{*\noindent
9541
d17c0b34d5c8 *** empty log message ***
nipkow
parents: 9458
diff changeset
    17
The two constants are represented by @{term"Const True"} and
d17c0b34d5c8 *** empty log message ***
nipkow
parents: 9458
diff changeset
    18
@{term"Const False"}. Variables are represented by terms of the form
9792
bbefb6ce5cb2 *** empty log message ***
nipkow
parents: 9541
diff changeset
    19
@{term"Var n"}, where @{term"n"} is a natural number (type @{typ"nat"}).
8745
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    20
For example, the formula $P@0 \land \neg P@1$ is represented by the term
9541
d17c0b34d5c8 *** empty log message ***
nipkow
parents: 9458
diff changeset
    21
@{term"And (Var 0) (Neg(Var 1))"}.
8745
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    22
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    23
\subsubsection{What is the value of a boolean expression?}
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    24
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    25
The value of a boolean expression depends on the value of its variables.
9792
bbefb6ce5cb2 *** empty log message ***
nipkow
parents: 9541
diff changeset
    26
Hence the function @{text"value"} takes an additional parameter, an
bbefb6ce5cb2 *** empty log message ***
nipkow
parents: 9541
diff changeset
    27
\emph{environment} of type @{typ"nat => bool"}, which maps variables to their
bbefb6ce5cb2 *** empty log message ***
nipkow
parents: 9541
diff changeset
    28
values:
8745
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    29
*}
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    30
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    31
consts value :: "boolex \\<Rightarrow> (nat \\<Rightarrow> bool) \\<Rightarrow> bool";
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    32
primrec
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    33
"value (Const b) env = b"
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    34
"value (Var x)   env = env x"
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    35
"value (Neg b)   env = (\\<not> value b env)"
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    36
"value (And b c) env = (value b env \\<and> value c env)";
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    37
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    38
text{*\noindent
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    39
\subsubsection{If-expressions}
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    40
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    41
An alternative and often more efficient (because in a certain sense
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    42
canonical) representation are so-called \emph{If-expressions} built up
9792
bbefb6ce5cb2 *** empty log message ***
nipkow
parents: 9541
diff changeset
    43
from constants (@{term"CIF"}), variables (@{term"VIF"}) and conditionals
bbefb6ce5cb2 *** empty log message ***
nipkow
parents: 9541
diff changeset
    44
(@{term"IF"}):
8745
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    45
*}
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    46
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    47
datatype ifex = CIF bool | VIF nat | IF ifex ifex ifex;
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    48
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    49
text{*\noindent
9792
bbefb6ce5cb2 *** empty log message ***
nipkow
parents: 9541
diff changeset
    50
The evaluation if If-expressions proceeds as for @{typ"boolex"}:
8745
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    51
*}
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    52
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    53
consts valif :: "ifex \\<Rightarrow> (nat \\<Rightarrow> bool) \\<Rightarrow> bool";
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    54
primrec
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    55
"valif (CIF b)    env = b"
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    56
"valif (VIF x)    env = env x"
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    57
"valif (IF b t e) env = (if valif b env then valif t env
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    58
                                        else valif e env)";
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    59
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    60
text{*
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    61
\subsubsection{Transformation into and of If-expressions}
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    62
9792
bbefb6ce5cb2 *** empty log message ***
nipkow
parents: 9541
diff changeset
    63
The type @{typ"boolex"} is close to the customary representation of logical
bbefb6ce5cb2 *** empty log message ***
nipkow
parents: 9541
diff changeset
    64
formulae, whereas @{typ"ifex"} is designed for efficiency. It is easy to
bbefb6ce5cb2 *** empty log message ***
nipkow
parents: 9541
diff changeset
    65
translate from @{typ"boolex"} into @{typ"ifex"}:
8745
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    66
*}
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    67
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    68
consts bool2if :: "boolex \\<Rightarrow> ifex";
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    69
primrec
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    70
"bool2if (Const b) = CIF b"
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    71
"bool2if (Var x)   = VIF x"
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    72
"bool2if (Neg b)   = IF (bool2if b) (CIF False) (CIF True)"
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    73
"bool2if (And b c) = IF (bool2if b) (bool2if c) (CIF False)";
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    74
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    75
text{*\noindent
9792
bbefb6ce5cb2 *** empty log message ***
nipkow
parents: 9541
diff changeset
    76
At last, we have something we can verify: that @{term"bool2if"} preserves the
8745
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    77
value of its argument:
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    78
*}
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    79
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    80
lemma "valif (bool2if b) env = value b env";
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    81
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    82
txt{*\noindent
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    83
The proof is canonical:
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    84
*}
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    85
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    86
apply(induct_tac b);
9458
c613cd06d5cf apply. -> by
nipkow
parents: 8771
diff changeset
    87
by(auto);
8745
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    88
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    89
text{*\noindent
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    90
In fact, all proofs in this case study look exactly like this. Hence we do
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    91
not show them below.
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    92
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    93
More interesting is the transformation of If-expressions into a normal form
9792
bbefb6ce5cb2 *** empty log message ***
nipkow
parents: 9541
diff changeset
    94
where the first argument of @{term"IF"} cannot be another @{term"IF"} but
8745
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    95
must be a constant or variable. Such a normal form can be computed by
9541
d17c0b34d5c8 *** empty log message ***
nipkow
parents: 9458
diff changeset
    96
repeatedly replacing a subterm of the form @{term"IF (IF b x y) z u"} by
d17c0b34d5c8 *** empty log message ***
nipkow
parents: 9458
diff changeset
    97
@{term"IF b (IF x z u) (IF y z u)"}, which has the same value. The following
8745
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    98
primitive recursive functions perform this task:
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
    99
*}
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
   100
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
   101
consts normif :: "ifex \\<Rightarrow> ifex \\<Rightarrow> ifex \\<Rightarrow> ifex";
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
   102
primrec
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
   103
"normif (CIF b)    t e = IF (CIF b) t e"
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
   104
"normif (VIF x)    t e = IF (VIF x) t e"
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
   105
"normif (IF b t e) u f = normif b (normif t u f) (normif e u f)";
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
   106
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
   107
consts norm :: "ifex \\<Rightarrow> ifex";
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
   108
primrec
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
   109
"norm (CIF b)    = CIF b"
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
   110
"norm (VIF x)    = VIF x"
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
   111
"norm (IF b t e) = normif b (norm t) (norm e)";
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
   112
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
   113
text{*\noindent
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
   114
Their interplay is a bit tricky, and we leave it to the reader to develop an
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
   115
intuitive understanding. Fortunately, Isabelle can help us to verify that the
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
   116
transformation preserves the value of the expression:
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
   117
*}
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
   118
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
   119
theorem "valif (norm b) env = valif b env";(*<*)oops;(*>*)
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
   120
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
   121
text{*\noindent
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
   122
The proof is canonical, provided we first show the following simplification
9792
bbefb6ce5cb2 *** empty log message ***
nipkow
parents: 9541
diff changeset
   123
lemma (which also helps to understand what @{term"normif"} does):
8745
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
   124
*}
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
   125
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
   126
lemma [simp]:
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
   127
  "\\<forall>t e. valif (normif b t e) env = valif (IF b t e) env";
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
   128
(*<*)
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
   129
apply(induct_tac b);
9458
c613cd06d5cf apply. -> by
nipkow
parents: 8771
diff changeset
   130
by(auto);
8745
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
   131
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
   132
theorem "valif (norm b) env = valif b env";
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
   133
apply(induct_tac b);
9458
c613cd06d5cf apply. -> by
nipkow
parents: 8771
diff changeset
   134
by(auto);
8745
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
   135
(*>*)
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
   136
text{*\noindent
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
   137
Note that the lemma does not have a name, but is implicitly used in the proof
9792
bbefb6ce5cb2 *** empty log message ***
nipkow
parents: 9541
diff changeset
   138
of the theorem shown above because of the @{text"[simp]"} attribute.
8745
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
   139
9792
bbefb6ce5cb2 *** empty log message ***
nipkow
parents: 9541
diff changeset
   140
But how can we be sure that @{term"norm"} really produces a normal form in
8745
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
   141
the above sense? We define a function that tests If-expressions for normality
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
   142
*}
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
   143
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
   144
consts normal :: "ifex \\<Rightarrow> bool";
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
   145
primrec
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
   146
"normal(CIF b) = True"
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
   147
"normal(VIF x) = True"
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
   148
"normal(IF b t e) = (normal t \\<and> normal e \\<and>
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
   149
     (case b of CIF b \\<Rightarrow> True | VIF x \\<Rightarrow> True | IF x y z \\<Rightarrow> False))";
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
   150
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
   151
text{*\noindent
9792
bbefb6ce5cb2 *** empty log message ***
nipkow
parents: 9541
diff changeset
   152
and prove @{term"normal(norm b)"}. Of course, this requires a lemma about
bbefb6ce5cb2 *** empty log message ***
nipkow
parents: 9541
diff changeset
   153
normality of @{term"normif"}:
8745
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
   154
*}
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
   155
9458
c613cd06d5cf apply. -> by
nipkow
parents: 8771
diff changeset
   156
lemma[simp]: "\\<forall>t e. normal(normif b t e) = (normal t \\<and> normal e)";
8771
026f37a86ea7 *** empty log message ***
nipkow
parents: 8745
diff changeset
   157
(*<*)
8745
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
   158
apply(induct_tac b);
9458
c613cd06d5cf apply. -> by
nipkow
parents: 8771
diff changeset
   159
by(auto);
8745
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
   160
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
   161
theorem "normal(norm b)";
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
   162
apply(induct_tac b);
9458
c613cd06d5cf apply. -> by
nipkow
parents: 8771
diff changeset
   163
by(auto);
8745
13b32661dde4 I wonder which files i forgot.
nipkow
parents:
diff changeset
   164
8771
026f37a86ea7 *** empty log message ***
nipkow
parents: 8745
diff changeset
   165
end
026f37a86ea7 *** empty log message ***
nipkow
parents: 8745
diff changeset
   166
(*>*)