src/HOL/IMP/Compiler.thy
author wenzelm
Fri, 05 Oct 2001 21:52:39 +0200
changeset 11701 3d51fbf81c17
parent 11275 71498de45241
child 11704 3c50a2cd6f00
permissions -rw-r--r--
sane numerals (stage 1): added generic 1, removed 1' and 2 on nat, "num" syntax (still with "#"), Numeral0, Numeral1;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10343
24c87e1366d8 *** empty log message ***
nipkow
parents: 10342
diff changeset
     1
(*  Title:      HOL/IMP/Compiler.thy
24c87e1366d8 *** empty log message ***
nipkow
parents: 10342
diff changeset
     2
    ID:         $Id$
24c87e1366d8 *** empty log message ***
nipkow
parents: 10342
diff changeset
     3
    Author:     Tobias Nipkow, TUM
24c87e1366d8 *** empty log message ***
nipkow
parents: 10342
diff changeset
     4
    Copyright   1996 TUM
24c87e1366d8 *** empty log message ***
nipkow
parents: 10342
diff changeset
     5
24c87e1366d8 *** empty log message ***
nipkow
parents: 10342
diff changeset
     6
A simple compiler for a simplistic machine.
24c87e1366d8 *** empty log message ***
nipkow
parents: 10342
diff changeset
     7
*)
24c87e1366d8 *** empty log message ***
nipkow
parents: 10342
diff changeset
     8
10342
b124d59f7b61 *** empty log message ***
nipkow
parents:
diff changeset
     9
theory Compiler = Natural:
b124d59f7b61 *** empty log message ***
nipkow
parents:
diff changeset
    10
b124d59f7b61 *** empty log message ***
nipkow
parents:
diff changeset
    11
datatype instr = ASIN loc aexp | JMPF bexp nat | JMPB nat
b124d59f7b61 *** empty log message ***
nipkow
parents:
diff changeset
    12
b124d59f7b61 *** empty log message ***
nipkow
parents:
diff changeset
    13
consts  stepa1 :: "instr list => ((state*nat) * (state*nat))set"
b124d59f7b61 *** empty log message ***
nipkow
parents:
diff changeset
    14
b124d59f7b61 *** empty log message ***
nipkow
parents:
diff changeset
    15
syntax
b124d59f7b61 *** empty log message ***
nipkow
parents:
diff changeset
    16
        "@stepa1" :: "[instr list,state,nat,state,nat] => bool"
b124d59f7b61 *** empty log message ***
nipkow
parents:
diff changeset
    17
                     ("_ |- <_,_>/ -1-> <_,_>" [50,0,0,0,0] 50)
b124d59f7b61 *** empty log message ***
nipkow
parents:
diff changeset
    18
        "@stepa" :: "[instr list,state,nat,state,nat] => bool"
b124d59f7b61 *** empty log message ***
nipkow
parents:
diff changeset
    19
                     ("_ |-/ <_,_>/ -*-> <_,_>" [50,0,0,0,0] 50)
b124d59f7b61 *** empty log message ***
nipkow
parents:
diff changeset
    20
b124d59f7b61 *** empty log message ***
nipkow
parents:
diff changeset
    21
translations  "P |- <s,m> -1-> <t,n>" == "((s,m),t,n) : stepa1 P"
b124d59f7b61 *** empty log message ***
nipkow
parents:
diff changeset
    22
              "P |- <s,m> -*-> <t,n>" == "((s,m),t,n) : ((stepa1 P)^*)"
b124d59f7b61 *** empty log message ***
nipkow
parents:
diff changeset
    23
b124d59f7b61 *** empty log message ***
nipkow
parents:
diff changeset
    24
b124d59f7b61 *** empty log message ***
nipkow
parents:
diff changeset
    25
inductive "stepa1 P"
b124d59f7b61 *** empty log message ***
nipkow
parents:
diff changeset
    26
intros
11275
71498de45241 new proof
nipkow
parents: 10425
diff changeset
    27
ASIN:
71498de45241 new proof
nipkow
parents: 10425
diff changeset
    28
       "\<lbrakk> n<size P; P!n = ASIN x a \<rbrakk> \<Longrightarrow> P |- <s,n> -1-> <s[x::= a s],Suc n>"
