thys/turing_basic.thy
author Christian Urban <christian dot urban at kcl dot ac dot uk>
Tue, 15 Jan 2013 19:05:15 +0000
changeset 43 a8785fa80278
parent 41 6d89ed67ba27
child 47 251e192339b7
permissions -rw-r--r--
updated literature
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
43
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
     1
(* Title: Turing machines
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
     2
   Author: Xu Jian <xujian817@hotmail.com>
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
     3
   Maintainer: Xu Jian
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
     4
*)
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
     5
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
     6
theory turing_basic
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
     7
imports Main
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
     8
begin
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
     9
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
    10
section {* Basic definitions of Turing machine *}
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
    11
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
    12
datatype action = W0 | W1 | L | R | Nop
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
    13
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
    14
datatype cell = Bk | Oc
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
    15
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
    16
type_synonym tape = "cell list \<times> cell list"
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
    17
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
    18
type_synonym state = nat
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
    19
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
    20
type_synonym instr = "action \<times> state"
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
    21
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
    22
type_synonym tprog = "instr list \<times> nat"
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
    23
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
    24
type_synonym config = "state \<times> tape"
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
    25
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
    26
fun nth_of where
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
    27
  "nth_of xs i = (if i \<ge> length xs then None
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
    28
                  else Some (xs ! i))"
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
    29
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
    30
lemma nth_of_map [simp]:
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
    31
  shows "nth_of (map f p) n = (case (nth_of p n) of None \<Rightarrow> None | Some x \<Rightarrow> Some (f x))"
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
    32
apply(induct p arbitrary: n)
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
    33
apply(auto)
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
    34
apply(case_tac n)
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
    35
apply(auto)
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
    36
done
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
    37
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
    38
fun 
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
    39
  fetch :: "instr list \<Rightarrow> state \<Rightarrow> cell \<Rightarrow> instr"
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
    40
where
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
    41
  "fetch p 0 b = (Nop, 0)"
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
    42
| "fetch p (Suc s) Bk = 
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
    43
     (case nth_of p (2 * s) of
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
    44
        Some i \<Rightarrow> i
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
    45
      | None \<Rightarrow> (Nop, 0))"
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
    46
|"fetch p (Suc s) Oc = 
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
    47
     (case nth_of p ((2 * s) + 1) of
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
    48
         Some i \<Rightarrow> i
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
    49
       | None \<Rightarrow> (Nop, 0))"
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
    50
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
    51
lemma fetch_Nil [simp]:
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
    52
  shows "fetch [] s b = (Nop, 0)"
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
    53
apply(case_tac s)
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
    54
apply(auto)
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
    55
apply(case_tac b)
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
    56
apply(auto)
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
    57
done
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
    58
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
    59
fun 
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
    60
  update :: "action \<Rightarrow> tape \<Rightarrow> tape"
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
    61
where 
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
    62
  "update W0 (l, r) = (l, Bk # (tl r))" 
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
    63
| "update W1 (l, r) = (l, Oc # (tl r))"
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
    64
| "update L (l, r) = (if l = [] then ([], Bk # r) else (tl l, (hd l) # r))" 
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
    65
| "update R (l, r) = (if r = [] then (Bk # l, []) else ((hd r) # l, tl r))" 
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
    66
| "update Nop (l, r) = (l, r)"
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
    67
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
    68
abbreviation 
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
    69
  "read r == if (r = []) then Bk else hd r"
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
    70
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
    71
fun step :: "config \<Rightarrow> tprog \<Rightarrow> config"
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
    72
  where 
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
    73
  "step (s, l, r) (p, off) = 
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
    74
  (let (a, s') = fetch p (s - off) (read r) in (s', update a (l, r)))"
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
    75
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
    76
fun steps :: "config \<Rightarrow> tprog \<Rightarrow> nat \<Rightarrow> config"
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
    77
  where
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
    78
  "steps c p 0 = c" |
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
    79
  "steps c p (Suc n) = steps (step c p) p n"
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
    80
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
    81
lemma step_red [simp]: 
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
    82
  shows "steps c p (Suc n) = step (steps c p n) p"
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
    83
by (induct n arbitrary: c) (auto)
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
    84
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
    85
lemma steps_add [simp]: 
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
    86
  shows "steps c p (m + n) = steps (steps c p m) p n"
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
    87
by (induct m arbitrary: c) (auto)
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
    88
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
    89
fun 
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
    90
  tm_wf :: "tprog \<Rightarrow> bool"
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
    91
