src/HOL/Hoare_Parallel/RG_Examples.thy
author huffman
Sun Apr 01 16:09:58 2012 +0200 (2012-04-01)
changeset 47255 30a1692557b0
parent 44890 22f665a2e91c
child 51121 34dbeb8f16a9
permissions -rw-r--r--
removed Nat_Numeral.thy, moving all theorems elsewhere
prensani@13020
     1
header {* \section{Examples} *}
prensani@13020
     2
haftmann@27651
     3
theory RG_Examples
haftmann@27651
     4
imports RG_Syntax
haftmann@27651
     5
begin
prensani@13020
     6
prensani@13020
     7
lemmas definitions [simp]= stable_def Pre_def Rely_def Guar_def Post_def Com_def 
prensani@13020
     8
prensani@13020
     9
subsection {* Set Elements of an Array to Zero *}
prensani@13020
    10
prensani@13020
    11
lemma le_less_trans2: "\<lbrakk>(j::nat)<k; i\<le> j\<rbrakk> \<Longrightarrow> i<k"
prensani@13020
    12
by simp
prensani@13020
    13
prensani@13020
    14
lemma add_le_less_mono: "\<lbrakk> (a::nat) < c; b\<le>d \<rbrakk> \<Longrightarrow> a + b < c + d"
prensani@13020
    15
by simp
prensani@13020
    16
prensani@13020
    17
record Example1 =
prensani@13020
    18
  A :: "nat list"
prensani@13020
    19
prensani@13020
    20
lemma Example1: 
prensani@13020
    21
 "\<turnstile> COBEGIN
prensani@13020
    22
      SCHEME [0 \<le> i < n]
prensani@13020
    23
     (\<acute>A := \<acute>A [i := 0], 
prensani@13020
    24
     \<lbrace> n < length \<acute>A \<rbrace>, 
prensani@13020
    25
     \<lbrace> length \<ordmasculine>A = length \<ordfeminine>A \<and> \<ordmasculine>A ! i = \<ordfeminine>A ! i \<rbrace>, 
prensani@13020
    26
     \<lbrace> length \<ordmasculine>A = length \<ordfeminine>A \<and> (\<forall>j<n. i \<noteq> j \<longrightarrow> \<ordmasculine>A ! j = \<ordfeminine>A ! j) \<rbrace>, 
prensani@13020
    27
     \<lbrace> \<acute>A ! i = 0 \<rbrace>) 
prensani@13020
    28
    COEND
prensani@13020
    29
 SAT [\<lbrace> n < length \<acute>A \<rbrace>, \<lbrace> \<ordmasculine>A = \<ordfeminine>A \<rbrace>, \<lbrace> True \<rbrace>, \<lbrace> \<forall>i < n. \<acute>A ! i = 0 \<rbrace>]"
prensani@13020
    30
apply(rule Parallel)
paulson@15102
    31
apply (auto intro!: Basic) 
prensani@13020
    32
done
prensani@13020
    33
prensani@13020
    34
lemma Example1_parameterized: 
prensani@13020
    35
"k < t \<Longrightarrow>
prensani@13020
    36
  \<turnstile> COBEGIN 
prensani@13020
    37
    SCHEME [k*n\<le>i<(Suc k)*n] (\<acute>A:=\<acute>A[i:=0], 
prensani@13020
    38
   \<lbrace>t*n < length \<acute>A\<rbrace>, 
prensani@13020
    39
   \<lbrace>t*n < length \<ordmasculine>A \<and> length \<ordmasculine>A=length \<ordfeminine>A \<and> \<ordmasculine>A!i = \<ordfeminine>A!i\<rbrace>, 
prensani@13020
    40
   \<lbrace>t*n < length \<ordmasculine>A \<and> length \<ordmasculine>A=length \<ordfeminine>A \<and> (\<forall>j<length \<ordmasculine>A . i\<noteq>j \<longrightarrow> \<ordmasculine>A!j = \<ordfeminine>A!j)\<rbrace>, 
prensani@13020
    41
   \<lbrace>\<acute>A!i=0\<rbrace>) 
