src/HOL/IMP/Machines.thy
author wenzelm
Fri Mar 28 19:43:54 2008 +0100 (2008-03-28)
changeset 26462 dac4e2bce00d
parent 23746 a455e69c31cc
child 30952 7ab2716dd93b
permissions -rw-r--r--
avoid rebinding of existing facts;
wenzelm@18372
     1
wenzelm@18372
     2
(* $Id$ *)
wenzelm@18372
     3
haftmann@16417
     4
theory Machines imports Natural begin
nipkow@13095
     5
nipkow@13095
     6
lemma rtrancl_eq: "R^* = Id \<union> (R O R^*)"
berghofe@22267
     7
  by (fast intro: rtrancl_into_rtrancl elim: rtranclE)
nipkow@13095
     8
nipkow@13095
     9
lemma converse_rtrancl_eq: "R^* = Id \<union> (R^* O R)"
wenzelm@18372
    10
  by (subst r_comp_rtrancl_eq[symmetric], rule rtrancl_eq)
nipkow@13095
    11
nipkow@13095
    12
lemmas converse_rel_powE = rel_pow_E2
nipkow@13095
    13
nipkow@13095
    14
lemma R_O_Rn_commute: "R O R^n = R^n O R"
wenzelm@18372
    15
  by (induct n) (simp, simp add: O_assoc [symmetric])
nipkow@13095
    16
nipkow@13095
    17
lemma converse_in_rel_pow_eq:
wenzelm@18372
    18
  "((x,z) \<in> R^n) = (n=0 \<and> z=x \<or> (\<exists>m y. n = Suc m \<and> (x,y) \<in> R \<and> (y,z) \<in> R^m))"
nipkow@13095
    19
apply(rule iffI)
nipkow@13095
    20
 apply(blast elim:converse_rel_powE)
nipkow@13095
    21
apply (fastsimp simp add:gr0_conv_Suc R_O_Rn_commute)
nipkow@13095
    22
done
nipkow@13095
    23
nipkow@13095
    24
lemma rel_pow_plus: "R^(m+n) = R^n O R^m"
wenzelm@18372
    25
  by (induct n) (simp, simp add: O_assoc)
nipkow@13095
    26
nipkow@13095
    27
lemma rel_pow_plusI: "\<lbrakk> (x,y) \<in> R^m; (y,z) \<in> R^n \<rbrakk> \<Longrightarrow> (x,z) \<in> R^(m+n)"
wenzelm@18372
    28
  by (simp add: rel_pow_plus rel_compI)
nipkow@13095
    29
nipkow@13095
    30
subsection "Instructions"
nipkow@13095
    31
nipkow@13095
    32
text {* There are only three instructions: *}
nipkow@13675
    33
datatype instr = SET loc aexp | JMPF bexp nat | JMPB nat
nipkow@13095
    34
nipkow@13095
    35
types instrs = "instr list"
nipkow@13095
    36
nipkow@13095
    37
subsection "M0 with PC"
nipkow@13095
    38
berghofe@23746
    39
inductive_set
berghofe@23746
    40
  exec01 :: "instr list \<Rightarrow> ((nat\<times>state) \<times> (nat\<times>state))set"
berghofe@23746
    41
  and exec01' :: "[instrs, nat,state, nat,state] \<Rightarrow> bool"
berghofe@23746
    42
    ("(_/ \<turnstile> (1\<langle>_,/_\<rangle>)/ -1\<rightarrow> (1\<langle>_,/_\<rangle>))" [50,0,0,0,0] 50)
berghofe@23746
    43
  for P :: "instr list"
berghofe@23746
    44
where
berghofe@23746
    45
  "p \<turnstile> \<langle>i,s\<rangle> -1\<rightarrow> \<langle>j,t\<rangle> == ((i,s),j,t) : (exec01 p)"
berghofe@23746
    46
| SET: "\<lbrakk> n<size P; P!n = SET x a \<rbrakk> \<Longrightarrow> P \<turnstile> \<langle>n,s\<rangle> -1\<rightarrow> \<langle>Suc n,s[x\<mapsto> a s]\<rangle>"
berghofe@23746
    47
| JMPFT: "\<lbrakk> n<size P; P!n = JMPF b i;  b s \<rbrakk> \<Longrightarrow> P \<turnstile> \<langle>n,s\<rangle> -1\<rightarrow> \<langle>Suc n,s\<rangle>"
berghofe@23746
    48
