src/HOL/Isar_examples/ExprCompiler.thy
changeset 6744 9727e83f0578
parent 6517 239c0eff6ce8
child 6746 cf6ad8d22793
equal deleted inserted replaced
6743:5d50225637c8 6744:9727e83f0578
    12   "Markus Wenzel";
    12   "Markus Wenzel";
    13 
    13 
    14 
    14 
    15 section "Basics";
    15 section "Basics";
    16 
    16 
    17 text {|
    17 text {*
    18   First we define a type abbreviation for binary operations over some
    18   First we define a type abbreviation for binary operations over some
    19   type @{type "'val"} of values.
    19   type @{type "'val"} of values.
    20 |};
    20 *};
    21 
    21 
    22 types
    22 types
    23   'val binop = "'val => 'val => 'val";
    23   'val binop = "'val => 'val => 'val";
    24 
    24 
    25 
    25 
    26 section "Machine";
    26 section "Machine";
    27 
    27 
    28 text {|
    28 text {*
    29   Next we model a simple stack machine, with three instructions.
    29   Next we model a simple stack machine, with three instructions.
    30 |};
    30 *};
    31 
    31 
    32 datatype ('adr, 'val) instr =
    32 datatype ('adr, 'val) instr =
    33   Const 'val |
    33   Const 'val |
    34   Load 'adr |
    34   Load 'adr |
    35   Apply "'val binop";
    35   Apply "'val binop";
    36 
    36 
    37 text {|
    37 text {*
    38   Execution of a list of stack machine instructions is
    38   Execution of a list of stack machine instructions is
    39   straight-forward.
    39   straight-forward.
    40 |};
    40 *};
    41 
    41 
    42 consts
    42 consts
    43   exec :: "(('adr, 'val) instr) list => 'val list => ('adr => 'val) => 'val list";
    43   exec :: "(('adr, 'val) instr) list => 'val list => ('adr => 'val) => 'val list";
    44 
    44 
    45 primrec
    45 primrec
    55   "execute instrs env == hd (exec instrs [] env)";
    55   "execute instrs env == hd (exec instrs [] env)";
    56 
    56 
    57 
    57 
    58 section "Expressions";
    58 section "Expressions";
    59 
    59 
    60 text {|
    60 text {*
    61   We introduce a simple language of expressions: variables ---
    61   We introduce a simple language of expressions: variables ---
    62   constants --- binary operations.
    62   constants --- binary operations.
    63 |};
    63 *};
    64 
    64 
    65 datatype ('adr, 'val) expr =
    65 datatype ('adr, 'val) expr =
    66   Variable 'adr |
    66   Variable 'adr |
    67   Constant 'val |
    67   Constant 'val |
    68   Binop "'val binop" "('adr, 'val) expr" "('adr, 'val) expr";
    68   Binop "'val binop" "('adr, 'val) expr" "('adr, 'val) expr";
    69 
    69 
    70 text {|
    70 text {*
    71   Evaluation of expressions does not do any unexpected things.
    71   Evaluation of expressions does not do any unexpected things.
    72 |};
    72 *};
    73 
    73 
    74 consts
    74 consts
    75   eval :: "('adr, 'val) expr => ('adr => 'val) => 'val";
    75   eval :: "('adr, 'val) expr => ('adr => 'val) => 'val";
    76 
    76 
    77 primrec
    77 primrec
    80   "eval (Binop f e1 e2) env = f (eval e1 env) (eval e2 env)";
    80   "eval (Binop f e1 e2) env = f (eval e1 env) (eval e2 env)";
    81 
    81 
    82 
    82 
    83 section "Compiler";
    83 section "Compiler";
    84 
    84 
    85 text {|
    85 text {*
    86   So we are ready to define the compilation function of expressions to
    86   So we are ready to define the compilation function of expressions to
    87   lists of stack machine instructions.
    87   lists of stack machine instructions.
    88 |};
    88 *};
    89 
    89 
    90 consts
    90 consts
    91   comp :: "('adr, 'val) expr => (('adr, 'val) instr) list";
    91   comp :: "('adr, 'val) expr => (('adr, 'val) instr) list";
    92 
    92 
    93 primrec
    93 primrec
    94   "comp (Variable x) = [Load x]"
    94   "comp (Variable x) = [Load x]"
    95   "comp (Constant c) = [Const c]"
    95   "comp (Constant c) = [Const c]"
    96   "comp (Binop f e1 e2) = comp e2 @ comp e1 @ [Apply f]";
    96   "comp (Binop f e1 e2) = comp e2 @ comp e1 @ [Apply f]";
    97 
    97 
    98 
    98 
    99 text {|
    99 text {*
   100   The main result of this developement is the correctness theorem for
   100   The main result of this developement is the correctness theorem for
   101   @{term "comp"}.  We first establish some lemmas.
   101   @{term "comp"}.  We first establish some lemmas.
   102 |};
   102 *};
   103 
   103 
   104 
   104 
   105 lemma exec_append:
   105 lemma exec_append:
   106   "ALL stack. exec (xs @ ys) stack env = exec ys (exec xs stack env) env" (is "??P xs");
   106   "ALL stack. exec (xs @ ys) stack env = exec ys (exec xs stack env) env" (is "??P xs");
   107 proof (induct ??P xs type: list);
   107 proof (induct ??P xs type: list);