prensani@13020
    42
   COEND  
prensani@13020
    43
 SAT [\<lbrace>t*n < length \<acute>A\<rbrace>, 
prensani@13020
    44
      \<lbrace>t*n < length \<ordmasculine>A \<and> length \<ordmasculine>A=length \<ordfeminine>A \<and> (\<forall>i<n. \<ordmasculine>A!(k*n+i)=\<ordfeminine>A!(k*n+i))\<rbrace>, 
prensani@13020
    45
      \<lbrace>t*n < length \<ordmasculine>A \<and> length \<ordmasculine>A=length \<ordfeminine>A \<and> 
prensani@13020
    46
      (\<forall>i<length \<ordmasculine>A . (i<k*n \<longrightarrow> \<ordmasculine>A!i = \<ordfeminine>A!i) \<and> ((Suc k)*n \<le> i\<longrightarrow> \<ordmasculine>A!i = \<ordfeminine>A!i))\<rbrace>, 
prensani@13020
    47
      \<lbrace>\<forall>i<n. \<acute>A!(k*n+i) = 0\<rbrace>]"
prensani@13020
    48
apply(rule Parallel)
paulson@15102
    49
    apply auto
paulson@15102
    50
  apply(erule_tac x="k*n +i" in allE)
paulson@15102
    51
  apply(subgoal_tac "k*n+i <length (A b)")
prensani@13020
    52
   apply force
paulson@15102
    53
  apply(erule le_less_trans2) 
paulson@15102
    54
  apply(case_tac t,simp+)
paulson@15102
    55
  apply (simp add:add_commute)
paulson@15102
    56
  apply(simp add: add_le_mono)
prensani@13020
    57
apply(rule Basic)
prensani@13020
    58
   apply simp
prensani@13020
    59
   apply clarify
prensani@13020
    60
   apply (subgoal_tac "k*n+i< length (A x)")
prensani@13020
    61
    apply simp
prensani@13020
    62
   apply(erule le_less_trans2)
prensani@13020
    63
   apply(case_tac t,simp+)
prensani@13020
    64
   apply (simp add:add_commute)
paulson@15102
    65
   apply(rule add_le_mono, auto)
prensani@13020
    66
done
prensani@13020
    67
paulson@15102
    68
prensani@13020
    69
subsection {* Increment a Variable in Parallel *}
prensani@13020
    70
prensani@13020
    71
subsubsection {* Two components *}
prensani@13020
    72
prensani@13020
    73
record Example2 =
prensani@13020
    74
  x  :: nat
prensani@13020
    75
  c_0 :: nat
prensani@13020
    76
  c_1 :: nat
prensani@13020
    77
prensani@13020
    78
lemma Example2: 
prensani@13020
    79
 "\<turnstile>  COBEGIN
prensani@13020
    80
    (\<langle> \<acute>x:=\<acute>x+1;; \<acute>c_0:=\<acute>c_0 + 1 \<rangle>, 
prensani@13020
    81
     \<lbrace>\<acute>x=\<acute>c_0 + \<acute>c_1  \<and> \<acute>c_0=0\<rbrace>, 
prensani@13020
    82
     \<lbrace>\<ordmasculine>c_0 = \<ordfeminine>c_0 \<and> 
prensani@13020
    83
        (\<ordmasculine>x=\<ordmasculine>c_0 + \<ordmasculine>c_1 
prensani@13020
    84
        \<longrightarrow> \<ordfeminine>x = \<ordfeminine>c_0 + \<ordfeminine>c_1)\<rbrace>,  
prensani@13020
    85
     \<lbrace>\<ordmasculine>c_1 = \<ordfeminine>c_1 \<and> 
prensani@13020
    86
         (\<ordmasculine>x=\<ordmasculine>c_0 + \<ordmasculine>c_1 
prensani@13020
    87
         \<longrightarrow> \<ordfeminine>x =\<ordfeminine>c_0 + \<ordfeminine>c_1)\<rbrace>,
prensani@13020
    88
     \<lbrace>\<acute>x=\<acute>c_0 + \<acute>c_1 \<and> \<acute>c_0=1 \<rbrace>)
