src/HOL/Isar_examples/ExprCompiler.thy
changeset 7748 5b9c45b21782
parent 7740 2fbe5ce9845f
child 7761 7fab9592384f
equal deleted inserted replaced
7747:ca4e3b75345a 7748:5b9c45b21782
     3     Author:     Markus Wenzel, TU Muenchen
     3     Author:     Markus Wenzel, TU Muenchen
     4 
     4 
     5 Correctness of a simple expression/stack-machine compiler.
     5 Correctness of a simple expression/stack-machine compiler.
     6 *)
     6 *)
     7 
     7 
       
     8 header {* Correctness of a simple expression compiler *};
       
     9 
     8 theory ExprCompiler = Main:;
    10 theory ExprCompiler = Main:;
     9 
    11 
    10 chapter {* Correctness of a simple expression/stack-machine compiler *};
    12 subsection {* Basics *};
    11 
       
    12 
       
    13 section {* Basics *};
       
    14 
    13 
    15 text {*
    14 text {*
    16  First we define a type abbreviation for binary operations over some
    15  First we define a type abbreviation for binary operations over some
    17  type @{type "'val"} of values.
    16  type of values.
    18 *};
    17 *};
    19 
    18 
    20 types
    19 types
    21   'val binop = "'val => 'val => 'val";
    20   'val binop = "'val => 'val => 'val";
    22 
    21 
    23 
    22 
    24 section {* Machine *};
    23 subsection {* Machine *};
    25 
    24 
    26 text {*
    25 text {*
    27  Next we model a simple stack machine, with three instructions.
    26  Next we model a simple stack machine, with three instructions.
    28 *};
    27 *};
    29 
    28 
    51 constdefs
    50 constdefs
    52   execute :: "(('adr, 'val) instr) list => ('adr => 'val) => 'val"
    51   execute :: "(('adr, 'val) instr) list => ('adr => 'val) => 'val"
    53   "execute instrs env == hd (exec instrs [] env)";
    52   "execute instrs env == hd (exec instrs [] env)";
    54 
    53 
    55 
    54 
    56 section {* Expressions *};
    55 subsection {* Expressions *};
    57 
    56 
    58 text {*
    57 text {*
    59  We introduce a simple language of expressions: variables ---
    58  We introduce a simple language of expressions: variables ---
    60  constants --- binary operations.
    59  constants --- binary operations.
    61 *};
    60 *};
    76   "eval (Variable x) env = env x"
    75   "eval (Variable x) env = env x"
    77   "eval (Constant c) env = c"
    76   "eval (Constant c) env = c"
    78   "eval (Binop f e1 e2) env = f (eval e1 env) (eval e2 env)";
    77   "eval (Binop f e1 e2) env = f (eval e1 env) (eval e2 env)";
    79 
    78 
    80 
    79 
    81 section {* Compiler *};
    80 subsection {* Compiler *};
    82 
    81 
    83 text {*
    82 text {*
    84  So we are ready to define the compilation function of expressions to
    83  So we are ready to define the compilation function of expressions to
    85  lists of stack machine instructions.
    84  lists of stack machine instructions.
    86 *};
    85 *};
    93   "comp (Constant c) = [Const c]"
    92   "comp (Constant c) = [Const c]"
    94   "comp (Binop f e1 e2) = comp e2 @ comp e1 @ [Apply f]";
    93   "comp (Binop f e1 e2) = comp e2 @ comp e1 @ [Apply f]";
    95 
    94 
    96 
    95 
    97 text {*
    96 text {*
    98  The main result of this development is the correctness theorem for
    97   The main result of this development is the correctness theorem for
    99  @{term "comp"}.  We first establish some lemmas.
    98   $\idt{comp}$.  We first establish some lemmas about $\idt{exec}$.
   100 *};
    99 *};
   101 
   100 
   102 lemma exec_append:
   101 lemma exec_append:
   103   "ALL stack. exec (xs @ ys) stack env = exec ys (exec xs stack env) env" (is "?P xs");
   102   "ALL stack. exec (xs @ ys) stack env =
       
   103     exec ys (exec xs stack env) env" (is "?P xs");
   104 proof (induct ?P xs type: list);
   104 proof (induct ?P xs type: list);
   105   show "?P []"; by simp;
   105   show "?P []"; by simp;
   106 
   106 
   107   fix x xs;
   107   fix x xs;
   108   assume "?P xs";
   108   assume "?P xs";
   115     fix fun; show "?Q (Apply fun)"; by (simp!);
   115     fix fun; show "?Q (Apply fun)"; by (simp!);
   116   qed;
   116   qed;
   117 qed;
   117 qed;
   118 
   118 
   119 lemma exec_comp:
   119 lemma exec_comp:
   120   "ALL stack. exec (comp e) stack env = eval e env # stack" (is "?P e");
   120   "ALL stack. exec (comp e) stack env =
       
   121     eval e env # stack" (is "?P e");
   121 proof (induct ?P e type: expr);
   122 proof (induct ?P e type: expr);
   122   fix adr; show "?P (Variable adr)"; by (simp!);
   123   fix adr; show "?P (Variable adr)"; by (simp!);
   123 next;
   124 next;
   124   fix val; show "?P (Constant val)"; by (simp!);
   125   fix val; show "?P (Constant val)"; by (simp!);
   125 next;
   126 next;
   131 text {* Main theorem ahead. *};
   132 text {* Main theorem ahead. *};
   132 
   133 
   133 theorem correctness: "execute (comp e) env = eval e env";
   134 theorem correctness: "execute (comp e) env = eval e env";
   134   by (simp add: execute_def exec_comp);
   135   by (simp add: execute_def exec_comp);
   135 
   136 
   136 
       
   137 end;
   137 end;