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