author | nipkow |
Mon, 02 Mar 2009 16:53:55 +0100 | |
changeset 30198 | 922f944f03b2 |
parent 27682 | 25aceefd4786 |
child 32235 | 8f9b8d14fc9f |
permissions | -rw-r--r-- |
6708 | 1 |
(* Title: HOL/UNITY/ListOrder |
2 |
ID: $Id$ |
|
3 |
Author: Lawrence C Paulson, Cambridge University Computer Laboratory |
|
4 |
Copyright 1998 University of Cambridge |
|
5 |
||
13798 | 6 |
Lists are partially ordered by Charpentier's Generalized Prefix Relation |
7 |
(xs,ys) : genPrefix(r) |
|
8 |
if ys = xs' @ zs where length xs = length xs' |
|
9 |
and corresponding elements of xs, xs' are pairwise related by r |
|
10 |
||
11 |
Also overloads <= and < for lists! |
|
12 |
||
13 |
Based on Lex/Prefix |
|
6708 | 14 |
*) |
15 |
||
13798 | 16 |
header {*The Prefix Ordering on Lists*} |
17 |
||
27682 | 18 |
theory ListOrder |
19 |
imports Main |
|
20 |
begin |
|
13798 | 21 |
|
23767 | 22 |
inductive_set |
13798 | 23 |
genPrefix :: "('a * 'a)set => ('a list * 'a list)set" |
23767 | 24 |
for r :: "('a * 'a)set" |
25 |
where |
|
13798 | 26 |
Nil: "([],[]) : genPrefix(r)" |
27 |
||
23767 | 28 |
| prepend: "[| (xs,ys) : genPrefix(r); (x,y) : r |] ==> |
13798 | 29 |
(x#xs, y#ys) : genPrefix(r)" |
30 |
||
23767 | 31 |
| append: "(xs,ys) : genPrefix(r) ==> (xs, ys@zs) : genPrefix(r)" |
13798 | 32 |
|
27682 | 33 |
instantiation list :: (type) ord |
34 |
begin |
|
13798 | 35 |
|
27682 | 36 |
definition |
37 |
prefix_def: "xs <= zs \<longleftrightarrow> (xs, zs) : genPrefix Id" |
|
13798 | 38 |
|
27682 | 39 |
definition |
40 |
strict_prefix_def: "xs < zs \<longleftrightarrow> xs \<le> zs \<and> \<not> zs \<le> (xs :: 'a list)" |
|
41 |
||
42 |
instance .. |
|
13798 | 43 |
|
44 |
(*Constants for the <= and >= relations, used below in translations*) |
|
27682 | 45 |
|
46 |
end |
|
47 |
||
13798 | 48 |
constdefs |
49 |
Le :: "(nat*nat) set" |
|
50 |
"Le == {(x,y). x <= y}" |
|
51 |
||
52 |
Ge :: "(nat*nat) set" |
|
53 |
"Ge == {(x,y). y <= x}" |
|
54 |
||
23767 | 55 |
abbreviation |
56 |
pfixLe :: "[nat list, nat list] => bool" (infixl "pfixLe" 50) where |
|
57 |
"xs pfixLe ys == (xs,ys) : genPrefix Le" |
|
13798 | 58 |
|
23767 | 59 |
abbreviation |
60 |
pfixGe :: "[nat list, nat list] => bool" (infixl "pfixGe" 50) where |
|
61 |
"xs pfixGe ys == (xs,ys) : genPrefix Ge" |
|
13798 | 62 |
|
63 |
||
64 |
subsection{*preliminary lemmas*} |
|
65 |
||
66 |
lemma Nil_genPrefix [iff]: "([], xs) : genPrefix r" |
|
67 |
by (cut_tac genPrefix.Nil [THEN genPrefix.append], auto) |
|
68 |
||
69 |
lemma genPrefix_length_le: "(xs,ys) : genPrefix r ==> length xs <= length ys" |
|
70 |
by (erule genPrefix.induct, auto) |
|
71 |
||
72 |
lemma cdlemma: |
|
73 |
"[| (xs', ys'): genPrefix r |] |
|
74 |
==> (ALL x xs. xs' = x#xs --> (EX y ys. ys' = y#ys & (x,y) : r & (xs, ys) : genPrefix r))" |
|
75 |
apply (erule genPrefix.induct, blast, blast) |
|
76 |
apply (force intro: genPrefix.append) |
|
77 |
done |
|
78 |
||
79 |
(*As usual converting it to an elimination rule is tiresome*) |
|
80 |
lemma cons_genPrefixE [elim!]: |
|
81 |
"[| (x#xs, zs): genPrefix r; |
|
82 |
!!y ys. [| zs = y#ys; (x,y) : r; (xs, ys) : genPrefix r |] ==> P |
|
83 |
|] ==> P" |
|
84 |
by (drule cdlemma, simp, blast) |
|
85 |
||
86 |
lemma Cons_genPrefix_Cons [iff]: |
|
87 |
"((x#xs,y#ys) : genPrefix r) = ((x,y) : r & (xs,ys) : genPrefix r)" |
|
88 |
by (blast intro: genPrefix.prepend) |
|
89 |
||
90 |
||
91 |
subsection{*genPrefix is a partial order*} |
|
92 |
||
30198 | 93 |
lemma refl_genPrefix: "refl r ==> refl (genPrefix r)" |
94 |
apply (unfold refl_on_def, auto) |
|
13798 | 95 |
apply (induct_tac "x") |
96 |
prefer 2 apply (blast intro: genPrefix.prepend) |
|
97 |
apply (blast intro: genPrefix.Nil) |
|
98 |
done |
|
99 |
||
30198 | 100 |
lemma genPrefix_refl [simp]: "refl r ==> (l,l) : genPrefix r" |
101 |
by (erule refl_onD [OF refl_genPrefix UNIV_I]) |
|
13798 | 102 |
|
103 |
lemma genPrefix_mono: "r<=s ==> genPrefix r <= genPrefix s" |
|
104 |
apply clarify |
|
105 |
apply (erule genPrefix.induct) |
|
106 |
apply (auto intro: genPrefix.append) |
|
107 |
done |
|
108 |
||
109 |
||
110 |
(** Transitivity **) |
|
111 |
||
112 |
(*A lemma for proving genPrefix_trans_O*) |
|
113 |
lemma append_genPrefix [rule_format]: |
|
114 |
"ALL zs. (xs @ ys, zs) : genPrefix r --> (xs, zs) : genPrefix r" |
|
115 |
by (induct_tac "xs", auto) |
|
116 |
||
117 |
(*Lemma proving transitivity and more*) |
|
118 |
lemma genPrefix_trans_O [rule_format]: |
|
119 |
"(x, y) : genPrefix r |
|
120 |
==> ALL z. (y,z) : genPrefix s --> (x, z) : genPrefix (s O r)" |
|
121 |
apply (erule genPrefix.induct) |
|
122 |
prefer 3 apply (blast dest: append_genPrefix) |
|
123 |
prefer 2 apply (blast intro: genPrefix.prepend, blast) |
|
124 |
done |
|
125 |
||
126 |
lemma genPrefix_trans [rule_format]: |
|
127 |
"[| (x,y) : genPrefix r; (y,z) : genPrefix r; trans r |] |
|
128 |
==> (x,z) : genPrefix r" |
|
129 |
apply (rule trans_O_subset [THEN genPrefix_mono, THEN subsetD]) |
|
130 |
apply assumption |
|
131 |
apply (blast intro: genPrefix_trans_O) |
|
132 |
done |
|
133 |
||
134 |
lemma prefix_genPrefix_trans [rule_format]: |
|
135 |
"[| x<=y; (y,z) : genPrefix r |] ==> (x, z) : genPrefix r" |
|
136 |
apply (unfold prefix_def) |
|
137 |
apply (subst R_O_Id [symmetric], erule genPrefix_trans_O, assumption) |
|
138 |
done |
|
139 |
||
140 |
lemma genPrefix_prefix_trans [rule_format]: |
|
141 |
"[| (x,y) : genPrefix r; y<=z |] ==> (x,z) : genPrefix r" |
|
142 |
apply (unfold prefix_def) |
|
143 |
apply (subst Id_O_R [symmetric], erule genPrefix_trans_O, assumption) |
|
144 |
done |
|
145 |
||
146 |
lemma trans_genPrefix: "trans r ==> trans (genPrefix r)" |
|
147 |
by (blast intro: transI genPrefix_trans) |
|
148 |
||
149 |
||
150 |
(** Antisymmetry **) |
|
151 |
||
152 |
lemma genPrefix_antisym [rule_format]: |
|
153 |
"[| (xs,ys) : genPrefix r; antisym r |] |
|
154 |
==> (ys,xs) : genPrefix r --> xs = ys" |
|
155 |
apply (erule genPrefix.induct) |
|
156 |
txt{*Base case*} |
|
157 |
apply blast |
|
158 |
txt{*prepend case*} |
|
159 |
apply (simp add: antisym_def) |
|
160 |
txt{*append case is the hardest*} |
|
161 |
apply clarify |
|
162 |
apply (subgoal_tac "length zs = 0", force) |
|
163 |
apply (drule genPrefix_length_le)+ |
|
164 |
apply (simp del: length_0_conv) |
|
165 |
done |
|
166 |
||
167 |
lemma antisym_genPrefix: "antisym r ==> antisym (genPrefix r)" |
|
168 |
by (blast intro: antisymI genPrefix_antisym) |
|
169 |
||
170 |
||
171 |
subsection{*recursion equations*} |
|
172 |
||
173 |
lemma genPrefix_Nil [simp]: "((xs, []) : genPrefix r) = (xs = [])" |
|
174 |
apply (induct_tac "xs") |
|
175 |
prefer 2 apply blast |
|
176 |
apply simp |
|
177 |
done |
|
178 |
||
179 |
lemma same_genPrefix_genPrefix [simp]: |
|
30198 | 180 |
"refl r ==> ((xs@ys, xs@zs) : genPrefix r) = ((ys,zs) : genPrefix r)" |
181 |
apply (unfold refl_on_def) |
|
13798 | 182 |
apply (induct_tac "xs") |
183 |
apply (simp_all (no_asm_simp)) |
|
184 |
done |
|
185 |
||
186 |
lemma genPrefix_Cons: |
|
187 |
"((xs, y#ys) : genPrefix r) = |
|
188 |
(xs=[] | (EX z zs. xs=z#zs & (z,y) : r & (zs,ys) : genPrefix r))" |
|
189 |
by (case_tac "xs", auto) |
|
190 |
||
191 |
lemma genPrefix_take_append: |
|
30198 | 192 |
"[| refl r; (xs,ys) : genPrefix r |] |
13798 | 193 |
==> (xs@zs, take (length xs) ys @ zs) : genPrefix r" |
194 |
apply (erule genPrefix.induct) |
|
195 |
apply (frule_tac [3] genPrefix_length_le) |
|
196 |
apply (simp_all (no_asm_simp) add: diff_is_0_eq [THEN iffD2]) |
|
197 |
done |
|
198 |
||
199 |
lemma genPrefix_append_both: |
|
30198 | 200 |
"[| refl r; (xs,ys) : genPrefix r; length xs = length ys |] |
13798 | 201 |
==> (xs@zs, ys @ zs) : genPrefix r" |
202 |
apply (drule genPrefix_take_append, assumption) |
|
203 |
apply (simp add: take_all) |
|
204 |
done |
|
205 |
||
206 |
||
207 |
(*NOT suitable for rewriting since [y] has the form y#ys*) |
|
208 |
lemma append_cons_eq: "xs @ y # ys = (xs @ [y]) @ ys" |
|
209 |
by auto |
|
210 |
||
211 |
lemma aolemma: |
|
30198 | 212 |
"[| (xs,ys) : genPrefix r; refl r |] |
13798 | 213 |
==> length xs < length ys --> (xs @ [ys ! length xs], ys) : genPrefix r" |
214 |
apply (erule genPrefix.induct) |
|
215 |
apply blast |
|
216 |
apply simp |
|
217 |
txt{*Append case is hardest*} |
|
218 |
apply simp |
|
219 |
apply (frule genPrefix_length_le [THEN le_imp_less_or_eq]) |
|
220 |
apply (erule disjE) |
|
221 |
apply (simp_all (no_asm_simp) add: neq_Nil_conv nth_append) |
|
222 |
apply (blast intro: genPrefix.append, auto) |
|
15481 | 223 |
apply (subst append_cons_eq, fast intro: genPrefix_append_both genPrefix.append) |
13798 | 224 |
done |
225 |
||
226 |
lemma append_one_genPrefix: |
|
30198 | 227 |
"[| (xs,ys) : genPrefix r; length xs < length ys; refl r |] |
13798 | 228 |
==> (xs @ [ys ! length xs], ys) : genPrefix r" |
229 |
by (blast intro: aolemma [THEN mp]) |
|
230 |
||
231 |
||
232 |
(** Proving the equivalence with Charpentier's definition **) |
|
233 |
||
234 |
lemma genPrefix_imp_nth [rule_format]: |
|
235 |
"ALL i ys. i < length xs |
|
236 |
--> (xs, ys) : genPrefix r --> (xs ! i, ys ! i) : r" |
|
237 |
apply (induct_tac "xs", auto) |
|
238 |
apply (case_tac "i", auto) |
|
239 |
done |
|
240 |
||
241 |
lemma nth_imp_genPrefix [rule_format]: |
|
242 |
"ALL ys. length xs <= length ys |
|
243 |
--> (ALL i. i < length xs --> (xs ! i, ys ! i) : r) |
|
244 |
--> (xs, ys) : genPrefix r" |
|
245 |
apply (induct_tac "xs") |
|
246 |
apply (simp_all (no_asm_simp) add: less_Suc_eq_0_disj all_conj_distrib) |
|
247 |
apply clarify |
|
248 |
apply (case_tac "ys") |
|
249 |
apply (force+) |
|
250 |
done |
|
251 |
||
252 |
lemma genPrefix_iff_nth: |
|
253 |
"((xs,ys) : genPrefix r) = |
|
254 |
(length xs <= length ys & (ALL i. i < length xs --> (xs!i, ys!i) : r))" |
|
255 |
apply (blast intro: genPrefix_length_le genPrefix_imp_nth nth_imp_genPrefix) |
|
256 |
done |
|
257 |
||
258 |
||
259 |
subsection{*The type of lists is partially ordered*} |
|
260 |
||
30198 | 261 |
declare refl_Id [iff] |
13798 | 262 |
antisym_Id [iff] |
263 |
trans_Id [iff] |
|
264 |
||
265 |
lemma prefix_refl [iff]: "xs <= (xs::'a list)" |
|
266 |
by (simp add: prefix_def) |
|
267 |
||
268 |
lemma prefix_trans: "!!xs::'a list. [| xs <= ys; ys <= zs |] ==> xs <= zs" |
|
269 |
apply (unfold prefix_def) |
|
270 |
apply (blast intro: genPrefix_trans) |
|
271 |
done |
|
272 |
||
273 |
lemma prefix_antisym: "!!xs::'a list. [| xs <= ys; ys <= xs |] ==> xs = ys" |
|
274 |
apply (unfold prefix_def) |
|
275 |
apply (blast intro: genPrefix_antisym) |
|
276 |
done |
|
277 |
||
27682 | 278 |
lemma prefix_less_le_not_le: "!!xs::'a list. (xs < zs) = (xs <= zs & \<not> zs \<le> xs)" |
13798 | 279 |
by (unfold strict_prefix_def, auto) |
6708 | 280 |
|
12338
de0f4a63baa5
renamed class "term" to "type" (actually "HOL.type");
wenzelm
parents:
6810
diff
changeset
|
281 |
instance list :: (type) order |
13798 | 282 |
by (intro_classes, |
283 |
(assumption | rule prefix_refl prefix_trans prefix_antisym |
|
27682 | 284 |
prefix_less_le_not_le)+) |
13798 | 285 |
|
286 |
(*Monotonicity of "set" operator WRT prefix*) |
|
287 |
lemma set_mono: "xs <= ys ==> set xs <= set ys" |
|
288 |
apply (unfold prefix_def) |
|
289 |
apply (erule genPrefix.induct, auto) |
|
290 |
done |
|
291 |
||
292 |
||
293 |
(** recursion equations **) |
|
294 |
||
295 |
lemma Nil_prefix [iff]: "[] <= xs" |
|
296 |
apply (unfold prefix_def) |
|
297 |
apply (simp add: Nil_genPrefix) |
|
298 |
done |
|
299 |
||
300 |
lemma prefix_Nil [simp]: "(xs <= []) = (xs = [])" |
|
301 |
apply (unfold prefix_def) |
|
302 |
apply (simp add: genPrefix_Nil) |
|
303 |
done |
|
304 |
||
305 |
lemma Cons_prefix_Cons [simp]: "(x#xs <= y#ys) = (x=y & xs<=ys)" |
|
306 |
by (simp add: prefix_def) |
|
307 |
||
308 |
lemma same_prefix_prefix [simp]: "(xs@ys <= xs@zs) = (ys <= zs)" |
|
309 |
by (simp add: prefix_def) |
|
310 |
||
311 |
lemma append_prefix [iff]: "(xs@ys <= xs) = (ys <= [])" |
|
312 |
by (insert same_prefix_prefix [of xs ys "[]"], simp) |
|
313 |
||
314 |
lemma prefix_appendI [simp]: "xs <= ys ==> xs <= ys@zs" |
|
315 |
apply (unfold prefix_def) |
|
316 |
apply (erule genPrefix.append) |
|
317 |
done |
|
318 |
||
319 |
lemma prefix_Cons: |
|
320 |
"(xs <= y#ys) = (xs=[] | (? zs. xs=y#zs & zs <= ys))" |
|
321 |
by (simp add: prefix_def genPrefix_Cons) |
|
322 |
||
323 |
lemma append_one_prefix: |
|
324 |
"[| xs <= ys; length xs < length ys |] ==> xs @ [ys ! length xs] <= ys" |
|
325 |
apply (unfold prefix_def) |
|
326 |
apply (simp add: append_one_genPrefix) |
|
327 |
done |
|
328 |
||
329 |
lemma prefix_length_le: "xs <= ys ==> length xs <= length ys" |
|
330 |
apply (unfold prefix_def) |
|
331 |
apply (erule genPrefix_length_le) |
|
332 |
done |
|
333 |
||
334 |
lemma splemma: "xs<=ys ==> xs~=ys --> length xs < length ys" |
|
335 |
apply (unfold prefix_def) |
|
336 |
apply (erule genPrefix.induct, auto) |
|
337 |
done |
|
338 |
||
339 |
lemma strict_prefix_length_less: "xs < ys ==> length xs < length ys" |
|
340 |
apply (unfold strict_prefix_def) |
|
341 |
apply (blast intro: splemma [THEN mp]) |
|
342 |
done |
|
343 |
||
344 |
lemma mono_length: "mono length" |
|
345 |
by (blast intro: monoI prefix_length_le) |
|
346 |
||
347 |
(*Equivalence to the definition used in Lex/Prefix.thy*) |
|
348 |
lemma prefix_iff: "(xs <= zs) = (EX ys. zs = xs@ys)" |
|
349 |
apply (unfold prefix_def) |
|
350 |
apply (auto simp add: genPrefix_iff_nth nth_append) |
|
351 |
apply (rule_tac x = "drop (length xs) zs" in exI) |
|
352 |
apply (rule nth_equalityI) |
|
353 |
apply (simp_all (no_asm_simp) add: nth_append) |
|
354 |
done |
|
355 |
||
356 |
lemma prefix_snoc [simp]: "(xs <= ys@[y]) = (xs = ys@[y] | xs <= ys)" |
|
357 |
apply (simp add: prefix_iff) |
|
358 |
apply (rule iffI) |
|
359 |
apply (erule exE) |
|
360 |
apply (rename_tac "zs") |
|
361 |
apply (rule_tac xs = zs in rev_exhaust) |
|
362 |
apply simp |
|
363 |
apply clarify |
|
364 |
apply (simp del: append_assoc add: append_assoc [symmetric], force) |
|
365 |
done |
|
366 |
||
367 |
lemma prefix_append_iff: |
|
368 |
"(xs <= ys@zs) = (xs <= ys | (? us. xs = ys@us & us <= zs))" |
|
369 |
apply (rule_tac xs = zs in rev_induct) |
|
370 |
apply force |
|
371 |
apply (simp del: append_assoc add: append_assoc [symmetric], force) |
|
372 |
done |
|
373 |
||
374 |
(*Although the prefix ordering is not linear, the prefixes of a list |
|
375 |
are linearly ordered.*) |
|
376 |
lemma common_prefix_linear [rule_format]: |
|
377 |
"!!zs::'a list. xs <= zs --> ys <= zs --> xs <= ys | ys <= xs" |
|
378 |
by (rule_tac xs = zs in rev_induct, auto) |
|
379 |
||
380 |
||
381 |
subsection{*pfixLe, pfixGe: properties inherited from the translations*} |
|
382 |
||
383 |
(** pfixLe **) |
|
384 |
||
30198 | 385 |
lemma refl_Le [iff]: "refl Le" |
386 |
by (unfold refl_on_def Le_def, auto) |
|
13798 | 387 |
|
388 |
lemma antisym_Le [iff]: "antisym Le" |
|
389 |
by (unfold antisym_def Le_def, auto) |
|
390 |
||
391 |
lemma trans_Le [iff]: "trans Le" |
|
392 |
by (unfold trans_def Le_def, auto) |
|
393 |
||
394 |
lemma pfixLe_refl [iff]: "x pfixLe x" |
|
395 |
by simp |
|
396 |
||
397 |
lemma pfixLe_trans: "[| x pfixLe y; y pfixLe z |] ==> x pfixLe z" |
|
398 |
by (blast intro: genPrefix_trans) |
|
399 |
||
400 |
lemma pfixLe_antisym: "[| x pfixLe y; y pfixLe x |] ==> x = y" |
|
401 |
by (blast intro: genPrefix_antisym) |
|
402 |
||
403 |
lemma prefix_imp_pfixLe: "xs<=ys ==> xs pfixLe ys" |
|
404 |
apply (unfold prefix_def Le_def) |
|
405 |
apply (blast intro: genPrefix_mono [THEN [2] rev_subsetD]) |
|
406 |
done |
|
407 |
||
30198 | 408 |
lemma refl_Ge [iff]: "refl Ge" |
409 |
by (unfold refl_on_def Ge_def, auto) |
|
13798 | 410 |
|
411 |
lemma antisym_Ge [iff]: "antisym Ge" |
|
412 |
by (unfold antisym_def Ge_def, auto) |
|
413 |
||
414 |
lemma trans_Ge [iff]: "trans Ge" |
|
415 |
by (unfold trans_def Ge_def, auto) |
|
416 |
||
417 |
lemma pfixGe_refl [iff]: "x pfixGe x" |
|
418 |
by simp |
|
419 |
||
420 |
lemma pfixGe_trans: "[| x pfixGe y; y pfixGe z |] ==> x pfixGe z" |
|
421 |
by (blast intro: genPrefix_trans) |
|
422 |
||
423 |
lemma pfixGe_antisym: "[| x pfixGe y; y pfixGe x |] ==> x = y" |
|
424 |
by (blast intro: genPrefix_antisym) |
|
425 |
||
426 |
lemma prefix_imp_pfixGe: "xs<=ys ==> xs pfixGe ys" |
|
427 |
apply (unfold prefix_def Ge_def) |
|
428 |
apply (blast intro: genPrefix_mono [THEN [2] rev_subsetD]) |
|
429 |
done |
|
6708 | 430 |
|
431 |
end |