| 8177 |      1 | (*  Title:      HOL/IMPP/EvenOdd.thy
 | 
| 41589 |      2 |     Author:     David von Oheimb, TUM
 | 
| 8177 |      3 | *)
 | 
|  |      4 | 
 | 
| 58889 |      5 | section {* Example of mutually recursive procedures verified with Hoare logic *}
 | 
| 8177 |      6 | 
 | 
| 17477 |      7 | theory EvenOdd
 | 
| 58770 |      8 | imports Main Misc
 | 
| 17477 |      9 | begin
 | 
|  |     10 | 
 | 
| 27362 |     11 | axiomatization
 | 
|  |     12 |   Even :: pname and
 | 
| 17477 |     13 |   Odd :: pname
 | 
| 27362 |     14 | where
 | 
|  |     15 |   Even_neq_Odd: "Even ~= Odd" and
 | 
| 17477 |     16 |   Arg_neq_Res:  "Arg  ~= Res"
 | 
| 8177 |     17 | 
 | 
| 27362 |     18 | definition
 | 
|  |     19 |   evn :: com where
 | 
|  |     20 |  "evn = (IF (%s. s<Arg> = 0)
 | 
| 8177 |     21 |          THEN Loc Res:==(%s. 0)
 | 
|  |     22 |          ELSE(Loc Res:=CALL Odd(%s. s<Arg> - 1);;
 | 
|  |     23 |               Loc Arg:=CALL Odd(%s. s<Arg> - 1);;
 | 
| 27362 |     24 |               Loc Res:==(%s. s<Res> * s<Arg>)))"
 | 
|  |     25 | 
 | 
|  |     26 | definition
 | 
|  |     27 |   odd :: com where
 | 
|  |     28 |  "odd = (IF (%s. s<Arg> = 0)
 | 
| 8177 |     29 |          THEN Loc Res:==(%s. 1)
 | 
| 27362 |     30 |          ELSE(Loc Res:=CALL Even (%s. s<Arg> - 1)))"
 | 
| 8177 |     31 | 
 | 
|  |     32 | defs
 | 
| 17477 |     33 |   bodies_def: "bodies == [(Even,evn),(Odd,odd)]"
 | 
|  |     34 | 
 | 
| 27362 |     35 | definition
 | 
|  |     36 |   Z_eq_Arg_plus :: "nat => nat assn" ("Z=Arg+_" [50]50) where
 | 
|  |     37 |   "Z=Arg+n = (%Z s.      Z =  s<Arg>+n)"
 | 
|  |     38 | 
 | 
|  |     39 | definition
 | 
|  |     40 |   Res_ok :: "nat assn" where
 | 
|  |     41 |   "Res_ok = (%Z s. even Z = (s<Res> = 0))"
 | 
| 17477 |     42 | 
 | 
| 19803 |     43 | 
 | 
|  |     44 | subsection "Arg, Res"
 | 
|  |     45 | 
 | 
|  |     46 | declare Arg_neq_Res [simp] Arg_neq_Res [THEN not_sym, simp]
 | 
|  |     47 | declare Even_neq_Odd [simp] Even_neq_Odd [THEN not_sym, simp]
 | 
|  |     48 | 
 | 
|  |     49 | lemma Z_eq_Arg_plus_def2: "(Z=Arg+n) Z s = (Z = s<Arg>+n)"
 | 
|  |     50 | apply (unfold Z_eq_Arg_plus_def)
 | 
|  |     51 | apply (rule refl)
 | 
|  |     52 | done
 | 
|  |     53 | 
 | 
|  |     54 | lemma Res_ok_def2: "Res_ok Z s = (even Z = (s<Res> = 0))"
 | 
|  |     55 | apply (unfold Res_ok_def)
 | 
|  |     56 | apply (rule refl)
 | 
|  |     57 | done
 | 
|  |     58 | 
 | 
|  |     59 | lemmas Arg_Res_simps = Z_eq_Arg_plus_def2 Res_ok_def2
 | 
|  |     60 | 
 | 
|  |     61 | lemma body_Odd [simp]: "body Odd = Some odd"
 | 
|  |     62 | apply (unfold body_def bodies_def)
 | 
|  |     63 | apply auto
 | 
|  |     64 | done
 | 
|  |     65 | 
 | 
|  |     66 | lemma body_Even [simp]: "body Even = Some evn"
 | 
|  |     67 | apply (unfold body_def bodies_def)
 | 
|  |     68 | apply auto
 | 
|  |     69 | done
 | 
|  |     70 | 
 | 
|  |     71 | 
 | 
