thys/turing_basic.thy
author Christian Urban <christian dot urban at kcl dot ac dot uk>
Sun, 13 Jan 2013 11:29:33 +0000
changeset 40 a37a21f7ccf4
parent 39 a95987e9c7e9
child 41 6d89ed67ba27
permissions -rw-r--r--
updated test
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
39
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     1
(* Title: Turing machines
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     2
   Author: Xu Jian <xujian817@hotmail.com>
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     3
   Maintainer: Xu Jian
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     4
*)
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     5
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     6
theory turing_basic
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     7
imports Main
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     8
begin
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     9
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    10
section {* Basic definitions of Turing machine *}
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    11
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    12
definition 
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    13
  "iseven n \<equiv> \<exists>x. n = 2 * x"
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    14
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    15
datatype action = W0 | W1 | L | R | Nop
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    16
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    17
datatype cell = Bk | Oc
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    18
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    19
type_synonym tape = "cell list \<times> cell list"
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    20
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    21
type_synonym state = nat
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    22
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    23
type_synonym instr = "action \<times> state"
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    24
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    25
type_synonym tprog = "instr list"
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    26
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    27
type_synonym config = "state \<times> tape"
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    28
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    29
fun nth_of where
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    30
  "nth_of [] _ = None"
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    31
| "nth_of (x # xs) 0 = Some x"
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    32
| "nth_of (x # xs) (Suc n) = nth_of xs n" 
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    33
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    34
fun 
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    35
  fetch :: "tprog \<Rightarrow> state \<Rightarrow> cell \<Rightarrow> instr"
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    36
where
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    37
  "fetch p 0 b = (Nop, 0)"
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    38
| "fetch p (Suc s) Bk = 
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    39
     (case nth_of p (2 * s) of
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    40
        Some i \<Rightarrow> i
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    41
      | None \<Rightarrow> (Nop, 0))"
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    42
|"fetch p (Suc s) Oc = 
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    43
     (case nth_of p ((2 * s) + 1) of
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    44
         Some i \<Rightarrow> i
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    45
       | None \<Rightarrow> (Nop, 0))"
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    46
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    47
fun 
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    48
  update :: "action \<Rightarrow> tape \<Rightarrow> tape"
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    49
where 
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    50
  "update W0 (l, r) = (l, Bk # (tl r))" 
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    51
| "update W1 (l, r) = (l, Oc # (tl r))"
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    52
| "update L (l, r) = (if l = [] then ([], Bk # r) else (tl l, (hd l) # r))" 
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    53
| "update R (l, r) = (if r = [] then (Bk # l, []) else ((hd r) # l, tl r))" 
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    54
| "update Nop (l, r) = (l, r)"
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    55
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    56
abbreviation 
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    57
  "read r == if (r = []) then Bk else hd r"
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    58
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    59
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    60
fun 
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    61
  step :: "config \<Rightarrow> tprog \<Rightarrow> config"
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    62
where
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    63
  "step (s, l, r) p = (let (a, s') = fetch p s (read r) in (s', update a (l, r)))"
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    64
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    65
fun steps :: "config \<Rightarrow> tprog \<Rightarrow> nat \<Rightarrow> config"
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    66
  where
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    67
  "steps c p 0 = c" |
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    68
  "steps c p (Suc n) = steps (step c p) p n"
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    69
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    70
lemma step_red [simp]: 
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    71
  shows "steps c p (Suc n) = step (steps c p n) p"
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    72
by (induct n arbitrary: c) (auto)
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    73
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    74
lemma steps_add [simp]: 
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    75
  shows "steps c p (m + n) = steps (steps c p m) p n"
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    76
by (induct m arbitrary: c) (auto)
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    77
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    78
definition 
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    79
  tm_wf :: "tprog \<Rightarrow> bool"
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    80
where
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    81
  "tm_wf p = (length p \<ge> 2 \<and> iseven (length p) \<and> (\<forall>(a, s) \<in> set p. s \<le> length p div 2))"
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    82
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    83
(* FIXME: needed? *)
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    84
lemma halt_lemma: 
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    85
  "\<lbrakk>wf LE; \<forall>n. (\<not> P (f n) \<longrightarrow> (f (Suc n), (f n)) \<in> LE)\<rbrakk> \<Longrightarrow> \<exists>n. P (f n)"
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    86
by (metis wf_iff_no_infinite_down_chain)
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    87
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    88
abbreviation exponent :: "'a \<Rightarrow> nat \<Rightarrow> 'a list" ("_ \<up> _" [100, 99] 100)
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    89
  where "x \<up> n == replicate n x"
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    90
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    91
definition tinres :: "cell list \<Rightarrow> cell list \<Rightarrow> bool"
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    92
  where
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    93
  "tinres xs ys = (\<exists>n. xs = ys @ Bk \<up> n \<or> ys = xs @ Bk \<up> n)"
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    94
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    95
fun 
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    96
  shift :: "tprog \<Rightarrow> nat \<Rightarrow> tprog"
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    97
where
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    98
  "shift p n = (map (\<lambda> (a, s). (a, (if s = 0 then 0 else s + n))) p)"
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    99
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   100
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   101
lemma [simp]: 
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   102
  "length (shift p n) = length p"
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   103
by (simp)
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   104
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   105
fun 
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   106
  adjust :: "tprog \<Rightarrow> tprog"
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   107
where
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   108
  "adjust p = (map (\<lambda> (a, s). (a, if s = 0 then ((length p) div 2) + 1 else s)) p)"
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   109
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   110
lemma [simp]: 
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   111
  shows "length (adjust p) = length p"
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   112
by (simp)
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   113
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   114
definition
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   115
  tm_comp :: "tprog \<Rightarrow> tprog \<Rightarrow> tprog" ("_ |+| _" [0, 0] 100)
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   116
where
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   117
  "tm_comp p1 p2 = ((adjust p1) @ (shift p2 ((length p1) div 2)))"
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   118
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   119
fun
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   120
  is_final :: "config \<Rightarrow> bool"
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   121
where
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   122
  "is_final (s, l, r) = (s = 0)"
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   123
40
a37a21f7ccf4 updated test
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 39
diff changeset
   124
