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