71498de45241 new proof
nipkow
parents: 10425
diff changeset
    29
JMPFT:
71498de45241 new proof
nipkow
parents: 10425
diff changeset
    30
       "\<lbrakk> n<size P; P!n = JMPF b i;  b s \<rbrakk> \<Longrightarrow> P |- <s,n> -1-> <s,Suc n>"
71498de45241 new proof
nipkow
parents: 10425
diff changeset
    31
JMPFF:
71498de45241 new proof
nipkow
parents: 10425
diff changeset
    32
       "\<lbrakk> n<size P; P!n = JMPF b i; ~b s; m=n+i \<rbrakk> \<Longrightarrow> P |- <s,n> -1-> <s,m>"
71498de45241 new proof
nipkow
parents: 10425
diff changeset
    33
JMPB:
71498de45241 new proof
nipkow
parents: 10425
diff changeset
    34
      "\<lbrakk> n<size P; P!n = JMPB i; i <= n \<rbrakk> \<Longrightarrow> P |- <s,n> -1-> <s,n-i>"
10342
b124d59f7b61 *** empty log message ***
nipkow
parents:
diff changeset
    35
b124d59f7b61 *** empty log message ***
nipkow
parents:
diff changeset
    36
consts compile :: "com => instr list"
b124d59f7b61 *** empty log message ***
nipkow
parents:
diff changeset
    37
primrec
b124d59f7b61 *** empty log message ***
nipkow
parents:
diff changeset
    38
"compile SKIP = []"
b124d59f7b61 *** empty log message ***
nipkow
parents:
diff changeset
    39
"compile (x:==a) = [ASIN x a]"
b124d59f7b61 *** empty log message ***
nipkow
parents:
diff changeset
    40
"compile (c1;c2) = compile c1 @ compile c2"
b124d59f7b61 *** empty log message ***
nipkow
parents:
diff changeset
    41
