author | lcp |
Fri, 17 Sep 1993 16:16:38 +0200 | |
changeset 6 | 8ce8c4d13d4d |
parent 0 | a5a9c433f639 |
child 15 | 6c6d2f6e3185 |
permissions | -rw-r--r-- |
0 | 1 |
(* Title: ZF/list-fn.ML |
2 |
ID: $Id$ |
|
3 |
Author: Lawrence C Paulson, Cambridge University Computer Laboratory |
|
4 |
Copyright 1992 University of Cambridge |
|
5 |
||
6 |
For list-fn.thy. Lists in Zermelo-Fraenkel Set Theory |
|
7 |
*) |
|
8 |
||
9 |
open ListFn; |
|
10 |
||
11 |
||
12 |
(** list_rec -- by Vset recursion **) |
|
13 |
||
14 |
goal ListFn.thy "list_rec(Nil,c,h) = c"; |
|
15 |
by (rtac (list_rec_def RS def_Vrec RS trans) 1); |
|
6
8ce8c4d13d4d
Installation of new simplifier for ZF. Deleted all congruence rules not
lcp
parents:
0
diff
changeset
|
16 |
by (simp_tac (ZF_ss addsimps List.case_eqns) 1); |
0 | 17 |
val list_rec_Nil = result(); |
18 |
||
19 |
goal ListFn.thy "list_rec(Cons(a,l), c, h) = h(a, l, list_rec(l,c,h))"; |
|
20 |
by (rtac (list_rec_def RS def_Vrec RS trans) 1); |
|
6
8ce8c4d13d4d
Installation of new simplifier for ZF. Deleted all congruence rules not
lcp
parents:
0
diff
changeset
|
21 |
by (simp_tac (ZF_ss addsimps (List.case_eqns @ [Vset_rankI, rank_Cons2])) 1); |
0 | 22 |
val list_rec_Cons = result(); |
23 |
||
24 |
(*Type checking -- proved by induction, as usual*) |
|
25 |
val prems = goal ListFn.thy |
|
26 |
"[| l: list(A); \ |
|
27 |
\ c: C(Nil); \ |
|
28 |
\ !!x y r. [| x:A; y: list(A); r: C(y) |] ==> h(x,y,r): C(Cons(x,y)) \ |
|
29 |
\ |] ==> list_rec(l,c,h) : C(l)"; |
|
30 |
by (list_ind_tac "l" prems 1); |
|
6
8ce8c4d13d4d
Installation of new simplifier for ZF. Deleted all congruence rules not
lcp
parents:
0
diff
changeset
|
31 |
by (ALLGOALS (asm_simp_tac |
8ce8c4d13d4d
Installation of new simplifier for ZF. Deleted all congruence rules not
lcp
parents:
0
diff
changeset
|
32 |
(ZF_ss addsimps (prems@[list_rec_Nil,list_rec_Cons])))); |
0 | 33 |
val list_rec_type = result(); |
34 |
||
35 |
(** Versions for use with definitions **) |
|
36 |
||
37 |
val [rew] = goal ListFn.thy |
|
38 |
"[| !!l. j(l)==list_rec(l, c, h) |] ==> j(Nil) = c"; |
|
39 |
by (rewtac rew); |
|
40 |
by (rtac list_rec_Nil 1); |
|
41 |
val def_list_rec_Nil = result(); |
|
42 |
||
43 |
val [rew] = goal ListFn.thy |
|
44 |
"[| !!l. j(l)==list_rec(l, c, h) |] ==> j(Cons(a,l)) = h(a,l,j(l))"; |
|
45 |
by (rewtac rew); |
|
46 |
by (rtac list_rec_Cons 1); |
|
47 |
val def_list_rec_Cons = result(); |
|
48 |
||
49 |
fun list_recs def = map standard |
|
50 |
([def] RL [def_list_rec_Nil, def_list_rec_Cons]); |
|
51 |
||
52 |
(** map **) |
|
53 |
||
54 |
val [map_Nil,map_Cons] = list_recs map_def; |
|
55 |
||
56 |
val prems = goalw ListFn.thy [map_def] |
|
57 |
"[| l: list(A); !!x. x: A ==> h(x): B |] ==> map(h,l) : list(B)"; |
|
58 |
by (REPEAT (ares_tac (prems@[list_rec_type, NilI, ConsI]) 1)); |
|
59 |
val map_type = result(); |
|
60 |
||
61 |
val [major] = goal ListFn.thy "l: list(A) ==> map(h,l) : list({h(u). u:A})"; |
|
62 |
by (rtac (major RS map_type) 1); |
|
63 |
by (etac RepFunI 1); |
|
64 |
val map_type2 = result(); |
|
65 |
||
66 |
(** length **) |
|
67 |
||
68 |
val [length_Nil,length_Cons] = list_recs length_def; |
|
69 |
||
70 |
val prems = goalw ListFn.thy [length_def] |
|
71 |
"l: list(A) ==> length(l) : nat"; |
|
72 |
by (REPEAT (ares_tac (prems @ [list_rec_type, nat_0I, nat_succI]) 1)); |
|
73 |
val length_type = result(); |
|
74 |
||
75 |
(** app **) |
|
76 |
||
77 |
val [app_Nil,app_Cons] = list_recs app_def; |
|
78 |
||
79 |
val prems = goalw ListFn.thy [app_def] |
|
80 |
"[| xs: list(A); ys: list(A) |] ==> xs@ys : list(A)"; |
|
81 |
by (REPEAT (ares_tac (prems @ [list_rec_type, ConsI]) 1)); |
|
82 |
val app_type = result(); |
|
83 |
||
84 |
(** rev **) |
|
85 |
||
86 |
val [rev_Nil,rev_Cons] = list_recs rev_def; |
|
87 |
||
88 |
val prems = goalw ListFn.thy [rev_def] |
|
89 |
"xs: list(A) ==> rev(xs) : list(A)"; |
|
90 |
by (REPEAT (ares_tac (prems @ [list_rec_type, NilI, ConsI, app_type]) 1)); |
|
91 |
val rev_type = result(); |
|
92 |
||
93 |
||
94 |
(** flat **) |
|
95 |
||
96 |
val [flat_Nil,flat_Cons] = list_recs flat_def; |
|
97 |
||
98 |
val prems = goalw ListFn.thy [flat_def] |
|
99 |
"ls: list(list(A)) ==> flat(ls) : list(A)"; |
|
100 |
by (REPEAT (ares_tac (prems @ [list_rec_type, NilI, ConsI, app_type]) 1)); |
|
101 |
val flat_type = result(); |
|
102 |
||
103 |
||
104 |
(** list_add **) |
|
105 |
||
106 |
val [list_add_Nil,list_add_Cons] = list_recs list_add_def; |
|
107 |
||
108 |
val prems = goalw ListFn.thy [list_add_def] |
|
109 |
"xs: list(nat) ==> list_add(xs) : nat"; |
|
110 |
by (REPEAT (ares_tac (prems @ [list_rec_type, nat_0I, add_type]) 1)); |
|
111 |
val list_add_type = result(); |
|
112 |
||
113 |
(** ListFn simplification **) |
|
114 |
||
115 |
val list_typechecks = |
|
116 |
[NilI, ConsI, list_rec_type, |
|
117 |
map_type, map_type2, app_type, length_type, rev_type, flat_type, |
|
118 |
list_add_type]; |
|
119 |
||
120 |
val list_ss = arith_ss |
|
6
8ce8c4d13d4d
Installation of new simplifier for ZF. Deleted all congruence rules not
lcp
parents:
0
diff
changeset
|
121 |
addsimps List.case_eqns |
8ce8c4d13d4d
Installation of new simplifier for ZF. Deleted all congruence rules not
lcp
parents:
0
diff
changeset
|
122 |
addsimps [list_rec_Nil, list_rec_Cons, |
0 | 123 |
map_Nil, map_Cons, |
124 |
app_Nil, app_Cons, |
|
125 |
length_Nil, length_Cons, |
|
126 |
rev_Nil, rev_Cons, |
|
127 |
flat_Nil, flat_Cons, |
|
6
8ce8c4d13d4d
Installation of new simplifier for ZF. Deleted all congruence rules not
lcp
parents:
0
diff
changeset
|
128 |
list_add_Nil, list_add_Cons] |
8ce8c4d13d4d
Installation of new simplifier for ZF. Deleted all congruence rules not
lcp
parents:
0
diff
changeset
|
129 |
setsolver (type_auto_tac list_typechecks); |
8ce8c4d13d4d
Installation of new simplifier for ZF. Deleted all congruence rules not
lcp
parents:
0
diff
changeset
|
130 |
(*Could also rewrite using the list_typechecks...*) |
0 | 131 |
|
132 |
(*** theorems about map ***) |
|
133 |
||
134 |
val prems = goal ListFn.thy |
|
135 |
"l: list(A) ==> map(%u.u, l) = l"; |
|
136 |
by (list_ind_tac "l" prems 1); |
|
6
8ce8c4d13d4d
Installation of new simplifier for ZF. Deleted all congruence rules not
lcp
parents:
0
diff
changeset
|
137 |
by (ALLGOALS (asm_simp_tac list_ss)); |
0 | 138 |
val map_ident = result(); |
139 |
||
140 |
val prems = goal ListFn.thy |
|
141 |
"l: list(A) ==> map(h, map(j,l)) = map(%u.h(j(u)), l)"; |
|
142 |
by (list_ind_tac "l" prems 1); |
|
6
8ce8c4d13d4d
Installation of new simplifier for ZF. Deleted all congruence rules not
lcp
parents:
0
diff
changeset
|
143 |
by (ALLGOALS (asm_simp_tac list_ss)); |
0 | 144 |
val map_compose = result(); |
145 |
||
146 |
val prems = goal ListFn.thy |
|
147 |
"xs: list(A) ==> map(h, xs@ys) = map(h,xs) @ map(h,ys)"; |
|
148 |
by (list_ind_tac "xs" prems 1); |
|
6
8ce8c4d13d4d
Installation of new simplifier for ZF. Deleted all congruence rules not
lcp
parents:
0
diff
changeset
|
149 |
by (ALLGOALS (asm_simp_tac list_ss)); |
0 | 150 |
val map_app_distrib = result(); |
151 |
||
152 |
val prems = goal ListFn.thy |
|
153 |
"ls: list(list(A)) ==> map(h, flat(ls)) = flat(map(map(h),ls))"; |
|
154 |
by (list_ind_tac "ls" prems 1); |
|
6
8ce8c4d13d4d
Installation of new simplifier for ZF. Deleted all congruence rules not
lcp
parents:
0
diff
changeset
|
155 |
by (ALLGOALS (asm_simp_tac (list_ss addsimps [map_app_distrib]))); |
0 | 156 |
val map_flat = result(); |
157 |
||
158 |
val prems = goal ListFn.thy |
|
159 |
"l: list(A) ==> \ |
|
160 |
\ list_rec(map(h,l), c, d) = \ |
|
161 |
\ list_rec(l, c, %x xs r. d(h(x), map(h,xs), r))"; |
|
162 |
by (list_ind_tac "l" prems 1); |
|
6
8ce8c4d13d4d
Installation of new simplifier for ZF. Deleted all congruence rules not
lcp
parents:
0
diff
changeset
|
163 |
by (ALLGOALS (asm_simp_tac list_ss)); |
0 | 164 |
val list_rec_map = result(); |
165 |
||
166 |
(** theorems about list(Collect(A,P)) -- used in ex/term.ML **) |
|
167 |
||
168 |
(* c : list(Collect(B,P)) ==> c : list(B) *) |
|
169 |
val list_CollectD = standard (Collect_subset RS list_mono RS subsetD); |
|
170 |
||
171 |
val prems = goal ListFn.thy |
|
172 |
"l: list({x:A. h(x)=j(x)}) ==> map(h,l) = map(j,l)"; |
|
173 |
by (list_ind_tac "l" prems 1); |
|
6
8ce8c4d13d4d
Installation of new simplifier for ZF. Deleted all congruence rules not
lcp
parents:
0
diff
changeset
|
174 |
by (ALLGOALS (asm_simp_tac list_ss)); |
0 | 175 |
val map_list_Collect = result(); |
176 |
||
177 |
(*** theorems about length ***) |
|
178 |
||
179 |
val prems = goal ListFn.thy |
|
180 |
"xs: list(A) ==> length(map(h,xs)) = length(xs)"; |
|
181 |
by (list_ind_tac "xs" prems 1); |
|
6
8ce8c4d13d4d
Installation of new simplifier for ZF. Deleted all congruence rules not
lcp
parents:
0
diff
changeset
|
182 |
by (ALLGOALS (asm_simp_tac list_ss)); |
0 | 183 |
val length_map = result(); |
184 |
||
185 |
val prems = goal ListFn.thy |
|
186 |
"xs: list(A) ==> length(xs@ys) = length(xs) #+ length(ys)"; |
|
187 |
by (list_ind_tac "xs" prems 1); |
|
6
8ce8c4d13d4d
Installation of new simplifier for ZF. Deleted all congruence rules not
lcp
parents:
0
diff
changeset
|
188 |
by (ALLGOALS (asm_simp_tac list_ss)); |
0 | 189 |
val length_app = result(); |
190 |
||
191 |
(* [| m: nat; n: nat |] ==> m #+ succ(n) = succ(n) #+ m |
|
192 |
Used for rewriting below*) |
|
193 |
val add_commute_succ = nat_succI RSN (2,add_commute); |
|
194 |
||
195 |
val prems = goal ListFn.thy |
|
196 |
"xs: list(A) ==> length(rev(xs)) = length(xs)"; |
|
197 |
by (list_ind_tac "xs" prems 1); |
|
6
8ce8c4d13d4d
Installation of new simplifier for ZF. Deleted all congruence rules not
lcp
parents:
0
diff
changeset
|
198 |
by (ALLGOALS (asm_simp_tac (list_ss addsimps [length_app, add_commute_succ]))); |
0 | 199 |
val length_rev = result(); |
200 |
||
201 |
val prems = goal ListFn.thy |
|
202 |
"ls: list(list(A)) ==> length(flat(ls)) = list_add(map(length,ls))"; |
|
203 |
by (list_ind_tac "ls" prems 1); |
|
6
8ce8c4d13d4d
Installation of new simplifier for ZF. Deleted all congruence rules not
lcp
parents:
0
diff
changeset
|
204 |
by (ALLGOALS (asm_simp_tac (list_ss addsimps [length_app]))); |
0 | 205 |
val length_flat = result(); |
206 |
||
207 |
(*** theorems about app ***) |
|
208 |
||
209 |
val [major] = goal ListFn.thy "xs: list(A) ==> xs@Nil=xs"; |
|
210 |
by (rtac (major RS List.induct) 1); |
|
6
8ce8c4d13d4d
Installation of new simplifier for ZF. Deleted all congruence rules not
lcp
parents:
0
diff
changeset
|
211 |
by (ALLGOALS (asm_simp_tac list_ss)); |
0 | 212 |
val app_right_Nil = result(); |
213 |
||
214 |
val prems = goal ListFn.thy "xs: list(A) ==> (xs@ys)@zs = xs@(ys@zs)"; |
|
215 |
by (list_ind_tac "xs" prems 1); |
|
6
8ce8c4d13d4d
Installation of new simplifier for ZF. Deleted all congruence rules not
lcp
parents:
0
diff
changeset
|
216 |
by (ALLGOALS (asm_simp_tac list_ss)); |
0 | 217 |
val app_assoc = result(); |
218 |
||
219 |
val prems = goal ListFn.thy |
|
220 |
"ls: list(list(A)) ==> flat(ls@ms) = flat(ls)@flat(ms)"; |
|
221 |
by (list_ind_tac "ls" prems 1); |
|
6
8ce8c4d13d4d
Installation of new simplifier for ZF. Deleted all congruence rules not
lcp
parents:
0
diff
changeset
|
222 |
by (ALLGOALS (asm_simp_tac (list_ss addsimps [app_assoc]))); |
0 | 223 |
val flat_app_distrib = result(); |
224 |
||
225 |
(*** theorems about rev ***) |
|
226 |
||
227 |
val prems = goal ListFn.thy "l: list(A) ==> rev(map(h,l)) = map(h,rev(l))"; |
|
228 |
by (list_ind_tac "l" prems 1); |
|
6
8ce8c4d13d4d
Installation of new simplifier for ZF. Deleted all congruence rules not
lcp
parents:
0
diff
changeset
|
229 |
by (ALLGOALS (asm_simp_tac (list_ss addsimps [map_app_distrib]))); |
0 | 230 |
val rev_map_distrib = result(); |
231 |
||
232 |
(*Simplifier needs the premises as assumptions because rewriting will not |
|
233 |
instantiate the variable ?A in the rules' typing conditions; note that |
|
234 |
rev_type does not instantiate ?A. Only the premises do. |
|
235 |
*) |
|
6
8ce8c4d13d4d
Installation of new simplifier for ZF. Deleted all congruence rules not
lcp
parents:
0
diff
changeset
|
236 |
goal ListFn.thy |
8ce8c4d13d4d
Installation of new simplifier for ZF. Deleted all congruence rules not
lcp
parents:
0
diff
changeset
|
237 |
"!!xs. [| xs: list(A); ys: list(A) |] ==> rev(xs@ys) = rev(ys)@rev(xs)"; |
0 | 238 |
by (etac List.induct 1); |
6
8ce8c4d13d4d
Installation of new simplifier for ZF. Deleted all congruence rules not
lcp
parents:
0
diff
changeset
|
239 |
by (ALLGOALS (asm_simp_tac (list_ss addsimps [app_right_Nil,app_assoc]))); |
0 | 240 |
val rev_app_distrib = result(); |
241 |
||
242 |
val prems = goal ListFn.thy "l: list(A) ==> rev(rev(l))=l"; |
|
243 |
by (list_ind_tac "l" prems 1); |
|
6
8ce8c4d13d4d
Installation of new simplifier for ZF. Deleted all congruence rules not
lcp
parents:
0
diff
changeset
|
244 |
by (ALLGOALS (asm_simp_tac (list_ss addsimps [rev_app_distrib]))); |
0 | 245 |
val rev_rev_ident = result(); |
246 |
||
247 |
val prems = goal ListFn.thy |
|
248 |
"ls: list(list(A)) ==> rev(flat(ls)) = flat(map(rev,rev(ls)))"; |
|
249 |
by (list_ind_tac "ls" prems 1); |
|
6
8ce8c4d13d4d
Installation of new simplifier for ZF. Deleted all congruence rules not
lcp
parents:
0
diff
changeset
|
250 |
by (ALLGOALS (asm_simp_tac (list_ss addsimps |
0 | 251 |
[map_app_distrib, flat_app_distrib, rev_app_distrib, app_right_Nil]))); |
252 |
val rev_flat = result(); |
|
253 |
||
254 |
||
255 |
(*** theorems about list_add ***) |
|
256 |
||
257 |
val prems = goal ListFn.thy |
|
258 |
"[| xs: list(nat); ys: list(nat) |] ==> \ |
|
259 |
\ list_add(xs@ys) = list_add(ys) #+ list_add(xs)"; |
|
260 |
by (cut_facts_tac prems 1); |
|
261 |
by (list_ind_tac "xs" prems 1); |
|
262 |
by (ALLGOALS |
|
6
8ce8c4d13d4d
Installation of new simplifier for ZF. Deleted all congruence rules not
lcp
parents:
0
diff
changeset
|
263 |
(asm_simp_tac (list_ss addsimps [add_0_right, add_assoc RS sym]))); |
8ce8c4d13d4d
Installation of new simplifier for ZF. Deleted all congruence rules not
lcp
parents:
0
diff
changeset
|
264 |
by (rtac (add_commute RS subst_context) 1); |
8ce8c4d13d4d
Installation of new simplifier for ZF. Deleted all congruence rules not
lcp
parents:
0
diff
changeset
|
265 |
by (REPEAT (ares_tac [refl, list_add_type] 1)); |
0 | 266 |
val list_add_app = result(); |
267 |
||
268 |
val prems = goal ListFn.thy |
|
269 |
"l: list(nat) ==> list_add(rev(l)) = list_add(l)"; |
|
270 |
by (list_ind_tac "l" prems 1); |
|
271 |
by (ALLGOALS |
|
6
8ce8c4d13d4d
Installation of new simplifier for ZF. Deleted all congruence rules not
lcp
parents:
0
diff
changeset
|
272 |
(asm_simp_tac (list_ss addsimps [list_add_app, add_0_right]))); |
0 | 273 |
val list_add_rev = result(); |
274 |
||
275 |
val prems = goal ListFn.thy |
|
276 |
"ls: list(list(nat)) ==> list_add(flat(ls)) = list_add(map(list_add,ls))"; |
|
277 |
by (list_ind_tac "ls" prems 1); |
|
6
8ce8c4d13d4d
Installation of new simplifier for ZF. Deleted all congruence rules not
lcp
parents:
0
diff
changeset
|
278 |
by (ALLGOALS (asm_simp_tac (list_ss addsimps [list_add_app]))); |
0 | 279 |
by (REPEAT (ares_tac [refl, list_add_type, map_type, add_commute] 1)); |
280 |
val list_add_flat = result(); |
|
281 |
||
282 |
(** New induction rule **) |
|
283 |
||
284 |
val major::prems = goal ListFn.thy |
|
285 |
"[| l: list(A); \ |
|
286 |
\ P(Nil); \ |
|
287 |
\ !!x y. [| x: A; y: list(A); P(y) |] ==> P(y @ [x]) \ |
|
288 |
\ |] ==> P(l)"; |
|
289 |
by (rtac (major RS rev_rev_ident RS subst) 1); |
|
290 |
by (rtac (major RS rev_type RS List.induct) 1); |
|
6
8ce8c4d13d4d
Installation of new simplifier for ZF. Deleted all congruence rules not
lcp
parents:
0
diff
changeset
|
291 |
by (ALLGOALS (asm_simp_tac (list_ss addsimps prems))); |
0 | 292 |
val list_append_induct = result(); |
293 |