lemma is_final_steps:
a37a21f7ccf4 updated test
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 39
diff changeset
   125
  assumes "is_final (s, l, r)"
a37a21f7ccf4 updated test
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 39
diff changeset
   126
  shows "is_final (steps (s, l, r) p n)"
a37a21f7ccf4 updated test
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 39
diff changeset
   127
using assms by (induct n) (auto)
a37a21f7ccf4 updated test
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 39
diff changeset
   128
39
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   129
fun 
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   130
  holds_for :: "(tape \<Rightarrow> bool) \<Rightarrow> config \<Rightarrow> bool" ("_ holds'_for _" [100, 99] 100)
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   131
where
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   132
  "P holds_for (s, l, r) = P (l, r)"  
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   133
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   134
lemma step_0 [simp]: 
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   135
  shows "step (0, (l, r)) p = (0, (l, r))"
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   136
by simp
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   137
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   138
lemma steps_0 [simp]: 
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   139
  shows "steps (0, (l, r)) p n = (0, (l, r))"
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   140
by (induct n) (simp_all)
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   141
40
a37a21f7ccf4 updated test
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 39
diff changeset
   142
lemma is_final_holds[simp]:
a37a21f7ccf4 updated test
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 39
diff changeset
   143
  assumes "is_final c"
a37a21f7ccf4 updated test
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 39
diff changeset
   144
  shows "Q holds_for (steps c p n) = Q holds_for c"
a37a21f7ccf4 updated test
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 39
diff changeset
   145
using assms 
a37a21f7ccf4 updated test
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 39
diff changeset
   146
apply(induct n)
a37a21f7ccf4 updated test
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 39
diff changeset
   147
apply(case_tac [!] c)
a37a21f7ccf4 updated test
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 39
diff changeset
   148
