Theory OG_Examples

theory OG_Examples
imports OG_Syntax
header {* \section{Examples} *}

theory OG_Examples imports OG_Syntax begin

subsection {* Mutual Exclusion *}

subsubsection {* Peterson's Algorithm I*}

text {* Eike Best. "Semantics of Sequential and Parallel Programs", page 217. *}

record Petersons_mutex_1 =
 pr1 :: nat
 pr2 :: nat
 in1 :: bool
 in2 :: bool 
 hold :: nat

lemma Petersons_mutex_1: 
  "\<parallel>- \<lbrace>´pr1=0 ∧ ¬´in1 ∧ ´pr2=0 ∧ ¬´in2 \<rbrace>  
  COBEGIN \<lbrace>´pr1=0 ∧ ¬´in1\<rbrace>  
  WHILE True INV \<lbrace>´pr1=0 ∧ ¬´in1\<rbrace>  
  DO  
  \<lbrace>´pr1=0 ∧ ¬´in1\<rbrace> ⟨ ´in1:=True,,´pr1:=1 ⟩;;  
  \<lbrace>´pr1=1 ∧ ´in1\<rbrace>  ⟨ ´hold:=1,,´pr1:=2 ⟩;;  
  \<lbrace>´pr1=2 ∧ ´in1 ∧ (´hold=1 ∨ ´hold=2 ∧ ´pr2=2)\<rbrace>  
  AWAIT (¬´in2 ∨ ¬(´hold=1)) THEN ´pr1:=3 END;;    
  \<lbrace>´pr1=3 ∧ ´in1 ∧ (´hold=1 ∨ ´hold=2 ∧ ´pr2=2)\<rbrace> 
   ⟨´in1:=False,,´pr1:=0⟩ 
  OD \<lbrace>´pr1=0 ∧ ¬´in1\<rbrace>  
  \<parallel>  
  \<lbrace>´pr2=0 ∧ ¬´in2\<rbrace>  
  WHILE True INV \<lbrace>´pr2=0 ∧ ¬´in2\<rbrace>  
  DO  
  \<lbrace>´pr2=0 ∧ ¬´in2\<rbrace> ⟨ ´in2:=True,,´pr2:=1 ⟩;;  
  \<lbrace>´pr2=1 ∧ ´in2\<rbrace> ⟨  ´hold:=2,,´pr2:=2 ⟩;;  
  \<lbrace>´pr2=2 ∧ ´in2 ∧ (´hold=2 ∨ (´hold=1 ∧ ´pr1=2))\<rbrace>  
  AWAIT (¬´in1 ∨ ¬(´hold=2)) THEN ´pr2:=3  END;;    
  \<lbrace>´pr2=3 ∧ ´in2 ∧ (´hold=2 ∨ (´hold=1 ∧ ´pr1=2))\<rbrace> 
    ⟨´in2:=False,,´pr2:=0⟩ 
  OD \<lbrace>´pr2=0 ∧ ¬´in2\<rbrace>  
  COEND  
  \<lbrace>´pr1=0 ∧ ¬´in1 ∧ ´pr2=0 ∧ ¬´in2\<rbrace>"
apply oghoare
--{* 104 verification conditions. *}
apply auto
done

subsubsection {*Peterson's Algorithm II: A Busy Wait Solution *}
 
text {* Apt and Olderog. "Verification of sequential and concurrent Programs", page 282. *}

record Busy_wait_mutex =
 flag1 :: bool
 flag2 :: bool
 turn  :: nat
 after1 :: bool 
 after2 :: bool

lemma Busy_wait_mutex: 
 "\<parallel>-  \<lbrace>True\<rbrace>  
  ´flag1:=False,, ´flag2:=False,,  
  COBEGIN \<lbrace>¬´flag1\<rbrace>  
        WHILE True  
        INV \<lbrace>¬´flag1\<rbrace>  
        DO \<lbrace>¬´flag1\<rbrace> ⟨ ´flag1:=True,,´after1:=False ⟩;;  
           \<lbrace>´flag1 ∧ ¬´after1\<rbrace> ⟨ ´turn:=1,,´after1:=True ⟩;;  
           \<lbrace>´flag1 ∧ ´after1 ∧ (´turn=1 ∨ ´turn=2)\<rbrace>  
            WHILE ¬(´flag2 --> ´turn=2)  
            INV \<lbrace>´flag1 ∧ ´after1 ∧ (´turn=1 ∨ ´turn=2)\<rbrace>  
            DO \<lbrace>´flag1 ∧ ´after1 ∧ (´turn=1 ∨ ´turn=2)\<rbrace> SKIP OD;; 
           \<lbrace>´flag1 ∧ ´after1 ∧ (´flag2 ∧ ´after2 --> ´turn=2)\<rbrace>
            ´flag1:=False  
        OD  
       \<lbrace>False\<rbrace>  
  \<parallel>  
     \<lbrace>¬´flag2\<rbrace>  
        WHILE True  
        INV \<lbrace>¬´flag2\<rbrace>  
        DO \<lbrace>¬´flag2\<rbrace> ⟨ ´flag2:=True,,´after2:=False ⟩;;  
           \<lbrace>´flag2 ∧ ¬´after2\<rbrace> ⟨ ´turn:=2,,´after2:=True ⟩;;  
           \<lbrace>´flag2 ∧ ´after2 ∧ (´turn=1 ∨ ´turn=2)\<rbrace>  
            WHILE ¬(´flag1 --> ´turn=1)  
            INV \<lbrace>´flag2 ∧ ´after2 ∧ (´turn=1 ∨ ´turn=2)\<rbrace>  
            DO \<lbrace>´flag2 ∧ ´after2 ∧ (´turn=1 ∨ ´turn=2)\<rbrace> SKIP OD;;  
           \<lbrace>´flag2 ∧ ´after2 ∧ (´flag1 ∧ ´after1 --> ´turn=1)\<rbrace> 
            ´flag2:=False  
        OD  
       \<lbrace>False\<rbrace>  
  COEND  
  \<lbrace>False\<rbrace>"
apply oghoare
--{* 122 vc *}
apply auto
done

subsubsection {* Peterson's Algorithm III: A Solution using Semaphores  *}

record  Semaphores_mutex =
 out :: bool
 who :: nat

lemma Semaphores_mutex: 
 "\<parallel>- \<lbrace>i≠j\<rbrace>  
  ´out:=True ,,  
  COBEGIN \<lbrace>i≠j\<rbrace>  
       WHILE True INV \<lbrace>i≠j\<rbrace>  
       DO \<lbrace>i≠j\<rbrace> AWAIT ´out THEN  ´out:=False,, ´who:=i END;;  
          \<lbrace>¬´out ∧ ´who=i ∧ i≠j\<rbrace> ´out:=True OD  
       \<lbrace>False\<rbrace>  
  \<parallel>  
       \<lbrace>i≠j\<rbrace>  
       WHILE True INV \<lbrace>i≠j\<rbrace>  
       DO \<lbrace>i≠j\<rbrace> AWAIT ´out THEN  ´out:=False,,´who:=j END;;  
          \<lbrace>¬´out ∧ ´who=j ∧ i≠j\<rbrace> ´out:=True OD  
       \<lbrace>False\<rbrace>  
  COEND  
  \<lbrace>False\<rbrace>"
apply oghoare
--{* 38 vc *}
apply auto
done

subsubsection {* Peterson's Algorithm III: Parameterized version: *}

lemma Semaphores_parameterized_mutex: 
 "0<n ==> \<parallel>- \<lbrace>True\<rbrace>  
  ´out:=True ,,  
 COBEGIN
  SCHEME [0≤ i< n]
    \<lbrace>True\<rbrace>  
     WHILE True INV \<lbrace>True\<rbrace>  
      DO \<lbrace>True\<rbrace> AWAIT ´out THEN  ´out:=False,, ´who:=i END;;  
         \<lbrace>¬´out ∧ ´who=i\<rbrace> ´out:=True OD
    \<lbrace>False\<rbrace> 
 COEND
  \<lbrace>False\<rbrace>" 
apply oghoare
--{* 20 vc *}
apply auto
done

subsubsection{* The Ticket Algorithm *}

record Ticket_mutex =
 num :: nat
 nextv :: nat
 turn :: "nat list"
 index :: nat 

lemma Ticket_mutex: 
 "[| 0<n; I=«n=length ´turn ∧ 0<´nextv ∧ (∀k l. k<n ∧ l<n ∧ k≠l 
    --> ´turn!k < ´num ∧ (´turn!k =0 ∨ ´turn!k≠´turn!l))» |]
   ==> \<parallel>- \<lbrace>n=length ´turn\<rbrace>  
   ´index:= 0,,
   WHILE ´index < n INV \<lbrace>n=length ´turn ∧ (∀i<´index. ´turn!i=0)\<rbrace> 
    DO ´turn:= ´turn[´index:=0],, ´index:=´index +1 OD,,
  ´num:=1 ,, ´nextv:=1 ,, 
 COBEGIN
  SCHEME [0≤ i< n]
    \<lbrace>´I\<rbrace>  
     WHILE True INV \<lbrace>´I\<rbrace>  
      DO \<lbrace>´I\<rbrace> ⟨ ´turn :=´turn[i:=´num],, ´num:=´num+1 ⟩;;  
         \<lbrace>´I\<rbrace> WAIT ´turn!i=´nextv END;;
         \<lbrace>´I ∧ ´turn!i=´nextv\<rbrace> ´nextv:=´nextv+1
      OD
    \<lbrace>False\<rbrace> 
 COEND
  \<lbrace>False\<rbrace>" 
apply oghoare
--{* 35 vc *}
apply simp_all
--{* 21 vc *}
apply(tactic {* ALLGOALS (clarify_tac @{context}) *})
--{* 11 vc *}
apply simp_all
apply(tactic {* ALLGOALS (clarify_tac @{context}) *})
--{* 10 subgoals left *}
apply(erule less_SucE)
 apply simp
apply simp
--{* 9 subgoals left *}
apply(case_tac "i=k")
 apply force
apply simp
apply(case_tac "i=l")
 apply force
apply force
--{* 8 subgoals left *}
prefer 8
apply force
apply force
--{* 6 subgoals left *}
prefer 6
apply(erule_tac x=i in allE)
apply fastforce
--{* 5 subgoals left *}
prefer 5
apply(case_tac [!] "j=k")
--{* 10 subgoals left *}
apply simp_all
apply(erule_tac x=k in allE)
apply force
--{* 9 subgoals left *}
apply(case_tac "j=l")
 apply simp
 apply(erule_tac x=k in allE)
 apply(erule_tac x=k in allE)
 apply(erule_tac x=l in allE)
 apply force
apply(erule_tac x=k in allE)
apply(erule_tac x=k in allE)
apply(erule_tac x=l in allE)
apply force
--{* 8 subgoals left *}
apply force
apply(case_tac "j=l")
 apply simp
apply(erule_tac x=k in allE)
apply(erule_tac x=l in allE)
apply force
apply force
apply force
--{* 5 subgoals left *}
apply(erule_tac x=k in allE)
apply(erule_tac x=l in allE)
apply(case_tac "j=l")
 apply force
apply force
apply force
--{* 3 subgoals left *}
apply(erule_tac x=k in allE)
apply(erule_tac x=l in allE)
apply(case_tac "j=l")
 apply force
apply force
apply force
--{* 1 subgoals left *}
apply(erule_tac x=k in allE)
apply(erule_tac x=l in allE)
apply(case_tac "j=l")
 apply force
apply force
done

subsection{* Parallel Zero Search *}

text {* Synchronized Zero Search. Zero-6 *}

text {*Apt and Olderog. "Verification of sequential and concurrent Programs" page 294: *}

record Zero_search =
   turn :: nat
   found :: bool
   x :: nat
   y :: nat

lemma Zero_search: 
  "[|I1= « a≤´x ∧ (´found --> (a<´x ∧ f(´x)=0) ∨ (´y≤a ∧ f(´y)=0)) 
      ∧ (¬´found ∧ a<´ x --> f(´x)≠0) » ;  
    I2= «´y≤a+1 ∧ (´found --> (a<´x ∧ f(´x)=0) ∨ (´y≤a ∧ f(´y)=0)) 
      ∧ (¬´found ∧ ´y≤a --> f(´y)≠0) » |] ==>  
  \<parallel>- \<lbrace>∃ u. f(u)=0\<rbrace>  
  ´turn:=1,, ´found:= False,,  
  ´x:=a,, ´y:=a+1 ,,  
  COBEGIN \<lbrace>´I1\<rbrace>  
       WHILE ¬´found  
       INV \<lbrace>´I1\<rbrace>  
       DO \<lbrace>a≤´x ∧ (´found --> ´y≤a ∧ f(´y)=0) ∧ (a<´x --> f(´x)≠0)\<rbrace>  
          WAIT ´turn=1 END;;  
          \<lbrace>a≤´x ∧ (´found --> ´y≤a ∧ f(´y)=0) ∧ (a<´x --> f(´x)≠0)\<rbrace>  
          ´turn:=2;;  
          \<lbrace>a≤´x ∧ (´found --> ´y≤a ∧ f(´y)=0) ∧ (a<´x --> f(´x)≠0)\<rbrace>    
          ⟨ ´x:=´x+1,,  
            IF f(´x)=0 THEN ´found:=True ELSE SKIP FI⟩  
       OD;;  
       \<lbrace>´I1  ∧ ´found\<rbrace>  
       ´turn:=2  
       \<lbrace>´I1 ∧ ´found\<rbrace>  
  \<parallel>  
      \<lbrace>´I2\<rbrace>  
       WHILE ¬´found  
       INV \<lbrace>´I2\<rbrace>  
       DO \<lbrace>´y≤a+1 ∧ (´found --> a<´x ∧ f(´x)=0) ∧ (´y≤a --> f(´y)≠0)\<rbrace>  
          WAIT ´turn=2 END;;  
          \<lbrace>´y≤a+1 ∧ (´found --> a<´x ∧ f(´x)=0) ∧ (´y≤a --> f(´y)≠0)\<rbrace>  
          ´turn:=1;;  
          \<lbrace>´y≤a+1 ∧ (´found --> a<´x ∧ f(´x)=0) ∧ (´y≤a --> f(´y)≠0)\<rbrace>  
          ⟨ ´y:=(´y - 1),,  
            IF f(´y)=0 THEN ´found:=True ELSE SKIP FI⟩  
       OD;;  
       \<lbrace>´I2 ∧ ´found\<rbrace>  
       ´turn:=1  
       \<lbrace>´I2 ∧ ´found\<rbrace>  
  COEND  
  \<lbrace>f(´x)=0 ∨ f(´y)=0\<rbrace>"
apply oghoare
--{* 98 verification conditions *}
apply auto 
--{* auto takes about 3 minutes !! *}
done

text {* Easier Version: without AWAIT.  Apt and Olderog. page 256: *}

lemma Zero_Search_2: 
"[|I1=« a≤´x ∧ (´found --> (a<´x ∧ f(´x)=0) ∨ (´y≤a ∧ f(´y)=0)) 
    ∧ (¬´found ∧ a<´x --> f(´x)≠0)»;  
 I2= «´y≤a+1 ∧ (´found --> (a<´x ∧ f(´x)=0) ∨ (´y≤a ∧ f(´y)=0)) 
    ∧ (¬´found ∧ ´y≤a --> f(´y)≠0)»|] ==>  
  \<parallel>- \<lbrace>∃u. f(u)=0\<rbrace>  
  ´found:= False,,  
  ´x:=a,, ´y:=a+1,,  
  COBEGIN \<lbrace>´I1\<rbrace>  
       WHILE ¬´found  
       INV \<lbrace>´I1\<rbrace>  
       DO \<lbrace>a≤´x ∧ (´found --> ´y≤a ∧ f(´y)=0) ∧ (a<´x --> f(´x)≠0)\<rbrace>  
          ⟨ ´x:=´x+1,,IF f(´x)=0 THEN  ´found:=True ELSE  SKIP FI⟩  
       OD  
       \<lbrace>´I1 ∧ ´found\<rbrace>  
  \<parallel>  
      \<lbrace>´I2\<rbrace>  
       WHILE ¬´found  
       INV \<lbrace>´I2\<rbrace>  
       DO \<lbrace>´y≤a+1 ∧ (´found --> a<´x ∧ f(´x)=0) ∧ (´y≤a --> f(´y)≠0)\<rbrace>  
          ⟨ ´y:=(´y - 1),,IF f(´y)=0 THEN  ´found:=True ELSE  SKIP FI⟩  
       OD  
       \<lbrace>´I2 ∧ ´found\<rbrace>  
  COEND  
  \<lbrace>f(´x)=0 ∨ f(´y)=0\<rbrace>"
