src/HOL/Hoare/Hoare_Logic_Abort.thy
author wenzelm
Tue Oct 10 19:23:03 2017 +0200 (2017-10-10)
changeset 66831 29ea2b900a05
parent 62042 6c6ccf573479
child 67410 64d928bacddd
permissions -rw-r--r--
tuned: each session has at most one defining entry;
wenzelm@41959
     1
(*  Title:      HOL/Hoare/Hoare_Logic_Abort.thy
nipkow@13857
     2
    Author:     Leonor Prensa Nieto & Tobias Nipkow
nipkow@13857
     3
    Copyright   2003 TUM
nipkow@13857
     4
nipkow@13857
     5
Like Hoare.thy, but with an Abort statement for modelling run time errors.
nipkow@13857
     6
*)
nipkow@13857
     7
haftmann@35320
     8
theory Hoare_Logic_Abort
wenzelm@28457
     9
imports Main
wenzelm@24470
    10
begin
nipkow@13857
    11
wenzelm@42174
    12
type_synonym 'a bexp = "'a set"
wenzelm@42174
    13
type_synonym 'a assn = "'a set"
nipkow@13857
    14
blanchet@58310
    15
datatype 'a com =
blanchet@58305
    16
  Basic "'a \<Rightarrow> 'a"
blanchet@58305
    17
| Abort
blanchet@58305
    18
| Seq "'a com" "'a com"               ("(_;/ _)"      [61,60] 60)
blanchet@58305
    19
| Cond "'a bexp" "'a com" "'a com"    ("(1IF _/ THEN _ / ELSE _/ FI)"  [0,0,0] 61)
blanchet@58305
    20
| While "'a bexp" "'a assn" "'a com"  ("(1WHILE _/ INV {_} //DO _ /OD)"  [0,0,0] 61)
wenzelm@35113
    21
wenzelm@35054
    22
abbreviation annskip ("SKIP") where "SKIP == Basic id"
nipkow@13857
    23
wenzelm@42174
    24
type_synonym 'a sem = "'a option => 'a option => bool"
nipkow@13857
    25
berghofe@36643
    26
inductive Sem :: "'a com \<Rightarrow> 'a sem"
berghofe@36643
    27
where
berghofe@36643
    28
  "Sem (Basic f) None None"
berghofe@36643
    29
| "Sem (Basic f) (Some s) (Some (f s))"
berghofe@36643
    30
| "Sem Abort s None"
berghofe@36643
    31
| "Sem c1 s s'' \<Longrightarrow> Sem c2 s'' s' \<Longrightarrow> Sem (c1;c2) s s'"
berghofe@36643
    32
| "Sem (IF b THEN c1 ELSE c2 FI) None None"
berghofe@36643
    33
| "s \<in> b \<Longrightarrow> Sem c1 (Some s) s' \<Longrightarrow> Sem (IF b THEN c1 ELSE c2 FI) (Some s) s'"
berghofe@36643
    34
| "s \<notin> b \<Longrightarrow> Sem c2 (Some s) s' \<Longrightarrow> Sem (IF b THEN c1 ELSE c2 FI) (Some s) s'"
berghofe@36643
    35
| "Sem (While b x c) None None"
berghofe@36643
    36
| "s \<notin> b \<Longrightarrow> Sem (While b x c) (Some s) (Some s)"
berghofe@36643
    37
| "s \<in> b \<Longrightarrow> Sem c (Some s) s'' \<Longrightarrow> Sem (While b x c) s'' s' \<Longrightarrow>
berghofe@36643
    38
   Sem (While b x c) (Some s) s'"
nipkow@13857
    39
berghofe@36643
    40
inductive_cases [elim!]:
berghofe@36643
    41
  "Sem (Basic f) s s'" "Sem (c1;c2) s s'"
berghofe@36643
    42
  "Sem (IF b THEN c1 ELSE c2 FI) s s'"
nipkow@13857
    43
haftmann@35416
    44
definition Valid :: "'a bexp \<Rightarrow> 'a com \<Rightarrow> 'a bexp \<Rightarrow> bool" where
nipkow@13857
    45
  "Valid p c q == \<forall>s s'. Sem c s s' \<longrightarrow> s : Some ` p \<longrightarrow> s' : Some ` q"
nipkow@13857
    46
nipkow@13857
    47
wenzelm@35054
    48
syntax
wenzelm@42152
    49
  "_assign" :: "idt => 'b => 'a com"  ("(2_ :=/ _)" [70, 65] 61)
wenzelm@35054
    50
wenzelm@35054
    51
syntax
haftmann@35320
    52
  "_hoare_abort_vars" :: "[idts, 'a assn,'a com,'a assn] => bool"
wenzelm@35054
    53
                 ("VARS _// {_} // _ // {_}" [0,0,55,0] 50)
wenzelm@35054
    54
syntax ("" output)
haftmann@35320
    55
  "_hoare_abort"      :: "['a assn,'a com,'a assn] => bool"
wenzelm@35054
    56
                 ("{_} // _ // {_}" [0,55,0] 50)
wenzelm@42152
    57
wenzelm@48891
    58
ML_file "hoare_syntax.ML"
wenzelm@62042
    59
parse_translation \<open>[(@{syntax_const "_hoare_abort_vars"}, K Hoare_Syntax.hoare_vars_tr)]\<close>
wenzelm@42153
    60
