thys/turing_basic.thy
author Christian Urban <christian dot urban at kcl dot ac dot uk>
Sat, 19 Jan 2013 15:27:21 +0000
changeset 56 0838b0ac52ab
parent 55 cd4ef33c8fb1
child 57 be5187b73ab9
permissions -rw-r--r--
some small changes to turing and uncomputable
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
54
e7d845acb0a7 changed slightly HOARE-def
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 53
diff changeset
    24
type_synonym tprog0 = "instr list"
e7d845acb0a7 changed slightly HOARE-def
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 53
diff changeset
    25
43
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
    26
type_synonym config = "state \<times> tape"
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
    27
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
    28
fun nth_of where
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
    29
  "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
    30
                  else Some (xs ! i))"
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
    31
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
    32
lemma nth_of_map [simp]:
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
    33
  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
    34
apply(induct p arbitrary: 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
apply(case_tac n)
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
    37
apply(auto)
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
    38
done
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
    39
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
    40
fun 
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
    41
  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
    42
where
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
    43
  "fetch p 0 b = (Nop, 0)"
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
    44
| "fetch p (Suc s) Bk = 
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
    45
     (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
    46
        Some i \<Rightarrow> i
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
    47
      | None \<Rightarrow> (Nop, 0))"
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
    48
|"fetch p (Suc s) Oc = 
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
    49
     (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
    50
         Some i \<Rightarrow> i
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
    51
       | None \<Rightarrow> (Nop, 0))"
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
    52
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
    53
lemma fetch_Nil [simp]:
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
    54
  shows "fetch [] s b = (Nop, 0)"
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
    55
apply(case_tac s)
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
apply(case_tac b)
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
    58
apply(auto)
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
    59
done
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
    60
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
    61
fun 
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
    62
  update :: "action \<Rightarrow> tape \<Rightarrow> tape"
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
    63
where 
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
    64
  "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
    65
| "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
    66
| "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
    67
| "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
    68
| "update Nop (l, r) = (l, r)"
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
    69
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
    70
abbreviation 
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
    71
  "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
    72
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
    73
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
    74
  where 
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
    75
  "step (s, l, r) (p, off) = 
50
816e84ca16d6 updated turing_basic by Jian
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 47
diff changeset
    76
     (let (a, s') = fetch p (s - off) (read r) in (s', update a (l, r)))"
43
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
    77
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
    78
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
    79
  where
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
    80
  "steps c p 0 = c" |
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
    81
  "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
    82
54
e7d845acb0a7 changed slightly HOARE-def
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 53
diff changeset
    83
e7d845acb0a7 changed slightly HOARE-def
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 53
diff changeset
    84
abbreviation
e7d845acb0a7 changed slightly HOARE-def
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 53
diff changeset
    85
  "step0 c p \<equiv> step c (p, 0)"
e7d845acb0a7 changed slightly HOARE-def
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 53
diff changeset
    86
e7d845acb0a7 changed slightly HOARE-def
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 53
diff changeset
    87
abbreviation
e7d845acb0a7 changed slightly HOARE-def
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 53
diff changeset
    88
  "steps0 c p n \<equiv> steps c (p, 0) n"
e7d845acb0a7 changed slightly HOARE-def
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 53
diff changeset
    89
43
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
    90
lemma step_red [simp]: 
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
    91
  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
    92
by (induct n arbitrary: c) (auto)
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
    93
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
    94
lemma steps_add [simp]: 
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
    95
  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
    96
by (induct m arbitrary: c) (auto)
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
    97
56
0838b0ac52ab some small changes to turing and uncomputable
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 55
diff changeset
    98
lemma step_0 [simp]: 
0838b0ac52ab some small changes to turing and uncomputable
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 55
diff changeset
    99
  shows "step (0, (l, r)) p = (0, (l, r))"
0838b0ac52ab some small changes to turing and uncomputable
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 55
diff changeset
   100
by (case_tac p, simp)
0838b0ac52ab some small changes to turing and uncomputable
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 55
diff changeset
   101
0838b0ac52ab some small changes to turing and uncomputable
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 55
diff changeset
   102
lemma steps_0 [simp]: 
0838b0ac52ab some small changes to turing and uncomputable
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 55
diff changeset
   103
  shows "steps (0, (l, r)) p n = (0, (l, r))"
0838b0ac52ab some small changes to turing and uncomputable
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 55
diff changeset
   104
by (induct n) (simp_all)
0838b0ac52ab some small changes to turing and uncomputable
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 55
diff changeset
   105
0838b0ac52ab some small changes to turing and uncomputable
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 55
diff changeset
   106
0838b0ac52ab some small changes to turing and uncomputable
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 55
diff changeset
   107
0838b0ac52ab some small changes to turing and uncomputable
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 55
diff changeset
   108
fun
0838b0ac52ab some small changes to turing and uncomputable
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 55
diff changeset
   109
  is_final :: "config \<Rightarrow> bool"
0838b0ac52ab some small changes to turing and uncomputable
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 55
diff changeset
   110
where
0838b0ac52ab some small changes to turing and uncomputable
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 55
diff changeset
   111
  "is_final (s, l, r) = (s = 0)"
0838b0ac52ab some small changes to turing and uncomputable
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 55
diff changeset
   112
0838b0ac52ab some small changes to turing and uncomputable
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 55
diff changeset
   113
lemma is_final_eq: 
0838b0ac52ab some small changes to turing and uncomputable
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 55
diff changeset
   114
  shows "is_final (s, tp) = (s = 0)"
0838b0ac52ab some small changes to turing and uncomputable
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 55
diff changeset
   115
by (case_tac tp) (auto)
0838b0ac52ab some small changes to turing and uncomputable
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 55
diff changeset
   116
0838b0ac52ab some small changes to turing and uncomputable
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 55
diff changeset
   117
lemma is_final_steps:
0838b0ac52ab some small changes to turing and uncomputable
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 55
diff changeset
   118
  assumes "is_final (s, l, r)"
0838b0ac52ab some small changes to turing and uncomputable
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 55
diff changeset
   119
  shows "is_final (steps (s, l, r) (p, off) n)"
0838b0ac52ab some small changes to turing and uncomputable
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 55
diff changeset
   120
using assms 
0838b0ac52ab some small changes to turing and uncomputable
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 55
diff changeset
   121
by (induct n) (auto)
0838b0ac52ab some small changes to turing and uncomputable
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 55
diff changeset
   122
0838b0ac52ab some small changes to turing and uncomputable
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 55
diff changeset
   123
0838b0ac52ab some small changes to turing and uncomputable
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 55
diff changeset
   124
(* if the machine is in the halting state, there must have 
0838b0ac52ab some small changes to turing and uncomputable
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 55
diff changeset
   125
   been a state just before the halting state *)
0838b0ac52ab some small changes to turing and uncomputable
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 55
diff changeset
   126
lemma before_final: 
0838b0ac52ab some small changes to turing and uncomputable
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 55
diff changeset
   127
  assumes "steps0 (1, tp) A n = (0, tp')"
0838b0ac52ab some small changes to turing and uncomputable
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 55
diff changeset
   128
  shows "\<exists> n'. \<not> is_final (steps0 (1, tp) A n') \<and> steps0 (1, tp) A (Suc n') = (0, tp')"
0838b0ac52ab some small changes to turing and uncomputable
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 55
diff changeset
   129
using assms
0838b0ac52ab some small changes to turing and uncomputable
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 55
diff changeset
   130
proof(induct n arbitrary: tp')
0838b0ac52ab some small changes to turing and uncomputable
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 55
diff changeset
   131
  case (0 tp')
0838b0ac52ab some small changes to turing and uncomputable
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 55
diff changeset
   132
  have asm: "steps0 (1, tp) A 0 = (0, tp')" by fact
0838b0ac52ab some small changes to turing and uncomputable
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 55
diff changeset
   133
  then show "\<exists>n'. \<not> is_final (steps0 (1, tp) A n') \<and> steps0 (1, tp) A (Suc n') = (0, tp')"
0838b0ac52ab some small changes to turing and uncomputable
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 55
diff changeset
   134
    by simp
0838b0ac52ab some small changes to turing and uncomputable
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 55
diff changeset
   135
next
0838b0ac52ab some small changes to turing and uncomputable
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 55
diff changeset
   136
  case (Suc n tp')
0838b0ac52ab some small changes to turing and uncomputable
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 55
diff changeset
   137
  have ih: "\<And>tp'. steps0 (1, tp) A n = (0, tp') \<Longrightarrow>
0838b0ac52ab some small changes to turing and uncomputable
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 55
diff changeset
   138
    \<exists>n'. \<not> is_final (steps0 (1, tp) A n') \<and> steps0 (1, tp) A (Suc n') = (0, tp')" by fact
0838b0ac52ab some small changes to turing and uncomputable
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 55
diff changeset
   139
  have asm: "steps0 (1, tp) A (Suc n) = (0, tp')" by fact
0838b0ac52ab some small changes to turing and uncomputable
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 55
diff changeset
   140
  obtain s l r where cases: "steps0 (1, tp) A n = (s, l, r)"
0838b0ac52ab some small changes to turing and uncomputable
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 55
diff changeset
   141
    by (auto intro: is_final.cases)
0838b0ac52ab some small changes to turing and uncomputable
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 55
diff changeset
   142
  then show "\<exists>n'. \<not> is_final (steps0 (1, tp) A n') \<and> steps0 (1, tp) A (Suc n') = (0, tp')"
0838b0ac52ab some small changes to turing and uncomputable
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 55
diff changeset
   143
  proof (cases "s = 0")
0838b0ac52ab some small changes to turing and uncomputable
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 55
diff changeset
   144
    case True (* in halting state *)
0838b0ac52ab some small changes to turing and uncomputable
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 55
diff changeset
   145
    then have "steps0 (1, tp) A n = (0, tp')"
0838b0ac52ab some small changes to turing and uncomputable
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 55
diff changeset
   146
      using asm cases by (simp del: steps.simps)
0838b0ac52ab some small changes to turing and uncomputable
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 55
diff changeset
   147
    then show ?thesis using ih by simp
0838b0ac52ab some small changes to turing and uncomputable
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 55
diff changeset
   148
  next
0838b0ac52ab some small changes to turing and uncomputable
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 55
diff changeset
   149
    case False (* not in halting state *)
0838b0ac52ab some small changes to turing and uncomputable
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 55
diff changeset
   150
    then have "\<not> is_final (steps0 (1, tp) A n) \<and> steps0 (1, tp) A (Suc n) = (0, tp')"
0838b0ac52ab some small changes to turing and uncomputable
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 55
diff changeset
   151
      using asm cases by simp
0838b0ac52ab some small changes to turing and uncomputable
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 55
diff changeset
   152
    then show ?thesis by auto
0838b0ac52ab some small changes to turing and uncomputable
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 55
diff changeset
   153
  qed
0838b0ac52ab some small changes to turing and uncomputable
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 55
diff changeset
   154
qed
0838b0ac52ab some small changes to turing and uncomputable
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 55
diff changeset
   155
0838b0ac52ab some small changes to turing and uncomputable
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 55
diff changeset
   156
(* well-formedness of Turing machine programs *)
43
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   157
fun 
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   158
  tm_wf :: "tprog \<Rightarrow> bool"
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   159
where
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   160
  "tm_wf (p, off) = (length p \<ge> 2 \<and> length p mod 2 = 0 \<and> 
54
e7d845acb0a7 changed slightly HOARE-def
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 53
diff changeset
   161
                    (\<forall>(a, s) \<in> set p. s \<le> length p div 2 + off \<and> s \<ge> off))"
43
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   162
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   163
lemma halt_lemma: 
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   164
  "\<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
   165
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
   166
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   167
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
   168
  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
   169
47
251e192339b7 added abacus
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 43
diff changeset
   170
consts tape_of :: "'a \<Rightarrow> cell list" ("<_>" 100)
251e192339b7 added abacus
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 43
diff changeset
   171
251e192339b7 added abacus
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 43
diff changeset
   172
fun tape_of_nat_list :: "nat list \<Rightarrow> cell list" 
251e192339b7 added abacus
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 43
diff changeset
   173
  where 
251e192339b7 added abacus
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 43
diff changeset
   174
  "tape_of_nat_list [] = []" |
251e192339b7 added abacus
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 43
diff changeset
   175
  "tape_of_nat_list [n] = Oc\<up>(Suc n)" |
251e192339b7 added abacus
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 43
diff changeset
   176
  "tape_of_nat_list (n#ns) = Oc\<up>(Suc n) @ Bk # (tape_of_nat_list ns)"
251e192339b7 added abacus
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 43
diff changeset
   177
251e192339b7 added abacus
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 43
diff changeset
   178
defs (overloaded)
251e192339b7 added abacus
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 43
diff changeset
   179
  tape_of_nl_abv: "<am> \<equiv> tape_of_nat_list am"
251e192339b7 added abacus
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 43
diff changeset
   180
  tape_of_nat_abv : "<(n::nat)> \<equiv> Oc\<up>(Suc n)"
251e192339b7 added abacus
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 43
diff changeset
   181
43
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   182
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
   183
  where
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   184
  "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
   185
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   186
fun 
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   187
  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
   188
where
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   189
  "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
   190
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   191
fun 
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   192
  adjust :: "instr list \<Rightarrow> instr list"
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   193
where
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   194
  "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
   195
54
e7d845acb0a7 changed slightly HOARE-def
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 53
diff changeset
   196
lemma length_shift [simp]: 
56
0838b0ac52ab some small changes to turing and uncomputable
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 55
diff changeset
   197
  shows "length (shift p n) = length p"
54
e7d845acb0a7 changed slightly HOARE-def
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 53
diff changeset
   198
by simp
e7d845acb0a7 changed slightly HOARE-def
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 53
diff changeset
   199
56
0838b0ac52ab some small changes to turing and uncomputable
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 55
diff changeset
   200
lemma length_adjust [simp]: 
43
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   201
  shows "length (adjust p) = length p"
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   202
by (induct p) (auto)
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   203
56
0838b0ac52ab some small changes to turing and uncomputable
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 55
diff changeset
   204
0838b0ac52ab some small changes to turing and uncomputable
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 55
diff changeset
   205
(* composition of two Turing machines *)
43
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   206
fun
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   207
  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
   208
where
54
e7d845acb0a7 changed slightly HOARE-def
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 53
diff changeset
   209
  "tm_comp p1 p2 = ((adjust p1) @ (shift p2 (length p1 div 2)))"
43
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   210
56
0838b0ac52ab some small changes to turing and uncomputable
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 55
diff changeset
   211
lemma tm_comp_length:
54
e7d845acb0a7 changed slightly HOARE-def
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 53
diff changeset
   212
  shows "length (A |+| B) = length A + length B"
e7d845acb0a7 changed slightly HOARE-def
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 53
diff changeset
   213
by auto
e7d845acb0a7 changed slightly HOARE-def
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 53
diff changeset
   214
56
0838b0ac52ab some small changes to turing and uncomputable
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 55
diff changeset
   215
lemma tm_comp_fetch_in_first:
43
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   216
  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
   217
  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
   218
using assms
56
0838b0ac52ab some small changes to turing and uncomputable
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 55
diff changeset
   219
apply(case_tac a)
0838b0ac52ab some small changes to turing and uncomputable
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 55
diff changeset
   220
apply(case_tac [!] x) 
0838b0ac52ab some small changes to turing and uncomputable
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 55
diff changeset
   221
apply(auto simp: tm_comp_length nth_append)
43
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   222
done
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   223
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   224
lemma t_merge_pre_eq_step: 
56
0838b0ac52ab some small changes to turing and uncomputable
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 55
diff changeset
   225
  assumes step: "step0 (s, l, r) A = cf"
43
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   226
  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
   227
  and     unfinal: "\<not> is_final cf"
56
0838b0ac52ab some small changes to turing and uncomputable
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 55
diff changeset
   228
  shows "step0 (s, l, r) (A |+| B) = cf"
43
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   229
proof -
56
0838b0ac52ab some small changes to turing and uncomputable
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 55
diff changeset
   230
  from step unfinal
0838b0ac52ab some small changes to turing and uncomputable
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 55
diff changeset
   231
  have "\<not> is_final (step0 (s, l, r) A)" by simp
0838b0ac52ab some small changes to turing and uncomputable
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 55
diff changeset
   232
  then  have "fetch (A |+| B) s (read r) = fetch A s (read r)"
0838b0ac52ab some small changes to turing and uncomputable
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 55
diff changeset
   233
    by (rule_tac tm_comp_fetch_in_first) (auto simp add: is_final_eq)
0838b0ac52ab some small changes to turing and uncomputable
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 55
diff changeset
   234
  then show ?thesis
0838b0ac52ab some small changes to turing and uncomputable
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 55
diff changeset
   235
    using step by auto
43
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   236
qed
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   237
56
0838b0ac52ab some small changes to turing and uncomputable
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 55
diff changeset
   238
declare steps.simps[simp del]
0838b0ac52ab some small changes to turing and uncomputable
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 55
diff changeset
   239
declare tm_comp.simps [simp del] adjust.simps[simp del] shift.simps[simp del]
43
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   240
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
   241
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   242
lemma t_merge_pre_eq:  
56
0838b0ac52ab some small changes to turing and uncomputable
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 55
diff changeset
   243
  "\<lbrakk>steps0 (Suc 0, tp) A stp = cf; \<not> is_final cf; tm_wf (A, 0)\<rbrakk>
0838b0ac52ab some small changes to turing and uncomputable
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 55
diff changeset
   244
  \<Longrightarrow> steps0 (Suc 0, tp) (A |+| B) stp = cf"
43
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   245
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
   246
  fix stp cf
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   247
  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
   248
    \<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
   249
  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
   250
      "\<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
   251
  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
   252
  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
   253
    fix a b c
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   254
    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
   255
      "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
   256
    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
   257
    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
   258
      show "0 < a"
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   259
        using g h
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   260
        apply(simp add: step_red)
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   261
        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
   262
        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
   263
        done
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   264
    qed
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   265
    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
   266
      apply(simp)
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   267
      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
   268
      done
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   269
  qed
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   270
qed
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   271
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   272
lemma tmcomp_fetch_in_first2:
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   273
  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
   274
          "tm_wf (A, 0)"
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   275
          "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
   276
  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
   277
using assms
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   278
apply(case_tac a, case_tac [!] x, 
56
0838b0ac52ab some small changes to turing and uncomputable
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 55
diff changeset
   279
auto simp: tm_comp_length tm_comp.simps length_adjust nth_append)
43
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   280
apply(simp_all add: adjust.simps)
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   281
done
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   282
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   283
lemma tmcomp_exec_after_first:
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   284
  "\<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
   285
       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
   286
       \<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
   287
apply(simp add: step.simps, auto)
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 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
   289
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
   290
done
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   291
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   292
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
   293
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
   294
done
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   295
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   296
lemma nth_in_set: 
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   297
  "\<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
   298
by auto
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   299
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   300
lemma step_nothalt: 
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   301
  "\<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
   302
  a \<le> length A div 2"
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   303
apply(simp add: step.simps)
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   304
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
   305
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
   306
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
   307
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
   308
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
   309
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
   310
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
   311
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
   312
done
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   313
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   314
lemma steps_in_range: 
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   315
  " \<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
   316
  \<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
   317
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
   318
  fix a b c
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   319
  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
   320
            "tm_wf (A, 0)"
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   321
  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
   322
    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
   323
    done
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   324
next
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   325
  fix stp a b c
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   326
  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
   327
    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
   328
  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
   329
  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
   330
  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
   331
    fix aa ba ca
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   332
    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
   333
           "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
   334
    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
   335
      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
   336
      done
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   337
    thus "?thesis"
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   338
      using g h
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   339
      apply(rule_tac step_nothalt, auto)
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   340
      done
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   341
  qed
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   342
qed
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   343
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   344
lemma t_merge_pre_halt_same: 
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   345
  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
   346
  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
   347
  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
   348
proof -
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   349
  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
   350
  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
   351
                          "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
   352
  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
   353
  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
   354
  proof(simp add: step_red)
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   355
    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
   356
           " 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
   357
    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
   358
      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
   359
      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
   360
      done
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   361
    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
   362
      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
   363
      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
   364
      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
   365
      done
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   366
  qed
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   367
  with a show thesis by blast
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   368
qed
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   369
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   370
lemma tm_comp_fetch_second_zero:
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   371
  "\<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
   372
     \<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
   373
apply(case_tac x)
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   374
apply(case_tac [!] sa',
56
0838b0ac52ab some small changes to turing and uncomputable
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 55
diff changeset
   375
  auto simp: fetch.simps tm_comp_length length_adjust nth_append tm_comp.simps
43
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   376
             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
   377
done 
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   378
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   379
lemma tm_comp_fetch_second_inst:
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   380
  "\<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
   381
     \<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
   382
apply(case_tac x)
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   383
apply(case_tac [!] sa,
56
0838b0ac52ab some small changes to turing and uncomputable
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 55
diff changeset
   384
  auto simp: fetch.simps tm_comp_length length_adjust nth_append tm_comp.simps
43
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   385
             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
   386
done 
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   387
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   388
lemma t_merge_second_same:
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   389
  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
   390
  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
   391
  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
   392
  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
   393
       = (if s = 0 then 0
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   394
          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
   395
using a_wf b_wf steps
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   396
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
   397
  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
   398
  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
   399
    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
   400
                (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
   401
  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
   402
  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
   403
    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
   404
    done
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   405
  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
   406
                (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
   407
    apply(erule_tac ind)
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   408
    done
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   409
  from a b h show 
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   410
    "(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
   411
    (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
   412
  proof(case_tac "sa' = 0", auto)
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   413
    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
   414
    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
   415
      using a_wf b_wf
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   416
      apply(simp add:  step.simps)
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   417
      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
   418
      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
   419
      done
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   420
  next
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   421
    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
   422
    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
   423
      using a_wf b_wf
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   424
      apply(simp add: step.simps)
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   425
      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
   426
      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
   427
      done
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   428
  qed
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   429
qed
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   430
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   431
lemma t_merge_second_halt_same:
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   432
  "\<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
   433
   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
   434
     \<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
   435
       = (0, l', r')"
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   436
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
   437
apply(auto)
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   438
done
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   439
47
251e192339b7 added abacus
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 43
diff changeset
   440
        
43
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   441
end
a8785fa80278 updated literature
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 41
diff changeset
   442