|  |     72 | subsection "verification"
 | 
|  |     73 | 
 | 
|  |     74 | lemma Odd_lemma: "{{Z=Arg+0}. BODY Even .{Res_ok}}|-{Z=Arg+Suc 0}. odd .{Res_ok}"
 | 
|  |     75 | apply (unfold odd_def)
 | 
|  |     76 | apply (rule hoare_derivs.If)
 | 
|  |     77 | apply (rule hoare_derivs.Ass [THEN conseq1])
 | 
|  |     78 | apply  (clarsimp simp: Arg_Res_simps)
 | 
|  |     79 | apply (rule export_s)
 | 
|  |     80 | apply (rule hoare_derivs.Call [THEN conseq1])
 | 
|  |     81 | apply  (rule_tac P = "Z=Arg+Suc (Suc 0) " in conseq12)
 | 
|  |     82 | apply (rule single_asm)
 | 
|  |     83 | apply (auto simp: Arg_Res_simps)
 | 
|  |     84 | done
 | 
|  |     85 | 
 | 
|  |     86 | lemma Even_lemma: "{{Z=Arg+1}. BODY Odd .{Res_ok}}|-{Z=Arg+0}. evn .{Res_ok}"
 | 
|  |     87 | apply (unfold evn_def)
 | 
|  |     88 | apply (rule hoare_derivs.If)
 | 
|  |     89 | apply (rule hoare_derivs.Ass [THEN conseq1])
 | 
|  |     90 | apply  (clarsimp simp: Arg_Res_simps)
 | 
|  |     91 | apply (rule hoare_derivs.Comp)
 | 
|  |     92 | apply (rule_tac [2] hoare_derivs.Ass)
 | 
|  |     93 | apply clarsimp
 | 
| 59807 |     94 | apply (rule_tac Q = "%Z s. P Z s & Res_ok Z s" and P = P for P in hoare_derivs.Comp)
 | 
| 19803 |     95 | apply (rule export_s)
 | 
|  |     96 | apply  (rule_tac I1 = "%Z l. Z = l Arg & 0 < Z" and Q1 = "Res_ok" in Call_invariant [THEN conseq12])
 | 
|  |     97 | apply (rule single_asm [THEN conseq2])
 | 
|  |     98 | apply   (clarsimp simp: Arg_Res_simps)
 | 
|  |     99 | apply  (force simp: Arg_Res_simps)
 | 
|  |    100 | apply (rule export_s)
 | 
|  |    101 | apply (rule_tac I1 = "%Z l. even Z = (l Res = 0) " and Q1 = "%Z s. even Z = (s<Arg> = 0) " in Call_invariant [THEN conseq12])
 | 
|  |    102 | apply (rule single_asm [THEN conseq2])
 | 
|  |    103 | apply  (clarsimp simp: Arg_Res_simps)
 | 
|  |    104 | apply (force simp: Arg_Res_simps)
 | 
|  |    105 | done
 | 
|  |    106 | 
 | 
|  |    107 | 
 | 
|  |    108 | lemma Even_ok_N: "{}|-{Z=Arg+0}. BODY Even .{Res_ok}"
 | 
|  |    109 | apply (rule BodyN)
 | 
|  |    110 | apply (simp (no_asm))
 | 
|  |    111 | apply (rule Even_lemma [THEN hoare_derivs.cut])
 | 
|  |    112 | apply (rule BodyN)
 | 
|  |    113 | apply (simp (no_asm))
 | 
|  |    114 | apply (rule Odd_lemma [THEN thin])
 | 
|  |    115 | apply (simp (no_asm))
 | 
|  |    116 | done
 | 
|  |    117 | 
 | 
|  |    118 | lemma Even_ok_S: "{}|-{Z=Arg+0}. BODY Even .{Res_ok}"
 | 
|  |    119 | apply (rule conseq1)
 | 
|  |    120 | apply  (rule_tac Procs = "{Odd, Even}" and pn = "Even" and P = "%pn. Z=Arg+ (if pn = Odd then 1 else 0) " and Q = "%pn. Res_ok" in Body1)
 | 
|  |    121 | apply    auto
 | 
|  |    122 | apply (rule hoare_derivs.insert)
 | 
|  |    123 | apply (rule Odd_lemma [THEN thin])
 | 
|  |    124 | apply  (simp (no_asm))
 | 
|  |    125 | apply (rule Even_lemma [THEN thin])
 | 
|  |    126 | apply (simp (no_asm))
 | 
|  |    127 | done
 | 
| 8177 |    128 | 
 | 
|  |    129 | end
 |