| JMPFF: "\<lbrakk> n<size P; P!n = JMPF b i; \<not>b s; m=n+i+1; m \<le> size P \<rbrakk>
berghofe@23746
    49
        \<Longrightarrow> P \<turnstile> \<langle>n,s\<rangle> -1\<rightarrow> \<langle>m,s\<rangle>"
berghofe@23746
    50
| JMPB:  "\<lbrakk> n<size P; P!n = JMPB i; i \<le> n; j = n-i \<rbrakk> \<Longrightarrow> P \<turnstile> \<langle>n,s\<rangle> -1\<rightarrow> \<langle>j,s\<rangle>"
nipkow@13095
    51
berghofe@23746
    52
abbreviation
berghofe@23746
    53
  exec0s :: "[instrs, nat,state, nat,state] \<Rightarrow> bool"
berghofe@23746
    54
    ("(_/ \<turnstile> (1\<langle>_,/_\<rangle>)/ -*\<rightarrow> (1\<langle>_,/_\<rangle>))" [50,0,0,0,0] 50)  where
berghofe@23746
    55
  "p \<turnstile> \<langle>i,s\<rangle> -*\<rightarrow> \<langle>j,t\<rangle> == ((i,s),j,t) : (exec01 p)^*"
kleing@14565
    56
berghofe@23746
    57
abbreviation
berghofe@23746
    58
  exec0n :: "[instrs, nat,state, nat, nat,state] \<Rightarrow> bool"
berghofe@23746
    59
    ("(_/ \<turnstile> (1\<langle>_,/_\<rangle>)/ -_\<rightarrow> (1\<langle>_,/_\<rangle>))" [50,0,0,0,0] 50)  where
berghofe@23746
    60
  "p \<turnstile> \<langle>i,s\<rangle> -n\<rightarrow> \<langle>j,t\<rangle> == ((i,s),j,t) : (exec01 p)^n"
nipkow@13095
    61
nipkow@13095
    62
subsection "M0 with lists"
nipkow@13095
    63
nipkow@13095
    64
text {* We describe execution of programs in the machine by
nipkow@13095
    65
  an operational (small step) semantics:
nipkow@13095
    66
*}
nipkow@13095
    67
nipkow@13095
    68
types config = "instrs \<times> instrs \<times> state"
nipkow@13095
    69
nipkow@13095
    70
berghofe@23746
    71
inductive_set
berghofe@23746
    72
  stepa1 :: "(config \<times> config)set"
berghofe@23746
    73
  and stepa1' :: "[instrs,instrs,state, instrs,instrs,state] \<Rightarrow> bool"
berghofe@23746
    74
    ("((1\<langle>_,/_,/_\<rangle>)/ -1\<rightarrow> (1\<langle>_,/_,/_\<rangle>))" 50)
berghofe@23746
    75
where
berghofe@23746
    76
  "\<langle>p,q,s\<rangle> -1\<rightarrow> \<langle>p',q',t\<rangle> == ((p,q,s),p',q',t) : stepa1"
berghofe@23746
    77
| "\<langle>SET x a#p,q,s\<rangle> -1\<rightarrow> \<langle>p,SET x a#q,s[x\<mapsto> a s]\<rangle>"
berghofe@23746
    78
| "b s \<Longrightarrow> \<langle>JMPF b i#p,q,s\<rangle> -1\<rightarrow> \<langle>p,JMPF b i#q,s\<rangle>"
berghofe@23746
    79
| "\<lbrakk> \<not> b s; i \<le> size p \<rbrakk>
nipkow@13095
    80
   \<Longrightarrow> \<langle>JMPF b i # p, q, s\<rangle> -1\<rightarrow> \<langle>drop i p, rev(take i p) @ JMPF b i # q, s\<rangle>"
berghofe@23746
    81
| "i \<le> size q
nipkow@13095
    82
   \<Longrightarrow> \<langle>JMPB i # p, q, s\<rangle> -1\<rightarrow> \<langle>rev(take i q) @ JMPB i # p, drop i q, s\<rangle>"
nipkow@13095
    83
berghofe@23746
    84
abbreviation
berghofe@23746
    85
  stepa :: "[instrs,instrs,state, instrs,instrs,state] \<Rightarrow> bool"
berghofe@23746
    86
    ("((1\<langle>_,/_,/_\<rangle>)/ -*\<rightarrow> (1\<langle>_,/_,/_\<rangle>))" 50)  where
