thys/turing_basic.thy
author Christian Urban <christian dot urban at kcl dot ac dot uk>
Sun, 13 Jan 2013 09:57:28 +0000
changeset 39 a95987e9c7e9
child 40 a37a21f7ccf4
permissions -rw-r--r--
added test about hoare triples
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
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   124
fun 
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   125
  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
   126
where
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   127
  "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
   128
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   129
lemma step_0 [simp]: 
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   130
  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
   131
by simp
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   132
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   133
lemma steps_0 [simp]: 
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   134
  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
   135
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
   136
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   137
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
   138
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   139
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
   140
  where
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   141
  "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
   142
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   143
definition
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   144
  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
   145
where
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   146
  "{P} p {Q} \<equiv> 
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   147
     (\<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
   148
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   149
lemma HoareI:
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   150
  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
   151
  shows "{P} p {Q}"
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   152
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
   153
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   154
lemma HoareI2:
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   155
  assumes "\<And>l r n. P (l, r) \<and> 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
   156
  shows "{P} p {Q}"
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   157
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
   158
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   159
text {*
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   160
{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
   161
-----------------------------------
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   162
    {P1} A |+| B {Q2}
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   163
*}
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   164
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   165
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   166
lemma Hoare_plus: 
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   167
  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
   168
  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
   169
  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
   170
  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
   171
  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
   172
  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
   173
proof(rule HoareI)
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   174
  fix a b
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   175
  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
   176
  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
   177
    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
   178
  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
   179
  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
   180
  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
   181
    fix aa ba c
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   182
    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
   183
      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
   184
    hence "P2 (ba, c)"
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   185
      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
   186
      done
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   187
    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
   188
      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
   189
    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
   190
    thus "?thesis"
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   191
    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
   192
      fix aa bb ca
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   193
      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
   194
      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
   195
        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
   196
        sorry
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   197
      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
   198
        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
   199
        sorry
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   200
      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
   201
        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
   202
        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
   203
        using g3 by simp
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   204
    qed
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   205
  qed
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   206
qed
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   207
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   208
lemma hoare_plus: 
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   209
  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
   210
  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
   211
  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
   212
  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
   213
  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
   214
  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
   215
unfolding hoare_def
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   216
proof(auto split: )
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   217
  fix a b
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   218
  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
   219
  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
   220
    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
   221
  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
   222
  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
   223
  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
   224
    fix aa ba c
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   225
    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
   226
      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
   227
    hence "P2 (ba, c)"
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   228
      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
   229
      done
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   230
    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
   231
      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
   232
    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
   233
    thus "?thesis"
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   234
    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
   235
      fix aa bb ca
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   236
      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
   237
      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
   238
        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
   239
        sorry
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   240
      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
   241
        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
   242
        sorry
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   243
      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
   244
        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
   245
        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
   246
        using g3 by simp
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   247
    qed
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   248
  qed
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   249
qed
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   250
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   251
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   252
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   253
locale turing_merge =
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   254
  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
   255
  and P2 :: "assert"
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   256
  and P3 :: "assert"
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   257
  and P4 :: "assert"
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   258
  and Q1:: "assert"
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   259
  and Q2 :: "assert"
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   260
  assumes 
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   261
  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
   262
  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
   263
  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
   264
  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
   265
  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
   266
  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
   267
begin
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   268
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   269
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   270
text {*
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   271
  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
   272
  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
   273
*}
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   274
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   275
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   276
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   277
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
   278
  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
   279
  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
   280
  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
   281
  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
   282
apply(rule_tac allI)
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   283
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
   284
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
   285
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
   286
proof -
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   287
  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
   288
  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
   289
  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
   290
  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
   291
      (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
   292
    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
   293
    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
   294
  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
   295
  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
   296
    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
   297
  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
   298
    using merge_ah
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   299
    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
   300
next
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   301
  fix stp
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   302
  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
   303
  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
   304
  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
   305
    using h
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   306
    apply(auto)
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   307
    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
   308
    done
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   309
qed
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   310
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   311
text {*
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   312
  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
   313
  *}
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   314
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   315
lemma t_merge_uhalt: 
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   316
  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
   317
  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
   318
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
   319
  fix tp 
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   320
  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
   321
  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
   322
  proof -
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   323
    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
   324
      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
   325
      by(simp)
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   326
    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
   327
    thus "?thesis"
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   328
    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
   329
      fix a b c
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   330
      assume "Q1 (b, c)"
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   331
        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
   332
      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
   333
        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
   334
      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
   335
        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
   336
        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
   337
        done
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   338
      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
   339
      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
   340
        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
   341
        done
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   342
      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
   343
        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
   344
    qed
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   345
  qed
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   346
qed
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   347
end
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   348
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   349
end
a95987e9c7e9 added test about hoare triples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   350