src/HOL/IMPP/EvenOdd.thy
author wenzelm
Fri, 05 Apr 2019 17:05:32 +0200
changeset 70067 9b34dbeb1103
parent 63167 0909deb8059b
child 80914 d97fdabd9e2b
permissions -rw-r--r--
auxiliary operation for common uses of 'compile_generated_files';
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
8177
e59e93ad85eb added IMPP to HOL
oheimb
parents:
diff changeset
     1
(*  Title:      HOL/IMPP/EvenOdd.thy
41589
bbd861837ebc tuned headers;
wenzelm
parents: 27362
diff changeset
     2
    Author:     David von Oheimb, TUM
8177
e59e93ad85eb added IMPP to HOL
oheimb
parents:
diff changeset
     3
*)
e59e93ad85eb added IMPP to HOL
oheimb
parents:
diff changeset
     4
63167
0909deb8059b isabelle update_cartouches -c -t;
wenzelm
parents: 62145
diff changeset
     5
section \<open>Example of mutually recursive procedures verified with Hoare logic\<close>
8177
e59e93ad85eb added IMPP to HOL
oheimb
parents:
diff changeset
     6
17477
ceb42ea2f223 converted to Isar theory format;
wenzelm
parents: 15354
diff changeset
     7
theory EvenOdd
58770
ae5e9b4f8daf downshift of theory Parity in the hierarchy
haftmann
parents: 58651
diff changeset
     8
imports Main Misc
17477
ceb42ea2f223 converted to Isar theory format;
wenzelm
parents: 15354
diff changeset
     9
begin
ceb42ea2f223 converted to Isar theory format;
wenzelm
parents: 15354
diff changeset
    10
27362
a6dc1769fdda modernized specifications;
wenzelm
parents: 19803
diff changeset
    11
axiomatization
a6dc1769fdda modernized specifications;
wenzelm
parents: 19803
diff changeset
    12
  Even :: pname and
17477
ceb42ea2f223 converted to Isar theory format;
wenzelm
parents: 15354
diff changeset
    13
  Odd :: pname
27362
a6dc1769fdda modernized specifications;
wenzelm
parents: 19803
diff changeset
    14
where
a6dc1769fdda modernized specifications;
wenzelm
parents: 19803
diff changeset
    15
  Even_neq_Odd: "Even ~= Odd" and
17477
ceb42ea2f223 converted to Isar theory format;
wenzelm
parents: 15354
diff changeset
    16
  Arg_neq_Res:  "Arg  ~= Res"
8177
e59e93ad85eb added IMPP to HOL
oheimb
parents:
diff changeset
    17
27362
a6dc1769fdda modernized specifications;
wenzelm
parents: 19803
diff changeset
    18
definition
a6dc1769fdda modernized specifications;
wenzelm
parents: 19803
diff changeset
    19
  evn :: com where
a6dc1769fdda modernized specifications;
wenzelm
parents: 19803
diff changeset
    20
 "evn = (IF (%s. s<Arg> = 0)
8177
e59e93ad85eb added IMPP to HOL
oheimb
parents:
diff changeset
    21
         THEN Loc Res:==(%s. 0)
e59e93ad85eb added IMPP to HOL
oheimb
parents:
diff changeset
    22
         ELSE(Loc Res:=CALL Odd(%s. s<Arg> - 1);;
e59e93ad85eb added IMPP to HOL
oheimb
parents:
diff changeset
    23
              Loc Arg:=CALL Odd(%s. s<Arg> - 1);;
27362
a6dc1769fdda modernized specifications;
wenzelm
parents: 19803
diff changeset
    24
              Loc Res:==(%s. s<Res> * s<Arg>)))"
a6dc1769fdda modernized specifications;
wenzelm
parents: 19803
diff changeset
    25
a6dc1769fdda modernized specifications;
wenzelm
parents: 19803
diff changeset
    26
definition
a6dc1769fdda modernized specifications;
wenzelm
parents: 19803
diff changeset
    27
  odd :: com where
a6dc1769fdda modernized specifications;
wenzelm
parents: 19803
diff changeset
    28
 "odd = (IF (%s. s<Arg> = 0)
8177
e59e93ad85eb added IMPP to HOL
oheimb
parents:
diff changeset
    29
         THEN Loc Res:==(%s. 1)
27362
a6dc1769fdda modernized specifications;
wenzelm
parents: 19803
diff changeset
    30
         ELSE(Loc Res:=CALL Even (%s. s<Arg> - 1)))"
8177
e59e93ad85eb added IMPP to HOL
oheimb
parents:
diff changeset
    31
62145
5b946c81dfbf eliminated old defs;
wenzelm
parents: 59807
diff changeset
    32