apply oghoare
--{* 20 vc *}
apply auto
--{* auto takes aprox. 2 minutes. *}
done

subsection {* Producer/Consumer *}

subsubsection {* Previous lemmas *}

lemma nat_lemma2: "[| b = m*(n::nat) + t; a = s*n + u; t=u; b-a < n |] ==> m ≤ s"
proof -
  assume "b = m*(n::nat) + t" "a = s*n + u" "t=u"
  hence "(m - s) * n = b - a" by (simp add: diff_mult_distrib)
  also assume "… < n"
  finally have "m - s < 1" by simp
  thus ?thesis by arith
qed

lemma mod_lemma: "[| (c::nat) ≤ a; a < b; b - c < n |] ==> b mod n ≠ a mod n"
apply(subgoal_tac "b=b div n*n + b mod n" )
 prefer 2  apply (simp add: mod_div_equality [symmetric])
apply(subgoal_tac "a=a div n*n + a mod n")
 prefer 2
 apply(simp add: mod_div_equality [symmetric])
apply(subgoal_tac "b - a ≤ b - c")
 prefer 2 apply arith
apply(drule le_less_trans)
back
 apply assumption
apply(frule less_not_refl2)
apply(drule less_imp_le)
apply (drule_tac m = "a" and k = n in div_le_mono)
apply(safe)
apply(frule_tac b = "b" and a = "a" and n = "n" in nat_lemma2, assumption, assumption)
apply assumption
apply(drule order_antisym, assumption)
apply(rotate_tac -3)
apply(simp)
done


