doc-src/TutorialI/Datatype/ABexpr.thy
author paulson
Fri, 03 Aug 2001 18:04:55 +0200
changeset 11458 09a6c44a48ea
parent 11309 d666f11ca2d4
child 12334 60bf75e157e4
permissions -rw-r--r--
numerous stylistic changes and indexing
nipkow@8745
     1
(*<*)
nipkow@8745
     2
theory ABexpr = Main:;
nipkow@8745
     3
(*>*)
nipkow@8745
     4
nipkow@8745
     5
text{*
paulson@11458
     6
\index{datatypes!mutually recursive}%
nipkow@8745
     7
Sometimes it is necessary to define two datatypes that depend on each
nipkow@8745
     8
other. This is called \textbf{mutual recursion}. As an example consider a
nipkow@8745
     9
language of arithmetic and boolean expressions where
nipkow@8745
    10
\begin{itemize}
nipkow@8745
    11
\item arithmetic expressions contain boolean expressions because there are
paulson@11458
    12
  conditional expressions like ``if $m<n$ then $n-m$ else $m-n$'',
nipkow@8745
    13
  and
nipkow@8745
    14
\item boolean expressions contain arithmetic expressions because of
nipkow@8745
    15
  comparisons like ``$m<n$''.
nipkow@8745
    16
\end{itemize}
nipkow@8745
    17
In Isabelle this becomes
nipkow@8745
    18
*}
nipkow@8745
    19
nipkow@8745
    20
datatype 'a aexp = IF   "'a bexp" "'a aexp" "'a aexp"
nipkow@8745
    21
                 | Sum  "'a aexp" "'a aexp"
nipkow@8745
    22
                 | Diff "'a aexp" "'a aexp"
nipkow@8745
    23
                 | Var 'a
nipkow@8745
    24
                 | Num nat
nipkow@8745
    25
and      'a bexp = Less "'a aexp" "'a aexp"
nipkow@8745
    26
                 | And  "'a bexp" "'a bexp"
nipkow@8745
    27
                 | Neg  "'a bexp";
nipkow@8745
    28
nipkow@8745
    29
text{*\noindent
nipkow@9792
    30
Type @{text"aexp"} is similar to @{text"expr"} in \S\ref{sec:ExprCompiler},
paulson@11309
    31
except that we have added an @{text IF} constructor,
paulson@11309
    32
fixed the values to be of type @{typ"nat"} and declared the two binary
paulson@11309
    33
operations @{text Sum} and @{term"Diff"}.  Boolean
nipkow@8745
    34
expressions can be arithmetic comparisons, conjunctions and negations.
paulson@11458
    35
The semantics is given by two evaluation functions:
nipkow@8745
    36
*}
nipkow@8745
    37
nipkow@10971
    38
consts  evala :: "'a aexp \<Rightarrow> ('a \<Rightarrow> nat) \<Rightarrow> nat"
nipkow@10971
    39
        evalb :: "'a bexp \<Rightarrow> ('a \<Rightarrow> nat) \<Rightarrow> bool";
nipkow@8745
    40
nipkow@8745
    41
text{*\noindent
paulson@11458
    42
Both take an expression and an environment (a mapping from variables @{typ"'a"} to values
nipkow@9792
    43
@{typ"nat"}) and return its arithmetic/boolean
nipkow@8745
    44
value. Since the datatypes are mutually recursive, so are functions that
nipkow@8745
    45
operate on them. Hence they need to be defined in a single \isacommand{primrec}
nipkow@8745
    46
section:
nipkow@8745
    47
*}
nipkow@8745
    48
nipkow@8745
    49
primrec
nipkow@8771
    50
  "evala (IF b a1 a2) env =
nipkow@8771
    51
     (if evalb b env then evala a1 env else evala a2 env)"
nipkow@8771
    52
  "evala (Sum a1 a2) env = evala a1 env + evala a2 env"
nipkow@8771
    53
  "evala (Diff a1 a2) env = evala a1 env - evala a2 env"
nipkow@8771
    54
  "evala (Var v) env = env v"
nipkow@8771
    55
  "evala (Num n) env = n"
nipkow@8745
    56
nipkow@8771
    57
  "evalb (Less a1 a2) env = (evala a1 env < evala a2 env)"
nipkow@10971
    58
  "evalb (And b1 b2) env = (evalb b1 env \<and> evalb b2 env)"
nipkow@10971
    59
  "evalb (Neg b) env = (\<not> evalb b env)"
nipkow@8745
    60
nipkow@8745
    61
text{*\noindent
nipkow@8745
    62
In the same fashion we also define two functions that perform substitution:
nipkow@8745
    63
*}
nipkow@8745
    64
nipkow@10971
    65