apply(auto)
a37a21f7ccf4 updated test
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 39
diff changeset
   149
done
a37a21f7ccf4 updated test
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 39
diff changeset
   150
39
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   151
type_synonym assert = "tape \<Rightarrow> bool"
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   152
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   153
definition assert_imp :: "assert \<Rightarrow> assert \<Rightarrow> bool" ("_ \<mapsto> _" [0, 0] 100)
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   154
  where
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   155
  "assert_imp P Q = (\<forall>l r. P (l, r) \<longrightarrow> Q (l, r))"
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   156
40
a37a21f7ccf4 updated test
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 39
diff changeset
   157
lemma test:
a37a21f7ccf4 updated test
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 39
diff changeset
   158
  assumes "is_final (steps (1, (l, r)) p n1)"
a37a21f7ccf4 updated test
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 39
diff changeset
   159
  and     "is_final (steps (1, (l, r)) p n2)"
a37a21f7ccf4 updated test
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 39
diff changeset
   160
  shows "Q holds_for (steps (1, (l, r)) p n1) \<longleftrightarrow> Q holds_for (steps (1, (l, r)) p n2)"
a37a21f7ccf4 updated test
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 39
diff changeset
   161
proof -
a37a21f7ccf4 updated test
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 39
diff changeset
   162
  obtain n3 where "n1 = n2 + n3 \<or> n2 = n1 + n3"
a37a21f7ccf4 updated test
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 39
diff changeset
   163
    by (metis le_iff_add nat_le_linear)
a37a21f7ccf4 updated test
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 39
diff changeset
   164
  with assms show "Q holds_for (steps (1, (l, r)) p n1) \<longleftrightarrow> Q holds_for (steps (1, (l, r)) p n2)"  
a37a21f7ccf4 updated test
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 39
diff changeset
   165
    by auto
a37a21f7ccf4 updated test
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 39
diff changeset
   166
qed
a37a21f7ccf4 updated test
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 39
diff changeset
   167
39
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   168
definition
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   169
  Hoare :: "assert \<Rightarrow> tprog \<Rightarrow> assert \<Rightarrow> bool" ("({(1_)}/ (_)/ {(1_)})" 50)
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   170
where
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   171
  "{P} p {Q} \<equiv> 
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   172
     (\<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)))"
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   173
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   174
lemma HoareI:
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   175
  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)"
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   176
  shows "{P} p {Q}"
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   177
unfolding Hoare_def using assms by auto
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   178
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   179
text {*
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   180
{P1} A {Q1}   {P2} B {Q2}  Q1 \<mapsto> P2
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   181
-----------------------------------
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   182
    {P1} A |+| B {Q2}
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   183
*}
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   184
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   185
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   186
lemma Hoare_plus: 
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   187
  assumes aimpb: "Q1 \<mapsto> P2"
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   188
  and A_wf : "tm_wf A"
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   189
  and B_wf : "tm_wf B"
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   190
  and A_halt : "{P1} A {Q1}"
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   191
  and B_halt : "{P2} B {Q2}"
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   192
  shows "{P1} A |+| B {Q2}"
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   193
proof(rule HoareI)
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   194
  fix a b
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   195
  assume h: "P1 (a, b)"
40
a37a21f7ccf4 updated test
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 39
diff changeset
   196
  then have "\<exists>n. is_final (steps (1, a, b) A n) \<and> Q1 holds_for (steps (1, a, b) A n)"
a37a21f7ccf4 updated test
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 39
diff changeset
   197
    using A_halt unfolding Hoare_def by simp
a37a21f7ccf4 updated test
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 39
diff changeset
   198
  then obtain n1 where "is_final (steps (1, a, b) A n1) \<and> Q1 holds_for (steps (1, a, b) A stp1)" ..
a37a21f7ccf4 updated test
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 39
diff changeset
   199
  then show "\<exists>n. is_final (steps (1, a, b) (A |+| B) n) \<and> Q2 holds_for (steps (1, a, b) (A |+| B) n)"