overloading bodies \<equiv> bodies
5b946c81dfbf eliminated old defs;
wenzelm
parents: 59807
diff changeset
    33
begin
5b946c81dfbf eliminated old defs;
wenzelm
parents: 59807
diff changeset
    34
  definition "bodies == [(Even,evn),(Odd,odd)]"
5b946c81dfbf eliminated old defs;
wenzelm
parents: 59807
diff changeset
    35
end
17477
ceb42ea2f223 converted to Isar theory format;
wenzelm
parents: 15354
diff changeset
    36
27362
a6dc1769fdda modernized specifications;
wenzelm
parents: 19803
diff changeset
    37
definition
a6dc1769fdda modernized specifications;
wenzelm
parents: 19803
diff changeset
    38
  Z_eq_Arg_plus :: "nat => nat assn" ("Z=Arg+_" [50]50) where
a6dc1769fdda modernized specifications;
wenzelm
parents: 19803
diff changeset
    39
  "Z=Arg+n = (%Z s.      Z =  s<Arg>+n)"
a6dc1769fdda modernized specifications;
wenzelm
parents: 19803
diff changeset
    40
a6dc1769fdda modernized specifications;
wenzelm
parents: 19803
diff changeset
    41
definition
a6dc1769fdda modernized specifications;
wenzelm
parents: 19803
diff changeset
    42
  Res_ok :: "nat assn" where
a6dc1769fdda modernized specifications;
wenzelm
parents: 19803
diff changeset
    43
  "Res_ok = (%Z s. even Z = (s<Res> = 0))"
17477
ceb42ea2f223 converted to Isar theory format;
wenzelm
parents: 15354
diff changeset
    44
19803
aa2581752afb removed obsolete ML files;
wenzelm
parents: 17477
diff changeset
    45
aa2581752afb removed obsolete ML files;
wenzelm
parents: 17477
diff changeset
    46
subsection "Arg, Res"
aa2581752afb removed obsolete ML files;
wenzelm
parents: 17477
diff changeset
    47
aa2581752afb removed obsolete ML files;
wenzelm
parents: 17477
diff changeset
    48
declare Arg_neq_Res [simp] Arg_neq_Res [THEN not_sym, simp]
aa2581752afb removed obsolete ML files;
wenzelm
parents: 17477
diff changeset
    49
declare Even_neq_Odd [simp] Even_neq_Odd [THEN not_sym, simp]
aa2581752afb removed obsolete ML files;
wenzelm
parents: 17477
diff changeset
    50
aa2581752afb removed obsolete ML files;
wenzelm
parents: 17477
diff changeset
    51
lemma Z_eq_Arg_plus_def2: "(Z=Arg+n) Z s = (Z = s<Arg>+n)"
aa2581752afb removed obsolete ML files;
wenzelm
parents: 17477
diff changeset
    52
apply (unfold Z_eq_Arg_plus_def)
aa2581752afb removed obsolete ML files;
wenzelm
parents: 17477
diff changeset
    53
apply (rule refl)
aa2581752afb removed obsolete ML files;
wenzelm
parents: 17477
diff changeset
    54
done
aa2581752afb removed obsolete ML files;
wenzelm
parents: 17477
diff changeset
    55
aa2581752afb removed obsolete ML files;
wenzelm
parents: 17477
diff changeset
    56
lemma Res_ok_def2: "Res_ok Z s = (even Z = (s<Res> = 0))"
aa2581752afb removed obsolete ML files;
wenzelm
parents: 17477
diff changeset
    57
apply (unfold Res_ok_def)
aa2581752afb removed obsolete ML files;
wenzelm
parents: 17477
diff changeset
    58
apply (rule refl)
aa2581752afb removed obsolete ML files;
wenzelm
parents: 17477
diff changeset
    59
done
aa2581752afb removed obsolete ML files;
wenzelm
parents: 17477
diff changeset
    60
aa2581752afb removed obsolete ML files;
wenzelm
parents: 17477
diff changeset
    61
lemmas Arg_Res_simps = Z_eq_Arg_plus_def2 Res_ok_def2
aa2581752afb removed obsolete ML files;
wenzelm
parents: 17477
diff changeset
    62
aa2581752afb removed obsolete ML files;
wenzelm
parents: 17477
diff changeset
    63
lemma body_Odd [simp]: "body Odd = Some odd"
aa2581752afb removed obsolete ML files;
wenzelm
parents: 17477
diff changeset
    64
apply (unfold body_def bodies_def)
aa2581752afb removed obsolete ML files;
wenzelm
parents: 17477
diff changeset
    65