subsubsection {* Producer/Consumer Algorithm *}

record Producer_consumer =
  ins :: nat
  outs :: nat
  li :: nat
  lj :: nat
  vx :: nat
  vy :: nat
  buffer :: "nat list"
  b :: "nat list"

text {* The whole proof takes aprox. 4 minutes. *}

lemma Producer_consumer: 
  "[|INIT= «0<length a ∧ 0<length ´buffer ∧ length ´b=length a» ;  
    I= «(∀k<´ins. ´outs≤k --> (a ! k) = ´buffer ! (k mod (length ´buffer))) ∧  
            ´outs≤´ins ∧ ´ins-´outs≤length ´buffer» ;  
    I1= «´I ∧ ´li≤length a» ;  
    p1= «´I1 ∧ ´li=´ins» ;  
    I2 = «´I ∧ (∀k<´lj. (a ! k)=(´b ! k)) ∧ ´lj≤length a» ;
    p2 = «´I2 ∧ ´lj=´outs» |] ==>   
  \<parallel>- \<lbrace>´INIT\<rbrace>  
 ´ins:=0,, ´outs:=0,, ´li:=0,, ´lj:=0,,
 COBEGIN \<lbrace>´p1 ∧ ´INIT\<rbrace> 
   WHILE ´li <length a 
     INV \<lbrace>´p1 ∧ ´INIT\<rbrace>   
   DO \<lbrace>´p1 ∧ ´INIT ∧ ´li<length a\<rbrace>  
       ´vx:= (a ! ´li);;  
      \<lbrace>´p1 ∧ ´INIT ∧ ´li<length a ∧ ´vx=(a ! ´li)\<rbrace> 
        WAIT ´ins-´outs < length ´buffer END;; 
      \<lbrace>´p1 ∧ ´INIT ∧ ´li<length a ∧ ´vx=(a ! ´li) 
         ∧ ´ins-´outs < length ´buffer\<rbrace> 
       ´buffer:=(list_update ´buffer (´ins mod (length ´buffer)) ´vx);; 
      \<lbrace>´p1 ∧ ´INIT ∧ ´li<length a 
         ∧ (a ! ´li)=(´buffer ! (´ins mod (length ´buffer))) 
         ∧ ´ins-´outs <length ´buffer\<rbrace>  
       ´ins:=´ins+1;; 
      \<lbrace>´I1 ∧ ´INIT ∧ (´li+1)=´ins ∧ ´li<length a\<rbrace>  
       ´li:=´li+1  
   OD  
  \<lbrace>´p1 ∧ ´INIT ∧ ´li=length a\<rbrace>  
  \<parallel>  
  \<lbrace>´p2 ∧ ´INIT\<rbrace>  
   WHILE ´lj < length a  
     INV \<lbrace>´p2 ∧ ´INIT\<rbrace>  
   DO \<lbrace>´p2 ∧ ´lj<length a ∧ ´INIT\<rbrace>  
        WAIT ´outs<´ins END;; 
      \<lbrace>´p2 ∧ ´lj<length a ∧ ´outs<´ins ∧ ´INIT\<rbrace>  
       ´vy:=(´buffer ! (´outs mod (length ´buffer)));; 
      \<lbrace>´p2 ∧ ´lj<length a ∧ ´outs<´ins ∧ ´vy=(a ! ´lj) ∧ ´INIT\<rbrace>  
       ´outs:=´outs+1;;  
      \<lbrace>´I2 ∧ (´lj+1)=´outs ∧ ´lj<length a ∧ ´vy=(a ! ´lj) ∧ ´INIT\<rbrace>  
       ´b:=(list_update ´b ´lj ´vy);; 
      \<lbrace>´I2 ∧ (´lj+1)=´outs ∧ ´lj<length a ∧ (a ! ´lj)=(´b ! ´lj) ∧ ´INIT\<rbrace>  
       ´lj:=´lj+1  
   OD  
  \<lbrace>´p2 ∧ ´lj=length a ∧ ´INIT\<rbrace>  
 COEND  
 \<lbrace> ∀k<length a. (a ! k)=(´b ! k)\<rbrace>"