berghofe@23746
    87
  "\<langle>p,q,s\<rangle> -*\<rightarrow> \<langle>p',q',t\<rangle> == ((p,q,s),p',q',t) : (stepa1^*)"
berghofe@23746
    88
berghofe@23746
    89
abbreviation
berghofe@23746
    90
  stepan :: "[instrs,instrs,state, nat, instrs,instrs,state] \<Rightarrow> bool"
berghofe@23746
    91
    ("((1\<langle>_,/_,/_\<rangle>)/ -_\<rightarrow> (1\<langle>_,/_,/_\<rangle>))" 50) where
berghofe@23746
    92
  "\<langle>p,q,s\<rangle> -i\<rightarrow> \<langle>p',q',t\<rangle> == ((p,q,s),p',q',t) : (stepa1^i)"
berghofe@23746
    93
berghofe@23746
    94
inductive_cases execE: "((i#is,p,s), (is',p',s')) : stepa1"
nipkow@13095
    95
nipkow@13095
    96
lemma exec_simp[simp]:
nipkow@13095
    97
 "(\<langle>i#p,q,s\<rangle> -1\<rightarrow> \<langle>p',q',t\<rangle>) = (case i of
nipkow@13675
    98
 SET x a \<Rightarrow> t = s[x\<mapsto> a s] \<and> p' = p \<and> q' = i#q |
nipkow@13095
    99
 JMPF b n \<Rightarrow> t=s \<and> (if b s then p' = p \<and> q' = i#q
nipkow@13095
   100
            else n \<le> size p \<and> p' = drop n p \<and> q' = rev(take n p) @ i # q) |
nipkow@13095
   101
 JMPB n \<Rightarrow> n \<le> size q \<and> t=s \<and> p' = rev(take n q) @ i # p \<and> q' = drop n q)"
nipkow@13095
   102
apply(rule iffI)
nipkow@13095
   103
defer
nipkow@13095
   104
apply(clarsimp simp add: stepa1.intros split: instr.split_asm split_if_asm)
nipkow@13095
   105
apply(erule execE)
nipkow@13095
   106
apply(simp_all)
nipkow@13095
   107
done
nipkow@13095
   108
nipkow@13095
   109
lemma execn_simp[simp]:
nipkow@13095
   110
