author | Manuel Eberl <eberlm@in.tum.de> |
Sat, 30 Nov 2019 13:47:33 +0100 | |
changeset 71189 | 954ee5acaae0 |
parent 70911 | 38298c04c12e |
child 71393 | fce780f9c9c6 |
permissions | -rw-r--r-- |
13462 | 1 |
(* Title: HOL/List.thy |
68709 | 2 |
Author: Tobias Nipkow; proofs tidied by LCP |
923 | 3 |
*) |
4 |
||
60758 | 5 |
section \<open>The datatype of finite lists\<close> |
13122 | 6 |
|
15131 | 7 |
theory List |
58916 | 8 |
imports Sledgehammer Code_Numeral Lifting_Set |
15131 | 9 |
begin |
923 | 10 |
|
58310 | 11 |
datatype (set: 'a) list = |
57200
aab87ffa60cc
use 'where' clause for selector default value syntax
blanchet
parents:
57198
diff
changeset
|
12 |
Nil ("[]") |
55405
0a155051bd9d
use new selector support to define 'the', 'hd', 'tl'
blanchet
parents:
55404
diff
changeset
|
13 |
| Cons (hd: 'a) (tl: "'a list") (infixr "#" 65) |
57206
d9be905d6283
changed syntax of map: and rel: arguments to BNF-based datatypes
blanchet
parents:
57200
diff
changeset
|
14 |
for |
d9be905d6283
changed syntax of map: and rel: arguments to BNF-based datatypes
blanchet
parents:
57200
diff
changeset
|
15 |
map: map |
d9be905d6283
changed syntax of map: and rel: arguments to BNF-based datatypes
blanchet
parents:
57200
diff
changeset
|
16 |
rel: list_all2 |
62328 | 17 |
pred: list_all |
57200
aab87ffa60cc
use 'where' clause for selector default value syntax
blanchet
parents:
57198
diff
changeset
|
18 |
where |
aab87ffa60cc
use 'where' clause for selector default value syntax
blanchet
parents:
57198
diff
changeset
|
19 |
"tl [] = []" |
57123
b5324647e0f1
tuned whitespace, to make datatype definitions slightly less intimidating
blanchet
parents:
57091
diff
changeset
|
20 |
|
55531
601ca8efa000
renamed 'datatype_new_compat' to 'datatype_compat'
blanchet
parents:
55525
diff
changeset
|
21 |
datatype_compat list |
55404
5cb95b79a51f
transformed 'option' and 'list' into new-style datatypes (but register them as old-style as well)
blanchet
parents:
55148
diff
changeset
|
22 |
|
55406 | 23 |
lemma [case_names Nil Cons, cases type: list]: |
61799 | 24 |
\<comment> \<open>for backward compatibility -- names of variables differ\<close> |
55406 | 25 |
"(y = [] \<Longrightarrow> P) \<Longrightarrow> (\<And>a list. y = a # list \<Longrightarrow> P) \<Longrightarrow> P" |
26 |
by (rule list.exhaust) |
|
27 |
||
28 |
lemma [case_names Nil Cons, induct type: list]: |
|
61799 | 29 |
\<comment> \<open>for backward compatibility -- names of variables differ\<close> |
55406 | 30 |
"P [] \<Longrightarrow> (\<And>a list. P list \<Longrightarrow> P (a # list)) \<Longrightarrow> P list" |
31 |
by (rule list.induct) |
|
32 |
||
60758 | 33 |
text \<open>Compatibility:\<close> |
34 |
||
35 |
setup \<open>Sign.mandatory_path "list"\<close> |
|
55404
5cb95b79a51f
transformed 'option' and 'list' into new-style datatypes (but register them as old-style as well)
blanchet
parents:
55148
diff
changeset
|
36 |
|
5cb95b79a51f
transformed 'option' and 'list' into new-style datatypes (but register them as old-style as well)
blanchet
parents:
55148
diff
changeset
|
37 |
lemmas inducts = list.induct |
5cb95b79a51f
transformed 'option' and 'list' into new-style datatypes (but register them as old-style as well)
blanchet
parents:
55148
diff
changeset
|
38 |
lemmas recs = list.rec |
5cb95b79a51f
transformed 'option' and 'list' into new-style datatypes (but register them as old-style as well)
blanchet
parents:
55148
diff
changeset
|
39 |
lemmas cases = list.case |
5cb95b79a51f
transformed 'option' and 'list' into new-style datatypes (but register them as old-style as well)
blanchet
parents:
55148
diff
changeset
|
40 |
|
60758 | 41 |
setup \<open>Sign.parent_path\<close> |
55404
5cb95b79a51f
transformed 'option' and 'list' into new-style datatypes (but register them as old-style as well)
blanchet
parents:
55148
diff
changeset
|
42 |
|
57816
d8bbb97689d3
no need for 'set_simps' now that 'datatype_new' generates the desired 'set' property
blanchet
parents:
57599
diff
changeset
|
43 |
lemmas set_simps = list.set (* legacy *) |
d8bbb97689d3
no need for 'set_simps' now that 'datatype_new' generates the desired 'set' property
blanchet
parents:
57599
diff
changeset
|
44 |
|
34941 | 45 |
syntax |
61799 | 46 |
\<comment> \<open>list Enumeration\<close> |
35115 | 47 |
"_list" :: "args => 'a list" ("[(_)]") |
34941 | 48 |
|
49 |
translations |
|
50 |
"[x, xs]" == "x#[xs]" |
|
51 |
"[x]" == "x#[]" |
|
52 |
||
35115 | 53 |
|
60758 | 54 |
subsection \<open>Basic list processing functions\<close> |
15302 | 55 |
|
58135 | 56 |
primrec (nonexhaustive) last :: "'a list \<Rightarrow> 'a" where |
50548 | 57 |
"last (x # xs) = (if xs = [] then x else last xs)" |
58 |
||
59 |
primrec butlast :: "'a list \<Rightarrow> 'a list" where |
|
57816
d8bbb97689d3
no need for 'set_simps' now that 'datatype_new' generates the desired 'set' property
blanchet
parents:
57599
diff
changeset
|
60 |
"butlast [] = []" | |
50548 | 61 |
"butlast (x # xs) = (if xs = [] then [] else x # butlast xs)" |
62 |
||
55584 | 63 |
lemma set_rec: "set xs = rec_list {} (\<lambda>x _. insert x) xs" |
64 |
by (induct xs) auto |
|
50548 | 65 |
|
66 |
definition coset :: "'a list \<Rightarrow> 'a set" where |
|
67 |
[simp]: "coset xs = - set xs" |
|
68 |
||
69 |
primrec append :: "'a list \<Rightarrow> 'a list \<Rightarrow> 'a list" (infixr "@" 65) where |
|
70 |
append_Nil: "[] @ ys = ys" | |
|
71 |
append_Cons: "(x#xs) @ ys = x # xs @ ys" |
|
72 |
||
73 |
primrec rev :: "'a list \<Rightarrow> 'a list" where |
|
74 |
"rev [] = []" | |
|
75 |
"rev (x # xs) = rev xs @ [x]" |
|
76 |
||
77 |
primrec filter:: "('a \<Rightarrow> bool) \<Rightarrow> 'a list \<Rightarrow> 'a list" where |
|
78 |
"filter P [] = []" | |
|
79 |
"filter P (x # xs) = (if P x then x # filter P xs else filter P xs)" |
|
34941 | 80 |
|
68249
949d93804740
First step to remove nonstandard "[x <- xs. P]" syntax: only input
nipkow
parents:
68215
diff
changeset
|
81 |
text \<open>Special input syntax for filter:\<close> |
61955
e96292f32c3c
former "xsymbols" syntax is used by default, and ASCII replacement syntax with print mode "ASCII";
wenzelm
parents:
61941
diff
changeset
|
82 |
syntax (ASCII) |
e96292f32c3c
former "xsymbols" syntax is used by default, and ASCII replacement syntax with print mode "ASCII";
wenzelm
parents:
61941
diff
changeset
|
83 |
"_filter" :: "[pttrn, 'a list, bool] => 'a list" ("(1[_<-_./ _])") |
34941 | 84 |
syntax |
61955
e96292f32c3c
former "xsymbols" syntax is used by default, and ASCII replacement syntax with print mode "ASCII";
wenzelm
parents:
61941
diff
changeset
|
85 |
"_filter" :: "[pttrn, 'a list, bool] => 'a list" ("(1[_\<leftarrow>_ ./ _])") |
34941 | 86 |
translations |
68249
949d93804740
First step to remove nonstandard "[x <- xs. P]" syntax: only input
nipkow
parents:
68215
diff
changeset
|
87 |
"[x<-xs . P]" \<rightharpoonup> "CONST filter (\<lambda>x. P) xs" |
34941 | 88 |
|
50548 | 89 |
primrec fold :: "('a \<Rightarrow> 'b \<Rightarrow> 'b) \<Rightarrow> 'a list \<Rightarrow> 'b \<Rightarrow> 'b" where |
90 |
fold_Nil: "fold f [] = id" | |
|
91 |
fold_Cons: "fold f (x # xs) = fold f xs \<circ> f x" |
|
92 |
||
93 |
primrec foldr :: "('a \<Rightarrow> 'b \<Rightarrow> 'b) \<Rightarrow> 'a list \<Rightarrow> 'b \<Rightarrow> 'b" where |
|
94 |
foldr_Nil: "foldr f [] = id" | |
|
95 |
foldr_Cons: "foldr f (x # xs) = f x \<circ> foldr f xs" |
|
96 |
||
97 |
primrec foldl :: "('b \<Rightarrow> 'a \<Rightarrow> 'b) \<Rightarrow> 'b \<Rightarrow> 'a list \<Rightarrow> 'b" where |
|
98 |
foldl_Nil: "foldl f a [] = a" | |
|
99 |
foldl_Cons: "foldl f a (x # xs) = foldl f (f a x) xs" |
|
100 |
||
101 |
primrec concat:: "'a list list \<Rightarrow> 'a list" where |
|
102 |
"concat [] = []" | |
|
103 |
"concat (x # xs) = x @ concat xs" |
|
104 |
||
105 |
primrec drop:: "nat \<Rightarrow> 'a list \<Rightarrow> 'a list" where |
|
106 |
drop_Nil: "drop n [] = []" | |
|
107 |
drop_Cons: "drop n (x # xs) = (case n of 0 \<Rightarrow> x # xs | Suc m \<Rightarrow> drop m xs)" |
|
61799 | 108 |
\<comment> \<open>Warning: simpset does not contain this definition, but separate |
109 |
theorems for \<open>n = 0\<close> and \<open>n = Suc k\<close>\<close> |
|
34941 | 110 |
|
50548 | 111 |
primrec take:: "nat \<Rightarrow> 'a list \<Rightarrow> 'a list" where |
112 |
take_Nil:"take n [] = []" | |
|
113 |
take_Cons: "take n (x # xs) = (case n of 0 \<Rightarrow> [] | Suc m \<Rightarrow> x # take m xs)" |
|
61799 | 114 |
\<comment> \<open>Warning: simpset does not contain this definition, but separate |
115 |
theorems for \<open>n = 0\<close> and \<open>n = Suc k\<close>\<close> |
|
34941 | 116 |
|
58135 | 117 |
primrec (nonexhaustive) nth :: "'a list => nat => 'a" (infixl "!" 100) where |
50548 | 118 |
nth_Cons: "(x # xs) ! n = (case n of 0 \<Rightarrow> x | Suc k \<Rightarrow> xs ! k)" |
61799 | 119 |
\<comment> \<open>Warning: simpset does not contain this definition, but separate |
120 |
theorems for \<open>n = 0\<close> and \<open>n = Suc k\<close>\<close> |
|
34941 | 121 |
|
50548 | 122 |
primrec list_update :: "'a list \<Rightarrow> nat \<Rightarrow> 'a \<Rightarrow> 'a list" where |
123 |
"list_update [] i v = []" | |
|
124 |
"list_update (x # xs) i v = |
|
125 |
(case i of 0 \<Rightarrow> v # xs | Suc j \<Rightarrow> x # list_update xs j v)" |
|
923 | 126 |
|
41229
d797baa3d57c
replaced command 'nonterminals' by slightly modernized version 'nonterminal';
wenzelm
parents:
41075
diff
changeset
|
127 |
nonterminal lupdbinds and lupdbind |
5077
71043526295f
* HOL/List: new function list_update written xs[i:=v] that updates the i-th
nipkow
parents:
4643
diff
changeset
|
128 |
|
923 | 129 |
syntax |
13366 | 130 |
"_lupdbind":: "['a, 'a] => lupdbind" ("(2_ :=/ _)") |
131 |
"" :: "lupdbind => lupdbinds" ("_") |
|
132 |
"_lupdbinds" :: "[lupdbind, lupdbinds] => lupdbinds" ("_,/ _") |
|
69084 | 133 |
"_LUpdate" :: "['a, lupdbinds] => 'a" ("_/[(_)]" [1000,0] 900) |
5077
71043526295f
* HOL/List: new function list_update written xs[i:=v] that updates the i-th
nipkow
parents:
4643
diff
changeset
|
134 |
|
923 | 135 |
translations |
35115 | 136 |
"_LUpdate xs (_lupdbinds b bs)" == "_LUpdate (_LUpdate xs b) bs" |
34941 | 137 |
"xs[i:=x]" == "CONST list_update xs i x" |
138 |
||
50548 | 139 |
primrec takeWhile :: "('a \<Rightarrow> bool) \<Rightarrow> 'a list \<Rightarrow> 'a list" where |
140 |
"takeWhile P [] = []" | |
|
141 |
"takeWhile P (x # xs) = (if P x then x # takeWhile P xs else [])" |
|
142 |
||
143 |
primrec dropWhile :: "('a \<Rightarrow> bool) \<Rightarrow> 'a list \<Rightarrow> 'a list" where |
|
144 |
"dropWhile P [] = []" | |
|
145 |
"dropWhile P (x # xs) = (if P x then dropWhile P xs else x # xs)" |
|
146 |
||
147 |
primrec zip :: "'a list \<Rightarrow> 'b list \<Rightarrow> ('a \<times> 'b) list" where |
|
148 |
"zip xs [] = []" | |
|
149 |
zip_Cons: "zip xs (y # ys) = |
|
67091 | 150 |
(case xs of [] \<Rightarrow> [] | z # zs \<Rightarrow> (z, y) # zip zs ys)" |
61799 | 151 |
\<comment> \<open>Warning: simpset does not contain this definition, but separate |
152 |
theorems for \<open>xs = []\<close> and \<open>xs = z # zs\<close>\<close> |
|
34941 | 153 |
|
66656 | 154 |
abbreviation map2 :: "('a \<Rightarrow> 'b \<Rightarrow> 'c) \<Rightarrow> 'a list \<Rightarrow> 'b list \<Rightarrow> 'c list" where |
155 |
"map2 f xs ys \<equiv> map (\<lambda>(x,y). f x y) (zip xs ys)" |
|
66655 | 156 |
|
50548 | 157 |
primrec product :: "'a list \<Rightarrow> 'b list \<Rightarrow> ('a \<times> 'b) list" where |
158 |
"product [] _ = []" | |
|
159 |
"product (x#xs) ys = map (Pair x) ys @ product xs ys" |
|
49948
744934b818c7
moved quite generic material from theory Enum to more appropriate places
haftmann
parents:
49808
diff
changeset
|
160 |
|
744934b818c7
moved quite generic material from theory Enum to more appropriate places
haftmann
parents:
49808
diff
changeset
|
161 |
hide_const (open) product |
744934b818c7
moved quite generic material from theory Enum to more appropriate places
haftmann
parents:
49808
diff
changeset
|
162 |
|
53721
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
traytel
parents:
53689
diff
changeset
|
163 |
primrec product_lists :: "'a list list \<Rightarrow> 'a list list" where |
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
traytel
parents:
53689
diff
changeset
|
164 |
"product_lists [] = [[]]" | |
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
traytel
parents:
53689
diff
changeset
|
165 |
"product_lists (xs # xss) = concat (map (\<lambda>x. map (Cons x) (product_lists xss)) xs)" |
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
traytel
parents:
53689
diff
changeset
|
166 |
|
50548 | 167 |
primrec upt :: "nat \<Rightarrow> nat \<Rightarrow> nat list" ("(1[_..</_'])") where |
168 |
upt_0: "[i..<0] = []" | |
|
68709 | 169 |
upt_Suc: "[i..<(Suc j)] = (if i \<le> j then [i..<j] @ [j] else [])" |
50548 | 170 |
|
171 |
definition insert :: "'a \<Rightarrow> 'a list \<Rightarrow> 'a list" where |
|
172 |
"insert x xs = (if x \<in> set xs then xs else x # xs)" |
|
34978
874150ddd50a
canonical insert operation; generalized lemma foldl_apply_inv to foldl_apply
haftmann
parents:
34942
diff
changeset
|
173 |
|
57198 | 174 |
definition union :: "'a list \<Rightarrow> 'a list \<Rightarrow> 'a list" where |
175 |
"union = fold insert" |
|
176 |
||
177 |
hide_const (open) insert union |
|
178 |
hide_fact (open) insert_def union_def |
|
34978
874150ddd50a
canonical insert operation; generalized lemma foldl_apply_inv to foldl_apply
haftmann
parents:
34942
diff
changeset
|
179 |
|
47122 | 180 |
primrec find :: "('a \<Rightarrow> bool) \<Rightarrow> 'a list \<Rightarrow> 'a option" where |
50548 | 181 |
"find _ [] = None" | |
182 |
"find P (x#xs) = (if P x then Some x else find P xs)" |
|
47122 | 183 |
|
61799 | 184 |
text \<open>In the context of multisets, \<open>count_list\<close> is equivalent to |
69593 | 185 |
\<^term>\<open>count \<circ> mset\<close> and it it advisable to use the latter.\<close> |
60541 | 186 |
primrec count_list :: "'a list \<Rightarrow> 'a \<Rightarrow> nat" where |
187 |
"count_list [] y = 0" | |
|
188 |
"count_list (x#xs) y = (if x=y then count_list xs y + 1 else count_list xs y)" |
|
59728 | 189 |
|
55807 | 190 |
definition |
191 |
"extract" :: "('a \<Rightarrow> bool) \<Rightarrow> 'a list \<Rightarrow> ('a list * 'a * 'a list) option" |
|
192 |
where "extract P xs = |
|
67091 | 193 |
(case dropWhile (Not \<circ> P) xs of |
55807 | 194 |
[] \<Rightarrow> None | |
67091 | 195 |
y#ys \<Rightarrow> Some(takeWhile (Not \<circ> P) xs, y, ys))" |
55807 | 196 |
|
197 |
hide_const (open) "extract" |
|
198 |
||
51096 | 199 |
primrec those :: "'a option list \<Rightarrow> 'a list option" |
200 |
where |
|
201 |
"those [] = Some []" | |
|
202 |
"those (x # xs) = (case x of |
|
203 |
None \<Rightarrow> None |
|
55466 | 204 |
| Some y \<Rightarrow> map_option (Cons y) (those xs))" |
51096 | 205 |
|
50548 | 206 |
primrec remove1 :: "'a \<Rightarrow> 'a list \<Rightarrow> 'a list" where |
207 |
"remove1 x [] = []" | |
|
208 |
"remove1 x (y # xs) = (if x = y then xs else y # remove1 x xs)" |
|
209 |
||
210 |
primrec removeAll :: "'a \<Rightarrow> 'a list \<Rightarrow> 'a list" where |
|
211 |
"removeAll x [] = []" | |
|
212 |
"removeAll x (y # xs) = (if x = y then removeAll x xs else y # removeAll x xs)" |
|
213 |
||
214 |
primrec distinct :: "'a list \<Rightarrow> bool" where |
|
215 |
"distinct [] \<longleftrightarrow> True" | |
|
216 |
"distinct (x # xs) \<longleftrightarrow> x \<notin> set xs \<and> distinct xs" |
|
217 |
||
218 |
primrec remdups :: "'a list \<Rightarrow> 'a list" where |
|
219 |
"remdups [] = []" | |
|
220 |
"remdups (x # xs) = (if x \<in> set xs then remdups xs else x # remdups xs)" |
|
221 |
||
53721
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
traytel
parents:
53689
diff
changeset
|
222 |
fun remdups_adj :: "'a list \<Rightarrow> 'a list" where |
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
traytel
parents:
53689
diff
changeset
|
223 |
"remdups_adj [] = []" | |
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
traytel
parents:
53689
diff
changeset
|
224 |
"remdups_adj [x] = [x]" | |
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
traytel
parents:
53689
diff
changeset
|
225 |
"remdups_adj (x # y # xs) = (if x = y then remdups_adj (x # xs) else x # remdups_adj (y # xs))" |
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
traytel
parents:
53689
diff
changeset
|
226 |
|
50548 | 227 |
primrec replicate :: "nat \<Rightarrow> 'a \<Rightarrow> 'a list" where |
228 |
replicate_0: "replicate 0 x = []" | |
|
229 |
replicate_Suc: "replicate (Suc n) x = x # replicate n x" |
|
3342
ec3b55fcb165
New operator "lists" for formalizing sets of lists
paulson
parents:
3320
diff
changeset
|
230 |
|
60758 | 231 |
text \<open> |
61799 | 232 |
Function \<open>size\<close> is overloaded for all datatypes. Users may |
233 |
refer to the list version as \<open>length\<close>.\<close> |
|
13142 | 234 |
|
50548 | 235 |
abbreviation length :: "'a list \<Rightarrow> nat" where |
236 |
"length \<equiv> size" |
|
15307 | 237 |
|
51173 | 238 |
definition enumerate :: "nat \<Rightarrow> 'a list \<Rightarrow> (nat \<times> 'a) list" where |
239 |
enumerate_eq_zip: "enumerate n xs = zip [n..<n + length xs] xs" |
|
240 |
||
46440
d4994e2e7364
use 'primrec' to define "rotate1", for uniformity (and to help first-order tools that rely on "Spec_Rules")
blanchet
parents:
46439
diff
changeset
|
241 |
primrec rotate1 :: "'a list \<Rightarrow> 'a list" where |
50548 | 242 |
"rotate1 [] = []" | |
243 |
"rotate1 (x # xs) = xs @ [x]" |
|
244 |
||
245 |
definition rotate :: "nat \<Rightarrow> 'a list \<Rightarrow> 'a list" where |
|
246 |
"rotate n = rotate1 ^^ n" |
|
247 |
||
65956
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65350
diff
changeset
|
248 |
definition nths :: "'a list => nat set => 'a list" where |
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65350
diff
changeset
|
249 |
"nths xs A = map fst (filter (\<lambda>p. snd p \<in> A) (zip xs [0..<size xs]))" |
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65350
diff
changeset
|
250 |
|
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65350
diff
changeset
|
251 |
primrec subseqs :: "'a list \<Rightarrow> 'a list list" where |
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65350
diff
changeset
|
252 |
"subseqs [] = [[]]" | |
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65350
diff
changeset
|
253 |
"subseqs (x#xs) = (let xss = subseqs xs in map (Cons x) xss @ xss)" |
50548 | 254 |
|
255 |
primrec n_lists :: "nat \<Rightarrow> 'a list \<Rightarrow> 'a list list" where |
|
256 |
"n_lists 0 xs = [[]]" | |
|
257 |
"n_lists (Suc n) xs = concat (map (\<lambda>ys. map (\<lambda>y. y # ys) xs) (n_lists n xs))" |
|
49948
744934b818c7
moved quite generic material from theory Enum to more appropriate places
haftmann
parents:
49808
diff
changeset
|
258 |
|
744934b818c7
moved quite generic material from theory Enum to more appropriate places
haftmann
parents:
49808
diff
changeset
|
259 |
hide_const (open) n_lists |
744934b818c7
moved quite generic material from theory Enum to more appropriate places
haftmann
parents:
49808
diff
changeset
|
260 |
|
69075 | 261 |
function splice :: "'a list \<Rightarrow> 'a list \<Rightarrow> 'a list" where |
40593
1e57b18d27b1
code eqn for slice was missing; redefined splice with fun
nipkow
parents:
40365
diff
changeset
|
262 |
"splice [] ys = ys" | |
69075 | 263 |
"splice (x#xs) ys = x # splice ys xs" |
264 |
by pat_completeness auto |
|
265 |
||
266 |
termination |
|
267 |
by(relation "measure(\<lambda>(xs,ys). size xs + size ys)") auto |
|
21061
580dfc999ef6
added normal post setup; cleaned up "execution" constants
haftmann
parents:
21046
diff
changeset
|
268 |
|
69107 | 269 |
function shuffles where |
270 |
"shuffles [] ys = {ys}" |
|
271 |
| "shuffles xs [] = {xs}" |
|
272 |
| "shuffles (x # xs) (y # ys) = (#) x ` shuffles xs (y # ys) \<union> (#) y ` shuffles (x # xs) ys" |
|
65350
b149abe619f7
added shuffle product to HOL/List
eberlm <eberlm@in.tum.de>
parents:
64966
diff
changeset
|
273 |
by pat_completeness simp_all |
b149abe619f7
added shuffle product to HOL/List
eberlm <eberlm@in.tum.de>
parents:
64966
diff
changeset
|
274 |
termination by lexicographic_order |
b149abe619f7
added shuffle product to HOL/List
eberlm <eberlm@in.tum.de>
parents:
64966
diff
changeset
|
275 |
|
69593 | 276 |
text\<open>Use only if you cannot use \<^const>\<open>Min\<close> instead:\<close> |
67170 | 277 |
fun min_list :: "'a::ord list \<Rightarrow> 'a" where |
278 |
"min_list (x # xs) = (case xs of [] \<Rightarrow> x | _ \<Rightarrow> min x (min_list xs))" |
|
279 |
||
280 |
text\<open>Returns first minimum:\<close> |
|
281 |
fun arg_min_list :: "('a \<Rightarrow> ('b::linorder)) \<Rightarrow> 'a list \<Rightarrow> 'a" where |
|
282 |
"arg_min_list f [x] = x" | |
|
283 |
"arg_min_list f (x#y#zs) = (let m = arg_min_list f (y#zs) in if f x \<le> f m then x else m)" |
|
284 |
||
60758 | 285 |
text\<open> |
26771 | 286 |
\begin{figure}[htbp] |
287 |
\fbox{ |
|
288 |
\begin{tabular}{l} |
|
27381 | 289 |
@{lemma "[a,b]@[c,d] = [a,b,c,d]" by simp}\\ |
290 |
@{lemma "length [a,b,c] = 3" by simp}\\ |
|
291 |
@{lemma "set [a,b,c] = {a,b,c}" by simp}\\ |
|
292 |
@{lemma "map f [a,b,c] = [f a, f b, f c]" by simp}\\ |
|
293 |
@{lemma "rev [a,b,c] = [c,b,a]" by simp}\\ |
|
294 |
@{lemma "hd [a,b,c,d] = a" by simp}\\ |
|
295 |
@{lemma "tl [a,b,c,d] = [b,c,d]" by simp}\\ |
|
296 |
@{lemma "last [a,b,c,d] = d" by simp}\\ |
|
297 |
@{lemma "butlast [a,b,c,d] = [a,b,c]" by simp}\\ |
|
298 |
@{lemma[source] "filter (\<lambda>n::nat. n<2) [0,2,1] = [0,1]" by simp}\\ |
|
299 |
@{lemma "concat [[a,b],[c,d,e],[],[f]] = [a,b,c,d,e,f]" by simp}\\ |
|
46133
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
haftmann
parents:
46125
diff
changeset
|
300 |
@{lemma "fold f [a,b,c] x = f c (f b (f a x))" by simp}\\ |
47397
d654c73e4b12
no preference wrt. fold(l/r); prefer fold rather than foldr for iterating over lists in generated code
haftmann
parents:
47131
diff
changeset
|
301 |
@{lemma "foldr f [a,b,c] x = f a (f b (f c x))" by simp}\\ |
d654c73e4b12
no preference wrt. fold(l/r); prefer fold rather than foldr for iterating over lists in generated code
haftmann
parents:
47131
diff
changeset
|
302 |
@{lemma "foldl f x [a,b,c] = f (f (f x a) b) c" by simp}\\ |
27381 | 303 |
@{lemma "zip [a,b,c] [x,y,z] = [(a,x),(b,y),(c,z)]" by simp}\\ |
304 |
@{lemma "zip [a,b] [x,y,z] = [(a,x),(b,y)]" by simp}\\ |
|
51173 | 305 |
@{lemma "enumerate 3 [a,b,c] = [(3,a),(4,b),(5,c)]" by normalization}\\ |
49948
744934b818c7
moved quite generic material from theory Enum to more appropriate places
haftmann
parents:
49808
diff
changeset
|
306 |
@{lemma "List.product [a,b] [c,d] = [(a, c), (a, d), (b, c), (b, d)]" by simp}\\ |
53721
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
traytel
parents:
53689
diff
changeset
|
307 |
@{lemma "product_lists [[a,b], [c], [d,e]] = [[a,c,d], [a,c,e], [b,c,d], [b,c,e]]" by simp}\\ |
27381 | 308 |
@{lemma "splice [a,b,c] [x,y,z] = [a,x,b,y,c,z]" by simp}\\ |
309 |
@{lemma "splice [a,b,c,d] [x,y] = [a,x,b,y,c,d]" by simp}\\ |
|
69107 | 310 |
@{lemma "shuffles [a,b] [c,d] = {[a,b,c,d],[a,c,b,d],[a,c,d,b],[c,a,b,d],[c,a,d,b],[c,d,a,b]}" |
65350
b149abe619f7
added shuffle product to HOL/List
eberlm <eberlm@in.tum.de>
parents:
64966
diff
changeset
|
311 |
by (simp add: insert_commute)}\\ |
27381 | 312 |
@{lemma "take 2 [a,b,c,d] = [a,b]" by simp}\\ |
313 |
@{lemma "take 6 [a,b,c,d] = [a,b,c,d]" by simp}\\ |
|
314 |
@{lemma "drop 2 [a,b,c,d] = [c,d]" by simp}\\ |
|
315 |
@{lemma "drop 6 [a,b,c,d] = []" by simp}\\ |
|
316 |
@{lemma "takeWhile (%n::nat. n<3) [1,2,3,0] = [1,2]" by simp}\\ |
|
317 |
@{lemma "dropWhile (%n::nat. n<3) [1,2,3,0] = [3,0]" by simp}\\ |
|
318 |
@{lemma "distinct [2,0,1::nat]" by simp}\\ |
|
319 |
@{lemma "remdups [2,0,2,1::nat,2] = [0,1,2]" by simp}\\ |
|
53721
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
traytel
parents:
53689
diff
changeset
|
320 |
@{lemma "remdups_adj [2,2,3,1,1::nat,2,1] = [2,3,1,2,1]" by simp}\\ |
34978
874150ddd50a
canonical insert operation; generalized lemma foldl_apply_inv to foldl_apply
haftmann
parents:
34942
diff
changeset
|
321 |
@{lemma "List.insert 2 [0::nat,1,2] = [0,1,2]" by (simp add: List.insert_def)}\\ |
35295 | 322 |
@{lemma "List.insert 3 [0::nat,1,2] = [3,0,1,2]" by (simp add: List.insert_def)}\\ |
57198 | 323 |
@{lemma "List.union [2,3,4] [0::int,1,2] = [4,3,0,1,2]" by (simp add: List.insert_def List.union_def)}\\ |
47122 | 324 |
@{lemma "List.find (%i::int. i>0) [0,0] = None" by simp}\\ |
325 |
@{lemma "List.find (%i::int. i>0) [0,1,0,2] = Some 1" by simp}\\ |
|
60541 | 326 |
@{lemma "count_list [0,1,0,2::int] 0 = 2" by (simp)}\\ |
55807 | 327 |
@{lemma "List.extract (%i::int. i>0) [0,0] = None" by(simp add: extract_def)}\\ |
328 |
@{lemma "List.extract (%i::int. i>0) [0,1,0,2] = Some([0], 1, [0,2])" by(simp add: extract_def)}\\ |
|
27381 | 329 |
@{lemma "remove1 2 [2,0,2,1::nat,2] = [0,2,1,2]" by simp}\\ |
27693 | 330 |
@{lemma "removeAll 2 [2,0,2,1::nat,2] = [0,1]" by simp}\\ |
27381 | 331 |
@{lemma "nth [a,b,c,d] 2 = c" by simp}\\ |
332 |
@{lemma "[a,b,c,d][2 := x] = [a,b,x,d]" by simp}\\ |
|
65956
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65350
diff
changeset
|
333 |
@{lemma "nths [a,b,c,d,e] {0,2,3} = [a,c,d]" by (simp add:nths_def)}\\ |
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65350
diff
changeset
|
334 |
@{lemma "subseqs [a,b] = [[a, b], [a], [b], []]" by simp}\\ |
49948
744934b818c7
moved quite generic material from theory Enum to more appropriate places
haftmann
parents:
49808
diff
changeset
|
335 |
@{lemma "List.n_lists 2 [a,b,c] = [[a, a], [b, a], [c, a], [a, b], [b, b], [c, b], [a, c], [b, c], [c, c]]" by (simp add: eval_nat_numeral)}\\ |
46440
d4994e2e7364
use 'primrec' to define "rotate1", for uniformity (and to help first-order tools that rely on "Spec_Rules")
blanchet
parents:
46439
diff
changeset
|
336 |
@{lemma "rotate1 [a,b,c,d] = [b,c,d,a]" by simp}\\ |
d4994e2e7364
use 'primrec' to define "rotate1", for uniformity (and to help first-order tools that rely on "Spec_Rules")
blanchet
parents:
46439
diff
changeset
|
337 |
@{lemma "rotate 3 [a,b,c,d] = [d,a,b,c]" by (simp add:rotate_def eval_nat_numeral)}\\ |
40077 | 338 |
@{lemma "replicate 4 a = [a,a,a,a]" by (simp add:eval_nat_numeral)}\\ |
67170 | 339 |
@{lemma "[2..<5] = [2,3,4]" by (simp add:eval_nat_numeral)}\\ |
340 |
@{lemma "min_list [3,1,-2::int] = -2" by (simp)}\\ |
|
341 |
@{lemma "arg_min_list (\<lambda>i. i*i) [3,-1,1,-2::int] = -1" by (simp)} |
|
26771 | 342 |
\end{tabular}} |
343 |
\caption{Characteristic examples} |
|
344 |
\label{fig:Characteristic} |
|
345 |
\end{figure} |
|
29927 | 346 |
Figure~\ref{fig:Characteristic} shows characteristic examples |
26771 | 347 |
that should give an intuitive understanding of the above functions. |
60758 | 348 |
\<close> |
349 |
||
350 |
text\<open>The following simple sort functions are intended for proofs, |
|
351 |
not for efficient implementations.\<close> |
|
24616 | 352 |
|
66434
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66358
diff
changeset
|
353 |
text \<open>A sorted predicate w.r.t. a relation:\<close> |
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66358
diff
changeset
|
354 |
|
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66358
diff
changeset
|
355 |
fun sorted_wrt :: "('a \<Rightarrow> 'a \<Rightarrow> bool) \<Rightarrow> 'a list \<Rightarrow> bool" where |
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66358
diff
changeset
|
356 |
"sorted_wrt P [] = True" | |
68109 | 357 |
"sorted_wrt P (x # ys) = ((\<forall>y \<in> set ys. P x y) \<and> sorted_wrt P ys)" |
66434
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66358
diff
changeset
|
358 |
|
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66358
diff
changeset
|
359 |
(* FIXME: define sorted in terms of sorted_wrt *) |
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66358
diff
changeset
|
360 |
|
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66358
diff
changeset
|
361 |
text \<open>A class-based sorted predicate:\<close> |
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66358
diff
changeset
|
362 |
|
25221
5ded95dda5df
append/member: more light-weight way to declare authentic syntax;
wenzelm
parents:
25215
diff
changeset
|
363 |
context linorder |
5ded95dda5df
append/member: more light-weight way to declare authentic syntax;
wenzelm
parents:
25215
diff
changeset
|
364 |
begin |
67479 | 365 |
|
366 |
fun sorted :: "'a list \<Rightarrow> bool" where |
|
367 |
"sorted [] = True" | |
|
68109 | 368 |
"sorted (x # ys) = ((\<forall>y \<in> set ys. x \<le> y) \<and> sorted ys)" |
67479 | 369 |
|
370 |
lemma sorted_sorted_wrt: "sorted = sorted_wrt (\<le>)" |
|
371 |
proof (rule ext) |
|
372 |
fix xs show "sorted xs = sorted_wrt (\<le>) xs" |
|
373 |
by(induction xs rule: sorted.induct) auto |
|
374 |
qed |
|
24697 | 375 |
|
33639
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
hoelzl
parents:
33593
diff
changeset
|
376 |
primrec insort_key :: "('b \<Rightarrow> 'a) \<Rightarrow> 'b \<Rightarrow> 'b list \<Rightarrow> 'b list" where |
50548 | 377 |
"insort_key f x [] = [x]" | |
378 |
"insort_key f x (y#ys) = |
|
379 |
(if f x \<le> f y then (x#y#ys) else y#(insort_key f x ys))" |
|
33639
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
hoelzl
parents:
33593
diff
changeset
|
380 |
|
35195 | 381 |
definition sort_key :: "('b \<Rightarrow> 'a) \<Rightarrow> 'b list \<Rightarrow> 'b list" where |
50548 | 382 |
"sort_key f xs = foldr (insort_key f) xs []" |
33639
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
hoelzl
parents:
33593
diff
changeset
|
383 |
|
40210
aee7ef725330
sorting: avoid _key suffix if lemma applies both to simple and generalized variant; generalized insort_insert to insort_insert_key; additional lemmas
haftmann
parents:
40195
diff
changeset
|
384 |
definition insort_insert_key :: "('b \<Rightarrow> 'a) \<Rightarrow> 'b \<Rightarrow> 'b list \<Rightarrow> 'b list" where |
50548 | 385 |
"insort_insert_key f x xs = |
386 |
(if f x \<in> f ` set xs then xs else insort_key f x xs)" |
|
40210
aee7ef725330
sorting: avoid _key suffix if lemma applies both to simple and generalized variant; generalized insort_insert to insort_insert_key; additional lemmas
haftmann
parents:
40195
diff
changeset
|
387 |
|
33639
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
hoelzl
parents:
33593
diff
changeset
|
388 |
abbreviation "sort \<equiv> sort_key (\<lambda>x. x)" |
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
hoelzl
parents:
33593
diff
changeset
|
389 |
abbreviation "insort \<equiv> insort_key (\<lambda>x. x)" |
40210
aee7ef725330
sorting: avoid _key suffix if lemma applies both to simple and generalized variant; generalized insort_insert to insort_insert_key; additional lemmas
haftmann
parents:
40195
diff
changeset
|
390 |
abbreviation "insort_insert \<equiv> insort_insert_key (\<lambda>x. x)" |
35608 | 391 |
|
67684 | 392 |
definition stable_sort_key :: "(('b \<Rightarrow> 'a) \<Rightarrow> 'b list \<Rightarrow> 'b list) \<Rightarrow> bool" where |
393 |
"stable_sort_key sk = |
|
394 |
(\<forall>f xs k. filter (\<lambda>y. f y = k) (sk f xs) = filter (\<lambda>y. f y = k) xs)" |
|
395 |
||
25221
5ded95dda5df
append/member: more light-weight way to declare authentic syntax;
wenzelm
parents:
25215
diff
changeset
|
396 |
end |
5ded95dda5df
append/member: more light-weight way to declare authentic syntax;
wenzelm
parents:
25215
diff
changeset
|
397 |
|
24616 | 398 |
|
60758 | 399 |
subsubsection \<open>List comprehension\<close> |
400 |
||
401 |
text\<open>Input syntax for Haskell-like list comprehension notation. |
|
61799 | 402 |
Typical example: \<open>[(x,y). x \<leftarrow> xs, y \<leftarrow> ys, x \<noteq> y]\<close>, |
403 |
the list of all pairs of distinct elements from \<open>xs\<close> and \<open>ys\<close>. |
|
404 |
The syntax is as in Haskell, except that \<open>|\<close> becomes a dot |
|
405 |
(like in Isabelle's set comprehension): \<open>[e. x \<leftarrow> xs, \<dots>]\<close> rather than |
|
24349 | 406 |
\verb![e| x <- xs, ...]!. |
407 |
||
408 |
The qualifiers after the dot are |
|
409 |
\begin{description} |
|
61799 | 410 |
\item[generators] \<open>p \<leftarrow> xs\<close>, |
411 |
where \<open>p\<close> is a pattern and \<open>xs\<close> an expression of list type, or |
|
412 |
\item[guards] \<open>b\<close>, where \<open>b\<close> is a boolean expression. |
|
24476
f7ad9fbbeeaa
turned list comprehension translations into ML to optimize base case
nipkow
parents:
24471
diff
changeset
|
413 |
%\item[local bindings] @ {text"let x = e"}. |
24349 | 414 |
\end{description} |
23240 | 415 |
|
24476
f7ad9fbbeeaa
turned list comprehension translations into ML to optimize base case
nipkow
parents:
24471
diff
changeset
|
416 |
Just like in Haskell, list comprehension is just a shorthand. To avoid |
f7ad9fbbeeaa
turned list comprehension translations into ML to optimize base case
nipkow
parents:
24471
diff
changeset
|
417 |
misunderstandings, the translation into desugared form is not reversed |
61799 | 418 |
upon output. Note that the translation of \<open>[e. x \<leftarrow> xs]\<close> is |
69593 | 419 |
optmized to \<^term>\<open>map (%x. e) xs\<close>. |
23240 | 420 |
|
24349 | 421 |
It is easy to write short list comprehensions which stand for complex |
422 |
expressions. During proofs, they may become unreadable (and |
|
423 |
mangled). In such cases it can be advisable to introduce separate |
|
60758 | 424 |
definitions for the list comprehensions in question.\<close> |
24349 | 425 |
|
46138
85f8d8a8c711
improved list comprehension syntax: more careful treatment of position constraints, which enables PIDE markup;
wenzelm
parents:
46133
diff
changeset
|
426 |
nonterminal lc_qual and lc_quals |
23192 | 427 |
|
428 |
syntax |
|
46138
85f8d8a8c711
improved list comprehension syntax: more careful treatment of position constraints, which enables PIDE markup;
wenzelm
parents:
46133
diff
changeset
|
429 |
"_listcompr" :: "'a \<Rightarrow> lc_qual \<Rightarrow> lc_quals \<Rightarrow> 'a list" ("[_ . __") |
61955
e96292f32c3c
former "xsymbols" syntax is used by default, and ASCII replacement syntax with print mode "ASCII";
wenzelm
parents:
61941
diff
changeset
|
430 |
"_lc_gen" :: "'a \<Rightarrow> 'a list \<Rightarrow> lc_qual" ("_ \<leftarrow> _") |
46138
85f8d8a8c711
improved list comprehension syntax: more careful treatment of position constraints, which enables PIDE markup;
wenzelm
parents:
46133
diff
changeset
|
431 |
"_lc_test" :: "bool \<Rightarrow> lc_qual" ("_") |
85f8d8a8c711
improved list comprehension syntax: more careful treatment of position constraints, which enables PIDE markup;
wenzelm
parents:
46133
diff
changeset
|
432 |
(*"_lc_let" :: "letbinds => lc_qual" ("let _")*) |
85f8d8a8c711
improved list comprehension syntax: more careful treatment of position constraints, which enables PIDE markup;
wenzelm
parents:
46133
diff
changeset
|
433 |
"_lc_end" :: "lc_quals" ("]") |
85f8d8a8c711
improved list comprehension syntax: more careful treatment of position constraints, which enables PIDE markup;
wenzelm
parents:
46133
diff
changeset
|
434 |
"_lc_quals" :: "lc_qual \<Rightarrow> lc_quals \<Rightarrow> lc_quals" (", __") |
23192 | 435 |
|
61955
e96292f32c3c
former "xsymbols" syntax is used by default, and ASCII replacement syntax with print mode "ASCII";
wenzelm
parents:
61941
diff
changeset
|
436 |
syntax (ASCII) |
e96292f32c3c
former "xsymbols" syntax is used by default, and ASCII replacement syntax with print mode "ASCII";
wenzelm
parents:
61941
diff
changeset
|
437 |
"_lc_gen" :: "'a \<Rightarrow> 'a list \<Rightarrow> lc_qual" ("_ <- _") |
e96292f32c3c
former "xsymbols" syntax is used by default, and ASCII replacement syntax with print mode "ASCII";
wenzelm
parents:
61941
diff
changeset
|
438 |
|
60758 | 439 |
parse_translation \<open> |
68362 | 440 |
let |
69593 | 441 |
val NilC = Syntax.const \<^const_syntax>\<open>Nil\<close>; |
442 |
val ConsC = Syntax.const \<^const_syntax>\<open>Cons\<close>; |
|
443 |
val mapC = Syntax.const \<^const_syntax>\<open>map\<close>; |
|
444 |
val concatC = Syntax.const \<^const_syntax>\<open>concat\<close>; |
|
445 |
val IfC = Syntax.const \<^const_syntax>\<open>If\<close>; |
|
446 |
val dummyC = Syntax.const \<^const_syntax>\<open>Pure.dummy_pattern\<close> |
|
68362 | 447 |
|
448 |
fun single x = ConsC $ x $ NilC; |
|
449 |
||
450 |
fun pat_tr ctxt p e opti = (* %x. case x of p => e | _ => [] *) |
|
451 |
let |
|
452 |
(* FIXME proper name context!? *) |
|
453 |
val x = |
|
454 |
Free (singleton (Name.variant_list (fold Term.add_free_names [p, e] [])) "x", dummyT); |
|
455 |
val e = if opti then single e else e; |
|
69593 | 456 |
val case1 = Syntax.const \<^syntax_const>\<open>_case1\<close> $ p $ e; |
68362 | 457 |
val case2 = |
69593 | 458 |
Syntax.const \<^syntax_const>\<open>_case1\<close> $ dummyC $ NilC; |
459 |
val cs = Syntax.const \<^syntax_const>\<open>_case2\<close> $ case1 $ case2; |
|
68362 | 460 |
in Syntax_Trans.abs_tr [x, Case_Translation.case_tr false ctxt [x, cs]] end; |
461 |
||
462 |
fun pair_pat_tr (x as Free _) e = Syntax_Trans.abs_tr [x, e] |
|
463 |
| pair_pat_tr (_ $ p1 $ p2) e = |
|
69593 | 464 |
Syntax.const \<^const_syntax>\<open>case_prod\<close> $ pair_pat_tr p1 (pair_pat_tr p2 e) |
68362 | 465 |
| pair_pat_tr dummy e = Syntax_Trans.abs_tr [Syntax.const "_idtdummy", e] |
466 |
||
69593 | 467 |
fun pair_pat ctxt (Const (\<^const_syntax>\<open>Pair\<close>,_) $ s $ t) = |
68362 | 468 |
pair_pat ctxt s andalso pair_pat ctxt t |
469 |
| pair_pat ctxt (Free (s,_)) = |
|
470 |
let |
|
471 |
val thy = Proof_Context.theory_of ctxt; |
|
472 |
val s' = Proof_Context.intern_const ctxt s; |
|
473 |
in not (Sign.declared_const thy s') end |
|
474 |
| pair_pat _ t = (t = dummyC); |
|
475 |
||
476 |
fun abs_tr ctxt p e opti = |
|
477 |
let val p = Term_Position.strip_positions p |
|
478 |
in if pair_pat ctxt p |
|
479 |
then (pair_pat_tr p e, true) |
|
480 |
else (pat_tr ctxt p e opti, false) |
|
481 |
end |
|
482 |
||
69593 | 483 |
fun lc_tr ctxt [e, Const (\<^syntax_const>\<open>_lc_test\<close>, _) $ b, qs] = |
68362 | 484 |
let |
485 |
val res = |
|
486 |
(case qs of |
|
69593 | 487 |
Const (\<^syntax_const>\<open>_lc_end\<close>, _) => single e |
488 |
| Const (\<^syntax_const>\<open>_lc_quals\<close>, _) $ q $ qs => lc_tr ctxt [e, q, qs]); |
|
68362 | 489 |
in IfC $ b $ res $ NilC end |
490 |
| lc_tr ctxt |
|
69593 | 491 |
[e, Const (\<^syntax_const>\<open>_lc_gen\<close>, _) $ p $ es, |
492 |
Const(\<^syntax_const>\<open>_lc_end\<close>, _)] = |
|
68362 | 493 |
(case abs_tr ctxt p e true of |
494 |
(f, true) => mapC $ f $ es |
|
495 |
| (f, false) => concatC $ (mapC $ f $ es)) |
|
496 |
| lc_tr ctxt |
|
69593 | 497 |
[e, Const (\<^syntax_const>\<open>_lc_gen\<close>, _) $ p $ es, |
498 |
Const (\<^syntax_const>\<open>_lc_quals\<close>, _) $ q $ qs] = |
|
68362 | 499 |
let val e' = lc_tr ctxt [e, q, qs]; |
500 |
in concatC $ (mapC $ (fst (abs_tr ctxt p e' false)) $ es) end; |
|
501 |
||
69593 | 502 |
in [(\<^syntax_const>\<open>_listcompr\<close>, lc_tr)] end |
60758 | 503 |
\<close> |
504 |
||
505 |
ML_val \<open> |
|
42167 | 506 |
let |
69593 | 507 |
val read = Syntax.read_term \<^context> o Syntax.implode_input; |
60160 | 508 |
fun check s1 s2 = |
509 |
read s1 aconv read s2 orelse |
|
510 |
error ("Check failed: " ^ |
|
69349
7cef9e386ffe
more accurate positions for "name" (quoted string) and "embedded" (cartouche): refer to content without delimiters, which is e.g. relevant for systematic selection/renaming of scope groups;
wenzelm
parents:
69312
diff
changeset
|
511 |
quote (#1 (Input.source_content s1)) ^ Position.here_list [Input.pos_of s1, Input.pos_of s2]); |
42167 | 512 |
in |
60160 | 513 |
check \<open>[(x,y,z). b]\<close> \<open>if b then [(x, y, z)] else []\<close>; |
68362 | 514 |
check \<open>[(x,y,z). (x,_,y)\<leftarrow>xs]\<close> \<open>map (\<lambda>(x,_,y). (x, y, z)) xs\<close>; |
515 |
check \<open>[e x y. (x,_)\<leftarrow>xs, y\<leftarrow>ys]\<close> \<open>concat (map (\<lambda>(x,_). map (\<lambda>y. e x y) ys) xs)\<close>; |
|
60160 | 516 |
check \<open>[(x,y,z). x<a, x>b]\<close> \<open>if x < a then if b < x then [(x, y, z)] else [] else []\<close>; |
517 |
check \<open>[(x,y,z). x\<leftarrow>xs, x>b]\<close> \<open>concat (map (\<lambda>x. if b < x then [(x, y, z)] else []) xs)\<close>; |
|
518 |
check \<open>[(x,y,z). x<a, x\<leftarrow>xs]\<close> \<open>if x < a then map (\<lambda>x. (x, y, z)) xs else []\<close>; |
|
519 |
check \<open>[(x,y). Cons True x \<leftarrow> xs]\<close> |
|
520 |
\<open>concat (map (\<lambda>xa. case xa of [] \<Rightarrow> [] | True # x \<Rightarrow> [(x, y)] | False # x \<Rightarrow> []) xs)\<close>; |
|
521 |
check \<open>[(x,y,z). Cons x [] \<leftarrow> xs]\<close> |
|
522 |
\<open>concat (map (\<lambda>xa. case xa of [] \<Rightarrow> [] | [x] \<Rightarrow> [(x, y, z)] | x # aa # lista \<Rightarrow> []) xs)\<close>; |
|
523 |
check \<open>[(x,y,z). x<a, x>b, x=d]\<close> |
|
524 |
\<open>if x < a then if b < x then if x = d then [(x, y, z)] else [] else [] else []\<close>; |
|
525 |
check \<open>[(x,y,z). x<a, x>b, y\<leftarrow>ys]\<close> |
|
526 |
\<open>if x < a then if b < x then map (\<lambda>y. (x, y, z)) ys else [] else []\<close>; |
|
68362 | 527 |
check \<open>[(x,y,z). x<a, (_,x)\<leftarrow>xs,y>b]\<close> |
528 |
\<open>if x < a then concat (map (\<lambda>(_,x). if b < y then [(x, y, z)] else []) xs) else []\<close>; |
|
60160 | 529 |
check \<open>[(x,y,z). x<a, x\<leftarrow>xs, y\<leftarrow>ys]\<close> |
530 |
\<open>if x < a then concat (map (\<lambda>x. map (\<lambda>y. (x, y, z)) ys) xs) else []\<close>; |
|
531 |
check \<open>[(x,y,z). x\<leftarrow>xs, x>b, y<a]\<close> |
|
532 |
\<open>concat (map (\<lambda>x. if b < x then if y < a then [(x, y, z)] else [] else []) xs)\<close>; |
|
533 |
check \<open>[(x,y,z). x\<leftarrow>xs, x>b, y\<leftarrow>ys]\<close> |
|
534 |
\<open>concat (map (\<lambda>x. if b < x then map (\<lambda>y. (x, y, z)) ys else []) xs)\<close>; |
|
68362 | 535 |
check \<open>[(x,y,z). x\<leftarrow>xs, (y,_)\<leftarrow>ys,y>x]\<close> |
536 |
\<open>concat (map (\<lambda>x. concat (map (\<lambda>(y,_). if x < y then [(x, y, z)] else []) ys)) xs)\<close>; |
|
60160 | 537 |
check \<open>[(x,y,z). x\<leftarrow>xs, y\<leftarrow>ys,z\<leftarrow>zs]\<close> |
538 |
\<open>concat (map (\<lambda>x. concat (map (\<lambda>y. map (\<lambda>z. (x, y, z)) zs) ys)) xs)\<close> |
|
42167 | 539 |
end; |
60758 | 540 |
\<close> |
42167 | 541 |
|
60758 | 542 |
ML \<open> |
50422
ee729dbd1b7f
avoid ML_file in large theory files to improve performance of dependency discovery of main HOL (approx. 1s CPU time) -- relevant for any application using it, e.g. small paper sessions;
wenzelm
parents:
50134
diff
changeset
|
543 |
(* Simproc for rewriting list comprehensions applied to List.set to set |
ee729dbd1b7f
avoid ML_file in large theory files to improve performance of dependency discovery of main HOL (approx. 1s CPU time) -- relevant for any application using it, e.g. small paper sessions;
wenzelm
parents:
50134
diff
changeset
|
544 |
comprehension. *) |
ee729dbd1b7f
avoid ML_file in large theory files to improve performance of dependency discovery of main HOL (approx. 1s CPU time) -- relevant for any application using it, e.g. small paper sessions;
wenzelm
parents:
50134
diff
changeset
|
545 |
|
ee729dbd1b7f
avoid ML_file in large theory files to improve performance of dependency discovery of main HOL (approx. 1s CPU time) -- relevant for any application using it, e.g. small paper sessions;
wenzelm
parents:
50134
diff
changeset
|
546 |
signature LIST_TO_SET_COMPREHENSION = |
ee729dbd1b7f
avoid ML_file in large theory files to improve performance of dependency discovery of main HOL (approx. 1s CPU time) -- relevant for any application using it, e.g. small paper sessions;
wenzelm
parents:
50134
diff
changeset
|
547 |
sig |
51717
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents:
51678
diff
changeset
|
548 |
val simproc : Proof.context -> cterm -> thm option |
50422
ee729dbd1b7f
avoid ML_file in large theory files to improve performance of dependency discovery of main HOL (approx. 1s CPU time) -- relevant for any application using it, e.g. small paper sessions;
wenzelm
parents:
50134
diff
changeset
|
549 |
end |
ee729dbd1b7f
avoid ML_file in large theory files to improve performance of dependency discovery of main HOL (approx. 1s CPU time) -- relevant for any application using it, e.g. small paper sessions;
wenzelm
parents:
50134
diff
changeset
|
550 |
|
ee729dbd1b7f
avoid ML_file in large theory files to improve performance of dependency discovery of main HOL (approx. 1s CPU time) -- relevant for any application using it, e.g. small paper sessions;
wenzelm
parents:
50134
diff
changeset
|
551 |
structure List_to_Set_Comprehension : LIST_TO_SET_COMPREHENSION = |
ee729dbd1b7f
avoid ML_file in large theory files to improve performance of dependency discovery of main HOL (approx. 1s CPU time) -- relevant for any application using it, e.g. small paper sessions;
wenzelm
parents:
50134
diff
changeset
|
552 |
struct |
ee729dbd1b7f
avoid ML_file in large theory files to improve performance of dependency discovery of main HOL (approx. 1s CPU time) -- relevant for any application using it, e.g. small paper sessions;
wenzelm
parents:
50134
diff
changeset
|
553 |
|
ee729dbd1b7f
avoid ML_file in large theory files to improve performance of dependency discovery of main HOL (approx. 1s CPU time) -- relevant for any application using it, e.g. small paper sessions;
wenzelm
parents:
50134
diff
changeset
|
554 |
(* conversion *) |
ee729dbd1b7f
avoid ML_file in large theory files to improve performance of dependency discovery of main HOL (approx. 1s CPU time) -- relevant for any application using it, e.g. small paper sessions;
wenzelm
parents:
50134
diff
changeset
|
555 |
|
ee729dbd1b7f
avoid ML_file in large theory files to improve performance of dependency discovery of main HOL (approx. 1s CPU time) -- relevant for any application using it, e.g. small paper sessions;
wenzelm
parents:
50134
diff
changeset
|
556 |
fun all_exists_conv cv ctxt ct = |
ee729dbd1b7f
avoid ML_file in large theory files to improve performance of dependency discovery of main HOL (approx. 1s CPU time) -- relevant for any application using it, e.g. small paper sessions;
wenzelm
parents:
50134
diff
changeset
|
557 |
(case Thm.term_of ct of |
69593 | 558 |
Const (\<^const_name>\<open>Ex\<close>, _) $ Abs _ => |
50422
ee729dbd1b7f
avoid ML_file in large theory files to improve performance of dependency discovery of main HOL (approx. 1s CPU time) -- relevant for any application using it, e.g. small paper sessions;
wenzelm
parents:
50134
diff
changeset
|
559 |
Conv.arg_conv (Conv.abs_conv (all_exists_conv cv o #2) ctxt) ct |
ee729dbd1b7f
avoid ML_file in large theory files to improve performance of dependency discovery of main HOL (approx. 1s CPU time) -- relevant for any application using it, e.g. small paper sessions;
wenzelm
parents:
50134
diff
changeset
|
560 |
| _ => cv ctxt ct) |
ee729dbd1b7f
avoid ML_file in large theory files to improve performance of dependency discovery of main HOL (approx. 1s CPU time) -- relevant for any application using it, e.g. small paper sessions;
wenzelm
parents:
50134
diff
changeset
|
561 |
|
ee729dbd1b7f
avoid ML_file in large theory files to improve performance of dependency discovery of main HOL (approx. 1s CPU time) -- relevant for any application using it, e.g. small paper sessions;
wenzelm
parents:
50134
diff
changeset
|
562 |
fun all_but_last_exists_conv cv ctxt ct = |
ee729dbd1b7f
avoid ML_file in large theory files to improve performance of dependency discovery of main HOL (approx. 1s CPU time) -- relevant for any application using it, e.g. small paper sessions;
wenzelm
parents:
50134
diff
changeset
|
563 |
(case Thm.term_of ct of |
69593 | 564 |
Const (\<^const_name>\<open>Ex\<close>, _) $ Abs (_, _, Const (\<^const_name>\<open>Ex\<close>, _) $ _) => |
50422
ee729dbd1b7f
avoid ML_file in large theory files to improve performance of dependency discovery of main HOL (approx. 1s CPU time) -- relevant for any application using it, e.g. small paper sessions;
wenzelm
parents:
50134
diff
changeset
|
565 |
Conv.arg_conv (Conv.abs_conv (all_but_last_exists_conv cv o #2) ctxt) ct |
ee729dbd1b7f
avoid ML_file in large theory files to improve performance of dependency discovery of main HOL (approx. 1s CPU time) -- relevant for any application using it, e.g. small paper sessions;
wenzelm
parents:
50134
diff
changeset
|
566 |
| _ => cv ctxt ct) |
ee729dbd1b7f
avoid ML_file in large theory files to improve performance of dependency discovery of main HOL (approx. 1s CPU time) -- relevant for any application using it, e.g. small paper sessions;
wenzelm
parents:
50134
diff
changeset
|
567 |
|
ee729dbd1b7f
avoid ML_file in large theory files to improve performance of dependency discovery of main HOL (approx. 1s CPU time) -- relevant for any application using it, e.g. small paper sessions;
wenzelm
parents:
50134
diff
changeset
|
568 |
fun Collect_conv cv ctxt ct = |
ee729dbd1b7f
avoid ML_file in large theory files to improve performance of dependency discovery of main HOL (approx. 1s CPU time) -- relevant for any application using it, e.g. small paper sessions;
wenzelm
parents:
50134
diff
changeset
|
569 |
(case Thm.term_of ct of |
69593 | 570 |
Const (\<^const_name>\<open>Collect\<close>, _) $ Abs _ => Conv.arg_conv (Conv.abs_conv cv ctxt) ct |
50422
ee729dbd1b7f
avoid ML_file in large theory files to improve performance of dependency discovery of main HOL (approx. 1s CPU time) -- relevant for any application using it, e.g. small paper sessions;
wenzelm
parents:
50134
diff
changeset
|
571 |
| _ => raise CTERM ("Collect_conv", [ct])) |
ee729dbd1b7f
avoid ML_file in large theory files to improve performance of dependency discovery of main HOL (approx. 1s CPU time) -- relevant for any application using it, e.g. small paper sessions;
wenzelm
parents:
50134
diff
changeset
|
572 |
|
ee729dbd1b7f
avoid ML_file in large theory files to improve performance of dependency discovery of main HOL (approx. 1s CPU time) -- relevant for any application using it, e.g. small paper sessions;
wenzelm
parents:
50134
diff
changeset
|
573 |
fun rewr_conv' th = Conv.rewr_conv (mk_meta_eq th) |
ee729dbd1b7f
avoid ML_file in large theory files to improve performance of dependency discovery of main HOL (approx. 1s CPU time) -- relevant for any application using it, e.g. small paper sessions;
wenzelm
parents:
50134
diff
changeset
|
574 |
|
ee729dbd1b7f
avoid ML_file in large theory files to improve performance of dependency discovery of main HOL (approx. 1s CPU time) -- relevant for any application using it, e.g. small paper sessions;
wenzelm
parents:
50134
diff
changeset
|
575 |
fun conjunct_assoc_conv ct = |
ee729dbd1b7f
avoid ML_file in large theory files to improve performance of dependency discovery of main HOL (approx. 1s CPU time) -- relevant for any application using it, e.g. small paper sessions;
wenzelm
parents:
50134
diff
changeset
|
576 |
Conv.try_conv |
51315
536a5603a138
provide common HOLogic.conj_conv and HOLogic.eq_conv;
wenzelm
parents:
51314
diff
changeset
|
577 |
(rewr_conv' @{thm conj_assoc} then_conv HOLogic.conj_conv Conv.all_conv conjunct_assoc_conv) ct |
50422
ee729dbd1b7f
avoid ML_file in large theory files to improve performance of dependency discovery of main HOL (approx. 1s CPU time) -- relevant for any application using it, e.g. small paper sessions;
wenzelm
parents:
50134
diff
changeset
|
578 |
|
ee729dbd1b7f
avoid ML_file in large theory files to improve performance of dependency discovery of main HOL (approx. 1s CPU time) -- relevant for any application using it, e.g. small paper sessions;
wenzelm
parents:
50134
diff
changeset
|
579 |
fun right_hand_set_comprehension_conv conv ctxt = |
51315
536a5603a138
provide common HOLogic.conj_conv and HOLogic.eq_conv;
wenzelm
parents:
51314
diff
changeset
|
580 |
HOLogic.Trueprop_conv (HOLogic.eq_conv Conv.all_conv |
50422
ee729dbd1b7f
avoid ML_file in large theory files to improve performance of dependency discovery of main HOL (approx. 1s CPU time) -- relevant for any application using it, e.g. small paper sessions;
wenzelm
parents:
50134
diff
changeset
|
581 |
(Collect_conv (all_exists_conv conv o #2) ctxt)) |
ee729dbd1b7f
avoid ML_file in large theory files to improve performance of dependency discovery of main HOL (approx. 1s CPU time) -- relevant for any application using it, e.g. small paper sessions;
wenzelm
parents:
50134
diff
changeset
|
582 |
|
ee729dbd1b7f
avoid ML_file in large theory files to improve performance of dependency discovery of main HOL (approx. 1s CPU time) -- relevant for any application using it, e.g. small paper sessions;
wenzelm
parents:
50134
diff
changeset
|
583 |
|
ee729dbd1b7f
avoid ML_file in large theory files to improve performance of dependency discovery of main HOL (approx. 1s CPU time) -- relevant for any application using it, e.g. small paper sessions;
wenzelm
parents:
50134
diff
changeset
|
584 |
(* term abstraction of list comprehension patterns *) |
ee729dbd1b7f
avoid ML_file in large theory files to improve performance of dependency discovery of main HOL (approx. 1s CPU time) -- relevant for any application using it, e.g. small paper sessions;
wenzelm
parents:
50134
diff
changeset
|
585 |
|
60156 | 586 |
datatype termlets = If | Case of typ * int |
50422
ee729dbd1b7f
avoid ML_file in large theory files to improve performance of dependency discovery of main HOL (approx. 1s CPU time) -- relevant for any application using it, e.g. small paper sessions;
wenzelm
parents:
50134
diff
changeset
|
587 |
|
60158 | 588 |
local |
589 |
||
590 |
val set_Nil_I = @{lemma "set [] = {x. False}" by (simp add: empty_def [symmetric])} |
|
591 |
val set_singleton = @{lemma "set [a] = {x. x = a}" by simp} |
|
592 |
val inst_Collect_mem_eq = @{lemma "set A = {x. x \<in> set A}" by simp} |
|
593 |
val del_refl_eq = @{lemma "(t = t \<and> P) \<equiv> P" by simp} |
|
594 |
||
69593 | 595 |
fun mk_set T = Const (\<^const_name>\<open>set\<close>, HOLogic.listT T --> HOLogic.mk_setT T) |
596 |
fun dest_set (Const (\<^const_name>\<open>set\<close>, _) $ xs) = xs |
|
597 |
||
598 |
fun dest_singleton_list (Const (\<^const_name>\<open>Cons\<close>, _) $ t $ (Const (\<^const_name>\<open>Nil\<close>, _))) = t |
|
60158 | 599 |
| dest_singleton_list t = raise TERM ("dest_singleton_list", [t]) |
600 |
||
601 |
(*We check that one case returns a singleton list and all other cases |
|
602 |
return [], and return the index of the one singleton list case.*) |
|
603 |
fun possible_index_of_singleton_case cases = |
|
50422
ee729dbd1b7f
avoid ML_file in large theory files to improve performance of dependency discovery of main HOL (approx. 1s CPU time) -- relevant for any application using it, e.g. small paper sessions;
wenzelm
parents:
50134
diff
changeset
|
604 |
let |
60158 | 605 |
fun check (i, case_t) s = |
606 |
(case strip_abs_body case_t of |
|
69593 | 607 |
(Const (\<^const_name>\<open>Nil\<close>, _)) => s |
60158 | 608 |
| _ => (case s of SOME NONE => SOME (SOME i) | _ => NONE)) |
609 |
in |
|
610 |
fold_index check cases (SOME NONE) |> the_default NONE |
|
611 |
end |
|
612 |
||
613 |
(*returns condition continuing term option*) |
|
69593 | 614 |
fun dest_if (Const (\<^const_name>\<open>If\<close>, _) $ cond $ then_t $ Const (\<^const_name>\<open>Nil\<close>, _)) = |
60158 | 615 |
SOME (cond, then_t) |
616 |
| dest_if _ = NONE |
|
617 |
||
618 |
(*returns (case_expr type index chosen_case constr_name) option*) |
|
619 |
fun dest_case ctxt case_term = |
|
620 |
let |
|
621 |
val (case_const, args) = strip_comb case_term |
|
622 |
in |
|
623 |
(case try dest_Const case_const of |
|
624 |
SOME (c, T) => |
|
625 |
(case Ctr_Sugar.ctr_sugar_of_case ctxt c of |
|
626 |
SOME {ctrs, ...} => |
|
627 |
(case possible_index_of_singleton_case (fst (split_last args)) of |
|
628 |
SOME i => |
|
629 |
let |
|
630 |
val constr_names = map (fst o dest_Const) ctrs |
|
631 |
val (Ts, _) = strip_type T |
|
632 |
val T' = List.last Ts |
|
633 |
in SOME (List.last args, T', i, nth args i, nth constr_names i) end |
|
50422
ee729dbd1b7f
avoid ML_file in large theory files to improve performance of dependency discovery of main HOL (approx. 1s CPU time) -- relevant for any application using it, e.g. small paper sessions;
wenzelm
parents:
50134
diff
changeset
|
634 |
| NONE => NONE) |
ee729dbd1b7f
avoid ML_file in large theory files to improve performance of dependency discovery of main HOL (approx. 1s CPU time) -- relevant for any application using it, e.g. small paper sessions;
wenzelm
parents:
50134
diff
changeset
|
635 |
| NONE => NONE) |
60158 | 636 |
| NONE => NONE) |
637 |
end |
|
638 |
||
60752 | 639 |
fun tac ctxt [] = |
640 |
resolve_tac ctxt [set_singleton] 1 ORELSE |
|
641 |
resolve_tac ctxt [inst_Collect_mem_eq] 1 |
|
60158 | 642 |
| tac ctxt (If :: cont) = |
62390 | 643 |
Splitter.split_tac ctxt @{thms if_split} 1 |
60752 | 644 |
THEN resolve_tac ctxt @{thms conjI} 1 |
645 |
THEN resolve_tac ctxt @{thms impI} 1 |
|
60159
879918f4ee0f
tuned -- avoid odd rebinding of "ctxt" and "context";
wenzelm
parents:
60158
diff
changeset
|
646 |
THEN Subgoal.FOCUS (fn {prems, context = ctxt', ...} => |
60158 | 647 |
CONVERSION (right_hand_set_comprehension_conv (K |
648 |
(HOLogic.conj_conv (Conv.rewr_conv (List.last prems RS @{thm Eq_TrueI})) Conv.all_conv |
|
649 |
then_conv |
|
60159
879918f4ee0f
tuned -- avoid odd rebinding of "ctxt" and "context";
wenzelm
parents:
60158
diff
changeset
|
650 |
rewr_conv' @{lemma "(True \<and> P) = P" by simp})) ctxt') 1) ctxt 1 |
60158 | 651 |
THEN tac ctxt cont |
60752 | 652 |
THEN resolve_tac ctxt @{thms impI} 1 |
60159
879918f4ee0f
tuned -- avoid odd rebinding of "ctxt" and "context";
wenzelm
parents:
60158
diff
changeset
|
653 |
THEN Subgoal.FOCUS (fn {prems, context = ctxt', ...} => |
60158 | 654 |
CONVERSION (right_hand_set_comprehension_conv (K |
655 |
(HOLogic.conj_conv (Conv.rewr_conv (List.last prems RS @{thm Eq_FalseI})) Conv.all_conv |
|
60159
879918f4ee0f
tuned -- avoid odd rebinding of "ctxt" and "context";
wenzelm
parents:
60158
diff
changeset
|
656 |
then_conv rewr_conv' @{lemma "(False \<and> P) = False" by simp})) ctxt') 1) ctxt 1 |
60752 | 657 |
THEN resolve_tac ctxt [set_Nil_I] 1 |
60158 | 658 |
| tac ctxt (Case (T, i) :: cont) = |
659 |
let |
|
660 |
val SOME {injects, distincts, case_thms, split, ...} = |
|
661 |
Ctr_Sugar.ctr_sugar_of ctxt (fst (dest_Type T)) |
|
662 |
in |
|
663 |
(* do case distinction *) |
|
664 |
Splitter.split_tac ctxt [split] 1 |
|
665 |
THEN EVERY (map_index (fn (i', _) => |
|
60752 | 666 |
(if i' < length case_thms - 1 then resolve_tac ctxt @{thms conjI} 1 else all_tac) |
667 |
THEN REPEAT_DETERM (resolve_tac ctxt @{thms allI} 1) |
|
668 |
THEN resolve_tac ctxt @{thms impI} 1 |
|
60158 | 669 |
THEN (if i' = i then |
670 |
(* continue recursively *) |
|
60159
879918f4ee0f
tuned -- avoid odd rebinding of "ctxt" and "context";
wenzelm
parents:
60158
diff
changeset
|
671 |
Subgoal.FOCUS (fn {prems, context = ctxt', ...} => |
60158 | 672 |
CONVERSION (Thm.eta_conversion then_conv right_hand_set_comprehension_conv (K |
673 |
((HOLogic.conj_conv |
|
674 |
(HOLogic.eq_conv Conv.all_conv (rewr_conv' (List.last prems)) then_conv |
|
675 |
(Conv.try_conv (Conv.rewrs_conv (map mk_meta_eq injects)))) |
|
676 |
Conv.all_conv) |
|
677 |
then_conv (Conv.try_conv (Conv.rewr_conv del_refl_eq)) |
|
60159
879918f4ee0f
tuned -- avoid odd rebinding of "ctxt" and "context";
wenzelm
parents:
60158
diff
changeset
|
678 |
then_conv conjunct_assoc_conv)) ctxt' |
879918f4ee0f
tuned -- avoid odd rebinding of "ctxt" and "context";
wenzelm
parents:
60158
diff
changeset
|
679 |
then_conv |
879918f4ee0f
tuned -- avoid odd rebinding of "ctxt" and "context";
wenzelm
parents:
60158
diff
changeset
|
680 |
(HOLogic.Trueprop_conv |
879918f4ee0f
tuned -- avoid odd rebinding of "ctxt" and "context";
wenzelm
parents:
60158
diff
changeset
|
681 |
(HOLogic.eq_conv Conv.all_conv (Collect_conv (fn (_, ctxt'') => |
879918f4ee0f
tuned -- avoid odd rebinding of "ctxt" and "context";
wenzelm
parents:
60158
diff
changeset
|
682 |
Conv.repeat_conv |
879918f4ee0f
tuned -- avoid odd rebinding of "ctxt" and "context";
wenzelm
parents:
60158
diff
changeset
|
683 |
(all_but_last_exists_conv |
879918f4ee0f
tuned -- avoid odd rebinding of "ctxt" and "context";
wenzelm
parents:
60158
diff
changeset
|
684 |
(K (rewr_conv' |
879918f4ee0f
tuned -- avoid odd rebinding of "ctxt" and "context";
wenzelm
parents:
60158
diff
changeset
|
685 |
@{lemma "(\<exists>x. x = t \<and> P x) = P t" by simp})) ctxt'')) ctxt')))) 1) ctxt 1 |
60158 | 686 |
THEN tac ctxt cont |
687 |
else |
|
60159
879918f4ee0f
tuned -- avoid odd rebinding of "ctxt" and "context";
wenzelm
parents:
60158
diff
changeset
|
688 |
Subgoal.FOCUS (fn {prems, context = ctxt', ...} => |
60158 | 689 |
CONVERSION |
690 |
(right_hand_set_comprehension_conv (K |
|
691 |
(HOLogic.conj_conv |
|
692 |
((HOLogic.eq_conv Conv.all_conv |
|
693 |
(rewr_conv' (List.last prems))) then_conv |
|
694 |
(Conv.rewrs_conv (map (fn th => th RS @{thm Eq_FalseI}) distincts))) |
|
695 |
Conv.all_conv then_conv |
|
60159
879918f4ee0f
tuned -- avoid odd rebinding of "ctxt" and "context";
wenzelm
parents:
60158
diff
changeset
|
696 |
(rewr_conv' @{lemma "(False \<and> P) = False" by simp}))) ctxt' then_conv |
60158 | 697 |
HOLogic.Trueprop_conv |
698 |
(HOLogic.eq_conv Conv.all_conv |
|
60159
879918f4ee0f
tuned -- avoid odd rebinding of "ctxt" and "context";
wenzelm
parents:
60158
diff
changeset
|
699 |
(Collect_conv (fn (_, ctxt'') => |
60158 | 700 |
Conv.repeat_conv |
701 |
(Conv.bottom_conv |
|
60159
879918f4ee0f
tuned -- avoid odd rebinding of "ctxt" and "context";
wenzelm
parents:
60158
diff
changeset
|
702 |
(K (rewr_conv' @{lemma "(\<exists>x. P) = P" by simp})) ctxt'')) ctxt'))) 1) ctxt 1 |
60752 | 703 |
THEN resolve_tac ctxt [set_Nil_I] 1)) case_thms) |
60158 | 704 |
end |
705 |
||
706 |
in |
|
707 |
||
708 |
fun simproc ctxt redex = |
|
709 |
let |
|
50422
ee729dbd1b7f
avoid ML_file in large theory files to improve performance of dependency discovery of main HOL (approx. 1s CPU time) -- relevant for any application using it, e.g. small paper sessions;
wenzelm
parents:
50134
diff
changeset
|
710 |
fun make_inner_eqs bound_vs Tis eqs t = |
60158 | 711 |
(case dest_case ctxt t of |
54404
9f0f1152c875
port list comprehension simproc to 'Ctr_Sugar' abstraction
blanchet
parents:
54295
diff
changeset
|
712 |
SOME (x, T, i, cont, constr_name) => |
50422
ee729dbd1b7f
avoid ML_file in large theory files to improve performance of dependency discovery of main HOL (approx. 1s CPU time) -- relevant for any application using it, e.g. small paper sessions;
wenzelm
parents:
50134
diff
changeset
|
713 |
let |
52131 | 714 |
val (vs, body) = strip_abs (Envir.eta_long (map snd bound_vs) cont) |
50422
ee729dbd1b7f
avoid ML_file in large theory files to improve performance of dependency discovery of main HOL (approx. 1s CPU time) -- relevant for any application using it, e.g. small paper sessions;
wenzelm
parents:
50134
diff
changeset
|
715 |
val x' = incr_boundvars (length vs) x |
ee729dbd1b7f
avoid ML_file in large theory files to improve performance of dependency discovery of main HOL (approx. 1s CPU time) -- relevant for any application using it, e.g. small paper sessions;
wenzelm
parents:
50134
diff
changeset
|
716 |
val eqs' = map (incr_boundvars (length vs)) eqs |
ee729dbd1b7f
avoid ML_file in large theory files to improve performance of dependency discovery of main HOL (approx. 1s CPU time) -- relevant for any application using it, e.g. small paper sessions;
wenzelm
parents:
50134
diff
changeset
|
717 |
val constr_t = |
ee729dbd1b7f
avoid ML_file in large theory files to improve performance of dependency discovery of main HOL (approx. 1s CPU time) -- relevant for any application using it, e.g. small paper sessions;
wenzelm
parents:
50134
diff
changeset
|
718 |
list_comb |
ee729dbd1b7f
avoid ML_file in large theory files to improve performance of dependency discovery of main HOL (approx. 1s CPU time) -- relevant for any application using it, e.g. small paper sessions;
wenzelm
parents:
50134
diff
changeset
|
719 |
(Const (constr_name, map snd vs ---> T), map Bound (((length vs) - 1) downto 0)) |
69593 | 720 |
val constr_eq = Const (\<^const_name>\<open>HOL.eq\<close>, T --> T --> \<^typ>\<open>bool\<close>) $ constr_t $ x' |
50422
ee729dbd1b7f
avoid ML_file in large theory files to improve performance of dependency discovery of main HOL (approx. 1s CPU time) -- relevant for any application using it, e.g. small paper sessions;
wenzelm
parents:
50134
diff
changeset
|
721 |
in |
ee729dbd1b7f
avoid ML_file in large theory files to improve performance of dependency discovery of main HOL (approx. 1s CPU time) -- relevant for any application using it, e.g. small paper sessions;
wenzelm
parents:
50134
diff
changeset
|
722 |
make_inner_eqs (rev vs @ bound_vs) (Case (T, i) :: Tis) (constr_eq :: eqs') body |
ee729dbd1b7f
avoid ML_file in large theory files to improve performance of dependency discovery of main HOL (approx. 1s CPU time) -- relevant for any application using it, e.g. small paper sessions;
wenzelm
parents:
50134
diff
changeset
|
723 |
end |
ee729dbd1b7f
avoid ML_file in large theory files to improve performance of dependency discovery of main HOL (approx. 1s CPU time) -- relevant for any application using it, e.g. small paper sessions;
wenzelm
parents:
50134
diff
changeset
|
724 |
| NONE => |
ee729dbd1b7f
avoid ML_file in large theory files to improve performance of dependency discovery of main HOL (approx. 1s CPU time) -- relevant for any application using it, e.g. small paper sessions;
wenzelm
parents:
50134
diff
changeset
|
725 |
(case dest_if t of |
ee729dbd1b7f
avoid ML_file in large theory files to improve performance of dependency discovery of main HOL (approx. 1s CPU time) -- relevant for any application using it, e.g. small paper sessions;
wenzelm
parents:
50134
diff
changeset
|
726 |
SOME (condition, cont) => make_inner_eqs bound_vs (If :: Tis) (condition :: eqs) cont |
ee729dbd1b7f
avoid ML_file in large theory files to improve performance of dependency discovery of main HOL (approx. 1s CPU time) -- relevant for any application using it, e.g. small paper sessions;
wenzelm
parents:
50134
diff
changeset
|
727 |
| NONE => |
60158 | 728 |
if null eqs then NONE (*no rewriting, nothing to be done*) |
50422
ee729dbd1b7f
avoid ML_file in large theory files to improve performance of dependency discovery of main HOL (approx. 1s CPU time) -- relevant for any application using it, e.g. small paper sessions;
wenzelm
parents:
50134
diff
changeset
|
729 |
else |
ee729dbd1b7f
avoid ML_file in large theory files to improve performance of dependency discovery of main HOL (approx. 1s CPU time) -- relevant for any application using it, e.g. small paper sessions;
wenzelm
parents:
50134
diff
changeset
|
730 |
let |
69593 | 731 |
val Type (\<^type_name>\<open>list\<close>, [rT]) = fastype_of1 (map snd bound_vs, t) |
50422
ee729dbd1b7f
avoid ML_file in large theory files to improve performance of dependency discovery of main HOL (approx. 1s CPU time) -- relevant for any application using it, e.g. small paper sessions;
wenzelm
parents:
50134
diff
changeset
|
732 |
val pat_eq = |
ee729dbd1b7f
avoid ML_file in large theory files to improve performance of dependency discovery of main HOL (approx. 1s CPU time) -- relevant for any application using it, e.g. small paper sessions;
wenzelm
parents:
50134
diff
changeset
|
733 |
(case try dest_singleton_list t of |
ee729dbd1b7f
avoid ML_file in large theory files to improve performance of dependency discovery of main HOL (approx. 1s CPU time) -- relevant for any application using it, e.g. small paper sessions;
wenzelm
parents:
50134
diff
changeset
|
734 |
SOME t' => |
69593 | 735 |
Const (\<^const_name>\<open>HOL.eq\<close>, rT --> rT --> \<^typ>\<open>bool\<close>) $ |
50422
ee729dbd1b7f
avoid ML_file in large theory files to improve performance of dependency discovery of main HOL (approx. 1s CPU time) -- relevant for any application using it, e.g. small paper sessions;
wenzelm
parents:
50134
diff
changeset
|
736 |
Bound (length bound_vs) $ t' |
ee729dbd1b7f
avoid ML_file in large theory files to improve performance of dependency discovery of main HOL (approx. 1s CPU time) -- relevant for any application using it, e.g. small paper sessions;
wenzelm
parents:
50134
diff
changeset
|
737 |
| NONE => |
69593 | 738 |
Const (\<^const_name>\<open>Set.member\<close>, rT --> HOLogic.mk_setT rT --> \<^typ>\<open>bool\<close>) $ |
50422
ee729dbd1b7f
avoid ML_file in large theory files to improve performance of dependency discovery of main HOL (approx. 1s CPU time) -- relevant for any application using it, e.g. small paper sessions;
wenzelm
parents:
50134
diff
changeset
|
739 |
Bound (length bound_vs) $ (mk_set rT $ t)) |
ee729dbd1b7f
avoid ML_file in large theory files to improve performance of dependency discovery of main HOL (approx. 1s CPU time) -- relevant for any application using it, e.g. small paper sessions;
wenzelm
parents:
50134
diff
changeset
|
740 |
val reverse_bounds = curry subst_bounds |
ee729dbd1b7f
avoid ML_file in large theory files to improve performance of dependency discovery of main HOL (approx. 1s CPU time) -- relevant for any application using it, e.g. small paper sessions;
wenzelm
parents:
50134
diff
changeset
|
741 |
((map Bound ((length bound_vs - 1) downto 0)) @ [Bound (length bound_vs)]) |
ee729dbd1b7f
avoid ML_file in large theory files to improve performance of dependency discovery of main HOL (approx. 1s CPU time) -- relevant for any application using it, e.g. small paper sessions;
wenzelm
parents:
50134
diff
changeset
|
742 |
val eqs' = map reverse_bounds eqs |
ee729dbd1b7f
avoid ML_file in large theory files to improve performance of dependency discovery of main HOL (approx. 1s CPU time) -- relevant for any application using it, e.g. small paper sessions;
wenzelm
parents:
50134
diff
changeset
|
743 |
val pat_eq' = reverse_bounds pat_eq |
ee729dbd1b7f
avoid ML_file in large theory files to improve performance of dependency discovery of main HOL (approx. 1s CPU time) -- relevant for any application using it, e.g. small paper sessions;
wenzelm
parents:
50134
diff
changeset
|
744 |
val inner_t = |
ee729dbd1b7f
avoid ML_file in large theory files to improve performance of dependency discovery of main HOL (approx. 1s CPU time) -- relevant for any application using it, e.g. small paper sessions;
wenzelm
parents:
50134
diff
changeset
|
745 |
fold (fn (_, T) => fn t => HOLogic.exists_const T $ absdummy T t) |
ee729dbd1b7f
avoid ML_file in large theory files to improve performance of dependency discovery of main HOL (approx. 1s CPU time) -- relevant for any application using it, e.g. small paper sessions;
wenzelm
parents:
50134
diff
changeset
|
746 |
(rev bound_vs) (fold (curry HOLogic.mk_conj) eqs' pat_eq') |
59582 | 747 |
val lhs = Thm.term_of redex |
50422
ee729dbd1b7f
avoid ML_file in large theory files to improve performance of dependency discovery of main HOL (approx. 1s CPU time) -- relevant for any application using it, e.g. small paper sessions;
wenzelm
parents:
50134
diff
changeset
|
748 |
val rhs = HOLogic.mk_Collect ("x", rT, inner_t) |
ee729dbd1b7f
avoid ML_file in large theory files to improve performance of dependency discovery of main HOL (approx. 1s CPU time) -- relevant for any application using it, e.g. small paper sessions;
wenzelm
parents:
50134
diff
changeset
|
749 |
val rewrite_rule_t = HOLogic.mk_Trueprop (HOLogic.mk_eq (lhs, rhs)) |
ee729dbd1b7f
avoid ML_file in large theory files to improve performance of dependency discovery of main HOL (approx. 1s CPU time) -- relevant for any application using it, e.g. small paper sessions;
wenzelm
parents:
50134
diff
changeset
|
750 |
in |
ee729dbd1b7f
avoid ML_file in large theory files to improve performance of dependency discovery of main HOL (approx. 1s CPU time) -- relevant for any application using it, e.g. small paper sessions;
wenzelm
parents:
50134
diff
changeset
|
751 |
SOME |
ee729dbd1b7f
avoid ML_file in large theory files to improve performance of dependency discovery of main HOL (approx. 1s CPU time) -- relevant for any application using it, e.g. small paper sessions;
wenzelm
parents:
50134
diff
changeset
|
752 |
((Goal.prove ctxt [] [] rewrite_rule_t |
60159
879918f4ee0f
tuned -- avoid odd rebinding of "ctxt" and "context";
wenzelm
parents:
60158
diff
changeset
|
753 |
(fn {context = ctxt', ...} => tac ctxt' (rev Tis))) RS @{thm eq_reflection}) |
50422
ee729dbd1b7f
avoid ML_file in large theory files to improve performance of dependency discovery of main HOL (approx. 1s CPU time) -- relevant for any application using it, e.g. small paper sessions;
wenzelm
parents:
50134
diff
changeset
|
754 |
end)) |
ee729dbd1b7f
avoid ML_file in large theory files to improve performance of dependency discovery of main HOL (approx. 1s CPU time) -- relevant for any application using it, e.g. small paper sessions;
wenzelm
parents:
50134
diff
changeset
|
755 |
in |
59582 | 756 |
make_inner_eqs [] [] [] (dest_set (Thm.term_of redex)) |
50422
ee729dbd1b7f
avoid ML_file in large theory files to improve performance of dependency discovery of main HOL (approx. 1s CPU time) -- relevant for any application using it, e.g. small paper sessions;
wenzelm
parents:
50134
diff
changeset
|
757 |
end |
ee729dbd1b7f
avoid ML_file in large theory files to improve performance of dependency discovery of main HOL (approx. 1s CPU time) -- relevant for any application using it, e.g. small paper sessions;
wenzelm
parents:
50134
diff
changeset
|
758 |
|
ee729dbd1b7f
avoid ML_file in large theory files to improve performance of dependency discovery of main HOL (approx. 1s CPU time) -- relevant for any application using it, e.g. small paper sessions;
wenzelm
parents:
50134
diff
changeset
|
759 |
end |
60158 | 760 |
|
761 |
end |
|
60758 | 762 |
\<close> |
41463
edbf0a86fb1c
adding simproc to rewrite list comprehensions to set comprehensions; adopting proofs
bulwahn
parents:
41372
diff
changeset
|
763 |
|
60159
879918f4ee0f
tuned -- avoid odd rebinding of "ctxt" and "context";
wenzelm
parents:
60158
diff
changeset
|
764 |
simproc_setup list_to_set_comprehension ("set xs") = |
60758 | 765 |
\<open>K List_to_Set_Comprehension.simproc\<close> |
41463
edbf0a86fb1c
adding simproc to rewrite list comprehensions to set comprehensions; adopting proofs
bulwahn
parents:
41372
diff
changeset
|
766 |
|
46133
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
haftmann
parents:
46125
diff
changeset
|
767 |
code_datatype set coset |
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
haftmann
parents:
46125
diff
changeset
|
768 |
hide_const (open) coset |
35115 | 769 |
|
49948
744934b818c7
moved quite generic material from theory Enum to more appropriate places
haftmann
parents:
49808
diff
changeset
|
770 |
|
69593 | 771 |
subsubsection \<open>\<^const>\<open>Nil\<close> and \<^const>\<open>Cons\<close>\<close> |
21061
580dfc999ef6
added normal post setup; cleaned up "execution" constants
haftmann
parents:
21046
diff
changeset
|
772 |
|
580dfc999ef6
added normal post setup; cleaned up "execution" constants
haftmann
parents:
21046
diff
changeset
|
773 |
lemma not_Cons_self [simp]: |
580dfc999ef6
added normal post setup; cleaned up "execution" constants
haftmann
parents:
21046
diff
changeset
|
774 |
"xs \<noteq> x # xs" |
13145 | 775 |
by (induct xs) auto |
13114 | 776 |
|
58807 | 777 |
lemma not_Cons_self2 [simp]: "x # xs \<noteq> xs" |
41697 | 778 |
by (rule not_Cons_self [symmetric]) |
13114 | 779 |
|
13142 | 780 |
lemma neq_Nil_conv: "(xs \<noteq> []) = (\<exists>y ys. xs = y # ys)" |
13145 | 781 |
by (induct xs) auto |
13114 | 782 |
|
67091 | 783 |
lemma tl_Nil: "tl xs = [] \<longleftrightarrow> xs = [] \<or> (\<exists>x. xs = [x])" |
53689 | 784 |
by (cases xs) auto |
785 |
||
67091 | 786 |
lemma Nil_tl: "[] = tl xs \<longleftrightarrow> xs = [] \<or> (\<exists>x. xs = [x])" |
53689 | 787 |
by (cases xs) auto |
788 |
||
13142 | 789 |
lemma length_induct: |
21061
580dfc999ef6
added normal post setup; cleaned up "execution" constants
haftmann
parents:
21046
diff
changeset
|
790 |
"(\<And>xs. \<forall>ys. length ys < length xs \<longrightarrow> P ys \<Longrightarrow> P xs) \<Longrightarrow> P xs" |
53689 | 791 |
by (fact measure_induct) |
13114 | 792 |
|
67168 | 793 |
lemma induct_list012: |
70275 | 794 |
"\<lbrakk>P []; \<And>x. P [x]; \<And>x y zs. \<lbrakk> P zs; P (y # zs) \<rbrakk> \<Longrightarrow> P (x # y # zs)\<rbrakk> \<Longrightarrow> P xs" |
67168 | 795 |
by induction_schema (pat_completeness, lexicographic_order) |
796 |
||
37289 | 797 |
lemma list_nonempty_induct [consumes 1, case_names single cons]: |
67168 | 798 |
"\<lbrakk> xs \<noteq> []; \<And>x. P [x]; \<And>x xs. xs \<noteq> [] \<Longrightarrow> P xs \<Longrightarrow> P (x # xs)\<rbrakk> \<Longrightarrow> P xs" |
799 |
by(induction xs rule: induct_list012) auto |
|
37289 | 800 |
|
45714 | 801 |
lemma inj_split_Cons: "inj_on (\<lambda>(xs, n). n#xs) X" |
802 |
by (auto intro!: inj_onI) |
|
13114 | 803 |
|
67399 | 804 |
lemma inj_on_Cons1 [simp]: "inj_on ((#) x) A" |
61630 | 805 |
by(simp add: inj_on_def) |
49948
744934b818c7
moved quite generic material from theory Enum to more appropriate places
haftmann
parents:
49808
diff
changeset
|
806 |
|
69593 | 807 |
subsubsection \<open>\<^const>\<open>length\<close>\<close> |
60758 | 808 |
|
809 |
text \<open> |
|
61799 | 810 |
Needs to come before \<open>@\<close> because of theorem \<open>append_eq_append_conv\<close>. |
60758 | 811 |
\<close> |
13114 | 812 |
|
13142 | 813 |
lemma length_append [simp]: "length (xs @ ys) = length xs + length ys" |
13145 | 814 |
by (induct xs) auto |
13114 | 815 |
|
13142 | 816 |
lemma length_map [simp]: "length (map f xs) = length xs" |
13145 | 817 |
by (induct xs) auto |
13114 | 818 |
|
13142 | 819 |
lemma length_rev [simp]: "length (rev xs) = length xs" |
13145 | 820 |
by (induct xs) auto |
13114 | 821 |
|
13142 | 822 |
lemma length_tl [simp]: "length (tl xs) = length xs - 1" |
13145 | 823 |
by (cases xs) auto |
13114 | 824 |
|
13142 | 825 |
lemma length_0_conv [iff]: "(length xs = 0) = (xs = [])" |
13145 | 826 |
by (induct xs) auto |
13114 | 827 |
|
13142 | 828 |
lemma length_greater_0_conv [iff]: "(0 < length xs) = (xs \<noteq> [])" |
13145 | 829 |
by (induct xs) auto |
13114 | 830 |
|
67613 | 831 |
lemma length_pos_if_in_set: "x \<in> set xs \<Longrightarrow> length xs > 0" |
23479 | 832 |
by auto |
833 |
||
13114 | 834 |
lemma length_Suc_conv: |
13145 | 835 |
"(length xs = Suc n) = (\<exists>y ys. xs = y # ys \<and> length ys = n)" |
836 |
by (induct xs) auto |
|
13142 | 837 |
|
14025 | 838 |
lemma Suc_length_conv: |
58807 | 839 |
"(Suc n = length xs) = (\<exists>y ys. xs = y # ys \<and> length ys = n)" |
68709 | 840 |
by (induct xs; simp; blast) |
841 |
||
69312 | 842 |
lemma Suc_le_length_iff: |
843 |
"(Suc n \<le> length xs) = (\<exists>x ys. xs = x # ys \<and> n \<le> length ys)" |
|
844 |
by (metis Suc_le_D[of n] Suc_le_mono[of n] Suc_length_conv[of _ xs]) |
|
845 |
||
68709 | 846 |
lemma impossible_Cons: "length xs \<le> length ys ==> xs = x # ys = False" |
58807 | 847 |
by (induct xs) auto |
25221
5ded95dda5df
append/member: more light-weight way to declare authentic syntax;
wenzelm
parents:
25215
diff
changeset
|
848 |
|
26442
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
haftmann
parents:
26300
diff
changeset
|
849 |
lemma list_induct2 [consumes 1, case_names Nil Cons]: |
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
haftmann
parents:
26300
diff
changeset
|
850 |
"length xs = length ys \<Longrightarrow> P [] [] \<Longrightarrow> |
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
haftmann
parents:
26300
diff
changeset
|
851 |
(\<And>x xs y ys. length xs = length ys \<Longrightarrow> P xs ys \<Longrightarrow> P (x#xs) (y#ys)) |
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
haftmann
parents:
26300
diff
changeset
|
852 |
\<Longrightarrow> P xs ys" |
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
haftmann
parents:
26300
diff
changeset
|
853 |
proof (induct xs arbitrary: ys) |
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
haftmann
parents:
26300
diff
changeset
|
854 |
case (Cons x xs ys) then show ?case by (cases ys) simp_all |
68709 | 855 |
qed simp |
26442
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
haftmann
parents:
26300
diff
changeset
|
856 |
|
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
haftmann
parents:
26300
diff
changeset
|
857 |
lemma list_induct3 [consumes 2, case_names Nil Cons]: |
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
haftmann
parents:
26300
diff
changeset
|
858 |
"length xs = length ys \<Longrightarrow> length ys = length zs \<Longrightarrow> P [] [] [] \<Longrightarrow> |
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
haftmann
parents:
26300
diff
changeset
|
859 |
(\<And>x xs y ys z zs. length xs = length ys \<Longrightarrow> length ys = length zs \<Longrightarrow> P xs ys zs \<Longrightarrow> P (x#xs) (y#ys) (z#zs)) |
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
haftmann
parents:
26300
diff
changeset
|
860 |
\<Longrightarrow> P xs ys zs" |
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
haftmann
parents:
26300
diff
changeset
|
861 |
proof (induct xs arbitrary: ys zs) |
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
haftmann
parents:
26300
diff
changeset
|
862 |
case Nil then show ?case by simp |
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
haftmann
parents:
26300
diff
changeset
|
863 |
next |
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
haftmann
parents:
26300
diff
changeset
|
864 |
case (Cons x xs ys zs) then show ?case by (cases ys, simp_all) |
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
haftmann
parents:
26300
diff
changeset
|
865 |
(cases zs, simp_all) |
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
haftmann
parents:
26300
diff
changeset
|
866 |
qed |
13114 | 867 |
|
36154
11c6106d7787
Respectfullness and preservation of list_rel
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
35828
diff
changeset
|
868 |
lemma list_induct4 [consumes 3, case_names Nil Cons]: |
11c6106d7787
Respectfullness and preservation of list_rel
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
35828
diff
changeset
|
869 |
"length xs = length ys \<Longrightarrow> length ys = length zs \<Longrightarrow> length zs = length ws \<Longrightarrow> |
11c6106d7787
Respectfullness and preservation of list_rel
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
35828
diff
changeset
|
870 |
P [] [] [] [] \<Longrightarrow> (\<And>x xs y ys z zs w ws. length xs = length ys \<Longrightarrow> |
11c6106d7787
Respectfullness and preservation of list_rel
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
35828
diff
changeset
|
871 |
length ys = length zs \<Longrightarrow> length zs = length ws \<Longrightarrow> P xs ys zs ws \<Longrightarrow> |
11c6106d7787
Respectfullness and preservation of list_rel
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
35828
diff
changeset
|
872 |
P (x#xs) (y#ys) (z#zs) (w#ws)) \<Longrightarrow> P xs ys zs ws" |
11c6106d7787
Respectfullness and preservation of list_rel
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
35828
diff
changeset
|
873 |
proof (induct xs arbitrary: ys zs ws) |
11c6106d7787
Respectfullness and preservation of list_rel
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
35828
diff
changeset
|
874 |
case Nil then show ?case by simp |
11c6106d7787
Respectfullness and preservation of list_rel
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
35828
diff
changeset
|
875 |
next |
11c6106d7787
Respectfullness and preservation of list_rel
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
35828
diff
changeset
|
876 |
case (Cons x xs ys zs ws) then show ?case by ((cases ys, simp_all), (cases zs,simp_all)) (cases ws, simp_all) |
11c6106d7787
Respectfullness and preservation of list_rel
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
35828
diff
changeset
|
877 |
qed |
11c6106d7787
Respectfullness and preservation of list_rel
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
35828
diff
changeset
|
878 |
|
64963 | 879 |
lemma list_induct2': |
22493
db930e490fe5
added another rule for simultaneous induction, and lemmas for zip
krauss
parents:
22422
diff
changeset
|
880 |
"\<lbrakk> P [] []; |
db930e490fe5
added another rule for simultaneous induction, and lemmas for zip
krauss
parents:
22422
diff
changeset
|
881 |
\<And>x xs. P (x#xs) []; |
db930e490fe5
added another rule for simultaneous induction, and lemmas for zip
krauss
parents:
22422
diff
changeset
|
882 |
\<And>y ys. P [] (y#ys); |
db930e490fe5
added another rule for simultaneous induction, and lemmas for zip
krauss
parents:
22422
diff
changeset
|
883 |
\<And>x xs y ys. P xs ys \<Longrightarrow> P (x#xs) (y#ys) \<rbrakk> |
db930e490fe5
added another rule for simultaneous induction, and lemmas for zip
krauss
parents:
22422
diff
changeset
|
884 |
\<Longrightarrow> P xs ys" |
db930e490fe5
added another rule for simultaneous induction, and lemmas for zip
krauss
parents:
22422
diff
changeset
|
885 |
by (induct xs arbitrary: ys) (case_tac x, auto)+ |
db930e490fe5
added another rule for simultaneous induction, and lemmas for zip
krauss
parents:
22422
diff
changeset
|
886 |
|
55524
f41ef840f09d
folded 'list_all2' with the relator generated by 'datatype_new'
blanchet
parents:
55473
diff
changeset
|
887 |
lemma list_all2_iff: |
f41ef840f09d
folded 'list_all2' with the relator generated by 'datatype_new'
blanchet
parents:
55473
diff
changeset
|
888 |
"list_all2 P xs ys \<longleftrightarrow> length xs = length ys \<and> (\<forall>(x, y) \<in> set (zip xs ys). P x y)" |
f41ef840f09d
folded 'list_all2' with the relator generated by 'datatype_new'
blanchet
parents:
55473
diff
changeset
|
889 |
by (induct xs ys rule: list_induct2') auto |
f41ef840f09d
folded 'list_all2' with the relator generated by 'datatype_new'
blanchet
parents:
55473
diff
changeset
|
890 |
|
22143
cf58486ca11b
Added simproc list_neq (prompted by an application)
nipkow
parents:
21911
diff
changeset
|
891 |
lemma neq_if_length_neq: "length xs \<noteq> length ys \<Longrightarrow> (xs = ys) == False" |
24349 | 892 |
by (rule Eq_FalseI) auto |
24037 | 893 |
|
60758 | 894 |
simproc_setup list_neq ("(xs::'a list) = ys") = \<open> |
22143
cf58486ca11b
Added simproc list_neq (prompted by an application)
nipkow
parents:
21911
diff
changeset
|
895 |
(* |
cf58486ca11b
Added simproc list_neq (prompted by an application)
nipkow
parents:
21911
diff
changeset
|
896 |
Reduces xs=ys to False if xs and ys cannot be of the same length. |
cf58486ca11b
Added simproc list_neq (prompted by an application)
nipkow
parents:
21911
diff
changeset
|
897 |
This is the case if the atomic sublists of one are a submultiset |
cf58486ca11b
Added simproc list_neq (prompted by an application)
nipkow
parents:
21911
diff
changeset
|
898 |
of those of the other list and there are fewer Cons's in one than the other. |
cf58486ca11b
Added simproc list_neq (prompted by an application)
nipkow
parents:
21911
diff
changeset
|
899 |
*) |
24037 | 900 |
|
901 |
let |
|
22143
cf58486ca11b
Added simproc list_neq (prompted by an application)
nipkow
parents:
21911
diff
changeset
|
902 |
|
69593 | 903 |
fun len (Const(\<^const_name>\<open>Nil\<close>,_)) acc = acc |
904 |
| len (Const(\<^const_name>\<open>Cons\<close>,_) $ _ $ xs) (ts,n) = len xs (ts,n+1) |
|
905 |
| len (Const(\<^const_name>\<open>append\<close>,_) $ xs $ ys) acc = len xs (len ys acc) |
|
906 |
| len (Const(\<^const_name>\<open>rev\<close>,_) $ xs) acc = len xs acc |
|
907 |
| len (Const(\<^const_name>\<open>map\<close>,_) $ _ $ xs) acc = len xs acc |
|
22143
cf58486ca11b
Added simproc list_neq (prompted by an application)
nipkow
parents:
21911
diff
changeset
|
908 |
| len t (ts,n) = (t::ts,n); |
cf58486ca11b
Added simproc list_neq (prompted by an application)
nipkow
parents:
21911
diff
changeset
|
909 |
|
69593 | 910 |
val ss = simpset_of \<^context>; |
51717
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents:
51678
diff
changeset
|
911 |
|
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents:
51678
diff
changeset
|
912 |
fun list_neq ctxt ct = |
22143
cf58486ca11b
Added simproc list_neq (prompted by an application)
nipkow
parents:
21911
diff
changeset
|
913 |
let |
24037 | 914 |
val (Const(_,eqT) $ lhs $ rhs) = Thm.term_of ct; |
22143
cf58486ca11b
Added simproc list_neq (prompted by an application)
nipkow
parents:
21911
diff
changeset
|
915 |
val (ls,m) = len lhs ([],0) and (rs,n) = len rhs ([],0); |
cf58486ca11b
Added simproc list_neq (prompted by an application)
nipkow
parents:
21911
diff
changeset
|
916 |
fun prove_neq() = |
cf58486ca11b
Added simproc list_neq (prompted by an application)
nipkow
parents:
21911
diff
changeset
|
917 |
let |
cf58486ca11b
Added simproc list_neq (prompted by an application)
nipkow
parents:
21911
diff
changeset
|
918 |
val Type(_,listT::_) = eqT; |
22994 | 919 |
val size = HOLogic.size_const listT; |
22143
cf58486ca11b
Added simproc list_neq (prompted by an application)
nipkow
parents:
21911
diff
changeset
|
920 |
val eq_len = HOLogic.mk_eq (size $ lhs, size $ rhs); |
cf58486ca11b
Added simproc list_neq (prompted by an application)
nipkow
parents:
21911
diff
changeset
|
921 |
val neq_len = HOLogic.mk_Trueprop (HOLogic.Not $ eq_len); |
51717
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents:
51678
diff
changeset
|
922 |
val thm = Goal.prove ctxt [] [] neq_len |
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents:
51678
diff
changeset
|
923 |
(K (simp_tac (put_simpset ss ctxt) 1)); |
22633 | 924 |
in SOME (thm RS @{thm neq_if_length_neq}) end |
22143
cf58486ca11b
Added simproc list_neq (prompted by an application)
nipkow
parents:
21911
diff
changeset
|
925 |
in |
69215 | 926 |
if m < n andalso submultiset (op aconv) (ls,rs) orelse |
927 |
n < m andalso submultiset (op aconv) (rs,ls) |
|
22143
cf58486ca11b
Added simproc list_neq (prompted by an application)
nipkow
parents:
21911
diff
changeset
|
928 |
then prove_neq() else NONE |
cf58486ca11b
Added simproc list_neq (prompted by an application)
nipkow
parents:
21911
diff
changeset
|
929 |
end; |
69215 | 930 |
in K list_neq end |
60758 | 931 |
\<close> |
932 |
||
933 |
||
61799 | 934 |
subsubsection \<open>\<open>@\<close> -- append\<close> |
13114 | 935 |
|
63662 | 936 |
global_interpretation append: monoid append Nil |
937 |
proof |
|
938 |
fix xs ys zs :: "'a list" |
|
939 |
show "(xs @ ys) @ zs = xs @ (ys @ zs)" |
|
940 |
by (induct xs) simp_all |
|
941 |
show "xs @ [] = xs" |
|
942 |
by (induct xs) simp_all |
|
943 |
qed simp |
|
944 |
||
13142 | 945 |
lemma append_assoc [simp]: "(xs @ ys) @ zs = xs @ (ys @ zs)" |
63662 | 946 |
by (fact append.assoc) |
947 |
||
948 |
lemma append_Nil2: "xs @ [] = xs" |
|
949 |
by (fact append.right_neutral) |
|
3507 | 950 |
|
13142 | 951 |
lemma append_is_Nil_conv [iff]: "(xs @ ys = []) = (xs = [] \<and> ys = [])" |
13145 | 952 |
by (induct xs) auto |
13114 | 953 |
|
13142 | 954 |
lemma Nil_is_append_conv [iff]: "([] = xs @ ys) = (xs = [] \<and> ys = [])" |
13145 | 955 |
by (induct xs) auto |
13114 | 956 |
|
13142 | 957 |
lemma append_self_conv [iff]: "(xs @ ys = xs) = (ys = [])" |
13145 | 958 |
by (induct xs) auto |
13114 | 959 |
|
13142 | 960 |
lemma self_append_conv [iff]: "(xs = xs @ ys) = (ys = [])" |
13145 | 961 |
by (induct xs) auto |
13114 | 962 |
|
54147
97a8ff4e4ac9
killed most "no_atp", to make Sledgehammer more complete
blanchet
parents:
53954
diff
changeset
|
963 |
lemma append_eq_append_conv [simp]: |
58807 | 964 |
"length xs = length ys \<or> length us = length vs |
965 |
==> (xs@us = ys@vs) = (xs=ys \<and> us=vs)" |
|
68709 | 966 |
by (induct xs arbitrary: ys; case_tac ys; force) |
13142 | 967 |
|
24526 | 968 |
lemma append_eq_append_conv2: "(xs @ ys = zs @ ts) = |
67091 | 969 |
(\<exists>us. xs = zs @ us \<and> us @ ys = ts \<or> xs @ us = zs \<and> ys = us @ ts)" |
68709 | 970 |
proof (induct xs arbitrary: ys zs ts) |
971 |
case (Cons x xs) |
|
972 |
then show ?case |
|
69850 | 973 |
by (cases zs) auto |
68709 | 974 |
qed fastforce |
14495 | 975 |
|
34910
b23bd3ee4813
same_append_eq / append_same_eq are now used for simplifying induction rules.
berghofe
parents:
34064
diff
changeset
|
976 |
lemma same_append_eq [iff, induct_simp]: "(xs @ ys = xs @ zs) = (ys = zs)" |
13145 | 977 |
by simp |
13142 | 978 |
|
979 |
lemma append1_eq_conv [iff]: "(xs @ [x] = ys @ [y]) = (xs = ys \<and> x = y)" |
|
13145 | 980 |
by simp |
13114 | 981 |
|
34910
b23bd3ee4813
same_append_eq / append_same_eq are now used for simplifying induction rules.
berghofe
parents:
34064
diff
changeset
|
982 |
lemma append_same_eq [iff, induct_simp]: "(ys @ xs = zs @ xs) = (ys = zs)" |
13145 | 983 |
by simp |
13114 | 984 |
|
13142 | 985 |
lemma append_self_conv2 [iff]: "(xs @ ys = ys) = (xs = [])" |
13145 | 986 |
using append_same_eq [of _ _ "[]"] by auto |
3507 | 987 |
|
13142 | 988 |
lemma self_append_conv2 [iff]: "(ys = xs @ ys) = (xs = [])" |
13145 | 989 |
using append_same_eq [of "[]"] by auto |
13114 | 990 |
|
63662 | 991 |
lemma hd_Cons_tl: "xs \<noteq> [] ==> hd xs # tl xs = xs" |
992 |
by (fact list.collapse) |
|
13114 | 993 |
|
13142 | 994 |
lemma hd_append: "hd (xs @ ys) = (if xs = [] then hd ys else hd xs)" |
13145 | 995 |
by (induct xs) auto |
13114 | 996 |
|
13142 | 997 |
lemma hd_append2 [simp]: "xs \<noteq> [] ==> hd (xs @ ys) = hd xs" |
13145 | 998 |
by (simp add: hd_append split: list.split) |
13114 | 999 |
|
67091 | 1000 |
lemma tl_append: "tl (xs @ ys) = (case xs of [] \<Rightarrow> tl ys | z#zs \<Rightarrow> zs @ ys)" |
13145 | 1001 |
by (simp split: list.split) |
13114 | 1002 |
|
13142 | 1003 |
lemma tl_append2 [simp]: "xs \<noteq> [] ==> tl (xs @ ys) = tl xs @ ys" |
13145 | 1004 |
by (simp add: tl_append split: list.split) |
13114 | 1005 |
|
1006 |
||
14300 | 1007 |
lemma Cons_eq_append_conv: "x#xs = ys@zs = |
67091 | 1008 |
(ys = [] \<and> x#xs = zs \<or> (\<exists>ys'. x#ys' = ys \<and> xs = ys'@zs))" |
14300 | 1009 |
by(cases ys) auto |
1010 |
||
15281 | 1011 |
lemma append_eq_Cons_conv: "(ys@zs = x#xs) = |
67091 | 1012 |
(ys = [] \<and> zs = x#xs \<or> (\<exists>ys'. ys = x#ys' \<and> ys'@zs = xs))" |
15281 | 1013 |
by(cases ys) auto |
1014 |
||
63173 | 1015 |
lemma longest_common_prefix: |
1016 |
"\<exists>ps xs' ys'. xs = ps @ xs' \<and> ys = ps @ ys' |
|
1017 |
\<and> (xs' = [] \<or> ys' = [] \<or> hd xs' \<noteq> hd ys')" |
|
1018 |
by (induct xs ys rule: list_induct2') |
|
1019 |
(blast, blast, blast, |
|
1020 |
metis (no_types, hide_lams) append_Cons append_Nil list.sel(1)) |
|
14300 | 1021 |
|
61799 | 1022 |
text \<open>Trivial rules for solving \<open>@\<close>-equations automatically.\<close> |
13114 | 1023 |
|
1024 |
lemma eq_Nil_appendI: "xs = ys ==> xs = [] @ ys" |
|
13145 | 1025 |
by simp |
13114 | 1026 |
|
13142 | 1027 |
lemma Cons_eq_appendI: |
13145 | 1028 |
"[| x # xs1 = ys; xs = xs1 @ zs |] ==> x # xs = ys @ zs" |
1029 |
by (drule sym) simp |
|
13114 | 1030 |
|
13142 | 1031 |
lemma append_eq_appendI: |
13145 | 1032 |
"[| xs @ xs1 = zs; ys = xs1 @ us |] ==> xs @ ys = zs @ us" |
1033 |
by (drule sym) simp |
|
13114 | 1034 |
|
1035 |
||
60758 | 1036 |
text \<open> |
13145 | 1037 |
Simplification procedure for all list equalities. |
61799 | 1038 |
Currently only tries to rearrange \<open>@\<close> to see if |
13145 | 1039 |
- both lists end in a singleton list, |
1040 |
- or both lists end in the same list. |
|
60758 | 1041 |
\<close> |
1042 |
||
1043 |
simproc_setup list_eq ("(xs::'a list) = ys") = \<open> |
|
13462 | 1044 |
let |
69593 | 1045 |
fun last (cons as Const (\<^const_name>\<open>Cons\<close>, _) $ _ $ xs) = |
1046 |
(case xs of Const (\<^const_name>\<open>Nil\<close>, _) => cons | _ => last xs) |
|
1047 |
| last (Const(\<^const_name>\<open>append\<close>,_) $ _ $ ys) = last ys |
|
43594 | 1048 |
| last t = t; |
64963 | 1049 |
|
69593 | 1050 |
fun list1 (Const(\<^const_name>\<open>Cons\<close>,_) $ _ $ Const(\<^const_name>\<open>Nil\<close>,_)) = true |
43594 | 1051 |
| list1 _ = false; |
64963 | 1052 |
|
69593 | 1053 |
fun butlast ((cons as Const(\<^const_name>\<open>Cons\<close>,_) $ x) $ xs) = |
1054 |
(case xs of Const (\<^const_name>\<open>Nil\<close>, _) => xs | _ => cons $ butlast xs) |
|
1055 |
| butlast ((app as Const (\<^const_name>\<open>append\<close>, _) $ xs) $ ys) = app $ butlast ys |
|
1056 |
| butlast xs = Const(\<^const_name>\<open>Nil\<close>, fastype_of xs); |
|
64963 | 1057 |
|
43594 | 1058 |
val rearr_ss = |
69593 | 1059 |
simpset_of (put_simpset HOL_basic_ss \<^context> |
51717
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents:
51678
diff
changeset
|
1060 |
addsimps [@{thm append_assoc}, @{thm append_Nil}, @{thm append_Cons}]); |
64963 | 1061 |
|
51717
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents:
51678
diff
changeset
|
1062 |
fun list_eq ctxt (F as (eq as Const(_,eqT)) $ lhs $ rhs) = |
13462 | 1063 |
let |
43594 | 1064 |
val lastl = last lhs and lastr = last rhs; |
1065 |
fun rearr conv = |
|
1066 |
let |
|
1067 |
val lhs1 = butlast lhs and rhs1 = butlast rhs; |
|
1068 |
val Type(_,listT::_) = eqT |
|
1069 |
val appT = [listT,listT] ---> listT |
|
69593 | 1070 |
val app = Const(\<^const_name>\<open>append\<close>,appT) |
43594 | 1071 |
val F2 = eq $ (app$lhs1$lastl) $ (app$rhs1$lastr) |
1072 |
val eq = HOLogic.mk_Trueprop (HOLogic.mk_eq (F,F2)); |
|
51717
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents:
51678
diff
changeset
|
1073 |
val thm = Goal.prove ctxt [] [] eq |
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents:
51678
diff
changeset
|
1074 |
(K (simp_tac (put_simpset rearr_ss ctxt) 1)); |
43594 | 1075 |
in SOME ((conv RS (thm RS trans)) RS eq_reflection) end; |
1076 |
in |
|
1077 |
if list1 lastl andalso list1 lastr then rearr @{thm append1_eq_conv} |
|
1078 |
else if lastl aconv lastr then rearr @{thm append_same_eq} |
|
1079 |
else NONE |
|
1080 |
end; |
|
69215 | 1081 |
in fn _ => fn ctxt => fn ct => list_eq ctxt (Thm.term_of ct) end |
60758 | 1082 |
\<close> |
1083 |
||
1084 |
||
69593 | 1085 |
subsubsection \<open>\<^const>\<open>map\<close>\<close> |
13114 | 1086 |
|
58807 | 1087 |
lemma hd_map: "xs \<noteq> [] \<Longrightarrow> hd (map f xs) = f (hd xs)" |
1088 |
by (cases xs) simp_all |
|
1089 |
||
1090 |
lemma map_tl: "map f (tl xs) = tl (map f xs)" |
|
1091 |
by (cases xs) simp_all |
|
40210
aee7ef725330
sorting: avoid _key suffix if lemma applies both to simple and generalized variant; generalized insort_insert to insort_insert_key; additional lemmas
haftmann
parents:
40195
diff
changeset
|
1092 |
|
67091 | 1093 |
lemma map_ext: "(\<And>x. x \<in> set xs \<longrightarrow> f x = g x) ==> map f xs = map g xs" |
13145 | 1094 |
by (induct xs) simp_all |
13114 | 1095 |
|
13142 | 1096 |
lemma map_ident [simp]: "map (\<lambda>x. x) = (\<lambda>xs. xs)" |
13145 | 1097 |
by (rule ext, induct_tac xs) auto |
13114 | 1098 |
|
13142 | 1099 |
lemma map_append [simp]: "map f (xs @ ys) = map f xs @ map f ys" |
13145 | 1100 |
by (induct xs) auto |
13114 | 1101 |
|
33639
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
hoelzl
parents:
33593
diff
changeset
|
1102 |
lemma map_map [simp]: "map f (map g xs) = map (f \<circ> g) xs" |
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
hoelzl
parents:
33593
diff
changeset
|
1103 |
by (induct xs) auto |
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
hoelzl
parents:
33593
diff
changeset
|
1104 |
|
67091 | 1105 |
lemma map_comp_map[simp]: "((map f) \<circ> (map g)) = map(f \<circ> g)" |
58807 | 1106 |
by (rule ext) simp |
35208 | 1107 |
|
13142 | 1108 |
lemma rev_map: "rev (map f xs) = map f (rev xs)" |
13145 | 1109 |
by (induct xs) auto |
13114 | 1110 |
|
67613 | 1111 |
lemma map_eq_conv[simp]: "(map f xs = map g xs) = (\<forall>x \<in> set xs. f x = g x)" |
13737 | 1112 |
by (induct xs) auto |
1113 |
||
44013
5cfc1c36ae97
moved recdef package to HOL/Library/Old_Recdef.thy
krauss
parents:
43594
diff
changeset
|
1114 |
lemma map_cong [fundef_cong]: |
40122
1d8ad2ff3e01
dropped (almost) redundant distinct.induct rule; distinct_simps again named distinct.simps
haftmann
parents:
40077
diff
changeset
|
1115 |
"xs = ys \<Longrightarrow> (\<And>x. x \<in> set ys \<Longrightarrow> f x = g x) \<Longrightarrow> map f xs = map g ys" |
58807 | 1116 |
by simp |
13114 | 1117 |
|
13142 | 1118 |
lemma map_is_Nil_conv [iff]: "(map f xs = []) = (xs = [])" |
13145 | 1119 |
by (cases xs) auto |
13114 | 1120 |
|
13142 | 1121 |
lemma Nil_is_map_conv [iff]: "([] = map f xs) = (xs = [])" |
13145 | 1122 |
by (cases xs) auto |
13114 | 1123 |
|
18447 | 1124 |
lemma map_eq_Cons_conv: |
58807 | 1125 |
"(map f xs = y#ys) = (\<exists>z zs. xs = z#zs \<and> f z = y \<and> map f zs = ys)" |
13145 | 1126 |
by (cases xs) auto |
13114 | 1127 |
|
18447 | 1128 |
lemma Cons_eq_map_conv: |
58807 | 1129 |
"(x#xs = map f ys) = (\<exists>z zs. ys = z#zs \<and> x = f z \<and> xs = map f zs)" |
14025 | 1130 |
by (cases ys) auto |
1131 |
||
18447 | 1132 |
lemmas map_eq_Cons_D = map_eq_Cons_conv [THEN iffD1] |
1133 |
lemmas Cons_eq_map_D = Cons_eq_map_conv [THEN iffD1] |
|
1134 |
declare map_eq_Cons_D [dest!] Cons_eq_map_D [dest!] |
|
1135 |
||
14111 | 1136 |
lemma ex_map_conv: |
67091 | 1137 |
"(\<exists>xs. ys = map f xs) = (\<forall>y \<in> set ys. \<exists>x. y = f x)" |
18447 | 1138 |
by(induct ys, auto simp add: Cons_eq_map_conv) |
14111 | 1139 |
|
15110
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
1140 |
lemma map_eq_imp_length_eq: |
35510 | 1141 |
assumes "map f xs = map g ys" |
26734 | 1142 |
shows "length xs = length ys" |
53374
a14d2a854c02
tuned proofs -- clarified flow of facts wrt. calculation;
wenzelm
parents:
53017
diff
changeset
|
1143 |
using assms |
a14d2a854c02
tuned proofs -- clarified flow of facts wrt. calculation;
wenzelm
parents:
53017
diff
changeset
|
1144 |
proof (induct ys arbitrary: xs) |
26734 | 1145 |
case Nil then show ?case by simp |
1146 |
next |
|
1147 |
case (Cons y ys) then obtain z zs where xs: "xs = z # zs" by auto |
|
35510 | 1148 |
from Cons xs have "map f zs = map g ys" by simp |
53374
a14d2a854c02
tuned proofs -- clarified flow of facts wrt. calculation;
wenzelm
parents:
53017
diff
changeset
|
1149 |
with Cons have "length zs = length ys" by blast |
26734 | 1150 |
with xs show ?case by simp |
1151 |
qed |
|
64963 | 1152 |
|
15110
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
1153 |
lemma map_inj_on: |
68709 | 1154 |
assumes map: "map f xs = map f ys" and inj: "inj_on f (set xs Un set ys)" |
1155 |
shows "xs = ys" |
|
1156 |
using map_eq_imp_length_eq [OF map] assms |
|
1157 |
proof (induct rule: list_induct2) |
|
1158 |
case (Cons x xs y ys) |
|
1159 |
then show ?case |
|
1160 |
by (auto intro: sym) |
|
1161 |
qed auto |
|
15110
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
1162 |
|
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
1163 |
lemma inj_on_map_eq_map: |
58807 | 1164 |
"inj_on f (set xs Un set ys) \<Longrightarrow> (map f xs = map f ys) = (xs = ys)" |
15110
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
1165 |
by(blast dest:map_inj_on) |
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
1166 |
|
13114 | 1167 |
lemma map_injective: |
58807 | 1168 |
"map f xs = map f ys ==> inj f ==> xs = ys" |
24526 | 1169 |
by (induct ys arbitrary: xs) (auto dest!:injD) |
13114 | 1170 |
|
14339 | 1171 |
lemma inj_map_eq_map[simp]: "inj f \<Longrightarrow> (map f xs = map f ys) = (xs = ys)" |
1172 |
by(blast dest:map_injective) |
|
1173 |
||
13114 | 1174 |
lemma inj_mapI: "inj f ==> inj (map f)" |
17589 | 1175 |
by (iprover dest: map_injective injD intro: inj_onI) |
13114 | 1176 |
|
1177 |
lemma inj_mapD: "inj (map f) ==> inj f" |
|
68709 | 1178 |
by (metis (no_types, hide_lams) injI list.inject list.simps(9) the_inv_f_f) |
13114 | 1179 |
|
14339 | 1180 |
lemma inj_map[iff]: "inj (map f) = inj f" |
13145 | 1181 |
by (blast dest: inj_mapD intro: inj_mapI) |
13114 | 1182 |
|
15303 | 1183 |
lemma inj_on_mapI: "inj_on f (\<Union>(set ` A)) \<Longrightarrow> inj_on (map f) A" |
68709 | 1184 |
by (blast intro:inj_onI dest:inj_onD map_inj_on) |
15303 | 1185 |
|
14343 | 1186 |
lemma map_idI: "(\<And>x. x \<in> set xs \<Longrightarrow> f x = x) \<Longrightarrow> map f xs = xs" |
1187 |
by (induct xs, auto) |
|
13114 | 1188 |
|
14402
4201e1916482
moved lemmas from MicroJava/Comp/AuxLemmas.thy to List.thy
nipkow
parents:
14395
diff
changeset
|
1189 |
lemma map_fun_upd [simp]: "y \<notin> set xs \<Longrightarrow> map (f(y:=v)) xs = map f xs" |
4201e1916482
moved lemmas from MicroJava/Comp/AuxLemmas.thy to List.thy
nipkow
parents:
14395
diff
changeset
|
1190 |
by (induct xs) auto |
4201e1916482
moved lemmas from MicroJava/Comp/AuxLemmas.thy to List.thy
nipkow
parents:
14395
diff
changeset
|
1191 |
|
15110
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
1192 |
lemma map_fst_zip[simp]: |
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
1193 |
"length xs = length ys \<Longrightarrow> map fst (zip xs ys) = xs" |
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
1194 |
by (induct rule:list_induct2, simp_all) |
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
1195 |
|
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
1196 |
lemma map_snd_zip[simp]: |
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
1197 |
"length xs = length ys \<Longrightarrow> map snd (zip xs ys) = ys" |
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
1198 |
by (induct rule:list_induct2, simp_all) |
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
1199 |
|
68215 | 1200 |
lemma map_fst_zip_take: |
1201 |
"map fst (zip xs ys) = take (min (length xs) (length ys)) xs" |
|
1202 |
by (induct xs ys rule: list_induct2') simp_all |
|
1203 |
||
1204 |
lemma map_snd_zip_take: |
|
1205 |
"map snd (zip xs ys) = take (min (length xs) (length ys)) ys" |
|
1206 |
by (induct xs ys rule: list_induct2') simp_all |
|
1207 |
||
66853 | 1208 |
lemma map2_map_map: "map2 h (map f xs) (map g xs) = map (\<lambda>x. h (f x) (g x)) xs" |
1209 |
by (induction xs) (auto) |
|
1210 |
||
55467
a5c9002bc54d
renamed 'enriched_type' to more informative 'functor' (following the renaming of enriched type constructors to bounded natural functors)
blanchet
parents:
55466
diff
changeset
|
1211 |
functor map: map |
47122 | 1212 |
by (simp_all add: id_def) |
1213 |
||
49948
744934b818c7
moved quite generic material from theory Enum to more appropriate places
haftmann
parents:
49808
diff
changeset
|
1214 |
declare map.id [simp] |
744934b818c7
moved quite generic material from theory Enum to more appropriate places
haftmann
parents:
49808
diff
changeset
|
1215 |
|
744934b818c7
moved quite generic material from theory Enum to more appropriate places
haftmann
parents:
49808
diff
changeset
|
1216 |
|
69593 | 1217 |
subsubsection \<open>\<^const>\<open>rev\<close>\<close> |
13114 | 1218 |
|
13142 | 1219 |
lemma rev_append [simp]: "rev (xs @ ys) = rev ys @ rev xs" |
13145 | 1220 |
by (induct xs) auto |
13114 | 1221 |
|
13142 | 1222 |
lemma rev_rev_ident [simp]: "rev (rev xs) = xs" |
13145 | 1223 |
by (induct xs) auto |
13114 | 1224 |
|
15870 | 1225 |
lemma rev_swap: "(rev xs = ys) = (xs = rev ys)" |
1226 |
by auto |
|
1227 |
||
13142 | 1228 |
lemma rev_is_Nil_conv [iff]: "(rev xs = []) = (xs = [])" |
13145 | 1229 |
by (induct xs) auto |
13114 | 1230 |
|
13142 | 1231 |
lemma Nil_is_rev_conv [iff]: "([] = rev xs) = (xs = [])" |
13145 | 1232 |
by (induct xs) auto |
13114 | 1233 |
|
15870 | 1234 |
lemma rev_singleton_conv [simp]: "(rev xs = [x]) = (xs = [x])" |
1235 |
by (cases xs) auto |
|
1236 |
||
1237 |
lemma singleton_rev_conv [simp]: "([x] = rev xs) = (xs = [x])" |
|
1238 |
by (cases xs) auto |
|
1239 |
||
54147
97a8ff4e4ac9
killed most "no_atp", to make Sledgehammer more complete
blanchet
parents:
53954
diff
changeset
|
1240 |
lemma rev_is_rev_conv [iff]: "(rev xs = rev ys) = (xs = ys)" |
69850 | 1241 |
proof (induct xs arbitrary: ys) |
1242 |
case Nil |
|
1243 |
then show ?case by force |
|
1244 |
next |
|
1245 |
case Cons |
|
1246 |
then show ?case by (cases ys) auto |
|
1247 |
qed |
|
13114 | 1248 |
|
15439 | 1249 |
lemma inj_on_rev[iff]: "inj_on rev A" |
1250 |
by(simp add:inj_on_def) |
|
1251 |
||
13366 | 1252 |
lemma rev_induct [case_names Nil snoc]: |
1253 |
"[| P []; !!x xs. P xs ==> P (xs @ [x]) |] ==> P xs" |
|
68723
60611540bcff
recovered HOL-Proofs-Lambda from 8aedca31957d: avoid problems with program extraction according to d136af442665;
wenzelm
parents:
68719
diff
changeset
|
1254 |
apply(simplesubst rev_rev_ident[symmetric]) |
13145 | 1255 |
apply(rule_tac list = "rev xs" in list.induct, simp_all) |
1256 |
done |
|
13114 | 1257 |
|
13366 | 1258 |
lemma rev_exhaust [case_names Nil snoc]: |
1259 |
"(xs = [] ==> P) ==>(!!ys y. xs = ys @ [y] ==> P) ==> P" |
|
13145 | 1260 |
by (induct xs rule: rev_induct) auto |
13114 | 1261 |
|
13366 | 1262 |
lemmas rev_cases = rev_exhaust |
1263 |
||
57577 | 1264 |
lemma rev_nonempty_induct [consumes 1, case_names single snoc]: |
1265 |
assumes "xs \<noteq> []" |
|
1266 |
and single: "\<And>x. P [x]" |
|
1267 |
and snoc': "\<And>x xs. xs \<noteq> [] \<Longrightarrow> P xs \<Longrightarrow> P (xs@[x])" |
|
1268 |
shows "P xs" |
|
60758 | 1269 |
using \<open>xs \<noteq> []\<close> proof (induct xs rule: rev_induct) |
57577 | 1270 |
case (snoc x xs) then show ?case |
1271 |
proof (cases xs) |
|
1272 |
case Nil thus ?thesis by (simp add: single) |
|
1273 |
next |
|
1274 |
case Cons with snoc show ?thesis by (fastforce intro!: snoc') |
|
1275 |
qed |
|
1276 |
qed simp |
|
1277 |
||
18423 | 1278 |
lemma rev_eq_Cons_iff[iff]: "(rev xs = y#ys) = (xs = rev ys @ [y])" |
1279 |
by(rule rev_cases[of xs]) auto |
|
1280 |
||
13114 | 1281 |
|
69593 | 1282 |
subsubsection \<open>\<^const>\<open>set\<close>\<close> |
13114 | 1283 |
|
67443
3abf6a722518
standardized towards new-style formal comments: isabelle update_comments;
wenzelm
parents:
67399
diff
changeset
|
1284 |
declare list.set[code_post] \<comment> \<open>pretty output\<close> |
57816
d8bbb97689d3
no need for 'set_simps' now that 'datatype_new' generates the desired 'set' property
blanchet
parents:
57599
diff
changeset
|
1285 |
|
13142 | 1286 |
lemma finite_set [iff]: "finite (set xs)" |
13145 | 1287 |
by (induct xs) auto |
13114 | 1288 |
|
13142 | 1289 |
lemma set_append [simp]: "set (xs @ ys) = (set xs \<union> set ys)" |
13145 | 1290 |
by (induct xs) auto |
13114 | 1291 |
|
67613 | 1292 |
lemma hd_in_set[simp]: "xs \<noteq> [] \<Longrightarrow> hd xs \<in> set xs" |
17830 | 1293 |
by(cases xs) auto |
14099 | 1294 |
|
13142 | 1295 |
lemma set_subset_Cons: "set xs \<subseteq> set (x # xs)" |
13145 | 1296 |
by auto |
13114 | 1297 |
|
64963 | 1298 |
lemma set_ConsD: "y \<in> set (x # xs) \<Longrightarrow> y=x \<or> y \<in> set xs" |
14099 | 1299 |
by auto |
1300 |
||
13142 | 1301 |
lemma set_empty [iff]: "(set xs = {}) = (xs = [])" |
13145 | 1302 |
by (induct xs) auto |
13114 | 1303 |
|
15245 | 1304 |
lemma set_empty2[iff]: "({} = set xs) = (xs = [])" |
1305 |
by(induct xs) auto |
|
1306 |
||
13142 | 1307 |
lemma set_rev [simp]: "set (rev xs) = set xs" |
13145 | 1308 |
by (induct xs) auto |
13114 | 1309 |
|
13142 | 1310 |
lemma set_map [simp]: "set (map f xs) = f`(set xs)" |
13145 | 1311 |
by (induct xs) auto |
13114 | 1312 |
|
67613 | 1313 |
lemma set_filter [simp]: "set (filter P xs) = {x. x \<in> set xs \<and> P x}" |
13145 | 1314 |
by (induct xs) auto |
13114 | 1315 |
|
32417 | 1316 |
lemma set_upt [simp]: "set[i..<j] = {i..<j}" |
41463
edbf0a86fb1c
adding simproc to rewrite list comprehensions to set comprehensions; adopting proofs
bulwahn
parents:
41372
diff
changeset
|
1317 |
by (induct j) auto |
13114 | 1318 |
|
13142 | 1319 |
|
67613 | 1320 |
lemma split_list: "x \<in> set xs \<Longrightarrow> \<exists>ys zs. xs = ys @ x # zs" |
18049 | 1321 |
proof (induct xs) |
26073 | 1322 |
case Nil thus ?case by simp |
1323 |
next |
|
1324 |
case Cons thus ?case by (auto intro: Cons_eq_appendI) |
|
1325 |
qed |
|
1326 |
||
26734 | 1327 |
lemma in_set_conv_decomp: "x \<in> set xs \<longleftrightarrow> (\<exists>ys zs. xs = ys @ x # zs)" |
1328 |
by (auto elim: split_list) |
|
26073 | 1329 |
|
67613 | 1330 |
lemma split_list_first: "x \<in> set xs \<Longrightarrow> \<exists>ys zs. xs = ys @ x # zs \<and> x \<notin> set ys" |
26073 | 1331 |
proof (induct xs) |
1332 |
case Nil thus ?case by simp |
|
18049 | 1333 |
next |
1334 |
case (Cons a xs) |
|
1335 |
show ?case |
|
1336 |
proof cases |
|
44890
22f665a2e91c
new fastforce replacing fastsimp - less confusing name
nipkow
parents:
44635
diff
changeset
|
1337 |
assume "x = a" thus ?case using Cons by fastforce |
18049 | 1338 |
next |
44890
22f665a2e91c
new fastforce replacing fastsimp - less confusing name
nipkow
parents:
44635
diff
changeset
|
1339 |
assume "x \<noteq> a" thus ?case using Cons by(fastforce intro!: Cons_eq_appendI) |
26073 | 1340 |
qed |
1341 |
qed |
|
1342 |
||
1343 |
lemma in_set_conv_decomp_first: |
|
67613 | 1344 |
"(x \<in> set xs) = (\<exists>ys zs. xs = ys @ x # zs \<and> x \<notin> set ys)" |
26734 | 1345 |
by (auto dest!: split_list_first) |
26073 | 1346 |
|
40122
1d8ad2ff3e01
dropped (almost) redundant distinct.induct rule; distinct_simps again named distinct.simps
haftmann
parents:
40077
diff
changeset
|
1347 |
lemma split_list_last: "x \<in> set xs \<Longrightarrow> \<exists>ys zs. xs = ys @ x # zs \<and> x \<notin> set zs" |
1d8ad2ff3e01
dropped (almost) redundant distinct.induct rule; distinct_simps again named distinct.simps
haftmann
parents:
40077
diff
changeset
|
1348 |
proof (induct xs rule: rev_induct) |
26073 | 1349 |
case Nil thus ?case by simp |
1350 |
next |
|
1351 |
case (snoc a xs) |
|
1352 |
show ?case |
|
1353 |
proof cases |
|
56085 | 1354 |
assume "x = a" thus ?case using snoc by (auto intro!: exI) |
26073 | 1355 |
next |
44890
22f665a2e91c
new fastforce replacing fastsimp - less confusing name
nipkow
parents:
44635
diff
changeset
|
1356 |
assume "x \<noteq> a" thus ?case using snoc by fastforce |
18049 | 1357 |
qed |
1358 |
qed |
|
1359 |
||
26073 | 1360 |
lemma in_set_conv_decomp_last: |
67613 | 1361 |
"(x \<in> set xs) = (\<exists>ys zs. xs = ys @ x # zs \<and> x \<notin> set zs)" |
26734 | 1362 |
by (auto dest!: split_list_last) |
26073 | 1363 |
|
67091 | 1364 |
lemma split_list_prop: "\<exists>x \<in> set xs. P x \<Longrightarrow> \<exists>ys x zs. xs = ys @ x # zs \<and> P x" |
26073 | 1365 |
proof (induct xs) |
1366 |
case Nil thus ?case by simp |
|
1367 |
next |
|
1368 |
case Cons thus ?case |
|
1369 |
by(simp add:Bex_def)(metis append_Cons append.simps(1)) |
|
1370 |
qed |
|
1371 |
||
1372 |
lemma split_list_propE: |
|
26734 | 1373 |
assumes "\<exists>x \<in> set xs. P x" |
1374 |
obtains ys x zs where "xs = ys @ x # zs" and "P x" |
|
1375 |
using split_list_prop [OF assms] by blast |
|
26073 | 1376 |
|
1377 |
lemma split_list_first_prop: |
|
1378 |
"\<exists>x \<in> set xs. P x \<Longrightarrow> |
|
1379 |
\<exists>ys x zs. xs = ys@x#zs \<and> P x \<and> (\<forall>y \<in> set ys. \<not> P y)" |
|
26734 | 1380 |
proof (induct xs) |
26073 | 1381 |
case Nil thus ?case by simp |
1382 |
next |
|
1383 |
case (Cons x xs) |
|
1384 |
show ?case |
|
1385 |
proof cases |
|
1386 |
assume "P x" |
|
56085 | 1387 |
hence "x # xs = [] @ x # xs \<and> P x \<and> (\<forall>y\<in>set []. \<not> P y)" by simp |
1388 |
thus ?thesis by fast |
|
26073 | 1389 |
next |
1390 |
assume "\<not> P x" |
|
1391 |
hence "\<exists>x\<in>set xs. P x" using Cons(2) by simp |
|
60758 | 1392 |
thus ?thesis using \<open>\<not> P x\<close> Cons(1) by (metis append_Cons set_ConsD) |
26073 | 1393 |
qed |
1394 |
qed |
|
1395 |
||
1396 |
lemma split_list_first_propE: |
|
26734 | 1397 |
assumes "\<exists>x \<in> set xs. P x" |
1398 |
obtains ys x zs where "xs = ys @ x # zs" and "P x" and "\<forall>y \<in> set ys. \<not> P y" |
|
1399 |
using split_list_first_prop [OF assms] by blast |
|
26073 | 1400 |
|
1401 |
lemma split_list_first_prop_iff: |
|
1402 |
"(\<exists>x \<in> set xs. P x) \<longleftrightarrow> |
|
1403 |
(\<exists>ys x zs. xs = ys@x#zs \<and> P x \<and> (\<forall>y \<in> set ys. \<not> P y))" |
|
26734 | 1404 |
by (rule, erule split_list_first_prop) auto |
26073 | 1405 |
|
1406 |
lemma split_list_last_prop: |
|
1407 |
"\<exists>x \<in> set xs. P x \<Longrightarrow> |
|
1408 |
\<exists>ys x zs. xs = ys@x#zs \<and> P x \<and> (\<forall>z \<in> set zs. \<not> P z)" |
|
1409 |
proof(induct xs rule:rev_induct) |
|
1410 |
case Nil thus ?case by simp |
|
1411 |
next |
|
1412 |
case (snoc x xs) |
|
1413 |
show ?case |
|
1414 |
proof cases |
|
56085 | 1415 |
assume "P x" thus ?thesis by (auto intro!: exI) |
26073 | 1416 |
next |
1417 |
assume "\<not> P x" |
|
1418 |
hence "\<exists>x\<in>set xs. P x" using snoc(2) by simp |
|
60758 | 1419 |
thus ?thesis using \<open>\<not> P x\<close> snoc(1) by fastforce |
26073 | 1420 |
qed |
1421 |
qed |
|
1422 |
||
1423 |
lemma split_list_last_propE: |
|
26734 | 1424 |
assumes "\<exists>x \<in> set xs. P x" |
1425 |
obtains ys x zs where "xs = ys @ x # zs" and "P x" and "\<forall>z \<in> set zs. \<not> P z" |
|
1426 |
using split_list_last_prop [OF assms] by blast |
|
26073 | 1427 |
|
1428 |
lemma split_list_last_prop_iff: |
|
1429 |
"(\<exists>x \<in> set xs. P x) \<longleftrightarrow> |
|
1430 |
(\<exists>ys x zs. xs = ys@x#zs \<and> P x \<and> (\<forall>z \<in> set zs. \<not> P z))" |
|
56085 | 1431 |
by rule (erule split_list_last_prop, auto) |
1432 |
||
26073 | 1433 |
|
67091 | 1434 |
lemma finite_list: "finite A \<Longrightarrow> \<exists>xs. set xs = A" |
57816
d8bbb97689d3
no need for 'set_simps' now that 'datatype_new' generates the desired 'set' property
blanchet
parents:
57599
diff
changeset
|
1435 |
by (erule finite_induct) (auto simp add: list.set(2)[symmetric] simp del: list.set(2)) |
13508 | 1436 |
|
14388 | 1437 |
lemma card_length: "card (set xs) \<le> length xs" |
1438 |
by (induct xs) (auto simp add: card_insert_if) |
|
13114 | 1439 |
|
26442
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
haftmann
parents:
26300
diff
changeset
|
1440 |
lemma set_minus_filter_out: |
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
haftmann
parents:
26300
diff
changeset
|
1441 |
"set xs - {y} = set (filter (\<lambda>x. \<not> (x = y)) xs)" |
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
haftmann
parents:
26300
diff
changeset
|
1442 |
by (induct xs) auto |
15168 | 1443 |
|
66257 | 1444 |
lemma append_Cons_eq_iff: |
1445 |
"\<lbrakk> x \<notin> set xs; x \<notin> set ys \<rbrakk> \<Longrightarrow> |
|
1446 |
xs @ x # ys = xs' @ x # ys' \<longleftrightarrow> (xs = xs' \<and> ys = ys')" |
|
1447 |
by(auto simp: append_eq_Cons_conv Cons_eq_append_conv append_eq_append_conv2) |
|
1448 |
||
35115 | 1449 |
|
69593 | 1450 |
subsubsection \<open>\<^const>\<open>filter\<close>\<close> |
13114 | 1451 |
|
13142 | 1452 |
lemma filter_append [simp]: "filter P (xs @ ys) = filter P xs @ filter P ys" |
13145 | 1453 |
by (induct xs) auto |
13114 | 1454 |
|
15305 | 1455 |
lemma rev_filter: "rev (filter P xs) = filter P (rev xs)" |
1456 |
by (induct xs) simp_all |
|
1457 |
||
13142 | 1458 |
lemma filter_filter [simp]: "filter P (filter Q xs) = filter (\<lambda>x. Q x \<and> P x) xs" |
13145 | 1459 |
by (induct xs) auto |
13114 | 1460 |
|
16998 | 1461 |
lemma length_filter_le [simp]: "length (filter P xs) \<le> length xs" |
1462 |
by (induct xs) (auto simp add: le_SucI) |
|
1463 |
||
18423 | 1464 |
lemma sum_length_filter_compl: |
67091 | 1465 |
"length(filter P xs) + length(filter (\<lambda>x. \<not>P x) xs) = length xs" |
18423 | 1466 |
by(induct xs) simp_all |
1467 |
||
13142 | 1468 |
lemma filter_True [simp]: "\<forall>x \<in> set xs. P x ==> filter P xs = xs" |
13145 | 1469 |
by (induct xs) auto |
13114 | 1470 |
|
13142 | 1471 |
lemma filter_False [simp]: "\<forall>x \<in> set xs. \<not> P x ==> filter P xs = []" |
13145 | 1472 |
by (induct xs) auto |
13114 | 1473 |
|
64963 | 1474 |
lemma filter_empty_conv: "(filter P xs = []) = (\<forall>x\<in>set xs. \<not> P x)" |
24349 | 1475 |
by (induct xs) simp_all |
16998 | 1476 |
|
1477 |
lemma filter_id_conv: "(filter P xs = xs) = (\<forall>x\<in>set xs. P x)" |
|
68709 | 1478 |
proof (induct xs) |
1479 |
case (Cons x xs) |
|
1480 |
then show ?case |
|
1481 |
using length_filter_le |
|
1482 |
by (simp add: impossible_Cons) |
|
1483 |
qed auto |
|
13114 | 1484 |
|
67091 | 1485 |
lemma filter_map: "filter P (map f xs) = map f (filter (P \<circ> f) xs)" |
16965 | 1486 |
by (induct xs) simp_all |
1487 |
||
1488 |
lemma length_filter_map[simp]: |
|
67091 | 1489 |
"length (filter P (map f xs)) = length(filter (P \<circ> f) xs)" |
16965 | 1490 |
by (simp add:filter_map) |
1491 |
||
13142 | 1492 |
lemma filter_is_subset [simp]: "set (filter P xs) \<le> set xs" |
13145 | 1493 |
by auto |
13114 | 1494 |
|
15246 | 1495 |
lemma length_filter_less: |
67091 | 1496 |
"\<lbrakk> x \<in> set xs; \<not> P x \<rbrakk> \<Longrightarrow> length(filter P xs) < length xs" |
15246 | 1497 |
proof (induct xs) |
1498 |
case Nil thus ?case by simp |
|
1499 |
next |
|
1500 |
case (Cons x xs) thus ?case |
|
68719 | 1501 |
using Suc_le_eq by fastforce |
15246 | 1502 |
qed |
13114 | 1503 |
|
15281 | 1504 |
lemma length_filter_conv_card: |
67091 | 1505 |
"length(filter p xs) = card{i. i < length xs \<and> p(xs!i)}" |
15281 | 1506 |
proof (induct xs) |
1507 |
case Nil thus ?case by simp |
|
1508 |
next |
|
1509 |
case (Cons x xs) |
|
67091 | 1510 |
let ?S = "{i. i < length xs \<and> p(xs!i)}" |
15281 | 1511 |
have fin: "finite ?S" by(fast intro: bounded_nat_set_is_finite) |
1512 |
show ?case (is "?l = card ?S'") |
|
1513 |
proof (cases) |
|
1514 |
assume "p x" |
|
1515 |
hence eq: "?S' = insert 0 (Suc ` ?S)" |
|
25162 | 1516 |
by(auto simp: image_def split:nat.split dest:gr0_implies_Suc) |
15281 | 1517 |
have "length (filter p (x # xs)) = Suc(card ?S)" |
60758 | 1518 |
using Cons \<open>p x\<close> by simp |
15281 | 1519 |
also have "\<dots> = Suc(card(Suc ` ?S))" using fin |
44921 | 1520 |
by (simp add: card_image) |
15281 | 1521 |
also have "\<dots> = card ?S'" using eq fin |
69700
7a92cbec7030
new material about summations and powers, along with some tweaks
paulson <lp15@cam.ac.uk>
parents:
69593
diff
changeset
|
1522 |
by (simp add:card_insert_if) |
15281 | 1523 |
finally show ?thesis . |
1524 |
next |
|
1525 |
assume "\<not> p x" |
|
1526 |
hence eq: "?S' = Suc ` ?S" |
|
25162 | 1527 |
by(auto simp add: image_def split:nat.split elim:lessE) |
15281 | 1528 |
have "length (filter p (x # xs)) = card ?S" |
60758 | 1529 |
using Cons \<open>\<not> p x\<close> by simp |
15281 | 1530 |
also have "\<dots> = card(Suc ` ?S)" using fin |
44921 | 1531 |
by (simp add: card_image) |
15281 | 1532 |
also have "\<dots> = card ?S'" using eq fin |
1533 |
by (simp add:card_insert_if) |
|
1534 |
finally show ?thesis . |
|
1535 |
qed |
|
1536 |
qed |
|
1537 |
||
17629 | 1538 |
lemma Cons_eq_filterD: |
58807 | 1539 |
"x#xs = filter P ys \<Longrightarrow> |
17629 | 1540 |
\<exists>us vs. ys = us @ x # vs \<and> (\<forall>u\<in>set us. \<not> P u) \<and> P x \<and> xs = filter P vs" |
19585 | 1541 |
(is "_ \<Longrightarrow> \<exists>us vs. ?P ys us vs") |
17629 | 1542 |
proof(induct ys) |
1543 |
case Nil thus ?case by simp |
|
1544 |
next |
|
1545 |
case (Cons y ys) |
|
1546 |
show ?case (is "\<exists>x. ?Q x") |
|
1547 |
proof cases |
|
1548 |
assume Py: "P y" |
|
1549 |
show ?thesis |
|
1550 |
proof cases |
|
25221
5ded95dda5df
append/member: more light-weight way to declare authentic syntax;
wenzelm
parents:
25215
diff
changeset
|
1551 |
assume "x = y" |
5ded95dda5df
append/member: more light-weight way to declare authentic syntax;
wenzelm
parents:
25215
diff
changeset
|
1552 |
with Py Cons.prems have "?Q []" by simp |
5ded95dda5df
append/member: more light-weight way to declare authentic syntax;
wenzelm
parents:
25215
diff
changeset
|
1553 |
then show ?thesis .. |
17629 | 1554 |
next |
25221
5ded95dda5df
append/member: more light-weight way to declare authentic syntax;
wenzelm
parents:
25215
diff
changeset
|
1555 |
assume "x \<noteq> y" |
5ded95dda5df
append/member: more light-weight way to declare authentic syntax;
wenzelm
parents:
25215
diff
changeset
|
1556 |
with Py Cons.prems show ?thesis by simp |
17629 | 1557 |
qed |
1558 |
next |
|
25221
5ded95dda5df
append/member: more light-weight way to declare authentic syntax;
wenzelm
parents:
25215
diff
changeset
|
1559 |
assume "\<not> P y" |
44890
22f665a2e91c
new fastforce replacing fastsimp - less confusing name
nipkow
parents:
44635
diff
changeset
|
1560 |
with Cons obtain us vs where "?P (y#ys) (y#us) vs" by fastforce |
25221
5ded95dda5df
append/member: more light-weight way to declare authentic syntax;
wenzelm
parents:
25215
diff
changeset
|
1561 |
then have "?Q (y#us)" by simp |
5ded95dda5df
append/member: more light-weight way to declare authentic syntax;
wenzelm
parents:
25215
diff
changeset
|
1562 |
then show ?thesis .. |
17629 | 1563 |
qed |
1564 |
qed |
|
1565 |
||
1566 |
lemma filter_eq_ConsD: |
|
58807 | 1567 |
"filter P ys = x#xs \<Longrightarrow> |
17629 | 1568 |
\<exists>us vs. ys = us @ x # vs \<and> (\<forall>u\<in>set us. \<not> P u) \<and> P x \<and> xs = filter P vs" |
68719 | 1569 |
by(rule Cons_eq_filterD) simp |
17629 | 1570 |
|
1571 |
lemma filter_eq_Cons_iff: |
|
58807 | 1572 |
"(filter P ys = x#xs) = |
17629 | 1573 |
(\<exists>us vs. ys = us @ x # vs \<and> (\<forall>u\<in>set us. \<not> P u) \<and> P x \<and> xs = filter P vs)" |
68719 | 1574 |
by(auto dest:filter_eq_ConsD) |
17629 | 1575 |
|
1576 |
lemma Cons_eq_filter_iff: |
|
58807 | 1577 |
"(x#xs = filter P ys) = |
17629 | 1578 |
(\<exists>us vs. ys = us @ x # vs \<and> (\<forall>u\<in>set us. \<not> P u) \<and> P x \<and> xs = filter P vs)" |
68719 | 1579 |
by(auto dest:Cons_eq_filterD) |
17629 | 1580 |
|
61031
162bd20dae23
more lemmas on sorting and multisets (due to Thomas Sewell)
haftmann
parents:
60758
diff
changeset
|
1581 |
lemma inj_on_filter_key_eq: |
162bd20dae23
more lemmas on sorting and multisets (due to Thomas Sewell)
haftmann
parents:
60758
diff
changeset
|
1582 |
assumes "inj_on f (insert y (set xs))" |
68249
949d93804740
First step to remove nonstandard "[x <- xs. P]" syntax: only input
nipkow
parents:
68215
diff
changeset
|
1583 |
shows "filter (\<lambda>x. f y = f x) xs = filter (HOL.eq y) xs" |
61031
162bd20dae23
more lemmas on sorting and multisets (due to Thomas Sewell)
haftmann
parents:
60758
diff
changeset
|
1584 |
using assms by (induct xs) auto |
162bd20dae23
more lemmas on sorting and multisets (due to Thomas Sewell)
haftmann
parents:
60758
diff
changeset
|
1585 |
|
44013
5cfc1c36ae97
moved recdef package to HOL/Library/Old_Recdef.thy
krauss
parents:
43594
diff
changeset
|
1586 |
lemma filter_cong[fundef_cong]: |
58807 | 1587 |
"xs = ys \<Longrightarrow> (\<And>x. x \<in> set ys \<Longrightarrow> P x = Q x) \<Longrightarrow> filter P xs = filter Q ys" |
68709 | 1588 |
by (induct ys arbitrary: xs) auto |
17501 | 1589 |
|
15281 | 1590 |
|
60758 | 1591 |
subsubsection \<open>List partitioning\<close> |
26442
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
haftmann
parents:
26300
diff
changeset
|
1592 |
|
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
haftmann
parents:
26300
diff
changeset
|
1593 |
primrec partition :: "('a \<Rightarrow> bool) \<Rightarrow>'a list \<Rightarrow> 'a list \<times> 'a list" where |
68719 | 1594 |
"partition P [] = ([], [])" | |
1595 |
"partition P (x # xs) = |
|
50548 | 1596 |
(let (yes, no) = partition P xs |
1597 |
in if P x then (x # yes, no) else (yes, x # no))" |
|
26442
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
haftmann
parents:
26300
diff
changeset
|
1598 |
|
58807 | 1599 |
lemma partition_filter1: "fst (partition P xs) = filter P xs" |
68719 | 1600 |
by (induct xs) (auto simp add: Let_def split_def) |
26442
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
haftmann
parents:
26300
diff
changeset
|
1601 |
|
67091 | 1602 |
lemma partition_filter2: "snd (partition P xs) = filter (Not \<circ> P) xs" |
68719 | 1603 |
by (induct xs) (auto simp add: Let_def split_def) |
26442
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
haftmann
parents:
26300
diff
changeset
|
1604 |
|
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
haftmann
parents:
26300
diff
changeset
|
1605 |
lemma partition_P: |
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
haftmann
parents:
26300
diff
changeset
|
1606 |
assumes "partition P xs = (yes, no)" |
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
haftmann
parents:
26300
diff
changeset
|
1607 |
shows "(\<forall>p \<in> set yes. P p) \<and> (\<forall>p \<in> set no. \<not> P p)" |
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
haftmann
parents:
26300
diff
changeset
|
1608 |
proof - |
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
haftmann
parents:
26300
diff
changeset
|
1609 |
from assms have "yes = fst (partition P xs)" and "no = snd (partition P xs)" |
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
haftmann
parents:
26300
diff
changeset
|
1610 |
by simp_all |
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
haftmann
parents:
26300
diff
changeset
|
1611 |
then show ?thesis by (simp_all add: partition_filter1 partition_filter2) |
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
haftmann
parents:
26300
diff
changeset
|
1612 |
qed |
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
haftmann
parents:
26300
diff
changeset
|
1613 |
|
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
haftmann
parents:
26300
diff
changeset
|
1614 |
lemma partition_set: |
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
haftmann
parents:
26300
diff
changeset
|
1615 |
assumes "partition P xs = (yes, no)" |
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
haftmann
parents:
26300
diff
changeset
|
1616 |
shows "set yes \<union> set no = set xs" |
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
haftmann
parents:
26300
diff
changeset
|
1617 |
proof - |
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
haftmann
parents:
26300
diff
changeset
|
1618 |
from assms have "yes = fst (partition P xs)" and "no = snd (partition P xs)" |
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
haftmann
parents:
26300
diff
changeset
|
1619 |
by simp_all |
64963 | 1620 |
then show ?thesis by (auto simp add: partition_filter1 partition_filter2) |
26442
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
haftmann
parents:
26300
diff
changeset
|
1621 |
qed |
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
haftmann
parents:
26300
diff
changeset
|
1622 |
|
33639
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
hoelzl
parents:
33593
diff
changeset
|
1623 |
lemma partition_filter_conv[simp]: |
67091 | 1624 |
"partition f xs = (filter f xs,filter (Not \<circ> f) xs)" |
68719 | 1625 |
unfolding partition_filter2[symmetric] |
1626 |
unfolding partition_filter1[symmetric] by simp |
|
33639
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
hoelzl
parents:
33593
diff
changeset
|
1627 |
|
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
hoelzl
parents:
33593
diff
changeset
|
1628 |
declare partition.simps[simp del] |
26442
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
haftmann
parents:
26300
diff
changeset
|
1629 |
|
35115 | 1630 |
|
69593 | 1631 |
subsubsection \<open>\<^const>\<open>concat\<close>\<close> |
13114 | 1632 |
|
13142 | 1633 |
lemma concat_append [simp]: "concat (xs @ ys) = concat xs @ concat ys" |
68719 | 1634 |
by (induct xs) auto |
13114 | 1635 |
|
18447 | 1636 |
lemma concat_eq_Nil_conv [simp]: "(concat xss = []) = (\<forall>xs \<in> set xss. xs = [])" |
68719 | 1637 |
by (induct xss) auto |
13114 | 1638 |
|
18447 | 1639 |
lemma Nil_eq_concat_conv [simp]: "([] = concat xss) = (\<forall>xs \<in> set xss. xs = [])" |
68719 | 1640 |
by (induct xss) auto |
13114 | 1641 |
|
69276 | 1642 |
lemma set_concat [simp]: "set (concat xs) = (\<Union>x\<in>set xs. set x)" |
68719 | 1643 |
by (induct xs) auto |
13114 | 1644 |
|
24476
f7ad9fbbeeaa
turned list comprehension translations into ML to optimize base case
nipkow
parents:
24471
diff
changeset
|
1645 |
lemma concat_map_singleton[simp]: "concat(map (%x. [f x]) xs) = map f xs" |
68719 | 1646 |
by (induct xs) auto |
24349 | 1647 |
|
13142 | 1648 |
lemma map_concat: "map f (concat xs) = concat (map (map f) xs)" |
68719 | 1649 |
by (induct xs) auto |
13114 | 1650 |
|
13142 | 1651 |
lemma filter_concat: "filter p (concat xs) = concat (map (filter p) xs)" |
68719 | 1652 |
by (induct xs) auto |
13114 | 1653 |
|
13142 | 1654 |
lemma rev_concat: "rev (concat xs) = concat (map rev (rev xs))" |
68719 | 1655 |
by (induct xs) auto |
13114 | 1656 |
|
40365
a1456f2e1c9d
added two lemmas about injectivity of concat to the list theory
bulwahn
parents:
40304
diff
changeset
|
1657 |
lemma concat_eq_concat_iff: "\<forall>(x, y) \<in> set (zip xs ys). length x = length y ==> length xs = length ys ==> (concat xs = concat ys) = (xs = ys)" |
a1456f2e1c9d
added two lemmas about injectivity of concat to the list theory
bulwahn
parents:
40304
diff
changeset
|
1658 |
proof (induct xs arbitrary: ys) |
a1456f2e1c9d
added two lemmas about injectivity of concat to the list theory
bulwahn
parents:
40304
diff
changeset
|
1659 |
case (Cons x xs ys) |
a1456f2e1c9d
added two lemmas about injectivity of concat to the list theory
bulwahn
parents:
40304
diff
changeset
|
1660 |
thus ?case by (cases ys) auto |
a1456f2e1c9d
added two lemmas about injectivity of concat to the list theory
bulwahn
parents:
40304
diff
changeset
|
1661 |
qed (auto) |
a1456f2e1c9d
added two lemmas about injectivity of concat to the list theory
bulwahn
parents:
40304
diff
changeset
|
1662 |
|
a1456f2e1c9d
added two lemmas about injectivity of concat to the list theory
bulwahn
parents:
40304
diff
changeset
|
1663 |
lemma concat_injective: "concat xs = concat ys ==> length xs = length ys ==> \<forall>(x, y) \<in> set (zip xs ys). length x = length y ==> xs = ys" |
68719 | 1664 |
by (simp add: concat_eq_concat_iff) |
40365
a1456f2e1c9d
added two lemmas about injectivity of concat to the list theory
bulwahn
parents:
40304
diff
changeset
|
1665 |
|
13114 | 1666 |
|
69593 | 1667 |
subsubsection \<open>\<^const>\<open>nth\<close>\<close> |
13114 | 1668 |
|
29827 | 1669 |
lemma nth_Cons_0 [simp, code]: "(x # xs)!0 = x" |
68719 | 1670 |
by auto |
13114 | 1671 |
|
29827 | 1672 |
lemma nth_Cons_Suc [simp, code]: "(x # xs)!(Suc n) = xs!n" |
68719 | 1673 |
by auto |
13114 | 1674 |
|
13142 | 1675 |
declare nth.simps [simp del] |
13114 | 1676 |
|
41842 | 1677 |
lemma nth_Cons_pos[simp]: "0 < n \<Longrightarrow> (x#xs) ! n = xs ! (n - 1)" |
68719 | 1678 |
by(auto simp: Nat.gr0_conv_Suc) |
41842 | 1679 |
|
13114 | 1680 |
lemma nth_append: |
24526 | 1681 |
"(xs @ ys)!n = (if n < length xs then xs!n else ys!(n - length xs))" |
68709 | 1682 |
proof (induct xs arbitrary: n) |
1683 |
case (Cons x xs) |
|
1684 |
then show ?case |
|
1685 |
using less_Suc_eq_0_disj by auto |
|
1686 |
qed simp |
|
13114 | 1687 |
|
14402
4201e1916482
moved lemmas from MicroJava/Comp/AuxLemmas.thy to List.thy
nipkow
parents:
14395
diff
changeset
|
1688 |
lemma nth_append_length [simp]: "(xs @ x # ys) ! length xs = x" |
68719 | 1689 |
by (induct xs) auto |
14402
4201e1916482
moved lemmas from MicroJava/Comp/AuxLemmas.thy to List.thy
nipkow
parents:
14395
diff
changeset
|
1690 |
|
4201e1916482
moved lemmas from MicroJava/Comp/AuxLemmas.thy to List.thy
nipkow
parents:
14395
diff
changeset
|
1691 |
lemma nth_append_length_plus[simp]: "(xs @ ys) ! (length xs + n) = ys ! n" |
68719 | 1692 |
by (induct xs) auto |
14402
4201e1916482
moved lemmas from MicroJava/Comp/AuxLemmas.thy to List.thy
nipkow
parents:
14395
diff
changeset
|
1693 |
|
24526 | 1694 |
lemma nth_map [simp]: "n < length xs ==> (map f xs)!n = f(xs!n)" |
68709 | 1695 |
proof (induct xs arbitrary: n) |
1696 |
case (Cons x xs) |
|
1697 |
then show ?case |
|
1698 |
using less_Suc_eq_0_disj by auto |
|
1699 |
qed simp |
|
13114 | 1700 |
|
66847 | 1701 |
lemma nth_tl: "n < length (tl xs) \<Longrightarrow> tl xs ! n = xs ! Suc n" |
68719 | 1702 |
by (induction xs) auto |
45841 | 1703 |
|
18423 | 1704 |
lemma hd_conv_nth: "xs \<noteq> [] \<Longrightarrow> hd xs = xs!0" |
68719 | 1705 |
by(cases xs) simp_all |
18423 | 1706 |
|
18049 | 1707 |
|
1708 |
lemma list_eq_iff_nth_eq: |
|
67717 | 1709 |
"(xs = ys) = (length xs = length ys \<and> (\<forall>i<length xs. xs!i = ys!i))" |
68709 | 1710 |
proof (induct xs arbitrary: ys) |
1711 |
case (Cons x xs ys) |
|
1712 |
show ?case |
|
1713 |
proof (cases ys) |
|
1714 |
case (Cons y ys) |
|
1715 |
then show ?thesis |
|
1716 |
using Cons.hyps by fastforce |
|
1717 |
qed simp |
|
1718 |
qed force |
|
18049 | 1719 |
|
13142 | 1720 |
lemma set_conv_nth: "set xs = {xs!i | i. i < length xs}" |
68709 | 1721 |
proof (induct xs) |
1722 |
case (Cons x xs) |
|
1723 |
have "insert x {xs ! i |i. i < length xs} = {(x # xs) ! i |i. i < Suc (length xs)}" (is "?L=?R") |
|
1724 |
proof |
|
1725 |
show "?L \<subseteq> ?R" |
|
1726 |
by force |
|
1727 |
show "?R \<subseteq> ?L" |
|
1728 |
using less_Suc_eq_0_disj by auto |
|
1729 |
qed |
|
1730 |
with Cons show ?case |
|
1731 |
by simp |
|
1732 |
qed simp |
|
13114 | 1733 |
|
17501 | 1734 |
lemma in_set_conv_nth: "(x \<in> set xs) = (\<exists>i < length xs. xs!i = x)" |
68719 | 1735 |
by(auto simp:set_conv_nth) |
17501 | 1736 |
|
51160
599ff65b85e2
systematic conversions between nat and nibble/char;
haftmann
parents:
51112
diff
changeset
|
1737 |
lemma nth_equal_first_eq: |
599ff65b85e2
systematic conversions between nat and nibble/char;
haftmann
parents:
51112
diff
changeset
|
1738 |
assumes "x \<notin> set xs" |
599ff65b85e2
systematic conversions between nat and nibble/char;
haftmann
parents:
51112
diff
changeset
|
1739 |
assumes "n \<le> length xs" |
599ff65b85e2
systematic conversions between nat and nibble/char;
haftmann
parents:
51112
diff
changeset
|
1740 |
shows "(x # xs) ! n = x \<longleftrightarrow> n = 0" (is "?lhs \<longleftrightarrow> ?rhs") |
599ff65b85e2
systematic conversions between nat and nibble/char;
haftmann
parents:
51112
diff
changeset
|
1741 |
proof |
599ff65b85e2
systematic conversions between nat and nibble/char;
haftmann
parents:
51112
diff
changeset
|
1742 |
assume ?lhs |
599ff65b85e2
systematic conversions between nat and nibble/char;
haftmann
parents:
51112
diff
changeset
|
1743 |
show ?rhs |
599ff65b85e2
systematic conversions between nat and nibble/char;
haftmann
parents:
51112
diff
changeset
|
1744 |
proof (rule ccontr) |
599ff65b85e2
systematic conversions between nat and nibble/char;
haftmann
parents:
51112
diff
changeset
|
1745 |
assume "n \<noteq> 0" |
599ff65b85e2
systematic conversions between nat and nibble/char;
haftmann
parents:
51112
diff
changeset
|
1746 |
then have "n > 0" by simp |
60758 | 1747 |
with \<open>?lhs\<close> have "xs ! (n - 1) = x" by simp |
1748 |
moreover from \<open>n > 0\<close> \<open>n \<le> length xs\<close> have "n - 1 < length xs" by simp |
|
51160
599ff65b85e2
systematic conversions between nat and nibble/char;
haftmann
parents:
51112
diff
changeset
|
1749 |
ultimately have "\<exists>i<length xs. xs ! i = x" by auto |
60758 | 1750 |
with \<open>x \<notin> set xs\<close> in_set_conv_nth [of x xs] show False by simp |
51160
599ff65b85e2
systematic conversions between nat and nibble/char;
haftmann
parents:
51112
diff
changeset
|
1751 |
qed |
599ff65b85e2
systematic conversions between nat and nibble/char;
haftmann
parents:
51112
diff
changeset
|
1752 |
next |
599ff65b85e2
systematic conversions between nat and nibble/char;
haftmann
parents:
51112
diff
changeset
|
1753 |
assume ?rhs then show ?lhs by simp |
599ff65b85e2
systematic conversions between nat and nibble/char;
haftmann
parents:
51112
diff
changeset
|
1754 |
qed |
599ff65b85e2
systematic conversions between nat and nibble/char;
haftmann
parents:
51112
diff
changeset
|
1755 |
|
599ff65b85e2
systematic conversions between nat and nibble/char;
haftmann
parents:
51112
diff
changeset
|
1756 |
lemma nth_non_equal_first_eq: |
599ff65b85e2
systematic conversions between nat and nibble/char;
haftmann
parents:
51112
diff
changeset
|
1757 |
assumes "x \<noteq> y" |
599ff65b85e2
systematic conversions between nat and nibble/char;
haftmann
parents:
51112
diff
changeset
|
1758 |
shows "(x # xs) ! n = y \<longleftrightarrow> xs ! (n - 1) = y \<and> n > 0" (is "?lhs \<longleftrightarrow> ?rhs") |
599ff65b85e2
systematic conversions between nat and nibble/char;
haftmann
parents:
51112
diff
changeset
|
1759 |
proof |
599ff65b85e2
systematic conversions between nat and nibble/char;
haftmann
parents:
51112
diff
changeset
|
1760 |
assume "?lhs" with assms have "n > 0" by (cases n) simp_all |
60758 | 1761 |
with \<open>?lhs\<close> show ?rhs by simp |
51160
599ff65b85e2
systematic conversions between nat and nibble/char;
haftmann
parents:
51112
diff
changeset
|
1762 |
next |
599ff65b85e2
systematic conversions between nat and nibble/char;
haftmann
parents:
51112
diff
changeset
|
1763 |
assume "?rhs" then show "?lhs" by simp |
599ff65b85e2
systematic conversions between nat and nibble/char;
haftmann
parents:
51112
diff
changeset
|
1764 |
qed |
599ff65b85e2
systematic conversions between nat and nibble/char;
haftmann
parents:
51112
diff
changeset
|
1765 |
|
67613 | 1766 |
lemma list_ball_nth: "\<lbrakk>n < length xs; \<forall>x \<in> set xs. P x\<rbrakk> \<Longrightarrow> P(xs!n)" |
68719 | 1767 |
by (auto simp add: set_conv_nth) |
13114 | 1768 |
|
67613 | 1769 |
lemma nth_mem [simp]: "n < length xs \<Longrightarrow> xs!n \<in> set xs" |
68719 | 1770 |
by (auto simp add: set_conv_nth) |
13114 | 1771 |
|
1772 |
lemma all_nth_imp_all_set: |
|
67717 | 1773 |
"\<lbrakk>\<forall>i < length xs. P(xs!i); x \<in> set xs\<rbrakk> \<Longrightarrow> P x" |
68719 | 1774 |
by (auto simp add: set_conv_nth) |
13114 | 1775 |
|
1776 |
lemma all_set_conv_all_nth: |
|
67091 | 1777 |
"(\<forall>x \<in> set xs. P x) = (\<forall>i. i < length xs \<longrightarrow> P (xs ! i))" |
68719 | 1778 |
by (auto simp add: set_conv_nth) |
13114 | 1779 |
|
25296 | 1780 |
lemma rev_nth: |
1781 |
"n < size xs \<Longrightarrow> rev xs ! n = xs ! (length xs - Suc n)" |
|
1782 |
proof (induct xs arbitrary: n) |
|
1783 |
case Nil thus ?case by simp |
|
1784 |
next |
|
1785 |
case (Cons x xs) |
|
1786 |
hence n: "n < Suc (length xs)" by simp |
|
1787 |
moreover |
|
1788 |
{ assume "n < length xs" |
|
53374
a14d2a854c02
tuned proofs -- clarified flow of facts wrt. calculation;
wenzelm
parents:
53017
diff
changeset
|
1789 |
with n obtain n' where n': "length xs - n = Suc n'" |
25296 | 1790 |
by (cases "length xs - n", auto) |
1791 |
moreover |
|
53374
a14d2a854c02
tuned proofs -- clarified flow of facts wrt. calculation;
wenzelm
parents:
53017
diff
changeset
|
1792 |
from n' have "length xs - Suc n = n'" by simp |
25296 | 1793 |
ultimately |
1794 |
have "xs ! (length xs - Suc n) = (x # xs) ! (length xs - n)" by simp |
|
1795 |
} |
|
1796 |
ultimately |
|
1797 |
show ?case by (clarsimp simp add: Cons nth_append) |
|
1798 |
qed |
|
13114 | 1799 |
|
31159 | 1800 |
lemma Skolem_list_nth: |
67091 | 1801 |
"(\<forall>i<k. \<exists>x. P i x) = (\<exists>xs. size xs = k \<and> (\<forall>i<k. P i (xs!i)))" |
1802 |
(is "_ = (\<exists>xs. ?P k xs)") |
|
31159 | 1803 |
proof(induct k) |
1804 |
case 0 show ?case by simp |
|
1805 |
next |
|
1806 |
case (Suc k) |
|
67091 | 1807 |
show ?case (is "?L = ?R" is "_ = (\<exists>xs. ?P' xs)") |
31159 | 1808 |
proof |
1809 |
assume "?R" thus "?L" using Suc by auto |
|
1810 |
next |
|
1811 |
assume "?L" |
|
67091 | 1812 |
with Suc obtain x xs where "?P k xs \<and> P k x" by (metis less_Suc_eq) |
31159 | 1813 |
hence "?P'(xs@[x])" by(simp add:nth_append less_Suc_eq) |
1814 |
thus "?R" .. |
|
1815 |
qed |
|
1816 |
qed |
|
1817 |
||
1818 |
||
69593 | 1819 |
subsubsection \<open>\<^const>\<open>list_update\<close>\<close> |
13114 | 1820 |
|
24526 | 1821 |
lemma length_list_update [simp]: "length(xs[i:=x]) = length xs" |
68719 | 1822 |
by (induct xs arbitrary: i) (auto split: nat.split) |
13114 | 1823 |
|
1824 |
lemma nth_list_update: |
|
68719 | 1825 |
"i < length xs==> (xs[i:=x])!j = (if i = j then x else xs!j)" |
1826 |
by (induct xs arbitrary: i j) (auto simp add: nth_Cons split: nat.split) |
|
13114 | 1827 |
|
13142 | 1828 |
lemma nth_list_update_eq [simp]: "i < length xs ==> (xs[i:=x])!i = x" |
68719 | 1829 |
by (simp add: nth_list_update) |
13114 | 1830 |
|
24526 | 1831 |
lemma nth_list_update_neq [simp]: "i \<noteq> j ==> xs[i:=x]!j = xs!j" |
68719 | 1832 |
by (induct xs arbitrary: i j) (auto simp add: nth_Cons split: nat.split) |
13114 | 1833 |
|
24526 | 1834 |
lemma list_update_id[simp]: "xs[i := xs!i] = xs" |
68719 | 1835 |
by (induct xs arbitrary: i) (simp_all split:nat.splits) |
24526 | 1836 |
|
1837 |
lemma list_update_beyond[simp]: "length xs \<le> i \<Longrightarrow> xs[i:=x] = xs" |
|
68709 | 1838 |
proof (induct xs arbitrary: i) |
1839 |
case (Cons x xs i) |
|
1840 |
then show ?case |
|
1841 |
by (metis leD length_list_update list_eq_iff_nth_eq nth_list_update_neq) |
|
1842 |
qed simp |
|
17501 | 1843 |
|
31077 | 1844 |
lemma list_update_nonempty[simp]: "xs[k:=x] = [] \<longleftrightarrow> xs=[]" |
68719 | 1845 |
by (simp only: length_0_conv[symmetric] length_list_update) |
31077 | 1846 |
|
13114 | 1847 |
lemma list_update_same_conv: |
58807 | 1848 |
"i < length xs ==> (xs[i := x] = xs) = (xs!i = x)" |
68719 | 1849 |
by (induct xs arbitrary: i) (auto split: nat.split) |
13114 | 1850 |
|
14187 | 1851 |
lemma list_update_append1: |
58807 | 1852 |
"i < size xs \<Longrightarrow> (xs @ ys)[i:=x] = xs[i:=x] @ ys" |
68719 | 1853 |
by (induct xs arbitrary: i)(auto split:nat.split) |
14187 | 1854 |
|
15868 | 1855 |
lemma list_update_append: |
64963 | 1856 |
"(xs @ ys) [n:= x] = |
15868 | 1857 |
(if n < length xs then xs[n:= x] @ ys else xs @ (ys [n-length xs:= x]))" |
68719 | 1858 |
by (induct xs arbitrary: n) (auto split:nat.splits) |
15868 | 1859 |
|
14402
4201e1916482
moved lemmas from MicroJava/Comp/AuxLemmas.thy to List.thy
nipkow
parents:
14395
diff
changeset
|
1860 |
lemma list_update_length [simp]: |
58807 | 1861 |
"(xs @ x # ys)[length xs := y] = (xs @ y # ys)" |
68719 | 1862 |
by (induct xs, auto) |
14402
4201e1916482
moved lemmas from MicroJava/Comp/AuxLemmas.thy to List.thy
nipkow
parents:
14395
diff
changeset
|
1863 |
|
31264 | 1864 |
lemma map_update: "map f (xs[k:= y]) = (map f xs)[k := f y]" |
68719 | 1865 |
by(induct xs arbitrary: k)(auto split:nat.splits) |
31264 | 1866 |
|
1867 |
lemma rev_update: |
|
1868 |
"k < length xs \<Longrightarrow> rev (xs[k:= y]) = (rev xs)[length xs - k - 1 := y]" |
|
68719 | 1869 |
by (induct xs arbitrary: k) (auto simp: list_update_append split:nat.splits) |
31264 | 1870 |
|
13114 | 1871 |
lemma update_zip: |
31080 | 1872 |
"(zip xs ys)[i:=xy] = zip (xs[i:=fst xy]) (ys[i:=snd xy])" |
68719 | 1873 |
by (induct ys arbitrary: i xy xs) (auto, case_tac xs, auto split: nat.split) |
24526 | 1874 |
|
68709 | 1875 |
lemma set_update_subset_insert: "set(xs[i:=x]) \<le> insert x (set xs)" |
68719 | 1876 |
by (induct xs arbitrary: i) (auto split: nat.split) |
13114 | 1877 |
|
67613 | 1878 |
lemma set_update_subsetI: "\<lbrakk>set xs \<subseteq> A; x \<in> A\<rbrakk> \<Longrightarrow> set(xs[i := x]) \<subseteq> A" |
68719 | 1879 |
by (blast dest!: set_update_subset_insert [THEN subsetD]) |
13114 | 1880 |
|
24526 | 1881 |
lemma set_update_memI: "n < length xs \<Longrightarrow> x \<in> set (xs[n := x])" |
68719 | 1882 |
by (induct xs arbitrary: n) (auto split:nat.splits) |
15868 | 1883 |
|
31077 | 1884 |
lemma list_update_overwrite[simp]: |
24796 | 1885 |
"xs [i := x, i := y] = xs [i := y]" |
68709 | 1886 |
by (induct xs arbitrary: i) (simp_all split: nat.split) |
24796 | 1887 |
|
1888 |
lemma list_update_swap: |
|
1889 |
"i \<noteq> i' \<Longrightarrow> xs [i := x, i' := x'] = xs [i' := x', i := x]" |
|
68709 | 1890 |
by (induct xs arbitrary: i i') (simp_all split: nat.split) |
24796 | 1891 |
|
29827 | 1892 |
lemma list_update_code [code]: |
1893 |
"[][i := y] = []" |
|
1894 |
"(x # xs)[0 := y] = y # xs" |
|
1895 |
"(x # xs)[Suc i := y] = x # xs[i := y]" |
|
68719 | 1896 |
by simp_all |
29827 | 1897 |
|
13114 | 1898 |
|
69593 | 1899 |
subsubsection \<open>\<^const>\<open>last\<close> and \<^const>\<open>butlast\<close>\<close> |
13114 | 1900 |
|
13142 | 1901 |
lemma last_snoc [simp]: "last (xs @ [x]) = x" |
68719 | 1902 |
by (induct xs) auto |
13114 | 1903 |
|
13142 | 1904 |
lemma butlast_snoc [simp]: "butlast (xs @ [x]) = xs" |
68719 | 1905 |
by (induct xs) auto |
13114 | 1906 |
|
14302 | 1907 |
lemma last_ConsL: "xs = [] \<Longrightarrow> last(x#xs) = x" |
68719 | 1908 |
by simp |
14302 | 1909 |
|
1910 |
lemma last_ConsR: "xs \<noteq> [] \<Longrightarrow> last(x#xs) = last xs" |
|
68719 | 1911 |
by simp |
14302 | 1912 |
|
1913 |
lemma last_append: "last(xs @ ys) = (if ys = [] then last xs else last ys)" |
|
68719 | 1914 |
by (induct xs) (auto) |
14302 | 1915 |
|
1916 |
lemma last_appendL[simp]: "ys = [] \<Longrightarrow> last(xs @ ys) = last xs" |
|
68719 | 1917 |
by(simp add:last_append) |
14302 | 1918 |
|
1919 |
lemma last_appendR[simp]: "ys \<noteq> [] \<Longrightarrow> last(xs @ ys) = last ys" |
|
68719 | 1920 |
by(simp add:last_append) |
14302 | 1921 |
|
45841 | 1922 |
lemma last_tl: "xs = [] \<or> tl xs \<noteq> [] \<Longrightarrow>last (tl xs) = last xs" |
68719 | 1923 |
by (induct xs) simp_all |
45841 | 1924 |
|
1925 |
lemma butlast_tl: "butlast (tl xs) = tl (butlast xs)" |
|
68719 | 1926 |
by (induct xs) simp_all |
45841 | 1927 |
|
17762 | 1928 |
lemma hd_rev: "xs \<noteq> [] \<Longrightarrow> hd(rev xs) = last xs" |
68719 | 1929 |
by(rule rev_exhaust[of xs]) simp_all |
17762 | 1930 |
|
1931 |
lemma last_rev: "xs \<noteq> [] \<Longrightarrow> last(rev xs) = hd xs" |
|
68719 | 1932 |
by(cases xs) simp_all |
17762 | 1933 |
|
17765 | 1934 |
lemma last_in_set[simp]: "as \<noteq> [] \<Longrightarrow> last as \<in> set as" |
68719 | 1935 |
by (induct as) auto |
17762 | 1936 |
|
13142 | 1937 |
lemma length_butlast [simp]: "length (butlast xs) = length xs - 1" |
68719 | 1938 |
by (induct xs rule: rev_induct) auto |
13114 | 1939 |
|
1940 |
lemma butlast_append: |
|
24526 | 1941 |
"butlast (xs @ ys) = (if ys = [] then butlast xs else xs @ butlast ys)" |
68719 | 1942 |
by (induct xs arbitrary: ys) auto |
13114 | 1943 |
|
13142 | 1944 |
lemma append_butlast_last_id [simp]: |
67613 | 1945 |
"xs \<noteq> [] \<Longrightarrow> butlast xs @ [last xs] = xs" |
68719 | 1946 |
by (induct xs) auto |
13114 | 1947 |
|
67613 | 1948 |
lemma in_set_butlastD: "x \<in> set (butlast xs) \<Longrightarrow> x \<in> set xs" |
68719 | 1949 |
by (induct xs) (auto split: if_split_asm) |
13114 | 1950 |
|
1951 |
lemma in_set_butlast_appendI: |
|
67091 | 1952 |
"x \<in> set (butlast xs) \<or> x \<in> set (butlast ys) \<Longrightarrow> x \<in> set (butlast (xs @ ys))" |
68719 | 1953 |
by (auto dest: in_set_butlastD simp add: butlast_append) |
13114 | 1954 |
|
24526 | 1955 |
lemma last_drop[simp]: "n < length xs \<Longrightarrow> last (drop n xs) = last xs" |
68719 | 1956 |
by (induct xs arbitrary: n)(auto split:nat.split) |
17501 | 1957 |
|
45841 | 1958 |
lemma nth_butlast: |
1959 |
assumes "n < length (butlast xs)" shows "butlast xs ! n = xs ! n" |
|
1960 |
proof (cases xs) |
|
1961 |
case (Cons y ys) |
|
1962 |
moreover from assms have "butlast xs ! n = (butlast xs @ [last xs]) ! n" |
|
1963 |
by (simp add: nth_append) |
|
1964 |
ultimately show ?thesis using append_butlast_last_id by simp |
|
1965 |
qed simp |
|
1966 |
||
30128
365ee7319b86
revert some Suc 0 lemmas back to their original forms; added some simp rules for (1::nat)
huffman
parents:
30079
diff
changeset
|
1967 |
lemma last_conv_nth: "xs\<noteq>[] \<Longrightarrow> last xs = xs!(length xs - 1)" |
68719 | 1968 |
by(induct xs)(auto simp:neq_Nil_conv) |
17589 | 1969 |
|
30128
365ee7319b86
revert some Suc 0 lemmas back to their original forms; added some simp rules for (1::nat)
huffman
parents:
30079
diff
changeset
|
1970 |
lemma butlast_conv_take: "butlast xs = take (length xs - 1) xs" |
68719 | 1971 |
by (induction xs rule: induct_list012) simp_all |
26584
46f3b89b2445
move lemmas from Word/BinBoolList.thy to List.thy
huffman
parents:
26480
diff
changeset
|
1972 |
|
31077 | 1973 |
lemma last_list_update: |
1974 |
"xs \<noteq> [] \<Longrightarrow> last(xs[k:=x]) = (if k = size xs - 1 then x else last xs)" |
|
68719 | 1975 |
by (auto simp: last_conv_nth) |
31077 | 1976 |
|
1977 |
lemma butlast_list_update: |
|
1978 |
"butlast(xs[k:=x]) = |
|
58807 | 1979 |
(if k = size xs - 1 then butlast xs else (butlast xs)[k:=x])" |
68719 | 1980 |
by(cases xs rule:rev_cases)(auto simp: list_update_append split: nat.splits) |
58807 | 1981 |
|
1982 |
lemma last_map: "xs \<noteq> [] \<Longrightarrow> last (map f xs) = f (last xs)" |
|
68719 | 1983 |
by (cases xs rule: rev_cases) simp_all |
58807 | 1984 |
|
1985 |
lemma map_butlast: "map f (butlast xs) = butlast (map f xs)" |
|
68719 | 1986 |
by (induct xs) simp_all |
36851 | 1987 |
|
40230 | 1988 |
lemma snoc_eq_iff_butlast: |
67091 | 1989 |
"xs @ [x] = ys \<longleftrightarrow> (ys \<noteq> [] \<and> butlast ys = xs \<and> last ys = x)" |
68719 | 1990 |
by fastforce |
40230 | 1991 |
|
63173 | 1992 |
corollary longest_common_suffix: |
1993 |
"\<exists>ss xs' ys'. xs = xs' @ ss \<and> ys = ys' @ ss |
|
1994 |
\<and> (xs' = [] \<or> ys' = [] \<or> last xs' \<noteq> last ys')" |
|
68719 | 1995 |
using longest_common_prefix[of "rev xs" "rev ys"] |
1996 |
unfolding rev_swap rev_append by (metis last_rev rev_is_Nil_conv) |
|
63173 | 1997 |
|
70183
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
69850
diff
changeset
|
1998 |
lemma butlast_rev [simp]: "butlast (rev xs) = rev (tl xs)" |
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
69850
diff
changeset
|
1999 |
by (cases xs) simp_all |
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
69850
diff
changeset
|
2000 |
|
24796 | 2001 |
|
69593 | 2002 |
subsubsection \<open>\<^const>\<open>take\<close> and \<^const>\<open>drop\<close>\<close> |
13114 | 2003 |
|
66658 | 2004 |
lemma take_0: "take 0 xs = []" |
68719 | 2005 |
by (induct xs) auto |
66658 | 2006 |
|
2007 |
lemma drop_0: "drop 0 xs = xs" |
|
68719 | 2008 |
by (induct xs) auto |
13114 | 2009 |
|
66658 | 2010 |
lemma take0[simp]: "take 0 = (\<lambda>xs. [])" |
68719 | 2011 |
by(rule ext) (rule take_0) |
66658 | 2012 |
|
2013 |
lemma drop0[simp]: "drop 0 = (\<lambda>x. x)" |
|
68719 | 2014 |
by(rule ext) (rule drop_0) |
13114 | 2015 |
|
13142 | 2016 |
lemma take_Suc_Cons [simp]: "take (Suc n) (x # xs) = x # take n xs" |
68719 | 2017 |
by simp |
13114 | 2018 |
|
13142 | 2019 |
lemma drop_Suc_Cons [simp]: "drop (Suc n) (x # xs) = drop n xs" |
68719 | 2020 |
by simp |
13114 | 2021 |
|
13142 | 2022 |
declare take_Cons [simp del] and drop_Cons [simp del] |
13114 | 2023 |
|
67091 | 2024 |
lemma take_Suc: "xs \<noteq> [] \<Longrightarrow> take (Suc n) xs = hd xs # take n (tl xs)" |
68719 | 2025 |
by(clarsimp simp add:neq_Nil_conv) |
15110
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
2026 |
|
14187 | 2027 |
lemma drop_Suc: "drop (Suc n) xs = drop n (tl xs)" |
68719 | 2028 |
by(cases xs, simp_all) |
14187 | 2029 |
|
66870 | 2030 |
lemma hd_take[simp]: "j > 0 \<Longrightarrow> hd (take j xs) = hd xs" |
68719 | 2031 |
by (metis gr0_conv_Suc list.sel(1) take.simps(1) take_Suc) |
66657 | 2032 |
|
26584
46f3b89b2445
move lemmas from Word/BinBoolList.thy to List.thy
huffman
parents:
26480
diff
changeset
|
2033 |
lemma take_tl: "take n (tl xs) = tl (take (Suc n) xs)" |
68719 | 2034 |
by (induct xs arbitrary: n) simp_all |
26584
46f3b89b2445
move lemmas from Word/BinBoolList.thy to List.thy
huffman
parents:
26480
diff
changeset
|
2035 |
|
24526 | 2036 |
lemma drop_tl: "drop n (tl xs) = tl(drop n xs)" |
68719 | 2037 |
by(induct xs arbitrary: n, simp_all add:drop_Cons drop_Suc split:nat.split) |
24526 | 2038 |
|
26584
46f3b89b2445
move lemmas from Word/BinBoolList.thy to List.thy
huffman
parents:
26480
diff
changeset
|
2039 |
lemma tl_take: "tl (take n xs) = take (n - 1) (tl xs)" |
68719 | 2040 |
by (cases n, simp, cases xs, auto) |
26584
46f3b89b2445
move lemmas from Word/BinBoolList.thy to List.thy
huffman
parents:
26480
diff
changeset
|
2041 |
|
46f3b89b2445
move lemmas from Word/BinBoolList.thy to List.thy
huffman
parents:
26480
diff
changeset
|
2042 |
lemma tl_drop: "tl (drop n xs) = drop n (tl xs)" |
68719 | 2043 |
by (simp only: drop_tl) |
26584
46f3b89b2445
move lemmas from Word/BinBoolList.thy to List.thy
huffman
parents:
26480
diff
changeset
|
2044 |
|
24526 | 2045 |
lemma nth_via_drop: "drop n xs = y#ys \<Longrightarrow> xs!n = y" |
68719 | 2046 |
by (induct xs arbitrary: n, simp)(auto simp: drop_Cons nth_Cons split: nat.splits) |
14187 | 2047 |
|
13913 | 2048 |
lemma take_Suc_conv_app_nth: |
24526 | 2049 |
"i < length xs \<Longrightarrow> take (Suc i) xs = take i xs @ [xs!i]" |
68709 | 2050 |
proof (induct xs arbitrary: i) |
69850 | 2051 |
case Nil |
2052 |
then show ?case by simp |
|
2053 |
next |
|
2054 |
case Cons |
|
2055 |
then show ?case by (cases i) auto |
|
2056 |
qed |
|
13913 | 2057 |
|
58247
98d0f85d247f
enamed drop_Suc_conv_tl and nth_drop' to Cons_nth_drop_Suc
nipkow
parents:
58195
diff
changeset
|
2058 |
lemma Cons_nth_drop_Suc: |
24526 | 2059 |
"i < length xs \<Longrightarrow> (xs!i) # (drop (Suc i) xs) = drop i xs" |
68709 | 2060 |
proof (induct xs arbitrary: i) |
69850 | 2061 |
case Nil |
2062 |
then show ?case by simp |
|
2063 |
next |
|
2064 |
case Cons |
|
2065 |
then show ?case by (cases i) auto |
|
2066 |
qed |
|
14591 | 2067 |
|
24526 | 2068 |
lemma length_take [simp]: "length (take n xs) = min (length xs) n" |
68719 | 2069 |
by (induct n arbitrary: xs) (auto, case_tac xs, auto) |
24526 | 2070 |
|
2071 |
lemma length_drop [simp]: "length (drop n xs) = (length xs - n)" |
|
68719 | 2072 |
by (induct n arbitrary: xs) (auto, case_tac xs, auto) |
24526 | 2073 |
|
68709 | 2074 |
lemma take_all [simp]: "length xs \<le> n ==> take n xs = xs" |
68719 | 2075 |
by (induct n arbitrary: xs) (auto, case_tac xs, auto) |
24526 | 2076 |
|
68709 | 2077 |
lemma drop_all [simp]: "length xs \<le> n ==> drop n xs = []" |
68719 | 2078 |
by (induct n arbitrary: xs) (auto, case_tac xs, auto) |
13114 | 2079 |
|
13142 | 2080 |
lemma take_append [simp]: |
24526 | 2081 |
"take n (xs @ ys) = (take n xs @ take (n - length xs) ys)" |
68719 | 2082 |
by (induct n arbitrary: xs) (auto, case_tac xs, auto) |
13114 | 2083 |
|
13142 | 2084 |
lemma drop_append [simp]: |
24526 | 2085 |
"drop n (xs @ ys) = drop n xs @ drop (n - length xs) ys" |
68719 | 2086 |
by (induct n arbitrary: xs) (auto, case_tac xs, auto) |
24526 | 2087 |
|
2088 |
lemma take_take [simp]: "take n (take m xs) = take (min n m) xs" |
|
68709 | 2089 |
proof (induct m arbitrary: xs n) |
69850 | 2090 |
case 0 |
2091 |
then show ?case by simp |
|
2092 |
next |
|
2093 |
case Suc |
|
2094 |
then show ?case by (cases xs; cases n) simp_all |
|
2095 |
qed |
|
13114 | 2096 |
|
24526 | 2097 |
lemma drop_drop [simp]: "drop n (drop m xs) = drop (n + m) xs" |
68709 | 2098 |
proof (induct m arbitrary: xs) |
69850 | 2099 |
case 0 |
2100 |
then show ?case by simp |
|
2101 |
next |
|
2102 |
case Suc |
|
2103 |
then show ?case by (cases xs) simp_all |
|
2104 |
qed |
|
13114 | 2105 |
|
24526 | 2106 |
lemma take_drop: "take n (drop m xs) = drop m (take (n + m) xs)" |
68709 | 2107 |
proof (induct m arbitrary: xs n) |
69850 | 2108 |
case 0 |
2109 |
then show ?case by simp |
|
2110 |
next |
|
2111 |
case Suc |
|
2112 |
then show ?case by (cases xs; cases n) simp_all |
|
2113 |
qed |
|
13114 | 2114 |
|
24526 | 2115 |
lemma drop_take: "drop n (take m xs) = take (m-n) (drop n xs)" |
68719 | 2116 |
by(induct xs arbitrary: m n)(auto simp: take_Cons drop_Cons split: nat.split) |
14802 | 2117 |
|
24526 | 2118 |
lemma append_take_drop_id [simp]: "take n xs @ drop n xs = xs" |
68709 | 2119 |
proof (induct n arbitrary: xs) |
69850 | 2120 |
case 0 |
2121 |
then show ?case by simp |
|
2122 |
next |
|
2123 |
case Suc |
|
2124 |
then show ?case by (cases xs) simp_all |
|
2125 |
qed |
|
13114 | 2126 |
|
24526 | 2127 |
lemma take_eq_Nil[simp]: "(take n xs = []) = (n = 0 \<or> xs = [])" |
68719 | 2128 |
by(induct xs arbitrary: n)(auto simp: take_Cons split:nat.split) |
15110
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
2129 |
|
68709 | 2130 |
lemma drop_eq_Nil[simp]: "(drop n xs = []) = (length xs \<le> n)" |
68719 | 2131 |
by (induct xs arbitrary: n) (auto simp: drop_Cons split:nat.split) |
15110
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
2132 |
|
24526 | 2133 |
lemma take_map: "take n (map f xs) = map f (take n xs)" |
68709 | 2134 |
proof (induct n arbitrary: xs) |
69850 | 2135 |
case 0 |
2136 |
then show ?case by simp |
|
2137 |
next |
|
2138 |
case Suc |
|
2139 |
then show ?case by (cases xs) simp_all |
|
2140 |
qed |
|
13114 | 2141 |
|
24526 | 2142 |
lemma drop_map: "drop n (map f xs) = map f (drop n xs)" |
68709 | 2143 |
proof (induct n arbitrary: xs) |
69850 | 2144 |
case 0 |
2145 |
then show ?case by simp |
|
2146 |
next |
|
2147 |
case Suc |
|
2148 |
then show ?case by (cases xs) simp_all |
|
2149 |
qed |
|
13114 | 2150 |
|
24526 | 2151 |
lemma rev_take: "rev (take i xs) = drop (length xs - i) (rev xs)" |
68709 | 2152 |
proof (induct xs arbitrary: i) |
69850 | 2153 |
case Nil |
2154 |
then show ?case by simp |
|
2155 |
next |
|
2156 |
case Cons |
|
2157 |
then show ?case by (cases i) auto |
|
2158 |
qed |
|
13114 | 2159 |
|
24526 | 2160 |
lemma rev_drop: "rev (drop i xs) = take (length xs - i) (rev xs)" |
68709 | 2161 |
proof (induct xs arbitrary: i) |
69850 | 2162 |
case Nil |
2163 |
then show ?case by simp |
|
2164 |
next |
|
2165 |
case Cons |
|
2166 |
then show ?case by (cases i) auto |
|
2167 |
qed |
|
13114 | 2168 |
|
61699
a81dc5c4d6a9
New theorems mostly from Peter Gammie
paulson <lp15@cam.ac.uk>
parents:
61682
diff
changeset
|
2169 |
lemma drop_rev: "drop n (rev xs) = rev (take (length xs - n) xs)" |
a81dc5c4d6a9
New theorems mostly from Peter Gammie
paulson <lp15@cam.ac.uk>
parents:
61682
diff
changeset
|
2170 |
by (cases "length xs < n") (auto simp: rev_take) |
a81dc5c4d6a9
New theorems mostly from Peter Gammie
paulson <lp15@cam.ac.uk>
parents:
61682
diff
changeset
|
2171 |
|
a81dc5c4d6a9
New theorems mostly from Peter Gammie
paulson <lp15@cam.ac.uk>
parents:
61682
diff
changeset
|
2172 |
lemma take_rev: "take n (rev xs) = rev (drop (length xs - n) xs)" |
a81dc5c4d6a9
New theorems mostly from Peter Gammie
paulson <lp15@cam.ac.uk>
parents:
61682
diff
changeset
|
2173 |
by (cases "length xs < n") (auto simp: rev_drop) |
a81dc5c4d6a9
New theorems mostly from Peter Gammie
paulson <lp15@cam.ac.uk>
parents:
61682
diff
changeset
|
2174 |
|
24526 | 2175 |
lemma nth_take [simp]: "i < n ==> (take n xs)!i = xs!i" |
68709 | 2176 |
proof (induct xs arbitrary: i n) |
69850 | 2177 |
case Nil |
2178 |
then show ?case by simp |
|
2179 |
next |
|
2180 |
case Cons |
|
2181 |
then show ?case by (cases n; cases i) simp_all |
|
2182 |
qed |
|
13114 | 2183 |
|
13142 | 2184 |
lemma nth_drop [simp]: |
68709 | 2185 |
"n \<le> length xs ==> (drop n xs)!i = xs!(n + i)" |
2186 |
proof (induct n arbitrary: xs) |
|
69850 | 2187 |
case 0 |
2188 |
then show ?case by simp |
|
2189 |
next |
|
2190 |
case Suc |
|
2191 |
then show ?case by (cases xs) simp_all |
|
2192 |
qed |
|
3507 | 2193 |
|
26584
46f3b89b2445
move lemmas from Word/BinBoolList.thy to List.thy
huffman
parents:
26480
diff
changeset
|
2194 |
lemma butlast_take: |
68709 | 2195 |
"n \<le> length xs ==> butlast (take n xs) = take (n - 1) xs" |
68719 | 2196 |
by (simp add: butlast_conv_take min.absorb1 min.absorb2) |
26584
46f3b89b2445
move lemmas from Word/BinBoolList.thy to List.thy
huffman
parents:
26480
diff
changeset
|
2197 |
|
46f3b89b2445
move lemmas from Word/BinBoolList.thy to List.thy
huffman
parents:
26480
diff
changeset
|
2198 |
lemma butlast_drop: "butlast (drop n xs) = drop n (butlast xs)" |
68719 | 2199 |
by (simp add: butlast_conv_take drop_take ac_simps) |
26584
46f3b89b2445
move lemmas from Word/BinBoolList.thy to List.thy
huffman
parents:
26480
diff
changeset
|
2200 |
|
46f3b89b2445
move lemmas from Word/BinBoolList.thy to List.thy
huffman
parents:
26480
diff
changeset
|
2201 |
lemma take_butlast: "n < length xs ==> take n (butlast xs) = take n xs" |
68719 | 2202 |
by (simp add: butlast_conv_take min.absorb1) |
26584
46f3b89b2445
move lemmas from Word/BinBoolList.thy to List.thy
huffman
parents:
26480
diff
changeset
|
2203 |
|
46f3b89b2445
move lemmas from Word/BinBoolList.thy to List.thy
huffman
parents:
26480
diff
changeset
|
2204 |
lemma drop_butlast: "drop n (butlast xs) = butlast (drop n xs)" |
68719 | 2205 |
by (simp add: butlast_conv_take drop_take ac_simps) |
26584
46f3b89b2445
move lemmas from Word/BinBoolList.thy to List.thy
huffman
parents:
26480
diff
changeset
|
2206 |
|
46500
0196966d6d2d
removing unnecessary premises in theorems of List theory
bulwahn
parents:
46448
diff
changeset
|
2207 |
lemma hd_drop_conv_nth: "n < length xs \<Longrightarrow> hd(drop n xs) = xs!n" |
68719 | 2208 |
by(simp add: hd_conv_nth) |
18423 | 2209 |
|
35248 | 2210 |
lemma set_take_subset_set_take: |
68709 | 2211 |
"m \<le> n \<Longrightarrow> set(take m xs) \<le> set(take n xs)" |
2212 |
proof (induct xs arbitrary: m n) |
|
2213 |
case (Cons x xs m n) then show ?case |
|
2214 |
by (cases n) (auto simp: take_Cons) |
|
2215 |
qed simp |
|
35248 | 2216 |
|
24526 | 2217 |
lemma set_take_subset: "set(take n xs) \<subseteq> set xs" |
68719 | 2218 |
by(induct xs arbitrary: n)(auto simp:take_Cons split:nat.split) |
24526 | 2219 |
|
2220 |
lemma set_drop_subset: "set(drop n xs) \<subseteq> set xs" |
|
68719 | 2221 |
by(induct xs arbitrary: n)(auto simp:drop_Cons split:nat.split) |
14025 | 2222 |
|
35248 | 2223 |
lemma set_drop_subset_set_drop: |
68709 | 2224 |
"m \<ge> n \<Longrightarrow> set(drop m xs) \<le> set(drop n xs)" |
2225 |
proof (induct xs arbitrary: m n) |
|
2226 |
case (Cons x xs m n) |
|
2227 |
then show ?case |
|
2228 |
by (clarsimp simp: drop_Cons split: nat.split) (metis set_drop_subset subset_iff) |
|
2229 |
qed simp |
|
35248 | 2230 |
|
67613 | 2231 |
lemma in_set_takeD: "x \<in> set(take n xs) \<Longrightarrow> x \<in> set xs" |
68719 | 2232 |
using set_take_subset by fast |
14187 | 2233 |
|
67613 | 2234 |
lemma in_set_dropD: "x \<in> set(drop n xs) \<Longrightarrow> x \<in> set xs" |
68719 | 2235 |
using set_drop_subset by fast |
14187 | 2236 |
|
13114 | 2237 |
lemma append_eq_conv_conj: |
24526 | 2238 |
"(xs @ ys = zs) = (xs = take (length xs) zs \<and> ys = drop (length xs) zs)" |
68709 | 2239 |
proof (induct xs arbitrary: zs) |
2240 |
case (Cons x xs zs) then show ?case |
|
2241 |
by (cases zs, auto) |
|
2242 |
qed auto |
|
13142 | 2243 |
|
58807 | 2244 |
lemma take_add: "take (i+j) xs = take i xs @ take j (drop i xs)" |
68709 | 2245 |
proof (induct xs arbitrary: i) |
2246 |
case (Cons x xs i) then show ?case |
|
2247 |
by (cases i, auto) |
|
2248 |
qed auto |
|
14050 | 2249 |
|
14300 | 2250 |
lemma append_eq_append_conv_if: |
58807 | 2251 |
"(xs\<^sub>1 @ xs\<^sub>2 = ys\<^sub>1 @ ys\<^sub>2) = |
53015
a1119cf551e8
standardized symbols via "isabelle update_sub_sup", excluding src/Pure and src/Tools/WWW_Find;
wenzelm
parents:
52435
diff
changeset
|
2252 |
(if size xs\<^sub>1 \<le> size ys\<^sub>1 |
a1119cf551e8
standardized symbols via "isabelle update_sub_sup", excluding src/Pure and src/Tools/WWW_Find;
wenzelm
parents:
52435
diff
changeset
|
2253 |
then xs\<^sub>1 = take (size xs\<^sub>1) ys\<^sub>1 \<and> xs\<^sub>2 = drop (size xs\<^sub>1) ys\<^sub>1 @ ys\<^sub>2 |
a1119cf551e8
standardized symbols via "isabelle update_sub_sup", excluding src/Pure and src/Tools/WWW_Find;
wenzelm
parents:
52435
diff
changeset
|
2254 |
else take (size ys\<^sub>1) xs\<^sub>1 = ys\<^sub>1 \<and> drop (size ys\<^sub>1) xs\<^sub>1 @ xs\<^sub>2 = ys\<^sub>2)" |
68709 | 2255 |
proof (induct xs\<^sub>1 arbitrary: ys\<^sub>1) |
2256 |
case (Cons a xs\<^sub>1 ys\<^sub>1) then show ?case |
|
2257 |
by (cases ys\<^sub>1, auto) |
|
2258 |
qed auto |
|
14300 | 2259 |
|
15110
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
2260 |
lemma take_hd_drop: |
30079
293b896b9c25
make proofs work whether or not One_nat_def is a simp rule; replace 1 with Suc 0 in the rhs of some simp rules
huffman
parents:
30008
diff
changeset
|
2261 |
"n < length xs \<Longrightarrow> take n xs @ [hd (drop n xs)] = take (Suc n) xs" |
68709 | 2262 |
by (induct xs arbitrary: n) (simp_all add:drop_Cons split:nat.split) |
15110
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
2263 |
|
17501 | 2264 |
lemma id_take_nth_drop: |
64963 | 2265 |
"i < length xs \<Longrightarrow> xs = take i xs @ xs!i # drop (Suc i) xs" |
17501 | 2266 |
proof - |
2267 |
assume si: "i < length xs" |
|
2268 |
hence "xs = take (Suc i) xs @ drop (Suc i) xs" by auto |
|
2269 |
moreover |
|
2270 |
from si have "take (Suc i) xs = take i xs @ [xs!i]" |
|
68709 | 2271 |
using take_Suc_conv_app_nth by blast |
17501 | 2272 |
ultimately show ?thesis by auto |
2273 |
qed |
|
64963 | 2274 |
|
59728 | 2275 |
lemma take_update_cancel[simp]: "n \<le> m \<Longrightarrow> take n (xs[m := y]) = take n xs" |
68719 | 2276 |
by(simp add: list_eq_iff_nth_eq) |
59728 | 2277 |
|
2278 |
lemma drop_update_cancel[simp]: "n < m \<Longrightarrow> drop m (xs[n := x]) = drop m xs" |
|
68719 | 2279 |
by(simp add: list_eq_iff_nth_eq) |
59728 | 2280 |
|
17501 | 2281 |
lemma upd_conv_take_nth_drop: |
58807 | 2282 |
"i < length xs \<Longrightarrow> xs[i:=a] = take i xs @ a # drop (Suc i) xs" |
17501 | 2283 |
proof - |
2284 |
assume i: "i < length xs" |
|
2285 |
have "xs[i:=a] = (take i xs @ xs!i # drop (Suc i) xs)[i:=a]" |
|
2286 |
by(rule arg_cong[OF id_take_nth_drop[OF i]]) |
|
2287 |
also have "\<dots> = take i xs @ a # drop (Suc i) xs" |
|
2288 |
using i by (simp add: list_update_append) |
|
2289 |
finally show ?thesis . |
|
2290 |
qed |
|
2291 |
||
66891
5ec8cd4db7e2
drop a superfluous assumption that was found by the find_unused_assms command
bulwahn
parents:
66890
diff
changeset
|
2292 |
lemma take_update_swap: "take m (xs[n := x]) = (take m xs)[n := x]" |
68709 | 2293 |
proof (cases "n \<ge> length xs") |
2294 |
case False |
|
2295 |
then show ?thesis |
|
2296 |
by (simp add: upd_conv_take_nth_drop take_Cons drop_take min_def diff_Suc split: nat.split) |
|
2297 |
qed auto |
|
2298 |
||
2299 |
lemma drop_update_swap: |
|
2300 |
assumes "m \<le> n" shows "drop m (xs[n := x]) = (drop m xs)[n-m := x]" |
|
2301 |
proof (cases "n \<ge> length xs") |
|
2302 |
case False |
|
2303 |
with assms show ?thesis |
|
2304 |
by (simp add: upd_conv_take_nth_drop drop_take) |
|
2305 |
qed auto |
|
59728 | 2306 |
|
2307 |
lemma nth_image: "l \<le> size xs \<Longrightarrow> nth xs ` {0..<l} = set(take l xs)" |
|
68719 | 2308 |
by(auto simp: set_conv_nth image_def) (metis Suc_le_eq nth_take order_trans) |
59728 | 2309 |
|
13114 | 2310 |
|
69593 | 2311 |
subsubsection \<open>\<^const>\<open>takeWhile\<close> and \<^const>\<open>dropWhile\<close>\<close> |
13114 | 2312 |
|
33639
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
hoelzl
parents:
33593
diff
changeset
|
2313 |
lemma length_takeWhile_le: "length (takeWhile P xs) \<le> length xs" |
68719 | 2314 |
by (induct xs) auto |
33639
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
hoelzl
parents:
33593
diff
changeset
|
2315 |
|
13142 | 2316 |
lemma takeWhile_dropWhile_id [simp]: "takeWhile P xs @ dropWhile P xs = xs" |
68719 | 2317 |
by (induct xs) auto |
13114 | 2318 |
|
13142 | 2319 |
lemma takeWhile_append1 [simp]: |
67091 | 2320 |
"\<lbrakk>x \<in> set xs; \<not>P(x)\<rbrakk> \<Longrightarrow> takeWhile P (xs @ ys) = takeWhile P xs" |
68719 | 2321 |
by (induct xs) auto |
13114 | 2322 |
|
13142 | 2323 |
lemma takeWhile_append2 [simp]: |
67613 | 2324 |
"(\<And>x. x \<in> set xs \<Longrightarrow> P x) \<Longrightarrow> takeWhile P (xs @ ys) = xs @ takeWhile P ys" |
68719 | 2325 |
by (induct xs) auto |
13114 | 2326 |
|
67613 | 2327 |
lemma takeWhile_tail: "\<not> P x \<Longrightarrow> takeWhile P (xs @ (x#l)) = takeWhile P xs" |
68719 | 2328 |
by (induct xs) auto |
13114 | 2329 |
|
33639
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
hoelzl
parents:
33593
diff
changeset
|
2330 |
lemma takeWhile_nth: "j < length (takeWhile P xs) \<Longrightarrow> takeWhile P xs ! j = xs ! j" |
68709 | 2331 |
by (metis nth_append takeWhile_dropWhile_id) |
33639
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
hoelzl
parents:
33593
diff
changeset
|
2332 |
|
58807 | 2333 |
lemma dropWhile_nth: "j < length (dropWhile P xs) \<Longrightarrow> |
2334 |
dropWhile P xs ! j = xs ! (j + length (takeWhile P xs))" |
|
68709 | 2335 |
by (metis add.commute nth_append_length_plus takeWhile_dropWhile_id) |
33639
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
hoelzl
parents:
33593
diff
changeset
|
2336 |
|
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
hoelzl
parents:
33593
diff
changeset
|
2337 |
lemma length_dropWhile_le: "length (dropWhile P xs) \<le> length xs" |
68719 | 2338 |
by (induct xs) auto |
33639
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
hoelzl
parents:
33593
diff
changeset
|
2339 |
|
13142 | 2340 |
lemma dropWhile_append1 [simp]: |
67091 | 2341 |
"\<lbrakk>x \<in> set xs; \<not>P(x)\<rbrakk> \<Longrightarrow> dropWhile P (xs @ ys) = (dropWhile P xs)@ys" |
68719 | 2342 |
by (induct xs) auto |
13114 | 2343 |
|
13142 | 2344 |
lemma dropWhile_append2 [simp]: |
67613 | 2345 |
"(\<And>x. x \<in> set xs \<Longrightarrow> P(x)) ==> dropWhile P (xs @ ys) = dropWhile P ys" |
68719 | 2346 |
by (induct xs) auto |
13114 | 2347 |
|
45841 | 2348 |
lemma dropWhile_append3: |
2349 |
"\<not> P y \<Longrightarrow>dropWhile P (xs @ y # ys) = dropWhile P xs @ y # ys" |
|
68719 | 2350 |
by (induct xs) auto |
45841 | 2351 |
|
2352 |
lemma dropWhile_last: |
|
2353 |
"x \<in> set xs \<Longrightarrow> \<not> P x \<Longrightarrow> last (dropWhile P xs) = last xs" |
|
68719 | 2354 |
by (auto simp add: dropWhile_append3 in_set_conv_decomp) |
45841 | 2355 |
|
2356 |
lemma set_dropWhileD: "x \<in> set (dropWhile P xs) \<Longrightarrow> x \<in> set xs" |
|
68719 | 2357 |
by (induct xs) (auto split: if_split_asm) |
45841 | 2358 |
|
67613 | 2359 |
lemma set_takeWhileD: "x \<in> set (takeWhile P xs) \<Longrightarrow> x \<in> set xs \<and> P x" |
68719 | 2360 |
by (induct xs) (auto split: if_split_asm) |
13114 | 2361 |
|
13913 | 2362 |
lemma takeWhile_eq_all_conv[simp]: |
58807 | 2363 |
"(takeWhile P xs = xs) = (\<forall>x \<in> set xs. P x)" |
68719 | 2364 |
by(induct xs, auto) |
13913 | 2365 |
|
2366 |
lemma dropWhile_eq_Nil_conv[simp]: |
|
58807 | 2367 |
"(dropWhile P xs = []) = (\<forall>x \<in> set xs. P x)" |
68719 | 2368 |
by(induct xs, auto) |
13913 | 2369 |
|
2370 |
lemma dropWhile_eq_Cons_conv: |
|
67091 | 2371 |
"(dropWhile P xs = y#ys) = (xs = takeWhile P xs @ y # ys \<and> \<not> P y)" |
68719 | 2372 |
by(induct xs, auto) |
13913 | 2373 |
|
31077 | 2374 |
lemma distinct_takeWhile[simp]: "distinct xs ==> distinct (takeWhile P xs)" |
68719 | 2375 |
by (induct xs) (auto dest: set_takeWhileD) |
31077 | 2376 |
|
2377 |
lemma distinct_dropWhile[simp]: "distinct xs ==> distinct (dropWhile P xs)" |
|
68719 | 2378 |
by (induct xs) auto |
31077 | 2379 |
|
33639
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
hoelzl
parents:
33593
diff
changeset
|
2380 |
lemma takeWhile_map: "takeWhile P (map f xs) = map f (takeWhile (P \<circ> f) xs)" |
68719 | 2381 |
by (induct xs) auto |
33639
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
hoelzl
parents:
33593
diff
changeset
|
2382 |
|
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
hoelzl
parents:
33593
diff
changeset
|
2383 |
lemma dropWhile_map: "dropWhile P (map f xs) = map f (dropWhile (P \<circ> f) xs)" |
68719 | 2384 |
by (induct xs) auto |
33639
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
hoelzl
parents:
33593
diff
changeset
|
2385 |
|
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
hoelzl
parents:
33593
diff
changeset
|
2386 |
lemma takeWhile_eq_take: "takeWhile P xs = take (length (takeWhile P xs)) xs" |
68719 | 2387 |
by (induct xs) auto |
33639
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
hoelzl
parents:
33593
diff
changeset
|
2388 |
|
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
hoelzl
parents:
33593
diff
changeset
|
2389 |
lemma dropWhile_eq_drop: "dropWhile P xs = drop (length (takeWhile P xs)) xs" |
68719 | 2390 |
by (induct xs) auto |
33639
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
hoelzl
parents:
33593
diff
changeset
|
2391 |
|
58807 | 2392 |
lemma hd_dropWhile: "dropWhile P xs \<noteq> [] \<Longrightarrow> \<not> P (hd (dropWhile P xs))" |
68719 | 2393 |
by (induct xs) auto |
33639
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
hoelzl
parents:
33593
diff
changeset
|
2394 |
|
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
hoelzl
parents:
33593
diff
changeset
|
2395 |
lemma takeWhile_eq_filter: |
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
hoelzl
parents:
33593
diff
changeset
|
2396 |
assumes "\<And> x. x \<in> set (dropWhile P xs) \<Longrightarrow> \<not> P x" |
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
hoelzl
parents:
33593
diff
changeset
|
2397 |
shows "takeWhile P xs = filter P xs" |
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
hoelzl
parents:
33593
diff
changeset
|
2398 |
proof - |
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
hoelzl
parents:
33593
diff
changeset
|
2399 |
have A: "filter P xs = filter P (takeWhile P xs @ dropWhile P xs)" |
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
hoelzl
parents:
33593
diff
changeset
|
2400 |
by simp |
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
hoelzl
parents:
33593
diff
changeset
|
2401 |
have B: "filter P (dropWhile P xs) = []" |
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
hoelzl
parents:
33593
diff
changeset
|
2402 |
unfolding filter_empty_conv using assms by blast |
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
hoelzl
parents:
33593
diff
changeset
|
2403 |
have "filter P xs = takeWhile P xs" |
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
hoelzl
parents:
33593
diff
changeset
|
2404 |
unfolding A filter_append B |
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
hoelzl
parents:
33593
diff
changeset
|
2405 |
by (auto simp add: filter_id_conv dest: set_takeWhileD) |
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
hoelzl
parents:
33593
diff
changeset
|
2406 |
thus ?thesis .. |
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
hoelzl
parents:
33593
diff
changeset
|
2407 |
qed |
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
hoelzl
parents:
33593
diff
changeset
|
2408 |
|
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
hoelzl
parents:
33593
diff
changeset
|
2409 |
lemma takeWhile_eq_take_P_nth: |
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
hoelzl
parents:
33593
diff
changeset
|
2410 |
"\<lbrakk> \<And> i. \<lbrakk> i < n ; i < length xs \<rbrakk> \<Longrightarrow> P (xs ! i) ; n < length xs \<Longrightarrow> \<not> P (xs ! n) \<rbrakk> \<Longrightarrow> |
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
hoelzl
parents:
33593
diff
changeset
|
2411 |
takeWhile P xs = take n xs" |
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
hoelzl
parents:
33593
diff
changeset
|
2412 |
proof (induct xs arbitrary: n) |
60580 | 2413 |
case Nil |
2414 |
thus ?case by simp |
|
2415 |
next |
|
33639
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
hoelzl
parents:
33593
diff
changeset
|
2416 |
case (Cons x xs) |
60580 | 2417 |
show ?case |
33639
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
hoelzl
parents:
33593
diff
changeset
|
2418 |
proof (cases n) |
60580 | 2419 |
case 0 |
2420 |
with Cons show ?thesis by simp |
|
2421 |
next |
|
2422 |
case [simp]: (Suc n') |
|
33639
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
hoelzl
parents:
33593
diff
changeset
|
2423 |
have "P x" using Cons.prems(1)[of 0] by simp |
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
hoelzl
parents:
33593
diff
changeset
|
2424 |
moreover have "takeWhile P xs = take n' xs" |
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
hoelzl
parents:
33593
diff
changeset
|
2425 |
proof (rule Cons.hyps) |
60580 | 2426 |
fix i |
2427 |
assume "i < n'" "i < length xs" |
|
2428 |
thus "P (xs ! i)" using Cons.prems(1)[of "Suc i"] by simp |
|
2429 |
next |
|
2430 |
assume "n' < length xs" |
|
2431 |
thus "\<not> P (xs ! n')" using Cons by auto |
|
33639
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
hoelzl
parents:
33593
diff
changeset
|
2432 |
qed |
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
hoelzl
parents:
33593
diff
changeset
|
2433 |
ultimately show ?thesis by simp |
68719 | 2434 |
qed |
60580 | 2435 |
qed |
33639
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
hoelzl
parents:
33593
diff
changeset
|
2436 |
|
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
hoelzl
parents:
33593
diff
changeset
|
2437 |
lemma nth_length_takeWhile: |
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
hoelzl
parents:
33593
diff
changeset
|
2438 |
"length (takeWhile P xs) < length xs \<Longrightarrow> \<not> P (xs ! length (takeWhile P xs))" |
68719 | 2439 |
by (induct xs) auto |
33639
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
hoelzl
parents:
33593
diff
changeset
|
2440 |
|
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
hoelzl
parents:
33593
diff
changeset
|
2441 |
lemma length_takeWhile_less_P_nth: |
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
hoelzl
parents:
33593
diff
changeset
|
2442 |
assumes all: "\<And> i. i < j \<Longrightarrow> P (xs ! i)" and "j \<le> length xs" |
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
hoelzl
parents:
33593
diff
changeset
|
2443 |
shows "j \<le> length (takeWhile P xs)" |
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
hoelzl
parents:
33593
diff
changeset
|
2444 |
proof (rule classical) |
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
hoelzl
parents:
33593
diff
changeset
|
2445 |
assume "\<not> ?thesis" |
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
hoelzl
parents:
33593
diff
changeset
|
2446 |
hence "length (takeWhile P xs) < length xs" using assms by simp |
60758 | 2447 |
thus ?thesis using all \<open>\<not> ?thesis\<close> nth_length_takeWhile[of P xs] by auto |
33639
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
hoelzl
parents:
33593
diff
changeset
|
2448 |
qed |
31077 | 2449 |
|
17501 | 2450 |
lemma takeWhile_neq_rev: "\<lbrakk>distinct xs; x \<in> set xs\<rbrakk> \<Longrightarrow> |
58807 | 2451 |
takeWhile (\<lambda>y. y \<noteq> x) (rev xs) = rev (tl (dropWhile (\<lambda>y. y \<noteq> x) xs))" |
68719 | 2452 |
by(induct xs) (auto simp: takeWhile_tail[where l="[]"]) |
17501 | 2453 |
|
2454 |
lemma dropWhile_neq_rev: "\<lbrakk>distinct xs; x \<in> set xs\<rbrakk> \<Longrightarrow> |
|
2455 |
dropWhile (\<lambda>y. y \<noteq> x) (rev xs) = x # rev (takeWhile (\<lambda>y. y \<noteq> x) xs)" |
|
68709 | 2456 |
proof (induct xs) |
2457 |
case (Cons a xs) |
|
2458 |
then show ?case |
|
2459 |
by(auto, subst dropWhile_append2, auto) |
|
2460 |
qed simp |
|
17501 | 2461 |
|
18423 | 2462 |
lemma takeWhile_not_last: |
58807 | 2463 |
"distinct xs \<Longrightarrow> takeWhile (\<lambda>y. y \<noteq> last xs) xs = butlast xs" |
68719 | 2464 |
by(induction xs rule: induct_list012) auto |
18423 | 2465 |
|
44013
5cfc1c36ae97
moved recdef package to HOL/Library/Old_Recdef.thy
krauss
parents:
43594
diff
changeset
|
2466 |
lemma takeWhile_cong [fundef_cong]: |
67613 | 2467 |
"\<lbrakk>l = k; \<And>x. x \<in> set l \<Longrightarrow> P x = Q x\<rbrakk> |
2468 |
\<Longrightarrow> takeWhile P l = takeWhile Q k" |
|
68719 | 2469 |
by (induct k arbitrary: l) (simp_all) |
18336
1a2e30b37ed3
Added recdef congruence rules for bounded quantifiers and commonly used
krauss
parents:
18049
diff
changeset
|
2470 |
|
44013
5cfc1c36ae97
moved recdef package to HOL/Library/Old_Recdef.thy
krauss
parents:
43594
diff
changeset
|
2471 |
lemma dropWhile_cong [fundef_cong]: |
67613 | 2472 |
"\<lbrakk>l = k; \<And>x. x \<in> set l \<Longrightarrow> P x = Q x\<rbrakk> |
2473 |
\<Longrightarrow> dropWhile P l = dropWhile Q k" |
|
68719 | 2474 |
by (induct k arbitrary: l, simp_all) |
18336
1a2e30b37ed3
Added recdef congruence rules for bounded quantifiers and commonly used
krauss
parents:
18049
diff
changeset
|
2475 |
|
52380 | 2476 |
lemma takeWhile_idem [simp]: |
2477 |
"takeWhile P (takeWhile P xs) = takeWhile P xs" |
|
68719 | 2478 |
by (induct xs) auto |
52380 | 2479 |
|
2480 |
lemma dropWhile_idem [simp]: |
|
2481 |
"dropWhile P (dropWhile P xs) = dropWhile P xs" |
|
68719 | 2482 |
by (induct xs) auto |
52380 | 2483 |
|
13114 | 2484 |
|
69593 | 2485 |
subsubsection \<open>\<^const>\<open>zip\<close>\<close> |
13114 | 2486 |
|
13142 | 2487 |
lemma zip_Nil [simp]: "zip [] ys = []" |
68719 | 2488 |
by (induct ys) auto |
13114 | 2489 |
|
13142 | 2490 |
lemma zip_Cons_Cons [simp]: "zip (x # xs) (y # ys) = (x, y) # zip xs ys" |
68719 | 2491 |
by simp |
13114 | 2492 |
|
13142 | 2493 |
declare zip_Cons [simp del] |
13114 | 2494 |
|
36198 | 2495 |
lemma [code]: |
2496 |
"zip [] ys = []" |
|
2497 |
"zip xs [] = []" |
|
2498 |
"zip (x # xs) (y # ys) = (x, y) # zip xs ys" |
|
68719 | 2499 |
by (fact zip_Nil zip.simps(1) zip_Cons_Cons)+ |
36198 | 2500 |
|
15281 | 2501 |
lemma zip_Cons1: |
58807 | 2502 |
"zip (x#xs) ys = (case ys of [] \<Rightarrow> [] | y#ys \<Rightarrow> (x,y)#zip xs ys)" |
68719 | 2503 |
by(auto split:list.split) |
15281 | 2504 |
|
13142 | 2505 |
lemma length_zip [simp]: |
58807 | 2506 |
"length (zip xs ys) = min (length xs) (length ys)" |
68719 | 2507 |
by (induct xs ys rule:list_induct2') auto |
13114 | 2508 |
|
34978
874150ddd50a
canonical insert operation; generalized lemma foldl_apply_inv to foldl_apply
haftmann
parents:
34942
diff
changeset
|
2509 |
lemma zip_obtain_same_length: |
874150ddd50a
canonical insert operation; generalized lemma foldl_apply_inv to foldl_apply
haftmann
parents:
34942
diff
changeset
|
2510 |
assumes "\<And>zs ws n. length zs = length ws \<Longrightarrow> n = min (length xs) (length ys) |
874150ddd50a
canonical insert operation; generalized lemma foldl_apply_inv to foldl_apply
haftmann
parents:
34942
diff
changeset
|
2511 |
\<Longrightarrow> zs = take n xs \<Longrightarrow> ws = take n ys \<Longrightarrow> P (zip zs ws)" |
874150ddd50a
canonical insert operation; generalized lemma foldl_apply_inv to foldl_apply
haftmann
parents:
34942
diff
changeset
|
2512 |
shows "P (zip xs ys)" |
874150ddd50a
canonical insert operation; generalized lemma foldl_apply_inv to foldl_apply
haftmann
parents:
34942
diff
changeset
|
2513 |
proof - |
874150ddd50a
canonical insert operation; generalized lemma foldl_apply_inv to foldl_apply
haftmann
parents:
34942
diff
changeset
|
2514 |
let ?n = "min (length xs) (length ys)" |
874150ddd50a
canonical insert operation; generalized lemma foldl_apply_inv to foldl_apply
haftmann
parents:
34942
diff
changeset
|
2515 |
have "P (zip (take ?n xs) (take ?n ys))" |
874150ddd50a
canonical insert operation; generalized lemma foldl_apply_inv to foldl_apply
haftmann
parents:
34942
diff
changeset
|
2516 |
by (rule assms) simp_all |
874150ddd50a
canonical insert operation; generalized lemma foldl_apply_inv to foldl_apply
haftmann
parents:
34942
diff
changeset
|
2517 |
moreover have "zip xs ys = zip (take ?n xs) (take ?n ys)" |
874150ddd50a
canonical insert operation; generalized lemma foldl_apply_inv to foldl_apply
haftmann
parents:
34942
diff
changeset
|
2518 |
proof (induct xs arbitrary: ys) |
874150ddd50a
canonical insert operation; generalized lemma foldl_apply_inv to foldl_apply
haftmann
parents:
34942
diff
changeset
|
2519 |
case Nil then show ?case by simp |
874150ddd50a
canonical insert operation; generalized lemma foldl_apply_inv to foldl_apply
haftmann
parents:
34942
diff
changeset
|
2520 |
next |
874150ddd50a
canonical insert operation; generalized lemma foldl_apply_inv to foldl_apply
haftmann
parents:
34942
diff
changeset
|
2521 |
case (Cons x xs) then show ?case by (cases ys) simp_all |
874150ddd50a
canonical insert operation; generalized lemma foldl_apply_inv to foldl_apply
haftmann
parents:
34942
diff
changeset
|
2522 |
qed |
874150ddd50a
canonical insert operation; generalized lemma foldl_apply_inv to foldl_apply
haftmann
parents:
34942
diff
changeset
|
2523 |
ultimately show ?thesis by simp |
874150ddd50a
canonical insert operation; generalized lemma foldl_apply_inv to foldl_apply
haftmann
parents:
34942
diff
changeset
|
2524 |
qed |
874150ddd50a
canonical insert operation; generalized lemma foldl_apply_inv to foldl_apply
haftmann
parents:
34942
diff
changeset
|
2525 |
|
13114 | 2526 |
lemma zip_append1: |
58807 | 2527 |
"zip (xs @ ys) zs = |
2528 |
zip xs (take (length xs) zs) @ zip ys (drop (length xs) zs)" |
|
68719 | 2529 |
by (induct xs zs rule:list_induct2') auto |
13114 | 2530 |
|
2531 |
lemma zip_append2: |
|
58807 | 2532 |
"zip xs (ys @ zs) = |
2533 |
zip (take (length ys) xs) ys @ zip (drop (length ys) xs) zs" |
|
68719 | 2534 |
by (induct xs ys rule:list_induct2') auto |
13114 | 2535 |
|
13142 | 2536 |
lemma zip_append [simp]: |
58807 | 2537 |
"[| length xs = length us |] ==> |
2538 |
zip (xs@ys) (us@vs) = zip xs us @ zip ys vs" |
|
68719 | 2539 |
by (simp add: zip_append1) |
13114 | 2540 |
|
2541 |
lemma zip_rev: |
|
58807 | 2542 |
"length xs = length ys ==> zip (rev xs) (rev ys) = rev (zip xs ys)" |
68719 | 2543 |
by (induct rule:list_induct2, simp_all) |
13114 | 2544 |
|
33639
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
hoelzl
parents:
33593
diff
changeset
|
2545 |
lemma zip_map_map: |
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
hoelzl
parents:
33593
diff
changeset
|
2546 |
"zip (map f xs) (map g ys) = map (\<lambda> (x, y). (f x, g y)) (zip xs ys)" |
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
hoelzl
parents:
33593
diff
changeset
|
2547 |
proof (induct xs arbitrary: ys) |
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
hoelzl
parents:
33593
diff
changeset
|
2548 |
case (Cons x xs) note Cons_x_xs = Cons.hyps |
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
hoelzl
parents:
33593
diff
changeset
|
2549 |
show ?case |
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
hoelzl
parents:
33593
diff
changeset
|
2550 |
proof (cases ys) |
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
hoelzl
parents:
33593
diff
changeset
|
2551 |
case (Cons y ys') |
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
hoelzl
parents:
33593
diff
changeset
|
2552 |
show ?thesis unfolding Cons using Cons_x_xs by simp |
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
hoelzl
parents:
33593
diff
changeset
|
2553 |
qed simp |
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
hoelzl
parents:
33593
diff
changeset
|
2554 |
qed simp |
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
hoelzl
parents:
33593
diff
changeset
|
2555 |
|
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
hoelzl
parents:
33593
diff
changeset
|
2556 |
lemma zip_map1: |
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
hoelzl
parents:
33593
diff
changeset
|
2557 |
"zip (map f xs) ys = map (\<lambda>(x, y). (f x, y)) (zip xs ys)" |
68719 | 2558 |
using zip_map_map[of f xs "\<lambda>x. x" ys] by simp |
33639
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
hoelzl
parents:
33593
diff
changeset
|
2559 |
|
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
hoelzl
parents:
33593
diff
changeset
|
2560 |
lemma zip_map2: |
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
hoelzl
parents:
33593
diff
changeset
|
2561 |
"zip xs (map f ys) = map (\<lambda>(x, y). (x, f y)) (zip xs ys)" |
68719 | 2562 |
using zip_map_map[of "\<lambda>x. x" xs f ys] by simp |
33639
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
hoelzl
parents:
33593
diff
changeset
|
2563 |
|
23096 | 2564 |
lemma map_zip_map: |
33639
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
hoelzl
parents:
33593
diff
changeset
|
2565 |
"map f (zip (map g xs) ys) = map (%(x,y). f(g x, y)) (zip xs ys)" |
68719 | 2566 |
by (auto simp: zip_map1) |
23096 | 2567 |
|
2568 |
lemma map_zip_map2: |
|
33639
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
hoelzl
parents:
33593
diff
changeset
|
2569 |
"map f (zip xs (map g ys)) = map (%(x,y). f(x, g y)) (zip xs ys)" |
68719 | 2570 |
by (auto simp: zip_map2) |
23096 | 2571 |
|
60758 | 2572 |
text\<open>Courtesy of Andreas Lochbihler:\<close> |
31080 | 2573 |
lemma zip_same_conv_map: "zip xs xs = map (\<lambda>x. (x, x)) xs" |
68719 | 2574 |
by(induct xs) auto |
31080 | 2575 |
|
13142 | 2576 |
lemma nth_zip [simp]: |
58807 | 2577 |
"[| i < length xs; i < length ys|] ==> (zip xs ys)!i = (xs!i, ys!i)" |
68709 | 2578 |
proof (induct ys arbitrary: i xs) |
2579 |
case (Cons y ys) |
|
2580 |
then show ?case |
|
2581 |
by (cases xs) (simp_all add: nth.simps split: nat.split) |
|
2582 |
qed auto |
|
13114 | 2583 |
|
2584 |
lemma set_zip: |
|
58807 | 2585 |
"set (zip xs ys) = {(xs!i, ys!i) | i. i < min (length xs) (length ys)}" |
68719 | 2586 |
by(simp add: set_conv_nth cong: rev_conj_cong) |
13114 | 2587 |
|
33639
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
hoelzl
parents:
33593
diff
changeset
|
2588 |
lemma zip_same: "((a,b) \<in> set (zip xs xs)) = (a \<in> set xs \<and> a = b)" |
68719 | 2589 |
by(induct xs) auto |
33639
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
hoelzl
parents:
33593
diff
changeset
|
2590 |
|
68709 | 2591 |
lemma zip_update: "zip (xs[i:=x]) (ys[i:=y]) = (zip xs ys)[i:=(x,y)]" |
2592 |
by (simp add: update_zip) |
|
13114 | 2593 |
|
13142 | 2594 |
lemma zip_replicate [simp]: |
24526 | 2595 |
"zip (replicate i x) (replicate j y) = replicate (min i j) (x,y)" |
68709 | 2596 |
proof (induct i arbitrary: j) |
2597 |
case (Suc i) |
|
2598 |
then show ?case |
|
2599 |
by (cases j, auto) |
|
2600 |
qed auto |
|
13114 | 2601 |
|
61630 | 2602 |
lemma zip_replicate1: "zip (replicate n x) ys = map (Pair x) (take n ys)" |
68719 | 2603 |
by(induction ys arbitrary: n)(case_tac [2] n, simp_all) |
61630 | 2604 |
|
68709 | 2605 |
lemma take_zip: "take n (zip xs ys) = zip (take n xs) (take n ys)" |
2606 |
proof (induct n arbitrary: xs ys) |
|
69850 | 2607 |
case 0 |
2608 |
then show ?case by simp |
|
2609 |
next |
|
2610 |
case Suc |
|
2611 |
then show ?case by (cases xs; cases ys) simp_all |
|
2612 |
qed |
|
68709 | 2613 |
|
2614 |
lemma drop_zip: "drop n (zip xs ys) = zip (drop n xs) (drop n ys)" |
|
2615 |
proof (induct n arbitrary: xs ys) |
|
69850 | 2616 |
case 0 |
2617 |
then show ?case by simp |
|
2618 |
next |
|
2619 |
case Suc |
|
2620 |
then show ?case by (cases xs; cases ys) simp_all |
|
2621 |
qed |
|
19487 | 2622 |
|
33639
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
hoelzl
parents:
33593
diff
changeset
|
2623 |
lemma zip_takeWhile_fst: "zip (takeWhile P xs) ys = takeWhile (P \<circ> fst) (zip xs ys)" |
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
hoelzl
parents:
33593
diff
changeset
|
2624 |
proof (induct xs arbitrary: ys) |
69850 | 2625 |
case Nil |
2626 |
then show ?case by simp |
|
2627 |
next |
|
2628 |
case Cons |
|
2629 |
then show ?case by (cases ys) auto |
|
2630 |
qed |
|
33639
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
hoelzl
parents:
33593
diff
changeset
|
2631 |
|
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
hoelzl
parents:
33593
diff
changeset
|
2632 |
lemma zip_takeWhile_snd: "zip xs (takeWhile P ys) = takeWhile (P \<circ> snd) (zip xs ys)" |
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
hoelzl
parents:
33593
diff
changeset
|
2633 |
proof (induct xs arbitrary: ys) |
69850 | 2634 |
case Nil |
2635 |
then show ?case by simp |
|
2636 |
next |
|
2637 |
case Cons |
|
2638 |
then show ?case by (cases ys) auto |
|
2639 |
qed |
|
33639
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
hoelzl
parents:
33593
diff
changeset
|
2640 |
|
58807 | 2641 |
lemma set_zip_leftD: "(x,y)\<in> set (zip xs ys) \<Longrightarrow> x \<in> set xs" |
68719 | 2642 |
by (induct xs ys rule:list_induct2') auto |
22493
db930e490fe5
added another rule for simultaneous induction, and lemmas for zip
krauss
parents:
22422
diff
changeset
|
2643 |
|
58807 | 2644 |
lemma set_zip_rightD: "(x,y)\<in> set (zip xs ys) \<Longrightarrow> y \<in> set ys" |
68719 | 2645 |
by (induct xs ys rule:list_induct2') auto |
13142 | 2646 |
|
23983 | 2647 |
lemma in_set_zipE: |
67613 | 2648 |
"(x,y) \<in> set(zip xs ys) \<Longrightarrow> (\<lbrakk> x \<in> set xs; y \<in> set ys \<rbrakk> \<Longrightarrow> R) \<Longrightarrow> R" |
68719 | 2649 |
by(blast dest: set_zip_leftD set_zip_rightD) |
23983 | 2650 |
|
58807 | 2651 |
lemma zip_map_fst_snd: "zip (map fst zs) (map snd zs) = zs" |
68719 | 2652 |
by (induct zs) simp_all |
29829 | 2653 |
|
2654 |
lemma zip_eq_conv: |
|
2655 |
"length xs = length ys \<Longrightarrow> zip xs ys = zs \<longleftrightarrow> map fst zs = xs \<and> map snd zs = ys" |
|
68719 | 2656 |
by (auto simp add: zip_map_fst_snd) |
29829 | 2657 |
|
51173 | 2658 |
lemma in_set_zip: |
2659 |
"p \<in> set (zip xs ys) \<longleftrightarrow> (\<exists>n. xs ! n = fst p \<and> ys ! n = snd p |
|
58807 | 2660 |
\<and> n < length xs \<and> n < length ys)" |
68719 | 2661 |
by (cases p) (auto simp add: set_zip) |
51173 | 2662 |
|
66584 | 2663 |
lemma in_set_impl_in_set_zip1: |
2664 |
assumes "length xs = length ys" |
|
2665 |
assumes "x \<in> set xs" |
|
2666 |
obtains y where "(x, y) \<in> set (zip xs ys)" |
|
2667 |
proof - |
|
2668 |
from assms have "x \<in> set (map fst (zip xs ys))" by simp |
|
2669 |
from this that show ?thesis by fastforce |
|
2670 |
qed |
|
2671 |
||
2672 |
lemma in_set_impl_in_set_zip2: |
|
2673 |
assumes "length xs = length ys" |
|
2674 |
assumes "y \<in> set ys" |
|
2675 |
obtains x where "(x, y) \<in> set (zip xs ys)" |
|
2676 |
proof - |
|
2677 |
from assms have "y \<in> set (map snd (zip xs ys))" by simp |
|
2678 |
from this that show ?thesis by fastforce |
|
2679 |
qed |
|
2680 |
||
70226 | 2681 |
lemma zip_eq_Nil_iff: |
2682 |
"zip xs ys = [] \<longleftrightarrow> xs = [] \<or> ys = []" |
|
2683 |
by (cases xs; cases ys) simp_all |
|
2684 |
||
2685 |
lemma zip_eq_ConsE: |
|
2686 |
assumes "zip xs ys = xy # xys" |
|
2687 |
obtains x xs' y ys' where "xs = x # xs'" |
|
2688 |
and "ys = y # ys'" and "xy = (x, y)" |
|
2689 |
and "xys = zip xs' ys'" |
|
2690 |
proof - |
|
2691 |
from assms have "xs \<noteq> []" and "ys \<noteq> []" |
|
2692 |
using zip_eq_Nil_iff [of xs ys] by simp_all |
|
2693 |
then obtain x xs' y ys' where xs: "xs = x # xs'" |
|
2694 |
and ys: "ys = y # ys'" |
|
2695 |
by (cases xs; cases ys) auto |
|
2696 |
with assms have "xy = (x, y)" and "xys = zip xs' ys'" |
|
2697 |
by simp_all |
|
2698 |
with xs ys show ?thesis .. |
|
2699 |
qed |
|
2700 |
||
70911 | 2701 |
lemma semilattice_map2: |
2702 |
"semilattice (map2 (\<^bold>*))" if "semilattice (\<^bold>*)" |
|
2703 |
for f (infixl "\<^bold>*" 70) |
|
2704 |
proof - |
|
2705 |
from that interpret semilattice f . |
|
2706 |
show ?thesis |
|
2707 |
proof |
|
2708 |
show "map2 (\<^bold>*) (map2 (\<^bold>*) xs ys) zs = map2 (\<^bold>*) xs (map2 (\<^bold>*) ys zs)" |
|
2709 |
for xs ys zs :: "'a list" |
|
2710 |
proof (induction "zip xs (zip ys zs)" arbitrary: xs ys zs) |
|
2711 |
case Nil |
|
2712 |
from Nil [symmetric] show ?case |
|
2713 |
by (auto simp add: zip_eq_Nil_iff) |
|
2714 |
next |
|
2715 |
case (Cons xyz xyzs) |
|
2716 |
from Cons.hyps(2) [symmetric] show ?case |
|
2717 |
by (rule zip_eq_ConsE) (erule zip_eq_ConsE, |
|
2718 |
auto intro: Cons.hyps(1) simp add: ac_simps) |
|
2719 |
qed |
|
2720 |
show "map2 (\<^bold>*) xs ys = map2 (\<^bold>*) ys xs" |
|
2721 |
for xs ys :: "'a list" |
|
2722 |
proof (induction "zip xs ys" arbitrary: xs ys) |
|
2723 |
case Nil |
|
2724 |
then show ?case |
|
2725 |
by (auto simp add: zip_eq_Nil_iff dest: sym) |
|
2726 |
next |
|
2727 |
case (Cons xy xys) |
|
2728 |
from Cons.hyps(2) [symmetric] show ?case |
|
2729 |
by (rule zip_eq_ConsE) (auto intro: Cons.hyps(1) simp add: ac_simps) |
|
2730 |
qed |
|
2731 |
show "map2 (\<^bold>*) xs xs = xs" |
|
2732 |
for xs :: "'a list" |
|
2733 |
by (induction xs) simp_all |
|
2734 |
qed |
|
2735 |
qed |
|
2736 |
||
51173 | 2737 |
lemma pair_list_eqI: |
2738 |
assumes "map fst xs = map fst ys" and "map snd xs = map snd ys" |
|
2739 |
shows "xs = ys" |
|
2740 |
proof - |
|
2741 |
from assms(1) have "length xs = length ys" by (rule map_eq_imp_length_eq) |
|
2742 |
from this assms show ?thesis |
|
2743 |
by (induct xs ys rule: list_induct2) (simp_all add: prod_eqI) |
|
2744 |
qed |
|
2745 |
||
35115 | 2746 |
|
69593 | 2747 |
subsubsection \<open>\<^const>\<open>list_all2\<close>\<close> |
13114 | 2748 |
|
64963 | 2749 |
lemma list_all2_lengthD [intro?]: |
14316
91b897b9a2dc
added some [intro?] and [trans] for list_all2 lemmas
kleing
parents:
14302
diff
changeset
|
2750 |
"list_all2 P xs ys ==> length xs = length ys" |
68719 | 2751 |
by (simp add: list_all2_iff) |
19607
07eeb832f28d
introduced characters for code generator; some improved code lemmas for some list functions
haftmann
parents:
19585
diff
changeset
|
2752 |
|
19787 | 2753 |
lemma list_all2_Nil [iff, code]: "list_all2 P [] ys = (ys = [])" |
68719 | 2754 |
by (simp add: list_all2_iff) |
19607
07eeb832f28d
introduced characters for code generator; some improved code lemmas for some list functions
haftmann
parents:
19585
diff
changeset
|
2755 |
|
19787 | 2756 |
lemma list_all2_Nil2 [iff, code]: "list_all2 P xs [] = (xs = [])" |
68719 | 2757 |
by (simp add: list_all2_iff) |
19607
07eeb832f28d
introduced characters for code generator; some improved code lemmas for some list functions
haftmann
parents:
19585
diff
changeset
|
2758 |
|
07eeb832f28d
introduced characters for code generator; some improved code lemmas for some list functions
haftmann
parents:
19585
diff
changeset
|
2759 |
lemma list_all2_Cons [iff, code]: |
07eeb832f28d
introduced characters for code generator; some improved code lemmas for some list functions
haftmann
parents:
19585
diff
changeset
|
2760 |
"list_all2 P (x # xs) (y # ys) = (P x y \<and> list_all2 P xs ys)" |
68719 | 2761 |
by (auto simp add: list_all2_iff) |
13114 | 2762 |
|
2763 |
lemma list_all2_Cons1: |
|
58807 | 2764 |
"list_all2 P (x # xs) ys = (\<exists>z zs. ys = z # zs \<and> P x z \<and> list_all2 P xs zs)" |
68719 | 2765 |
by (cases ys) auto |
13114 | 2766 |
|
2767 |
lemma list_all2_Cons2: |
|
58807 | 2768 |
"list_all2 P xs (y # ys) = (\<exists>z zs. xs = z # zs \<and> P z y \<and> list_all2 P zs ys)" |
68719 | 2769 |
by (cases xs) auto |
13114 | 2770 |
|
45794 | 2771 |
lemma list_all2_induct |
2772 |
[consumes 1, case_names Nil Cons, induct set: list_all2]: |
|
2773 |
assumes P: "list_all2 P xs ys" |
|
2774 |
assumes Nil: "R [] []" |
|
47640 | 2775 |
assumes Cons: "\<And>x xs y ys. |
2776 |
\<lbrakk>P x y; list_all2 P xs ys; R xs ys\<rbrakk> \<Longrightarrow> R (x # xs) (y # ys)" |
|
45794 | 2777 |
shows "R xs ys" |
68719 | 2778 |
using P |
2779 |
by (induct xs arbitrary: ys) (auto simp add: list_all2_Cons1 Nil Cons) |
|
45794 | 2780 |
|
13142 | 2781 |
lemma list_all2_rev [iff]: |
58807 | 2782 |
"list_all2 P (rev xs) (rev ys) = list_all2 P xs ys" |
68719 | 2783 |
by (simp add: list_all2_iff zip_rev cong: conj_cong) |
13114 | 2784 |
|
13863 | 2785 |
lemma list_all2_rev1: |
58807 | 2786 |
"list_all2 P (rev xs) ys = list_all2 P xs (rev ys)" |
68719 | 2787 |
by (subst list_all2_rev [symmetric]) simp |
13863 | 2788 |
|
13114 | 2789 |
lemma list_all2_append1: |
58807 | 2790 |
"list_all2 P (xs @ ys) zs = |
67091 | 2791 |
(\<exists>us vs. zs = us @ vs \<and> length us = length xs \<and> length vs = length ys \<and> |
68709 | 2792 |
list_all2 P xs us \<and> list_all2 P ys vs)" (is "?lhs = ?rhs") |
2793 |
proof |
|
2794 |
assume ?lhs |
|
2795 |
then show ?rhs |
|
2796 |
apply (rule_tac x = "take (length xs) zs" in exI) |
|
2797 |
apply (rule_tac x = "drop (length xs) zs" in exI) |
|
2798 |
apply (force split: nat_diff_split simp add: list_all2_iff zip_append1) |
|
2799 |
done |
|
2800 |
next |
|
2801 |
assume ?rhs |
|
2802 |
then show ?lhs |
|
2803 |
by (auto simp add: list_all2_iff) |
|
2804 |
qed |
|
13114 | 2805 |
|
2806 |
lemma list_all2_append2: |
|
58807 | 2807 |
"list_all2 P xs (ys @ zs) = |
67091 | 2808 |
(\<exists>us vs. xs = us @ vs \<and> length us = length ys \<and> length vs = length zs \<and> |
68709 | 2809 |
list_all2 P us ys \<and> list_all2 P vs zs)" (is "?lhs = ?rhs") |
2810 |
proof |
|
2811 |
assume ?lhs |
|
2812 |
then show ?rhs |
|
2813 |
apply (rule_tac x = "take (length ys) xs" in exI) |
|
2814 |
apply (rule_tac x = "drop (length ys) xs" in exI) |
|
2815 |
apply (force split: nat_diff_split simp add: list_all2_iff zip_append2) |
|
2816 |
done |
|
2817 |
next |
|
2818 |
assume ?rhs |
|
2819 |
then show ?lhs |
|
2820 |
by (auto simp add: list_all2_iff) |
|
2821 |
qed |
|
13114 | 2822 |
|
13863 | 2823 |
lemma list_all2_append: |
14247 | 2824 |
"length xs = length ys \<Longrightarrow> |
2825 |
list_all2 P (xs@us) (ys@vs) = (list_all2 P xs ys \<and> list_all2 P us vs)" |
|
68719 | 2826 |
by (induct rule:list_induct2, simp_all) |
13863 | 2827 |
|
2828 |
lemma list_all2_appendI [intro?, trans]: |
|
2829 |
"\<lbrakk> list_all2 P a b; list_all2 P c d \<rbrakk> \<Longrightarrow> list_all2 P (a@c) (b@d)" |
|
68719 | 2830 |
by (simp add: list_all2_append list_all2_lengthD) |
13863 | 2831 |
|
13114 | 2832 |
lemma list_all2_conv_all_nth: |
58807 | 2833 |
"list_all2 P xs ys = |
2834 |
(length xs = length ys \<and> (\<forall>i < length xs. P (xs!i) (ys!i)))" |
|
68719 | 2835 |
by (force simp add: list_all2_iff set_zip) |
13114 | 2836 |
|
13883
0451e0fb3f22
Re-structured some proofs in order to get rid of rule_format attribute.
berghofe
parents:
13863
diff
changeset
|
2837 |
lemma list_all2_trans: |
0451e0fb3f22
Re-structured some proofs in order to get rid of rule_format attribute.
berghofe
parents:
13863
diff
changeset
|
2838 |
assumes tr: "!!a b c. P1 a b ==> P2 b c ==> P3 a c" |
0451e0fb3f22
Re-structured some proofs in order to get rid of rule_format attribute.
berghofe
parents:
13863
diff
changeset
|
2839 |
shows "!!bs cs. list_all2 P1 as bs ==> list_all2 P2 bs cs ==> list_all2 P3 as cs" |
68719 | 2840 |
(is "!!bs cs. PROP ?Q as bs cs") |
13883
0451e0fb3f22
Re-structured some proofs in order to get rid of rule_format attribute.
berghofe
parents:
13863
diff
changeset
|
2841 |
proof (induct as) |
0451e0fb3f22
Re-structured some proofs in order to get rid of rule_format attribute.
berghofe
parents:
13863
diff
changeset
|
2842 |
fix x xs bs assume I1: "!!bs cs. PROP ?Q xs bs cs" |
0451e0fb3f22
Re-structured some proofs in order to get rid of rule_format attribute.
berghofe
parents:
13863
diff
changeset
|
2843 |
show "!!cs. PROP ?Q (x # xs) bs cs" |
0451e0fb3f22
Re-structured some proofs in order to get rid of rule_format attribute.
berghofe
parents:
13863
diff
changeset
|
2844 |
proof (induct bs) |
0451e0fb3f22
Re-structured some proofs in order to get rid of rule_format attribute.
berghofe
parents:
13863
diff
changeset
|
2845 |
fix y ys cs assume I2: "!!cs. PROP ?Q (x # xs) ys cs" |
0451e0fb3f22
Re-structured some proofs in order to get rid of rule_format attribute.
berghofe
parents:
13863
diff
changeset
|
2846 |
show "PROP ?Q (x # xs) (y # ys) cs" |
0451e0fb3f22
Re-structured some proofs in order to get rid of rule_format attribute.
berghofe
parents:
13863
diff
changeset
|
2847 |
by (induct cs) (auto intro: tr I1 I2) |
0451e0fb3f22
Re-structured some proofs in order to get rid of rule_format attribute.
berghofe
parents:
13863
diff
changeset
|
2848 |
qed simp |
0451e0fb3f22
Re-structured some proofs in order to get rid of rule_format attribute.
berghofe
parents:
13863
diff
changeset
|
2849 |
qed simp |
0451e0fb3f22
Re-structured some proofs in order to get rid of rule_format attribute.
berghofe
parents:
13863
diff
changeset
|
2850 |
|
13863 | 2851 |
lemma list_all2_all_nthI [intro?]: |
2852 |
"length a = length b \<Longrightarrow> (\<And>n. n < length a \<Longrightarrow> P (a!n) (b!n)) \<Longrightarrow> list_all2 P a b" |
|
68719 | 2853 |
by (simp add: list_all2_conv_all_nth) |
13863 | 2854 |
|
14395 | 2855 |
lemma list_all2I: |
61032
b57df8eecad6
standardized some occurences of ancient "split" alias
haftmann
parents:
61031
diff
changeset
|
2856 |
"\<forall>x \<in> set (zip a b). case_prod P x \<Longrightarrow> length a = length b \<Longrightarrow> list_all2 P a b" |
68719 | 2857 |
by (simp add: list_all2_iff) |
14395 | 2858 |
|
14328 | 2859 |
lemma list_all2_nthD: |
13863 | 2860 |
"\<lbrakk> list_all2 P xs ys; p < size xs \<rbrakk> \<Longrightarrow> P (xs!p) (ys!p)" |
68719 | 2861 |
by (simp add: list_all2_conv_all_nth) |
13863 | 2862 |
|
14302 | 2863 |
lemma list_all2_nthD2: |
2864 |
"\<lbrakk>list_all2 P xs ys; p < size ys\<rbrakk> \<Longrightarrow> P (xs!p) (ys!p)" |
|
68719 | 2865 |
by (frule list_all2_lengthD) (auto intro: list_all2_nthD) |
14302 | 2866 |
|
64963 | 2867 |
lemma list_all2_map1: |
13863 | 2868 |
"list_all2 P (map f as) bs = list_all2 (\<lambda>x y. P (f x) y) as bs" |
68719 | 2869 |
by (simp add: list_all2_conv_all_nth) |
13863 | 2870 |
|
64963 | 2871 |
lemma list_all2_map2: |
13863 | 2872 |
"list_all2 P as (map f bs) = list_all2 (\<lambda>x y. P x (f y)) as bs" |
68719 | 2873 |
by (auto simp add: list_all2_conv_all_nth) |
13863 | 2874 |
|
14316
91b897b9a2dc
added some [intro?] and [trans] for list_all2 lemmas
kleing
parents:
14302
diff
changeset
|
2875 |
lemma list_all2_refl [intro?]: |
13863 | 2876 |
"(\<And>x. P x x) \<Longrightarrow> list_all2 P xs xs" |
68719 | 2877 |
by (simp add: list_all2_conv_all_nth) |
13863 | 2878 |
|
2879 |
lemma list_all2_update_cong: |
|
46669
c1d2ab32174a
one general list_all2_update_cong instead of two special ones
bulwahn
parents:
46664
diff
changeset
|
2880 |
"\<lbrakk> list_all2 P xs ys; P x y \<rbrakk> \<Longrightarrow> list_all2 P (xs[i:=x]) (ys[i:=y])" |
68719 | 2881 |
by (cases "i < length ys") (auto simp add: list_all2_conv_all_nth nth_list_update) |
13863 | 2882 |
|
14302 | 2883 |
lemma list_all2_takeI [simp,intro?]: |
24526 | 2884 |
"list_all2 P xs ys \<Longrightarrow> list_all2 P (take n xs) (take n ys)" |
68709 | 2885 |
proof (induct xs arbitrary: n ys) |
2886 |
case (Cons x xs) |
|
2887 |
then show ?case |
|
2888 |
by (cases n) (auto simp: list_all2_Cons1) |
|
2889 |
qed auto |
|
14302 | 2890 |
|
2891 |
lemma list_all2_dropI [simp,intro?]: |
|
68709 | 2892 |
"list_all2 P xs ys \<Longrightarrow> list_all2 P (drop n xs) (drop n ys)" |
2893 |
proof (induct xs arbitrary: n ys) |
|
2894 |
case (Cons x xs) |
|
2895 |
then show ?case |
|
2896 |
by (cases n) (auto simp: list_all2_Cons1) |
|
2897 |
qed auto |
|
13863 | 2898 |
|
14327 | 2899 |
lemma list_all2_mono [intro?]: |
24526 | 2900 |
"list_all2 P xs ys \<Longrightarrow> (\<And>xs ys. P xs ys \<Longrightarrow> Q xs ys) \<Longrightarrow> list_all2 Q xs ys" |
68709 | 2901 |
by (rule list.rel_mono_strong) |
13863 | 2902 |
|
22551 | 2903 |
lemma list_all2_eq: |
67399 | 2904 |
"xs = ys \<longleftrightarrow> list_all2 (=) xs ys" |
68719 | 2905 |
by (induct xs ys rule: list_induct2') auto |
22551 | 2906 |
|
40230 | 2907 |
lemma list_eq_iff_zip_eq: |
2908 |
"xs = ys \<longleftrightarrow> length xs = length ys \<and> (\<forall>(x,y) \<in> set (zip xs ys). x = y)" |
|
68719 | 2909 |
by(auto simp add: set_zip list_all2_eq list_all2_conv_all_nth cong: conj_cong) |
40230 | 2910 |
|
57308 | 2911 |
lemma list_all2_same: "list_all2 P xs xs \<longleftrightarrow> (\<forall>x\<in>set xs. P x x)" |
68719 | 2912 |
by(auto simp add: list_all2_conv_all_nth set_conv_nth) |
13142 | 2913 |
|
61630 | 2914 |
lemma zip_assoc: |
2915 |
"zip xs (zip ys zs) = map (\<lambda>((x, y), z). (x, y, z)) (zip (zip xs ys) zs)" |
|
68719 | 2916 |
by(rule list_all2_all_nthI[where P="(=)", unfolded list.rel_eq]) simp_all |
61630 | 2917 |
|
2918 |
lemma zip_commute: "zip xs ys = map (\<lambda>(x, y). (y, x)) (zip ys xs)" |
|
68719 | 2919 |
by(rule list_all2_all_nthI[where P="(=)", unfolded list.rel_eq]) simp_all |
61630 | 2920 |
|
2921 |
lemma zip_left_commute: |
|
2922 |
"zip xs (zip ys zs) = map (\<lambda>(y, (x, z)). (x, y, z)) (zip ys (zip xs zs))" |
|
68719 | 2923 |
by(rule list_all2_all_nthI[where P="(=)", unfolded list.rel_eq]) simp_all |
61630 | 2924 |
|
2925 |
lemma zip_replicate2: "zip xs (replicate n y) = map (\<lambda>x. (x, y)) (take n xs)" |
|
68719 | 2926 |
by(subst zip_commute)(simp add: zip_replicate1) |
61630 | 2927 |
|
69593 | 2928 |
subsubsection \<open>\<^const>\<open>List.product\<close> and \<^const>\<open>product_lists\<close>\<close> |
49948
744934b818c7
moved quite generic material from theory Enum to more appropriate places
haftmann
parents:
49808
diff
changeset
|
2929 |
|
63720 | 2930 |
lemma product_concat_map: |
2931 |
"List.product xs ys = concat (map (\<lambda>x. map (\<lambda>y. (x,y)) ys) xs)" |
|
68719 | 2932 |
by(induction xs) (simp)+ |
63720 | 2933 |
|
58807 | 2934 |
lemma set_product[simp]: "set (List.product xs ys) = set xs \<times> set ys" |
68719 | 2935 |
by (induct xs) auto |
49948
744934b818c7
moved quite generic material from theory Enum to more appropriate places
haftmann
parents:
49808
diff
changeset
|
2936 |
|
51160
599ff65b85e2
systematic conversions between nat and nibble/char;
haftmann
parents:
51112
diff
changeset
|
2937 |
lemma length_product [simp]: |
599ff65b85e2
systematic conversions between nat and nibble/char;
haftmann
parents:
51112
diff
changeset
|
2938 |
"length (List.product xs ys) = length xs * length ys" |
68719 | 2939 |
by (induct xs) simp_all |
51160
599ff65b85e2
systematic conversions between nat and nibble/char;
haftmann
parents:
51112
diff
changeset
|
2940 |
|
599ff65b85e2
systematic conversions between nat and nibble/char;
haftmann
parents:
51112
diff
changeset
|
2941 |
lemma product_nth: |
599ff65b85e2
systematic conversions between nat and nibble/char;
haftmann
parents:
51112
diff
changeset
|
2942 |
assumes "n < length xs * length ys" |
599ff65b85e2
systematic conversions between nat and nibble/char;
haftmann
parents:
51112
diff
changeset
|
2943 |
shows "List.product xs ys ! n = (xs ! (n div length ys), ys ! (n mod length ys))" |
68719 | 2944 |
using assms proof (induct xs arbitrary: n) |
51160
599ff65b85e2
systematic conversions between nat and nibble/char;
haftmann
parents:
51112
diff
changeset
|
2945 |
case Nil then show ?case by simp |
599ff65b85e2
systematic conversions between nat and nibble/char;
haftmann
parents:
51112
diff
changeset
|
2946 |
next |
599ff65b85e2
systematic conversions between nat and nibble/char;
haftmann
parents:
51112
diff
changeset
|
2947 |
case (Cons x xs n) |
599ff65b85e2
systematic conversions between nat and nibble/char;
haftmann
parents:
51112
diff
changeset
|
2948 |
then have "length ys > 0" by auto |
599ff65b85e2
systematic conversions between nat and nibble/char;
haftmann
parents:
51112
diff
changeset
|
2949 |
with Cons show ?case |
599ff65b85e2
systematic conversions between nat and nibble/char;
haftmann
parents:
51112
diff
changeset
|
2950 |
by (auto simp add: nth_append not_less le_mod_geq le_div_geq) |
599ff65b85e2
systematic conversions between nat and nibble/char;
haftmann
parents:
51112
diff
changeset
|
2951 |
qed |
599ff65b85e2
systematic conversions between nat and nibble/char;
haftmann
parents:
51112
diff
changeset
|
2952 |
|
64963 | 2953 |
lemma in_set_product_lists_length: |
53721
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
traytel
parents:
53689
diff
changeset
|
2954 |
"xs \<in> set (product_lists xss) \<Longrightarrow> length xs = length xss" |
68719 | 2955 |
by (induct xss arbitrary: xs) auto |
53721
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
traytel
parents:
53689
diff
changeset
|
2956 |
|
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
traytel
parents:
53689
diff
changeset
|
2957 |
lemma product_lists_set: |
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
traytel
parents:
53689
diff
changeset
|
2958 |
"set (product_lists xss) = {xs. list_all2 (\<lambda>x ys. x \<in> set ys) xs xss}" (is "?L = Collect ?R") |
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
traytel
parents:
53689
diff
changeset
|
2959 |
proof (intro equalityI subsetI, unfold mem_Collect_eq) |
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
traytel
parents:
53689
diff
changeset
|
2960 |
fix xs assume "xs \<in> ?L" |
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
traytel
parents:
53689
diff
changeset
|
2961 |
then have "length xs = length xss" by (rule in_set_product_lists_length) |
60758 | 2962 |
from this \<open>xs \<in> ?L\<close> show "?R xs" by (induct xs xss rule: list_induct2) auto |
53721
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
traytel
parents:
53689
diff
changeset
|
2963 |
next |
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
traytel
parents:
53689
diff
changeset
|
2964 |
fix xs assume "?R xs" |
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
traytel
parents:
53689
diff
changeset
|
2965 |
then show "xs \<in> ?L" by induct auto |
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
traytel
parents:
53689
diff
changeset
|
2966 |
qed |
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
traytel
parents:
53689
diff
changeset
|
2967 |
|
49948
744934b818c7
moved quite generic material from theory Enum to more appropriate places
haftmann
parents:
49808
diff
changeset
|
2968 |
|
69593 | 2969 |
subsubsection \<open>\<^const>\<open>fold\<close> with natural argument order\<close> |
60758 | 2970 |
|
61799 | 2971 |
lemma fold_simps [code]: \<comment> \<open>eta-expanded variant for generated code -- enables tail-recursion optimisation in Scala\<close> |
48828
441a4eed7823
prefer eta-expanded code equations for fold, to accomodate tail recursion optimisation in Scala
haftmann
parents:
48619
diff
changeset
|
2972 |
"fold f [] s = s" |
64963 | 2973 |
"fold f (x # xs) s = fold f xs (f x s)" |
68719 | 2974 |
by simp_all |
48828
441a4eed7823
prefer eta-expanded code equations for fold, to accomodate tail recursion optimisation in Scala
haftmann
parents:
48619
diff
changeset
|
2975 |
|
46133
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
haftmann
parents:
46125
diff
changeset
|
2976 |
lemma fold_remove1_split: |
58807 | 2977 |
"\<lbrakk> \<And>x y. x \<in> set xs \<Longrightarrow> y \<in> set xs \<Longrightarrow> f x \<circ> f y = f y \<circ> f x; |
2978 |
x \<in> set xs \<rbrakk> |
|
2979 |
\<Longrightarrow> fold f xs = fold f (remove1 x xs) \<circ> f x" |
|
68719 | 2980 |
by (induct xs) (auto simp add: comp_assoc) |
46133
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
haftmann
parents:
46125
diff
changeset
|
2981 |
|
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
haftmann
parents:
46125
diff
changeset
|
2982 |
lemma fold_cong [fundef_cong]: |
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
haftmann
parents:
46125
diff
changeset
|
2983 |
"a = b \<Longrightarrow> xs = ys \<Longrightarrow> (\<And>x. x \<in> set xs \<Longrightarrow> f x = g x) |
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
haftmann
parents:
46125
diff
changeset
|
2984 |
\<Longrightarrow> fold f xs a = fold g ys b" |
68719 | 2985 |
by (induct ys arbitrary: a b xs) simp_all |
58807 | 2986 |
|
2987 |
lemma fold_id: "(\<And>x. x \<in> set xs \<Longrightarrow> f x = id) \<Longrightarrow> fold f xs = id" |
|
68719 | 2988 |
by (induct xs) simp_all |
46133
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
haftmann
parents:
46125
diff
changeset
|
2989 |
|
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
haftmann
parents:
46125
diff
changeset
|
2990 |
lemma fold_commute: |
58807 | 2991 |
"(\<And>x. x \<in> set xs \<Longrightarrow> h \<circ> g x = f x \<circ> h) \<Longrightarrow> h \<circ> fold g xs = fold f xs \<circ> h" |
68719 | 2992 |
by (induct xs) (simp_all add: fun_eq_iff) |
46133
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
haftmann
parents:
46125
diff
changeset
|
2993 |
|
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
haftmann
parents:
46125
diff
changeset
|
2994 |
lemma fold_commute_apply: |
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
haftmann
parents:
46125
diff
changeset
|
2995 |
assumes "\<And>x. x \<in> set xs \<Longrightarrow> h \<circ> g x = f x \<circ> h" |
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
haftmann
parents:
46125
diff
changeset
|
2996 |
shows "h (fold g xs s) = fold f xs (h s)" |
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
haftmann
parents:
46125
diff
changeset
|
2997 |
proof - |
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
haftmann
parents:
46125
diff
changeset
|
2998 |
from assms have "h \<circ> fold g xs = fold f xs \<circ> h" by (rule fold_commute) |
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
haftmann
parents:
46125
diff
changeset
|
2999 |
then show ?thesis by (simp add: fun_eq_iff) |
37605
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
3000 |
qed |
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
3001 |
|
64963 | 3002 |
lemma fold_invariant: |
58807 | 3003 |
"\<lbrakk> \<And>x. x \<in> set xs \<Longrightarrow> Q x; P s; \<And>x s. Q x \<Longrightarrow> P s \<Longrightarrow> P (f x s) \<rbrakk> |
3004 |
\<Longrightarrow> P (fold f xs s)" |
|
68719 | 3005 |
by (induct xs arbitrary: s) simp_all |
58807 | 3006 |
|
3007 |
lemma fold_append [simp]: "fold f (xs @ ys) = fold f ys \<circ> fold f xs" |
|
68719 | 3008 |
by (induct xs) simp_all |
58807 | 3009 |
|
67091 | 3010 |
lemma fold_map [code_unfold]: "fold g (map f xs) = fold (g \<circ> f) xs" |
68719 | 3011 |
by (induct xs) simp_all |
46133
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
haftmann
parents:
46125
diff
changeset
|
3012 |
|
58437 | 3013 |
lemma fold_filter: |
3014 |
"fold f (filter P xs) = fold (\<lambda>x. if P x then f x else id) xs" |
|
68719 | 3015 |
by (induct xs) simp_all |
58437 | 3016 |
|
46133
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
haftmann
parents:
46125
diff
changeset
|
3017 |
lemma fold_rev: |
58807 | 3018 |
"(\<And>x y. x \<in> set xs \<Longrightarrow> y \<in> set xs \<Longrightarrow> f y \<circ> f x = f x \<circ> f y) |
3019 |
\<Longrightarrow> fold f (rev xs) = fold f xs" |
|
68719 | 3020 |
by (induct xs) (simp_all add: fold_commute_apply fun_eq_iff) |
58807 | 3021 |
|
3022 |
lemma fold_Cons_rev: "fold Cons xs = append (rev xs)" |
|
68719 | 3023 |
by (induct xs) simp_all |
58807 | 3024 |
|
3025 |
lemma rev_conv_fold [code]: "rev xs = fold Cons xs []" |
|
68719 | 3026 |
by (simp add: fold_Cons_rev) |
58807 | 3027 |
|
3028 |
lemma fold_append_concat_rev: "fold append xss = append (concat (rev xss))" |
|
68719 | 3029 |
by (induct xss) simp_all |
46133
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
haftmann
parents:
46125
diff
changeset
|
3030 |
|
69593 | 3031 |
text \<open>\<^const>\<open>Finite_Set.fold\<close> and \<^const>\<open>fold\<close>\<close> |
46133
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
haftmann
parents:
46125
diff
changeset
|
3032 |
|
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
haftmann
parents:
46125
diff
changeset
|
3033 |
lemma (in comp_fun_commute) fold_set_fold_remdups: |
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
haftmann
parents:
46125
diff
changeset
|
3034 |
"Finite_Set.fold f y (set xs) = fold f (remdups xs) y" |
68719 | 3035 |
by (rule sym, induct xs arbitrary: y) (simp_all add: fold_fun_left_comm insert_absorb) |
48619 | 3036 |
|
46133
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
haftmann
parents:
46125
diff
changeset
|
3037 |
lemma (in comp_fun_idem) fold_set_fold: |
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
haftmann
parents:
46125
diff
changeset
|
3038 |
"Finite_Set.fold f y (set xs) = fold f xs y" |
68719 | 3039 |
by (rule sym, induct xs arbitrary: y) (simp_all add: fold_fun_left_comm) |
58807 | 3040 |
|
3041 |
lemma union_set_fold [code]: "set xs \<union> A = fold Set.insert xs A" |
|
46147
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
haftmann
parents:
46143
diff
changeset
|
3042 |
proof - |
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
haftmann
parents:
46143
diff
changeset
|
3043 |
interpret comp_fun_idem Set.insert |
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
haftmann
parents:
46143
diff
changeset
|
3044 |
by (fact comp_fun_idem_insert) |
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
haftmann
parents:
46143
diff
changeset
|
3045 |
show ?thesis by (simp add: union_fold_insert fold_set_fold) |
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
haftmann
parents:
46143
diff
changeset
|
3046 |
qed |
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
haftmann
parents:
46143
diff
changeset
|
3047 |
|
47397
d654c73e4b12
no preference wrt. fold(l/r); prefer fold rather than foldr for iterating over lists in generated code
haftmann
parents:
47131
diff
changeset
|
3048 |
lemma union_coset_filter [code]: |
d654c73e4b12
no preference wrt. fold(l/r); prefer fold rather than foldr for iterating over lists in generated code
haftmann
parents:
47131
diff
changeset
|
3049 |
"List.coset xs \<union> A = List.coset (List.filter (\<lambda>x. x \<notin> A) xs)" |
68719 | 3050 |
by auto |
58807 | 3051 |
|
3052 |
lemma minus_set_fold [code]: "A - set xs = fold Set.remove xs A" |
|
46147
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
haftmann
parents:
46143
diff
changeset
|
3053 |
proof - |
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
haftmann
parents:
46143
diff
changeset
|
3054 |
interpret comp_fun_idem Set.remove |
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
haftmann
parents:
46143
diff
changeset
|
3055 |
by (fact comp_fun_idem_remove) |
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
haftmann
parents:
46143
diff
changeset
|
3056 |
show ?thesis |
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
haftmann
parents:
46143
diff
changeset
|
3057 |
by (simp add: minus_fold_remove [of _ A] fold_set_fold) |
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
haftmann
parents:
46143
diff
changeset
|
3058 |
qed |
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
haftmann
parents:
46143
diff
changeset
|
3059 |
|
47397
d654c73e4b12
no preference wrt. fold(l/r); prefer fold rather than foldr for iterating over lists in generated code
haftmann
parents:
47131
diff
changeset
|
3060 |
lemma minus_coset_filter [code]: |
d654c73e4b12
no preference wrt. fold(l/r); prefer fold rather than foldr for iterating over lists in generated code
haftmann
parents:
47131
diff
changeset
|
3061 |
"A - List.coset xs = set (List.filter (\<lambda>x. x \<in> A) xs)" |
68719 | 3062 |
by auto |
47397
d654c73e4b12
no preference wrt. fold(l/r); prefer fold rather than foldr for iterating over lists in generated code
haftmann
parents:
47131
diff
changeset
|
3063 |
|
d654c73e4b12
no preference wrt. fold(l/r); prefer fold rather than foldr for iterating over lists in generated code
haftmann
parents:
47131
diff
changeset
|
3064 |
lemma inter_set_filter [code]: |
d654c73e4b12
no preference wrt. fold(l/r); prefer fold rather than foldr for iterating over lists in generated code
haftmann
parents:
47131
diff
changeset
|
3065 |
"A \<inter> set xs = set (List.filter (\<lambda>x. x \<in> A) xs)" |
68719 | 3066 |
by auto |
47397
d654c73e4b12
no preference wrt. fold(l/r); prefer fold rather than foldr for iterating over lists in generated code
haftmann
parents:
47131
diff
changeset
|
3067 |
|
d654c73e4b12
no preference wrt. fold(l/r); prefer fold rather than foldr for iterating over lists in generated code
haftmann
parents:
47131
diff
changeset
|
3068 |
lemma inter_coset_fold [code]: |
d654c73e4b12
no preference wrt. fold(l/r); prefer fold rather than foldr for iterating over lists in generated code
haftmann
parents:
47131
diff
changeset
|
3069 |
"A \<inter> List.coset xs = fold Set.remove xs A" |
68719 | 3070 |
by (simp add: Diff_eq [symmetric] minus_set_fold) |
47397
d654c73e4b12
no preference wrt. fold(l/r); prefer fold rather than foldr for iterating over lists in generated code
haftmann
parents:
47131
diff
changeset
|
3071 |
|
54885 | 3072 |
lemma (in semilattice_set) set_eq_fold [code]: |
51489 | 3073 |
"F (set (x # xs)) = fold f xs x" |
46133
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
haftmann
parents:
46125
diff
changeset
|
3074 |
proof - |
51489 | 3075 |
interpret comp_fun_idem f |
61169 | 3076 |
by standard (simp_all add: fun_eq_iff left_commute) |
51489 | 3077 |
show ?thesis by (simp add: eq_fold fold_set_fold) |
46133
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
haftmann
parents:
46125
diff
changeset
|
3078 |
qed |
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
haftmann
parents:
46125
diff
changeset
|
3079 |
|
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
haftmann
parents:
46125
diff
changeset
|
3080 |
lemma (in complete_lattice) Inf_set_fold: |
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
haftmann
parents:
46125
diff
changeset
|
3081 |
"Inf (set xs) = fold inf xs top" |
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
haftmann
parents:
46125
diff
changeset
|
3082 |
proof - |
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
haftmann
parents:
46125
diff
changeset
|
3083 |
interpret comp_fun_idem "inf :: 'a \<Rightarrow> 'a \<Rightarrow> 'a" |
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
haftmann
parents:
46125
diff
changeset
|
3084 |
by (fact comp_fun_idem_inf) |
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
haftmann
parents:
46125
diff
changeset
|
3085 |
show ?thesis by (simp add: Inf_fold_inf fold_set_fold inf_commute) |
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
haftmann
parents:
46125
diff
changeset
|
3086 |
qed |
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
haftmann
parents:
46125
diff
changeset
|
3087 |
|
47397
d654c73e4b12
no preference wrt. fold(l/r); prefer fold rather than foldr for iterating over lists in generated code
haftmann
parents:
47131
diff
changeset
|
3088 |
declare Inf_set_fold [where 'a = "'a set", code] |
d654c73e4b12
no preference wrt. fold(l/r); prefer fold rather than foldr for iterating over lists in generated code
haftmann
parents:
47131
diff
changeset
|
3089 |
|
46133
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
haftmann
parents:
46125
diff
changeset
|
3090 |
lemma (in complete_lattice) Sup_set_fold: |
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
haftmann
parents:
46125
diff
changeset
|
3091 |
"Sup (set xs) = fold sup xs bot" |
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
haftmann
parents:
46125
diff
changeset
|
3092 |
proof - |
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
haftmann
parents:
46125
diff
changeset
|
3093 |
interpret comp_fun_idem "sup :: 'a \<Rightarrow> 'a \<Rightarrow> 'a" |
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
haftmann
parents:
46125
diff
changeset
|
3094 |
by (fact comp_fun_idem_sup) |
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
haftmann
parents:
46125
diff
changeset
|
3095 |
show ?thesis by (simp add: Sup_fold_sup fold_set_fold sup_commute) |
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
haftmann
parents:
46125
diff
changeset
|
3096 |
qed |
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
haftmann
parents:
46125
diff
changeset
|
3097 |
|
47397
d654c73e4b12
no preference wrt. fold(l/r); prefer fold rather than foldr for iterating over lists in generated code
haftmann
parents:
47131
diff
changeset
|
3098 |
declare Sup_set_fold [where 'a = "'a set", code] |
d654c73e4b12
no preference wrt. fold(l/r); prefer fold rather than foldr for iterating over lists in generated code
haftmann
parents:
47131
diff
changeset
|
3099 |
|
46133
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
haftmann
parents:
46125
diff
changeset
|
3100 |
lemma (in complete_lattice) INF_set_fold: |
69275 | 3101 |
"\<Sqinter>(f ` set xs) = fold (inf \<circ> f) xs top" |
68781 | 3102 |
using Inf_set_fold [of "map f xs"] by (simp add: fold_map) |
46133
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
haftmann
parents:
46125
diff
changeset
|
3103 |
|
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
haftmann
parents:
46125
diff
changeset
|
3104 |
lemma (in complete_lattice) SUP_set_fold: |
69275 | 3105 |
"\<Squnion>(f ` set xs) = fold (sup \<circ> f) xs bot" |
68781 | 3106 |
using Sup_set_fold [of "map f xs"] by (simp add: fold_map) |
46133
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
haftmann
parents:
46125
diff
changeset
|
3107 |
|
49948
744934b818c7
moved quite generic material from theory Enum to more appropriate places
haftmann
parents:
49808
diff
changeset
|
3108 |
|
69593 | 3109 |
subsubsection \<open>Fold variants: \<^const>\<open>foldr\<close> and \<^const>\<open>foldl\<close>\<close> |
60758 | 3110 |
|
3111 |
text \<open>Correspondence\<close> |
|
46133
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
haftmann
parents:
46125
diff
changeset
|
3112 |
|
58807 | 3113 |
lemma foldr_conv_fold [code_abbrev]: "foldr f xs = fold f (rev xs)" |
68719 | 3114 |
by (induct xs) simp_all |
58807 | 3115 |
|
3116 |
lemma foldl_conv_fold: "foldl f s xs = fold (\<lambda>x s. f s x) xs s" |
|
68719 | 3117 |
by (induct xs arbitrary: s) simp_all |
47397
d654c73e4b12
no preference wrt. fold(l/r); prefer fold rather than foldr for iterating over lists in generated code
haftmann
parents:
47131
diff
changeset
|
3118 |
|
61799 | 3119 |
lemma foldr_conv_foldl: \<comment> \<open>The ``Third Duality Theorem'' in Bird \& Wadler:\<close> |
46133
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
haftmann
parents:
46125
diff
changeset
|
3120 |
"foldr f xs a = foldl (\<lambda>x y. f y x) a (rev xs)" |
68719 | 3121 |
by (simp add: foldr_conv_fold foldl_conv_fold) |
47397
d654c73e4b12
no preference wrt. fold(l/r); prefer fold rather than foldr for iterating over lists in generated code
haftmann
parents:
47131
diff
changeset
|
3122 |
|
d654c73e4b12
no preference wrt. fold(l/r); prefer fold rather than foldr for iterating over lists in generated code
haftmann
parents:
47131
diff
changeset
|
3123 |
lemma foldl_conv_foldr: |
46133
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
haftmann
parents:
46125
diff
changeset
|
3124 |
"foldl f a xs = foldr (\<lambda>x y. f y x) (rev xs) a" |
68719 | 3125 |
by (simp add: foldr_conv_fold foldl_conv_fold) |
46133
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
haftmann
parents:
46125
diff
changeset
|
3126 |
|
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
haftmann
parents:
46125
diff
changeset
|
3127 |
lemma foldr_fold: |
58807 | 3128 |
"(\<And>x y. x \<in> set xs \<Longrightarrow> y \<in> set xs \<Longrightarrow> f y \<circ> f x = f x \<circ> f y) |
3129 |
\<Longrightarrow> foldr f xs = fold f xs" |
|
68719 | 3130 |
unfolding foldr_conv_fold by (rule fold_rev) |
46133
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
haftmann
parents:
46125
diff
changeset
|
3131 |
|
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
haftmann
parents:
46125
diff
changeset
|
3132 |
lemma foldr_cong [fundef_cong]: |
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
haftmann
parents:
46125
diff
changeset
|
3133 |
"a = b \<Longrightarrow> l = k \<Longrightarrow> (\<And>a x. x \<in> set l \<Longrightarrow> f x a = g x a) \<Longrightarrow> foldr f l a = foldr g k b" |
68719 | 3134 |
by (auto simp add: foldr_conv_fold intro!: fold_cong) |
46133
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
haftmann
parents:
46125
diff
changeset
|
3135 |
|
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
haftmann
parents:
46125
diff
changeset
|
3136 |
lemma foldl_cong [fundef_cong]: |
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
haftmann
parents:
46125
diff
changeset
|
3137 |
"a = b \<Longrightarrow> l = k \<Longrightarrow> (\<And>a x. x \<in> set l \<Longrightarrow> f a x = g a x) \<Longrightarrow> foldl f a l = foldl g b k" |
68719 | 3138 |
by (auto simp add: foldl_conv_fold intro!: fold_cong) |
58807 | 3139 |
|
3140 |
lemma foldr_append [simp]: "foldr f (xs @ ys) a = foldr f xs (foldr f ys a)" |
|
68719 | 3141 |
by (simp add: foldr_conv_fold) |
58807 | 3142 |
|
3143 |
lemma foldl_append [simp]: "foldl f a (xs @ ys) = foldl f (foldl f a xs) ys" |
|
68719 | 3144 |
by (simp add: foldl_conv_fold) |
58807 | 3145 |
|
67091 | 3146 |
lemma foldr_map [code_unfold]: "foldr g (map f xs) a = foldr (g \<circ> f) xs a" |
68719 | 3147 |
by (simp add: foldr_conv_fold fold_map rev_map) |
46133
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
haftmann
parents:
46125
diff
changeset
|
3148 |
|
58437 | 3149 |
lemma foldr_filter: |
3150 |
"foldr f (filter P xs) = foldr (\<lambda>x. if P x then f x else id) xs" |
|
68719 | 3151 |
by (simp add: foldr_conv_fold rev_filter fold_filter) |
64963 | 3152 |
|
46133
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
haftmann
parents:
46125
diff
changeset
|
3153 |
lemma foldl_map [code_unfold]: |
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
haftmann
parents:
46125
diff
changeset
|
3154 |
"foldl g a (map f xs) = foldl (\<lambda>a x. g a (f x)) a xs" |
68719 | 3155 |
by (simp add: foldl_conv_fold fold_map comp_def) |
46133
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
haftmann
parents:
46125
diff
changeset
|
3156 |
|
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
haftmann
parents:
46125
diff
changeset
|
3157 |
lemma concat_conv_foldr [code]: |
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
haftmann
parents:
46125
diff
changeset
|
3158 |
"concat xss = foldr append xss []" |
68719 | 3159 |
by (simp add: fold_append_concat_rev foldr_conv_fold) |
46133
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
haftmann
parents:
46125
diff
changeset
|
3160 |
|
35115 | 3161 |
|
69593 | 3162 |
subsubsection \<open>\<^const>\<open>upt\<close>\<close> |
13114 | 3163 |
|
17090 | 3164 |
lemma upt_rec[code]: "[i..<j] = (if i<j then i#[Suc i..<j] else [])" |
68719 | 3165 |
\<comment> \<open>simp does not terminate!\<close> |
3166 |
by (induct j) auto |
|
13142 | 3167 |
|
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46898
diff
changeset
|
3168 |
lemmas upt_rec_numeral[simp] = upt_rec[of "numeral m" "numeral n"] for m n |
32005 | 3169 |
|
68709 | 3170 |
lemma upt_conv_Nil [simp]: "j \<le> i ==> [i..<j] = []" |
68719 | 3171 |
by (subst upt_rec) simp |
13114 | 3172 |
|
68709 | 3173 |
lemma upt_eq_Nil_conv[simp]: "([i..<j] = []) = (j = 0 \<or> j \<le> i)" |
68719 | 3174 |
by(induct j)simp_all |
15281 | 3175 |
|
3176 |
lemma upt_eq_Cons_conv: |
|
68719 | 3177 |
"([i..<j] = x#xs) = (i < j \<and> i = x \<and> [i+1..<j] = xs)" |
68709 | 3178 |
proof (induct j arbitrary: x xs) |
3179 |
case (Suc j) |
|
3180 |
then show ?case |
|
3181 |
by (simp add: upt_rec) |
|
3182 |
qed simp |
|
3183 |
||
3184 |
lemma upt_Suc_append: "i \<le> j ==> [i..<(Suc j)] = [i..<j]@[j]" |
|
68719 | 3185 |
\<comment> \<open>Only needed if \<open>upt_Suc\<close> is deleted from the simpset.\<close> |
3186 |
by simp |
|
13114 | 3187 |
|
15425 | 3188 |
lemma upt_conv_Cons: "i < j ==> [i..<j] = i # [Suc i..<j]" |
68719 | 3189 |
by (simp add: upt_rec) |
13114 | 3190 |
|
63145 | 3191 |
lemma upt_conv_Cons_Cons: \<comment> \<open>no precondition\<close> |
62580 | 3192 |
"m # n # ns = [m..<q] \<longleftrightarrow> n # ns = [Suc m..<q]" |
3193 |
proof (cases "m < q") |
|
3194 |
case False then show ?thesis by simp |
|
3195 |
next |
|
3196 |
case True then show ?thesis by (simp add: upt_conv_Cons) |
|
3197 |
qed |
|
3198 |
||
15425 | 3199 |
lemma upt_add_eq_append: "i<=j ==> [i..<j+k] = [i..<j]@[j..<j+k]" |
68719 | 3200 |
\<comment> \<open>LOOPS as a simprule, since \<open>j \<le> j\<close>.\<close> |
3201 |
by (induct k) auto |
|
13114 | 3202 |
|
15425 | 3203 |
lemma length_upt [simp]: "length [i..<j] = j - i" |
68719 | 3204 |
by (induct j) (auto simp add: Suc_diff_le) |
13114 | 3205 |
|
15425 | 3206 |
lemma nth_upt [simp]: "i + k < j ==> [i..<j] ! k = i + k" |
68719 | 3207 |
by (induct j) (auto simp add: less_Suc_eq nth_append split: nat_diff_split) |
17906 | 3208 |
|
3209 |
lemma hd_upt[simp]: "i < j \<Longrightarrow> hd[i..<j] = i" |
|
68719 | 3210 |
by(simp add:upt_conv_Cons) |
17906 | 3211 |
|
70183
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
69850
diff
changeset
|
3212 |
lemma tl_upt [simp]: "tl [m..<n] = [Suc m..<n]" |
68719 | 3213 |
by (simp add: upt_rec) |
63317
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
eberlm
parents:
63173
diff
changeset
|
3214 |
|
17906 | 3215 |
lemma last_upt[simp]: "i < j \<Longrightarrow> last[i..<j] = j - 1" |
68719 | 3216 |
by(cases j)(auto simp: upt_Suc_append) |
17906 | 3217 |
|
68709 | 3218 |
lemma take_upt [simp]: "i+m \<le> n ==> take m [i..<n] = [i..<i+m]" |
3219 |
proof (induct m arbitrary: i) |
|
3220 |
case (Suc m) |
|
3221 |
then show ?case |
|
3222 |
by (subst take_Suc_conv_app_nth) auto |
|
3223 |
qed simp |
|
3507 | 3224 |
|
17501 | 3225 |
lemma drop_upt[simp]: "drop m [i..<j] = [i+m..<j]" |
68719 | 3226 |
by(induct j) auto |
17501 | 3227 |
|
24645 | 3228 |
lemma map_Suc_upt: "map Suc [m..<n] = [Suc m..<Suc n]" |
68719 | 3229 |
by (induct n) auto |
13114 | 3230 |
|
54496
178922b63b58
add lemmas Suc_funpow and id_funpow to simpset; add lemma map_add_upt
hoelzl
parents:
54404
diff
changeset
|
3231 |
lemma map_add_upt: "map (\<lambda>i. i + n) [0..<m] = [n..<m + n]" |
68719 | 3232 |
by (induct m) simp_all |
54496
178922b63b58
add lemmas Suc_funpow and id_funpow to simpset; add lemma map_add_upt
hoelzl
parents:
54404
diff
changeset
|
3233 |
|
24526 | 3234 |
lemma nth_map_upt: "i < n-m ==> (map f [m..<n]) ! i = f(m+i)" |
68709 | 3235 |
proof (induct n m arbitrary: i rule: diff_induct) |
68719 | 3236 |
case (3 x y) |
68709 | 3237 |
then show ?case |
3238 |
by (metis add.commute length_upt less_diff_conv nth_map nth_upt) |
|
3239 |
qed auto |
|
13114 | 3240 |
|
58807 | 3241 |
lemma map_decr_upt: "map (\<lambda>n. n - Suc 0) [Suc m..<Suc n] = [m..<n]" |
68719 | 3242 |
by (induct n) simp_all |
66550
e5d82cf3c387
Some small lemmas about polynomials and FPSs
eberlm <eberlm@in.tum.de>
parents:
66502
diff
changeset
|
3243 |
|
e5d82cf3c387
Some small lemmas about polynomials and FPSs
eberlm <eberlm@in.tum.de>
parents:
66502
diff
changeset
|
3244 |
lemma map_upt_Suc: "map f [0 ..< Suc n] = f 0 # map (\<lambda>i. f (Suc i)) [0 ..< n]" |
68719 | 3245 |
by (induct n arbitrary: f) auto |
52380 | 3246 |
|
13883
0451e0fb3f22
Re-structured some proofs in order to get rid of rule_format attribute.
berghofe
parents:
13863
diff
changeset
|
3247 |
lemma nth_take_lemma: |
67091 | 3248 |
"k \<le> length xs \<Longrightarrow> k \<le> length ys \<Longrightarrow> |
3249 |
(\<And>i. i < k \<longrightarrow> xs!i = ys!i) \<Longrightarrow> take k xs = take k ys" |
|
68709 | 3250 |
proof (induct k arbitrary: xs ys) |
3251 |
case (Suc k) |
|
3252 |
then show ?case |
|
3253 |
apply (simp add: less_Suc_eq_0_disj) |
|
3254 |
by (simp add: Suc.prems(3) take_Suc_conv_app_nth) |
|
3255 |
qed simp |
|
13114 | 3256 |
|
3257 |
lemma nth_equalityI: |
|
68975
5ce4d117cea7
A few new results, elimination of duplicates and more use of "pairwise"
paulson <lp15@cam.ac.uk>
parents:
68781
diff
changeset
|
3258 |
"\<lbrakk>length xs = length ys; \<And>i. i < length xs \<Longrightarrow> xs!i = ys!i\<rbrakk> \<Longrightarrow> xs = ys" |
68719 | 3259 |
by (frule nth_take_lemma [OF le_refl eq_imp_le]) simp_all |
13142 | 3260 |
|
24796 | 3261 |
lemma map_nth: |
3262 |
"map (\<lambda>i. xs ! i) [0..<length xs] = xs" |
|
68719 | 3263 |
by (rule nth_equalityI, auto) |
58807 | 3264 |
|
13863 | 3265 |
lemma list_all2_antisym: |
64963 | 3266 |
"\<lbrakk> (\<And>x y. \<lbrakk>P x y; Q y x\<rbrakk> \<Longrightarrow> x = y); list_all2 P xs ys; list_all2 Q ys xs \<rbrakk> |
13863 | 3267 |
\<Longrightarrow> xs = ys" |
68719 | 3268 |
by (simp add: list_all2_conv_all_nth nth_equalityI) |
13863 | 3269 |
|
13142 | 3270 |
lemma take_equalityI: "(\<forall>i. take i xs = take i ys) ==> xs = ys" |
61799 | 3271 |
\<comment> \<open>The famous take-lemma.\<close> |
68719 | 3272 |
by (metis length_take min.commute order_refl take_all) |
13142 | 3273 |
|
15302 | 3274 |
lemma take_Cons': |
58807 | 3275 |
"take n (x # xs) = (if n = 0 then [] else x # take (n - 1) xs)" |
15302 | 3276 |
by (cases n) simp_all |
3277 |
||
3278 |
lemma drop_Cons': |
|
58807 | 3279 |
"drop n (x # xs) = (if n = 0 then x # xs else drop (n - 1) xs)" |
15302 | 3280 |
by (cases n) simp_all |
3281 |
||
3282 |
lemma nth_Cons': "(x # xs)!n = (if n = 0 then x else xs!(n - 1))" |
|
3283 |
by (cases n) simp_all |
|
3284 |
||
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46898
diff
changeset
|
3285 |
lemma take_Cons_numeral [simp]: |
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46898
diff
changeset
|
3286 |
"take (numeral v) (x # xs) = x # take (numeral v - 1) xs" |
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46898
diff
changeset
|
3287 |
by (simp add: take_Cons') |
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46898
diff
changeset
|
3288 |
|
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46898
diff
changeset
|
3289 |
lemma drop_Cons_numeral [simp]: |
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46898
diff
changeset
|
3290 |
"drop (numeral v) (x # xs) = drop (numeral v - 1) xs" |
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46898
diff
changeset
|
3291 |
by (simp add: drop_Cons') |
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46898
diff
changeset
|
3292 |
|
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46898
diff
changeset
|
3293 |
lemma nth_Cons_numeral [simp]: |
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46898
diff
changeset
|
3294 |
"(x # xs) ! numeral v = xs ! (numeral v - 1)" |
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46898
diff
changeset
|
3295 |
by (simp add: nth_Cons') |
15302 | 3296 |
|
3297 |
||
69593 | 3298 |
subsubsection \<open>\<open>upto\<close>: interval-list on \<^typ>\<open>int\<close>\<close> |
32415
1dddf2f64266
got rid of complicated class finite_intvl_succ and defined "upto" directly on int, the only instance of the class.
nipkow
parents:
32078
diff
changeset
|
3299 |
|
1dddf2f64266
got rid of complicated class finite_intvl_succ and defined "upto" directly on int, the only instance of the class.
nipkow
parents:
32078
diff
changeset
|
3300 |
function upto :: "int \<Rightarrow> int \<Rightarrow> int list" ("(1[_../_])") where |
51166 | 3301 |
"upto i j = (if i \<le> j then i # [i+1..j] else [])" |
32415
1dddf2f64266
got rid of complicated class finite_intvl_succ and defined "upto" directly on int, the only instance of the class.
nipkow
parents:
32078
diff
changeset
|
3302 |
by auto |
1dddf2f64266
got rid of complicated class finite_intvl_succ and defined "upto" directly on int, the only instance of the class.
nipkow
parents:
32078
diff
changeset
|
3303 |
termination |
1dddf2f64266
got rid of complicated class finite_intvl_succ and defined "upto" directly on int, the only instance of the class.
nipkow
parents:
32078
diff
changeset
|
3304 |
by(relation "measure(%(i::int,j). nat(j - i + 1))") auto |
1dddf2f64266
got rid of complicated class finite_intvl_succ and defined "upto" directly on int, the only instance of the class.
nipkow
parents:
32078
diff
changeset
|
3305 |
|
51166 | 3306 |
declare upto.simps[simp del] |
32415
1dddf2f64266
got rid of complicated class finite_intvl_succ and defined "upto" directly on int, the only instance of the class.
nipkow
parents:
32078
diff
changeset
|
3307 |
|
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46898
diff
changeset
|
3308 |
lemmas upto_rec_numeral [simp] = |
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46898
diff
changeset
|
3309 |
upto.simps[of "numeral m" "numeral n"] |
54489
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents:
54404
diff
changeset
|
3310 |
upto.simps[of "numeral m" "- numeral n"] |
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents:
54404
diff
changeset
|
3311 |
upto.simps[of "- numeral m" "numeral n"] |
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents:
54404
diff
changeset
|
3312 |
upto.simps[of "- numeral m" "- numeral n"] for m n |
32415
1dddf2f64266
got rid of complicated class finite_intvl_succ and defined "upto" directly on int, the only instance of the class.
nipkow
parents:
32078
diff
changeset
|
3313 |
|
1dddf2f64266
got rid of complicated class finite_intvl_succ and defined "upto" directly on int, the only instance of the class.
nipkow
parents:
32078
diff
changeset
|
3314 |
lemma upto_empty[simp]: "j < i \<Longrightarrow> [i..j] = []" |
1dddf2f64266
got rid of complicated class finite_intvl_succ and defined "upto" directly on int, the only instance of the class.
nipkow
parents:
32078
diff
changeset
|
3315 |
by(simp add: upto.simps) |
1dddf2f64266
got rid of complicated class finite_intvl_succ and defined "upto" directly on int, the only instance of the class.
nipkow
parents:
32078
diff
changeset
|
3316 |
|
67942 | 3317 |
lemma upto_single[simp]: "[i..i] = [i]" |
3318 |
by(simp add: upto.simps) |
|
3319 |
||
67124 | 3320 |
lemma upto_Nil[simp]: "[i..j] = [] \<longleftrightarrow> j < i" |
3321 |
by (simp add: upto.simps) |
|
3322 |
||
67168 | 3323 |
lemma upto_Nil2[simp]: "[] = [i..j] \<longleftrightarrow> j < i" |
3324 |
by (simp add: upto.simps) |
|
3325 |
||
51166 | 3326 |
lemma upto_rec1: "i \<le> j \<Longrightarrow> [i..j] = i#[i+1..j]" |
3327 |
by(simp add: upto.simps) |
|
3328 |
||
3329 |
lemma upto_rec2: "i \<le> j \<Longrightarrow> [i..j] = [i..j - 1]@[j]" |
|
3330 |
proof(induct "nat(j-i)" arbitrary: i j) |
|
3331 |
case 0 thus ?case by(simp add: upto.simps) |
|
3332 |
next |
|
3333 |
case (Suc n) |
|
3334 |
hence "n = nat (j - (i + 1))" "i < j" by linarith+ |
|
3335 |
from this(2) Suc.hyps(1)[OF this(1)] Suc(2,3) upto_rec1 show ?case by simp |
|
3336 |
qed |
|
3337 |
||
67949 | 3338 |
lemma length_upto[simp]: "length [i..j] = nat(j - i + 1)" |
3339 |
by(induction i j rule: upto.induct) (auto simp: upto.simps) |
|
3340 |
||
32415
1dddf2f64266
got rid of complicated class finite_intvl_succ and defined "upto" directly on int, the only instance of the class.
nipkow
parents:
32078
diff
changeset
|
3341 |
lemma set_upto[simp]: "set[i..j] = {i..j}" |
41463
edbf0a86fb1c
adding simproc to rewrite list comprehensions to set comprehensions; adopting proofs
bulwahn
parents:
41372
diff
changeset
|
3342 |
proof(induct i j rule:upto.induct) |
edbf0a86fb1c
adding simproc to rewrite list comprehensions to set comprehensions; adopting proofs
bulwahn
parents:
41372
diff
changeset
|
3343 |
case (1 i j) |
edbf0a86fb1c
adding simproc to rewrite list comprehensions to set comprehensions; adopting proofs
bulwahn
parents:
41372
diff
changeset
|
3344 |
from this show ?case |
55811 | 3345 |
unfolding upto.simps[of i j] by auto |
41463
edbf0a86fb1c
adding simproc to rewrite list comprehensions to set comprehensions; adopting proofs
bulwahn
parents:
41372
diff
changeset
|
3346 |
qed |
32415
1dddf2f64266
got rid of complicated class finite_intvl_succ and defined "upto" directly on int, the only instance of the class.
nipkow
parents:
32078
diff
changeset
|
3347 |
|
69125 | 3348 |
lemma nth_upto[simp]: "i + int k \<le> j \<Longrightarrow> [i..j] ! k = i + int k" |
68719 | 3349 |
apply(induction i j arbitrary: k rule: upto.induct) |
67949 | 3350 |
apply(subst upto_rec1) |
3351 |
apply(auto simp add: nth_Cons') |
|
3352 |
done |
|
3353 |
||
67081 | 3354 |
lemma upto_split1: |
3355 |
"i \<le> j \<Longrightarrow> j \<le> k \<Longrightarrow> [i..k] = [i..j-1] @ [j..k]" |
|
3356 |
proof (induction j rule: int_ge_induct) |
|
3357 |
case base thus ?case by (simp add: upto_rec1) |
|
3358 |
next |
|
3359 |
case step thus ?case using upto_rec1 upto_rec2 by simp |
|
3360 |
qed |
|
3361 |
||
3362 |
lemma upto_split2: |
|
3363 |
"i \<le> j \<Longrightarrow> j \<le> k \<Longrightarrow> [i..k] = [i..j] @ [j+1..k]" |
|
3364 |
using upto_rec1 upto_rec2 upto_split1 by auto |
|
3365 |
||
67124 | 3366 |
lemma upto_split3: "\<lbrakk> i \<le> j; j \<le> k \<rbrakk> \<Longrightarrow> [i..k] = [i..j-1] @ j # [j+1..k]" |
3367 |
using upto_rec1 upto_split1 by auto |
|
3368 |
||
60758 | 3369 |
text\<open>Tail recursive version for code generation:\<close> |
51166 | 3370 |
|
51170 | 3371 |
definition upto_aux :: "int \<Rightarrow> int \<Rightarrow> int list \<Rightarrow> int list" where |
3372 |
"upto_aux i j js = [i..j] @ js" |
|
3373 |
||
3374 |
lemma upto_aux_rec [code]: |
|
51166 | 3375 |
"upto_aux i j js = (if j<i then js else upto_aux i (j - 1) (j#js))" |
58807 | 3376 |
by (simp add: upto_aux_def upto_rec2) |
51166 | 3377 |
|
3378 |
lemma upto_code[code]: "[i..j] = upto_aux i j []" |
|
51170 | 3379 |
by(simp add: upto_aux_def) |
51166 | 3380 |
|
32415
1dddf2f64266
got rid of complicated class finite_intvl_succ and defined "upto" directly on int, the only instance of the class.
nipkow
parents:
32078
diff
changeset
|
3381 |
|
69593 | 3382 |
subsubsection \<open>\<^const>\<open>distinct\<close> and \<^const>\<open>remdups\<close> and \<^const>\<open>remdups_adj\<close>\<close> |
13142 | 3383 |
|
58807 | 3384 |
lemma distinct_tl: "distinct xs \<Longrightarrow> distinct (tl xs)" |
3385 |
by (cases xs) simp_all |
|
40210
aee7ef725330
sorting: avoid _key suffix if lemma applies both to simple and generalized variant; generalized insort_insert to insort_insert_key; additional lemmas
haftmann
parents:
40195
diff
changeset
|
3386 |
|
13142 | 3387 |
lemma distinct_append [simp]: |
58807 | 3388 |
"distinct (xs @ ys) = (distinct xs \<and> distinct ys \<and> set xs \<inter> set ys = {})" |
13145 | 3389 |
by (induct xs) auto |
13142 | 3390 |
|
15305 | 3391 |
lemma distinct_rev[simp]: "distinct(rev xs) = distinct xs" |
3392 |
by(induct xs) auto |
|
3393 |
||
13142 | 3394 |
lemma set_remdups [simp]: "set (remdups xs) = set xs" |
13145 | 3395 |
by (induct xs) (auto simp add: insert_absorb) |
13142 | 3396 |
|
3397 |
lemma distinct_remdups [iff]: "distinct (remdups xs)" |
|
13145 | 3398 |
by (induct xs) auto |
13142 | 3399 |
|
25287 | 3400 |
lemma distinct_remdups_id: "distinct xs ==> remdups xs = xs" |
3401 |
by (induct xs, auto) |
|
3402 |
||
26734 | 3403 |
lemma remdups_id_iff_distinct [simp]: "remdups xs = xs \<longleftrightarrow> distinct xs" |
3404 |
by (metis distinct_remdups distinct_remdups_id) |
|
25287 | 3405 |
|
67091 | 3406 |
lemma finite_distinct_list: "finite A \<Longrightarrow> \<exists>xs. set xs = A \<and> distinct xs" |
24632 | 3407 |
by (metis distinct_remdups finite_list set_remdups) |
24566 | 3408 |
|
15072 | 3409 |
lemma remdups_eq_nil_iff [simp]: "(remdups x = []) = (x = [])" |
46440
d4994e2e7364
use 'primrec' to define "rotate1", for uniformity (and to help first-order tools that rely on "Spec_Rules")
blanchet
parents:
46439
diff
changeset
|
3410 |
by (induct x, auto) |
15072 | 3411 |
|
3412 |
lemma remdups_eq_nil_right_iff [simp]: "([] = remdups x) = (x = [])" |
|
24349 | 3413 |
by (induct x, auto) |
15072 | 3414 |
|
68709 | 3415 |
lemma length_remdups_leq[iff]: "length(remdups xs) \<le> length xs" |
15245 | 3416 |
by (induct xs) auto |
3417 |
||
3418 |
lemma length_remdups_eq[iff]: |
|
3419 |
"(length (remdups xs) = length xs) = (remdups xs = xs)" |
|
68719 | 3420 |
proof (induct xs) |
3421 |
case (Cons a xs) |
|
3422 |
then show ?case |
|
3423 |
by simp (metis Suc_n_not_le_n impossible_Cons length_remdups_leq) |
|
3424 |
qed auto |
|
15245 | 3425 |
|
33945 | 3426 |
lemma remdups_filter: "remdups(filter P xs) = filter P (remdups xs)" |
58807 | 3427 |
by (induct xs) auto |
18490 | 3428 |
|
3429 |
lemma distinct_map: |
|
67091 | 3430 |
"distinct(map f xs) = (distinct xs \<and> inj_on f (set xs))" |
18490 | 3431 |
by (induct xs) auto |
3432 |
||
58195 | 3433 |
lemma distinct_map_filter: |
3434 |
"distinct (map f xs) \<Longrightarrow> distinct (map f (filter P xs))" |
|
58807 | 3435 |
by (induct xs) auto |
58195 | 3436 |
|
13142 | 3437 |
lemma distinct_filter [simp]: "distinct xs ==> distinct (filter P xs)" |
13145 | 3438 |
by (induct xs) auto |
13114 | 3439 |
|
17501 | 3440 |
lemma distinct_upt[simp]: "distinct[i..<j]" |
3441 |
by (induct j) auto |
|
3442 |
||
32415
1dddf2f64266
got rid of complicated class finite_intvl_succ and defined "upto" directly on int, the only instance of the class.
nipkow
parents:
32078
diff
changeset
|
3443 |
lemma distinct_upto[simp]: "distinct[i..j]" |
1dddf2f64266
got rid of complicated class finite_intvl_succ and defined "upto" directly on int, the only instance of the class.
nipkow
parents:
32078
diff
changeset
|
3444 |
apply(induct i j rule:upto.induct) |
1dddf2f64266
got rid of complicated class finite_intvl_succ and defined "upto" directly on int, the only instance of the class.
nipkow
parents:
32078
diff
changeset
|
3445 |
apply(subst upto.simps) |
1dddf2f64266
got rid of complicated class finite_intvl_succ and defined "upto" directly on int, the only instance of the class.
nipkow
parents:
32078
diff
changeset
|
3446 |
apply(simp) |
1dddf2f64266
got rid of complicated class finite_intvl_succ and defined "upto" directly on int, the only instance of the class.
nipkow
parents:
32078
diff
changeset
|
3447 |
done |
1dddf2f64266
got rid of complicated class finite_intvl_succ and defined "upto" directly on int, the only instance of the class.
nipkow
parents:
32078
diff
changeset
|
3448 |
|
24526 | 3449 |
lemma distinct_take[simp]: "distinct xs \<Longrightarrow> distinct (take i xs)" |
68719 | 3450 |
proof (induct xs arbitrary: i) |
3451 |
case (Cons a xs) |
|
3452 |
then show ?case |
|
3453 |
by (metis Cons.prems append_take_drop_id distinct_append) |
|
3454 |
qed auto |
|
17501 | 3455 |
|
24526 | 3456 |
lemma distinct_drop[simp]: "distinct xs \<Longrightarrow> distinct (drop i xs)" |
68719 | 3457 |
proof (induct xs arbitrary: i) |
3458 |
case (Cons a xs) |
|
3459 |
then show ?case |
|
3460 |
by (metis Cons.prems append_take_drop_id distinct_append) |
|
3461 |
qed auto |
|
17501 | 3462 |
|
3463 |
lemma distinct_list_update: |
|
68719 | 3464 |
assumes d: "distinct xs" and a: "a \<notin> set xs - {xs!i}" |
3465 |
shows "distinct (xs[i:=a])" |
|
17501 | 3466 |
proof (cases "i < length xs") |
3467 |
case True |
|
68719 | 3468 |
with a have anot: "a \<notin> set (take i xs @ xs ! i # drop (Suc i) xs) - {xs!i}" |
3469 |
by simp (metis in_set_dropD in_set_takeD) |
|
3470 |
show ?thesis |
|
3471 |
proof (cases "a = xs!i") |
|
3472 |
case True |
|
3473 |
with d show ?thesis |
|
3474 |
by auto |
|
3475 |
next |
|
3476 |
case False |
|
3477 |
then show ?thesis |
|
3478 |
using d anot \<open>i < length xs\<close> |
|
3479 |
apply (simp add: upd_conv_take_nth_drop) |
|
3480 |
by (metis disjoint_insert(1) distinct_append id_take_nth_drop set_simps(2)) |
|
3481 |
qed |
|
17501 | 3482 |
next |
3483 |
case False with d show ?thesis by auto |
|
3484 |
qed |
|
3485 |
||
31363
7493b571b37d
Added theorems about distinct & concat, map & replicate and concat & replicate
hoelzl
parents:
31264
diff
changeset
|
3486 |
lemma distinct_concat: |
58807 | 3487 |
"\<lbrakk> distinct xs; |
3488 |
\<And> ys. ys \<in> set xs \<Longrightarrow> distinct ys; |
|
3489 |
\<And> ys zs. \<lbrakk> ys \<in> set xs ; zs \<in> set xs ; ys \<noteq> zs \<rbrakk> \<Longrightarrow> set ys \<inter> set zs = {} |
|
3490 |
\<rbrakk> \<Longrightarrow> distinct (concat xs)" |
|
3491 |
by (induct xs) auto |
|
17501 | 3492 |
|
60758 | 3493 |
text \<open>It is best to avoid this indexed version of distinct, but |
3494 |
sometimes it is useful.\<close> |
|
17501 | 3495 |
|
68719 | 3496 |
lemma distinct_conv_nth: "distinct xs = (\<forall>i < size xs. \<forall>j < size xs. i \<noteq> j \<longrightarrow> xs!i \<noteq> xs!j)" |
3497 |
proof (induct xs) |
|
3498 |
case (Cons x xs) |
|
3499 |
show ?case |
|
3500 |
apply (auto simp add: Cons nth_Cons split: nat.split_asm) |
|
3501 |
apply (metis Suc_less_eq2 in_set_conv_nth less_not_refl zero_less_Suc)+ |
|
3502 |
done |
|
3503 |
qed auto |
|
13114 | 3504 |
|
18490 | 3505 |
lemma nth_eq_iff_index_eq: |
58807 | 3506 |
"\<lbrakk> distinct xs; i < length xs; j < length xs \<rbrakk> \<Longrightarrow> (xs!i = xs!j) = (i = j)" |
18490 | 3507 |
by(auto simp: distinct_conv_nth) |
3508 |
||
64963 | 3509 |
lemma distinct_Ex1: |
63099
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents:
63092
diff
changeset
|
3510 |
"distinct xs \<Longrightarrow> x \<in> set xs \<Longrightarrow> (\<exists>!i. i < length xs \<and> xs ! i = x)" |
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents:
63092
diff
changeset
|
3511 |
by (auto simp: in_set_conv_nth nth_eq_iff_index_eq) |
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents:
63092
diff
changeset
|
3512 |
|
59728 | 3513 |
lemma inj_on_nth: "distinct xs \<Longrightarrow> \<forall>i \<in> I. i < length xs \<Longrightarrow> inj_on (nth xs) I" |
3514 |
by (rule inj_onI) (simp add: nth_eq_iff_index_eq) |
|
3515 |
||
63099
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents:
63092
diff
changeset
|
3516 |
lemma bij_betw_nth: |
64963 | 3517 |
assumes "distinct xs" "A = {..<length xs}" "B = set xs" |
67399 | 3518 |
shows "bij_betw ((!) xs) A B" |
63099
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents:
63092
diff
changeset
|
3519 |
using assms unfolding bij_betw_def |
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents:
63092
diff
changeset
|
3520 |
by (auto intro!: inj_on_nth simp: set_conv_nth) |
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents:
63092
diff
changeset
|
3521 |
|
56953 | 3522 |
lemma set_update_distinct: "\<lbrakk> distinct xs; n < length xs \<rbrakk> \<Longrightarrow> |
3523 |
set(xs[n := x]) = insert x (set xs - {xs!n})" |
|
3524 |
by(auto simp: set_eq_iff in_set_conv_nth nth_list_update nth_eq_iff_index_eq) |
|
3525 |
||
57537 | 3526 |
lemma distinct_swap[simp]: "\<lbrakk> i < size xs; j < size xs \<rbrakk> \<Longrightarrow> |
3527 |
distinct(xs[i := xs!j, j := xs!i]) = distinct xs" |
|
68719 | 3528 |
apply (simp add: distinct_conv_nth nth_list_update) |
57537 | 3529 |
apply safe |
3530 |
apply metis+ |
|
3531 |
done |
|
3532 |
||
3533 |
lemma set_swap[simp]: |
|
3534 |
"\<lbrakk> i < size xs; j < size xs \<rbrakk> \<Longrightarrow> set(xs[i := xs!j, j := xs!i]) = set xs" |
|
3535 |
by(simp add: set_conv_nth nth_list_update) metis |
|
3536 |
||
15110
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
3537 |
lemma distinct_card: "distinct xs ==> card (set xs) = size xs" |
24349 | 3538 |
by (induct xs) auto |
14388 | 3539 |
|
15110
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
3540 |
lemma card_distinct: "card (set xs) = size xs ==> distinct xs" |
14388 | 3541 |
proof (induct xs) |
3542 |
case (Cons x xs) |
|
3543 |
show ?case |
|
3544 |
proof (cases "x \<in> set xs") |
|
3545 |
case False with Cons show ?thesis by simp |
|
3546 |
next |
|
3547 |
case True with Cons.prems |
|
46440
d4994e2e7364
use 'primrec' to define "rotate1", for uniformity (and to help first-order tools that rely on "Spec_Rules")
blanchet
parents:
46439
diff
changeset
|
3548 |
have "card (set xs) = Suc (length xs)" |
62390 | 3549 |
by (simp add: card_insert_if split: if_split_asm) |
14388 | 3550 |
moreover have "card (set xs) \<le> length xs" by (rule card_length) |
3551 |
ultimately have False by simp |
|
3552 |
thus ?thesis .. |
|
3553 |
qed |
|
68719 | 3554 |
qed simp |
14388 | 3555 |
|
45115
93c1ac6727a3
adding lemma to List library for executable equation of the (refl) transitive closure
bulwahn
parents:
44928
diff
changeset
|
3556 |
lemma distinct_length_filter: "distinct xs \<Longrightarrow> length (filter P xs) = card ({x. P x} Int set xs)" |
93c1ac6727a3
adding lemma to List library for executable equation of the (refl) transitive closure
bulwahn
parents:
44928
diff
changeset
|
3557 |
by (induct xs) (auto) |
93c1ac6727a3
adding lemma to List library for executable equation of the (refl) transitive closure
bulwahn
parents:
44928
diff
changeset
|
3558 |
|
67091 | 3559 |
lemma not_distinct_decomp: "\<not> distinct ws \<Longrightarrow> \<exists>xs ys zs y. ws = xs@[y]@ys@[y]@zs" |
68719 | 3560 |
proof (induct n == "length ws" arbitrary:ws) |
3561 |
case (Suc n ws) |
|
3562 |
then show ?case |
|
3563 |
using length_Suc_conv [of ws n] |
|
3564 |
apply (auto simp: eq_commute) |
|
3565 |
apply (metis append_Nil in_set_conv_decomp_first) |
|
3566 |
by (metis append_Cons) |
|
3567 |
qed simp |
|
18490 | 3568 |
|
45841 | 3569 |
lemma not_distinct_conv_prefix: |
3570 |
defines "dec as xs y ys \<equiv> y \<in> set xs \<and> distinct xs \<and> as = xs @ y # ys" |
|
3571 |
shows "\<not>distinct as \<longleftrightarrow> (\<exists>xs y ys. dec as xs y ys)" (is "?L = ?R") |
|
3572 |
proof |
|
3573 |
assume "?L" then show "?R" |
|
3574 |
proof (induct "length as" arbitrary: as rule: less_induct) |
|
3575 |
case less |
|
3576 |
obtain xs ys zs y where decomp: "as = (xs @ y # ys) @ y # zs" |
|
3577 |
using not_distinct_decomp[OF less.prems] by auto |
|
3578 |
show ?case |
|
3579 |
proof (cases "distinct (xs @ y # ys)") |
|
3580 |
case True |
|
3581 |
with decomp have "dec as (xs @ y # ys) y zs" by (simp add: dec_def) |
|
3582 |
then show ?thesis by blast |
|
3583 |
next |
|
3584 |
case False |
|
3585 |
with less decomp obtain xs' y' ys' where "dec (xs @ y # ys) xs' y' ys'" |
|
3586 |
by atomize_elim auto |
|
3587 |
with decomp have "dec as xs' y' (ys' @ y # zs)" by (simp add: dec_def) |
|
3588 |
then show ?thesis by blast |
|
3589 |
qed |
|
3590 |
qed |
|
3591 |
qed (auto simp: dec_def) |
|
3592 |
||
49948
744934b818c7
moved quite generic material from theory Enum to more appropriate places
haftmann
parents:
49808
diff
changeset
|
3593 |
lemma distinct_product: |
57247 | 3594 |
"distinct xs \<Longrightarrow> distinct ys \<Longrightarrow> distinct (List.product xs ys)" |
3595 |
by (induct xs) (auto intro: inj_onI simp add: distinct_map) |
|
49948
744934b818c7
moved quite generic material from theory Enum to more appropriate places
haftmann
parents:
49808
diff
changeset
|
3596 |
|
53721
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
traytel
parents:
53689
diff
changeset
|
3597 |
lemma distinct_product_lists: |
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
traytel
parents:
53689
diff
changeset
|
3598 |
assumes "\<forall>xs \<in> set xss. distinct xs" |
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
traytel
parents:
53689
diff
changeset
|
3599 |
shows "distinct (product_lists xss)" |
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
traytel
parents:
53689
diff
changeset
|
3600 |
using assms proof (induction xss) |
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
traytel
parents:
53689
diff
changeset
|
3601 |
case (Cons xs xss) note * = this |
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
traytel
parents:
53689
diff
changeset
|
3602 |
then show ?case |
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
traytel
parents:
53689
diff
changeset
|
3603 |
proof (cases "product_lists xss") |
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
traytel
parents:
53689
diff
changeset
|
3604 |
case Nil then show ?thesis by (induct xs) simp_all |
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
traytel
parents:
53689
diff
changeset
|
3605 |
next |
64963 | 3606 |
case (Cons ps pss) with * show ?thesis |
53721
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
traytel
parents:
53689
diff
changeset
|
3607 |
by (auto intro!: inj_onI distinct_concat simp add: distinct_map) |
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
traytel
parents:
53689
diff
changeset
|
3608 |
qed |
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
traytel
parents:
53689
diff
changeset
|
3609 |
qed simp |
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
traytel
parents:
53689
diff
changeset
|
3610 |
|
18490 | 3611 |
lemma length_remdups_concat: |
44921 | 3612 |
"length (remdups (concat xss)) = card (\<Union>xs\<in>set xss. set xs)" |
58807 | 3613 |
by (simp add: distinct_card [symmetric]) |
17906 | 3614 |
|
33639
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
hoelzl
parents:
33593
diff
changeset
|
3615 |
lemma length_remdups_card_conv: "length(remdups xs) = card(set xs)" |
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
hoelzl
parents:
33593
diff
changeset
|
3616 |
proof - |
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
hoelzl
parents:
33593
diff
changeset
|
3617 |
have xs: "concat[xs] = xs" by simp |
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
hoelzl
parents:
33593
diff
changeset
|
3618 |
from length_remdups_concat[of "[xs]"] show ?thesis unfolding xs by simp |
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
hoelzl
parents:
33593
diff
changeset
|
3619 |
qed |
17906 | 3620 |
|
58807 | 3621 |
lemma remdups_remdups: "remdups (remdups xs) = remdups xs" |
3622 |
by (induct xs) simp_all |
|
36275 | 3623 |
|
36851 | 3624 |
lemma distinct_butlast: |
46500
0196966d6d2d
removing unnecessary premises in theorems of List theory
bulwahn
parents:
46448
diff
changeset
|
3625 |
assumes "distinct xs" |
36851 | 3626 |
shows "distinct (butlast xs)" |
46500
0196966d6d2d
removing unnecessary premises in theorems of List theory
bulwahn
parents:
46448
diff
changeset
|
3627 |
proof (cases "xs = []") |
0196966d6d2d
removing unnecessary premises in theorems of List theory
bulwahn
parents:
46448
diff
changeset
|
3628 |
case False |
60758 | 3629 |
from \<open>xs \<noteq> []\<close> obtain ys y where "xs = ys @ [y]" by (cases xs rule: rev_cases) auto |
3630 |
with \<open>distinct xs\<close> show ?thesis by simp |
|
46500
0196966d6d2d
removing unnecessary premises in theorems of List theory
bulwahn
parents:
46448
diff
changeset
|
3631 |
qed (auto) |
36851 | 3632 |
|
39728 | 3633 |
lemma remdups_map_remdups: |
3634 |
"remdups (map f (remdups xs)) = remdups (map f xs)" |
|
58807 | 3635 |
by (induct xs) simp_all |
39728 | 3636 |
|
39915
ecf97cf3d248
turned distinct and sorted into inductive predicates: yields nice induction principles for free; more elegant proofs
haftmann
parents:
39774
diff
changeset
|
3637 |
lemma distinct_zipI1: |
ecf97cf3d248
turned distinct and sorted into inductive predicates: yields nice induction principles for free; more elegant proofs
haftmann
parents:
39774
diff
changeset
|
3638 |
assumes "distinct xs" |
ecf97cf3d248
turned distinct and sorted into inductive predicates: yields nice induction principles for free; more elegant proofs
haftmann
parents:
39774
diff
changeset
|
3639 |
shows "distinct (zip xs ys)" |
ecf97cf3d248
turned distinct and sorted into inductive predicates: yields nice induction principles for free; more elegant proofs
haftmann
parents:
39774
diff
changeset
|
3640 |
proof (rule zip_obtain_same_length) |
ecf97cf3d248
turned distinct and sorted into inductive predicates: yields nice induction principles for free; more elegant proofs
haftmann
parents:
39774
diff
changeset
|
3641 |
fix xs' :: "'a list" and ys' :: "'b list" and n |
ecf97cf3d248
turned distinct and sorted into inductive predicates: yields nice induction principles for free; more elegant proofs
haftmann
parents:
39774
diff
changeset
|
3642 |
assume "length xs' = length ys'" |
ecf97cf3d248
turned distinct and sorted into inductive predicates: yields nice induction principles for free; more elegant proofs
haftmann
parents:
39774
diff
changeset
|
3643 |
assume "xs' = take n xs" |
ecf97cf3d248
turned distinct and sorted into inductive predicates: yields nice induction principles for free; more elegant proofs
haftmann
parents:
39774
diff
changeset
|
3644 |
with assms have "distinct xs'" by simp |
60758 | 3645 |
with \<open>length xs' = length ys'\<close> show "distinct (zip xs' ys')" |
39915
ecf97cf3d248
turned distinct and sorted into inductive predicates: yields nice induction principles for free; more elegant proofs
haftmann
parents:
39774
diff
changeset
|
3646 |
by (induct xs' ys' rule: list_induct2) (auto elim: in_set_zipE) |
ecf97cf3d248
turned distinct and sorted into inductive predicates: yields nice induction principles for free; more elegant proofs
haftmann
parents:
39774
diff
changeset
|
3647 |
qed |
ecf97cf3d248
turned distinct and sorted into inductive predicates: yields nice induction principles for free; more elegant proofs
haftmann
parents:
39774
diff
changeset
|
3648 |
|
ecf97cf3d248
turned distinct and sorted into inductive predicates: yields nice induction principles for free; more elegant proofs
haftmann
parents:
39774
diff
changeset
|
3649 |
lemma distinct_zipI2: |
ecf97cf3d248
turned distinct and sorted into inductive predicates: yields nice induction principles for free; more elegant proofs
haftmann
parents:
39774
diff
changeset
|
3650 |
assumes "distinct ys" |
ecf97cf3d248
turned distinct and sorted into inductive predicates: yields nice induction principles for free; more elegant proofs
haftmann
parents:
39774
diff
changeset
|
3651 |
shows "distinct (zip xs ys)" |
ecf97cf3d248
turned distinct and sorted into inductive predicates: yields nice induction principles for free; more elegant proofs
haftmann
parents:
39774
diff
changeset
|
3652 |
proof (rule zip_obtain_same_length) |
ecf97cf3d248
turned distinct and sorted into inductive predicates: yields nice induction principles for free; more elegant proofs
haftmann
parents:
39774
diff
changeset
|
3653 |
fix xs' :: "'b list" and ys' :: "'a list" and n |
ecf97cf3d248
turned distinct and sorted into inductive predicates: yields nice induction principles for free; more elegant proofs
haftmann
parents:
39774
diff
changeset
|
3654 |
assume "length xs' = length ys'" |
ecf97cf3d248
turned distinct and sorted into inductive predicates: yields nice induction principles for free; more elegant proofs
haftmann
parents:
39774
diff
changeset
|
3655 |
assume "ys' = take n ys" |
ecf97cf3d248
turned distinct and sorted into inductive predicates: yields nice induction principles for free; more elegant proofs
haftmann
parents:
39774
diff
changeset
|
3656 |
with assms have "distinct ys'" by simp |
60758 | 3657 |
with \<open>length xs' = length ys'\<close> show "distinct (zip xs' ys')" |
39915
ecf97cf3d248
turned distinct and sorted into inductive predicates: yields nice induction principles for free; more elegant proofs
haftmann
parents:
39774
diff
changeset
|
3658 |
by (induct xs' ys' rule: list_induct2) (auto elim: in_set_zipE) |
ecf97cf3d248
turned distinct and sorted into inductive predicates: yields nice induction principles for free; more elegant proofs
haftmann
parents:
39774
diff
changeset
|
3659 |
qed |
ecf97cf3d248
turned distinct and sorted into inductive predicates: yields nice induction principles for free; more elegant proofs
haftmann
parents:
39774
diff
changeset
|
3660 |
|
47122 | 3661 |
lemma set_take_disj_set_drop_if_distinct: |
3662 |
"distinct vs \<Longrightarrow> i \<le> j \<Longrightarrow> set (take i vs) \<inter> set (drop j vs) = {}" |
|
3663 |
by (auto simp: in_set_conv_nth distinct_conv_nth) |
|
3664 |
||
44635
3d046864ebe6
added two lemmas about "distinct" to help Sledgehammer
blanchet
parents:
44619
diff
changeset
|
3665 |
(* The next two lemmas help Sledgehammer. *) |
3d046864ebe6
added two lemmas about "distinct" to help Sledgehammer
blanchet
parents:
44619
diff
changeset
|
3666 |
|
3d046864ebe6
added two lemmas about "distinct" to help Sledgehammer
blanchet
parents:
44619
diff
changeset
|
3667 |
lemma distinct_singleton: "distinct [x]" by simp |
3d046864ebe6
added two lemmas about "distinct" to help Sledgehammer
blanchet
parents:
44619
diff
changeset
|
3668 |
|
3d046864ebe6
added two lemmas about "distinct" to help Sledgehammer
blanchet
parents:
44619
diff
changeset
|
3669 |
lemma distinct_length_2_or_more: |
58807 | 3670 |
"distinct (a # b # xs) \<longleftrightarrow> (a \<noteq> b \<and> distinct (a # xs) \<and> distinct (b # xs))" |
56085 | 3671 |
by force |
44635
3d046864ebe6
added two lemmas about "distinct" to help Sledgehammer
blanchet
parents:
44619
diff
changeset
|
3672 |
|
58969 | 3673 |
lemma remdups_adj_altdef: "(remdups_adj xs = ys) \<longleftrightarrow> |
67091 | 3674 |
(\<exists>f::nat => nat. mono f \<and> f ` {0 ..< size xs} = {0 ..< size ys} |
58969 | 3675 |
\<and> (\<forall>i < size xs. xs!i = ys!(f i)) |
61941 | 3676 |
\<and> (\<forall>i. i + 1 < size xs \<longrightarrow> (xs!i = xs!(i+1) \<longleftrightarrow> f i = f(i+1))))" (is "?L \<longleftrightarrow> (\<exists>f. ?p f xs ys)") |
58969 | 3677 |
proof |
3678 |
assume ?L |
|
3679 |
then show "\<exists>f. ?p f xs ys" |
|
3680 |
proof (induct xs arbitrary: ys rule: remdups_adj.induct) |
|
3681 |
case (1 ys) |
|
3682 |
thus ?case by (intro exI[of _ id]) (auto simp: mono_def) |
|
3683 |
next |
|
3684 |
case (2 x ys) |
|
3685 |
thus ?case by (intro exI[of _ id]) (auto simp: mono_def) |
|
3686 |
next |
|
3687 |
case (3 x1 x2 xs ys) |
|
3688 |
let ?xs = "x1 # x2 # xs" |
|
3689 |
let ?cond = "x1 = x2" |
|
63040 | 3690 |
define zs where "zs = remdups_adj (x2 # xs)" |
58969 | 3691 |
from 3(1-2)[of zs] |
3692 |
obtain f where p: "?p f (x2 # xs) zs" unfolding zs_def by (cases ?cond) auto |
|
3693 |
then have f0: "f 0 = 0" |
|
3694 |
by (intro mono_image_least[where f=f]) blast+ |
|
3695 |
from p have mono: "mono f" and f_xs_zs: "f ` {0..<length (x2 # xs)} = {0..<length zs}" by auto |
|
3696 |
have ys: "ys = (if x1 = x2 then zs else x1 # zs)" |
|
3697 |
unfolding 3(3)[symmetric] zs_def by auto |
|
3698 |
have zs0: "zs ! 0 = x2" unfolding zs_def by (induct xs) auto |
|
3699 |
have zsne: "zs \<noteq> []" unfolding zs_def by (induct xs) auto |
|
3700 |
let ?Succ = "if ?cond then id else Suc" |
|
3701 |
let ?x1 = "if ?cond then id else Cons x1" |
|
3702 |
let ?f = "\<lambda> i. if i = 0 then 0 else ?Succ (f (i - 1))" |
|
3703 |
have ys: "ys = ?x1 zs" unfolding ys by (cases ?cond, auto) |
|
60758 | 3704 |
have mono: "mono ?f" using \<open>mono f\<close> unfolding mono_def by auto |
58969 | 3705 |
show ?case unfolding ys |
3706 |
proof (intro exI[of _ ?f] conjI allI impI) |
|
3707 |
show "mono ?f" by fact |
|
3708 |
next |
|
3709 |
fix i assume i: "i < length ?xs" |
|
3710 |
with p show "?xs ! i = ?x1 zs ! (?f i)" using zs0 by auto |
|
3711 |
next |
|
3712 |
fix i assume i: "i + 1 < length ?xs" |
|
3713 |
with p show "(?xs ! i = ?xs ! (i + 1)) = (?f i = ?f (i + 1))" |
|
3714 |
by (cases i) (auto simp: f0) |
|
3715 |
next |
|
3716 |
have id: "{0 ..< length (?x1 zs)} = insert 0 (?Succ ` {0 ..< length zs})" |
|
3717 |
using zsne by (cases ?cond, auto) |
|
3718 |
{ fix i assume "i < Suc (length xs)" |
|
67399 | 3719 |
hence "Suc i \<in> {0..<Suc (Suc (length xs))} \<inter> Collect ((<) 0)" by auto |
58969 | 3720 |
from imageI[OF this, of "\<lambda>i. ?Succ (f (i - Suc 0))"] |
67399 | 3721 |
have "?Succ (f i) \<in> (\<lambda>i. ?Succ (f (i - Suc 0))) ` ({0..<Suc (Suc (length xs))} \<inter> Collect ((<) 0))" by auto |
58969 | 3722 |
} |
3723 |
then show "?f ` {0 ..< length ?xs} = {0 ..< length (?x1 zs)}" |
|
3724 |
unfolding id f_xs_zs[symmetric] by auto |
|
3725 |
qed |
|
3726 |
qed |
|
3727 |
next |
|
3728 |
assume "\<exists> f. ?p f xs ys" |
|
3729 |
then show ?L |
|
3730 |
proof (induct xs arbitrary: ys rule: remdups_adj.induct) |
|
3731 |
case 1 then show ?case by auto |
|
3732 |
next |
|
3733 |
case (2 x) then obtain f where f_img: "f ` {0 ..< size [x]} = {0 ..< size ys}" |
|
3734 |
and f_nth: "\<And>i. i < size [x] \<Longrightarrow> [x]!i = ys!(f i)" |
|
3735 |
by blast |
|
3736 |
||
3737 |
have "length ys = card (f ` {0 ..< size [x]})" |
|
3738 |
using f_img by auto |
|
63540 | 3739 |
then have *: "length ys = 1" by auto |
58969 | 3740 |
then have "f 0 = 0" using f_img by auto |
63540 | 3741 |
with * show ?case using f_nth by (cases ys) auto |
58969 | 3742 |
next |
3743 |
case (3 x1 x2 xs) |
|
3744 |
from "3.prems" obtain f where f_mono: "mono f" |
|
3745 |
and f_img: "f ` {0..<length (x1 # x2 # xs)} = {0..<length ys}" |
|
3746 |
and f_nth: |
|
3747 |
"\<And>i. i < length (x1 # x2 # xs) \<Longrightarrow> (x1 # x2 # xs) ! i = ys ! f i" |
|
3748 |
"\<And>i. i + 1 < length (x1 # x2 #xs) \<Longrightarrow> |
|
3749 |
((x1 # x2 # xs) ! i = (x1 # x2 # xs) ! (i + 1)) = (f i = f (i + 1))" |
|
3750 |
by blast |
|
3751 |
||
3752 |
show ?case |
|
3753 |
proof cases |
|
3754 |
assume "x1 = x2" |
|
3755 |
||
67091 | 3756 |
let ?f' = "f \<circ> Suc" |
58969 | 3757 |
|
3758 |
have "remdups_adj (x1 # xs) = ys" |
|
3759 |
proof (intro "3.hyps" exI conjI impI allI) |
|
3760 |
show "mono ?f'" |
|
3761 |
using f_mono by (simp add: mono_iff_le_Suc) |
|
3762 |
next |
|
3763 |
have "?f' ` {0 ..< length (x1 # xs)} = f ` {Suc 0 ..< length (x1 # x2 # xs)}" |
|
69850 | 3764 |
apply safe |
3765 |
apply fastforce |
|
3766 |
subgoal for \<dots> x by (cases x) auto |
|
3767 |
done |
|
58969 | 3768 |
also have "\<dots> = f ` {0 ..< length (x1 # x2 # xs)}" |
3769 |
proof - |
|
3770 |
have "f 0 = f (Suc 0)" using \<open>x1 = x2\<close> f_nth[of 0] by simp |
|
69850 | 3771 |
then show ?thesis |
3772 |
apply safe |
|
3773 |
apply fastforce |
|
3774 |
subgoal for \<dots> x by (cases x) auto |
|
3775 |
done |
|
58969 | 3776 |
qed |
3777 |
also have "\<dots> = {0 ..< length ys}" by fact |
|
3778 |
finally show "?f' ` {0 ..< length (x1 # xs)} = {0 ..< length ys}" . |
|
3779 |
qed (insert f_nth[of "Suc i" for i], auto simp: \<open>x1 = x2\<close>) |
|
3780 |
then show ?thesis using \<open>x1 = x2\<close> by simp |
|
3781 |
next |
|
3782 |
assume "x1 \<noteq> x2" |
|
3783 |
||
3784 |
have "2 \<le> length ys" |
|
3785 |
proof - |
|
3786 |
have "2 = card {f 0, f 1}" using \<open>x1 \<noteq> x2\<close> f_nth[of 0] by auto |
|
3787 |
also have "\<dots> \<le> card (f ` {0..< length (x1 # x2 # xs)})" |
|
3788 |
by (rule card_mono) auto |
|
3789 |
finally show ?thesis using f_img by simp |
|
3790 |
qed |
|
3791 |
||
3792 |
have "f 0 = 0" using f_mono f_img by (rule mono_image_least) simp |
|
3793 |
||
3794 |
have "f (Suc 0) = Suc 0" |
|
3795 |
proof (rule ccontr) |
|
3796 |
assume "f (Suc 0) \<noteq> Suc 0" |
|
3797 |
then have "Suc 0 < f (Suc 0)" using f_nth[of 0] \<open>x1 \<noteq> x2\<close> \<open>f 0 = 0\<close> by auto |
|
3798 |
then have "\<And>i. Suc 0 < f (Suc i)" |
|
3799 |
using f_mono |
|
3800 |
by (meson Suc_le_mono le0 less_le_trans monoD) |
|
69850 | 3801 |
then have "Suc 0 \<noteq> f i" for i using \<open>f 0 = 0\<close> |
3802 |
by (cases i) fastforce+ |
|
58969 | 3803 |
then have "Suc 0 \<notin> f ` {0 ..< length (x1 # x2 # xs)}" by auto |
3804 |
then show False using f_img \<open>2 \<le> length ys\<close> by auto |
|
3805 |
qed |
|
3806 |
obtain ys' where "ys = x1 # x2 # ys'" |
|
3807 |
using \<open>2 \<le> length ys\<close> f_nth[of 0] f_nth[of 1] |
|
3808 |
apply (cases ys) |
|
69850 | 3809 |
apply (rename_tac [2] ys', case_tac [2] ys') |
3810 |
apply (auto simp: \<open>f 0 = 0\<close> \<open>f (Suc 0) = Suc 0\<close>) |
|
3811 |
done |
|
58969 | 3812 |
|
63040 | 3813 |
define f' where "f' x = f (Suc x) - 1" for x |
58969 | 3814 |
|
3815 |
{ fix i |
|
3816 |
have "Suc 0 \<le> f (Suc 0)" using f_nth[of 0] \<open>x1 \<noteq> x2\<close> \<open>f 0 = 0\<close> by auto |
|
3817 |
also have "\<dots> \<le> f (Suc i)" using f_mono by (rule monoD) arith |
|
3818 |
finally have "Suc 0 \<le> f (Suc i)" . |
|
3819 |
} note Suc0_le_f_Suc = this |
|
3820 |
||
3821 |
{ fix i have "f (Suc i) = Suc (f' i)" |
|
3822 |
using Suc0_le_f_Suc[of i] by (auto simp: f'_def) |
|
3823 |
} note f_Suc = this |
|
3824 |
||
3825 |
have "remdups_adj (x2 # xs) = (x2 # ys')" |
|
3826 |
proof (intro "3.hyps" exI conjI impI allI) |
|
3827 |
show "mono f'" |
|
3828 |
using Suc0_le_f_Suc f_mono by (auto simp: f'_def mono_iff_le_Suc le_diff_iff) |
|
3829 |
next |
|
3830 |
have "f' ` {0 ..< length (x2 # xs)} = (\<lambda>x. f x - 1) ` {0 ..< length (x1 # x2 #xs)}" |
|
68719 | 3831 |
by (auto simp: f'_def \<open>f 0 = 0\<close> \<open>f (Suc 0) = Suc 0\<close> image_def Bex_def less_Suc_eq_0_disj) |
58969 | 3832 |
also have "\<dots> = (\<lambda>x. x - 1) ` f ` {0 ..< length (x1 # x2 #xs)}" |
3833 |
by (auto simp: image_comp) |
|
3834 |
also have "\<dots> = (\<lambda>x. x - 1) ` {0 ..< length ys}" |
|
3835 |
by (simp only: f_img) |
|
3836 |
also have "\<dots> = {0 ..< length (x2 # ys')}" |
|
3837 |
using \<open>ys = _\<close> by (fastforce intro: rev_image_eqI) |
|
3838 |
finally show "f' ` {0 ..< length (x2 # xs)} = {0 ..< length (x2 # ys')}" . |
|
3839 |
qed (insert f_nth[of "Suc i" for i] \<open>x1 \<noteq> x2\<close>, auto simp add: f_Suc \<open>ys = _\<close>) |
|
3840 |
then show ?case using \<open>ys = _\<close> \<open>x1 \<noteq> x2\<close> by simp |
|
3841 |
qed |
|
3842 |
qed |
|
3843 |
qed |
|
3844 |
||
58041 | 3845 |
lemma hd_remdups_adj[simp]: "hd (remdups_adj xs) = hd xs" |
58807 | 3846 |
by (induction xs rule: remdups_adj.induct) simp_all |
58041 | 3847 |
|
53721
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
traytel
parents:
53689
diff
changeset
|
3848 |
lemma remdups_adj_Cons: "remdups_adj (x # xs) = |
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
traytel
parents:
53689
diff
changeset
|
3849 |
(case remdups_adj xs of [] \<Rightarrow> [x] | y # xs \<Rightarrow> if x = y then y # xs else x # y # xs)" |
58807 | 3850 |
by (induct xs arbitrary: x) (auto split: list.splits) |
53721
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
traytel
parents:
53689
diff
changeset
|
3851 |
|
64963 | 3852 |
lemma remdups_adj_append_two: |
53721
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
traytel
parents:
53689
diff
changeset
|
3853 |
"remdups_adj (xs @ [x,y]) = remdups_adj (xs @ [x]) @ (if x = y then [] else [y])" |
58807 | 3854 |
by (induct xs rule: remdups_adj.induct, simp_all) |
53721
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
traytel
parents:
53689
diff
changeset
|
3855 |
|
58041 | 3856 |
lemma remdups_adj_adjacent: |
3857 |
"Suc i < length (remdups_adj xs) \<Longrightarrow> remdups_adj xs ! i \<noteq> remdups_adj xs ! Suc i" |
|
3858 |
proof (induction xs arbitrary: i rule: remdups_adj.induct) |
|
3859 |
case (3 x y xs i) |
|
3860 |
thus ?case by (cases i, cases "x = y") (simp, auto simp: hd_conv_nth[symmetric]) |
|
3861 |
qed simp_all |
|
3862 |
||
53721
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
traytel
parents:
53689
diff
changeset
|
3863 |
lemma remdups_adj_rev[simp]: "remdups_adj (rev xs) = rev (remdups_adj xs)" |
58807 | 3864 |
by (induct xs rule: remdups_adj.induct, simp_all add: remdups_adj_append_two) |
53721
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
traytel
parents:
53689
diff
changeset
|
3865 |
|
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
traytel
parents:
53689
diff
changeset
|
3866 |
lemma remdups_adj_length[simp]: "length (remdups_adj xs) \<le> length xs" |
58807 | 3867 |
by (induct xs rule: remdups_adj.induct, auto) |
53721
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
traytel
parents:
53689
diff
changeset
|
3868 |
|
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
traytel
parents:
53689
diff
changeset
|
3869 |
lemma remdups_adj_length_ge1[simp]: "xs \<noteq> [] \<Longrightarrow> length (remdups_adj xs) \<ge> Suc 0" |
58807 | 3870 |
by (induct xs rule: remdups_adj.induct, simp_all) |
53721
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
traytel
parents:
53689
diff
changeset
|
3871 |
|
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
traytel
parents:
53689
diff
changeset
|
3872 |
lemma remdups_adj_Nil_iff[simp]: "remdups_adj xs = [] \<longleftrightarrow> xs = []" |
58807 | 3873 |
by (induct xs rule: remdups_adj.induct, simp_all) |
53721
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
traytel
parents:
53689
diff
changeset
|
3874 |
|
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
traytel
parents:
53689
diff
changeset
|
3875 |
lemma remdups_adj_set[simp]: "set (remdups_adj xs) = set xs" |
58807 | 3876 |
by (induct xs rule: remdups_adj.induct, simp_all) |
53721
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
traytel
parents:
53689
diff
changeset
|
3877 |
|
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
traytel
parents:
53689
diff
changeset
|
3878 |
lemma remdups_adj_Cons_alt[simp]: "x # tl (remdups_adj (x # xs)) = remdups_adj (x # xs)" |
58807 | 3879 |
by (induct xs rule: remdups_adj.induct, auto) |
53721
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
traytel
parents:
53689
diff
changeset
|
3880 |
|
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
traytel
parents:
53689
diff
changeset
|
3881 |
lemma remdups_adj_distinct: "distinct xs \<Longrightarrow> remdups_adj xs = xs" |
58807 | 3882 |
by (induct xs rule: remdups_adj.induct, simp_all) |
53721
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
traytel
parents:
53689
diff
changeset
|
3883 |
|
64963 | 3884 |
lemma remdups_adj_append: |
53721
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
traytel
parents:
53689
diff
changeset
|
3885 |
"remdups_adj (xs\<^sub>1 @ x # xs\<^sub>2) = remdups_adj (xs\<^sub>1 @ [x]) @ tl (remdups_adj (x # xs\<^sub>2))" |
58807 | 3886 |
by (induct xs\<^sub>1 rule: remdups_adj.induct, simp_all) |
53721
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
traytel
parents:
53689
diff
changeset
|
3887 |
|
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
traytel
parents:
53689
diff
changeset
|
3888 |
lemma remdups_adj_singleton: |
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
traytel
parents:
53689
diff
changeset
|
3889 |
"remdups_adj xs = [x] \<Longrightarrow> xs = replicate (length xs) x" |
62390 | 3890 |
by (induct xs rule: remdups_adj.induct, auto split: if_split_asm) |
53721
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
traytel
parents:
53689
diff
changeset
|
3891 |
|
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
traytel
parents:
53689
diff
changeset
|
3892 |
lemma remdups_adj_map_injective: |
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
traytel
parents:
53689
diff
changeset
|
3893 |
assumes "inj f" |
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
traytel
parents:
53689
diff
changeset
|
3894 |
shows "remdups_adj (map f xs) = map f (remdups_adj xs)" |
58807 | 3895 |
by (induct xs rule: remdups_adj.induct) (auto simp add: injD[OF assms]) |
3896 |
||
62065 | 3897 |
lemma remdups_adj_replicate: |
3898 |
"remdups_adj (replicate n x) = (if n = 0 then [] else [x])" |
|
3899 |
by (induction n) (auto simp: remdups_adj_Cons) |
|
3900 |
||
58807 | 3901 |
lemma remdups_upt [simp]: "remdups [m..<n] = [m..<n]" |
58437 | 3902 |
proof (cases "m \<le> n") |
3903 |
case False then show ?thesis by simp |
|
3904 |
next |
|
3905 |
case True then obtain q where "n = m + q" |
|
3906 |
by (auto simp add: le_iff_add) |
|
3907 |
moreover have "remdups [m..<m + q] = [m..<m + q]" |
|
3908 |
by (induct q) simp_all |
|
3909 |
ultimately show ?thesis by simp |
|
3910 |
qed |
|
3911 |
||
49948
744934b818c7
moved quite generic material from theory Enum to more appropriate places
haftmann
parents:
49808
diff
changeset
|
3912 |
|
69593 | 3913 |
subsubsection \<open>\<^const>\<open>insert\<close>\<close> |
34978
874150ddd50a
canonical insert operation; generalized lemma foldl_apply_inv to foldl_apply
haftmann
parents:
34942
diff
changeset
|
3914 |
|
874150ddd50a
canonical insert operation; generalized lemma foldl_apply_inv to foldl_apply
haftmann
parents:
34942
diff
changeset
|
3915 |
lemma in_set_insert [simp]: |
874150ddd50a
canonical insert operation; generalized lemma foldl_apply_inv to foldl_apply
haftmann
parents:
34942
diff
changeset
|
3916 |
"x \<in> set xs \<Longrightarrow> List.insert x xs = xs" |
58807 | 3917 |
by (simp add: List.insert_def) |
34978
874150ddd50a
canonical insert operation; generalized lemma foldl_apply_inv to foldl_apply
haftmann
parents:
34942
diff
changeset
|
3918 |
|
874150ddd50a
canonical insert operation; generalized lemma foldl_apply_inv to foldl_apply
haftmann
parents:
34942
diff
changeset
|
3919 |
lemma not_in_set_insert [simp]: |
874150ddd50a
canonical insert operation; generalized lemma foldl_apply_inv to foldl_apply
haftmann
parents:
34942
diff
changeset
|
3920 |
"x \<notin> set xs \<Longrightarrow> List.insert x xs = x # xs" |
58807 | 3921 |
by (simp add: List.insert_def) |
3922 |
||
3923 |
lemma insert_Nil [simp]: "List.insert x [] = [x]" |
|
3924 |
by simp |
|
3925 |
||
3926 |
lemma set_insert [simp]: "set (List.insert x xs) = insert x (set xs)" |
|
3927 |
by (auto simp add: List.insert_def) |
|
3928 |
||
3929 |
lemma distinct_insert [simp]: "distinct (List.insert x xs) = distinct xs" |
|
3930 |
by (simp add: List.insert_def) |
|
35295 | 3931 |
|
36275 | 3932 |
lemma insert_remdups: |
3933 |
"List.insert x (remdups xs) = remdups (List.insert x xs)" |
|
58807 | 3934 |
by (simp add: List.insert_def) |
36275 | 3935 |
|
34978
874150ddd50a
canonical insert operation; generalized lemma foldl_apply_inv to foldl_apply
haftmann
parents:
34942
diff
changeset
|
3936 |
|
69593 | 3937 |
subsubsection \<open>\<^const>\<open>List.union\<close>\<close> |
60758 | 3938 |
|
3939 |
text\<open>This is all one should need to know about union:\<close> |
|
57198 | 3940 |
lemma set_union[simp]: "set (List.union xs ys) = set xs \<union> set ys" |
3941 |
unfolding List.union_def |
|
3942 |
by(induct xs arbitrary: ys) simp_all |
|
3943 |
||
3944 |
lemma distinct_union[simp]: "distinct(List.union xs ys) = distinct ys" |
|
3945 |
unfolding List.union_def |
|
3946 |
by(induct xs arbitrary: ys) simp_all |
|
3947 |
||
3948 |
||
69593 | 3949 |
subsubsection \<open>\<^const>\<open>List.find\<close>\<close> |
47122 | 3950 |
|
3951 |
lemma find_None_iff: "List.find P xs = None \<longleftrightarrow> \<not> (\<exists>x. x \<in> set xs \<and> P x)" |
|
3952 |
proof (induction xs) |
|
3953 |
case Nil thus ?case by simp |
|
3954 |
next |
|
3955 |
case (Cons x xs) thus ?case by (fastforce split: if_splits) |
|
3956 |
qed |
|
3957 |
||
3958 |
lemma find_Some_iff: |
|
3959 |
"List.find P xs = Some x \<longleftrightarrow> |
|
3960 |
(\<exists>i<length xs. P (xs!i) \<and> x = xs!i \<and> (\<forall>j<i. \<not> P (xs!j)))" |
|
3961 |
proof (induction xs) |
|
3962 |
case Nil thus ?case by simp |
|
3963 |
next |
|
3964 |
case (Cons x xs) thus ?case |
|
56085 | 3965 |
apply(auto simp: nth_Cons' split: if_splits) |
3966 |
using diff_Suc_1[unfolded One_nat_def] less_Suc_eq_0_disj by fastforce |
|
47122 | 3967 |
qed |
3968 |
||
3969 |
lemma find_cong[fundef_cong]: |
|
64963 | 3970 |
assumes "xs = ys" and "\<And>x. x \<in> set ys \<Longrightarrow> P x = Q x" |
47122 | 3971 |
shows "List.find P xs = List.find Q ys" |
3972 |
proof (cases "List.find P xs") |
|
3973 |
case None thus ?thesis by (metis find_None_iff assms) |
|
3974 |
next |
|
3975 |
case (Some x) |
|
3976 |
hence "List.find Q ys = Some x" using assms |
|
3977 |
by (auto simp add: find_Some_iff) |
|
3978 |
thus ?thesis using Some by auto |
|
3979 |
qed |
|
3980 |
||
52379 | 3981 |
lemma find_dropWhile: |
3982 |
"List.find P xs = (case dropWhile (Not \<circ> P) xs |
|
3983 |
of [] \<Rightarrow> None |
|
3984 |
| x # _ \<Rightarrow> Some x)" |
|
58807 | 3985 |
by (induct xs) simp_all |
52379 | 3986 |
|
47122 | 3987 |
|
69593 | 3988 |
subsubsection \<open>\<^const>\<open>count_list\<close>\<close> |
60541 | 3989 |
|
3990 |
lemma count_notin[simp]: "x \<notin> set xs \<Longrightarrow> count_list xs x = 0" |
|
59728 | 3991 |
by (induction xs) auto |
3992 |
||
60541 | 3993 |
lemma count_le_length: "count_list xs x \<le> length xs" |
59728 | 3994 |
by (induction xs) auto |
3995 |
||
64267 | 3996 |
lemma sum_count_set: |
3997 |
"set xs \<subseteq> X \<Longrightarrow> finite X \<Longrightarrow> sum (count_list xs) X = length xs" |
|
68719 | 3998 |
proof (induction xs arbitrary: X) |
3999 |
case (Cons x xs) |
|
4000 |
then show ?case |
|
4001 |
apply (auto simp: sum.If_cases sum.remove) |
|
4002 |
by (metis (no_types) Cons.IH Cons.prems(2) diff_eq sum.remove) |
|
4003 |
qed simp |
|
59728 | 4004 |
|
4005 |
||
69593 | 4006 |
subsubsection \<open>\<^const>\<open>List.extract\<close>\<close> |
55807 | 4007 |
|
4008 |
lemma extract_None_iff: "List.extract P xs = None \<longleftrightarrow> \<not> (\<exists> x\<in>set xs. P x)" |
|
4009 |
by(auto simp: extract_def dropWhile_eq_Cons_conv split: list.splits) |
|
4010 |
(metis in_set_conv_decomp) |
|
4011 |
||
4012 |
lemma extract_SomeE: |
|
4013 |
"List.extract P xs = Some (ys, y, zs) \<Longrightarrow> |
|
64963 | 4014 |
xs = ys @ y # zs \<and> P y \<and> \<not> (\<exists> y \<in> set ys. P y)" |
55807 | 4015 |
by(auto simp: extract_def dropWhile_eq_Cons_conv split: list.splits) |
4016 |
||
4017 |
lemma extract_Some_iff: |
|
4018 |
"List.extract P xs = Some (ys, y, zs) \<longleftrightarrow> |
|
64963 | 4019 |
xs = ys @ y # zs \<and> P y \<and> \<not> (\<exists> y \<in> set ys. P y)" |
55807 | 4020 |
by(auto simp: extract_def dropWhile_eq_Cons_conv dest: set_takeWhileD split: list.splits) |
4021 |
||
4022 |
lemma extract_Nil_code[code]: "List.extract P [] = None" |
|
4023 |
by(simp add: extract_def) |
|
4024 |
||
4025 |
lemma extract_Cons_code[code]: |
|
4026 |
"List.extract P (x # xs) = (if P x then Some ([], x, xs) else |
|
4027 |
(case List.extract P xs of |
|
4028 |
None \<Rightarrow> None | |
|
4029 |
Some (ys, y, zs) \<Rightarrow> Some (x#ys, y, zs)))" |
|
56085 | 4030 |
by(auto simp add: extract_def comp_def split: list.splits) |
4031 |
(metis dropWhile_eq_Nil_conv list.distinct(1)) |
|
55807 | 4032 |
|
4033 |
||
69593 | 4034 |
subsubsection \<open>\<^const>\<open>remove1\<close>\<close> |
15110
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
4035 |
|
18049 | 4036 |
lemma remove1_append: |
4037 |
"remove1 x (xs @ ys) = |
|
4038 |
(if x \<in> set xs then remove1 x xs @ ys else xs @ remove1 x ys)" |
|
4039 |
by (induct xs) auto |
|
4040 |
||
36903 | 4041 |
lemma remove1_commute: "remove1 x (remove1 y zs) = remove1 y (remove1 x zs)" |
4042 |
by (induct zs) auto |
|
4043 |
||
23479 | 4044 |
lemma in_set_remove1[simp]: |
67613 | 4045 |
"a \<noteq> b \<Longrightarrow> a \<in> set(remove1 b xs) = (a \<in> set xs)" |
68719 | 4046 |
by (induct xs) auto |
23479 | 4047 |
|
67613 | 4048 |
lemma set_remove1_subset: "set(remove1 x xs) \<subseteq> set xs" |
68719 | 4049 |
by (induct xs) auto |
15110
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
4050 |
|
67613 | 4051 |
lemma set_remove1_eq [simp]: "distinct xs \<Longrightarrow> set(remove1 x xs) = set xs - {x}" |
68719 | 4052 |
by (induct xs) auto |
15110
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
4053 |
|
23479 | 4054 |
lemma length_remove1: |
67613 | 4055 |
"length(remove1 x xs) = (if x \<in> set xs then length xs - 1 else length xs)" |
58807 | 4056 |
by (induct xs) (auto dest!:length_pos_if_in_set) |
23479 | 4057 |
|
18049 | 4058 |
lemma remove1_filter_not[simp]: |
4059 |
"\<not> P x \<Longrightarrow> remove1 x (filter P xs) = filter P xs" |
|
4060 |
by(induct xs) auto |
|
4061 |
||
39073 | 4062 |
lemma filter_remove1: |
4063 |
"filter Q (remove1 x xs) = remove1 x (filter Q xs)" |
|
4064 |
by (induct xs) auto |
|
4065 |
||
67613 | 4066 |
lemma notin_set_remove1[simp]: "x \<notin> set xs \<Longrightarrow> x \<notin> set(remove1 y xs)" |
58807 | 4067 |
by(insert set_remove1_subset) fast |
15110
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
4068 |
|
67613 | 4069 |
lemma distinct_remove1[simp]: "distinct xs \<Longrightarrow> distinct(remove1 x xs)" |
15110
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
4070 |
by (induct xs) simp_all |
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
4071 |
|
36275 | 4072 |
lemma remove1_remdups: |
4073 |
"distinct xs \<Longrightarrow> remove1 x (remdups xs) = remdups (remove1 x xs)" |
|
58807 | 4074 |
by (induct xs) simp_all |
4075 |
||
4076 |
lemma remove1_idem: "x \<notin> set xs \<Longrightarrow> remove1 x xs = xs" |
|
4077 |
by (induct xs) simp_all |
|
37107 | 4078 |
|
13114 | 4079 |
|
69593 | 4080 |
subsubsection \<open>\<^const>\<open>removeAll\<close>\<close> |
27693 | 4081 |
|
34978
874150ddd50a
canonical insert operation; generalized lemma foldl_apply_inv to foldl_apply
haftmann
parents:
34942
diff
changeset
|
4082 |
lemma removeAll_filter_not_eq: |
874150ddd50a
canonical insert operation; generalized lemma foldl_apply_inv to foldl_apply
haftmann
parents:
34942
diff
changeset
|
4083 |
"removeAll x = filter (\<lambda>y. x \<noteq> y)" |
874150ddd50a
canonical insert operation; generalized lemma foldl_apply_inv to foldl_apply
haftmann
parents:
34942
diff
changeset
|
4084 |
proof |
874150ddd50a
canonical insert operation; generalized lemma foldl_apply_inv to foldl_apply
haftmann
parents:
34942
diff
changeset
|
4085 |
fix xs |
874150ddd50a
canonical insert operation; generalized lemma foldl_apply_inv to foldl_apply
haftmann
parents:
34942
diff
changeset
|
4086 |
show "removeAll x xs = filter (\<lambda>y. x \<noteq> y) xs" |
874150ddd50a
canonical insert operation; generalized lemma foldl_apply_inv to foldl_apply
haftmann
parents:
34942
diff
changeset
|
4087 |
by (induct xs) auto |
874150ddd50a
canonical insert operation; generalized lemma foldl_apply_inv to foldl_apply
haftmann
parents:
34942
diff
changeset
|
4088 |
qed |
874150ddd50a
canonical insert operation; generalized lemma foldl_apply_inv to foldl_apply
haftmann
parents:
34942
diff
changeset
|
4089 |
|
27693 | 4090 |
lemma removeAll_append[simp]: |
4091 |
"removeAll x (xs @ ys) = removeAll x xs @ removeAll x ys" |
|
4092 |
by (induct xs) auto |
|
4093 |
||
4094 |
lemma set_removeAll[simp]: "set(removeAll x xs) = set xs - {x}" |
|
4095 |
by (induct xs) auto |
|
4096 |
||
4097 |
lemma removeAll_id[simp]: "x \<notin> set xs \<Longrightarrow> removeAll x xs = xs" |
|
4098 |
by (induct xs) auto |
|
4099 |
||
46448
f1201fac7398
more specification of the quotient package in IsarRef
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
46440
diff
changeset
|
4100 |
(* Needs count:: 'a \<Rightarrow> 'a list \<Rightarrow> nat |
27693 | 4101 |
lemma length_removeAll: |
4102 |
"length(removeAll x xs) = length xs - count x xs" |
|
4103 |
*) |
|
4104 |
||
4105 |
lemma removeAll_filter_not[simp]: |
|
4106 |
"\<not> P x \<Longrightarrow> removeAll x (filter P xs) = filter P xs" |
|
4107 |
by(induct xs) auto |
|
4108 |
||
34978
874150ddd50a
canonical insert operation; generalized lemma foldl_apply_inv to foldl_apply
haftmann
parents:
34942
diff
changeset
|
4109 |
lemma distinct_removeAll: |
874150ddd50a
canonical insert operation; generalized lemma foldl_apply_inv to foldl_apply
haftmann
parents:
34942
diff
changeset
|
4110 |
"distinct xs \<Longrightarrow> distinct (removeAll x xs)" |
58807 | 4111 |
by (simp add: removeAll_filter_not_eq) |
27693 | 4112 |
|
4113 |
lemma distinct_remove1_removeAll: |
|
67613 | 4114 |
"distinct xs \<Longrightarrow> remove1 x xs = removeAll x xs" |
27693 | 4115 |
by (induct xs) simp_all |
4116 |
||
4117 |
lemma map_removeAll_inj_on: "inj_on f (insert x (set xs)) \<Longrightarrow> |
|
4118 |
map f (removeAll x xs) = removeAll (f x) (map f xs)" |
|
4119 |
by (induct xs) (simp_all add:inj_on_def) |
|
4120 |
||
4121 |
lemma map_removeAll_inj: "inj f \<Longrightarrow> |
|
4122 |
map f (removeAll x xs) = removeAll (f x) (map f xs)" |
|
56085 | 4123 |
by (rule map_removeAll_inj_on, erule subset_inj_on, rule subset_UNIV) |
27693 | 4124 |
|
63365 | 4125 |
lemma length_removeAll_less_eq [simp]: |
4126 |
"length (removeAll x xs) \<le> length xs" |
|
4127 |
by (simp add: removeAll_filter_not_eq) |
|
4128 |
||
4129 |
lemma length_removeAll_less [termination_simp]: |
|
4130 |
"x \<in> set xs \<Longrightarrow> length (removeAll x xs) < length xs" |
|
4131 |
by (auto dest: length_filter_less simp add: removeAll_filter_not_eq) |
|
4132 |
||
27693 | 4133 |
|
69593 | 4134 |
subsubsection \<open>\<^const>\<open>replicate\<close>\<close> |
13114 | 4135 |
|
13142 | 4136 |
lemma length_replicate [simp]: "length (replicate n x) = n" |
13145 | 4137 |
by (induct n) auto |
13124 | 4138 |
|
58437 | 4139 |
lemma replicate_eqI: |
4140 |
assumes "length xs = n" and "\<And>y. y \<in> set xs \<Longrightarrow> y = x" |
|
4141 |
shows "xs = replicate n x" |
|
67613 | 4142 |
using assms |
4143 |
proof (induct xs arbitrary: n) |
|
58437 | 4144 |
case Nil then show ?case by simp |
4145 |
next |
|
4146 |
case (Cons x xs) then show ?case by (cases n) simp_all |
|
4147 |
qed |
|
4148 |
||
36622
e393a91f86df
Generalize swap_inj_on; add simps for Times; add Ex_list_of_length, log_inj; Added missing locale edges for linordered semiring with 1.
hoelzl
parents:
36275
diff
changeset
|
4149 |
lemma Ex_list_of_length: "\<exists>xs. length xs = n" |
e393a91f86df
Generalize swap_inj_on; add simps for Times; add Ex_list_of_length, log_inj; Added missing locale edges for linordered semiring with 1.
hoelzl
parents:
36275
diff
changeset
|
4150 |
by (rule exI[of _ "replicate n undefined"]) simp |
e393a91f86df
Generalize swap_inj_on; add simps for Times; add Ex_list_of_length, log_inj; Added missing locale edges for linordered semiring with 1.
hoelzl
parents:
36275
diff
changeset
|
4151 |
|
13142 | 4152 |
lemma map_replicate [simp]: "map f (replicate n x) = replicate n (f x)" |
13145 | 4153 |
by (induct n) auto |
13114 | 4154 |
|
31363
7493b571b37d
Added theorems about distinct & concat, map & replicate and concat & replicate
hoelzl
parents:
31264
diff
changeset
|
4155 |
lemma map_replicate_const: |
7493b571b37d
Added theorems about distinct & concat, map & replicate and concat & replicate
hoelzl
parents:
31264
diff
changeset
|
4156 |
"map (\<lambda> x. k) lst = replicate (length lst) k" |
66853 | 4157 |
by (induct lst) auto |
31363
7493b571b37d
Added theorems about distinct & concat, map & replicate and concat & replicate
hoelzl
parents:
31264
diff
changeset
|
4158 |
|
13114 | 4159 |
lemma replicate_app_Cons_same: |
13145 | 4160 |
"(replicate n x) @ (x # xs) = x # replicate n x @ xs" |
4161 |
by (induct n) auto |
|
13114 | 4162 |
|
13142 | 4163 |
lemma rev_replicate [simp]: "rev (replicate n x) = replicate n x" |
58807 | 4164 |
by (induct n) (auto simp: replicate_app_Cons_same) |
13114 | 4165 |
|
13142 | 4166 |
lemma replicate_add: "replicate (n + m) x = replicate n x @ replicate m x" |
13145 | 4167 |
by (induct n) auto |
13114 | 4168 |
|
60758 | 4169 |
text\<open>Courtesy of Matthias Daum:\<close> |
16397 | 4170 |
lemma append_replicate_commute: |
4171 |
"replicate n x @ replicate k x = replicate k x @ replicate n x" |
|
68719 | 4172 |
by (metis add.commute replicate_add) |
16397 | 4173 |
|
60758 | 4174 |
text\<open>Courtesy of Andreas Lochbihler:\<close> |
31080 | 4175 |
lemma filter_replicate: |
4176 |
"filter P (replicate n x) = (if P x then replicate n x else [])" |
|
4177 |
by(induct n) auto |
|
4178 |
||
13142 | 4179 |
lemma hd_replicate [simp]: "n \<noteq> 0 ==> hd (replicate n x) = x" |
13145 | 4180 |
by (induct n) auto |
13114 | 4181 |
|
46500
0196966d6d2d
removing unnecessary premises in theorems of List theory
bulwahn
parents:
46448
diff
changeset
|
4182 |
lemma tl_replicate [simp]: "tl (replicate n x) = replicate (n - 1) x" |
13145 | 4183 |
by (induct n) auto |
13114 | 4184 |
|
13142 | 4185 |
lemma last_replicate [simp]: "n \<noteq> 0 ==> last (replicate n x) = x" |
13145 | 4186 |
by (atomize (full), induct n) auto |
13114 | 4187 |
|
24526 | 4188 |
lemma nth_replicate[simp]: "i < n ==> (replicate n x)!i = x" |
58807 | 4189 |
by (induct n arbitrary: i)(auto simp: nth_Cons split: nat.split) |
13114 | 4190 |
|
60758 | 4191 |
text\<open>Courtesy of Matthias Daum (2 lemmas):\<close> |
16397 | 4192 |
lemma take_replicate[simp]: "take i (replicate k x) = replicate (min i k) x" |
68719 | 4193 |
proof (cases "k \<le> i") |
4194 |
case True |
|
4195 |
then show ?thesis |
|
4196 |
by (simp add: min_def) |
|
4197 |
next |
|
4198 |
case False |
|
4199 |
then have "replicate k x = replicate i x @ replicate (k - i) x" |
|
4200 |
by (simp add: replicate_add [symmetric]) |
|
4201 |
then show ?thesis |
|
4202 |
by (simp add: min_def) |
|
4203 |
qed |
|
16397 | 4204 |
|
24526 | 4205 |
lemma drop_replicate[simp]: "drop i (replicate k x) = replicate (k-i) x" |
68719 | 4206 |
proof (induct k arbitrary: i) |
4207 |
case (Suc k) |
|
4208 |
then show ?case |
|
4209 |
by (simp add: drop_Cons') |
|
4210 |
qed simp |
|
16397 | 4211 |
|
13142 | 4212 |
lemma set_replicate_Suc: "set (replicate (Suc n) x) = {x}" |
13145 | 4213 |
by (induct n) auto |
13114 | 4214 |
|
13142 | 4215 |
lemma set_replicate [simp]: "n \<noteq> 0 ==> set (replicate n x) = {x}" |
13145 | 4216 |
by (fast dest!: not0_implies_Suc intro!: set_replicate_Suc) |
13114 | 4217 |
|
13142 | 4218 |
lemma set_replicate_conv_if: "set (replicate n x) = (if n = 0 then {} else {x})" |
13145 | 4219 |
by auto |
13114 | 4220 |
|
67091 | 4221 |
lemma in_set_replicate[simp]: "(x \<in> set (replicate n y)) = (x = y \<and> n \<noteq> 0)" |
37456 | 4222 |
by (simp add: set_replicate_conv_if) |
4223 |
||
37454 | 4224 |
lemma Ball_set_replicate[simp]: |
67091 | 4225 |
"(\<forall>x \<in> set(replicate n a). P x) = (P a \<or> n=0)" |
37454 | 4226 |
by(simp add: set_replicate_conv_if) |
4227 |
||
4228 |
lemma Bex_set_replicate[simp]: |
|
67091 | 4229 |
"(\<exists>x \<in> set(replicate n a). P x) = (P a \<and> n\<noteq>0)" |
37454 | 4230 |
by(simp add: set_replicate_conv_if) |
13114 | 4231 |
|
24796 | 4232 |
lemma replicate_append_same: |
4233 |
"replicate i x @ [x] = x # replicate i x" |
|
4234 |
by (induct i) simp_all |
|
4235 |
||
4236 |
lemma map_replicate_trivial: |
|
4237 |
"map (\<lambda>i. x) [0..<i] = replicate i x" |
|
66853 | 4238 |
by (induct i) (simp_all add: replicate_append_same) |
24796 | 4239 |
|
31363
7493b571b37d
Added theorems about distinct & concat, map & replicate and concat & replicate
hoelzl
parents:
31264
diff
changeset
|
4240 |
lemma concat_replicate_trivial[simp]: |
7493b571b37d
Added theorems about distinct & concat, map & replicate and concat & replicate
hoelzl
parents:
31264
diff
changeset
|
4241 |
"concat (replicate i []) = []" |
7493b571b37d
Added theorems about distinct & concat, map & replicate and concat & replicate
hoelzl
parents:
31264
diff
changeset
|
4242 |
by (induct i) (auto simp add: map_replicate_const) |
13114 | 4243 |
|
28642 | 4244 |
lemma replicate_empty[simp]: "(replicate n x = []) \<longleftrightarrow> n=0" |
4245 |
by (induct n) auto |
|
4246 |
||
4247 |
lemma empty_replicate[simp]: "([] = replicate n x) \<longleftrightarrow> n=0" |
|
4248 |
by (induct n) auto |
|
4249 |
||
4250 |
lemma replicate_eq_replicate[simp]: |
|
67091 | 4251 |
"(replicate m x = replicate n y) \<longleftrightarrow> (m=n \<and> (m\<noteq>0 \<longrightarrow> x=y))" |
68719 | 4252 |
proof (induct m arbitrary: n) |
4253 |
case (Suc m n) |
|
4254 |
then show ?case |
|
4255 |
by (induct n) auto |
|
4256 |
qed simp |
|
28642 | 4257 |
|
39534
c798d4f1b682
generalized lemma insort_remove1 to insort_key_remove1
haftmann
parents:
39302
diff
changeset
|
4258 |
lemma replicate_length_filter: |
c798d4f1b682
generalized lemma insort_remove1 to insort_key_remove1
haftmann
parents:
39302
diff
changeset
|
4259 |
"replicate (length (filter (\<lambda>y. x = y) xs)) x = filter (\<lambda>y. x = y) xs" |
c798d4f1b682
generalized lemma insort_remove1 to insort_key_remove1
haftmann
parents:
39302
diff
changeset
|
4260 |
by (induct xs) auto |
c798d4f1b682
generalized lemma insort_remove1 to insort_key_remove1
haftmann
parents:
39302
diff
changeset
|
4261 |
|
42714
fcba668b0839
add a lemma about commutative append to List.thy
noschinl
parents:
42713
diff
changeset
|
4262 |
lemma comm_append_are_replicate: |
66853 | 4263 |
"\<lbrakk> xs \<noteq> []; ys \<noteq> []; xs @ ys = ys @ xs \<rbrakk> |
4264 |
\<Longrightarrow> \<exists>m n zs. concat (replicate m zs) = xs \<and> concat (replicate n zs) = ys" |
|
4265 |
proof (induction "length (xs @ ys)" arbitrary: xs ys rule: less_induct) |
|
42714
fcba668b0839
add a lemma about commutative append to List.thy
noschinl
parents:
42713
diff
changeset
|
4266 |
case less |
fcba668b0839
add a lemma about commutative append to List.thy
noschinl
parents:
42713
diff
changeset
|
4267 |
|
63040 | 4268 |
define xs' ys' where "xs' = (if (length xs \<le> length ys) then xs else ys)" |
4269 |
and "ys' = (if (length xs \<le> length ys) then ys else xs)" |
|
42714
fcba668b0839
add a lemma about commutative append to List.thy
noschinl
parents:
42713
diff
changeset
|
4270 |
then have |
fcba668b0839
add a lemma about commutative append to List.thy
noschinl
parents:
42713
diff
changeset
|
4271 |
prems': "length xs' \<le> length ys'" |
fcba668b0839
add a lemma about commutative append to List.thy
noschinl
parents:
42713
diff
changeset
|
4272 |
"xs' @ ys' = ys' @ xs'" |
fcba668b0839
add a lemma about commutative append to List.thy
noschinl
parents:
42713
diff
changeset
|
4273 |
and "xs' \<noteq> []" |
fcba668b0839
add a lemma about commutative append to List.thy
noschinl
parents:
42713
diff
changeset
|
4274 |
and len: "length (xs @ ys) = length (xs' @ ys')" |
fcba668b0839
add a lemma about commutative append to List.thy
noschinl
parents:
42713
diff
changeset
|
4275 |
using less by (auto intro: less.hyps) |
fcba668b0839
add a lemma about commutative append to List.thy
noschinl
parents:
42713
diff
changeset
|
4276 |
|
fcba668b0839
add a lemma about commutative append to List.thy
noschinl
parents:
42713
diff
changeset
|
4277 |
from prems' |
fcba668b0839
add a lemma about commutative append to List.thy
noschinl
parents:
42713
diff
changeset
|
4278 |
obtain ws where "ys' = xs' @ ws" |
fcba668b0839
add a lemma about commutative append to List.thy
noschinl
parents:
42713
diff
changeset
|
4279 |
by (auto simp: append_eq_append_conv2) |
fcba668b0839
add a lemma about commutative append to List.thy
noschinl
parents:
42713
diff
changeset
|
4280 |
|
fcba668b0839
add a lemma about commutative append to List.thy
noschinl
parents:
42713
diff
changeset
|
4281 |
have "\<exists>m n zs. concat (replicate m zs) = xs' \<and> concat (replicate n zs) = ys'" |
fcba668b0839
add a lemma about commutative append to List.thy
noschinl
parents:
42713
diff
changeset
|
4282 |
proof (cases "ws = []") |
fcba668b0839
add a lemma about commutative append to List.thy
noschinl
parents:
42713
diff
changeset
|
4283 |
case True |
fcba668b0839
add a lemma about commutative append to List.thy
noschinl
parents:
42713
diff
changeset
|
4284 |
then have "concat (replicate 1 xs') = xs'" |
fcba668b0839
add a lemma about commutative append to List.thy
noschinl
parents:
42713
diff
changeset
|
4285 |
and "concat (replicate 1 xs') = ys'" |
60758 | 4286 |
using \<open>ys' = xs' @ ws\<close> by auto |
42714
fcba668b0839
add a lemma about commutative append to List.thy
noschinl
parents:
42713
diff
changeset
|
4287 |
then show ?thesis by blast |
fcba668b0839
add a lemma about commutative append to List.thy
noschinl
parents:
42713
diff
changeset
|
4288 |
next |
fcba668b0839
add a lemma about commutative append to List.thy
noschinl
parents:
42713
diff
changeset
|
4289 |
case False |
60758 | 4290 |
from \<open>ys' = xs' @ ws\<close> and \<open>xs' @ ys' = ys' @ xs'\<close> |
42714
fcba668b0839
add a lemma about commutative append to List.thy
noschinl
parents:
42713
diff
changeset
|
4291 |
have "xs' @ ws = ws @ xs'" by simp |
fcba668b0839
add a lemma about commutative append to List.thy
noschinl
parents:
42713
diff
changeset
|
4292 |
then have "\<exists>m n zs. concat (replicate m zs) = xs' \<and> concat (replicate n zs) = ws" |
60758 | 4293 |
using False and \<open>xs' \<noteq> []\<close> and \<open>ys' = xs' @ ws\<close> and len |
42714
fcba668b0839
add a lemma about commutative append to List.thy
noschinl
parents:
42713
diff
changeset
|
4294 |
by (intro less.hyps) auto |
53374
a14d2a854c02
tuned proofs -- clarified flow of facts wrt. calculation;
wenzelm
parents:
53017
diff
changeset
|
4295 |
then obtain m n zs where *: "concat (replicate m zs) = xs'" |
42714
fcba668b0839
add a lemma about commutative append to List.thy
noschinl
parents:
42713
diff
changeset
|
4296 |
and "concat (replicate n zs) = ws" by blast |
fcba668b0839
add a lemma about commutative append to List.thy
noschinl
parents:
42713
diff
changeset
|
4297 |
then have "concat (replicate (m + n) zs) = ys'" |
60758 | 4298 |
using \<open>ys' = xs' @ ws\<close> |
42714
fcba668b0839
add a lemma about commutative append to List.thy
noschinl
parents:
42713
diff
changeset
|
4299 |
by (simp add: replicate_add) |
53374
a14d2a854c02
tuned proofs -- clarified flow of facts wrt. calculation;
wenzelm
parents:
53017
diff
changeset
|
4300 |
with * show ?thesis by blast |
42714
fcba668b0839
add a lemma about commutative append to List.thy
noschinl
parents:
42713
diff
changeset
|
4301 |
qed |
fcba668b0839
add a lemma about commutative append to List.thy
noschinl
parents:
42713
diff
changeset
|
4302 |
then show ?case |
56085 | 4303 |
using xs'_def ys'_def by meson |
42714
fcba668b0839
add a lemma about commutative append to List.thy
noschinl
parents:
42713
diff
changeset
|
4304 |
qed |
fcba668b0839
add a lemma about commutative append to List.thy
noschinl
parents:
42713
diff
changeset
|
4305 |
|
fcba668b0839
add a lemma about commutative append to List.thy
noschinl
parents:
42713
diff
changeset
|
4306 |
lemma comm_append_is_replicate: |
fcba668b0839
add a lemma about commutative append to List.thy
noschinl
parents:
42713
diff
changeset
|
4307 |
fixes xs ys :: "'a list" |
fcba668b0839
add a lemma about commutative append to List.thy
noschinl
parents:
42713
diff
changeset
|
4308 |
assumes "xs \<noteq> []" "ys \<noteq> []" |
fcba668b0839
add a lemma about commutative append to List.thy
noschinl
parents:
42713
diff
changeset
|
4309 |
assumes "xs @ ys = ys @ xs" |
fcba668b0839
add a lemma about commutative append to List.thy
noschinl
parents:
42713
diff
changeset
|
4310 |
shows "\<exists>n zs. n > 1 \<and> concat (replicate n zs) = xs @ ys" |
fcba668b0839
add a lemma about commutative append to List.thy
noschinl
parents:
42713
diff
changeset
|
4311 |
proof - |
fcba668b0839
add a lemma about commutative append to List.thy
noschinl
parents:
42713
diff
changeset
|
4312 |
obtain m n zs where "concat (replicate m zs) = xs" |
fcba668b0839
add a lemma about commutative append to List.thy
noschinl
parents:
42713
diff
changeset
|
4313 |
and "concat (replicate n zs) = ys" |
56085 | 4314 |
using comm_append_are_replicate[of xs ys, OF assms] by blast |
42714
fcba668b0839
add a lemma about commutative append to List.thy
noschinl
parents:
42713
diff
changeset
|
4315 |
then have "m + n > 1" and "concat (replicate (m+n) zs) = xs @ ys" |
60758 | 4316 |
using \<open>xs \<noteq> []\<close> and \<open>ys \<noteq> []\<close> |
42714
fcba668b0839
add a lemma about commutative append to List.thy
noschinl
parents:
42713
diff
changeset
|
4317 |
by (auto simp: replicate_add) |
fcba668b0839
add a lemma about commutative append to List.thy
noschinl
parents:
42713
diff
changeset
|
4318 |
then show ?thesis by blast |
fcba668b0839
add a lemma about commutative append to List.thy
noschinl
parents:
42713
diff
changeset
|
4319 |
qed |
fcba668b0839
add a lemma about commutative append to List.thy
noschinl
parents:
42713
diff
changeset
|
4320 |
|
52380 | 4321 |
lemma Cons_replicate_eq: |
4322 |
"x # xs = replicate n y \<longleftrightarrow> x = y \<and> n > 0 \<and> xs = replicate (n - 1) x" |
|
66853 | 4323 |
by (induct n) auto |
52380 | 4324 |
|
4325 |
lemma replicate_length_same: |
|
4326 |
"(\<forall>y\<in>set xs. y = x) \<Longrightarrow> replicate (length xs) x = xs" |
|
66853 | 4327 |
by (induct xs) simp_all |
52380 | 4328 |
|
4329 |
lemma foldr_replicate [simp]: |
|
4330 |
"foldr f (replicate n x) = f x ^^ n" |
|
66853 | 4331 |
by (induct n) (simp_all) |
52380 | 4332 |
|
4333 |
lemma fold_replicate [simp]: |
|
4334 |
"fold f (replicate n x) = f x ^^ n" |
|
66853 | 4335 |
by (subst foldr_fold [symmetric]) simp_all |
52380 | 4336 |
|
28642 | 4337 |
|
69593 | 4338 |
subsubsection \<open>\<^const>\<open>enumerate\<close>\<close> |
51173 | 4339 |
|
4340 |
lemma enumerate_simps [simp, code]: |
|
4341 |
"enumerate n [] = []" |
|
4342 |
"enumerate n (x # xs) = (n, x) # enumerate (Suc n) xs" |
|
68719 | 4343 |
by (simp_all add: enumerate_eq_zip upt_rec) |
51173 | 4344 |
|
4345 |
lemma length_enumerate [simp]: |
|
4346 |
"length (enumerate n xs) = length xs" |
|
66853 | 4347 |
by (simp add: enumerate_eq_zip) |
51173 | 4348 |
|
4349 |
lemma map_fst_enumerate [simp]: |
|
4350 |
"map fst (enumerate n xs) = [n..<n + length xs]" |
|
66853 | 4351 |
by (simp add: enumerate_eq_zip) |
51173 | 4352 |
|
4353 |
lemma map_snd_enumerate [simp]: |
|
4354 |
"map snd (enumerate n xs) = xs" |
|
66853 | 4355 |
by (simp add: enumerate_eq_zip) |
64963 | 4356 |
|
51173 | 4357 |
lemma in_set_enumerate_eq: |
4358 |
"p \<in> set (enumerate n xs) \<longleftrightarrow> n \<le> fst p \<and> fst p < length xs + n \<and> nth xs (fst p - n) = snd p" |
|
4359 |
proof - |
|
4360 |
{ fix m |
|
4361 |
assume "n \<le> m" |
|
4362 |
moreover assume "m < length xs + n" |
|
4363 |
ultimately have "[n..<n + length xs] ! (m - n) = m \<and> |
|
4364 |
xs ! (m - n) = xs ! (m - n) \<and> m - n < length xs" by auto |
|
4365 |
then have "\<exists>q. [n..<n + length xs] ! q = m \<and> |
|
4366 |
xs ! q = xs ! (m - n) \<and> q < length xs" .. |
|
4367 |
} then show ?thesis by (cases p) (auto simp add: enumerate_eq_zip in_set_zip) |
|
4368 |
qed |
|
4369 |
||
66853 | 4370 |
lemma nth_enumerate_eq: "m < length xs \<Longrightarrow> enumerate n xs ! m = (n + m, xs ! m)" |
4371 |
by (simp add: enumerate_eq_zip) |
|
51173 | 4372 |
|
4373 |
lemma enumerate_replicate_eq: |
|
4374 |
"enumerate n (replicate m a) = map (\<lambda>q. (q, a)) [n..<n + m]" |
|
66853 | 4375 |
by (rule pair_list_eqI) |
4376 |
(simp_all add: enumerate_eq_zip comp_def map_replicate_const) |
|
51173 | 4377 |
|
4378 |
lemma enumerate_Suc_eq: |
|
4379 |
"enumerate (Suc n) xs = map (apfst Suc) (enumerate n xs)" |
|
66853 | 4380 |
by (rule pair_list_eqI) |
4381 |
(simp_all add: not_le, simp del: map_map add: map_Suc_upt map_map [symmetric]) |
|
51173 | 4382 |
|
52379 | 4383 |
lemma distinct_enumerate [simp]: |
4384 |
"distinct (enumerate n xs)" |
|
66853 | 4385 |
by (simp add: enumerate_eq_zip distinct_zipI1) |
52379 | 4386 |
|
58437 | 4387 |
lemma enumerate_append_eq: |
4388 |
"enumerate n (xs @ ys) = enumerate n xs @ enumerate (n + length xs) ys" |
|
68719 | 4389 |
by (simp add: enumerate_eq_zip add.assoc zip_append2) |
58437 | 4390 |
|
4391 |
lemma enumerate_map_upt: |
|
4392 |
"enumerate n (map f [n..<m]) = map (\<lambda>k. (k, f k)) [n..<m]" |
|
66853 | 4393 |
by (cases "n \<le> m") (simp_all add: zip_map2 zip_same_conv_map enumerate_eq_zip) |
64963 | 4394 |
|
51173 | 4395 |
|
69593 | 4396 |
subsubsection \<open>\<^const>\<open>rotate1\<close> and \<^const>\<open>rotate\<close>\<close> |
15302 | 4397 |
|
4398 |
lemma rotate0[simp]: "rotate 0 = id" |
|
4399 |
by(simp add:rotate_def) |
|
4400 |
||
4401 |
lemma rotate_Suc[simp]: "rotate (Suc n) xs = rotate1(rotate n xs)" |
|
4402 |
by(simp add:rotate_def) |
|
4403 |
||
4404 |
lemma rotate_add: |
|
67091 | 4405 |
"rotate (m+n) = rotate m \<circ> rotate n" |
15302 | 4406 |
by(simp add:rotate_def funpow_add) |
4407 |
||
4408 |
lemma rotate_rotate: "rotate m (rotate n xs) = rotate (m+n) xs" |
|
4409 |
by(simp add:rotate_add) |
|
4410 |
||
18049 | 4411 |
lemma rotate1_rotate_swap: "rotate1 (rotate n xs) = rotate n (rotate1 xs)" |
4412 |
by(simp add:rotate_def funpow_swap1) |
|
4413 |
||
68709 | 4414 |
lemma rotate1_length01[simp]: "length xs \<le> 1 \<Longrightarrow> rotate1 xs = xs" |
15302 | 4415 |
by(cases xs) simp_all |
4416 |
||
68709 | 4417 |
lemma rotate_length01[simp]: "length xs \<le> 1 \<Longrightarrow> rotate n xs = xs" |
68719 | 4418 |
by (induct n) (simp_all add:rotate_def) |
13114 | 4419 |
|
15302 | 4420 |
lemma rotate1_hd_tl: "xs \<noteq> [] \<Longrightarrow> rotate1 xs = tl xs @ [hd xs]" |
46440
d4994e2e7364
use 'primrec' to define "rotate1", for uniformity (and to help first-order tools that rely on "Spec_Rules")
blanchet
parents:
46439
diff
changeset
|
4421 |
by (cases xs) simp_all |
15302 | 4422 |
|
4423 |
lemma rotate_drop_take: |
|
4424 |
"rotate n xs = drop (n mod length xs) xs @ take (n mod length xs) xs" |
|
68719 | 4425 |
proof (induct n) |
4426 |
case (Suc n) |
|
4427 |
show ?case |
|
4428 |
proof (cases "xs = []") |
|
4429 |
case False |
|
4430 |
then show ?thesis |
|
4431 |
proof (cases "n mod length xs = 0") |
|
4432 |
case True |
|
4433 |
then show ?thesis |
|
4434 |
apply (simp add: mod_Suc) |
|
4435 |
by (simp add: False Suc.hyps drop_Suc rotate1_hd_tl take_Suc) |
|
4436 |
next |
|
4437 |
case False |
|
4438 |
with \<open>xs \<noteq> []\<close> Suc |
|
4439 |
show ?thesis |
|
4440 |
by (simp add: rotate_def mod_Suc rotate1_hd_tl drop_Suc[symmetric] drop_tl[symmetric] |
|
4441 |
take_hd_drop linorder_not_le) |
|
4442 |
qed |
|
4443 |
qed simp |
|
4444 |
qed simp |
|
13114 | 4445 |
|
15302 | 4446 |
lemma rotate_conv_mod: "rotate n xs = rotate (n mod length xs) xs" |
4447 |
by(simp add:rotate_drop_take) |
|
4448 |
||
4449 |
lemma rotate_id[simp]: "n mod length xs = 0 \<Longrightarrow> rotate n xs = xs" |
|
4450 |
by(simp add:rotate_drop_take) |
|
4451 |
||
4452 |
lemma length_rotate1[simp]: "length(rotate1 xs) = length xs" |
|
46440
d4994e2e7364
use 'primrec' to define "rotate1", for uniformity (and to help first-order tools that rely on "Spec_Rules")
blanchet
parents:
46439
diff
changeset
|
4453 |
by (cases xs) simp_all |
15302 | 4454 |
|
24526 | 4455 |
lemma length_rotate[simp]: "length(rotate n xs) = length xs" |
4456 |
by (induct n arbitrary: xs) (simp_all add:rotate_def) |
|
15302 | 4457 |
|
4458 |
lemma distinct1_rotate[simp]: "distinct(rotate1 xs) = distinct xs" |
|
46440
d4994e2e7364
use 'primrec' to define "rotate1", for uniformity (and to help first-order tools that rely on "Spec_Rules")
blanchet
parents:
46439
diff
changeset
|
4459 |
by (cases xs) auto |
15302 | 4460 |
|
4461 |
lemma distinct_rotate[simp]: "distinct(rotate n xs) = distinct xs" |
|
4462 |
by (induct n) (simp_all add:rotate_def) |
|
4463 |
||
4464 |
lemma rotate_map: "rotate n (map f xs) = map f (rotate n xs)" |
|
4465 |
by(simp add:rotate_drop_take take_map drop_map) |
|
4466 |
||
4467 |
lemma set_rotate1[simp]: "set(rotate1 xs) = set xs" |
|
46440
d4994e2e7364
use 'primrec' to define "rotate1", for uniformity (and to help first-order tools that rely on "Spec_Rules")
blanchet
parents:
46439
diff
changeset
|
4468 |
by (cases xs) auto |
15302 | 4469 |
|
4470 |
lemma set_rotate[simp]: "set(rotate n xs) = set xs" |
|
4471 |
by (induct n) (simp_all add:rotate_def) |
|
4472 |
||
4473 |
lemma rotate1_is_Nil_conv[simp]: "(rotate1 xs = []) = (xs = [])" |
|
46440
d4994e2e7364
use 'primrec' to define "rotate1", for uniformity (and to help first-order tools that rely on "Spec_Rules")
blanchet
parents:
46439
diff
changeset
|
4474 |
by (cases xs) auto |
15302 | 4475 |
|
4476 |
lemma rotate_is_Nil_conv[simp]: "(rotate n xs = []) = (xs = [])" |
|
4477 |
by (induct n) (simp_all add:rotate_def) |
|
13114 | 4478 |
|
15439 | 4479 |
lemma rotate_rev: |
4480 |
"rotate n (rev xs) = rev(rotate (length xs - (n mod length xs)) xs)" |
|
68527
2f4e2aab190a
Generalising and renaming some basic results
paulson <lp15@cam.ac.uk>
parents:
68362
diff
changeset
|
4481 |
proof (cases "length xs = 0 \<or> n mod length xs = 0") |
2f4e2aab190a
Generalising and renaming some basic results
paulson <lp15@cam.ac.uk>
parents:
68362
diff
changeset
|
4482 |
case False |
2f4e2aab190a
Generalising and renaming some basic results
paulson <lp15@cam.ac.uk>
parents:
68362
diff
changeset
|
4483 |
then show ?thesis |
2f4e2aab190a
Generalising and renaming some basic results
paulson <lp15@cam.ac.uk>
parents:
68362
diff
changeset
|
4484 |
by(simp add:rotate_drop_take rev_drop rev_take) |
2f4e2aab190a
Generalising and renaming some basic results
paulson <lp15@cam.ac.uk>
parents:
68362
diff
changeset
|
4485 |
qed force |
15439 | 4486 |
|
68719 | 4487 |
lemma hd_rotate_conv_nth: |
4488 |
assumes "xs \<noteq> []" shows "hd(rotate n xs) = xs!(n mod length xs)" |
|
4489 |
proof - |
|
4490 |
have "n mod length xs < length xs" |
|
4491 |
using assms by simp |
|
4492 |
then show ?thesis |
|
4493 |
by (metis drop_eq_Nil hd_append2 hd_drop_conv_nth leD rotate_drop_take) |
|
4494 |
qed |
|
18423 | 4495 |
|
68527
2f4e2aab190a
Generalising and renaming some basic results
paulson <lp15@cam.ac.uk>
parents:
68362
diff
changeset
|
4496 |
lemma rotate_append: "rotate (length l) (l @ q) = q @ l" |
2f4e2aab190a
Generalising and renaming some basic results
paulson <lp15@cam.ac.uk>
parents:
68362
diff
changeset
|
4497 |
by (induct l arbitrary: q) (auto simp add: rotate1_rotate_swap) |
2f4e2aab190a
Generalising and renaming some basic results
paulson <lp15@cam.ac.uk>
parents:
68362
diff
changeset
|
4498 |
|
13114 | 4499 |
|
69593 | 4500 |
subsubsection \<open>\<^const>\<open>nths\<close> --- a generalization of \<^const>\<open>nth\<close> to sets\<close> |
65956
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65350
diff
changeset
|
4501 |
|
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65350
diff
changeset
|
4502 |
lemma nths_empty [simp]: "nths xs {} = []" |
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65350
diff
changeset
|
4503 |
by (auto simp add: nths_def) |
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65350
diff
changeset
|
4504 |
|
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65350
diff
changeset
|
4505 |
lemma nths_nil [simp]: "nths [] A = []" |
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65350
diff
changeset
|
4506 |
by (auto simp add: nths_def) |
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65350
diff
changeset
|
4507 |
|
69203 | 4508 |
lemma nths_all: "\<forall>i < length xs. i \<in> I \<Longrightarrow> nths xs I = xs" |
4509 |
apply (simp add: nths_def) |
|
4510 |
apply (subst filter_True) |
|
4511 |
apply (clarsimp simp: in_set_zip subset_iff) |
|
4512 |
apply simp |
|
4513 |
done |
|
4514 |
||
65956
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65350
diff
changeset
|
4515 |
lemma length_nths: |
67613 | 4516 |
"length (nths xs I) = card{i. i < length xs \<and> i \<in> I}" |
65956
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65350
diff
changeset
|
4517 |
by(simp add: nths_def length_filter_conv_card cong:conj_cong) |
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65350
diff
changeset
|
4518 |
|
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65350
diff
changeset
|
4519 |
lemma nths_shift_lemma_Suc: |
68719 | 4520 |
"map fst (filter (\<lambda>p. P(Suc(snd p))) (zip xs is)) = |
4521 |
map fst (filter (\<lambda>p. P(snd p)) (zip xs (map Suc is)))" |
|
4522 |
proof (induct xs arbitrary: "is") |
|
4523 |
case (Cons x xs "is") |
|
4524 |
show ?case |
|
4525 |
by (cases "is") (auto simp add: Cons.hyps) |
|
4526 |
qed simp |
|
15281 | 4527 |
|
65956
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65350
diff
changeset
|
4528 |
lemma nths_shift_lemma: |
68249
949d93804740
First step to remove nonstandard "[x <- xs. P]" syntax: only input
nipkow
parents:
68215
diff
changeset
|
4529 |
"map fst (filter (\<lambda>p. snd p \<in> A) (zip xs [i..<i + length xs])) = |
949d93804740
First step to remove nonstandard "[x <- xs. P]" syntax: only input
nipkow
parents:
68215
diff
changeset
|
4530 |
map fst (filter (\<lambda>p. snd p + i \<in> A) (zip xs [0..<length xs]))" |
57512
cc97b347b301
reduced name variants for assoc and commute on plus and mult
haftmann
parents:
57418
diff
changeset
|
4531 |
by (induct xs rule: rev_induct) (simp_all add: add.commute) |
13114 | 4532 |
|
65956
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65350
diff
changeset
|
4533 |
lemma nths_append: |
67613 | 4534 |
"nths (l @ l') A = nths l A @ nths l' {j. j + length l \<in> A}" |
68719 | 4535 |
unfolding nths_def |
4536 |
proof (induct l' rule: rev_induct) |
|
4537 |
case (snoc x xs) |
|
4538 |
then show ?case |
|
4539 |
by (simp add: upt_add_eq_append[of 0] nths_shift_lemma add.commute) |
|
4540 |
qed auto |
|
13114 | 4541 |
|
65956
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65350
diff
changeset
|
4542 |
lemma nths_Cons: |
67613 | 4543 |
"nths (x # l) A = (if 0 \<in> A then [x] else []) @ nths l {j. Suc j \<in> A}" |
68719 | 4544 |
proof (induct l rule: rev_induct) |
4545 |
case (snoc x xs) |
|
4546 |
then show ?case |
|
4547 |
by (simp flip: append_Cons add: nths_append) |
|
4548 |
qed (auto simp: nths_def) |
|
13114 | 4549 |
|
66853 | 4550 |
lemma nths_map: "nths (map f xs) I = map f (nths xs I)" |
4551 |
by(induction xs arbitrary: I) (simp_all add: nths_Cons) |
|
4552 |
||
65956
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65350
diff
changeset
|
4553 |
lemma set_nths: "set(nths xs I) = {xs!i|i. i<size xs \<and> i \<in> I}" |
69203 | 4554 |
by (induct xs arbitrary: I) (auto simp: nths_Cons nth_Cons split:nat.split dest!: gr0_implies_Suc) |
15281 | 4555 |
|
65956
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65350
diff
changeset
|
4556 |
lemma set_nths_subset: "set(nths xs I) \<subseteq> set xs" |
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65350
diff
changeset
|
4557 |
by(auto simp add:set_nths) |
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65350
diff
changeset
|
4558 |
|
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65350
diff
changeset
|
4559 |
lemma notin_set_nthsI[simp]: "x \<notin> set xs \<Longrightarrow> x \<notin> set(nths xs I)" |
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65350
diff
changeset
|
4560 |
by(auto simp add:set_nths) |
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65350
diff
changeset
|
4561 |
|
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65350
diff
changeset
|
4562 |
lemma in_set_nthsD: "x \<in> set(nths xs I) \<Longrightarrow> x \<in> set xs" |
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65350
diff
changeset
|
4563 |
by(auto simp add:set_nths) |
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65350
diff
changeset
|
4564 |
|
67613 | 4565 |
lemma nths_singleton [simp]: "nths [x] A = (if 0 \<in> A then [x] else [])" |
65956
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65350
diff
changeset
|
4566 |
by (simp add: nths_Cons) |
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65350
diff
changeset
|
4567 |
|
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65350
diff
changeset
|
4568 |
lemma distinct_nthsI[simp]: "distinct xs \<Longrightarrow> distinct (nths xs I)" |
66656 | 4569 |
by (induct xs arbitrary: I) (auto simp: nths_Cons) |
65956
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65350
diff
changeset
|
4570 |
|
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65350
diff
changeset
|
4571 |
lemma nths_upt_eq_take [simp]: "nths l {..<n} = take n l" |
66656 | 4572 |
by (induct l rule: rev_induct) |
4573 |
(simp_all split: nat_diff_split add: nths_append) |
|
4574 |
||
69203 | 4575 |
lemma nths_nths: "nths (nths xs A) B = nths xs {i \<in> A. \<exists>j \<in> B. card {i' \<in> A. i' < i} = j}" |
4576 |
apply(induction xs arbitrary: A B) |
|
4577 |
apply(auto simp add: nths_Cons card_less_Suc card_less_Suc2) |
|
4578 |
done |
|
4579 |
||
4580 |
lemma drop_eq_nths: "drop n xs = nths xs {i. i \<ge> n}" |
|
4581 |
apply(induction xs arbitrary: n) |
|
4582 |
apply (auto simp add: nths_Cons nths_all drop_Cons' intro: arg_cong2[where f=nths, OF refl]) |
|
4583 |
done |
|
4584 |
||
4585 |
lemma nths_drop: "nths (drop n xs) I = nths xs ((+) n ` I)" |
|
4586 |
by(force simp: drop_eq_nths nths_nths simp flip: atLeastLessThan_iff |
|
4587 |
intro: arg_cong2[where f=nths, OF refl]) |
|
4588 |
||
66656 | 4589 |
lemma filter_eq_nths: "filter P xs = nths xs {i. i<length xs \<and> P(xs!i)}" |
4590 |
by(induction xs) (auto simp: nths_Cons) |
|
65956
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65350
diff
changeset
|
4591 |
|
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65350
diff
changeset
|
4592 |
lemma filter_in_nths: |
66656 | 4593 |
"distinct xs \<Longrightarrow> filter (%x. x \<in> set (nths xs s)) xs = nths xs s" |
24526 | 4594 |
proof (induct xs arbitrary: s) |
17501 | 4595 |
case Nil thus ?case by simp |
4596 |
next |
|
4597 |
case (Cons a xs) |
|
67091 | 4598 |
then have "\<forall>x. x \<in> set xs \<longrightarrow> x \<noteq> a" by auto |
65956
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65350
diff
changeset
|
4599 |
with Cons show ?case by(simp add: nths_Cons cong:filter_cong) |
17501 | 4600 |
qed |
4601 |
||
13114 | 4602 |
|
69593 | 4603 |
subsubsection \<open>\<^const>\<open>subseqs\<close> and \<^const>\<open>List.n_lists\<close>\<close> |
65956
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65350
diff
changeset
|
4604 |
|
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65350
diff
changeset
|
4605 |
lemma length_subseqs: "length (subseqs xs) = 2 ^ length xs" |
49948
744934b818c7
moved quite generic material from theory Enum to more appropriate places
haftmann
parents:
49808
diff
changeset
|
4606 |
by (induct xs) (simp_all add: Let_def) |
744934b818c7
moved quite generic material from theory Enum to more appropriate places
haftmann
parents:
49808
diff
changeset
|
4607 |
|
65956
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65350
diff
changeset
|
4608 |
lemma subseqs_powset: "set ` set (subseqs xs) = Pow (set xs)" |
49948
744934b818c7
moved quite generic material from theory Enum to more appropriate places
haftmann
parents:
49808
diff
changeset
|
4609 |
proof - |
744934b818c7
moved quite generic material from theory Enum to more appropriate places
haftmann
parents:
49808
diff
changeset
|
4610 |
have aux: "\<And>x A. set ` Cons x ` A = insert x ` set ` A" |
744934b818c7
moved quite generic material from theory Enum to more appropriate places
haftmann
parents:
49808
diff
changeset
|
4611 |
by (auto simp add: image_def) |
65956
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65350
diff
changeset
|
4612 |
have "set (map set (subseqs xs)) = Pow (set xs)" |
64963 | 4613 |
by (induct xs) (simp_all add: aux Let_def Pow_insert Un_commute comp_def del: map_map) |
49948
744934b818c7
moved quite generic material from theory Enum to more appropriate places
haftmann
parents:
49808
diff
changeset
|
4614 |
then show ?thesis by simp |
744934b818c7
moved quite generic material from theory Enum to more appropriate places
haftmann
parents:
49808
diff
changeset
|
4615 |
qed |
744934b818c7
moved quite generic material from theory Enum to more appropriate places
haftmann
parents:
49808
diff
changeset
|
4616 |
|
65956
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65350
diff
changeset
|
4617 |
lemma distinct_set_subseqs: |
49948
744934b818c7
moved quite generic material from theory Enum to more appropriate places
haftmann
parents:
49808
diff
changeset
|
4618 |
assumes "distinct xs" |
65956
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65350
diff
changeset
|
4619 |
shows "distinct (map set (subseqs xs))" |
49948
744934b818c7
moved quite generic material from theory Enum to more appropriate places
haftmann
parents:
49808
diff
changeset
|
4620 |
proof (rule card_distinct) |
64963 | 4621 |
have "finite (set xs)" .. |
4622 |
then have "card (Pow (set xs)) = 2 ^ card (set xs)" |
|
4623 |
by (rule card_Pow) |
|
4624 |
with assms distinct_card [of xs] have "card (Pow (set xs)) = 2 ^ length xs" |
|
4625 |
by simp |
|
65956
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65350
diff
changeset
|
4626 |
then show "card (set (map set (subseqs xs))) = length (map set (subseqs xs))" |
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65350
diff
changeset
|
4627 |
by (simp add: subseqs_powset length_subseqs) |
49948
744934b818c7
moved quite generic material from theory Enum to more appropriate places
haftmann
parents:
49808
diff
changeset
|
4628 |
qed |
744934b818c7
moved quite generic material from theory Enum to more appropriate places
haftmann
parents:
49808
diff
changeset
|
4629 |
|
744934b818c7
moved quite generic material from theory Enum to more appropriate places
haftmann
parents:
49808
diff
changeset
|
4630 |
lemma n_lists_Nil [simp]: "List.n_lists n [] = (if n = 0 then [[]] else [])" |
744934b818c7
moved quite generic material from theory Enum to more appropriate places
haftmann
parents:
49808
diff
changeset
|
4631 |
by (induct n) simp_all |
744934b818c7
moved quite generic material from theory Enum to more appropriate places
haftmann
parents:
49808
diff
changeset
|
4632 |
|
744934b818c7
moved quite generic material from theory Enum to more appropriate places
haftmann
parents:
49808
diff
changeset
|
4633 |
lemma length_n_lists_elem: "ys \<in> set (List.n_lists n xs) \<Longrightarrow> length ys = n" |
744934b818c7
moved quite generic material from theory Enum to more appropriate places
haftmann
parents:
49808
diff
changeset
|
4634 |
by (induct n arbitrary: ys) auto |
744934b818c7
moved quite generic material from theory Enum to more appropriate places
haftmann
parents:
49808
diff
changeset
|
4635 |
|
744934b818c7
moved quite generic material from theory Enum to more appropriate places
haftmann
parents:
49808
diff
changeset
|
4636 |
lemma set_n_lists: "set (List.n_lists n xs) = {ys. length ys = n \<and> set ys \<subseteq> set xs}" |
744934b818c7
moved quite generic material from theory Enum to more appropriate places
haftmann
parents:
49808
diff
changeset
|
4637 |
proof (rule set_eqI) |
744934b818c7
moved quite generic material from theory Enum to more appropriate places
haftmann
parents:
49808
diff
changeset
|
4638 |
fix ys :: "'a list" |
744934b818c7
moved quite generic material from theory Enum to more appropriate places
haftmann
parents:
49808
diff
changeset
|
4639 |
show "ys \<in> set (List.n_lists n xs) \<longleftrightarrow> ys \<in> {ys. length ys = n \<and> set ys \<subseteq> set xs}" |
744934b818c7
moved quite generic material from theory Enum to more appropriate places
haftmann
parents:
49808
diff
changeset
|
4640 |
proof - |
744934b818c7
moved quite generic material from theory Enum to more appropriate places
haftmann
parents:
49808
diff
changeset
|
4641 |
have "ys \<in> set (List.n_lists n xs) \<Longrightarrow> length ys = n" |
744934b818c7
moved quite generic material from theory Enum to more appropriate places
haftmann
parents:
49808
diff
changeset
|
4642 |
by (induct n arbitrary: ys) auto |
744934b818c7
moved quite generic material from theory Enum to more appropriate places
haftmann
parents:
49808
diff
changeset
|
4643 |
moreover have "\<And>x. ys \<in> set (List.n_lists n xs) \<Longrightarrow> x \<in> set ys \<Longrightarrow> x \<in> set xs" |
744934b818c7
moved quite generic material from theory Enum to more appropriate places
haftmann
parents:
49808
diff
changeset
|
4644 |
by (induct n arbitrary: ys) auto |
744934b818c7
moved quite generic material from theory Enum to more appropriate places
haftmann
parents:
49808
diff
changeset
|
4645 |
moreover have "set ys \<subseteq> set xs \<Longrightarrow> ys \<in> set (List.n_lists (length ys) xs)" |
744934b818c7
moved quite generic material from theory Enum to more appropriate places
haftmann
parents:
49808
diff
changeset
|
4646 |
by (induct ys) auto |
744934b818c7
moved quite generic material from theory Enum to more appropriate places
haftmann
parents:
49808
diff
changeset
|
4647 |
ultimately show ?thesis by auto |
744934b818c7
moved quite generic material from theory Enum to more appropriate places
haftmann
parents:
49808
diff
changeset
|
4648 |
qed |
744934b818c7
moved quite generic material from theory Enum to more appropriate places
haftmann
parents:
49808
diff
changeset
|
4649 |
qed |
744934b818c7
moved quite generic material from theory Enum to more appropriate places
haftmann
parents:
49808
diff
changeset
|
4650 |
|
65956
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65350
diff
changeset
|
4651 |
lemma subseqs_refl: "xs \<in> set (subseqs xs)" |
64963 | 4652 |
by (induct xs) (simp_all add: Let_def) |
63561
fba08009ff3e
add lemmas contributed by Peter Gammie
Andreas Lochbihler
parents:
63540
diff
changeset
|
4653 |
|
65956
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65350
diff
changeset
|
4654 |
lemma subset_subseqs: "X \<subseteq> set xs \<Longrightarrow> X \<in> set ` set (subseqs xs)" |
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65350
diff
changeset
|
4655 |
unfolding subseqs_powset by simp |
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65350
diff
changeset
|
4656 |
|
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65350
diff
changeset
|
4657 |
lemma Cons_in_subseqsD: "y # ys \<in> set (subseqs xs) \<Longrightarrow> ys \<in> set (subseqs xs)" |
64963 | 4658 |
by (induct xs) (auto simp: Let_def) |
63561
fba08009ff3e
add lemmas contributed by Peter Gammie
Andreas Lochbihler
parents:
63540
diff
changeset
|
4659 |
|
65956
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65350
diff
changeset
|
4660 |
lemma subseqs_distinctD: "\<lbrakk> ys \<in> set (subseqs xs); distinct xs \<rbrakk> \<Longrightarrow> distinct ys" |
64963 | 4661 |
proof (induct xs arbitrary: ys) |
63561
fba08009ff3e
add lemmas contributed by Peter Gammie
Andreas Lochbihler
parents:
63540
diff
changeset
|
4662 |
case (Cons x xs ys) |
fba08009ff3e
add lemmas contributed by Peter Gammie
Andreas Lochbihler
parents:
63540
diff
changeset
|
4663 |
then show ?case |
65956
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65350
diff
changeset
|
4664 |
by (auto simp: Let_def) (metis Pow_iff contra_subsetD image_eqI subseqs_powset) |
63561
fba08009ff3e
add lemmas contributed by Peter Gammie
Andreas Lochbihler
parents:
63540
diff
changeset
|
4665 |
qed simp |
fba08009ff3e
add lemmas contributed by Peter Gammie
Andreas Lochbihler
parents:
63540
diff
changeset
|
4666 |
|
49948
744934b818c7
moved quite generic material from theory Enum to more appropriate places
haftmann
parents:
49808
diff
changeset
|
4667 |
|
69593 | 4668 |
subsubsection \<open>\<^const>\<open>splice\<close>\<close> |
19390 | 4669 |
|
69075 | 4670 |
lemma splice_Nil2 [simp]: "splice xs [] = xs" |
19390 | 4671 |
by (cases xs) simp_all |
4672 |
||
24526 | 4673 |
lemma length_splice[simp]: "length(splice xs ys) = length xs + length ys" |
69136 | 4674 |
by (induct xs ys rule: splice.induct) auto |
4675 |
||
4676 |
lemma split_Nil_iff[simp]: "splice xs ys = [] \<longleftrightarrow> xs = [] \<and> ys = []" |
|
4677 |
by (induct xs ys rule: splice.induct) auto |
|
65350
b149abe619f7
added shuffle product to HOL/List
eberlm <eberlm@in.tum.de>
parents:
64966
diff
changeset
|
4678 |
|
69140 | 4679 |
lemma splice_replicate[simp]: "splice (replicate m x) (replicate n x) = replicate (m+n) x" |
4680 |
apply(induction "replicate m x" "replicate n x" arbitrary: m n rule: splice.induct) |
|
4681 |
apply (auto simp add: Cons_replicate_eq dest: gr0_implies_Suc) |
|
4682 |
done |
|
65350
b149abe619f7
added shuffle product to HOL/List
eberlm <eberlm@in.tum.de>
parents:
64966
diff
changeset
|
4683 |
|
69593 | 4684 |
subsubsection \<open>\<^const>\<open>shuffles\<close>\<close> |
69107 | 4685 |
|
4686 |
lemma shuffles_commutes: "shuffles xs ys = shuffles ys xs" |
|
4687 |
by (induction xs ys rule: shuffles.induct) (simp_all add: Un_commute) |
|
4688 |
||
4689 |
lemma Nil_in_shuffles[simp]: "[] \<in> shuffles xs ys \<longleftrightarrow> xs = [] \<and> ys = []" |
|
4690 |
by (induct xs ys rule: shuffles.induct) auto |
|
4691 |
||
4692 |
lemma shufflesE: |
|
4693 |
"zs \<in> shuffles xs ys \<Longrightarrow> |
|
65350
b149abe619f7
added shuffle product to HOL/List
eberlm <eberlm@in.tum.de>
parents:
64966
diff
changeset
|
4694 |
(zs = xs \<Longrightarrow> ys = [] \<Longrightarrow> P) \<Longrightarrow> |
b149abe619f7
added shuffle product to HOL/List
eberlm <eberlm@in.tum.de>
parents:
64966
diff
changeset
|
4695 |
(zs = ys \<Longrightarrow> xs = [] \<Longrightarrow> P) \<Longrightarrow> |
69107 | 4696 |
(\<And>x xs' z zs'. xs = x # xs' \<Longrightarrow> zs = z # zs' \<Longrightarrow> x = z \<Longrightarrow> zs' \<in> shuffles xs' ys \<Longrightarrow> P) \<Longrightarrow> |
4697 |
(\<And>y ys' z zs'. ys = y # ys' \<Longrightarrow> zs = z # zs' \<Longrightarrow> y = z \<Longrightarrow> zs' \<in> shuffles xs ys' \<Longrightarrow> P) \<Longrightarrow> P" |
|
4698 |
by (induct xs ys rule: shuffles.induct) auto |
|
4699 |
||
4700 |
lemma Cons_in_shuffles_iff: |
|
4701 |
"z # zs \<in> shuffles xs ys \<longleftrightarrow> |
|
4702 |
(xs \<noteq> [] \<and> hd xs = z \<and> zs \<in> shuffles (tl xs) ys \<or> |
|
4703 |
ys \<noteq> [] \<and> hd ys = z \<and> zs \<in> shuffles xs (tl ys))" |
|
4704 |
by (induct xs ys rule: shuffles.induct) auto |
|
4705 |
||
4706 |
lemma splice_in_shuffles [simp, intro]: "splice xs ys \<in> shuffles xs ys" |
|
4707 |
by (induction xs ys rule: splice.induct) (simp_all add: Cons_in_shuffles_iff shuffles_commutes) |
|
4708 |
||
4709 |
lemma Nil_in_shufflesI: "xs = [] \<Longrightarrow> ys = [] \<Longrightarrow> [] \<in> shuffles xs ys" |
|
65350
b149abe619f7
added shuffle product to HOL/List
eberlm <eberlm@in.tum.de>
parents:
64966
diff
changeset
|
4710 |
by simp |
66892 | 4711 |
|
69107 | 4712 |
lemma Cons_in_shuffles_leftI: "zs \<in> shuffles xs ys \<Longrightarrow> z # zs \<in> shuffles (z # xs) ys" |
65350
b149abe619f7
added shuffle product to HOL/List
eberlm <eberlm@in.tum.de>
parents:
64966
diff
changeset
|
4713 |
by (cases ys) auto |
b149abe619f7
added shuffle product to HOL/List
eberlm <eberlm@in.tum.de>
parents:
64966
diff
changeset
|
4714 |
|
69107 | 4715 |
lemma Cons_in_shuffles_rightI: "zs \<in> shuffles xs ys \<Longrightarrow> z # zs \<in> shuffles xs (z # ys)" |
65350
b149abe619f7
added shuffle product to HOL/List
eberlm <eberlm@in.tum.de>
parents:
64966
diff
changeset
|
4716 |
by (cases xs) auto |
b149abe619f7
added shuffle product to HOL/List
eberlm <eberlm@in.tum.de>
parents:
64966
diff
changeset
|
4717 |
|
69107 | 4718 |
lemma finite_shuffles [simp, intro]: "finite (shuffles xs ys)" |
4719 |
by (induction xs ys rule: shuffles.induct) simp_all |
|
4720 |
||
4721 |
lemma length_shuffles: "zs \<in> shuffles xs ys \<Longrightarrow> length zs = length xs + length ys" |
|
4722 |
by (induction xs ys arbitrary: zs rule: shuffles.induct) auto |
|
4723 |
||
4724 |
lemma set_shuffles: "zs \<in> shuffles xs ys \<Longrightarrow> set zs = set xs \<union> set ys" |
|
4725 |
by (induction xs ys arbitrary: zs rule: shuffles.induct) auto |
|
4726 |
||
4727 |
lemma distinct_disjoint_shuffles: |
|
4728 |
assumes "distinct xs" "distinct ys" "set xs \<inter> set ys = {}" "zs \<in> shuffles xs ys" |
|
65350
b149abe619f7
added shuffle product to HOL/List
eberlm <eberlm@in.tum.de>
parents:
64966
diff
changeset
|
4729 |
shows "distinct zs" |
b149abe619f7
added shuffle product to HOL/List
eberlm <eberlm@in.tum.de>
parents:
64966
diff
changeset
|
4730 |
using assms |
69107 | 4731 |
proof (induction xs ys arbitrary: zs rule: shuffles.induct) |
65350
b149abe619f7
added shuffle product to HOL/List
eberlm <eberlm@in.tum.de>
parents:
64966
diff
changeset
|
4732 |
case (3 x xs y ys) |
b149abe619f7
added shuffle product to HOL/List
eberlm <eberlm@in.tum.de>
parents:
64966
diff
changeset
|
4733 |
show ?case |
b149abe619f7
added shuffle product to HOL/List
eberlm <eberlm@in.tum.de>
parents:
64966
diff
changeset
|
4734 |
proof (cases zs) |
b149abe619f7
added shuffle product to HOL/List
eberlm <eberlm@in.tum.de>
parents:
64966
diff
changeset
|
4735 |
case (Cons z zs') |
69107 | 4736 |
with "3.prems" and "3.IH"[of zs'] show ?thesis by (force dest: set_shuffles) |
65350
b149abe619f7
added shuffle product to HOL/List
eberlm <eberlm@in.tum.de>
parents:
64966
diff
changeset
|
4737 |
qed simp_all |
b149abe619f7
added shuffle product to HOL/List
eberlm <eberlm@in.tum.de>
parents:
64966
diff
changeset
|
4738 |
qed simp_all |
b149abe619f7
added shuffle product to HOL/List
eberlm <eberlm@in.tum.de>
parents:
64966
diff
changeset
|
4739 |
|
69107 | 4740 |
lemma Cons_shuffles_subset1: "(#) x ` shuffles xs ys \<subseteq> shuffles (x # xs) ys" |
65350
b149abe619f7
added shuffle product to HOL/List
eberlm <eberlm@in.tum.de>
parents:
64966
diff
changeset
|
4741 |
by (cases ys) auto |
66892 | 4742 |
|
69107 | 4743 |
lemma Cons_shuffles_subset2: "(#) y ` shuffles xs ys \<subseteq> shuffles xs (y # ys)" |
65350
b149abe619f7
added shuffle product to HOL/List
eberlm <eberlm@in.tum.de>
parents:
64966
diff
changeset
|
4744 |
by (cases xs) auto |
b149abe619f7
added shuffle product to HOL/List
eberlm <eberlm@in.tum.de>
parents:
64966
diff
changeset
|
4745 |
|
69107 | 4746 |
lemma filter_shuffles: |
4747 |
"filter P ` shuffles xs ys = shuffles (filter P xs) (filter P ys)" |
|
65350
b149abe619f7
added shuffle product to HOL/List
eberlm <eberlm@in.tum.de>
parents:
64966
diff
changeset
|
4748 |
proof - |
67399 | 4749 |
have *: "filter P ` (#) x ` A = (if P x then (#) x ` filter P ` A else filter P ` A)" for x A |
65350
b149abe619f7
added shuffle product to HOL/List
eberlm <eberlm@in.tum.de>
parents:
64966
diff
changeset
|
4750 |
by (auto simp: image_image) |
b149abe619f7
added shuffle product to HOL/List
eberlm <eberlm@in.tum.de>
parents:
64966
diff
changeset
|
4751 |
show ?thesis |
69107 | 4752 |
by (induction xs ys rule: shuffles.induct) |
66892 | 4753 |
(simp_all split: if_splits add: image_Un * Un_absorb1 Un_absorb2 |
69107 | 4754 |
Cons_shuffles_subset1 Cons_shuffles_subset2) |
65350
b149abe619f7
added shuffle product to HOL/List
eberlm <eberlm@in.tum.de>
parents:
64966
diff
changeset
|
4755 |
qed |
b149abe619f7
added shuffle product to HOL/List
eberlm <eberlm@in.tum.de>
parents:
64966
diff
changeset
|
4756 |
|
69107 | 4757 |
lemma filter_shuffles_disjoint1: |
4758 |
assumes "set xs \<inter> set ys = {}" "zs \<in> shuffles xs ys" |
|
65350
b149abe619f7
added shuffle product to HOL/List
eberlm <eberlm@in.tum.de>
parents:
64966
diff
changeset
|
4759 |
shows "filter (\<lambda>x. x \<in> set xs) zs = xs" (is "filter ?P _ = _") |
b149abe619f7
added shuffle product to HOL/List
eberlm <eberlm@in.tum.de>
parents:
64966
diff
changeset
|
4760 |
and "filter (\<lambda>x. x \<notin> set xs) zs = ys" (is "filter ?Q _ = _") |
b149abe619f7
added shuffle product to HOL/List
eberlm <eberlm@in.tum.de>
parents:
64966
diff
changeset
|
4761 |
using assms |
b149abe619f7
added shuffle product to HOL/List
eberlm <eberlm@in.tum.de>
parents:
64966
diff
changeset
|
4762 |
proof - |
69107 | 4763 |
from assms have "filter ?P zs \<in> filter ?P ` shuffles xs ys" by blast |
4764 |
also have "filter ?P ` shuffles xs ys = shuffles (filter ?P xs) (filter ?P ys)" |
|
4765 |
by (rule filter_shuffles) |
|
65350
b149abe619f7
added shuffle product to HOL/List
eberlm <eberlm@in.tum.de>
parents:
64966
diff
changeset
|
4766 |
also have "filter ?P xs = xs" by (rule filter_True) simp_all |
b149abe619f7
added shuffle product to HOL/List
eberlm <eberlm@in.tum.de>
parents:
64966
diff
changeset
|
4767 |
also have "filter ?P ys = []" by (rule filter_False) (insert assms(1), auto) |
69107 | 4768 |
also have "shuffles xs [] = {xs}" by simp |
65350
b149abe619f7
added shuffle product to HOL/List
eberlm <eberlm@in.tum.de>
parents:
64966
diff
changeset
|
4769 |
finally show "filter ?P zs = xs" by simp |
b149abe619f7
added shuffle product to HOL/List
eberlm <eberlm@in.tum.de>
parents:
64966
diff
changeset
|
4770 |
next |
69107 | 4771 |
from assms have "filter ?Q zs \<in> filter ?Q ` shuffles xs ys" by blast |
4772 |
also have "filter ?Q ` shuffles xs ys = shuffles (filter ?Q xs) (filter ?Q ys)" |
|
4773 |
by (rule filter_shuffles) |
|
65350
b149abe619f7
added shuffle product to HOL/List
eberlm <eberlm@in.tum.de>
parents:
64966
diff
changeset
|
4774 |
also have "filter ?Q ys = ys" by (rule filter_True) (insert assms(1), auto) |
b149abe619f7
added shuffle product to HOL/List
eberlm <eberlm@in.tum.de>
parents:
64966
diff
changeset
|
4775 |
also have "filter ?Q xs = []" by (rule filter_False) (insert assms(1), auto) |
69107 | 4776 |
also have "shuffles [] ys = {ys}" by simp |
65350
b149abe619f7
added shuffle product to HOL/List
eberlm <eberlm@in.tum.de>
parents:
64966
diff
changeset
|
4777 |
finally show "filter ?Q zs = ys" by simp |
b149abe619f7
added shuffle product to HOL/List
eberlm <eberlm@in.tum.de>
parents:
64966
diff
changeset
|
4778 |
qed |
b149abe619f7
added shuffle product to HOL/List
eberlm <eberlm@in.tum.de>
parents:
64966
diff
changeset
|
4779 |
|
69107 | 4780 |
lemma filter_shuffles_disjoint2: |
4781 |
assumes "set xs \<inter> set ys = {}" "zs \<in> shuffles xs ys" |
|
65350
b149abe619f7
added shuffle product to HOL/List
eberlm <eberlm@in.tum.de>
parents:
64966
diff
changeset
|
4782 |
shows "filter (\<lambda>x. x \<in> set ys) zs = ys" "filter (\<lambda>x. x \<notin> set ys) zs = xs" |
69107 | 4783 |
using filter_shuffles_disjoint1[of ys xs zs] assms |
4784 |
by (simp_all add: shuffles_commutes Int_commute) |
|
4785 |
||
4786 |
lemma partition_in_shuffles: |
|
4787 |
"xs \<in> shuffles (filter P xs) (filter (\<lambda>x. \<not>P x) xs)" |
|
65350
b149abe619f7
added shuffle product to HOL/List
eberlm <eberlm@in.tum.de>
parents:
64966
diff
changeset
|
4788 |
proof (induction xs) |
b149abe619f7
added shuffle product to HOL/List
eberlm <eberlm@in.tum.de>
parents:
64966
diff
changeset
|
4789 |
case (Cons x xs) |
b149abe619f7
added shuffle product to HOL/List
eberlm <eberlm@in.tum.de>
parents:
64966
diff
changeset
|
4790 |
show ?case |
b149abe619f7
added shuffle product to HOL/List
eberlm <eberlm@in.tum.de>
parents:
64966
diff
changeset
|
4791 |
proof (cases "P x") |
b149abe619f7
added shuffle product to HOL/List
eberlm <eberlm@in.tum.de>
parents:
64966
diff
changeset
|
4792 |
case True |
69107 | 4793 |
hence "x # xs \<in> (#) x ` shuffles (filter P xs) (filter (\<lambda>x. \<not>P x) xs)" |
65350
b149abe619f7
added shuffle product to HOL/List
eberlm <eberlm@in.tum.de>
parents:
64966
diff
changeset
|
4794 |
by (intro imageI Cons.IH) |
69107 | 4795 |
also have "\<dots> \<subseteq> shuffles (filter P (x # xs)) (filter (\<lambda>x. \<not>P x) (x # xs))" |
4796 |
by (simp add: True Cons_shuffles_subset1) |
|
65350
b149abe619f7
added shuffle product to HOL/List
eberlm <eberlm@in.tum.de>
parents:
64966
diff
changeset
|
4797 |
finally show ?thesis . |
b149abe619f7
added shuffle product to HOL/List
eberlm <eberlm@in.tum.de>
parents:
64966
diff
changeset
|
4798 |
next |
b149abe619f7
added shuffle product to HOL/List
eberlm <eberlm@in.tum.de>
parents:
64966
diff
changeset
|
4799 |
case False |
69107 | 4800 |
hence "x # xs \<in> (#) x ` shuffles (filter P xs) (filter (\<lambda>x. \<not>P x) xs)" |
65350
b149abe619f7
added shuffle product to HOL/List
eberlm <eberlm@in.tum.de>
parents:
64966
diff
changeset
|
4801 |
by (intro imageI Cons.IH) |
69107 | 4802 |
also have "\<dots> \<subseteq> shuffles (filter P (x # xs)) (filter (\<lambda>x. \<not>P x) (x # xs))" |
4803 |
by (simp add: False Cons_shuffles_subset2) |
|
65350
b149abe619f7
added shuffle product to HOL/List
eberlm <eberlm@in.tum.de>
parents:
64966
diff
changeset
|
4804 |
finally show ?thesis . |
b149abe619f7
added shuffle product to HOL/List
eberlm <eberlm@in.tum.de>
parents:
64966
diff
changeset
|
4805 |
qed |
b149abe619f7
added shuffle product to HOL/List
eberlm <eberlm@in.tum.de>
parents:
64966
diff
changeset
|
4806 |
qed auto |
b149abe619f7
added shuffle product to HOL/List
eberlm <eberlm@in.tum.de>
parents:
64966
diff
changeset
|
4807 |
|
b149abe619f7
added shuffle product to HOL/List
eberlm <eberlm@in.tum.de>
parents:
64966
diff
changeset
|
4808 |
lemma inv_image_partition: |
b149abe619f7
added shuffle product to HOL/List
eberlm <eberlm@in.tum.de>
parents:
64966
diff
changeset
|
4809 |
assumes "\<And>x. x \<in> set xs \<Longrightarrow> P x" "\<And>y. y \<in> set ys \<Longrightarrow> \<not>P y" |
69107 | 4810 |
shows "partition P -` {(xs, ys)} = shuffles xs ys" |
65350
b149abe619f7
added shuffle product to HOL/List
eberlm <eberlm@in.tum.de>
parents:
64966
diff
changeset
|
4811 |
proof (intro equalityI subsetI) |
69107 | 4812 |
fix zs assume zs: "zs \<in> shuffles xs ys" |
4813 |
hence [simp]: "set zs = set xs \<union> set ys" by (rule set_shuffles) |
|
66892 | 4814 |
from assms have "filter P zs = filter (\<lambda>x. x \<in> set xs) zs" |
65350
b149abe619f7
added shuffle product to HOL/List
eberlm <eberlm@in.tum.de>
parents:
64966
diff
changeset
|
4815 |
"filter (\<lambda>x. \<not>P x) zs = filter (\<lambda>x. x \<in> set ys) zs" |
b149abe619f7
added shuffle product to HOL/List
eberlm <eberlm@in.tum.de>
parents:
64966
diff
changeset
|
4816 |
by (intro filter_cong refl; force)+ |
b149abe619f7
added shuffle product to HOL/List
eberlm <eberlm@in.tum.de>
parents:
64966
diff
changeset
|
4817 |
moreover from assms have "set xs \<inter> set ys = {}" by auto |
b149abe619f7
added shuffle product to HOL/List
eberlm <eberlm@in.tum.de>
parents:
64966
diff
changeset
|
4818 |
ultimately show "zs \<in> partition P -` {(xs, ys)}" using zs |
69107 | 4819 |
by (simp add: o_def filter_shuffles_disjoint1 filter_shuffles_disjoint2) |
65350
b149abe619f7
added shuffle product to HOL/List
eberlm <eberlm@in.tum.de>
parents:
64966
diff
changeset
|
4820 |
next |
b149abe619f7
added shuffle product to HOL/List
eberlm <eberlm@in.tum.de>
parents:
64966
diff
changeset
|
4821 |
fix zs assume "zs \<in> partition P -` {(xs, ys)}" |
69107 | 4822 |
thus "zs \<in> shuffles xs ys" using partition_in_shuffles[of zs] by (auto simp: o_def) |
65350
b149abe619f7
added shuffle product to HOL/List
eberlm <eberlm@in.tum.de>
parents:
64966
diff
changeset
|
4823 |
qed |
22793 | 4824 |
|
35115 | 4825 |
|
60758 | 4826 |
subsubsection \<open>Transpose\<close> |
34933 | 4827 |
|
4828 |
function transpose where |
|
4829 |
"transpose [] = []" | |
|
4830 |
"transpose ([] # xss) = transpose xss" | |
|
4831 |
"transpose ((x#xs) # xss) = |
|
4832 |
(x # [h. (h#t) \<leftarrow> xss]) # transpose (xs # [t. (h#t) \<leftarrow> xss])" |
|
4833 |
by pat_completeness auto |
|
4834 |
||
4835 |
lemma transpose_aux_filter_head: |
|
55404
5cb95b79a51f
transformed 'option' and 'list' into new-style datatypes (but register them as old-style as well)
blanchet
parents:
55148
diff
changeset
|
4836 |
"concat (map (case_list [] (\<lambda>h t. [h])) xss) = |
68249
949d93804740
First step to remove nonstandard "[x <- xs. P]" syntax: only input
nipkow
parents:
68215
diff
changeset
|
4837 |
map (\<lambda>xs. hd xs) (filter (\<lambda>ys. ys \<noteq> []) xss)" |
34933 | 4838 |
by (induct xss) (auto split: list.split) |
4839 |
||
4840 |
lemma transpose_aux_filter_tail: |
|
55404
5cb95b79a51f
transformed 'option' and 'list' into new-style datatypes (but register them as old-style as well)
blanchet
parents:
55148
diff
changeset
|
4841 |
"concat (map (case_list [] (\<lambda>h t. [t])) xss) = |
68249
949d93804740
First step to remove nonstandard "[x <- xs. P]" syntax: only input
nipkow
parents:
68215
diff
changeset
|
4842 |
map (\<lambda>xs. tl xs) (filter (\<lambda>ys. ys \<noteq> []) xss)" |
34933 | 4843 |
by (induct xss) (auto split: list.split) |
4844 |
||
4845 |
lemma transpose_aux_max: |
|
4846 |
"max (Suc (length xs)) (foldr (\<lambda>xs. max (length xs)) xss 0) = |
|
68249
949d93804740
First step to remove nonstandard "[x <- xs. P]" syntax: only input
nipkow
parents:
68215
diff
changeset
|
4847 |
Suc (max (length xs) (foldr (\<lambda>x. max (length x - Suc 0)) (filter (\<lambda>ys. ys \<noteq> []) xss) 0))" |
34933 | 4848 |
(is "max _ ?foldB = Suc (max _ ?foldA)") |
68249
949d93804740
First step to remove nonstandard "[x <- xs. P]" syntax: only input
nipkow
parents:
68215
diff
changeset
|
4849 |
proof (cases "(filter (\<lambda>ys. ys \<noteq> []) xss) = []") |
34933 | 4850 |
case True |
4851 |
hence "foldr (\<lambda>xs. max (length xs)) xss 0 = 0" |
|
4852 |
proof (induct xss) |
|
4853 |
case (Cons x xs) |
|
53374
a14d2a854c02
tuned proofs -- clarified flow of facts wrt. calculation;
wenzelm
parents:
53017
diff
changeset
|
4854 |
then have "x = []" by (cases x) auto |
a14d2a854c02
tuned proofs -- clarified flow of facts wrt. calculation;
wenzelm
parents:
53017
diff
changeset
|
4855 |
with Cons show ?case by auto |
34933 | 4856 |
qed simp |
4857 |
thus ?thesis using True by simp |
|
4858 |
next |
|
4859 |
case False |
|
4860 |
||
68249
949d93804740
First step to remove nonstandard "[x <- xs. P]" syntax: only input
nipkow
parents:
68215
diff
changeset
|
4861 |
have foldA: "?foldA = foldr (\<lambda>x. max (length x)) (filter (\<lambda>ys. ys \<noteq> []) xss) 0 - 1" |
34933 | 4862 |
by (induct xss) auto |
68249
949d93804740
First step to remove nonstandard "[x <- xs. P]" syntax: only input
nipkow
parents:
68215
diff
changeset
|
4863 |
have foldB: "?foldB = foldr (\<lambda>x. max (length x)) (filter (\<lambda>ys. ys \<noteq> []) xss) 0" |
34933 | 4864 |
by (induct xss) auto |
4865 |
||
4866 |
have "0 < ?foldB" |
|
4867 |
proof - |
|
4868 |
from False |
|
68249
949d93804740
First step to remove nonstandard "[x <- xs. P]" syntax: only input
nipkow
parents:
68215
diff
changeset
|
4869 |
obtain z zs where zs: "(filter (\<lambda>ys. ys \<noteq> []) xss) = z#zs" by (auto simp: neq_Nil_conv) |
949d93804740
First step to remove nonstandard "[x <- xs. P]" syntax: only input
nipkow
parents:
68215
diff
changeset
|
4870 |
hence "z \<in> set (filter (\<lambda>ys. ys \<noteq> []) xss)" by auto |
34933 | 4871 |
hence "z \<noteq> []" by auto |
4872 |
thus ?thesis |
|
4873 |
unfolding foldB zs |
|
4874 |
by (auto simp: max_def intro: less_le_trans) |
|
4875 |
qed |
|
4876 |
thus ?thesis |
|
4877 |
unfolding foldA foldB max_Suc_Suc[symmetric] |
|
4878 |
by simp |
|
4879 |
qed |
|
4880 |
||
4881 |
termination transpose |
|
4882 |
by (relation "measure (\<lambda>xs. foldr (\<lambda>xs. max (length xs)) xs 0 + length xs)") |
|
4883 |
(auto simp: transpose_aux_filter_tail foldr_map comp_def transpose_aux_max less_Suc_eq_le) |
|
4884 |
||
4885 |
lemma transpose_empty: "(transpose xs = []) \<longleftrightarrow> (\<forall>x \<in> set xs. x = [])" |
|
4886 |
by (induct rule: transpose.induct) simp_all |
|
4887 |
||
4888 |
lemma length_transpose: |
|
4889 |
fixes xs :: "'a list list" |
|
4890 |
shows "length (transpose xs) = foldr (\<lambda>xs. max (length xs)) xs 0" |
|
4891 |
by (induct rule: transpose.induct) |
|
4892 |
(auto simp: transpose_aux_filter_tail foldr_map comp_def transpose_aux_max |
|
4893 |
max_Suc_Suc[symmetric] simp del: max_Suc_Suc) |
|
4894 |
||
4895 |
lemma nth_transpose: |
|
4896 |
fixes xs :: "'a list list" |
|
4897 |
assumes "i < length (transpose xs)" |
|
68249
949d93804740
First step to remove nonstandard "[x <- xs. P]" syntax: only input
nipkow
parents:
68215
diff
changeset
|
4898 |
shows "transpose xs ! i = map (\<lambda>xs. xs ! i) (filter (\<lambda>ys. i < length ys) xs)" |
34933 | 4899 |
using assms proof (induct arbitrary: i rule: transpose.induct) |
4900 |
case (3 x xs xss) |
|
63040 | 4901 |
define XS where "XS = (x # xs) # xss" |
34933 | 4902 |
hence [simp]: "XS \<noteq> []" by auto |
4903 |
thus ?case |
|
4904 |
proof (cases i) |
|
4905 |
case 0 |
|
4906 |
thus ?thesis by (simp add: transpose_aux_filter_head hd_conv_nth) |
|
4907 |
next |
|
4908 |
case (Suc j) |
|
4909 |
have *: "\<And>xss. xs # map tl xss = map tl ((x#xs)#xss)" by simp |
|
4910 |
have **: "\<And>xss. (x#xs) # filter (\<lambda>ys. ys \<noteq> []) xss = filter (\<lambda>ys. ys \<noteq> []) ((x#xs)#xss)" by simp |
|
4911 |
{ fix x have "Suc j < length x \<longleftrightarrow> x \<noteq> [] \<and> j < length x - Suc 0" |
|
4912 |
by (cases x) simp_all |
|
4913 |
} note *** = this |
|
4914 |
||
55404
5cb95b79a51f
transformed 'option' and 'list' into new-style datatypes (but register them as old-style as well)
blanchet
parents:
55148
diff
changeset
|
4915 |
have j_less: "j < length (transpose (xs # concat (map (case_list [] (\<lambda>h t. [t])) xss)))" |
34933 | 4916 |
using "3.prems" by (simp add: transpose_aux_filter_tail length_transpose Suc) |
4917 |
||
4918 |
show ?thesis |
|
60758 | 4919 |
unfolding transpose.simps \<open>i = Suc j\<close> nth_Cons_Suc "3.hyps"[OF j_less] |
34933 | 4920 |
apply (auto simp: transpose_aux_filter_tail filter_map comp_def length_transpose * ** *** XS_def[symmetric]) |
68719 | 4921 |
by (simp add: nth_tl) |
34933 | 4922 |
qed |
4923 |
qed simp_all |
|
4924 |
||
4925 |
lemma transpose_map_map: |
|
4926 |
"transpose (map (map f) xs) = map (map f) (transpose xs)" |
|
68975
5ce4d117cea7
A few new results, elimination of duplicates and more use of "pairwise"
paulson <lp15@cam.ac.uk>
parents:
68781
diff
changeset
|
4927 |
proof (rule nth_equalityI) |
34933 | 4928 |
have [simp]: "length (transpose (map (map f) xs)) = length (transpose xs)" |
4929 |
by (simp add: length_transpose foldr_map comp_def) |
|
4930 |
show "length (transpose (map (map f) xs)) = length (map (map f) (transpose xs))" by simp |
|
4931 |
||
4932 |
fix i assume "i < length (transpose (map (map f) xs))" |
|
4933 |
thus "transpose (map (map f) xs) ! i = map (map f) (transpose xs) ! i" |
|
4934 |
by (simp add: nth_transpose filter_map comp_def) |
|
4935 |
qed |
|
24616 | 4936 |
|
69593 | 4937 |
subsubsection \<open>\<^const>\<open>min\<close> and \<^const>\<open>arg_min\<close>\<close> |
67170 | 4938 |
|
4939 |
lemma min_list_Min: "xs \<noteq> [] \<Longrightarrow> min_list xs = Min (set xs)" |
|
4940 |
by (induction xs rule: induct_list012)(auto) |
|
4941 |
||
4942 |
lemma f_arg_min_list_f: "xs \<noteq> [] \<Longrightarrow> f (arg_min_list f xs) = Min (f ` (set xs))" |
|
4943 |
by(induction f xs rule: arg_min_list.induct) (auto simp: min_def intro!: antisym) |
|
4944 |
||
67171 | 4945 |
lemma arg_min_list_in: "xs \<noteq> [] \<Longrightarrow> arg_min_list f xs \<in> set xs" |
4946 |
by(induction xs rule: induct_list012) (auto simp: Let_def) |
|
4947 |
||
35115 | 4948 |
|
60758 | 4949 |
subsubsection \<open>(In)finiteness\<close> |
28642 | 4950 |
|
4951 |
lemma finite_maxlen: |
|
67091 | 4952 |
"finite (M::'a list set) \<Longrightarrow> \<exists>n. \<forall>s\<in>M. size s < n" |
28642 | 4953 |
proof (induct rule: finite.induct) |
4954 |
case emptyI show ?case by simp |
|
4955 |
next |
|
4956 |
case (insertI M xs) |
|
4957 |
then obtain n where "\<forall>s\<in>M. length s < n" by blast |
|
67613 | 4958 |
hence "\<forall>s\<in>insert xs M. size s < max n (size xs) + 1" by auto |
28642 | 4959 |
thus ?case .. |
4960 |
qed |
|
4961 |
||
45714 | 4962 |
lemma lists_length_Suc_eq: |
4963 |
"{xs. set xs \<subseteq> A \<and> length xs = Suc n} = |
|
4964 |
(\<lambda>(xs, n). n#xs) ` ({xs. set xs \<subseteq> A \<and> length xs = n} \<times> A)" |
|
4965 |
by (auto simp: length_Suc_conv) |
|
4966 |
||
4967 |
lemma |
|
4968 |
assumes "finite A" |
|
4969 |
shows finite_lists_length_eq: "finite {xs. set xs \<subseteq> A \<and> length xs = n}" |
|
4970 |
and card_lists_length_eq: "card {xs. set xs \<subseteq> A \<and> length xs = n} = (card A)^n" |
|
60758 | 4971 |
using \<open>finite A\<close> |
45714 | 4972 |
by (induct n) |
4973 |
(auto simp: card_image inj_split_Cons lists_length_Suc_eq cong: conj_cong) |
|
31557 | 4974 |
|
4975 |
lemma finite_lists_length_le: |
|
4976 |
assumes "finite A" shows "finite {xs. set xs \<subseteq> A \<and> length xs \<le> n}" |
|
4977 |
(is "finite ?S") |
|
4978 |
proof- |
|
4979 |
have "?S = (\<Union>n\<in>{0..n}. {xs. set xs \<subseteq> A \<and> length xs = n})" by auto |
|
60758 | 4980 |
thus ?thesis by (auto intro!: finite_lists_length_eq[OF \<open>finite A\<close>] simp only:) |
31557 | 4981 |
qed |
4982 |
||
45714 | 4983 |
lemma card_lists_length_le: |
4984 |
assumes "finite A" shows "card {xs. set xs \<subseteq> A \<and> length xs \<le> n} = (\<Sum>i\<le>n. card A^i)" |
|
4985 |
proof - |
|
4986 |
have "(\<Sum>i\<le>n. card A^i) = card (\<Union>i\<le>n. {xs. set xs \<subseteq> A \<and> length xs = i})" |
|
60758 | 4987 |
using \<open>finite A\<close> |
45714 | 4988 |
by (subst card_UN_disjoint) |
4989 |
(auto simp add: card_lists_length_eq finite_lists_length_eq) |
|
4990 |
also have "(\<Union>i\<le>n. {xs. set xs \<subseteq> A \<and> length xs = i}) = {xs. set xs \<subseteq> A \<and> length xs \<le> n}" |
|
4991 |
by auto |
|
4992 |
finally show ?thesis by simp |
|
4993 |
qed |
|
4994 |
||
67478
14d3163588ae
add lemma on lists from Falling_Factorial_Sum entry
bulwahn
parents:
67443
diff
changeset
|
4995 |
lemma finite_lists_distinct_length_eq [intro]: |
14d3163588ae
add lemma on lists from Falling_Factorial_Sum entry
bulwahn
parents:
67443
diff
changeset
|
4996 |
assumes "finite A" |
14d3163588ae
add lemma on lists from Falling_Factorial_Sum entry
bulwahn
parents:
67443
diff
changeset
|
4997 |
shows "finite {xs. length xs = n \<and> distinct xs \<and> set xs \<subseteq> A}" (is "finite ?S") |
14d3163588ae
add lemma on lists from Falling_Factorial_Sum entry
bulwahn
parents:
67443
diff
changeset
|
4998 |
proof - |
14d3163588ae
add lemma on lists from Falling_Factorial_Sum entry
bulwahn
parents:
67443
diff
changeset
|
4999 |
have "finite {xs. set xs \<subseteq> A \<and> length xs = n}" |
14d3163588ae
add lemma on lists from Falling_Factorial_Sum entry
bulwahn
parents:
67443
diff
changeset
|
5000 |
using \<open>finite A\<close> by (rule finite_lists_length_eq) |
14d3163588ae
add lemma on lists from Falling_Factorial_Sum entry
bulwahn
parents:
67443
diff
changeset
|
5001 |
moreover have "?S \<subseteq> {xs. set xs \<subseteq> A \<and> length xs = n}" by auto |
14d3163588ae
add lemma on lists from Falling_Factorial_Sum entry
bulwahn
parents:
67443
diff
changeset
|
5002 |
ultimately show ?thesis using finite_subset by auto |
14d3163588ae
add lemma on lists from Falling_Factorial_Sum entry
bulwahn
parents:
67443
diff
changeset
|
5003 |
qed |
14d3163588ae
add lemma on lists from Falling_Factorial_Sum entry
bulwahn
parents:
67443
diff
changeset
|
5004 |
|
45932 | 5005 |
lemma card_lists_distinct_length_eq: |
66358
fab9a53158f8
slightly generalized card_lists_distinct_length_eq; renamed specialized card_lists_distinct_length_eq to card_lists_distinct_length_eq'; tuned
bulwahn
parents:
66257
diff
changeset
|
5006 |
assumes "finite A" "k \<le> card A" |
45932 | 5007 |
shows "card {xs. length xs = k \<and> distinct xs \<and> set xs \<subseteq> A} = \<Prod>{card A - k + 1 .. card A}" |
5008 |
using assms |
|
5009 |
proof (induct k) |
|
5010 |
case 0 |
|
5011 |
then have "{xs. length xs = 0 \<and> distinct xs \<and> set xs \<subseteq> A} = {[]}" by auto |
|
5012 |
then show ?case by simp |
|
5013 |
next |
|
5014 |
case (Suc k) |
|
5015 |
let "?k_list" = "\<lambda>k xs. length xs = k \<and> distinct xs \<and> set xs \<subseteq> A" |
|
5016 |
have inj_Cons: "\<And>A. inj_on (\<lambda>(xs, n). n # xs) A" by (rule inj_onI) auto |
|
5017 |
||
66358
fab9a53158f8
slightly generalized card_lists_distinct_length_eq; renamed specialized card_lists_distinct_length_eq to card_lists_distinct_length_eq'; tuned
bulwahn
parents:
66257
diff
changeset
|
5018 |
from Suc have "k \<le> card A" by simp |
fab9a53158f8
slightly generalized card_lists_distinct_length_eq; renamed specialized card_lists_distinct_length_eq to card_lists_distinct_length_eq'; tuned
bulwahn
parents:
66257
diff
changeset
|
5019 |
moreover note \<open>finite A\<close> |
45932 | 5020 |
moreover have "finite {xs. ?k_list k xs}" |
63834 | 5021 |
by (rule finite_subset) (use finite_lists_length_eq[OF \<open>finite A\<close>, of k] in auto) |
45932 | 5022 |
moreover have "\<And>i j. i \<noteq> j \<longrightarrow> {i} \<times> (A - set i) \<inter> {j} \<times> (A - set j) = {}" |
5023 |
by auto |
|
66358
fab9a53158f8
slightly generalized card_lists_distinct_length_eq; renamed specialized card_lists_distinct_length_eq to card_lists_distinct_length_eq'; tuned
bulwahn
parents:
66257
diff
changeset
|
5024 |
moreover have "\<And>i. i \<in> {xs. ?k_list k xs} \<Longrightarrow> card (A - set i) = card A - k" |
45932 | 5025 |
by (simp add: card_Diff_subset distinct_card) |
5026 |
moreover have "{xs. ?k_list (Suc k) xs} = |
|
52141
eff000cab70f
weaker precendence of syntax for big intersection and union on sets
haftmann
parents:
52131
diff
changeset
|
5027 |
(\<lambda>(xs, n). n#xs) ` \<Union>((\<lambda>xs. {xs} \<times> (A - set xs)) ` {xs. ?k_list k xs})" |
45932 | 5028 |
by (auto simp: length_Suc_conv) |
66358
fab9a53158f8
slightly generalized card_lists_distinct_length_eq; renamed specialized card_lists_distinct_length_eq to card_lists_distinct_length_eq'; tuned
bulwahn
parents:
66257
diff
changeset
|
5029 |
moreover have "Suc (card A - Suc k) = card A - k" using Suc.prems by simp |
45932 | 5030 |
then have "(card A - k) * \<Prod>{Suc (card A - k)..card A} = \<Prod>{Suc (card A - Suc k)..card A}" |
64272 | 5031 |
by (subst prod.insert[symmetric]) (simp add: atLeastAtMost_insertL)+ |
45932 | 5032 |
ultimately show ?case |
5033 |
by (simp add: card_image inj_Cons card_UN_disjoint Suc.hyps algebra_simps) |
|
5034 |
qed |
|
5035 |
||
66358
fab9a53158f8
slightly generalized card_lists_distinct_length_eq; renamed specialized card_lists_distinct_length_eq to card_lists_distinct_length_eq'; tuned
bulwahn
parents:
66257
diff
changeset
|
5036 |
lemma card_lists_distinct_length_eq': |
fab9a53158f8
slightly generalized card_lists_distinct_length_eq; renamed specialized card_lists_distinct_length_eq to card_lists_distinct_length_eq'; tuned
bulwahn
parents:
66257
diff
changeset
|
5037 |
assumes "k < card A" |
fab9a53158f8
slightly generalized card_lists_distinct_length_eq; renamed specialized card_lists_distinct_length_eq to card_lists_distinct_length_eq'; tuned
bulwahn
parents:
66257
diff
changeset
|
5038 |
shows "card {xs. length xs = k \<and> distinct xs \<and> set xs \<subseteq> A} = \<Prod>{card A - k + 1 .. card A}" |
fab9a53158f8
slightly generalized card_lists_distinct_length_eq; renamed specialized card_lists_distinct_length_eq to card_lists_distinct_length_eq'; tuned
bulwahn
parents:
66257
diff
changeset
|
5039 |
proof - |
fab9a53158f8
slightly generalized card_lists_distinct_length_eq; renamed specialized card_lists_distinct_length_eq to card_lists_distinct_length_eq'; tuned
bulwahn
parents:
66257
diff
changeset
|
5040 |
from \<open>k < card A\<close> have "finite A" and "k \<le> card A" using card_infinite by force+ |
fab9a53158f8
slightly generalized card_lists_distinct_length_eq; renamed specialized card_lists_distinct_length_eq to card_lists_distinct_length_eq'; tuned
bulwahn
parents:
66257
diff
changeset
|
5041 |
from this show ?thesis by (rule card_lists_distinct_length_eq) |
fab9a53158f8
slightly generalized card_lists_distinct_length_eq; renamed specialized card_lists_distinct_length_eq to card_lists_distinct_length_eq'; tuned
bulwahn
parents:
66257
diff
changeset
|
5042 |
qed |
fab9a53158f8
slightly generalized card_lists_distinct_length_eq; renamed specialized card_lists_distinct_length_eq to card_lists_distinct_length_eq'; tuned
bulwahn
parents:
66257
diff
changeset
|
5043 |
|
67091 | 5044 |
lemma infinite_UNIV_listI: "\<not> finite(UNIV::'a list set)" |
68719 | 5045 |
by (metis UNIV_I finite_maxlen length_replicate less_irrefl) |
28642 | 5046 |
|
5047 |
||
60758 | 5048 |
subsection \<open>Sorting\<close> |
5049 |
||
69593 | 5050 |
subsubsection \<open>\<^const>\<open>sorted_wrt\<close>\<close> |
5051 |
||
5052 |
text \<open>Sometimes the second equation in the definition of \<^const>\<open>sorted_wrt\<close> is too aggressive |
|
68125 | 5053 |
because it relates each list element to \emph{all} its successors. Then this equation |
5054 |
should be removed and \<open>sorted_wrt2_simps\<close> should be added instead.\<close> |
|
5055 |
||
5056 |
lemma sorted_wrt1: "sorted_wrt P [x] = True" |
|
5057 |
by(simp) |
|
5058 |
||
5059 |
lemma sorted_wrt2: "transp P \<Longrightarrow> sorted_wrt P (x # y # zs) = (P x y \<and> sorted_wrt P (y # zs))" |
|
68719 | 5060 |
proof (induction zs arbitrary: x y) |
5061 |
case (Cons z zs) |
|
5062 |
then show ?case |
|
5063 |
by simp (meson transpD)+ |
|
5064 |
qed auto |
|
68125 | 5065 |
|
5066 |
lemmas sorted_wrt2_simps = sorted_wrt1 sorted_wrt2 |
|
66434
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66358
diff
changeset
|
5067 |
|
68141 | 5068 |
lemma sorted_wrt_true [simp]: |
5069 |
"sorted_wrt (\<lambda>_ _. True) xs" |
|
5070 |
by (induction xs) simp_all |
|
5071 |
||
66434
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66358
diff
changeset
|
5072 |
lemma sorted_wrt_append: |
68109 | 5073 |
"sorted_wrt P (xs @ ys) \<longleftrightarrow> |
66892 | 5074 |
sorted_wrt P xs \<and> sorted_wrt P ys \<and> (\<forall>x\<in>set xs. \<forall>y\<in>set ys. P x y)" |
68109 | 5075 |
by (induction xs) auto |
66890
e92d48a42a01
drop a superfluous assumption that was found by the find_unused_assms command and tune proof
bulwahn
parents:
66889
diff
changeset
|
5076 |
|
68141 | 5077 |
lemma sorted_wrt_map: |
5078 |
"sorted_wrt R (map f xs) = sorted_wrt (\<lambda>x y. R (f x) (f y)) xs" |
|
5079 |
by (induction xs) simp_all |
|
5080 |
||
66890
e92d48a42a01
drop a superfluous assumption that was found by the find_unused_assms command and tune proof
bulwahn
parents:
66889
diff
changeset
|
5081 |
lemma sorted_wrt_rev: |
e92d48a42a01
drop a superfluous assumption that was found by the find_unused_assms command and tune proof
bulwahn
parents:
66889
diff
changeset
|
5082 |
"sorted_wrt P (rev xs) = sorted_wrt (\<lambda>x y. P y x) xs" |
68109 | 5083 |
by (induction xs) (auto simp add: sorted_wrt_append) |
66434
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66358
diff
changeset
|
5084 |
|
68141 | 5085 |
lemma sorted_wrt_mono_rel: |
5086 |
"(\<And>x y. \<lbrakk> x \<in> set xs; y \<in> set xs; P x y \<rbrakk> \<Longrightarrow> Q x y) \<Longrightarrow> sorted_wrt P xs \<Longrightarrow> sorted_wrt Q xs" |
|
68109 | 5087 |
by(induction xs)(auto) |
66442 | 5088 |
|
67973 | 5089 |
lemma sorted_wrt01: "length xs \<le> 1 \<Longrightarrow> sorted_wrt P xs" |
5090 |
by(auto simp: le_Suc_eq length_Suc_conv) |
|
5091 |
||
68175 | 5092 |
lemma sorted_wrt_iff_nth_less: |
68156 | 5093 |
"sorted_wrt P xs = (\<forall>i j. i < j \<longrightarrow> j < length xs \<longrightarrow> P (xs ! i) (xs ! j))" |
68719 | 5094 |
by (induction xs) (auto simp add: in_set_conv_nth Ball_def nth_Cons split: nat.split) |
68156 | 5095 |
|
5096 |
lemma sorted_wrt_nth_less: |
|
5097 |
"\<lbrakk> sorted_wrt P xs; i < j; j < length xs \<rbrakk> \<Longrightarrow> P (xs ! i) (xs ! j)" |
|
68175 | 5098 |
by(auto simp: sorted_wrt_iff_nth_less) |
66434
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66358
diff
changeset
|
5099 |
|
68141 | 5100 |
lemma sorted_wrt_upt[simp]: "sorted_wrt (<) [m..<n]" |
66434
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66358
diff
changeset
|
5101 |
by(induction n) (auto simp: sorted_wrt_append) |
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66358
diff
changeset
|
5102 |
|
68191 | 5103 |
lemma sorted_wrt_upto[simp]: "sorted_wrt (<) [i..j]" |
68719 | 5104 |
proof(induct i j rule:upto.induct) |
5105 |
case (1 i j) |
|
5106 |
from this show ?case |
|
5107 |
unfolding upto.simps[of i j] by auto |
|
5108 |
qed |
|
68175 | 5109 |
|
66434
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66358
diff
changeset
|
5110 |
text \<open>Each element is greater or equal to its index:\<close> |
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66358
diff
changeset
|
5111 |
|
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66358
diff
changeset
|
5112 |
lemma sorted_wrt_less_idx: |
67399 | 5113 |
"sorted_wrt (<) ns \<Longrightarrow> i < length ns \<Longrightarrow> i \<le> ns!i" |
66434
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66358
diff
changeset
|
5114 |
proof (induction ns arbitrary: i rule: rev_induct) |
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66358
diff
changeset
|
5115 |
case Nil thus ?case by simp |
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66358
diff
changeset
|
5116 |
next |
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66358
diff
changeset
|
5117 |
case snoc |
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66358
diff
changeset
|
5118 |
thus ?case |
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66358
diff
changeset
|
5119 |
by (auto simp: nth_append sorted_wrt_append) |
66892 | 5120 |
(metis less_antisym not_less nth_mem) |
66434
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66358
diff
changeset
|
5121 |
qed |
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66358
diff
changeset
|
5122 |
|
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66358
diff
changeset
|
5123 |
|
69593 | 5124 |
subsubsection \<open>\<^const>\<open>sorted\<close>\<close> |
24617 | 5125 |
|
24616 | 5126 |
context linorder |
5127 |
begin |
|
5128 |
||
69593 | 5129 |
text \<open>Sometimes the second equation in the definition of \<^const>\<open>sorted\<close> is too aggressive |
68111 | 5130 |
because it relates each list element to \emph{all} its successors. Then this equation |
68118 | 5131 |
should be removed and \<open>sorted2_simps\<close> should be added instead. |
5132 |
Executable code is one such use case.\<close> |
|
5133 |
||
5134 |
lemma sorted1: "sorted [x] = True" |
|
68111 | 5135 |
by simp |
5136 |
||
5137 |
lemma sorted2: "sorted (x # y # zs) = (x \<le> y \<and> sorted (y # zs))" |
|
5138 |
by(induction zs) auto |
|
5139 |
||
5140 |
lemmas sorted2_simps = sorted1 sorted2 |
|
5141 |
||
68118 | 5142 |
lemmas [code] = sorted.simps(1) sorted2_simps |
5143 |
||
68175 | 5144 |
lemma sorted_append: |
5145 |
"sorted (xs@ys) = (sorted xs \<and> sorted ys \<and> (\<forall>x \<in> set xs. \<forall>y \<in> set ys. x\<le>y))" |
|
5146 |
by (simp add: sorted_sorted_wrt sorted_wrt_append) |
|
5147 |
||
5148 |
lemma sorted_map: |
|
5149 |
"sorted (map f xs) = sorted_wrt (\<lambda>x y. f x \<le> f y) xs" |
|
5150 |
by (simp add: sorted_sorted_wrt sorted_wrt_map) |
|
5151 |
||
68160 | 5152 |
lemma sorted01: "length xs \<le> 1 \<Longrightarrow> sorted xs" |
5153 |
by (simp add: sorted_sorted_wrt sorted_wrt01) |
|
5154 |
||
40210
aee7ef725330
sorting: avoid _key suffix if lemma applies both to simple and generalized variant; generalized insort_insert to insort_insert_key; additional lemmas
haftmann
parents:
40195
diff
changeset
|
5155 |
lemma sorted_tl: |
aee7ef725330
sorting: avoid _key suffix if lemma applies both to simple and generalized variant; generalized insort_insert to insort_insert_key; additional lemmas
haftmann
parents:
40195
diff
changeset
|
5156 |
"sorted xs \<Longrightarrow> sorted (tl xs)" |
68109 | 5157 |
by (cases xs) (simp_all) |
40210
aee7ef725330
sorting: avoid _key suffix if lemma applies both to simple and generalized variant; generalized insort_insert to insort_insert_key; additional lemmas
haftmann
parents:
40195
diff
changeset
|
5158 |
|
68186 | 5159 |
lemma sorted_iff_nth_mono_less: |
68175 | 5160 |
"sorted xs = (\<forall>i j. i < j \<longrightarrow> j < length xs \<longrightarrow> xs ! i \<le> xs ! j)" |
5161 |
by (simp add: sorted_sorted_wrt sorted_wrt_iff_nth_less) |
|
24616 | 5162 |
|
68186 | 5163 |
lemma sorted_iff_nth_mono: |
5164 |
"sorted xs = (\<forall>i j. i \<le> j \<longrightarrow> j < length xs \<longrightarrow> xs ! i \<le> xs ! j)" |
|
5165 |
by (auto simp: sorted_iff_nth_mono_less nat_less_le) |
|
5166 |
||
31201 | 5167 |
lemma sorted_nth_mono: |
33639
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
hoelzl
parents:
33593
diff
changeset
|
5168 |
"sorted xs \<Longrightarrow> i \<le> j \<Longrightarrow> j < length xs \<Longrightarrow> xs!i \<le> xs!j" |
68186 | 5169 |
by (auto simp: sorted_iff_nth_mono) |
31201 | 5170 |
|
33639
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
hoelzl
parents:
33593
diff
changeset
|
5171 |
lemma sorted_rev_nth_mono: |
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
hoelzl
parents:
33593
diff
changeset
|
5172 |
"sorted (rev xs) \<Longrightarrow> i \<le> j \<Longrightarrow> j < length xs \<Longrightarrow> xs!j \<le> xs!i" |
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
hoelzl
parents:
33593
diff
changeset
|
5173 |
using sorted_nth_mono[ of "rev xs" "length xs - j - 1" "length xs - i - 1"] |
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
hoelzl
parents:
33593
diff
changeset
|
5174 |
rev_nth[of "length xs - i - 1" "xs"] rev_nth[of "length xs - j - 1" "xs"] |
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
hoelzl
parents:
33593
diff
changeset
|
5175 |
by auto |
68176 | 5176 |
|
66434
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66358
diff
changeset
|
5177 |
lemma sorted_map_remove1: |
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66358
diff
changeset
|
5178 |
"sorted (map f xs) \<Longrightarrow> sorted (map f (remove1 x xs))" |
68109 | 5179 |
by (induct xs) (auto) |
66434
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66358
diff
changeset
|
5180 |
|
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66358
diff
changeset
|
5181 |
lemma sorted_remove1: "sorted xs \<Longrightarrow> sorted (remove1 a xs)" |
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66358
diff
changeset
|
5182 |
using sorted_map_remove1 [of "\<lambda>x. x"] by simp |
33639
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
hoelzl
parents:
33593
diff
changeset
|
5183 |
|
36851 | 5184 |
lemma sorted_butlast: |
5185 |
assumes "xs \<noteq> []" and "sorted xs" |
|
5186 |
shows "sorted (butlast xs)" |
|
5187 |
proof - |
|
66434
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66358
diff
changeset
|
5188 |
from \<open>xs \<noteq> []\<close> obtain ys y where "xs = ys @ [y]" |
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66358
diff
changeset
|
5189 |
by (cases xs rule: rev_cases) auto |
60758 | 5190 |
with \<open>sorted xs\<close> show ?thesis by (simp add: sorted_append) |
36851 | 5191 |
qed |
64963 | 5192 |
|
66905 | 5193 |
lemma sorted_replicate [simp]: "sorted(replicate n x)" |
68109 | 5194 |
by(induction n) (auto) |
66905 | 5195 |
|
26143
314c0bcb7df7
Added useful general lemmas from the work with the HeapMonad
bulwahn
parents:
26073
diff
changeset
|
5196 |
lemma sorted_remdups[simp]: |
68109 | 5197 |
"sorted xs \<Longrightarrow> sorted (remdups xs)" |
5198 |
by (induct xs) (auto) |
|
26143
314c0bcb7df7
Added useful general lemmas from the work with the HeapMonad
bulwahn
parents:
26073
diff
changeset
|
5199 |
|
53721
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
traytel
parents:
53689
diff
changeset
|
5200 |
lemma sorted_remdups_adj[simp]: |
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
traytel
parents:
53689
diff
changeset
|
5201 |
"sorted xs \<Longrightarrow> sorted (remdups_adj xs)" |
68109 | 5202 |
by (induct xs rule: remdups_adj.induct, simp_all split: if_split_asm) |
53721
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
traytel
parents:
53689
diff
changeset
|
5203 |
|
66853 | 5204 |
lemma sorted_nths: "sorted xs \<Longrightarrow> sorted (nths xs I)" |
68109 | 5205 |
by(induction xs arbitrary: I)(auto simp: nths_Cons) |
66853 | 5206 |
|
24645 | 5207 |
lemma sorted_distinct_set_unique: |
5208 |
assumes "sorted xs" "distinct xs" "sorted ys" "distinct ys" "set xs = set ys" |
|
5209 |
shows "xs = ys" |
|
5210 |
proof - |
|
26734 | 5211 |
from assms have 1: "length xs = length ys" by (auto dest!: distinct_card) |
24645 | 5212 |
from assms show ?thesis |
5213 |
proof(induct rule:list_induct2[OF 1]) |
|
5214 |
case 1 show ?case by simp |
|
5215 |
next |
|
68109 | 5216 |
case 2 thus ?case by simp (metis Diff_insert_absorb antisym insertE insert_iff) |
24645 | 5217 |
qed |
5218 |
qed |
|
5219 |
||
35603 | 5220 |
lemma map_sorted_distinct_set_unique: |
5221 |
assumes "inj_on f (set xs \<union> set ys)" |
|
5222 |
assumes "sorted (map f xs)" "distinct (map f xs)" |
|
5223 |
"sorted (map f ys)" "distinct (map f ys)" |
|
5224 |
assumes "set xs = set ys" |
|
5225 |
shows "xs = ys" |
|
5226 |
proof - |
|
5227 |
from assms have "map f xs = map f ys" |
|
5228 |
by (simp add: sorted_distinct_set_unique) |
|
60758 | 5229 |
with \<open>inj_on f (set xs \<union> set ys)\<close> show "xs = ys" |
35603 | 5230 |
by (blast intro: map_inj_on) |
5231 |
qed |
|
5232 |
||
39915
ecf97cf3d248
turned distinct and sorted into inductive predicates: yields nice induction principles for free; more elegant proofs
haftmann
parents:
39774
diff
changeset
|
5233 |
lemma |
ecf97cf3d248
turned distinct and sorted into inductive predicates: yields nice induction principles for free; more elegant proofs
haftmann
parents:
39774
diff
changeset
|
5234 |
assumes "sorted xs" |
ecf97cf3d248
turned distinct and sorted into inductive predicates: yields nice induction principles for free; more elegant proofs
haftmann
parents:
39774
diff
changeset
|
5235 |
shows sorted_take: "sorted (take n xs)" |
ecf97cf3d248
turned distinct and sorted into inductive predicates: yields nice induction principles for free; more elegant proofs
haftmann
parents:
39774
diff
changeset
|
5236 |
and sorted_drop: "sorted (drop n xs)" |
ecf97cf3d248
turned distinct and sorted into inductive predicates: yields nice induction principles for free; more elegant proofs
haftmann
parents:
39774
diff
changeset
|
5237 |
proof - |
ecf97cf3d248
turned distinct and sorted into inductive predicates: yields nice induction principles for free; more elegant proofs
haftmann
parents:
39774
diff
changeset
|
5238 |
from assms have "sorted (take n xs @ drop n xs)" by simp |
ecf97cf3d248
turned distinct and sorted into inductive predicates: yields nice induction principles for free; more elegant proofs
haftmann
parents:
39774
diff
changeset
|
5239 |
then show "sorted (take n xs)" and "sorted (drop n xs)" |
ecf97cf3d248
turned distinct and sorted into inductive predicates: yields nice induction principles for free; more elegant proofs
haftmann
parents:
39774
diff
changeset
|
5240 |
unfolding sorted_append by simp_all |
29626 | 5241 |
qed |
5242 |
||
33639
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
hoelzl
parents:
33593
diff
changeset
|
5243 |
lemma sorted_dropWhile: "sorted xs \<Longrightarrow> sorted (dropWhile P xs)" |
39915
ecf97cf3d248
turned distinct and sorted into inductive predicates: yields nice induction principles for free; more elegant proofs
haftmann
parents:
39774
diff
changeset
|
5244 |
by (auto dest: sorted_drop simp add: dropWhile_eq_drop) |
33639
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
hoelzl
parents:
33593
diff
changeset
|
5245 |
|
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
hoelzl
parents:
33593
diff
changeset
|
5246 |
lemma sorted_takeWhile: "sorted xs \<Longrightarrow> sorted (takeWhile P xs)" |
39915
ecf97cf3d248
turned distinct and sorted into inductive predicates: yields nice induction principles for free; more elegant proofs
haftmann
parents:
39774
diff
changeset
|
5247 |
by (subst takeWhile_eq_take) (auto dest: sorted_take) |
29626 | 5248 |
|
34933 | 5249 |
lemma sorted_filter: |
5250 |
"sorted (map f xs) \<Longrightarrow> sorted (map f (filter P xs))" |
|
68109 | 5251 |
by (induct xs) simp_all |
34933 | 5252 |
|
5253 |
lemma foldr_max_sorted: |
|
5254 |
assumes "sorted (rev xs)" |
|
5255 |
shows "foldr max xs y = (if xs = [] then y else max (xs ! 0) y)" |
|
53374
a14d2a854c02
tuned proofs -- clarified flow of facts wrt. calculation;
wenzelm
parents:
53017
diff
changeset
|
5256 |
using assms |
a14d2a854c02
tuned proofs -- clarified flow of facts wrt. calculation;
wenzelm
parents:
53017
diff
changeset
|
5257 |
proof (induct xs) |
34933 | 5258 |
case (Cons x xs) |
53374
a14d2a854c02
tuned proofs -- clarified flow of facts wrt. calculation;
wenzelm
parents:
53017
diff
changeset
|
5259 |
then have "sorted (rev xs)" using sorted_append by auto |
a14d2a854c02
tuned proofs -- clarified flow of facts wrt. calculation;
wenzelm
parents:
53017
diff
changeset
|
5260 |
with Cons show ?case |
a14d2a854c02
tuned proofs -- clarified flow of facts wrt. calculation;
wenzelm
parents:
53017
diff
changeset
|
5261 |
by (cases xs) (auto simp add: sorted_append max_def) |
34933 | 5262 |
qed simp |
5263 |
||
5264 |
lemma filter_equals_takeWhile_sorted_rev: |
|
5265 |
assumes sorted: "sorted (rev (map f xs))" |
|
68249
949d93804740
First step to remove nonstandard "[x <- xs. P]" syntax: only input
nipkow
parents:
68215
diff
changeset
|
5266 |
shows "filter (\<lambda>x. t < f x) xs = takeWhile (\<lambda> x. t < f x) xs" |
34933 | 5267 |
(is "filter ?P xs = ?tW") |
5268 |
proof (rule takeWhile_eq_filter[symmetric]) |
|
5269 |
let "?dW" = "dropWhile ?P xs" |
|
5270 |
fix x assume "x \<in> set ?dW" |
|
5271 |
then obtain i where i: "i < length ?dW" and nth_i: "x = ?dW ! i" |
|
5272 |
unfolding in_set_conv_nth by auto |
|
5273 |
hence "length ?tW + i < length (?tW @ ?dW)" |
|
5274 |
unfolding length_append by simp |
|
5275 |
hence i': "length (map f ?tW) + i < length (map f xs)" by simp |
|
5276 |
have "(map f ?tW @ map f ?dW) ! (length (map f ?tW) + i) \<le> |
|
5277 |
(map f ?tW @ map f ?dW) ! (length (map f ?tW) + 0)" |
|
5278 |
using sorted_rev_nth_mono[OF sorted _ i', of "length ?tW"] |
|
5279 |
unfolding map_append[symmetric] by simp |
|
5280 |
hence "f x \<le> f (?dW ! 0)" |
|
5281 |
unfolding nth_append_length_plus nth_i |
|
5282 |
using i preorder_class.le_less_trans[OF le0 i] by simp |
|
5283 |
also have "... \<le> t" |
|
5284 |
using hd_dropWhile[of "?P" xs] le0[THEN preorder_class.le_less_trans, OF i] |
|
5285 |
using hd_conv_nth[of "?dW"] by simp |
|
5286 |
finally show "\<not> t < f x" by simp |
|
5287 |
qed |
|
5288 |
||
66434
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66358
diff
changeset
|
5289 |
lemma sorted_map_same: |
68249
949d93804740
First step to remove nonstandard "[x <- xs. P]" syntax: only input
nipkow
parents:
68215
diff
changeset
|
5290 |
"sorted (map f (filter (\<lambda>x. f x = g xs) xs))" |
66434
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66358
diff
changeset
|
5291 |
proof (induct xs arbitrary: g) |
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66358
diff
changeset
|
5292 |
case Nil then show ?case by simp |
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66358
diff
changeset
|
5293 |
next |
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66358
diff
changeset
|
5294 |
case (Cons x xs) |
68249
949d93804740
First step to remove nonstandard "[x <- xs. P]" syntax: only input
nipkow
parents:
68215
diff
changeset
|
5295 |
then have "sorted (map f (filter (\<lambda>y. f y = (\<lambda>xs. f x) xs) xs))" . |
949d93804740
First step to remove nonstandard "[x <- xs. P]" syntax: only input
nipkow
parents:
68215
diff
changeset
|
5296 |
moreover from Cons have "sorted (map f (filter (\<lambda>y. f y = (g \<circ> Cons x) xs) xs))" . |
68109 | 5297 |
ultimately show ?case by simp_all |
66434
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66358
diff
changeset
|
5298 |
qed |
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66358
diff
changeset
|
5299 |
|
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66358
diff
changeset
|
5300 |
lemma sorted_same: |
68249
949d93804740
First step to remove nonstandard "[x <- xs. P]" syntax: only input
nipkow
parents:
68215
diff
changeset
|
5301 |
"sorted (filter (\<lambda>x. x = g xs) xs)" |
66434
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66358
diff
changeset
|
5302 |
using sorted_map_same [of "\<lambda>x. x"] by simp |
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66358
diff
changeset
|
5303 |
|
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66358
diff
changeset
|
5304 |
end |
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66358
diff
changeset
|
5305 |
|
68190 | 5306 |
lemma sorted_upt[simp]: "sorted [m..<n]" |
5307 |
by(simp add: sorted_sorted_wrt sorted_wrt_mono_rel[OF _ sorted_wrt_upt]) |
|
5308 |
||
5309 |
lemma sorted_upto[simp]: "sorted [m..n]" |
|
5310 |
by(simp add: sorted_sorted_wrt sorted_wrt_mono_rel[OF _ sorted_wrt_upto]) |
|
5311 |
||
66434
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66358
diff
changeset
|
5312 |
|
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66358
diff
changeset
|
5313 |
subsubsection \<open>Sorting functions\<close> |
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66358
diff
changeset
|
5314 |
|
69593 | 5315 |
text\<open>Currently it is not shown that \<^const>\<open>sort\<close> returns a |
66434
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66358
diff
changeset
|
5316 |
permutation of its input because the nicest proof is via multisets, |
66654 | 5317 |
which are not part of Main. Alternatively one could define a function |
66434
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66358
diff
changeset
|
5318 |
that counts the number of occurrences of an element in a list and use |
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66358
diff
changeset
|
5319 |
that instead of multisets to state the correctness property.\<close> |
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66358
diff
changeset
|
5320 |
|
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66358
diff
changeset
|
5321 |
context linorder |
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66358
diff
changeset
|
5322 |
begin |
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66358
diff
changeset
|
5323 |
|
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66358
diff
changeset
|
5324 |
lemma set_insort_key: |
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66358
diff
changeset
|
5325 |
"set (insort_key f x xs) = insert x (set xs)" |
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66358
diff
changeset
|
5326 |
by (induct xs) auto |
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66358
diff
changeset
|
5327 |
|
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66358
diff
changeset
|
5328 |
lemma length_insort [simp]: |
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66358
diff
changeset
|
5329 |
"length (insort_key f x xs) = Suc (length xs)" |
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66358
diff
changeset
|
5330 |
by (induct xs) simp_all |
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66358
diff
changeset
|
5331 |
|
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66358
diff
changeset
|
5332 |
lemma insort_key_left_comm: |
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66358
diff
changeset
|
5333 |
assumes "f x \<noteq> f y" |
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66358
diff
changeset
|
5334 |
shows "insort_key f y (insort_key f x xs) = insort_key f x (insort_key f y xs)" |
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66358
diff
changeset
|
5335 |
by (induct xs) (auto simp add: assms dest: antisym) |
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66358
diff
changeset
|
5336 |
|
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66358
diff
changeset
|
5337 |
lemma insort_left_comm: |
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66358
diff
changeset
|
5338 |
"insort x (insort y xs) = insort y (insort x xs)" |
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66358
diff
changeset
|
5339 |
by (cases "x = y") (auto intro: insort_key_left_comm) |
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66358
diff
changeset
|
5340 |
|
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66358
diff
changeset
|
5341 |
lemma comp_fun_commute_insort: "comp_fun_commute insort" |
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66358
diff
changeset
|
5342 |
proof |
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66358
diff
changeset
|
5343 |
qed (simp add: insort_left_comm fun_eq_iff) |
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66358
diff
changeset
|
5344 |
|
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66358
diff
changeset
|
5345 |
lemma sort_key_simps [simp]: |
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66358
diff
changeset
|
5346 |
"sort_key f [] = []" |
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66358
diff
changeset
|
5347 |
"sort_key f (x#xs) = insort_key f x (sort_key f xs)" |
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66358
diff
changeset
|
5348 |
by (simp_all add: sort_key_def) |
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66358
diff
changeset
|
5349 |
|
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66358
diff
changeset
|
5350 |
lemma sort_key_conv_fold: |
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66358
diff
changeset
|
5351 |
assumes "inj_on f (set xs)" |
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66358
diff
changeset
|
5352 |
shows "sort_key f xs = fold (insort_key f) xs []" |
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66358
diff
changeset
|
5353 |
proof - |
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66358
diff
changeset
|
5354 |
have "fold (insort_key f) (rev xs) = fold (insort_key f) xs" |
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66358
diff
changeset
|
5355 |
proof (rule fold_rev, rule ext) |
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66358
diff
changeset
|
5356 |
fix zs |
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66358
diff
changeset
|
5357 |
fix x y |
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66358
diff
changeset
|
5358 |
assume "x \<in> set xs" "y \<in> set xs" |
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66358
diff
changeset
|
5359 |
with assms have *: "f y = f x \<Longrightarrow> y = x" by (auto dest: inj_onD) |
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66358
diff
changeset
|
5360 |
have **: "x = y \<longleftrightarrow> y = x" by auto |
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66358
diff
changeset
|
5361 |
show "(insort_key f y \<circ> insort_key f x) zs = (insort_key f x \<circ> insort_key f y) zs" |
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66358
diff
changeset
|
5362 |
by (induct zs) (auto intro: * simp add: **) |
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66358
diff
changeset
|
5363 |
qed |
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66358
diff
changeset
|
5364 |
then show ?thesis by (simp add: sort_key_def foldr_conv_fold) |
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66358
diff
changeset
|
5365 |
qed |
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66358
diff
changeset
|
5366 |
|
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66358
diff
changeset
|
5367 |
lemma sort_conv_fold: |
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66358
diff
changeset
|
5368 |
"sort xs = fold insort xs []" |
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66358
diff
changeset
|
5369 |
by (rule sort_key_conv_fold) simp |
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66358
diff
changeset
|
5370 |
|
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66358
diff
changeset
|
5371 |
lemma length_sort[simp]: "length (sort_key f xs) = length xs" |
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66358
diff
changeset
|
5372 |
by (induct xs, auto) |
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66358
diff
changeset
|
5373 |
|
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66358
diff
changeset
|
5374 |
lemma set_sort[simp]: "set(sort_key f xs) = set xs" |
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66358
diff
changeset
|
5375 |
by (induct xs) (simp_all add: set_insort_key) |
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66358
diff
changeset
|
5376 |
|
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66358
diff
changeset
|
5377 |
lemma distinct_insort: "distinct (insort_key f x xs) = (x \<notin> set xs \<and> distinct xs)" |
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66358
diff
changeset
|
5378 |
by(induct xs)(auto simp: set_insort_key) |
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66358
diff
changeset
|
5379 |
|
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66358
diff
changeset
|
5380 |
lemma distinct_sort[simp]: "distinct (sort_key f xs) = distinct xs" |
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66358
diff
changeset
|
5381 |
by (induct xs) (simp_all add: distinct_insort) |
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66358
diff
changeset
|
5382 |
|
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66358
diff
changeset
|
5383 |
lemma sorted_insort_key: "sorted (map f (insort_key f x xs)) = sorted (map f xs)" |
68109 | 5384 |
by (induct xs) (auto simp: set_insort_key) |
66434
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66358
diff
changeset
|
5385 |
|
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66358
diff
changeset
|
5386 |
lemma sorted_insort: "sorted (insort x xs) = sorted xs" |
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66358
diff
changeset
|
5387 |
using sorted_insort_key [where f="\<lambda>x. x"] by simp |
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66358
diff
changeset
|
5388 |
|
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66358
diff
changeset
|
5389 |
theorem sorted_sort_key [simp]: "sorted (map f (sort_key f xs))" |
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66358
diff
changeset
|
5390 |
by (induct xs) (auto simp:sorted_insort_key) |
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66358
diff
changeset
|
5391 |
|
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66358
diff
changeset
|
5392 |
theorem sorted_sort [simp]: "sorted (sort xs)" |
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66358
diff
changeset
|
5393 |
using sorted_sort_key [where f="\<lambda>x. x"] by simp |
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66358
diff
changeset
|
5394 |
|
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66358
diff
changeset
|
5395 |
lemma insort_not_Nil [simp]: |
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66358
diff
changeset
|
5396 |
"insort_key f a xs \<noteq> []" |
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66358
diff
changeset
|
5397 |
by (induction xs) simp_all |
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66358
diff
changeset
|
5398 |
|
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66358
diff
changeset
|
5399 |
lemma insort_is_Cons: "\<forall>x\<in>set xs. f a \<le> f x \<Longrightarrow> insort_key f a xs = a # xs" |
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66358
diff
changeset
|
5400 |
by (cases xs) auto |
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66358
diff
changeset
|
5401 |
|
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66358
diff
changeset
|
5402 |
lemma sorted_sort_id: "sorted xs \<Longrightarrow> sort xs = xs" |
68109 | 5403 |
by (induct xs) (auto simp add: insort_is_Cons) |
66434
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66358
diff
changeset
|
5404 |
|
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66358
diff
changeset
|
5405 |
lemma insort_key_remove1: |
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66358
diff
changeset
|
5406 |
assumes "a \<in> set xs" and "sorted (map f xs)" and "hd (filter (\<lambda>x. f a = f x) xs) = a" |
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66358
diff
changeset
|
5407 |
shows "insort_key f a (remove1 a xs) = xs" |
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66358
diff
changeset
|
5408 |
using assms proof (induct xs) |
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66358
diff
changeset
|
5409 |
case (Cons x xs) |
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66358
diff
changeset
|
5410 |
then show ?case |
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66358
diff
changeset
|
5411 |
proof (cases "x = a") |
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66358
diff
changeset
|
5412 |
case False |
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66358
diff
changeset
|
5413 |
then have "f x \<noteq> f a" using Cons.prems by auto |
68109 | 5414 |
then have "f x < f a" using Cons.prems by auto |
5415 |
with \<open>f x \<noteq> f a\<close> show ?thesis using Cons by (auto simp: insort_is_Cons) |
|
5416 |
qed (auto simp: insort_is_Cons) |
|
66434
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66358
diff
changeset
|
5417 |
qed simp |
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66358
diff
changeset
|
5418 |
|
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66358
diff
changeset
|
5419 |
lemma insort_remove1: |
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66358
diff
changeset
|
5420 |
assumes "a \<in> set xs" and "sorted xs" |
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66358
diff
changeset
|
5421 |
shows "insort a (remove1 a xs) = xs" |
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66358
diff
changeset
|
5422 |
proof (rule insort_key_remove1) |
67399 | 5423 |
define n where "n = length (filter ((=) a) xs) - 1" |
66434
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66358
diff
changeset
|
5424 |
from \<open>a \<in> set xs\<close> show "a \<in> set xs" . |
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66358
diff
changeset
|
5425 |
from \<open>sorted xs\<close> show "sorted (map (\<lambda>x. x) xs)" by simp |
67399 | 5426 |
from \<open>a \<in> set xs\<close> have "a \<in> set (filter ((=) a) xs)" by auto |
5427 |
then have "set (filter ((=) a) xs) \<noteq> {}" by auto |
|
5428 |
then have "filter ((=) a) xs \<noteq> []" by (auto simp only: set_empty) |
|
5429 |
then have "length (filter ((=) a) xs) > 0" by simp |
|
5430 |
then have n: "Suc n = length (filter ((=) a) xs)" by (simp add: n_def) |
|
66434
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66358
diff
changeset
|
5431 |
moreover have "replicate (Suc n) a = a # replicate n a" |
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66358
diff
changeset
|
5432 |
by simp |
67399 | 5433 |
ultimately show "hd (filter ((=) a) xs) = a" by (simp add: replicate_length_filter) |
66434
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66358
diff
changeset
|
5434 |
qed |
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66358
diff
changeset
|
5435 |
|
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66358
diff
changeset
|
5436 |
lemma finite_sorted_distinct_unique: |
68719 | 5437 |
assumes "finite A" shows "\<exists>!xs. set xs = A \<and> sorted xs \<and> distinct xs" |
5438 |
proof - |
|
5439 |
obtain xs where "distinct xs" "A = set xs" |
|
5440 |
using finite_distinct_list [OF assms] by metis |
|
5441 |
then show ?thesis |
|
5442 |
by (rule_tac a="sort xs" in ex1I) (auto simp: sorted_distinct_set_unique) |
|
5443 |
qed |
|
66434
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66358
diff
changeset
|
5444 |
|
40210
aee7ef725330
sorting: avoid _key suffix if lemma applies both to simple and generalized variant; generalized insort_insert to insort_insert_key; additional lemmas
haftmann
parents:
40195
diff
changeset
|
5445 |
lemma insort_insert_key_triv: |
aee7ef725330
sorting: avoid _key suffix if lemma applies both to simple and generalized variant; generalized insort_insert to insort_insert_key; additional lemmas
haftmann
parents:
40195
diff
changeset
|
5446 |
"f x \<in> f ` set xs \<Longrightarrow> insort_insert_key f x xs = xs" |
aee7ef725330
sorting: avoid _key suffix if lemma applies both to simple and generalized variant; generalized insort_insert to insort_insert_key; additional lemmas
haftmann
parents:
40195
diff
changeset
|
5447 |
by (simp add: insort_insert_key_def) |
aee7ef725330
sorting: avoid _key suffix if lemma applies both to simple and generalized variant; generalized insort_insert to insort_insert_key; additional lemmas
haftmann
parents:
40195
diff
changeset
|
5448 |
|
aee7ef725330
sorting: avoid _key suffix if lemma applies both to simple and generalized variant; generalized insort_insert to insort_insert_key; additional lemmas
haftmann
parents:
40195
diff
changeset
|
5449 |
lemma insort_insert_triv: |
aee7ef725330
sorting: avoid _key suffix if lemma applies both to simple and generalized variant; generalized insort_insert to insort_insert_key; additional lemmas
haftmann
parents:
40195
diff
changeset
|
5450 |
"x \<in> set xs \<Longrightarrow> insort_insert x xs = xs" |
aee7ef725330
sorting: avoid _key suffix if lemma applies both to simple and generalized variant; generalized insort_insert to insort_insert_key; additional lemmas
haftmann
parents:
40195
diff
changeset
|
5451 |
using insort_insert_key_triv [of "\<lambda>x. x"] by simp |
aee7ef725330
sorting: avoid _key suffix if lemma applies both to simple and generalized variant; generalized insort_insert to insort_insert_key; additional lemmas
haftmann
parents:
40195
diff
changeset
|
5452 |
|
aee7ef725330
sorting: avoid _key suffix if lemma applies both to simple and generalized variant; generalized insort_insert to insort_insert_key; additional lemmas
haftmann
parents:
40195
diff
changeset
|
5453 |
lemma insort_insert_insort_key: |
aee7ef725330
sorting: avoid _key suffix if lemma applies both to simple and generalized variant; generalized insort_insert to insort_insert_key; additional lemmas
haftmann
parents:
40195
diff
changeset
|
5454 |
"f x \<notin> f ` set xs \<Longrightarrow> insort_insert_key f x xs = insort_key f x xs" |
aee7ef725330
sorting: avoid _key suffix if lemma applies both to simple and generalized variant; generalized insort_insert to insort_insert_key; additional lemmas
haftmann
parents:
40195
diff
changeset
|
5455 |
by (simp add: insort_insert_key_def) |
aee7ef725330
sorting: avoid _key suffix if lemma applies both to simple and generalized variant; generalized insort_insert to insort_insert_key; additional lemmas
haftmann
parents:
40195
diff
changeset
|
5456 |
|
aee7ef725330
sorting: avoid _key suffix if lemma applies both to simple and generalized variant; generalized insort_insert to insort_insert_key; additional lemmas
haftmann
parents:
40195
diff
changeset
|
5457 |
lemma insort_insert_insort: |
aee7ef725330
sorting: avoid _key suffix if lemma applies both to simple and generalized variant; generalized insort_insert to insort_insert_key; additional lemmas
haftmann
parents:
40195
diff
changeset
|
5458 |
"x \<notin> set xs \<Longrightarrow> insort_insert x xs = insort x xs" |
aee7ef725330
sorting: avoid _key suffix if lemma applies both to simple and generalized variant; generalized insort_insert to insort_insert_key; additional lemmas
haftmann
parents:
40195
diff
changeset
|
5459 |
using insort_insert_insort_key [of "\<lambda>x. x"] by simp |
aee7ef725330
sorting: avoid _key suffix if lemma applies both to simple and generalized variant; generalized insort_insert to insort_insert_key; additional lemmas
haftmann
parents:
40195
diff
changeset
|
5460 |
|
35608 | 5461 |
lemma set_insort_insert: |
5462 |
"set (insort_insert x xs) = insert x (set xs)" |
|
66434
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66358
diff
changeset
|
5463 |
by (auto simp add: insort_insert_key_def set_insort_key) |
35608 | 5464 |
|
5465 |
lemma distinct_insort_insert: |
|
5466 |
assumes "distinct xs" |
|
40210
aee7ef725330
sorting: avoid _key suffix if lemma applies both to simple and generalized variant; generalized insort_insert to insort_insert_key; additional lemmas
haftmann
parents:
40195
diff
changeset
|
5467 |
shows "distinct (insort_insert_key f x xs)" |
66434
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66358
diff
changeset
|
5468 |
using assms by (induct xs) (auto simp add: insort_insert_key_def set_insort_key) |
40210
aee7ef725330
sorting: avoid _key suffix if lemma applies both to simple and generalized variant; generalized insort_insert to insort_insert_key; additional lemmas
haftmann
parents:
40195
diff
changeset
|
5469 |
|
aee7ef725330
sorting: avoid _key suffix if lemma applies both to simple and generalized variant; generalized insort_insert to insort_insert_key; additional lemmas
haftmann
parents:
40195
diff
changeset
|
5470 |
lemma sorted_insort_insert_key: |
aee7ef725330
sorting: avoid _key suffix if lemma applies both to simple and generalized variant; generalized insort_insert to insort_insert_key; additional lemmas
haftmann
parents:
40195
diff
changeset
|
5471 |
assumes "sorted (map f xs)" |
aee7ef725330
sorting: avoid _key suffix if lemma applies both to simple and generalized variant; generalized insort_insert to insort_insert_key; additional lemmas
haftmann
parents:
40195
diff
changeset
|
5472 |
shows "sorted (map f (insort_insert_key f x xs))" |
aee7ef725330
sorting: avoid _key suffix if lemma applies both to simple and generalized variant; generalized insort_insert to insort_insert_key; additional lemmas
haftmann
parents:
40195
diff
changeset
|
5473 |
using assms by (simp add: insort_insert_key_def sorted_insort_key) |
35608 | 5474 |
|
5475 |
lemma sorted_insort_insert: |
|
5476 |
assumes "sorted xs" |
|
5477 |
shows "sorted (insort_insert x xs)" |
|
40210
aee7ef725330
sorting: avoid _key suffix if lemma applies both to simple and generalized variant; generalized insort_insert to insort_insert_key; additional lemmas
haftmann
parents:
40195
diff
changeset
|
5478 |
using assms sorted_insort_insert_key [of "\<lambda>x. x"] by simp |
aee7ef725330
sorting: avoid _key suffix if lemma applies both to simple and generalized variant; generalized insort_insert to insort_insert_key; additional lemmas
haftmann
parents:
40195
diff
changeset
|
5479 |
|
aee7ef725330
sorting: avoid _key suffix if lemma applies both to simple and generalized variant; generalized insort_insert to insort_insert_key; additional lemmas
haftmann
parents:
40195
diff
changeset
|
5480 |
lemma filter_insort_triv: |
37107 | 5481 |
"\<not> P x \<Longrightarrow> filter P (insort_key f x xs) = filter P xs" |
5482 |
by (induct xs) simp_all |
|
5483 |
||
40210
aee7ef725330
sorting: avoid _key suffix if lemma applies both to simple and generalized variant; generalized insort_insert to insort_insert_key; additional lemmas
haftmann
parents:
40195
diff
changeset
|
5484 |
lemma filter_insort: |
37107 | 5485 |
"sorted (map f xs) \<Longrightarrow> P x \<Longrightarrow> filter P (insort_key f x xs) = insort_key f x (filter P xs)" |
68109 | 5486 |
by (induct xs) (auto, subst insort_is_Cons, auto) |
37107 | 5487 |
|
40210
aee7ef725330
sorting: avoid _key suffix if lemma applies both to simple and generalized variant; generalized insort_insert to insort_insert_key; additional lemmas
haftmann
parents:
40195
diff
changeset
|
5488 |
lemma filter_sort: |
37107 | 5489 |
"filter P (sort_key f xs) = sort_key f (filter P xs)" |
40210
aee7ef725330
sorting: avoid _key suffix if lemma applies both to simple and generalized variant; generalized insort_insert to insort_insert_key; additional lemmas
haftmann
parents:
40195
diff
changeset
|
5490 |
by (induct xs) (simp_all add: filter_insort_triv filter_insort) |
37107 | 5491 |
|
5492 |
lemma remove1_insort [simp]: |
|
5493 |
"remove1 x (insort x xs) = xs" |
|
5494 |
by (induct xs) simp_all |
|
5495 |
||
24616 | 5496 |
end |
5497 |
||
68191 | 5498 |
lemma sort_upt [simp]: "sort [m..<n] = [m..<n]" |
5499 |
by (rule sorted_sort_id) simp |
|
5500 |
||
5501 |
lemma sort_upto [simp]: "sort [i..j] = [i..j]" |
|
5502 |
by (rule sorted_sort_id) simp |
|
32415
1dddf2f64266
got rid of complicated class finite_intvl_succ and defined "upto" directly on int, the only instance of the class.
nipkow
parents:
32078
diff
changeset
|
5503 |
|
52379 | 5504 |
lemma sorted_find_Min: |
67479 | 5505 |
"sorted xs \<Longrightarrow> \<exists>x \<in> set xs. P x \<Longrightarrow> List.find P xs = Some (Min {x\<in>set xs. P x})" |
5506 |
proof (induct xs) |
|
52379 | 5507 |
case Nil then show ?case by simp |
5508 |
next |
|
67479 | 5509 |
case (Cons x xs) show ?case proof (cases "P x") |
5510 |
case True |
|
68109 | 5511 |
with Cons show ?thesis by (auto intro: Min_eqI [symmetric]) |
52379 | 5512 |
next |
5513 |
case False then have "{y. (y = x \<or> y \<in> set xs) \<and> P y} = {y \<in> set xs. P y}" |
|
5514 |
by auto |
|
68109 | 5515 |
with Cons False show ?thesis by (simp_all) |
52379 | 5516 |
qed |
5517 |
qed |
|
5518 |
||
66853 | 5519 |
lemma sorted_enumerate [simp]: "sorted (map fst (enumerate n xs))" |
5520 |
by (simp add: enumerate_eq_zip) |
|
58437 | 5521 |
|
69593 | 5522 |
text \<open>Stability of \<^const>\<open>sort_key\<close>:\<close> |
67684 | 5523 |
|
68249
949d93804740
First step to remove nonstandard "[x <- xs. P]" syntax: only input
nipkow
parents:
68215
diff
changeset
|
5524 |
lemma sort_key_stable: "filter (\<lambda>y. f y = k) (sort_key f xs) = filter (\<lambda>y. f y = k) xs" |
70296 | 5525 |
by (induction xs) (auto simp: filter_insort insort_is_Cons filter_insort_triv) |
66654 | 5526 |
|
67684 | 5527 |
corollary stable_sort_key_sort_key: "stable_sort_key sort_key" |
5528 |
by(simp add: stable_sort_key_def sort_key_stable) |
|
5529 |
||
67606 | 5530 |
lemma sort_key_const: "sort_key (\<lambda>x. c) xs = xs" |
67684 | 5531 |
by (metis (mono_tags) filter_True sort_key_stable) |
67606 | 5532 |
|
35115 | 5533 |
|
69593 | 5534 |
subsubsection \<open>\<^const>\<open>transpose\<close> on sorted lists\<close> |
34933 | 5535 |
|
66853 | 5536 |
lemma sorted_transpose[simp]: "sorted (rev (map length (transpose xs)))" |
68175 | 5537 |
by (auto simp: sorted_iff_nth_mono rev_nth nth_transpose |
34933 | 5538 |
length_filter_conv_card intro: card_mono) |
5539 |
||
5540 |
lemma transpose_max_length: |
|
68249
949d93804740
First step to remove nonstandard "[x <- xs. P]" syntax: only input
nipkow
parents:
68215
diff
changeset
|
5541 |
"foldr (\<lambda>xs. max (length xs)) (transpose xs) 0 = length (filter (\<lambda>x. x \<noteq> []) xs)" |
34933 | 5542 |
(is "?L = ?R") |
5543 |
proof (cases "transpose xs = []") |
|
5544 |
case False |
|
5545 |
have "?L = foldr max (map length (transpose xs)) 0" |
|
5546 |
by (simp add: foldr_map comp_def) |
|
5547 |
also have "... = length (transpose xs ! 0)" |
|
5548 |
using False sorted_transpose by (simp add: foldr_max_sorted) |
|
5549 |
finally show ?thesis |
|
5550 |
using False by (simp add: nth_transpose) |
|
5551 |
next |
|
5552 |
case True |
|
68249
949d93804740
First step to remove nonstandard "[x <- xs. P]" syntax: only input
nipkow
parents:
68215
diff
changeset
|
5553 |
hence "filter (\<lambda>x. x \<noteq> []) xs = []" |
34933 | 5554 |
by (auto intro!: filter_False simp: transpose_empty) |
5555 |
thus ?thesis by (simp add: transpose_empty True) |
|
5556 |
qed |
|
5557 |
||
5558 |
lemma length_transpose_sorted: |
|
5559 |
fixes xs :: "'a list list" |
|
5560 |
assumes sorted: "sorted (rev (map length xs))" |
|
5561 |
shows "length (transpose xs) = (if xs = [] then 0 else length (xs ! 0))" |
|
5562 |
proof (cases "xs = []") |
|
5563 |
case False |
|
5564 |
thus ?thesis |
|
5565 |
using foldr_max_sorted[OF sorted] False |
|
5566 |
unfolding length_transpose foldr_map comp_def |
|
5567 |
by simp |
|
5568 |
qed simp |
|
5569 |
||
5570 |
lemma nth_nth_transpose_sorted[simp]: |
|
5571 |
fixes xs :: "'a list list" |
|
5572 |
assumes sorted: "sorted (rev (map length xs))" |
|
5573 |
and i: "i < length (transpose xs)" |
|
68249
949d93804740
First step to remove nonstandard "[x <- xs. P]" syntax: only input
nipkow
parents:
68215
diff
changeset
|
5574 |
and j: "j < length (filter (\<lambda>ys. i < length ys) xs)" |
34933 | 5575 |
shows "transpose xs ! i ! j = xs ! j ! i" |
66853 | 5576 |
using j filter_equals_takeWhile_sorted_rev[OF sorted, of i] |
34933 | 5577 |
nth_transpose[OF i] nth_map[OF j] |
66853 | 5578 |
by (simp add: takeWhile_nth) |
34933 | 5579 |
|
5580 |
lemma transpose_column_length: |
|
5581 |
fixes xs :: "'a list list" |
|
5582 |
assumes sorted: "sorted (rev (map length xs))" and "i < length xs" |
|
5583 |
shows "length (filter (\<lambda>ys. i < length ys) (transpose xs)) = length (xs ! i)" |
|
5584 |
proof - |
|
60758 | 5585 |
have "xs \<noteq> []" using \<open>i < length xs\<close> by auto |
34933 | 5586 |
note filter_equals_takeWhile_sorted_rev[OF sorted, simp] |
5587 |
{ fix j assume "j \<le> i" |
|
60758 | 5588 |
note sorted_rev_nth_mono[OF sorted, of j i, simplified, OF this \<open>i < length xs\<close>] |
34933 | 5589 |
} note sortedE = this[consumes 1] |
5590 |
||
5591 |
have "{j. j < length (transpose xs) \<and> i < length (transpose xs ! j)} |
|
5592 |
= {..< length (xs ! i)}" |
|
5593 |
proof safe |
|
5594 |
fix j |
|
5595 |
assume "j < length (transpose xs)" and "i < length (transpose xs ! j)" |
|
5596 |
with this(2) nth_transpose[OF this(1)] |
|
5597 |
have "i < length (takeWhile (\<lambda>ys. j < length ys) xs)" by simp |
|
5598 |
from nth_mem[OF this] takeWhile_nth[OF this] |
|
5599 |
show "j < length (xs ! i)" by (auto dest: set_takeWhileD) |
|
5600 |
next |
|
5601 |
fix j assume "j < length (xs ! i)" |
|
5602 |
thus "j < length (transpose xs)" |
|
60758 | 5603 |
using foldr_max_sorted[OF sorted] \<open>xs \<noteq> []\<close> sortedE[OF le0] |
34933 | 5604 |
by (auto simp: length_transpose comp_def foldr_map) |
5605 |
||
5606 |
have "Suc i \<le> length (takeWhile (\<lambda>ys. j < length ys) xs)" |
|
60758 | 5607 |
using \<open>i < length xs\<close> \<open>j < length (xs ! i)\<close> less_Suc_eq_le |
34933 | 5608 |
by (auto intro!: length_takeWhile_less_P_nth dest!: sortedE) |
60758 | 5609 |
with nth_transpose[OF \<open>j < length (transpose xs)\<close>] |
34933 | 5610 |
show "i < length (transpose xs ! j)" by simp |
5611 |
qed |
|
5612 |
thus ?thesis by (simp add: length_filter_conv_card) |
|
5613 |
qed |
|
5614 |
||
5615 |
lemma transpose_column: |
|
5616 |
fixes xs :: "'a list list" |
|
5617 |
assumes sorted: "sorted (rev (map length xs))" and "i < length xs" |
|
5618 |
shows "map (\<lambda>ys. ys ! i) (filter (\<lambda>ys. i < length ys) (transpose xs)) |
|
5619 |
= xs ! i" (is "?R = _") |
|
68975
5ce4d117cea7
A few new results, elimination of duplicates and more use of "pairwise"
paulson <lp15@cam.ac.uk>
parents:
68781
diff
changeset
|
5620 |
proof (rule nth_equalityI) |
34933 | 5621 |
show length: "length ?R = length (xs ! i)" |
5622 |
using transpose_column_length[OF assms] by simp |
|
5623 |
||
5624 |
fix j assume j: "j < length ?R" |
|
5625 |
note * = less_le_trans[OF this, unfolded length_map, OF length_filter_le] |
|
5626 |
from j have j_less: "j < length (xs ! i)" using length by simp |
|
5627 |
have i_less_tW: "Suc i \<le> length (takeWhile (\<lambda>ys. Suc j \<le> length ys) xs)" |
|
5628 |
proof (rule length_takeWhile_less_P_nth) |
|
60758 | 5629 |
show "Suc i \<le> length xs" using \<open>i < length xs\<close> by simp |
34933 | 5630 |
fix k assume "k < Suc i" |
5631 |
hence "k \<le> i" by auto |
|
60758 | 5632 |
with sorted_rev_nth_mono[OF sorted this] \<open>i < length xs\<close> |
34933 | 5633 |
have "length (xs ! i) \<le> length (xs ! k)" by simp |
5634 |
thus "Suc j \<le> length (xs ! k)" using j_less by simp |
|
5635 |
qed |
|
68249
949d93804740
First step to remove nonstandard "[x <- xs. P]" syntax: only input
nipkow
parents:
68215
diff
changeset
|
5636 |
have i_less_filter: "i < length (filter (\<lambda>ys. j < length ys) xs) " |
34933 | 5637 |
unfolding filter_equals_takeWhile_sorted_rev[OF sorted, of j] |
5638 |
using i_less_tW by (simp_all add: Suc_le_eq) |
|
5639 |
from j show "?R ! j = xs ! i ! j" |
|
5640 |
unfolding filter_equals_takeWhile_sorted_rev[OF sorted_transpose, of i] |
|
5641 |
by (simp add: takeWhile_nth nth_nth_transpose_sorted[OF sorted * i_less_filter]) |
|
5642 |
qed |
|
5643 |
||
5644 |
lemma transpose_transpose: |
|
5645 |
fixes xs :: "'a list list" |
|
5646 |
assumes sorted: "sorted (rev (map length xs))" |
|
5647 |
shows "transpose (transpose xs) = takeWhile (\<lambda>x. x \<noteq> []) xs" (is "?L = ?R") |
|
5648 |
proof - |
|
5649 |
have len: "length ?L = length ?R" |
|
5650 |
unfolding length_transpose transpose_max_length |
|
5651 |
using filter_equals_takeWhile_sorted_rev[OF sorted, of 0] |
|
5652 |
by simp |
|
5653 |
||
5654 |
{ fix i assume "i < length ?R" |
|
5655 |
with less_le_trans[OF _ length_takeWhile_le[of _ xs]] |
|
5656 |
have "i < length xs" by simp |
|
5657 |
} note * = this |
|
5658 |
show ?thesis |
|
5659 |
by (rule nth_equalityI) |
|
5660 |
(simp_all add: len nth_transpose transpose_column[OF sorted] * takeWhile_nth) |
|
5661 |
qed |
|
24616 | 5662 |
|
34934
440605046777
Added transpose_rectangle, when the input list is rectangular.
hoelzl
parents:
34933
diff
changeset
|
5663 |
theorem transpose_rectangle: |
440605046777
Added transpose_rectangle, when the input list is rectangular.
hoelzl
parents:
34933
diff
changeset
|
5664 |
assumes "xs = [] \<Longrightarrow> n = 0" |
440605046777
Added transpose_rectangle, when the input list is rectangular.
hoelzl
parents:
34933
diff
changeset
|
5665 |
assumes rect: "\<And> i. i < length xs \<Longrightarrow> length (xs ! i) = n" |
440605046777
Added transpose_rectangle, when the input list is rectangular.
hoelzl
parents:
34933
diff
changeset
|
5666 |
shows "transpose xs = map (\<lambda> i. map (\<lambda> j. xs ! j ! i) [0..<length xs]) [0..<n]" |
440605046777
Added transpose_rectangle, when the input list is rectangular.
hoelzl
parents:
34933
diff
changeset
|
5667 |
(is "?trans = ?map") |
440605046777
Added transpose_rectangle, when the input list is rectangular.
hoelzl
parents:
34933
diff
changeset
|
5668 |
proof (rule nth_equalityI) |
440605046777
Added transpose_rectangle, when the input list is rectangular.
hoelzl
parents:
34933
diff
changeset
|
5669 |
have "sorted (rev (map length xs))" |
68175 | 5670 |
by (auto simp: rev_nth rect sorted_iff_nth_mono) |
34934
440605046777
Added transpose_rectangle, when the input list is rectangular.
hoelzl
parents:
34933
diff
changeset
|
5671 |
from foldr_max_sorted[OF this] assms |
440605046777
Added transpose_rectangle, when the input list is rectangular.
hoelzl
parents:
34933
diff
changeset
|
5672 |
show len: "length ?trans = length ?map" |
440605046777
Added transpose_rectangle, when the input list is rectangular.
hoelzl
parents:
34933
diff
changeset
|
5673 |
by (simp_all add: length_transpose foldr_map comp_def) |
440605046777
Added transpose_rectangle, when the input list is rectangular.
hoelzl
parents:
34933
diff
changeset
|
5674 |
moreover |
68249
949d93804740
First step to remove nonstandard "[x <- xs. P]" syntax: only input
nipkow
parents:
68215
diff
changeset
|
5675 |
{ fix i assume "i < n" hence "filter (\<lambda>ys. i < length ys) xs = xs" |
34934
440605046777
Added transpose_rectangle, when the input list is rectangular.
hoelzl
parents:
34933
diff
changeset
|
5676 |
using rect by (auto simp: in_set_conv_nth intro!: filter_True) } |
68975
5ce4d117cea7
A few new results, elimination of duplicates and more use of "pairwise"
paulson <lp15@cam.ac.uk>
parents:
68781
diff
changeset
|
5677 |
ultimately show "\<And>i. i < length (transpose xs) \<Longrightarrow> ?trans ! i = ?map ! i" |
34934
440605046777
Added transpose_rectangle, when the input list is rectangular.
hoelzl
parents:
34933
diff
changeset
|
5678 |
by (auto simp: nth_transpose intro: nth_equalityI) |
440605046777
Added transpose_rectangle, when the input list is rectangular.
hoelzl
parents:
34933
diff
changeset
|
5679 |
qed |
24616 | 5680 |
|
35115 | 5681 |
|
61799 | 5682 |
subsubsection \<open>\<open>sorted_list_of_set\<close>\<close> |
60758 | 5683 |
|
5684 |
text\<open>This function maps (finite) linearly ordered sets to sorted |
|
25069 | 5685 |
lists. Warning: in most cases it is not a good idea to convert from |
5686 |
sets to lists but one should convert in the other direction (via |
|
69593 | 5687 |
\<^const>\<open>set\<close>).\<close> |
60758 | 5688 |
|
54868 | 5689 |
context linorder |
5690 |
begin |
|
5691 |
||
5692 |
definition sorted_list_of_set :: "'a set \<Rightarrow> 'a list" where |
|
51489 | 5693 |
"sorted_list_of_set = folding.F insort []" |
5694 |
||
61605 | 5695 |
sublocale sorted_list_of_set: folding insort Nil |
61566
c3d6e570ccef
Keyword 'rewrites' identifies rewrite morphisms.
ballarin
parents:
61468
diff
changeset
|
5696 |
rewrites |
51489 | 5697 |
"folding.F insort [] = sorted_list_of_set" |
5698 |
proof - |
|
5699 |
interpret comp_fun_commute insort by (fact comp_fun_commute_insort) |
|
61169 | 5700 |
show "folding insort" by standard (fact comp_fun_commute) |
51489 | 5701 |
show "folding.F insort [] = sorted_list_of_set" by (simp only: sorted_list_of_set_def) |
5702 |
qed |
|
5703 |
||
5704 |
lemma sorted_list_of_set_empty: |
|
35195 | 5705 |
"sorted_list_of_set {} = []" |
66853 | 5706 |
by (fact sorted_list_of_set.empty) |
35195 | 5707 |
|
5708 |
lemma sorted_list_of_set_insert [simp]: |
|
54868 | 5709 |
"finite A \<Longrightarrow> sorted_list_of_set (insert x A) = insort x (sorted_list_of_set (A - {x}))" |
66853 | 5710 |
by (fact sorted_list_of_set.insert_remove) |
35195 | 5711 |
|
52122 | 5712 |
lemma sorted_list_of_set_eq_Nil_iff [simp]: |
5713 |
"finite A \<Longrightarrow> sorted_list_of_set A = [] \<longleftrightarrow> A = {}" |
|
66853 | 5714 |
by (auto simp: sorted_list_of_set.remove) |
52122 | 5715 |
|
68083 | 5716 |
lemma set_sorted_list_of_set [simp]: |
5717 |
"finite A \<Longrightarrow> set (sorted_list_of_set A) = A" |
|
5718 |
by(induct A rule: finite_induct) (simp_all add: set_insort_key) |
|
5719 |
||
5720 |
lemma sorted_sorted_list_of_set [simp]: "sorted (sorted_list_of_set A)" |
|
5721 |
proof (cases "finite A") |
|
5722 |
case True thus ?thesis by(induction A) (simp_all add: sorted_insort) |
|
5723 |
next |
|
5724 |
case False thus ?thesis by simp |
|
5725 |
qed |
|
5726 |
||
5727 |
lemma distinct_sorted_list_of_set [simp]: "distinct (sorted_list_of_set A)" |
|
5728 |
proof (cases "finite A") |
|
5729 |
case True thus ?thesis by(induction A) (simp_all add: distinct_insort) |
|
5730 |
next |
|
5731 |
case False thus ?thesis by simp |
|
5732 |
qed |
|
52122 | 5733 |
|
68085 | 5734 |
lemmas sorted_list_of_set = set_sorted_list_of_set sorted_sorted_list_of_set distinct_sorted_list_of_set |
5735 |
||
47841 | 5736 |
lemma sorted_list_of_set_sort_remdups [code]: |
35195 | 5737 |
"sorted_list_of_set (set xs) = sort (remdups xs)" |
5738 |
proof - |
|
42871
1c0b99f950d9
names of fold_set locales resemble name of characteristic property more closely
haftmann
parents:
42809
diff
changeset
|
5739 |
interpret comp_fun_commute insort by (fact comp_fun_commute_insort) |
51489 | 5740 |
show ?thesis by (simp add: sorted_list_of_set.eq_fold sort_conv_fold fold_set_fold_remdups) |
35195 | 5741 |
qed |
25069 | 5742 |
|
37107 | 5743 |
lemma sorted_list_of_set_remove: |
5744 |
assumes "finite A" |
|
5745 |
shows "sorted_list_of_set (A - {x}) = remove1 x (sorted_list_of_set A)" |
|
5746 |
proof (cases "x \<in> A") |
|
5747 |
case False with assms have "x \<notin> set (sorted_list_of_set A)" by simp |
|
5748 |
with False show ?thesis by (simp add: remove1_idem) |
|
5749 |
next |
|
5750 |
case True then obtain B where A: "A = insert x B" by (rule Set.set_insert) |
|
5751 |
with assms show ?thesis by simp |
|
5752 |
qed |
|
5753 |
||
25069 | 5754 |
end |
5755 |
||
37107 | 5756 |
lemma sorted_list_of_set_range [simp]: |
5757 |
"sorted_list_of_set {m..<n} = [m..<n]" |
|
66853 | 5758 |
by (rule sorted_distinct_set_unique) simp_all |
37107 | 5759 |
|
5760 |
||
61799 | 5761 |
subsubsection \<open>\<open>lists\<close>: the list-forming operator over sets\<close> |
15302 | 5762 |
|
23740 | 5763 |
inductive_set |
22262 | 5764 |
lists :: "'a set => 'a list set" |
23740 | 5765 |
for A :: "'a set" |
5766 |
where |
|
67613 | 5767 |
Nil [intro!, simp]: "[] \<in> lists A" |
5768 |
| Cons [intro!, simp]: "\<lbrakk>a \<in> A; l \<in> lists A\<rbrakk> \<Longrightarrow> a#l \<in> lists A" |
|
5769 |
||
5770 |
inductive_cases listsE [elim!]: "x#l \<in> lists A" |
|
54147
97a8ff4e4ac9
killed most "no_atp", to make Sledgehammer more complete
blanchet
parents:
53954
diff
changeset
|
5771 |
inductive_cases listspE [elim!]: "listsp A (x # l)" |
23740 | 5772 |
|
46313 | 5773 |
inductive_simps listsp_simps[code]: |
5774 |
"listsp A []" |
|
5775 |
"listsp A (x # xs)" |
|
5776 |
||
23740 | 5777 |
lemma listsp_mono [mono]: "A \<le> B ==> listsp A \<le> listsp B" |
46884 | 5778 |
by (rule predicate1I, erule listsp.induct, blast+) |
26795
a27607030a1c
- Explicitely applied predicate1I in a few proofs, because it is no longer
berghofe
parents:
26771
diff
changeset
|
5779 |
|
46176
1898e61e89c4
pred_subset/equals_eq are now standard pred_set_conv rules
berghofe
parents:
46156
diff
changeset
|
5780 |
lemmas lists_mono = listsp_mono [to_set] |
22262 | 5781 |
|
22422
ee19cdb07528
stepping towards uniform lattice theory development in HOL
haftmann
parents:
22262
diff
changeset
|
5782 |
lemma listsp_infI: |
ee19cdb07528
stepping towards uniform lattice theory development in HOL
haftmann
parents:
22262
diff
changeset
|
5783 |
assumes l: "listsp A l" shows "listsp B l ==> listsp (inf A B) l" using l |
24349 | 5784 |
by induct blast+ |
15302 | 5785 |
|
22422
ee19cdb07528
stepping towards uniform lattice theory development in HOL
haftmann
parents:
22262
diff
changeset
|
5786 |
lemmas lists_IntI = listsp_infI [to_set] |
ee19cdb07528
stepping towards uniform lattice theory development in HOL
haftmann
parents:
22262
diff
changeset
|
5787 |
|
ee19cdb07528
stepping towards uniform lattice theory development in HOL
haftmann
parents:
22262
diff
changeset
|
5788 |
lemma listsp_inf_eq [simp]: "listsp (inf A B) = inf (listsp A) (listsp B)" |
ee19cdb07528
stepping towards uniform lattice theory development in HOL
haftmann
parents:
22262
diff
changeset
|
5789 |
proof (rule mono_inf [where f=listsp, THEN order_antisym]) |
22262 | 5790 |
show "mono listsp" by (simp add: mono_def listsp_mono) |
47397
d654c73e4b12
no preference wrt. fold(l/r); prefer fold rather than foldr for iterating over lists in generated code
haftmann
parents:
47131
diff
changeset
|
5791 |
show "inf (listsp A) (listsp B) \<le> listsp (inf A B)" by (blast intro!: listsp_infI) |
14388 | 5792 |
qed |
5793 |
||
41075
4bed56dc95fb
primitive definitions of bot/top/inf/sup for bool and fun are named with canonical suffix `_def` rather than `_eq`
haftmann
parents:
40968
diff
changeset
|
5794 |
lemmas listsp_conj_eq [simp] = listsp_inf_eq [simplified inf_fun_def inf_bool_def] |
22422
ee19cdb07528
stepping towards uniform lattice theory development in HOL
haftmann
parents:
22262
diff
changeset
|
5795 |
|
46176
1898e61e89c4
pred_subset/equals_eq are now standard pred_set_conv rules
berghofe
parents:
46156
diff
changeset
|
5796 |
lemmas lists_Int_eq [simp] = listsp_inf_eq [to_set] |
22262 | 5797 |
|
67613 | 5798 |
lemma Cons_in_lists_iff[simp]: "x#xs \<in> lists A \<longleftrightarrow> x \<in> A \<and> xs \<in> lists A" |
39613 | 5799 |
by auto |
5800 |
||
22262 | 5801 |
lemma append_in_listsp_conv [iff]: |
5802 |
"(listsp A (xs @ ys)) = (listsp A xs \<and> listsp A ys)" |
|
15302 | 5803 |
by (induct xs) auto |
5804 |
||
22262 | 5805 |
lemmas append_in_lists_conv [iff] = append_in_listsp_conv [to_set] |
5806 |
||
5807 |
lemma in_listsp_conv_set: "(listsp A xs) = (\<forall>x \<in> set xs. A x)" |
|
61799 | 5808 |
\<comment> \<open>eliminate \<open>listsp\<close> in favour of \<open>set\<close>\<close> |
15302 | 5809 |
by (induct xs) auto |
5810 |
||
46313 | 5811 |
lemmas in_lists_conv_set [code_unfold] = in_listsp_conv_set [to_set] |
22262 | 5812 |
|
54147
97a8ff4e4ac9
killed most "no_atp", to make Sledgehammer more complete
blanchet
parents:
53954
diff
changeset
|
5813 |
lemma in_listspD [dest!]: "listsp A xs ==> \<forall>x\<in>set xs. A x" |
22262 | 5814 |
by (rule in_listsp_conv_set [THEN iffD1]) |
5815 |
||
54147
97a8ff4e4ac9
killed most "no_atp", to make Sledgehammer more complete
blanchet
parents:
53954
diff
changeset
|
5816 |
lemmas in_listsD [dest!] = in_listspD [to_set] |
97a8ff4e4ac9
killed most "no_atp", to make Sledgehammer more complete
blanchet
parents:
53954
diff
changeset
|
5817 |
|
97a8ff4e4ac9
killed most "no_atp", to make Sledgehammer more complete
blanchet
parents:
53954
diff
changeset
|
5818 |
lemma in_listspI [intro!]: "\<forall>x\<in>set xs. A x ==> listsp A xs" |
22262 | 5819 |
by (rule in_listsp_conv_set [THEN iffD2]) |
5820 |
||
54147
97a8ff4e4ac9
killed most "no_atp", to make Sledgehammer more complete
blanchet
parents:
53954
diff
changeset
|
5821 |
lemmas in_listsI [intro!] = in_listspI [to_set] |
15302 | 5822 |
|
68709 | 5823 |
lemma lists_eq_set: "lists A = {xs. set xs \<le> A}" |
39597 | 5824 |
by auto |
5825 |
||
39613 | 5826 |
lemma lists_empty [simp]: "lists {} = {[]}" |
5827 |
by auto |
|
5828 |
||
15302 | 5829 |
lemma lists_UNIV [simp]: "lists UNIV = UNIV" |
5830 |
by auto |
|
5831 |
||
50134 | 5832 |
lemma lists_image: "lists (f`A) = map f ` lists A" |
5833 |
proof - |
|
5834 |
{ fix xs have "\<forall>x\<in>set xs. x \<in> f ` A \<Longrightarrow> xs \<in> map f ` lists A" |
|
55465 | 5835 |
by (induct xs) (auto simp del: list.map simp add: list.map[symmetric] intro!: imageI) } |
50134 | 5836 |
then show ?thesis by auto |
5837 |
qed |
|
17086 | 5838 |
|
60758 | 5839 |
subsubsection \<open>Inductive definition for membership\<close> |
17086 | 5840 |
|
23740 | 5841 |
inductive ListMem :: "'a \<Rightarrow> 'a list \<Rightarrow> bool" |
22262 | 5842 |
where |
5843 |
elem: "ListMem x (x # xs)" |
|
5844 |
| insert: "ListMem x xs \<Longrightarrow> ListMem x (y # xs)" |
|
5845 |
||
5846 |
lemma ListMem_iff: "(ListMem x xs) = (x \<in> set xs)" |
|
68719 | 5847 |
proof |
5848 |
show "ListMem x xs \<Longrightarrow> x \<in> set xs" |
|
5849 |
by (induct set: ListMem) auto |
|
5850 |
show "x \<in> set xs \<Longrightarrow> ListMem x xs" |
|
5851 |
by (induct xs) (auto intro: ListMem.intros) |
|
5852 |
qed |
|
17086 | 5853 |
|
5854 |
||
60758 | 5855 |
subsubsection \<open>Lists as Cartesian products\<close> |
5856 |
||
61799 | 5857 |
text\<open>\<open>set_Cons A Xs\<close>: the set of lists with head drawn from |
69593 | 5858 |
\<^term>\<open>A\<close> and tail drawn from \<^term>\<open>Xs\<close>.\<close> |
15302 | 5859 |
|
50548 | 5860 |
definition set_Cons :: "'a set \<Rightarrow> 'a list set \<Rightarrow> 'a list set" where |
5861 |
"set_Cons A XS = {z. \<exists>x xs. z = x # xs \<and> x \<in> A \<and> xs \<in> XS}" |
|
15302 | 5862 |
|
17724 | 5863 |
lemma set_Cons_sing_Nil [simp]: "set_Cons A {[]} = (%x. [x])`A" |
15302 | 5864 |
by (auto simp add: set_Cons_def) |
5865 |
||
60758 | 5866 |
text\<open>Yields the set of lists, all of the same length as the argument and |
5867 |
with elements drawn from the corresponding element of the argument.\<close> |
|
15302 | 5868 |
|
50548 | 5869 |
primrec listset :: "'a set list \<Rightarrow> 'a list set" where |
5870 |
"listset [] = {[]}" | |
|
5871 |
"listset (A # As) = set_Cons A (listset As)" |
|
15302 | 5872 |
|
5873 |
||
60758 | 5874 |
subsection \<open>Relations on Lists\<close> |
5875 |
||
5876 |
subsubsection \<open>Length Lexicographic Ordering\<close> |
|
5877 |
||
64963 | 5878 |
text\<open>These orderings preserve well-foundedness: shorter lists |
60758 | 5879 |
precede longer lists. These ordering are not used in dictionaries.\<close> |
64963 | 5880 |
|
61799 | 5881 |
primrec \<comment> \<open>The lexicographic ordering for lists of the specified length\<close> |
34941 | 5882 |
lexn :: "('a \<times> 'a) set \<Rightarrow> nat \<Rightarrow> ('a list \<times> 'a list) set" where |
50548 | 5883 |
"lexn r 0 = {}" | |
5884 |
"lexn r (Suc n) = |
|
55932 | 5885 |
(map_prod (%(x, xs). x#xs) (%(x, xs). x#xs) ` (r <*lex*> lexn r n)) Int |
50548 | 5886 |
{(xs, ys). length xs = Suc n \<and> length ys = Suc n}" |
5887 |
||
5888 |
definition lex :: "('a \<times> 'a) set \<Rightarrow> ('a list \<times> 'a list) set" where |
|
61799 | 5889 |
"lex r = (\<Union>n. lexn r n)" \<comment> \<open>Holds only between lists of the same length\<close> |
50548 | 5890 |
|
5891 |
definition lenlex :: "('a \<times> 'a) set => ('a list \<times> 'a list) set" where |
|
5892 |
"lenlex r = inv_image (less_than <*lex*> lex r) (\<lambda>xs. (length xs, xs))" |
|
61799 | 5893 |
\<comment> \<open>Compares lists by their length and then lexicographically\<close> |
15302 | 5894 |
|
68719 | 5895 |
lemma wf_lexn: assumes "wf r" shows "wf (lexn r n)" |
5896 |
proof (induct n) |
|
5897 |
case (Suc n) |
|
5898 |
have inj: "inj (\<lambda>(x, xs). x # xs)" |
|
5899 |
using assms by (auto simp: inj_on_def) |
|
5900 |
have wf: "wf (map_prod (\<lambda>(x, xs). x # xs) (\<lambda>(x, xs). x # xs) ` (r <*lex*> lexn r n))" |
|
5901 |
by (simp add: Suc.hyps assms wf_lex_prod wf_map_prod_image [OF _ inj]) |
|
5902 |
then show ?case |
|
5903 |
by (rule wf_subset) auto |
|
5904 |
qed auto |
|
15302 | 5905 |
|
5906 |
lemma lexn_length: |
|
67613 | 5907 |
"(xs, ys) \<in> lexn r n \<Longrightarrow> length xs = n \<and> length ys = n" |
24526 | 5908 |
by (induct n arbitrary: xs ys) auto |
15302 | 5909 |
|
5910 |
lemma wf_lex [intro!]: "wf r ==> wf (lex r)" |
|
68719 | 5911 |
unfolding lex_def |
68646 | 5912 |
apply (rule wf_UN) |
5913 |
apply (simp add: wf_lexn) |
|
5914 |
apply (metis DomainE Int_emptyI RangeE lexn_length) |
|
5915 |
done |
|
15302 | 5916 |
|
5917 |
lemma lexn_conv: |
|
15656 | 5918 |
"lexn r n = |
5919 |
{(xs,ys). length xs = n \<and> length ys = n \<and> |
|
67613 | 5920 |
(\<exists>xys x y xs' ys'. xs= xys @ x#xs' \<and> ys= xys @ y # ys' \<and> (x, y) \<in> r)}" |
18423 | 5921 |
apply (induct n, simp) |
15302 | 5922 |
apply (simp add: image_Collect lex_prod_def, safe, blast) |
5923 |
apply (rule_tac x = "ab # xys" in exI, simp) |
|
5924 |
apply (case_tac xys, simp_all, blast) |
|
5925 |
done |
|
5926 |
||
62175 | 5927 |
text\<open>By Mathias Fleury:\<close> |
62090 | 5928 |
lemma lexn_transI: |
5929 |
assumes "trans r" shows "trans (lexn r n)" |
|
5930 |
unfolding trans_def |
|
5931 |
proof (intro allI impI) |
|
5932 |
fix as bs cs |
|
5933 |
assume asbs: "(as, bs) \<in> lexn r n" and bscs: "(bs, cs) \<in> lexn r n" |
|
5934 |
obtain abs a b as' bs' where |
|
5935 |
n: "length as = n" and "length bs = n" and |
|
5936 |
as: "as = abs @ a # as'" and |
|
5937 |
bs: "bs = abs @ b # bs'" and |
|
5938 |
abr: "(a, b) \<in> r" |
|
5939 |
using asbs unfolding lexn_conv by blast |
|
5940 |
obtain bcs b' c' cs' bs' where |
|
5941 |
n': "length cs = n" and "length bs = n" and |
|
5942 |
bs': "bs = bcs @ b' # bs'" and |
|
5943 |
cs: "cs = bcs @ c' # cs'" and |
|
5944 |
b'c'r: "(b', c') \<in> r" |
|
5945 |
using bscs unfolding lexn_conv by blast |
|
5946 |
consider (le) "length bcs < length abs" |
|
5947 |
| (eq) "length bcs = length abs" |
|
5948 |
| (ge) "length bcs > length abs" by linarith |
|
5949 |
thus "(as, cs) \<in> lexn r n" |
|
5950 |
proof cases |
|
5951 |
let ?k = "length bcs" |
|
5952 |
case le |
|
5953 |
hence "as ! ?k = bs ! ?k" unfolding as bs by (simp add: nth_append) |
|
5954 |
hence "(as ! ?k, cs ! ?k) \<in> r" using b'c'r unfolding bs' cs by auto |
|
5955 |
moreover |
|
5956 |
have "length bcs < length as" using le unfolding as by simp |
|
5957 |
from id_take_nth_drop[OF this] |
|
5958 |
have "as = take ?k as @ as ! ?k # drop (Suc ?k) as" . |
|
5959 |
moreover |
|
5960 |
have "length bcs < length cs" unfolding cs by simp |
|
5961 |
from id_take_nth_drop[OF this] |
|
5962 |
have "cs = take ?k cs @ cs ! ?k # drop (Suc ?k) cs" . |
|
5963 |
moreover have "take ?k as = take ?k cs" |
|
5964 |
using le arg_cong[OF bs, of "take (length bcs)"] |
|
5965 |
unfolding cs as bs' by auto |
|
5966 |
ultimately show ?thesis using n n' unfolding lexn_conv by auto |
|
5967 |
next |
|
5968 |
let ?k = "length abs" |
|
5969 |
case ge |
|
5970 |
hence "bs ! ?k = cs ! ?k" unfolding bs' cs by (simp add: nth_append) |
|
5971 |
hence "(as ! ?k, cs ! ?k) \<in> r" using abr unfolding as bs by auto |
|
5972 |
moreover |
|
5973 |
have "length abs < length as" using ge unfolding as by simp |
|
5974 |
from id_take_nth_drop[OF this] |
|
5975 |
have "as = take ?k as @ as ! ?k # drop (Suc ?k) as" . |
|
5976 |
moreover have "length abs < length cs" using n n' unfolding as by simp |
|
5977 |
from id_take_nth_drop[OF this] |
|
5978 |
have "cs = take ?k cs @ cs ! ?k # drop (Suc ?k) cs" . |
|
5979 |
moreover have "take ?k as = take ?k cs" |
|
5980 |
using ge arg_cong[OF bs', of "take (length abs)"] |
|
5981 |
unfolding cs as bs by auto |
|
5982 |
ultimately show ?thesis using n n' unfolding lexn_conv by auto |
|
5983 |
next |
|
5984 |
let ?k = "length abs" |
|
5985 |
case eq |
|
63540 | 5986 |
hence *: "abs = bcs" "b = b'" using bs bs' by auto |
5987 |
hence "(a, c') \<in> r" |
|
62090 | 5988 |
using abr b'c'r assms unfolding trans_def by blast |
63540 | 5989 |
with * show ?thesis using n n' unfolding lexn_conv as bs cs by auto |
62090 | 5990 |
qed |
5991 |
qed |
|
5992 |
||
15302 | 5993 |
lemma lex_conv: |
15656 | 5994 |
"lex r = |
5995 |
{(xs,ys). length xs = length ys \<and> |
|
67613 | 5996 |
(\<exists>xys x y xs' ys'. xs = xys @ x # xs' \<and> ys = xys @ y # ys' \<and> (x, y) \<in> r)}" |
15302 | 5997 |
by (force simp add: lex_def lexn_conv) |
5998 |
||
15693 | 5999 |
lemma wf_lenlex [intro!]: "wf r ==> wf (lenlex r)" |
6000 |
by (unfold lenlex_def) blast |
|
6001 |
||
6002 |
lemma lenlex_conv: |
|
67091 | 6003 |
"lenlex r = {(xs,ys). length xs < length ys \<or> |
6004 |
length xs = length ys \<and> (xs, ys) \<in> lex r}" |
|
30198 | 6005 |
by (simp add: lenlex_def Id_on_def lex_prod_def inv_image_def) |
15302 | 6006 |
|
6007 |
lemma Nil_notin_lex [iff]: "([], ys) \<notin> lex r" |
|
6008 |
by (simp add: lex_conv) |
|
6009 |
||
6010 |
lemma Nil2_notin_lex [iff]: "(xs, []) \<notin> lex r" |
|
6011 |
by (simp add:lex_conv) |
|
6012 |
||
18447 | 6013 |
lemma Cons_in_lex [simp]: |
68719 | 6014 |
"((x # xs, y # ys) \<in> lex r) = |
67091 | 6015 |
((x, y) \<in> r \<and> length xs = length ys \<or> x = y \<and> (xs, ys) \<in> lex r)" |
68719 | 6016 |
apply (simp add: lex_conv) |
6017 |
apply (rule iffI) |
|
6018 |
prefer 2 apply (blast intro: Cons_eq_appendI, clarify) |
|
6019 |
by (metis hd_append append_Nil list.sel(1) list.sel(3) tl_append2) |
|
66502 | 6020 |
|
6021 |
lemma lex_append_rightI: |
|
6022 |
"(xs, ys) \<in> lex r \<Longrightarrow> length vs = length us \<Longrightarrow> (xs @ us, ys @ vs) \<in> lex r" |
|
6023 |
by (fastforce simp: lex_def lexn_conv) |
|
6024 |
||
6025 |
lemma lex_append_leftI: |
|
6026 |
"(ys, zs) \<in> lex r \<Longrightarrow> (xs @ ys, xs @ zs) \<in> lex r" |
|
6027 |
by (induct xs) auto |
|
6028 |
||
6029 |
lemma lex_append_leftD: |
|
6030 |
"\<forall>x. (x,x) \<notin> r \<Longrightarrow> (xs @ ys, xs @ zs) \<in> lex r \<Longrightarrow> (ys, zs) \<in> lex r" |
|
6031 |
by (induct xs) auto |
|
6032 |
||
6033 |
lemma lex_append_left_iff: |
|
6034 |
"\<forall>x. (x,x) \<notin> r \<Longrightarrow> (xs @ ys, xs @ zs) \<in> lex r \<longleftrightarrow> (ys, zs) \<in> lex r" |
|
6035 |
by(metis lex_append_leftD lex_append_leftI) |
|
6036 |
||
6037 |
lemma lex_take_index: |
|
6038 |
assumes "(xs, ys) \<in> lex r" |
|
6039 |
obtains i where "i < length xs" and "i < length ys" and "take i xs = |
|
6040 |
take i ys" |
|
6041 |
and "(xs ! i, ys ! i) \<in> r" |
|
6042 |
proof - |
|
6043 |
obtain n us x xs' y ys' where "(xs, ys) \<in> lexn r n" and "length xs = n" and "length ys = n" |
|
6044 |
and "xs = us @ x # xs'" and "ys = us @ y # ys'" and "(x, y) \<in> r" |
|
6045 |
using assms by (fastforce simp: lex_def lexn_conv) |
|
6046 |
then show ?thesis by (intro that [of "length us"]) auto |
|
6047 |
qed |
|
15302 | 6048 |
|
6049 |
||
60758 | 6050 |
subsubsection \<open>Lexicographic Ordering\<close> |
6051 |
||
6052 |
text \<open>Classical lexicographic ordering on lists, ie. "a" < "ab" < "b". |
|
15656 | 6053 |
This ordering does \emph{not} preserve well-foundedness. |
64963 | 6054 |
Author: N. Voelker, March 2005.\<close> |
15656 | 6055 |
|
50548 | 6056 |
definition lexord :: "('a \<times> 'a) set \<Rightarrow> ('a list \<times> 'a list) set" where |
6057 |
"lexord r = {(x,y). \<exists> a v. y = x @ a # v \<or> |
|
15656 | 6058 |
(\<exists> u a b v w. (a,b) \<in> r \<and> x = u @ (a # v) \<and> y = u @ (b # w))}" |
6059 |
||
6060 |
lemma lexord_Nil_left[simp]: "([],y) \<in> lexord r = (\<exists> a x. y = a # x)" |
|
64963 | 6061 |
by (unfold lexord_def, induct_tac y, auto) |
15656 | 6062 |
|
6063 |
lemma lexord_Nil_right[simp]: "(x,[]) \<notin> lexord r" |
|
24349 | 6064 |
by (unfold lexord_def, induct_tac x, auto) |
15656 | 6065 |
|
6066 |
lemma lexord_cons_cons[simp]: |
|
68719 | 6067 |
"((a # x, b # y) \<in> lexord r) = ((a,b)\<in> r \<or> (a = b \<and> (x,y)\<in> lexord r))" |
6068 |
unfolding lexord_def |
|
6069 |
apply (safe, simp_all) |
|
6070 |
apply (metis hd_append list.sel(1)) |
|
6071 |
apply (metis hd_append list.sel(1) list.sel(3) tl_append2) |
|
6072 |
apply blast |
|
6073 |
by (meson Cons_eq_appendI) |
|
15656 | 6074 |
|
6075 |
lemmas lexord_simps = lexord_Nil_left lexord_Nil_right lexord_cons_cons |
|
6076 |
||
6077 |
lemma lexord_append_rightI: "\<exists> b z. y = b # z \<Longrightarrow> (x, x @ y) \<in> lexord r" |
|
64963 | 6078 |
by (induct_tac x, auto) |
15656 | 6079 |
|
6080 |
lemma lexord_append_left_rightI: |
|
6081 |
"(a,b) \<in> r \<Longrightarrow> (u @ a # x, u @ b # y) \<in> lexord r" |
|
24349 | 6082 |
by (induct_tac u, auto) |
15656 | 6083 |
|
6084 |
lemma lexord_append_leftI: " (u,v) \<in> lexord r \<Longrightarrow> (x @ u, x @ v) \<in> lexord r" |
|
24349 | 6085 |
by (induct x, auto) |
15656 | 6086 |
|
6087 |
lemma lexord_append_leftD: |
|
67091 | 6088 |
"\<lbrakk> (x @ u, x @ v) \<in> lexord r; (\<forall>a. (a,a) \<notin> r) \<rbrakk> \<Longrightarrow> (u,v) \<in> lexord r" |
24349 | 6089 |
by (erule rev_mp, induct_tac x, auto) |
15656 | 6090 |
|
64963 | 6091 |
lemma lexord_take_index_conv: |
67091 | 6092 |
"((x,y) \<in> lexord r) = |
64963 | 6093 |
((length x < length y \<and> take (length x) y = x) \<or> |
67091 | 6094 |
(\<exists>i. i < min(length x)(length y) \<and> take i x = take i y \<and> (x!i,y!i) \<in> r))" |
64963 | 6095 |
apply (unfold lexord_def Let_def, clarsimp) |
15656 | 6096 |
apply (rule_tac f = "(% a b. a \<or> b)" in arg_cong2) |
68719 | 6097 |
apply (metis Cons_nth_drop_Suc append_eq_conv_conj drop_all list.simps(3) not_le) |
64963 | 6098 |
apply auto |
6099 |
apply (rule_tac x ="length u" in exI, simp) |
|
68719 | 6100 |
by (metis id_take_nth_drop) |
64963 | 6101 |
|
6102 |
\<comment> \<open>lexord is extension of partial ordering List.lex\<close> |
|
41986 | 6103 |
lemma lexord_lex: "(x,y) \<in> lex r = ((x,y) \<in> lexord r \<and> length x = length y)" |
68719 | 6104 |
proof (induction x arbitrary: y) |
6105 |
case (Cons a x y) then show ?case |
|
6106 |
by (cases y) (force+) |
|
6107 |
qed auto |
|
15656 | 6108 |
|
67091 | 6109 |
lemma lexord_irreflexive: "\<forall>x. (x,x) \<notin> r \<Longrightarrow> (xs,xs) \<notin> lexord r" |
41986 | 6110 |
by (induct xs) auto |
6111 |
||
60758 | 6112 |
text\<open>By Ren\'e Thiemann:\<close> |
64963 | 6113 |
lemma lexord_partial_trans: |
41986 | 6114 |
"(\<And>x y z. x \<in> set xs \<Longrightarrow> (x,y) \<in> r \<Longrightarrow> (y,z) \<in> r \<Longrightarrow> (x,z) \<in> r) |
6115 |
\<Longrightarrow> (xs,ys) \<in> lexord r \<Longrightarrow> (ys,zs) \<in> lexord r \<Longrightarrow> (xs,zs) \<in> lexord r" |
|
6116 |
proof (induct xs arbitrary: ys zs) |
|
6117 |
case Nil |
|
6118 |
from Nil(3) show ?case unfolding lexord_def by (cases zs, auto) |
|
6119 |
next |
|
6120 |
case (Cons x xs yys zzs) |
|
6121 |
from Cons(3) obtain y ys where yys: "yys = y # ys" unfolding lexord_def |
|
6122 |
by (cases yys, auto) |
|
6123 |
note Cons = Cons[unfolded yys] |
|
6124 |
from Cons(3) have one: "(x,y) \<in> r \<or> x = y \<and> (xs,ys) \<in> lexord r" by auto |
|
6125 |
from Cons(4) obtain z zs where zzs: "zzs = z # zs" unfolding lexord_def |
|
6126 |
by (cases zzs, auto) |
|
6127 |
note Cons = Cons[unfolded zzs] |
|
6128 |
from Cons(4) have two: "(y,z) \<in> r \<or> y = z \<and> (ys,zs) \<in> lexord r" by auto |
|
6129 |
{ |
|
6130 |
assume "(xs,ys) \<in> lexord r" and "(ys,zs) \<in> lexord r" |
|
6131 |
from Cons(1)[OF _ this] Cons(2) |
|
6132 |
have "(xs,zs) \<in> lexord r" by auto |
|
6133 |
} note ind1 = this |
|
6134 |
{ |
|
6135 |
assume "(x,y) \<in> r" and "(y,z) \<in> r" |
|
6136 |
from Cons(2)[OF _ this] have "(x,z) \<in> r" by auto |
|
6137 |
} note ind2 = this |
|
6138 |
from one two ind1 ind2 |
|
6139 |
have "(x,z) \<in> r \<or> x = z \<and> (xs,zs) \<in> lexord r" by blast |
|
6140 |
thus ?case unfolding zzs by auto |
|
6141 |
qed |
|
15656 | 6142 |
|
64963 | 6143 |
lemma lexord_trans: |
15656 | 6144 |
"\<lbrakk> (x, y) \<in> lexord r; (y, z) \<in> lexord r; trans r \<rbrakk> \<Longrightarrow> (x, z) \<in> lexord r" |
41986 | 6145 |
by(auto simp: trans_def intro:lexord_partial_trans) |
15656 | 6146 |
|
6147 |
lemma lexord_transI: "trans r \<Longrightarrow> trans (lexord r)" |
|
64963 | 6148 |
by (rule transI, drule lexord_trans, blast) |
15656 | 6149 |
|
68719 | 6150 |
lemma lexord_linear: "(\<forall>a b. (a,b) \<in> r \<or> a = b \<or> (b,a) \<in> r) \<Longrightarrow> (x,y) \<in> lexord r \<or> x = y \<or> (y,x) \<in> lexord r" |
6151 |
proof (induction x arbitrary: y) |
|
6152 |
case Nil |
|
6153 |
then show ?case |
|
6154 |
by (metis lexord_Nil_left list.exhaust) |
|
6155 |
next |
|
6156 |
case (Cons a x y) then show ?case |
|
6157 |
by (cases y) (force+) |
|
6158 |
qed |
|
15656 | 6159 |
|
56545 | 6160 |
lemma lexord_irrefl: |
6161 |
"irrefl R \<Longrightarrow> irrefl (lexord R)" |
|
68085 | 6162 |
by (simp add: irrefl_def lexord_irreflexive) |
64963 | 6163 |
|
56545 | 6164 |
lemma lexord_asym: |
6165 |
assumes "asym R" |
|
6166 |
shows "asym (lexord R)" |
|
6167 |
proof |
|
6168 |
from assms obtain "irrefl R" by (blast elim: asym.cases) |
|
6169 |
then show "irrefl (lexord R)" by (rule lexord_irrefl) |
|
6170 |
next |
|
6171 |
fix xs ys |
|
6172 |
assume "(xs, ys) \<in> lexord R" |
|
6173 |
then show "(ys, xs) \<notin> lexord R" |
|
6174 |
proof (induct xs arbitrary: ys) |
|
6175 |
case Nil |
|
6176 |
then show ?case by simp |
|
6177 |
next |
|
6178 |
case (Cons x xs) |
|
6179 |
then obtain z zs where ys: "ys = z # zs" by (cases ys) auto |
|
6180 |
with assms Cons show ?case by (auto elim: asym.cases) |
|
6181 |
qed |
|
6182 |
qed |
|
64963 | 6183 |
|
56545 | 6184 |
lemma lexord_asymmetric: |
6185 |
assumes "asym R" |
|
6186 |
assumes hyp: "(a, b) \<in> lexord R" |
|
6187 |
shows "(b, a) \<notin> lexord R" |
|
6188 |
proof - |
|
60758 | 6189 |
from \<open>asym R\<close> have "asym (lexord R)" by (rule lexord_asym) |
56545 | 6190 |
then show ?thesis by (rule asym.cases) (auto simp add: hyp) |
6191 |
qed |
|
6192 |
||
6193 |
||
60758 | 6194 |
text \<open> |
54593
8c0a27b9c1bd
add predicate version of lexicographic order on lists
Andreas Lochbihler
parents:
54498
diff
changeset
|
6195 |
Predicate version of lexicographic order integrated with Isabelle's order type classes. |
8c0a27b9c1bd
add predicate version of lexicographic order on lists
Andreas Lochbihler
parents:
54498
diff
changeset
|
6196 |
Author: Andreas Lochbihler |
60758 | 6197 |
\<close> |
54593
8c0a27b9c1bd
add predicate version of lexicographic order on lists
Andreas Lochbihler
parents:
54498
diff
changeset
|
6198 |
|
61681
ca53150406c9
option "inductive_defs" controls exposure of def and mono facts;
wenzelm
parents:
61630
diff
changeset
|
6199 |
context ord |
ca53150406c9
option "inductive_defs" controls exposure of def and mono facts;
wenzelm
parents:
61630
diff
changeset
|
6200 |
begin |
ca53150406c9
option "inductive_defs" controls exposure of def and mono facts;
wenzelm
parents:
61630
diff
changeset
|
6201 |
|
ca53150406c9
option "inductive_defs" controls exposure of def and mono facts;
wenzelm
parents:
61630
diff
changeset
|
6202 |
context |
62093 | 6203 |
notes [[inductive_internals]] |
61681
ca53150406c9
option "inductive_defs" controls exposure of def and mono facts;
wenzelm
parents:
61630
diff
changeset
|
6204 |
begin |
54593
8c0a27b9c1bd
add predicate version of lexicographic order on lists
Andreas Lochbihler
parents:
54498
diff
changeset
|
6205 |
|
8c0a27b9c1bd
add predicate version of lexicographic order on lists
Andreas Lochbihler
parents:
54498
diff
changeset
|
6206 |
inductive lexordp :: "'a list \<Rightarrow> 'a list \<Rightarrow> bool" |
8c0a27b9c1bd
add predicate version of lexicographic order on lists
Andreas Lochbihler
parents:
54498
diff
changeset
|
6207 |
where |
8c0a27b9c1bd
add predicate version of lexicographic order on lists
Andreas Lochbihler
parents:
54498
diff
changeset
|
6208 |
Nil: "lexordp [] (y # ys)" |
8c0a27b9c1bd
add predicate version of lexicographic order on lists
Andreas Lochbihler
parents:
54498
diff
changeset
|
6209 |
| Cons: "x < y \<Longrightarrow> lexordp (x # xs) (y # ys)" |
8c0a27b9c1bd
add predicate version of lexicographic order on lists
Andreas Lochbihler
parents:
54498
diff
changeset
|
6210 |
| Cons_eq: |
8c0a27b9c1bd
add predicate version of lexicographic order on lists
Andreas Lochbihler
parents:
54498
diff
changeset
|
6211 |
"\<lbrakk> \<not> x < y; \<not> y < x; lexordp xs ys \<rbrakk> \<Longrightarrow> lexordp (x # xs) (y # ys)" |
8c0a27b9c1bd
add predicate version of lexicographic order on lists
Andreas Lochbihler
parents:
54498
diff
changeset
|
6212 |
|
61681
ca53150406c9
option "inductive_defs" controls exposure of def and mono facts;
wenzelm
parents:
61630
diff
changeset
|
6213 |
end |
ca53150406c9
option "inductive_defs" controls exposure of def and mono facts;
wenzelm
parents:
61630
diff
changeset
|
6214 |
|
54593
8c0a27b9c1bd
add predicate version of lexicographic order on lists
Andreas Lochbihler
parents:
54498
diff
changeset
|
6215 |
lemma lexordp_simps [simp]: |
8c0a27b9c1bd
add predicate version of lexicographic order on lists
Andreas Lochbihler
parents:
54498
diff
changeset
|
6216 |
"lexordp [] ys = (ys \<noteq> [])" |
8c0a27b9c1bd
add predicate version of lexicographic order on lists
Andreas Lochbihler
parents:
54498
diff
changeset
|
6217 |
"lexordp xs [] = False" |
8c0a27b9c1bd
add predicate version of lexicographic order on lists
Andreas Lochbihler
parents:
54498
diff
changeset
|
6218 |
"lexordp (x # xs) (y # ys) \<longleftrightarrow> x < y \<or> \<not> y < x \<and> lexordp xs ys" |
8c0a27b9c1bd
add predicate version of lexicographic order on lists
Andreas Lochbihler
parents:
54498
diff
changeset
|
6219 |
by(subst lexordp.simps, fastforce simp add: neq_Nil_conv)+ |
8c0a27b9c1bd
add predicate version of lexicographic order on lists
Andreas Lochbihler
parents:
54498
diff
changeset
|
6220 |
|
8c0a27b9c1bd
add predicate version of lexicographic order on lists
Andreas Lochbihler
parents:
54498
diff
changeset
|
6221 |
inductive lexordp_eq :: "'a list \<Rightarrow> 'a list \<Rightarrow> bool" where |
8c0a27b9c1bd
add predicate version of lexicographic order on lists
Andreas Lochbihler
parents:
54498
diff
changeset
|
6222 |
Nil: "lexordp_eq [] ys" |
8c0a27b9c1bd
add predicate version of lexicographic order on lists
Andreas Lochbihler
parents:
54498
diff
changeset
|
6223 |
| Cons: "x < y \<Longrightarrow> lexordp_eq (x # xs) (y # ys)" |
8c0a27b9c1bd
add predicate version of lexicographic order on lists
Andreas Lochbihler
parents:
54498
diff
changeset
|
6224 |
| Cons_eq: "\<lbrakk> \<not> x < y; \<not> y < x; lexordp_eq xs ys \<rbrakk> \<Longrightarrow> lexordp_eq (x # xs) (y # ys)" |
8c0a27b9c1bd
add predicate version of lexicographic order on lists
Andreas Lochbihler
parents:
54498
diff
changeset
|
6225 |
|
8c0a27b9c1bd
add predicate version of lexicographic order on lists
Andreas Lochbihler
parents:
54498
diff
changeset
|
6226 |
lemma lexordp_eq_simps [simp]: |
8c0a27b9c1bd
add predicate version of lexicographic order on lists
Andreas Lochbihler
parents:
54498
diff
changeset
|
6227 |
"lexordp_eq [] ys = True" |
8c0a27b9c1bd
add predicate version of lexicographic order on lists
Andreas Lochbihler
parents:
54498
diff
changeset
|
6228 |
"lexordp_eq xs [] \<longleftrightarrow> xs = []" |
8c0a27b9c1bd
add predicate version of lexicographic order on lists
Andreas Lochbihler
parents:
54498
diff
changeset
|
6229 |
"lexordp_eq (x # xs) [] = False" |
8c0a27b9c1bd
add predicate version of lexicographic order on lists
Andreas Lochbihler
parents:
54498
diff
changeset
|
6230 |
"lexordp_eq (x # xs) (y # ys) \<longleftrightarrow> x < y \<or> \<not> y < x \<and> lexordp_eq xs ys" |
8c0a27b9c1bd
add predicate version of lexicographic order on lists
Andreas Lochbihler
parents:
54498
diff
changeset
|
6231 |
by(subst lexordp_eq.simps, fastforce)+ |
8c0a27b9c1bd
add predicate version of lexicographic order on lists
Andreas Lochbihler
parents:
54498
diff
changeset
|
6232 |
|
8c0a27b9c1bd
add predicate version of lexicographic order on lists
Andreas Lochbihler
parents:
54498
diff
changeset
|
6233 |
lemma lexordp_append_rightI: "ys \<noteq> Nil \<Longrightarrow> lexordp xs (xs @ ys)" |
8c0a27b9c1bd
add predicate version of lexicographic order on lists
Andreas Lochbihler
parents:
54498
diff
changeset
|
6234 |
by(induct xs)(auto simp add: neq_Nil_conv) |
8c0a27b9c1bd
add predicate version of lexicographic order on lists
Andreas Lochbihler
parents:
54498
diff
changeset
|
6235 |
|
8c0a27b9c1bd
add predicate version of lexicographic order on lists
Andreas Lochbihler
parents:
54498
diff
changeset
|
6236 |
lemma lexordp_append_left_rightI: "x < y \<Longrightarrow> lexordp (us @ x # xs) (us @ y # ys)" |
8c0a27b9c1bd
add predicate version of lexicographic order on lists
Andreas Lochbihler
parents:
54498
diff
changeset
|
6237 |
by(induct us) auto |
8c0a27b9c1bd
add predicate version of lexicographic order on lists
Andreas Lochbihler
parents:
54498
diff
changeset
|
6238 |
|
8c0a27b9c1bd
add predicate version of lexicographic order on lists
Andreas Lochbihler
parents:
54498
diff
changeset
|
6239 |
lemma lexordp_eq_refl: "lexordp_eq xs xs" |
8c0a27b9c1bd
add predicate version of lexicographic order on lists
Andreas Lochbihler
parents:
54498
diff
changeset
|
6240 |
by(induct xs) simp_all |
8c0a27b9c1bd
add predicate version of lexicographic order on lists
Andreas Lochbihler
parents:
54498
diff
changeset
|
6241 |
|
8c0a27b9c1bd
add predicate version of lexicographic order on lists
Andreas Lochbihler
parents:
54498
diff
changeset
|
6242 |
lemma lexordp_append_leftI: "lexordp us vs \<Longrightarrow> lexordp (xs @ us) (xs @ vs)" |
8c0a27b9c1bd
add predicate version of lexicographic order on lists
Andreas Lochbihler
parents:
54498
diff
changeset
|
6243 |
by(induct xs) auto |
8c0a27b9c1bd
add predicate version of lexicographic order on lists
Andreas Lochbihler
parents:
54498
diff
changeset
|
6244 |
|
8c0a27b9c1bd
add predicate version of lexicographic order on lists
Andreas Lochbihler
parents:
54498
diff
changeset
|
6245 |
lemma lexordp_append_leftD: "\<lbrakk> lexordp (xs @ us) (xs @ vs); \<forall>a. \<not> a < a \<rbrakk> \<Longrightarrow> lexordp us vs" |
8c0a27b9c1bd
add predicate version of lexicographic order on lists
Andreas Lochbihler
parents:
54498
diff
changeset
|
6246 |
by(induct xs) auto |
8c0a27b9c1bd
add predicate version of lexicographic order on lists
Andreas Lochbihler
parents:
54498
diff
changeset
|
6247 |
|
64963 | 6248 |
lemma lexordp_irreflexive: |
54593
8c0a27b9c1bd
add predicate version of lexicographic order on lists
Andreas Lochbihler
parents:
54498
diff
changeset
|
6249 |
assumes irrefl: "\<forall>x. \<not> x < x" |
8c0a27b9c1bd
add predicate version of lexicographic order on lists
Andreas Lochbihler
parents:
54498
diff
changeset
|
6250 |
shows "\<not> lexordp xs xs" |
8c0a27b9c1bd
add predicate version of lexicographic order on lists
Andreas Lochbihler
parents:
54498
diff
changeset
|
6251 |
proof |
8c0a27b9c1bd
add predicate version of lexicographic order on lists
Andreas Lochbihler
parents:
54498
diff
changeset
|
6252 |
assume "lexordp xs xs" |
8c0a27b9c1bd
add predicate version of lexicographic order on lists
Andreas Lochbihler
parents:
54498
diff
changeset
|
6253 |
thus False by(induct xs ys\<equiv>xs)(simp_all add: irrefl) |
8c0a27b9c1bd
add predicate version of lexicographic order on lists
Andreas Lochbihler
parents:
54498
diff
changeset
|
6254 |
qed |
8c0a27b9c1bd
add predicate version of lexicographic order on lists
Andreas Lochbihler
parents:
54498
diff
changeset
|
6255 |
|
8c0a27b9c1bd
add predicate version of lexicographic order on lists
Andreas Lochbihler
parents:
54498
diff
changeset
|
6256 |
lemma lexordp_into_lexordp_eq: |
8c0a27b9c1bd
add predicate version of lexicographic order on lists
Andreas Lochbihler
parents:
54498
diff
changeset
|
6257 |
assumes "lexordp xs ys" |
8c0a27b9c1bd
add predicate version of lexicographic order on lists
Andreas Lochbihler
parents:
54498
diff
changeset
|
6258 |
shows "lexordp_eq xs ys" |
8c0a27b9c1bd
add predicate version of lexicographic order on lists
Andreas Lochbihler
parents:
54498
diff
changeset
|
6259 |
using assms by induct simp_all |
8c0a27b9c1bd
add predicate version of lexicographic order on lists
Andreas Lochbihler
parents:
54498
diff
changeset
|
6260 |
|
8c0a27b9c1bd
add predicate version of lexicographic order on lists
Andreas Lochbihler
parents:
54498
diff
changeset
|
6261 |
end |
8c0a27b9c1bd
add predicate version of lexicographic order on lists
Andreas Lochbihler
parents:
54498
diff
changeset
|
6262 |
|
8c0a27b9c1bd
add predicate version of lexicographic order on lists
Andreas Lochbihler
parents:
54498
diff
changeset
|
6263 |
declare ord.lexordp_simps [simp, code] |
8c0a27b9c1bd
add predicate version of lexicographic order on lists
Andreas Lochbihler
parents:
54498
diff
changeset
|
6264 |
declare ord.lexordp_eq_simps [code, simp] |
8c0a27b9c1bd
add predicate version of lexicographic order on lists
Andreas Lochbihler
parents:
54498
diff
changeset
|
6265 |
|
8c0a27b9c1bd
add predicate version of lexicographic order on lists
Andreas Lochbihler
parents:
54498
diff
changeset
|
6266 |
lemma lexord_code [code, code_unfold]: "lexordp = ord.lexordp less" |
8c0a27b9c1bd
add predicate version of lexicographic order on lists
Andreas Lochbihler
parents:
54498
diff
changeset
|
6267 |
unfolding lexordp_def ord.lexordp_def .. |
8c0a27b9c1bd
add predicate version of lexicographic order on lists
Andreas Lochbihler
parents:
54498
diff
changeset
|
6268 |
|
61681
ca53150406c9
option "inductive_defs" controls exposure of def and mono facts;
wenzelm
parents:
61630
diff
changeset
|
6269 |
context order |
ca53150406c9
option "inductive_defs" controls exposure of def and mono facts;
wenzelm
parents:
61630
diff
changeset
|
6270 |
begin |
54593
8c0a27b9c1bd
add predicate version of lexicographic order on lists
Andreas Lochbihler
parents:
54498
diff
changeset
|
6271 |
|
8c0a27b9c1bd
add predicate version of lexicographic order on lists
Andreas Lochbihler
parents:
54498
diff
changeset
|
6272 |
lemma lexordp_antisym: |
8c0a27b9c1bd
add predicate version of lexicographic order on lists
Andreas Lochbihler
parents:
54498
diff
changeset
|
6273 |
assumes "lexordp xs ys" "lexordp ys xs" |
8c0a27b9c1bd
add predicate version of lexicographic order on lists
Andreas Lochbihler
parents:
54498
diff
changeset
|
6274 |
shows False |
8c0a27b9c1bd
add predicate version of lexicographic order on lists
Andreas Lochbihler
parents:
54498
diff
changeset
|
6275 |
using assms by induct auto |
8c0a27b9c1bd
add predicate version of lexicographic order on lists
Andreas Lochbihler
parents:
54498
diff
changeset
|
6276 |
|
8c0a27b9c1bd
add predicate version of lexicographic order on lists
Andreas Lochbihler
parents:
54498
diff
changeset
|
6277 |
lemma lexordp_irreflexive': "\<not> lexordp xs xs" |
8c0a27b9c1bd
add predicate version of lexicographic order on lists
Andreas Lochbihler
parents:
54498
diff
changeset
|
6278 |
by(rule lexordp_irreflexive) simp |
8c0a27b9c1bd
add predicate version of lexicographic order on lists
Andreas Lochbihler
parents:
54498
diff
changeset
|
6279 |
|
8c0a27b9c1bd
add predicate version of lexicographic order on lists
Andreas Lochbihler
parents:
54498
diff
changeset
|
6280 |
end |
8c0a27b9c1bd
add predicate version of lexicographic order on lists
Andreas Lochbihler
parents:
54498
diff
changeset
|
6281 |
|
8c0a27b9c1bd
add predicate version of lexicographic order on lists
Andreas Lochbihler
parents:
54498
diff
changeset
|
6282 |
context linorder begin |
8c0a27b9c1bd
add predicate version of lexicographic order on lists
Andreas Lochbihler
parents:
54498
diff
changeset
|
6283 |
|
8c0a27b9c1bd
add predicate version of lexicographic order on lists
Andreas Lochbihler
parents:
54498
diff
changeset
|
6284 |
lemma lexordp_cases [consumes 1, case_names Nil Cons Cons_eq, cases pred: lexordp]: |
8c0a27b9c1bd
add predicate version of lexicographic order on lists
Andreas Lochbihler
parents:
54498
diff
changeset
|
6285 |
assumes "lexordp xs ys" |
8c0a27b9c1bd
add predicate version of lexicographic order on lists
Andreas Lochbihler
parents:
54498
diff
changeset
|
6286 |
obtains (Nil) y ys' where "xs = []" "ys = y # ys'" |
8c0a27b9c1bd
add predicate version of lexicographic order on lists
Andreas Lochbihler
parents:
54498
diff
changeset
|
6287 |
| (Cons) x xs' y ys' where "xs = x # xs'" "ys = y # ys'" "x < y" |
8c0a27b9c1bd
add predicate version of lexicographic order on lists
Andreas Lochbihler
parents:
54498
diff
changeset
|
6288 |
| (Cons_eq) x xs' ys' where "xs = x # xs'" "ys = x # ys'" "lexordp xs' ys'" |
8c0a27b9c1bd
add predicate version of lexicographic order on lists
Andreas Lochbihler
parents:
54498
diff
changeset
|
6289 |
using assms by cases (fastforce simp add: not_less_iff_gr_or_eq)+ |
8c0a27b9c1bd
add predicate version of lexicographic order on lists
Andreas Lochbihler
parents:
54498
diff
changeset
|
6290 |
|
8c0a27b9c1bd
add predicate version of lexicographic order on lists
Andreas Lochbihler
parents:
54498
diff
changeset
|
6291 |
lemma lexordp_induct [consumes 1, case_names Nil Cons Cons_eq, induct pred: lexordp]: |
8c0a27b9c1bd
add predicate version of lexicographic order on lists
Andreas Lochbihler
parents:
54498
diff
changeset
|
6292 |
assumes major: "lexordp xs ys" |
8c0a27b9c1bd
add predicate version of lexicographic order on lists
Andreas Lochbihler
parents:
54498
diff
changeset
|
6293 |
and Nil: "\<And>y ys. P [] (y # ys)" |
8c0a27b9c1bd
add predicate version of lexicographic order on lists
Andreas Lochbihler
parents:
54498
diff
changeset
|
6294 |
and Cons: "\<And>x xs y ys. x < y \<Longrightarrow> P (x # xs) (y # ys)" |
8c0a27b9c1bd
add predicate version of lexicographic order on lists
Andreas Lochbihler
parents:
54498
diff
changeset
|
6295 |
and Cons_eq: "\<And>x xs ys. \<lbrakk> lexordp xs ys; P xs ys \<rbrakk> \<Longrightarrow> P (x # xs) (x # ys)" |
8c0a27b9c1bd
add predicate version of lexicographic order on lists
Andreas Lochbihler
parents:
54498
diff
changeset
|
6296 |
shows "P xs ys" |
8c0a27b9c1bd
add predicate version of lexicographic order on lists
Andreas Lochbihler
parents:
54498
diff
changeset
|
6297 |
using major by induct (simp_all add: Nil Cons not_less_iff_gr_or_eq Cons_eq) |
8c0a27b9c1bd
add predicate version of lexicographic order on lists
Andreas Lochbihler
parents:
54498
diff
changeset
|
6298 |
|
8c0a27b9c1bd
add predicate version of lexicographic order on lists
Andreas Lochbihler
parents:
54498
diff
changeset
|
6299 |
lemma lexordp_iff: |
8c0a27b9c1bd
add predicate version of lexicographic order on lists
Andreas Lochbihler
parents:
54498
diff
changeset
|
6300 |
"lexordp xs ys \<longleftrightarrow> (\<exists>x vs. ys = xs @ x # vs) \<or> (\<exists>us a b vs ws. a < b \<and> xs = us @ a # vs \<and> ys = us @ b # ws)" |
8c0a27b9c1bd
add predicate version of lexicographic order on lists
Andreas Lochbihler
parents:
54498
diff
changeset
|
6301 |
(is "?lhs = ?rhs") |
8c0a27b9c1bd
add predicate version of lexicographic order on lists
Andreas Lochbihler
parents:
54498
diff
changeset
|
6302 |
proof |
8c0a27b9c1bd
add predicate version of lexicographic order on lists
Andreas Lochbihler
parents:
54498
diff
changeset
|
6303 |
assume ?lhs thus ?rhs |
8c0a27b9c1bd
add predicate version of lexicographic order on lists
Andreas Lochbihler
parents:
54498
diff
changeset
|
6304 |
proof induct |
8c0a27b9c1bd
add predicate version of lexicographic order on lists
Andreas Lochbihler
parents:
54498
diff
changeset
|
6305 |
case Cons_eq thus ?case by simp (metis append.simps(2)) |
8c0a27b9c1bd
add predicate version of lexicographic order on lists
Andreas Lochbihler
parents:
54498
diff
changeset
|
6306 |
qed(fastforce intro: disjI2 del: disjCI intro: exI[where x="[]"])+ |
8c0a27b9c1bd
add predicate version of lexicographic order on lists
Andreas Lochbihler
parents:
54498
diff
changeset
|
6307 |
next |
8c0a27b9c1bd
add predicate version of lexicographic order on lists
Andreas Lochbihler
parents:
54498
diff
changeset
|
6308 |
assume ?rhs thus ?lhs |
8c0a27b9c1bd
add predicate version of lexicographic order on lists
Andreas Lochbihler
parents:
54498
diff
changeset
|
6309 |
by(auto intro: lexordp_append_leftI[where us="[]", simplified] lexordp_append_leftI) |
8c0a27b9c1bd
add predicate version of lexicographic order on lists
Andreas Lochbihler
parents:
54498
diff
changeset
|
6310 |
qed |
8c0a27b9c1bd
add predicate version of lexicographic order on lists
Andreas Lochbihler
parents:
54498
diff
changeset
|
6311 |
|
8c0a27b9c1bd
add predicate version of lexicographic order on lists
Andreas Lochbihler
parents:
54498
diff
changeset
|
6312 |
lemma lexordp_conv_lexord: |
8c0a27b9c1bd
add predicate version of lexicographic order on lists
Andreas Lochbihler
parents:
54498
diff
changeset
|
6313 |
"lexordp xs ys \<longleftrightarrow> (xs, ys) \<in> lexord {(x, y). x < y}" |
8c0a27b9c1bd
add predicate version of lexicographic order on lists
Andreas Lochbihler
parents:
54498
diff
changeset
|
6314 |
by(simp add: lexordp_iff lexord_def) |
8c0a27b9c1bd
add predicate version of lexicographic order on lists
Andreas Lochbihler
parents:
54498
diff
changeset
|
6315 |
|
64963 | 6316 |
lemma lexordp_eq_antisym: |
6317 |
assumes "lexordp_eq xs ys" "lexordp_eq ys xs" |
|
54593
8c0a27b9c1bd
add predicate version of lexicographic order on lists
Andreas Lochbihler
parents:
54498
diff
changeset
|
6318 |
shows "xs = ys" |
8c0a27b9c1bd
add predicate version of lexicographic order on lists
Andreas Lochbihler
parents:
54498
diff
changeset
|
6319 |
using assms by induct simp_all |
8c0a27b9c1bd
add predicate version of lexicographic order on lists
Andreas Lochbihler
parents:
54498
diff
changeset
|
6320 |
|
8c0a27b9c1bd
add predicate version of lexicographic order on lists
Andreas Lochbihler
parents:
54498
diff
changeset
|
6321 |
lemma lexordp_eq_trans: |
8c0a27b9c1bd
add predicate version of lexicographic order on lists
Andreas Lochbihler
parents:
54498
diff
changeset
|
6322 |
assumes "lexordp_eq xs ys" and "lexordp_eq ys zs" |
8c0a27b9c1bd
add predicate version of lexicographic order on lists
Andreas Lochbihler
parents:
54498
diff
changeset
|
6323 |
shows "lexordp_eq xs zs" |
68719 | 6324 |
using assms |
6325 |
by (induct arbitrary: zs) (case_tac zs; auto)+ |
|
54593
8c0a27b9c1bd
add predicate version of lexicographic order on lists
Andreas Lochbihler
parents:
54498
diff
changeset
|
6326 |
|
8c0a27b9c1bd
add predicate version of lexicographic order on lists
Andreas Lochbihler
parents:
54498
diff
changeset
|
6327 |
lemma lexordp_trans: |
8c0a27b9c1bd
add predicate version of lexicographic order on lists
Andreas Lochbihler
parents:
54498
diff
changeset
|
6328 |
assumes "lexordp xs ys" "lexordp ys zs" |
8c0a27b9c1bd
add predicate version of lexicographic order on lists
Andreas Lochbihler
parents:
54498
diff
changeset
|
6329 |
shows "lexordp xs zs" |
68719 | 6330 |
using assms |
6331 |
by (induct arbitrary: zs) (case_tac zs; auto)+ |
|
54593
8c0a27b9c1bd
add predicate version of lexicographic order on lists
Andreas Lochbihler
parents:
54498
diff
changeset
|
6332 |
|
8c0a27b9c1bd
add predicate version of lexicographic order on lists
Andreas Lochbihler
parents:
54498
diff
changeset
|
6333 |
lemma lexordp_linear: "lexordp xs ys \<or> xs = ys \<or> lexordp ys xs" |
68719 | 6334 |
by(induct xs arbitrary: ys; case_tac ys; fastforce) |
54593
8c0a27b9c1bd
add predicate version of lexicographic order on lists
Andreas Lochbihler
parents:
54498
diff
changeset
|
6335 |
|
8c0a27b9c1bd
add predicate version of lexicographic order on lists
Andreas Lochbihler
parents:
54498
diff
changeset
|
6336 |
lemma lexordp_conv_lexordp_eq: "lexordp xs ys \<longleftrightarrow> lexordp_eq xs ys \<and> \<not> lexordp_eq ys xs" |
8c0a27b9c1bd
add predicate version of lexicographic order on lists
Andreas Lochbihler
parents:
54498
diff
changeset
|
6337 |
(is "?lhs \<longleftrightarrow> ?rhs") |
8c0a27b9c1bd
add predicate version of lexicographic order on lists
Andreas Lochbihler
parents:
54498
diff
changeset
|
6338 |
proof |
8c0a27b9c1bd
add predicate version of lexicographic order on lists
Andreas Lochbihler
parents:
54498
diff
changeset
|
6339 |
assume ?lhs |
63540 | 6340 |
hence "\<not> lexordp_eq ys xs" by induct simp_all |
6341 |
with \<open>?lhs\<close> show ?rhs by (simp add: lexordp_into_lexordp_eq) |
|
54593
8c0a27b9c1bd
add predicate version of lexicographic order on lists
Andreas Lochbihler
parents:
54498
diff
changeset
|
6342 |
next |
8c0a27b9c1bd
add predicate version of lexicographic order on lists
Andreas Lochbihler
parents:
54498
diff
changeset
|
6343 |
assume ?rhs |
8c0a27b9c1bd
add predicate version of lexicographic order on lists
Andreas Lochbihler
parents:
54498
diff
changeset
|
6344 |
hence "lexordp_eq xs ys" "\<not> lexordp_eq ys xs" by simp_all |
8c0a27b9c1bd
add predicate version of lexicographic order on lists
Andreas Lochbihler
parents:
54498
diff
changeset
|
6345 |
thus ?lhs by induct simp_all |
8c0a27b9c1bd
add predicate version of lexicographic order on lists
Andreas Lochbihler
parents:
54498
diff
changeset
|
6346 |
qed |
8c0a27b9c1bd
add predicate version of lexicographic order on lists
Andreas Lochbihler
parents:
54498
diff
changeset
|
6347 |
|
8c0a27b9c1bd
add predicate version of lexicographic order on lists
Andreas Lochbihler
parents:
54498
diff
changeset
|
6348 |
lemma lexordp_eq_conv_lexord: "lexordp_eq xs ys \<longleftrightarrow> xs = ys \<or> lexordp xs ys" |
8c0a27b9c1bd
add predicate version of lexicographic order on lists
Andreas Lochbihler
parents:
54498
diff
changeset
|
6349 |
by(auto simp add: lexordp_conv_lexordp_eq lexordp_eq_refl dest: lexordp_eq_antisym) |
8c0a27b9c1bd
add predicate version of lexicographic order on lists
Andreas Lochbihler
parents:
54498
diff
changeset
|
6350 |
|
8c0a27b9c1bd
add predicate version of lexicographic order on lists
Andreas Lochbihler
parents:
54498
diff
changeset
|
6351 |
lemma lexordp_eq_linear: "lexordp_eq xs ys \<or> lexordp_eq ys xs" |
68719 | 6352 |
by (induct xs arbitrary: ys) (case_tac ys; auto)+ |
54593
8c0a27b9c1bd
add predicate version of lexicographic order on lists
Andreas Lochbihler
parents:
54498
diff
changeset
|
6353 |
|
8c0a27b9c1bd
add predicate version of lexicographic order on lists
Andreas Lochbihler
parents:
54498
diff
changeset
|
6354 |
lemma lexordp_linorder: "class.linorder lexordp_eq lexordp" |
68719 | 6355 |
by unfold_locales |
6356 |
(auto simp add: lexordp_conv_lexordp_eq lexordp_eq_refl lexordp_eq_antisym intro: lexordp_eq_trans del: disjCI intro: lexordp_eq_linear) |
|
54593
8c0a27b9c1bd
add predicate version of lexicographic order on lists
Andreas Lochbihler
parents:
54498
diff
changeset
|
6357 |
|
8c0a27b9c1bd
add predicate version of lexicographic order on lists
Andreas Lochbihler
parents:
54498
diff
changeset
|
6358 |
end |
15656 | 6359 |
|
64886 | 6360 |
lemma sorted_insort_is_snoc: "sorted xs \<Longrightarrow> \<forall>x \<in> set xs. a \<ge> x \<Longrightarrow> insort a xs = xs @ [a]" |
68109 | 6361 |
by (induct xs) (auto dest!: insort_is_Cons) |
64886 | 6362 |
|
6363 |
||
60758 | 6364 |
subsubsection \<open>Lexicographic combination of measure functions\<close> |
6365 |
||
6366 |
text \<open>These are useful for termination proofs\<close> |
|
21103
367b4ad7c7cc
Added "measures" combinator for lexicographic combinations of multiple measures.
krauss
parents:
21079
diff
changeset
|
6367 |
|
50548 | 6368 |
definition "measures fs = inv_image (lex less_than) (%a. map (%f. f a) fs)" |
21103
367b4ad7c7cc
Added "measures" combinator for lexicographic combinations of multiple measures.
krauss
parents:
21079
diff
changeset
|
6369 |
|
44013
5cfc1c36ae97
moved recdef package to HOL/Library/Old_Recdef.thy
krauss
parents:
43594
diff
changeset
|
6370 |
lemma wf_measures[simp]: "wf (measures fs)" |
24349 | 6371 |
unfolding measures_def |
6372 |
by blast |
|
21103
367b4ad7c7cc
Added "measures" combinator for lexicographic combinations of multiple measures.
krauss
parents:
21079
diff
changeset
|
6373 |
|
64963 | 6374 |
lemma in_measures[simp]: |
21103
367b4ad7c7cc
Added "measures" combinator for lexicographic combinations of multiple measures.
krauss
parents:
21079
diff
changeset
|
6375 |
"(x, y) \<in> measures [] = False" |
367b4ad7c7cc
Added "measures" combinator for lexicographic combinations of multiple measures.
krauss
parents:
21079
diff
changeset
|
6376 |
"(x, y) \<in> measures (f # fs) |
64963 | 6377 |
= (f x < f y \<or> (f x = f y \<and> (x, y) \<in> measures fs))" |
24349 | 6378 |
unfolding measures_def |
6379 |
by auto |
|
21103
367b4ad7c7cc
Added "measures" combinator for lexicographic combinations of multiple measures.
krauss
parents:
21079
diff
changeset
|
6380 |
|
367b4ad7c7cc
Added "measures" combinator for lexicographic combinations of multiple measures.
krauss
parents:
21079
diff
changeset
|
6381 |
lemma measures_less: "f x < f y ==> (x, y) \<in> measures (f#fs)" |
24349 | 6382 |
by simp |
21103
367b4ad7c7cc
Added "measures" combinator for lexicographic combinations of multiple measures.
krauss
parents:
21079
diff
changeset
|
6383 |
|
68709 | 6384 |
lemma measures_lesseq: "f x \<le> f y ==> (x, y) \<in> measures fs ==> (x, y) \<in> measures (f#fs)" |
24349 | 6385 |
by auto |
21103
367b4ad7c7cc
Added "measures" combinator for lexicographic combinations of multiple measures.
krauss
parents:
21079
diff
changeset
|
6386 |
|
367b4ad7c7cc
Added "measures" combinator for lexicographic combinations of multiple measures.
krauss
parents:
21079
diff
changeset
|
6387 |
|
60758 | 6388 |
subsubsection \<open>Lifting Relations to Lists: one element\<close> |
40230 | 6389 |
|
6390 |
definition listrel1 :: "('a \<times> 'a) set \<Rightarrow> ('a list \<times> 'a list) set" where |
|
6391 |
"listrel1 r = {(xs,ys). |
|
6392 |
\<exists>us z z' vs. xs = us @ z # vs \<and> (z,z') \<in> r \<and> ys = us @ z' # vs}" |
|
6393 |
||
6394 |
lemma listrel1I: |
|
6395 |
"\<lbrakk> (x, y) \<in> r; xs = us @ x # vs; ys = us @ y # vs \<rbrakk> \<Longrightarrow> |
|
6396 |
(xs, ys) \<in> listrel1 r" |
|
6397 |
unfolding listrel1_def by auto |
|
6398 |
||
6399 |
lemma listrel1E: |
|
6400 |
"\<lbrakk> (xs, ys) \<in> listrel1 r; |
|
6401 |
!!x y us vs. \<lbrakk> (x, y) \<in> r; xs = us @ x # vs; ys = us @ y # vs \<rbrakk> \<Longrightarrow> P |
|
6402 |
\<rbrakk> \<Longrightarrow> P" |
|
6403 |
unfolding listrel1_def by auto |
|
6404 |
||
6405 |
lemma not_Nil_listrel1 [iff]: "([], xs) \<notin> listrel1 r" |
|
6406 |
unfolding listrel1_def by blast |
|
6407 |
||
6408 |
lemma not_listrel1_Nil [iff]: "(xs, []) \<notin> listrel1 r" |
|
6409 |
unfolding listrel1_def by blast |
|
6410 |
||
6411 |
lemma Cons_listrel1_Cons [iff]: |
|
6412 |
"(x # xs, y # ys) \<in> listrel1 r \<longleftrightarrow> |
|
6413 |
(x,y) \<in> r \<and> xs = ys \<or> x = y \<and> (xs, ys) \<in> listrel1 r" |
|
6414 |
by (simp add: listrel1_def Cons_eq_append_conv) (blast) |
|
6415 |
||
6416 |
lemma listrel1I1: "(x,y) \<in> r \<Longrightarrow> (x # xs, y # xs) \<in> listrel1 r" |
|
56085 | 6417 |
by fast |
40230 | 6418 |
|
6419 |
lemma listrel1I2: "(xs, ys) \<in> listrel1 r \<Longrightarrow> (x # xs, x # ys) \<in> listrel1 r" |
|
56085 | 6420 |
by fast |
40230 | 6421 |
|
6422 |
lemma append_listrel1I: |
|
6423 |
"(xs, ys) \<in> listrel1 r \<and> us = vs \<or> xs = ys \<and> (us, vs) \<in> listrel1 r |
|
6424 |
\<Longrightarrow> (xs @ us, ys @ vs) \<in> listrel1 r" |
|
6425 |
unfolding listrel1_def |
|
6426 |
by auto (blast intro: append_eq_appendI)+ |
|
6427 |
||
6428 |
lemma Cons_listrel1E1[elim!]: |
|
6429 |
assumes "(x # xs, ys) \<in> listrel1 r" |
|
6430 |
and "\<And>y. ys = y # xs \<Longrightarrow> (x, y) \<in> r \<Longrightarrow> R" |
|
6431 |
and "\<And>zs. ys = x # zs \<Longrightarrow> (xs, zs) \<in> listrel1 r \<Longrightarrow> R" |
|
6432 |
shows R |
|
6433 |
using assms by (cases ys) blast+ |
|
6434 |
||
6435 |
lemma Cons_listrel1E2[elim!]: |
|
6436 |
assumes "(xs, y # ys) \<in> listrel1 r" |
|
6437 |
and "\<And>x. xs = x # ys \<Longrightarrow> (x, y) \<in> r \<Longrightarrow> R" |
|
6438 |
and "\<And>zs. xs = y # zs \<Longrightarrow> (zs, ys) \<in> listrel1 r \<Longrightarrow> R" |
|
6439 |
shows R |
|
6440 |
using assms by (cases xs) blast+ |
|
6441 |
||
6442 |
lemma snoc_listrel1_snoc_iff: |
|
6443 |
"(xs @ [x], ys @ [y]) \<in> listrel1 r |
|
6444 |
\<longleftrightarrow> (xs, ys) \<in> listrel1 r \<and> x = y \<or> xs = ys \<and> (x,y) \<in> r" (is "?L \<longleftrightarrow> ?R") |
|
6445 |
proof |
|
6446 |
assume ?L thus ?R |
|
44890
22f665a2e91c
new fastforce replacing fastsimp - less confusing name
nipkow
parents:
44635
diff
changeset
|
6447 |
by (fastforce simp: listrel1_def snoc_eq_iff_butlast butlast_append) |
40230 | 6448 |
next |
6449 |
assume ?R then show ?L unfolding listrel1_def by force |
|
6450 |
qed |
|
6451 |
||
6452 |
lemma listrel1_eq_len: "(xs,ys) \<in> listrel1 r \<Longrightarrow> length xs = length ys" |
|
6453 |
unfolding listrel1_def by auto |
|
6454 |
||
6455 |
lemma listrel1_mono: |
|
6456 |
"r \<subseteq> s \<Longrightarrow> listrel1 r \<subseteq> listrel1 s" |
|
6457 |
unfolding listrel1_def by blast |
|
6458 |
||
6459 |
||
67613 | 6460 |
lemma listrel1_converse: "listrel1 (r\<inverse>) = (listrel1 r)\<inverse>" |
40230 | 6461 |
unfolding listrel1_def by blast |
6462 |
||
6463 |
lemma in_listrel1_converse: |
|
67613 | 6464 |
"(x,y) \<in> listrel1 (r\<inverse>) \<longleftrightarrow> (x,y) \<in> (listrel1 r)\<inverse>" |
40230 | 6465 |
unfolding listrel1_def by blast |
6466 |
||
6467 |
lemma listrel1_iff_update: |
|
6468 |
"(xs,ys) \<in> (listrel1 r) |
|
6469 |
\<longleftrightarrow> (\<exists>y n. (xs ! n, y) \<in> r \<and> n < length xs \<and> ys = xs[n:=y])" (is "?L \<longleftrightarrow> ?R") |
|
6470 |
proof |
|
6471 |
assume "?L" |
|
6472 |
then obtain x y u v where "xs = u @ x # v" "ys = u @ y # v" "(x,y) \<in> r" |
|
6473 |
unfolding listrel1_def by auto |
|
6474 |
then have "ys = xs[length u := y]" and "length u < length xs" |
|
6475 |
and "(xs ! length u, y) \<in> r" by auto |
|
6476 |
then show "?R" by auto |
|
6477 |
next |
|
6478 |
assume "?R" |
|
6479 |
then obtain x y n where "(xs!n, y) \<in> r" "n < size xs" "ys = xs[n:=y]" "x = xs!n" |
|
6480 |
by auto |
|
6481 |
then obtain u v where "xs = u @ x # v" and "ys = u @ y # v" and "(x, y) \<in> r" |
|
6482 |
by (auto intro: upd_conv_take_nth_drop id_take_nth_drop) |
|
6483 |
then show "?L" by (auto simp: listrel1_def) |
|
6484 |
qed |
|
6485 |
||
6486 |
||
60758 | 6487 |
text\<open>Accessible part and wellfoundedness:\<close> |
40230 | 6488 |
|
6489 |
lemma Cons_acc_listrel1I [intro!]: |
|
54295 | 6490 |
"x \<in> Wellfounded.acc r \<Longrightarrow> xs \<in> Wellfounded.acc (listrel1 r) \<Longrightarrow> (x # xs) \<in> Wellfounded.acc (listrel1 r)" |
6491 |
apply (induct arbitrary: xs set: Wellfounded.acc) |
|
40230 | 6492 |
apply (erule thin_rl) |
6493 |
apply (erule acc_induct) |
|
68719 | 6494 |
apply (rule accI) |
40230 | 6495 |
apply (blast) |
6496 |
done |
|
6497 |
||
54295 | 6498 |
lemma lists_accD: "xs \<in> lists (Wellfounded.acc r) \<Longrightarrow> xs \<in> Wellfounded.acc (listrel1 r)" |
68719 | 6499 |
proof (induct set: lists) |
6500 |
case Nil |
|
6501 |
then show ?case |
|
6502 |
by (meson acc.intros not_listrel1_Nil) |
|
6503 |
next |
|
6504 |
case (Cons a l) |
|
6505 |
then show ?case |
|
6506 |
by blast |
|
6507 |
qed |
|
40230 | 6508 |
|
54295 | 6509 |
lemma lists_accI: "xs \<in> Wellfounded.acc (listrel1 r) \<Longrightarrow> xs \<in> lists (Wellfounded.acc r)" |
6510 |
apply (induct set: Wellfounded.acc) |
|
40230 | 6511 |
apply clarify |
6512 |
apply (rule accI) |
|
44890
22f665a2e91c
new fastforce replacing fastsimp - less confusing name
nipkow
parents:
44635
diff
changeset
|
6513 |
apply (fastforce dest!: in_set_conv_decomp[THEN iffD1] simp: listrel1_def) |
40230 | 6514 |
done |
6515 |
||
44510 | 6516 |
lemma wf_listrel1_iff[simp]: "wf(listrel1 r) = wf r" |
56085 | 6517 |
by (auto simp: wf_acc_iff |
6518 |
intro: lists_accD lists_accI[THEN Cons_in_lists_iff[THEN iffD1, THEN conjunct1]]) |
|
40230 | 6519 |
|
60758 | 6520 |
subsubsection \<open>Lifting Relations to Lists: all elements\<close> |
15302 | 6521 |
|
23740 | 6522 |
inductive_set |
46317 | 6523 |
listrel :: "('a \<times> 'b) set \<Rightarrow> ('a list \<times> 'b list) set" |
6524 |
for r :: "('a \<times> 'b) set" |
|
22262 | 6525 |
where |
23740 | 6526 |
Nil: "([],[]) \<in> listrel r" |
6527 |
| Cons: "[| (x,y) \<in> r; (xs,ys) \<in> listrel r |] ==> (x#xs, y#ys) \<in> listrel r" |
|
6528 |
||
6529 |
inductive_cases listrel_Nil1 [elim!]: "([],xs) \<in> listrel r" |
|
6530 |
inductive_cases listrel_Nil2 [elim!]: "(xs,[]) \<in> listrel r" |
|
6531 |
inductive_cases listrel_Cons1 [elim!]: "(y#ys,xs) \<in> listrel r" |
|
6532 |
inductive_cases listrel_Cons2 [elim!]: "(xs,y#ys) \<in> listrel r" |
|
15302 | 6533 |
|
6534 |
||
40230 | 6535 |
lemma listrel_eq_len: "(xs, ys) \<in> listrel r \<Longrightarrow> length xs = length ys" |
6536 |
by(induct rule: listrel.induct) auto |
|
6537 |
||
67091 | 6538 |
lemma listrel_iff_zip [code_unfold]: "(xs,ys) \<in> listrel r \<longleftrightarrow> |
6539 |
length xs = length ys \<and> (\<forall>(x,y) \<in> set(zip xs ys). (x,y) \<in> r)" (is "?L \<longleftrightarrow> ?R") |
|
40230 | 6540 |
proof |
6541 |
assume ?L thus ?R by induct (auto intro: listrel_eq_len) |
|
6542 |
next |
|
6543 |
assume ?R thus ?L |
|
6544 |
apply (clarify) |
|
6545 |
by (induct rule: list_induct2) (auto intro: listrel.intros) |
|
6546 |
qed |
|
6547 |
||
67091 | 6548 |
lemma listrel_iff_nth: "(xs,ys) \<in> listrel r \<longleftrightarrow> |
6549 |
length xs = length ys \<and> (\<forall>n < length xs. (xs!n, ys!n) \<in> r)" (is "?L \<longleftrightarrow> ?R") |
|
40230 | 6550 |
by (auto simp add: all_set_conv_all_nth listrel_iff_zip) |
6551 |
||
6552 |
||
15302 | 6553 |
lemma listrel_mono: "r \<subseteq> s \<Longrightarrow> listrel r \<subseteq> listrel s" |
68719 | 6554 |
by (meson listrel_iff_nth subrelI subset_eq) |
15302 | 6555 |
|
6556 |
lemma listrel_subset: "r \<subseteq> A \<times> A \<Longrightarrow> listrel r \<subseteq> lists A \<times> lists A" |
|
64963 | 6557 |
apply clarify |
6558 |
apply (erule listrel.induct, auto) |
|
15302 | 6559 |
done |
6560 |
||
64963 | 6561 |
lemma listrel_refl_on: "refl_on A r \<Longrightarrow> refl_on (lists A) (listrel r)" |
30198 | 6562 |
apply (simp add: refl_on_def listrel_subset Ball_def) |
64963 | 6563 |
apply (rule allI) |
6564 |
apply (induct_tac x) |
|
23740 | 6565 |
apply (auto intro: listrel.intros) |
15302 | 6566 |
done |
6567 |
||
64963 | 6568 |
lemma listrel_sym: "sym r \<Longrightarrow> sym (listrel r)" |
68719 | 6569 |
by (simp add: listrel_iff_nth sym_def) |
15302 | 6570 |
|
64963 | 6571 |
lemma listrel_trans: "trans r \<Longrightarrow> trans (listrel r)" |
15302 | 6572 |
apply (simp add: trans_def) |
64963 | 6573 |
apply (intro allI) |
6574 |
apply (rule impI) |
|
6575 |
apply (erule listrel.induct) |
|
23740 | 6576 |
apply (blast intro: listrel.intros)+ |
15302 | 6577 |
done |
6578 |
||
6579 |
theorem equiv_listrel: "equiv A r \<Longrightarrow> equiv (lists A) (listrel r)" |
|
64963 | 6580 |
by (simp add: equiv_def listrel_refl_on listrel_sym listrel_trans) |
15302 | 6581 |
|
67613 | 6582 |
lemma listrel_rtrancl_refl[iff]: "(xs,xs) \<in> listrel(r\<^sup>*)" |
40230 | 6583 |
using listrel_refl_on[of UNIV, OF refl_rtrancl] |
6584 |
by(auto simp: refl_on_def) |
|
6585 |
||
6586 |
lemma listrel_rtrancl_trans: |
|
67613 | 6587 |
"\<lbrakk>(xs,ys) \<in> listrel(r\<^sup>*); (ys,zs) \<in> listrel(r\<^sup>*)\<rbrakk> |
6588 |
\<Longrightarrow> (xs,zs) \<in> listrel(r\<^sup>*)" |
|
40230 | 6589 |
by (metis listrel_trans trans_def trans_rtrancl) |
6590 |
||
6591 |
||
15302 | 6592 |
lemma listrel_Nil [simp]: "listrel r `` {[]} = {[]}" |
23740 | 6593 |
by (blast intro: listrel.intros) |
15302 | 6594 |
|
6595 |
lemma listrel_Cons: |
|
33318
ddd97d9dfbfb
moved Nat_Transfer before Divides; distributed Nat_Transfer setup accordingly
haftmann
parents:
32960
diff
changeset
|
6596 |
"listrel r `` {x#xs} = set_Cons (r``{x}) (listrel r `` {xs})" |
ddd97d9dfbfb
moved Nat_Transfer before Divides; distributed Nat_Transfer setup accordingly
haftmann
parents:
32960
diff
changeset
|
6597 |
by (auto simp add: set_Cons_def intro: listrel.intros) |
15302 | 6598 |
|
69593 | 6599 |
text \<open>Relating \<^term>\<open>listrel1\<close>, \<^term>\<open>listrel\<close> and closures:\<close> |
40230 | 6600 |
|
6601 |
lemma listrel1_rtrancl_subset_rtrancl_listrel1: |
|
67613 | 6602 |
"listrel1 (r\<^sup>*) \<subseteq> (listrel1 r)\<^sup>*" |
40230 | 6603 |
proof (rule subrelI) |
67613 | 6604 |
fix xs ys assume 1: "(xs,ys) \<in> listrel1 (r\<^sup>*)" |
40230 | 6605 |
{ fix x y us vs |
67613 | 6606 |
have "(x,y) \<in> r\<^sup>* \<Longrightarrow> (us @ x # vs, us @ y # vs) \<in> (listrel1 r)\<^sup>*" |
40230 | 6607 |
proof(induct rule: rtrancl.induct) |
6608 |
case rtrancl_refl show ?case by simp |
|
6609 |
next |
|
6610 |
case rtrancl_into_rtrancl thus ?case |
|
6611 |
by (metis listrel1I rtrancl.rtrancl_into_rtrancl) |
|
6612 |
qed } |
|
67613 | 6613 |
thus "(xs,ys) \<in> (listrel1 r)\<^sup>*" using 1 by(blast elim: listrel1E) |
40230 | 6614 |
qed |
6615 |
||
67613 | 6616 |
lemma rtrancl_listrel1_eq_len: "(x,y) \<in> (listrel1 r)\<^sup>* \<Longrightarrow> length x = length y" |
40230 | 6617 |
by (induct rule: rtrancl.induct) (auto intro: listrel1_eq_len) |
6618 |
||
6619 |
lemma rtrancl_listrel1_ConsI1: |
|
67613 | 6620 |
"(xs,ys) \<in> (listrel1 r)\<^sup>* \<Longrightarrow> (x#xs,x#ys) \<in> (listrel1 r)\<^sup>*" |
40230 | 6621 |
apply(induct rule: rtrancl.induct) |
6622 |
apply simp |
|
6623 |
by (metis listrel1I2 rtrancl.rtrancl_into_rtrancl) |
|
6624 |
||
6625 |
lemma rtrancl_listrel1_ConsI2: |
|
67613 | 6626 |
"(x,y) \<in> r\<^sup>* \<Longrightarrow> (xs, ys) \<in> (listrel1 r)\<^sup>* |
6627 |
\<Longrightarrow> (x # xs, y # ys) \<in> (listrel1 r)\<^sup>*" |
|
64963 | 6628 |
by (blast intro: rtrancl_trans rtrancl_listrel1_ConsI1 |
40230 | 6629 |
subsetD[OF listrel1_rtrancl_subset_rtrancl_listrel1 listrel1I1]) |
6630 |
||
6631 |
lemma listrel1_subset_listrel: |
|
6632 |
"r \<subseteq> r' \<Longrightarrow> refl r' \<Longrightarrow> listrel1 r \<subseteq> listrel(r')" |
|
6633 |
by(auto elim!: listrel1E simp add: listrel_iff_zip set_zip refl_on_def) |
|
6634 |
||
6635 |
lemma listrel_reflcl_if_listrel1: |
|
67613 | 6636 |
"(xs,ys) \<in> listrel1 r \<Longrightarrow> (xs,ys) \<in> listrel(r\<^sup>*)" |
40230 | 6637 |
by(erule listrel1E)(auto simp add: listrel_iff_zip set_zip) |
6638 |
||
67613 | 6639 |
lemma listrel_rtrancl_eq_rtrancl_listrel1: "listrel (r\<^sup>*) = (listrel1 r)\<^sup>*" |
40230 | 6640 |
proof |
67613 | 6641 |
{ fix x y assume "(x,y) \<in> listrel (r\<^sup>*)" |
6642 |
then have "(x,y) \<in> (listrel1 r)\<^sup>*" |
|
40230 | 6643 |
by induct (auto intro: rtrancl_listrel1_ConsI2) } |
67613 | 6644 |
then show "listrel (r\<^sup>*) \<subseteq> (listrel1 r)\<^sup>*" |
40230 | 6645 |
by (rule subrelI) |
6646 |
next |
|
67613 | 6647 |
show "listrel (r\<^sup>*) \<supseteq> (listrel1 r)\<^sup>*" |
40230 | 6648 |
proof(rule subrelI) |
67613 | 6649 |
fix xs ys assume "(xs,ys) \<in> (listrel1 r)\<^sup>*" |
6650 |
then show "(xs,ys) \<in> listrel (r\<^sup>*)" |
|
40230 | 6651 |
proof induct |
6652 |
case base show ?case by(auto simp add: listrel_iff_zip set_zip) |
|
6653 |
next |
|
6654 |
case (step ys zs) |
|
56085 | 6655 |
thus ?case by (metis listrel_reflcl_if_listrel1 listrel_rtrancl_trans) |
40230 | 6656 |
qed |
6657 |
qed |
|
6658 |
qed |
|
6659 |
||
6660 |
lemma rtrancl_listrel1_if_listrel: |
|
67613 | 6661 |
"(xs,ys) \<in> listrel r \<Longrightarrow> (xs,ys) \<in> (listrel1 r)\<^sup>*" |
40230 | 6662 |
by(metis listrel_rtrancl_eq_rtrancl_listrel1 subsetD[OF listrel_mono] r_into_rtrancl subsetI) |
6663 |
||
67613 | 6664 |
lemma listrel_subset_rtrancl_listrel1: "listrel r \<subseteq> (listrel1 r)\<^sup>*" |
40230 | 6665 |
by(fast intro:rtrancl_listrel1_if_listrel) |
6666 |
||
15302 | 6667 |
|
60758 | 6668 |
subsection \<open>Size function\<close> |
26749
397a1aeede7d
* New attribute "termination_simp": Simp rules for termination proofs
krauss
parents:
26734
diff
changeset
|
6669 |
|
56643
41d3596d8a64
move size hooks together, with new one preceding old one and sharing same theory data
blanchet
parents:
56545
diff
changeset
|
6670 |
lemma [measure_function]: "is_measure f \<Longrightarrow> is_measure (size_list f)" |
26875
e18574413bc4
Measure functions can now be declared via special rules, allowing for a
krauss
parents:
26795
diff
changeset
|
6671 |
by (rule is_measure_trivial) |
e18574413bc4
Measure functions can now be declared via special rules, allowing for a
krauss
parents:
26795
diff
changeset
|
6672 |
|
56643
41d3596d8a64
move size hooks together, with new one preceding old one and sharing same theory data
blanchet
parents:
56545
diff
changeset
|
6673 |
lemma [measure_function]: "is_measure f \<Longrightarrow> is_measure (size_option f)" |
26875
e18574413bc4
Measure functions can now be declared via special rules, allowing for a
krauss
parents:
26795
diff
changeset
|
6674 |
by (rule is_measure_trivial) |
e18574413bc4
Measure functions can now be declared via special rules, allowing for a
krauss
parents:
26795
diff
changeset
|
6675 |
|
64963 | 6676 |
lemma size_list_estimation[termination_simp]: |
56643
41d3596d8a64
move size hooks together, with new one preceding old one and sharing same theory data
blanchet
parents:
56545
diff
changeset
|
6677 |
"x \<in> set xs \<Longrightarrow> y < f x \<Longrightarrow> y < size_list f xs" |
26749
397a1aeede7d
* New attribute "termination_simp": Simp rules for termination proofs
krauss
parents:
26734
diff
changeset
|
6678 |
by (induct xs) auto |
397a1aeede7d
* New attribute "termination_simp": Simp rules for termination proofs
krauss
parents:
26734
diff
changeset
|
6679 |
|
64963 | 6680 |
lemma size_list_estimation'[termination_simp]: |
56643
41d3596d8a64
move size hooks together, with new one preceding old one and sharing same theory data
blanchet
parents:
56545
diff
changeset
|
6681 |
"x \<in> set xs \<Longrightarrow> y \<le> f x \<Longrightarrow> y \<le> size_list f xs" |
26875
e18574413bc4
Measure functions can now be declared via special rules, allowing for a
krauss
parents:
26795
diff
changeset
|
6682 |
by (induct xs) auto |
e18574413bc4
Measure functions can now be declared via special rules, allowing for a
krauss
parents:
26795
diff
changeset
|
6683 |
|
67091 | 6684 |
lemma size_list_map[simp]: "size_list f (map g xs) = size_list (f \<circ> g) xs" |
26875
e18574413bc4
Measure functions can now be declared via special rules, allowing for a
krauss
parents:
26795
diff
changeset
|
6685 |
by (induct xs) auto |
e18574413bc4
Measure functions can now be declared via special rules, allowing for a
krauss
parents:
26795
diff
changeset
|
6686 |
|
56643
41d3596d8a64
move size hooks together, with new one preceding old one and sharing same theory data
blanchet
parents:
56545
diff
changeset
|
6687 |
lemma size_list_append[simp]: "size_list f (xs @ ys) = size_list f xs + size_list f ys" |
44619
fd520fa2fb09
adding list_size_append (thanks to Rene Thiemann)
bulwahn
parents:
44618
diff
changeset
|
6688 |
by (induct xs, auto) |
fd520fa2fb09
adding list_size_append (thanks to Rene Thiemann)
bulwahn
parents:
44618
diff
changeset
|
6689 |
|
64963 | 6690 |
lemma size_list_pointwise[termination_simp]: |
56643
41d3596d8a64
move size hooks together, with new one preceding old one and sharing same theory data
blanchet
parents:
56545
diff
changeset
|
6691 |
"(\<And>x. x \<in> set xs \<Longrightarrow> f x \<le> g x) \<Longrightarrow> size_list f xs \<le> size_list g xs" |
26875
e18574413bc4
Measure functions can now be declared via special rules, allowing for a
krauss
parents:
26795
diff
changeset
|
6692 |
by (induct xs) force+ |
26749
397a1aeede7d
* New attribute "termination_simp": Simp rules for termination proofs
krauss
parents:
26734
diff
changeset
|
6693 |
|
31048
ac146fc38b51
refined HOL string theories and corresponding ML fragments
haftmann
parents:
31022
diff
changeset
|
6694 |
|
60758 | 6695 |
subsection \<open>Monad operation\<close> |
46143 | 6696 |
|
6697 |
definition bind :: "'a list \<Rightarrow> ('a \<Rightarrow> 'b list) \<Rightarrow> 'b list" where |
|
50548 | 6698 |
"bind xs f = concat (map f xs)" |
46143 | 6699 |
|
6700 |
hide_const (open) bind |
|
6701 |
||
6702 |
lemma bind_simps [simp]: |
|
6703 |
"List.bind [] f = []" |
|
6704 |
"List.bind (x # xs) f = f x @ List.bind xs f" |
|
6705 |
by (simp_all add: bind_def) |
|
6706 |
||
63099
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents:
63092
diff
changeset
|
6707 |
lemma list_bind_cong [fundef_cong]: |
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents:
63092
diff
changeset
|
6708 |
assumes "xs = ys" "(\<And>x. x \<in> set xs \<Longrightarrow> f x = g x)" |
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents:
63092
diff
changeset
|
6709 |
shows "List.bind xs f = List.bind ys g" |
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents:
63092
diff
changeset
|
6710 |
proof - |
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents:
63092
diff
changeset
|
6711 |
from assms(2) have "List.bind xs f = List.bind xs g" |
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents:
63092
diff
changeset
|
6712 |
by (induction xs) simp_all |
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents:
63092
diff
changeset
|
6713 |
with assms(1) show ?thesis by simp |
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents:
63092
diff
changeset
|
6714 |
qed |
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents:
63092
diff
changeset
|
6715 |
|
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents:
63092
diff
changeset
|
6716 |
lemma set_list_bind: "set (List.bind xs f) = (\<Union>x\<in>set xs. set (f x))" |
64963 | 6717 |
by (induction xs) simp_all |
63099
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents:
63092
diff
changeset
|
6718 |
|
46143 | 6719 |
|
60758 | 6720 |
subsection \<open>Code generation\<close> |
6721 |
||
69593 | 6722 |
text\<open>Optional tail recursive version of \<^const>\<open>map\<close>. Can avoid |
60758 | 6723 |
stack overflow in some target languages.\<close> |
51875
dafd097dd1f4
tail recursive version of map, for code generation, optionally
nipkow
parents:
51738
diff
changeset
|
6724 |
|
dafd097dd1f4
tail recursive version of map, for code generation, optionally
nipkow
parents:
51738
diff
changeset
|
6725 |
fun map_tailrec_rev :: "('a \<Rightarrow> 'b) \<Rightarrow> 'a list \<Rightarrow> 'b list \<Rightarrow> 'b list" where |
dafd097dd1f4
tail recursive version of map, for code generation, optionally
nipkow
parents:
51738
diff
changeset
|
6726 |
"map_tailrec_rev f [] bs = bs" | |
dafd097dd1f4
tail recursive version of map, for code generation, optionally
nipkow
parents:
51738
diff
changeset
|
6727 |
"map_tailrec_rev f (a#as) bs = map_tailrec_rev f as (f a # bs)" |
dafd097dd1f4
tail recursive version of map, for code generation, optionally
nipkow
parents:
51738
diff
changeset
|
6728 |
|
dafd097dd1f4
tail recursive version of map, for code generation, optionally
nipkow
parents:
51738
diff
changeset
|
6729 |
lemma map_tailrec_rev: |
dafd097dd1f4
tail recursive version of map, for code generation, optionally
nipkow
parents:
51738
diff
changeset
|
6730 |
"map_tailrec_rev f as bs = rev(map f as) @ bs" |
dafd097dd1f4
tail recursive version of map, for code generation, optionally
nipkow
parents:
51738
diff
changeset
|
6731 |
by(induction as arbitrary: bs) simp_all |
dafd097dd1f4
tail recursive version of map, for code generation, optionally
nipkow
parents:
51738
diff
changeset
|
6732 |
|
dafd097dd1f4
tail recursive version of map, for code generation, optionally
nipkow
parents:
51738
diff
changeset
|
6733 |
definition map_tailrec :: "('a \<Rightarrow> 'b) \<Rightarrow> 'a list \<Rightarrow> 'b list" where |
dafd097dd1f4
tail recursive version of map, for code generation, optionally
nipkow
parents:
51738
diff
changeset
|
6734 |
"map_tailrec f as = rev (map_tailrec_rev f as [])" |
dafd097dd1f4
tail recursive version of map, for code generation, optionally
nipkow
parents:
51738
diff
changeset
|
6735 |
|
60758 | 6736 |
text\<open>Code equation:\<close> |
51875
dafd097dd1f4
tail recursive version of map, for code generation, optionally
nipkow
parents:
51738
diff
changeset
|
6737 |
lemma map_eq_map_tailrec: "map = map_tailrec" |
dafd097dd1f4
tail recursive version of map, for code generation, optionally
nipkow
parents:
51738
diff
changeset
|
6738 |
by(simp add: fun_eq_iff map_tailrec_def map_tailrec_rev) |
dafd097dd1f4
tail recursive version of map, for code generation, optionally
nipkow
parents:
51738
diff
changeset
|
6739 |
|
dafd097dd1f4
tail recursive version of map, for code generation, optionally
nipkow
parents:
51738
diff
changeset
|
6740 |
|
60758 | 6741 |
subsubsection \<open>Counterparts for set-related operations\<close> |
37605
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
6742 |
|
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
6743 |
definition member :: "'a list \<Rightarrow> 'a \<Rightarrow> bool" where |
50548 | 6744 |
[code_abbrev]: "member xs x \<longleftrightarrow> x \<in> set xs" |
37605
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
6745 |
|
60758 | 6746 |
text \<open> |
61799 | 6747 |
Use \<open>member\<close> only for generating executable code. Otherwise use |
69593 | 6748 |
\<^prop>\<open>x \<in> set xs\<close> instead --- it is much easier to reason about. |
60758 | 6749 |
\<close> |
37605
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
6750 |
|
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
6751 |
lemma member_rec [code]: |
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
6752 |
"member (x # xs) y \<longleftrightarrow> x = y \<or> member xs y" |
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
6753 |
"member [] y \<longleftrightarrow> False" |
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
6754 |
by (auto simp add: member_def) |
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
6755 |
|
46030
51b2f3412a03
attribute code_abbrev superseedes code_unfold_post; tuned text
haftmann
parents:
45993
diff
changeset
|
6756 |
lemma in_set_member (* FIXME delete candidate *): |
37605
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
6757 |
"x \<in> set xs \<longleftrightarrow> member xs x" |
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
6758 |
by (simp add: member_def) |
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
6759 |
|
62328 | 6760 |
lemmas list_all_iff [code_abbrev] = fun_cong[OF list.pred_set] |
37605
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
6761 |
|
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
6762 |
definition list_ex :: "('a \<Rightarrow> bool) \<Rightarrow> 'a list \<Rightarrow> bool" where |
50548 | 6763 |
list_ex_iff [code_abbrev]: "list_ex P xs \<longleftrightarrow> Bex (set xs) P" |
46030
51b2f3412a03
attribute code_abbrev superseedes code_unfold_post; tuned text
haftmann
parents:
45993
diff
changeset
|
6764 |
|
51b2f3412a03
attribute code_abbrev superseedes code_unfold_post; tuned text
haftmann
parents:
45993
diff
changeset
|
6765 |
definition list_ex1 :: "('a \<Rightarrow> bool) \<Rightarrow> 'a list \<Rightarrow> bool" where |
50548 | 6766 |
list_ex1_iff [code_abbrev]: "list_ex1 P xs \<longleftrightarrow> (\<exists>! x. x \<in> set xs \<and> P x)" |
40652 | 6767 |
|
60758 | 6768 |
text \<open> |
61799 | 6769 |
Usually you should prefer \<open>\<forall>x\<in>set xs\<close>, \<open>\<exists>x\<in>set xs\<close> |
69593 | 6770 |
and \<open>\<exists>!x. x\<in>set xs \<and> _\<close> over \<^const>\<open>list_all\<close>, \<^const>\<open>list_ex\<close> |
6771 |
and \<^const>\<open>list_ex1\<close> in specifications. |
|
60758 | 6772 |
\<close> |
37605
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
6773 |
|
56527
907f04603177
make list_all an abbreviation of pred_list - prevent duplication
kuncar
parents:
56525
diff
changeset
|
6774 |
lemma list_all_simps [code]: |
37605
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
6775 |
"list_all P (x # xs) \<longleftrightarrow> P x \<and> list_all P xs" |
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
6776 |
"list_all P [] \<longleftrightarrow> True" |
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
6777 |
by (simp_all add: list_all_iff) |
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
6778 |
|
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
6779 |
lemma list_ex_simps [simp, code]: |
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
6780 |
"list_ex P (x # xs) \<longleftrightarrow> P x \<or> list_ex P xs" |
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
6781 |
"list_ex P [] \<longleftrightarrow> False" |
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
6782 |
by (simp_all add: list_ex_iff) |
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
6783 |
|
40652 | 6784 |
lemma list_ex1_simps [simp, code]: |
6785 |
"list_ex1 P [] = False" |
|
6786 |
"list_ex1 P (x # xs) = (if P x then list_all (\<lambda>y. \<not> P y \<or> x = y) xs else list_ex1 P xs)" |
|
46030
51b2f3412a03
attribute code_abbrev superseedes code_unfold_post; tuned text
haftmann
parents:
45993
diff
changeset
|
6787 |
by (auto simp add: list_ex1_iff list_all_iff) |
51b2f3412a03
attribute code_abbrev superseedes code_unfold_post; tuned text
haftmann
parents:
45993
diff
changeset
|
6788 |
|
51b2f3412a03
attribute code_abbrev superseedes code_unfold_post; tuned text
haftmann
parents:
45993
diff
changeset
|
6789 |
lemma Ball_set_list_all: (* FIXME delete candidate *) |
37605
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
6790 |
"Ball (set xs) P \<longleftrightarrow> list_all P xs" |
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
6791 |
by (simp add: list_all_iff) |
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
6792 |
|
46030
51b2f3412a03
attribute code_abbrev superseedes code_unfold_post; tuned text
haftmann
parents:
45993
diff
changeset
|
6793 |
lemma Bex_set_list_ex: (* FIXME delete candidate *) |
37605
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
6794 |
"Bex (set xs) P \<longleftrightarrow> list_ex P xs" |
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
6795 |
by (simp add: list_ex_iff) |
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
6796 |
|
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
6797 |
lemma list_all_append [simp]: |
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
6798 |
"list_all P (xs @ ys) \<longleftrightarrow> list_all P xs \<and> list_all P ys" |
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
6799 |
by (auto simp add: list_all_iff) |
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
6800 |
|
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
6801 |
lemma list_ex_append [simp]: |
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
6802 |
"list_ex P (xs @ ys) \<longleftrightarrow> list_ex P xs \<or> list_ex P ys" |
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
6803 |
by (auto simp add: list_ex_iff) |
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
6804 |
|
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
6805 |
lemma list_all_rev [simp]: |
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
6806 |
"list_all P (rev xs) \<longleftrightarrow> list_all P xs" |
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
6807 |
by (simp add: list_all_iff) |
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
6808 |
|
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
6809 |
lemma list_ex_rev [simp]: |
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
6810 |
"list_ex P (rev xs) \<longleftrightarrow> list_ex P xs" |
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
6811 |
by (simp add: list_ex_iff) |
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
6812 |
|
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
6813 |
lemma list_all_length: |
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
6814 |
"list_all P xs \<longleftrightarrow> (\<forall>n < length xs. P (xs ! n))" |
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
6815 |
by (auto simp add: list_all_iff set_conv_nth) |
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
6816 |
|
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
6817 |
lemma list_ex_length: |
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
6818 |
"list_ex P xs \<longleftrightarrow> (\<exists>n < length xs. P (xs ! n))" |
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
6819 |
by (auto simp add: list_ex_iff set_conv_nth) |
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
6820 |
|
62328 | 6821 |
lemmas list_all_cong [fundef_cong] = list.pred_cong |
37605
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
6822 |
|
47131 | 6823 |
lemma list_ex_cong [fundef_cong]: |
37605
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
6824 |
"xs = ys \<Longrightarrow> (\<And>x. x \<in> set ys \<Longrightarrow> f x = g x) \<Longrightarrow> list_ex f xs = list_ex g ys" |
47131 | 6825 |
by (simp add: list_ex_iff) |
37605
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
6826 |
|
50548 | 6827 |
definition can_select :: "('a \<Rightarrow> bool) \<Rightarrow> 'a set \<Rightarrow> bool" where |
6828 |
[code_abbrev]: "can_select P A = (\<exists>!x\<in>A. P x)" |
|
49948
744934b818c7
moved quite generic material from theory Enum to more appropriate places
haftmann
parents:
49808
diff
changeset
|
6829 |
|
744934b818c7
moved quite generic material from theory Enum to more appropriate places
haftmann
parents:
49808
diff
changeset
|
6830 |
lemma can_select_set_list_ex1 [code]: |
744934b818c7
moved quite generic material from theory Enum to more appropriate places
haftmann
parents:
49808
diff
changeset
|
6831 |
"can_select P (set A) = list_ex1 P A" |
744934b818c7
moved quite generic material from theory Enum to more appropriate places
haftmann
parents:
49808
diff
changeset
|
6832 |
by (simp add: list_ex1_iff can_select_def) |
744934b818c7
moved quite generic material from theory Enum to more appropriate places
haftmann
parents:
49808
diff
changeset
|
6833 |
|
744934b818c7
moved quite generic material from theory Enum to more appropriate places
haftmann
parents:
49808
diff
changeset
|
6834 |
|
60758 | 6835 |
text \<open>Executable checks for relations on sets\<close> |
46313 | 6836 |
|
6837 |
definition listrel1p :: "('a \<Rightarrow> 'a \<Rightarrow> bool) \<Rightarrow> 'a list \<Rightarrow> 'a list \<Rightarrow> bool" where |
|
6838 |
"listrel1p r xs ys = ((xs, ys) \<in> listrel1 {(x, y). r x y})" |
|
6839 |
||
6840 |
lemma [code_unfold]: |
|
6841 |
"(xs, ys) \<in> listrel1 r = listrel1p (\<lambda>x y. (x, y) \<in> r) xs ys" |
|
6842 |
unfolding listrel1p_def by auto |
|
6843 |
||
6844 |
lemma [code]: |
|
6845 |
"listrel1p r [] xs = False" |
|
6846 |
"listrel1p r xs [] = False" |
|
6847 |
"listrel1p r (x # xs) (y # ys) \<longleftrightarrow> |
|
6848 |
r x y \<and> xs = ys \<or> x = y \<and> listrel1p r xs ys" |
|
6849 |
by (simp add: listrel1p_def)+ |
|
6850 |
||
6851 |
definition |
|
6852 |
lexordp :: "('a \<Rightarrow> 'a \<Rightarrow> bool) \<Rightarrow> 'a list \<Rightarrow> 'a list \<Rightarrow> bool" where |
|
6853 |
"lexordp r xs ys = ((xs, ys) \<in> lexord {(x, y). r x y})" |
|
6854 |
||
6855 |
lemma [code_unfold]: |
|
6856 |
"(xs, ys) \<in> lexord r = lexordp (\<lambda>x y. (x, y) \<in> r) xs ys" |
|
6857 |
unfolding lexordp_def by auto |
|
6858 |
||
6859 |
lemma [code]: |
|
6860 |
"lexordp r xs [] = False" |
|
6861 |
"lexordp r [] (y#ys) = True" |
|
67091 | 6862 |
"lexordp r (x # xs) (y # ys) = (r x y \<or> (x = y \<and> lexordp r xs ys))" |
46313 | 6863 |
unfolding lexordp_def by auto |
6864 |
||
60758 | 6865 |
text \<open>Bounded quantification and summation over nats.\<close> |
37605
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
6866 |
|
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
6867 |
lemma atMost_upto [code_unfold]: |
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
6868 |
"{..n} = set [0..<Suc n]" |
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
6869 |
by auto |
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
6870 |
|
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
6871 |
lemma atLeast_upt [code_unfold]: |
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
6872 |
"{..<n} = set [0..<n]" |
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
6873 |
by auto |
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
6874 |
|
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
6875 |
lemma greaterThanLessThan_upt [code_unfold]: |
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
6876 |
"{n<..<m} = set [Suc n..<m]" |
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
6877 |
by auto |
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
6878 |
|
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
6879 |
lemmas atLeastLessThan_upt [code_unfold] = set_upt [symmetric] |
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
6880 |
|
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
6881 |
lemma greaterThanAtMost_upt [code_unfold]: |
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
6882 |
"{n<..m} = set [Suc n..<Suc m]" |
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
6883 |
by auto |
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
6884 |
|
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
6885 |
lemma atLeastAtMost_upt [code_unfold]: |
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
6886 |
"{n..m} = set [n..<Suc m]" |
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
6887 |
by auto |
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
6888 |
|
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
6889 |
lemma all_nat_less_eq [code_unfold]: |
61076 | 6890 |
"(\<forall>m<n::nat. P m) \<longleftrightarrow> (\<forall>m \<in> {0..<n}. P m)" |
37605
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
6891 |
by auto |
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
6892 |
|
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
6893 |
lemma ex_nat_less_eq [code_unfold]: |
61076 | 6894 |
"(\<exists>m<n::nat. P m) \<longleftrightarrow> (\<exists>m \<in> {0..<n}. P m)" |
37605
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
6895 |
by auto |
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
6896 |
|
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
6897 |
lemma all_nat_less [code_unfold]: |
61076 | 6898 |
"(\<forall>m\<le>n::nat. P m) \<longleftrightarrow> (\<forall>m \<in> {0..n}. P m)" |
37605
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
6899 |
by auto |
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
6900 |
|
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
6901 |
lemma ex_nat_less [code_unfold]: |
61076 | 6902 |
"(\<exists>m\<le>n::nat. P m) \<longleftrightarrow> (\<exists>m \<in> {0..n}. P m)" |
37605
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
6903 |
by auto |
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
6904 |
|
61799 | 6905 |
text\<open>Bounded \<open>LEAST\<close> operator:\<close> |
53954 | 6906 |
|
6907 |
definition "Bleast S P = (LEAST x. x \<in> S \<and> P x)" |
|
6908 |
||
6909 |
definition "abort_Bleast S P = (LEAST x. x \<in> S \<and> P x)" |
|
6910 |
||
54890
cb892d835803
fundamental treatment of undefined vs. universally partial replaces code_abort
haftmann
parents:
54885
diff
changeset
|
6911 |
declare [[code abort: abort_Bleast]] |
53954 | 6912 |
|
6913 |
lemma Bleast_code [code]: |
|
6914 |
"Bleast (set xs) P = (case filter P (sort xs) of |
|
6915 |
x#xs \<Rightarrow> x | |
|
6916 |
[] \<Rightarrow> abort_Bleast (set xs) P)" |
|
6917 |
proof (cases "filter P (sort xs)") |
|
6918 |
case Nil thus ?thesis by (simp add: Bleast_def abort_Bleast_def) |
|
6919 |
next |
|
6920 |
case (Cons x ys) |
|
6921 |
have "(LEAST x. x \<in> set xs \<and> P x) = x" |
|
6922 |
proof (rule Least_equality) |
|
6923 |
show "x \<in> set xs \<and> P x" |
|
6924 |
by (metis Cons Cons_eq_filter_iff in_set_conv_decomp set_sort) |
|
6925 |
next |
|
67613 | 6926 |
fix y assume "y \<in> set xs \<and> P y" |
6927 |
hence "y \<in> set (filter P xs)" by auto |
|
53954 | 6928 |
thus "x \<le> y" |
68109 | 6929 |
by (metis Cons eq_iff filter_sort set_ConsD set_sort sorted.simps(2) sorted_sort) |
53954 | 6930 |
qed |
6931 |
thus ?thesis using Cons by (simp add: Bleast_def) |
|
6932 |
qed |
|
6933 |
||
6934 |
declare Bleast_def[symmetric, code_unfold] |
|
6935 |
||
60758 | 6936 |
text \<open>Summation over ints.\<close> |
37605
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
6937 |
|
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
6938 |
lemma greaterThanLessThan_upto [code_unfold]: |
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
6939 |
"{i<..<j::int} = set [i+1..j - 1]" |
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
6940 |
by auto |
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
6941 |
|
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
6942 |
lemma atLeastLessThan_upto [code_unfold]: |
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
6943 |
"{i..<j::int} = set [i..j - 1]" |
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
6944 |
by auto |
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
6945 |
|
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
6946 |
lemma greaterThanAtMost_upto [code_unfold]: |
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
6947 |
"{i<..j::int} = set [i+1..j]" |
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
6948 |
by auto |
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
6949 |
|
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
6950 |
lemmas atLeastAtMost_upto [code_unfold] = set_upto [symmetric] |
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
6951 |
|
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
6952 |
|
60758 | 6953 |
subsubsection \<open>Optimizing by rewriting\<close> |
37605
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
6954 |
|
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
6955 |
definition null :: "'a list \<Rightarrow> bool" where |
46030
51b2f3412a03
attribute code_abbrev superseedes code_unfold_post; tuned text
haftmann
parents:
45993
diff
changeset
|
6956 |
[code_abbrev]: "null xs \<longleftrightarrow> xs = []" |
37605
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
6957 |
|
60758 | 6958 |
text \<open> |
69593 | 6959 |
Efficient emptyness check is implemented by \<^const>\<open>null\<close>. |
60758 | 6960 |
\<close> |
37605
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
6961 |
|
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
6962 |
lemma null_rec [code]: |
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
6963 |
"null (x # xs) \<longleftrightarrow> False" |
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
6964 |
"null [] \<longleftrightarrow> True" |
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
6965 |
by (simp_all add: null_def) |
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
6966 |
|
46030
51b2f3412a03
attribute code_abbrev superseedes code_unfold_post; tuned text
haftmann
parents:
45993
diff
changeset
|
6967 |
lemma eq_Nil_null: (* FIXME delete candidate *) |
37605
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
6968 |
"xs = [] \<longleftrightarrow> null xs" |
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
6969 |
by (simp add: null_def) |
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
6970 |
|
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
6971 |
lemma equal_Nil_null [code_unfold]: |
38857
97775f3e8722
renamed class/constant eq to equal; tuned some instantiations
haftmann
parents:
38715
diff
changeset
|
6972 |
"HOL.equal xs [] \<longleftrightarrow> null xs" |
53940
36cf426cb1c6
Added symmetric code_unfold-lemmas for null and is_none
lammich <lammich@in.tum.de>
parents:
53721
diff
changeset
|
6973 |
"HOL.equal [] = null" |
36cf426cb1c6
Added symmetric code_unfold-lemmas for null and is_none
lammich <lammich@in.tum.de>
parents:
53721
diff
changeset
|
6974 |
by (auto simp add: equal null_def) |
37605
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
6975 |
|
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
6976 |
definition maps :: "('a \<Rightarrow> 'b list) \<Rightarrow> 'a list \<Rightarrow> 'b list" where |
46030
51b2f3412a03
attribute code_abbrev superseedes code_unfold_post; tuned text
haftmann
parents:
45993
diff
changeset
|
6977 |
[code_abbrev]: "maps f xs = concat (map f xs)" |
37605
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
6978 |
|
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
6979 |
definition map_filter :: "('a \<Rightarrow> 'b option) \<Rightarrow> 'a list \<Rightarrow> 'b list" where |
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
6980 |
[code_post]: "map_filter f xs = map (the \<circ> f) (filter (\<lambda>x. f x \<noteq> None) xs)" |
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
6981 |
|
60758 | 6982 |
text \<open> |
69593 | 6983 |
Operations \<^const>\<open>maps\<close> and \<^const>\<open>map_filter\<close> avoid |
37605
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
6984 |
intermediate lists on execution -- do not use for proving. |
60758 | 6985 |
\<close> |
37605
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
6986 |
|
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
6987 |
lemma maps_simps [code]: |
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
6988 |
"maps f (x # xs) = f x @ maps f xs" |
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
6989 |
"maps f [] = []" |
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
6990 |
by (simp_all add: maps_def) |
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
6991 |
|
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
6992 |
lemma map_filter_simps [code]: |
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
6993 |
"map_filter f (x # xs) = (case f x of None \<Rightarrow> map_filter f xs | Some y \<Rightarrow> y # map_filter f xs)" |
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
6994 |
"map_filter f [] = []" |
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
6995 |
by (simp_all add: map_filter_def split: option.split) |
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
6996 |
|
46030
51b2f3412a03
attribute code_abbrev superseedes code_unfold_post; tuned text
haftmann
parents:
45993
diff
changeset
|
6997 |
lemma concat_map_maps: (* FIXME delete candidate *) |
37605
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
6998 |
"concat (map f xs) = maps f xs" |
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
6999 |
by (simp add: maps_def) |
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
7000 |
|
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
7001 |
lemma map_filter_map_filter [code_unfold]: |
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
7002 |
"map f (filter P xs) = map_filter (\<lambda>x. if P x then Some (f x) else None) xs" |
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
7003 |
by (simp add: map_filter_def) |
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
7004 |
|
61799 | 7005 |
text \<open>Optimized code for \<open>\<forall>i\<in>{a..b::int}\<close> and \<open>\<forall>n:{a..<b::nat}\<close> |
7006 |
and similiarly for \<open>\<exists>\<close>.\<close> |
|
37605
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
7007 |
|
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
7008 |
definition all_interval_nat :: "(nat \<Rightarrow> bool) \<Rightarrow> nat \<Rightarrow> nat \<Rightarrow> bool" where |
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
7009 |
"all_interval_nat P i j \<longleftrightarrow> (\<forall>n \<in> {i..<j}. P n)" |
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
7010 |
|
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
7011 |
lemma [code]: |
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
7012 |
"all_interval_nat P i j \<longleftrightarrow> i \<ge> j \<or> P i \<and> all_interval_nat P (Suc i) j" |
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
7013 |
proof - |
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
7014 |
have *: "\<And>n. P i \<Longrightarrow> \<forall>n\<in>{Suc i..<j}. P n \<Longrightarrow> i \<le> n \<Longrightarrow> n < j \<Longrightarrow> P n" |
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
7015 |
proof - |
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
7016 |
fix n |
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
7017 |
assume "P i" "\<forall>n\<in>{Suc i..<j}. P n" "i \<le> n" "n < j" |
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
7018 |
then show "P n" by (cases "n = i") simp_all |
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
7019 |
qed |
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
7020 |
show ?thesis by (auto simp add: all_interval_nat_def intro: *) |
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
7021 |
qed |
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
7022 |
|
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
7023 |
lemma list_all_iff_all_interval_nat [code_unfold]: |
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
7024 |
"list_all P [i..<j] \<longleftrightarrow> all_interval_nat P i j" |
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
7025 |
by (simp add: list_all_iff all_interval_nat_def) |
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
7026 |
|
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
7027 |
lemma list_ex_iff_not_all_inverval_nat [code_unfold]: |
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
7028 |
"list_ex P [i..<j] \<longleftrightarrow> \<not> (all_interval_nat (Not \<circ> P) i j)" |
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
7029 |
by (simp add: list_ex_iff all_interval_nat_def) |
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
7030 |
|
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
7031 |
definition all_interval_int :: "(int \<Rightarrow> bool) \<Rightarrow> int \<Rightarrow> int \<Rightarrow> bool" where |
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
7032 |
"all_interval_int P i j \<longleftrightarrow> (\<forall>k \<in> {i..j}. P k)" |
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
7033 |
|
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
7034 |
lemma [code]: |
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
7035 |
"all_interval_int P i j \<longleftrightarrow> i > j \<or> P i \<and> all_interval_int P (i + 1) j" |
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
7036 |
proof - |
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
7037 |
have *: "\<And>k. P i \<Longrightarrow> \<forall>k\<in>{i+1..j}. P k \<Longrightarrow> i \<le> k \<Longrightarrow> k \<le> j \<Longrightarrow> P k" |
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
7038 |
proof - |
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
7039 |
fix k |
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
7040 |
assume "P i" "\<forall>k\<in>{i+1..j}. P k" "i \<le> k" "k \<le> j" |
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
7041 |
then show "P k" by (cases "k = i") simp_all |
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
7042 |
qed |
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
7043 |
show ?thesis by (auto simp add: all_interval_int_def intro: *) |
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
7044 |
qed |
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
7045 |
|
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
7046 |
lemma list_all_iff_all_interval_int [code_unfold]: |
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
7047 |
"list_all P [i..j] \<longleftrightarrow> all_interval_int P i j" |
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
7048 |
by (simp add: list_all_iff all_interval_int_def) |
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
7049 |
|
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
7050 |
lemma list_ex_iff_not_all_inverval_int [code_unfold]: |
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
7051 |
"list_ex P [i..j] \<longleftrightarrow> \<not> (all_interval_int (Not \<circ> P) i j)" |
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
7052 |
by (simp add: list_ex_iff all_interval_int_def) |
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
7053 |
|
69593 | 7054 |
text \<open>optimized code (tail-recursive) for \<^term>\<open>length\<close>\<close> |
49808
418991ce7567
tail-recursive implementation for length
Andreas Lochbihler
parents:
49757
diff
changeset
|
7055 |
|
418991ce7567
tail-recursive implementation for length
Andreas Lochbihler
parents:
49757
diff
changeset
|
7056 |
definition gen_length :: "nat \<Rightarrow> 'a list \<Rightarrow> nat" |
418991ce7567
tail-recursive implementation for length
Andreas Lochbihler
parents:
49757
diff
changeset
|
7057 |
where "gen_length n xs = n + length xs" |
418991ce7567
tail-recursive implementation for length
Andreas Lochbihler
parents:
49757
diff
changeset
|
7058 |
|
418991ce7567
tail-recursive implementation for length
Andreas Lochbihler
parents:
49757
diff
changeset
|
7059 |
lemma gen_length_code [code]: |
418991ce7567
tail-recursive implementation for length
Andreas Lochbihler
parents:
49757
diff
changeset
|
7060 |
"gen_length n [] = n" |
418991ce7567
tail-recursive implementation for length
Andreas Lochbihler
parents:
49757
diff
changeset
|
7061 |
"gen_length n (x # xs) = gen_length (Suc n) xs" |
418991ce7567
tail-recursive implementation for length
Andreas Lochbihler
parents:
49757
diff
changeset
|
7062 |
by(simp_all add: gen_length_def) |
418991ce7567
tail-recursive implementation for length
Andreas Lochbihler
parents:
49757
diff
changeset
|
7063 |
|
418991ce7567
tail-recursive implementation for length
Andreas Lochbihler
parents:
49757
diff
changeset
|
7064 |
declare list.size(3-4)[code del] |
418991ce7567
tail-recursive implementation for length
Andreas Lochbihler
parents:
49757
diff
changeset
|
7065 |
|
418991ce7567
tail-recursive implementation for length
Andreas Lochbihler
parents:
49757
diff
changeset
|
7066 |
lemma length_code [code]: "length = gen_length 0" |
418991ce7567
tail-recursive implementation for length
Andreas Lochbihler
parents:
49757
diff
changeset
|
7067 |
by(simp add: gen_length_def fun_eq_iff) |
418991ce7567
tail-recursive implementation for length
Andreas Lochbihler
parents:
49757
diff
changeset
|
7068 |
|
418991ce7567
tail-recursive implementation for length
Andreas Lochbihler
parents:
49757
diff
changeset
|
7069 |
hide_const (open) member null maps map_filter all_interval_nat all_interval_int gen_length |
37605
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
7070 |
|
49948
744934b818c7
moved quite generic material from theory Enum to more appropriate places
haftmann
parents:
49808
diff
changeset
|
7071 |
|
60758 | 7072 |
subsubsection \<open>Pretty lists\<close> |
7073 |
||
7074 |
ML \<open> |
|
50422
ee729dbd1b7f
avoid ML_file in large theory files to improve performance of dependency discovery of main HOL (approx. 1s CPU time) -- relevant for any application using it, e.g. small paper sessions;
wenzelm
parents:
50134
diff
changeset
|
7075 |
(* Code generation for list literals. *) |
ee729dbd1b7f
avoid ML_file in large theory files to improve performance of dependency discovery of main HOL (approx. 1s CPU time) -- relevant for any application using it, e.g. small paper sessions;
wenzelm
parents:
50134
diff
changeset
|
7076 |
|
ee729dbd1b7f
avoid ML_file in large theory files to improve performance of dependency discovery of main HOL (approx. 1s CPU time) -- relevant for any application using it, e.g. small paper sessions;
wenzelm
parents:
50134
diff
changeset
|
7077 |
signature LIST_CODE = |
ee729dbd1b7f
avoid ML_file in large theory files to improve performance of dependency discovery of main HOL (approx. 1s CPU time) -- relevant for any application using it, e.g. small paper sessions;
wenzelm
parents:
50134
diff
changeset
|
7078 |
sig |
ee729dbd1b7f
avoid ML_file in large theory files to improve performance of dependency discovery of main HOL (approx. 1s CPU time) -- relevant for any application using it, e.g. small paper sessions;
wenzelm
parents:
50134
diff
changeset
|
7079 |
val add_literal_list: string -> theory -> theory |
ee729dbd1b7f
avoid ML_file in large theory files to improve performance of dependency discovery of main HOL (approx. 1s CPU time) -- relevant for any application using it, e.g. small paper sessions;
wenzelm
parents:
50134
diff
changeset
|
7080 |
end; |
ee729dbd1b7f
avoid ML_file in large theory files to improve performance of dependency discovery of main HOL (approx. 1s CPU time) -- relevant for any application using it, e.g. small paper sessions;
wenzelm
parents:
50134
diff
changeset
|
7081 |
|
ee729dbd1b7f
avoid ML_file in large theory files to improve performance of dependency discovery of main HOL (approx. 1s CPU time) -- relevant for any application using it, e.g. small paper sessions;
wenzelm
parents:
50134
diff
changeset
|
7082 |
structure List_Code : LIST_CODE = |
ee729dbd1b7f
avoid ML_file in large theory files to improve performance of dependency discovery of main HOL (approx. 1s CPU time) -- relevant for any application using it, e.g. small paper sessions;
wenzelm
parents:
50134
diff
changeset
|
7083 |
struct |
ee729dbd1b7f
avoid ML_file in large theory files to improve performance of dependency discovery of main HOL (approx. 1s CPU time) -- relevant for any application using it, e.g. small paper sessions;
wenzelm
parents:
50134
diff
changeset
|
7084 |
|
ee729dbd1b7f
avoid ML_file in large theory files to improve performance of dependency discovery of main HOL (approx. 1s CPU time) -- relevant for any application using it, e.g. small paper sessions;
wenzelm
parents:
50134
diff
changeset
|
7085 |
open Basic_Code_Thingol; |
ee729dbd1b7f
avoid ML_file in large theory files to improve performance of dependency discovery of main HOL (approx. 1s CPU time) -- relevant for any application using it, e.g. small paper sessions;
wenzelm
parents:
50134
diff
changeset
|
7086 |
|
55148
7e1b7cb54114
avoid (now superfluous) indirect passing of constant names
haftmann
parents:
55147
diff
changeset
|
7087 |
fun implode_list t = |
50422
ee729dbd1b7f
avoid ML_file in large theory files to improve performance of dependency discovery of main HOL (approx. 1s CPU time) -- relevant for any application using it, e.g. small paper sessions;
wenzelm
parents:
50134
diff
changeset
|
7088 |
let |
69593 | 7089 |
fun dest_cons (IConst { sym = Code_Symbol.Constant \<^const_name>\<open>Cons\<close>, ... } `$ t1 `$ t2) = SOME (t1, t2) |
50422
ee729dbd1b7f
avoid ML_file in large theory files to improve performance of dependency discovery of main HOL (approx. 1s CPU time) -- relevant for any application using it, e.g. small paper sessions;
wenzelm
parents:
50134
diff
changeset
|
7090 |
| dest_cons _ = NONE; |
ee729dbd1b7f
avoid ML_file in large theory files to improve performance of dependency discovery of main HOL (approx. 1s CPU time) -- relevant for any application using it, e.g. small paper sessions;
wenzelm
parents:
50134
diff
changeset
|
7091 |
val (ts, t') = Code_Thingol.unfoldr dest_cons t; |
ee729dbd1b7f
avoid ML_file in large theory files to improve performance of dependency discovery of main HOL (approx. 1s CPU time) -- relevant for any application using it, e.g. small paper sessions;
wenzelm
parents:
50134
diff
changeset
|
7092 |
in case t' |
69593 | 7093 |
of IConst { sym = Code_Symbol.Constant \<^const_name>\<open>Nil\<close>, ... } => SOME ts |
50422
ee729dbd1b7f
avoid ML_file in large theory files to improve performance of dependency discovery of main HOL (approx. 1s CPU time) -- relevant for any application using it, e.g. small paper sessions;
wenzelm
parents:
50134
diff
changeset
|
7094 |
| _ => NONE |
ee729dbd1b7f
avoid ML_file in large theory files to improve performance of dependency discovery of main HOL (approx. 1s CPU time) -- relevant for any application using it, e.g. small paper sessions;
wenzelm
parents:
50134
diff
changeset
|
7095 |
end; |
ee729dbd1b7f
avoid ML_file in large theory files to improve performance of dependency discovery of main HOL (approx. 1s CPU time) -- relevant for any application using it, e.g. small paper sessions;
wenzelm
parents:
50134
diff
changeset
|
7096 |
|
68028 | 7097 |
fun print_list (target_fxy, target_cons) pr fxy t1 t2 = |
50422
ee729dbd1b7f
avoid ML_file in large theory files to improve performance of dependency discovery of main HOL (approx. 1s CPU time) -- relevant for any application using it, e.g. small paper sessions;
wenzelm
parents:
50134
diff
changeset
|
7098 |
Code_Printer.brackify_infix (target_fxy, Code_Printer.R) fxy ( |
ee729dbd1b7f
avoid ML_file in large theory files to improve performance of dependency discovery of main HOL (approx. 1s CPU time) -- relevant for any application using it, e.g. small paper sessions;
wenzelm
parents:
50134
diff
changeset
|
7099 |
pr (Code_Printer.INFX (target_fxy, Code_Printer.X)) t1, |
ee729dbd1b7f
avoid ML_file in large theory files to improve performance of dependency discovery of main HOL (approx. 1s CPU time) -- relevant for any application using it, e.g. small paper sessions;
wenzelm
parents:
50134
diff
changeset
|
7100 |
Code_Printer.str target_cons, |
ee729dbd1b7f
avoid ML_file in large theory files to improve performance of dependency discovery of main HOL (approx. 1s CPU time) -- relevant for any application using it, e.g. small paper sessions;
wenzelm
parents:
50134
diff
changeset
|
7101 |
pr (Code_Printer.INFX (target_fxy, Code_Printer.R)) t2 |
ee729dbd1b7f
avoid ML_file in large theory files to improve performance of dependency discovery of main HOL (approx. 1s CPU time) -- relevant for any application using it, e.g. small paper sessions;
wenzelm
parents:
50134
diff
changeset
|
7102 |
); |
ee729dbd1b7f
avoid ML_file in large theory files to improve performance of dependency discovery of main HOL (approx. 1s CPU time) -- relevant for any application using it, e.g. small paper sessions;
wenzelm
parents:
50134
diff
changeset
|
7103 |
|
ee729dbd1b7f
avoid ML_file in large theory files to improve performance of dependency discovery of main HOL (approx. 1s CPU time) -- relevant for any application using it, e.g. small paper sessions;
wenzelm
parents:
50134
diff
changeset
|
7104 |
fun add_literal_list target = |
ee729dbd1b7f
avoid ML_file in large theory files to improve performance of dependency discovery of main HOL (approx. 1s CPU time) -- relevant for any application using it, e.g. small paper sessions;
wenzelm
parents:
50134
diff
changeset
|
7105 |
let |
55148
7e1b7cb54114
avoid (now superfluous) indirect passing of constant names
haftmann
parents:
55147
diff
changeset
|
7106 |
fun pretty literals pr _ vars fxy [(t1, _), (t2, _)] = |
7e1b7cb54114
avoid (now superfluous) indirect passing of constant names
haftmann
parents:
55147
diff
changeset
|
7107 |
case Option.map (cons t1) (implode_list t2) |
50422
ee729dbd1b7f
avoid ML_file in large theory files to improve performance of dependency discovery of main HOL (approx. 1s CPU time) -- relevant for any application using it, e.g. small paper sessions;
wenzelm
parents:
50134
diff
changeset
|
7108 |
of SOME ts => |
ee729dbd1b7f
avoid ML_file in large theory files to improve performance of dependency discovery of main HOL (approx. 1s CPU time) -- relevant for any application using it, e.g. small paper sessions;
wenzelm
parents:
50134
diff
changeset
|
7109 |
Code_Printer.literal_list literals (map (pr vars Code_Printer.NOBR) ts) |
ee729dbd1b7f
avoid ML_file in large theory files to improve performance of dependency discovery of main HOL (approx. 1s CPU time) -- relevant for any application using it, e.g. small paper sessions;
wenzelm
parents:
50134
diff
changeset
|
7110 |
| NONE => |
68028 | 7111 |
print_list (Code_Printer.infix_cons literals) (pr vars) fxy t1 t2; |
52435
6646bb548c6b
migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
haftmann
parents:
52380
diff
changeset
|
7112 |
in |
69593 | 7113 |
Code_Target.set_printings (Code_Symbol.Constant (\<^const_name>\<open>Cons\<close>, |
55148
7e1b7cb54114
avoid (now superfluous) indirect passing of constant names
haftmann
parents:
55147
diff
changeset
|
7114 |
[(target, SOME (Code_Printer.complex_const_syntax (2, pretty)))])) |
50422
ee729dbd1b7f
avoid ML_file in large theory files to improve performance of dependency discovery of main HOL (approx. 1s CPU time) -- relevant for any application using it, e.g. small paper sessions;
wenzelm
parents:
50134
diff
changeset
|
7115 |
end |
ee729dbd1b7f
avoid ML_file in large theory files to improve performance of dependency discovery of main HOL (approx. 1s CPU time) -- relevant for any application using it, e.g. small paper sessions;
wenzelm
parents:
50134
diff
changeset
|
7116 |
|
ee729dbd1b7f
avoid ML_file in large theory files to improve performance of dependency discovery of main HOL (approx. 1s CPU time) -- relevant for any application using it, e.g. small paper sessions;
wenzelm
parents:
50134
diff
changeset
|
7117 |
end; |
60758 | 7118 |
\<close> |
31055
2cf6efca6c71
proper structures for list and string code generation stuff
haftmann
parents:
31048
diff
changeset
|
7119 |
|
52435
6646bb548c6b
migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
haftmann
parents:
52380
diff
changeset
|
7120 |
code_printing |
6646bb548c6b
migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
haftmann
parents:
52380
diff
changeset
|
7121 |
type_constructor list \<rightharpoonup> |
6646bb548c6b
migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
haftmann
parents:
52380
diff
changeset
|
7122 |
(SML) "_ list" |
6646bb548c6b
migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
haftmann
parents:
52380
diff
changeset
|
7123 |
and (OCaml) "_ list" |
6646bb548c6b
migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
haftmann
parents:
52380
diff
changeset
|
7124 |
and (Haskell) "![(_)]" |
6646bb548c6b
migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
haftmann
parents:
52380
diff
changeset
|
7125 |
and (Scala) "List[(_)]" |
6646bb548c6b
migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
haftmann
parents:
52380
diff
changeset
|
7126 |
| constant Nil \<rightharpoonup> |
6646bb548c6b
migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
haftmann
parents:
52380
diff
changeset
|
7127 |
(SML) "[]" |
6646bb548c6b
migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
haftmann
parents:
52380
diff
changeset
|
7128 |
and (OCaml) "[]" |
6646bb548c6b
migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
haftmann
parents:
52380
diff
changeset
|
7129 |
and (Haskell) "[]" |
6646bb548c6b
migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
haftmann
parents:
52380
diff
changeset
|
7130 |
and (Scala) "!Nil" |
6646bb548c6b
migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
haftmann
parents:
52380
diff
changeset
|
7131 |
| class_instance list :: equal \<rightharpoonup> |
6646bb548c6b
migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
haftmann
parents:
52380
diff
changeset
|
7132 |
(Haskell) - |
6646bb548c6b
migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
haftmann
parents:
52380
diff
changeset
|
7133 |
| constant "HOL.equal :: 'a list \<Rightarrow> 'a list \<Rightarrow> bool" \<rightharpoonup> |
6646bb548c6b
migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
haftmann
parents:
52380
diff
changeset
|
7134 |
(Haskell) infix 4 "==" |
6646bb548c6b
migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
haftmann
parents:
52380
diff
changeset
|
7135 |
|
60758 | 7136 |
setup \<open>fold (List_Code.add_literal_list) ["SML", "OCaml", "Haskell", "Scala"]\<close> |
31048
ac146fc38b51
refined HOL string theories and corresponding ML fragments
haftmann
parents:
31022
diff
changeset
|
7137 |
|
ac146fc38b51
refined HOL string theories and corresponding ML fragments
haftmann
parents:
31022
diff
changeset
|
7138 |
code_reserved SML |
ac146fc38b51
refined HOL string theories and corresponding ML fragments
haftmann
parents:
31022
diff
changeset
|
7139 |
list |
ac146fc38b51
refined HOL string theories and corresponding ML fragments
haftmann
parents:
31022
diff
changeset
|
7140 |
|
ac146fc38b51
refined HOL string theories and corresponding ML fragments
haftmann
parents:
31022
diff
changeset
|
7141 |
code_reserved OCaml |
ac146fc38b51
refined HOL string theories and corresponding ML fragments
haftmann
parents:
31022
diff
changeset
|
7142 |
list |
ac146fc38b51
refined HOL string theories and corresponding ML fragments
haftmann
parents:
31022
diff
changeset
|
7143 |
|
21061
580dfc999ef6
added normal post setup; cleaned up "execution" constants
haftmann
parents:
21046
diff
changeset
|
7144 |
|
60758 | 7145 |
subsubsection \<open>Use convenient predefined operations\<close> |
37424
ed431cc99f17
use various predefined Haskell operations when generating code
haftmann
parents:
37408
diff
changeset
|
7146 |
|
52435
6646bb548c6b
migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
haftmann
parents:
52380
diff
changeset
|
7147 |
code_printing |
67399 | 7148 |
constant "(@)" \<rightharpoonup> |
52435
6646bb548c6b
migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
haftmann
parents:
52380
diff
changeset
|
7149 |
(SML) infixr 7 "@" |
6646bb548c6b
migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
haftmann
parents:
52380
diff
changeset
|
7150 |
and (OCaml) infixr 6 "@" |
6646bb548c6b
migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
haftmann
parents:
52380
diff
changeset
|
7151 |
and (Haskell) infixr 5 "++" |
6646bb548c6b
migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
haftmann
parents:
52380
diff
changeset
|
7152 |
and (Scala) infixl 7 "++" |
6646bb548c6b
migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
haftmann
parents:
52380
diff
changeset
|
7153 |
| constant map \<rightharpoonup> |
6646bb548c6b
migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
haftmann
parents:
52380
diff
changeset
|
7154 |
(Haskell) "map" |
6646bb548c6b
migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
haftmann
parents:
52380
diff
changeset
|
7155 |
| constant filter \<rightharpoonup> |
6646bb548c6b
migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
haftmann
parents:
52380
diff
changeset
|
7156 |
(Haskell) "filter" |
6646bb548c6b
migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
haftmann
parents:
52380
diff
changeset
|
7157 |
| constant concat \<rightharpoonup> |
6646bb548c6b
migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
haftmann
parents:
52380
diff
changeset
|
7158 |
(Haskell) "concat" |
6646bb548c6b
migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
haftmann
parents:
52380
diff
changeset
|
7159 |
| constant List.maps \<rightharpoonup> |
6646bb548c6b
migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
haftmann
parents:
52380
diff
changeset
|
7160 |
(Haskell) "concatMap" |
6646bb548c6b
migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
haftmann
parents:
52380
diff
changeset
|
7161 |
| constant rev \<rightharpoonup> |
6646bb548c6b
migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
haftmann
parents:
52380
diff
changeset
|
7162 |
(Haskell) "reverse" |
6646bb548c6b
migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
haftmann
parents:
52380
diff
changeset
|
7163 |
| constant zip \<rightharpoonup> |
6646bb548c6b
migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
haftmann
parents:
52380
diff
changeset
|
7164 |
(Haskell) "zip" |
6646bb548c6b
migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
haftmann
parents:
52380
diff
changeset
|
7165 |
| constant List.null \<rightharpoonup> |
6646bb548c6b
migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
haftmann
parents:
52380
diff
changeset
|
7166 |
(Haskell) "null" |
6646bb548c6b
migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
haftmann
parents:
52380
diff
changeset
|
7167 |
| constant takeWhile \<rightharpoonup> |
6646bb548c6b
migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
haftmann
parents:
52380
diff
changeset
|
7168 |
(Haskell) "takeWhile" |
6646bb548c6b
migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
haftmann
parents:
52380
diff
changeset
|
7169 |
| constant dropWhile \<rightharpoonup> |
6646bb548c6b
migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
haftmann
parents:
52380
diff
changeset
|
7170 |
(Haskell) "dropWhile" |
6646bb548c6b
migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
haftmann
parents:
52380
diff
changeset
|
7171 |
| constant list_all \<rightharpoonup> |
6646bb548c6b
migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
haftmann
parents:
52380
diff
changeset
|
7172 |
(Haskell) "all" |
6646bb548c6b
migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
haftmann
parents:
52380
diff
changeset
|
7173 |
| constant list_ex \<rightharpoonup> |
6646bb548c6b
migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
haftmann
parents:
52380
diff
changeset
|
7174 |
(Haskell) "any" |
37605
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
7175 |
|
46147
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
haftmann
parents:
46143
diff
changeset
|
7176 |
|
60758 | 7177 |
subsubsection \<open>Implementation of sets by lists\<close> |
46147
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
haftmann
parents:
46143
diff
changeset
|
7178 |
|
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
haftmann
parents:
46143
diff
changeset
|
7179 |
lemma is_empty_set [code]: |
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
haftmann
parents:
46143
diff
changeset
|
7180 |
"Set.is_empty (set xs) \<longleftrightarrow> List.null xs" |
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
haftmann
parents:
46143
diff
changeset
|
7181 |
by (simp add: Set.is_empty_def null_def) |
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
haftmann
parents:
46143
diff
changeset
|
7182 |
|
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
haftmann
parents:
46143
diff
changeset
|
7183 |
lemma empty_set [code]: |
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
haftmann
parents:
46143
diff
changeset
|
7184 |
"{} = set []" |
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
haftmann
parents:
46143
diff
changeset
|
7185 |
by simp |
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
haftmann
parents:
46143
diff
changeset
|
7186 |
|
46156 | 7187 |
lemma UNIV_coset [code]: |
7188 |
"UNIV = List.coset []" |
|
7189 |
by simp |
|
7190 |
||
7191 |
lemma compl_set [code]: |
|
7192 |
"- set xs = List.coset xs" |
|
7193 |
by simp |
|
7194 |
||
7195 |
lemma compl_coset [code]: |
|
7196 |
"- List.coset xs = set xs" |
|
7197 |
by simp |
|
7198 |
||
46147
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
haftmann
parents:
46143
diff
changeset
|
7199 |
lemma [code]: |
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
haftmann
parents:
46143
diff
changeset
|
7200 |
"x \<in> set xs \<longleftrightarrow> List.member xs x" |
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
haftmann
parents:
46143
diff
changeset
|
7201 |
"x \<in> List.coset xs \<longleftrightarrow> \<not> List.member xs x" |
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
haftmann
parents:
46143
diff
changeset
|
7202 |
by (simp_all add: member_def) |
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
haftmann
parents:
46143
diff
changeset
|
7203 |
|
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
haftmann
parents:
46143
diff
changeset
|
7204 |
lemma insert_code [code]: |
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
haftmann
parents:
46143
diff
changeset
|
7205 |
"insert x (set xs) = set (List.insert x xs)" |
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
haftmann
parents:
46143
diff
changeset
|
7206 |
"insert x (List.coset xs) = List.coset (removeAll x xs)" |
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
haftmann
parents:
46143
diff
changeset
|
7207 |
by simp_all |
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
haftmann
parents:
46143
diff
changeset
|
7208 |
|
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
haftmann
parents:
46143
diff
changeset
|
7209 |
lemma remove_code [code]: |
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
haftmann
parents:
46143
diff
changeset
|
7210 |
"Set.remove x (set xs) = set (removeAll x xs)" |
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
haftmann
parents:
46143
diff
changeset
|
7211 |
"Set.remove x (List.coset xs) = List.coset (List.insert x xs)" |
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
haftmann
parents:
46143
diff
changeset
|
7212 |
by (simp_all add: remove_def Compl_insert) |
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
haftmann
parents:
46143
diff
changeset
|
7213 |
|
49757
73ab6d4a9236
rename Set.project to Set.filter - more appropriate name
kuncar
parents:
49739
diff
changeset
|
7214 |
lemma filter_set [code]: |
73ab6d4a9236
rename Set.project to Set.filter - more appropriate name
kuncar
parents:
49739
diff
changeset
|
7215 |
"Set.filter P (set xs) = set (filter P xs)" |
46156 | 7216 |
by auto |
7217 |
||
7218 |
lemma image_set [code]: |
|
7219 |
"image f (set xs) = set (map f xs)" |
|
7220 |
by simp |
|
7221 |
||
47398 | 7222 |
lemma subset_code [code]: |
7223 |
"set xs \<le> B \<longleftrightarrow> (\<forall>x\<in>set xs. x \<in> B)" |
|
7224 |
"A \<le> List.coset ys \<longleftrightarrow> (\<forall>y\<in>set ys. y \<notin> A)" |
|
68709 | 7225 |
"List.coset [] \<subseteq> set [] \<longleftrightarrow> False" |
47398 | 7226 |
by auto |
7227 |
||
60758 | 7228 |
text \<open>A frequent case -- avoid intermediate sets\<close> |
47398 | 7229 |
lemma [code_unfold]: |
7230 |
"set xs \<subseteq> set ys \<longleftrightarrow> list_all (\<lambda>x. x \<in> set ys) xs" |
|
7231 |
by (auto simp: list_all_iff) |
|
7232 |
||
46147
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
haftmann
parents:
46143
diff
changeset
|
7233 |
lemma Ball_set [code]: |
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
haftmann
parents:
46143
diff
changeset
|
7234 |
"Ball (set xs) P \<longleftrightarrow> list_all P xs" |
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
haftmann
parents:
46143
diff
changeset
|
7235 |
by (simp add: list_all_iff) |
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
haftmann
parents:
46143
diff
changeset
|
7236 |
|
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
haftmann
parents:
46143
diff
changeset
|
7237 |
lemma Bex_set [code]: |
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
haftmann
parents:
46143
diff
changeset
|
7238 |
"Bex (set xs) P \<longleftrightarrow> list_ex P xs" |
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
haftmann
parents:
46143
diff
changeset
|
7239 |
by (simp add: list_ex_iff) |
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
haftmann
parents:
46143
diff
changeset
|
7240 |
|
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
haftmann
parents:
46143
diff
changeset
|
7241 |
lemma card_set [code]: |
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
haftmann
parents:
46143
diff
changeset
|
7242 |
"card (set xs) = length (remdups xs)" |
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
haftmann
parents:
46143
diff
changeset
|
7243 |
proof - |
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
haftmann
parents:
46143
diff
changeset
|
7244 |
have "card (set (remdups xs)) = length (remdups xs)" |
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
haftmann
parents:
46143
diff
changeset
|
7245 |
by (rule distinct_card) simp |
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
haftmann
parents:
46143
diff
changeset
|
7246 |
then show ?thesis by simp |
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
haftmann
parents:
46143
diff
changeset
|
7247 |
qed |
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
haftmann
parents:
46143
diff
changeset
|
7248 |
|
46156 | 7249 |
lemma the_elem_set [code]: |
7250 |
"the_elem (set [x]) = x" |
|
7251 |
by simp |
|
7252 |
||
7253 |
lemma Pow_set [code]: |
|
7254 |
"Pow (set []) = {{}}" |
|
7255 |
"Pow (set (x # xs)) = (let A = Pow (set xs) in A \<union> insert x ` A)" |
|
7256 |
by (simp_all add: Pow_insert Let_def) |
|
7257 |
||
46424
b447318e5e1a
adding code equation for Relation.image; adding map_project as correspondence to map_filter on lists
bulwahn
parents:
46418
diff
changeset
|
7258 |
definition map_project :: "('a \<Rightarrow> 'b option) \<Rightarrow> 'a set \<Rightarrow> 'b set" where |
b447318e5e1a
adding code equation for Relation.image; adding map_project as correspondence to map_filter on lists
bulwahn
parents:
46418
diff
changeset
|
7259 |
"map_project f A = {b. \<exists> a \<in> A. f a = Some b}" |
b447318e5e1a
adding code equation for Relation.image; adding map_project as correspondence to map_filter on lists
bulwahn
parents:
46418
diff
changeset
|
7260 |
|
b447318e5e1a
adding code equation for Relation.image; adding map_project as correspondence to map_filter on lists
bulwahn
parents:
46418
diff
changeset
|
7261 |
lemma [code]: |
b447318e5e1a
adding code equation for Relation.image; adding map_project as correspondence to map_filter on lists
bulwahn
parents:
46418
diff
changeset
|
7262 |
"map_project f (set xs) = set (List.map_filter f xs)" |
47398 | 7263 |
by (auto simp add: map_project_def map_filter_def image_def) |
46424
b447318e5e1a
adding code equation for Relation.image; adding map_project as correspondence to map_filter on lists
bulwahn
parents:
46418
diff
changeset
|
7264 |
|
b447318e5e1a
adding code equation for Relation.image; adding map_project as correspondence to map_filter on lists
bulwahn
parents:
46418
diff
changeset
|
7265 |
hide_const (open) map_project |
b447318e5e1a
adding code equation for Relation.image; adding map_project as correspondence to map_filter on lists
bulwahn
parents:
46418
diff
changeset
|
7266 |
|
49948
744934b818c7
moved quite generic material from theory Enum to more appropriate places
haftmann
parents:
49808
diff
changeset
|
7267 |
|
60758 | 7268 |
text \<open>Operations on relations\<close> |
46147
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
haftmann
parents:
46143
diff
changeset
|
7269 |
|
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
haftmann
parents:
46143
diff
changeset
|
7270 |
lemma product_code [code]: |
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
haftmann
parents:
46143
diff
changeset
|
7271 |
"Product_Type.product (set xs) (set ys) = set [(x, y). x \<leftarrow> xs, y \<leftarrow> ys]" |
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
haftmann
parents:
46143
diff
changeset
|
7272 |
by (auto simp add: Product_Type.product_def) |
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
haftmann
parents:
46143
diff
changeset
|
7273 |
|
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
haftmann
parents:
46143
diff
changeset
|
7274 |
lemma Id_on_set [code]: |
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
haftmann
parents:
46143
diff
changeset
|
7275 |
"Id_on (set xs) = set [(x, x). x \<leftarrow> xs]" |
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
haftmann
parents:
46143
diff
changeset
|
7276 |
by (auto simp add: Id_on_def) |
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
haftmann
parents:
46143
diff
changeset
|
7277 |
|
46424
b447318e5e1a
adding code equation for Relation.image; adding map_project as correspondence to map_filter on lists
bulwahn
parents:
46418
diff
changeset
|
7278 |
lemma [code]: |
67613 | 7279 |
"R `` S = List.map_project (\<lambda>(x, y). if x \<in> S then Some y else None) R" |
62390 | 7280 |
unfolding map_project_def by (auto split: prod.split if_split_asm) |
46424
b447318e5e1a
adding code equation for Relation.image; adding map_project as correspondence to map_filter on lists
bulwahn
parents:
46418
diff
changeset
|
7281 |
|
46147
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
haftmann
parents:
46143
diff
changeset
|
7282 |
lemma trancl_set_ntrancl [code]: |
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
haftmann
parents:
46143
diff
changeset
|
7283 |
"trancl (set xs) = ntrancl (card (set xs) - 1) (set xs)" |
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
haftmann
parents:
46143
diff
changeset
|
7284 |
by (simp add: finite_trancl_ntranl) |
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
haftmann
parents:
46143
diff
changeset
|
7285 |
|
47433
07f4bf913230
renamed "rel_comp" to "relcomp" (to be consistent with, e.g., "relpow")
griff
parents:
47131
diff
changeset
|
7286 |
lemma set_relcomp [code]: |
46147
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
haftmann
parents:
46143
diff
changeset
|
7287 |
"set xys O set yzs = set ([(fst xy, snd yz). xy \<leftarrow> xys, yz \<leftarrow> yzs, snd xy = fst yz])" |
62343
24106dc44def
prefer abbreviations for compound operators INFIMUM and SUPREMUM
haftmann
parents:
62328
diff
changeset
|
7288 |
by auto (auto simp add: Bex_def image_def) |
46147
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
haftmann
parents:
46143
diff
changeset
|
7289 |
|
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
haftmann
parents:
46143
diff
changeset
|
7290 |
lemma wf_set [code]: |
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
haftmann
parents:
46143
diff
changeset
|
7291 |
"wf (set xs) = acyclic (set xs)" |
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
haftmann
parents:
46143
diff
changeset
|
7292 |
by (simp add: wf_iff_acyclic_if_finite) |
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
haftmann
parents:
46143
diff
changeset
|
7293 |
|
55129
26bd1cba3ab5
killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents:
54890
diff
changeset
|
7294 |
|
60758 | 7295 |
subsection \<open>Setup for Lifting/Transfer\<close> |
7296 |
||
7297 |
subsubsection \<open>Transfer rules for the Transfer package\<close> |
|
53012
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
kuncar
parents:
52435
diff
changeset
|
7298 |
|
63343 | 7299 |
context includes lifting_syntax |
53012
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
kuncar
parents:
52435
diff
changeset
|
7300 |
begin |
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
kuncar
parents:
52435
diff
changeset
|
7301 |
|
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
kuncar
parents:
52435
diff
changeset
|
7302 |
lemma tl_transfer [transfer_rule]: |
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
kuncar
parents:
52435
diff
changeset
|
7303 |
"(list_all2 A ===> list_all2 A) tl tl" |
55405
0a155051bd9d
use new selector support to define 'the', 'hd', 'tl'
blanchet
parents:
55404
diff
changeset
|
7304 |
unfolding tl_def[abs_def] by transfer_prover |
53012
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
kuncar
parents:
52435
diff
changeset
|
7305 |
|
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
kuncar
parents:
52435
diff
changeset
|
7306 |
lemma butlast_transfer [transfer_rule]: |
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
kuncar
parents:
52435
diff
changeset
|
7307 |
"(list_all2 A ===> list_all2 A) butlast butlast" |
55945 | 7308 |
by (rule rel_funI, erule list_all2_induct, auto) |
53012
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
kuncar
parents:
52435
diff
changeset
|
7309 |
|
55465 | 7310 |
lemma map_rec: "map f xs = rec_list Nil (%x _ y. Cons (f x) y) xs" |
7311 |
by (induct xs) auto |
|
7312 |
||
53012
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
kuncar
parents:
52435
diff
changeset
|
7313 |
lemma append_transfer [transfer_rule]: |
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
kuncar
parents:
52435
diff
changeset
|
7314 |
"(list_all2 A ===> list_all2 A ===> list_all2 A) append append" |
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
kuncar
parents:
52435
diff
changeset
|
7315 |
unfolding List.append_def by transfer_prover |
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
kuncar
parents:
52435
diff
changeset
|
7316 |
|
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
kuncar
parents:
52435
diff
changeset
|
7317 |
lemma rev_transfer [transfer_rule]: |
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
kuncar
parents:
52435
diff
changeset
|
7318 |
"(list_all2 A ===> list_all2 A) rev rev" |
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
kuncar
parents:
52435
diff
changeset
|
7319 |
unfolding List.rev_def by transfer_prover |
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
kuncar
parents:
52435
diff
changeset
|
7320 |
|
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
kuncar
parents:
52435
diff
changeset
|
7321 |
lemma filter_transfer [transfer_rule]: |
67399 | 7322 |
"((A ===> (=)) ===> list_all2 A ===> list_all2 A) filter filter" |
53012
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
kuncar
parents:
52435
diff
changeset
|
7323 |
unfolding List.filter_def by transfer_prover |
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
kuncar
parents:
52435
diff
changeset
|
7324 |
|
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
kuncar
parents:
52435
diff
changeset
|
7325 |
lemma fold_transfer [transfer_rule]: |
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
kuncar
parents:
52435
diff
changeset
|
7326 |
"((A ===> B ===> B) ===> list_all2 A ===> B ===> B) fold fold" |
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
kuncar
parents:
52435
diff
changeset
|
7327 |
unfolding List.fold_def by transfer_prover |
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
kuncar
parents:
52435
diff
changeset
|
7328 |
|
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
kuncar
parents:
52435
diff
changeset
|
7329 |
lemma foldr_transfer [transfer_rule]: |
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
kuncar
parents:
52435
diff
changeset
|
7330 |
"((A ===> B ===> B) ===> list_all2 A ===> B ===> B) foldr foldr" |
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
kuncar
parents:
52435
diff
changeset
|
7331 |
unfolding List.foldr_def by transfer_prover |
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
kuncar
parents:
52435
diff
changeset
|
7332 |
|
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
kuncar
parents:
52435
diff
changeset
|
7333 |
lemma foldl_transfer [transfer_rule]: |
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
kuncar
parents:
52435
diff
changeset
|
7334 |
"((B ===> A ===> B) ===> B ===> list_all2 A ===> B) foldl foldl" |
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
kuncar
parents:
52435
diff
changeset
|
7335 |
unfolding List.foldl_def by transfer_prover |
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
kuncar
parents:
52435
diff
changeset
|
7336 |
|
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
kuncar
parents:
52435
diff
changeset
|
7337 |
lemma concat_transfer [transfer_rule]: |
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
kuncar
parents:
52435
diff
changeset
|
7338 |
"(list_all2 (list_all2 A) ===> list_all2 A) concat concat" |
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
kuncar
parents:
52435
diff
changeset
|
7339 |
unfolding List.concat_def by transfer_prover |
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
kuncar
parents:
52435
diff
changeset
|
7340 |
|
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
kuncar
parents:
52435
diff
changeset
|
7341 |
lemma drop_transfer [transfer_rule]: |
67399 | 7342 |
"((=) ===> list_all2 A ===> list_all2 A) drop drop" |
53012
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
kuncar
parents:
52435
diff
changeset
|
7343 |
unfolding List.drop_def by transfer_prover |
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
kuncar
parents:
52435
diff
changeset
|
7344 |
|
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
kuncar
parents:
52435
diff
changeset
|
7345 |
lemma take_transfer [transfer_rule]: |
67399 | 7346 |
"((=) ===> list_all2 A ===> list_all2 A) take take" |
53012
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
kuncar
parents:
52435
diff
changeset
|
7347 |
unfolding List.take_def by transfer_prover |
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
kuncar
parents:
52435
diff
changeset
|
7348 |
|
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
kuncar
parents:
52435
diff
changeset
|
7349 |
lemma list_update_transfer [transfer_rule]: |
67399 | 7350 |
"(list_all2 A ===> (=) ===> A ===> list_all2 A) list_update list_update" |
53012
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
kuncar
parents:
52435
diff
changeset
|
7351 |
unfolding list_update_def by transfer_prover |
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
kuncar
parents:
52435
diff
changeset
|
7352 |
|
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
kuncar
parents:
52435
diff
changeset
|
7353 |
lemma takeWhile_transfer [transfer_rule]: |
67399 | 7354 |
"((A ===> (=)) ===> list_all2 A ===> list_all2 A) takeWhile takeWhile" |
53012
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
kuncar
parents:
52435
diff
changeset
|
7355 |
unfolding takeWhile_def by transfer_prover |
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
kuncar
parents:
52435
diff
changeset
|
7356 |
|
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
kuncar
parents:
52435
diff
changeset
|
7357 |
lemma dropWhile_transfer [transfer_rule]: |
67399 | 7358 |
"((A ===> (=)) ===> list_all2 A ===> list_all2 A) dropWhile dropWhile" |
53012
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
kuncar
parents:
52435
diff
changeset
|
7359 |
unfolding dropWhile_def by transfer_prover |
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
kuncar
parents:
52435
diff
changeset
|
7360 |
|
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
kuncar
parents:
52435
diff
changeset
|
7361 |
lemma zip_transfer [transfer_rule]: |
55944 | 7362 |
"(list_all2 A ===> list_all2 B ===> list_all2 (rel_prod A B)) zip zip" |
53012
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
kuncar
parents:
52435
diff
changeset
|
7363 |
unfolding zip_def by transfer_prover |
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
kuncar
parents:
52435
diff
changeset
|
7364 |
|
53721
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
traytel
parents:
53689
diff
changeset
|
7365 |
lemma product_transfer [transfer_rule]: |
55944 | 7366 |
"(list_all2 A ===> list_all2 B ===> list_all2 (rel_prod A B)) List.product List.product" |
53721
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
traytel
parents:
53689
diff
changeset
|
7367 |
unfolding List.product_def by transfer_prover |
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
traytel
parents:
53689
diff
changeset
|
7368 |
|
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
traytel
parents:
53689
diff
changeset
|
7369 |
lemma product_lists_transfer [transfer_rule]: |
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
traytel
parents:
53689
diff
changeset
|
7370 |
"(list_all2 (list_all2 A) ===> list_all2 (list_all2 A)) product_lists product_lists" |
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
traytel
parents:
53689
diff
changeset
|
7371 |
unfolding product_lists_def by transfer_prover |
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
traytel
parents:
53689
diff
changeset
|
7372 |
|
53012
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
kuncar
parents:
52435
diff
changeset
|
7373 |
lemma insert_transfer [transfer_rule]: |
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
kuncar
parents:
52435
diff
changeset
|
7374 |
assumes [transfer_rule]: "bi_unique A" |
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
kuncar
parents:
52435
diff
changeset
|
7375 |
shows "(A ===> list_all2 A ===> list_all2 A) List.insert List.insert" |
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
kuncar
parents:
52435
diff
changeset
|
7376 |
unfolding List.insert_def [abs_def] by transfer_prover |
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
kuncar
parents:
52435
diff
changeset
|
7377 |
|
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
kuncar
parents:
52435
diff
changeset
|
7378 |
lemma find_transfer [transfer_rule]: |
67399 | 7379 |
"((A ===> (=)) ===> list_all2 A ===> rel_option A) List.find List.find" |
53012
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
kuncar
parents:
52435
diff
changeset
|
7380 |
unfolding List.find_def by transfer_prover |
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
kuncar
parents:
52435
diff
changeset
|
7381 |
|
63521
32da860241b8
added missing transfer rule
Lars Hupel <lars.hupel@mytum.de>
parents:
63365
diff
changeset
|
7382 |
lemma those_transfer [transfer_rule]: |
32da860241b8
added missing transfer rule
Lars Hupel <lars.hupel@mytum.de>
parents:
63365
diff
changeset
|
7383 |
"(list_all2 (rel_option P) ===> rel_option (list_all2 P)) those those" |
32da860241b8
added missing transfer rule
Lars Hupel <lars.hupel@mytum.de>
parents:
63365
diff
changeset
|
7384 |
unfolding List.those_def by transfer_prover |
32da860241b8
added missing transfer rule
Lars Hupel <lars.hupel@mytum.de>
parents:
63365
diff
changeset
|
7385 |
|
53012
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
kuncar
parents:
52435
diff
changeset
|
7386 |
lemma remove1_transfer [transfer_rule]: |
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
kuncar
parents:
52435
diff
changeset
|
7387 |
assumes [transfer_rule]: "bi_unique A" |
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
kuncar
parents:
52435
diff
changeset
|
7388 |
shows "(A ===> list_all2 A ===> list_all2 A) remove1 remove1" |
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
kuncar
parents:
52435
diff
changeset
|
7389 |
unfolding remove1_def by transfer_prover |
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
kuncar
parents:
52435
diff
changeset
|
7390 |
|
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
kuncar
parents:
52435
diff
changeset
|
7391 |
lemma removeAll_transfer [transfer_rule]: |
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
kuncar
parents:
52435
diff
changeset
|
7392 |
assumes [transfer_rule]: "bi_unique A" |
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
kuncar
parents:
52435
diff
changeset
|
7393 |
shows "(A ===> list_all2 A ===> list_all2 A) removeAll removeAll" |
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
kuncar
parents:
52435
diff
changeset
|
7394 |
unfolding removeAll_def by transfer_prover |
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
kuncar
parents:
52435
diff
changeset
|
7395 |
|
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
kuncar
parents:
52435
diff
changeset
|
7396 |
lemma distinct_transfer [transfer_rule]: |
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
kuncar
parents:
52435
diff
changeset
|
7397 |
assumes [transfer_rule]: "bi_unique A" |
67399 | 7398 |
shows "(list_all2 A ===> (=)) distinct distinct" |
53012
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
kuncar
parents:
52435
diff
changeset
|
7399 |
unfolding distinct_def by transfer_prover |
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
kuncar
parents:
52435
diff
changeset
|
7400 |
|
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
kuncar
parents:
52435
diff
changeset
|
7401 |
lemma remdups_transfer [transfer_rule]: |
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
kuncar
parents:
52435
diff
changeset
|
7402 |
assumes [transfer_rule]: "bi_unique A" |
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
kuncar
parents:
52435
diff
changeset
|
7403 |
shows "(list_all2 A ===> list_all2 A) remdups remdups" |
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
kuncar
parents:
52435
diff
changeset
|
7404 |
unfolding remdups_def by transfer_prover |
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
kuncar
parents:
52435
diff
changeset
|
7405 |
|
53721
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
traytel
parents:
53689
diff
changeset
|
7406 |
lemma remdups_adj_transfer [transfer_rule]: |
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
traytel
parents:
53689
diff
changeset
|
7407 |
assumes [transfer_rule]: "bi_unique A" |
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
traytel
parents:
53689
diff
changeset
|
7408 |
shows "(list_all2 A ===> list_all2 A) remdups_adj remdups_adj" |
55945 | 7409 |
proof (rule rel_funI, erule list_all2_induct) |
53721
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
traytel
parents:
53689
diff
changeset
|
7410 |
qed (auto simp: remdups_adj_Cons assms[unfolded bi_unique_def] split: list.splits) |
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
traytel
parents:
53689
diff
changeset
|
7411 |
|
53012
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
kuncar
parents:
52435
diff
changeset
|
7412 |
lemma replicate_transfer [transfer_rule]: |
67399 | 7413 |
"((=) ===> A ===> list_all2 A) replicate replicate" |
53012
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
kuncar
parents:
52435
diff
changeset
|
7414 |
unfolding replicate_def by transfer_prover |
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
kuncar
parents:
52435
diff
changeset
|
7415 |
|
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
kuncar
parents:
52435
diff
changeset
|
7416 |
lemma length_transfer [transfer_rule]: |
67399 | 7417 |
"(list_all2 A ===> (=)) length length" |
56643
41d3596d8a64
move size hooks together, with new one preceding old one and sharing same theory data
blanchet
parents:
56545
diff
changeset
|
7418 |
unfolding size_list_overloaded_def size_list_def by transfer_prover |
53012
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
kuncar
parents:
52435
diff
changeset
|
7419 |
|
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
kuncar
parents:
52435
diff
changeset
|
7420 |
lemma rotate1_transfer [transfer_rule]: |
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
kuncar
parents:
52435
diff
changeset
|
7421 |
"(list_all2 A ===> list_all2 A) rotate1 rotate1" |
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
kuncar
parents:
52435
diff
changeset
|
7422 |
unfolding rotate1_def by transfer_prover |
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
kuncar
parents:
52435
diff
changeset
|
7423 |
|
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
kuncar
parents:
52435
diff
changeset
|
7424 |
lemma rotate_transfer [transfer_rule]: |
67399 | 7425 |
"((=) ===> list_all2 A ===> list_all2 A) rotate rotate" |
53012
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
kuncar
parents:
52435
diff
changeset
|
7426 |
unfolding rotate_def [abs_def] by transfer_prover |
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
kuncar
parents:
52435
diff
changeset
|
7427 |
|
65956
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65350
diff
changeset
|
7428 |
lemma nths_transfer [transfer_rule]: |
67399 | 7429 |
"(list_all2 A ===> rel_set (=) ===> list_all2 A) nths nths" |
65956
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65350
diff
changeset
|
7430 |
unfolding nths_def [abs_def] by transfer_prover |
66892 | 7431 |
|
65956
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65350
diff
changeset
|
7432 |
lemma subseqs_transfer [transfer_rule]: |
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65350
diff
changeset
|
7433 |
"(list_all2 A ===> list_all2 (list_all2 A)) subseqs subseqs" |
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65350
diff
changeset
|
7434 |
unfolding subseqs_def [abs_def] by transfer_prover |
53012
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
kuncar
parents:
52435
diff
changeset
|
7435 |
|
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
kuncar
parents:
52435
diff
changeset
|
7436 |
lemma partition_transfer [transfer_rule]: |
67399 | 7437 |
"((A ===> (=)) ===> list_all2 A ===> rel_prod (list_all2 A) (list_all2 A)) |
53012
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
kuncar
parents:
52435
diff
changeset
|
7438 |
partition partition" |
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
kuncar
parents:
52435
diff
changeset
|
7439 |
unfolding partition_def by transfer_prover |
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
kuncar
parents:
52435
diff
changeset
|
7440 |
|
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
kuncar
parents:
52435
diff
changeset
|
7441 |
lemma lists_transfer [transfer_rule]: |
55938 | 7442 |
"(rel_set A ===> rel_set (list_all2 A)) lists lists" |
55945 | 7443 |
apply (rule rel_funI, rule rel_setI) |
53012
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
kuncar
parents:
52435
diff
changeset
|
7444 |
apply (erule lists.induct, simp) |
55938 | 7445 |
apply (simp only: rel_set_def list_all2_Cons1, metis lists.Cons) |
53012
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
kuncar
parents:
52435
diff
changeset
|
7446 |
apply (erule lists.induct, simp) |
55938 | 7447 |
apply (simp only: rel_set_def list_all2_Cons2, metis lists.Cons) |
53012
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
kuncar
parents:
52435
diff
changeset
|
7448 |
done |
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
kuncar
parents:
52435
diff
changeset
|
7449 |
|
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
kuncar
parents:
52435
diff
changeset
|
7450 |
lemma set_Cons_transfer [transfer_rule]: |
55938 | 7451 |
"(rel_set A ===> rel_set (list_all2 A) ===> rel_set (list_all2 A)) |
53012
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
kuncar
parents:
52435
diff
changeset
|
7452 |
set_Cons set_Cons" |
55945 | 7453 |
unfolding rel_fun_def rel_set_def set_Cons_def |
68719 | 7454 |
by (fastforce simp add: list_all2_Cons1 list_all2_Cons2) |
53012
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
kuncar
parents:
52435
diff
changeset
|
7455 |
|
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
kuncar
parents:
52435
diff
changeset
|
7456 |
lemma listset_transfer [transfer_rule]: |
55938 | 7457 |
"(list_all2 (rel_set A) ===> rel_set (list_all2 A)) listset listset" |
53012
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
kuncar
parents:
52435
diff
changeset
|
7458 |
unfolding listset_def by transfer_prover |
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
kuncar
parents:
52435
diff
changeset
|
7459 |
|
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
kuncar
parents:
52435
diff
changeset
|
7460 |
lemma null_transfer [transfer_rule]: |
67399 | 7461 |
"(list_all2 A ===> (=)) List.null List.null" |
55945 | 7462 |
unfolding rel_fun_def List.null_def by auto |
53012
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
kuncar
parents:
52435
diff
changeset
|
7463 |
|
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
kuncar
parents:
52435
diff
changeset
|
7464 |
lemma list_all_transfer [transfer_rule]: |
67399 | 7465 |
"((A ===> (=)) ===> list_all2 A ===> (=)) list_all list_all" |
53012
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
kuncar
parents:
52435
diff
changeset
|
7466 |
unfolding list_all_iff [abs_def] by transfer_prover |
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
kuncar
parents:
52435
diff
changeset
|
7467 |
|
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
kuncar
parents:
52435
diff
changeset
|
7468 |
lemma list_ex_transfer [transfer_rule]: |
67399 | 7469 |
"((A ===> (=)) ===> list_all2 A ===> (=)) list_ex list_ex" |
53012
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
kuncar
parents:
52435
diff
changeset
|
7470 |
unfolding list_ex_iff [abs_def] by transfer_prover |
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
kuncar
parents:
52435
diff
changeset
|
7471 |
|
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
kuncar
parents:
52435
diff
changeset
|
7472 |
lemma splice_transfer [transfer_rule]: |
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
kuncar
parents:
52435
diff
changeset
|
7473 |
"(list_all2 A ===> list_all2 A ===> list_all2 A) splice splice" |
55945 | 7474 |
apply (rule rel_funI, erule list_all2_induct, simp add: rel_fun_def, simp) |
7475 |
apply (rule rel_funI) |
|
7476 |
apply (erule_tac xs=x in list_all2_induct, simp, simp add: rel_fun_def) |
|
53012
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
kuncar
parents:
52435
diff
changeset
|
7477 |
done |
66892 | 7478 |
|
69107 | 7479 |
lemma shuffles_transfer [transfer_rule]: |
7480 |
"(list_all2 A ===> list_all2 A ===> rel_set (list_all2 A)) shuffles shuffles" |
|
65350
b149abe619f7
added shuffle product to HOL/List
eberlm <eberlm@in.tum.de>
parents:
64966
diff
changeset
|
7481 |
proof (intro rel_funI, goal_cases) |
b149abe619f7
added shuffle product to HOL/List
eberlm <eberlm@in.tum.de>
parents:
64966
diff
changeset
|
7482 |
case (1 xs xs' ys ys') |
b149abe619f7
added shuffle product to HOL/List
eberlm <eberlm@in.tum.de>
parents:
64966
diff
changeset
|
7483 |
thus ?case |
69107 | 7484 |
proof (induction xs ys arbitrary: xs' ys' rule: shuffles.induct) |
65350
b149abe619f7
added shuffle product to HOL/List
eberlm <eberlm@in.tum.de>
parents:
64966
diff
changeset
|
7485 |
case (3 x xs y ys xs' ys') |
b149abe619f7
added shuffle product to HOL/List
eberlm <eberlm@in.tum.de>
parents:
64966
diff
changeset
|
7486 |
from "3.prems" obtain x' xs'' where xs': "xs' = x' # xs''" by (cases xs') auto |
b149abe619f7
added shuffle product to HOL/List
eberlm <eberlm@in.tum.de>
parents:
64966
diff
changeset
|
7487 |
from "3.prems" obtain y' ys'' where ys': "ys' = y' # ys''" by (cases ys') auto |
b149abe619f7
added shuffle product to HOL/List
eberlm <eberlm@in.tum.de>
parents:
64966
diff
changeset
|
7488 |
have [transfer_rule]: "A x x'" "A y y'" "list_all2 A xs xs''" "list_all2 A ys ys''" |
b149abe619f7
added shuffle product to HOL/List
eberlm <eberlm@in.tum.de>
parents:
64966
diff
changeset
|
7489 |
using "3.prems" by (simp_all add: xs' ys') |
69107 | 7490 |
have [transfer_rule]: "rel_set (list_all2 A) (shuffles xs (y # ys)) (shuffles xs'' ys')" and |
7491 |
[transfer_rule]: "rel_set (list_all2 A) (shuffles (x # xs) ys) (shuffles xs' ys'')" |
|
65350
b149abe619f7
added shuffle product to HOL/List
eberlm <eberlm@in.tum.de>
parents:
64966
diff
changeset
|
7492 |
using "3.prems" by (auto intro!: "3.IH" simp: xs' ys') |
69107 | 7493 |
have "rel_set (list_all2 A) ((#) x ` shuffles xs (y # ys) \<union> (#) y ` shuffles (x # xs) ys) |
7494 |
((#) x' ` shuffles xs'' ys' \<union> (#) y' ` shuffles xs' ys'')" by transfer_prover |
|
65350
b149abe619f7
added shuffle product to HOL/List
eberlm <eberlm@in.tum.de>
parents:
64966
diff
changeset
|
7495 |
thus ?case by (simp add: xs' ys') |
b149abe619f7
added shuffle product to HOL/List
eberlm <eberlm@in.tum.de>
parents:
64966
diff
changeset
|
7496 |
qed (auto simp: rel_set_def) |
66892 | 7497 |
qed |
53012
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
kuncar
parents:
52435
diff
changeset
|
7498 |
|
57599 | 7499 |
lemma rtrancl_parametric [transfer_rule]: |
7500 |
assumes [transfer_rule]: "bi_unique A" "bi_total A" |
|
7501 |
shows "(rel_set (rel_prod A A) ===> rel_set (rel_prod A A)) rtrancl rtrancl" |
|
7502 |
unfolding rtrancl_def by transfer_prover |
|
7503 |
||
59516
d92b74f3f6e3
add parametricity rules for monotone, fun_lub, and fun_ord
Andreas Lochbihler
parents:
59199
diff
changeset
|
7504 |
lemma monotone_parametric [transfer_rule]: |
d92b74f3f6e3
add parametricity rules for monotone, fun_lub, and fun_ord
Andreas Lochbihler
parents:
59199
diff
changeset
|
7505 |
assumes [transfer_rule]: "bi_total A" |
67399 | 7506 |
shows "((A ===> A ===> (=)) ===> (B ===> B ===> (=)) ===> (A ===> B) ===> (=)) monotone monotone" |
59516
d92b74f3f6e3
add parametricity rules for monotone, fun_lub, and fun_ord
Andreas Lochbihler
parents:
59199
diff
changeset
|
7507 |
unfolding monotone_def[abs_def] by transfer_prover |
d92b74f3f6e3
add parametricity rules for monotone, fun_lub, and fun_ord
Andreas Lochbihler
parents:
59199
diff
changeset
|
7508 |
|
d92b74f3f6e3
add parametricity rules for monotone, fun_lub, and fun_ord
Andreas Lochbihler
parents:
59199
diff
changeset
|
7509 |
lemma fun_ord_parametric [transfer_rule]: |
d92b74f3f6e3
add parametricity rules for monotone, fun_lub, and fun_ord
Andreas Lochbihler
parents:
59199
diff
changeset
|
7510 |
assumes [transfer_rule]: "bi_total C" |
67399 | 7511 |
shows "((A ===> B ===> (=)) ===> (C ===> A) ===> (C ===> B) ===> (=)) fun_ord fun_ord" |
59516
d92b74f3f6e3
add parametricity rules for monotone, fun_lub, and fun_ord
Andreas Lochbihler
parents:
59199
diff
changeset
|
7512 |
unfolding fun_ord_def[abs_def] by transfer_prover |
d92b74f3f6e3
add parametricity rules for monotone, fun_lub, and fun_ord
Andreas Lochbihler
parents:
59199
diff
changeset
|
7513 |
|
d92b74f3f6e3
add parametricity rules for monotone, fun_lub, and fun_ord
Andreas Lochbihler
parents:
59199
diff
changeset
|
7514 |
lemma fun_lub_parametric [transfer_rule]: |
d92b74f3f6e3
add parametricity rules for monotone, fun_lub, and fun_ord
Andreas Lochbihler
parents:
59199
diff
changeset
|
7515 |
assumes [transfer_rule]: "bi_total A" "bi_unique A" |
d92b74f3f6e3
add parametricity rules for monotone, fun_lub, and fun_ord
Andreas Lochbihler
parents:
59199
diff
changeset
|
7516 |
shows "((rel_set A ===> B) ===> rel_set (C ===> A) ===> C ===> B) fun_lub fun_lub" |
d92b74f3f6e3
add parametricity rules for monotone, fun_lub, and fun_ord
Andreas Lochbihler
parents:
59199
diff
changeset
|
7517 |
unfolding fun_lub_def[abs_def] by transfer_prover |
d92b74f3f6e3
add parametricity rules for monotone, fun_lub, and fun_ord
Andreas Lochbihler
parents:
59199
diff
changeset
|
7518 |
|
23388 | 7519 |
end |
47397
d654c73e4b12
no preference wrt. fold(l/r); prefer fold rather than foldr for iterating over lists in generated code
haftmann
parents:
47131
diff
changeset
|
7520 |
|
53012
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
kuncar
parents:
52435
diff
changeset
|
7521 |
end |