apply auto
aa2581752afb removed obsolete ML files;
wenzelm
parents: 17477
diff changeset
    66
done
aa2581752afb removed obsolete ML files;
wenzelm
parents: 17477
diff changeset
    67
aa2581752afb removed obsolete ML files;
wenzelm
parents: 17477
diff changeset
    68
lemma body_Even [simp]: "body Even = Some evn"
aa2581752afb removed obsolete ML files;
wenzelm
parents: 17477
diff changeset
    69
apply (unfold body_def bodies_def)
aa2581752afb removed obsolete ML files;
wenzelm
parents: 17477
diff changeset
    70
apply auto
aa2581752afb removed obsolete ML files;
wenzelm
parents: 17477
diff changeset
    71
done
aa2581752afb removed obsolete ML files;
wenzelm
parents: 17477
diff changeset
    72
aa2581752afb removed obsolete ML files;
wenzelm
parents: 17477
diff changeset
    73
aa2581752afb removed obsolete ML files;
wenzelm
parents: 17477
diff changeset
    74
subsection "verification"
aa2581752afb removed obsolete ML files;
wenzelm
parents: 17477
diff changeset
    75
aa2581752afb removed obsolete ML files;
wenzelm
parents: 17477
diff changeset
    76
lemma Odd_lemma: "{{Z=Arg+0}. BODY Even .{Res_ok}}|-{Z=Arg+Suc 0}. odd .{Res_ok}"
aa2581752afb removed obsolete ML files;
wenzelm
parents: 17477
diff changeset
    77
apply (unfold odd_def)
aa2581752afb removed obsolete ML files;
wenzelm
parents: 17477
diff changeset
    78
apply (rule hoare_derivs.If)
aa2581752afb removed obsolete ML files;
wenzelm
parents: 17477
diff changeset
    79
apply (rule hoare_derivs.Ass [THEN conseq1])
aa2581752afb removed obsolete ML files;
wenzelm
parents: 17477
diff changeset
    80
apply  (clarsimp simp: Arg_Res_simps)
aa2581752afb removed obsolete ML files;
wenzelm
parents: 17477
diff changeset
    81
apply (rule export_s)
aa2581752afb removed obsolete ML files;
wenzelm
parents: 17477
diff changeset
    82
apply (rule hoare_derivs.Call [THEN conseq1])
aa2581752afb removed obsolete ML files;
wenzelm
parents: 17477
diff changeset
    83
apply  (rule_tac P = "Z=Arg+Suc (Suc 0) " in conseq12)
aa2581752afb removed obsolete ML files;
wenzelm
parents: 17477
diff changeset
    84
apply (rule single_asm)
aa2581752afb removed obsolete ML files;
wenzelm
parents: 17477
diff changeset
    85
apply (auto simp: Arg_Res_simps)
aa2581752afb removed obsolete ML files;
wenzelm
parents: 17477
diff changeset
    86
done
aa2581752afb removed obsolete ML files;
wenzelm
parents: 17477
diff changeset
    87
aa2581752afb removed obsolete ML files;
wenzelm
parents: 17477
diff changeset
    88
lemma Even_lemma: "{{Z=Arg+1}. BODY Odd .{Res_ok}}|-{Z=Arg+0}. evn .{Res_ok}"
aa2581752afb removed obsolete ML files;
wenzelm
parents: 17477
diff changeset
    89
apply (unfold evn_def)
aa2581752afb removed obsolete ML files;
wenzelm
parents: 17477
diff changeset
    90
apply (rule hoare_derivs.If)
aa2581752afb removed obsolete ML files;
wenzelm
parents: 17477
diff changeset
    91
apply (rule hoare_derivs.Ass [THEN conseq1])
aa2581752afb removed obsolete ML files;
wenzelm
parents: 17477
diff changeset
    92
apply  (clarsimp simp: Arg_Res_simps)
aa2581752afb removed obsolete ML files;
wenzelm
parents: 17477
diff changeset
    93
apply (rule hoare_derivs.Comp)
aa2581752afb removed obsolete ML files;
wenzelm
parents: 17477
diff changeset
    94
apply (rule_tac [2] hoare_derivs.Ass)
aa2581752afb removed obsolete ML files;
wenzelm
parents: 17477
diff changeset
    95
apply clarsimp
59807
22bc39064290 prefer local fixes;
wenzelm
parents: 58889
diff changeset
    96
apply (rule_tac Q = "%Z s. P Z s & Res_ok Z s" and P = P for P in hoare_derivs.Comp)
19803
aa2581752afb removed obsolete ML files;
wenzelm
parents: 17477
diff changeset
    97
