src/HOL/IMPP/EvenOdd.thy
author wenzelm
Sat, 23 Nov 2013 17:07:36 +0100
changeset 54566 5f3e9baa8f13
parent 41589 bbd861837ebc
child 58648 3ccafeb9a1d1
permissions -rw-r--r--
more accurate goal context;
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
17477
ceb42ea2f223 converted to Isar theory format;
wenzelm
parents: 15354
diff changeset
     5
header {* Example of mutually recursive procedures verified with Hoare logic *}
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
ceb42ea2f223 converted to Isar theory format;
wenzelm
parents: 15354
diff changeset
     8
imports Misc
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
definition
a6dc1769fdda modernized specifications;
wenzelm
parents: 19803
diff changeset
    12
  even :: "nat => bool" where
a6dc1769fdda modernized specifications;
wenzelm
parents: 19803
diff changeset
    13
  "even n = (2 dvd n)"
8177
e59e93ad85eb added IMPP to HOL
oheimb
parents:
diff changeset
    14
27362
a6dc1769fdda modernized specifications;
wenzelm
parents: 19803
diff changeset
    15
axiomatization
a6dc1769fdda modernized specifications;
wenzelm
parents: 19803
diff changeset
    16
  Even :: pname and
17477
ceb42ea2f223 converted to Isar theory format;
wenzelm
parents: 15354
diff changeset
    17
  Odd :: pname
27362
a6dc1769fdda modernized specifications;
wenzelm
parents: 19803
diff changeset
    18
where
a6dc1769fdda modernized specifications;
wenzelm
parents: 19803
diff changeset
    19
  Even_neq_Odd: "Even ~= Odd" and
17477
ceb42ea2f223 converted to Isar theory format;
wenzelm
parents: 15354
diff changeset
    20
  Arg_neq_Res:  "Arg  ~= Res"
8177
e59e93ad85eb added IMPP to HOL
oheimb
parents:
diff changeset
    21
27362
a6dc1769fdda modernized specifications;
wenzelm
parents: 19803
diff changeset
    22
definition
a6dc1769fdda modernized specifications;
wenzelm
parents: 19803
diff changeset
    23
  evn :: com where
a6dc1769fdda modernized specifications;
wenzelm
parents: 19803
diff changeset
    24
 "evn = (IF (%s. s<Arg> = 0)
8177
e59e93ad85eb added IMPP to HOL
oheimb
parents:
diff changeset
    25
         THEN Loc Res:==(%s. 0)
e59e93ad85eb added IMPP to HOL
oheimb
parents:
diff changeset
    26
         ELSE(Loc Res:=CALL Odd(%s. s<Arg> - 1);;
e59e93ad85eb added IMPP to HOL
oheimb
parents:
diff changeset
    27
              Loc Arg:=CALL Odd(%s. s<Arg> - 1);;
27362
a6dc1769fdda modernized specifications;
wenzelm
parents: 19803
diff changeset
    28
              Loc Res:==(%s. s<Res> * s<Arg>)))"
a6dc1769fdda modernized specifications;
wenzelm
parents: 19803
diff changeset
    29
a6dc1769fdda modernized specifications;
wenzelm
parents: 19803
diff changeset
    30
definition
a6dc1769fdda modernized specifications;
wenzelm
parents: 19803
diff changeset
    31
  odd :: com where
a6dc1769fdda modernized specifications;
wenzelm
parents: 19803
diff changeset
    32
 "odd = (IF (%s. s<Arg> = 0)
8177
e59e93ad85eb added IMPP to HOL
oheimb
parents:
diff changeset
    33
         THEN Loc Res:==(%s. 1)
27362
a6dc1769fdda modernized specifications;
wenzelm
parents: 19803
diff changeset
    34
         ELSE(Loc Res:=CALL Even (%s. s<Arg> - 1)))"
8177
e59e93ad85eb added IMPP to HOL
oheimb
parents:
diff changeset
    35
e59e93ad85eb added IMPP to HOL
oheimb
parents:
diff changeset
    36
defs
17477
ceb42ea2f223 converted to Isar theory format;
wenzelm
parents: 15354
diff changeset
    37
  bodies_def: "bodies == [(Even,evn),(Odd,odd)]"
ceb42ea2f223 converted to Isar theory format;
wenzelm
parents: 15354
diff changeset
    38
27362
a6dc1769fdda modernized specifications;
wenzelm
parents: 19803
diff changeset
    39
definition
a6dc1769fdda modernized specifications;
wenzelm
parents: 19803
diff changeset
    40
  Z_eq_Arg_plus :: "nat => nat assn" ("Z=Arg+_" [50]50) where
a6dc1769fdda modernized specifications;
wenzelm
parents: 19803
diff changeset
    41
  "Z=Arg+n = (%Z s.      Z =  s<Arg>+n)"