prensani@13020
    89
  \<parallel>
prensani@13020
    90
      (\<langle> \<acute>x:=\<acute>x+1;; \<acute>c_1:=\<acute>c_1+1 \<rangle>, 
prensani@13020
    91
     \<lbrace>\<acute>x=\<acute>c_0 + \<acute>c_1 \<and> \<acute>c_1=0 \<rbrace>, 
prensani@13020
    92
     \<lbrace>\<ordmasculine>c_1 = \<ordfeminine>c_1 \<and> 
prensani@13020
    93
        (\<ordmasculine>x=\<ordmasculine>c_0 + \<ordmasculine>c_1 
prensani@13020
    94
        \<longrightarrow> \<ordfeminine>x = \<ordfeminine>c_0 + \<ordfeminine>c_1)\<rbrace>,  
prensani@13020
    95
     \<lbrace>\<ordmasculine>c_0 = \<ordfeminine>c_0 \<and> 
prensani@13020
    96
         (\<ordmasculine>x=\<ordmasculine>c_0 + \<ordmasculine>c_1 
prensani@13020
    97
        \<longrightarrow> \<ordfeminine>x =\<ordfeminine>c_0 + \<ordfeminine>c_1)\<rbrace>,
prensani@13020
    98
     \<lbrace>\<acute>x=\<acute>c_0 + \<acute>c_1 \<and> \<acute>c_1=1\<rbrace>)
prensani@13020
    99
 COEND
prensani@13020
   100
 SAT [\<lbrace>\<acute>x=0 \<and> \<acute>c_0=0 \<and> \<acute>c_1=0\<rbrace>, 
prensani@13020
   101
      \<lbrace>\<ordmasculine>x=\<ordfeminine>x \<and>  \<ordmasculine>c_0= \<ordfeminine>c_0 \<and> \<ordmasculine>c_1=\<ordfeminine>c_1\<rbrace>,
prensani@13020
   102
      \<lbrace>True\<rbrace>,
prensani@13020
   103
      \<lbrace>\<acute>x=2\<rbrace>]"
prensani@13020
   104
apply(rule Parallel)
prensani@13020
   105
   apply simp_all
prensani@13020
   106
   apply clarify
prensani@13020
   107
   apply(case_tac i)
prensani@13020
   108
    apply simp
paulson@15102
   109
    apply(rule conjI)
prensani@13020
   110
     apply clarify
prensani@13020
   111
     apply simp
prensani@13020
   112
    apply clarify
prensani@13020
   113
    apply simp
prensani@13020
   114
   apply simp
paulson@15102
   115
   apply(rule conjI)
prensani@13020
   116
    apply clarify
prensani@13020
   117
    apply simp
prensani@13020
   118
   apply clarify
prensani@13020
   119
   apply simp
nipkow@13187
   120
   apply(subgoal_tac "j=0")
nipkow@34233
   121
    apply (simp)
nipkow@13187
   122
   apply arith
prensani@13020
   123
  apply clarify
prensani@13020
   124
  apply(case_tac i,simp,simp)
nipkow@34233
   125
 apply clarify
prensani@13020
   126
 apply simp
prensani@13020
   127
 apply(erule_tac x=0 in all_dupE)
prensani@13020
   128
 apply(erule_tac x=1 in allE,simp)
prensani@13020
   129
apply clarify
prensani@13020
   130
apply(case_tac i,simp)
prensani@13020
   131
 apply(rule Await)
prensani@13020
   132
  apply simp_all
prensani@13020
   133
 apply(clarify)
prensani@13020
   134
 apply(rule Seq)
prensani@13020
   135
  prefer 2
prensani@13020
   136
  apply(rule Basic)
prensani@13020
   137
   apply simp_all