"compile (IF b THEN c1 ELSE c2) =
11701
3d51fbf81c17 sane numerals (stage 1): added generic 1, removed 1' and 2 on nat,
wenzelm
parents: 11275
diff changeset
    42
 [JMPF b (length(compile c1) + # 2)] @ compile c1 @
10342
b124d59f7b61 *** empty log message ***
nipkow
parents:
diff changeset
    43
 [JMPF (%x. False) (length(compile c2)+1)] @ compile c2"
11701
3d51fbf81c17 sane numerals (stage 1): added generic 1, removed 1' and 2 on nat,
wenzelm
parents: 11275
diff changeset
    44
"compile (WHILE b DO c) = [JMPF b (length(compile c) + # 2)] @ compile c @
10342
b124d59f7b61 *** empty log message ***
nipkow
parents:
diff changeset
    45
 [JMPB (length(compile c)+1)]"
b124d59f7b61 *** empty log message ***
nipkow
parents:
diff changeset
    46
b124d59f7b61 *** empty log message ***
nipkow
parents:
diff changeset
    47
declare nth_append[simp];
b124d59f7b61 *** empty log message ***
nipkow
parents:
diff changeset
    48
11275
71498de45241 new proof
nipkow
parents: 10425
diff changeset
    49
(* Lemmas for lifting an execution into a prefix and suffix
71498de45241 new proof
nipkow
parents: 10425
diff changeset
    50
   of instructions; only needed for the first proof *)
71498de45241 new proof
nipkow
parents: 10425
diff changeset
    51
71498de45241 new proof
nipkow
parents: 10425
diff changeset
    52
lemma app_right_1:
71498de45241 new proof
nipkow
parents: 10425
diff changeset
    53
  "is1 |- <s1,i1> -1-> <s2,i2> \<Longrightarrow> is1 @ is2 |- <s1,i1> -1-> <s2,i2>"
71498de45241 new proof
nipkow
parents: 10425
diff changeset
    54
apply(erule stepa1.induct);
71498de45241 new proof
nipkow
parents: 10425
diff changeset
    55
   apply (simp add:ASIN)
71498de45241 new proof
nipkow
parents: 10425
diff changeset
    56
  apply (force intro!:JMPFT)
71498de45241 new proof
nipkow
parents: 10425
diff changeset
    57
 apply (force intro!:JMPFF)
71498de45241 new proof
nipkow
parents: 10425
diff changeset
    58
apply (simp add: JMPB)
71498de45241 new proof
nipkow
parents: 10425
diff changeset
    59
done      
71498de45241 new proof
nipkow
parents: 10425
diff changeset
    60
      
71498de45241 new proof
nipkow
parents: 10425
diff changeset
    61
lemma app_left_1:
71498de45241 new proof
nipkow
parents: 10425
diff changeset
    62
  "is2 |- <s1,i1> -1-> <s2,i2> \<Longrightarrow>
71498de45241 new proof
nipkow
parents: 10425
diff changeset
    63
   is1 @ is2 |- <s1,size is1+i1> -1-> <s2,size is1+i2>"
71498de45241 new proof
nipkow
parents: 10425
diff changeset
    64
apply(erule stepa1.induct);
71498de45241 new proof
nipkow
parents: 10425
diff changeset
    65
   apply (simp add:ASIN)
71498de45241 new proof
nipkow
parents: 10425
diff changeset
    66
  apply (fastsimp intro!:JMPFT)
71498de45241 new proof
nipkow
parents: 10425
diff changeset
    67
 apply (fastsimp intro!:JMPFF)
71498de45241 new proof
nipkow
parents: 10425
diff changeset
    68
apply (simp add: JMPB)
71498de45241 new proof
nipkow
parents: 10425
diff changeset
    69
done
71498de45241 new proof
nipkow
parents: 10425
diff changeset
    70
71498de45241 new proof
nipkow
parents: 10425
diff changeset
    71
lemma app_right:
71498de45241 new proof
nipkow
parents: 10425
diff changeset
    72
  "is1 |- <s1,i1> -*-> <s2,i2> \<Longrightarrow> is1 @ is2 |- <s1,i1> -*-> <s2,i2>"
71498de45241 new proof
nipkow
parents: 10425
diff changeset
    73
apply(erule rtrancl_induct2);
71498de45241 new proof
nipkow
parents: 10425
diff changeset
    74
 apply simp
71498de45241 new proof
nipkow
parents: 10425
diff changeset
    75
apply(blast intro:app_right_1 rtrancl_trans)
71498de45241 new proof
nipkow
parents: 10425
diff changeset
    76
done
71498de45241 new proof
nipkow
parents: 10425
diff changeset
    77
71498de45241 new proof
nipkow
parents: 10425
diff changeset
    78
lemma app_left:
71498de45241 new proof
nipkow
parents: 10425
diff changeset
    79
  "is2 |- <s1,i1> -*-> <s2,i2> \<Longrightarrow>
71498de45241 new proof
nipkow
parents: 10425
diff changeset
    80
   is1 @ is2 |- <s1,size is1+i1> -*-> <s2,size is1+i2>"
71498de45241 new proof
nipkow
parents: 10425
diff changeset
    81
apply(erule rtrancl_induct2);
71498de45241 new proof
nipkow
parents: 10425
diff changeset
    82
 apply simp
71498de45241 new proof
nipkow
parents: 10425
diff changeset
    83
apply(blast intro:app_left_1 rtrancl_trans)
71498de45241 new proof
nipkow
parents: 10425
diff changeset
    84
done
71498de45241 new proof
nipkow
parents: 10425
diff changeset
    85
71498de45241 new proof
nipkow
parents: 10425
diff changeset
    86
lemma app_left2:
71498de45241 new proof
nipkow
parents: 10425
diff changeset
    87
  "\<lbrakk> is2 |- <s1,i1> -*-> <s2,i2>; j1 = size is1+i1; j2 = size is1+i2 \<rbrakk> \<Longrightarrow>
71498de45241 new proof
nipkow
parents: 10425
diff changeset
    88
   is1 @ is2 |- <s1,j1> -*-> <s2,j2>"
71498de45241 new proof
nipkow
parents: 10425
diff changeset
    89
by (simp add:app_left)
10342
b124d59f7b61 *** empty log message ***
nipkow
parents:
diff changeset
    90
11275
71498de45241 new proof
nipkow
parents: 10425
diff changeset
    91
lemma app1_left:
71498de45241 new proof
nipkow
parents: 10425
diff changeset
    92
  "is |- <s1,i1> -*-> <s2,i2> \<Longrightarrow>
71498de45241 new proof
nipkow
parents: 10425
diff changeset
    93
   instr # is |- <s1,Suc i1> -*-> <s2,Suc i2>"
71498de45241 new proof
nipkow
parents: 10425
diff changeset
    94
by(erule app_left[of _ _ _ _ _ "[instr]",simplified])
71498de45241 new proof
nipkow
parents: 10425
diff changeset
    95
71498de45241 new proof
nipkow
parents: 10425
diff changeset
    96
71498de45241 new proof
nipkow
parents: 10425
diff changeset
    97
(* The first proof; statement very intuitive,
71498de45241 new proof
nipkow
parents: 10425
diff changeset
    98
   but application of induction hypothesis requires the above lifting lemmas
71498de45241 new proof
nipkow
parents: 10425
diff changeset
    99
*)
71498de45241 new proof
nipkow
parents: 10425
diff changeset
   100
theorem "<c,s> -c-> t ==> compile c |- <s,0> -*-> <t,length(compile c)>"
71498de45241 new proof
nipkow
parents: 10425
diff changeset
   101
apply(erule evalc.induct);
71498de45241 new proof
nipkow
parents: 10425
diff changeset
   102
      apply simp;
71498de45241 new proof
nipkow
parents: 10425
diff changeset
   103
     apply(force intro!: ASIN);
71498de45241 new proof
nipkow
parents: 10425
diff changeset
   104
    apply simp
71498de45241 new proof
nipkow
parents: 10425
diff changeset
   105
    apply(rule rtrancl_trans)
71498de45241 new proof
nipkow
parents: 10425
diff changeset
   106
    apply(erule app_right)
71498de45241 new proof
nipkow
parents: 10425
diff changeset
   107
    apply(erule app_left[of _ 0,simplified])
71498de45241 new proof
nipkow
parents: 10425
diff changeset
   108
(* IF b THEN c0 ELSE c1; case b is true *)
71498de45241 new proof
nipkow
parents: 10425
diff changeset
   109
   apply(simp);
71498de45241 new proof
nipkow
parents: 10425
diff changeset
   110
   (* execute JMPF: *)
71498de45241 new proof
nipkow
parents: 10425
diff changeset
   111
   apply (rule rtrancl_into_rtrancl2)
71498de45241 new proof
nipkow
parents: 10425
diff changeset
   112
    apply(force intro!: JMPFT);
71498de45241 new proof
nipkow
parents: 10425
diff changeset
   113
   (* execute compile c0: *)
71498de45241 new proof
nipkow
parents: 10425
diff changeset
   114
   apply(rule app1_left)
71498de45241 new proof
nipkow
parents: 10425
diff changeset
   115
   apply(rule rtrancl_into_rtrancl);
71498de45241 new proof
nipkow
parents: 10425
diff changeset
   116
    apply(erule app_right)
71498de45241 new proof
nipkow
parents: 10425
diff changeset
   117
   (* execute JMPF: *)
71498de45241 new proof
nipkow
parents: 10425
diff changeset
   118
   apply(force intro!: JMPFF);
71498de45241 new proof
nipkow
parents: 10425
diff changeset
   119
(* end of case b is true *)
71498de45241 new proof
nipkow
parents: 10425
diff changeset
   120
  apply simp
71498de45241 new proof
nipkow
parents: 10425
diff changeset
   121
  apply (rule rtrancl_into_rtrancl2)
71498de45241 new proof
nipkow
parents: 10425
diff changeset
   122
   apply(force intro!: JMPFF)
71498de45241 new proof
nipkow
parents: 10425
diff changeset
   123
  apply(force intro!: app_left2 app1_left)
71498de45241 new proof
nipkow
parents: 10425
diff changeset
   124
(* WHILE False *)
71498de45241 new proof
nipkow
parents: 10425
diff changeset
   125
 apply(force intro: JMPFF);
71498de45241 new proof
nipkow
parents: 10425
diff changeset
   126
(* WHILE True *)
71498de45241 new proof
nipkow
parents: 10425
diff changeset
   127
apply(simp)
71498de45241 new proof
nipkow
parents: 10425
diff changeset
   128
apply(rule rtrancl_into_rtrancl2);
71498de45241 new proof
nipkow
parents: 10425
diff changeset
   129
 apply(force intro!: JMPFT);
71498de45241 new proof
nipkow
parents: 10425
diff changeset
   130
apply(rule rtrancl_trans);
71498de45241 new proof
nipkow
parents: 10425
diff changeset
   131
 apply(rule app1_left)
71498de45241 new proof
nipkow
parents: 10425
diff changeset
   132
 apply(erule app_right)
71498de45241 new proof
nipkow
parents: 10425
diff changeset
   133
apply(rule rtrancl_into_rtrancl2);
71498de45241 new proof
nipkow
parents: 10425
diff changeset
   134
 apply(force intro!: JMPB)
71498de45241 new proof
nipkow
parents: 10425
diff changeset
   135
apply(simp)
71498de45241 new proof
nipkow
parents: 10425
diff changeset
   136
done
71498de45241 new proof
nipkow
parents: 10425
diff changeset
   137
71498de45241 new proof
nipkow
parents: 10425
diff changeset
   138
(* Second proof; statement is generalized to cater for prefixes and suffixes;
71498de45241 new proof
nipkow
parents: 10425
diff changeset
   139
   needs none of the lifting lemmas, but instantiations of pre/suffix.
71498de45241 new proof
nipkow
parents: 10425
diff changeset
   140
*)
10342
b124d59f7b61 *** empty log message ***
nipkow
parents:
diff changeset
   141
theorem "<c,s> -c-> t ==> 
b124d59f7b61 *** empty log message ***
nipkow
parents:
diff changeset
   142
 !a z. a@compile c@z |- <s,length a> -*-> <t,length a + length(compile c)>";
b124d59f7b61 *** empty log message ***
nipkow
parents:
diff changeset
   143
apply(erule evalc.induct);
b124d59f7b61 *** empty log message ***
nipkow
parents:
diff changeset
   144
      apply simp;
b124d59f7b61 *** empty log message ***
nipkow
parents:
diff changeset
   145
     apply(force intro!: ASIN);
b124d59f7b61 *** empty log message ***
nipkow
parents:
diff changeset
   146
    apply(intro strip);
b124d59f7b61 *** empty log message ***
nipkow
parents:
diff changeset
   147
    apply(erule_tac x = a in allE);
b124d59f7b61 *** empty log message ***
nipkow
parents:
diff changeset
   148
    apply(erule_tac x = "a@compile c0" in allE);
b124d59f7b61 *** empty log message ***
nipkow
parents:
diff changeset
   149
    apply(erule_tac x = "compile c1@z" in allE);
b124d59f7b61 *** empty log message ***
nipkow
parents:
diff changeset
   150
    apply(erule_tac x = z in allE);
b124d59f7b61 *** empty log message ***
nipkow
parents:
diff changeset
   151
    apply(simp add:add_assoc[THEN sym]);
b124d59f7b61 *** empty log message ***
nipkow
parents:
diff changeset
   152
    apply(blast intro:rtrancl_trans);
b124d59f7b61 *** empty log message ***
nipkow
parents:
diff changeset
   153
(* IF b THEN c0 ELSE c1; case b is true *)
b124d59f7b61 *** empty log message ***
nipkow
parents:
diff changeset
   154
   apply(intro strip);
b124d59f7b61 *** empty log message ***
nipkow
parents:
diff changeset
   155
   (* instantiate assumption sufficiently for later: *)
b124d59f7b61 *** empty log message ***
nipkow
parents:
diff changeset
   156
   apply(erule_tac x = "a@[?I]" in allE);
b124d59f7b61 *** empty log message ***
nipkow
parents:
diff changeset
   157
   apply(simp);
b124d59f7b61 *** empty log message ***
nipkow
parents:
diff changeset
   158
   (* execute JMPF: *)
b124d59f7b61 *** empty log message ***
nipkow
parents:
diff changeset
   159
   apply(rule rtrancl_into_rtrancl2);
11275
71498de45241 new proof
nipkow
parents: 10425
diff changeset
   160
    apply(force intro!: JMPFT);
10342
b124d59f7b61 *** empty log message ***
nipkow
parents:
diff changeset
   161
   (* execute compile c0: *)
b124d59f7b61 *** empty log message ***
nipkow
parents:
diff changeset
   162
   apply(rule rtrancl_trans);
b124d59f7b61 *** empty log message ***
nipkow
parents:
diff changeset
   163
    apply(erule allE);
b124d59f7b61 *** empty log message ***
nipkow
parents:
diff changeset
   164
    apply assumption;
b124d59f7b61 *** empty log message ***
nipkow
parents:
diff changeset
   165
   (* execute JMPF: *)
b124d59f7b61 *** empty log message ***
nipkow
parents:
diff changeset
   166
   apply(rule r_into_rtrancl);
11275
71498de45241 new proof
nipkow
parents: 10425
diff changeset
   167
   apply(force intro!: JMPFF);
10342
b124d59f7b61 *** empty log message ***
nipkow
parents:
diff changeset
   168
(* end of case b is true *)
b124d59f7b61 *** empty log message ***
nipkow
parents:
diff changeset
   169
  apply(intro strip);
b124d59f7b61 *** empty log message ***
nipkow
parents:
diff changeset
   170
  apply(erule_tac x = "a@[?I]@compile c0@[?J]" in allE);
b124d59f7b61 *** empty log message ***
nipkow
parents:
diff changeset
   171
  apply(simp add:add_assoc);
b124d59f7b61 *** empty log message ***
nipkow
parents:
diff changeset
   172
  apply(rule rtrancl_into_rtrancl2);
11275
71498de45241 new proof
nipkow
parents: 10425
diff changeset
   173
   apply(force intro!: JMPFF);
10342
b124d59f7b61 *** empty log message ***
nipkow
parents:
diff changeset
   174
  apply(blast);
b124d59f7b61 *** empty log message ***
nipkow
parents:
diff changeset
   175
 apply(force intro: JMPFF);
b124d59f7b61 *** empty log message ***
nipkow
parents:
diff changeset
   176
apply(intro strip);
b124d59f7b61 *** empty log message ***
nipkow
parents:
diff changeset
   177
apply(erule_tac x = "a@[?I]" in allE);
b124d59f7b61 *** empty log message ***
nipkow
parents:
diff changeset
   178
apply(erule_tac x = a in allE);
b124d59f7b61 *** empty log message ***
nipkow
parents:
diff changeset
   179
apply(simp);
b124d59f7b61 *** empty log message ***
nipkow
parents:
diff changeset
   180
apply(rule rtrancl_into_rtrancl2);
11275
71498de45241 new proof
nipkow
parents: 10425
diff changeset
   181
 apply(force intro!: JMPFT);
10342
b124d59f7b61 *** empty log message ***
nipkow
parents:
diff changeset
   182
apply(rule rtrancl_trans);
b124d59f7b61 *** empty log message ***
nipkow
parents:
diff changeset
   183
 apply(erule allE);
b124d59f7b61 *** empty log message ***
nipkow
parents:
diff changeset
   184
 apply assumption;
b124d59f7b61 *** empty log message ***
nipkow
parents:
diff changeset
   185
apply(rule rtrancl_into_rtrancl2);
11275
71498de45241 new proof
nipkow
parents: 10425
diff changeset
   186
 apply(force intro!: JMPB);
10342
b124d59f7b61 *** empty log message ***
nipkow
parents:
diff changeset
   187
apply(simp);
b124d59f7b61 *** empty log message ***
nipkow
parents:
diff changeset
   188
done
b124d59f7b61 *** empty log message ***
nipkow
parents:
diff changeset
   189
b124d59f7b61 *** empty log message ***
nipkow
parents:
diff changeset
   190
(* Missing: the other direction! *)
b124d59f7b61 *** empty log message ***
nipkow
parents:
diff changeset
   191
b124d59f7b61 *** empty log message ***
nipkow
parents:
diff changeset
   192
end