39
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   200
  proof(case_tac "steps (Suc 0, a, b) A stp1", simp, erule_tac conjE)
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   201
    fix aa ba c
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   202
    assume g1: "Q1 (ba, c)" 
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   203
      and g2: "steps (Suc 0, a, b) A stp1 = (0, ba, c)"
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   204
    hence "P2 (ba, c)"
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   205
      using aimpb apply(simp add: assert_imp_def)
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   206
      done
40
a37a21f7ccf4 updated test
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 39
diff changeset
   207
    hence "\<exists> stp. is_final (steps (Suc 0, ba, c) B stp) \<and> Q2 holds_for (steps (Suc 0, ba, c) B stp)"
a37a21f7ccf4 updated test
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 39
diff changeset
   208
      using B_halt unfolding Hoare_def by simp
a37a21f7ccf4 updated test
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 39
diff changeset
   209
    from this obtain stp2 where 
a37a21f7ccf4 updated test
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 39
diff changeset
   210
      "is_final (steps (Suc 0, ba, c) B stp2) \<and> Q2 holds_for (steps (Suc 0, ba, c) B stp2)" ..
39
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   211
    thus "?thesis"
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   212
    proof(case_tac "steps (Suc 0, ba, c) B stp2", simp, erule_tac conjE)
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   213
      fix aa bb ca
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   214
      assume g3: " Q2 (bb, ca)" "steps (Suc 0, ba, c) B stp2 = (0, bb, ca)"
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   215
      have "\<exists> stp. steps (Suc 0, a, b) (A |+| B) stp = (Suc (length A div 2), ba , c)"
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   216
        using g2 A_wf B_wf
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   217
        sorry
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   218
      moreover have "\<exists> stp. steps (Suc (length A div 2), ba, c) (A |+| B) stp = (0, bb, ca)"
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   219
        using g3 A_wf B_wf
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   220
        sorry
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   221
      ultimately show "\<exists>n. case steps (Suc 0, a, b) (A |+| B) n of (s, tp') \<Rightarrow> s = 0 \<and> Q2 tp'"
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   222
        apply(erule_tac exE, erule_tac exE)
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   223
        apply(rule_tac x = "stp + stpa" in exI, simp)
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   224
        using g3 by simp
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   225
    qed
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   226
  qed
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   227
qed
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   228
40
a37a21f7ccf4 updated test
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 39
diff changeset
   229
