doc-src/TutorialI/CodeGen/document/CodeGen.tex
changeset 8771 026f37a86ea7
parent 8746 ccbb5e0dccdf
child 9145 9f7b8de5bfaf
equal deleted inserted replaced
8770:bfab4d4b7516 8771:026f37a86ea7
    14 \isacommand{datatype}~('a,'v)expr~=~Cex~'v\isanewline
    14 \isacommand{datatype}~('a,'v)expr~=~Cex~'v\isanewline
    15 ~~~~~~~~~~~~~~~~~~~~~|~Vex~'a\isanewline
    15 ~~~~~~~~~~~~~~~~~~~~~|~Vex~'a\isanewline
    16 ~~~~~~~~~~~~~~~~~~~~~|~Bex~{"}'v~binop{"}~~{"}('a,'v)expr{"}~~{"}('a,'v)expr{"}%
    16 ~~~~~~~~~~~~~~~~~~~~~|~Bex~{"}'v~binop{"}~~{"}('a,'v)expr{"}~~{"}('a,'v)expr{"}%
    17 \begin{isamarkuptext}%
    17 \begin{isamarkuptext}%
    18 \noindent
    18 \noindent
    19 The three constructors represent constants, variables and the combination of
    19 The three constructors represent constants, variables and the application of
    20 two subexpressions with a binary operation.
    20 a binary operation to two subexpressions.
    21 
    21 
    22 The value of an expression w.r.t.\ an environment that maps variables to
    22 The value of an expression w.r.t.\ an environment that maps variables to
    23 values is easily defined:%
    23 values is easily defined:%
    24 \end{isamarkuptext}%
    24 \end{isamarkuptext}%
    25 \isacommand{consts}~value~::~{"}('a~{\isasymRightarrow}~'v)~{\isasymRightarrow}~('a,'v)expr~{\isasymRightarrow}~'v{"}\isanewline
    25 \isacommand{consts}~value~::~{"}('a,'v)expr~{\isasymRightarrow}~('a~{\isasymRightarrow}~'v)~{\isasymRightarrow}~'v{"}\isanewline
    26 \isacommand{primrec}\isanewline
    26 \isacommand{primrec}\isanewline
    27 {"}value~env~(Cex~v)~=~v{"}\isanewline
    27 {"}value~(Cex~v)~env~=~v{"}\isanewline
    28 {"}value~env~(Vex~a)~=~env~a{"}\isanewline
    28 {"}value~(Vex~a)~env~=~env~a{"}\isanewline
    29 {"}value~env~(Bex~f~e1~e2)~=~f~(value~env~e1)~(value~env~e2){"}%
    29 {"}value~(Bex~f~e1~e2)~env~=~f~(value~e1~env)~(value~e2~env){"}%
    30 \begin{isamarkuptext}%
    30 \begin{isamarkuptext}%
    31 The stack machine has three instructions: load a constant value onto the
    31 The stack machine has three instructions: load a constant value onto the
    32 stack, load the contents of a certain address onto the stack, and apply a
    32 stack, load the contents of a certain address onto the stack, and apply a
    33 binary operation to the two topmost elements of the stack, replacing them by
    33 binary operation to the two topmost elements of the stack, replacing them by
    34 the result. As for \isa{expr}, addresses and values are type parameters:%
    34 the result. As for \isa{expr}, addresses and values are type parameters:%
    35 \end{isamarkuptext}%
    35 \end{isamarkuptext}%
    36 \isacommand{datatype}~('a,'v)~instr~=~Const~'v\isanewline
    36 \isacommand{datatype}~('a,'v)~instr~=~Const~'v\isanewline
    37 ~~~~~~~~~~~~~~~~~~~~~~~|~Load~'a\isanewline
    37 ~~~~~~~~~~~~~~~~~~~~~~~|~Load~'a\isanewline
    38 ~~~~~~~~~~~~~~~~~~~~~~~|~Apply~{"}'v~binop{"}%
    38 ~~~~~~~~~~~~~~~~~~~~~~~|~Apply~{"}'v~binop{"}%
    39 \begin{isamarkuptext}%
    39 \begin{isamarkuptext}%
    40 The execution of the stack machine is modelled by a function \isa{exec}
    40 The execution of the stack machine is modelled by a function
    41 that takes a store (modelled as a function from addresses to values, just
    41 \isa{exec} that takes a list of instructions, a store (modelled as a
    42 like the environment for evaluating expressions), a stack (modelled as a
    42 function from addresses to values, just like the environment for
    43 list) of values, and a list of instructions, and returns the stack at the end
    43 evaluating expressions), and a stack (modelled as a list) of values,
    44 of the execution---the store remains unchanged:%
    44 and returns the stack at the end of the execution---the store remains
       
    45 unchanged:%
    45 \end{isamarkuptext}%
    46 \end{isamarkuptext}%
    46 \isacommand{consts}~exec~::~{"}('a{\isasymRightarrow}'v)~{\isasymRightarrow}~'v~list~{\isasymRightarrow}~('a,'v)instr~list~{\isasymRightarrow}~'v~list{"}\isanewline
    47 \isacommand{consts}~exec~::~{"}('a,'v)instr~list~{\isasymRightarrow}~('a{\isasymRightarrow}'v)~{\isasymRightarrow}~'v~list~{\isasymRightarrow}~'v~list{"}\isanewline
    47 \isacommand{primrec}\isanewline
    48 \isacommand{primrec}\isanewline
    48 {"}exec~s~vs~[]~=~vs{"}\isanewline
    49 {"}exec~[]~s~vs~=~vs{"}\isanewline
    49 {"}exec~s~vs~(i\#is)~=~(case~i~of\isanewline
    50 {"}exec~(i\#is)~s~vs~=~(case~i~of\isanewline
    50 ~~~~Const~v~~{\isasymRightarrow}~exec~s~(v\#vs)~is\isanewline
    51 ~~~~Const~v~~{\isasymRightarrow}~exec~is~s~(v\#vs)\isanewline
    51 ~~|~Load~a~~~{\isasymRightarrow}~exec~s~((s~a)\#vs)~is\isanewline
    52 ~~|~Load~a~~~{\isasymRightarrow}~exec~is~s~((s~a)\#vs)\isanewline
    52 ~~|~Apply~f~~{\isasymRightarrow}~exec~s~(~(f~(hd~vs)~(hd(tl~vs)))\#(tl(tl~vs))~)~is){"}%
    53 ~~|~Apply~f~~{\isasymRightarrow}~exec~is~s~((f~(hd~vs)~(hd(tl~vs)))\#(tl(tl~vs)))){"}%
    53 \begin{isamarkuptext}%
    54 \begin{isamarkuptext}%
    54 \noindent
    55 \noindent
    55 Recall that \isa{hd} and \isa{tl}
    56 Recall that \isa{hd} and \isa{tl}
    56 return the first element and the remainder of a list.
    57 return the first element and the remainder of a list.
    57 Because all functions are total, \isa{hd} is defined even for the empty
    58 Because all functions are total, \isa{hd} is defined even for the empty
    70 {"}comp~(Bex~f~e1~e2)~=~(comp~e2)~@~(comp~e1)~@~[Apply~f]{"}%
    71 {"}comp~(Bex~f~e1~e2)~=~(comp~e2)~@~(comp~e1)~@~[Apply~f]{"}%
    71 \begin{isamarkuptext}%
    72 \begin{isamarkuptext}%
    72 Now we have to prove the correctness of the compiler, i.e.\ that the
    73 Now we have to prove the correctness of the compiler, i.e.\ that the
    73 execution of a compiled expression results in the value of the expression:%
    74 execution of a compiled expression results in the value of the expression:%
    74 \end{isamarkuptext}%
    75 \end{isamarkuptext}%
    75 \isacommand{theorem}~{"}exec~s~[]~(comp~e)~=~[value~s~e]{"}%
    76 \isacommand{theorem}~{"}exec~(comp~e)~s~[]~=~[value~e~s]{"}%
    76 \begin{isamarkuptext}%
    77 \begin{isamarkuptext}%
    77 \noindent
    78 \noindent
    78 This theorem needs to be generalized to%
    79 This theorem needs to be generalized to%
    79 \end{isamarkuptext}%
    80 \end{isamarkuptext}%
    80 \isacommand{theorem}~{"}{\isasymforall}vs.~exec~s~vs~(comp~e)~=~(value~s~e)~\#~vs{"}%
    81 \isacommand{theorem}~{"}{\isasymforall}vs.~exec~(comp~e)~s~vs~=~(value~e~s)~\#~vs{"}%
    81 \begin{isamarkuptxt}%
    82 \begin{isamarkuptxt}%
    82 \noindent
    83 \noindent
    83 which is proved by induction on \isa{e} followed by simplification, once
    84 which is proved by induction on \isa{e} followed by simplification, once
    84 we have the following lemma about executing the concatenation of two
    85 we have the following lemma about executing the concatenation of two
    85 instruction sequences:%
    86 instruction sequences:%
    86 \end{isamarkuptxt}%
    87 \end{isamarkuptxt}%
    87 \isacommand{lemma}~exec\_app[simp]:\isanewline
    88 \isacommand{lemma}~exec\_app[simp]:\isanewline
    88 ~~{"}{\isasymforall}vs.~exec~s~vs~(xs@ys)~=~exec~s~(exec~s~vs~xs)~ys{"}%
    89 ~~{"}{\isasymforall}vs.~exec~(xs@ys)~s~vs~=~exec~ys~s~(exec~xs~s~vs){"}%
    89 \begin{isamarkuptxt}%
    90 \begin{isamarkuptxt}%
    90 \noindent
    91 \noindent
    91 This requires induction on \isa{xs} and ordinary simplification for the
    92 This requires induction on \isa{xs} and ordinary simplification for the
    92 base cases. In the induction step, simplification leaves us with a formula
    93 base cases. In the induction step, simplification leaves us with a formula
    93 that contains two \isa{case}-expressions over instructions. Thus we add
    94 that contains two \isa{case}-expressions over instructions. Thus we add
   103 \isacommand{apply}(induct\_tac~xs,~auto~split:~instr.split)\isacommand{.}%
   104 \isacommand{apply}(induct\_tac~xs,~auto~split:~instr.split)\isacommand{.}%
   104 \begin{isamarkuptext}%
   105 \begin{isamarkuptext}%
   105 \noindent
   106 \noindent
   106 Although this is more compact, it is less clear for the reader of the proof.
   107 Although this is more compact, it is less clear for the reader of the proof.
   107 
   108 
   108 We could now go back and prove \isa{exec s [] (comp e) = [value s e]}
   109 We could now go back and prove \isa{exec (comp e) s [] = [value e s]}
   109 merely by simplification with the generalized version we just proved.
   110 merely by simplification with the generalized version we just proved.
   110 However, this is unnecessary because the generalized version fully subsumes
   111 However, this is unnecessary because the generalized version fully subsumes
   111 its instance.%
   112 its instance.%
   112 \end{isamarkuptext}%
   113 \end{isamarkuptext}%
   113 \end{isabelle}%
   114 \end{isabelle}%