author | wenzelm |
Tue, 23 Oct 2001 22:58:15 +0200 | |
changeset 11914 | bca734def300 |
parent 11704 | 3c50a2cd6f00 |
child 12791 | ccc0f45ad2c4 |
permissions | -rw-r--r-- |
10251 | 1 |
(* Title: HOL/Library/While.thy |
2 |
ID: $Id$ |
|
3 |
Author: Tobias Nipkow |
|
4 |
Copyright 2000 TU Muenchen |
|
5 |
*) |
|
6 |
||
7 |
header {* |
|
8 |
\title{A general ``while'' combinator} |
|
9 |
\author{Tobias Nipkow} |
|
10 |
*} |
|
11 |
||
12 |
theory While_Combinator = Main: |
|
13 |
||
14 |
text {* |
|
15 |
We define a while-combinator @{term while} and prove: (a) an |
|
16 |
unrestricted unfolding law (even if while diverges!) (I got this |
|
17 |
idea from Wolfgang Goerigk), and (b) the invariant rule for reasoning |
|
18 |
about @{term while}. |
|
19 |
*} |
|
20 |
||
21 |
consts while_aux :: "('a => bool) \<times> ('a => 'a) \<times> 'a => 'a" |
|
11626 | 22 |
recdef (permissive) while_aux |
10251 | 23 |
"same_fst (\<lambda>b. True) (\<lambda>b. same_fst (\<lambda>c. True) (\<lambda>c. |
24 |
{(t, s). b s \<and> c s = t \<and> |
|
11701
3d51fbf81c17
sane numerals (stage 1): added generic 1, removed 1' and 2 on nat,
wenzelm
parents:
11626
diff
changeset
|
25 |
\<not> (\<exists>f. f (0::nat) = s \<and> (\<forall>i. b (f i) \<and> c (f i) = f (i + 1)))}))" |
10251 | 26 |
"while_aux (b, c, s) = |
11701
3d51fbf81c17
sane numerals (stage 1): added generic 1, removed 1' and 2 on nat,
wenzelm
parents:
11626
diff
changeset
|
27 |
(if (\<exists>f. f (0::nat) = s \<and> (\<forall>i. b (f i) \<and> c (f i) = f (i + 1))) |
10251 | 28 |
then arbitrary |
29 |
else if b s then while_aux (b, c, c s) |
|
30 |
else s)" |
|
31 |
||
10774 | 32 |
recdef_tc while_aux_tc: while_aux |
33 |
apply (rule wf_same_fst) |
|
34 |
apply (rule wf_same_fst) |
|
35 |
apply (simp add: wf_iff_no_infinite_down_chain) |
|
36 |
apply blast |
|
37 |
done |
|
38 |
||
10251 | 39 |
constdefs |
40 |
while :: "('a => bool) => ('a => 'a) => 'a => 'a" |
|
41 |
"while b c s == while_aux (b, c, s)" |
|
42 |
||
43 |
lemma while_aux_unfold: |
|
44 |
"while_aux (b, c, s) = |
|
11701
3d51fbf81c17
sane numerals (stage 1): added generic 1, removed 1' and 2 on nat,
wenzelm
parents:
11626
diff
changeset
|
45 |
(if \<exists>f. f (0::nat) = s \<and> (\<forall>i. b (f i) \<and> c (f i) = f (i + 1)) |
10251 | 46 |
then arbitrary |
47 |
else if b s then while_aux (b, c, c s) |
|
48 |
else s)" |
|
49 |
apply (rule while_aux_tc [THEN while_aux.simps [THEN trans]]) |
|
50 |
apply (rule refl) |
|
51 |
done |
|
52 |
||
53 |
text {* |
|
54 |
The recursion equation for @{term while}: directly executable! |
|
55 |
*} |
|
56 |
||
57 |
theorem while_unfold: |
|
58 |
"while b c s = (if b s then while b c (c s) else s)" |
|
59 |
apply (unfold while_def) |
|
60 |
apply (rule while_aux_unfold [THEN trans]) |
|
61 |
apply auto |
|
62 |
apply (subst while_aux_unfold) |
|
63 |
apply simp |
|
64 |
apply clarify |
|
65 |
apply (erule_tac x = "\<lambda>i. f (Suc i)" in allE) |
|
66 |
apply blast |
|
67 |
done |
|
68 |
||
10984 | 69 |
hide const while_aux |
70 |
||
10251 | 71 |
text {* |
72 |
The proof rule for @{term while}, where @{term P} is the invariant. |
|
73 |
*} |
|
74 |
||
10653 | 75 |
theorem while_rule_lemma[rule_format]: |
10984 | 76 |
"[| !!s. P s ==> b s ==> P (c s); |
77 |
!!s. P s ==> \<not> b s ==> Q s; |
|
78 |
wf {(t, s). P s \<and> b s \<and> t = c s} |] ==> |
|
10251 | 79 |
P s --> Q (while b c s)" |
80 |
proof - |
|
11549 | 81 |
case rule_context |
10251 | 82 |
assume wf: "wf {(t, s). P s \<and> b s \<and> t = c s}" |
83 |
show ?thesis |
|
84 |
apply (induct s rule: wf [THEN wf_induct]) |
|
85 |
apply simp |
|
86 |
apply clarify |
|
87 |
apply (subst while_unfold) |
|
11549 | 88 |
apply (simp add: rule_context) |
10251 | 89 |
done |
90 |
qed |
|
91 |
||
10653 | 92 |
theorem while_rule: |
10984 | 93 |
"[| P s; |
94 |
!!s. [| P s; b s |] ==> P (c s); |
|
95 |
!!s. [| P s; \<not> b s |] ==> Q s; |
|
10997 | 96 |
wf r; |
10984 | 97 |
!!s. [| P s; b s |] ==> (c s, s) \<in> r |] ==> |
98 |
Q (while b c s)" |
|
10653 | 99 |
apply (rule while_rule_lemma) |
100 |
prefer 4 apply assumption |
|
101 |
apply blast |
|
102 |
apply blast |
|
103 |
apply(erule wf_subset) |
|
104 |
apply blast |
|
105 |
done |
|
106 |
||
10984 | 107 |
text {* |
108 |
\medskip An application: computation of the @{term lfp} on finite |
|
109 |
sets via iteration. |
|
110 |
*} |
|
111 |
||
112 |
theorem lfp_conv_while: |
|
113 |
"[| mono f; finite U; f U = U |] ==> |
|
114 |
lfp f = fst (while (\<lambda>(A, fA). A \<noteq> fA) (\<lambda>(A, fA). (fA, f fA)) ({}, f {}))" |
|
115 |
apply (rule_tac P = "\<lambda>(A, B). (A \<subseteq> U \<and> B = f A \<and> A \<subseteq> B \<and> B \<subseteq> lfp f)" and |
|
11047 | 116 |
r = "((Pow U \<times> UNIV) \<times> (Pow U \<times> UNIV)) \<inter> |
10984 | 117 |
inv_image finite_psubset (op - U o fst)" in while_rule) |
118 |
apply (subst lfp_unfold) |
|
119 |
apply assumption |
|
120 |
apply (simp add: monoD) |
|
121 |
apply (subst lfp_unfold) |
|
122 |
apply assumption |
|
123 |
apply clarsimp |
|
124 |
apply (blast dest: monoD) |
|
125 |
apply (fastsimp intro!: lfp_lowerbound) |
|
126 |
apply (blast intro: wf_finite_psubset Int_lower2 [THEN [2] wf_subset]) |
|
127 |
apply (clarsimp simp add: inv_image_def finite_psubset_def order_less_le) |
|
128 |
apply (blast intro!: finite_Diff dest: monoD) |
|
129 |
done |
|
130 |
||
131 |
||
132 |
text {* |
|
10997 | 133 |
An example of using the @{term while} combinator.\footnote{It is safe |
134 |
to keep the example here, since there is no effect on the current |
|
135 |
theory.} |
|
10984 | 136 |
*} |
137 |
||
11914 | 138 |
theorem "P (lfp (\<lambda>N::int set. {0} \<union> {(n + 2) mod 6 | n. n \<in> N})) = P {0, 4, 2}" |
10997 | 139 |
proof - |
140 |
have aux: "!!f A B. {f n | n. A n \<or> B n} = {f n | n. A n} \<union> {f n | n. B n}" |
|
10984 | 141 |
apply blast |
10997 | 142 |
done |
143 |
show ?thesis |
|
11914 | 144 |
apply (subst lfp_conv_while [where ?U = "{0, 1, 2, 3, 4, 5}"]) |
10997 | 145 |
apply (rule monoI) |
146 |
apply blast |
|
147 |
apply simp |
|
148 |
apply (simp add: aux set_eq_subset) |
|
149 |
txt {* The fixpoint computation is performed purely by rewriting: *} |
|
150 |
apply (simp add: while_unfold aux set_eq_subset del: subset_empty) |
|
151 |
done |
|
152 |
qed |
|
10251 | 153 |
|
154 |
end |