apply oghoare
--{* 138 vc  *}
apply(tactic {* ALLGOALS (clarify_tac @{context}) *})
--{* 112 subgoals left *}
apply(simp_all (no_asm))
--{* 97 subgoals left *}
apply(tactic {*ALLGOALS (conjI_Tac (K all_tac)) *})
--{* 930 subgoals left *}
apply(tactic {* ALLGOALS (clarify_tac @{context}) *})
--{* 99 subgoals left *}
apply(simp_all (*asm_lr*) only:length_0_conv [THEN sym])
--{* 20 subgoals left *}
apply (simp_all (*asm_lr*) del:length_0_conv length_greater_0_conv add: nth_list_update mod_lemma)
--{* 9 subgoals left *}
apply (force simp add:less_Suc_eq)
apply(hypsubst_thin, drule sym)
apply (force simp add:less_Suc_eq)+
done

subsection {* Parameterized Examples *}

subsubsection {* Set Elements of an Array to Zero *}

record Example1 =
  a :: "nat => nat"

lemma Example1: 
 "\<parallel>- \<lbrace>True\<rbrace>
   COBEGIN SCHEME [0≤i<n] \<lbrace>True\<rbrace> ´a:=´a (i:=0) \<lbrace>´a i=0\<rbrace> COEND 
  \<lbrace>∀i < n. ´a i = 0\<rbrace>"
