src/HOL/Hoare_Parallel/OG_Examples.thy
author wenzelm
Thu May 24 17:25:53 2012 +0200 (2012-05-24)
changeset 47988 e4b69e10b990
parent 44890 22f665a2e91c
child 53241 effd8fcabca2
permissions -rw-r--r--
tuned proofs;
prensani@13020
     1
header {* \section{Examples} *}
prensani@13020
     2
haftmann@16417
     3
theory OG_Examples imports OG_Syntax begin
prensani@13020
     4
prensani@13020
     5
subsection {* Mutual Exclusion *}
prensani@13020
     6
prensani@13020
     7
subsubsection {* Peterson's Algorithm I*}
prensani@13020
     8
prensani@13020
     9
text {* Eike Best. "Semantics of Sequential and Parallel Programs", page 217. *}
prensani@13020
    10
prensani@13020
    11
record Petersons_mutex_1 =
prensani@13020
    12
 pr1 :: nat
prensani@13020
    13
 pr2 :: nat
prensani@13020
    14
 in1 :: bool
prensani@13020
    15
 in2 :: bool 
prensani@13020
    16
 hold :: nat
prensani@13020
    17
prensani@13020
    18
lemma Petersons_mutex_1: 
prensani@13020
    19
  "\<parallel>- .{\<acute>pr1=0 \<and> \<not>\<acute>in1 \<and> \<acute>pr2=0 \<and> \<not>\<acute>in2 }.  
prensani@13020
    20
  COBEGIN .{\<acute>pr1=0 \<and> \<not>\<acute>in1}.  
prensani@13020
    21
  WHILE True INV .{\<acute>pr1=0 \<and> \<not>\<acute>in1}.  
prensani@13020
    22
  DO  
prensani@13020
    23
  .{\<acute>pr1=0 \<and> \<not>\<acute>in1}. \<langle> \<acute>in1:=True,,\<acute>pr1:=1 \<rangle>;;  
prensani@13020
    24
  .{\<acute>pr1=1 \<and> \<acute>in1}.  \<langle> \<acute>hold:=1,,\<acute>pr1:=2 \<rangle>;;  
prensani@13020
    25
  .{\<acute>pr1=2 \<and> \<acute>in1 \<and> (\<acute>hold=1 \<or> \<acute>hold=2 \<and> \<acute>pr2=2)}.  
prensani@13020
    26
  AWAIT (\<not>\<acute>in2 \<or> \<not>(\<acute>hold=1)) THEN \<acute>pr1:=3 END;;    
prensani@13020
    27
  .{\<acute>pr1=3 \<and> \<acute>in1 \<and> (\<acute>hold=1 \<or> \<acute>hold=2 \<and> \<acute>pr2=2)}. 
prensani@13020
    28
   \<langle>\<acute>in1:=False,,\<acute>pr1:=0\<rangle> 
prensani@13020
    29
  OD .{\<acute>pr1=0 \<and> \<not>\<acute>in1}.  
prensani@13020
    30
  \<parallel>  
prensani@13020
    31
  .{\<acute>pr2=0 \<and> \<not>\<acute>in2}.  
prensani@13020
    32
  WHILE True INV .{\<acute>pr2=0 \<and> \<not>\<acute>in2}.  
prensani@13020
    33
  DO  
prensani@13020
    34
  .{\<acute>pr2=0 \<and> \<not>\<acute>in2}. \<langle> \<acute>in2:=True,,\<acute>pr2:=1 \<rangle>;;  
prensani@13020
    35
  .{\<acute>pr2=1 \<and> \<acute>in2}. \<langle>  \<acute>hold:=2,,\<acute>pr2:=2 \<rangle>;;  
prensani@13020
    36
  .{\<acute>pr2=2 \<and> \<acute>in2 \<and> (\<acute>hold=2 \<or> (\<acute>hold=1 \<and> \<acute>pr1=2))}.  
prensani@13020
    37
  AWAIT (\<not>\<acute>in1 \<or> \<not>(\<acute>hold=2)) THEN \<acute>pr2:=3  END;;    
prensani@13020
    38
  .{\<acute>pr2=3 \<and> \<acute>in2 \<and> (\<acute>hold=2 \<or> (\<acute>hold=1 \<and> \<acute>pr1=2))}. 
prensani@13020
    39
    \<langle>\<acute>in2:=False,,\<acute>pr2:=0\<rangle> 
prensani@13020
    40
  OD .{\<acute>pr2=0 \<and> \<not>\<acute>in2}.  
prensani@13020
    41
  COEND  
prensani@13020
    42
  .{\<acute>pr1=0 \<and> \<not>\<acute>in1 \<and> \<acute>pr2=0 \<and> \<not>\<acute>in2}."
prensani@13020
    43
apply oghoare
prensani@13020
    44
--{* 104 verification conditions. *}
prensani@13020
    45
apply auto
prensani@13020
    46
done
prensani@13020
    47
prensani@13020
    48
subsubsection {*Peterson's Algorithm II: A Busy Wait Solution *}
prensani@13020
    49
 
prensani@13020
    50
text {* Apt and Olderog. "Verification of sequential and concurrent Programs", page 282. *}
prensani@13020
    51
prensani@13020
    52
record Busy_wait_mutex =
prensani@13020
    53
 flag1 :: bool
prensani@13020
    54
 flag2 :: bool
prensani@13020
    55
 turn  :: nat
prensani@13020
    56
 after1 :: bool 
prensani@13020
    57
 after2 :: bool
prensani@13020
    58
prensani@13020
    59
lemma Busy_wait_mutex: 
prensani@13020
    60
 "\<parallel>-  .{True}.  
prensani@13020
    61
  \<acute>flag1:=False,, \<acute>flag2:=False,,  
prensani@13020
    62
  COBEGIN .{\<not>\<acute>flag1}.  
prensani@13020
    63
        WHILE True  
prensani@13020
    64
        INV .{\<not>\<acute>flag1}.  
prensani@13020
    65
        DO .{\<not>\<acute>flag1}. \<langle> \<acute>flag1:=True,,\<acute>after1:=False \<rangle>;;  
prensani@13020
    66
           .{\<acute>flag1 \<and> \<not>\<acute>after1}. \<langle> \<acute>turn:=1,,\<acute>after1:=True \<rangle>;;  
prensani@13020
    67
           .{\<acute>flag1 \<and> \<acute>after1 \<and> (\<acute>turn=1 \<or> \<acute>turn=2)}.  
prensani@13020
    68
            WHILE \<not>(\<acute>flag2 \<longrightarrow> \<acute>turn=2)  
prensani@13020
    69
            INV .{\<acute>flag1 \<and> \<acute>after1 \<and> (\<acute>turn=1 \<or> \<acute>turn=2)}.  
prensani@13020
    70
            DO .{\<acute>flag1 \<and> \<acute>after1 \<and> (\<acute>turn=1 \<or> \<acute>turn=2)}. SKIP OD;; 
prensani@13020
    71
           .{\<acute>flag1 \<and> \<acute>after1 \<and> (\<acute>flag2 \<and> \<acute>after2 \<longrightarrow> \<acute>turn=2)}.
prensani@13020
    72
            \<acute>flag1:=False  
prensani@13020
    73
        OD  
prensani@13020
    74
       .{False}.  
prensani@13020
    75
  \<parallel>  
prensani@13020
    76
     .{\<not>\<acute>flag2}.  
prensani@13020
    77
        WHILE True  
prensani@13020
    78
        INV .{\<not>\<acute>flag2}.  
prensani@13020
    79
        DO .{\<not>\<acute>flag2}. \<langle> \<acute>flag2:=True,,\<acute>after2:=False \<rangle>;;  
prensani@13020
    80
           .{\<acute>flag2 \<and> \<not>\<acute>after2}. \<langle> \<acute>turn:=2,,\<acute>after2:=True \<rangle>;;  
prensani@13020
    81
           .{\<acute>flag2 \<and> \<acute>after2 \<and> (\<acute>turn=1 \<or> \<acute>turn=2)}.  
prensani@13020
    82
            WHILE \<not>(\<acute>flag1 \<longrightarrow> \<acute>turn=1)  
prensani@13020
    83
            INV .{\<acute>flag2 \<and> \<acute>after2 \<and> (\<acute>turn=1 \<or> \<acute>turn=2)}.  
prensani@13020
    84
            DO .{\<acute>flag2 \<and> \<acute>after2 \<and> (\<acute>turn=1 \<or> \<acute>turn=2)}. SKIP OD;;  
prensani@13020
    85
           .{\<acute>flag2 \<and> \<acute>after2 \<and> (\<acute>flag1 \<and> \<acute>after1 \<longrightarrow> \<acute>turn=1)}. 
prensani@13020
    86
            \<acute>flag2:=False  
prensani@13020
    87
        OD  
prensani@13020
    88
       .{False}.  
prensani@13020
    89
  COEND  
prensani@13020
    90
  .{False}."
prensani@13020
    91
apply oghoare
prensani@13020
    92
--{* 122 vc *}
prensani@13020
    93
apply auto
prensani@13020
    94
done
prensani@13020
    95
prensani@13020
    96
subsubsection {* Peterson's Algorithm III: A Solution using Semaphores  *}
prensani@13020
    97
prensani@13020
    98
record  Semaphores_mutex =
prensani@13020
    99
 out :: bool
prensani@13020
   100
 who :: nat
prensani@13020
   101
prensani@13020
   102
lemma Semaphores_mutex: 
prensani@13020
   103
 "\<parallel>- .{i\<noteq>j}.  
prensani@13020
   104
  \<acute>out:=True ,,  
prensani@13020
   105
  COBEGIN .{i\<noteq>j}.  
prensani@13020
   106
       WHILE True INV .{i\<noteq>j}.  
prensani@13020
   107
       DO .{i\<noteq>j}. AWAIT \<acute>out THEN  \<acute>out:=False,, \<acute>who:=i END;;  
prensani@13020
   108
          .{\<not>\<acute>out \<and> \<acute>who=i \<and> i\<noteq>j}. \<acute>out:=True OD  
prensani@13020
   109
       .{False}.  
prensani@13020
   110
  \<parallel>  
prensani@13020
   111
       .{i\<noteq>j}.  
prensani@13020
   112
       WHILE True INV .{i\<noteq>j}.  
prensani@13020
   113
       DO .{i\<noteq>j}. AWAIT \<acute>out THEN  \<acute>out:=False,,\<acute>who:=j END;;  
prensani@13020
   114
          .{\<not>\<acute>out \<and> \<acute>who=j \<and> i\<noteq>j}. \<acute>out:=True OD  
prensani@13020
   115
       .{False}.  
prensani@13020
   116
  COEND  
prensani@13020
   117
  .{False}."
prensani@13020
   118
apply oghoare
prensani@13020
   119
--{* 38 vc *}
prensani@13020
   120
apply auto
prensani@13020
   121
done
prensani@13020
   122
prensani@13020
   123
subsubsection {* Peterson's Algorithm III: Parameterized version: *}
prensani@13020
   124
prensani@13020
   125
lemma Semaphores_parameterized_mutex: 
prensani@13020
   126
 "0<n \<Longrightarrow> \<parallel>- .{True}.  
prensani@13020
   127
  \<acute>out:=True ,,  
prensani@13020
   128
 COBEGIN
prensani@13020
   129
  SCHEME [0\<le> i< n]
prensani@13020
   130
    .{True}.  
prensani@13020
   131
     WHILE True INV .{True}.  
prensani@13020
   132
      DO .{True}. AWAIT \<acute>out THEN  \<acute>out:=False,, \<acute>who:=i END;;  
prensani@13020
   133
         .{\<not>\<acute>out \<and> \<acute>who=i}. \<acute>out:=True OD
prensani@13020
   134
    .{False}. 
prensani@13020
   135
 COEND
prensani@13020
   136
  .{False}." 
prensani@13020
   137
apply oghoare
nipkow@13187
   138
--{* 20 vc *}
prensani@13020
   139
apply auto
prensani@13020
   140
done
prensani@13020
   141
prensani@13020
   142
subsubsection{* The Ticket Algorithm *}
prensani@13020
   143
prensani@13020
   144
record Ticket_mutex =
prensani@13020
   145
 num :: nat
prensani@13020
   146
 nextv :: nat
prensani@13020
   147
 turn :: "nat list"
prensani@13020
   148
 index :: nat 
prensani@13020
   149
prensani@13020
   150
lemma Ticket_mutex: 
prensani@13020
   151
 "\<lbrakk> 0<n; I=\<guillemotleft>n=length \<acute>turn \<and> 0<\<acute>nextv \<and> (\<forall>k l. k<n \<and> l<n \<and> k\<noteq>l 
prensani@13020
   152
    \<longrightarrow> \<acute>turn!k < \<acute>num \<and> (\<acute>turn!k =0 \<or> \<acute>turn!k\<noteq>\<acute>turn!l))\<guillemotright> \<rbrakk>
prensani@13020
   153
   \<Longrightarrow> \<parallel>- .{n=length \<acute>turn}.  
prensani@13020
   154
   \<acute>index:= 0,,
prensani@13020
   155
   WHILE \<acute>index < n INV .{n=length \<acute>turn \<and> (\<forall>i<\<acute>index. \<acute>turn!i=0)}. 
prensani@13020
   156
    DO \<acute>turn:= \<acute>turn[\<acute>index:=0],, \<acute>index:=\<acute>index +1 OD,,
prensani@13020
   157
  \<acute>num:=1 ,, \<acute>nextv:=1 ,, 
prensani@13020
   158
 COBEGIN
prensani@13020
   159
  SCHEME [0\<le> i< n]
prensani@13020
   160
    .{\<acute>I}.  
prensani@13020
   161
     WHILE True INV .{\<acute>I}.  
prensani@13020
   162
      DO .{\<acute>I}. \<langle> \<acute>turn :=\<acute>turn[i:=\<acute>num],, \<acute>num:=\<acute>num+1 \<rangle>;;  
prensani@13020
   163
         .{\<acute>I}. WAIT \<acute>turn!i=\<acute>nextv END;;
prensani@13020
   164
         .{\<acute>I \<and> \<acute>turn!i=\<acute>nextv}. \<acute>nextv:=\<acute>nextv+1
prensani@13020
   165
      OD
prensani@13020
   166
    .{False}. 
prensani@13020
   167
 COEND
prensani@13020
   168
  .{False}." 
prensani@13020
   169
apply oghoare
prensani@13020
   170
--{* 35 vc *}
prensani@13020
   171
apply simp_all
nipkow@13187
   172
--{* 21 vc *}
wenzelm@42793
   173
apply(tactic {* ALLGOALS (clarify_tac @{context}) *})
nipkow@13187
   174
--{* 11 vc *}
prensani@13020
   175
apply simp_all
wenzelm@42793
   176
apply(tactic {* ALLGOALS (clarify_tac @{context}) *})
nipkow@16733
   177
--{* 10 subgoals left *}
prensani@13020
   178
apply(erule less_SucE)
prensani@13020
   179
 apply simp
prensani@13020
   180
apply simp
nipkow@16733
   181
--{* 9 subgoals left *}
prensani@13020
   182
apply(case_tac "i=k")
prensani@13020
   183
 apply force
prensani@13020
   184
apply simp
prensani@13020
   185
apply(case_tac "i=l")
prensani@13020
   186
 apply force
prensani@13020
   187
apply force
prensani@13020
   188
--{* 8 subgoals left *}
prensani@13020
   189
prefer 8
prensani@13020
   190
apply force
prensani@13020
   191
apply force
prensani@13020
   192
--{* 6 subgoals left *}
prensani@13020
   193
prefer 6
prensani@13020
   194
apply(erule_tac x=i in allE)
nipkow@44890
   195
apply fastforce
prensani@13020
   196
--{* 5 subgoals left *}
prensani@13020
   197
prefer 5
wenzelm@27095
   198
apply(case_tac [!] "j=k")
nipkow@13187
   199
--{* 10 subgoals left *}
prensani@13020
   200
apply simp_all
berghofe@13601
   201
apply(erule_tac x=k in allE)
prensani@13020
   202
apply force
nipkow@13187
   203
--{* 9 subgoals left *}
prensani@13020
   204
apply(case_tac "j=l")
prensani@13020
   205
 apply simp
prensani@13020
   206
 apply(erule_tac x=k in allE)
prensani@13020
   207
 apply(erule_tac x=k in allE)
prensani@13020
   208
 apply(erule_tac x=l in allE)
prensani@13020
   209
 apply force
prensani@13020
   210
apply(erule_tac x=k in allE)
prensani@13020
   211
apply(erule_tac x=k in allE)
prensani@13020
   212
apply(erule_tac x=l in allE)
prensani@13020
   213
apply force
prensani@13020
   214
--{* 8 subgoals left *}
prensani@13020
   215
apply force
prensani@13020
   216
apply(case_tac "j=l")
prensani@13020
   217
 apply simp
prensani@13020
   218
apply(erule_tac x=k in allE)
prensani@13020
   219
apply(erule_tac x=l in allE)
prensani@13020
   220
apply force
prensani@13020
   221
apply force
prensani@13020
   222
apply force
nipkow@13187
   223
--{* 5 subgoals left *}
prensani@13020
   224
apply(erule_tac x=k in allE)
prensani@13020
   225
apply(erule_tac x=l in allE)
prensani@13020
   226
apply(case_tac "j=l")
prensani@13020
   227
 apply force
prensani@13020
   228
apply force
prensani@13020
   229
apply force
nipkow@13187
   230
--{* 3 subgoals left *}
prensani@13020
   231
apply(erule_tac x=k in allE)
prensani@13020
   232
apply(erule_tac x=l in allE)
prensani@13020
   233
apply(case_tac "j=l")
prensani@13020
   234
 apply force
prensani@13020
   235
apply force
prensani@13020
   236
apply force
nipkow@13187
   237
--{* 1 subgoals left *}
prensani@13020
   238
apply(erule_tac x=k in allE)
prensani@13020
   239
apply(erule_tac x=l in allE)
prensani@13020
   240
apply(case_tac "j=l")
prensani@13020
   241
 apply force
prensani@13020
   242
apply force
prensani@13020
   243
done
prensani@13020
   244
prensani@13020
   245
subsection{* Parallel Zero Search *}
prensani@13020
   246
prensani@13020
   247
text {* Synchronized Zero Search. Zero-6 *}
prensani@13020
   248
prensani@13020
   249
text {*Apt and Olderog. "Verification of sequential and concurrent Programs" page 294: *}
prensani@13020
   250
prensani@13020
   251
record Zero_search =
prensani@13020
   252
   turn :: nat
prensani@13020
   253
   found :: bool
prensani@13020
   254
   x :: nat
prensani@13020
   255
   y :: nat
prensani@13020
   256
prensani@13020
   257
lemma Zero_search: 
prensani@13020
   258
  "\<lbrakk>I1= \<guillemotleft> a\<le>\<acute>x \<and> (\<acute>found \<longrightarrow> (a<\<acute>x \<and> f(\<acute>x)=0) \<or> (\<acute>y\<le>a \<and> f(\<acute>y)=0)) 
prensani@13020
   259
      \<and> (\<not>\<acute>found \<and> a<\<acute> x \<longrightarrow> f(\<acute>x)\<noteq>0) \<guillemotright> ;  
prensani@13020
   260
    I2= \<guillemotleft>\<acute>y\<le>a+1 \<and> (\<acute>found \<longrightarrow> (a<\<acute>x \<and> f(\<acute>x)=0) \<or> (\<acute>y\<le>a \<and> f(\<acute>y)=0)) 
prensani@13020
   261
      \<and> (\<not>\<acute>found \<and> \<acute>y\<le>a \<longrightarrow> f(\<acute>y)\<noteq>0) \<guillemotright> \<rbrakk> \<Longrightarrow>  
prensani@13020
   262
  \<parallel>- .{\<exists> u. f(u)=0}.  
prensani@13020
   263
  \<acute>turn:=1,, \<acute>found:= False,,  
prensani@13020
   264
  \<acute>x:=a,, \<acute>y:=a+1 ,,  
prensani@13020
   265
  COBEGIN .{\<acute>I1}.  
prensani@13020
   266
       WHILE \<not>\<acute>found  
prensani@13020
   267
       INV .{\<acute>I1}.  
prensani@13020
   268
       DO .{a\<le>\<acute>x \<and> (\<acute>found \<longrightarrow> \<acute>y\<le>a \<and> f(\<acute>y)=0) \<and> (a<\<acute>x \<longrightarrow> f(\<acute>x)\<noteq>0)}.  
prensani@13020
   269
          WAIT \<acute>turn=1 END;;  
prensani@13020
   270
          .{a\<le>\<acute>x \<and> (\<acute>found \<longrightarrow> \<acute>y\<le>a \<and> f(\<acute>y)=0) \<and> (a<\<acute>x \<longrightarrow> f(\<acute>x)\<noteq>0)}.  
prensani@13020
   271
          \<acute>turn:=2;;  
prensani@13020
   272
          .{a\<le>\<acute>x \<and> (\<acute>found \<longrightarrow> \<acute>y\<le>a \<and> f(\<acute>y)=0) \<and> (a<\<acute>x \<longrightarrow> f(\<acute>x)\<noteq>0)}.    
prensani@13020
   273
          \<langle> \<acute>x:=\<acute>x+1,,  
prensani@13020
   274
            IF f(\<acute>x)=0 THEN \<acute>found:=True ELSE SKIP FI\<rangle>  
prensani@13020
   275
       OD;;  
prensani@13020
   276
       .{\<acute>I1  \<and> \<acute>found}.  
prensani@13020
   277
       \<acute>turn:=2  
prensani@13020
   278
       .{\<acute>I1 \<and> \<acute>found}.  
prensani@13020
   279
  \<parallel>  
prensani@13020
   280
      .{\<acute>I2}.  
prensani@13020
   281
       WHILE \<not>\<acute>found  
prensani@13020
   282
       INV .{\<acute>I2}.  
prensani@13020
   283
       DO .{\<acute>y\<le>a+1 \<and> (\<acute>found \<longrightarrow> a<\<acute>x \<and> f(\<acute>x)=0) \<and> (\<acute>y\<le>a \<longrightarrow> f(\<acute>y)\<noteq>0)}.  
prensani@13020
   284
          WAIT \<acute>turn=2 END;;  
prensani@13020
   285
          .{\<acute>y\<le>a+1 \<and> (\<acute>found \<longrightarrow> a<\<acute>x \<and> f(\<acute>x)=0) \<and> (\<acute>y\<le>a \<longrightarrow> f(\<acute>y)\<noteq>0)}.  
prensani@13020
   286
          \<acute>turn:=1;;  
prensani@13020
   287
          .{\<acute>y\<le>a+1 \<and> (\<acute>found \<longrightarrow> a<\<acute>x \<and> f(\<acute>x)=0) \<and> (\<acute>y\<le>a \<longrightarrow> f(\<acute>y)\<noteq>0)}.  
prensani@13020
   288
          \<langle> \<acute>y:=(\<acute>y - 1),,  
prensani@13020
   289
            IF f(\<acute>y)=0 THEN \<acute>found:=True ELSE SKIP FI\<rangle>  
prensani@13020
   290
       OD;;  
prensani@13020
   291
       .{\<acute>I2 \<and> \<acute>found}.  
prensani@13020
   292
       \<acute>turn:=1  
prensani@13020
   293
       .{\<acute>I2 \<and> \<acute>found}.  
prensani@13020
   294
  COEND  
prensani@13020
   295
  .{f(\<acute>x)=0 \<or> f(\<acute>y)=0}."
prensani@13020
   296
apply oghoare
prensani@13020
   297
--{* 98 verification conditions *}
prensani@13020
   298
apply auto 
prensani@13020
   299
--{* auto takes about 3 minutes !! *}
prensani@13020
   300
done
prensani@13020
   301
prensani@13020
   302
text {* Easier Version: without AWAIT.  Apt and Olderog. page 256: *}
prensani@13020
   303
prensani@13020
   304
lemma Zero_Search_2: 
prensani@13020
   305
"\<lbrakk>I1=\<guillemotleft> a\<le>\<acute>x \<and> (\<acute>found \<longrightarrow> (a<\<acute>x \<and> f(\<acute>x)=0) \<or> (\<acute>y\<le>a \<and> f(\<acute>y)=0)) 
prensani@13020
   306
    \<and> (\<not>\<acute>found \<and> a<\<acute>x \<longrightarrow> f(\<acute>x)\<noteq>0)\<guillemotright>;  
prensani@13020
   307
 I2= \<guillemotleft>\<acute>y\<le>a+1 \<and> (\<acute>found \<longrightarrow> (a<\<acute>x \<and> f(\<acute>x)=0) \<or> (\<acute>y\<le>a \<and> f(\<acute>y)=0)) 
prensani@13020
   308
    \<and> (\<not>\<acute>found \<and> \<acute>y\<le>a \<longrightarrow> f(\<acute>y)\<noteq>0)\<guillemotright>\<rbrakk> \<Longrightarrow>  
prensani@13020
   309
  \<parallel>- .{\<exists>u. f(u)=0}.  
prensani@13020
   310
  \<acute>found:= False,,  
prensani@13020
   311
  \<acute>x:=a,, \<acute>y:=a+1,,  
prensani@13020
   312
  COBEGIN .{\<acute>I1}.  
prensani@13020
   313
       WHILE \<not>\<acute>found  
prensani@13020
   314
       INV .{\<acute>I1}.  
prensani@13020
   315
       DO .{a\<le>\<acute>x \<and> (\<acute>found \<longrightarrow> \<acute>y\<le>a \<and> f(\<acute>y)=0) \<and> (a<\<acute>x \<longrightarrow> f(\<acute>x)\<noteq>0)}.  
prensani@13020
   316
          \<langle> \<acute>x:=\<acute>x+1,,IF f(\<acute>x)=0 THEN  \<acute>found:=True ELSE  SKIP FI\<rangle>  
prensani@13020
   317
       OD  
prensani@13020
   318
       .{\<acute>I1 \<and> \<acute>found}.  
prensani@13020
   319
  \<parallel>  
prensani@13020
   320
      .{\<acute>I2}.  
prensani@13020
   321
       WHILE \<not>\<acute>found  
prensani@13020
   322
       INV .{\<acute>I2}.  
prensani@13020
   323
       DO .{\<acute>y\<le>a+1 \<and> (\<acute>found \<longrightarrow> a<\<acute>x \<and> f(\<acute>x)=0) \<and> (\<acute>y\<le>a \<longrightarrow> f(\<acute>y)\<noteq>0)}.  
prensani@13020
   324
          \<langle> \<acute>y:=(\<acute>y - 1),,IF f(\<acute>y)=0 THEN  \<acute>found:=True ELSE  SKIP FI\<rangle>  
prensani@13020
   325
       OD  
prensani@13020
   326
       .{\<acute>I2 \<and> \<acute>found}.  
prensani@13020
   327
  COEND  
prensani@13020
   328
  .{f(\<acute>x)=0 \<or> f(\<acute>y)=0}."
prensani@13020
   329
apply oghoare
prensani@13020
   330
--{* 20 vc *}
prensani@13020
   331
apply auto
prensani@13020
   332
--{* auto takes aprox. 2 minutes. *}
prensani@13020
   333
done
prensani@13020
   334
prensani@13020
   335
subsection {* Producer/Consumer *}
prensani@13020
   336
prensani@13020
   337
subsubsection {* Previous lemmas *}
prensani@13020
   338
nipkow@13517
   339
lemma nat_lemma2: "\<lbrakk> b = m*(n::nat) + t; a = s*n + u; t=u; b-a < n \<rbrakk> \<Longrightarrow> m \<le> s"
prensani@13020
   340
proof -
nipkow@13517
   341
  assume "b = m*(n::nat) + t" "a = s*n + u" "t=u"
prensani@13020
   342
  hence "(m - s) * n = b - a" by (simp add: diff_mult_distrib)
prensani@13020
   343
  also assume "\<dots> < n"
prensani@13020
   344
  finally have "m - s < 1" by simp
prensani@13020
   345
  thus ?thesis by arith
prensani@13020
   346
qed
prensani@13020
   347
prensani@13020
   348
lemma mod_lemma: "\<lbrakk> (c::nat) \<le> a; a < b; b - c < n \<rbrakk> \<Longrightarrow> b mod n \<noteq> a mod n"
prensani@13020
   349
apply(subgoal_tac "b=b div n*n + b mod n" )
prensani@13020
   350
 prefer 2  apply (simp add: mod_div_equality [symmetric])
prensani@13020
   351
apply(subgoal_tac "a=a div n*n + a mod n")
prensani@13020
   352
 prefer 2
prensani@13020
   353
 apply(simp add: mod_div_equality [symmetric])
nipkow@13517
   354
apply(subgoal_tac "b - a \<le> b - c")
nipkow@13517
   355
 prefer 2 apply arith
prensani@13020
   356
apply(drule le_less_trans)
prensani@13020
   357
back
prensani@13020
   358
 apply assumption
prensani@13020
   359
apply(frule less_not_refl2)
prensani@13020
   360
apply(drule less_imp_le)
nipkow@13517
   361
apply (drule_tac m = "a" and k = n in div_le_mono)
prensani@13020
   362
apply(safe)
nipkow@13517
   363
apply(frule_tac b = "b" and a = "a" and n = "n" in nat_lemma2, assumption, assumption)
prensani@13020
   364
apply assumption
nipkow@13517
   365
apply(drule order_antisym, assumption)
nipkow@13517
   366
apply(rotate_tac -3)
prensani@13020
   367
apply(simp)
prensani@13020
   368
done
prensani@13020
   369
nipkow@13517
   370
prensani@13020
   371
subsubsection {* Producer/Consumer Algorithm *}
prensani@13020
   372
prensani@13020
   373
record Producer_consumer =
prensani@13020
   374
  ins :: nat
prensani@13020
   375
  outs :: nat
prensani@13020
   376
  li :: nat
prensani@13020
   377
  lj :: nat
prensani@13020
   378
  vx :: nat
prensani@13020
   379
  vy :: nat
prensani@13020
   380
  buffer :: "nat list"
prensani@13020
   381
  b :: "nat list"
prensani@13020
   382
prensani@13020
   383
text {* The whole proof takes aprox. 4 minutes. *}
prensani@13020
   384
prensani@13020
   385
lemma Producer_consumer: 
prensani@13020
   386
  "\<lbrakk>INIT= \<guillemotleft>0<length a \<and> 0<length \<acute>buffer \<and> length \<acute>b=length a\<guillemotright> ;  
prensani@13020
   387
    I= \<guillemotleft>(\<forall>k<\<acute>ins. \<acute>outs\<le>k \<longrightarrow> (a ! k) = \<acute>buffer ! (k mod (length \<acute>buffer))) \<and>  
prensani@13020
   388
            \<acute>outs\<le>\<acute>ins \<and> \<acute>ins-\<acute>outs\<le>length \<acute>buffer\<guillemotright> ;  
prensani@13020
   389
    I1= \<guillemotleft>\<acute>I \<and> \<acute>li\<le>length a\<guillemotright> ;  
prensani@13020
   390
    p1= \<guillemotleft>\<acute>I1 \<and> \<acute>li=\<acute>ins\<guillemotright> ;  
prensani@13020
   391
    I2 = \<guillemotleft>\<acute>I \<and> (\<forall>k<\<acute>lj. (a ! k)=(\<acute>b ! k)) \<and> \<acute>lj\<le>length a\<guillemotright> ;
prensani@13020
   392
    p2 = \<guillemotleft>\<acute>I2 \<and> \<acute>lj=\<acute>outs\<guillemotright> \<rbrakk> \<Longrightarrow>   
prensani@13020
   393
  \<parallel>- .{\<acute>INIT}.  
prensani@13020
   394
 \<acute>ins:=0,, \<acute>outs:=0,, \<acute>li:=0,, \<acute>lj:=0,,
prensani@13020
   395
 COBEGIN .{\<acute>p1 \<and> \<acute>INIT}. 
prensani@13020
   396
   WHILE \<acute>li <length a 
prensani@13020
   397
     INV .{\<acute>p1 \<and> \<acute>INIT}.   
prensani@13020
   398
   DO .{\<acute>p1 \<and> \<acute>INIT \<and> \<acute>li<length a}.  
prensani@13020
   399
       \<acute>vx:= (a ! \<acute>li);;  
prensani@13020
   400
      .{\<acute>p1 \<and> \<acute>INIT \<and> \<acute>li<length a \<and> \<acute>vx=(a ! \<acute>li)}. 
prensani@13020
   401
        WAIT \<acute>ins-\<acute>outs < length \<acute>buffer END;; 
prensani@13020
   402
      .{\<acute>p1 \<and> \<acute>INIT \<and> \<acute>li<length a \<and> \<acute>vx=(a ! \<acute>li) 
prensani@13020
   403
         \<and> \<acute>ins-\<acute>outs < length \<acute>buffer}. 
prensani@13020
   404
       \<acute>buffer:=(list_update \<acute>buffer (\<acute>ins mod (length \<acute>buffer)) \<acute>vx);; 
prensani@13020
   405
      .{\<acute>p1 \<and> \<acute>INIT \<and> \<acute>li<length a 
prensani@13020
   406
         \<and> (a ! \<acute>li)=(\<acute>buffer ! (\<acute>ins mod (length \<acute>buffer))) 
prensani@13020
   407
         \<and> \<acute>ins-\<acute>outs <length \<acute>buffer}.  
prensani@13020
   408
       \<acute>ins:=\<acute>ins+1;; 
prensani@13020
   409
      .{\<acute>I1 \<and> \<acute>INIT \<and> (\<acute>li+1)=\<acute>ins \<and> \<acute>li<length a}.  
prensani@13020
   410
       \<acute>li:=\<acute>li+1  
prensani@13020
   411
   OD  
prensani@13020
   412
  .{\<acute>p1 \<and> \<acute>INIT \<and> \<acute>li=length a}.  
prensani@13020
   413
  \<parallel>  
prensani@13020
   414
  .{\<acute>p2 \<and> \<acute>INIT}.  
prensani@13020
   415
   WHILE \<acute>lj < length a  
prensani@13020
   416
     INV .{\<acute>p2 \<and> \<acute>INIT}.  
prensani@13020
   417
   DO .{\<acute>p2 \<and> \<acute>lj<length a \<and> \<acute>INIT}.  
prensani@13020
   418
        WAIT \<acute>outs<\<acute>ins END;; 
prensani@13020
   419
      .{\<acute>p2 \<and> \<acute>lj<length a \<and> \<acute>outs<\<acute>ins \<and> \<acute>INIT}.  
prensani@13020
   420
       \<acute>vy:=(\<acute>buffer ! (\<acute>outs mod (length \<acute>buffer)));; 
prensani@13020
   421
      .{\<acute>p2 \<and> \<acute>lj<length a \<and> \<acute>outs<\<acute>ins \<and> \<acute>vy=(a ! \<acute>lj) \<and> \<acute>INIT}.  
prensani@13020
   422
       \<acute>outs:=\<acute>outs+1;;  
prensani@13020
   423
      .{\<acute>I2 \<and> (\<acute>lj+1)=\<acute>outs \<and> \<acute>lj<length a \<and> \<acute>vy=(a ! \<acute>lj) \<and> \<acute>INIT}.  
prensani@13020
   424
       \<acute>b:=(list_update \<acute>b \<acute>lj \<acute>vy);; 
prensani@13020
   425
      .{\<acute>I2 \<and> (\<acute>lj+1)=\<acute>outs \<and> \<acute>lj<length a \<and> (a ! \<acute>lj)=(\<acute>b ! \<acute>lj) \<and> \<acute>INIT}.  
prensani@13020
   426
       \<acute>lj:=\<acute>lj+1  
prensani@13020
   427
   OD  
prensani@13020
   428
  .{\<acute>p2 \<and> \<acute>lj=length a \<and> \<acute>INIT}.  
prensani@13020
   429
 COEND  
prensani@13020
   430
 .{ \<forall>k<length a. (a ! k)=(\<acute>b ! k)}."
prensani@13020
   431
apply oghoare
prensani@13020
   432
--{* 138 vc  *}
wenzelm@42793
   433
apply(tactic {* ALLGOALS (clarify_tac @{context}) *})
prensani@13020
   434
--{* 112 subgoals left *}
prensani@13020
   435
apply(simp_all (no_asm))
nipkow@34233
   436
--{* 97 subgoals left *}
prensani@13020
   437
apply(tactic {*ALLGOALS (conjI_Tac (K all_tac)) *})
berghofe@13601
   438
--{* 930 subgoals left *}
wenzelm@42793
   439
apply(tactic {* ALLGOALS (clarify_tac @{context}) *})
nipkow@34233
   440
--{* 99 subgoals left *}
nipkow@34233
   441
apply(simp_all (*asm_lr*) only:length_0_conv [THEN sym])
nipkow@34233
   442
--{* 20 subgoals left *}
nipkow@34233
   443
apply (simp_all (*asm_lr*) del:length_0_conv length_greater_0_conv add: nth_list_update mod_lemma)
nipkow@16733
   444
--{* 9 subgoals left *}
prensani@13020
   445
apply (force simp add:less_Suc_eq)
prensani@13020
   446
apply(drule sym)
prensani@13020
   447
apply (force simp add:less_Suc_eq)+
prensani@13020
   448
done
prensani@13020
   449
prensani@13020
   450
subsection {* Parameterized Examples *}
prensani@13020
   451
prensani@13020
   452
subsubsection {* Set Elements of an Array to Zero *}
prensani@13020
   453
prensani@13022
   454
record Example1 =
prensani@13020
   455
  a :: "nat \<Rightarrow> nat"
prensani@13022
   456
prensani@13022
   457
lemma Example1: 
prensani@13020
   458
 "\<parallel>- .{True}.
prensani@13020
   459
   COBEGIN SCHEME [0\<le>i<n] .{True}. \<acute>a:=\<acute>a (i:=0) .{\<acute>a i=0}. COEND 
prensani@13020
   460
  .{\<forall>i < n. \<acute>a i = 0}."
prensani@13020
   461
apply oghoare
prensani@13020
   462
apply simp_all
prensani@13020
   463
done
prensani@13020
   464
prensani@13020
   465
text {* Same example with lists as auxiliary variables. *}
prensani@13022
   466
record Example1_list =
prensani@13022
   467
  A :: "nat list"
prensani@13022
   468
lemma Example1_list: 
prensani@13022
   469
 "\<parallel>- .{n < length \<acute>A}. 
prensani@13020
   470
   COBEGIN 
prensani@13022
   471
     SCHEME [0\<le>i<n] .{n < length \<acute>A}. \<acute>A:=\<acute>A[i:=0] .{\<acute>A!i=0}. 
prensani@13020
   472
   COEND 
prensani@13022
   473
    .{\<forall>i < n. \<acute>A!i = 0}."
prensani@13020
   474
apply oghoare
nipkow@13187
   475
apply force+
prensani@13020
   476
done
prensani@13020
   477
prensani@13020
   478
subsubsection {* Increment a Variable in Parallel *}
prensani@13020
   479
nipkow@15561
   480
text {* First some lemmas about summation properties. *}
nipkow@15561
   481
(*
nipkow@15043
   482
lemma Example2_lemma1: "!!b. j<n \<Longrightarrow> (\<Sum>i::nat<n. b i) = (0::nat) \<Longrightarrow> b j = 0 "
prensani@13020
   483
apply(induct n)
prensani@13020
   484
 apply simp_all
prensani@13020
   485
apply(force simp add: less_Suc_eq)
prensani@13020
   486
done
nipkow@15561
   487
*)
prensani@13022
   488
lemma Example2_lemma2_aux: "!!b. j<n \<Longrightarrow> 
nipkow@15561
   489
 (\<Sum>i=0..<n. (b i::nat)) =
nipkow@15561
   490
 (\<Sum>i=0..<j. b i) + b j + (\<Sum>i=0..<n-(Suc j) . b (Suc j + i))"
prensani@13020
   491
apply(induct n)
prensani@13020
   492
 apply simp_all
prensani@13020
   493
apply(simp add:less_Suc_eq)
prensani@13020
   494
 apply(auto)
prensani@13020
   495
apply(subgoal_tac "n - j = Suc(n- Suc j)")
prensani@13020
   496
  apply simp
prensani@13020
   497
apply arith
nipkow@13187
   498
done
prensani@13020
   499
prensani@13022
   500
lemma Example2_lemma2_aux2: 
nipkow@15561
   501
  "!!b. j\<le> s \<Longrightarrow> (\<Sum>i::nat=0..<j. (b (s:=t)) i) = (\<Sum>i=0..<j. b i)"
wenzelm@27095
   502
apply(induct j) 
wenzelm@27095
   503
 apply simp_all
prensani@13020
   504
done
prensani@13020
   505
prensani@13022
   506
lemma Example2_lemma2: 
nipkow@15561
   507
 "!!b. \<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)"
prensani@13022
   508
apply(frule_tac b="(b (j:=(Suc 0)))" in Example2_lemma2_aux)
nipkow@15561
   509
apply(erule_tac  t="setsum (b(j := (Suc 0))) {0..<n}" in ssubst)
prensani@13022
   510
apply(frule_tac b=b in Example2_lemma2_aux)
nipkow@15561
   511
apply(erule_tac  t="setsum b {0..<n}" in ssubst)
nipkow@15561
   512
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)))")
prensani@13020
   513