prensani@13020
   138
  apply(rule subset_refl)
prensani@13020
   139
 apply(rule Basic)
prensani@13020
   140
 apply simp_all
prensani@13020
   141
 apply clarify
prensani@13020
   142
 apply simp
prensani@13020
   143
apply(rule Await)
prensani@13020
   144
 apply simp_all
prensani@13020
   145
apply(clarify)
prensani@13020
   146
apply(rule Seq)
prensani@13020
   147
 prefer 2
prensani@13020
   148
 apply(rule Basic)
prensani@13020
   149
  apply simp_all
prensani@13020
   150
 apply(rule subset_refl)
paulson@15102
   151
apply(auto intro!: Basic)
prensani@13020
   152
done
prensani@13020
   153
prensani@13020
   154
subsubsection {* Parameterized *}
prensani@13020
   155
nipkow@15561
   156
lemma Example2_lemma2_aux: "j<n \<Longrightarrow> 
nipkow@15561
   157
 (\<Sum>i=0..<n. (b i::nat)) =
nipkow@15561
   158
 (\<Sum>i=0..<j. b i) + b j + (\<Sum>i=0..<n-(Suc j) . b (Suc j + i))"
prensani@13020
   159
apply(induct n)
prensani@13020
   160
 apply simp_all
prensani@13020
   161
apply(simp add:less_Suc_eq)
prensani@13020
   162
 apply(auto)
prensani@13020
   163
apply(subgoal_tac "n - j = Suc(n- Suc j)")
prensani@13020
   164
  apply simp
prensani@13020
   165
apply arith
nipkow@15561
   166
done
prensani@13020
   167
nipkow@15561
   168
lemma Example2_lemma2_aux2: 
nipkow@15561
   169
  "j\<le> s \<Longrightarrow> (\<Sum>i::nat=0..<j. (b (s:=t)) i) = (\<Sum>i=0..<j. b i)"
prensani@13020
   170
apply(induct j)
nipkow@15041
   171
 apply (simp_all cong:setsum_cong)
prensani@13020
   172
done
prensani@13020
   173
nipkow@15041
   174
lemma Example2_lemma2: 
nipkow@15561
   175
 "\<lbrakk>j<n; b j=0\<rbrakk> \<Longrightarrow> Suc (\<Sum>i::nat=0..<n. b i)=(\<Sum>i=0..<n. (b (j := Suc 0)) i)"
nipkow@15041
   176
apply(frule_tac b="(b (j:=(Suc 0)))" in Example2_lemma2_aux)
nipkow@15561
   177
apply(erule_tac  t="setsum (b(j := (Suc 0))) {0..<n}" in ssubst)
prensani@13020
   178
apply(frule_tac b=b in Example2_lemma2_aux)
nipkow@15561
   179
apply(erule_tac  t="setsum b {0..<n}" in ssubst)
nipkow@15561
   180
apply(subgoal_tac "Suc (setsum b {0..<j} + b j + (\<Sum>i=0..<n - Suc j. b (Suc j + i)))=(setsum b {0..<j} + Suc (b j) + (\<Sum>i=0..<n - Suc j. b (Suc j + i)))")
nipkow@15041
   181
apply(rotate_tac -1)
nipkow@15041
   182
apply(erule ssubst)
nipkow@15041
   183
apply(subgoal_tac "j\<le>j")
nipkow@15041
   184
 apply(drule_tac b="b" and t="(Suc 0)" in Example2_lemma2_aux2)
nipkow@15041
   185
apply(rotate_tac -1)
nipkow@15041
   186
apply(erule ssubst)
prensani@13020
   187
apply simp_all
prensani@13020
   188
done
prensani@13020
   189
nipkow@15561
   190
lemma Example2_lemma2_Suc0: "\<lbrakk>j<n; b j=0\<rbrakk> \<Longrightarrow>
nipkow@15561
   191
 Suc (\<Sum>i::nat=0..< n. b i)=(\<Sum>i=0..< n. (b (j:=Suc 0)) i)"
