author | wenzelm |
Wed, 04 May 2011 15:37:39 +0200 | |
changeset 42676 | 8724f20bf69c |
parent 42174 | d0be2722ce9f |
child 44890 | 22f665a2e91c |
permissions | -rw-r--r-- |
41959 | 1 |
(* Title: HOL/Hoare/Hoare_Logic_Abort.thy |
13857 | 2 |
Author: Leonor Prensa Nieto & Tobias Nipkow |
3 |
Copyright 2003 TUM |
|
4 |
||
5 |
Like Hoare.thy, but with an Abort statement for modelling run time errors. |
|
6 |
*) |
|
7 |
||
35320
f80aee1ed475
dropped axclass; dropped Id; session theory Hoare.thy
haftmann
parents:
35113
diff
changeset
|
8 |
theory Hoare_Logic_Abort |
28457
25669513fd4c
major cleanup of hoare_tac.ML: just one copy for Hoare.thy and HoareAbort.thy (only 1 line different), refrain from inspecting the main goal, proper context;
wenzelm
parents:
27330
diff
changeset
|
9 |
imports Main |
42153 | 10 |
uses ("hoare_syntax.ML") ("hoare_tac.ML") |
24470
41c81e23c08d
removed Hoare/hoare.ML, Hoare/hoareAbort.ML, ex/svc_oracle.ML (which can be mistaken as attached ML script on case-insensitive file-system);
wenzelm
parents:
21588
diff
changeset
|
11 |
begin |
13857 | 12 |
|
42174 | 13 |
type_synonym 'a bexp = "'a set" |
14 |
type_synonym 'a assn = "'a set" |
|
13857 | 15 |
|
16 |
datatype |
|
17 |
'a com = Basic "'a \<Rightarrow> 'a" |
|
18 |
| Abort |
|
19 |
| Seq "'a com" "'a com" ("(_;/ _)" [61,60] 60) |
|
20 |
| Cond "'a bexp" "'a com" "'a com" ("(1IF _/ THEN _ / ELSE _/ FI)" [0,0,0] 61) |
|
21 |
| While "'a bexp" "'a assn" "'a com" ("(1WHILE _/ INV {_} //DO _ /OD)" [0,0,0] 61) |
|
35113 | 22 |
|
35054 | 23 |
abbreviation annskip ("SKIP") where "SKIP == Basic id" |
13857 | 24 |
|
42174 | 25 |
type_synonym 'a sem = "'a option => 'a option => bool" |
13857 | 26 |
|
36643 | 27 |
inductive Sem :: "'a com \<Rightarrow> 'a sem" |
28 |
where |
|
29 |
"Sem (Basic f) None None" |
|
30 |
| "Sem (Basic f) (Some s) (Some (f s))" |
|
31 |
| "Sem Abort s None" |
|
32 |
| "Sem c1 s s'' \<Longrightarrow> Sem c2 s'' s' \<Longrightarrow> Sem (c1;c2) s s'" |
|
33 |
| "Sem (IF b THEN c1 ELSE c2 FI) None None" |
|
34 |
| "s \<in> b \<Longrightarrow> Sem c1 (Some s) s' \<Longrightarrow> Sem (IF b THEN c1 ELSE c2 FI) (Some s) s'" |
|
35 |
| "s \<notin> b \<Longrightarrow> Sem c2 (Some s) s' \<Longrightarrow> Sem (IF b THEN c1 ELSE c2 FI) (Some s) s'" |
|
36 |
| "Sem (While b x c) None None" |
|
37 |
| "s \<notin> b \<Longrightarrow> Sem (While b x c) (Some s) (Some s)" |
|
38 |
| "s \<in> b \<Longrightarrow> Sem c (Some s) s'' \<Longrightarrow> Sem (While b x c) s'' s' \<Longrightarrow> |
|
39 |
Sem (While b x c) (Some s) s'" |
|
13857 | 40 |
|
36643 | 41 |
inductive_cases [elim!]: |
42 |
"Sem (Basic f) s s'" "Sem (c1;c2) s s'" |
|
43 |
"Sem (IF b THEN c1 ELSE c2 FI) s s'" |
|
13857 | 44 |
|
35416
d8d7d1b785af
replaced a couple of constsdefs by definitions (also some old primrecs by modern ones)
haftmann
parents:
35320
diff
changeset
|
45 |
definition Valid :: "'a bexp \<Rightarrow> 'a com \<Rightarrow> 'a bexp \<Rightarrow> bool" where |
13857 | 46 |
"Valid p c q == \<forall>s s'. Sem c s s' \<longrightarrow> s : Some ` p \<longrightarrow> s' : Some ` q" |
47 |
||
48 |
||
35054 | 49 |
syntax |
42152
6c17259724b2
Hoare syntax: standard abstraction syntax admits source positions;
wenzelm
parents:
42054
diff
changeset
|
50 |
"_assign" :: "idt => 'b => 'a com" ("(2_ :=/ _)" [70, 65] 61) |
35054 | 51 |
|
52 |
syntax |
|
35320
f80aee1ed475
dropped axclass; dropped Id; session theory Hoare.thy
haftmann
parents:
35113
diff
changeset
|
53 |
"_hoare_abort_vars" :: "[idts, 'a assn,'a com,'a assn] => bool" |
35054 | 54 |
("VARS _// {_} // _ // {_}" [0,0,55,0] 50) |
55 |
syntax ("" output) |
|
35320
f80aee1ed475
dropped axclass; dropped Id; session theory Hoare.thy
haftmann
parents:
35113
diff
changeset
|
56 |
"_hoare_abort" :: "['a assn,'a com,'a assn] => bool" |
35054 | 57 |
("{_} // _ // {_}" [0,55,0] 50) |
42152
6c17259724b2
Hoare syntax: standard abstraction syntax admits source positions;
wenzelm
parents:
42054
diff
changeset
|
58 |
|
42153 | 59 |
use "hoare_syntax.ML" |
60 |
parse_translation {* [(@{syntax_const "_hoare_abort_vars"}, Hoare_Syntax.hoare_vars_tr)] *} |
|
61 |
print_translation |
|
62 |
{* [(@{const_syntax Valid}, Hoare_Syntax.spec_tr' @{syntax_const "_hoare_abort"})] *} |
|
13857 | 63 |
|
64 |
||
65 |
(*** The proof rules ***) |
|
66 |
||
67 |
lemma SkipRule: "p \<subseteq> q \<Longrightarrow> Valid p (Basic id) q" |
|
68 |
by (auto simp:Valid_def) |
|
69 |
||
70 |
lemma BasicRule: "p \<subseteq> {s. f s \<in> q} \<Longrightarrow> Valid p (Basic f) q" |
|
71 |
by (auto simp:Valid_def) |
|
72 |
||
73 |
lemma SeqRule: "Valid P c1 Q \<Longrightarrow> Valid Q c2 R \<Longrightarrow> Valid P (c1;c2) R" |
|
74 |
by (auto simp:Valid_def) |
|
75 |
||
76 |
lemma CondRule: |
|
77 |
"p \<subseteq> {s. (s \<in> b \<longrightarrow> s \<in> w) \<and> (s \<notin> b \<longrightarrow> s \<in> w')} |
|
78 |
\<Longrightarrow> Valid w c1 q \<Longrightarrow> Valid w' c2 q \<Longrightarrow> Valid p (Cond b c1 c2) q" |
|
79 |
by (fastsimp simp:Valid_def image_def) |
|
80 |
||
36643 | 81 |
lemma While_aux: |
82 |
assumes "Sem (WHILE b INV {i} DO c OD) s s'" |
|
83 |
shows "\<forall>s s'. Sem c s s' \<longrightarrow> s \<in> Some ` (I \<inter> b) \<longrightarrow> s' \<in> Some ` I \<Longrightarrow> |
|
84 |
s \<in> Some ` I \<Longrightarrow> s' \<in> Some ` (I \<inter> -b)" |
|
85 |
using assms |
|
86 |
by (induct "WHILE b INV {i} DO c OD" s s') auto |
|
13857 | 87 |
|
88 |
lemma WhileRule: |
|
89 |
"p \<subseteq> i \<Longrightarrow> Valid (i \<inter> b) c i \<Longrightarrow> i \<inter> (-b) \<subseteq> q \<Longrightarrow> Valid p (While b i c) q" |
|
90 |
apply(simp add:Valid_def) |
|
91 |
apply(simp (no_asm) add:image_def) |
|
92 |
apply clarify |
|
36643 | 93 |
apply(drule While_aux) |
94 |
apply assumption |
|
13857 | 95 |
apply blast |
96 |
apply blast |
|
97 |
done |
|
98 |
||
99 |
lemma AbortRule: "p \<subseteq> {s. False} \<Longrightarrow> Valid p Abort q" |
|
100 |
by(auto simp:Valid_def) |
|
101 |
||
24470
41c81e23c08d
removed Hoare/hoare.ML, Hoare/hoareAbort.ML, ex/svc_oracle.ML (which can be mistaken as attached ML script on case-insensitive file-system);
wenzelm
parents:
21588
diff
changeset
|
102 |
|
41c81e23c08d
removed Hoare/hoare.ML, Hoare/hoareAbort.ML, ex/svc_oracle.ML (which can be mistaken as attached ML script on case-insensitive file-system);
wenzelm
parents:
21588
diff
changeset
|
103 |
subsection {* Derivation of the proof rules and, most importantly, the VCG tactic *} |
41c81e23c08d
removed Hoare/hoare.ML, Hoare/hoareAbort.ML, ex/svc_oracle.ML (which can be mistaken as attached ML script on case-insensitive file-system);
wenzelm
parents:
21588
diff
changeset
|
104 |
|
41c81e23c08d
removed Hoare/hoare.ML, Hoare/hoareAbort.ML, ex/svc_oracle.ML (which can be mistaken as attached ML script on case-insensitive file-system);
wenzelm
parents:
21588
diff
changeset
|
105 |
lemma Compl_Collect: "-(Collect b) = {x. ~(b x)}" |
41c81e23c08d
removed Hoare/hoare.ML, Hoare/hoareAbort.ML, ex/svc_oracle.ML (which can be mistaken as attached ML script on case-insensitive file-system);
wenzelm
parents:
21588
diff
changeset
|
106 |
by blast |
41c81e23c08d
removed Hoare/hoare.ML, Hoare/hoareAbort.ML, ex/svc_oracle.ML (which can be mistaken as attached ML script on case-insensitive file-system);
wenzelm
parents:
21588
diff
changeset
|
107 |
|
28457
25669513fd4c
major cleanup of hoare_tac.ML: just one copy for Hoare.thy and HoareAbort.thy (only 1 line different), refrain from inspecting the main goal, proper context;
wenzelm
parents:
27330
diff
changeset
|
108 |
use "hoare_tac.ML" |
13857 | 109 |
|
110 |
method_setup vcg = {* |
|
30549 | 111 |
Scan.succeed (fn ctxt => SIMPLE_METHOD' (hoare_tac ctxt (K all_tac))) *} |
13857 | 112 |
"verification condition generator" |
113 |
||
114 |
method_setup vcg_simp = {* |
|
30549 | 115 |
Scan.succeed (fn ctxt => |
32149
ef59550a55d3
renamed simpset_of to global_simpset_of, and local_simpset_of to simpset_of -- same for claset and clasimpset;
wenzelm
parents:
32134
diff
changeset
|
116 |
SIMPLE_METHOD' (hoare_tac ctxt (asm_full_simp_tac (simpset_of ctxt)))) *} |
13857 | 117 |
"verification condition generator plus simplification" |
118 |
||
13875 | 119 |
(* Special syntax for guarded statements and guarded array updates: *) |
120 |
||
121 |
syntax |
|
35352 | 122 |
"_guarded_com" :: "bool \<Rightarrow> 'a com \<Rightarrow> 'a com" ("(2_ \<rightarrow>/ _)" 71) |
123 |
"_array_update" :: "'a list \<Rightarrow> nat \<Rightarrow> 'a \<Rightarrow> 'a com" ("(2_[_] :=/ _)" [70, 65] 61) |
|
13875 | 124 |
translations |
35101 | 125 |
"P \<rightarrow> c" == "IF P THEN c ELSE CONST Abort FI" |
34940 | 126 |
"a[i] := v" => "(i < CONST length a) \<rightarrow> (a := CONST list_update a i v)" |
13875 | 127 |
(* reverse translation not possible because of duplicate "a" *) |
128 |
||
129 |
text{* Note: there is no special syntax for guarded array access. Thus |
|
130 |
you must write @{text"j < length a \<rightarrow> a[i] := a!j"}. *} |
|
131 |
||
13857 | 132 |
end |