diff -r 13b32661dde4 -r ccbb5e0dccdf doc-src/TutorialI/CodeGen/document/CodeGen.tex --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/doc-src/TutorialI/CodeGen/document/CodeGen.tex Wed Apr 19 12:54:56 2000 +0200 @@ -0,0 +1,113 @@ +\begin{isabelle}% +% +\begin{isamarkuptext}% +\noindent +The task is to develop a compiler from a generic type of expressions (built +up from variables, constants and binary operations) to a stack machine. This +generic type of expressions is a generalization of the boolean expressions in +\S\ref{sec:boolex}. This time we do not commit ourselves to a particular +type of variables or values but make them type parameters. Neither is there +a fixed set of binary operations: instead the expression contains the +appropriate function itself.% +\end{isamarkuptext}% +\isacommand{types}~'v~binop~=~{"}'v~{\isasymRightarrow}~'v~{\isasymRightarrow}~'v{"}\isanewline +\isacommand{datatype}~('a,'v)expr~=~Cex~'v\isanewline +~~~~~~~~~~~~~~~~~~~~~|~Vex~'a\isanewline +~~~~~~~~~~~~~~~~~~~~~|~Bex~{"}'v~binop{"}~~{"}('a,'v)expr{"}~~{"}('a,'v)expr{"}% +\begin{isamarkuptext}% +\noindent +The three constructors represent constants, variables and the combination of +two subexpressions with a binary operation. + +The value of an expression w.r.t.\ an environment that maps variables to +values is easily defined:% +\end{isamarkuptext}% +\isacommand{consts}~value~::~{"}('a~{\isasymRightarrow}~'v)~{\isasymRightarrow}~('a,'v)expr~{\isasymRightarrow}~'v{"}\isanewline +\isacommand{primrec}\isanewline +{"}value~env~(Cex~v)~=~v{"}\isanewline +{"}value~env~(Vex~a)~=~env~a{"}\isanewline +{"}value~env~(Bex~f~e1~e2)~=~f~(value~env~e1)~(value~env~e2){"}% +\begin{isamarkuptext}% +The stack machine has three instructions: load a constant value onto the +stack, load the contents of a certain address onto the stack, and apply a +binary operation to the two topmost elements of the stack, replacing them by +the result. As for \isa{expr}, addresses and values are type parameters:% +\end{isamarkuptext}% +\isacommand{datatype}~('a,'v)~instr~=~Const~'v\isanewline +~~~~~~~~~~~~~~~~~~~~~~~|~Load~'a\isanewline +~~~~~~~~~~~~~~~~~~~~~~~|~Apply~{"}'v~binop{"}% +\begin{isamarkuptext}% +The execution of the stack machine is modelled by a function \isa{exec} +that takes a store (modelled as a function from addresses to values, just +like the environment for evaluating expressions), a stack (modelled as a +list) of values, and a list of instructions, and returns the stack at the end +of the execution---the store remains unchanged:% +\end{isamarkuptext}% +\isacommand{consts}~exec~::~{"}('a{\isasymRightarrow}'v)~{\isasymRightarrow}~'v~list~{\isasymRightarrow}~('a,'v)instr~list~{\isasymRightarrow}~'v~list{"}\isanewline +\isacommand{primrec}\isanewline +{"}exec~s~vs~[]~=~vs{"}\isanewline +{"}exec~s~vs~(i\#is)~=~(case~i~of\isanewline +~~~~Const~v~~{\isasymRightarrow}~exec~s~(v\#vs)~is\isanewline +~~|~Load~a~~~{\isasymRightarrow}~exec~s~((s~a)\#vs)~is\isanewline +~~|~Apply~f~~{\isasymRightarrow}~exec~s~(~(f~(hd~vs)~(hd(tl~vs)))\#(tl(tl~vs))~)~is){"}% +\begin{isamarkuptext}% +\noindent +Recall that \isa{hd} and \isa{tl} +return the first element and the remainder of a list. +Because all functions are total, \isa{hd} is defined even for the empty +list, although we do not know what the result is. Thus our model of the +machine always terminates properly, although the above definition does not +tell us much about the result in situations where \isa{Apply} was executed +with fewer than two elements on the stack. + +The compiler is a function from expressions to a list of instructions. Its +definition is pretty much obvious:% +\end{isamarkuptext}% +\isacommand{consts}~comp~::~{"}('a,'v)expr~{\isasymRightarrow}~('a,'v)instr~list{"}\isanewline +\isacommand{primrec}\isanewline +{"}comp~(Cex~v)~~~~~~~=~[Const~v]{"}\isanewline +{"}comp~(Vex~a)~~~~~~~=~[Load~a]{"}\isanewline +{"}comp~(Bex~f~e1~e2)~=~(comp~e2)~@~(comp~e1)~@~[Apply~f]{"}% +\begin{isamarkuptext}% +Now we have to prove the correctness of the compiler, i.e.\ that the +execution of a compiled expression results in the value of the expression:% +\end{isamarkuptext}% +\isacommand{theorem}~{"}exec~s~[]~(comp~e)~=~[value~s~e]{"}% +\begin{isamarkuptext}% +\noindent +This theorem needs to be generalized to% +\end{isamarkuptext}% +\isacommand{theorem}~{"}{\isasymforall}vs.~exec~s~vs~(comp~e)~=~(value~s~e)~\#~vs{"}% +\begin{isamarkuptxt}% +\noindent +which is proved by induction on \isa{e} followed by simplification, once +we have the following lemma about executing the concatenation of two +instruction sequences:% +\end{isamarkuptxt}% +\isacommand{lemma}~exec\_app[simp]:\isanewline +~~{"}{\isasymforall}vs.~exec~s~vs~(xs@ys)~=~exec~s~(exec~s~vs~xs)~ys{"}% +\begin{isamarkuptxt}% +\noindent +This requires induction on \isa{xs} and ordinary simplification for the +base cases. In the induction step, simplification leaves us with a formula +that contains two \isa{case}-expressions over instructions. Thus we add +automatic case splitting as well, which finishes the proof:% +\end{isamarkuptxt}% +\isacommand{apply}(induct\_tac~xs,~simp,~simp~split:~instr.split)\isacommand{.}% +\begin{isamarkuptext}% +\noindent +Note that because \isaindex{auto} performs simplification, it can +also be modified in the same way \isa{simp} can. Thus the proof can be +rewritten as% +\end{isamarkuptext}% +\isacommand{apply}(induct\_tac~xs,~auto~split:~instr.split)\isacommand{.}% +\begin{isamarkuptext}% +\noindent +Although this is more compact, it is less clear for the reader of the proof. + +We could now go back and prove \isa{exec s [] (comp e) = [value s e]} +merely by simplification with the generalized version we just proved. +However, this is unnecessary because the generalized version fully subsumes +its instance.% +\end{isamarkuptext}% +\end{isabelle}%