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