prensani@13020
   192
by(simp add:Example2_lemma2)
prensani@13020
   193
prensani@13020
   194
record Example2_parameterized =   
prensani@13020
   195
  C :: "nat \<Rightarrow> nat"
prensani@13020
   196
  y  :: nat
prensani@13020
   197
prensani@13020
   198
lemma Example2_parameterized: "0<n \<Longrightarrow> 
prensani@13020
   199
  \<turnstile> COBEGIN SCHEME  [0\<le>i<n]
prensani@13020
   200
     (\<langle> \<acute>y:=\<acute>y+1;; \<acute>C:=\<acute>C (i:=1) \<rangle>, 
nipkow@15561
   201
     \<lbrace>\<acute>y=(\<Sum>i=0..<n. \<acute>C i) \<and> \<acute>C i=0\<rbrace>, 
prensani@13020
   202
     \<lbrace>\<ordmasculine>C i = \<ordfeminine>C i \<and> 
nipkow@15561
   203
      (\<ordmasculine>y=(\<Sum>i=0..<n. \<ordmasculine>C i) \<longrightarrow> \<ordfeminine>y =(\<Sum>i=0..<n. \<ordfeminine>C i))\<rbrace>,  
prensani@13020
   204
     \<lbrace>(\<forall>j<n. i\<noteq>j \<longrightarrow> \<ordmasculine>C j = \<ordfeminine>C j) \<and> 
nipkow@15561
   205
       (\<ordmasculine>y=(\<Sum>i=0..<n. \<ordmasculine>C i) \<longrightarrow> \<ordfeminine>y =(\<Sum>i=0..<n. \<ordfeminine>C i))\<rbrace>,
nipkow@15561
   206
     \<lbrace>\<acute>y=(\<Sum>i=0..<n. \<acute>C i) \<and> \<acute>C i=1\<rbrace>) 
prensani@13020
   207
    COEND
nipkow@15561
   208
 SAT [\<lbrace>\<acute>y=0 \<and> (\<Sum>i=0..<n. \<acute>C i)=0 \<rbrace>, \<lbrace>\<ordmasculine>C=\<ordfeminine>C \<and> \<ordmasculine>y=\<ordfeminine>y\<rbrace>, \<lbrace>True\<rbrace>, \<lbrace>\<acute>y=n\<rbrace>]"
prensani@13020
   209
apply(rule Parallel)
prensani@13020
   210
apply force
prensani@13020
   211
apply force
nipkow@15561
   212
apply(force)
prensani@13020
   213
apply clarify
prensani@13020
   214
apply simp
nipkow@15561
   215
apply(simp cong:setsum_ivl_cong)
prensani@13020
   216
apply clarify
prensani@13020
   217
apply simp
prensani@13020
   218
apply(rule Await)
prensani@13020
   219
apply simp_all
prensani@13020
   220
apply clarify
prensani@13020
   221
apply(rule Seq)
prensani@13020
   222
prefer 2
prensani@13020
   223
apply(rule Basic)
prensani@13020
   224
apply(rule subset_refl)
prensani@13020
   225
apply simp+
prensani@13020
   226
apply(rule Basic)
prensani@13020
   227
apply simp
prensani@13020
   228
apply clarify
prensani@13020
   229
apply simp
nipkow@16733
   230
apply(simp add:Example2_lemma2_Suc0 cong:if_cong)
prensani@13020
   231
apply simp+
prensani@13020
   232
done
prensani@13020
   233
prensani@13020
   234
subsection {* Find Least Element *}
prensani@13020
   235
prensani@13020
   236
text {* A previous lemma: *}
prensani@13020
   237
prensani@13020
   238
lemma mod_aux :"\<lbrakk>i < (n::nat); a mod n = i;  j < a + n; j mod n = i; a < j\<rbrakk> \<Longrightarrow> False"
prensani@13020
   239
apply(subgoal_tac "a=a div n*n + a mod n" )
nipkow@13517
   240
 prefer 2 apply (simp (no_asm_use))
