src/HOL/IMP/Machines.thy
author krauss
Fri Nov 24 13:44:51 2006 +0100 (2006-11-24)
changeset 21512 3786eb1b69d6
parent 20503 503ac4c5ef91
child 22267 ea31e6ea0e2e
permissions -rw-r--r--
Lemma "fundef_default_value" uses predicate instead of set.
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^*)"
wenzelm@18372
     7
  by (fast intro: rtrancl.intros 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
wenzelm@18372
    39
consts  exec01 :: "instr list \<Rightarrow> ((nat\<times>state) \<times> (nat\<times>state))set"
nipkow@13095
    40
syntax
nipkow@13095
    41
  "_exec01" :: "[instrs, nat,state, nat,state] \<Rightarrow> bool"
nipkow@13095
    42
               ("(_/ |- (1<_,/_>)/ -1-> (1<_,/_>))" [50,0,0,0,0] 50)
nipkow@13095
    43
  "_exec0s" :: "[instrs, nat,state, nat,state] \<Rightarrow> bool"
nipkow@13095
    44
               ("(_/ |- (1<_,/_>)/ -*-> (1<_,/_>))" [50,0,0,0,0] 50)
nipkow@13095
    45
  "_exec0n" :: "[instrs, nat,state, nat, nat,state] \<Rightarrow> bool"
nipkow@13095
    46
               ("(_/ |- (1<_,/_>)/ -_-> (1<_,/_>))" [50,0,0,0,0] 50)
nipkow@13095
    47
nipkow@13095
    48
syntax (xsymbols)
nipkow@13095
    49
  "_exec01" :: "[instrs, nat,state, nat,state] \<Rightarrow> bool"
nipkow@13095
    50
               ("(_/ \<turnstile> (1\<langle>_,/_\<rangle>)/ -1\<rightarrow> (1\<langle>_,/_\<rangle>))" [50,0,0,0,0] 50)
nipkow@13095
    51
  "_exec0s" :: "[instrs, nat,state, nat,state] \<Rightarrow> bool"
nipkow@13095
    52
               ("(_/ \<turnstile> (1\<langle>_,/_\<rangle>)/ -*\<rightarrow> (1\<langle>_,/_\<rangle>))" [50,0,0,0,0] 50)
nipkow@13095
    53
  "_exec0n" :: "[instrs, nat,state, nat, nat,state] \<Rightarrow> bool"
nipkow@13095
    54
               ("(_/ \<turnstile> (1\<langle>_,/_\<rangle>)/ -_\<rightarrow> (1\<langle>_,/_\<rangle>))" [50,0,0,0,0] 50)
nipkow@13095
    55
kleing@14565
    56
syntax (HTML output)
kleing@14565
    57
  "_exec01" :: "[instrs, nat,state, nat,state] \<Rightarrow> bool"
kleing@14565
    58
               ("(_/ |- (1\<langle>_,/_\<rangle>)/ -1\<rightarrow> (1\<langle>_,/_\<rangle>))" [50,0,0,0,0] 50)
kleing@14565
    59
  "_exec0s" :: "[instrs, nat,state, nat,state] \<Rightarrow> bool"
kleing@14565
    60
               ("(_/ |- (1\<langle>_,/_\<rangle>)/ -*\<rightarrow> (1\<langle>_,/_\<rangle>))" [50,0,0,0,0] 50)
kleing@14565
    61
  "_exec0n" :: "[instrs, nat,state, nat, nat,state] \<Rightarrow> bool"
kleing@14565
    62
               ("(_/ |- (1\<langle>_,/_\<rangle>)/ -_\<rightarrow> (1\<langle>_,/_\<rangle>))" [50,0,0,0,0] 50)
kleing@14565
    63
wenzelm@18372
    64
translations
nipkow@13095
    65
  "p \<turnstile> \<langle>i,s\<rangle> -1\<rightarrow> \<langle>j,t\<rangle>" == "((i,s),j,t) : (exec01 p)"
nipkow@13095
    66
  "p \<turnstile> \<langle>i,s\<rangle> -*\<rightarrow> \<langle>j,t\<rangle>" == "((i,s),j,t) : (exec01 p)^*"
nipkow@13095
    67
  "p \<turnstile> \<langle>i,s\<rangle> -n\<rightarrow> \<langle>j,t\<rangle>" == "((i,s),j,t) : (exec01 p)^n"
nipkow@13095
    68
nipkow@13095
    69
inductive "exec01 P"
nipkow@13095
    70
intros
nipkow@13675
    71
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>"
nipkow@13095
    72
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>"
nipkow@13095
    73
JMPFF: "\<lbrakk> n<size P; P!n = JMPF b i; \<not>b s; m=n+i+1; m \<le> size P \<rbrakk>
nipkow@13095
    74
        \<Longrightarrow> P \<turnstile> \<langle>n,s\<rangle> -1\<rightarrow> \<langle>m,s\<rangle>"
nipkow@13095
    75
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
    76
nipkow@13095
    77
subsection "M0 with lists"
nipkow@13095
    78
nipkow@13095
    79
text {* We describe execution of programs in the machine by
nipkow@13095
    80
  an operational (small step) semantics:
nipkow@13095
    81
*}
nipkow@13095
    82
nipkow@13095
    83
types config = "instrs \<times> instrs \<times> state"
nipkow@13095
    84
nipkow@13095
    85
consts  stepa1 :: "(config \<times> config)set"
nipkow@13095
    86
nipkow@13095
    87
syntax
nipkow@13095
    88
  "_stepa1" :: "[instrs,instrs,state, instrs,instrs,state] \<Rightarrow> bool"
nipkow@13095
    89
               ("((1<_,/_,/_>)/ -1-> (1<_,/_,/_>))" 50)
nipkow@13095
    90
  "_stepa" :: "[instrs,instrs,state, instrs,instrs,state] \<Rightarrow> bool"
nipkow@13095
    91
               ("((1<_,/_,/_>)/ -*-> (1<_,/_,/_>))" 50)
nipkow@13095
    92
  "_stepan" :: "[state,instrs,instrs, nat, instrs,instrs,state] \<Rightarrow> bool"
nipkow@13095
    93
               ("((1<_,/_,/_>)/ -_-> (1<_,/_,/_>))" 50)
nipkow@13095
    94
nipkow@13095
    95
syntax (xsymbols)
nipkow@13095
    96
  "_stepa1" :: "[instrs,instrs,state, instrs,instrs,state] \<Rightarrow> bool"
nipkow@13095
    97
               ("((1\<langle>_,/_,/_\<rangle>)/ -1\<rightarrow> (1\<langle>_,/_,/_\<rangle>))" 50)
nipkow@13095
    98
  "_stepa" :: "[instrs,instrs,state, instrs,instrs,state] \<Rightarrow> bool"
nipkow@13095
    99
               ("((1\<langle>_,/_,/_\<rangle>)/ -*\<rightarrow> (1\<langle>_,/_,/_\<rangle>))" 50)
nipkow@13095
   100
  "_stepan" :: "[instrs,instrs,state, nat, instrs,instrs,state] \<Rightarrow> bool"
nipkow@13095
   101
               ("((1\<langle>_,/_,/_\<rangle>)/ -_\<rightarrow> (1\<langle>_,/_,/_\<rangle>))" 50)
nipkow@13095
   102
wenzelm@18372
   103
translations
nipkow@13095
   104
  "\<langle>p,q,s\<rangle> -1\<rightarrow> \<langle>p',q',t\<rangle>" == "((p,q,s),p',q',t) : stepa1"
nipkow@13095
   105
  "\<langle>p,q,s\<rangle> -*\<rightarrow> \<langle>p',q',t\<rangle>" == "((p,q,s),p',q',t) : (stepa1^*)"
nipkow@13095
   106
  "\<langle>p,q,s\<rangle> -i\<rightarrow> \<langle>p',q',t\<rangle>" == "((p,q,s),p',q',t) : (stepa1^i)"
nipkow@13095
   107
nipkow@13095
   108
nipkow@13095
   109
inductive "stepa1"
nipkow@13095
   110
intros
nipkow@13675
   111
  "\<langle>SET x a#p,q,s\<rangle> -1\<rightarrow> \<langle>p,SET x a#q,s[x\<mapsto> a s]\<rangle>"
nipkow@13095
   112
  "b s \<Longrightarrow> \<langle>JMPF b i#p,q,s\<rangle> -1\<rightarrow> \<langle>p,JMPF b i#q,s\<rangle>"
nipkow@13095
   113
  "\<lbrakk> \<not> b s; i \<le> size p \<rbrakk>
nipkow@13095
   114
   \<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>"
nipkow@13095
   115
  "i \<le> size q
nipkow@13095
   116
   \<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
   117
nipkow@13095
   118
inductive_cases execE: "((i#is,p,s),next) : stepa1"
nipkow@13095
   119
nipkow@13095
   120
lemma exec_simp[simp]:
nipkow@13095
   121
 "(\<langle>i#p,q,s\<rangle> -1\<rightarrow> \<langle>p',q',t\<rangle>) = (case i of
nipkow@13675
   122
 SET x a \<Rightarrow> t = s[x\<mapsto> a s] \<and> p' = p \<and> q' = i#q |
nipkow@13095
   123
 JMPF b n \<Rightarrow> t=s \<and> (if b s then p' = p \<and> q' = i#q
nipkow@13095
   124
            else n \<le> size p \<and> p' = drop n p \<and> q' = rev(take n p) @ i # q) |
nipkow@13095
   125
 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
   126
apply(rule iffI)
nipkow@13095
   127
defer
nipkow@13095
   128
apply(clarsimp simp add: stepa1.intros split: instr.split_asm split_if_asm)
nipkow@13095
   129
apply(erule execE)
nipkow@13095
   130
apply(simp_all)
nipkow@13095
   131
done
nipkow@13095
   132
nipkow@13095
   133
lemma execn_simp[simp]:
nipkow@13095
   134
"(\<langle>i#p,q,s\<rangle> -n\<rightarrow> \<langle>p'',q'',u\<rangle>) =
nipkow@13095
   135
 (n=0 \<and> p'' = i#p \<and> q'' = q \<and> u = s \<or>
nipkow@13095
   136
  ((\<exists>m p' q' t. n = Suc m \<and>
nipkow@13095
   137
                \<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
   138
by(subst converse_in_rel_pow_eq, simp)
nipkow@13095
   139
nipkow@13095
   140
nipkow@13095
   141
lemma exec_star_simp[simp]: "(\<langle>i#p,q,s\<rangle> -*\<rightarrow> \<langle>p'',q'',u\<rangle>) =
nipkow@13095
   142
 (p'' = i#p & q''=q & u=s |
nipkow@13095
   143
 (\<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
   144
apply(simp add: rtrancl_is_UN_rel_pow del:exec_simp)
nipkow@13095
   145
apply(blast)
nipkow@13095
   146
done
nipkow@13095
   147
nipkow@13095
   148
declare nth_append[simp]
nipkow@13095
   149
nipkow@13095
   150
lemma rev_revD: "rev xs = rev ys \<Longrightarrow> xs = ys"
nipkow@13095
   151
by simp
nipkow@13095
   152
nipkow@13095
   153
lemma [simp]: "(rev xs @ rev ys = rev zs) = (ys @ xs = zs)"
nipkow@13095
   154
apply(rule iffI)
nipkow@13095
   155
 apply(rule rev_revD, simp)
nipkow@13095
   156
apply fastsimp
nipkow@13095
   157
done
nipkow@13095
   158
nipkow@13095
   159
lemma direction1:
nipkow@13095
   160
 "\<langle>q,p,s\<rangle> -1\<rightarrow> \<langle>q',p',t\<rangle> \<Longrightarrow>
nipkow@13095
   161
  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
   162
apply(induct set: stepa1)
nipkow@13675
   163
   apply(simp add:exec01.SET)
nipkow@13095
   164
  apply(fastsimp intro:exec01.JMPFT)
nipkow@13095
   165
 apply simp
nipkow@13095
   166
 apply(rule exec01.JMPFF)
nipkow@13095
   167
     apply simp
nipkow@13095
   168
    apply fastsimp
nipkow@13095
   169
   apply simp
nipkow@13095
   170
  apply simp
nipkow@13095
   171
 apply simp
nipkow@13095
   172
apply(fastsimp simp add:exec01.JMPB)
nipkow@13095
   173
done
webertj@20217
   174
nipkow@13098
   175
(*
nipkow@13098
   176
lemma rev_take: "\<And>i. rev (take i xs) = drop (length xs - i) (rev xs)"
nipkow@13095
   177
apply(induct xs)
nipkow@13095
   178
 apply simp_all
nipkow@13095
   179
apply(case_tac i)
nipkow@13095
   180
apply simp_all
nipkow@13095
   181
done
nipkow@13095
   182
nipkow@13098
   183
lemma rev_drop: "\<And>i. rev (drop i xs) = take (length xs - i) (rev xs)"
nipkow@13098
   184
apply(induct xs)
nipkow@13098
   185
 apply simp_all
nipkow@13098
   186
apply(case_tac i)
nipkow@13098
   187
apply simp_all
nipkow@13098
   188
done
nipkow@13098
   189
*)
webertj@20217
   190
nipkow@13095
   191
lemma direction2:
nipkow@13095
   192
 "rpq \<turnstile> \<langle>sp,s\<rangle> -1\<rightarrow> \<langle>sp',t\<rangle> \<Longrightarrow>
wenzelm@18372
   193
  rpq = rev p @ q & sp = size p & sp' = size p' \<longrightarrow>
nipkow@13095
   194
          rev p' @ q' = rev p @ q \<longrightarrow> \<langle>q,p,s\<rangle> -1\<rightarrow> \<langle>q',p',t\<rangle>"
wenzelm@20503
   195
apply(induct arbitrary: p q p' q' set: exec01)
nipkow@13098
   196
   apply(clarsimp simp add: neq_Nil_conv append_eq_conv_conj)
nipkow@13095
   197
   apply(drule sym)
nipkow@13095
   198
   apply simp
nipkow@13095
   199
   apply(rule rev_revD)
nipkow@13095
   200
   apply simp
nipkow@13098
   201
  apply(clarsimp simp add: neq_Nil_conv append_eq_conv_conj)
nipkow@13095
   202
  apply(drule sym)
nipkow@13095
   203
  apply simp
nipkow@13095
   204
  apply(rule rev_revD)
nipkow@13095
   205
  apply simp
berghofe@13612
   206
 apply(simp (no_asm_use) add: neq_Nil_conv append_eq_conv_conj, clarify)+
nipkow@13095
   207
 apply(drule sym)
nipkow@13095
   208
 apply simp
nipkow@13095
   209
 apply(rule rev_revD)
nipkow@13095
   210
 apply simp
nipkow@13098
   211
apply(clarsimp simp add: neq_Nil_conv append_eq_conv_conj)
nipkow@13095
   212
apply(drule sym)
nipkow@13098
   213
apply(simp add:rev_take)
nipkow@13095
   214
apply(rule rev_revD)
nipkow@13098
   215
apply(simp add:rev_drop)
nipkow@13095
   216
done
nipkow@13095
   217
nipkow@13095
   218
nipkow@13095
   219
theorem M_eqiv:
nipkow@13095
   220
"(\<langle>q,p,s\<rangle> -1\<rightarrow> \<langle>q',p',t\<rangle>) =
nipkow@13095
   221
 (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
   222
  by (blast dest: direction1 direction2)
nipkow@13095
   223
nipkow@13095
   224
end