apply oghoare
apply simp_all
done

text {* Same example with lists as auxiliary variables. *}
record Example1_list =
  A :: "nat list"
lemma Example1_list: 
 "\<parallel>- \<lbrace>n < length ´A\<rbrace> 
   COBEGIN 
     SCHEME [0≤i<n] \<lbrace>n < length ´A\<rbrace> ´A:=´A[i:=0] \<lbrace>´A!i=0\<rbrace> 
   COEND 
    \<lbrace>∀i < n. ´A!i = 0\<rbrace>"
apply oghoare
apply force+
done

subsubsection {* Increment a Variable in Parallel *}

text {* First some lemmas about summation properties. *}
(*
lemma Example2_lemma1: "!!b. j<n ==> (∑i::nat<n. b i) = (0::nat) ==> b j = 0 "
apply(induct n)
 apply simp_all
apply(force simp add: less_Suc_eq)
done
*)
lemma Example2_lemma2_aux: "!!b. j<n ==> 
 (∑i=0..<n. (b i::nat)) =
 (∑i=0..<j. b i) + b j + (∑i=0..<n-(Suc j) . b (Suc j + i))"
apply(induct n)
 apply simp_all
apply(simp add:less_Suc_eq)
 apply(auto)
apply(subgoal_tac "n - j = Suc(n- Suc j)")
  apply simp