apply (rule export_s)
aa2581752afb removed obsolete ML files;
wenzelm
parents: 17477
diff changeset
    98
apply  (rule_tac I1 = "%Z l. Z = l Arg & 0 < Z" and Q1 = "Res_ok" in Call_invariant [THEN conseq12])
aa2581752afb removed obsolete ML files;
wenzelm
parents: 17477
diff changeset
    99
apply (rule single_asm [THEN conseq2])
aa2581752afb removed obsolete ML files;
wenzelm
parents: 17477
diff changeset
   100
apply   (clarsimp simp: Arg_Res_simps)
aa2581752afb removed obsolete ML files;
wenzelm
parents: 17477
diff changeset
   101
apply  (force simp: Arg_Res_simps)
aa2581752afb removed obsolete ML files;
wenzelm
parents: 17477
diff changeset
   102
apply (rule export_s)
aa2581752afb removed obsolete ML files;
wenzelm
parents: 17477
diff changeset
   103
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])
aa2581752afb removed obsolete ML files;
wenzelm
parents: 17477
diff changeset
   104
apply (rule single_asm [THEN conseq2])
aa2581752afb removed obsolete ML files;
wenzelm
parents: 17477
diff changeset
   105
apply  (clarsimp simp: Arg_Res_simps)
aa2581752afb removed obsolete ML files;
wenzelm
parents: 17477
diff changeset
   106
apply (force simp: Arg_Res_simps)
aa2581752afb removed obsolete ML files;
wenzelm
parents: 17477
diff changeset
   107
done
aa2581752afb removed obsolete ML files;
wenzelm
parents: 17477
diff changeset
   108
aa2581752afb removed obsolete ML files;
wenzelm
parents: 17477
diff changeset
   109
aa2581752afb removed obsolete ML files;
wenzelm
parents: 17477
diff changeset
   110
lemma Even_ok_N: "{}|-{Z=Arg+0}. BODY Even .{Res_ok}"
aa2581752afb removed obsolete ML files;
wenzelm
parents: 17477
diff changeset
   111
apply (rule BodyN)
aa2581752afb removed obsolete ML files;
wenzelm
parents: 17477
diff changeset
   112
apply (simp (no_asm))
aa2581752afb removed obsolete ML files;
wenzelm
parents: 17477
diff changeset
   113
apply (rule Even_lemma [THEN hoare_derivs.cut])
aa2581752afb removed obsolete ML files;
wenzelm
parents: 17477
diff changeset
   114
apply (rule BodyN)
aa2581752afb removed obsolete ML files;
wenzelm
parents: 17477
diff changeset
   115
apply (simp (no_asm))
aa2581752afb removed obsolete ML files;
wenzelm
parents: 17477
diff changeset
   116
apply (rule Odd_lemma [THEN thin])
aa2581752afb removed obsolete ML files;
wenzelm
parents: 17477
diff changeset
   117
apply (simp (no_asm))
aa2581752afb removed obsolete ML files;
wenzelm
parents: 17477
diff changeset
   118
done
aa2581752afb removed obsolete ML files;
wenzelm
parents: 17477
diff changeset
   119
aa2581752afb removed obsolete ML files;
wenzelm
parents: 17477
diff changeset
   120
lemma Even_ok_S: "{}|-{Z=Arg+0}. BODY Even .{Res_ok}"
aa2581752afb removed obsolete ML files;
wenzelm
parents: 17477
diff changeset
   121
apply (rule conseq1)
aa2581752afb removed obsolete ML files;
wenzelm
parents: 17477
diff changeset
   122
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)
aa2581752afb removed obsolete ML files;
wenzelm
parents: 17477
diff changeset
   123
apply    auto
aa2581752afb removed obsolete ML files;
wenzelm
parents: 17477
diff changeset
   124
apply (rule hoare_derivs.insert)
aa2581752afb removed obsolete ML files;
wenzelm
parents: 17477
diff changeset
   125
apply (rule Odd_lemma [THEN thin])
aa2581752afb removed obsolete ML files;
wenzelm
parents: 17477
diff changeset
   126
apply  (simp (no_asm))
aa2581752afb removed obsolete ML files;
wenzelm
parents: 17477
diff changeset
   127
apply (rule Even_lemma [THEN thin])
aa2581752afb removed obsolete ML files;
wenzelm
parents: 17477
diff changeset
   128
apply (simp (no_asm))
aa2581752afb removed obsolete ML files;
wenzelm
parents: 17477
diff changeset
   129
done
8177
e59e93ad85eb added IMPP to HOL
oheimb
parents:
diff changeset
   130
e59e93ad85eb added IMPP to HOL
oheimb
parents:
diff changeset
   131
end