lemma Hoare_plus2: 
39
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   230
  assumes A_wf : "tm_wf A"
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   231
  and B_wf : "tm_wf B"
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   232
  and A_halt : "{P1} A {Q1}"
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   233
  and B_halt : "{P2} B {Q2}"
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   234
  and aimpb: "Q1 \<mapsto> P2"
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   235
  shows "{P1} A |+| B {Q2}"
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   236
unfolding hoare_def
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   237
proof(auto split: )
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   238
  fix a b
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   239
  assume h: "P1 (a, b)"
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   240
  hence "\<exists>n. let (s, tp') = steps (Suc 0, a, b) A n in s = 0 \<and> Q1 tp'"
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   241
    using A_halt unfolding hoare_def by simp
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   242
  from this obtain stp1 where "let (s, tp') = steps (Suc 0, a, b) A stp1 in s = 0 \<and> Q1 tp'" ..
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   243
  then show "\<exists>n. case steps (Suc 0, a, b) (A |+| B) n of (s, tp') \<Rightarrow> s = 0 \<and> Q2 tp'"
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   244
  proof(case_tac "steps (Suc 0, a, b) A stp1", simp, erule_tac conjE)
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   245
    fix aa ba c
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   246
    assume g1: "Q1 (ba, c)" 
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   247
      and g2: "steps (Suc 0, a, b) A stp1 = (0, ba, c)"
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   248
    hence "P2 (ba, c)"
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   249
      using aimpb apply(simp add: assert_imp_def)
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   250
      done
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   251
    hence "\<exists> stp. let (s, tp') = steps (Suc 0, ba, c) B stp in s = 0 \<and> Q2 tp'"
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   252
      using B_halt unfolding hoare_def by simp
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   253
    from this obtain stp2 where "let (s, tp') = steps (Suc 0, ba, c) B stp2 in s = 0 \<and> Q2 tp'" ..
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   254
    thus "?thesis"
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   255
    proof(case_tac "steps (Suc 0, ba, c) B stp2", simp, erule_tac conjE)
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   256
      fix aa bb ca
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   257
      assume g3: " Q2 (bb, ca)" "steps (Suc 0, ba, c) B stp2 = (0, bb, ca)"
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   258
      have "\<exists> stp. steps (Suc 0, a, b) (A |+| B) stp = (Suc (length A div 2), ba , c)"
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   259
        using g2 A_wf B_wf
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   260
        sorry
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   261
      moreover have "\<exists> stp. steps (Suc (length A div 2), ba, c) (A |+| B) stp = (0, bb, ca)"
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   262
        using g3 A_wf B_wf
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   263
        sorry
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   264
      ultimately show "\<exists>n. case steps (Suc 0, a, b) (A |+| B) n of (s, tp') \<Rightarrow> s = 0 \<and> Q2 tp'"
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   265
        apply(erule_tac exE, erule_tac exE)
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   266
        apply(rule_tac x = "stp + stpa" in exI, simp)
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   267
        using g3 by simp
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   268
    qed
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   269
  qed
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   270
qed
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   271
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   272
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   273
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   274
locale turing_merge =
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   275
  fixes A :: "tprog" and B :: "tprog" and P1 :: "assert"
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   276
  and P2 :: "assert"
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   277
  and P3 :: "assert"
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   278
  and P4 :: "assert"
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   279
  and Q1:: "assert"
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   280
  and Q2 :: "assert"
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   281
  assumes 
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   282
  A_wf : "tm_wf A"
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   283
  and B_wf : "tm_wf B"
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   284
  and A_halt : "P1 tp \<Longrightarrow> \<exists> stp. let (s, tp') = steps (Suc 0, tp) A stp in s = 0 \<and> Q1 tp'"
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   285
  and B_halt : "P2 tp \<Longrightarrow> \<exists> stp. let (s, tp') = steps (Suc 0, tp) B stp in s = 0 \<and> Q2 tp'"
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   286
  and A_uhalt : "P3 tp \<Longrightarrow> \<not> (\<exists> stp. is_final (steps (Suc 0, tp) A stp))"
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   287
  and B_uhalt: "P4 tp \<Longrightarrow> \<not> (\<exists> stp. is_final (steps (Suc 0, tp) B stp))"
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   288
begin
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   289
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   290
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   291
text {*
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   292
  The following lemma tries to derive the Hoare logic rule for sequentially combined TMs.
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   293
  It deals with the situtation when both @{text "A"} and @{text "B"} are terminated.
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   294
*}
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   295
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   296
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   297
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   298
lemma  t_merge_uhalt_tmp:
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   299
  assumes B_uh: "\<forall>stp. \<not> is_final (steps (Suc 0, b, c) B stp)"
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   300
  and merge_ah: "steps (Suc 0, tp) (A |+| B) stpa = (Suc (length A div 2), b, c)" 
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   301
  shows "\<forall> stp. \<not> is_final (steps (Suc 0, tp) (A |+| B) stp)"
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   302
  using B_uh merge_ah
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   303
apply(rule_tac allI)
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   304
apply(case_tac "stp > stpa")
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   305
apply(erule_tac x = "stp - stpa" in allE)
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   306
apply(case_tac "(steps (Suc 0, b, c) B (stp - stpa))", simp)
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   307
proof -
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   308
  fix stp a ba ca 
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   309
  assume h1: "\<not> is_final (a, ba, ca)" "stpa < stp"
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   310
  and h2: "steps (Suc 0, b, c) B (stp - stpa) = (a, ba, ca)"
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   311
  have "steps (Suc 0 + length A div 2, b, c) (A |+| B) (stp - stpa) = 
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   312
      (if a = 0 then 0 else a + length A div 2, ba, ca)"
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   313
    using A_wf B_wf h2
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   314
    by(rule_tac t_merge_snd_eq_steps, auto)
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   315
  moreover have "a > 0" using h1 by(simp add: is_final_def)
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   316
  moreover have "\<exists> stpb. stp = stpa + stpb" 
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   317
    using h1 by(rule_tac x = "stp - stpa" in exI, simp)
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   318
  ultimately show "\<not> is_final (steps (Suc 0, tp) (A |+| B) stp)"
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   319
    using merge_ah
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   320
    by(auto simp: steps_add is_final_def)
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   321
next
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   322
  fix stp
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   323
  assume h: "steps (Suc 0, tp) (A |+| B) stpa = (Suc (length A div 2), b, c)" "\<not> stpa < stp"
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   324
  hence "\<exists> stpb. stpa = stp + stpb" apply(rule_tac x = "stpa - stp" in exI, auto) done
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   325
  thus "\<not> is_final (steps (Suc 0, tp) (A |+| B) stp)"
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   326
    using h
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   327
    apply(auto)
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   328
    apply(cases "steps (Suc 0, tp) (A |+| B) stp", simp add: steps_add is_final_def steps_0)
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   329
    done
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   330
qed
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   331
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   332
text {*
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   333
  The following lemma deals with the situation when TM @{text "B"} can not terminate.
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   334
  *}
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   335
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   336
lemma t_merge_uhalt: 
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   337
  assumes aimpb: "Q1 \<mapsto> P4"
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   338
  shows "P1 \<mapsto> \<lambda> tp. \<not> (\<exists> stp. is_final (steps (Suc 0, tp) (A |+| B) stp))"
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   339
proof(simp only: assert_imp_def, rule_tac allI, rule_tac impI)
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   340
  fix tp 
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   341
  assume init_asst: "P1 tp"
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   342
  show "\<not> (\<exists>stp. is_final (steps (Suc 0, tp) (A |+| B) stp))"
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   343
  proof -
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   344
    have "\<exists> stp. let (s, tp') = steps (Suc 0, tp) A stp in s = 0 \<and> Q1 tp'"
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   345
      using A_halt[of tp] init_asst
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   346
      by(simp)
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   347
    from this obtain stpx where "let (s, tp') = steps (Suc 0, tp) A stpx in s = 0 \<and> Q1 tp'" ..
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   348
    thus "?thesis"
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   349
    proof(cases "steps (Suc 0, tp) A stpx", simp, erule_tac conjE)
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   350
      fix a b c
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   351
      assume "Q1 (b, c)"
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   352
        and h3: "steps (Suc 0, tp) A stpx = (0, b, c)"
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   353
      hence h2: "P4 (b, c)"  using aimpb
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   354
        by(simp add: assert_imp_def)
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   355
      have "\<exists> stp. steps (Suc 0, tp) (A |+| B) stp = (Suc (length A div 2), b, c)"
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   356
        using h3 A_wf B_wf
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   357
        apply(rule_tac stp = stpx in t_merge_pre_halt_same, auto)
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   358
        done
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   359
      from this obtain stpa where h4:"steps (Suc 0, tp) (A |+| B) stpa = (Suc (length A div 2), b, c)" ..
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   360
      have " \<not> (\<exists> stp. is_final (steps (Suc 0, b, c) B stp))"
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   361
        using B_uhalt [of "(b, c)"] h2 apply simp
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   362
        done
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   363
      from this and h4 show "\<forall>stp. \<not> is_final (steps (Suc 0, tp) (A |+| B) stp)"
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   364
        by(rule_tac t_merge_uhalt_tmp, auto)
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   365
    qed
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   366
  qed
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   367
qed
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   368
end
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   369
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   370
end
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   371