apply(rotate_tac -1)
prensani@13020
   514
apply(erule ssubst)
prensani@13020
   515
apply(subgoal_tac "j\<le>j")
prensani@13022
   516
 apply(drule_tac b="b" and t="(Suc 0)" in Example2_lemma2_aux2)
prensani@13020
   517
apply(rotate_tac -1)
prensani@13020
   518
apply(erule ssubst)
prensani@13020
   519
apply simp_all
prensani@13020
   520
done
prensani@13020
   521
prensani@13020
   522
prensani@13022
   523
record Example2 = 
prensani@13020
   524
 c :: "nat \<Rightarrow> nat" 
prensani@13020
   525
 x :: nat
prensani@13022
   526
prensani@13022
   527
lemma Example_2: "0<n \<Longrightarrow> 
nipkow@15561
   528
 \<parallel>- .{\<acute>x=0 \<and> (\<Sum>i=0..<n. \<acute>c i)=0}.  
prensani@13020
   529
 COBEGIN 
prensani@13020
   530
   SCHEME [0\<le>i<n] 
nipkow@15561
   531
  .{\<acute>x=(\<Sum>i=0..<n. \<acute>c i) \<and> \<acute>c i=0}. 
prensani@13020
   532
   \<langle> \<acute>x:=\<acute>x+(Suc 0),, \<acute>c:=\<acute>c (i:=(Suc 0)) \<rangle>
nipkow@15561
   533
  .{\<acute>x=(\<Sum>i=0..<n. \<acute>c i) \<and> \<acute>c i=(Suc 0)}.
prensani@13020
   534
 COEND 
prensani@13020
   535
 .{\<acute>x=n}."
prensani@13020
   536
apply oghoare
nipkow@16733
   537
apply (simp_all cong del: strong_setsum_cong)
wenzelm@42793
   538
apply (tactic {* ALLGOALS (clarify_tac @{context}) *})
nipkow@16733
   539
apply (simp_all cong del: strong_setsum_cong)
nipkow@16733
   540
   apply(erule (1) Example2_lemma2)
nipkow@16733
   541
  apply(erule (1) Example2_lemma2)
nipkow@16733
   542
 apply(erule (1) Example2_lemma2)
nipkow@16733
   543
apply(simp)
prensani@13020
   544
done
prensani@13020
   545
nipkow@13187
   546
end