prensani@13020
   241
apply(subgoal_tac "j=j div n*n + j mod n")
nipkow@13517
   242
 prefer 2 apply (simp (no_asm_use))
prensani@13020
   243
apply simp
prensani@13020
   244
apply(subgoal_tac "a div n*n < j div n*n")
prensani@13020
   245
prefer 2 apply arith
prensani@13020
   246
apply(subgoal_tac "j div n*n < (a div n + 1)*n")
nipkow@13517
   247
prefer 2 apply simp
prensani@13020
   248
apply (simp only:mult_less_cancel2)
prensani@13020
   249
apply arith
prensani@13020
   250
done
prensani@13020
   251
prensani@13020
   252
record Example3 =
prensani@13020
   253
  X :: "nat \<Rightarrow> nat"
prensani@13020
   254
  Y :: "nat \<Rightarrow> nat"
prensani@13020
   255
prensani@13020
   256
lemma Example3: "m mod n=0 \<Longrightarrow> 
prensani@13020
   257
 \<turnstile> COBEGIN 
prensani@13020
   258
 SCHEME [0\<le>i<n]
prensani@13020
   259
 (WHILE (\<forall>j<n. \<acute>X i < \<acute>Y j)  DO 
prensani@13020
   260
   IF P(B!(\<acute>X i)) THEN \<acute>Y:=\<acute>Y (i:=\<acute>X i) 
prensani@13020
   261
   ELSE \<acute>X:= \<acute>X (i:=(\<acute>X i)+ n) FI 
prensani@13020
   262
  OD,
prensani@13020
   263
 \<lbrace>(\<acute>X i) mod n=i \<and> (\<forall>j<\<acute>X i. j mod n=i \<longrightarrow> \<not>P(B!j)) \<and> (\<acute>Y i<m \<longrightarrow> P(B!(\<acute>Y i)) \<and> \<acute>Y i\<le> m+i)\<rbrace>,
prensani@13020
   264
 \<lbrace>(\<forall>j<n. i\<noteq>j \<longrightarrow> \<ordfeminine>Y j \<le> \<ordmasculine>Y j) \<and> \<ordmasculine>X i = \<ordfeminine>X i \<and> 
prensani@13020
   265
   \<ordmasculine>Y i = \<ordfeminine>Y i\<rbrace>,
prensani@13020
   266
 \<lbrace>(\<forall>j<n. i\<noteq>j \<longrightarrow> \<ordmasculine>X j = \<ordfeminine>X j \<and> \<ordmasculine>Y j = \<ordfeminine>Y j) \<and>   
prensani@13020
   267
   \<ordfeminine>Y i \<le> \<ordmasculine>Y i\<rbrace>,
prensani@13020
   268
 \<lbrace>(\<acute>X i) mod n=i \<and> (\<forall>j<\<acute>X i. j mod n=i \<longrightarrow> \<not>P(B!j)) \<and> (\<acute>Y i<m \<longrightarrow> P(B!(\<acute>Y i)) \<and> \<acute>Y i\<le> m+i) \<and> (\<exists>j<n. \<acute>Y j \<le> \<acute>X i) \<rbrace>) 
prensani@13020
   269
 COEND
prensani@13020
   270
 SAT [\<lbrace> \<forall>i<n. \<acute>X i=i \<and> \<acute>Y i=m+i \<rbrace>,\<lbrace>\<ordmasculine>X=\<ordfeminine>X \<and> \<ordmasculine>Y=\<ordfeminine>Y\<rbrace>,\<lbrace>True\<rbrace>,
prensani@13020
   271
  \<lbrace>\<forall>i<n. (\<acute>X i) mod n=i \<and> (\<forall>j<\<acute>X i. j mod n=i \<longrightarrow> \<not>P(B!j)) \<and> 
prensani@13020
   272
    (\<acute>Y i<m \<longrightarrow> P(B!(\<acute>Y i)) \<and> \<acute>Y i\<le> m+i) \<and> (\<exists>j<n. \<acute>Y j \<le> \<acute>X i)\<rbrace>]"