consts substa :: "('a \<Rightarrow> 'b aexp) \<Rightarrow> 'a aexp \<Rightarrow> 'b aexp"
nipkow@10971
    66
       substb :: "('a \<Rightarrow> 'b aexp) \<Rightarrow> 'a bexp \<Rightarrow> 'b bexp";
nipkow@8745
    67
nipkow@8745
    68
text{*\noindent
nipkow@8745
    69
The first argument is a function mapping variables to expressions, the
nipkow@8745
    70
substitution. It is applied to all variables in the second argument. As a
nipkow@9792
    71
result, the type of variables in the expression may change from @{typ"'a"}
nipkow@9792
    72
to @{typ"'b"}. Note that there are only arithmetic and no boolean variables.
nipkow@8745
    73
*}
nipkow@8745
    74
nipkow@8745
    75
primrec
nipkow@8745
    76
  "substa s (IF b a1 a2) =
nipkow@8745
    77
     IF (substb s b) (substa s a1) (substa s a2)"
nipkow@8745
    78
  "substa s (Sum a1 a2) = Sum (substa s a1) (substa s a2)"
nipkow@8745
    79
  "substa s (Diff a1 a2) = Diff (substa s a1) (substa s a2)"
nipkow@8745
    80
  "substa s (Var v) = s v"
nipkow@8745
    81
  "substa s (Num n) = Num n"
nipkow@8745
    82
nipkow@8745
    83
  "substb s (Less a1 a2) = Less (substa s a1) (substa s a2)"
nipkow@8745
    84
  "substb s (And b1 b2) = And (substb s b1) (substb s b2)"
nipkow@8745
    85
  "substb s (Neg b) = Neg (substb s b)";
nipkow@8745
    86
nipkow@8745
    87
text{*
nipkow@8745
    88
Now we can prove a fundamental theorem about the interaction between
nipkow@8745
    89
evaluation and substitution: applying a substitution $s$ to an expression $a$
nipkow@8745
    90
and evaluating the result in an environment $env$ yields the same result as
nipkow@8745
    91
evaluation $a$ in the environment that maps every variable $x$ to the value
nipkow@8745
    92
of $s(x)$ under $env$. If you try to prove this separately for arithmetic or
nipkow@8745
    93
boolean expressions (by induction), you find that you always need the other
nipkow@8745
    94
theorem in the induction step. Therefore you need to state and prove both
nipkow@8745
    95
theorems simultaneously:
nipkow@8745
    96
*}
nipkow@8745
    97
nipkow@10971
    98
lemma "evala (substa s a) env = evala a (\<lambda>x. evala (s x) env) \<and>
nipkow@10971
    99
        evalb (substb s b) env = evalb b (\<lambda>x. evala (s x) env)";
nipkow@8745
   100
apply(induct_tac a and b);
nipkow@8745
   101
nipkow@8745
   102
txt{*\noindent
nipkow@8745
   103
The resulting 8 goals (one for each constructor) are proved in one fell swoop:
nipkow@8745
   104
*}
nipkow@8745
   105
nipkow@10171
   106
apply simp_all;
nipkow@10171
   107
(*<*)done(*>*)
nipkow@8745
   108
nipkow@8745
   109
text{*
nipkow@8745
   110
In general, given $n$ mutually recursive datatypes $\tau@1$, \dots, $\tau@n$,
nipkow@8745
   111
an inductive proof expects a goal of the form
nipkow@8745
   112
\[ P@1(x@1)\ \land \dots \land P@n(x@n) \]
nipkow@8745
   113
where each variable $x@i$ is of type $\tau@i$. Induction is started by
nipkow@9792
   114
\begin{isabelle}
nipkow@10971
   115
\isacommand{apply}@{text"(induct_tac"} $x@1$ \isacommand{and} \dots\ \isacommand{and} $x@n$@{text")"}
nipkow@9792
   116
\end{isabelle}
nipkow@8745
   117
nipkow@8745
   118
\begin{exercise}
nipkow@9792
   119
  Define a function @{text"norma"} of type @{typ"'a aexp => 'a aexp"} that
nipkow@9792
   120
  replaces @{term"IF"}s with complex boolean conditions by nested
paulson@11458
   121
  @{term"IF"}s; it should eliminate the constructors
paulson@11458
   122
  @{term"And"} and @{term"Neg"}, leaving only @{term"Less"}.
paulson@11458
   123
  Prove that @{text"norma"}
nipkow@9792
   124
  preserves the value of an expression and that the result of @{text"norma"}
nipkow@9792
   125
  is really normal, i.e.\ no more @{term"And"}s and @{term"Neg"}s occur in
nipkow@8745
   126
  it.  ({\em Hint:} proceed as in \S\ref{sec:boolex}).
nipkow@8745
   127
\end{exercise}
nipkow@8745
   128
*}
paulson@11458
   129
(*<*) 
nipkow@8745
   130
end
nipkow@8745
   131
(*>*)