a6dc1769fdda modernized specifications;
wenzelm
parents: 19803
diff changeset
    42
a6dc1769fdda modernized specifications;
wenzelm
parents: 19803
diff changeset
    43
definition
a6dc1769fdda modernized specifications;
wenzelm
parents: 19803
diff changeset
    44
  Res_ok :: "nat assn" where
a6dc1769fdda modernized specifications;
wenzelm
parents: 19803
diff changeset
    45
  "Res_ok = (%Z s. even Z = (s<Res> = 0))"
17477
ceb42ea2f223 converted to Isar theory format;
wenzelm
parents: 15354
diff changeset
    46
19803
aa2581752afb removed obsolete ML files;
wenzelm
parents: 17477
diff changeset
    47
aa2581752afb removed obsolete ML files;
wenzelm
parents: 17477
diff changeset
    48
subsection "even"
aa2581752afb removed obsolete ML files;
wenzelm
parents: 17477
diff changeset
    49
aa2581752afb removed obsolete ML files;
wenzelm
parents: 17477
diff changeset
    50
lemma even_0 [simp]: "even 0"
aa2581752afb removed obsolete ML files;
wenzelm
parents: 17477
diff changeset
    51
apply (unfold even_def)
aa2581752afb removed obsolete ML files;
wenzelm
parents: 17477
diff changeset
    52
apply simp
aa2581752afb removed obsolete ML files;
wenzelm
parents: 17477
diff changeset
    53
done
aa2581752afb removed obsolete ML files;
wenzelm
parents: 17477
diff changeset
    54
aa2581752afb removed obsolete ML files;
wenzelm
parents: 17477
diff changeset
    55
lemma not_even_1 [simp]: "even (Suc 0) = False"
aa2581752afb removed obsolete ML files;
wenzelm
parents: 17477
diff changeset
    56
apply (unfold even_def)
aa2581752afb removed obsolete ML files;
wenzelm
parents: 17477
diff changeset
    57
apply simp
aa2581752afb removed obsolete ML files;
wenzelm
parents: 17477
diff changeset
    58
done
aa2581752afb removed obsolete ML files;
wenzelm
parents: 17477
diff changeset
    59
aa2581752afb removed obsolete ML files;
wenzelm
parents: 17477
diff changeset
    60
lemma even_step [simp]: "even (Suc (Suc n)) = even n"
aa2581752afb removed obsolete ML files;
wenzelm
parents: 17477
diff changeset
    61
apply (unfold even_def)
aa2581752afb removed obsolete ML files;
wenzelm
parents: 17477
diff changeset
    62
apply (subgoal_tac "Suc (Suc n) = n+2")
aa2581752afb removed obsolete ML files;
wenzelm
parents: 17477
diff changeset
    63
prefer 2
aa2581752afb removed obsolete ML files;
wenzelm
parents: 17477
diff changeset
    64
apply  simp
aa2581752afb removed obsolete ML files;
wenzelm
parents: 17477
diff changeset
    65
apply (erule ssubst)
aa2581752afb removed obsolete ML files;
wenzelm
parents: 17477
diff changeset
    66
apply (rule dvd_reduce)
aa2581752afb removed obsolete ML files;
wenzelm
parents: 17477
diff changeset
    67
done
aa2581752afb removed obsolete ML files;
wenzelm
parents: 17477
diff changeset
    68
aa2581752afb removed obsolete ML files;
wenzelm
parents: 17477
diff changeset
    69
aa2581752afb removed obsolete ML files;
wenzelm
parents: 17477
diff changeset
    70
subsection "Arg, Res"
aa2581752afb removed obsolete ML files;
wenzelm
parents: 17477
diff changeset
    71
aa2581752afb removed obsolete ML files;
wenzelm
parents: 17477
diff changeset
    72
declare Arg_neq_Res [simp] Arg_neq_Res [THEN not_sym, simp]
aa2581752afb removed obsolete ML files;
wenzelm
parents: 17477
diff changeset
    73
declare Even_neq_Odd [simp] Even_neq_Odd [THEN not_sym, simp]
aa2581752afb removed obsolete ML files;
wenzelm
parents: 17477
diff changeset
    74
aa2581752afb removed obsolete ML files;
wenzelm
parents: 17477
diff changeset
    75
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
    76
apply (unfold Z_eq_Arg_plus_def)
aa2581752afb removed obsolete ML files;
wenzelm
parents: 17477
diff changeset
    77
apply (rule refl)
aa2581752afb removed obsolete ML files;
wenzelm
parents: 17477
diff changeset
    78
done
aa2581752afb removed obsolete ML files;
wenzelm
parents: 17477
diff changeset
    79
aa2581752afb removed obsolete ML files;
wenzelm
parents: 17477
diff changeset
    80
