author | paulson |
Tue, 05 May 1998 17:28:22 +0200 | |
changeset 4896 | 4727272f3db6 |
parent 4776 | 1f9362e769c1 |
child 5069 | 3ea049f7979d |
permissions | -rw-r--r-- |
4776 | 1 |
(* Title: HOL/UNITY/Reach.thy |
2 |
ID: $Id$ |
|
3 |
Author: Lawrence C Paulson, Cambridge University Computer Laboratory |
|
4 |
Copyright 1998 University of Cambridge |
|
5 |
||
6 |
Reachability in Directed Graphs. From Chandy and Misra, section 6.4. |
|
4896
4727272f3db6
New syntax for function update; moved to main HOL directory
paulson
parents:
4776
diff
changeset
|
7 |
[ this example took only four days!] |
4776 | 8 |
*) |
9 |
||
10 |
open Reach; |
|
11 |
||
12 |
(*TO SIMPDATA.ML?? FOR CLASET?? *) |
|
13 |
val major::prems = goal thy |
|
14 |
"[| if P then Q else R; \ |
|
15 |
\ [| P; Q |] ==> S; \ |
|
16 |
\ [| ~ P; R |] ==> S |] ==> S"; |
|
17 |
by (cut_facts_tac [major] 1); |
|
18 |
by (blast_tac (claset() addSDs [if_bool_eq_disj RS iffD1] addIs prems) 1); |
|
19 |
qed "ifE"; |
|
20 |
||
21 |
AddSEs [ifE]; |
|
22 |
||
23 |
||
24 |
val cmd_defs = [racts_def, asgt_def, update_def]; |
|
25 |
||
26 |
goalw thy [racts_def] "id : racts"; |
|
27 |
by (Simp_tac 1); |
|
28 |
qed "id_in_racts"; |
|
29 |
AddIffs [id_in_racts]; |
|
30 |
||
31 |
(*All vertex sets are finite*) |
|
32 |
AddIffs [[subset_UNIV, finite_graph] MRS finite_subset]; |
|
33 |
||
34 |
||
35 |
(** Constrains/Ensures tactics: NEED TO BE GENERALIZED OVER ALL PROGRAMS **) |
|
36 |
||
37 |
(*proves "constrains" properties when the program is specified*) |
|
38 |
val constrains_tac = |
|
39 |
SELECT_GOAL |
|
40 |
(EVERY [rtac constrainsI 1, |
|
41 |
rewtac racts_def, |
|
42 |
REPEAT_FIRST (eresolve_tac [insertE, emptyE]), |
|
43 |
rewrite_goals_tac [racts_def, asgt_def], |
|
44 |
ALLGOALS (SELECT_GOAL Auto_tac)]); |
|
45 |
||
46 |
||
47 |
(*proves "ensures" properties when the program is specified*) |
|
48 |
fun ensures_tac sact = |
|
49 |
SELECT_GOAL |
|
50 |
(EVERY [REPEAT (resolve_tac [LeadsTo_Basis, leadsTo_Basis, ensuresI] 1), |
|
51 |
res_inst_tac [("act", sact)] transient_mem 2, |
|
52 |
Simp_tac 2, |
|
53 |
constrains_tac 1, |
|
54 |
rewrite_goals_tac [racts_def, asgt_def], |
|
55 |
Auto_tac]); |
|
56 |
||
57 |
||
58 |
goalw thy [stable_def, invariant_def] |
|
59 |
"stable racts invariant"; |
|
60 |
by (constrains_tac 1); |
|
61 |
by (blast_tac (claset() addIs [r_into_rtrancl,rtrancl_trans]) 1); |
|
62 |
qed "stable_invariant"; |
|
63 |
||
64 |
goalw thy [rinit_def, invariant_def] "rinit <= invariant"; |
|
65 |
by Auto_tac; |
|
66 |
qed "rinit_invariant"; |
|
67 |
||
68 |
goal thy "reachable rinit racts <= invariant"; |
|
69 |
by (simp_tac (simpset() addsimps |
|
70 |
[strongest_invariant, stable_invariant, rinit_invariant]) 1); |
|
71 |
qed "reachable_subset_invariant"; |
|
72 |
||
73 |
val reachable_subset_invariant' = |
|
74 |
rewrite_rule [invariant_def] reachable_subset_invariant; |
|
75 |
||
76 |
||
77 |
(*** Fixedpoint ***) |
|
78 |
||
79 |
(*If it reaches a fixedpoint, it has found a solution*) |
|
80 |
goalw thy [fixedpoint_def, invariant_def] |
|
81 |
"fixedpoint Int invariant = { %v. (init, v) : edges^* }"; |
|
82 |
by (rtac equalityI 1); |
|
83 |
by (blast_tac (claset() addIs [r_into_rtrancl,rtrancl_trans]) 2); |
|
84 |
by (auto_tac (claset() addSIs [ext], simpset())); |
|
85 |
by (etac rtrancl_induct 1); |
|
86 |
by Auto_tac; |
|
87 |
qed "fixedpoint_invariant_correct"; |
|
88 |
||
89 |
goalw thy (cmd_defs @ [FP_def, fixedpoint_def, stable_def, constrains_def]) |
|
90 |
"FP racts <= fixedpoint"; |
|
91 |
by Auto_tac; |
|
92 |
by (dtac bspec 1); |
|
93 |
by (Blast_tac 1); |
|
94 |
by (asm_full_simp_tac (simpset() addsimps [Image_singleton, image_iff]) 1); |
|
95 |
by (dtac fun_cong 1); |
|
96 |
by Auto_tac; |
|
97 |
val lemma1 = result(); |
|
98 |
||
99 |
goalw thy (cmd_defs @ [FP_def, fixedpoint_def, stable_def, constrains_def]) |
|
100 |
"fixedpoint <= FP racts"; |
|
101 |
by (auto_tac (claset() addIs [ext], simpset())); |
|
102 |
val lemma2 = result(); |
|
103 |
||
104 |
goal thy "FP racts = fixedpoint"; |
|
105 |
by (rtac ([lemma1,lemma2] MRS equalityI) 1); |
|
106 |
qed "FP_fixedpoint"; |
|
107 |
||
108 |
||
109 |
(*If we haven't reached a fixedpoint then there is some edge for which u but |
|
110 |
not v holds. Progress will be proved via an ENSURES assertion that the |
|
111 |
metric will decrease for each suitable edge. A union over all edges proves |
|
112 |
a LEADSTO assertion that the metric decreases if we are not at a fixedpoint. |
|
113 |
*) |
|
114 |
||
115 |
goal thy "Compl fixedpoint = (UN (u,v): edges. {s. s u & ~ s v})"; |
|
116 |
by (simp_tac (simpset() addsimps |
|
117 |
([Compl_FP, UN_UN_flatten, FP_fixedpoint RS sym, |
|
118 |
racts_def, asgt_def])) 1); |
|
119 |
by Safe_tac; |
|
120 |
by (rtac update_idem 1); |
|
121 |
by (Blast_tac 1); |
|
122 |
by (Full_simp_tac 1); |
|
123 |
by (REPEAT (dtac bspec 1 THEN Simp_tac 1)); |
|
124 |
by (dtac subsetD 1); |
|
125 |
by (Simp_tac 1); |
|
126 |
by (asm_full_simp_tac (simpset() addsimps [update_idem_iff]) 1); |
|
127 |
qed "Compl_fixedpoint"; |
|
128 |
||
129 |
goal thy "A - fixedpoint = (UN (u,v): edges. A Int {s. s u & ~ s v})"; |
|
130 |
by (simp_tac (simpset() addsimps [Diff_eq, Compl_fixedpoint]) 1); |
|
131 |
by (Blast_tac 1); |
|
132 |
qed "Diff_fixedpoint"; |
|
133 |
||
134 |
||
135 |
(*** Progress ***) |
|
136 |
||
4896
4727272f3db6
New syntax for function update; moved to main HOL directory
paulson
parents:
4776
diff
changeset
|
137 |
goalw thy [metric_def] "!!s. ~ s x ==> Suc (metric (s[x:=True])) = metric s"; |
4727272f3db6
New syntax for function update; moved to main HOL directory
paulson
parents:
4776
diff
changeset
|
138 |
by (subgoal_tac "{v. ~ (s[x:=True]) v} = {v. ~ s v} - {x}" 1); |
4776 | 139 |
by Auto_tac; |
140 |
by (asm_simp_tac (simpset() addsimps [card_Suc_Diff]) 1); |
|
141 |
qed "Suc_metric"; |
|
142 |
||
4896
4727272f3db6
New syntax for function update; moved to main HOL directory
paulson
parents:
4776
diff
changeset
|
143 |
goal thy "!!s. ~ s x ==> metric (s[x:=True]) < metric s"; |
4776 | 144 |
by (etac (Suc_metric RS subst) 1); |
145 |
by (Blast_tac 1); |
|
146 |
qed "metric_less"; |
|
147 |
AddSIs [metric_less]; |
|
148 |
||
4896
4727272f3db6
New syntax for function update; moved to main HOL directory
paulson
parents:
4776
diff
changeset
|
149 |
goal thy "metric (s[y:=s x | s y]) <= metric s"; |
4776 | 150 |
by (case_tac "s x --> s y" 1); |
151 |
by (auto_tac (claset() addIs [less_imp_le], |
|
152 |
simpset() addsimps [update_idem])); |
|
153 |
qed "metric_le"; |
|
154 |
||
155 |
goal thy "!!m. (u,v): edges ==> \ |
|
156 |
\ ensures racts ((metric-``{m}) Int {s. s u & ~ s v}) \ |
|
157 |
\ (metric-``(lessThan m))"; |
|
158 |
by (ensures_tac "asgt u v" 1); |
|
159 |
by (cut_facts_tac [metric_le] 1); |
|
160 |
by (fast_tac (claset() addSDs [le_imp_less_or_eq]) 1); |
|
161 |
qed "edges_ensures"; |
|
162 |
||
163 |
goal thy "leadsTo racts ((metric-``{m}) - fixedpoint) (metric-``(lessThan m))"; |
|
164 |
by (simp_tac (simpset() addsimps [Diff_fixedpoint]) 1); |
|
165 |
by (rtac leadsTo_UN 1); |
|
166 |
by (split_all_tac 1); |
|
167 |
by (asm_simp_tac (simpset() addsimps [edges_ensures RS leadsTo_Basis]) 1); |
|
168 |
qed "leadsTo_Diff_fixedpoint"; |
|
169 |
||
170 |
goal thy "leadsTo racts (metric-``{m}) (metric-``(lessThan m) Un fixedpoint)"; |
|
171 |
by (rtac (leadsTo_Diff_fixedpoint RS leadsTo_weaken_R RS leadsTo_Diff) 1); |
|
172 |
by (ALLGOALS (blast_tac (claset() addIs [subset_imp_leadsTo]))); |
|
173 |
qed "leadsTo_Un_fixedpoint"; |
|
174 |
||
175 |
||
176 |
(*Execution in any state leads to a fixedpoint (i.e. can terminate)*) |
|
177 |
goal thy "leadsTo racts UNIV fixedpoint"; |
|
178 |
by (rtac lessThan_induct 1); |
|
179 |
by Auto_tac; |
|
180 |
by (rtac leadsTo_Un_fixedpoint 1); |
|
181 |
qed "leadsTo_fixedpoint"; |
|
182 |
||
183 |
goal thy "LeadsTo rinit racts UNIV { %v. (init, v) : edges^* }"; |
|
184 |
by (stac (fixedpoint_invariant_correct RS sym) 1); |
|
185 |
by (rtac (leadsTo_fixedpoint RS leadsTo_imp_LeadsTo RS LeadsTo_weaken_R) 1); |
|
186 |
by (cut_facts_tac [reachable_subset_invariant] 1); |
|
187 |
by (Blast_tac 1); |
|
188 |
qed "LeadsTo_correct"; |
|
189 |