"(\<langle>i#p,q,s\<rangle> -n\<rightarrow> \<langle>p'',q'',u\<rangle>) =
nipkow@13095
   111
 (n=0 \<and> p'' = i#p \<and> q'' = q \<and> u = s \<or>
nipkow@13095
   112
  ((\<exists>m p' q' t. n = Suc m \<and>
nipkow@13095
   113
                \<langle>i#p,q,s\<rangle> -1\<rightarrow> \<langle>p',q',t\<rangle> \<and> \<langle>p',q',t\<rangle> -m\<rightarrow> \<langle>p'',q'',u\<rangle>)))"
nipkow@13095
   114
by(subst converse_in_rel_pow_eq, simp)
nipkow@13095
   115
nipkow@13095
   116
nipkow@13095
   117
lemma exec_star_simp[simp]: "(\<langle>i#p,q,s\<rangle> -*\<rightarrow> \<langle>p'',q'',u\<rangle>) =
nipkow@13095
   118
 (p'' = i#p & q''=q & u=s |
nipkow@13095
   119
 (\<exists>p' q' t. \<langle>i#p,q,s\<rangle> -1\<rightarrow> \<langle>p',q',t\<rangle> \<and> \<langle>p',q',t\<rangle> -*\<rightarrow> \<langle>p'',q'',u\<rangle>))"
nipkow@13095
   120
apply(simp add: rtrancl_is_UN_rel_pow del:exec_simp)
nipkow@13095
   121
apply(blast)
nipkow@13095
   122
done
nipkow@13095
   123
nipkow@13095
   124
declare nth_append[simp]
nipkow@13095
   125
nipkow@13095
   126
lemma rev_revD: "rev xs = rev ys \<Longrightarrow> xs = ys"
nipkow@13095
   127
by simp
nipkow@13095
   128
nipkow@13095
   129
lemma [simp]: "(rev xs @ rev ys = rev zs) = (ys @ xs = zs)"
nipkow@13095
   130
apply(rule iffI)
nipkow@13095
   131
 apply(rule rev_revD, simp)
nipkow@13095
   132
apply fastsimp
nipkow@13095
   133
done
nipkow@13095
   134
nipkow@13095
   135
lemma direction1:
nipkow@13095
   136
 "\<langle>q,p,s\<rangle> -1\<rightarrow> \<langle>q',p',t\<rangle> \<Longrightarrow>
nipkow@13095
   137
  rev p' @ q' = rev p @ q \<and> rev p @ q \<turnstile> \<langle>size p,s\<rangle> -1\<rightarrow> \<langle>size p',t\<rangle>"
wenzelm@18372
   138
apply(induct set: stepa1)
nipkow@13675
   139
   apply(simp add:exec01.SET)
nipkow@13095
   140
  apply(fastsimp intro:exec01.JMPFT)
nipkow@13095
   141
 apply simp
nipkow@13095
   142
 apply(rule exec01.JMPFF)
nipkow@13095
   143
     apply simp
nipkow@13095
   144
    apply fastsimp
nipkow@13095
   145
   apply simp
nipkow@13095
   146
  apply simp
nipkow@13095
   147
 apply simp
nipkow@13095
   148
apply(fastsimp simp add:exec01.JMPB)
nipkow@13095
   149
done
webertj@20217
   150
nipkow@13098
   151
(*
nipkow@13098
   152
lemma rev_take: "\<And>i. rev (take i xs) = drop (length xs - i) (rev xs)"
nipkow@13095
   153
apply(induct xs)
nipkow@13095
   154
 apply simp_all
nipkow@13095
   155
apply(case_tac i)
nipkow@13095
   156
apply simp_all
nipkow@13095
   157
done
nipkow@13095
   158
nipkow@13098
   159
lemma rev_drop: "\<And>i. rev (drop i xs) = take (length xs - i) (rev xs)"
nipkow@13098
   160
apply(induct xs)
nipkow@13098
   161
 apply simp_all
nipkow@13098
   162
apply(case_tac i)
nipkow@13098
   163
apply simp_all
nipkow@13098
   164
done
nipkow@13098
   165
*)
webertj@20217
   166
nipkow@13095
   167
lemma direction2:
nipkow@13095
   168
 "rpq \<turnstile> \<langle>sp,s\<rangle> -1\<rightarrow> \<langle>sp',t\<rangle> \<Longrightarrow>
wenzelm@18372
   169
  rpq = rev p @ q & sp = size p & sp' = size p' \<longrightarrow>
nipkow@13095
   170
          rev p' @ q' = rev p @ q \<longrightarrow> \<langle>q,p,s\<rangle> -1\<rightarrow> \<langle>q',p',t\<rangle>"
wenzelm@20503
   171
apply(induct arbitrary: p q p' q' set: exec01)
nipkow@13098
   172
   apply(clarsimp simp add: neq_Nil_conv append_eq_conv_conj)
nipkow@13095
   173
   apply(drule sym)
nipkow@13095
   174
   apply simp
nipkow@13095
   175
   apply(rule rev_revD)
nipkow@13095
   176
   apply simp
nipkow@13098
   177
  apply(clarsimp simp add: neq_Nil_conv append_eq_conv_conj)
nipkow@13095
   178
  apply(drule sym)
nipkow@13095
   179
  apply simp
nipkow@13095
   180
  apply(rule rev_revD)
nipkow@13095
   181
  apply simp
berghofe@13612
   182
 apply(simp (no_asm_use) add: neq_Nil_conv append_eq_conv_conj, clarify)+
nipkow@13095
   183
 apply(drule sym)
nipkow@13095
   184
 apply simp
nipkow@13095
   185
 apply(rule rev_revD)
nipkow@13095
   186
 apply simp
nipkow@13098
   187
apply(clarsimp simp add: neq_Nil_conv append_eq_conv_conj)
nipkow@13095
   188
apply(drule sym)
nipkow@13098
   189
apply(simp add:rev_take)
nipkow@13095
   190
apply(rule rev_revD)
nipkow@13098
   191
apply(simp add:rev_drop)
nipkow@13095
   192
done
nipkow@13095
   193
nipkow@13095
   194
nipkow@13095
   195
theorem M_eqiv:
nipkow@13095
   196
"(\<langle>q,p,s\<rangle> -1\<rightarrow> \<langle>q',p',t\<rangle>) =
nipkow@13095
   197
 (rev p' @ q' = rev p @ q \<and> rev p @ q \<turnstile> \<langle>size p,s\<rangle> -1\<rightarrow> \<langle>size p',t\<rangle>)"
wenzelm@18372
   198
  by (blast dest: direction1 direction2)
nipkow@13095
   199
nipkow@13095
   200
end