author | clasohm |
Tue, 04 Oct 1994 13:00:20 +0100 | |
changeset 149 | 7cfa79d92a83 |
parent 128 | 89669c58e506 |
permissions | -rw-r--r-- |
0 | 1 |
(* Title: HOL/llist |
2 |
ID: $Id$ |
|
3 |
Author: Lawrence C Paulson, Cambridge University Computer Laboratory |
|
4 |
Copyright 1993 University of Cambridge |
|
5 |
||
2
befa4e9f7c90
Added weak congruence rules to HOL: if_weak_cong, case_weak_cong,
lcp
parents:
0
diff
changeset
|
6 |
SHOULD LListD_Fun_CONS_I, etc., be equations (for rewriting)? |
0 | 7 |
*) |
8 |
||
9 |
open LList; |
|
10 |
||
11 |
(** Simplification **) |
|
12 |
||
105 | 13 |
val llist_ss = univ_ss addcongs [split_weak_cong, sum_case_weak_cong] |
14 |
setloop split_tac [expand_split, expand_sum_case]; |
|
15 |
||
16 |
(*For adding _eqI rules to a simpset; we must remove Pair_eq because |
|
17 |
it may turn an instance of reflexivity into a conjunction!*) |
|
18 |
fun add_eqI ss = ss addsimps [range_eqI, image_eqI] |
|
19 |
delsimps [Pair_eq]; |
|
20 |
||
0 | 21 |
|
128 | 22 |
(*This justifies using llist in other recursive type definitions*) |
23 |
goalw LList.thy llist.defs "!!A B. A<=B ==> llist(A) <= llist(B)"; |
|
24 |
by (rtac gfp_mono 1); |
|
25 |
by (REPEAT (ares_tac basic_monos 1)); |
|
26 |
val llist_mono = result(); |
|
0 | 27 |
|
28 |
||
128 | 29 |
goal LList.thy "llist(A) = {Numb(0)} <+> (A <*> llist(A))"; |
30 |
let val rew = rewrite_rule [NIL_def, CONS_def] in |
|
31 |
by (fast_tac (univ_cs addSIs (equalityI :: map rew llist.intrs) |
|
32 |
addEs [rew llist.elim]) 1) |
|
33 |
end; |
|
34 |
val llist_unfold = result(); |
|
0 | 35 |
|
36 |
||
128 | 37 |
(*** Type checking by coinduction, using list_Fun |
38 |
THE COINDUCTIVE DEFINITION PACKAGE COULD DO THIS! |
|
39 |
***) |
|
0 | 40 |
|
128 | 41 |
goalw LList.thy [list_Fun_def] |
42 |
"!!M. [| M : X; X <= list_Fun(A, X Un llist(A)) |] ==> M : llist(A)"; |
|
43 |
be llist.coinduct 1; |
|
44 |
be (subsetD RS CollectD) 1; |
|
45 |
ba 1; |
|
46 |
val llist_coinduct = result(); |
|
0 | 47 |
|
128 | 48 |
goalw LList.thy [list_Fun_def, NIL_def] "NIL: list_Fun(A,X)"; |
49 |
by (fast_tac set_cs 1); |
|
50 |
val list_Fun_NIL_I = result(); |
|
51 |
||
52 |
goalw LList.thy [list_Fun_def,CONS_def] |
|
53 |
"!!M N. [| M: A; N: X |] ==> CONS(M,N) : list_Fun(A,X)"; |
|
54 |
by (fast_tac set_cs 1); |
|
55 |
val list_Fun_CONS_I = result(); |
|
0 | 56 |
|
2
befa4e9f7c90
Added weak congruence rules to HOL: if_weak_cong, case_weak_cong,
lcp
parents:
0
diff
changeset
|
57 |
(*Utilise the "strong" part, i.e. gfp(f)*) |
128 | 58 |
goalw LList.thy (llist.defs @ [list_Fun_def]) |
59 |
"!!M N. M: llist(A) ==> M : list_Fun(A, X Un llist(A))"; |
|
60 |
by (etac (llist.mono RS gfp_fun_UnI2) 1); |
|
61 |
val list_Fun_llist_I = result(); |
|
2
befa4e9f7c90
Added weak congruence rules to HOL: if_weak_cong, case_weak_cong,
lcp
parents:
0
diff
changeset
|
62 |
|
0 | 63 |
(*** LList_corec satisfies the desired recurion equation ***) |
64 |
||
65 |
(*A continuity result?*) |
|
66 |
goalw LList.thy [CONS_def] "CONS(M, UN x.f(x)) = (UN x. CONS(M, f(x)))"; |
|
2
befa4e9f7c90
Added weak congruence rules to HOL: if_weak_cong, case_weak_cong,
lcp
parents:
0
diff
changeset
|
67 |
by (simp_tac (univ_ss addsimps [In1_UN1, Scons_UN1_y]) 1); |
0 | 68 |
val CONS_UN1 = result(); |
69 |
||
105 | 70 |
(*UNUSED; obsolete? |
0 | 71 |
goal Prod.thy "split(p, %x y.UN z.f(x,y,z)) = (UN z. split(p, %x y.f(x,y,z)))"; |
105 | 72 |
by (simp_tac (prod_ss setloop (split_tac [expand_split])) 1); |
0 | 73 |
val split_UN1 = result(); |
105 | 74 |
|
38 | 75 |
goal Sum.thy "sum_case(s,f,%y.UN z.g(y,z)) = (UN z.sum_case(s,f,%y. g(y,z)))"; |
76 |
by (simp_tac (sum_ss setloop (split_tac [expand_sum_case])) 1); |
|
77 |
val sum_case2_UN1 = result(); |
|
78 |
*) |
|
105 | 79 |
|
0 | 80 |
val prems = goalw LList.thy [CONS_def] |
81 |
"[| M<=M'; N<=N' |] ==> CONS(M,N) <= CONS(M',N')"; |
|
82 |
by (REPEAT (resolve_tac ([In1_mono,Scons_mono]@prems) 1)); |
|
83 |
val CONS_mono = result(); |
|
84 |
||
85 |
val corec_fun_simps = [LList_corec_fun_def RS def_nat_rec_0, |
|
86 |
LList_corec_fun_def RS def_nat_rec_Suc]; |
|
87 |
val corec_fun_ss = llist_ss addsimps corec_fun_simps; |
|
88 |
||
89 |
(** The directions of the equality are proved separately **) |
|
90 |
||
91 |
goalw LList.thy [LList_corec_def] |
|
105 | 92 |
"LList_corec(a,f) <= sum_case(%u.NIL, \ |
93 |
\ split(%z w. CONS(z, LList_corec(w,f))), f(a))"; |
|
0 | 94 |
by (rtac UN1_least 1); |
22 | 95 |
by (res_inst_tac [("n","k")] natE 1); |
96 |
by (ALLGOALS (asm_simp_tac corec_fun_ss)); |
|
0 | 97 |
by (REPEAT (resolve_tac [allI, impI, subset_refl RS CONS_mono, UN1_upper] 1)); |
98 |
val LList_corec_subset1 = result(); |
|
99 |
||
100 |
goalw LList.thy [LList_corec_def] |
|
105 | 101 |
"sum_case(%u.NIL, split(%z w. CONS(z, LList_corec(w,f))), f(a)) <= \ |
0 | 102 |
\ LList_corec(a,f)"; |
103 |
by (simp_tac (corec_fun_ss addsimps [CONS_UN1]) 1); |
|
104 |
by (safe_tac set_cs); |
|
105 |
by (ALLGOALS (res_inst_tac [("x","Suc(?k)")] UN1_I THEN' |
|
106 |
asm_simp_tac corec_fun_ss)); |
|
107 |
val LList_corec_subset2 = result(); |
|
108 |
||
109 |
(*the recursion equation for LList_corec -- NOT SUITABLE FOR REWRITING!*) |
|
110 |
goal LList.thy |
|
105 | 111 |
"LList_corec(a,f) = sum_case(%u. NIL, \ |
112 |
\ split(%z w. CONS(z, LList_corec(w,f))), f(a))"; |
|
0 | 113 |
by (REPEAT (resolve_tac [equalityI, LList_corec_subset1, |
114 |
LList_corec_subset2] 1)); |
|
115 |
val LList_corec = result(); |
|
116 |
||
117 |
(*definitional version of same*) |
|
118 |
val [rew] = goal LList.thy |
|
119 |
"[| !!x. h(x) == LList_corec(x,f) |] ==> \ |
|
105 | 120 |
\ h(a) = sum_case(%u.NIL, split(%z w. CONS(z, h(w))), f(a))"; |
0 | 121 |
by (rewtac rew); |
122 |
by (rtac LList_corec 1); |
|
123 |
val def_LList_corec = result(); |
|
124 |
||
125 |
(*A typical use of co-induction to show membership in the gfp. |
|
126 |
Bisimulation is range(%x. LList_corec(x,f)) *) |
|
128 | 127 |
goal LList.thy "LList_corec(a,f) : llist({u.True})"; |
128 |
by (res_inst_tac [("X", "range(%x.LList_corec(x,?g))")] llist_coinduct 1); |
|
0 | 129 |
by (rtac rangeI 1); |
130 |
by (safe_tac set_cs); |
|
131 |
by (stac LList_corec 1); |
|
128 | 132 |
by (simp_tac (llist_ss addsimps [list_Fun_NIL_I, list_Fun_CONS_I, CollectI] |
105 | 133 |
|> add_eqI) 1); |
0 | 134 |
val LList_corec_type = result(); |
135 |
||
136 |
(*Lemma for the proof of llist_corec*) |
|
137 |
goal LList.thy |
|
105 | 138 |
"LList_corec(a, %z.sum_case(Inl, split(%v w.Inr(<Leaf(v),w>)), f(z))) : \ |
128 | 139 |
\ llist(range(Leaf))"; |
140 |
by (res_inst_tac [("X", "range(%x.LList_corec(x,?g))")] llist_coinduct 1); |
|
0 | 141 |
by (rtac rangeI 1); |
142 |
by (safe_tac set_cs); |
|
143 |
by (stac LList_corec 1); |
|
128 | 144 |
by (asm_simp_tac (llist_ss addsimps [list_Fun_NIL_I]) 1); |
145 |
by (fast_tac (set_cs addSIs [list_Fun_CONS_I]) 1); |
|
0 | 146 |
val LList_corec_type2 = result(); |
147 |
||
128 | 148 |
|
149 |
(**** llist equality as a gfp; the bisimulation principle ****) |
|
0 | 150 |
|
128 | 151 |
(*This theorem is actually used, unlike the many similar ones in ZF*) |
152 |
goal LList.thy "LListD(r) = diag({Numb(0)}) <++> (r <**> LListD(r))"; |
|
153 |
let val rew = rewrite_rule [NIL_def, CONS_def] in |
|
154 |
by (fast_tac (univ_cs addSIs (equalityI :: map rew LListD.intrs) |
|
155 |
addEs [rew LListD.elim]) 1) |
|
156 |
end; |
|
157 |
val LListD_unfold = result(); |
|
0 | 158 |
|
159 |
goal LList.thy "!M N. <M,N> : LListD(diag(A)) --> ntrunc(k,M) = ntrunc(k,N)"; |
|
160 |
by (res_inst_tac [("n", "k")] less_induct 1); |
|
161 |
by (safe_tac set_cs); |
|
128 | 162 |
by (etac LListD.elim 1); |
163 |
by (safe_tac (prod_cs addSEs [diagE])); |
|
0 | 164 |
by (res_inst_tac [("n", "n")] natE 1); |
2
befa4e9f7c90
Added weak congruence rules to HOL: if_weak_cong, case_weak_cong,
lcp
parents:
0
diff
changeset
|
165 |
by (asm_simp_tac (univ_ss addsimps [ntrunc_0]) 1); |
128 | 166 |
by (rename_tac "n'" 1); |
167 |
by (res_inst_tac [("n", "n'")] natE 1); |
|
168 |
by (asm_simp_tac (univ_ss addsimps [CONS_def, ntrunc_one_In1]) 1); |
|
169 |
by (asm_simp_tac (univ_ss addsimps [CONS_def, ntrunc_In1, ntrunc_Scons]) 1); |
|
0 | 170 |
val LListD_implies_ntrunc_equality = result(); |
171 |
||
128 | 172 |
(*The domain of the LListD relation*) |
173 |
goalw LList.thy (llist.defs @ [NIL_def, CONS_def]) |
|
174 |
"fst``LListD(diag(A)) <= llist(A)"; |
|
0 | 175 |
by (rtac gfp_upperbound 1); |
128 | 176 |
(*avoids unfolding LListD on the rhs*) |
0 | 177 |
by (res_inst_tac [("P", "%x. fst``x <= ?B")] (LListD_unfold RS ssubst) 1); |
2
befa4e9f7c90
Added weak congruence rules to HOL: if_weak_cong, case_weak_cong,
lcp
parents:
0
diff
changeset
|
178 |
by (simp_tac fst_image_ss 1); |
128 | 179 |
by (fast_tac univ_cs 1); |
0 | 180 |
val fst_image_LListD = result(); |
181 |
||
182 |
(*This inclusion justifies the use of coinduction to show M=N*) |
|
128 | 183 |
goal LList.thy "LListD(diag(A)) <= diag(llist(A))"; |
0 | 184 |
by (rtac subsetI 1); |
185 |
by (res_inst_tac [("p","x")] PairE 1); |
|
186 |
by (safe_tac HOL_cs); |
|
128 | 187 |
by (rtac diag_eqI 1); |
0 | 188 |
by (rtac (LListD_implies_ntrunc_equality RS spec RS spec RS mp RS |
189 |
ntrunc_equality) 1); |
|
190 |
by (assume_tac 1); |
|
191 |
by (etac (fst_imageI RS (fst_image_LListD RS subsetD)) 1); |
|
192 |
val LListD_subset_diag = result(); |
|
193 |
||
128 | 194 |
(** Coinduction, using LListD_Fun |
195 |
THE COINDUCTIVE DEFINITION PACKAGE COULD DO THIS! |
|
196 |
**) |
|
197 |
||
198 |
goalw LList.thy [LListD_Fun_def] |
|
199 |
"!!M. [| M : X; X <= LListD_Fun(r, X Un LListD(r)) |] ==> M : LListD(r)"; |
|
200 |
be LListD.coinduct 1; |
|
201 |
be (subsetD RS CollectD) 1; |
|
202 |
ba 1; |
|
203 |
val LListD_coinduct = result(); |
|
204 |
||
205 |
goalw LList.thy [LListD_Fun_def,NIL_def] "<NIL,NIL> : LListD_Fun(r,s)"; |
|
206 |
by (fast_tac set_cs 1); |
|
207 |
val LListD_Fun_NIL_I = result(); |
|
208 |
||
209 |
goalw LList.thy [LListD_Fun_def,CONS_def] |
|
210 |
"!!x. [| x:A; <M,N>:s |] ==> <CONS(x,M), CONS(x,N)> : LListD_Fun(diag(A),s)"; |
|
211 |
by (fast_tac univ_cs 1); |
|
212 |
val LListD_Fun_CONS_I = result(); |
|
213 |
||
214 |
(*Utilise the "strong" part, i.e. gfp(f)*) |
|
215 |
goalw LList.thy (LListD.defs @ [LListD_Fun_def]) |
|
216 |
"!!M N. M: LListD(r) ==> M : LListD_Fun(r, X Un LListD(r))"; |
|
217 |
by (etac (LListD.mono RS gfp_fun_UnI2) 1); |
|
218 |
val LListD_Fun_LListD_I = result(); |
|
219 |
||
220 |
||
221 |
(*This converse inclusion helps to strengthen llist_equalityI*) |
|
222 |
goal LList.thy "diag(llist(A)) <= LListD(diag(A))"; |
|
0 | 223 |
by (rtac subsetI 1); |
128 | 224 |
by (etac LListD_coinduct 1); |
225 |
by (rtac subsetI 1); |
|
226 |
by (eresolve_tac [diagE] 1); |
|
227 |
by (eresolve_tac [ssubst] 1); |
|
228 |
by (eresolve_tac [llist.elim] 1); |
|
229 |
by (ALLGOALS |
|
230 |
(asm_simp_tac (llist_ss addsimps [diagI, LListD_Fun_NIL_I, |
|
231 |
LListD_Fun_CONS_I]))); |
|
0 | 232 |
val diag_subset_LListD = result(); |
233 |
||
128 | 234 |
goal LList.thy "LListD(diag(A)) = diag(llist(A))"; |
0 | 235 |
by (REPEAT (resolve_tac [equalityI, LListD_subset_diag, |
236 |
diag_subset_LListD] 1)); |
|
237 |
val LListD_eq_diag = result(); |
|
238 |
||
128 | 239 |
goal LList.thy |
240 |
"!!M N. M: llist(A) ==> <M,M> : LListD_Fun(diag(A), X Un diag(llist(A)))"; |
|
241 |
by (rtac (LListD_eq_diag RS subst) 1); |
|
242 |
br LListD_Fun_LListD_I 1; |
|
243 |
by (asm_simp_tac (HOL_ss addsimps [LListD_eq_diag, diagI]) 1); |
|
244 |
val LListD_Fun_diag_I = result(); |
|
245 |
||
246 |
||
2
befa4e9f7c90
Added weak congruence rules to HOL: if_weak_cong, case_weak_cong,
lcp
parents:
0
diff
changeset
|
247 |
(** To show two LLists are equal, exhibit a bisimulation! |
befa4e9f7c90
Added weak congruence rules to HOL: if_weak_cong, case_weak_cong,
lcp
parents:
0
diff
changeset
|
248 |
[also admits true equality] |
befa4e9f7c90
Added weak congruence rules to HOL: if_weak_cong, case_weak_cong,
lcp
parents:
0
diff
changeset
|
249 |
Replace "A" by some particular set, like {x.True}??? *) |
befa4e9f7c90
Added weak congruence rules to HOL: if_weak_cong, case_weak_cong,
lcp
parents:
0
diff
changeset
|
250 |
goal LList.thy |
128 | 251 |
"!!r. [| <M,N> : r; r <= LListD_Fun(diag(A), r Un diag(llist(A))) \ |
2
befa4e9f7c90
Added weak congruence rules to HOL: if_weak_cong, case_weak_cong,
lcp
parents:
0
diff
changeset
|
252 |
\ |] ==> M=N"; |
128 | 253 |
by (rtac (LListD_subset_diag RS subsetD RS diagE) 1); |
254 |
by (etac LListD_coinduct 1); |
|
255 |
by (asm_simp_tac (HOL_ss addsimps [LListD_eq_diag]) 1); |
|
256 |
by (safe_tac prod_cs); |
|
257 |
val llist_equalityI = result(); |
|
2
befa4e9f7c90
Added weak congruence rules to HOL: if_weak_cong, case_weak_cong,
lcp
parents:
0
diff
changeset
|
258 |
|
0 | 259 |
|
128 | 260 |
(*** Finality of llist(A): Uniqueness of functions defined by corecursion ***) |
0 | 261 |
|
262 |
(*abstract proof using a bisimulation*) |
|
263 |
val [prem1,prem2] = goal LList.thy |
|
105 | 264 |
"[| !!x. h1(x) = sum_case(%u.NIL, split(%z w. CONS(z,h1(w))), f(x)); \ |
265 |
\ !!x. h2(x) = sum_case(%u.NIL, split(%z w. CONS(z,h2(w))), f(x)) |]\ |
|
0 | 266 |
\ ==> h1=h2"; |
267 |
by (rtac ext 1); |
|
268 |
(*next step avoids an unknown (and flexflex pair) in simplification*) |
|
269 |
by (res_inst_tac [("A", "{u.True}"), |
|
128 | 270 |
("r", "range(%u. <h1(u),h2(u)>)")] llist_equalityI 1); |
0 | 271 |
by (rtac rangeI 1); |
272 |
by (safe_tac set_cs); |
|
273 |
by (stac prem1 1); |
|
274 |
by (stac prem2 1); |
|
105 | 275 |
by (simp_tac (llist_ss addsimps [LListD_Fun_NIL_I, |
276 |
CollectI RS LListD_Fun_CONS_I] |
|
277 |
|> add_eqI) 1); |
|
0 | 278 |
val LList_corec_unique = result(); |
279 |
||
280 |
val [prem] = goal LList.thy |
|
105 | 281 |
"[| !!x. h(x) = sum_case(%u.NIL, split(%z w. CONS(z,h(w))), f(x)) |] \ |
0 | 282 |
\ ==> h = (%x.LList_corec(x,f))"; |
283 |
by (rtac (LList_corec RS (prem RS LList_corec_unique)) 1); |
|
284 |
val equals_LList_corec = result(); |
|
285 |
||
286 |
||
287 |
(** Obsolete LList_corec_unique proof: complete induction, not coinduction **) |
|
288 |
||
289 |
goalw LList.thy [CONS_def] "ntrunc(Suc(0), CONS(M,N)) = {}"; |
|
290 |
by (rtac ntrunc_one_In1 1); |
|
291 |
val ntrunc_one_CONS = result(); |
|
292 |
||
293 |
goalw LList.thy [CONS_def] |
|
294 |
"ntrunc(Suc(Suc(k)), CONS(M,N)) = CONS (ntrunc(k,M), ntrunc(k,N))"; |
|
2
befa4e9f7c90
Added weak congruence rules to HOL: if_weak_cong, case_weak_cong,
lcp
parents:
0
diff
changeset
|
295 |
by (simp_tac (HOL_ss addsimps [ntrunc_Scons,ntrunc_In1]) 1); |
0 | 296 |
val ntrunc_CONS = result(); |
297 |
||
298 |
val [prem1,prem2] = goal LList.thy |
|
105 | 299 |
"[| !!x. h1(x) = sum_case(%u.NIL, split(%z w. CONS(z,h1(w))), f(x)); \ |
300 |
\ !!x. h2(x) = sum_case(%u.NIL, split(%z w. CONS(z,h2(w))), f(x)) |]\ |
|
0 | 301 |
\ ==> h1=h2"; |
302 |
by (rtac (ntrunc_equality RS ext) 1); |
|
303 |
by (res_inst_tac [("x", "x")] spec 1); |
|
304 |
by (res_inst_tac [("n", "k")] less_induct 1); |
|
305 |
by (rtac allI 1); |
|
306 |
by (stac prem1 1); |
|
307 |
by (stac prem2 1); |
|
38 | 308 |
by (simp_tac (sum_ss setloop (split_tac [expand_split,expand_sum_case])) 1); |
0 | 309 |
by (strip_tac 1); |
310 |
by (res_inst_tac [("n", "n")] natE 1); |
|
311 |
by (res_inst_tac [("n", "xc")] natE 2); |
|
2
befa4e9f7c90
Added weak congruence rules to HOL: if_weak_cong, case_weak_cong,
lcp
parents:
0
diff
changeset
|
312 |
by (ALLGOALS(asm_simp_tac(nat_ss addsimps |
0 | 313 |
[ntrunc_0,ntrunc_one_CONS,ntrunc_CONS]))); |
314 |
val LList_corec_unique = result(); |
|
315 |
||
316 |
||
317 |
(*** Lconst -- defined directly using lfp, but equivalent to a LList_corec ***) |
|
318 |
||
319 |
goal LList.thy "mono(CONS(M))"; |
|
320 |
by (REPEAT (ares_tac [monoI, subset_refl, CONS_mono] 1)); |
|
321 |
val Lconst_fun_mono = result(); |
|
322 |
||
323 |
(* Lconst(M) = CONS(M,Lconst(M)) *) |
|
324 |
val Lconst = standard (Lconst_fun_mono RS (Lconst_def RS def_lfp_Tarski)); |
|
325 |
||
326 |
(*A typical use of co-induction to show membership in the gfp. |
|
327 |
The containing set is simply the singleton {Lconst(M)}. *) |
|
128 | 328 |
goal LList.thy "!!M A. M:A ==> Lconst(M): llist(A)"; |
329 |
by (rtac (singletonI RS llist_coinduct) 1); |
|
0 | 330 |
by (safe_tac set_cs); |
331 |
by (res_inst_tac [("P", "%u. u: ?A")] (Lconst RS ssubst) 1); |
|
128 | 332 |
by (REPEAT (ares_tac [list_Fun_CONS_I, singletonI, UnI1] 1)); |
0 | 333 |
val Lconst_type = result(); |
334 |
||
335 |
goal LList.thy "Lconst(M) = LList_corec(M, %x.Inr(<x,x>))"; |
|
336 |
by (rtac (equals_LList_corec RS fun_cong) 1); |
|
2
befa4e9f7c90
Added weak congruence rules to HOL: if_weak_cong, case_weak_cong,
lcp
parents:
0
diff
changeset
|
337 |
by (simp_tac sum_ss 1); |
0 | 338 |
by (rtac Lconst 1); |
339 |
val Lconst_eq_LList_corec = result(); |
|
340 |
||
341 |
(*Thus we could have used gfp in the definition of Lconst*) |
|
342 |
goal LList.thy "gfp(%N. CONS(M, N)) = LList_corec(M, %x.Inr(<x,x>))"; |
|
343 |
by (rtac (equals_LList_corec RS fun_cong) 1); |
|
2
befa4e9f7c90
Added weak congruence rules to HOL: if_weak_cong, case_weak_cong,
lcp
parents:
0
diff
changeset
|
344 |
by (simp_tac sum_ss 1); |
0 | 345 |
by (rtac (Lconst_fun_mono RS gfp_Tarski) 1); |
346 |
val gfp_Lconst_eq_LList_corec = result(); |
|
347 |
||
348 |
||
349 |
(*** Isomorphisms ***) |
|
350 |
||
128 | 351 |
goal LList.thy "inj(Rep_llist)"; |
0 | 352 |
by (rtac inj_inverseI 1); |
128 | 353 |
by (rtac Rep_llist_inverse 1); |
354 |
val inj_Rep_llist = result(); |
|
0 | 355 |
|
128 | 356 |
goal LList.thy "inj_onto(Abs_llist,llist(range(Leaf)))"; |
0 | 357 |
by (rtac inj_onto_inverseI 1); |
128 | 358 |
by (etac Abs_llist_inverse 1); |
359 |
val inj_onto_Abs_llist = result(); |
|
0 | 360 |
|
361 |
(** Distinctness of constructors **) |
|
362 |
||
363 |
goalw LList.thy [LNil_def,LCons_def] "~ LCons(x,xs) = LNil"; |
|
128 | 364 |
by (rtac (CONS_not_NIL RS (inj_onto_Abs_llist RS inj_onto_contraD)) 1); |
365 |
by (REPEAT (resolve_tac (llist.intrs @ [rangeI, Rep_llist]) 1)); |
|
0 | 366 |
val LCons_not_LNil = result(); |
367 |
||
368 |
val LNil_not_LCons = standard (LCons_not_LNil RS not_sym); |
|
369 |
||
370 |
val LCons_neq_LNil = standard (LCons_not_LNil RS notE); |
|
371 |
val LNil_neq_LCons = sym RS LCons_neq_LNil; |
|
372 |
||
373 |
(** llist constructors **) |
|
374 |
||
375 |
goalw LList.thy [LNil_def] |
|
128 | 376 |
"Rep_llist(LNil) = NIL"; |
377 |
by (rtac (llist.NIL_I RS Abs_llist_inverse) 1); |
|
378 |
val Rep_llist_LNil = result(); |
|
0 | 379 |
|
380 |
goalw LList.thy [LCons_def] |
|
128 | 381 |
"Rep_llist(LCons(x,l)) = CONS(Leaf(x),Rep_llist(l))"; |
382 |
by (REPEAT (resolve_tac [llist.CONS_I RS Abs_llist_inverse, |
|
383 |
rangeI, Rep_llist] 1)); |
|
384 |
val Rep_llist_LCons = result(); |
|
0 | 385 |
|
386 |
(** Injectiveness of CONS and LCons **) |
|
387 |
||
388 |
goalw LList.thy [CONS_def] "(CONS(M,N)=CONS(M',N')) = (M=M' & N=N')"; |
|
389 |
by (fast_tac (HOL_cs addSEs [Scons_inject, make_elim In1_inject]) 1); |
|
390 |
val CONS_CONS_eq = result(); |
|
391 |
||
392 |
val CONS_inject = standard (CONS_CONS_eq RS iffD1 RS conjE); |
|
393 |
||
394 |
||
395 |
(*For reasoning about abstract llist constructors*) |
|
128 | 396 |
val llist_cs = set_cs addIs [Rep_llist]@llist.intrs |
0 | 397 |
addSEs [CONS_neq_NIL,NIL_neq_CONS,CONS_inject] |
128 | 398 |
addSDs [inj_onto_Abs_llist RS inj_ontoD, |
399 |
inj_Rep_llist RS injD, Leaf_inject]; |
|
0 | 400 |
|
401 |
goalw LList.thy [LCons_def] "(LCons(x,xs)=LCons(y,ys)) = (x=y & xs=ys)"; |
|
128 | 402 |
by (fast_tac llist_cs 1); |
0 | 403 |
val LCons_LCons_eq = result(); |
404 |
val LCons_inject = standard (LCons_LCons_eq RS iffD1 RS conjE); |
|
405 |
||
128 | 406 |
val [major] = goal LList.thy "CONS(M,N): llist(A) ==> M: A & N: llist(A)"; |
407 |
by (rtac (major RS llist.elim) 1); |
|
0 | 408 |
by (etac CONS_neq_NIL 1); |
128 | 409 |
by (fast_tac llist_cs 1); |
0 | 410 |
val CONS_D = result(); |
411 |
||
412 |
||
128 | 413 |
(****** Reasoning about llist(A) ******) |
0 | 414 |
|
105 | 415 |
(*Don't use llist_ss, as it does case splits!*) |
416 |
val List_case_ss = univ_ss addsimps [List_case_NIL, List_case_CONS]; |
|
0 | 417 |
|
418 |
(*A special case of list_equality for functions over lazy lists*) |
|
128 | 419 |
val [Mlist,gMlist,NILcase,CONScase] = goal LList.thy |
420 |
"[| M: llist(A); g(NIL): llist(A); \ |
|
0 | 421 |
\ f(NIL)=g(NIL); \ |
128 | 422 |
\ !!x l. [| x:A; l: llist(A) |] ==> \ |
0 | 423 |
\ <f(CONS(x,l)),g(CONS(x,l))> : \ |
128 | 424 |
\ LListD_Fun(diag(A), (%u.<f(u),g(u)>)``llist(A) Un \ |
425 |
\ diag(llist(A))) \ |
|
0 | 426 |
\ |] ==> f(M) = g(M)"; |
128 | 427 |
by (rtac llist_equalityI 1); |
428 |
br (Mlist RS imageI) 1; |
|
0 | 429 |
by (rtac subsetI 1); |
430 |
by (etac imageE 1); |
|
431 |
by (etac ssubst 1); |
|
128 | 432 |
by (etac llist.elim 1); |
0 | 433 |
by (etac ssubst 1); |
434 |
by (stac NILcase 1); |
|
128 | 435 |
br (gMlist RS LListD_Fun_diag_I) 1; |
0 | 436 |
by (etac ssubst 1); |
437 |
by (REPEAT (ares_tac [CONScase] 1)); |
|
128 | 438 |
val llist_fun_equalityI = result(); |
0 | 439 |
|
440 |
||
441 |
(*** The functional "Lmap" ***) |
|
442 |
||
443 |
goal LList.thy "Lmap(f,NIL) = NIL"; |
|
444 |
by (rtac (Lmap_def RS def_LList_corec RS trans) 1); |
|
2
befa4e9f7c90
Added weak congruence rules to HOL: if_weak_cong, case_weak_cong,
lcp
parents:
0
diff
changeset
|
445 |
by (simp_tac List_case_ss 1); |
0 | 446 |
val Lmap_NIL = result(); |
447 |
||
448 |
goal LList.thy "Lmap(f, CONS(M,N)) = CONS(f(M), Lmap(f,N))"; |
|
449 |
by (rtac (Lmap_def RS def_LList_corec RS trans) 1); |
|
2
befa4e9f7c90
Added weak congruence rules to HOL: if_weak_cong, case_weak_cong,
lcp
parents:
0
diff
changeset
|
450 |
by (simp_tac List_case_ss 1); |
0 | 451 |
val Lmap_CONS = result(); |
452 |
||
453 |
(*Another type-checking proof by coinduction*) |
|
454 |
val [major,minor] = goal LList.thy |
|
128 | 455 |
"[| M: llist(A); !!x. x:A ==> f(x):B |] ==> Lmap(f,M): llist(B)"; |
456 |
by (rtac (major RS imageI RS llist_coinduct) 1); |
|
0 | 457 |
by (safe_tac set_cs); |
128 | 458 |
by (etac llist.elim 1); |
2
befa4e9f7c90
Added weak congruence rules to HOL: if_weak_cong, case_weak_cong,
lcp
parents:
0
diff
changeset
|
459 |
by (ALLGOALS (asm_simp_tac (HOL_ss addsimps [Lmap_NIL,Lmap_CONS]))); |
128 | 460 |
by (REPEAT (ares_tac [list_Fun_NIL_I, list_Fun_CONS_I, |
2
befa4e9f7c90
Added weak congruence rules to HOL: if_weak_cong, case_weak_cong,
lcp
parents:
0
diff
changeset
|
461 |
minor, imageI, UnI1] 1)); |
0 | 462 |
val Lmap_type = result(); |
463 |
||
464 |
(*This type checking rule synthesises a sufficiently large set for f*) |
|
128 | 465 |
val [major] = goal LList.thy "M: llist(A) ==> Lmap(f,M): llist(f``A)"; |
0 | 466 |
by (rtac (major RS Lmap_type) 1); |
467 |
by (etac imageI 1); |
|
468 |
val Lmap_type2 = result(); |
|
469 |
||
470 |
(** Two easy results about Lmap **) |
|
471 |
||
66 | 472 |
val [prem] = goalw LList.thy [o_def] |
128 | 473 |
"M: llist(A) ==> Lmap(f o g, M) = Lmap(f, Lmap(g, M))"; |
474 |
by (rtac (prem RS imageI RS llist_equalityI) 1); |
|
0 | 475 |
by (safe_tac set_cs); |
128 | 476 |
by (etac llist.elim 1); |
2
befa4e9f7c90
Added weak congruence rules to HOL: if_weak_cong, case_weak_cong,
lcp
parents:
0
diff
changeset
|
477 |
by (ALLGOALS (asm_simp_tac (HOL_ss addsimps [Lmap_NIL,Lmap_CONS]))); |
befa4e9f7c90
Added weak congruence rules to HOL: if_weak_cong, case_weak_cong,
lcp
parents:
0
diff
changeset
|
478 |
by (REPEAT (ares_tac [LListD_Fun_NIL_I, imageI, UnI1, |
befa4e9f7c90
Added weak congruence rules to HOL: if_weak_cong, case_weak_cong,
lcp
parents:
0
diff
changeset
|
479 |
rangeI RS LListD_Fun_CONS_I] 1)); |
0 | 480 |
val Lmap_compose = result(); |
481 |
||
128 | 482 |
val [prem] = goal LList.thy "M: llist(A) ==> Lmap(%x.x, M) = M"; |
483 |
by (rtac (prem RS imageI RS llist_equalityI) 1); |
|
0 | 484 |
by (safe_tac set_cs); |
128 | 485 |
by (etac llist.elim 1); |
2
befa4e9f7c90
Added weak congruence rules to HOL: if_weak_cong, case_weak_cong,
lcp
parents:
0
diff
changeset
|
486 |
by (ALLGOALS (asm_simp_tac (HOL_ss addsimps [Lmap_NIL,Lmap_CONS]))); |
befa4e9f7c90
Added weak congruence rules to HOL: if_weak_cong, case_weak_cong,
lcp
parents:
0
diff
changeset
|
487 |
by (REPEAT (ares_tac [LListD_Fun_NIL_I, imageI RS UnI1, |
befa4e9f7c90
Added weak congruence rules to HOL: if_weak_cong, case_weak_cong,
lcp
parents:
0
diff
changeset
|
488 |
rangeI RS LListD_Fun_CONS_I] 1)); |
0 | 489 |
val Lmap_ident = result(); |
490 |
||
491 |
||
492 |
(*** Lappend -- its two arguments cause some complications! ***) |
|
493 |
||
494 |
goalw LList.thy [Lappend_def] "Lappend(NIL,NIL) = NIL"; |
|
495 |
by (rtac (LList_corec RS trans) 1); |
|
2
befa4e9f7c90
Added weak congruence rules to HOL: if_weak_cong, case_weak_cong,
lcp
parents:
0
diff
changeset
|
496 |
by (simp_tac List_case_ss 1); |
0 | 497 |
val Lappend_NIL_NIL = result(); |
498 |
||
499 |
goalw LList.thy [Lappend_def] |
|
500 |
"Lappend(NIL,CONS(N,N')) = CONS(N, Lappend(NIL,N'))"; |
|
501 |
by (rtac (LList_corec RS trans) 1); |
|
2
befa4e9f7c90
Added weak congruence rules to HOL: if_weak_cong, case_weak_cong,
lcp
parents:
0
diff
changeset
|
502 |
by (simp_tac List_case_ss 1); |
0 | 503 |
val Lappend_NIL_CONS = result(); |
504 |
||
505 |
goalw LList.thy [Lappend_def] |
|
506 |
"Lappend(CONS(M,M'), N) = CONS(M, Lappend(M',N))"; |
|
507 |
by (rtac (LList_corec RS trans) 1); |
|
2
befa4e9f7c90
Added weak congruence rules to HOL: if_weak_cong, case_weak_cong,
lcp
parents:
0
diff
changeset
|
508 |
by (simp_tac List_case_ss 1); |
0 | 509 |
val Lappend_CONS = result(); |
510 |
||
105 | 511 |
val Lappend_ss = |
128 | 512 |
List_case_ss addsimps [llist.NIL_I, Lappend_NIL_NIL, Lappend_NIL_CONS, |
105 | 513 |
Lappend_CONS, LListD_Fun_CONS_I] |
514 |
|> add_eqI; |
|
0 | 515 |
|
128 | 516 |
goal LList.thy "!!M. M: llist(A) ==> Lappend(NIL,M) = M"; |
517 |
by (etac llist_fun_equalityI 1); |
|
0 | 518 |
by (ALLGOALS (asm_simp_tac Lappend_ss)); |
519 |
val Lappend_NIL = result(); |
|
520 |
||
128 | 521 |
goal LList.thy "!!M. M: llist(A) ==> Lappend(M,NIL) = M"; |
522 |
by (etac llist_fun_equalityI 1); |
|
0 | 523 |
by (ALLGOALS (asm_simp_tac Lappend_ss)); |
524 |
val Lappend_NIL2 = result(); |
|
525 |
||
526 |
(** Alternative type-checking proofs for Lappend **) |
|
527 |
||
528 |
(*weak co-induction: bisimulation and case analysis on both variables*) |
|
529 |
goal LList.thy |
|
128 | 530 |
"!!M N. [| M: llist(A); N: llist(A) |] ==> Lappend(M,N): llist(A)"; |
0 | 531 |
by (res_inst_tac |
128 | 532 |
[("X", "UN u:llist(A). UN v: llist(A). {Lappend(u,v)}")] llist_coinduct 1); |
0 | 533 |
by (fast_tac set_cs 1); |
534 |
by (safe_tac set_cs); |
|
128 | 535 |
by (eres_inst_tac [("a", "u")] llist.elim 1); |
536 |
by (eres_inst_tac [("a", "v")] llist.elim 1); |
|
0 | 537 |
by (ALLGOALS |
538 |
(asm_simp_tac Lappend_ss THEN' |
|
128 | 539 |
fast_tac (set_cs addSIs [llist.NIL_I, list_Fun_NIL_I, list_Fun_CONS_I]))); |
0 | 540 |
val Lappend_type = result(); |
541 |
||
542 |
(*strong co-induction: bisimulation and case analysis on one variable*) |
|
543 |
goal LList.thy |
|
128 | 544 |
"!!M N. [| M: llist(A); N: llist(A) |] ==> Lappend(M,N): llist(A)"; |
545 |
by (res_inst_tac [("X", "(%u.Lappend(u,N))``llist(A)")] llist_coinduct 1); |
|
2
befa4e9f7c90
Added weak congruence rules to HOL: if_weak_cong, case_weak_cong,
lcp
parents:
0
diff
changeset
|
546 |
be imageI 1; |
0 | 547 |
br subsetI 1; |
548 |
be imageE 1; |
|
128 | 549 |
by (eres_inst_tac [("a", "u")] llist.elim 1); |
550 |
by (asm_simp_tac (Lappend_ss addsimps [Lappend_NIL, list_Fun_llist_I]) 1); |
|
0 | 551 |
by (asm_simp_tac Lappend_ss 1); |
128 | 552 |
by (fast_tac (set_cs addSIs [list_Fun_CONS_I]) 1); |
0 | 553 |
val Lappend_type = result(); |
554 |
||
555 |
(**** Lazy lists as the type 'a llist -- strongly typed versions of above ****) |
|
556 |
||
557 |
(** llist_case: case analysis for 'a llist **) |
|
558 |
||
128 | 559 |
val Rep_llist_simps = |
0 | 560 |
[List_case_NIL, List_case_CONS, |
128 | 561 |
Abs_llist_inverse, Rep_llist_inverse, |
562 |
Rep_llist, rangeI, inj_Leaf, Inv_f_f] |
|
563 |
@ llist.intrs; |
|
564 |
val Rep_llist_ss = llist_ss addsimps Rep_llist_simps; |
|
0 | 565 |
|
105 | 566 |
goalw LList.thy [llist_case_def,LNil_def] "llist_case(c, d, LNil) = c"; |
128 | 567 |
by (simp_tac Rep_llist_ss 1); |
0 | 568 |
val llist_case_LNil = result(); |
569 |
||
570 |
goalw LList.thy [llist_case_def,LCons_def] |
|
105 | 571 |
"llist_case(c, d, LCons(M,N)) = d(M,N)"; |
128 | 572 |
by (simp_tac Rep_llist_ss 1); |
0 | 573 |
val llist_case_LCons = result(); |
574 |
||
575 |
(*Elimination is case analysis, not induction.*) |
|
576 |
val [prem1,prem2] = goalw LList.thy [NIL_def,CONS_def] |
|
577 |
"[| l=LNil ==> P; !!x l'. l=LCons(x,l') ==> P \ |
|
578 |
\ |] ==> P"; |
|
128 | 579 |
by (rtac (Rep_llist RS llist.elim) 1); |
580 |
by (rtac (inj_Rep_llist RS injD RS prem1) 1); |
|
581 |
by (stac Rep_llist_LNil 1); |
|
0 | 582 |
by (assume_tac 1); |
583 |
by (etac rangeE 1); |
|
128 | 584 |
by (rtac (inj_Rep_llist RS injD RS prem2) 1); |
585 |
by (asm_simp_tac (HOL_ss addsimps [Rep_llist_LCons]) 1); |
|
586 |
by (etac (Abs_llist_inverse RS ssubst) 1); |
|
0 | 587 |
by (rtac refl 1); |
588 |
val llistE = result(); |
|
589 |
||
590 |
(** llist_corec: corecursion for 'a llist **) |
|
591 |
||
592 |
goalw LList.thy [llist_corec_def,LNil_def,LCons_def] |
|
105 | 593 |
"llist_corec(a,f) = sum_case(%u. LNil, \ |
594 |
\ split(%z w. LCons(z, llist_corec(w,f))), f(a))"; |
|
0 | 595 |
by (stac LList_corec 1); |
2
befa4e9f7c90
Added weak congruence rules to HOL: if_weak_cong, case_weak_cong,
lcp
parents:
0
diff
changeset
|
596 |
by (res_inst_tac [("s","f(a)")] sumE 1); |
128 | 597 |
by (asm_simp_tac (llist_ss addsimps [LList_corec_type2,Abs_llist_inverse]) 1); |
2
befa4e9f7c90
Added weak congruence rules to HOL: if_weak_cong, case_weak_cong,
lcp
parents:
0
diff
changeset
|
598 |
by (res_inst_tac [("p","y")] PairE 1); |
128 | 599 |
by (asm_simp_tac (llist_ss addsimps [LList_corec_type2,Abs_llist_inverse]) 1); |
0 | 600 |
(*FIXME: correct case splits usd to be found automatically: |
128 | 601 |
by (ASM_SIMP_TAC(llist_ss addsimps [LList_corec_type2,Abs_llist_inverse]) 1);*) |
0 | 602 |
val llist_corec = result(); |
603 |
||
604 |
(*definitional version of same*) |
|
605 |
val [rew] = goal LList.thy |
|
606 |
"[| !!x. h(x) == llist_corec(x,f) |] ==> \ |
|
105 | 607 |
\ h(a) = sum_case(%u.LNil, split(%z w. LCons(z, h(w))), f(a))"; |
0 | 608 |
by (rewtac rew); |
609 |
by (rtac llist_corec 1); |
|
610 |
val def_llist_corec = result(); |
|
611 |
||
612 |
(**** Proofs about type 'a llist functions ****) |
|
613 |
||
614 |
(*** Deriving llist_equalityI -- llist equality is a bisimulation ***) |
|
615 |
||
128 | 616 |
goalw LList.thy [LListD_Fun_def] |
617 |
"!!r A. r <= Sigma(llist(A), %x.llist(A)) ==> \ |
|
618 |
\ LListD_Fun(diag(A),r) <= Sigma(llist(A), %x.llist(A))"; |
|
619 |
by (stac llist_unfold 1); |
|
620 |
by (simp_tac (HOL_ss addsimps [NIL_def, CONS_def]) 1); |
|
0 | 621 |
by (fast_tac univ_cs 1); |
128 | 622 |
val LListD_Fun_subset_Sigma_llist = result(); |
0 | 623 |
|
624 |
goal LList.thy |
|
128 | 625 |
"prod_fun(Rep_llist,Rep_llist) `` r <= \ |
626 |
\ Sigma(llist(range(Leaf)), %x.llist(range(Leaf)))"; |
|
627 |
by (fast_tac (prod_cs addIs [Rep_llist]) 1); |
|
628 |
val subset_Sigma_llist = result(); |
|
0 | 629 |
|
630 |
val [prem] = goal LList.thy |
|
128 | 631 |
"r <= Sigma(llist(range(Leaf)), %x.llist(range(Leaf))) ==> \ |
632 |
\ prod_fun(Rep_llist o Abs_llist, Rep_llist o Abs_llist) `` r <= r"; |
|
105 | 633 |
by (safe_tac prod_cs); |
0 | 634 |
by (rtac (prem RS subsetD RS SigmaE2) 1); |
635 |
by (assume_tac 1); |
|
128 | 636 |
by (asm_simp_tac (HOL_ss addsimps [o_def,prod_fun,Abs_llist_inverse]) 1); |
0 | 637 |
val prod_fun_lemma = result(); |
638 |
||
2
befa4e9f7c90
Added weak congruence rules to HOL: if_weak_cong, case_weak_cong,
lcp
parents:
0
diff
changeset
|
639 |
goal LList.thy |
128 | 640 |
"prod_fun(Rep_llist, Rep_llist) `` range(%x. <x, x>) = \ |
641 |
\ diag(llist(range(Leaf)))"; |
|
2
befa4e9f7c90
Added weak congruence rules to HOL: if_weak_cong, case_weak_cong,
lcp
parents:
0
diff
changeset
|
642 |
br equalityI 1; |
128 | 643 |
by (fast_tac (univ_cs addIs [Rep_llist]) 1); |
644 |
by (fast_tac (univ_cs addSEs [Abs_llist_inverse RS subst]) 1); |
|
2
befa4e9f7c90
Added weak congruence rules to HOL: if_weak_cong, case_weak_cong,
lcp
parents:
0
diff
changeset
|
645 |
val prod_fun_range_eq_diag = result(); |
befa4e9f7c90
Added weak congruence rules to HOL: if_weak_cong, case_weak_cong,
lcp
parents:
0
diff
changeset
|
646 |
|
befa4e9f7c90
Added weak congruence rules to HOL: if_weak_cong, case_weak_cong,
lcp
parents:
0
diff
changeset
|
647 |
(** To show two llists are equal, exhibit a bisimulation! |
befa4e9f7c90
Added weak congruence rules to HOL: if_weak_cong, case_weak_cong,
lcp
parents:
0
diff
changeset
|
648 |
[also admits true equality] **) |
0 | 649 |
val [prem1,prem2] = goalw LList.thy [llistD_Fun_def] |
2
befa4e9f7c90
Added weak congruence rules to HOL: if_weak_cong, case_weak_cong,
lcp
parents:
0
diff
changeset
|
650 |
"[| <l1,l2> : r; r <= llistD_Fun(r Un range(%x.<x,x>)) |] ==> l1=l2"; |
128 | 651 |
by (rtac (inj_Rep_llist RS injD) 1); |
652 |
by (res_inst_tac [("r", "prod_fun(Rep_llist,Rep_llist)``r"), |
|
2
befa4e9f7c90
Added weak congruence rules to HOL: if_weak_cong, case_weak_cong,
lcp
parents:
0
diff
changeset
|
653 |
("A", "range(Leaf)")] |
128 | 654 |
llist_equalityI 1); |
0 | 655 |
by (rtac (prem1 RS prod_fun_imageI) 1); |
656 |
by (rtac (prem2 RS image_mono RS subset_trans) 1); |
|
657 |
by (rtac (image_compose RS subst) 1); |
|
658 |
by (rtac (prod_fun_compose RS subst) 1); |
|
2
befa4e9f7c90
Added weak congruence rules to HOL: if_weak_cong, case_weak_cong,
lcp
parents:
0
diff
changeset
|
659 |
by (rtac (image_Un RS ssubst) 1); |
befa4e9f7c90
Added weak congruence rules to HOL: if_weak_cong, case_weak_cong,
lcp
parents:
0
diff
changeset
|
660 |
by (stac prod_fun_range_eq_diag 1); |
128 | 661 |
by (rtac (LListD_Fun_subset_Sigma_llist RS prod_fun_lemma) 1); |
662 |
by (rtac (subset_Sigma_llist RS Un_least) 1); |
|
2
befa4e9f7c90
Added weak congruence rules to HOL: if_weak_cong, case_weak_cong,
lcp
parents:
0
diff
changeset
|
663 |
by (rtac diag_subset_Sigma 1); |
0 | 664 |
val llist_equalityI = result(); |
665 |
||
666 |
(** Rules to prove the 2nd premise of llist_equalityI **) |
|
667 |
goalw LList.thy [llistD_Fun_def,LNil_def] "<LNil,LNil> : llistD_Fun(r)"; |
|
668 |
by (rtac (LListD_Fun_NIL_I RS prod_fun_imageI) 1); |
|
669 |
val llistD_Fun_LNil_I = result(); |
|
670 |
||
671 |
val [prem] = goalw LList.thy [llistD_Fun_def,LCons_def] |
|
672 |
"<l1,l2>:r ==> <LCons(x,l1), LCons(x,l2)> : llistD_Fun(r)"; |
|
673 |
by (rtac (rangeI RS LListD_Fun_CONS_I RS prod_fun_imageI) 1); |
|
674 |
by (rtac (prem RS prod_fun_imageI) 1); |
|
675 |
val llistD_Fun_LCons_I = result(); |
|
676 |
||
2
befa4e9f7c90
Added weak congruence rules to HOL: if_weak_cong, case_weak_cong,
lcp
parents:
0
diff
changeset
|
677 |
(*Utilise the "strong" part, i.e. gfp(f)*) |
befa4e9f7c90
Added weak congruence rules to HOL: if_weak_cong, case_weak_cong,
lcp
parents:
0
diff
changeset
|
678 |
goalw LList.thy [llistD_Fun_def] |
befa4e9f7c90
Added weak congruence rules to HOL: if_weak_cong, case_weak_cong,
lcp
parents:
0
diff
changeset
|
679 |
"!!l. <l,l> : llistD_Fun(r Un range(%x.<x,x>))"; |
128 | 680 |
br (Rep_llist_inverse RS subst) 1; |
2
befa4e9f7c90
Added weak congruence rules to HOL: if_weak_cong, case_weak_cong,
lcp
parents:
0
diff
changeset
|
681 |
br prod_fun_imageI 1; |
befa4e9f7c90
Added weak congruence rules to HOL: if_weak_cong, case_weak_cong,
lcp
parents:
0
diff
changeset
|
682 |
by (rtac (image_Un RS ssubst) 1); |
befa4e9f7c90
Added weak congruence rules to HOL: if_weak_cong, case_weak_cong,
lcp
parents:
0
diff
changeset
|
683 |
by (stac prod_fun_range_eq_diag 1); |
128 | 684 |
br (Rep_llist RS LListD_Fun_diag_I) 1; |
2
befa4e9f7c90
Added weak congruence rules to HOL: if_weak_cong, case_weak_cong,
lcp
parents:
0
diff
changeset
|
685 |
val llistD_Fun_range_I = result(); |
0 | 686 |
|
687 |
(*A special case of list_equality for functions over lazy lists*) |
|
688 |
val [prem1,prem2] = goal LList.thy |
|
689 |
"[| f(LNil)=g(LNil); \ |
|
690 |
\ !!x l. <f(LCons(x,l)),g(LCons(x,l))> : \ |
|
2
befa4e9f7c90
Added weak congruence rules to HOL: if_weak_cong, case_weak_cong,
lcp
parents:
0
diff
changeset
|
691 |
\ llistD_Fun(range(%u. <f(u),g(u)>) Un range(%v. <v,v>)) \ |
90
5c7a69cef18b
added parentheses made necessary by change of constrain's precedence
clasohm
parents:
66
diff
changeset
|
692 |
\ |] ==> f(l) = (g(l :: 'a llist) :: 'b llist)"; |
2
befa4e9f7c90
Added weak congruence rules to HOL: if_weak_cong, case_weak_cong,
lcp
parents:
0
diff
changeset
|
693 |
by (res_inst_tac [("r", "range(%u. <f(u),g(u)>)")] llist_equalityI 1); |
0 | 694 |
by (rtac rangeI 1); |
695 |
by (rtac subsetI 1); |
|
696 |
by (etac rangeE 1); |
|
697 |
by (etac ssubst 1); |
|
698 |
by (res_inst_tac [("l", "u")] llistE 1); |
|
699 |
by (etac ssubst 1); |
|
700 |
by (stac prem1 1); |
|
2
befa4e9f7c90
Added weak congruence rules to HOL: if_weak_cong, case_weak_cong,
lcp
parents:
0
diff
changeset
|
701 |
by (rtac llistD_Fun_range_I 1); |
0 | 702 |
by (etac ssubst 1); |
703 |
by (rtac prem2 1); |
|
704 |
val llist_fun_equalityI = result(); |
|
705 |
||
706 |
(*simpset for llist bisimulations*) |
|
105 | 707 |
val llistD_simps = [llist_case_LNil, llist_case_LCons, |
0 | 708 |
llistD_Fun_LNil_I, llistD_Fun_LCons_I]; |
105 | 709 |
(*Don't use llist_ss, as it does case splits!*) |
710 |
val llistD_ss = univ_ss addsimps llistD_simps |> add_eqI; |
|
0 | 711 |
|
712 |
||
713 |
(*** The functional "lmap" ***) |
|
714 |
||
715 |
goal LList.thy "lmap(f,LNil) = LNil"; |
|
716 |
by (rtac (lmap_def RS def_llist_corec RS trans) 1); |
|
2
befa4e9f7c90
Added weak congruence rules to HOL: if_weak_cong, case_weak_cong,
lcp
parents:
0
diff
changeset
|
717 |
by (simp_tac llistD_ss 1); |
0 | 718 |
val lmap_LNil = result(); |
719 |
||
720 |
goal LList.thy "lmap(f, LCons(M,N)) = LCons(f(M), lmap(f,N))"; |
|
721 |
by (rtac (lmap_def RS def_llist_corec RS trans) 1); |
|
2
befa4e9f7c90
Added weak congruence rules to HOL: if_weak_cong, case_weak_cong,
lcp
parents:
0
diff
changeset
|
722 |
by (simp_tac llistD_ss 1); |
0 | 723 |
val lmap_LCons = result(); |
724 |
||
725 |
||
726 |
(** Two easy results about lmap **) |
|
727 |
||
728 |
goal LList.thy "lmap(f o g, l) = lmap(f, lmap(g, l))"; |
|
729 |
by (res_inst_tac [("l","l")] llist_fun_equalityI 1); |
|
730 |
by (ALLGOALS (simp_tac (llistD_ss addsimps [lmap_LNil, lmap_LCons]))); |
|
731 |
val lmap_compose = result(); |
|
732 |
||
733 |
goal LList.thy "lmap(%x.x, l) = l"; |
|
734 |
by (res_inst_tac [("l","l")] llist_fun_equalityI 1); |
|
735 |
by (ALLGOALS (simp_tac (llistD_ss addsimps [lmap_LNil, lmap_LCons]))); |
|
736 |
val lmap_ident = result(); |
|
737 |
||
738 |
||
739 |
(*** iterates -- llist_fun_equalityI cannot be used! ***) |
|
740 |
||
741 |
goal LList.thy "iterates(f,x) = LCons(x, iterates(f,f(x)))"; |
|
742 |
by (rtac (iterates_def RS def_llist_corec RS trans) 1); |
|
2
befa4e9f7c90
Added weak congruence rules to HOL: if_weak_cong, case_weak_cong,
lcp
parents:
0
diff
changeset
|
743 |
by (simp_tac sum_ss 1); |
0 | 744 |
val iterates = result(); |
745 |
||
746 |
goal LList.thy "lmap(f, iterates(f,x)) = iterates(f,f(x))"; |
|
747 |
by (res_inst_tac [("r", "range(%u.<lmap(f,iterates(f,u)),iterates(f,f(u))>)")] |
|
748 |
llist_equalityI 1); |
|
749 |
by (rtac rangeI 1); |
|
750 |
by (safe_tac set_cs); |
|
751 |
by (res_inst_tac [("x1", "f(u)")] (iterates RS ssubst) 1); |
|
752 |
by (res_inst_tac [("x1", "u")] (iterates RS ssubst) 1); |
|
753 |
by (simp_tac (llistD_ss addsimps [lmap_LCons]) 1); |
|
754 |
val lmap_iterates = result(); |
|
755 |
||
756 |
goal LList.thy "iterates(f,x) = LCons(x, lmap(f, iterates(f,x)))"; |
|
757 |
br (lmap_iterates RS ssubst) 1; |
|
758 |
br iterates 1; |
|
759 |
val iterates_lmap = result(); |
|
760 |
||
761 |
(*** A rather complex proof about iterates -- cf Andy Pitts ***) |
|
762 |
||
763 |
(** Two lemmas about natrec(n,x,%m.g), which is essentially (g^n)(x) **) |
|
764 |
||
765 |
goal LList.thy |
|
766 |
"nat_rec(n, LCons(b, l), %m. lmap(f)) = \ |
|
767 |
\ LCons(nat_rec(n, b, %m. f), nat_rec(n, l, %m. lmap(f)))"; |
|
768 |
by (nat_ind_tac "n" 1); |
|
2
befa4e9f7c90
Added weak congruence rules to HOL: if_weak_cong, case_weak_cong,
lcp
parents:
0
diff
changeset
|
769 |
by (ALLGOALS (asm_simp_tac (nat_ss addsimps [lmap_LCons]))); |
0 | 770 |
val fun_power_lmap = result(); |
771 |
||
772 |
goal Nat.thy "nat_rec(n, g(x), %m. g) = nat_rec(Suc(n), x, %m. g)"; |
|
773 |
by (nat_ind_tac "n" 1); |
|
2
befa4e9f7c90
Added weak congruence rules to HOL: if_weak_cong, case_weak_cong,
lcp
parents:
0
diff
changeset
|
774 |
by (ALLGOALS (asm_simp_tac nat_ss)); |
0 | 775 |
val fun_power_Suc = result(); |
776 |
||
777 |
val Pair_cong = read_instantiate_sg (sign_of Prod.thy) |
|
778 |
[("f","Pair")] (standard(refl RS cong RS cong)); |
|
779 |
||
780 |
(*The bisimulation consists of {<lmap(f)^n (h(u)), lmap(f)^n (iterates(f,u))>} |
|
781 |
for all u and all n::nat.*) |
|
782 |
val [prem] = goal LList.thy |
|
783 |
"(!!x. h(x) = LCons(x, lmap(f,h(x)))) ==> h = iterates(f)"; |
|
784 |
br ext 1; |
|
785 |
by (res_inst_tac [("r", |
|
786 |
"UN u. range(%n. <nat_rec(n, h(u), %m y.lmap(f,y)), \ |
|
787 |
\ nat_rec(n, iterates(f,u), %m y.lmap(f,y))>)")] |
|
788 |
llist_equalityI 1); |
|
789 |
by (REPEAT (resolve_tac [UN1_I, range_eqI, Pair_cong, nat_rec_0 RS sym] 1)); |
|
790 |
by (safe_tac set_cs); |
|
791 |
by (stac iterates 1); |
|
792 |
by (stac prem 1); |
|
793 |
by (stac fun_power_lmap 1); |
|
794 |
by (stac fun_power_lmap 1); |
|
795 |
br llistD_Fun_LCons_I 1; |
|
796 |
by (rtac (lmap_iterates RS subst) 1); |
|
797 |
by (stac fun_power_Suc 1); |
|
798 |
by (stac fun_power_Suc 1); |
|
2
befa4e9f7c90
Added weak congruence rules to HOL: if_weak_cong, case_weak_cong,
lcp
parents:
0
diff
changeset
|
799 |
br (UN1_I RS UnI1) 1; |
0 | 800 |
br rangeI 1; |
801 |
val iterates_equality = result(); |
|
802 |
||
803 |
||
804 |
(*** lappend -- its two arguments cause some complications! ***) |
|
805 |
||
806 |
goalw LList.thy [lappend_def] "lappend(LNil,LNil) = LNil"; |
|
807 |
by (rtac (llist_corec RS trans) 1); |
|
2
befa4e9f7c90
Added weak congruence rules to HOL: if_weak_cong, case_weak_cong,
lcp
parents:
0
diff
changeset
|
808 |
by (simp_tac llistD_ss 1); |
0 | 809 |
val lappend_LNil_LNil = result(); |
810 |
||
811 |
goalw LList.thy [lappend_def] |
|
812 |
"lappend(LNil,LCons(l,l')) = LCons(l, lappend(LNil,l'))"; |
|
813 |
by (rtac (llist_corec RS trans) 1); |
|
2
befa4e9f7c90
Added weak congruence rules to HOL: if_weak_cong, case_weak_cong,
lcp
parents:
0
diff
changeset
|
814 |
by (simp_tac llistD_ss 1); |
0 | 815 |
val lappend_LNil_LCons = result(); |
816 |
||
817 |
goalw LList.thy [lappend_def] |
|
818 |
"lappend(LCons(l,l'), N) = LCons(l, lappend(l',N))"; |
|
819 |
by (rtac (llist_corec RS trans) 1); |
|
2
befa4e9f7c90
Added weak congruence rules to HOL: if_weak_cong, case_weak_cong,
lcp
parents:
0
diff
changeset
|
820 |
by (simp_tac llistD_ss 1); |
0 | 821 |
val lappend_LCons = result(); |
822 |
||
823 |
goal LList.thy "lappend(LNil,l) = l"; |
|
824 |
by (res_inst_tac [("l","l")] llist_fun_equalityI 1); |
|
105 | 825 |
by (ALLGOALS |
826 |
(simp_tac (llistD_ss addsimps [lappend_LNil_LNil, lappend_LNil_LCons]))); |
|
0 | 827 |
val lappend_LNil = result(); |
828 |
||
829 |
goal LList.thy "lappend(l,LNil) = l"; |
|
830 |
by (res_inst_tac [("l","l")] llist_fun_equalityI 1); |
|
105 | 831 |
by (ALLGOALS |
832 |
(simp_tac (llistD_ss addsimps [lappend_LNil_LNil, lappend_LCons]))); |
|
0 | 833 |
val lappend_LNil2 = result(); |
834 |
||
835 |
(*The infinite first argument blocks the second*) |
|
836 |
goal LList.thy "lappend(iterates(f,x), N) = iterates(f,x)"; |
|
837 |
by (res_inst_tac [("r", "range(%u.<lappend(iterates(f,u),N),iterates(f,u)>)")] |
|
838 |
llist_equalityI 1); |
|
839 |
by (rtac rangeI 1); |
|
840 |
by (safe_tac set_cs); |
|
841 |
by (stac iterates 1); |
|
842 |
by (simp_tac (llistD_ss addsimps [lappend_LCons]) 1); |
|
843 |
val lappend_iterates = result(); |
|
844 |
||
845 |
(** Two proofs that lmap distributes over lappend **) |
|
846 |
||
847 |
(*Long proof requiring case analysis on both both arguments*) |
|
848 |
goal LList.thy "lmap(f, lappend(l,n)) = lappend(lmap(f,l), lmap(f,n))"; |
|
849 |
by (res_inst_tac |
|
850 |
[("r", |
|
851 |
"UN n. range(%l.<lmap(f,lappend(l,n)), lappend(lmap(f,l),lmap(f,n))>)")] |
|
852 |
llist_equalityI 1); |
|
853 |
by (rtac UN1_I 1); |
|
854 |
by (rtac rangeI 1); |
|
855 |
by (safe_tac set_cs); |
|
856 |
by (res_inst_tac [("l", "l")] llistE 1); |
|
857 |
by (res_inst_tac [("l", "n")] llistE 1); |
|
858 |
by (ALLGOALS (asm_simp_tac (llistD_ss addsimps |
|
859 |
[lappend_LNil_LNil,lappend_LCons,lappend_LNil_LCons, |
|
860 |
lmap_LNil,lmap_LCons]))); |
|
2
befa4e9f7c90
Added weak congruence rules to HOL: if_weak_cong, case_weak_cong,
lcp
parents:
0
diff
changeset
|
861 |
by (REPEAT_SOME (ares_tac [llistD_Fun_LCons_I, UN1_I RS UnI1, rangeI])); |
0 | 862 |
by (rtac range_eqI 1); |
863 |
by (rtac (refl RS Pair_cong) 1); |
|
864 |
by (stac lmap_LNil 1); |
|
865 |
by (rtac refl 1); |
|
866 |
val lmap_lappend_distrib = result(); |
|
867 |
||
2
befa4e9f7c90
Added weak congruence rules to HOL: if_weak_cong, case_weak_cong,
lcp
parents:
0
diff
changeset
|
868 |
(*Shorter proof of theorem above using llist_equalityI as strong coinduction*) |
0 | 869 |
goal LList.thy "lmap(f, lappend(l,n)) = lappend(lmap(f,l), lmap(f,n))"; |
870 |
by (res_inst_tac [("l","l")] llist_fun_equalityI 1); |
|
871 |
by (simp_tac (llistD_ss addsimps [lappend_LNil, lmap_LNil])1); |
|
872 |
by (simp_tac (llistD_ss addsimps [lappend_LCons, lmap_LCons]) 1); |
|
873 |
val lmap_lappend_distrib = result(); |
|
874 |
||
2
befa4e9f7c90
Added weak congruence rules to HOL: if_weak_cong, case_weak_cong,
lcp
parents:
0
diff
changeset
|
875 |
(*Without strong coinduction, three case analyses might be needed*) |
0 | 876 |
goal LList.thy "lappend(lappend(l1,l2) ,l3) = lappend(l1, lappend(l2,l3))"; |
877 |
by (res_inst_tac [("l","l1")] llist_fun_equalityI 1); |
|
878 |
by (simp_tac (llistD_ss addsimps [lappend_LNil])1); |
|
879 |
by (simp_tac (llistD_ss addsimps [lappend_LCons]) 1); |
|
880 |
val lappend_assoc = result(); |