apply arith
done

lemma Example2_lemma2_aux2: 
  "!!b. j≤ s ==> (∑i::nat=0..<j. (b (s:=t)) i) = (∑i=0..<j. b i)"
apply(induct j) 
 apply simp_all
done

lemma Example2_lemma2: 
 "!!b. [|j<n; b j=0|] ==> Suc (∑i::nat=0..<n. b i)=(∑i=0..<n. (b (j := Suc 0)) i)"
apply(frule_tac b="(b (j:=(Suc 0)))" in Example2_lemma2_aux)
apply(erule_tac  t="setsum (b(j := (Suc 0))) {0..<n}" in ssubst)
apply(frule_tac b=b in Example2_lemma2_aux)
apply(erule_tac  t="setsum b {0..<n}" in ssubst)
apply(subgoal_tac "Suc (setsum b {0..<j} + b j + (∑i=0..<n - Suc j. b (Suc j + i)))=(setsum b {0..<j} + Suc (b j) + (∑i=0..<n - Suc j. b (Suc j + i)))")
apply(rotate_tac -1)
apply(erule ssubst)
apply(subgoal_tac "j≤j")
 apply(drule_tac b="b" and t="(Suc 0)" in Example2_lemma2_aux2)
apply(rotate_tac -1)
apply(erule ssubst)
apply simp_all
done


record Example2 = 
 c :: "nat => nat" 
 x :: nat

lemma Example_2: "0<n ==> 
 \<parallel>- \<lbrace>´x=0 ∧ (∑i=0..<n. ´c i)=0\<rbrace>  
 COBEGIN 
   SCHEME [0≤i<n] 
  \<lbrace>´x=(∑i=0..<n. ´c i) ∧ ´c i=0\<rbrace> 
   ⟨ ´x:=´x+(Suc 0),, ´c:=´c (i:=(Suc 0)) ⟩
  \<lbrace>´x=(∑i=0..<n. ´c i) ∧ ´c i=(Suc 0)\<rbrace>
 COEND 
 \<lbrace>´x=n\<rbrace>"
apply oghoare
apply (simp_all cong del: setsum.strong_cong)
apply (tactic {* ALLGOALS (clarify_tac @{context}) *})
apply (simp_all cong del: setsum.strong_cong)
   apply(erule (1) Example2_lemma2)
  apply(erule (1) Example2_lemma2)
 apply(erule (1) Example2_lemma2)
apply(simp)
done

end