prensani@13020
   273
apply(rule Parallel)
prensani@13099
   274
--{*5 subgoals left *}
prensani@13020
   275
apply force+
prensani@13020
   276
apply clarify
prensani@13020
   277
apply simp
prensani@13020
   278
apply(rule While)
prensani@13020
   279
    apply force
prensani@13020
   280
   apply force
prensani@13020
   281
  apply force
ballarin@14174
   282
 apply(rule_tac pre'="\<lbrace> \<acute>X i mod n = i \<and> (\<forall>j. j<\<acute>X i \<longrightarrow> j mod n = i \<longrightarrow> \<not>P(B!j)) \<and> (\<acute>Y i < n * q \<longrightarrow> P (B!(\<acute>Y i))) \<and> \<acute>X i<\<acute>Y i\<rbrace>" in Conseq)
prensani@13020
   283
     apply force
prensani@13020
   284
    apply(rule subset_refl)+
prensani@13020
   285
 apply(rule Cond)
prensani@13020
   286
    apply force
prensani@13020
   287
   apply(rule Basic)
prensani@13020
   288
      apply force
nipkow@44890
   289
     apply fastforce
prensani@13020
   290
    apply force
prensani@13020
   291
   apply force
prensani@13020
   292
  apply(rule Basic)
haftmann@27676
   293
     apply simp
prensani@13020
   294
     apply clarify
prensani@13020
   295
     apply simp
haftmann@27651
   296
     apply (case_tac "X x (j mod n) \<le> j")
haftmann@27651
   297
     apply (drule le_imp_less_or_eq)
haftmann@27651
   298
     apply (erule disjE)
haftmann@27651
   299
     apply (drule_tac j=j and n=n and i="j mod n" and a="X x (j mod n)" in mod_aux)
haftmann@27651
   300
     apply auto
prensani@13020
   301
done
prensani@13020
   302
prensani@13020
   303
text {* Same but with a list as auxiliary variable: *}
prensani@13020
   304
prensani@13020
   305
record Example3_list =
prensani@13020
   306
  X :: "nat list"
prensani@13020
   307
  Y :: "nat list"
prensani@13020
   308
prensani@13020
   309
lemma Example3_list: "m mod n=0 \<Longrightarrow> \<turnstile> (COBEGIN SCHEME [0\<le>i<n]
prensani@13020
   310
 (WHILE (\<forall>j<n. \<acute>X!i < \<acute>Y!j)  DO 
prensani@13020
   311
     IF P(B!(\<acute>X!i)) THEN \<acute>Y:=\<acute>Y[i:=\<acute>X!i] ELSE \<acute>X:= \<acute>X[i:=(\<acute>X!i)+ n] FI 
prensani@13020
   312
  OD,
prensani@13020
   313
 \<lbrace>n<length \<acute>X \<and> n<length \<acute>Y \<and> (\<acute>X!i) mod n=i \<and> (\<forall>j<\<acute>X!i. j mod n=i \<longrightarrow> \<not>P(B!j)) \<and> (\<acute>Y!i<m \<longrightarrow> P(B!(\<acute>Y!i)) \<and> \<acute>Y!i\<le> m+i)\<rbrace>,
prensani@13020
   314
 \<lbrace>(\<forall>j<n. i\<noteq>j \<longrightarrow> \<ordfeminine>Y!j \<le> \<ordmasculine>Y!j) \<and> \<ordmasculine>X!i = \<ordfeminine>X!i \<and> 
prensani@13020
   315
   \<ordmasculine>Y!i = \<ordfeminine>Y!i \<and> length \<ordmasculine>X = length \<ordfeminine>X \<and> length \<ordmasculine>Y = length \<ordfeminine>Y\<rbrace>,
prensani@13020
   316
 \<lbrace>(\<forall>j<n. i\<noteq>j \<longrightarrow> \<ordmasculine>X!j = \<ordfeminine>X!j \<and> \<ordmasculine>Y!j = \<ordfeminine>Y!j) \<and>   
prensani@13020
   317
   \<ordfeminine>Y!i \<le> \<ordmasculine>Y!i \<and> length \<ordmasculine>X = length \<ordfeminine>X \<and> length \<ordmasculine>Y = length \<ordfeminine>Y\<rbrace>,
prensani@13020
   318
 \<lbrace>(\<acute>X!i) mod n=i \<and> (\<forall>j<\<acute>X!i. j mod n=i \<longrightarrow> \<not>P(B!j)) \<and> (\<acute>Y!i<m \<longrightarrow> P(B!(\<acute>Y!i)) \<and> \<acute>Y!i\<le> m+i) \<and> (\<exists>j<n. \<acute>Y!j \<le> \<acute>X!i) \<rbrace>) COEND)
