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