lemma Res_ok_def2: "Res_ok Z s = (even Z = (s<Res> = 0))"
aa2581752afb removed obsolete ML files;
wenzelm
parents: 17477
diff changeset
    81
apply (unfold Res_ok_def)
aa2581752afb removed obsolete ML files;
wenzelm
parents: 17477
diff changeset
    82
apply (rule refl)
aa2581752afb removed obsolete ML files;
wenzelm
parents: 17477
diff changeset
    83
done
aa2581752afb removed obsolete ML files;
wenzelm
parents: 17477
diff changeset
    84
aa2581752afb removed obsolete ML files;
wenzelm
parents: 17477
diff changeset
    85
lemmas Arg_Res_simps = Z_eq_Arg_plus_def2 Res_ok_def2
aa2581752afb removed obsolete ML files;
wenzelm
parents: 17477
diff changeset
    86
aa2581752afb removed obsolete ML files;
wenzelm
parents: 17477
diff changeset
    87
lemma body_Odd [simp]: "body Odd = Some odd"
aa2581752afb removed obsolete ML files;
wenzelm
parents: 17477
diff changeset
    88
apply (unfold body_def bodies_def)
aa2581752afb removed obsolete ML files;
wenzelm
parents: 17477
diff changeset
    89
apply auto
aa2581752afb removed obsolete ML files;
wenzelm
parents: 17477
diff changeset
    90
done
aa2581752afb removed obsolete ML files;
wenzelm
parents: 17477
diff changeset
    91
aa2581752afb removed obsolete ML files;
wenzelm
parents: 17477
diff changeset
    92
lemma body_Even [simp]: "body Even = Some evn"
aa2581752afb removed obsolete ML files;
wenzelm
parents: 17477
diff changeset
    93
apply (unfold body_def bodies_def)
aa2581752afb removed obsolete ML files;
wenzelm
parents: 17477
diff changeset
    94
apply auto
aa2581752afb removed obsolete ML files;
wenzelm
parents: 17477
diff changeset
    95
done
aa2581752afb removed obsolete ML files;
wenzelm
parents: 17477
diff changeset
    96
aa2581752afb removed obsolete ML files;
wenzelm
parents: 17477
diff changeset
    97
aa2581752afb removed obsolete ML files;
wenzelm
parents: 17477
diff changeset
    98
subsection "verification"
aa2581752afb removed obsolete ML files;
wenzelm
parents: 17477
diff changeset
    99
aa2581752afb removed obsolete ML files;
wenzelm
parents: 17477
diff changeset
   100
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
   101
apply (unfold odd_def)
aa2581752afb removed obsolete ML files;
wenzelm
parents: 17477
diff changeset
   102
apply (rule hoare_derivs.If)
aa2581752afb removed obsolete ML files;
wenzelm
parents: 17477
diff changeset
   103
apply (rule hoare_derivs.Ass [THEN conseq1])
aa2581752afb removed obsolete ML files;
wenzelm
parents: 17477
diff changeset
   104
apply  (clarsimp simp: Arg_Res_simps)
aa2581752afb removed obsolete ML files;
wenzelm
parents: 17477
diff changeset
   105
apply (rule export_s)
aa2581752afb removed obsolete ML files;
wenzelm
parents: 17477
diff changeset
   106
apply (rule hoare_derivs.Call [THEN conseq1])
aa2581752afb removed obsolete ML files;
wenzelm
parents: 17477
diff changeset
   107
apply  (rule_tac P = "Z=Arg+Suc (Suc 0) " in conseq12)
aa2581752afb removed obsolete ML files;
wenzelm
parents: 17477
diff changeset
   108
apply (rule single_asm)
aa2581752afb removed obsolete ML files;
wenzelm
parents: 17477
diff changeset
   109
apply (auto simp: Arg_Res_simps)
aa2581752afb removed obsolete ML files;
wenzelm
parents: 17477
diff changeset
   110
done
aa2581752afb removed obsolete ML files;
wenzelm
parents: 17477
diff changeset
   111
aa2581752afb removed obsolete ML files;
wenzelm
parents: 17477
diff changeset
   112
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
   113
apply (unfold evn_def)
aa2581752afb removed obsolete ML files;
wenzelm
parents: 17477
diff changeset
   114
apply (rule hoare_derivs.If)
aa2581752afb removed obsolete ML files;
wenzelm
parents: 17477
diff changeset
   115
apply (rule hoare_derivs.Ass [THEN conseq1])
aa2581752afb removed obsolete ML files;
wenzelm
parents: 17477
diff changeset
   116
apply  (clarsimp simp: Arg_Res_simps)
aa2581752afb removed obsolete ML files;
wenzelm
parents: 17477
diff changeset
   117
apply (rule hoare_derivs.Comp)
aa2581752afb removed obsolete ML files;
wenzelm
parents: 17477
diff changeset
   118
