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