prensani@13020
   319
 SAT [\<lbrace>n<length \<acute>X \<and> n<length \<acute>Y \<and> (\<forall>i<n. \<acute>X!i=i \<and> \<acute>Y!i=m+i) \<rbrace>,
prensani@13020
   320
      \<lbrace>\<ordmasculine>X=\<ordfeminine>X \<and> \<ordmasculine>Y=\<ordfeminine>Y\<rbrace>,
prensani@13020
   321
      \<lbrace>True\<rbrace>,
prensani@13020
   322
      \<lbrace>\<forall>i<n. (\<acute>X!i) mod n=i \<and> (\<forall>j<\<acute>X!i. j mod n=i \<longrightarrow> \<not>P(B!j)) \<and> 
prensani@13020
   323
        (\<acute>Y!i<m \<longrightarrow> P(B!(\<acute>Y!i)) \<and> \<acute>Y!i\<le> m+i) \<and> (\<exists>j<n. \<acute>Y!j \<le> \<acute>X!i)\<rbrace>]"
prensani@13020
   324
apply(rule Parallel)
prensani@13099
   325
--{* 5 subgoals left *}
prensani@13020
   326
apply force+
prensani@13020
   327
apply clarify
prensani@13020
   328
apply simp
prensani@13020
   329
apply(rule While)
prensani@13020
   330
    apply force
prensani@13020
   331
   apply force
prensani@13020
   332
  apply force
ballarin@14174
   333
 apply(rule_tac pre'="\<lbrace>n<length \<acute>X \<and> n<length \<acute>Y \<and> \<acute>X ! i mod n = i \<and> (\<forall>j. j < \<acute>X ! i \<longrightarrow> j mod n = i \<longrightarrow> \<not> P (B ! j)) \<and> (\<acute>Y ! i < n * q \<longrightarrow> P (B ! (\<acute>Y ! i))) \<and> \<acute>X!i<\<acute>Y!i\<rbrace>" in Conseq)
prensani@13020
   334
     apply force
prensani@13020
   335
    apply(rule subset_refl)+
prensani@13020
   336
 apply(rule Cond)
prensani@13020
   337
    apply force
prensani@13020
   338
   apply(rule Basic)
prensani@13020
   339
      apply force
prensani@13020
   340
     apply force
prensani@13020
   341
    apply force
prensani@13020
   342
   apply force
prensani@13020
   343
  apply(rule Basic)
prensani@13020
   344
     apply simp
prensani@13020
   345
     apply clarify
haftmann@27676
   346
     apply simp
prensani@13020
   347
     apply(rule allI)
prensani@13020
   348
     apply(rule impI)+
prensani@13020
   349
     apply(case_tac "X x ! i\<le> j")
prensani@13020
   350
      apply(drule le_imp_less_or_eq)
prensani@13020
   351
      apply(erule disjE)
prensani@13020
   352
       apply(drule_tac j=j and n=n and i=i and a="X x ! i" in mod_aux)
haftmann@27651
   353
     apply auto
prensani@13020
   354
done
prensani@13020
   355
nipkow@13187
   356
end