where
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
    92
  "tm_wf (p, off) = (length p \<ge> 2 \<and> length p mod 2 = 0 \<and> 
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
    93
                    (\<forall>(a, s) \<in> set p. s \<le> length p div 2
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
    94
                                             + off \<and> s \<ge> off))"
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
    95
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
    96
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
    97
(* FIXME: needed? *)
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
    98
lemma halt_lemma: 
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
    99
  "\<lbrakk>wf LE; \<forall>n. (\<not> P (f n) \<longrightarrow> (f (Suc n), (f n)) \<in> LE)\<rbrakk> \<Longrightarrow> \<exists>n. P (f n)"
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   100
by (metis wf_iff_no_infinite_down_chain)
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   101
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   102
abbreviation exponent :: "'a \<Rightarrow> nat \<Rightarrow> 'a list" ("_ \<up> _" [100, 99] 100)
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   103
  where "x \<up> n == replicate n x"
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   104
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   105
definition tinres :: "cell list \<Rightarrow> cell list \<Rightarrow> bool"
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   106
  where
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   107
  "tinres xs ys = (\<exists>n. xs = ys @ Bk \<up> n \<or> ys = xs @ Bk \<up> n)"
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   108
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   109
fun 
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   110
  shift :: "instr list \<Rightarrow> nat \<Rightarrow> instr list"
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   111
where
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   112
  "shift p n = (map (\<lambda> (a, s). (a, (if s = 0 then 0 else s + n))) p)"
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   113
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   114
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   115
lemma length_shift [simp]: 
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   116
  "length (shift p n) = length p"
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   117
by (simp)
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   118
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   119
fun 
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   120
  adjust :: "instr list \<Rightarrow> instr list"
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   121
where
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   122
  "adjust p = map (\<lambda> (a, s). (a, if s = 0 then (Suc (length p div 2)) else s)) p"
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   123
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   124
lemma length_adjust[simp]: 
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   125
  shows "length (adjust p) = length p"
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   126
by (induct p) (auto)
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   127
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   128
fun
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   129
  tm_comp :: "instr list \<Rightarrow> instr list \<Rightarrow> instr list" ("_ |+| _" [0, 0] 100)
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   130
where
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   131
  "tm_comp p1 p2 = ((adjust p1) @ (shift p2 ((length p1) div 2)))"
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   132
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   133
fun
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   134
  is_final :: "config \<Rightarrow> bool"
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   135
where
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   136
  "is_final (s, l, r) = (s = 0)"
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   137
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   138
lemma is_final_steps:
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   139
  assumes "is_final (s, l, r)"
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   140
  shows "is_final (steps (s, l, r) (p, off) n)"
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   141
using assms by (induct n) (auto)
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   142
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   143
fun 
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   144
  holds_for :: "(tape \<Rightarrow> bool) \<Rightarrow> config \<Rightarrow> bool" ("_ holds'_for _" [100, 99] 100)
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   145
where
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   146
  "P holds_for (s, l, r) = P (l, r)"  
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   147
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   148
(*
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   149
lemma step_0 [simp]: 
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   150
  shows "step (0, (l, r)) p = (0, (l, r))"
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   151
by simp
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   152
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   153
lemma steps_0 [simp]: 
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   154
  shows "steps (0, (l, r)) p n = (0, (l, r))"
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   155
by (induct n) (simp_all)
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   156
*)
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   157
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   158
lemma is_final_holds[simp]:
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   159
  assumes "is_final c"
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   160
  shows "Q holds_for (steps c (p, off) n) = Q holds_for c"
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   161
using assms 
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   162
apply(induct n)
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   163
apply(case_tac [!] c)
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   164
apply(auto)
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   165
done
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   166
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   167
type_synonym assert = "tape \<Rightarrow> bool"
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   168
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   169
definition assert_imp :: "assert \<Rightarrow> assert \<Rightarrow> bool" ("_ \<mapsto> _" [0, 0] 100)
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   170
  where
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   171
  "assert_imp P Q = (\<forall>l r. P (l, r) \<longrightarrow> Q (l, r))"
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   172
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   173
lemma holds_for_imp:
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   174
  assumes "P holds_for c"
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   175
  and "P \<mapsto> Q"
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   176
  shows "Q holds_for c"
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   177
using assms unfolding assert_imp_def by (case_tac c, auto)
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   178
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   179
lemma test:
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   180
  assumes "is_final (steps (1, (l, r)) p n1)"
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   181
  and     "is_final (steps (1, (l, r)) p n2)"
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   182
  shows "Q holds_for (steps (1, (l, r)) p n1) \<longleftrightarrow> Q holds_for (steps (1, (l, r)) p n2)"
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   183
proof -
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   184
  obtain n3 where "n1 = n2 + n3 \<or> n2 = n1 + n3"
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   185
    by (metis le_iff_add nat_le_linear)
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   186
  with assms show "Q holds_for (steps (1, (l, r)) p n1) \<longleftrightarrow> Q holds_for (steps (1, (l, r)) p n2)"  
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   187
    by(case_tac p) (auto)
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   188
qed
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   189
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   190
definition
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   191
  Hoare :: "assert \<Rightarrow> tprog \<Rightarrow> assert \<Rightarrow> bool" ("({(1_)}/ (_)/ {(1_)})" 50)
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   192
where
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   193
  "{P} p {Q} \<equiv> 
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   194
     (\<forall>l r. P (l, r) \<longrightarrow> (\<exists>n. is_final (steps (1, (l, r)) p n) \<and> Q holds_for (steps (1, (l, r)) p n)))"
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   195
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   196
lemma HoareI:
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   197
  assumes "\<And>l r. P (l, r) \<Longrightarrow> \<exists>n. is_final (steps (1, (l, r)) p n) \<and> Q holds_for (steps (1, (l, r)) p n)"
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   198
  shows "{P} p {Q}"
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   199
unfolding Hoare_def using assms by auto
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   200
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   201
text {*
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   202
{P1} A {Q1}   {P2} B {Q2}  Q1 \<mapsto> P2
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   203
-----------------------------------
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   204
    {P1} A |+| B {Q2}
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   205
*}
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   206
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   207
lemma step_0 [simp]: 
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   208
  shows "step (0, (l, r)) p = (0, (l, r))"
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   209
by (case_tac p, simp)
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   210
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   211
lemma steps_0 [simp]: 
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   212
  shows "steps (0, (l, r)) p n = (0, (l, r))"
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   213
by (induct n) (simp_all)
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   214
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   215
declare steps.simps[simp del]
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   216
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   217
lemma before_final: 
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   218
  assumes "steps (1, tp) A n = (0, tp')"
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   219
  obtains n' where "\<not> is_final (steps (1, tp) A n')" 
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   220
        and "steps (1, tp) A (Suc n') = (0, tp')"
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   221
proof -
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   222
  from assms have "\<exists> n'. \<not> is_final (steps (1, tp) A n') \<and> 
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   223
               steps (1, tp) A (Suc n') = (0, tp')"
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   224
  proof(induct n arbitrary: tp', simp add: steps.simps)
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   225
    fix n tp'
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   226
    assume ind: 
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   227
      "\<And>tp'. steps (1, tp) A n = (0, tp') \<Longrightarrow>
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   228
      \<exists>n'. \<not> is_final (steps (1, tp) A n') \<and> steps (1, tp) A (Suc n') = (0, tp')"
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   229
    and h: " steps (1, tp) A (Suc n) = (0, tp')"
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   230
    from h show  "\<exists>n'. \<not> is_final (steps (1, tp) A n') \<and> steps (1, tp) A (Suc n') = (0, tp')"
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   231
    proof(simp add: step_red del: steps.simps, 
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   232
                     case_tac "(steps (Suc 0, tp) A n)", case_tac "a = 0", simp)
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   233
      fix a b c
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   234
      assume " steps (Suc 0, tp) A n = (0, tp')"
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   235
      hence "\<exists>n'. \<not> is_final (steps (1, tp) A n') \<and> steps (1, tp) A (Suc n') = (0, tp')"
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   236
        apply(rule_tac ind, simp)
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   237
        done
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   238
      thus "\<exists>n'. \<not> is_final (steps (Suc 0, tp) A n') \<and> step (steps (Suc 0, tp) A n') A = (0, tp')"
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   239
        apply(simp)
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   240
        done
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   241
    next
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   242
      fix a b c
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   243
      assume "steps (Suc 0, tp) A n = (a, b, c)"
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   244
             "step (steps (Suc 0, tp) A n) A = (0, tp')"
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   245
        "a \<noteq> 0"
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   246
      thus "\<exists>n'. \<not> is_final (steps (Suc 0, tp) A n') \<and> 
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   247
        step (steps (Suc 0, tp) A n') A = (0, tp')"
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   248
        apply(rule_tac x = n in exI, simp)
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   249
        done
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   250
    qed
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   251
  qed
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   252
  thus "(\<And>n'. \<lbrakk>\<not> is_final (steps (1, tp) A n'); 
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   253
    steps (1, tp) A (Suc n') = (0, tp')\<rbrakk> \<Longrightarrow> thesis) \<Longrightarrow> thesis"
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   254
    by auto
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   255
qed
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   256
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   257
declare tm_comp.simps [simp del] adjust.simps[simp del] shift.simps[simp del]
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   258
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   259
lemma length_comp:
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   260
"length (A |+| B) = length A + length B"
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   261
apply(auto simp: tm_comp.simps)
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   262
done
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   263
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   264
lemma tmcomp_fetch_in_first:
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   265
  assumes "case (fetch A a x) of (ac, ns) \<Rightarrow> ns \<noteq> 0"
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   266
  shows "fetch (A |+| B) a x = fetch A a x"
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   267
using assms
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   268
apply(case_tac a, case_tac [!] x, 
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   269
auto simp: length_comp tm_comp.simps length_adjust nth_append)
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   270
apply(simp_all add: adjust.simps)
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   271
done
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   272
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   273
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   274
lemma is_final_eq: "is_final (ba, tp) = (ba = 0)"
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   275
apply(case_tac tp, simp add: is_final.simps)
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   276
done
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   277
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   278
lemma t_merge_pre_eq_step: 
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   279
  assumes step: "step (a, b, c) (A, 0) = cf"
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   280
  and     tm_wf: "tm_wf (A, 0)" 
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   281
  and     unfinal: "\<not> is_final cf"
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   282
  shows "step (a, b, c) (A |+| B, 0) = cf"
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   283
proof -
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   284
  have "fetch (A |+| B) a (read c) = fetch A a (read c)"
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   285
  proof(rule_tac tmcomp_fetch_in_first)
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   286
    from step and unfinal show "case fetch A a (read c) of (ac, ns) \<Rightarrow> ns \<noteq> 0"
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   287
      apply(auto simp: is_final.simps)
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   288
      apply(case_tac "fetch A a (read c)", simp_all add: is_final_eq)
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   289
      done
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   290
  qed      
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   291
  thus "?thesis"
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   292
    using step
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   293
    apply(auto simp: step.simps is_final.simps)
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   294
    done
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   295
qed
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   296
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   297
declare tm_wf.simps[simp del] step.simps[simp del]
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   298
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   299
lemma t_merge_pre_eq:  
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   300
  "\<lbrakk>steps (Suc 0, tp) (A, 0) stp = cf; \<not> is_final cf; tm_wf (A, 0)\<rbrakk>
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   301
  \<Longrightarrow> steps (Suc 0, tp) (A |+| B, 0) stp = cf"
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   302
proof(induct stp arbitrary: cf, simp add: steps.simps)
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   303
  fix stp cf
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   304
  assume ind: "\<And>cf. \<lbrakk>steps (Suc 0, tp) (A, 0) stp = cf; \<not> is_final cf; tm_wf (A, 0)\<rbrakk> 
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   305
    \<Longrightarrow> steps (Suc 0, tp) (A |+| B, 0) stp = cf"
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   306
  and h: "steps (Suc 0, tp) (A, 0) (Suc stp) = cf"
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   307
      "\<not> is_final cf" "tm_wf (A, 0)"
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   308
  from h show "steps (Suc 0, tp) (A |+| B, 0) (Suc stp) = cf"
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   309
  proof(simp add: step_red, case_tac "(steps (Suc 0, tp) (A, 0) stp)", simp)
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   310
    fix a b c
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   311
    assume g: "steps (Suc 0, tp) (A, 0) stp = (a, b, c)"
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   312
      "step (a, b, c) (A, 0) = cf"
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   313
    have "(steps (Suc 0, tp) (A |+| B, 0) stp) = (a, b, c)"
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   314
    proof(rule ind, simp_all add: h g)
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   315
      show "0 < a"
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   316
        using g h
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   317
        apply(simp add: step_red)
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   318
        apply(case_tac a, auto simp: step_0)
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   319
        apply(case_tac "steps (Suc 0, tp) (A, 0) stp", simp)
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   320
        done
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   321
    qed
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   322
    thus "step (steps (Suc 0, tp) (A |+| B, 0) stp) (A |+| B, 0) = cf"
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   323
      apply(simp)
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   324
      apply(rule_tac t_merge_pre_eq_step, simp_all add: g h)
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   325
      done
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   326
  qed
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   327
qed
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   328
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   329
lemma tmcomp_fetch_in_first2:
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   330
  assumes "fetch A a x = (ac, 0)"
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   331
          "tm_wf (A, 0)"
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   332
          "a \<le> length A div 2" "a > 0"
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   333
  shows "fetch (A |+| B) a x = (ac, Suc (length A div 2))"
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   334
using assms
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   335
apply(case_tac a, case_tac [!] x, 
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   336
auto simp: length_comp tm_comp.simps length_adjust nth_append)
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   337
apply(simp_all add: adjust.simps)
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   338
done
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   339
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   340
lemma tmcomp_exec_after_first:
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   341
  "\<lbrakk>0 < a; step (a, b, c) (A, 0) = (0, tp'); tm_wf (A, 0); 
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   342
       a \<le> length A div 2\<rbrakk>
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   343
       \<Longrightarrow> step (a, b, c) (A |+| B, 0) = (Suc (length A div 2), tp')"
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   344
apply(simp add: step.simps, auto)
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   345
apply(case_tac "fetch A a Bk", simp add: tmcomp_fetch_in_first2)
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   346
apply(case_tac "fetch A a (hd c)", simp add: tmcomp_fetch_in_first2)
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   347
done
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   348
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   349
lemma step_nothalt_pre: "\<lbrakk>step (aa, ba, ca) (A, 0) = (a, b, c);  0 < a\<rbrakk> \<Longrightarrow> 0 < aa"
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   350
apply(case_tac "aa = 0", simp add: step_0, simp)
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   351
done
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   352
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   353
lemma nth_in_set: 
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   354
  "\<lbrakk> A ! i = x; i <  length A\<rbrakk> \<Longrightarrow> x \<in> set A"
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   355
by auto
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   356
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   357
lemma step_nothalt: 
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   358
  "\<lbrakk>step (aa, ba, ca) (A, 0) = (a, b, c); 0 < a; tm_wf (A, 0)\<rbrakk> \<Longrightarrow> 
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   359
  a \<le> length A div 2"
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   360
apply(simp add: step.simps)
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   361
apply(case_tac aa, case_tac [!] aa, auto split: if_splits simp: tm_wf.simps)
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   362
apply(case_tac "A ! (2 * nat)", simp)
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   363
apply(erule_tac x = "(aa, a)" in ballE, simp_all add: nth_in_set)
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   364
apply(case_tac "hd ca", auto split: if_splits simp: tm_wf.simps)
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   365
apply(case_tac "A ! (2 * nat)", simp)
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   366
apply(erule_tac x = "(aa, a)" in ballE, simp_all add: nth_in_set)
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   367
apply(case_tac "A ! (Suc (2 * nat))")
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   368
apply(erule_tac x = "(aa,bb)" in ballE, simp_all add: nth_in_set)
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   369
done
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   370
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   371
lemma steps_in_range: 
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   372
  " \<lbrakk>0 < a; steps (Suc 0, tp) (A, 0) stp = (a, b, c); tm_wf (A, 0)\<rbrakk>
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   373
  \<Longrightarrow> a \<le> length A div 2"
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   374
proof(induct stp arbitrary: a b c)
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   375
  fix a b c
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   376
  assume h: "0 < a" "steps (Suc 0, tp) (A, 0) 0 = (a, b, c)" 
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   377
            "tm_wf (A, 0)"
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   378
  thus "a \<le> length A div 2"
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   379
    apply(simp add: steps.simps tm_wf.simps, auto)
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   380
    done
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   381
next
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   382
  fix stp a b c
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   383
  assume ind: "\<And>a b c. \<lbrakk>0 < a; steps (Suc 0, tp) (A, 0) stp = (a, b, c); 
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   384
    tm_wf (A, 0)\<rbrakk> \<Longrightarrow> a \<le> length A div 2"
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   385
  and h: "0 < a" "steps (Suc 0, tp) (A, 0) (Suc stp) = (a, b, c)" "tm_wf (A, 0)"
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   386
  from h show "a \<le> length A div 2"
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   387
  proof(simp add: step_red, case_tac "(steps (Suc 0, tp) (A, 0) stp)", simp)
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   388
    fix aa ba ca
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   389
    assume g: "step (aa, ba, ca) (A, 0) = (a, b, c)" 
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   390
           "steps (Suc 0, tp) (A, 0) stp = (aa, ba, ca)"
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   391
    hence "aa \<le> length A div 2"
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   392
      apply(rule_tac ind, auto simp: h step_nothalt_pre)
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   393
      done
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   394
    thus "?thesis"
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   395
      using g h
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   396
      apply(rule_tac step_nothalt, auto)
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   397
      done
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   398
  qed
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   399
qed
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   400
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   401
lemma t_merge_pre_halt_same: 
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   402
  assumes a_ht: "steps (1, tp) (A, 0) n = (0, tp')"
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   403
  and a_wf: "tm_wf (A, 0)"
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   404
  obtains n' where "steps (1, tp) (A |+| B, 0) n' = (Suc (length A div 2), tp')"
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   405
proof -
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   406
  assume a: "\<And>n. steps (1, tp) (A |+| B, 0) n = (Suc (length A div 2), tp') \<Longrightarrow> thesis"
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   407
  obtain stp' where "\<not> is_final (steps (1, tp) (A, 0) stp')" and 
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   408
                          "steps (1, tp) (A, 0) (Suc stp') = (0, tp')"
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   409
  using a_ht before_final by blast
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   410
  then have "steps (1, tp) (A |+| B, 0) (Suc stp') = (Suc (length A div 2), tp')"
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   411
  proof(simp add: step_red)
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   412
    assume "\<not> is_final (steps (Suc 0, tp) (A, 0) stp')"
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   413
           " step (steps (Suc 0, tp) (A, 0) stp') (A, 0) = (0, tp')"
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   414
    moreover hence "(steps (Suc 0, tp) (A |+| B, 0) stp') = (steps (Suc 0, tp) (A, 0) stp')"
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   415
      apply(rule_tac t_merge_pre_eq)
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   416
      apply(simp_all add: a_wf a_ht)
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   417
      done
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   418
    ultimately show "step (steps (Suc 0, tp) (A |+| B, 0) stp') (A |+| B, 0) = (Suc (length A div 2), tp')"
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   419
      apply(case_tac " steps (Suc 0, tp) (A, 0) stp'", simp)
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   420
      apply(rule tmcomp_exec_after_first, simp_all add: a_wf)
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   421
      apply(erule_tac steps_in_range, auto simp: a_wf)
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   422
      done
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   423
  qed
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   424
  with a show thesis by blast
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   425
qed
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   426
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   427
lemma tm_comp_fetch_second_zero:
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   428
  "\<lbrakk>fetch B sa' x = (a, 0); tm_wf (A, 0); tm_wf (B, 0); sa' > 0\<rbrakk>
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   429
     \<Longrightarrow> fetch (A |+| B) (sa' + (length A div 2)) x = (a, 0)"
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   430
apply(case_tac x)
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   431
apply(case_tac [!] sa',
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   432
  auto simp: fetch.simps length_comp length_adjust nth_append tm_comp.simps
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   433
             tm_wf.simps shift.simps split: if_splits)
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   434
done 
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   435
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   436
lemma tm_comp_fetch_second_inst:
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   437
  "\<lbrakk>sa > 0; s > 0;  tm_wf (A, 0); tm_wf (B, 0); fetch B sa x = (a, s)\<rbrakk>
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   438
     \<Longrightarrow> fetch (A |+| B) (sa + length A div 2) x = (a, s + length A div 2)"
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   439
apply(case_tac x)
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   440
apply(case_tac [!] sa,
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   441
  auto simp: fetch.simps length_comp length_adjust nth_append tm_comp.simps
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   442
             tm_wf.simps shift.simps split: if_splits)
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   443
done 
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   444
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   445
lemma t_merge_second_same:
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   446
  assumes a_wf: "tm_wf (A, 0)"
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   447
  and b_wf: "tm_wf (B, 0)"
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   448
  and steps: "steps (Suc 0, l, r) (B, 0) stp = (s, l', r')"
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   449
  shows "steps (Suc (length A div 2), l, r)  (A |+| B, 0) stp
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   450
       = (if s = 0 then 0
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   451
          else s + length A div 2, l', r')"
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   452
using a_wf b_wf steps
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   453
proof(induct stp arbitrary: s l' r', simp add: steps.simps, simp)
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   454
  fix stpa sa l'a r'a
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   455
  assume ind: "\<And>s l' r'. steps (Suc 0, l, r) (B, 0) stpa = (s, l', r') \<Longrightarrow>
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   456
    steps (Suc (length A div 2), l, r) (A |+| B, 0) stpa = 
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   457
                (if s = 0 then 0 else s + length A div 2, l', r')"
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   458
  and h: "step (steps (Suc 0, l, r) (B, 0) stpa) (B, 0) = (sa, l'a, r'a)"
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   459
  obtain sa' l'' r'' where a: "(steps (Suc 0, l, r) (B, 0) stpa) = (sa', l'', r'')"
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   460
    apply(case_tac "steps (Suc 0, l, r) (B, 0) stpa", auto)
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   461
    done
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   462
  from this have b: "steps (Suc (length A div 2), l, r) (A |+| B, 0) stpa = 
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   463
                (if sa' = 0 then 0 else sa' + length A div 2, l'', r'')"
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   464
    apply(erule_tac ind)
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   465
    done
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   466
  from a b h show 
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   467
    "(sa = 0 \<longrightarrow> step (steps (Suc (length A div 2), l, r) (A |+| B, 0) stpa) (A |+| B, 0) = (0, l'a, r'a)) \<and>
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   468
    (0 < sa \<longrightarrow> step (steps (Suc (length A div 2), l, r) (A |+| B, 0) stpa) (A |+| B, 0) = (sa + length A div 2, l'a, r'a))"
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   469
  proof(case_tac "sa' = 0", auto)
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   470
    assume "step (sa', l'', r'') (B, 0) = (0, l'a, r'a)" "0 < sa'"
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   471
    thus "step (sa' + length A div 2, l'', r'') (A |+| B, 0) = (0, l'a, r'a)"
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   472
      using a_wf b_wf
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   473
      apply(simp add:  step.simps)
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   474
      apply(case_tac "fetch B sa' (read r'')", auto)
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   475
      apply(simp_all add: step.simps tm_comp_fetch_second_zero)
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   476
      done
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   477
  next
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   478
    assume "step (sa', l'', r'') (B, 0) = (sa, l'a, r'a)" "0 < sa'" "0 < sa"
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   479
    thus "step (sa' + length A div 2, l'', r'') (A |+| B, 0) = (sa + length A div 2, l'a, r'a)"
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   480
      using a_wf b_wf
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   481
      apply(simp add: step.simps)
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   482
      apply(case_tac "fetch B sa' (read r'')", auto)
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   483
      apply(simp_all add: step.simps tm_comp_fetch_second_inst)
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   484
      done
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   485
  qed
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   486
qed
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   487
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   488
lemma t_merge_second_halt_same:
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   489
  "\<lbrakk>tm_wf (A, 0); tm_wf (B, 0); 
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   490
   steps (1, l, r) (B, 0) stp = (0, l', r')\<rbrakk>
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   491
     \<Longrightarrow> steps (Suc (length A div 2), l, r)  (A |+| B, 0) stp
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   492
       = (0, l', r')"
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   493
using t_merge_second_same[where s = "0"]
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   494
apply(auto)
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   495
done
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   496
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   497
lemma Hoare_plus_halt: 
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   498
  assumes aimpb: "Q1 \<mapsto> P2"
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   499
  and A_wf : "tm_wf (A, 0)"
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   500
  and B_wf : "tm_wf (B, 0)"
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   501
  and A_halt : "{P1} (A, 0) {Q1}"
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   502
  and B_halt : "{P2} (B, 0) {Q2}"
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   503
  shows "{P1} (A |+| B, 0) {Q2}"
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   504
proof(rule HoareI)
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   505
  fix l r
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   506
  assume h: "P1 (l, r)"
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   507
  then obtain n1 where a: "is_final (steps (1, l, r) (A, 0) n1)" and b: "Q1 holds_for (steps (1, l, r) (A, 0) n1)"
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   508
    using A_halt unfolding Hoare_def by auto
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   509
  then obtain l' r' where "steps (1, l, r) (A, 0) n1 = (0, l', r')" and c: "Q1 holds_for (0, l', r')"
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   510
    by(case_tac "steps (1, l, r) (A, 0) n1", auto)
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   511
  then obtain stpa where d: "steps (1, l, r) (A |+| B, 0) stpa = (Suc (length A div 2), l', r')"
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   512
    using A_wf
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   513
    by(rule_tac t_merge_pre_halt_same, auto)
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   514
  from c aimpb have "P2 holds_for (0, l', r')"
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   515
    by(rule holds_for_imp)
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   516
  from this have "P2 (l', r')" by auto
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   517
  from this obtain n2 where e: "is_final (steps (1, l', r') (B, 0) n2)" and f: "Q2 holds_for (steps (1, l', r') (B, 0) n2)"
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   518
    using B_halt unfolding Hoare_def
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   519
    by auto
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   520
  then obtain l'' r'' where "steps (1, l', r') (B, 0) n2 = (0, l'', r'')" and g: "Q2 holds_for (0, l'', r'')"
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   521
    by(case_tac "steps (1, l', r') (B, 0) n2", auto)
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   522
  from this have "steps (Suc (length A div 2), l', r')  (A |+| B, 0) n2
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   523
    = (0, l'', r'')"
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   524
    apply(rule_tac t_merge_second_halt_same, auto simp: A_wf B_wf)
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   525
    done
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   526
  thus "\<exists>n. is_final (steps (1, l, r) (A |+| B, 0) n) \<and> Q2 holds_for (steps (1, l, r) (A |+| B, 0) n)"
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   527
    using d g
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   528
    apply(rule_tac x = "stpa + n2" in exI)
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   529
    apply(simp add: steps_add)
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   530
    done
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   531
qed
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   532
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   533
definition
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   534
  Hoare_unhalt :: "assert \<Rightarrow> tprog \<Rightarrow> bool" ("({(1_)}/ (_))" 50)
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   535
where
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   536
  "{P} p \<equiv> 
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   537
     (\<forall>l r. P (l, r) \<longrightarrow> (\<forall> n . \<not> (is_final (steps (1, (l, r)) p n))))"
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   538
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   539
lemma Hoare_unhalt_I:
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   540
  assumes "\<And>l r. P (l, r) \<Longrightarrow> \<forall> n. \<not> is_final (steps (1, (l, r)) p n)"
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   541
  shows "{P} p"
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   542
unfolding Hoare_unhalt_def using assms by auto
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   543
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   544
lemma Hoare_plus_unhalt: 
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   545
  assumes aimpb: "Q1 \<mapsto> P2"
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   546
  and A_wf : "tm_wf (A, 0)"
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   547
  and B_wf : "tm_wf (B, 0)"
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   548
  and A_halt : "{P1} (A, 0) {Q1}"
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   549
  and B_uhalt : "{P2} (B, 0)"
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   550
  shows "{P1} (A |+| B, 0)"
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   551
proof(rule_tac Hoare_unhalt_I)
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   552
  fix l r
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   553
  assume h: "P1 (l, r)"
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   554
  then obtain n1 where a: "is_final (steps (1, l, r) (A, 0) n1)" and b: "Q1 holds_for (steps (1, l, r) (A, 0) n1)"
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   555
    using A_halt unfolding Hoare_def by auto
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   556
  then obtain l' r' where "steps (1, l, r) (A, 0) n1 = (0, l', r')" and c: "Q1 holds_for (0, l', r')"
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   557
    by(case_tac "steps (1, l, r) (A, 0) n1", auto)
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   558
  then obtain stpa where d: "steps (1, l, r) (A |+| B, 0) stpa = (Suc (length A div 2), l', r')"
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   559
    using A_wf
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   560
    by(rule_tac t_merge_pre_halt_same, auto)
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   561
  from c aimpb have "P2 holds_for (0, l', r')"
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   562
    by(rule holds_for_imp)
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   563
  from this have "P2 (l', r')" by auto
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   564
  from this have e: "\<forall> n. \<not> is_final (steps (Suc 0, l', r') (B, 0) n)  "
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   565
    using B_uhalt unfolding Hoare_unhalt_def
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   566
    by auto
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   567
  from e show "\<forall>n. \<not> is_final (steps (1, l, r) (A |+| B, 0) n)"
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   568
  proof(rule_tac allI, case_tac "n > stpa")
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   569
    fix n
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   570
    assume h2: "stpa < n"
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   571
    hence "\<not> is_final (steps (Suc 0, l', r') (B, 0) (n - stpa))"
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   572
      using e
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   573
      apply(erule_tac x = "n - stpa" in allE) by simp
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   574
    then obtain s'' l'' r'' where f: "steps (Suc 0, l', r') (B, 0) (n - stpa) = (s'', l'', r'')" and g: "s'' \<noteq> 0"
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   575
      apply(case_tac "steps (Suc 0, l', r') (B, 0) (n - stpa)", auto)
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   576
      done
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   577
    have k: "steps (Suc (length A div 2), l', r') (A |+| B, 0) (n - stpa) = (s''+ length A div 2, l'', r'') "
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   578
      using A_wf B_wf f g
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   579
      apply(drule_tac t_merge_second_same, auto)
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   580
      done
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   581
    show "\<not> is_final (steps (1, l, r) (A |+| B, 0) n)"
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   582
    proof -
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   583
      have "\<not> is_final (steps (1, l, r) (A |+| B, 0) (stpa + (n  - stpa)))"
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   584
        using d k A_wf
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   585
        apply(simp only: steps_add d, simp add: tm_wf.simps)
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   586
        done
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   587
      thus "\<not> is_final (steps (1, l, r) (A |+| B, 0) n)"
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   588
        using h2 by simp
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   589
    qed
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   590
  next
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   591
    fix n
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   592
    assume h2: "\<not> stpa < n"
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   593
    with d show "\<not> is_final (steps (1, l, r) (A |+| B, 0) n)"
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   594
      apply(auto)
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   595
      apply(subgoal_tac "\<exists> d. stpa = n + d", auto)
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   596
      apply(case_tac "(steps (Suc 0, l, r) (A |+| B, 0) n)", simp)
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   597
      apply(rule_tac x = "stpa - n" in exI, simp)
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   598
      done
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   599
  qed
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   600
qed
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   601
    
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   602
      
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   603
       
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   604
end
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   605