0
|
1 |
(* Title: HOL/list
|
|
2 |
ID: $Id$
|
|
3 |
Author: Lawrence C Paulson, Cambridge University Computer Laboratory
|
|
4 |
Copyright 1993 University of Cambridge
|
|
5 |
|
|
6 |
For list.thy.
|
|
7 |
*)
|
|
8 |
|
|
9 |
open List;
|
|
10 |
|
|
11 |
(** the list functional **)
|
|
12 |
|
|
13 |
goalw List.thy [List_Fun_def] "mono(List_Fun(A))";
|
|
14 |
by (REPEAT (ares_tac [monoI, subset_refl, usum_mono, uprod_mono] 1));
|
|
15 |
val List_Fun_mono = result();
|
|
16 |
|
|
17 |
goalw List.thy [List_Fun_def]
|
|
18 |
"!!A B. A<=B ==> List_Fun(A,Z) <= List_Fun(B,Z)";
|
|
19 |
by (REPEAT (ares_tac [subset_refl, usum_mono, uprod_mono] 1));
|
|
20 |
val List_Fun_mono2 = result();
|
|
21 |
|
|
22 |
(*This justifies using List in other recursive type definitions*)
|
|
23 |
goalw List.thy [List_def] "!!A B. A<=B ==> List(A) <= List(B)";
|
|
24 |
by (rtac lfp_mono 1);
|
|
25 |
by (etac List_Fun_mono2 1);
|
|
26 |
val List_mono = result();
|
|
27 |
|
|
28 |
(** Type checking rules -- List creates well-founded sets **)
|
|
29 |
|
|
30 |
val prems = goalw List.thy [List_def,List_Fun_def] "List(Sexp) <= Sexp";
|
|
31 |
by (rtac lfp_lowerbound 1);
|
|
32 |
by (fast_tac (univ_cs addIs [Sexp_NumbI,Sexp_In0I,Sexp_In1I,Sexp_SconsI]) 1);
|
|
33 |
val List_Sexp = result();
|
|
34 |
|
|
35 |
(* A <= Sexp ==> List(A) <= Sexp *)
|
|
36 |
val List_subset_Sexp = standard
|
|
37 |
(List_mono RS (List_Sexp RSN (2,subset_trans)));
|
|
38 |
|
|
39 |
(** Induction **)
|
|
40 |
|
|
41 |
(*Induction for the set List(A) *)
|
|
42 |
val major::prems = goalw List.thy [NIL_def,CONS_def]
|
|
43 |
"[| M: List(A); P(NIL); \
|
|
44 |
\ !!M N. [| M: A; N: List(A); P(N) |] ==> P(CONS(M,N)) |] \
|
|
45 |
\ ==> P(M)";
|
|
46 |
by (rtac (major RS (List_def RS def_induct)) 1);
|
|
47 |
by (rtac List_Fun_mono 1);
|
|
48 |
by (rewtac List_Fun_def);
|
|
49 |
by (fast_tac (set_cs addIs prems addEs [usumE,uprodE]) 1);
|
|
50 |
val List_induct = result();
|
|
51 |
|
|
52 |
(*Induction for the type 'a list *)
|
|
53 |
val prems = goalw List.thy [Nil_def,Cons_def]
|
|
54 |
"[| P(Nil); \
|
|
55 |
\ !!x xs. P(xs) ==> P(Cons(x,xs)) |] ==> P(l)";
|
|
56 |
by (rtac (Rep_List_inverse RS subst) 1); (*types force good instantiation*)
|
|
57 |
by (rtac (Rep_List RS List_induct) 1);
|
|
58 |
by (REPEAT (ares_tac prems 1
|
|
59 |
ORELSE eresolve_tac [rangeE, ssubst, Abs_List_inverse RS subst] 1));
|
|
60 |
val list_induct = result();
|
|
61 |
|
|
62 |
(*Perform induction on xs. *)
|
|
63 |
fun list_ind_tac a M =
|
|
64 |
EVERY [res_inst_tac [("l",a)] list_induct M,
|
|
65 |
rename_last_tac a ["1"] (M+1)];
|
|
66 |
|
|
67 |
(** Introduction rules for List constructors **)
|
|
68 |
|
|
69 |
val List_unfold = rewrite_rule [List_Fun_def]
|
|
70 |
(List_Fun_mono RS (List_def RS def_lfp_Tarski));
|
|
71 |
|
|
72 |
(* c : {Numb(0)} <+> A <*> List(A) ==> c : List(A) *)
|
|
73 |
val ListI = List_unfold RS equalityD2 RS subsetD;
|
|
74 |
|
|
75 |
(* NIL is a List -- this also justifies the type definition*)
|
|
76 |
goalw List.thy [NIL_def] "NIL: List(A)";
|
|
77 |
by (rtac (singletonI RS usum_In0I RS ListI) 1);
|
|
78 |
val NIL_I = result();
|
|
79 |
|
|
80 |
goalw List.thy [CONS_def]
|
|
81 |
"!!a A M. [| a: A; M: List(A) |] ==> CONS(a,M) : List(A)";
|
|
82 |
by (REPEAT (ares_tac [uprodI RS usum_In1I RS ListI] 1));
|
|
83 |
val CONS_I = result();
|
|
84 |
|
|
85 |
(*** Isomorphisms ***)
|
|
86 |
|
|
87 |
goal List.thy "inj(Rep_List)";
|
|
88 |
by (rtac inj_inverseI 1);
|
|
89 |
by (rtac Rep_List_inverse 1);
|
|
90 |
val inj_Rep_List = result();
|
|
91 |
|
|
92 |
goal List.thy "inj_onto(Abs_List,List(range(Leaf)))";
|
|
93 |
by (rtac inj_onto_inverseI 1);
|
|
94 |
by (etac Abs_List_inverse 1);
|
|
95 |
val inj_onto_Abs_List = result();
|
|
96 |
|
|
97 |
(** Distinctness of constructors **)
|
|
98 |
|
5
|
99 |
goalw List.thy [NIL_def,CONS_def] "CONS(M,N) ~= NIL";
|
0
|
100 |
by (rtac In1_not_In0 1);
|
|
101 |
val CONS_not_NIL = result();
|
|
102 |
val NIL_not_CONS = standard (CONS_not_NIL RS not_sym);
|
|
103 |
|
|
104 |
val CONS_neq_NIL = standard (CONS_not_NIL RS notE);
|
|
105 |
val NIL_neq_CONS = sym RS CONS_neq_NIL;
|
|
106 |
|
5
|
107 |
goalw List.thy [Nil_def,Cons_def] "Cons(x,xs) ~= Nil";
|
0
|
108 |
by (rtac (CONS_not_NIL RS (inj_onto_Abs_List RS inj_onto_contraD)) 1);
|
|
109 |
by (REPEAT (resolve_tac [rangeI, NIL_I, CONS_I, Rep_List] 1));
|
|
110 |
val Cons_not_Nil = result();
|
|
111 |
|
|
112 |
val Nil_not_Cons = standard (Cons_not_Nil RS not_sym);
|
|
113 |
|
|
114 |
val Cons_neq_Nil = standard (Cons_not_Nil RS notE);
|
|
115 |
val Nil_neq_Cons = sym RS Cons_neq_Nil;
|
|
116 |
|
|
117 |
(** Injectiveness of CONS and Cons **)
|
|
118 |
|
|
119 |
goalw List.thy [CONS_def] "(CONS(K,M)=CONS(L,N)) = (K=L & M=N)";
|
|
120 |
by (fast_tac (HOL_cs addSEs [Scons_inject, make_elim In1_inject]) 1);
|
|
121 |
val CONS_CONS_eq = result();
|
|
122 |
|
|
123 |
val CONS_inject = standard (CONS_CONS_eq RS iffD1 RS conjE);
|
|
124 |
|
|
125 |
(*For reasoning about abstract list constructors*)
|
|
126 |
val List_cs = set_cs addIs [Rep_List, NIL_I, CONS_I]
|
|
127 |
addSEs [CONS_neq_NIL,NIL_neq_CONS,CONS_inject]
|
|
128 |
addSDs [inj_onto_Abs_List RS inj_ontoD,
|
|
129 |
inj_Rep_List RS injD, Leaf_inject];
|
|
130 |
|
|
131 |
goalw List.thy [Cons_def] "(Cons(x,xs)=Cons(y,ys)) = (x=y & xs=ys)";
|
|
132 |
by (fast_tac List_cs 1);
|
|
133 |
val Cons_Cons_eq = result();
|
|
134 |
val Cons_inject = standard (Cons_Cons_eq RS iffD1 RS conjE);
|
|
135 |
|
|
136 |
val [major] = goal List.thy "CONS(M,N): List(A) ==> M: A & N: List(A)";
|
|
137 |
by (rtac (major RS setup_induction) 1);
|
|
138 |
by (etac List_induct 1);
|
|
139 |
by (ALLGOALS (fast_tac List_cs));
|
|
140 |
val CONS_D = result();
|
|
141 |
|
|
142 |
val prems = goalw List.thy [CONS_def,In1_def]
|
|
143 |
"CONS(M,N): Sexp ==> M: Sexp & N: Sexp";
|
|
144 |
by (cut_facts_tac prems 1);
|
|
145 |
by (fast_tac (set_cs addSDs [Scons_D]) 1);
|
|
146 |
val Sexp_CONS_D = result();
|
|
147 |
|
|
148 |
|
|
149 |
(*Basic ss with constructors and their freeness*)
|
|
150 |
val list_free_simps = [Cons_not_Nil, Nil_not_Cons, Cons_Cons_eq,
|
|
151 |
CONS_not_NIL, NIL_not_CONS, CONS_CONS_eq,
|
|
152 |
NIL_I, CONS_I];
|
|
153 |
val list_free_ss = HOL_ss addsimps list_free_simps;
|
|
154 |
|
5
|
155 |
goal List.thy "!!N. N: List(A) ==> !M. N ~= CONS(M,N)";
|
0
|
156 |
by (etac List_induct 1);
|
|
157 |
by (ALLGOALS (asm_simp_tac list_free_ss));
|
|
158 |
val not_CONS_self = result();
|
|
159 |
|
5
|
160 |
goal List.thy "!x. l ~= Cons(x,l)";
|
0
|
161 |
by (list_ind_tac "l" 1);
|
|
162 |
by (ALLGOALS (asm_simp_tac list_free_ss));
|
|
163 |
val not_Cons_self = result();
|
|
164 |
|
|
165 |
|
|
166 |
(** Conversion rules for List_case: case analysis operator **)
|
|
167 |
|
|
168 |
goalw List.thy [List_case_def,NIL_def] "List_case(NIL,c,h) = c";
|
|
169 |
by (rtac Case_In0 1);
|
|
170 |
val List_case_NIL = result();
|
|
171 |
|
|
172 |
goalw List.thy [List_case_def,CONS_def] "List_case(CONS(M,N), c, h) = h(M,N)";
|
|
173 |
by (simp_tac (HOL_ss addsimps [Split,Case_In1]) 1);
|
|
174 |
val List_case_CONS = result();
|
|
175 |
|
|
176 |
(*** List_rec -- by wf recursion on pred_Sexp ***)
|
|
177 |
|
|
178 |
(* The trancl(pred_sexp) is essential because pred_Sexp_CONS_I1,2 would not
|
|
179 |
hold if pred_Sexp^+ were changed to pred_Sexp. *)
|
|
180 |
|
|
181 |
val List_rec_unfold = wf_pred_Sexp RS wf_trancl RS (List_rec_def RS def_wfrec);
|
|
182 |
|
|
183 |
(** pred_Sexp lemmas **)
|
|
184 |
|
|
185 |
goalw List.thy [CONS_def,In1_def]
|
|
186 |
"!!M. [| M: Sexp; N: Sexp |] ==> <M, CONS(M,N)> : pred_Sexp^+";
|
|
187 |
by (asm_simp_tac pred_Sexp_ss 1);
|
|
188 |
val pred_Sexp_CONS_I1 = result();
|
|
189 |
|
|
190 |
goalw List.thy [CONS_def,In1_def]
|
|
191 |
"!!M. [| M: Sexp; N: Sexp |] ==> <N, CONS(M,N)> : pred_Sexp^+";
|
|
192 |
by (asm_simp_tac pred_Sexp_ss 1);
|
|
193 |
val pred_Sexp_CONS_I2 = result();
|
|
194 |
|
|
195 |
val [prem] = goal List.thy
|
|
196 |
"<CONS(M1,M2), N> : pred_Sexp^+ ==> \
|
|
197 |
\ <M1,N> : pred_Sexp^+ & <M2,N> : pred_Sexp^+";
|
|
198 |
by (rtac (prem RS (pred_Sexp_subset_Sigma RS trancl_subset_Sigma RS
|
|
199 |
subsetD RS SigmaE2)) 1);
|
|
200 |
by (etac (Sexp_CONS_D RS conjE) 1);
|
|
201 |
by (REPEAT (ares_tac [conjI, pred_Sexp_CONS_I1, pred_Sexp_CONS_I2,
|
|
202 |
prem RSN (2, trans_trancl RS transD)] 1));
|
|
203 |
val pred_Sexp_CONS_D = result();
|
|
204 |
|
|
205 |
(** Conversion rules for List_rec **)
|
|
206 |
|
|
207 |
goal List.thy "List_rec(NIL,c,h) = c";
|
|
208 |
by (rtac (List_rec_unfold RS trans) 1);
|
|
209 |
by (rtac List_case_NIL 1);
|
|
210 |
val List_rec_NIL = result();
|
|
211 |
|
|
212 |
goal List.thy "!!M. [| M: Sexp; N: Sexp |] ==> \
|
|
213 |
\ List_rec(CONS(M,N), c, h) = h(M, N, List_rec(N,c,h))";
|
|
214 |
by (rtac (List_rec_unfold RS trans) 1);
|
|
215 |
by (rtac (List_case_CONS RS trans) 1);
|
|
216 |
by(asm_simp_tac(HOL_ss addsimps [CONS_I, pred_Sexp_CONS_I2, cut_apply])1);
|
|
217 |
val List_rec_CONS = result();
|
|
218 |
|
|
219 |
(*** list_rec -- by List_rec ***)
|
|
220 |
|
|
221 |
val Rep_List_in_Sexp =
|
|
222 |
Rep_List RS (range_Leaf_subset_Sexp RS List_subset_Sexp RS subsetD);
|
|
223 |
|
|
224 |
local
|
|
225 |
val list_rec_simps = list_free_simps @
|
|
226 |
[List_rec_NIL, List_rec_CONS,
|
|
227 |
Abs_List_inverse, Rep_List_inverse,
|
|
228 |
Rep_List, rangeI, inj_Leaf, Inv_f_f,
|
|
229 |
Sexp_LeafI, Rep_List_in_Sexp]
|
|
230 |
in
|
|
231 |
val list_rec_Nil = prove_goalw List.thy [list_rec_def, Nil_def]
|
|
232 |
"list_rec(Nil,c,h) = c"
|
|
233 |
(fn _=> [simp_tac (HOL_ss addsimps list_rec_simps) 1]);
|
|
234 |
|
|
235 |
val list_rec_Cons = prove_goalw List.thy [list_rec_def, Cons_def]
|
|
236 |
"list_rec(Cons(a,l), c, h) = h(a, l, list_rec(l,c,h))"
|
|
237 |
(fn _=> [simp_tac (HOL_ss addsimps list_rec_simps) 1]);
|
|
238 |
end;
|
|
239 |
|
|
240 |
val list_simps = [List_rec_NIL, List_rec_CONS,
|
|
241 |
list_rec_Nil, list_rec_Cons];
|
|
242 |
val list_ss = list_free_ss addsimps list_simps;
|
|
243 |
|
|
244 |
|
|
245 |
(*Type checking. Useful?*)
|
|
246 |
val major::A_subset_Sexp::prems = goal List.thy
|
|
247 |
"[| M: List(A); \
|
|
248 |
\ A<=Sexp; \
|
|
249 |
\ c: C(NIL); \
|
|
250 |
\ !!x y r. [| x: A; y: List(A); r: C(y) |] ==> h(x,y,r): C(CONS(x,y)) \
|
|
251 |
\ |] ==> List_rec(M,c,h) : C(M :: 'a node set)";
|
|
252 |
val Sexp_ListA_I = A_subset_Sexp RS List_subset_Sexp RS subsetD;
|
|
253 |
val Sexp_A_I = A_subset_Sexp RS subsetD;
|
|
254 |
by (rtac (major RS List_induct) 1);
|
|
255 |
by (ALLGOALS(asm_simp_tac (list_ss addsimps ([Sexp_A_I,Sexp_ListA_I]@prems))));
|
|
256 |
val List_rec_type = result();
|
|
257 |
|
|
258 |
(** Generalized map functionals **)
|
|
259 |
|
|
260 |
goalw List.thy [Rep_map_def] "Rep_map(f,Nil) = NIL";
|
|
261 |
by (rtac list_rec_Nil 1);
|
|
262 |
val Rep_map_Nil = result();
|
|
263 |
|
|
264 |
goalw List.thy [Rep_map_def]
|
|
265 |
"Rep_map(f, Cons(x,xs)) = CONS(f(x), Rep_map(f,xs))";
|
|
266 |
by (rtac list_rec_Cons 1);
|
|
267 |
val Rep_map_Cons = result();
|
|
268 |
|
|
269 |
goalw List.thy [Rep_map_def] "!!f. (!!x. f(x): A) ==> Rep_map(f,xs): List(A)";
|
|
270 |
by (rtac list_induct 1);
|
|
271 |
by(ALLGOALS(asm_simp_tac list_ss));
|
|
272 |
val Rep_map_type = result();
|
|
273 |
|
|
274 |
goalw List.thy [Abs_map_def] "Abs_map(g,NIL) = Nil";
|
|
275 |
by (rtac List_rec_NIL 1);
|
|
276 |
val Abs_map_NIL = result();
|
|
277 |
|
|
278 |
val prems = goalw List.thy [Abs_map_def]
|
|
279 |
"[| M: Sexp; N: Sexp |] ==> \
|
|
280 |
\ Abs_map(g, CONS(M,N)) = Cons(g(M), Abs_map(g,N))";
|
|
281 |
by (REPEAT (resolve_tac (List_rec_CONS::prems) 1));
|
|
282 |
val Abs_map_CONS = result();
|
|
283 |
|
|
284 |
(** null, hd, tl, list_case **)
|
|
285 |
|
|
286 |
goalw List.thy [null_def] "null([]) = True";
|
|
287 |
by (rtac list_rec_Nil 1);
|
|
288 |
val null_Nil = result();
|
|
289 |
|
|
290 |
goalw List.thy [null_def] "null(Cons(x,xs)) = False";
|
|
291 |
by (rtac list_rec_Cons 1);
|
|
292 |
val null_Cons = result();
|
|
293 |
|
|
294 |
|
|
295 |
goalw List.thy [hd_def] "hd(Cons(x,xs)) = x";
|
|
296 |
by (rtac list_rec_Cons 1);
|
|
297 |
val hd_Cons = result();
|
|
298 |
|
|
299 |
|
|
300 |
goalw List.thy [tl_def] "tl(Cons(x,xs)) = xs";
|
|
301 |
by (rtac list_rec_Cons 1);
|
|
302 |
val tl_Cons = result();
|
|
303 |
|
|
304 |
|
|
305 |
goalw List.thy [list_case_def] "list_case([],a,f) = a";
|
|
306 |
by (rtac list_rec_Nil 1);
|
|
307 |
val list_case_Nil = result();
|
|
308 |
|
|
309 |
goalw List.thy [list_case_def] "list_case(Cons(x,xs),a,f) = f(x,xs)";
|
|
310 |
by (rtac list_rec_Cons 1);
|
|
311 |
val list_case_Cons = result();
|
|
312 |
|
13
|
313 |
(** @ - append **)
|
|
314 |
|
|
315 |
goalw List.thy [append_def] "[]@xs = xs";
|
|
316 |
by (rtac list_rec_Nil 1);
|
|
317 |
val append_Nil = result();
|
|
318 |
|
|
319 |
goalw List.thy [append_def] "Cons(x,xs)@ys = Cons(x,xs@ys)";
|
|
320 |
by (rtac list_rec_Cons 1);
|
|
321 |
val append_Cons = result();
|
|
322 |
|
|
323 |
goal List.thy "(xs@ys)@zs = xs@(ys@zs)";
|
|
324 |
by(res_inst_tac [("l","xs")] list_induct 1);
|
|
325 |
by(REPEAT(asm_simp_tac (list_ss addsimps [append_Nil,append_Cons]) 1));
|
|
326 |
val append_assoc = result();
|
0
|
327 |
|
|
328 |
(** The functional "map" **)
|
|
329 |
|
|
330 |
goalw List.thy [map_def] "map(f,Nil) = Nil";
|
|
331 |
by (rtac list_rec_Nil 1);
|
|
332 |
val map_Nil = result();
|
|
333 |
|
|
334 |
goalw List.thy [map_def] "map(f, Cons(x,xs)) = Cons(f(x), map(f,xs))";
|
|
335 |
by (rtac list_rec_Cons 1);
|
|
336 |
val map_Cons = result();
|
|
337 |
|
|
338 |
val map_simps = [Abs_map_NIL, Abs_map_CONS,
|
|
339 |
Rep_map_Nil, Rep_map_Cons,
|
|
340 |
map_Nil, map_Cons];
|
|
341 |
val map_ss = list_free_ss addsimps map_simps;
|
|
342 |
|
|
343 |
val [major,A_subset_Sexp,minor] = goal List.thy
|
|
344 |
"[| M: List(A); A<=Sexp; !!z. z: A ==> f(g(z)) = z |] \
|
|
345 |
\ ==> Rep_map(f, Abs_map(g,M)) = M";
|
|
346 |
by (rtac (major RS List_induct) 1);
|
|
347 |
by (ALLGOALS (asm_simp_tac(map_ss addsimps [Sexp_A_I,Sexp_ListA_I,minor])));
|
|
348 |
val Abs_map_inverse = result();
|
|
349 |
|
|
350 |
(*Rep_map_inverse is obtained via Abs_Rep_map and map_ident*)
|
|
351 |
|
|
352 |
|
|
353 |
(** The functional "list_all" -- creates predicates over lists **)
|
|
354 |
|
|
355 |
goalw List.thy [list_all_def] "list_all(P,Nil) = True";
|
|
356 |
by (rtac list_rec_Nil 1);
|
|
357 |
val list_all_Nil = result();
|
|
358 |
|
|
359 |
goalw List.thy [list_all_def]
|
|
360 |
"list_all(P, Cons(x,xs)) = (P(x) & list_all(P,xs))";
|
|
361 |
by (rtac list_rec_Cons 1);
|
|
362 |
val list_all_Cons = result();
|
|
363 |
|
|
364 |
(** Additional mapping lemmas **)
|
|
365 |
|
|
366 |
goal List.thy "map(%x.x, xs) = xs";
|
|
367 |
by (list_ind_tac "xs" 1);
|
|
368 |
by (ALLGOALS (asm_simp_tac map_ss));
|
|
369 |
val map_ident = result();
|
|
370 |
|
|
371 |
goal List.thy "!!f. (!!x. f(x): Sexp) ==> \
|
|
372 |
\ Abs_map(g, Rep_map(f,xs)) = map(%t. g(f(t)), xs)";
|
|
373 |
by (list_ind_tac "xs" 1);
|
|
374 |
by(ALLGOALS(asm_simp_tac(map_ss addsimps
|
|
375 |
[Rep_map_type,List_Sexp RS subsetD])));
|
|
376 |
val Abs_Rep_map = result();
|
20
|
377 |
|
|
378 |
val list_ss = arith_ss addsimps
|
|
379 |
[Cons_not_Nil, Nil_not_Cons, Cons_Cons_eq,
|
|
380 |
list_rec_Nil, list_rec_Cons,
|
|
381 |
null_Nil, null_Cons, hd_Cons, tl_Cons,
|
|
382 |
list_case_Nil, list_case_Cons,
|
|
383 |
append_Nil, append_Cons, append_assoc,
|
|
384 |
map_Nil, map_Cons, map_ident,
|
|
385 |
list_all_Nil, list_all_Cons];
|