apply (rule_tac [2] hoare_derivs.Ass)
aa2581752afb removed obsolete ML files;
wenzelm
parents: 17477
diff changeset
   119
apply clarsimp
aa2581752afb removed obsolete ML files;
wenzelm
parents: 17477
diff changeset
   120
apply (rule_tac Q = "%Z s. ?P Z s & Res_ok Z s" in hoare_derivs.Comp)
aa2581752afb removed obsolete ML files;
wenzelm
parents: 17477
diff changeset
   121
apply (rule export_s)
aa2581752afb removed obsolete ML files;
wenzelm
parents: 17477
diff changeset
   122
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
   123
apply (rule single_asm [THEN conseq2])
aa2581752afb removed obsolete ML files;
wenzelm
parents: 17477
diff changeset
   124
apply   (clarsimp simp: Arg_Res_simps)
aa2581752afb removed obsolete ML files;
wenzelm
parents: 17477
diff changeset
   125
apply  (force simp: Arg_Res_simps)
aa2581752afb removed obsolete ML files;
wenzelm
parents: 17477
diff changeset
   126
apply (rule export_s)
aa2581752afb removed obsolete ML files;
wenzelm
parents: 17477
diff changeset
   127
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
   128
apply (rule single_asm [THEN conseq2])
aa2581752afb removed obsolete ML files;
wenzelm
parents: 17477
diff changeset
   129
apply  (clarsimp simp: Arg_Res_simps)
aa2581752afb removed obsolete ML files;
wenzelm
parents: 17477
diff changeset
   130
apply (force simp: Arg_Res_simps)
aa2581752afb removed obsolete ML files;
wenzelm
parents: 17477
diff changeset
   131
done
aa2581752afb removed obsolete ML files;
wenzelm
parents: 17477
diff changeset
   132
aa2581752afb removed obsolete ML files;
wenzelm
parents: 17477
diff changeset
   133
aa2581752afb removed obsolete ML files;
wenzelm
parents: 17477
diff changeset
   134
lemma Even_ok_N: "{}|-{Z=Arg+0}. BODY Even .{Res_ok}"
aa2581752afb removed obsolete ML files;
wenzelm
parents: 17477
diff changeset
   135
apply (rule BodyN)
aa2581752afb removed obsolete ML files;
wenzelm
parents: 17477
diff changeset
   136
apply (simp (no_asm))
aa2581752afb removed obsolete ML files;
wenzelm
parents: 17477
diff changeset
   137
apply (rule Even_lemma [THEN hoare_derivs.cut])
aa2581752afb removed obsolete ML files;
wenzelm
parents: 17477
diff changeset
   138
apply (rule BodyN)
aa2581752afb removed obsolete ML files;
wenzelm
parents: 17477
diff changeset
   139
apply (simp (no_asm))
aa2581752afb removed obsolete ML files;
wenzelm
parents: 17477
diff changeset
   140
apply (rule Odd_lemma [THEN thin])
aa2581752afb removed obsolete ML files;
wenzelm
parents: 17477
diff changeset
   141
apply (simp (no_asm))
aa2581752afb removed obsolete ML files;
wenzelm
parents: 17477
diff changeset
   142
done
aa2581752afb removed obsolete ML files;
wenzelm
parents: 17477
diff changeset
   143
aa2581752afb removed obsolete ML files;
wenzelm
parents: 17477
diff changeset
   144
lemma Even_ok_S: "{}|-{Z=Arg+0}. BODY Even .{Res_ok}"
aa2581752afb removed obsolete ML files;
wenzelm
parents: 17477
diff changeset
   145
apply (rule conseq1)
aa2581752afb removed obsolete ML files;
wenzelm
parents: 17477
diff changeset
   146
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
   147
apply    auto
aa2581752afb removed obsolete ML files;
wenzelm
parents: 17477
diff changeset
   148
apply (rule hoare_derivs.insert)
aa2581752afb removed obsolete ML files;
wenzelm
parents: 17477
diff changeset
   149
apply (rule Odd_lemma [THEN thin])
aa2581752afb removed obsolete ML files;
wenzelm
parents: 17477
diff changeset
   150
apply  (simp (no_asm))
aa2581752afb removed obsolete ML files;
wenzelm
parents: 17477
diff changeset
   151
apply (rule Even_lemma [THEN thin])
aa2581752afb removed obsolete ML files;
wenzelm
parents: 17477
diff changeset
   152
apply (simp (no_asm))
aa2581752afb removed obsolete ML files;
wenzelm
parents: 17477
diff changeset
   153
done
8177
e59e93ad85eb added IMPP to HOL
oheimb
parents:
diff changeset
   154
e59e93ad85eb added IMPP to HOL
oheimb
parents:
diff changeset
   155
end