print_translation
wenzelm@62042
    61
  \<open>[(@{const_syntax Valid}, K (Hoare_Syntax.spec_tr' @{syntax_const "_hoare_abort"}))]\<close>
nipkow@13857
    62
nipkow@13857
    63
nipkow@13857
    64
(*** The proof rules ***)
nipkow@13857
    65
nipkow@13857
    66
lemma SkipRule: "p \<subseteq> q \<Longrightarrow> Valid p (Basic id) q"
nipkow@13857
    67
by (auto simp:Valid_def)
nipkow@13857
    68
nipkow@13857
    69
lemma BasicRule: "p \<subseteq> {s. f s \<in> q} \<Longrightarrow> Valid p (Basic f) q"
nipkow@13857
    70
by (auto simp:Valid_def)
nipkow@13857
    71
nipkow@13857
    72
lemma SeqRule: "Valid P c1 Q \<Longrightarrow> Valid Q c2 R \<Longrightarrow> Valid P (c1;c2) R"
nipkow@13857
    73
by (auto simp:Valid_def)
nipkow@13857
    74
nipkow@13857
    75
lemma CondRule:
nipkow@13857
    76
 "p \<subseteq> {s. (s \<in> b \<longrightarrow> s \<in> w) \<and> (s \<notin> b \<longrightarrow> s \<in> w')}
nipkow@13857
    77
  \<Longrightarrow> Valid w c1 q \<Longrightarrow> Valid w' c2 q \<Longrightarrow> Valid p (Cond b c1 c2) q"
nipkow@44890
    78
by (fastforce simp:Valid_def image_def)
nipkow@13857
    79
berghofe@36643
    80
lemma While_aux:
berghofe@36643
    81
  assumes "Sem (WHILE b INV {i} DO c OD) s s'"
berghofe@36643
    82
  shows "\<forall>s s'. Sem c s s' \<longrightarrow> s \<in> Some ` (I \<inter> b) \<longrightarrow> s' \<in> Some ` I \<Longrightarrow>
berghofe@36643
    83
    s \<in> Some ` I \<Longrightarrow> s' \<in> Some ` (I \<inter> -b)"
berghofe@36643
    84
  using assms
berghofe@36643
    85
  by (induct "WHILE b INV {i} DO c OD" s s') auto
nipkow@13857
    86
nipkow@13857
    87
lemma WhileRule:
nipkow@13857
    88
 "p \<subseteq> i \<Longrightarrow> Valid (i \<inter> b) c i \<Longrightarrow> i \<inter> (-b) \<subseteq> q \<Longrightarrow> Valid p (While b i c) q"
nipkow@13857
    89
apply(simp add:Valid_def)
nipkow@13857
    90
apply(simp (no_asm) add:image_def)
nipkow@13857
    91
apply clarify
berghofe@36643
    92
apply(drule While_aux)
berghofe@36643
    93
  apply assumption
nipkow@13857
    94
 apply blast
nipkow@13857
    95
apply blast
nipkow@13857
    96
done
nipkow@13857
    97
nipkow@13857
    98
lemma AbortRule: "p \<subseteq> {s. False} \<Longrightarrow> Valid p Abort q"
nipkow@13857
    99
by(auto simp:Valid_def)
nipkow@13857
   100
wenzelm@24470
   101
wenzelm@62042
   102
subsection \<open>Derivation of the proof rules and, most importantly, the VCG tactic\<close>
wenzelm@24470
   103
wenzelm@24470
   104
lemma Compl_Collect: "-(Collect b) = {x. ~(b x)}"
wenzelm@24470
   105
  by blast
wenzelm@24470
   106
wenzelm@48891
   107
ML_file "hoare_tac.ML"
nipkow@13857
   108
wenzelm@62042
   109
method_setup vcg = \<open>
wenzelm@62042
   110
  Scan.succeed (fn ctxt => SIMPLE_METHOD' (Hoare.hoare_tac ctxt (K all_tac)))\<close>
nipkow@13857
   111
  "verification condition generator"
nipkow@13857
   112
wenzelm@62042
   113
method_setup vcg_simp = \<open>
wenzelm@30549
   114
  Scan.succeed (fn ctxt =>
wenzelm@62042
   115
    SIMPLE_METHOD' (Hoare.hoare_tac ctxt (asm_full_simp_tac ctxt)))\<close>
nipkow@13857
   116
  "verification condition generator plus simplification"
nipkow@13857
   117
nipkow@13875
   118
(* Special syntax for guarded statements and guarded array updates: *)
nipkow@13875
   119
nipkow@13875
   120
syntax
wenzelm@35352
   121
  "_guarded_com" :: "bool \<Rightarrow> 'a com \<Rightarrow> 'a com"  ("(2_ \<rightarrow>/ _)" 71)
wenzelm@35352
   122
  "_array_update" :: "'a list \<Rightarrow> nat \<Rightarrow> 'a \<Rightarrow> 'a com"  ("(2_[_] :=/ _)" [70, 65] 61)
nipkow@13875
   123
translations
wenzelm@35101
   124
  "P \<rightarrow> c" == "IF P THEN c ELSE CONST Abort FI"
haftmann@34940
   125
  "a[i] := v" => "(i < CONST length a) \<rightarrow> (a := CONST list_update a i v)"
nipkow@13875
   126
  (* reverse translation not possible because of duplicate "a" *)
nipkow@13875
   127
wenzelm@62042
   128
text\<open>Note: there is no special syntax for guarded array access. Thus
wenzelm@62042
   129
you must write \<open>j < length a \<rightarrow> a[i] := a!j\<close>.\<close>
nipkow@13875
   130
nipkow@13857
   131
end