author | Fabian Huch <huch@in.tum.de> |
Thu, 30 Nov 2023 13:44:51 +0100 | |
changeset 79084 | dd689c4ab688 |
parent 78833 | 98e164c3059f |
child 79445 | 8e3e9e6ca538 |
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 |
74101 | 8 |
imports Sledgehammer 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 |
||
71779 | 218 |
fun successively :: "('a \<Rightarrow> 'a \<Rightarrow> bool) \<Rightarrow> 'a list \<Rightarrow> bool" where |
219 |
"successively P [] = True" | |
|
220 |
"successively P [x] = True" | |
|
221 |
"successively P (x # y # xs) = (P x y \<and> successively P (y#xs))" |
|
222 |
||
223 |
definition distinct_adj where |
|
224 |
"distinct_adj = successively (\<noteq>)" |
|
225 |
||
50548 | 226 |
primrec remdups :: "'a list \<Rightarrow> 'a list" where |
227 |
"remdups [] = []" | |
|
228 |
"remdups (x # xs) = (if x \<in> set xs then remdups xs else x # remdups xs)" |
|
229 |
||
53721
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
traytel
parents:
53689
diff
changeset
|
230 |
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
|
231 |
"remdups_adj [] = []" | |
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
traytel
parents:
53689
diff
changeset
|
232 |
"remdups_adj [x] = [x]" | |
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
traytel
parents:
53689
diff
changeset
|
233 |
"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
|
234 |
|
50548 | 235 |
primrec replicate :: "nat \<Rightarrow> 'a \<Rightarrow> 'a list" where |
236 |
replicate_0: "replicate 0 x = []" | |
|
237 |
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
|
238 |
|
60758 | 239 |
text \<open> |
61799 | 240 |
Function \<open>size\<close> is overloaded for all datatypes. Users may |
241 |
refer to the list version as \<open>length\<close>.\<close> |
|
13142 | 242 |
|
50548 | 243 |
abbreviation length :: "'a list \<Rightarrow> nat" where |
244 |
"length \<equiv> size" |
|
15307 | 245 |
|
51173 | 246 |
definition enumerate :: "nat \<Rightarrow> 'a list \<Rightarrow> (nat \<times> 'a) list" where |
247 |
enumerate_eq_zip: "enumerate n xs = zip [n..<n + length xs] xs" |
|
248 |
||
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
|
249 |
primrec rotate1 :: "'a list \<Rightarrow> 'a list" where |
50548 | 250 |
"rotate1 [] = []" | |
251 |
"rotate1 (x # xs) = xs @ [x]" |
|
252 |
||
253 |
definition rotate :: "nat \<Rightarrow> 'a list \<Rightarrow> 'a list" where |
|
254 |
"rotate n = rotate1 ^^ n" |
|
255 |
||
65956
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65350
diff
changeset
|
256 |
definition nths :: "'a list => nat set => 'a list" where |
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65350
diff
changeset
|
257 |
"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
|
258 |
|
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65350
diff
changeset
|
259 |
primrec subseqs :: "'a list \<Rightarrow> 'a list list" where |
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65350
diff
changeset
|
260 |
"subseqs [] = [[]]" | |
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65350
diff
changeset
|
261 |
"subseqs (x#xs) = (let xss = subseqs xs in map (Cons x) xss @ xss)" |
50548 | 262 |
|
263 |
primrec n_lists :: "nat \<Rightarrow> 'a list \<Rightarrow> 'a list list" where |
|
264 |
"n_lists 0 xs = [[]]" | |
|
265 |
"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
|
266 |
|
744934b818c7
moved quite generic material from theory Enum to more appropriate places
haftmann
parents:
49808
diff
changeset
|
267 |
hide_const (open) n_lists |
744934b818c7
moved quite generic material from theory Enum to more appropriate places
haftmann
parents:
49808
diff
changeset
|
268 |
|
69075 | 269 |
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
|
270 |
"splice [] ys = ys" | |
69075 | 271 |
"splice (x#xs) ys = x # splice ys xs" |
272 |
by pat_completeness auto |
|
273 |
||
274 |
termination |
|
275 |
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
|
276 |
|
69107 | 277 |
function shuffles where |
278 |
"shuffles [] ys = {ys}" |
|
279 |
| "shuffles xs [] = {xs}" |
|
280 |
| "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
|
281 |
by pat_completeness simp_all |
b149abe619f7
added shuffle product to HOL/List
eberlm <eberlm@in.tum.de>
parents:
64966
diff
changeset
|
282 |
termination by lexicographic_order |
b149abe619f7
added shuffle product to HOL/List
eberlm <eberlm@in.tum.de>
parents:
64966
diff
changeset
|
283 |
|
69593 | 284 |
text\<open>Use only if you cannot use \<^const>\<open>Min\<close> instead:\<close> |
67170 | 285 |
fun min_list :: "'a::ord list \<Rightarrow> 'a" where |
286 |
"min_list (x # xs) = (case xs of [] \<Rightarrow> x | _ \<Rightarrow> min x (min_list xs))" |
|
287 |
||
288 |
text\<open>Returns first minimum:\<close> |
|
289 |
fun arg_min_list :: "('a \<Rightarrow> ('b::linorder)) \<Rightarrow> 'a list \<Rightarrow> 'a" where |
|
290 |
"arg_min_list f [x] = x" | |
|
291 |
"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)" |
|
292 |
||
60758 | 293 |
text\<open> |
26771 | 294 |
\begin{figure}[htbp] |
295 |
\fbox{ |
|
296 |
\begin{tabular}{l} |
|
27381 | 297 |
@{lemma "[a,b]@[c,d] = [a,b,c,d]" by simp}\\ |
298 |
@{lemma "length [a,b,c] = 3" by simp}\\ |
|
299 |
@{lemma "set [a,b,c] = {a,b,c}" by simp}\\ |
|
300 |
@{lemma "map f [a,b,c] = [f a, f b, f c]" by simp}\\ |
|
301 |
@{lemma "rev [a,b,c] = [c,b,a]" by simp}\\ |
|
302 |
@{lemma "hd [a,b,c,d] = a" by simp}\\ |
|
303 |
@{lemma "tl [a,b,c,d] = [b,c,d]" by simp}\\ |
|
304 |
@{lemma "last [a,b,c,d] = d" by simp}\\ |
|
305 |
@{lemma "butlast [a,b,c,d] = [a,b,c]" by simp}\\ |
|
306 |
@{lemma[source] "filter (\<lambda>n::nat. n<2) [0,2,1] = [0,1]" by simp}\\ |
|
307 |
@{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
|
308 |
@{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
|
309 |
@{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
|
310 |
@{lemma "foldl f x [a,b,c] = f (f (f x a) b) c" by simp}\\ |
71779 | 311 |
@{lemma "successively (\<noteq>) [True,False,True,False]" by simp}\\ |
27381 | 312 |
@{lemma "zip [a,b,c] [x,y,z] = [(a,x),(b,y),(c,z)]" by simp}\\ |
313 |
@{lemma "zip [a,b] [x,y,z] = [(a,x),(b,y)]" by simp}\\ |
|
51173 | 314 |
@{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
|
315 |
@{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
|
316 |
@{lemma "product_lists [[a,b], [c], [d,e]] = [[a,c,d], [a,c,e], [b,c,d], [b,c,e]]" by simp}\\ |
27381 | 317 |
@{lemma "splice [a,b,c] [x,y,z] = [a,x,b,y,c,z]" by simp}\\ |
318 |
@{lemma "splice [a,b,c,d] [x,y] = [a,x,b,y,c,d]" by simp}\\ |
|
69107 | 319 |
@{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
|
320 |
by (simp add: insert_commute)}\\ |
27381 | 321 |
@{lemma "take 2 [a,b,c,d] = [a,b]" by simp}\\ |
322 |
@{lemma "take 6 [a,b,c,d] = [a,b,c,d]" by simp}\\ |
|
323 |
@{lemma "drop 2 [a,b,c,d] = [c,d]" by simp}\\ |
|
324 |
@{lemma "drop 6 [a,b,c,d] = []" by simp}\\ |
|
325 |
@{lemma "takeWhile (%n::nat. n<3) [1,2,3,0] = [1,2]" by simp}\\ |
|
326 |
@{lemma "dropWhile (%n::nat. n<3) [1,2,3,0] = [3,0]" by simp}\\ |
|
327 |
@{lemma "distinct [2,0,1::nat]" by simp}\\ |
|
328 |
@{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
|
329 |
@{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
|
330 |
@{lemma "List.insert 2 [0::nat,1,2] = [0,1,2]" by (simp add: List.insert_def)}\\ |
35295 | 331 |
@{lemma "List.insert 3 [0::nat,1,2] = [3,0,1,2]" by (simp add: List.insert_def)}\\ |
57198 | 332 |
@{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 | 333 |
@{lemma "List.find (%i::int. i>0) [0,0] = None" by simp}\\ |
334 |
@{lemma "List.find (%i::int. i>0) [0,1,0,2] = Some 1" by simp}\\ |
|
60541 | 335 |
@{lemma "count_list [0,1,0,2::int] 0 = 2" by (simp)}\\ |
55807 | 336 |
@{lemma "List.extract (%i::int. i>0) [0,0] = None" by(simp add: extract_def)}\\ |
337 |
@{lemma "List.extract (%i::int. i>0) [0,1,0,2] = Some([0], 1, [0,2])" by(simp add: extract_def)}\\ |
|
27381 | 338 |
@{lemma "remove1 2 [2,0,2,1::nat,2] = [0,2,1,2]" by simp}\\ |
27693 | 339 |
@{lemma "removeAll 2 [2,0,2,1::nat,2] = [0,1]" by simp}\\ |
27381 | 340 |
@{lemma "nth [a,b,c,d] 2 = c" by simp}\\ |
341 |
@{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
|
342 |
@{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
|
343 |
@{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
|
344 |
@{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
|
345 |
@{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
|
346 |
@{lemma "rotate 3 [a,b,c,d] = [d,a,b,c]" by (simp add:rotate_def eval_nat_numeral)}\\ |
40077 | 347 |
@{lemma "replicate 4 a = [a,a,a,a]" by (simp add:eval_nat_numeral)}\\ |
67170 | 348 |
@{lemma "[2..<5] = [2,3,4]" by (simp add:eval_nat_numeral)}\\ |
349 |
@{lemma "min_list [3,1,-2::int] = -2" by (simp)}\\ |
|
350 |
@{lemma "arg_min_list (\<lambda>i. i*i) [3,-1,1,-2::int] = -1" by (simp)} |
|
26771 | 351 |
\end{tabular}} |
352 |
\caption{Characteristic examples} |
|
353 |
\label{fig:Characteristic} |
|
354 |
\end{figure} |
|
29927 | 355 |
Figure~\ref{fig:Characteristic} shows characteristic examples |
26771 | 356 |
that should give an intuitive understanding of the above functions. |
60758 | 357 |
\<close> |
358 |
||
71779 | 359 |
text\<open>The following simple sort(ed) functions are intended for proofs, |
60758 | 360 |
not for efficient implementations.\<close> |
24616 | 361 |
|
66434
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66358
diff
changeset
|
362 |
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
|
363 |
|
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66358
diff
changeset
|
364 |
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
|
365 |
"sorted_wrt P [] = True" | |
68109 | 366 |
"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
|
367 |
|
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66358
diff
changeset
|
368 |
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
|
369 |
|
25221
5ded95dda5df
append/member: more light-weight way to declare authentic syntax;
wenzelm
parents:
25215
diff
changeset
|
370 |
context linorder |
5ded95dda5df
append/member: more light-weight way to declare authentic syntax;
wenzelm
parents:
25215
diff
changeset
|
371 |
begin |
67479 | 372 |
|
73707 | 373 |
abbreviation sorted :: "'a list \<Rightarrow> bool" where |
74424 | 374 |
"sorted \<equiv> sorted_wrt (\<le>)" |
67479 | 375 |
|
73707 | 376 |
lemma sorted_simps: "sorted [] = True" "sorted (x # ys) = ((\<forall>y \<in> set ys. x\<le>y) \<and> sorted ys)" |
377 |
by auto |
|
378 |
||
73709 | 379 |
lemma strict_sorted_simps: "sorted_wrt (<) [] = True" "sorted_wrt (<) (x # ys) = ((\<forall>y \<in> set ys. x<y) \<and> sorted_wrt (<) ys)" |
73707 | 380 |
by auto |
24697 | 381 |
|
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
|
382 |
primrec insort_key :: "('b \<Rightarrow> 'a) \<Rightarrow> 'b \<Rightarrow> 'b list \<Rightarrow> 'b list" where |
72095
cfb6c22a5636
lemmas about sets and the enumerate operator
paulson <lp15@cam.ac.uk>
parents:
71991
diff
changeset
|
383 |
"insort_key f x [] = [x]" | |
cfb6c22a5636
lemmas about sets and the enumerate operator
paulson <lp15@cam.ac.uk>
parents:
71991
diff
changeset
|
384 |
"insort_key f x (y#ys) = |
50548 | 385 |
(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
|
386 |
|
35195 | 387 |
definition sort_key :: "('b \<Rightarrow> 'a) \<Rightarrow> 'b list \<Rightarrow> 'b list" where |
72095
cfb6c22a5636
lemmas about sets and the enumerate operator
paulson <lp15@cam.ac.uk>
parents:
71991
diff
changeset
|
388 |
"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
|
389 |
|
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 |
definition insort_insert_key :: "('b \<Rightarrow> 'a) \<Rightarrow> 'b \<Rightarrow> 'b list \<Rightarrow> 'b list" where |
72095
cfb6c22a5636
lemmas about sets and the enumerate operator
paulson <lp15@cam.ac.uk>
parents:
71991
diff
changeset
|
391 |
"insort_insert_key f x xs = |
50548 | 392 |
(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
|
393 |
|
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
|
394 |
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
|
395 |
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
|
396 |
abbreviation "insort_insert \<equiv> insort_insert_key (\<lambda>x. x)" |
35608 | 397 |
|
67684 | 398 |
definition stable_sort_key :: "(('b \<Rightarrow> 'a) \<Rightarrow> 'b list \<Rightarrow> 'b list) \<Rightarrow> bool" where |
72095
cfb6c22a5636
lemmas about sets and the enumerate operator
paulson <lp15@cam.ac.uk>
parents:
71991
diff
changeset
|
399 |
"stable_sort_key sk = |
67684 | 400 |
(\<forall>f xs k. filter (\<lambda>y. f y = k) (sk f xs) = filter (\<lambda>y. f y = k) xs)" |
401 |
||
73709 | 402 |
lemma strict_sorted_iff: "sorted_wrt (<) l \<longleftrightarrow> sorted l \<and> distinct l" |
72095
cfb6c22a5636
lemmas about sets and the enumerate operator
paulson <lp15@cam.ac.uk>
parents:
71991
diff
changeset
|
403 |
by (induction l) (auto iff: antisym_conv1) |
cfb6c22a5636
lemmas about sets and the enumerate operator
paulson <lp15@cam.ac.uk>
parents:
71991
diff
changeset
|
404 |
|
73709 | 405 |
lemma strict_sorted_imp_sorted: "sorted_wrt (<) xs \<Longrightarrow> sorted xs" |
72095
cfb6c22a5636
lemmas about sets and the enumerate operator
paulson <lp15@cam.ac.uk>
parents:
71991
diff
changeset
|
406 |
by (auto simp: strict_sorted_iff) |
71860 | 407 |
|
25221
5ded95dda5df
append/member: more light-weight way to declare authentic syntax;
wenzelm
parents:
25215
diff
changeset
|
408 |
end |
5ded95dda5df
append/member: more light-weight way to declare authentic syntax;
wenzelm
parents:
25215
diff
changeset
|
409 |
|
24616 | 410 |
|
60758 | 411 |
subsubsection \<open>List comprehension\<close> |
412 |
||
413 |
text\<open>Input syntax for Haskell-like list comprehension notation. |
|
61799 | 414 |
Typical example: \<open>[(x,y). x \<leftarrow> xs, y \<leftarrow> ys, x \<noteq> y]\<close>, |
415 |
the list of all pairs of distinct elements from \<open>xs\<close> and \<open>ys\<close>. |
|
416 |
The syntax is as in Haskell, except that \<open>|\<close> becomes a dot |
|
417 |
(like in Isabelle's set comprehension): \<open>[e. x \<leftarrow> xs, \<dots>]\<close> rather than |
|
24349 | 418 |
\verb![e| x <- xs, ...]!. |
419 |
||
420 |
The qualifiers after the dot are |
|
421 |
\begin{description} |
|
61799 | 422 |
\item[generators] \<open>p \<leftarrow> xs\<close>, |
423 |
where \<open>p\<close> is a pattern and \<open>xs\<close> an expression of list type, or |
|
424 |
\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
|
425 |
%\item[local bindings] @ {text"let x = e"}. |
24349 | 426 |
\end{description} |
23240 | 427 |
|
24476
f7ad9fbbeeaa
turned list comprehension translations into ML to optimize base case
nipkow
parents:
24471
diff
changeset
|
428 |
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
|
429 |
misunderstandings, the translation into desugared form is not reversed |
61799 | 430 |
upon output. Note that the translation of \<open>[e. x \<leftarrow> xs]\<close> is |
69593 | 431 |
optmized to \<^term>\<open>map (%x. e) xs\<close>. |
23240 | 432 |
|
24349 | 433 |
It is easy to write short list comprehensions which stand for complex |
434 |
expressions. During proofs, they may become unreadable (and |
|
435 |
mangled). In such cases it can be advisable to introduce separate |
|
60758 | 436 |
definitions for the list comprehensions in question.\<close> |
24349 | 437 |
|
46138
85f8d8a8c711
improved list comprehension syntax: more careful treatment of position constraints, which enables PIDE markup;
wenzelm
parents:
46133
diff
changeset
|
438 |
nonterminal lc_qual and lc_quals |
23192 | 439 |
|
440 |
syntax |
|
46138
85f8d8a8c711
improved list comprehension syntax: more careful treatment of position constraints, which enables PIDE markup;
wenzelm
parents:
46133
diff
changeset
|
441 |
"_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
|
442 |
"_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
|
443 |
"_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
|
444 |
(*"_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
|
445 |
"_lc_end" :: "lc_quals" ("]") |
85f8d8a8c711
improved list comprehension syntax: more careful treatment of position constraints, which enables PIDE markup;
wenzelm
parents:
46133
diff
changeset
|
446 |
"_lc_quals" :: "lc_qual \<Rightarrow> lc_quals \<Rightarrow> lc_quals" (", __") |
23192 | 447 |
|
61955
e96292f32c3c
former "xsymbols" syntax is used by default, and ASCII replacement syntax with print mode "ASCII";
wenzelm
parents:
61941
diff
changeset
|
448 |
syntax (ASCII) |
e96292f32c3c
former "xsymbols" syntax is used by default, and ASCII replacement syntax with print mode "ASCII";
wenzelm
parents:
61941
diff
changeset
|
449 |
"_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
|
450 |
|
60758 | 451 |
parse_translation \<open> |
68362 | 452 |
let |
69593 | 453 |
val NilC = Syntax.const \<^const_syntax>\<open>Nil\<close>; |
454 |
val ConsC = Syntax.const \<^const_syntax>\<open>Cons\<close>; |
|
455 |
val mapC = Syntax.const \<^const_syntax>\<open>map\<close>; |
|
456 |
val concatC = Syntax.const \<^const_syntax>\<open>concat\<close>; |
|
457 |
val IfC = Syntax.const \<^const_syntax>\<open>If\<close>; |
|
458 |
val dummyC = Syntax.const \<^const_syntax>\<open>Pure.dummy_pattern\<close> |
|
68362 | 459 |
|
460 |
fun single x = ConsC $ x $ NilC; |
|
461 |
||
462 |
fun pat_tr ctxt p e opti = (* %x. case x of p => e | _ => [] *) |
|
463 |
let |
|
464 |
(* FIXME proper name context!? *) |
|
465 |
val x = |
|
466 |
Free (singleton (Name.variant_list (fold Term.add_free_names [p, e] [])) "x", dummyT); |
|
467 |
val e = if opti then single e else e; |
|
69593 | 468 |
val case1 = Syntax.const \<^syntax_const>\<open>_case1\<close> $ p $ e; |
68362 | 469 |
val case2 = |
69593 | 470 |
Syntax.const \<^syntax_const>\<open>_case1\<close> $ dummyC $ NilC; |
471 |
val cs = Syntax.const \<^syntax_const>\<open>_case2\<close> $ case1 $ case2; |
|
68362 | 472 |
in Syntax_Trans.abs_tr [x, Case_Translation.case_tr false ctxt [x, cs]] end; |
473 |
||
474 |
fun pair_pat_tr (x as Free _) e = Syntax_Trans.abs_tr [x, e] |
|
475 |
| pair_pat_tr (_ $ p1 $ p2) e = |
|
69593 | 476 |
Syntax.const \<^const_syntax>\<open>case_prod\<close> $ pair_pat_tr p1 (pair_pat_tr p2 e) |
68362 | 477 |
| pair_pat_tr dummy e = Syntax_Trans.abs_tr [Syntax.const "_idtdummy", e] |
478 |
||
69593 | 479 |
fun pair_pat ctxt (Const (\<^const_syntax>\<open>Pair\<close>,_) $ s $ t) = |
68362 | 480 |
pair_pat ctxt s andalso pair_pat ctxt t |
481 |
| pair_pat ctxt (Free (s,_)) = |
|
482 |
let |
|
483 |
val thy = Proof_Context.theory_of ctxt; |
|
484 |
val s' = Proof_Context.intern_const ctxt s; |
|
485 |
in not (Sign.declared_const thy s') end |
|
486 |
| pair_pat _ t = (t = dummyC); |
|
487 |
||
488 |
fun abs_tr ctxt p e opti = |
|
489 |
let val p = Term_Position.strip_positions p |
|
490 |
in if pair_pat ctxt p |
|
491 |
then (pair_pat_tr p e, true) |
|
492 |
else (pat_tr ctxt p e opti, false) |
|
493 |
end |
|
494 |
||
69593 | 495 |
fun lc_tr ctxt [e, Const (\<^syntax_const>\<open>_lc_test\<close>, _) $ b, qs] = |
68362 | 496 |
let |
497 |
val res = |
|
498 |
(case qs of |
|
69593 | 499 |
Const (\<^syntax_const>\<open>_lc_end\<close>, _) => single e |
500 |
| Const (\<^syntax_const>\<open>_lc_quals\<close>, _) $ q $ qs => lc_tr ctxt [e, q, qs]); |
|
68362 | 501 |
in IfC $ b $ res $ NilC end |
502 |
| lc_tr ctxt |
|
69593 | 503 |
[e, Const (\<^syntax_const>\<open>_lc_gen\<close>, _) $ p $ es, |
504 |
Const(\<^syntax_const>\<open>_lc_end\<close>, _)] = |
|
68362 | 505 |
(case abs_tr ctxt p e true of |
506 |
(f, true) => mapC $ f $ es |
|
507 |
| (f, false) => concatC $ (mapC $ f $ es)) |
|
508 |
| lc_tr ctxt |
|
69593 | 509 |
[e, Const (\<^syntax_const>\<open>_lc_gen\<close>, _) $ p $ es, |
510 |
Const (\<^syntax_const>\<open>_lc_quals\<close>, _) $ q $ qs] = |
|
68362 | 511 |
let val e' = lc_tr ctxt [e, q, qs]; |
512 |
in concatC $ (mapC $ (fst (abs_tr ctxt p e' false)) $ es) end; |
|
513 |
||
69593 | 514 |
in [(\<^syntax_const>\<open>_listcompr\<close>, lc_tr)] end |
60758 | 515 |
\<close> |
516 |
||
517 |
ML_val \<open> |
|
42167 | 518 |
let |
69593 | 519 |
val read = Syntax.read_term \<^context> o Syntax.implode_input; |
60160 | 520 |
fun check s1 s2 = |
521 |
read s1 aconv read s2 orelse |
|
522 |
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
|
523 |
quote (#1 (Input.source_content s1)) ^ Position.here_list [Input.pos_of s1, Input.pos_of s2]); |
42167 | 524 |
in |
60160 | 525 |
check \<open>[(x,y,z). b]\<close> \<open>if b then [(x, y, z)] else []\<close>; |
68362 | 526 |
check \<open>[(x,y,z). (x,_,y)\<leftarrow>xs]\<close> \<open>map (\<lambda>(x,_,y). (x, y, z)) xs\<close>; |
527 |
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 | 528 |
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>; |
529 |
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>; |
|
530 |
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>; |
|
531 |
check \<open>[(x,y). Cons True x \<leftarrow> xs]\<close> |
|
532 |
\<open>concat (map (\<lambda>xa. case xa of [] \<Rightarrow> [] | True # x \<Rightarrow> [(x, y)] | False # x \<Rightarrow> []) xs)\<close>; |
|
533 |
check \<open>[(x,y,z). Cons x [] \<leftarrow> xs]\<close> |
|
534 |
\<open>concat (map (\<lambda>xa. case xa of [] \<Rightarrow> [] | [x] \<Rightarrow> [(x, y, z)] | x # aa # lista \<Rightarrow> []) xs)\<close>; |
|
535 |
check \<open>[(x,y,z). x<a, x>b, x=d]\<close> |
|
536 |
\<open>if x < a then if b < x then if x = d then [(x, y, z)] else [] else [] else []\<close>; |
|
537 |
check \<open>[(x,y,z). x<a, x>b, y\<leftarrow>ys]\<close> |
|
538 |
\<open>if x < a then if b < x then map (\<lambda>y. (x, y, z)) ys else [] else []\<close>; |
|
68362 | 539 |
check \<open>[(x,y,z). x<a, (_,x)\<leftarrow>xs,y>b]\<close> |
540 |
\<open>if x < a then concat (map (\<lambda>(_,x). if b < y then [(x, y, z)] else []) xs) else []\<close>; |
|
60160 | 541 |
check \<open>[(x,y,z). x<a, x\<leftarrow>xs, y\<leftarrow>ys]\<close> |
542 |
\<open>if x < a then concat (map (\<lambda>x. map (\<lambda>y. (x, y, z)) ys) xs) else []\<close>; |
|
543 |
check \<open>[(x,y,z). x\<leftarrow>xs, x>b, y<a]\<close> |
|
544 |
\<open>concat (map (\<lambda>x. if b < x then if y < a then [(x, y, z)] else [] else []) xs)\<close>; |
|
545 |
check \<open>[(x,y,z). x\<leftarrow>xs, x>b, y\<leftarrow>ys]\<close> |
|
546 |
\<open>concat (map (\<lambda>x. if b < x then map (\<lambda>y. (x, y, z)) ys else []) xs)\<close>; |
|
68362 | 547 |
check \<open>[(x,y,z). x\<leftarrow>xs, (y,_)\<leftarrow>ys,y>x]\<close> |
548 |
\<open>concat (map (\<lambda>x. concat (map (\<lambda>(y,_). if x < y then [(x, y, z)] else []) ys)) xs)\<close>; |
|
60160 | 549 |
check \<open>[(x,y,z). x\<leftarrow>xs, y\<leftarrow>ys,z\<leftarrow>zs]\<close> |
550 |
\<open>concat (map (\<lambda>x. concat (map (\<lambda>y. map (\<lambda>z. (x, y, z)) zs) ys)) xs)\<close> |
|
42167 | 551 |
end; |
60758 | 552 |
\<close> |
42167 | 553 |
|
60758 | 554 |
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
|
555 |
(* 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
|
556 |
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
|
557 |
|
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
|
558 |
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
|
559 |
sig |
78801 | 560 |
val proc: Simplifier.proc |
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
|
561 |
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
|
562 |
|
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 |
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
|
564 |
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
|
565 |
|
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 |
(* 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
|
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 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
|
569 |
(case Thm.term_of ct of |
69593 | 570 |
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
|
571 |
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
|
572 |
| _ => 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
|
573 |
|
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 |
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
|
575 |
(case Thm.term_of ct of |
69593 | 576 |
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
|
577 |
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
|
578 |
| _ => 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
|
579 |
|
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
|
580 |
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
|
581 |
(case Thm.term_of ct of |
69593 | 582 |
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
|
583 |
| _ => 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
|
584 |
|
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 |
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
|
586 |
|
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 |
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
|
588 |
Conv.try_conv |
51315
536a5603a138
provide common HOLogic.conj_conv and HOLogic.eq_conv;
wenzelm
parents:
51314
diff
changeset
|
589 |
(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
|
590 |
|
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
|
591 |
fun right_hand_set_comprehension_conv conv ctxt = |
51315
536a5603a138
provide common HOLogic.conj_conv and HOLogic.eq_conv;
wenzelm
parents:
51314
diff
changeset
|
592 |
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
|
593 |
(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
|
594 |
|
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
|
595 |
|
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
|
596 |
(* 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
|
597 |
|
60156 | 598 |
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
|
599 |
|
60158 | 600 |
local |
601 |
||
602 |
val set_Nil_I = @{lemma "set [] = {x. False}" by (simp add: empty_def [symmetric])} |
|
603 |
val set_singleton = @{lemma "set [a] = {x. x = a}" by simp} |
|
604 |
val inst_Collect_mem_eq = @{lemma "set A = {x. x \<in> set A}" by simp} |
|
605 |
val del_refl_eq = @{lemma "(t = t \<and> P) \<equiv> P" by simp} |
|
606 |
||
69593 | 607 |
fun mk_set T = Const (\<^const_name>\<open>set\<close>, HOLogic.listT T --> HOLogic.mk_setT T) |
608 |
fun dest_set (Const (\<^const_name>\<open>set\<close>, _) $ xs) = xs |
|
609 |
||
610 |
fun dest_singleton_list (Const (\<^const_name>\<open>Cons\<close>, _) $ t $ (Const (\<^const_name>\<open>Nil\<close>, _))) = t |
|
60158 | 611 |
| dest_singleton_list t = raise TERM ("dest_singleton_list", [t]) |
612 |
||
613 |
(*We check that one case returns a singleton list and all other cases |
|
614 |
return [], and return the index of the one singleton list case.*) |
|
615 |
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
|
616 |
let |
60158 | 617 |
fun check (i, case_t) s = |
618 |
(case strip_abs_body case_t of |
|
69593 | 619 |
(Const (\<^const_name>\<open>Nil\<close>, _)) => s |
60158 | 620 |
| _ => (case s of SOME NONE => SOME (SOME i) | _ => NONE)) |
621 |
in |
|
622 |
fold_index check cases (SOME NONE) |> the_default NONE |
|
623 |
end |
|
624 |
||
625 |
(*returns condition continuing term option*) |
|
69593 | 626 |
fun dest_if (Const (\<^const_name>\<open>If\<close>, _) $ cond $ then_t $ Const (\<^const_name>\<open>Nil\<close>, _)) = |
60158 | 627 |
SOME (cond, then_t) |
628 |
| dest_if _ = NONE |
|
629 |
||
630 |
(*returns (case_expr type index chosen_case constr_name) option*) |
|
631 |
fun dest_case ctxt case_term = |
|
632 |
let |
|
633 |
val (case_const, args) = strip_comb case_term |
|
634 |
in |
|
635 |
(case try dest_Const case_const of |
|
636 |
SOME (c, T) => |
|
637 |
(case Ctr_Sugar.ctr_sugar_of_case ctxt c of |
|
638 |
SOME {ctrs, ...} => |
|
639 |
(case possible_index_of_singleton_case (fst (split_last args)) of |
|
640 |
SOME i => |
|
641 |
let |
|
642 |
val constr_names = map (fst o dest_Const) ctrs |
|
643 |
val (Ts, _) = strip_type T |
|
644 |
val T' = List.last Ts |
|
645 |
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
|
646 |
| 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
|
647 |
| NONE => NONE) |
60158 | 648 |
| NONE => NONE) |
649 |
end |
|
650 |
||
60752 | 651 |
fun tac ctxt [] = |
652 |
resolve_tac ctxt [set_singleton] 1 ORELSE |
|
653 |
resolve_tac ctxt [inst_Collect_mem_eq] 1 |
|
60158 | 654 |
| tac ctxt (If :: cont) = |
62390 | 655 |
Splitter.split_tac ctxt @{thms if_split} 1 |
60752 | 656 |
THEN resolve_tac ctxt @{thms conjI} 1 |
657 |
THEN resolve_tac ctxt @{thms impI} 1 |
|
60159
879918f4ee0f
tuned -- avoid odd rebinding of "ctxt" and "context";
wenzelm
parents:
60158
diff
changeset
|
658 |
THEN Subgoal.FOCUS (fn {prems, context = ctxt', ...} => |
60158 | 659 |
CONVERSION (right_hand_set_comprehension_conv (K |
660 |
(HOLogic.conj_conv (Conv.rewr_conv (List.last prems RS @{thm Eq_TrueI})) Conv.all_conv |
|
661 |
then_conv |
|
60159
879918f4ee0f
tuned -- avoid odd rebinding of "ctxt" and "context";
wenzelm
parents:
60158
diff
changeset
|
662 |
rewr_conv' @{lemma "(True \<and> P) = P" by simp})) ctxt') 1) ctxt 1 |
60158 | 663 |
THEN tac ctxt cont |
60752 | 664 |
THEN resolve_tac ctxt @{thms impI} 1 |
60159
879918f4ee0f
tuned -- avoid odd rebinding of "ctxt" and "context";
wenzelm
parents:
60158
diff
changeset
|
665 |
THEN Subgoal.FOCUS (fn {prems, context = ctxt', ...} => |
60158 | 666 |
CONVERSION (right_hand_set_comprehension_conv (K |
667 |
(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
|
668 |
then_conv rewr_conv' @{lemma "(False \<and> P) = False" by simp})) ctxt') 1) ctxt 1 |
60752 | 669 |
THEN resolve_tac ctxt [set_Nil_I] 1 |
60158 | 670 |
| tac ctxt (Case (T, i) :: cont) = |
671 |
let |
|
672 |
val SOME {injects, distincts, case_thms, split, ...} = |
|
673 |
Ctr_Sugar.ctr_sugar_of ctxt (fst (dest_Type T)) |
|
674 |
in |
|
675 |
(* do case distinction *) |
|
676 |
Splitter.split_tac ctxt [split] 1 |
|
677 |
THEN EVERY (map_index (fn (i', _) => |
|
60752 | 678 |
(if i' < length case_thms - 1 then resolve_tac ctxt @{thms conjI} 1 else all_tac) |
679 |
THEN REPEAT_DETERM (resolve_tac ctxt @{thms allI} 1) |
|
680 |
THEN resolve_tac ctxt @{thms impI} 1 |
|
60158 | 681 |
THEN (if i' = i then |
682 |
(* continue recursively *) |
|
60159
879918f4ee0f
tuned -- avoid odd rebinding of "ctxt" and "context";
wenzelm
parents:
60158
diff
changeset
|
683 |
Subgoal.FOCUS (fn {prems, context = ctxt', ...} => |
60158 | 684 |
CONVERSION (Thm.eta_conversion then_conv right_hand_set_comprehension_conv (K |
685 |
((HOLogic.conj_conv |
|
686 |
(HOLogic.eq_conv Conv.all_conv (rewr_conv' (List.last prems)) then_conv |
|
687 |
(Conv.try_conv (Conv.rewrs_conv (map mk_meta_eq injects)))) |
|
688 |
Conv.all_conv) |
|
689 |
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
|
690 |
then_conv conjunct_assoc_conv)) ctxt' |
879918f4ee0f
tuned -- avoid odd rebinding of "ctxt" and "context";
wenzelm
parents:
60158
diff
changeset
|
691 |
then_conv |
879918f4ee0f
tuned -- avoid odd rebinding of "ctxt" and "context";
wenzelm
parents:
60158
diff
changeset
|
692 |
(HOLogic.Trueprop_conv |
879918f4ee0f
tuned -- avoid odd rebinding of "ctxt" and "context";
wenzelm
parents:
60158
diff
changeset
|
693 |
(HOLogic.eq_conv Conv.all_conv (Collect_conv (fn (_, ctxt'') => |
879918f4ee0f
tuned -- avoid odd rebinding of "ctxt" and "context";
wenzelm
parents:
60158
diff
changeset
|
694 |
Conv.repeat_conv |
879918f4ee0f
tuned -- avoid odd rebinding of "ctxt" and "context";
wenzelm
parents:
60158
diff
changeset
|
695 |
(all_but_last_exists_conv |
879918f4ee0f
tuned -- avoid odd rebinding of "ctxt" and "context";
wenzelm
parents:
60158
diff
changeset
|
696 |
(K (rewr_conv' |
879918f4ee0f
tuned -- avoid odd rebinding of "ctxt" and "context";
wenzelm
parents:
60158
diff
changeset
|
697 |
@{lemma "(\<exists>x. x = t \<and> P x) = P t" by simp})) ctxt'')) ctxt')))) 1) ctxt 1 |
60158 | 698 |
THEN tac ctxt cont |
699 |
else |
|
60159
879918f4ee0f
tuned -- avoid odd rebinding of "ctxt" and "context";
wenzelm
parents:
60158
diff
changeset
|
700 |
Subgoal.FOCUS (fn {prems, context = ctxt', ...} => |
60158 | 701 |
CONVERSION |
702 |
(right_hand_set_comprehension_conv (K |
|
703 |
(HOLogic.conj_conv |
|
704 |
((HOLogic.eq_conv Conv.all_conv |
|
705 |
(rewr_conv' (List.last prems))) then_conv |
|
706 |
(Conv.rewrs_conv (map (fn th => th RS @{thm Eq_FalseI}) distincts))) |
|
707 |
Conv.all_conv then_conv |
|
60159
879918f4ee0f
tuned -- avoid odd rebinding of "ctxt" and "context";
wenzelm
parents:
60158
diff
changeset
|
708 |
(rewr_conv' @{lemma "(False \<and> P) = False" by simp}))) ctxt' then_conv |
60158 | 709 |
HOLogic.Trueprop_conv |
710 |
(HOLogic.eq_conv Conv.all_conv |
|
60159
879918f4ee0f
tuned -- avoid odd rebinding of "ctxt" and "context";
wenzelm
parents:
60158
diff
changeset
|
711 |
(Collect_conv (fn (_, ctxt'') => |
60158 | 712 |
Conv.repeat_conv |
713 |
(Conv.bottom_conv |
|
60159
879918f4ee0f
tuned -- avoid odd rebinding of "ctxt" and "context";
wenzelm
parents:
60158
diff
changeset
|
714 |
(K (rewr_conv' @{lemma "(\<exists>x. P) = P" by simp})) ctxt'')) ctxt'))) 1) ctxt 1 |
60752 | 715 |
THEN resolve_tac ctxt [set_Nil_I] 1)) case_thms) |
60158 | 716 |
end |
717 |
||
718 |
in |
|
719 |
||
78801 | 720 |
fun proc ctxt redex = |
60158 | 721 |
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
|
722 |
fun make_inner_eqs bound_vs Tis eqs t = |
60158 | 723 |
(case dest_case ctxt t of |
54404
9f0f1152c875
port list comprehension simproc to 'Ctr_Sugar' abstraction
blanchet
parents:
54295
diff
changeset
|
724 |
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
|
725 |
let |
52131 | 726 |
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
|
727 |
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
|
728 |
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
|
729 |
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
|
730 |
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
|
731 |
(Const (constr_name, map snd vs ---> T), map Bound (((length vs) - 1) downto 0)) |
69593 | 732 |
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
|
733 |
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
|
734 |
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
|
735 |
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
|
736 |
| 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
|
737 |
(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
|
738 |
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
|
739 |
| NONE => |
60158 | 740 |
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
|
741 |
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
|
742 |
let |
69593 | 743 |
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
|
744 |
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
|
745 |
(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
|
746 |
SOME t' => |
69593 | 747 |
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
|
748 |
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
|
749 |
| NONE => |
69593 | 750 |
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
|
751 |
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
|
752 |
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
|
753 |
((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
|
754 |
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
|
755 |
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
|
756 |
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
|
757 |
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
|
758 |
(rev bound_vs) (fold (curry HOLogic.mk_conj) eqs' pat_eq') |
59582 | 759 |
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
|
760 |
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
|
761 |
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
|
762 |
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
|
763 |
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
|
764 |
((Goal.prove ctxt [] [] rewrite_rule_t |
60159
879918f4ee0f
tuned -- avoid odd rebinding of "ctxt" and "context";
wenzelm
parents:
60158
diff
changeset
|
765 |
(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
|
766 |
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
|
767 |
in |
59582 | 768 |
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
|
769 |
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
|
770 |
|
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
|
771 |
end |
60158 | 772 |
|
773 |
end |
|
60758 | 774 |
\<close> |
41463
edbf0a86fb1c
adding simproc to rewrite list comprehensions to set comprehensions; adopting proofs
bulwahn
parents:
41372
diff
changeset
|
775 |
|
60159
879918f4ee0f
tuned -- avoid odd rebinding of "ctxt" and "context";
wenzelm
parents:
60158
diff
changeset
|
776 |
simproc_setup list_to_set_comprehension ("set xs") = |
78801 | 777 |
\<open>K List_to_Set_Comprehension.proc\<close> |
41463
edbf0a86fb1c
adding simproc to rewrite list comprehensions to set comprehensions; adopting proofs
bulwahn
parents:
41372
diff
changeset
|
778 |
|
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
|
779 |
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
|
780 |
hide_const (open) coset |
35115 | 781 |
|
49948
744934b818c7
moved quite generic material from theory Enum to more appropriate places
haftmann
parents:
49808
diff
changeset
|
782 |
|
69593 | 783 |
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
|
784 |
|
580dfc999ef6
added normal post setup; cleaned up "execution" constants
haftmann
parents:
21046
diff
changeset
|
785 |
lemma not_Cons_self [simp]: |
580dfc999ef6
added normal post setup; cleaned up "execution" constants
haftmann
parents:
21046
diff
changeset
|
786 |
"xs \<noteq> x # xs" |
13145 | 787 |
by (induct xs) auto |
13114 | 788 |
|
58807 | 789 |
lemma not_Cons_self2 [simp]: "x # xs \<noteq> xs" |
41697 | 790 |
by (rule not_Cons_self [symmetric]) |
13114 | 791 |
|
13142 | 792 |
lemma neq_Nil_conv: "(xs \<noteq> []) = (\<exists>y ys. xs = y # ys)" |
13145 | 793 |
by (induct xs) auto |
13114 | 794 |
|
67091 | 795 |
lemma tl_Nil: "tl xs = [] \<longleftrightarrow> xs = [] \<or> (\<exists>x. xs = [x])" |
53689 | 796 |
by (cases xs) auto |
797 |
||
74741
6e1fad4f602b
added eq_iff_swap for creating symmetric variants of thms; applied it in List.
nipkow
parents:
74424
diff
changeset
|
798 |
lemmas Nil_tl = tl_Nil[THEN eq_iff_swap] |
53689 | 799 |
|
13142 | 800 |
lemma length_induct: |
21061
580dfc999ef6
added normal post setup; cleaned up "execution" constants
haftmann
parents:
21046
diff
changeset
|
801 |
"(\<And>xs. \<forall>ys. length ys < length xs \<longrightarrow> P ys \<Longrightarrow> P xs) \<Longrightarrow> P xs" |
53689 | 802 |
by (fact measure_induct) |
13114 | 803 |
|
67168 | 804 |
lemma induct_list012: |
70275 | 805 |
"\<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 | 806 |
by induction_schema (pat_completeness, lexicographic_order) |
807 |
||
37289 | 808 |
lemma list_nonempty_induct [consumes 1, case_names single cons]: |
67168 | 809 |
"\<lbrakk> xs \<noteq> []; \<And>x. P [x]; \<And>x xs. xs \<noteq> [] \<Longrightarrow> P xs \<Longrightarrow> P (x # xs)\<rbrakk> \<Longrightarrow> P xs" |
810 |
by(induction xs rule: induct_list012) auto |
|
37289 | 811 |
|
45714 | 812 |
lemma inj_split_Cons: "inj_on (\<lambda>(xs, n). n#xs) X" |
813 |
by (auto intro!: inj_onI) |
|
13114 | 814 |
|
67399 | 815 |
lemma inj_on_Cons1 [simp]: "inj_on ((#) x) A" |
61630 | 816 |
by(simp add: inj_on_def) |
49948
744934b818c7
moved quite generic material from theory Enum to more appropriate places
haftmann
parents:
49808
diff
changeset
|
817 |
|
73307 | 818 |
|
69593 | 819 |
subsubsection \<open>\<^const>\<open>length\<close>\<close> |
60758 | 820 |
|
821 |
text \<open> |
|
61799 | 822 |
Needs to come before \<open>@\<close> because of theorem \<open>append_eq_append_conv\<close>. |
60758 | 823 |
\<close> |
13114 | 824 |
|
13142 | 825 |
lemma length_append [simp]: "length (xs @ ys) = length xs + length ys" |
13145 | 826 |
by (induct xs) auto |
13114 | 827 |
|
13142 | 828 |
lemma length_map [simp]: "length (map f xs) = length xs" |
13145 | 829 |
by (induct xs) auto |
13114 | 830 |
|
13142 | 831 |
lemma length_rev [simp]: "length (rev xs) = length xs" |
13145 | 832 |
by (induct xs) auto |
13114 | 833 |
|
13142 | 834 |
lemma length_tl [simp]: "length (tl xs) = length xs - 1" |
13145 | 835 |
by (cases xs) auto |
13114 | 836 |
|
13142 | 837 |
lemma length_0_conv [iff]: "(length xs = 0) = (xs = [])" |
13145 | 838 |
by (induct xs) auto |
13114 | 839 |
|
13142 | 840 |
lemma length_greater_0_conv [iff]: "(0 < length xs) = (xs \<noteq> [])" |
13145 | 841 |
by (induct xs) auto |
13114 | 842 |
|
67613 | 843 |
lemma length_pos_if_in_set: "x \<in> set xs \<Longrightarrow> length xs > 0" |
23479 | 844 |
by auto |
845 |
||
73307 | 846 |
lemma length_Suc_conv: "(length xs = Suc n) = (\<exists>y ys. xs = y # ys \<and> length ys = n)" |
13145 | 847 |
by (induct xs) auto |
13142 | 848 |
|
74741
6e1fad4f602b
added eq_iff_swap for creating symmetric variants of thms; applied it in List.
nipkow
parents:
74424
diff
changeset
|
849 |
lemmas Suc_length_conv = length_Suc_conv[THEN eq_iff_swap] |
68709 | 850 |
|
69312 | 851 |
lemma Suc_le_length_iff: |
852 |
"(Suc n \<le> length xs) = (\<exists>x ys. xs = x # ys \<and> n \<le> length ys)" |
|
853 |
by (metis Suc_le_D[of n] Suc_le_mono[of n] Suc_length_conv[of _ xs]) |
|
854 |
||
71585 | 855 |
lemma impossible_Cons: "length xs \<le> length ys \<Longrightarrow> xs = x # ys = False" |
58807 | 856 |
by (induct xs) auto |
25221
5ded95dda5df
append/member: more light-weight way to declare authentic syntax;
wenzelm
parents:
25215
diff
changeset
|
857 |
|
26442
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
haftmann
parents:
26300
diff
changeset
|
858 |
lemma list_induct2 [consumes 1, case_names Nil Cons]: |
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
haftmann
parents:
26300
diff
changeset
|
859 |
"length xs = length ys \<Longrightarrow> P [] [] \<Longrightarrow> |
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
haftmann
parents:
26300
diff
changeset
|
860 |
(\<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
|
861 |
\<Longrightarrow> P xs ys" |
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
haftmann
parents:
26300
diff
changeset
|
862 |
proof (induct xs arbitrary: ys) |
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
haftmann
parents:
26300
diff
changeset
|
863 |
case (Cons x xs ys) then show ?case by (cases ys) simp_all |
68709 | 864 |
qed simp |
26442
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
haftmann
parents:
26300
diff
changeset
|
865 |
|
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
haftmann
parents:
26300
diff
changeset
|
866 |
lemma list_induct3 [consumes 2, case_names Nil Cons]: |
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
haftmann
parents:
26300
diff
changeset
|
867 |
"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
|
868 |
(\<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
|
869 |
\<Longrightarrow> P xs ys zs" |
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
haftmann
parents:
26300
diff
changeset
|
870 |
proof (induct xs arbitrary: ys zs) |
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
haftmann
parents:
26300
diff
changeset
|
871 |
case Nil then show ?case by simp |
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
haftmann
parents:
26300
diff
changeset
|
872 |
next |
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
haftmann
parents:
26300
diff
changeset
|
873 |
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
|
874 |
(cases zs, simp_all) |
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
haftmann
parents:
26300
diff
changeset
|
875 |
qed |
13114 | 876 |
|
36154
11c6106d7787
Respectfullness and preservation of list_rel
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
35828
diff
changeset
|
877 |
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
|
878 |
"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
|
879 |
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
|
880 |
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
|
881 |
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
|
882 |
proof (induct xs arbitrary: ys zs ws) |
11c6106d7787
Respectfullness and preservation of list_rel
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
35828
diff
changeset
|
883 |
case Nil then show ?case by simp |
11c6106d7787
Respectfullness and preservation of list_rel
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
35828
diff
changeset
|
884 |
next |
11c6106d7787
Respectfullness and preservation of list_rel
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
35828
diff
changeset
|
885 |
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
|
886 |
qed |
11c6106d7787
Respectfullness and preservation of list_rel
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
35828
diff
changeset
|
887 |
|
64963 | 888 |
lemma list_induct2': |
22493
db930e490fe5
added another rule for simultaneous induction, and lemmas for zip
krauss
parents:
22422
diff
changeset
|
889 |
"\<lbrakk> P [] []; |
db930e490fe5
added another rule for simultaneous induction, and lemmas for zip
krauss
parents:
22422
diff
changeset
|
890 |
\<And>x xs. P (x#xs) []; |
db930e490fe5
added another rule for simultaneous induction, and lemmas for zip
krauss
parents:
22422
diff
changeset
|
891 |
\<And>y ys. P [] (y#ys); |
db930e490fe5
added another rule for simultaneous induction, and lemmas for zip
krauss
parents:
22422
diff
changeset
|
892 |
\<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
|
893 |
\<Longrightarrow> P xs ys" |
db930e490fe5
added another rule for simultaneous induction, and lemmas for zip
krauss
parents:
22422
diff
changeset
|
894 |
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
|
895 |
|
55524
f41ef840f09d
folded 'list_all2' with the relator generated by 'datatype_new'
blanchet
parents:
55473
diff
changeset
|
896 |
lemma list_all2_iff: |
f41ef840f09d
folded 'list_all2' with the relator generated by 'datatype_new'
blanchet
parents:
55473
diff
changeset
|
897 |
"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
|
898 |
by (induct xs ys rule: list_induct2') auto |
f41ef840f09d
folded 'list_all2' with the relator generated by 'datatype_new'
blanchet
parents:
55473
diff
changeset
|
899 |
|
22143
cf58486ca11b
Added simproc list_neq (prompted by an application)
nipkow
parents:
21911
diff
changeset
|
900 |
lemma neq_if_length_neq: "length xs \<noteq> length ys \<Longrightarrow> (xs = ys) == False" |
24349 | 901 |
by (rule Eq_FalseI) auto |
24037 | 902 |
|
60758 | 903 |
|
61799 | 904 |
subsubsection \<open>\<open>@\<close> -- append\<close> |
13114 | 905 |
|
63662 | 906 |
global_interpretation append: monoid append Nil |
907 |
proof |
|
908 |
fix xs ys zs :: "'a list" |
|
909 |
show "(xs @ ys) @ zs = xs @ (ys @ zs)" |
|
910 |
by (induct xs) simp_all |
|
911 |
show "xs @ [] = xs" |
|
912 |
by (induct xs) simp_all |
|
913 |
qed simp |
|
914 |
||
13142 | 915 |
lemma append_assoc [simp]: "(xs @ ys) @ zs = xs @ (ys @ zs)" |
63662 | 916 |
by (fact append.assoc) |
917 |
||
918 |
lemma append_Nil2: "xs @ [] = xs" |
|
919 |
by (fact append.right_neutral) |
|
3507 | 920 |
|
13142 | 921 |
lemma append_is_Nil_conv [iff]: "(xs @ ys = []) = (xs = [] \<and> ys = [])" |
13145 | 922 |
by (induct xs) auto |
13114 | 923 |
|
74741
6e1fad4f602b
added eq_iff_swap for creating symmetric variants of thms; applied it in List.
nipkow
parents:
74424
diff
changeset
|
924 |
lemmas Nil_is_append_conv [iff] = append_is_Nil_conv[THEN eq_iff_swap] |
13114 | 925 |
|
13142 | 926 |
lemma append_self_conv [iff]: "(xs @ ys = xs) = (ys = [])" |
13145 | 927 |
by (induct xs) auto |
13114 | 928 |
|
74741
6e1fad4f602b
added eq_iff_swap for creating symmetric variants of thms; applied it in List.
nipkow
parents:
74424
diff
changeset
|
929 |
lemmas self_append_conv [iff] = append_self_conv[THEN eq_iff_swap] |
13114 | 930 |
|
54147
97a8ff4e4ac9
killed most "no_atp", to make Sledgehammer more complete
blanchet
parents:
53954
diff
changeset
|
931 |
lemma append_eq_append_conv [simp]: |
58807 | 932 |
"length xs = length ys \<or> length us = length vs |
71585 | 933 |
\<Longrightarrow> (xs@us = ys@vs) = (xs=ys \<and> us=vs)" |
68709 | 934 |
by (induct xs arbitrary: ys; case_tac ys; force) |
13142 | 935 |
|
24526 | 936 |
lemma append_eq_append_conv2: "(xs @ ys = zs @ ts) = |
67091 | 937 |
(\<exists>us. xs = zs @ us \<and> us @ ys = ts \<or> xs @ us = zs \<and> ys = us @ ts)" |
68709 | 938 |
proof (induct xs arbitrary: ys zs ts) |
939 |
case (Cons x xs) |
|
940 |
then show ?case |
|
69850 | 941 |
by (cases zs) auto |
68709 | 942 |
qed fastforce |
14495 | 943 |
|
34910
b23bd3ee4813
same_append_eq / append_same_eq are now used for simplifying induction rules.
berghofe
parents:
34064
diff
changeset
|
944 |
lemma same_append_eq [iff, induct_simp]: "(xs @ ys = xs @ zs) = (ys = zs)" |
13145 | 945 |
by simp |
13142 | 946 |
|
947 |
lemma append1_eq_conv [iff]: "(xs @ [x] = ys @ [y]) = (xs = ys \<and> x = y)" |
|
13145 | 948 |
by simp |
13114 | 949 |
|
34910
b23bd3ee4813
same_append_eq / append_same_eq are now used for simplifying induction rules.
berghofe
parents:
34064
diff
changeset
|
950 |
lemma append_same_eq [iff, induct_simp]: "(ys @ xs = zs @ xs) = (ys = zs)" |
13145 | 951 |
by simp |
13114 | 952 |
|
13142 | 953 |
lemma append_self_conv2 [iff]: "(xs @ ys = ys) = (xs = [])" |
13145 | 954 |
using append_same_eq [of _ _ "[]"] by auto |
3507 | 955 |
|
74741
6e1fad4f602b
added eq_iff_swap for creating symmetric variants of thms; applied it in List.
nipkow
parents:
74424
diff
changeset
|
956 |
lemmas self_append_conv2 [iff] = append_self_conv2[THEN eq_iff_swap] |
13114 | 957 |
|
71585 | 958 |
lemma hd_Cons_tl: "xs \<noteq> [] \<Longrightarrow> hd xs # tl xs = xs" |
63662 | 959 |
by (fact list.collapse) |
13114 | 960 |
|
13142 | 961 |
lemma hd_append: "hd (xs @ ys) = (if xs = [] then hd ys else hd xs)" |
13145 | 962 |
by (induct xs) auto |
13114 | 963 |
|
71585 | 964 |
lemma hd_append2 [simp]: "xs \<noteq> [] \<Longrightarrow> hd (xs @ ys) = hd xs" |
13145 | 965 |
by (simp add: hd_append split: list.split) |
13114 | 966 |
|
67091 | 967 |
lemma tl_append: "tl (xs @ ys) = (case xs of [] \<Rightarrow> tl ys | z#zs \<Rightarrow> zs @ ys)" |
13145 | 968 |
by (simp split: list.split) |
13114 | 969 |
|
71585 | 970 |
lemma tl_append2 [simp]: "xs \<noteq> [] \<Longrightarrow> tl (xs @ ys) = tl xs @ ys" |
13145 | 971 |
by (simp add: tl_append split: list.split) |
13114 | 972 |
|
75448 | 973 |
lemma tl_append_if: "tl (xs @ ys) = (if xs = [] then tl ys else tl xs @ ys)" |
974 |
by (simp) |
|
13114 | 975 |
|
14300 | 976 |
lemma Cons_eq_append_conv: "x#xs = ys@zs = |
67091 | 977 |
(ys = [] \<and> x#xs = zs \<or> (\<exists>ys'. x#ys' = ys \<and> xs = ys'@zs))" |
14300 | 978 |
by(cases ys) auto |
979 |
||
15281 | 980 |
lemma append_eq_Cons_conv: "(ys@zs = x#xs) = |
67091 | 981 |
(ys = [] \<and> zs = x#xs \<or> (\<exists>ys'. ys = x#ys' \<and> ys'@zs = xs))" |
15281 | 982 |
by(cases ys) auto |
983 |
||
63173 | 984 |
lemma longest_common_prefix: |
985 |
"\<exists>ps xs' ys'. xs = ps @ xs' \<and> ys = ps @ ys' |
|
986 |
\<and> (xs' = [] \<or> ys' = [] \<or> hd xs' \<noteq> hd ys')" |
|
987 |
by (induct xs ys rule: list_induct2') |
|
988 |
(blast, blast, blast, |
|
73932
fd21b4a93043
added opaque_combs and renamed hide_lams to opaque_lifting
desharna
parents:
73832
diff
changeset
|
989 |
metis (no_types, opaque_lifting) append_Cons append_Nil list.sel(1)) |
14300 | 990 |
|
61799 | 991 |
text \<open>Trivial rules for solving \<open>@\<close>-equations automatically.\<close> |
13114 | 992 |
|
71585 | 993 |
lemma eq_Nil_appendI: "xs = ys \<Longrightarrow> xs = [] @ ys" |
994 |
by simp |
|
995 |
||
996 |
lemma Cons_eq_appendI: "\<lbrakk>x # xs1 = ys; xs = xs1 @ zs\<rbrakk> \<Longrightarrow> x # xs = ys @ zs" |
|
997 |
by auto |
|
998 |
||
999 |
lemma append_eq_appendI: "\<lbrakk>xs @ xs1 = zs; ys = xs1 @ us\<rbrakk> \<Longrightarrow> xs @ ys = zs @ us" |
|
1000 |
by auto |
|
13114 | 1001 |
|
1002 |
||
60758 | 1003 |
text \<open> |
13145 | 1004 |
Simplification procedure for all list equalities. |
61799 | 1005 |
Currently only tries to rearrange \<open>@\<close> to see if |
13145 | 1006 |
- both lists end in a singleton list, |
1007 |
- or both lists end in the same list. |
|
60758 | 1008 |
\<close> |
1009 |
||
1010 |
simproc_setup list_eq ("(xs::'a list) = ys") = \<open> |
|
13462 | 1011 |
let |
69593 | 1012 |
fun last (cons as Const (\<^const_name>\<open>Cons\<close>, _) $ _ $ xs) = |
1013 |
(case xs of Const (\<^const_name>\<open>Nil\<close>, _) => cons | _ => last xs) |
|
1014 |
| last (Const(\<^const_name>\<open>append\<close>,_) $ _ $ ys) = last ys |
|
43594 | 1015 |
| last t = t; |
64963 | 1016 |
|
69593 | 1017 |
fun list1 (Const(\<^const_name>\<open>Cons\<close>,_) $ _ $ Const(\<^const_name>\<open>Nil\<close>,_)) = true |
43594 | 1018 |
| list1 _ = false; |
64963 | 1019 |
|
69593 | 1020 |
fun butlast ((cons as Const(\<^const_name>\<open>Cons\<close>,_) $ x) $ xs) = |
1021 |
(case xs of Const (\<^const_name>\<open>Nil\<close>, _) => xs | _ => cons $ butlast xs) |
|
1022 |
| butlast ((app as Const (\<^const_name>\<open>append\<close>, _) $ xs) $ ys) = app $ butlast ys |
|
1023 |
| butlast xs = Const(\<^const_name>\<open>Nil\<close>, fastype_of xs); |
|
64963 | 1024 |
|
43594 | 1025 |
val rearr_ss = |
69593 | 1026 |
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
|
1027 |
addsimps [@{thm append_assoc}, @{thm append_Nil}, @{thm append_Cons}]); |
64963 | 1028 |
|
51717
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents:
51678
diff
changeset
|
1029 |
fun list_eq ctxt (F as (eq as Const(_,eqT)) $ lhs $ rhs) = |
13462 | 1030 |
let |
43594 | 1031 |
val lastl = last lhs and lastr = last rhs; |
1032 |
fun rearr conv = |
|
1033 |
let |
|
1034 |
val lhs1 = butlast lhs and rhs1 = butlast rhs; |
|
1035 |
val Type(_,listT::_) = eqT |
|
1036 |
val appT = [listT,listT] ---> listT |
|
69593 | 1037 |
val app = Const(\<^const_name>\<open>append\<close>,appT) |
43594 | 1038 |
val F2 = eq $ (app$lhs1$lastl) $ (app$rhs1$lastr) |
1039 |
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
|
1040 |
val thm = Goal.prove ctxt [] [] eq |
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents:
51678
diff
changeset
|
1041 |
(K (simp_tac (put_simpset rearr_ss ctxt) 1)); |
43594 | 1042 |
in SOME ((conv RS (thm RS trans)) RS eq_reflection) end; |
1043 |
in |
|
1044 |
if list1 lastl andalso list1 lastr then rearr @{thm append1_eq_conv} |
|
1045 |
else if lastl aconv lastr then rearr @{thm append_same_eq} |
|
1046 |
else NONE |
|
1047 |
end; |
|
78099
4d9349989d94
more uniform simproc_setup: avoid vacuous abstraction over morphism, which sometimes captures context values in its functional closure;
wenzelm
parents:
77433
diff
changeset
|
1048 |
in K (fn ctxt => fn ct => list_eq ctxt (Thm.term_of ct)) end |
60758 | 1049 |
\<close> |
1050 |
||
1051 |
||
69593 | 1052 |
subsubsection \<open>\<^const>\<open>map\<close>\<close> |
13114 | 1053 |
|
58807 | 1054 |
lemma hd_map: "xs \<noteq> [] \<Longrightarrow> hd (map f xs) = f (hd xs)" |
1055 |
by (cases xs) simp_all |
|
1056 |
||
1057 |
lemma map_tl: "map f (tl xs) = tl (map f xs)" |
|
1058 |
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
|
1059 |
|
71585 | 1060 |
lemma map_ext: "(\<And>x. x \<in> set xs \<longrightarrow> f x = g x) \<Longrightarrow> map f xs = map g xs" |
13145 | 1061 |
by (induct xs) simp_all |
13114 | 1062 |
|
13142 | 1063 |
lemma map_ident [simp]: "map (\<lambda>x. x) = (\<lambda>xs. xs)" |
13145 | 1064 |
by (rule ext, induct_tac xs) auto |
13114 | 1065 |
|
13142 | 1066 |
lemma map_append [simp]: "map f (xs @ ys) = map f xs @ map f ys" |
13145 | 1067 |
by (induct xs) auto |
13114 | 1068 |
|
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
|
1069 |
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
|
1070 |
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
|
1071 |
|
67091 | 1072 |
lemma map_comp_map[simp]: "((map f) \<circ> (map g)) = map(f \<circ> g)" |
58807 | 1073 |
by (rule ext) simp |
35208 | 1074 |
|
13142 | 1075 |
lemma rev_map: "rev (map f xs) = map f (rev xs)" |
13145 | 1076 |
by (induct xs) auto |
13114 | 1077 |
|
67613 | 1078 |
lemma map_eq_conv[simp]: "(map f xs = map g xs) = (\<forall>x \<in> set xs. f x = g x)" |
13737 | 1079 |
by (induct xs) auto |
1080 |
||
44013
5cfc1c36ae97
moved recdef package to HOL/Library/Old_Recdef.thy
krauss
parents:
43594
diff
changeset
|
1081 |
lemma map_cong [fundef_cong]: |
40122
1d8ad2ff3e01
dropped (almost) redundant distinct.induct rule; distinct_simps again named distinct.simps
haftmann
parents:
40077
diff
changeset
|
1082 |
"xs = ys \<Longrightarrow> (\<And>x. x \<in> set ys \<Longrightarrow> f x = g x) \<Longrightarrow> map f xs = map g ys" |
58807 | 1083 |
by simp |
13114 | 1084 |
|
13142 | 1085 |
lemma map_is_Nil_conv [iff]: "(map f xs = []) = (xs = [])" |
74741
6e1fad4f602b
added eq_iff_swap for creating symmetric variants of thms; applied it in List.
nipkow
parents:
74424
diff
changeset
|
1086 |
by (rule list.map_disc_iff) |
6e1fad4f602b
added eq_iff_swap for creating symmetric variants of thms; applied it in List.
nipkow
parents:
74424
diff
changeset
|
1087 |
|
6e1fad4f602b
added eq_iff_swap for creating symmetric variants of thms; applied it in List.
nipkow
parents:
74424
diff
changeset
|
1088 |
lemmas Nil_is_map_conv [iff] = map_is_Nil_conv[THEN eq_iff_swap] |
13114 | 1089 |
|
18447 | 1090 |
lemma map_eq_Cons_conv: |
58807 | 1091 |
"(map f xs = y#ys) = (\<exists>z zs. xs = z#zs \<and> f z = y \<and> map f zs = ys)" |
13145 | 1092 |
by (cases xs) auto |
13114 | 1093 |
|
18447 | 1094 |
lemma Cons_eq_map_conv: |
58807 | 1095 |
"(x#xs = map f ys) = (\<exists>z zs. ys = z#zs \<and> x = f z \<and> xs = map f zs)" |
14025 | 1096 |
by (cases ys) auto |
1097 |
||
18447 | 1098 |
lemmas map_eq_Cons_D = map_eq_Cons_conv [THEN iffD1] |
1099 |
lemmas Cons_eq_map_D = Cons_eq_map_conv [THEN iffD1] |
|
1100 |
declare map_eq_Cons_D [dest!] Cons_eq_map_D [dest!] |
|
1101 |
||
14111 | 1102 |
lemma ex_map_conv: |
67091 | 1103 |
"(\<exists>xs. ys = map f xs) = (\<forall>y \<in> set ys. \<exists>x. y = f x)" |
18447 | 1104 |
by(induct ys, auto simp add: Cons_eq_map_conv) |
14111 | 1105 |
|
15110
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
1106 |
lemma map_eq_imp_length_eq: |
35510 | 1107 |
assumes "map f xs = map g ys" |
26734 | 1108 |
shows "length xs = length ys" |
53374
a14d2a854c02
tuned proofs -- clarified flow of facts wrt. calculation;
wenzelm
parents:
53017
diff
changeset
|
1109 |
using assms |
a14d2a854c02
tuned proofs -- clarified flow of facts wrt. calculation;
wenzelm
parents:
53017
diff
changeset
|
1110 |
proof (induct ys arbitrary: xs) |
26734 | 1111 |
case Nil then show ?case by simp |
1112 |
next |
|
1113 |
case (Cons y ys) then obtain z zs where xs: "xs = z # zs" by auto |
|
35510 | 1114 |
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
|
1115 |
with Cons have "length zs = length ys" by blast |
26734 | 1116 |
with xs show ?case by simp |
1117 |
qed |
|
64963 | 1118 |
|
15110
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
1119 |
lemma map_inj_on: |
68709 | 1120 |
assumes map: "map f xs = map f ys" and inj: "inj_on f (set xs Un set ys)" |
1121 |
shows "xs = ys" |
|
1122 |
using map_eq_imp_length_eq [OF map] assms |
|
1123 |
proof (induct rule: list_induct2) |
|
1124 |
case (Cons x xs y ys) |
|
1125 |
then show ?case |
|
1126 |
by (auto intro: sym) |
|
1127 |
qed auto |
|
15110
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
1128 |
|
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
1129 |
lemma inj_on_map_eq_map: |
58807 | 1130 |
"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
|
1131 |
by(blast dest:map_inj_on) |
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
1132 |
|
13114 | 1133 |
lemma map_injective: |
71585 | 1134 |
"map f xs = map f ys \<Longrightarrow> inj f \<Longrightarrow> xs = ys" |
24526 | 1135 |
by (induct ys arbitrary: xs) (auto dest!:injD) |
13114 | 1136 |
|
14339 | 1137 |
lemma inj_map_eq_map[simp]: "inj f \<Longrightarrow> (map f xs = map f ys) = (xs = ys)" |
1138 |
by(blast dest:map_injective) |
|
1139 |
||
71585 | 1140 |
lemma inj_mapI: "inj f \<Longrightarrow> inj (map f)" |
17589 | 1141 |
by (iprover dest: map_injective injD intro: inj_onI) |
13114 | 1142 |
|
71585 | 1143 |
lemma inj_mapD: "inj (map f) \<Longrightarrow> inj f" |
73932
fd21b4a93043
added opaque_combs and renamed hide_lams to opaque_lifting
desharna
parents:
73832
diff
changeset
|
1144 |
by (metis (no_types, opaque_lifting) injI list.inject list.simps(9) the_inv_f_f) |
13114 | 1145 |
|
14339 | 1146 |
lemma inj_map[iff]: "inj (map f) = inj f" |
13145 | 1147 |
by (blast dest: inj_mapD intro: inj_mapI) |
13114 | 1148 |
|
15303 | 1149 |
lemma inj_on_mapI: "inj_on f (\<Union>(set ` A)) \<Longrightarrow> inj_on (map f) A" |
68709 | 1150 |
by (blast intro:inj_onI dest:inj_onD map_inj_on) |
15303 | 1151 |
|
14343 | 1152 |
lemma map_idI: "(\<And>x. x \<in> set xs \<Longrightarrow> f x = x) \<Longrightarrow> map f xs = xs" |
1153 |
by (induct xs, auto) |
|
13114 | 1154 |
|
14402
4201e1916482
moved lemmas from MicroJava/Comp/AuxLemmas.thy to List.thy
nipkow
parents:
14395
diff
changeset
|
1155 |
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
|
1156 |
by (induct xs) auto |
4201e1916482
moved lemmas from MicroJava/Comp/AuxLemmas.thy to List.thy
nipkow
parents:
14395
diff
changeset
|
1157 |
|
15110
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
1158 |
lemma map_fst_zip[simp]: |
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
1159 |
"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
|
1160 |
by (induct rule:list_induct2, simp_all) |
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
1161 |
|
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
1162 |
lemma map_snd_zip[simp]: |
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
1163 |
"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
|
1164 |
by (induct rule:list_induct2, simp_all) |
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
1165 |
|
68215 | 1166 |
lemma map_fst_zip_take: |
1167 |
"map fst (zip xs ys) = take (min (length xs) (length ys)) xs" |
|
1168 |
by (induct xs ys rule: list_induct2') simp_all |
|
1169 |
||
1170 |
lemma map_snd_zip_take: |
|
1171 |
"map snd (zip xs ys) = take (min (length xs) (length ys)) ys" |
|
1172 |
by (induct xs ys rule: list_induct2') simp_all |
|
1173 |
||
66853 | 1174 |
lemma map2_map_map: "map2 h (map f xs) (map g xs) = map (\<lambda>x. h (f x) (g x)) xs" |
1175 |
by (induction xs) (auto) |
|
1176 |
||
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
|
1177 |
functor map: map |
47122 | 1178 |
by (simp_all add: id_def) |
1179 |
||
49948
744934b818c7
moved quite generic material from theory Enum to more appropriate places
haftmann
parents:
49808
diff
changeset
|
1180 |
declare map.id [simp] |
744934b818c7
moved quite generic material from theory Enum to more appropriate places
haftmann
parents:
49808
diff
changeset
|
1181 |
|
744934b818c7
moved quite generic material from theory Enum to more appropriate places
haftmann
parents:
49808
diff
changeset
|
1182 |
|
69593 | 1183 |
subsubsection \<open>\<^const>\<open>rev\<close>\<close> |
13114 | 1184 |
|
13142 | 1185 |
lemma rev_append [simp]: "rev (xs @ ys) = rev ys @ rev xs" |
13145 | 1186 |
by (induct xs) auto |
13114 | 1187 |
|
13142 | 1188 |
lemma rev_rev_ident [simp]: "rev (rev xs) = xs" |
13145 | 1189 |
by (induct xs) auto |
13114 | 1190 |
|
15870 | 1191 |
lemma rev_swap: "(rev xs = ys) = (xs = rev ys)" |
1192 |
by auto |
|
1193 |
||
13142 | 1194 |
lemma rev_is_Nil_conv [iff]: "(rev xs = []) = (xs = [])" |
13145 | 1195 |
by (induct xs) auto |
13114 | 1196 |
|
74741
6e1fad4f602b
added eq_iff_swap for creating symmetric variants of thms; applied it in List.
nipkow
parents:
74424
diff
changeset
|
1197 |
lemmas Nil_is_rev_conv [iff] = rev_is_Nil_conv[THEN eq_iff_swap] |
13114 | 1198 |
|
15870 | 1199 |
lemma rev_singleton_conv [simp]: "(rev xs = [x]) = (xs = [x])" |
1200 |
by (cases xs) auto |
|
1201 |
||
74741
6e1fad4f602b
added eq_iff_swap for creating symmetric variants of thms; applied it in List.
nipkow
parents:
74424
diff
changeset
|
1202 |
lemma singleton_rev_conv [simp]: "([x] = rev xs) = ([x] = xs)" |
15870 | 1203 |
by (cases xs) auto |
1204 |
||
54147
97a8ff4e4ac9
killed most "no_atp", to make Sledgehammer more complete
blanchet
parents:
53954
diff
changeset
|
1205 |
lemma rev_is_rev_conv [iff]: "(rev xs = rev ys) = (xs = ys)" |
69850 | 1206 |
proof (induct xs arbitrary: ys) |
1207 |
case Nil |
|
1208 |
then show ?case by force |
|
1209 |
next |
|
1210 |
case Cons |
|
1211 |
then show ?case by (cases ys) auto |
|
1212 |
qed |
|
13114 | 1213 |
|
15439 | 1214 |
lemma inj_on_rev[iff]: "inj_on rev A" |
1215 |
by(simp add:inj_on_def) |
|
1216 |
||
13366 | 1217 |
lemma rev_induct [case_names Nil snoc]: |
71585 | 1218 |
assumes "P []" and "\<And>x xs. P xs \<Longrightarrow> P (xs @ [x])" |
1219 |
shows "P xs" |
|
1220 |
proof - |
|
1221 |
have "P (rev (rev xs))" |
|
1222 |
by (rule_tac list = "rev xs" in list.induct, simp_all add: assms) |
|
1223 |
then show ?thesis by simp |
|
1224 |
qed |
|
13114 | 1225 |
|
13366 | 1226 |
lemma rev_exhaust [case_names Nil snoc]: |
71585 | 1227 |
"(xs = [] \<Longrightarrow> P) \<Longrightarrow>(\<And>ys y. xs = ys @ [y] \<Longrightarrow> P) \<Longrightarrow> P" |
13145 | 1228 |
by (induct xs rule: rev_induct) auto |
13114 | 1229 |
|
13366 | 1230 |
lemmas rev_cases = rev_exhaust |
1231 |
||
57577 | 1232 |
lemma rev_nonempty_induct [consumes 1, case_names single snoc]: |
1233 |
assumes "xs \<noteq> []" |
|
1234 |
and single: "\<And>x. P [x]" |
|
1235 |
and snoc': "\<And>x xs. xs \<noteq> [] \<Longrightarrow> P xs \<Longrightarrow> P (xs@[x])" |
|
1236 |
shows "P xs" |
|
60758 | 1237 |
using \<open>xs \<noteq> []\<close> proof (induct xs rule: rev_induct) |
57577 | 1238 |
case (snoc x xs) then show ?case |
1239 |
proof (cases xs) |
|
1240 |
case Nil thus ?thesis by (simp add: single) |
|
1241 |
next |
|
1242 |
case Cons with snoc show ?thesis by (fastforce intro!: snoc') |
|
1243 |
qed |
|
1244 |
qed simp |
|
1245 |
||
74742 | 1246 |
lemma rev_eq_Cons_iff[iff]: "(rev xs = y#ys) = (xs = rev ys @ [y])" |
18423 | 1247 |
by(rule rev_cases[of xs]) auto |
1248 |
||
74966 | 1249 |
lemma length_Suc_conv_rev: "(length xs = Suc n) = (\<exists>y ys. xs = ys @ [y] \<and> length ys = n)" |
1250 |
by (induct xs rule: rev_induct) auto |
|
1251 |
||
13114 | 1252 |
|
69593 | 1253 |
subsubsection \<open>\<^const>\<open>set\<close>\<close> |
13114 | 1254 |
|
67443
3abf6a722518
standardized towards new-style formal comments: isabelle update_comments;
wenzelm
parents:
67399
diff
changeset
|
1255 |
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
|
1256 |
|
13142 | 1257 |
lemma finite_set [iff]: "finite (set xs)" |
13145 | 1258 |
by (induct xs) auto |
13114 | 1259 |
|
13142 | 1260 |
lemma set_append [simp]: "set (xs @ ys) = (set xs \<union> set ys)" |
13145 | 1261 |
by (induct xs) auto |
13114 | 1262 |
|
67613 | 1263 |
lemma hd_in_set[simp]: "xs \<noteq> [] \<Longrightarrow> hd xs \<in> set xs" |
17830 | 1264 |
by(cases xs) auto |
14099 | 1265 |
|
13142 | 1266 |
lemma set_subset_Cons: "set xs \<subseteq> set (x # xs)" |
13145 | 1267 |
by auto |
13114 | 1268 |
|
64963 | 1269 |
lemma set_ConsD: "y \<in> set (x # xs) \<Longrightarrow> y=x \<or> y \<in> set xs" |
14099 | 1270 |
by auto |
1271 |
||
13142 | 1272 |
lemma set_empty [iff]: "(set xs = {}) = (xs = [])" |
13145 | 1273 |
by (induct xs) auto |
13114 | 1274 |
|
74741
6e1fad4f602b
added eq_iff_swap for creating symmetric variants of thms; applied it in List.
nipkow
parents:
74424
diff
changeset
|
1275 |
lemmas set_empty2[iff] = set_empty[THEN eq_iff_swap] |
15245 | 1276 |
|
13142 | 1277 |
lemma set_rev [simp]: "set (rev xs) = set xs" |
13145 | 1278 |
by (induct xs) auto |
13114 | 1279 |
|
13142 | 1280 |
lemma set_map [simp]: "set (map f xs) = f`(set xs)" |
13145 | 1281 |
by (induct xs) auto |
13114 | 1282 |
|
67613 | 1283 |
lemma set_filter [simp]: "set (filter P xs) = {x. x \<in> set xs \<and> P x}" |
13145 | 1284 |
by (induct xs) auto |
13114 | 1285 |
|
32417 | 1286 |
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
|
1287 |
by (induct j) auto |
13114 | 1288 |
|
13142 | 1289 |
|
67613 | 1290 |
lemma split_list: "x \<in> set xs \<Longrightarrow> \<exists>ys zs. xs = ys @ x # zs" |
18049 | 1291 |
proof (induct xs) |
26073 | 1292 |
case Nil thus ?case by simp |
1293 |
next |
|
1294 |
case Cons thus ?case by (auto intro: Cons_eq_appendI) |
|
1295 |
qed |
|
1296 |
||
26734 | 1297 |
lemma in_set_conv_decomp: "x \<in> set xs \<longleftrightarrow> (\<exists>ys zs. xs = ys @ x # zs)" |
1298 |
by (auto elim: split_list) |
|
26073 | 1299 |
|
67613 | 1300 |
lemma split_list_first: "x \<in> set xs \<Longrightarrow> \<exists>ys zs. xs = ys @ x # zs \<and> x \<notin> set ys" |
26073 | 1301 |
proof (induct xs) |
1302 |
case Nil thus ?case by simp |
|
18049 | 1303 |
next |
1304 |
case (Cons a xs) |
|
1305 |
show ?case |
|
1306 |
proof cases |
|
44890
22f665a2e91c
new fastforce replacing fastsimp - less confusing name
nipkow
parents:
44635
diff
changeset
|
1307 |
assume "x = a" thus ?case using Cons by fastforce |
18049 | 1308 |
next |
44890
22f665a2e91c
new fastforce replacing fastsimp - less confusing name
nipkow
parents:
44635
diff
changeset
|
1309 |
assume "x \<noteq> a" thus ?case using Cons by(fastforce intro!: Cons_eq_appendI) |
26073 | 1310 |
qed |
1311 |
qed |
|
1312 |
||
1313 |
lemma in_set_conv_decomp_first: |
|
67613 | 1314 |
"(x \<in> set xs) = (\<exists>ys zs. xs = ys @ x # zs \<and> x \<notin> set ys)" |
26734 | 1315 |
by (auto dest!: split_list_first) |
26073 | 1316 |
|
40122
1d8ad2ff3e01
dropped (almost) redundant distinct.induct rule; distinct_simps again named distinct.simps
haftmann
parents:
40077
diff
changeset
|
1317 |
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
|
1318 |
proof (induct xs rule: rev_induct) |
26073 | 1319 |
case Nil thus ?case by simp |
1320 |
next |
|
1321 |
case (snoc a xs) |
|
1322 |
show ?case |
|
1323 |
proof cases |
|
56085 | 1324 |
assume "x = a" thus ?case using snoc by (auto intro!: exI) |
26073 | 1325 |
next |
44890
22f665a2e91c
new fastforce replacing fastsimp - less confusing name
nipkow
parents:
44635
diff
changeset
|
1326 |
assume "x \<noteq> a" thus ?case using snoc by fastforce |
18049 | 1327 |
qed |
1328 |
qed |
|
1329 |
||
26073 | 1330 |
lemma in_set_conv_decomp_last: |
67613 | 1331 |
"(x \<in> set xs) = (\<exists>ys zs. xs = ys @ x # zs \<and> x \<notin> set zs)" |
26734 | 1332 |
by (auto dest!: split_list_last) |
26073 | 1333 |
|
67091 | 1334 |
lemma split_list_prop: "\<exists>x \<in> set xs. P x \<Longrightarrow> \<exists>ys x zs. xs = ys @ x # zs \<and> P x" |
26073 | 1335 |
proof (induct xs) |
1336 |
case Nil thus ?case by simp |
|
1337 |
next |
|
1338 |
case Cons thus ?case |
|
1339 |
by(simp add:Bex_def)(metis append_Cons append.simps(1)) |
|
1340 |
qed |
|
1341 |
||
1342 |
lemma split_list_propE: |
|
26734 | 1343 |
assumes "\<exists>x \<in> set xs. P x" |
1344 |
obtains ys x zs where "xs = ys @ x # zs" and "P x" |
|
1345 |
using split_list_prop [OF assms] by blast |
|
26073 | 1346 |
|
1347 |
lemma split_list_first_prop: |
|
1348 |
"\<exists>x \<in> set xs. P x \<Longrightarrow> |
|
1349 |
\<exists>ys x zs. xs = ys@x#zs \<and> P x \<and> (\<forall>y \<in> set ys. \<not> P y)" |
|
26734 | 1350 |
proof (induct xs) |
26073 | 1351 |
case Nil thus ?case by simp |
1352 |
next |
|
1353 |
case (Cons x xs) |
|
1354 |
show ?case |
|
1355 |
proof cases |
|
1356 |
assume "P x" |
|
56085 | 1357 |
hence "x # xs = [] @ x # xs \<and> P x \<and> (\<forall>y\<in>set []. \<not> P y)" by simp |
1358 |
thus ?thesis by fast |
|
26073 | 1359 |
next |
1360 |
assume "\<not> P x" |
|
1361 |
hence "\<exists>x\<in>set xs. P x" using Cons(2) by simp |
|
60758 | 1362 |
thus ?thesis using \<open>\<not> P x\<close> Cons(1) by (metis append_Cons set_ConsD) |
26073 | 1363 |
qed |
1364 |
qed |
|
1365 |
||
1366 |
lemma split_list_first_propE: |
|
26734 | 1367 |
assumes "\<exists>x \<in> set xs. P x" |
1368 |
obtains ys x zs where "xs = ys @ x # zs" and "P x" and "\<forall>y \<in> set ys. \<not> P y" |
|
1369 |
using split_list_first_prop [OF assms] by blast |
|
26073 | 1370 |
|
1371 |
lemma split_list_first_prop_iff: |
|
1372 |
"(\<exists>x \<in> set xs. P x) \<longleftrightarrow> |
|
1373 |
(\<exists>ys x zs. xs = ys@x#zs \<and> P x \<and> (\<forall>y \<in> set ys. \<not> P y))" |
|
26734 | 1374 |
by (rule, erule split_list_first_prop) auto |
26073 | 1375 |
|
1376 |
lemma split_list_last_prop: |
|
1377 |
"\<exists>x \<in> set xs. P x \<Longrightarrow> |
|
1378 |
\<exists>ys x zs. xs = ys@x#zs \<and> P x \<and> (\<forall>z \<in> set zs. \<not> P z)" |
|
1379 |
proof(induct xs rule:rev_induct) |
|
1380 |
case Nil thus ?case by simp |
|
1381 |
next |
|
1382 |
case (snoc x xs) |
|
1383 |
show ?case |
|
1384 |
proof cases |
|
56085 | 1385 |
assume "P x" thus ?thesis by (auto intro!: exI) |
26073 | 1386 |
next |
1387 |
assume "\<not> P x" |
|
1388 |
hence "\<exists>x\<in>set xs. P x" using snoc(2) by simp |
|
60758 | 1389 |
thus ?thesis using \<open>\<not> P x\<close> snoc(1) by fastforce |
26073 | 1390 |
qed |
1391 |
qed |
|
1392 |
||
1393 |
lemma split_list_last_propE: |
|
26734 | 1394 |
assumes "\<exists>x \<in> set xs. P x" |
1395 |
obtains ys x zs where "xs = ys @ x # zs" and "P x" and "\<forall>z \<in> set zs. \<not> P z" |
|
1396 |
using split_list_last_prop [OF assms] by blast |
|
26073 | 1397 |
|
1398 |
lemma split_list_last_prop_iff: |
|
1399 |
"(\<exists>x \<in> set xs. P x) \<longleftrightarrow> |
|
1400 |
(\<exists>ys x zs. xs = ys@x#zs \<and> P x \<and> (\<forall>z \<in> set zs. \<not> P z))" |
|
56085 | 1401 |
by rule (erule split_list_last_prop, auto) |
1402 |
||
26073 | 1403 |
|
67091 | 1404 |
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
|
1405 |
by (erule finite_induct) (auto simp add: list.set(2)[symmetric] simp del: list.set(2)) |
13508 | 1406 |
|
14388 | 1407 |
lemma card_length: "card (set xs) \<le> length xs" |
1408 |
by (induct xs) (auto simp add: card_insert_if) |
|
13114 | 1409 |
|
26442
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
haftmann
parents:
26300
diff
changeset
|
1410 |
lemma set_minus_filter_out: |
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
haftmann
parents:
26300
diff
changeset
|
1411 |
"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
|
1412 |
by (induct xs) auto |
15168 | 1413 |
|
66257 | 1414 |
lemma append_Cons_eq_iff: |
1415 |
"\<lbrakk> x \<notin> set xs; x \<notin> set ys \<rbrakk> \<Longrightarrow> |
|
1416 |
xs @ x # ys = xs' @ x # ys' \<longleftrightarrow> (xs = xs' \<and> ys = ys')" |
|
1417 |
by(auto simp: append_eq_Cons_conv Cons_eq_append_conv append_eq_append_conv2) |
|
1418 |
||
35115 | 1419 |
|
73307 | 1420 |
subsubsection \<open>\<^const>\<open>concat\<close>\<close> |
1421 |
||
1422 |
lemma concat_append [simp]: "concat (xs @ ys) = concat xs @ concat ys" |
|
1423 |
by (induct xs) auto |
|
1424 |
||
1425 |
lemma concat_eq_Nil_conv [simp]: "(concat xss = []) = (\<forall>xs \<in> set xss. xs = [])" |
|
1426 |
by (induct xss) auto |
|
1427 |
||
74741
6e1fad4f602b
added eq_iff_swap for creating symmetric variants of thms; applied it in List.
nipkow
parents:
74424
diff
changeset
|
1428 |
lemmas Nil_eq_concat_conv [simp] = concat_eq_Nil_conv[THEN eq_iff_swap] |
73307 | 1429 |
|
1430 |
lemma set_concat [simp]: "set (concat xs) = (\<Union>x\<in>set xs. set x)" |
|
1431 |
by (induct xs) auto |
|
1432 |
||
1433 |
lemma concat_map_singleton[simp]: "concat(map (%x. [f x]) xs) = map f xs" |
|
1434 |
by (induct xs) auto |
|
1435 |
||
1436 |
lemma map_concat: "map f (concat xs) = concat (map (map f) xs)" |
|
1437 |
by (induct xs) auto |
|
1438 |
||
1439 |
lemma rev_concat: "rev (concat xs) = concat (map rev (rev xs))" |
|
1440 |
by (induct xs) auto |
|
1441 |
||
1442 |
lemma length_concat_rev[simp]: "length (concat (rev xs)) = length (concat xs)" |
|
1443 |
by (induction xs) auto |
|
1444 |
||
1445 |
lemma concat_eq_concat_iff: "\<forall>(x, y) \<in> set (zip xs ys). length x = length y \<Longrightarrow> length xs = length ys \<Longrightarrow> (concat xs = concat ys) = (xs = ys)" |
|
1446 |
proof (induct xs arbitrary: ys) |
|
1447 |
case (Cons x xs ys) |
|
1448 |
thus ?case by (cases ys) auto |
|
1449 |
qed (auto) |
|
1450 |
||
1451 |
lemma concat_injective: "concat xs = concat ys \<Longrightarrow> length xs = length ys \<Longrightarrow> \<forall>(x, y) \<in> set (zip xs ys). length x = length y \<Longrightarrow> xs = ys" |
|
1452 |
by (simp add: concat_eq_concat_iff) |
|
1453 |
||
1454 |
lemma concat_eq_appendD: |
|
1455 |
assumes "concat xss = ys @ zs" "xss \<noteq> []" |
|
1456 |
shows "\<exists>xss1 xs xs' xss2. xss = xss1 @ (xs @ xs') # xss2 \<and> ys = concat xss1 @ xs \<and> zs = xs' @ concat xss2" |
|
1457 |
using assms |
|
1458 |
proof(induction xss arbitrary: ys) |
|
1459 |
case (Cons xs xss) |
|
1460 |
from Cons.prems consider |
|
1461 |
us where "xs @ us = ys" "concat xss = us @ zs" | |
|
1462 |
us where "xs = ys @ us" "us @ concat xss = zs" |
|
1463 |
by(auto simp add: append_eq_append_conv2) |
|
1464 |
then show ?case |
|
1465 |
proof cases |
|
1466 |
case 1 |
|
1467 |
then show ?thesis using Cons.IH[OF 1(2)] |
|
1468 |
by(cases xss)(auto intro: exI[where x="[]"], metis append.assoc append_Cons concat.simps(2)) |
|
1469 |
qed(auto intro: exI[where x="[]"]) |
|
1470 |
qed simp |
|
1471 |
||
1472 |
lemma concat_eq_append_conv: |
|
1473 |
"concat xss = ys @ zs \<longleftrightarrow> |
|
1474 |
(if xss = [] then ys = [] \<and> zs = [] |
|
1475 |
else \<exists>xss1 xs xs' xss2. xss = xss1 @ (xs @ xs') # xss2 \<and> ys = concat xss1 @ xs \<and> zs = xs' @ concat xss2)" |
|
1476 |
by(auto dest: concat_eq_appendD) |
|
1477 |
||
1478 |
lemma hd_concat: "\<lbrakk>xs \<noteq> []; hd xs \<noteq> []\<rbrakk> \<Longrightarrow> hd (concat xs) = hd (hd xs)" |
|
1479 |
by (metis concat.simps(2) hd_Cons_tl hd_append2) |
|
1480 |
||
1481 |
||
1482 |
simproc_setup list_neq ("(xs::'a list) = ys") = \<open> |
|
1483 |
(* |
|
1484 |
Reduces xs=ys to False if xs and ys cannot be of the same length. |
|
1485 |
This is the case if the atomic sublists of one are a submultiset |
|
1486 |
of those of the other list and there are fewer Cons's in one than the other. |
|
1487 |
*) |
|
1488 |
||
1489 |
let |
|
1490 |
||
1491 |
fun len (Const(\<^const_name>\<open>Nil\<close>,_)) acc = acc |
|
1492 |
| len (Const(\<^const_name>\<open>Cons\<close>,_) $ _ $ xs) (ts,n) = len xs (ts,n+1) |
|
1493 |
| len (Const(\<^const_name>\<open>append\<close>,_) $ xs $ ys) acc = len xs (len ys acc) |
|
1494 |
| len (Const(\<^const_name>\<open>rev\<close>,_) $ xs) acc = len xs acc |
|
1495 |
| len (Const(\<^const_name>\<open>map\<close>,_) $ _ $ xs) acc = len xs acc |
|
1496 |
| len (Const(\<^const_name>\<open>concat\<close>,T) $ (Const(\<^const_name>\<open>rev\<close>,_) $ xss)) acc |
|
74424 | 1497 |
= len (Const(\<^const_name>\<open>concat\<close>,T) $ xss) acc |
73307 | 1498 |
| len t (ts,n) = (t::ts,n); |
1499 |
||
1500 |
val ss = simpset_of \<^context>; |
|
1501 |
||
1502 |
fun list_neq ctxt ct = |
|
1503 |
let |
|
1504 |
val (Const(_,eqT) $ lhs $ rhs) = Thm.term_of ct; |
|
1505 |
val (ls,m) = len lhs ([],0) and (rs,n) = len rhs ([],0); |
|
1506 |
fun prove_neq() = |
|
1507 |
let |
|
1508 |
val Type(_,listT::_) = eqT; |
|
1509 |
val size = HOLogic.size_const listT; |
|
1510 |
val eq_len = HOLogic.mk_eq (size $ lhs, size $ rhs); |
|
1511 |
val neq_len = HOLogic.mk_Trueprop (HOLogic.Not $ eq_len); |
|
1512 |
val thm = Goal.prove ctxt [] [] neq_len |
|
1513 |
(K (simp_tac (put_simpset ss ctxt) 1)); |
|
1514 |
in SOME (thm RS @{thm neq_if_length_neq}) end |
|
1515 |
in |
|
1516 |
if m < n andalso submultiset (op aconv) (ls,rs) orelse |
|
1517 |
n < m andalso submultiset (op aconv) (rs,ls) |
|
1518 |
then prove_neq() else NONE |
|
1519 |
end; |
|
1520 |
in K list_neq end |
|
1521 |
\<close> |
|
1522 |
||
69593 | 1523 |
subsubsection \<open>\<^const>\<open>filter\<close>\<close> |
13114 | 1524 |
|
13142 | 1525 |
lemma filter_append [simp]: "filter P (xs @ ys) = filter P xs @ filter P ys" |
13145 | 1526 |
by (induct xs) auto |
13114 | 1527 |
|
15305 | 1528 |
lemma rev_filter: "rev (filter P xs) = filter P (rev xs)" |
1529 |
by (induct xs) simp_all |
|
1530 |
||
13142 | 1531 |
lemma filter_filter [simp]: "filter P (filter Q xs) = filter (\<lambda>x. Q x \<and> P x) xs" |
13145 | 1532 |
by (induct xs) auto |
13114 | 1533 |
|
73307 | 1534 |
lemma filter_concat: "filter p (concat xs) = concat (map (filter p) xs)" |
1535 |
by (induct xs) auto |
|
1536 |
||
16998 | 1537 |
lemma length_filter_le [simp]: "length (filter P xs) \<le> length xs" |
1538 |
by (induct xs) (auto simp add: le_SucI) |
|
1539 |
||
18423 | 1540 |
lemma sum_length_filter_compl: |
67091 | 1541 |
"length(filter P xs) + length(filter (\<lambda>x. \<not>P x) xs) = length xs" |
18423 | 1542 |
by(induct xs) simp_all |
1543 |
||
71585 | 1544 |
lemma filter_True [simp]: "\<forall>x \<in> set xs. P x \<Longrightarrow> filter P xs = xs" |
13145 | 1545 |
by (induct xs) auto |
13114 | 1546 |
|
71585 | 1547 |
lemma filter_False [simp]: "\<forall>x \<in> set xs. \<not> P x \<Longrightarrow> filter P xs = []" |
13145 | 1548 |
by (induct xs) auto |
13114 | 1549 |
|
64963 | 1550 |
lemma filter_empty_conv: "(filter P xs = []) = (\<forall>x\<in>set xs. \<not> P x)" |
24349 | 1551 |
by (induct xs) simp_all |
16998 | 1552 |
|
74744 | 1553 |
lemmas empty_filter_conv = filter_empty_conv[THEN eq_iff_swap] |
1554 |
||
16998 | 1555 |
lemma filter_id_conv: "(filter P xs = xs) = (\<forall>x\<in>set xs. P x)" |
68709 | 1556 |
proof (induct xs) |
1557 |
case (Cons x xs) |
|
1558 |
then show ?case |
|
1559 |
using length_filter_le |
|
1560 |
by (simp add: impossible_Cons) |
|
1561 |
qed auto |
|
13114 | 1562 |
|
67091 | 1563 |
lemma filter_map: "filter P (map f xs) = map f (filter (P \<circ> f) xs)" |
16965 | 1564 |
by (induct xs) simp_all |
1565 |
||
1566 |
lemma length_filter_map[simp]: |
|
67091 | 1567 |
"length (filter P (map f xs)) = length(filter (P \<circ> f) xs)" |
16965 | 1568 |
by (simp add:filter_map) |
1569 |
||
13142 | 1570 |
lemma filter_is_subset [simp]: "set (filter P xs) \<le> set xs" |
13145 | 1571 |
by auto |
13114 | 1572 |
|
15246 | 1573 |
lemma length_filter_less: |
67091 | 1574 |
"\<lbrakk> x \<in> set xs; \<not> P x \<rbrakk> \<Longrightarrow> length(filter P xs) < length xs" |
15246 | 1575 |
proof (induct xs) |
1576 |
case Nil thus ?case by simp |
|
1577 |
next |
|
1578 |
case (Cons x xs) thus ?case |
|
68719 | 1579 |
using Suc_le_eq by fastforce |
15246 | 1580 |
qed |
13114 | 1581 |
|
15281 | 1582 |
lemma length_filter_conv_card: |
67091 | 1583 |
"length(filter p xs) = card{i. i < length xs \<and> p(xs!i)}" |
15281 | 1584 |
proof (induct xs) |
1585 |
case Nil thus ?case by simp |
|
1586 |
next |
|
1587 |
case (Cons x xs) |
|
67091 | 1588 |
let ?S = "{i. i < length xs \<and> p(xs!i)}" |
15281 | 1589 |
have fin: "finite ?S" by(fast intro: bounded_nat_set_is_finite) |
1590 |
show ?case (is "?l = card ?S'") |
|
1591 |
proof (cases) |
|
1592 |
assume "p x" |
|
1593 |
hence eq: "?S' = insert 0 (Suc ` ?S)" |
|
25162 | 1594 |
by(auto simp: image_def split:nat.split dest:gr0_implies_Suc) |
15281 | 1595 |
have "length (filter p (x # xs)) = Suc(card ?S)" |
60758 | 1596 |
using Cons \<open>p x\<close> by simp |
15281 | 1597 |
also have "\<dots> = Suc(card(Suc ` ?S))" using fin |
44921 | 1598 |
by (simp add: card_image) |
15281 | 1599 |
also have "\<dots> = card ?S'" using eq fin |
71393
fce780f9c9c6
new examples of BNF lifting across quotients using a new theory of confluence,
traytel
parents:
70911
diff
changeset
|
1600 |
by (simp add:card_insert_if) |
15281 | 1601 |
finally show ?thesis . |
1602 |
next |
|
1603 |
assume "\<not> p x" |
|
1604 |
hence eq: "?S' = Suc ` ?S" |
|
25162 | 1605 |
by(auto simp add: image_def split:nat.split elim:lessE) |
15281 | 1606 |
have "length (filter p (x # xs)) = card ?S" |
60758 | 1607 |
using Cons \<open>\<not> p x\<close> by simp |
15281 | 1608 |
also have "\<dots> = card(Suc ` ?S)" using fin |
44921 | 1609 |
by (simp add: card_image) |
15281 | 1610 |
also have "\<dots> = card ?S'" using eq fin |
1611 |
by (simp add:card_insert_if) |
|
1612 |
finally show ?thesis . |
|
1613 |
qed |
|
1614 |
qed |
|
1615 |
||
17629 | 1616 |
lemma Cons_eq_filterD: |
58807 | 1617 |
"x#xs = filter P ys \<Longrightarrow> |
17629 | 1618 |
\<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 | 1619 |
(is "_ \<Longrightarrow> \<exists>us vs. ?P ys us vs") |
17629 | 1620 |
proof(induct ys) |
1621 |
case Nil thus ?case by simp |
|
1622 |
next |
|
1623 |
case (Cons y ys) |
|
1624 |
show ?case (is "\<exists>x. ?Q x") |
|
1625 |
proof cases |
|
1626 |
assume Py: "P y" |
|
1627 |
show ?thesis |
|
1628 |
proof cases |
|
25221
5ded95dda5df
append/member: more light-weight way to declare authentic syntax;
wenzelm
parents:
25215
diff
changeset
|
1629 |
assume "x = y" |
5ded95dda5df
append/member: more light-weight way to declare authentic syntax;
wenzelm
parents:
25215
diff
changeset
|
1630 |
with Py Cons.prems have "?Q []" by simp |
5ded95dda5df
append/member: more light-weight way to declare authentic syntax;
wenzelm
parents:
25215
diff
changeset
|
1631 |
then show ?thesis .. |
17629 | 1632 |
next |
25221
5ded95dda5df
append/member: more light-weight way to declare authentic syntax;
wenzelm
parents:
25215
diff
changeset
|
1633 |
assume "x \<noteq> y" |
5ded95dda5df
append/member: more light-weight way to declare authentic syntax;
wenzelm
parents:
25215
diff
changeset
|
1634 |
with Py Cons.prems show ?thesis by simp |
17629 | 1635 |
qed |
1636 |
next |
|
25221
5ded95dda5df
append/member: more light-weight way to declare authentic syntax;
wenzelm
parents:
25215
diff
changeset
|
1637 |
assume "\<not> P y" |
44890
22f665a2e91c
new fastforce replacing fastsimp - less confusing name
nipkow
parents:
44635
diff
changeset
|
1638 |
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
|
1639 |
then have "?Q (y#us)" by simp |
5ded95dda5df
append/member: more light-weight way to declare authentic syntax;
wenzelm
parents:
25215
diff
changeset
|
1640 |
then show ?thesis .. |
17629 | 1641 |
qed |
1642 |
qed |
|
1643 |
||
1644 |
lemma filter_eq_ConsD: |
|
58807 | 1645 |
"filter P ys = x#xs \<Longrightarrow> |
17629 | 1646 |
\<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 | 1647 |
by(rule Cons_eq_filterD) simp |
17629 | 1648 |
|
1649 |
lemma filter_eq_Cons_iff: |
|
58807 | 1650 |
"(filter P ys = x#xs) = |
17629 | 1651 |
(\<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 | 1652 |
by(auto dest:filter_eq_ConsD) |
17629 | 1653 |
|
74741
6e1fad4f602b
added eq_iff_swap for creating symmetric variants of thms; applied it in List.
nipkow
parents:
74424
diff
changeset
|
1654 |
lemmas Cons_eq_filter_iff = filter_eq_Cons_iff[THEN eq_iff_swap] |
17629 | 1655 |
|
61031
162bd20dae23
more lemmas on sorting and multisets (due to Thomas Sewell)
haftmann
parents:
60758
diff
changeset
|
1656 |
lemma inj_on_filter_key_eq: |
162bd20dae23
more lemmas on sorting and multisets (due to Thomas Sewell)
haftmann
parents:
60758
diff
changeset
|
1657 |
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
|
1658 |
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
|
1659 |
using assms by (induct xs) auto |
162bd20dae23
more lemmas on sorting and multisets (due to Thomas Sewell)
haftmann
parents:
60758
diff
changeset
|
1660 |
|
44013
5cfc1c36ae97
moved recdef package to HOL/Library/Old_Recdef.thy
krauss
parents:
43594
diff
changeset
|
1661 |
lemma filter_cong[fundef_cong]: |
58807 | 1662 |
"xs = ys \<Longrightarrow> (\<And>x. x \<in> set ys \<Longrightarrow> P x = Q x) \<Longrightarrow> filter P xs = filter Q ys" |
68709 | 1663 |
by (induct ys arbitrary: xs) auto |
17501 | 1664 |
|
15281 | 1665 |
|
60758 | 1666 |
subsubsection \<open>List partitioning\<close> |
26442
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
haftmann
parents:
26300
diff
changeset
|
1667 |
|
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
haftmann
parents:
26300
diff
changeset
|
1668 |
primrec partition :: "('a \<Rightarrow> bool) \<Rightarrow>'a list \<Rightarrow> 'a list \<times> 'a list" where |
68719 | 1669 |
"partition P [] = ([], [])" | |
1670 |
"partition P (x # xs) = |
|
50548 | 1671 |
(let (yes, no) = partition P xs |
1672 |
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
|
1673 |
|
58807 | 1674 |
lemma partition_filter1: "fst (partition P xs) = filter P xs" |
68719 | 1675 |
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
|
1676 |
|
67091 | 1677 |
lemma partition_filter2: "snd (partition P xs) = filter (Not \<circ> P) xs" |
68719 | 1678 |
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
|
1679 |
|
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
haftmann
parents:
26300
diff
changeset
|
1680 |
lemma partition_P: |
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
haftmann
parents:
26300
diff
changeset
|
1681 |
assumes "partition P xs = (yes, no)" |
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
haftmann
parents:
26300
diff
changeset
|
1682 |
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
|
1683 |
proof - |
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
haftmann
parents:
26300
diff
changeset
|
1684 |
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
|
1685 |
by simp_all |
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
haftmann
parents:
26300
diff
changeset
|
1686 |
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
|
1687 |
qed |
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
haftmann
parents:
26300
diff
changeset
|
1688 |
|
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
haftmann
parents:
26300
diff
changeset
|
1689 |
lemma partition_set: |
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
haftmann
parents:
26300
diff
changeset
|
1690 |
assumes "partition P xs = (yes, no)" |
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
haftmann
parents:
26300
diff
changeset
|
1691 |
shows "set yes \<union> set no = set xs" |
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
haftmann
parents:
26300
diff
changeset
|
1692 |
proof - |
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
haftmann
parents:
26300
diff
changeset
|
1693 |
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
|
1694 |
by simp_all |
64963 | 1695 |
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
|
1696 |
qed |
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
haftmann
parents:
26300
diff
changeset
|
1697 |
|
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
|
1698 |
lemma partition_filter_conv[simp]: |
67091 | 1699 |
"partition f xs = (filter f xs,filter (Not \<circ> f) xs)" |
68719 | 1700 |
unfolding partition_filter2[symmetric] |
1701 |
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
|
1702 |
|
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
hoelzl
parents:
33593
diff
changeset
|
1703 |
declare partition.simps[simp del] |
26442
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
haftmann
parents:
26300
diff
changeset
|
1704 |
|
35115 | 1705 |
|
69593 | 1706 |
subsubsection \<open>\<^const>\<open>nth\<close>\<close> |
13114 | 1707 |
|
29827 | 1708 |
lemma nth_Cons_0 [simp, code]: "(x # xs)!0 = x" |
68719 | 1709 |
by auto |
13114 | 1710 |
|
29827 | 1711 |
lemma nth_Cons_Suc [simp, code]: "(x # xs)!(Suc n) = xs!n" |
68719 | 1712 |
by auto |
13114 | 1713 |
|
13142 | 1714 |
declare nth.simps [simp del] |
13114 | 1715 |
|
41842 | 1716 |
lemma nth_Cons_pos[simp]: "0 < n \<Longrightarrow> (x#xs) ! n = xs ! (n - 1)" |
68719 | 1717 |
by(auto simp: Nat.gr0_conv_Suc) |
41842 | 1718 |
|
13114 | 1719 |
lemma nth_append: |
24526 | 1720 |
"(xs @ ys)!n = (if n < length xs then xs!n else ys!(n - length xs))" |
68709 | 1721 |
proof (induct xs arbitrary: n) |
1722 |
case (Cons x xs) |
|
1723 |
then show ?case |
|
1724 |
using less_Suc_eq_0_disj by auto |
|
1725 |
qed simp |
|
13114 | 1726 |
|
14402
4201e1916482
moved lemmas from MicroJava/Comp/AuxLemmas.thy to List.thy
nipkow
parents:
14395
diff
changeset
|
1727 |
lemma nth_append_length [simp]: "(xs @ x # ys) ! length xs = x" |
68719 | 1728 |
by (induct xs) auto |
14402
4201e1916482
moved lemmas from MicroJava/Comp/AuxLemmas.thy to List.thy
nipkow
parents:
14395
diff
changeset
|
1729 |
|
4201e1916482
moved lemmas from MicroJava/Comp/AuxLemmas.thy to List.thy
nipkow
parents:
14395
diff
changeset
|
1730 |
lemma nth_append_length_plus[simp]: "(xs @ ys) ! (length xs + n) = ys ! n" |
68719 | 1731 |
by (induct xs) auto |
14402
4201e1916482
moved lemmas from MicroJava/Comp/AuxLemmas.thy to List.thy
nipkow
parents:
14395
diff
changeset
|
1732 |
|
71585 | 1733 |
lemma nth_map [simp]: "n < length xs \<Longrightarrow> (map f xs)!n = f(xs!n)" |
68709 | 1734 |
proof (induct xs arbitrary: n) |
1735 |
case (Cons x xs) |
|
1736 |
then show ?case |
|
1737 |
using less_Suc_eq_0_disj by auto |
|
1738 |
qed simp |
|
13114 | 1739 |
|
66847 | 1740 |
lemma nth_tl: "n < length (tl xs) \<Longrightarrow> tl xs ! n = xs ! Suc n" |
68719 | 1741 |
by (induction xs) auto |
45841 | 1742 |
|
18423 | 1743 |
lemma hd_conv_nth: "xs \<noteq> [] \<Longrightarrow> hd xs = xs!0" |
68719 | 1744 |
by(cases xs) simp_all |
18423 | 1745 |
|
18049 | 1746 |
lemma list_eq_iff_nth_eq: |
67717 | 1747 |
"(xs = ys) = (length xs = length ys \<and> (\<forall>i<length xs. xs!i = ys!i))" |
68709 | 1748 |
proof (induct xs arbitrary: ys) |
1749 |
case (Cons x xs ys) |
|
71393
fce780f9c9c6
new examples of BNF lifting across quotients using a new theory of confluence,
traytel
parents:
70911
diff
changeset
|
1750 |
show ?case |
68709 | 1751 |
proof (cases ys) |
1752 |
case (Cons y ys) |
|
76376
934d4aed8497
A couple of new theorems. Also additional coercions to the complex numbers
paulson <lp15@cam.ac.uk>
parents:
76294
diff
changeset
|
1753 |
with Cons.hyps show ?thesis by fastforce |
68709 | 1754 |
qed simp |
1755 |
qed force |
|
18049 | 1756 |
|
76376
934d4aed8497
A couple of new theorems. Also additional coercions to the complex numbers
paulson <lp15@cam.ac.uk>
parents:
76294
diff
changeset
|
1757 |
lemma map_equality_iff: |
934d4aed8497
A couple of new theorems. Also additional coercions to the complex numbers
paulson <lp15@cam.ac.uk>
parents:
76294
diff
changeset
|
1758 |
"map f xs = map g ys \<longleftrightarrow> length xs = length ys \<and> (\<forall>i<length ys. f (xs!i) = g (ys!i))" |
934d4aed8497
A couple of new theorems. Also additional coercions to the complex numbers
paulson <lp15@cam.ac.uk>
parents:
76294
diff
changeset
|
1759 |
by (fastforce simp: list_eq_iff_nth_eq) |
934d4aed8497
A couple of new theorems. Also additional coercions to the complex numbers
paulson <lp15@cam.ac.uk>
parents:
76294
diff
changeset
|
1760 |
|
13142 | 1761 |
lemma set_conv_nth: "set xs = {xs!i | i. i < length xs}" |
68709 | 1762 |
proof (induct xs) |
1763 |
case (Cons x xs) |
|
1764 |
have "insert x {xs ! i |i. i < length xs} = {(x # xs) ! i |i. i < Suc (length xs)}" (is "?L=?R") |
|
1765 |
proof |
|
1766 |
show "?L \<subseteq> ?R" |
|
1767 |
by force |
|
1768 |
show "?R \<subseteq> ?L" |
|
1769 |
using less_Suc_eq_0_disj by auto |
|
1770 |
qed |
|
1771 |
with Cons show ?case |
|
1772 |
by simp |
|
1773 |
qed simp |
|
13114 | 1774 |
|
17501 | 1775 |
lemma in_set_conv_nth: "(x \<in> set xs) = (\<exists>i < length xs. xs!i = x)" |
68719 | 1776 |
by(auto simp:set_conv_nth) |
17501 | 1777 |
|
51160
599ff65b85e2
systematic conversions between nat and nibble/char;
haftmann
parents:
51112
diff
changeset
|
1778 |
lemma nth_equal_first_eq: |
599ff65b85e2
systematic conversions between nat and nibble/char;
haftmann
parents:
51112
diff
changeset
|
1779 |
assumes "x \<notin> set xs" |
599ff65b85e2
systematic conversions between nat and nibble/char;
haftmann
parents:
51112
diff
changeset
|
1780 |
assumes "n \<le> length xs" |
599ff65b85e2
systematic conversions between nat and nibble/char;
haftmann
parents:
51112
diff
changeset
|
1781 |
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
|
1782 |
proof |
599ff65b85e2
systematic conversions between nat and nibble/char;
haftmann
parents:
51112
diff
changeset
|
1783 |
assume ?lhs |
599ff65b85e2
systematic conversions between nat and nibble/char;
haftmann
parents:
51112
diff
changeset
|
1784 |
show ?rhs |
599ff65b85e2
systematic conversions between nat and nibble/char;
haftmann
parents:
51112
diff
changeset
|
1785 |
proof (rule ccontr) |
599ff65b85e2
systematic conversions between nat and nibble/char;
haftmann
parents:
51112
diff
changeset
|
1786 |
assume "n \<noteq> 0" |
599ff65b85e2
systematic conversions between nat and nibble/char;
haftmann
parents:
51112
diff
changeset
|
1787 |
then have "n > 0" by simp |
60758 | 1788 |
with \<open>?lhs\<close> have "xs ! (n - 1) = x" by simp |
1789 |
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
|
1790 |
ultimately have "\<exists>i<length xs. xs ! i = x" by auto |
60758 | 1791 |
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
|
1792 |
qed |
599ff65b85e2
systematic conversions between nat and nibble/char;
haftmann
parents:
51112
diff
changeset
|
1793 |
next |
599ff65b85e2
systematic conversions between nat and nibble/char;
haftmann
parents:
51112
diff
changeset
|
1794 |
assume ?rhs then show ?lhs by simp |
599ff65b85e2
systematic conversions between nat and nibble/char;
haftmann
parents:
51112
diff
changeset
|
1795 |
qed |
599ff65b85e2
systematic conversions between nat and nibble/char;
haftmann
parents:
51112
diff
changeset
|
1796 |
|
599ff65b85e2
systematic conversions between nat and nibble/char;
haftmann
parents:
51112
diff
changeset
|
1797 |
lemma nth_non_equal_first_eq: |
599ff65b85e2
systematic conversions between nat and nibble/char;
haftmann
parents:
51112
diff
changeset
|
1798 |
assumes "x \<noteq> y" |
599ff65b85e2
systematic conversions between nat and nibble/char;
haftmann
parents:
51112
diff
changeset
|
1799 |
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
|
1800 |
proof |
599ff65b85e2
systematic conversions between nat and nibble/char;
haftmann
parents:
51112
diff
changeset
|
1801 |
assume "?lhs" with assms have "n > 0" by (cases n) simp_all |
60758 | 1802 |
with \<open>?lhs\<close> show ?rhs by simp |
51160
599ff65b85e2
systematic conversions between nat and nibble/char;
haftmann
parents:
51112
diff
changeset
|
1803 |
next |
599ff65b85e2
systematic conversions between nat and nibble/char;
haftmann
parents:
51112
diff
changeset
|
1804 |
assume "?rhs" then show "?lhs" by simp |
599ff65b85e2
systematic conversions between nat and nibble/char;
haftmann
parents:
51112
diff
changeset
|
1805 |
qed |
599ff65b85e2
systematic conversions between nat and nibble/char;
haftmann
parents:
51112
diff
changeset
|
1806 |
|
67613 | 1807 |
lemma list_ball_nth: "\<lbrakk>n < length xs; \<forall>x \<in> set xs. P x\<rbrakk> \<Longrightarrow> P(xs!n)" |
68719 | 1808 |
by (auto simp add: set_conv_nth) |
13114 | 1809 |
|
67613 | 1810 |
lemma nth_mem [simp]: "n < length xs \<Longrightarrow> xs!n \<in> set xs" |
68719 | 1811 |
by (auto simp add: set_conv_nth) |
13114 | 1812 |
|
1813 |
lemma all_nth_imp_all_set: |
|
67717 | 1814 |
"\<lbrakk>\<forall>i < length xs. P(xs!i); x \<in> set xs\<rbrakk> \<Longrightarrow> P x" |
68719 | 1815 |
by (auto simp add: set_conv_nth) |
13114 | 1816 |
|
1817 |
lemma all_set_conv_all_nth: |
|
67091 | 1818 |
"(\<forall>x \<in> set xs. P x) = (\<forall>i. i < length xs \<longrightarrow> P (xs ! i))" |
68719 | 1819 |
by (auto simp add: set_conv_nth) |
13114 | 1820 |
|
25296 | 1821 |
lemma rev_nth: |
1822 |
"n < size xs \<Longrightarrow> rev xs ! n = xs ! (length xs - Suc n)" |
|
1823 |
proof (induct xs arbitrary: n) |
|
1824 |
case Nil thus ?case by simp |
|
1825 |
next |
|
1826 |
case (Cons x xs) |
|
1827 |
hence n: "n < Suc (length xs)" by simp |
|
1828 |
moreover |
|
1829 |
{ assume "n < length xs" |
|
53374
a14d2a854c02
tuned proofs -- clarified flow of facts wrt. calculation;
wenzelm
parents:
53017
diff
changeset
|
1830 |
with n obtain n' where n': "length xs - n = Suc n'" |
25296 | 1831 |
by (cases "length xs - n", auto) |
1832 |
moreover |
|
53374
a14d2a854c02
tuned proofs -- clarified flow of facts wrt. calculation;
wenzelm
parents:
53017
diff
changeset
|
1833 |
from n' have "length xs - Suc n = n'" by simp |
25296 | 1834 |
ultimately |
1835 |
have "xs ! (length xs - Suc n) = (x # xs) ! (length xs - n)" by simp |
|
1836 |
} |
|
1837 |
ultimately |
|
1838 |
show ?case by (clarsimp simp add: Cons nth_append) |
|
1839 |
qed |
|
13114 | 1840 |
|
31159 | 1841 |
lemma Skolem_list_nth: |
67091 | 1842 |
"(\<forall>i<k. \<exists>x. P i x) = (\<exists>xs. size xs = k \<and> (\<forall>i<k. P i (xs!i)))" |
1843 |
(is "_ = (\<exists>xs. ?P k xs)") |
|
31159 | 1844 |
proof(induct k) |
1845 |
case 0 show ?case by simp |
|
1846 |
next |
|
1847 |
case (Suc k) |
|
67091 | 1848 |
show ?case (is "?L = ?R" is "_ = (\<exists>xs. ?P' xs)") |
31159 | 1849 |
proof |
1850 |
assume "?R" thus "?L" using Suc by auto |
|
1851 |
next |
|
1852 |
assume "?L" |
|
67091 | 1853 |
with Suc obtain x xs where "?P k xs \<and> P k x" by (metis less_Suc_eq) |
31159 | 1854 |
hence "?P'(xs@[x])" by(simp add:nth_append less_Suc_eq) |
1855 |
thus "?R" .. |
|
1856 |
qed |
|
1857 |
qed |
|
1858 |
||
1859 |
||
69593 | 1860 |
subsubsection \<open>\<^const>\<open>list_update\<close>\<close> |
13114 | 1861 |
|
24526 | 1862 |
lemma length_list_update [simp]: "length(xs[i:=x]) = length xs" |
68719 | 1863 |
by (induct xs arbitrary: i) (auto split: nat.split) |
13114 | 1864 |
|
1865 |
lemma nth_list_update: |
|
71585 | 1866 |
"i < length xs\<Longrightarrow> (xs[i:=x])!j = (if i = j then x else xs!j)" |
68719 | 1867 |
by (induct xs arbitrary: i j) (auto simp add: nth_Cons split: nat.split) |
13114 | 1868 |
|
71585 | 1869 |
lemma nth_list_update_eq [simp]: "i < length xs \<Longrightarrow> (xs[i:=x])!i = x" |
68719 | 1870 |
by (simp add: nth_list_update) |
13114 | 1871 |
|
71585 | 1872 |
lemma nth_list_update_neq [simp]: "i \<noteq> j \<Longrightarrow> xs[i:=x]!j = xs!j" |
68719 | 1873 |
by (induct xs arbitrary: i j) (auto simp add: nth_Cons split: nat.split) |
13114 | 1874 |
|
24526 | 1875 |
lemma list_update_id[simp]: "xs[i := xs!i] = xs" |
68719 | 1876 |
by (induct xs arbitrary: i) (simp_all split:nat.splits) |
24526 | 1877 |
|
1878 |
lemma list_update_beyond[simp]: "length xs \<le> i \<Longrightarrow> xs[i:=x] = xs" |
|
68709 | 1879 |
proof (induct xs arbitrary: i) |
1880 |
case (Cons x xs i) |
|
1881 |
then show ?case |
|
1882 |
by (metis leD length_list_update list_eq_iff_nth_eq nth_list_update_neq) |
|
1883 |
qed simp |
|
17501 | 1884 |
|
31077 | 1885 |
lemma list_update_nonempty[simp]: "xs[k:=x] = [] \<longleftrightarrow> xs=[]" |
68719 | 1886 |
by (simp only: length_0_conv[symmetric] length_list_update) |
31077 | 1887 |
|
13114 | 1888 |
lemma list_update_same_conv: |
71585 | 1889 |
"i < length xs \<Longrightarrow> (xs[i := x] = xs) = (xs!i = x)" |
68719 | 1890 |
by (induct xs arbitrary: i) (auto split: nat.split) |
13114 | 1891 |
|
14187 | 1892 |
lemma list_update_append1: |
58807 | 1893 |
"i < size xs \<Longrightarrow> (xs @ ys)[i:=x] = xs[i:=x] @ ys" |
68719 | 1894 |
by (induct xs arbitrary: i)(auto split:nat.split) |
14187 | 1895 |
|
15868 | 1896 |
lemma list_update_append: |
64963 | 1897 |
"(xs @ ys) [n:= x] = |
15868 | 1898 |
(if n < length xs then xs[n:= x] @ ys else xs @ (ys [n-length xs:= x]))" |
68719 | 1899 |
by (induct xs arbitrary: n) (auto split:nat.splits) |
15868 | 1900 |
|
14402
4201e1916482
moved lemmas from MicroJava/Comp/AuxLemmas.thy to List.thy
nipkow
parents:
14395
diff
changeset
|
1901 |
lemma list_update_length [simp]: |
58807 | 1902 |
"(xs @ x # ys)[length xs := y] = (xs @ y # ys)" |
68719 | 1903 |
by (induct xs, auto) |
14402
4201e1916482
moved lemmas from MicroJava/Comp/AuxLemmas.thy to List.thy
nipkow
parents:
14395
diff
changeset
|
1904 |
|
31264 | 1905 |
lemma map_update: "map f (xs[k:= y]) = (map f xs)[k := f y]" |
68719 | 1906 |
by(induct xs arbitrary: k)(auto split:nat.splits) |
31264 | 1907 |
|
1908 |
lemma rev_update: |
|
1909 |
"k < length xs \<Longrightarrow> rev (xs[k:= y]) = (rev xs)[length xs - k - 1 := y]" |
|
68719 | 1910 |
by (induct xs arbitrary: k) (auto simp: list_update_append split:nat.splits) |
31264 | 1911 |
|
13114 | 1912 |
lemma update_zip: |
31080 | 1913 |
"(zip xs ys)[i:=xy] = zip (xs[i:=fst xy]) (ys[i:=snd xy])" |
68719 | 1914 |
by (induct ys arbitrary: i xy xs) (auto, case_tac xs, auto split: nat.split) |
24526 | 1915 |
|
68709 | 1916 |
lemma set_update_subset_insert: "set(xs[i:=x]) \<le> insert x (set xs)" |
68719 | 1917 |
by (induct xs arbitrary: i) (auto split: nat.split) |
13114 | 1918 |
|
67613 | 1919 |
lemma set_update_subsetI: "\<lbrakk>set xs \<subseteq> A; x \<in> A\<rbrakk> \<Longrightarrow> set(xs[i := x]) \<subseteq> A" |
68719 | 1920 |
by (blast dest!: set_update_subset_insert [THEN subsetD]) |
13114 | 1921 |
|
24526 | 1922 |
lemma set_update_memI: "n < length xs \<Longrightarrow> x \<in> set (xs[n := x])" |
68719 | 1923 |
by (induct xs arbitrary: n) (auto split:nat.splits) |
15868 | 1924 |
|
31077 | 1925 |
lemma list_update_overwrite[simp]: |
24796 | 1926 |
"xs [i := x, i := y] = xs [i := y]" |
68709 | 1927 |
by (induct xs arbitrary: i) (simp_all split: nat.split) |
24796 | 1928 |
|
1929 |
lemma list_update_swap: |
|
1930 |
"i \<noteq> i' \<Longrightarrow> xs [i := x, i' := x'] = xs [i' := x', i := x]" |
|
68709 | 1931 |
by (induct xs arbitrary: i i') (simp_all split: nat.split) |
24796 | 1932 |
|
29827 | 1933 |
lemma list_update_code [code]: |
1934 |
"[][i := y] = []" |
|
1935 |
"(x # xs)[0 := y] = y # xs" |
|
1936 |
"(x # xs)[Suc i := y] = x # xs[i := y]" |
|
68719 | 1937 |
by simp_all |
29827 | 1938 |
|
13114 | 1939 |
|
69593 | 1940 |
subsubsection \<open>\<^const>\<open>last\<close> and \<^const>\<open>butlast\<close>\<close> |
13114 | 1941 |
|
73510
c526eb2c7ca0
removal of needless hypothesis in hd_rev and last_rev
paulson <lp15@cam.ac.uk>
parents:
73443
diff
changeset
|
1942 |
lemma hd_Nil_eq_last: "hd Nil = last Nil" |
c526eb2c7ca0
removal of needless hypothesis in hd_rev and last_rev
paulson <lp15@cam.ac.uk>
parents:
73443
diff
changeset
|
1943 |
unfolding hd_def last_def by simp |
c526eb2c7ca0
removal of needless hypothesis in hd_rev and last_rev
paulson <lp15@cam.ac.uk>
parents:
73443
diff
changeset
|
1944 |
|
13142 | 1945 |
lemma last_snoc [simp]: "last (xs @ [x]) = x" |
68719 | 1946 |
by (induct xs) auto |
13114 | 1947 |
|
13142 | 1948 |
lemma butlast_snoc [simp]: "butlast (xs @ [x]) = xs" |
68719 | 1949 |
by (induct xs) auto |
13114 | 1950 |
|
14302 | 1951 |
lemma last_ConsL: "xs = [] \<Longrightarrow> last(x#xs) = x" |
68719 | 1952 |
by simp |
14302 | 1953 |
|
1954 |
lemma last_ConsR: "xs \<noteq> [] \<Longrightarrow> last(x#xs) = last xs" |
|
68719 | 1955 |
by simp |
14302 | 1956 |
|
1957 |
lemma last_append: "last(xs @ ys) = (if ys = [] then last xs else last ys)" |
|
68719 | 1958 |
by (induct xs) (auto) |
14302 | 1959 |
|
1960 |
lemma last_appendL[simp]: "ys = [] \<Longrightarrow> last(xs @ ys) = last xs" |
|
68719 | 1961 |
by(simp add:last_append) |
14302 | 1962 |
|
1963 |
lemma last_appendR[simp]: "ys \<noteq> [] \<Longrightarrow> last(xs @ ys) = last ys" |
|
68719 | 1964 |
by(simp add:last_append) |
14302 | 1965 |
|
45841 | 1966 |
lemma last_tl: "xs = [] \<or> tl xs \<noteq> [] \<Longrightarrow>last (tl xs) = last xs" |
68719 | 1967 |
by (induct xs) simp_all |
45841 | 1968 |
|
1969 |
lemma butlast_tl: "butlast (tl xs) = tl (butlast xs)" |
|
68719 | 1970 |
by (induct xs) simp_all |
45841 | 1971 |
|
73510
c526eb2c7ca0
removal of needless hypothesis in hd_rev and last_rev
paulson <lp15@cam.ac.uk>
parents:
73443
diff
changeset
|
1972 |
lemma hd_rev: "hd(rev xs) = last xs" |
c526eb2c7ca0
removal of needless hypothesis in hd_rev and last_rev
paulson <lp15@cam.ac.uk>
parents:
73443
diff
changeset
|
1973 |
by (metis hd_Cons_tl hd_Nil_eq_last last_snoc rev_eq_Cons_iff rev_is_Nil_conv) |
c526eb2c7ca0
removal of needless hypothesis in hd_rev and last_rev
paulson <lp15@cam.ac.uk>
parents:
73443
diff
changeset
|
1974 |
|
c526eb2c7ca0
removal of needless hypothesis in hd_rev and last_rev
paulson <lp15@cam.ac.uk>
parents:
73443
diff
changeset
|
1975 |
lemma last_rev: "last(rev xs) = hd xs" |
c526eb2c7ca0
removal of needless hypothesis in hd_rev and last_rev
paulson <lp15@cam.ac.uk>
parents:
73443
diff
changeset
|
1976 |
by (metis hd_rev rev_swap) |
17762 | 1977 |
|
17765 | 1978 |
lemma last_in_set[simp]: "as \<noteq> [] \<Longrightarrow> last as \<in> set as" |
68719 | 1979 |
by (induct as) auto |
17762 | 1980 |
|
13142 | 1981 |
lemma length_butlast [simp]: "length (butlast xs) = length xs - 1" |
68719 | 1982 |
by (induct xs rule: rev_induct) auto |
13114 | 1983 |
|
1984 |
lemma butlast_append: |
|
24526 | 1985 |
"butlast (xs @ ys) = (if ys = [] then butlast xs else xs @ butlast ys)" |
68719 | 1986 |
by (induct xs arbitrary: ys) auto |
13114 | 1987 |
|
13142 | 1988 |
lemma append_butlast_last_id [simp]: |
67613 | 1989 |
"xs \<noteq> [] \<Longrightarrow> butlast xs @ [last xs] = xs" |
68719 | 1990 |
by (induct xs) auto |
13114 | 1991 |
|
67613 | 1992 |
lemma in_set_butlastD: "x \<in> set (butlast xs) \<Longrightarrow> x \<in> set xs" |
68719 | 1993 |
by (induct xs) (auto split: if_split_asm) |
13114 | 1994 |
|
1995 |
lemma in_set_butlast_appendI: |
|
67091 | 1996 |
"x \<in> set (butlast xs) \<or> x \<in> set (butlast ys) \<Longrightarrow> x \<in> set (butlast (xs @ ys))" |
68719 | 1997 |
by (auto dest: in_set_butlastD simp add: butlast_append) |
13114 | 1998 |
|
24526 | 1999 |
lemma last_drop[simp]: "n < length xs \<Longrightarrow> last (drop n xs) = last xs" |
68719 | 2000 |
by (induct xs arbitrary: n)(auto split:nat.split) |
17501 | 2001 |
|
45841 | 2002 |
lemma nth_butlast: |
2003 |
assumes "n < length (butlast xs)" shows "butlast xs ! n = xs ! n" |
|
2004 |
proof (cases xs) |
|
2005 |
case (Cons y ys) |
|
2006 |
moreover from assms have "butlast xs ! n = (butlast xs @ [last xs]) ! n" |
|
2007 |
by (simp add: nth_append) |
|
2008 |
ultimately show ?thesis using append_butlast_last_id by simp |
|
2009 |
qed simp |
|
2010 |
||
30128
365ee7319b86
revert some Suc 0 lemmas back to their original forms; added some simp rules for (1::nat)
huffman
parents:
30079
diff
changeset
|
2011 |
lemma last_conv_nth: "xs\<noteq>[] \<Longrightarrow> last xs = xs!(length xs - 1)" |
68719 | 2012 |
by(induct xs)(auto simp:neq_Nil_conv) |
17589 | 2013 |
|
30128
365ee7319b86
revert some Suc 0 lemmas back to their original forms; added some simp rules for (1::nat)
huffman
parents:
30079
diff
changeset
|
2014 |
lemma butlast_conv_take: "butlast xs = take (length xs - 1) xs" |
68719 | 2015 |
by (induction xs rule: induct_list012) simp_all |
26584
46f3b89b2445
move lemmas from Word/BinBoolList.thy to List.thy
huffman
parents:
26480
diff
changeset
|
2016 |
|
31077 | 2017 |
lemma last_list_update: |
2018 |
"xs \<noteq> [] \<Longrightarrow> last(xs[k:=x]) = (if k = size xs - 1 then x else last xs)" |
|
68719 | 2019 |
by (auto simp: last_conv_nth) |
31077 | 2020 |
|
2021 |
lemma butlast_list_update: |
|
2022 |
"butlast(xs[k:=x]) = |
|
58807 | 2023 |
(if k = size xs - 1 then butlast xs else (butlast xs)[k:=x])" |
68719 | 2024 |
by(cases xs rule:rev_cases)(auto simp: list_update_append split: nat.splits) |
58807 | 2025 |
|
2026 |
lemma last_map: "xs \<noteq> [] \<Longrightarrow> last (map f xs) = f (last xs)" |
|
68719 | 2027 |
by (cases xs rule: rev_cases) simp_all |
58807 | 2028 |
|
2029 |
lemma map_butlast: "map f (butlast xs) = butlast (map f xs)" |
|
68719 | 2030 |
by (induct xs) simp_all |
36851 | 2031 |
|
40230 | 2032 |
lemma snoc_eq_iff_butlast: |
67091 | 2033 |
"xs @ [x] = ys \<longleftrightarrow> (ys \<noteq> [] \<and> butlast ys = xs \<and> last ys = x)" |
68719 | 2034 |
by fastforce |
40230 | 2035 |
|
63173 | 2036 |
corollary longest_common_suffix: |
2037 |
"\<exists>ss xs' ys'. xs = xs' @ ss \<and> ys = ys' @ ss |
|
2038 |
\<and> (xs' = [] \<or> ys' = [] \<or> last xs' \<noteq> last ys')" |
|
68719 | 2039 |
using longest_common_prefix[of "rev xs" "rev ys"] |
2040 |
unfolding rev_swap rev_append by (metis last_rev rev_is_Nil_conv) |
|
63173 | 2041 |
|
70183
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
69850
diff
changeset
|
2042 |
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
|
2043 |
by (cases xs) simp_all |
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
69850
diff
changeset
|
2044 |
|
24796 | 2045 |
|
69593 | 2046 |
subsubsection \<open>\<^const>\<open>take\<close> and \<^const>\<open>drop\<close>\<close> |
13114 | 2047 |
|
66658 | 2048 |
lemma take_0: "take 0 xs = []" |
68719 | 2049 |
by (induct xs) auto |
66658 | 2050 |
|
2051 |
lemma drop_0: "drop 0 xs = xs" |
|
68719 | 2052 |
by (induct xs) auto |
13114 | 2053 |
|
66658 | 2054 |
lemma take0[simp]: "take 0 = (\<lambda>xs. [])" |
68719 | 2055 |
by(rule ext) (rule take_0) |
66658 | 2056 |
|
2057 |
lemma drop0[simp]: "drop 0 = (\<lambda>x. x)" |
|
68719 | 2058 |
by(rule ext) (rule drop_0) |
13114 | 2059 |
|
13142 | 2060 |
lemma take_Suc_Cons [simp]: "take (Suc n) (x # xs) = x # take n xs" |
68719 | 2061 |
by simp |
13114 | 2062 |
|
13142 | 2063 |
lemma drop_Suc_Cons [simp]: "drop (Suc n) (x # xs) = drop n xs" |
68719 | 2064 |
by simp |
13114 | 2065 |
|
13142 | 2066 |
declare take_Cons [simp del] and drop_Cons [simp del] |
13114 | 2067 |
|
67091 | 2068 |
lemma take_Suc: "xs \<noteq> [] \<Longrightarrow> take (Suc n) xs = hd xs # take n (tl xs)" |
68719 | 2069 |
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
|
2070 |
|
14187 | 2071 |
lemma drop_Suc: "drop (Suc n) xs = drop n (tl xs)" |
68719 | 2072 |
by(cases xs, simp_all) |
14187 | 2073 |
|
66870 | 2074 |
lemma hd_take[simp]: "j > 0 \<Longrightarrow> hd (take j xs) = hd xs" |
68719 | 2075 |
by (metis gr0_conv_Suc list.sel(1) take.simps(1) take_Suc) |
66657 | 2076 |
|
26584
46f3b89b2445
move lemmas from Word/BinBoolList.thy to List.thy
huffman
parents:
26480
diff
changeset
|
2077 |
lemma take_tl: "take n (tl xs) = tl (take (Suc n) xs)" |
68719 | 2078 |
by (induct xs arbitrary: n) simp_all |
26584
46f3b89b2445
move lemmas from Word/BinBoolList.thy to List.thy
huffman
parents:
26480
diff
changeset
|
2079 |
|
24526 | 2080 |
lemma drop_tl: "drop n (tl xs) = tl(drop n xs)" |
68719 | 2081 |
by(induct xs arbitrary: n, simp_all add:drop_Cons drop_Suc split:nat.split) |
24526 | 2082 |
|
26584
46f3b89b2445
move lemmas from Word/BinBoolList.thy to List.thy
huffman
parents:
26480
diff
changeset
|
2083 |
lemma tl_take: "tl (take n xs) = take (n - 1) (tl xs)" |
68719 | 2084 |
by (cases n, simp, cases xs, auto) |
26584
46f3b89b2445
move lemmas from Word/BinBoolList.thy to List.thy
huffman
parents:
26480
diff
changeset
|
2085 |
|
46f3b89b2445
move lemmas from Word/BinBoolList.thy to List.thy
huffman
parents:
26480
diff
changeset
|
2086 |
lemma tl_drop: "tl (drop n xs) = drop n (tl xs)" |
68719 | 2087 |
by (simp only: drop_tl) |
26584
46f3b89b2445
move lemmas from Word/BinBoolList.thy to List.thy
huffman
parents:
26480
diff
changeset
|
2088 |
|
24526 | 2089 |
lemma nth_via_drop: "drop n xs = y#ys \<Longrightarrow> xs!n = y" |
68719 | 2090 |
by (induct xs arbitrary: n, simp)(auto simp: drop_Cons nth_Cons split: nat.splits) |
14187 | 2091 |
|
13913 | 2092 |
lemma take_Suc_conv_app_nth: |
24526 | 2093 |
"i < length xs \<Longrightarrow> take (Suc i) xs = take i xs @ [xs!i]" |
68709 | 2094 |
proof (induct xs arbitrary: i) |
69850 | 2095 |
case Nil |
2096 |
then show ?case by simp |
|
2097 |
next |
|
2098 |
case Cons |
|
2099 |
then show ?case by (cases i) auto |
|
2100 |
qed |
|
13913 | 2101 |
|
58247
98d0f85d247f
enamed drop_Suc_conv_tl and nth_drop' to Cons_nth_drop_Suc
nipkow
parents:
58195
diff
changeset
|
2102 |
lemma Cons_nth_drop_Suc: |
24526 | 2103 |
"i < length xs \<Longrightarrow> (xs!i) # (drop (Suc i) xs) = drop i xs" |
68709 | 2104 |
proof (induct xs arbitrary: i) |
69850 | 2105 |
case Nil |
2106 |
then show ?case by simp |
|
2107 |
next |
|
2108 |
case Cons |
|
2109 |
then show ?case by (cases i) auto |
|
2110 |
qed |
|
14591 | 2111 |
|
24526 | 2112 |
lemma length_take [simp]: "length (take n xs) = min (length xs) n" |
68719 | 2113 |
by (induct n arbitrary: xs) (auto, case_tac xs, auto) |
24526 | 2114 |
|
2115 |
lemma length_drop [simp]: "length (drop n xs) = (length xs - n)" |
|
68719 | 2116 |
by (induct n arbitrary: xs) (auto, case_tac xs, auto) |
24526 | 2117 |
|
71585 | 2118 |
lemma take_all [simp]: "length xs \<le> n \<Longrightarrow> take n xs = xs" |
68719 | 2119 |
by (induct n arbitrary: xs) (auto, case_tac xs, auto) |
24526 | 2120 |
|
71585 | 2121 |
lemma drop_all [simp]: "length xs \<le> n \<Longrightarrow> drop n xs = []" |
68719 | 2122 |
by (induct n arbitrary: xs) (auto, case_tac xs, auto) |
13114 | 2123 |
|
72852 | 2124 |
lemma take_all_iff [simp]: "take n xs = xs \<longleftrightarrow> length xs \<le> n" |
2125 |
by (metis length_take min.order_iff take_all) |
|
2126 |
||
74742 | 2127 |
(* Looks like a good simp rule but can cause looping; |
2128 |
too much interaction between take and length |
|
74741
6e1fad4f602b
added eq_iff_swap for creating symmetric variants of thms; applied it in List.
nipkow
parents:
74424
diff
changeset
|
2129 |
lemmas take_all_iff2[simp] = take_all_iff[THEN eq_iff_swap] |
74742 | 2130 |
*) |
74741
6e1fad4f602b
added eq_iff_swap for creating symmetric variants of thms; applied it in List.
nipkow
parents:
74424
diff
changeset
|
2131 |
|
6e1fad4f602b
added eq_iff_swap for creating symmetric variants of thms; applied it in List.
nipkow
parents:
74424
diff
changeset
|
2132 |
lemma take_eq_Nil[simp]: "(take n xs = []) = (n = 0 \<or> xs = [])" |
6e1fad4f602b
added eq_iff_swap for creating symmetric variants of thms; applied it in List.
nipkow
parents:
74424
diff
changeset
|
2133 |
by(induct xs arbitrary: n)(auto simp: take_Cons split:nat.split) |
6e1fad4f602b
added eq_iff_swap for creating symmetric variants of thms; applied it in List.
nipkow
parents:
74424
diff
changeset
|
2134 |
|
6e1fad4f602b
added eq_iff_swap for creating symmetric variants of thms; applied it in List.
nipkow
parents:
74424
diff
changeset
|
2135 |
lemmas take_eq_Nil2[simp] = take_eq_Nil[THEN eq_iff_swap] |
6e1fad4f602b
added eq_iff_swap for creating symmetric variants of thms; applied it in List.
nipkow
parents:
74424
diff
changeset
|
2136 |
|
6e1fad4f602b
added eq_iff_swap for creating symmetric variants of thms; applied it in List.
nipkow
parents:
74424
diff
changeset
|
2137 |
lemma drop_eq_Nil [simp]: "drop n xs = [] \<longleftrightarrow> length xs \<le> n" |
72852 | 2138 |
by (metis diff_is_0_eq drop_all length_drop list.size(3)) |
2139 |
||
74741
6e1fad4f602b
added eq_iff_swap for creating symmetric variants of thms; applied it in List.
nipkow
parents:
74424
diff
changeset
|
2140 |
lemmas drop_eq_Nil2 [simp] = drop_eq_Nil[THEN eq_iff_swap] |
6e1fad4f602b
added eq_iff_swap for creating symmetric variants of thms; applied it in List.
nipkow
parents:
74424
diff
changeset
|
2141 |
|
13142 | 2142 |
lemma take_append [simp]: |
24526 | 2143 |
"take n (xs @ ys) = (take n xs @ take (n - length xs) ys)" |
68719 | 2144 |
by (induct n arbitrary: xs) (auto, case_tac xs, auto) |
13114 | 2145 |
|
13142 | 2146 |
lemma drop_append [simp]: |
24526 | 2147 |
"drop n (xs @ ys) = drop n xs @ drop (n - length xs) ys" |
68719 | 2148 |
by (induct n arbitrary: xs) (auto, case_tac xs, auto) |
24526 | 2149 |
|
2150 |
lemma take_take [simp]: "take n (take m xs) = take (min n m) xs" |
|
68709 | 2151 |
proof (induct m arbitrary: xs n) |
69850 | 2152 |
case 0 |
2153 |
then show ?case by simp |
|
2154 |
next |
|
2155 |
case Suc |
|
2156 |
then show ?case by (cases xs; cases n) simp_all |
|
2157 |
qed |
|
13114 | 2158 |
|
24526 | 2159 |
lemma drop_drop [simp]: "drop n (drop m xs) = drop (n + m) xs" |
68709 | 2160 |
proof (induct m arbitrary: xs) |
69850 | 2161 |
case 0 |
2162 |
then show ?case by simp |
|
2163 |
next |
|
2164 |
case Suc |
|
2165 |
then show ?case by (cases xs) simp_all |
|
2166 |
qed |
|
13114 | 2167 |
|
24526 | 2168 |
lemma take_drop: "take n (drop m xs) = drop m (take (n + m) xs)" |
68709 | 2169 |
proof (induct m arbitrary: xs n) |
69850 | 2170 |
case 0 |
2171 |
then show ?case by simp |
|
2172 |
next |
|
2173 |
case Suc |
|
2174 |
then show ?case by (cases xs; cases n) simp_all |
|
2175 |
qed |
|
13114 | 2176 |
|
24526 | 2177 |
lemma drop_take: "drop n (take m xs) = take (m-n) (drop n xs)" |
68719 | 2178 |
by(induct xs arbitrary: m n)(auto simp: take_Cons drop_Cons split: nat.split) |
14802 | 2179 |
|
24526 | 2180 |
lemma append_take_drop_id [simp]: "take n xs @ drop n xs = xs" |
68709 | 2181 |
proof (induct n arbitrary: xs) |
69850 | 2182 |
case 0 |
2183 |
then show ?case by simp |
|
2184 |
next |
|
2185 |
case Suc |
|
2186 |
then show ?case by (cases xs) simp_all |
|
2187 |
qed |
|
13114 | 2188 |
|
24526 | 2189 |
lemma take_map: "take n (map f xs) = map f (take n xs)" |
68709 | 2190 |
proof (induct n arbitrary: xs) |
69850 | 2191 |
case 0 |
2192 |
then show ?case by simp |
|
2193 |
next |
|
2194 |
case Suc |
|
2195 |
then show ?case by (cases xs) simp_all |
|
2196 |
qed |
|
13114 | 2197 |
|
24526 | 2198 |
lemma drop_map: "drop n (map f xs) = map f (drop n xs)" |
68709 | 2199 |
proof (induct n arbitrary: xs) |
69850 | 2200 |
case 0 |
2201 |
then show ?case by simp |
|
2202 |
next |
|
2203 |
case Suc |
|
2204 |
then show ?case by (cases xs) simp_all |
|
2205 |
qed |
|
13114 | 2206 |
|
24526 | 2207 |
lemma rev_take: "rev (take i xs) = drop (length xs - i) (rev xs)" |
68709 | 2208 |
proof (induct xs arbitrary: i) |
69850 | 2209 |
case Nil |
2210 |
then show ?case by simp |
|
2211 |
next |
|
2212 |
case Cons |
|
2213 |
then show ?case by (cases i) auto |
|
2214 |
qed |
|
13114 | 2215 |
|
24526 | 2216 |
lemma rev_drop: "rev (drop i xs) = take (length xs - i) (rev xs)" |
68709 | 2217 |
proof (induct xs arbitrary: i) |
69850 | 2218 |
case Nil |
2219 |
then show ?case by simp |
|
2220 |
next |
|
2221 |
case Cons |
|
2222 |
then show ?case by (cases i) auto |
|
2223 |
qed |
|
13114 | 2224 |
|
61699
a81dc5c4d6a9
New theorems mostly from Peter Gammie
paulson <lp15@cam.ac.uk>
parents:
61682
diff
changeset
|
2225 |
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
|
2226 |
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
|
2227 |
|
a81dc5c4d6a9
New theorems mostly from Peter Gammie
paulson <lp15@cam.ac.uk>
parents:
61682
diff
changeset
|
2228 |
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
|
2229 |
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
|
2230 |
|
71585 | 2231 |
lemma nth_take [simp]: "i < n \<Longrightarrow> (take n xs)!i = xs!i" |
68709 | 2232 |
proof (induct xs arbitrary: i n) |
69850 | 2233 |
case Nil |
2234 |
then show ?case by simp |
|
2235 |
next |
|
2236 |
case Cons |
|
2237 |
then show ?case by (cases n; cases i) simp_all |
|
2238 |
qed |
|
13114 | 2239 |
|
13142 | 2240 |
lemma nth_drop [simp]: |
71585 | 2241 |
"n \<le> length xs \<Longrightarrow> (drop n xs)!i = xs!(n + i)" |
68709 | 2242 |
proof (induct n arbitrary: xs) |
69850 | 2243 |
case 0 |
2244 |
then show ?case by simp |
|
2245 |
next |
|
2246 |
case Suc |
|
2247 |
then show ?case by (cases xs) simp_all |
|
2248 |
qed |
|
3507 | 2249 |
|
26584
46f3b89b2445
move lemmas from Word/BinBoolList.thy to List.thy
huffman
parents:
26480
diff
changeset
|
2250 |
lemma butlast_take: |
71585 | 2251 |
"n \<le> length xs \<Longrightarrow> butlast (take n xs) = take (n - 1) xs" |
75598 | 2252 |
by (simp add: butlast_conv_take) |
26584
46f3b89b2445
move lemmas from Word/BinBoolList.thy to List.thy
huffman
parents:
26480
diff
changeset
|
2253 |
|
46f3b89b2445
move lemmas from Word/BinBoolList.thy to List.thy
huffman
parents:
26480
diff
changeset
|
2254 |
lemma butlast_drop: "butlast (drop n xs) = drop n (butlast xs)" |
68719 | 2255 |
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
|
2256 |
|
71585 | 2257 |
lemma take_butlast: "n < length xs \<Longrightarrow> take n (butlast xs) = take n xs" |
75598 | 2258 |
by (simp add: butlast_conv_take) |
26584
46f3b89b2445
move lemmas from Word/BinBoolList.thy to List.thy
huffman
parents:
26480
diff
changeset
|
2259 |
|
46f3b89b2445
move lemmas from Word/BinBoolList.thy to List.thy
huffman
parents:
26480
diff
changeset
|
2260 |
lemma drop_butlast: "drop n (butlast xs) = butlast (drop n xs)" |
68719 | 2261 |
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
|
2262 |
|
72555 | 2263 |
lemma butlast_power: "(butlast ^^ n) xs = take (length xs - n) xs" |
2264 |
by (induct n) (auto simp: butlast_take) |
|
2265 |
||
46500
0196966d6d2d
removing unnecessary premises in theorems of List theory
bulwahn
parents:
46448
diff
changeset
|
2266 |
lemma hd_drop_conv_nth: "n < length xs \<Longrightarrow> hd(drop n xs) = xs!n" |
68719 | 2267 |
by(simp add: hd_conv_nth) |
18423 | 2268 |
|
35248 | 2269 |
lemma set_take_subset_set_take: |
68709 | 2270 |
"m \<le> n \<Longrightarrow> set(take m xs) \<le> set(take n xs)" |
2271 |
proof (induct xs arbitrary: m n) |
|
2272 |
case (Cons x xs m n) then show ?case |
|
2273 |
by (cases n) (auto simp: take_Cons) |
|
2274 |
qed simp |
|
35248 | 2275 |
|
24526 | 2276 |
lemma set_take_subset: "set(take n xs) \<subseteq> set xs" |
68719 | 2277 |
by(induct xs arbitrary: n)(auto simp:take_Cons split:nat.split) |
24526 | 2278 |
|
2279 |
lemma set_drop_subset: "set(drop n xs) \<subseteq> set xs" |
|
68719 | 2280 |
by(induct xs arbitrary: n)(auto simp:drop_Cons split:nat.split) |
14025 | 2281 |
|
35248 | 2282 |
lemma set_drop_subset_set_drop: |
68709 | 2283 |
"m \<ge> n \<Longrightarrow> set(drop m xs) \<le> set(drop n xs)" |
2284 |
proof (induct xs arbitrary: m n) |
|
2285 |
case (Cons x xs m n) |
|
2286 |
then show ?case |
|
2287 |
by (clarsimp simp: drop_Cons split: nat.split) (metis set_drop_subset subset_iff) |
|
2288 |
qed simp |
|
35248 | 2289 |
|
67613 | 2290 |
lemma in_set_takeD: "x \<in> set(take n xs) \<Longrightarrow> x \<in> set xs" |
68719 | 2291 |
using set_take_subset by fast |
14187 | 2292 |
|
67613 | 2293 |
lemma in_set_dropD: "x \<in> set(drop n xs) \<Longrightarrow> x \<in> set xs" |
68719 | 2294 |
using set_drop_subset by fast |
14187 | 2295 |
|
13114 | 2296 |
lemma append_eq_conv_conj: |
24526 | 2297 |
"(xs @ ys = zs) = (xs = take (length xs) zs \<and> ys = drop (length xs) zs)" |
68709 | 2298 |
proof (induct xs arbitrary: zs) |
2299 |
case (Cons x xs zs) then show ?case |
|
2300 |
by (cases zs, auto) |
|
2301 |
qed auto |
|
13142 | 2302 |
|
71393
fce780f9c9c6
new examples of BNF lifting across quotients using a new theory of confluence,
traytel
parents:
70911
diff
changeset
|
2303 |
lemma map_eq_append_conv: |
fce780f9c9c6
new examples of BNF lifting across quotients using a new theory of confluence,
traytel
parents:
70911
diff
changeset
|
2304 |
"map f xs = ys @ zs \<longleftrightarrow> (\<exists>us vs. xs = us @ vs \<and> ys = map f us \<and> zs = map f vs)" |
fce780f9c9c6
new examples of BNF lifting across quotients using a new theory of confluence,
traytel
parents:
70911
diff
changeset
|
2305 |
proof - |
fce780f9c9c6
new examples of BNF lifting across quotients using a new theory of confluence,
traytel
parents:
70911
diff
changeset
|
2306 |
have "map f xs \<noteq> ys @ zs \<and> map f xs \<noteq> ys @ zs \<or> map f xs \<noteq> ys @ zs \<or> map f xs = ys @ zs \<and> |
fce780f9c9c6
new examples of BNF lifting across quotients using a new theory of confluence,
traytel
parents:
70911
diff
changeset
|
2307 |
(\<exists>bs bsa. xs = bs @ bsa \<and> ys = map f bs \<and> zs = map f bsa)" |
fce780f9c9c6
new examples of BNF lifting across quotients using a new theory of confluence,
traytel
parents:
70911
diff
changeset
|
2308 |
by (metis append_eq_conv_conj append_take_drop_id drop_map take_map) |
fce780f9c9c6
new examples of BNF lifting across quotients using a new theory of confluence,
traytel
parents:
70911
diff
changeset
|
2309 |
then show ?thesis |
fce780f9c9c6
new examples of BNF lifting across quotients using a new theory of confluence,
traytel
parents:
70911
diff
changeset
|
2310 |
using map_append by blast |
fce780f9c9c6
new examples of BNF lifting across quotients using a new theory of confluence,
traytel
parents:
70911
diff
changeset
|
2311 |
qed |
fce780f9c9c6
new examples of BNF lifting across quotients using a new theory of confluence,
traytel
parents:
70911
diff
changeset
|
2312 |
|
74741
6e1fad4f602b
added eq_iff_swap for creating symmetric variants of thms; applied it in List.
nipkow
parents:
74424
diff
changeset
|
2313 |
lemmas append_eq_map_conv = map_eq_append_conv[THEN eq_iff_swap] |
71393
fce780f9c9c6
new examples of BNF lifting across quotients using a new theory of confluence,
traytel
parents:
70911
diff
changeset
|
2314 |
|
58807 | 2315 |
lemma take_add: "take (i+j) xs = take i xs @ take j (drop i xs)" |
68709 | 2316 |
proof (induct xs arbitrary: i) |
2317 |
case (Cons x xs i) then show ?case |
|
2318 |
by (cases i, auto) |
|
2319 |
qed auto |
|
14050 | 2320 |
|
14300 | 2321 |
lemma append_eq_append_conv_if: |
58807 | 2322 |
"(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
|
2323 |
(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
|
2324 |
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
|
2325 |
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 | 2326 |
proof (induct xs\<^sub>1 arbitrary: ys\<^sub>1) |
2327 |
case (Cons a xs\<^sub>1 ys\<^sub>1) then show ?case |
|
2328 |
by (cases ys\<^sub>1, auto) |
|
2329 |
qed auto |
|
14300 | 2330 |
|
15110
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
2331 |
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
|
2332 |
"n < length xs \<Longrightarrow> take n xs @ [hd (drop n xs)] = take (Suc n) xs" |
68709 | 2333 |
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
|
2334 |
|
17501 | 2335 |
lemma id_take_nth_drop: |
64963 | 2336 |
"i < length xs \<Longrightarrow> xs = take i xs @ xs!i # drop (Suc i) xs" |
17501 | 2337 |
proof - |
2338 |
assume si: "i < length xs" |
|
2339 |
hence "xs = take (Suc i) xs @ drop (Suc i) xs" by auto |
|
2340 |
moreover |
|
2341 |
from si have "take (Suc i) xs = take i xs @ [xs!i]" |
|
68709 | 2342 |
using take_Suc_conv_app_nth by blast |
17501 | 2343 |
ultimately show ?thesis by auto |
2344 |
qed |
|
64963 | 2345 |
|
59728 | 2346 |
lemma take_update_cancel[simp]: "n \<le> m \<Longrightarrow> take n (xs[m := y]) = take n xs" |
68719 | 2347 |
by(simp add: list_eq_iff_nth_eq) |
59728 | 2348 |
|
2349 |
lemma drop_update_cancel[simp]: "n < m \<Longrightarrow> drop m (xs[n := x]) = drop m xs" |
|
68719 | 2350 |
by(simp add: list_eq_iff_nth_eq) |
59728 | 2351 |
|
17501 | 2352 |
lemma upd_conv_take_nth_drop: |
58807 | 2353 |
"i < length xs \<Longrightarrow> xs[i:=a] = take i xs @ a # drop (Suc i) xs" |
17501 | 2354 |
proof - |
2355 |
assume i: "i < length xs" |
|
2356 |
have "xs[i:=a] = (take i xs @ xs!i # drop (Suc i) xs)[i:=a]" |
|
2357 |
by(rule arg_cong[OF id_take_nth_drop[OF i]]) |
|
2358 |
also have "\<dots> = take i xs @ a # drop (Suc i) xs" |
|
2359 |
using i by (simp add: list_update_append) |
|
2360 |
finally show ?thesis . |
|
2361 |
qed |
|
2362 |
||
66891
5ec8cd4db7e2
drop a superfluous assumption that was found by the find_unused_assms command
bulwahn
parents:
66890
diff
changeset
|
2363 |
lemma take_update_swap: "take m (xs[n := x]) = (take m xs)[n := x]" |
68709 | 2364 |
proof (cases "n \<ge> length xs") |
2365 |
case False |
|
2366 |
then show ?thesis |
|
2367 |
by (simp add: upd_conv_take_nth_drop take_Cons drop_take min_def diff_Suc split: nat.split) |
|
2368 |
qed auto |
|
2369 |
||
71393
fce780f9c9c6
new examples of BNF lifting across quotients using a new theory of confluence,
traytel
parents:
70911
diff
changeset
|
2370 |
lemma drop_update_swap: |
68709 | 2371 |
assumes "m \<le> n" shows "drop m (xs[n := x]) = (drop m xs)[n-m := x]" |
2372 |
proof (cases "n \<ge> length xs") |
|
2373 |
case False |
|
2374 |
with assms show ?thesis |
|
2375 |
by (simp add: upd_conv_take_nth_drop drop_take) |
|
2376 |
qed auto |
|
59728 | 2377 |
|
2378 |
lemma nth_image: "l \<le> size xs \<Longrightarrow> nth xs ` {0..<l} = set(take l xs)" |
|
75598 | 2379 |
by (simp add: set_conv_nth) force |
59728 | 2380 |
|
13114 | 2381 |
|
69593 | 2382 |
subsubsection \<open>\<^const>\<open>takeWhile\<close> and \<^const>\<open>dropWhile\<close>\<close> |
13114 | 2383 |
|
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
|
2384 |
lemma length_takeWhile_le: "length (takeWhile P xs) \<le> length xs" |
68719 | 2385 |
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
|
2386 |
|
13142 | 2387 |
lemma takeWhile_dropWhile_id [simp]: "takeWhile P xs @ dropWhile P xs = xs" |
68719 | 2388 |
by (induct xs) auto |
13114 | 2389 |
|
13142 | 2390 |
lemma takeWhile_append1 [simp]: |
67091 | 2391 |
"\<lbrakk>x \<in> set xs; \<not>P(x)\<rbrakk> \<Longrightarrow> takeWhile P (xs @ ys) = takeWhile P xs" |
68719 | 2392 |
by (induct xs) auto |
13114 | 2393 |
|
13142 | 2394 |
lemma takeWhile_append2 [simp]: |
67613 | 2395 |
"(\<And>x. x \<in> set xs \<Longrightarrow> P x) \<Longrightarrow> takeWhile P (xs @ ys) = xs @ takeWhile P ys" |
68719 | 2396 |
by (induct xs) auto |
13114 | 2397 |
|
71778 | 2398 |
lemma takeWhile_append: |
2399 |
"takeWhile P (xs @ ys) = (if \<forall>x\<in>set xs. P x then xs @ takeWhile P ys else takeWhile P xs)" |
|
2400 |
using takeWhile_append1[of _ xs P ys] takeWhile_append2[of xs P ys] by auto |
|
2401 |
||
67613 | 2402 |
lemma takeWhile_tail: "\<not> P x \<Longrightarrow> takeWhile P (xs @ (x#l)) = takeWhile P xs" |
68719 | 2403 |
by (induct xs) auto |
13114 | 2404 |
|
71778 | 2405 |
lemma takeWhile_eq_Nil_iff: "takeWhile P xs = [] \<longleftrightarrow> xs = [] \<or> \<not>P (hd xs)" |
2406 |
by (cases xs) auto |
|
2407 |
||
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
|
2408 |
lemma takeWhile_nth: "j < length (takeWhile P xs) \<Longrightarrow> takeWhile P xs ! j = xs ! j" |
68709 | 2409 |
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
|
2410 |
|
75496 | 2411 |
lemma takeWhile_takeWhile: "takeWhile Q (takeWhile P xs) = takeWhile (\<lambda>x. P x \<and> Q x) xs" |
2412 |
by(induct xs) simp_all |
|
2413 |
||
58807 | 2414 |
lemma dropWhile_nth: "j < length (dropWhile P xs) \<Longrightarrow> |
2415 |
dropWhile P xs ! j = xs ! (j + length (takeWhile P xs))" |
|
68709 | 2416 |
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
|
2417 |
|
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 |
lemma length_dropWhile_le: "length (dropWhile P xs) \<le> length xs" |
68719 | 2419 |
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
|
2420 |
|
13142 | 2421 |
lemma dropWhile_append1 [simp]: |
67091 | 2422 |
"\<lbrakk>x \<in> set xs; \<not>P(x)\<rbrakk> \<Longrightarrow> dropWhile P (xs @ ys) = (dropWhile P xs)@ys" |
68719 | 2423 |
by (induct xs) auto |
13114 | 2424 |
|
13142 | 2425 |
lemma dropWhile_append2 [simp]: |
71585 | 2426 |
"(\<And>x. x \<in> set xs \<Longrightarrow> P(x)) \<Longrightarrow> dropWhile P (xs @ ys) = dropWhile P ys" |
68719 | 2427 |
by (induct xs) auto |
13114 | 2428 |
|
45841 | 2429 |
lemma dropWhile_append3: |
2430 |
"\<not> P y \<Longrightarrow>dropWhile P (xs @ y # ys) = dropWhile P xs @ y # ys" |
|
68719 | 2431 |
by (induct xs) auto |
45841 | 2432 |
|
71778 | 2433 |
lemma dropWhile_append: |
2434 |
"dropWhile P (xs @ ys) = (if \<forall>x\<in>set xs. P x then dropWhile P ys else dropWhile P xs @ ys)" |
|
2435 |
using dropWhile_append1[of _ xs P ys] dropWhile_append2[of xs P ys] by auto |
|
2436 |
||
45841 | 2437 |
lemma dropWhile_last: |
2438 |
"x \<in> set xs \<Longrightarrow> \<not> P x \<Longrightarrow> last (dropWhile P xs) = last xs" |
|
68719 | 2439 |
by (auto simp add: dropWhile_append3 in_set_conv_decomp) |
45841 | 2440 |
|
2441 |
lemma set_dropWhileD: "x \<in> set (dropWhile P xs) \<Longrightarrow> x \<in> set xs" |
|
68719 | 2442 |
by (induct xs) (auto split: if_split_asm) |
45841 | 2443 |
|
67613 | 2444 |
lemma set_takeWhileD: "x \<in> set (takeWhile P xs) \<Longrightarrow> x \<in> set xs \<and> P x" |
68719 | 2445 |
by (induct xs) (auto split: if_split_asm) |
13114 | 2446 |
|
13913 | 2447 |
lemma takeWhile_eq_all_conv[simp]: |
58807 | 2448 |
"(takeWhile P xs = xs) = (\<forall>x \<in> set xs. P x)" |
68719 | 2449 |
by(induct xs, auto) |
13913 | 2450 |
|
2451 |
lemma dropWhile_eq_Nil_conv[simp]: |
|
58807 | 2452 |
"(dropWhile P xs = []) = (\<forall>x \<in> set xs. P x)" |
68719 | 2453 |
by(induct xs, auto) |
13913 | 2454 |
|
2455 |
lemma dropWhile_eq_Cons_conv: |
|
67091 | 2456 |
"(dropWhile P xs = y#ys) = (xs = takeWhile P xs @ y # ys \<and> \<not> P y)" |
68719 | 2457 |
by(induct xs, auto) |
13913 | 2458 |
|
71778 | 2459 |
lemma dropWhile_eq_self_iff: "dropWhile P xs = xs \<longleftrightarrow> xs = [] \<or> \<not>P (hd xs)" |
2460 |
by (cases xs) (auto simp: dropWhile_eq_Cons_conv) |
|
2461 |
||
75496 | 2462 |
lemma dropWhile_dropWhile1: "(\<And>x. Q x \<Longrightarrow> P x) \<Longrightarrow> dropWhile Q (dropWhile P xs) = dropWhile P xs" |
2463 |
by(induct xs) simp_all |
|
2464 |
||
2465 |
lemma dropWhile_dropWhile2: "(\<And>x. P x \<Longrightarrow> Q x) \<Longrightarrow> takeWhile P (takeWhile Q xs) = takeWhile P xs" |
|
2466 |
by(induct xs) simp_all |
|
2467 |
||
2468 |
lemma dropWhile_takeWhile: |
|
2469 |
"(\<And>x. P x \<Longrightarrow> Q x) \<Longrightarrow> dropWhile P (takeWhile Q xs) = takeWhile Q (dropWhile P xs)" |
|
2470 |
by (induction xs) auto |
|
2471 |
||
71585 | 2472 |
lemma distinct_takeWhile[simp]: "distinct xs \<Longrightarrow> distinct (takeWhile P xs)" |
68719 | 2473 |
by (induct xs) (auto dest: set_takeWhileD) |
31077 | 2474 |
|
71585 | 2475 |
lemma distinct_dropWhile[simp]: "distinct xs \<Longrightarrow> distinct (dropWhile P xs)" |
68719 | 2476 |
by (induct xs) auto |
31077 | 2477 |
|
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
|
2478 |
lemma takeWhile_map: "takeWhile P (map f xs) = map f (takeWhile (P \<circ> f) xs)" |
68719 | 2479 |
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
|
2480 |
|
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
hoelzl
parents:
33593
diff
changeset
|
2481 |
lemma dropWhile_map: "dropWhile P (map f xs) = map f (dropWhile (P \<circ> f) xs)" |
68719 | 2482 |
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
|
2483 |
|
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
hoelzl
parents:
33593
diff
changeset
|
2484 |
lemma takeWhile_eq_take: "takeWhile P xs = take (length (takeWhile P xs)) xs" |
68719 | 2485 |
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
|
2486 |
|
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
hoelzl
parents:
33593
diff
changeset
|
2487 |
lemma dropWhile_eq_drop: "dropWhile P xs = drop (length (takeWhile P xs)) xs" |
68719 | 2488 |
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
|
2489 |
|
58807 | 2490 |
lemma hd_dropWhile: "dropWhile P xs \<noteq> [] \<Longrightarrow> \<not> P (hd (dropWhile P xs))" |
68719 | 2491 |
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
|
2492 |
|
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
hoelzl
parents:
33593
diff
changeset
|
2493 |
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
|
2494 |
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
|
2495 |
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
|
2496 |
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
|
2497 |
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
|
2498 |
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
|
2499 |
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
|
2500 |
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
|
2501 |
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
|
2502 |
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
|
2503 |
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
|
2504 |
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
|
2505 |
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
|
2506 |
|
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
hoelzl
parents:
33593
diff
changeset
|
2507 |
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
|
2508 |
"\<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
|
2509 |
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
|
2510 |
proof (induct xs arbitrary: n) |
60580 | 2511 |
case Nil |
2512 |
thus ?case by simp |
|
2513 |
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
|
2514 |
case (Cons x xs) |
60580 | 2515 |
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
|
2516 |
proof (cases n) |
60580 | 2517 |
case 0 |
2518 |
with Cons show ?thesis by simp |
|
2519 |
next |
|
2520 |
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
|
2521 |
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
|
2522 |
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
|
2523 |
proof (rule Cons.hyps) |
60580 | 2524 |
fix i |
2525 |
assume "i < n'" "i < length xs" |
|
2526 |
thus "P (xs ! i)" using Cons.prems(1)[of "Suc i"] by simp |
|
2527 |
next |
|
2528 |
assume "n' < length xs" |
|
2529 |
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
|
2530 |
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
|
2531 |
ultimately show ?thesis by simp |
68719 | 2532 |
qed |
60580 | 2533 |
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
|
2534 |
|
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
hoelzl
parents:
33593
diff
changeset
|
2535 |
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
|
2536 |
"length (takeWhile P xs) < length xs \<Longrightarrow> \<not> P (xs ! length (takeWhile P xs))" |
68719 | 2537 |
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
|
2538 |
|
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
hoelzl
parents:
33593
diff
changeset
|
2539 |
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
|
2540 |
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
|
2541 |
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
|
2542 |
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
|
2543 |
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
|
2544 |
hence "length (takeWhile P xs) < length xs" using assms by simp |
60758 | 2545 |
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
|
2546 |
qed |
31077 | 2547 |
|
17501 | 2548 |
lemma takeWhile_neq_rev: "\<lbrakk>distinct xs; x \<in> set xs\<rbrakk> \<Longrightarrow> |
58807 | 2549 |
takeWhile (\<lambda>y. y \<noteq> x) (rev xs) = rev (tl (dropWhile (\<lambda>y. y \<noteq> x) xs))" |
68719 | 2550 |
by(induct xs) (auto simp: takeWhile_tail[where l="[]"]) |
17501 | 2551 |
|
2552 |
lemma dropWhile_neq_rev: "\<lbrakk>distinct xs; x \<in> set xs\<rbrakk> \<Longrightarrow> |
|
2553 |
dropWhile (\<lambda>y. y \<noteq> x) (rev xs) = x # rev (takeWhile (\<lambda>y. y \<noteq> x) xs)" |
|
68709 | 2554 |
proof (induct xs) |
2555 |
case (Cons a xs) |
|
2556 |
then show ?case |
|
2557 |
by(auto, subst dropWhile_append2, auto) |
|
2558 |
qed simp |
|
17501 | 2559 |
|
18423 | 2560 |
lemma takeWhile_not_last: |
58807 | 2561 |
"distinct xs \<Longrightarrow> takeWhile (\<lambda>y. y \<noteq> last xs) xs = butlast xs" |
68719 | 2562 |
by(induction xs rule: induct_list012) auto |
18423 | 2563 |
|
44013
5cfc1c36ae97
moved recdef package to HOL/Library/Old_Recdef.thy
krauss
parents:
43594
diff
changeset
|
2564 |
lemma takeWhile_cong [fundef_cong]: |
67613 | 2565 |
"\<lbrakk>l = k; \<And>x. x \<in> set l \<Longrightarrow> P x = Q x\<rbrakk> |
2566 |
\<Longrightarrow> takeWhile P l = takeWhile Q k" |
|
68719 | 2567 |
by (induct k arbitrary: l) (simp_all) |
18336
1a2e30b37ed3
Added recdef congruence rules for bounded quantifiers and commonly used
krauss
parents:
18049
diff
changeset
|
2568 |
|
44013
5cfc1c36ae97
moved recdef package to HOL/Library/Old_Recdef.thy
krauss
parents:
43594
diff
changeset
|
2569 |
lemma dropWhile_cong [fundef_cong]: |
67613 | 2570 |
"\<lbrakk>l = k; \<And>x. x \<in> set l \<Longrightarrow> P x = Q x\<rbrakk> |
2571 |
\<Longrightarrow> dropWhile P l = dropWhile Q k" |
|
68719 | 2572 |
by (induct k arbitrary: l, simp_all) |
18336
1a2e30b37ed3
Added recdef congruence rules for bounded quantifiers and commonly used
krauss
parents:
18049
diff
changeset
|
2573 |
|
52380 | 2574 |
lemma takeWhile_idem [simp]: |
2575 |
"takeWhile P (takeWhile P xs) = takeWhile P xs" |
|
68719 | 2576 |
by (induct xs) auto |
52380 | 2577 |
|
2578 |
lemma dropWhile_idem [simp]: |
|
2579 |
"dropWhile P (dropWhile P xs) = dropWhile P xs" |
|
68719 | 2580 |
by (induct xs) auto |
52380 | 2581 |
|
13114 | 2582 |
|
69593 | 2583 |
subsubsection \<open>\<^const>\<open>zip\<close>\<close> |
13114 | 2584 |
|
13142 | 2585 |
lemma zip_Nil [simp]: "zip [] ys = []" |
68719 | 2586 |
by (induct ys) auto |
13114 | 2587 |
|
13142 | 2588 |
lemma zip_Cons_Cons [simp]: "zip (x # xs) (y # ys) = (x, y) # zip xs ys" |
68719 | 2589 |
by simp |
13114 | 2590 |
|
13142 | 2591 |
declare zip_Cons [simp del] |
13114 | 2592 |
|
36198 | 2593 |
lemma [code]: |
2594 |
"zip [] ys = []" |
|
2595 |
"zip xs [] = []" |
|
2596 |
"zip (x # xs) (y # ys) = (x, y) # zip xs ys" |
|
68719 | 2597 |
by (fact zip_Nil zip.simps(1) zip_Cons_Cons)+ |
36198 | 2598 |
|
15281 | 2599 |
lemma zip_Cons1: |
58807 | 2600 |
"zip (x#xs) ys = (case ys of [] \<Rightarrow> [] | y#ys \<Rightarrow> (x,y)#zip xs ys)" |
68719 | 2601 |
by(auto split:list.split) |
15281 | 2602 |
|
13142 | 2603 |
lemma length_zip [simp]: |
58807 | 2604 |
"length (zip xs ys) = min (length xs) (length ys)" |
68719 | 2605 |
by (induct xs ys rule:list_induct2') auto |
13114 | 2606 |
|
34978
874150ddd50a
canonical insert operation; generalized lemma foldl_apply_inv to foldl_apply
haftmann
parents:
34942
diff
changeset
|
2607 |
lemma zip_obtain_same_length: |
874150ddd50a
canonical insert operation; generalized lemma foldl_apply_inv to foldl_apply
haftmann
parents:
34942
diff
changeset
|
2608 |
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
|
2609 |
\<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
|
2610 |
shows "P (zip xs ys)" |
874150ddd50a
canonical insert operation; generalized lemma foldl_apply_inv to foldl_apply
haftmann
parents:
34942
diff
changeset
|
2611 |
proof - |
874150ddd50a
canonical insert operation; generalized lemma foldl_apply_inv to foldl_apply
haftmann
parents:
34942
diff
changeset
|
2612 |
let ?n = "min (length xs) (length ys)" |
874150ddd50a
canonical insert operation; generalized lemma foldl_apply_inv to foldl_apply
haftmann
parents:
34942
diff
changeset
|
2613 |
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
|
2614 |
by (rule assms) simp_all |
874150ddd50a
canonical insert operation; generalized lemma foldl_apply_inv to foldl_apply
haftmann
parents:
34942
diff
changeset
|
2615 |
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
|
2616 |
proof (induct xs arbitrary: ys) |
874150ddd50a
canonical insert operation; generalized lemma foldl_apply_inv to foldl_apply
haftmann
parents:
34942
diff
changeset
|
2617 |
case Nil then show ?case by simp |
874150ddd50a
canonical insert operation; generalized lemma foldl_apply_inv to foldl_apply
haftmann
parents:
34942
diff
changeset
|
2618 |
next |
874150ddd50a
canonical insert operation; generalized lemma foldl_apply_inv to foldl_apply
haftmann
parents:
34942
diff
changeset
|
2619 |
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
|
2620 |
qed |
874150ddd50a
canonical insert operation; generalized lemma foldl_apply_inv to foldl_apply
haftmann
parents:
34942
diff
changeset
|
2621 |
ultimately show ?thesis by simp |
874150ddd50a
canonical insert operation; generalized lemma foldl_apply_inv to foldl_apply
haftmann
parents:
34942
diff
changeset
|
2622 |
qed |
874150ddd50a
canonical insert operation; generalized lemma foldl_apply_inv to foldl_apply
haftmann
parents:
34942
diff
changeset
|
2623 |
|
13114 | 2624 |
lemma zip_append1: |
58807 | 2625 |
"zip (xs @ ys) zs = |
2626 |
zip xs (take (length xs) zs) @ zip ys (drop (length xs) zs)" |
|
68719 | 2627 |
by (induct xs zs rule:list_induct2') auto |
13114 | 2628 |
|
2629 |
lemma zip_append2: |
|
58807 | 2630 |
"zip xs (ys @ zs) = |
2631 |
zip (take (length ys) xs) ys @ zip (drop (length ys) xs) zs" |
|
68719 | 2632 |
by (induct xs ys rule:list_induct2') auto |
13114 | 2633 |
|
13142 | 2634 |
lemma zip_append [simp]: |
71585 | 2635 |
"\<lbrakk>length xs = length us\<rbrakk> \<Longrightarrow> |
58807 | 2636 |
zip (xs@ys) (us@vs) = zip xs us @ zip ys vs" |
68719 | 2637 |
by (simp add: zip_append1) |
13114 | 2638 |
|
2639 |
lemma zip_rev: |
|
71585 | 2640 |
"length xs = length ys \<Longrightarrow> zip (rev xs) (rev ys) = rev (zip xs ys)" |
68719 | 2641 |
by (induct rule:list_induct2, simp_all) |
13114 | 2642 |
|
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
|
2643 |
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
|
2644 |
"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
|
2645 |
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
|
2646 |
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
|
2647 |
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
|
2648 |
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
|
2649 |
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
|
2650 |
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
|
2651 |
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
|
2652 |
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
|
2653 |
|
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
hoelzl
parents:
33593
diff
changeset
|
2654 |
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
|
2655 |
"zip (map f xs) ys = map (\<lambda>(x, y). (f x, y)) (zip xs ys)" |
68719 | 2656 |
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
|
2657 |
|
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
hoelzl
parents:
33593
diff
changeset
|
2658 |
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
|
2659 |
"zip xs (map f ys) = map (\<lambda>(x, y). (x, f y)) (zip xs ys)" |
68719 | 2660 |
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
|
2661 |
|
23096 | 2662 |
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
|
2663 |
"map f (zip (map g xs) ys) = map (%(x,y). f(g x, y)) (zip xs ys)" |
68719 | 2664 |
by (auto simp: zip_map1) |
23096 | 2665 |
|
2666 |
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
|
2667 |
"map f (zip xs (map g ys)) = map (%(x,y). f(x, g y)) (zip xs ys)" |
68719 | 2668 |
by (auto simp: zip_map2) |
23096 | 2669 |
|
60758 | 2670 |
text\<open>Courtesy of Andreas Lochbihler:\<close> |
31080 | 2671 |
lemma zip_same_conv_map: "zip xs xs = map (\<lambda>x. (x, x)) xs" |
68719 | 2672 |
by(induct xs) auto |
31080 | 2673 |
|
13142 | 2674 |
lemma nth_zip [simp]: |
71585 | 2675 |
"\<lbrakk>i < length xs; i < length ys\<rbrakk> \<Longrightarrow> (zip xs ys)!i = (xs!i, ys!i)" |
68709 | 2676 |
proof (induct ys arbitrary: i xs) |
2677 |
case (Cons y ys) |
|
2678 |
then show ?case |
|
2679 |
by (cases xs) (simp_all add: nth.simps split: nat.split) |
|
2680 |
qed auto |
|
13114 | 2681 |
|
2682 |
lemma set_zip: |
|
58807 | 2683 |
"set (zip xs ys) = {(xs!i, ys!i) | i. i < min (length xs) (length ys)}" |
68719 | 2684 |
by(simp add: set_conv_nth cong: rev_conj_cong) |
13114 | 2685 |
|
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
|
2686 |
lemma zip_same: "((a,b) \<in> set (zip xs xs)) = (a \<in> set xs \<and> a = b)" |
68719 | 2687 |
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
|
2688 |
|
68709 | 2689 |
lemma zip_update: "zip (xs[i:=x]) (ys[i:=y]) = (zip xs ys)[i:=(x,y)]" |
2690 |
by (simp add: update_zip) |
|
13114 | 2691 |
|
13142 | 2692 |
lemma zip_replicate [simp]: |
24526 | 2693 |
"zip (replicate i x) (replicate j y) = replicate (min i j) (x,y)" |
68709 | 2694 |
proof (induct i arbitrary: j) |
2695 |
case (Suc i) |
|
2696 |
then show ?case |
|
2697 |
by (cases j, auto) |
|
2698 |
qed auto |
|
13114 | 2699 |
|
61630 | 2700 |
lemma zip_replicate1: "zip (replicate n x) ys = map (Pair x) (take n ys)" |
68719 | 2701 |
by(induction ys arbitrary: n)(case_tac [2] n, simp_all) |
61630 | 2702 |
|
68709 | 2703 |
lemma take_zip: "take n (zip xs ys) = zip (take n xs) (take n ys)" |
2704 |
proof (induct n arbitrary: xs ys) |
|
69850 | 2705 |
case 0 |
2706 |
then show ?case by simp |
|
2707 |
next |
|
2708 |
case Suc |
|
2709 |
then show ?case by (cases xs; cases ys) simp_all |
|
2710 |
qed |
|
68709 | 2711 |
|
2712 |
lemma drop_zip: "drop n (zip xs ys) = zip (drop n xs) (drop n ys)" |
|
2713 |
proof (induct n arbitrary: xs ys) |
|
69850 | 2714 |
case 0 |
2715 |
then show ?case by simp |
|
2716 |
next |
|
2717 |
case Suc |
|
2718 |
then show ?case by (cases xs; cases ys) simp_all |
|
2719 |
qed |
|
19487 | 2720 |
|
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
|
2721 |
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
|
2722 |
proof (induct xs arbitrary: ys) |
69850 | 2723 |
case Nil |
2724 |
then show ?case by simp |
|
2725 |
next |
|
2726 |
case Cons |
|
2727 |
then show ?case by (cases ys) auto |
|
2728 |
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
|
2729 |
|
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
hoelzl
parents:
33593
diff
changeset
|
2730 |
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
|
2731 |
proof (induct xs arbitrary: ys) |
69850 | 2732 |
case Nil |
2733 |
then show ?case by simp |
|
2734 |
next |
|
2735 |
case Cons |
|
2736 |
then show ?case by (cases ys) auto |
|
2737 |
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
|
2738 |
|
58807 | 2739 |
lemma set_zip_leftD: "(x,y)\<in> set (zip xs ys) \<Longrightarrow> x \<in> set xs" |
68719 | 2740 |
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
|
2741 |
|
58807 | 2742 |
lemma set_zip_rightD: "(x,y)\<in> set (zip xs ys) \<Longrightarrow> y \<in> set ys" |
68719 | 2743 |
by (induct xs ys rule:list_induct2') auto |
13142 | 2744 |
|
23983 | 2745 |
lemma in_set_zipE: |
67613 | 2746 |
"(x,y) \<in> set(zip xs ys) \<Longrightarrow> (\<lbrakk> x \<in> set xs; y \<in> set ys \<rbrakk> \<Longrightarrow> R) \<Longrightarrow> R" |
68719 | 2747 |
by(blast dest: set_zip_leftD set_zip_rightD) |
23983 | 2748 |
|
58807 | 2749 |
lemma zip_map_fst_snd: "zip (map fst zs) (map snd zs) = zs" |
68719 | 2750 |
by (induct zs) simp_all |
29829 | 2751 |
|
2752 |
lemma zip_eq_conv: |
|
2753 |
"length xs = length ys \<Longrightarrow> zip xs ys = zs \<longleftrightarrow> map fst zs = xs \<and> map snd zs = ys" |
|
68719 | 2754 |
by (auto simp add: zip_map_fst_snd) |
29829 | 2755 |
|
51173 | 2756 |
lemma in_set_zip: |
2757 |
"p \<in> set (zip xs ys) \<longleftrightarrow> (\<exists>n. xs ! n = fst p \<and> ys ! n = snd p |
|
58807 | 2758 |
\<and> n < length xs \<and> n < length ys)" |
68719 | 2759 |
by (cases p) (auto simp add: set_zip) |
51173 | 2760 |
|
66584 | 2761 |
lemma in_set_impl_in_set_zip1: |
2762 |
assumes "length xs = length ys" |
|
2763 |
assumes "x \<in> set xs" |
|
2764 |
obtains y where "(x, y) \<in> set (zip xs ys)" |
|
2765 |
proof - |
|
2766 |
from assms have "x \<in> set (map fst (zip xs ys))" by simp |
|
2767 |
from this that show ?thesis by fastforce |
|
2768 |
qed |
|
2769 |
||
2770 |
lemma in_set_impl_in_set_zip2: |
|
2771 |
assumes "length xs = length ys" |
|
2772 |
assumes "y \<in> set ys" |
|
2773 |
obtains x where "(x, y) \<in> set (zip xs ys)" |
|
2774 |
proof - |
|
2775 |
from assms have "y \<in> set (map snd (zip xs ys))" by simp |
|
2776 |
from this that show ?thesis by fastforce |
|
2777 |
qed |
|
2778 |
||
74741
6e1fad4f602b
added eq_iff_swap for creating symmetric variants of thms; applied it in List.
nipkow
parents:
74424
diff
changeset
|
2779 |
lemma zip_eq_Nil_iff[simp]: |
70226 | 2780 |
"zip xs ys = [] \<longleftrightarrow> xs = [] \<or> ys = []" |
2781 |
by (cases xs; cases ys) simp_all |
|
2782 |
||
74741
6e1fad4f602b
added eq_iff_swap for creating symmetric variants of thms; applied it in List.
nipkow
parents:
74424
diff
changeset
|
2783 |
lemmas Nil_eq_zip_iff[simp] = zip_eq_Nil_iff[THEN eq_iff_swap] |
6e1fad4f602b
added eq_iff_swap for creating symmetric variants of thms; applied it in List.
nipkow
parents:
74424
diff
changeset
|
2784 |
|
70226 | 2785 |
lemma zip_eq_ConsE: |
2786 |
assumes "zip xs ys = xy # xys" |
|
2787 |
obtains x xs' y ys' where "xs = x # xs'" |
|
2788 |
and "ys = y # ys'" and "xy = (x, y)" |
|
2789 |
and "xys = zip xs' ys'" |
|
2790 |
proof - |
|
2791 |
from assms have "xs \<noteq> []" and "ys \<noteq> []" |
|
2792 |
using zip_eq_Nil_iff [of xs ys] by simp_all |
|
2793 |
then obtain x xs' y ys' where xs: "xs = x # xs'" |
|
2794 |
and ys: "ys = y # ys'" |
|
2795 |
by (cases xs; cases ys) auto |
|
2796 |
with assms have "xy = (x, y)" and "xys = zip xs' ys'" |
|
2797 |
by simp_all |
|
2798 |
with xs ys show ?thesis .. |
|
2799 |
qed |
|
2800 |
||
70911 | 2801 |
lemma semilattice_map2: |
2802 |
"semilattice (map2 (\<^bold>*))" if "semilattice (\<^bold>*)" |
|
2803 |
for f (infixl "\<^bold>*" 70) |
|
2804 |
proof - |
|
2805 |
from that interpret semilattice f . |
|
2806 |
show ?thesis |
|
2807 |
proof |
|
2808 |
show "map2 (\<^bold>*) (map2 (\<^bold>*) xs ys) zs = map2 (\<^bold>*) xs (map2 (\<^bold>*) ys zs)" |
|
2809 |
for xs ys zs :: "'a list" |
|
2810 |
proof (induction "zip xs (zip ys zs)" arbitrary: xs ys zs) |
|
2811 |
case Nil |
|
2812 |
from Nil [symmetric] show ?case |
|
75598 | 2813 |
by auto |
70911 | 2814 |
next |
2815 |
case (Cons xyz xyzs) |
|
2816 |
from Cons.hyps(2) [symmetric] show ?case |
|
2817 |
by (rule zip_eq_ConsE) (erule zip_eq_ConsE, |
|
2818 |
auto intro: Cons.hyps(1) simp add: ac_simps) |
|
2819 |
qed |
|
2820 |
show "map2 (\<^bold>*) xs ys = map2 (\<^bold>*) ys xs" |
|
2821 |
for xs ys :: "'a list" |
|
2822 |
proof (induction "zip xs ys" arbitrary: xs ys) |
|
2823 |
case Nil |
|
2824 |
then show ?case |
|
75598 | 2825 |
by auto |
70911 | 2826 |
next |
2827 |
case (Cons xy xys) |
|
2828 |
from Cons.hyps(2) [symmetric] show ?case |
|
2829 |
by (rule zip_eq_ConsE) (auto intro: Cons.hyps(1) simp add: ac_simps) |
|
2830 |
qed |
|
2831 |
show "map2 (\<^bold>*) xs xs = xs" |
|
2832 |
for xs :: "'a list" |
|
2833 |
by (induction xs) simp_all |
|
2834 |
qed |
|
2835 |
qed |
|
2836 |
||
51173 | 2837 |
lemma pair_list_eqI: |
2838 |
assumes "map fst xs = map fst ys" and "map snd xs = map snd ys" |
|
2839 |
shows "xs = ys" |
|
2840 |
proof - |
|
2841 |
from assms(1) have "length xs = length ys" by (rule map_eq_imp_length_eq) |
|
2842 |
from this assms show ?thesis |
|
2843 |
by (induct xs ys rule: list_induct2) (simp_all add: prod_eqI) |
|
2844 |
qed |
|
2845 |
||
71423 | 2846 |
lemma hd_zip: |
2847 |
\<open>hd (zip xs ys) = (hd xs, hd ys)\<close> if \<open>xs \<noteq> []\<close> and \<open>ys \<noteq> []\<close> |
|
2848 |
using that by (cases xs; cases ys) simp_all |
|
2849 |
||
2850 |
lemma last_zip: |
|
2851 |
\<open>last (zip xs ys) = (last xs, last ys)\<close> if \<open>xs \<noteq> []\<close> and \<open>ys \<noteq> []\<close> |
|
2852 |
and \<open>length xs = length ys\<close> |
|
2853 |
using that by (cases xs rule: rev_cases; cases ys rule: rev_cases) simp_all |
|
2854 |
||
35115 | 2855 |
|
69593 | 2856 |
subsubsection \<open>\<^const>\<open>list_all2\<close>\<close> |
13114 | 2857 |
|
64963 | 2858 |
lemma list_all2_lengthD [intro?]: |
71585 | 2859 |
"list_all2 P xs ys \<Longrightarrow> length xs = length ys" |
68719 | 2860 |
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
|
2861 |
|
19787 | 2862 |
lemma list_all2_Nil [iff, code]: "list_all2 P [] ys = (ys = [])" |
68719 | 2863 |
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
|
2864 |
|
19787 | 2865 |
lemma list_all2_Nil2 [iff, code]: "list_all2 P xs [] = (xs = [])" |
68719 | 2866 |
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
|
2867 |
|
07eeb832f28d
introduced characters for code generator; some improved code lemmas for some list functions
haftmann
parents:
19585
diff
changeset
|
2868 |
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
|
2869 |
"list_all2 P (x # xs) (y # ys) = (P x y \<and> list_all2 P xs ys)" |
68719 | 2870 |
by (auto simp add: list_all2_iff) |
13114 | 2871 |
|
2872 |
lemma list_all2_Cons1: |
|
58807 | 2873 |
"list_all2 P (x # xs) ys = (\<exists>z zs. ys = z # zs \<and> P x z \<and> list_all2 P xs zs)" |
68719 | 2874 |
by (cases ys) auto |
13114 | 2875 |
|
2876 |
lemma list_all2_Cons2: |
|
58807 | 2877 |
"list_all2 P xs (y # ys) = (\<exists>z zs. xs = z # zs \<and> P z y \<and> list_all2 P zs ys)" |
68719 | 2878 |
by (cases xs) auto |
13114 | 2879 |
|
45794 | 2880 |
lemma list_all2_induct |
2881 |
[consumes 1, case_names Nil Cons, induct set: list_all2]: |
|
2882 |
assumes P: "list_all2 P xs ys" |
|
2883 |
assumes Nil: "R [] []" |
|
47640 | 2884 |
assumes Cons: "\<And>x xs y ys. |
2885 |
\<lbrakk>P x y; list_all2 P xs ys; R xs ys\<rbrakk> \<Longrightarrow> R (x # xs) (y # ys)" |
|
45794 | 2886 |
shows "R xs ys" |
68719 | 2887 |
using P |
2888 |
by (induct xs arbitrary: ys) (auto simp add: list_all2_Cons1 Nil Cons) |
|
45794 | 2889 |
|
13142 | 2890 |
lemma list_all2_rev [iff]: |
58807 | 2891 |
"list_all2 P (rev xs) (rev ys) = list_all2 P xs ys" |
68719 | 2892 |
by (simp add: list_all2_iff zip_rev cong: conj_cong) |
13114 | 2893 |
|
13863 | 2894 |
lemma list_all2_rev1: |
58807 | 2895 |
"list_all2 P (rev xs) ys = list_all2 P xs (rev ys)" |
68719 | 2896 |
by (subst list_all2_rev [symmetric]) simp |
13863 | 2897 |
|
13114 | 2898 |
lemma list_all2_append1: |
58807 | 2899 |
"list_all2 P (xs @ ys) zs = |
67091 | 2900 |
(\<exists>us vs. zs = us @ vs \<and> length us = length xs \<and> length vs = length ys \<and> |
68709 | 2901 |
list_all2 P xs us \<and> list_all2 P ys vs)" (is "?lhs = ?rhs") |
2902 |
proof |
|
2903 |
assume ?lhs |
|
2904 |
then show ?rhs |
|
2905 |
apply (rule_tac x = "take (length xs) zs" in exI) |
|
2906 |
apply (rule_tac x = "drop (length xs) zs" in exI) |
|
2907 |
apply (force split: nat_diff_split simp add: list_all2_iff zip_append1) |
|
2908 |
done |
|
2909 |
next |
|
2910 |
assume ?rhs |
|
2911 |
then show ?lhs |
|
2912 |
by (auto simp add: list_all2_iff) |
|
2913 |
qed |
|
13114 | 2914 |
|
2915 |
lemma list_all2_append2: |
|
58807 | 2916 |
"list_all2 P xs (ys @ zs) = |
67091 | 2917 |
(\<exists>us vs. xs = us @ vs \<and> length us = length ys \<and> length vs = length zs \<and> |
68709 | 2918 |
list_all2 P us ys \<and> list_all2 P vs zs)" (is "?lhs = ?rhs") |
2919 |
proof |
|
2920 |
assume ?lhs |
|
2921 |
then show ?rhs |
|
2922 |
apply (rule_tac x = "take (length ys) xs" in exI) |
|
2923 |
apply (rule_tac x = "drop (length ys) xs" in exI) |
|
2924 |
apply (force split: nat_diff_split simp add: list_all2_iff zip_append2) |
|
2925 |
done |
|
2926 |
next |
|
2927 |
assume ?rhs |
|
2928 |
then show ?lhs |
|
2929 |
by (auto simp add: list_all2_iff) |
|
2930 |
qed |
|
13114 | 2931 |
|
13863 | 2932 |
lemma list_all2_append: |
14247 | 2933 |
"length xs = length ys \<Longrightarrow> |
2934 |
list_all2 P (xs@us) (ys@vs) = (list_all2 P xs ys \<and> list_all2 P us vs)" |
|
68719 | 2935 |
by (induct rule:list_induct2, simp_all) |
13863 | 2936 |
|
2937 |
lemma list_all2_appendI [intro?, trans]: |
|
2938 |
"\<lbrakk> list_all2 P a b; list_all2 P c d \<rbrakk> \<Longrightarrow> list_all2 P (a@c) (b@d)" |
|
68719 | 2939 |
by (simp add: list_all2_append list_all2_lengthD) |
13863 | 2940 |
|
13114 | 2941 |
lemma list_all2_conv_all_nth: |
58807 | 2942 |
"list_all2 P xs ys = |
2943 |
(length xs = length ys \<and> (\<forall>i < length xs. P (xs!i) (ys!i)))" |
|
68719 | 2944 |
by (force simp add: list_all2_iff set_zip) |
13114 | 2945 |
|
13883
0451e0fb3f22
Re-structured some proofs in order to get rid of rule_format attribute.
berghofe
parents:
13863
diff
changeset
|
2946 |
lemma list_all2_trans: |
71585 | 2947 |
assumes tr: "!!a b c. P1 a b \<Longrightarrow> P2 b c \<Longrightarrow> P3 a c" |
2948 |
shows "!!bs cs. list_all2 P1 as bs \<Longrightarrow> list_all2 P2 bs cs \<Longrightarrow> list_all2 P3 as cs" |
|
68719 | 2949 |
(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
|
2950 |
proof (induct as) |
0451e0fb3f22
Re-structured some proofs in order to get rid of rule_format attribute.
berghofe
parents:
13863
diff
changeset
|
2951 |
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
|
2952 |
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
|
2953 |
proof (induct bs) |
0451e0fb3f22
Re-structured some proofs in order to get rid of rule_format attribute.
berghofe
parents:
13863
diff
changeset
|
2954 |
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
|
2955 |
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
|
2956 |
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
|
2957 |
qed simp |
0451e0fb3f22
Re-structured some proofs in order to get rid of rule_format attribute.
berghofe
parents:
13863
diff
changeset
|
2958 |
qed simp |
0451e0fb3f22
Re-structured some proofs in order to get rid of rule_format attribute.
berghofe
parents:
13863
diff
changeset
|
2959 |
|
13863 | 2960 |
lemma list_all2_all_nthI [intro?]: |
2961 |
"length a = length b \<Longrightarrow> (\<And>n. n < length a \<Longrightarrow> P (a!n) (b!n)) \<Longrightarrow> list_all2 P a b" |
|
68719 | 2962 |
by (simp add: list_all2_conv_all_nth) |
13863 | 2963 |
|
14395 | 2964 |
lemma list_all2I: |
61032
b57df8eecad6
standardized some occurences of ancient "split" alias
haftmann
parents:
61031
diff
changeset
|
2965 |
"\<forall>x \<in> set (zip a b). case_prod P x \<Longrightarrow> length a = length b \<Longrightarrow> list_all2 P a b" |
68719 | 2966 |
by (simp add: list_all2_iff) |
14395 | 2967 |
|
14328 | 2968 |
lemma list_all2_nthD: |
13863 | 2969 |
"\<lbrakk> list_all2 P xs ys; p < size xs \<rbrakk> \<Longrightarrow> P (xs!p) (ys!p)" |
68719 | 2970 |
by (simp add: list_all2_conv_all_nth) |
13863 | 2971 |
|
14302 | 2972 |
lemma list_all2_nthD2: |
2973 |
"\<lbrakk>list_all2 P xs ys; p < size ys\<rbrakk> \<Longrightarrow> P (xs!p) (ys!p)" |
|
68719 | 2974 |
by (frule list_all2_lengthD) (auto intro: list_all2_nthD) |
14302 | 2975 |
|
64963 | 2976 |
lemma list_all2_map1: |
13863 | 2977 |
"list_all2 P (map f as) bs = list_all2 (\<lambda>x y. P (f x) y) as bs" |
68719 | 2978 |
by (simp add: list_all2_conv_all_nth) |
13863 | 2979 |
|
64963 | 2980 |
lemma list_all2_map2: |
13863 | 2981 |
"list_all2 P as (map f bs) = list_all2 (\<lambda>x y. P x (f y)) as bs" |
68719 | 2982 |
by (auto simp add: list_all2_conv_all_nth) |
13863 | 2983 |
|
14316
91b897b9a2dc
added some [intro?] and [trans] for list_all2 lemmas
kleing
parents:
14302
diff
changeset
|
2984 |
lemma list_all2_refl [intro?]: |
13863 | 2985 |
"(\<And>x. P x x) \<Longrightarrow> list_all2 P xs xs" |
68719 | 2986 |
by (simp add: list_all2_conv_all_nth) |
13863 | 2987 |
|
2988 |
lemma list_all2_update_cong: |
|
46669
c1d2ab32174a
one general list_all2_update_cong instead of two special ones
bulwahn
parents:
46664
diff
changeset
|
2989 |
"\<lbrakk> list_all2 P xs ys; P x y \<rbrakk> \<Longrightarrow> list_all2 P (xs[i:=x]) (ys[i:=y])" |
68719 | 2990 |
by (cases "i < length ys") (auto simp add: list_all2_conv_all_nth nth_list_update) |
13863 | 2991 |
|
14302 | 2992 |
lemma list_all2_takeI [simp,intro?]: |
24526 | 2993 |
"list_all2 P xs ys \<Longrightarrow> list_all2 P (take n xs) (take n ys)" |
68709 | 2994 |
proof (induct xs arbitrary: n ys) |
2995 |
case (Cons x xs) |
|
2996 |
then show ?case |
|
2997 |
by (cases n) (auto simp: list_all2_Cons1) |
|
2998 |
qed auto |
|
14302 | 2999 |
|
3000 |
lemma list_all2_dropI [simp,intro?]: |
|
68709 | 3001 |
"list_all2 P xs ys \<Longrightarrow> list_all2 P (drop n xs) (drop n ys)" |
3002 |
proof (induct xs arbitrary: n ys) |
|
3003 |
case (Cons x xs) |
|
3004 |
then show ?case |
|
3005 |
by (cases n) (auto simp: list_all2_Cons1) |
|
3006 |
qed auto |
|
13863 | 3007 |
|
14327 | 3008 |
lemma list_all2_mono [intro?]: |
24526 | 3009 |
"list_all2 P xs ys \<Longrightarrow> (\<And>xs ys. P xs ys \<Longrightarrow> Q xs ys) \<Longrightarrow> list_all2 Q xs ys" |
68709 | 3010 |
by (rule list.rel_mono_strong) |
13863 | 3011 |
|
22551 | 3012 |
lemma list_all2_eq: |
67399 | 3013 |
"xs = ys \<longleftrightarrow> list_all2 (=) xs ys" |
68719 | 3014 |
by (induct xs ys rule: list_induct2') auto |
22551 | 3015 |
|
40230 | 3016 |
lemma list_eq_iff_zip_eq: |
3017 |
"xs = ys \<longleftrightarrow> length xs = length ys \<and> (\<forall>(x,y) \<in> set (zip xs ys). x = y)" |
|
68719 | 3018 |
by(auto simp add: set_zip list_all2_eq list_all2_conv_all_nth cong: conj_cong) |
40230 | 3019 |
|
57308 | 3020 |
lemma list_all2_same: "list_all2 P xs xs \<longleftrightarrow> (\<forall>x\<in>set xs. P x x)" |
68719 | 3021 |
by(auto simp add: list_all2_conv_all_nth set_conv_nth) |
13142 | 3022 |
|
61630 | 3023 |
lemma zip_assoc: |
3024 |
"zip xs (zip ys zs) = map (\<lambda>((x, y), z). (x, y, z)) (zip (zip xs ys) zs)" |
|
68719 | 3025 |
by(rule list_all2_all_nthI[where P="(=)", unfolded list.rel_eq]) simp_all |
61630 | 3026 |
|
3027 |
lemma zip_commute: "zip xs ys = map (\<lambda>(x, y). (y, x)) (zip ys xs)" |
|
68719 | 3028 |
by(rule list_all2_all_nthI[where P="(=)", unfolded list.rel_eq]) simp_all |
61630 | 3029 |
|
3030 |
lemma zip_left_commute: |
|
3031 |
"zip xs (zip ys zs) = map (\<lambda>(y, (x, z)). (x, y, z)) (zip ys (zip xs zs))" |
|
68719 | 3032 |
by(rule list_all2_all_nthI[where P="(=)", unfolded list.rel_eq]) simp_all |
61630 | 3033 |
|
3034 |
lemma zip_replicate2: "zip xs (replicate n y) = map (\<lambda>x. (x, y)) (take n xs)" |
|
68719 | 3035 |
by(subst zip_commute)(simp add: zip_replicate1) |
61630 | 3036 |
|
69593 | 3037 |
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
|
3038 |
|
63720 | 3039 |
lemma product_concat_map: |
3040 |
"List.product xs ys = concat (map (\<lambda>x. map (\<lambda>y. (x,y)) ys) xs)" |
|
68719 | 3041 |
by(induction xs) (simp)+ |
63720 | 3042 |
|
58807 | 3043 |
lemma set_product[simp]: "set (List.product xs ys) = set xs \<times> set ys" |
68719 | 3044 |
by (induct xs) auto |
49948
744934b818c7
moved quite generic material from theory Enum to more appropriate places
haftmann
parents:
49808
diff
changeset
|
3045 |
|
51160
599ff65b85e2
systematic conversions between nat and nibble/char;
haftmann
parents:
51112
diff
changeset
|
3046 |
lemma length_product [simp]: |
599ff65b85e2
systematic conversions between nat and nibble/char;
haftmann
parents:
51112
diff
changeset
|
3047 |
"length (List.product xs ys) = length xs * length ys" |
68719 | 3048 |
by (induct xs) simp_all |
51160
599ff65b85e2
systematic conversions between nat and nibble/char;
haftmann
parents:
51112
diff
changeset
|
3049 |
|
599ff65b85e2
systematic conversions between nat and nibble/char;
haftmann
parents:
51112
diff
changeset
|
3050 |
lemma product_nth: |
599ff65b85e2
systematic conversions between nat and nibble/char;
haftmann
parents:
51112
diff
changeset
|
3051 |
assumes "n < length xs * length ys" |
599ff65b85e2
systematic conversions between nat and nibble/char;
haftmann
parents:
51112
diff
changeset
|
3052 |
shows "List.product xs ys ! n = (xs ! (n div length ys), ys ! (n mod length ys))" |
68719 | 3053 |
using assms proof (induct xs arbitrary: n) |
51160
599ff65b85e2
systematic conversions between nat and nibble/char;
haftmann
parents:
51112
diff
changeset
|
3054 |
case Nil then show ?case by simp |
599ff65b85e2
systematic conversions between nat and nibble/char;
haftmann
parents:
51112
diff
changeset
|
3055 |
next |
599ff65b85e2
systematic conversions between nat and nibble/char;
haftmann
parents:
51112
diff
changeset
|
3056 |
case (Cons x xs n) |
599ff65b85e2
systematic conversions between nat and nibble/char;
haftmann
parents:
51112
diff
changeset
|
3057 |
then have "length ys > 0" by auto |
599ff65b85e2
systematic conversions between nat and nibble/char;
haftmann
parents:
51112
diff
changeset
|
3058 |
with Cons show ?case |
599ff65b85e2
systematic conversions between nat and nibble/char;
haftmann
parents:
51112
diff
changeset
|
3059 |
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
|
3060 |
qed |
599ff65b85e2
systematic conversions between nat and nibble/char;
haftmann
parents:
51112
diff
changeset
|
3061 |
|
64963 | 3062 |
lemma in_set_product_lists_length: |
53721
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
traytel
parents:
53689
diff
changeset
|
3063 |
"xs \<in> set (product_lists xss) \<Longrightarrow> length xs = length xss" |
68719 | 3064 |
by (induct xss arbitrary: xs) auto |
53721
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
traytel
parents:
53689
diff
changeset
|
3065 |
|
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
traytel
parents:
53689
diff
changeset
|
3066 |
lemma product_lists_set: |
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
traytel
parents:
53689
diff
changeset
|
3067 |
"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
|
3068 |
proof (intro equalityI subsetI, unfold mem_Collect_eq) |
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
traytel
parents:
53689
diff
changeset
|
3069 |
fix xs assume "xs \<in> ?L" |
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
traytel
parents:
53689
diff
changeset
|
3070 |
then have "length xs = length xss" by (rule in_set_product_lists_length) |
60758 | 3071 |
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
|
3072 |
next |
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
traytel
parents:
53689
diff
changeset
|
3073 |
fix xs assume "?R xs" |
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
traytel
parents:
53689
diff
changeset
|
3074 |
then show "xs \<in> ?L" by induct auto |
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
traytel
parents:
53689
diff
changeset
|
3075 |
qed |
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
traytel
parents:
53689
diff
changeset
|
3076 |
|
49948
744934b818c7
moved quite generic material from theory Enum to more appropriate places
haftmann
parents:
49808
diff
changeset
|
3077 |
|
69593 | 3078 |
subsubsection \<open>\<^const>\<open>fold\<close> with natural argument order\<close> |
60758 | 3079 |
|
61799 | 3080 |
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
|
3081 |
"fold f [] s = s" |
64963 | 3082 |
"fold f (x # xs) s = fold f xs (f x s)" |
68719 | 3083 |
by simp_all |
48828
441a4eed7823
prefer eta-expanded code equations for fold, to accomodate tail recursion optimisation in Scala
haftmann
parents:
48619
diff
changeset
|
3084 |
|
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
|
3085 |
lemma fold_remove1_split: |
58807 | 3086 |
"\<lbrakk> \<And>x y. x \<in> set xs \<Longrightarrow> y \<in> set xs \<Longrightarrow> f x \<circ> f y = f y \<circ> f x; |
3087 |
x \<in> set xs \<rbrakk> |
|
3088 |
\<Longrightarrow> fold f xs = fold f (remove1 x xs) \<circ> f x" |
|
68719 | 3089 |
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
|
3090 |
|
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 |
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
|
3092 |
"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
|
3093 |
\<Longrightarrow> fold f xs a = fold g ys b" |
68719 | 3094 |
by (induct ys arbitrary: a b xs) simp_all |
58807 | 3095 |
|
3096 |
lemma fold_id: "(\<And>x. x \<in> set xs \<Longrightarrow> f x = id) \<Longrightarrow> fold f xs = id" |
|
68719 | 3097 |
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
|
3098 |
|
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
haftmann
parents:
46125
diff
changeset
|
3099 |
lemma fold_commute: |
58807 | 3100 |
"(\<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 | 3101 |
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
|
3102 |
|
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 |
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
|
3104 |
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
|
3105 |
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
|
3106 |
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
|
3107 |
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
|
3108 |
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
|
3109 |
qed |
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
3110 |
|
64963 | 3111 |
lemma fold_invariant: |
58807 | 3112 |
"\<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> |
3113 |
\<Longrightarrow> P (fold f xs s)" |
|
68719 | 3114 |
by (induct xs arbitrary: s) simp_all |
58807 | 3115 |
|
3116 |
lemma fold_append [simp]: "fold f (xs @ ys) = fold f ys \<circ> fold f xs" |
|
68719 | 3117 |
by (induct xs) simp_all |
58807 | 3118 |
|
67091 | 3119 |
lemma fold_map [code_unfold]: "fold g (map f xs) = fold (g \<circ> f) xs" |
68719 | 3120 |
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
|
3121 |
|
58437 | 3122 |
lemma fold_filter: |
3123 |
"fold f (filter P xs) = fold (\<lambda>x. if P x then f x else id) xs" |
|
68719 | 3124 |
by (induct xs) simp_all |
58437 | 3125 |
|
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 |
lemma fold_rev: |
58807 | 3127 |
"(\<And>x y. x \<in> set xs \<Longrightarrow> y \<in> set xs \<Longrightarrow> f y \<circ> f x = f x \<circ> f y) |
3128 |
\<Longrightarrow> fold f (rev xs) = fold f xs" |
|
68719 | 3129 |
by (induct xs) (simp_all add: fold_commute_apply fun_eq_iff) |
58807 | 3130 |
|
3131 |
lemma fold_Cons_rev: "fold Cons xs = append (rev xs)" |
|
68719 | 3132 |
by (induct xs) simp_all |
58807 | 3133 |
|
3134 |
lemma rev_conv_fold [code]: "rev xs = fold Cons xs []" |
|
68719 | 3135 |
by (simp add: fold_Cons_rev) |
58807 | 3136 |
|
3137 |
lemma fold_append_concat_rev: "fold append xss = append (concat (rev xss))" |
|
68719 | 3138 |
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
|
3139 |
|
69593 | 3140 |
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
|
3141 |
|
73832 | 3142 |
lemma (in comp_fun_commute_on) fold_set_fold_remdups: |
3143 |
assumes "set xs \<subseteq> S" |
|
3144 |
shows "Finite_Set.fold f y (set xs) = fold f (remdups xs) y" |
|
3145 |
by (rule sym, use assms in \<open>induct xs arbitrary: y\<close>) |
|
74424 | 3146 |
(simp_all add: insert_absorb fold_fun_left_comm) |
73832 | 3147 |
|
3148 |
lemma (in comp_fun_idem_on) fold_set_fold: |
|
3149 |
assumes "set xs \<subseteq> S" |
|
3150 |
shows "Finite_Set.fold f y (set xs) = fold f xs y" |
|
3151 |
by (rule sym, use assms in \<open>induct xs arbitrary: y\<close>) (simp_all add: fold_fun_left_comm) |
|
58807 | 3152 |
|
3153 |
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
|
3154 |
proof - |
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
haftmann
parents:
46143
diff
changeset
|
3155 |
interpret comp_fun_idem Set.insert |
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
haftmann
parents:
46143
diff
changeset
|
3156 |
by (fact comp_fun_idem_insert) |
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
haftmann
parents:
46143
diff
changeset
|
3157 |
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
|
3158 |
qed |
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
haftmann
parents:
46143
diff
changeset
|
3159 |
|
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
|
3160 |
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
|
3161 |
"List.coset xs \<union> A = List.coset (List.filter (\<lambda>x. x \<notin> A) xs)" |
68719 | 3162 |
by auto |
58807 | 3163 |
|
3164 |
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
|
3165 |
proof - |
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
haftmann
parents:
46143
diff
changeset
|
3166 |
interpret comp_fun_idem Set.remove |
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
haftmann
parents:
46143
diff
changeset
|
3167 |
by (fact comp_fun_idem_remove) |
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
haftmann
parents:
46143
diff
changeset
|
3168 |
show ?thesis |
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
haftmann
parents:
46143
diff
changeset
|
3169 |
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
|
3170 |
qed |
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
haftmann
parents:
46143
diff
changeset
|
3171 |
|
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
|
3172 |
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
|
3173 |
"A - List.coset xs = set (List.filter (\<lambda>x. x \<in> A) xs)" |
68719 | 3174 |
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
|
3175 |
|
d654c73e4b12
no preference wrt. fold(l/r); prefer fold rather than foldr for iterating over lists in generated code
haftmann
parents:
47131
diff
changeset
|
3176 |
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
|
3177 |
"A \<inter> set xs = set (List.filter (\<lambda>x. x \<in> A) xs)" |
68719 | 3178 |
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
|
3179 |
|
d654c73e4b12
no preference wrt. fold(l/r); prefer fold rather than foldr for iterating over lists in generated code
haftmann
parents:
47131
diff
changeset
|
3180 |
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
|
3181 |
"A \<inter> List.coset xs = fold Set.remove xs A" |
68719 | 3182 |
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
|
3183 |
|
54885 | 3184 |
lemma (in semilattice_set) set_eq_fold [code]: |
51489 | 3185 |
"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
|
3186 |
proof - |
51489 | 3187 |
interpret comp_fun_idem f |
61169 | 3188 |
by standard (simp_all add: fun_eq_iff left_commute) |
51489 | 3189 |
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
|
3190 |
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
|
3191 |
|
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
haftmann
parents:
46125
diff
changeset
|
3192 |
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
|
3193 |
"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
|
3194 |
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
|
3195 |
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
|
3196 |
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
|
3197 |
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
|
3198 |
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
|
3199 |
|
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
|
3200 |
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
|
3201 |
|
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
|
3202 |
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
|
3203 |
"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
|
3204 |
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
|
3205 |
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
|
3206 |
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
|
3207 |
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
|
3208 |
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
|
3209 |
|
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
|
3210 |
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
|
3211 |
|
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
|
3212 |
lemma (in complete_lattice) INF_set_fold: |
69275 | 3213 |
"\<Sqinter>(f ` set xs) = fold (inf \<circ> f) xs top" |
68781 | 3214 |
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
|
3215 |
|
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
haftmann
parents:
46125
diff
changeset
|
3216 |
lemma (in complete_lattice) SUP_set_fold: |
69275 | 3217 |
"\<Squnion>(f ` set xs) = fold (sup \<circ> f) xs bot" |
68781 | 3218 |
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
|
3219 |
|
49948
744934b818c7
moved quite generic material from theory Enum to more appropriate places
haftmann
parents:
49808
diff
changeset
|
3220 |
|
69593 | 3221 |
subsubsection \<open>Fold variants: \<^const>\<open>foldr\<close> and \<^const>\<open>foldl\<close>\<close> |
60758 | 3222 |
|
3223 |
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
|
3224 |
|
58807 | 3225 |
lemma foldr_conv_fold [code_abbrev]: "foldr f xs = fold f (rev xs)" |
68719 | 3226 |
by (induct xs) simp_all |
58807 | 3227 |
|
3228 |
lemma foldl_conv_fold: "foldl f s xs = fold (\<lambda>x s. f s x) xs s" |
|
68719 | 3229 |
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
|
3230 |
|
61799 | 3231 |
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
|
3232 |
"foldr f xs a = foldl (\<lambda>x y. f y x) a (rev xs)" |
68719 | 3233 |
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
|
3234 |
|
d654c73e4b12
no preference wrt. fold(l/r); prefer fold rather than foldr for iterating over lists in generated code
haftmann
parents:
47131
diff
changeset
|
3235 |
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
|
3236 |
"foldl f a xs = foldr (\<lambda>x y. f y x) (rev xs) a" |
68719 | 3237 |
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
|
3238 |
|
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
haftmann
parents:
46125
diff
changeset
|
3239 |
lemma foldr_fold: |
58807 | 3240 |
"(\<And>x y. x \<in> set xs \<Longrightarrow> y \<in> set xs \<Longrightarrow> f y \<circ> f x = f x \<circ> f y) |
3241 |
\<Longrightarrow> foldr f xs = fold f xs" |
|
68719 | 3242 |
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
|
3243 |
|
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
haftmann
parents:
46125
diff
changeset
|
3244 |
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
|
3245 |
"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 | 3246 |
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
|
3247 |
|
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
haftmann
parents:
46125
diff
changeset
|
3248 |
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
|
3249 |
"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 | 3250 |
by (auto simp add: foldl_conv_fold intro!: fold_cong) |
58807 | 3251 |
|
3252 |
lemma foldr_append [simp]: "foldr f (xs @ ys) a = foldr f xs (foldr f ys a)" |
|
68719 | 3253 |
by (simp add: foldr_conv_fold) |
58807 | 3254 |
|
3255 |
lemma foldl_append [simp]: "foldl f a (xs @ ys) = foldl f (foldl f a xs) ys" |
|
68719 | 3256 |
by (simp add: foldl_conv_fold) |
58807 | 3257 |
|
67091 | 3258 |
lemma foldr_map [code_unfold]: "foldr g (map f xs) a = foldr (g \<circ> f) xs a" |
68719 | 3259 |
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
|
3260 |
|
58437 | 3261 |
lemma foldr_filter: |
3262 |
"foldr f (filter P xs) = foldr (\<lambda>x. if P x then f x else id) xs" |
|
68719 | 3263 |
by (simp add: foldr_conv_fold rev_filter fold_filter) |
64963 | 3264 |
|
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
|
3265 |
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
|
3266 |
"foldl g a (map f xs) = foldl (\<lambda>a x. g a (f x)) a xs" |
68719 | 3267 |
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
|
3268 |
|
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
haftmann
parents:
46125
diff
changeset
|
3269 |
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
|
3270 |
"concat xss = foldr append xss []" |
68719 | 3271 |
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
|
3272 |
|
35115 | 3273 |
|
69593 | 3274 |
subsubsection \<open>\<^const>\<open>upt\<close>\<close> |
13114 | 3275 |
|
17090 | 3276 |
lemma upt_rec[code]: "[i..<j] = (if i<j then i#[Suc i..<j] else [])" |
68719 | 3277 |
\<comment> \<open>simp does not terminate!\<close> |
3278 |
by (induct j) auto |
|
13142 | 3279 |
|
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46898
diff
changeset
|
3280 |
lemmas upt_rec_numeral[simp] = upt_rec[of "numeral m" "numeral n"] for m n |
32005 | 3281 |
|
71585 | 3282 |
lemma upt_conv_Nil [simp]: "j \<le> i \<Longrightarrow> [i..<j] = []" |
68719 | 3283 |
by (subst upt_rec) simp |
13114 | 3284 |
|
68709 | 3285 |
lemma upt_eq_Nil_conv[simp]: "([i..<j] = []) = (j = 0 \<or> j \<le> i)" |
68719 | 3286 |
by(induct j)simp_all |
15281 | 3287 |
|
3288 |
lemma upt_eq_Cons_conv: |
|
68719 | 3289 |
"([i..<j] = x#xs) = (i < j \<and> i = x \<and> [i+1..<j] = xs)" |
68709 | 3290 |
proof (induct j arbitrary: x xs) |
3291 |
case (Suc j) |
|
3292 |
then show ?case |
|
3293 |
by (simp add: upt_rec) |
|
3294 |
qed simp |
|
3295 |
||
71585 | 3296 |
lemma upt_Suc_append: "i \<le> j \<Longrightarrow> [i..<(Suc j)] = [i..<j]@[j]" |
68719 | 3297 |
\<comment> \<open>Only needed if \<open>upt_Suc\<close> is deleted from the simpset.\<close> |
3298 |
by simp |
|
13114 | 3299 |
|
71585 | 3300 |
lemma upt_conv_Cons: "i < j \<Longrightarrow> [i..<j] = i # [Suc i..<j]" |
68719 | 3301 |
by (simp add: upt_rec) |
13114 | 3302 |
|
63145 | 3303 |
lemma upt_conv_Cons_Cons: \<comment> \<open>no precondition\<close> |
62580 | 3304 |
"m # n # ns = [m..<q] \<longleftrightarrow> n # ns = [Suc m..<q]" |
3305 |
proof (cases "m < q") |
|
3306 |
case False then show ?thesis by simp |
|
3307 |
next |
|
3308 |
case True then show ?thesis by (simp add: upt_conv_Cons) |
|
3309 |
qed |
|
3310 |
||
71585 | 3311 |
lemma upt_add_eq_append: "i<=j \<Longrightarrow> [i..<j+k] = [i..<j]@[j..<j+k]" |
68719 | 3312 |
\<comment> \<open>LOOPS as a simprule, since \<open>j \<le> j\<close>.\<close> |
3313 |
by (induct k) auto |
|
13114 | 3314 |
|
15425 | 3315 |
lemma length_upt [simp]: "length [i..<j] = j - i" |
68719 | 3316 |
by (induct j) (auto simp add: Suc_diff_le) |
13114 | 3317 |
|
71585 | 3318 |
lemma nth_upt [simp]: "i + k < j \<Longrightarrow> [i..<j] ! k = i + k" |
68719 | 3319 |
by (induct j) (auto simp add: less_Suc_eq nth_append split: nat_diff_split) |
17906 | 3320 |
|
3321 |
lemma hd_upt[simp]: "i < j \<Longrightarrow> hd[i..<j] = i" |
|
68719 | 3322 |
by(simp add:upt_conv_Cons) |
17906 | 3323 |
|
70183
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
69850
diff
changeset
|
3324 |
lemma tl_upt [simp]: "tl [m..<n] = [Suc m..<n]" |
68719 | 3325 |
by (simp add: upt_rec) |
63317
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
eberlm
parents:
63173
diff
changeset
|
3326 |
|
17906 | 3327 |
lemma last_upt[simp]: "i < j \<Longrightarrow> last[i..<j] = j - 1" |
68719 | 3328 |
by(cases j)(auto simp: upt_Suc_append) |
17906 | 3329 |
|
71585 | 3330 |
lemma take_upt [simp]: "i+m \<le> n \<Longrightarrow> take m [i..<n] = [i..<i+m]" |
68709 | 3331 |
proof (induct m arbitrary: i) |
3332 |
case (Suc m) |
|
3333 |
then show ?case |
|
3334 |
by (subst take_Suc_conv_app_nth) auto |
|
3335 |
qed simp |
|
3507 | 3336 |
|
17501 | 3337 |
lemma drop_upt[simp]: "drop m [i..<j] = [i+m..<j]" |
68719 | 3338 |
by(induct j) auto |
17501 | 3339 |
|
24645 | 3340 |
lemma map_Suc_upt: "map Suc [m..<n] = [Suc m..<Suc n]" |
68719 | 3341 |
by (induct n) auto |
13114 | 3342 |
|
54496
178922b63b58
add lemmas Suc_funpow and id_funpow to simpset; add lemma map_add_upt
hoelzl
parents:
54404
diff
changeset
|
3343 |
lemma map_add_upt: "map (\<lambda>i. i + n) [0..<m] = [n..<m + n]" |
68719 | 3344 |
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
|
3345 |
|
71585 | 3346 |
lemma nth_map_upt: "i < n-m \<Longrightarrow> (map f [m..<n]) ! i = f(m+i)" |
68709 | 3347 |
proof (induct n m arbitrary: i rule: diff_induct) |
68719 | 3348 |
case (3 x y) |
68709 | 3349 |
then show ?case |
3350 |
by (metis add.commute length_upt less_diff_conv nth_map nth_upt) |
|
3351 |
qed auto |
|
13114 | 3352 |
|
58807 | 3353 |
lemma map_decr_upt: "map (\<lambda>n. n - Suc 0) [Suc m..<Suc n] = [m..<n]" |
68719 | 3354 |
by (induct n) simp_all |
66550
e5d82cf3c387
Some small lemmas about polynomials and FPSs
eberlm <eberlm@in.tum.de>
parents:
66502
diff
changeset
|
3355 |
|
e5d82cf3c387
Some small lemmas about polynomials and FPSs
eberlm <eberlm@in.tum.de>
parents:
66502
diff
changeset
|
3356 |
lemma map_upt_Suc: "map f [0 ..< Suc n] = f 0 # map (\<lambda>i. f (Suc i)) [0 ..< n]" |
68719 | 3357 |
by (induct n arbitrary: f) auto |
52380 | 3358 |
|
13883
0451e0fb3f22
Re-structured some proofs in order to get rid of rule_format attribute.
berghofe
parents:
13863
diff
changeset
|
3359 |
lemma nth_take_lemma: |
67091 | 3360 |
"k \<le> length xs \<Longrightarrow> k \<le> length ys \<Longrightarrow> |
3361 |
(\<And>i. i < k \<longrightarrow> xs!i = ys!i) \<Longrightarrow> take k xs = take k ys" |
|
68709 | 3362 |
proof (induct k arbitrary: xs ys) |
3363 |
case (Suc k) |
|
3364 |
then show ?case |
|
3365 |
apply (simp add: less_Suc_eq_0_disj) |
|
3366 |
by (simp add: Suc.prems(3) take_Suc_conv_app_nth) |
|
3367 |
qed simp |
|
13114 | 3368 |
|
3369 |
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
|
3370 |
"\<lbrakk>length xs = length ys; \<And>i. i < length xs \<Longrightarrow> xs!i = ys!i\<rbrakk> \<Longrightarrow> xs = ys" |
68719 | 3371 |
by (frule nth_take_lemma [OF le_refl eq_imp_le]) simp_all |
13142 | 3372 |
|
24796 | 3373 |
lemma map_nth: |
3374 |
"map (\<lambda>i. xs ! i) [0..<length xs] = xs" |
|
68719 | 3375 |
by (rule nth_equalityI, auto) |
58807 | 3376 |
|
13863 | 3377 |
lemma list_all2_antisym: |
64963 | 3378 |
"\<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 | 3379 |
\<Longrightarrow> xs = ys" |
68719 | 3380 |
by (simp add: list_all2_conv_all_nth nth_equalityI) |
13863 | 3381 |
|
71585 | 3382 |
lemma take_equalityI: "(\<forall>i. take i xs = take i ys) \<Longrightarrow> xs = ys" |
61799 | 3383 |
\<comment> \<open>The famous take-lemma.\<close> |
68719 | 3384 |
by (metis length_take min.commute order_refl take_all) |
13142 | 3385 |
|
15302 | 3386 |
lemma take_Cons': |
58807 | 3387 |
"take n (x # xs) = (if n = 0 then [] else x # take (n - 1) xs)" |
15302 | 3388 |
by (cases n) simp_all |
3389 |
||
3390 |
lemma drop_Cons': |
|
58807 | 3391 |
"drop n (x # xs) = (if n = 0 then x # xs else drop (n - 1) xs)" |
15302 | 3392 |
by (cases n) simp_all |
3393 |
||
3394 |
lemma nth_Cons': "(x # xs)!n = (if n = 0 then x else xs!(n - 1))" |
|
3395 |
by (cases n) simp_all |
|
3396 |
||
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46898
diff
changeset
|
3397 |
lemma take_Cons_numeral [simp]: |
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46898
diff
changeset
|
3398 |
"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
|
3399 |
by (simp add: take_Cons') |
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46898
diff
changeset
|
3400 |
|
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46898
diff
changeset
|
3401 |
lemma drop_Cons_numeral [simp]: |
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46898
diff
changeset
|
3402 |
"drop (numeral v) (x # xs) = drop (numeral v - 1) xs" |
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46898
diff
changeset
|
3403 |
by (simp add: drop_Cons') |
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46898
diff
changeset
|
3404 |
|
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46898
diff
changeset
|
3405 |
lemma nth_Cons_numeral [simp]: |
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46898
diff
changeset
|
3406 |
"(x # xs) ! numeral v = xs ! (numeral v - 1)" |
73301 | 3407 |
by (simp add: nth_Cons') |
3408 |
||
3409 |
lemma map_upt_eqI: |
|
3410 |
\<open>map f [m..<n] = xs\<close> if \<open>length xs = n - m\<close> |
|
3411 |
\<open>\<And>i. i < length xs \<Longrightarrow> xs ! i = f (m + i)\<close> |
|
3412 |
proof (rule nth_equalityI) |
|
3413 |
from \<open>length xs = n - m\<close> show \<open>length (map f [m..<n]) = length xs\<close> |
|
3414 |
by simp |
|
3415 |
next |
|
3416 |
fix i |
|
3417 |
assume \<open>i < length (map f [m..<n])\<close> |
|
3418 |
then have \<open>i < n - m\<close> |
|
3419 |
by simp |
|
3420 |
with that have \<open>xs ! i = f (m + i)\<close> |
|
3421 |
by simp |
|
3422 |
with \<open>i < n - m\<close> show \<open>map f [m..<n] ! i = xs ! i\<close> |
|
3423 |
by simp |
|
3424 |
qed |
|
15302 | 3425 |
|
3426 |
||
69593 | 3427 |
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
|
3428 |
|
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
|
3429 |
function upto :: "int \<Rightarrow> int \<Rightarrow> int list" ("(1[_../_])") where |
51166 | 3430 |
"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
|
3431 |
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
|
3432 |
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
|
3433 |
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
|
3434 |
|
51166 | 3435 |
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
|
3436 |
|
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46898
diff
changeset
|
3437 |
lemmas upto_rec_numeral [simp] = |
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46898
diff
changeset
|
3438 |
upto.simps[of "numeral m" "numeral n"] |
54489
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents:
54404
diff
changeset
|
3439 |
upto.simps[of "numeral m" "- numeral n"] |
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents:
54404
diff
changeset
|
3440 |
upto.simps[of "- numeral m" "numeral n"] |
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents:
54404
diff
changeset
|
3441 |
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
|
3442 |
|
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 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
|
3444 |
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
|
3445 |
|
67942 | 3446 |
lemma upto_single[simp]: "[i..i] = [i]" |
3447 |
by(simp add: upto.simps) |
|
3448 |
||
67124 | 3449 |
lemma upto_Nil[simp]: "[i..j] = [] \<longleftrightarrow> j < i" |
3450 |
by (simp add: upto.simps) |
|
3451 |
||
74741
6e1fad4f602b
added eq_iff_swap for creating symmetric variants of thms; applied it in List.
nipkow
parents:
74424
diff
changeset
|
3452 |
lemmas upto_Nil2[simp] = upto_Nil[THEN eq_iff_swap] |
67168 | 3453 |
|
51166 | 3454 |
lemma upto_rec1: "i \<le> j \<Longrightarrow> [i..j] = i#[i+1..j]" |
3455 |
by(simp add: upto.simps) |
|
3456 |
||
3457 |
lemma upto_rec2: "i \<le> j \<Longrightarrow> [i..j] = [i..j - 1]@[j]" |
|
3458 |
proof(induct "nat(j-i)" arbitrary: i j) |
|
3459 |
case 0 thus ?case by(simp add: upto.simps) |
|
3460 |
next |
|
3461 |
case (Suc n) |
|
3462 |
hence "n = nat (j - (i + 1))" "i < j" by linarith+ |
|
3463 |
from this(2) Suc.hyps(1)[OF this(1)] Suc(2,3) upto_rec1 show ?case by simp |
|
3464 |
qed |
|
3465 |
||
67949 | 3466 |
lemma length_upto[simp]: "length [i..j] = nat(j - i + 1)" |
3467 |
by(induction i j rule: upto.induct) (auto simp: upto.simps) |
|
3468 |
||
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
|
3469 |
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
|
3470 |
proof(induct i j rule:upto.induct) |
edbf0a86fb1c
adding simproc to rewrite list comprehensions to set comprehensions; adopting proofs
bulwahn
parents:
41372
diff
changeset
|
3471 |
case (1 i j) |
edbf0a86fb1c
adding simproc to rewrite list comprehensions to set comprehensions; adopting proofs
bulwahn
parents:
41372
diff
changeset
|
3472 |
from this show ?case |
55811 | 3473 |
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
|
3474 |
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
|
3475 |
|
69125 | 3476 |
lemma nth_upto[simp]: "i + int k \<le> j \<Longrightarrow> [i..j] ! k = i + int k" |
71585 | 3477 |
proof(induction i j arbitrary: k rule: upto.induct) |
3478 |
case (1 i j) |
|
3479 |
then show ?case |
|
3480 |
by (auto simp add: upto_rec1 [of i j] nth_Cons') |
|
3481 |
qed |
|
67949 | 3482 |
|
71393
fce780f9c9c6
new examples of BNF lifting across quotients using a new theory of confluence,
traytel
parents:
70911
diff
changeset
|
3483 |
lemma upto_split1: |
67081 | 3484 |
"i \<le> j \<Longrightarrow> j \<le> k \<Longrightarrow> [i..k] = [i..j-1] @ [j..k]" |
3485 |
proof (induction j rule: int_ge_induct) |
|
3486 |
case base thus ?case by (simp add: upto_rec1) |
|
3487 |
next |
|
3488 |
case step thus ?case using upto_rec1 upto_rec2 by simp |
|
3489 |
qed |
|
3490 |
||
71393
fce780f9c9c6
new examples of BNF lifting across quotients using a new theory of confluence,
traytel
parents:
70911
diff
changeset
|
3491 |
lemma upto_split2: |
67081 | 3492 |
"i \<le> j \<Longrightarrow> j \<le> k \<Longrightarrow> [i..k] = [i..j] @ [j+1..k]" |
3493 |
using upto_rec1 upto_rec2 upto_split1 by auto |
|
3494 |
||
67124 | 3495 |
lemma upto_split3: "\<lbrakk> i \<le> j; j \<le> k \<rbrakk> \<Longrightarrow> [i..k] = [i..j-1] @ j # [j+1..k]" |
3496 |
using upto_rec1 upto_split1 by auto |
|
3497 |
||
60758 | 3498 |
text\<open>Tail recursive version for code generation:\<close> |
51166 | 3499 |
|
51170 | 3500 |
definition upto_aux :: "int \<Rightarrow> int \<Rightarrow> int list \<Rightarrow> int list" where |
3501 |
"upto_aux i j js = [i..j] @ js" |
|
3502 |
||
3503 |
lemma upto_aux_rec [code]: |
|
51166 | 3504 |
"upto_aux i j js = (if j<i then js else upto_aux i (j - 1) (j#js))" |
58807 | 3505 |
by (simp add: upto_aux_def upto_rec2) |
51166 | 3506 |
|
3507 |
lemma upto_code[code]: "[i..j] = upto_aux i j []" |
|
51170 | 3508 |
by(simp add: upto_aux_def) |
51166 | 3509 |
|
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
|
3510 |
|
71779 | 3511 |
|
3512 |
subsubsection \<open>\<^const>\<open>successively\<close>\<close> |
|
3513 |
||
3514 |
lemma successively_Cons: |
|
3515 |
"successively P (x # xs) \<longleftrightarrow> xs = [] \<or> P x (hd xs) \<and> successively P xs" |
|
3516 |
by (cases xs) auto |
|
3517 |
||
3518 |
lemma successively_cong [cong]: |
|
3519 |
assumes "\<And>x y. x \<in> set xs \<Longrightarrow> y \<in> set xs \<Longrightarrow> P x y \<longleftrightarrow> Q x y" "xs = ys" |
|
3520 |
shows "successively P xs \<longleftrightarrow> successively Q ys" |
|
3521 |
unfolding assms(2) [symmetric] using assms(1) |
|
3522 |
by (induction xs) (auto simp: successively_Cons) |
|
3523 |
||
3524 |
||
3525 |
lemma successively_append_iff: |
|
3526 |
"successively P (xs @ ys) \<longleftrightarrow> |
|
74424 | 3527 |
successively P xs \<and> successively P ys \<and> |
71779 | 3528 |
(xs = [] \<or> ys = [] \<or> P (last xs) (hd ys))" |
3529 |
by (induction xs) (auto simp: successively_Cons) |
|
3530 |
||
3531 |
lemma successively_if_sorted_wrt: "sorted_wrt P xs \<Longrightarrow> successively P xs" |
|
3532 |
by (induction xs rule: induct_list012) auto |
|
3533 |
||
3534 |
||
3535 |
lemma successively_iff_sorted_wrt_strong: |
|
3536 |
assumes "\<And>x y z. x \<in> set xs \<Longrightarrow> y \<in> set xs \<Longrightarrow> z \<in> set xs \<Longrightarrow> |
|
3537 |
P x y \<Longrightarrow> P y z \<Longrightarrow> P x z" |
|
3538 |
shows "successively P xs \<longleftrightarrow> sorted_wrt P xs" |
|
3539 |
proof |
|
3540 |
assume "successively P xs" |
|
3541 |
from this and assms show "sorted_wrt P xs" |
|
3542 |
proof (induction xs rule: induct_list012) |
|
3543 |
case (3 x y xs) |
|
3544 |
from "3.prems" have "P x y" |
|
3545 |
by auto |
|
3546 |
have IH: "sorted_wrt P (y # xs)" |
|
3547 |
using "3.prems" |
|
3548 |
by(intro "3.IH"(2) list.set_intros(2))(simp, blast intro: list.set_intros(2)) |
|
3549 |
have "P x z" if asm: "z \<in> set xs" for z |
|
3550 |
proof - |
|
3551 |
from IH and asm have "P y z" |
|
3552 |
by auto |
|
3553 |
with \<open>P x y\<close> show "P x z" |
|
3554 |
using "3.prems" asm by auto |
|
3555 |
qed |
|
3556 |
with IH and \<open>P x y\<close> show ?case by auto |
|
3557 |
qed auto |
|
3558 |
qed (use successively_if_sorted_wrt in blast) |
|
3559 |
||
3560 |
lemma successively_conv_sorted_wrt: |
|
3561 |
assumes "transp P" |
|
3562 |
shows "successively P xs \<longleftrightarrow> sorted_wrt P xs" |
|
3563 |
using assms unfolding transp_def |
|
3564 |
by (intro successively_iff_sorted_wrt_strong) blast |
|
3565 |
||
3566 |
lemma successively_rev [simp]: "successively P (rev xs) \<longleftrightarrow> successively (\<lambda>x y. P y x) xs" |
|
3567 |
by (induction xs rule: remdups_adj.induct) |
|
3568 |
(auto simp: successively_append_iff successively_Cons) |
|
3569 |
||
3570 |
lemma successively_map: "successively P (map f xs) \<longleftrightarrow> successively (\<lambda>x y. P (f x) (f y)) xs" |
|
3571 |
by (induction xs rule: induct_list012) auto |
|
3572 |
||
3573 |
lemma successively_mono: |
|
3574 |
assumes "successively P xs" |
|
3575 |
assumes "\<And>x y. x \<in> set xs \<Longrightarrow> y \<in> set xs \<Longrightarrow> P x y \<Longrightarrow> Q x y" |
|
3576 |
shows "successively Q xs" |
|
3577 |
using assms by (induction Q xs rule: successively.induct) auto |
|
3578 |
||
3579 |
lemma successively_altdef: |
|
3580 |
"successively = (\<lambda>P. rec_list True (\<lambda>x xs b. case xs of [] \<Rightarrow> True | y # _ \<Rightarrow> P x y \<and> b))" |
|
3581 |
proof (intro ext) |
|
3582 |
fix P and xs :: "'a list" |
|
3583 |
show "successively P xs = rec_list True (\<lambda>x xs b. case xs of [] \<Rightarrow> True | y # _ \<Rightarrow> P x y \<and> b) xs" |
|
3584 |
by (induction xs) (auto simp: successively_Cons split: list.splits) |
|
3585 |
qed |
|
3586 |
||
3587 |
||
69593 | 3588 |
subsubsection \<open>\<^const>\<open>distinct\<close> and \<^const>\<open>remdups\<close> and \<^const>\<open>remdups_adj\<close>\<close> |
13142 | 3589 |
|
58807 | 3590 |
lemma distinct_tl: "distinct xs \<Longrightarrow> distinct (tl xs)" |
3591 |
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
|
3592 |
|
13142 | 3593 |
lemma distinct_append [simp]: |
58807 | 3594 |
"distinct (xs @ ys) = (distinct xs \<and> distinct ys \<and> set xs \<inter> set ys = {})" |
13145 | 3595 |
by (induct xs) auto |
13142 | 3596 |
|
15305 | 3597 |
lemma distinct_rev[simp]: "distinct(rev xs) = distinct xs" |
3598 |
by(induct xs) auto |
|
3599 |
||
13142 | 3600 |
lemma set_remdups [simp]: "set (remdups xs) = set xs" |
13145 | 3601 |
by (induct xs) (auto simp add: insert_absorb) |
13142 | 3602 |
|
3603 |
lemma distinct_remdups [iff]: "distinct (remdups xs)" |
|
13145 | 3604 |
by (induct xs) auto |
13142 | 3605 |
|
71585 | 3606 |
lemma distinct_remdups_id: "distinct xs \<Longrightarrow> remdups xs = xs" |
25287 | 3607 |
by (induct xs, auto) |
3608 |
||
26734 | 3609 |
lemma remdups_id_iff_distinct [simp]: "remdups xs = xs \<longleftrightarrow> distinct xs" |
3610 |
by (metis distinct_remdups distinct_remdups_id) |
|
25287 | 3611 |
|
67091 | 3612 |
lemma finite_distinct_list: "finite A \<Longrightarrow> \<exists>xs. set xs = A \<and> distinct xs" |
24632 | 3613 |
by (metis distinct_remdups finite_list set_remdups) |
24566 | 3614 |
|
15072 | 3615 |
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
|
3616 |
by (induct x, auto) |
15072 | 3617 |
|
74741
6e1fad4f602b
added eq_iff_swap for creating symmetric variants of thms; applied it in List.
nipkow
parents:
74424
diff
changeset
|
3618 |
lemmas remdups_eq_nil_right_iff [simp] = remdups_eq_nil_iff[THEN eq_iff_swap] |
15072 | 3619 |
|
68709 | 3620 |
lemma length_remdups_leq[iff]: "length(remdups xs) \<le> length xs" |
15245 | 3621 |
by (induct xs) auto |
3622 |
||
3623 |
lemma length_remdups_eq[iff]: |
|
3624 |
"(length (remdups xs) = length xs) = (remdups xs = xs)" |
|
68719 | 3625 |
proof (induct xs) |
3626 |
case (Cons a xs) |
|
3627 |
then show ?case |
|
3628 |
by simp (metis Suc_n_not_le_n impossible_Cons length_remdups_leq) |
|
3629 |
qed auto |
|
15245 | 3630 |
|
33945 | 3631 |
lemma remdups_filter: "remdups(filter P xs) = filter P (remdups xs)" |
58807 | 3632 |
by (induct xs) auto |
18490 | 3633 |
|
3634 |
lemma distinct_map: |
|
67091 | 3635 |
"distinct(map f xs) = (distinct xs \<and> inj_on f (set xs))" |
18490 | 3636 |
by (induct xs) auto |
3637 |
||
58195 | 3638 |
lemma distinct_map_filter: |
3639 |
"distinct (map f xs) \<Longrightarrow> distinct (map f (filter P xs))" |
|
58807 | 3640 |
by (induct xs) auto |
58195 | 3641 |
|
71585 | 3642 |
lemma distinct_filter [simp]: "distinct xs \<Longrightarrow> distinct (filter P xs)" |
13145 | 3643 |
by (induct xs) auto |
13114 | 3644 |
|
17501 | 3645 |
lemma distinct_upt[simp]: "distinct[i..<j]" |
3646 |
by (induct j) auto |
|
3647 |
||
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
|
3648 |
lemma distinct_upto[simp]: "distinct[i..j]" |
71585 | 3649 |
proof (induction i j rule: upto.induct) |
3650 |
case (1 i j) |
|
3651 |
then show ?case |
|
3652 |
by (simp add: upto.simps [of i]) |
|
3653 |
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
|
3654 |
|
24526 | 3655 |
lemma distinct_take[simp]: "distinct xs \<Longrightarrow> distinct (take i xs)" |
68719 | 3656 |
proof (induct xs arbitrary: i) |
3657 |
case (Cons a xs) |
|
3658 |
then show ?case |
|
3659 |
by (metis Cons.prems append_take_drop_id distinct_append) |
|
3660 |
qed auto |
|
17501 | 3661 |
|
24526 | 3662 |
lemma distinct_drop[simp]: "distinct xs \<Longrightarrow> distinct (drop i xs)" |
68719 | 3663 |
proof (induct xs arbitrary: i) |
3664 |
case (Cons a xs) |
|
3665 |
then show ?case |
|
3666 |
by (metis Cons.prems append_take_drop_id distinct_append) |
|
3667 |
qed auto |
|
17501 | 3668 |
|
3669 |
lemma distinct_list_update: |
|
68719 | 3670 |
assumes d: "distinct xs" and a: "a \<notin> set xs - {xs!i}" |
3671 |
shows "distinct (xs[i:=a])" |
|
17501 | 3672 |
proof (cases "i < length xs") |
3673 |
case True |
|
68719 | 3674 |
with a have anot: "a \<notin> set (take i xs @ xs ! i # drop (Suc i) xs) - {xs!i}" |
3675 |
by simp (metis in_set_dropD in_set_takeD) |
|
3676 |
show ?thesis |
|
3677 |
proof (cases "a = xs!i") |
|
3678 |
case True |
|
3679 |
with d show ?thesis |
|
3680 |
by auto |
|
3681 |
next |
|
3682 |
case False |
|
71585 | 3683 |
have "set (take i xs) \<inter> set (drop (Suc i) xs) = {}" |
3684 |
by (metis True d disjoint_insert(1) distinct_append id_take_nth_drop list.set(2)) |
|
68719 | 3685 |
then show ?thesis |
71585 | 3686 |
using d False anot \<open>i < length xs\<close> by (simp add: upd_conv_take_nth_drop) |
68719 | 3687 |
qed |
17501 | 3688 |
next |
3689 |
case False with d show ?thesis by auto |
|
3690 |
qed |
|
3691 |
||
31363
7493b571b37d
Added theorems about distinct & concat, map & replicate and concat & replicate
hoelzl
parents:
31264
diff
changeset
|
3692 |
lemma distinct_concat: |
58807 | 3693 |
"\<lbrakk> distinct xs; |
3694 |
\<And> ys. ys \<in> set xs \<Longrightarrow> distinct ys; |
|
3695 |
\<And> ys zs. \<lbrakk> ys \<in> set xs ; zs \<in> set xs ; ys \<noteq> zs \<rbrakk> \<Longrightarrow> set ys \<inter> set zs = {} |
|
3696 |
\<rbrakk> \<Longrightarrow> distinct (concat xs)" |
|
3697 |
by (induct xs) auto |
|
17501 | 3698 |
|
71593 | 3699 |
text \<open>An iff-version of @{thm distinct_concat} is available further down as \<open>distinct_concat_iff\<close>.\<close> |
3700 |
||
3701 |
text \<open>It is best to avoid the following indexed version of distinct, but sometimes it is useful.\<close> |
|
17501 | 3702 |
|
68719 | 3703 |
lemma distinct_conv_nth: "distinct xs = (\<forall>i < size xs. \<forall>j < size xs. i \<noteq> j \<longrightarrow> xs!i \<noteq> xs!j)" |
3704 |
proof (induct xs) |
|
3705 |
case (Cons x xs) |
|
3706 |
show ?case |
|
75598 | 3707 |
apply (auto simp add: Cons nth_Cons less_Suc_eq_le split: nat.split_asm) |
3708 |
apply (metis Suc_leI in_set_conv_nth length_pos_if_in_set lessI less_imp_le_nat less_nat_zero_code) |
|
3709 |
apply (metis Suc_le_eq) |
|
68719 | 3710 |
done |
3711 |
qed auto |
|
13114 | 3712 |
|
18490 | 3713 |
lemma nth_eq_iff_index_eq: |
58807 | 3714 |
"\<lbrakk> distinct xs; i < length xs; j < length xs \<rbrakk> \<Longrightarrow> (xs!i = xs!j) = (i = j)" |
18490 | 3715 |
by(auto simp: distinct_conv_nth) |
3716 |
||
64963 | 3717 |
lemma distinct_Ex1: |
63099
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents:
63092
diff
changeset
|
3718 |
"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
|
3719 |
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
|
3720 |
|
59728 | 3721 |
lemma inj_on_nth: "distinct xs \<Longrightarrow> \<forall>i \<in> I. i < length xs \<Longrightarrow> inj_on (nth xs) I" |
3722 |
by (rule inj_onI) (simp add: nth_eq_iff_index_eq) |
|
3723 |
||
63099
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents:
63092
diff
changeset
|
3724 |
lemma bij_betw_nth: |
64963 | 3725 |
assumes "distinct xs" "A = {..<length xs}" "B = set xs" |
67399 | 3726 |
shows "bij_betw ((!) xs) A B" |
63099
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents:
63092
diff
changeset
|
3727 |
using assms unfolding bij_betw_def |
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents:
63092
diff
changeset
|
3728 |
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
|
3729 |
|
56953 | 3730 |
lemma set_update_distinct: "\<lbrakk> distinct xs; n < length xs \<rbrakk> \<Longrightarrow> |
3731 |
set(xs[n := x]) = insert x (set xs - {xs!n})" |
|
3732 |
by(auto simp: set_eq_iff in_set_conv_nth nth_list_update nth_eq_iff_index_eq) |
|
3733 |
||
71585 | 3734 |
lemma distinct_swap[simp]: "\<lbrakk> i < size xs; j < size xs\<rbrakk> \<Longrightarrow> |
57537 | 3735 |
distinct(xs[i := xs!j, j := xs!i]) = distinct xs" |
68719 | 3736 |
apply (simp add: distinct_conv_nth nth_list_update) |
71585 | 3737 |
apply (safe; metis) |
3738 |
done |
|
57537 | 3739 |
|
3740 |
lemma set_swap[simp]: |
|
3741 |
"\<lbrakk> i < size xs; j < size xs \<rbrakk> \<Longrightarrow> set(xs[i := xs!j, j := xs!i]) = set xs" |
|
3742 |
by(simp add: set_conv_nth nth_list_update) metis |
|
3743 |
||
71585 | 3744 |
lemma distinct_card: "distinct xs \<Longrightarrow> card (set xs) = size xs" |
24349 | 3745 |
by (induct xs) auto |
14388 | 3746 |
|
71585 | 3747 |
lemma card_distinct: "card (set xs) = size xs \<Longrightarrow> distinct xs" |
14388 | 3748 |
proof (induct xs) |
3749 |
case (Cons x xs) |
|
3750 |
show ?case |
|
3751 |
proof (cases "x \<in> set xs") |
|
3752 |
case False with Cons show ?thesis by simp |
|
3753 |
next |
|
3754 |
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
|
3755 |
have "card (set xs) = Suc (length xs)" |
62390 | 3756 |
by (simp add: card_insert_if split: if_split_asm) |
14388 | 3757 |
moreover have "card (set xs) \<le> length xs" by (rule card_length) |
3758 |
ultimately have False by simp |
|
3759 |
thus ?thesis .. |
|
3760 |
qed |
|
68719 | 3761 |
qed simp |
14388 | 3762 |
|
45115
93c1ac6727a3
adding lemma to List library for executable equation of the (refl) transitive closure
bulwahn
parents:
44928
diff
changeset
|
3763 |
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
|
3764 |
by (induct xs) (auto) |
93c1ac6727a3
adding lemma to List library for executable equation of the (refl) transitive closure
bulwahn
parents:
44928
diff
changeset
|
3765 |
|
67091 | 3766 |
lemma not_distinct_decomp: "\<not> distinct ws \<Longrightarrow> \<exists>xs ys zs y. ws = xs@[y]@ys@[y]@zs" |
68719 | 3767 |
proof (induct n == "length ws" arbitrary:ws) |
3768 |
case (Suc n ws) |
|
3769 |
then show ?case |
|
3770 |
using length_Suc_conv [of ws n] |
|
3771 |
apply (auto simp: eq_commute) |
|
3772 |
apply (metis append_Nil in_set_conv_decomp_first) |
|
3773 |
by (metis append_Cons) |
|
3774 |
qed simp |
|
18490 | 3775 |
|
45841 | 3776 |
lemma not_distinct_conv_prefix: |
3777 |
defines "dec as xs y ys \<equiv> y \<in> set xs \<and> distinct xs \<and> as = xs @ y # ys" |
|
3778 |
shows "\<not>distinct as \<longleftrightarrow> (\<exists>xs y ys. dec as xs y ys)" (is "?L = ?R") |
|
3779 |
proof |
|
3780 |
assume "?L" then show "?R" |
|
3781 |
proof (induct "length as" arbitrary: as rule: less_induct) |
|
3782 |
case less |
|
3783 |
obtain xs ys zs y where decomp: "as = (xs @ y # ys) @ y # zs" |
|
3784 |
using not_distinct_decomp[OF less.prems] by auto |
|
3785 |
show ?case |
|
3786 |
proof (cases "distinct (xs @ y # ys)") |
|
3787 |
case True |
|
3788 |
with decomp have "dec as (xs @ y # ys) y zs" by (simp add: dec_def) |
|
3789 |
then show ?thesis by blast |
|
3790 |
next |
|
3791 |
case False |
|
3792 |
with less decomp obtain xs' y' ys' where "dec (xs @ y # ys) xs' y' ys'" |
|
3793 |
by atomize_elim auto |
|
3794 |
with decomp have "dec as xs' y' (ys' @ y # zs)" by (simp add: dec_def) |
|
3795 |
then show ?thesis by blast |
|
3796 |
qed |
|
3797 |
qed |
|
3798 |
qed (auto simp: dec_def) |
|
3799 |
||
49948
744934b818c7
moved quite generic material from theory Enum to more appropriate places
haftmann
parents:
49808
diff
changeset
|
3800 |
lemma distinct_product: |
57247 | 3801 |
"distinct xs \<Longrightarrow> distinct ys \<Longrightarrow> distinct (List.product xs ys)" |
3802 |
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
|
3803 |
|
53721
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
traytel
parents:
53689
diff
changeset
|
3804 |
lemma distinct_product_lists: |
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
traytel
parents:
53689
diff
changeset
|
3805 |
assumes "\<forall>xs \<in> set xss. distinct xs" |
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
traytel
parents:
53689
diff
changeset
|
3806 |
shows "distinct (product_lists xss)" |
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
traytel
parents:
53689
diff
changeset
|
3807 |
using assms proof (induction xss) |
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
traytel
parents:
53689
diff
changeset
|
3808 |
case (Cons xs xss) note * = this |
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
traytel
parents:
53689
diff
changeset
|
3809 |
then show ?case |
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
traytel
parents:
53689
diff
changeset
|
3810 |
proof (cases "product_lists xss") |
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
traytel
parents:
53689
diff
changeset
|
3811 |
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
|
3812 |
next |
64963 | 3813 |
case (Cons ps pss) with * show ?thesis |
53721
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
traytel
parents:
53689
diff
changeset
|
3814 |
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
|
3815 |
qed |
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
traytel
parents:
53689
diff
changeset
|
3816 |
qed simp |
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
traytel
parents:
53689
diff
changeset
|
3817 |
|
18490 | 3818 |
lemma length_remdups_concat: |
44921 | 3819 |
"length (remdups (concat xss)) = card (\<Union>xs\<in>set xss. set xs)" |
58807 | 3820 |
by (simp add: distinct_card [symmetric]) |
17906 | 3821 |
|
71393
fce780f9c9c6
new examples of BNF lifting across quotients using a new theory of confluence,
traytel
parents:
70911
diff
changeset
|
3822 |
lemma remdups_append2: |
fce780f9c9c6
new examples of BNF lifting across quotients using a new theory of confluence,
traytel
parents:
70911
diff
changeset
|
3823 |
"remdups (xs @ remdups ys) = remdups (xs @ ys)" |
fce780f9c9c6
new examples of BNF lifting across quotients using a new theory of confluence,
traytel
parents:
70911
diff
changeset
|
3824 |
by(induction xs) auto |
fce780f9c9c6
new examples of BNF lifting across quotients using a new theory of confluence,
traytel
parents:
70911
diff
changeset
|
3825 |
|
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
|
3826 |
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
|
3827 |
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
|
3828 |
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
|
3829 |
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
|
3830 |
qed |
17906 | 3831 |
|
58807 | 3832 |
lemma remdups_remdups: "remdups (remdups xs) = remdups xs" |
3833 |
by (induct xs) simp_all |
|
36275 | 3834 |
|
36851 | 3835 |
lemma distinct_butlast: |
46500
0196966d6d2d
removing unnecessary premises in theorems of List theory
bulwahn
parents:
46448
diff
changeset
|
3836 |
assumes "distinct xs" |
36851 | 3837 |
shows "distinct (butlast xs)" |
46500
0196966d6d2d
removing unnecessary premises in theorems of List theory
bulwahn
parents:
46448
diff
changeset
|
3838 |
proof (cases "xs = []") |
0196966d6d2d
removing unnecessary premises in theorems of List theory
bulwahn
parents:
46448
diff
changeset
|
3839 |
case False |
60758 | 3840 |
from \<open>xs \<noteq> []\<close> obtain ys y where "xs = ys @ [y]" by (cases xs rule: rev_cases) auto |
3841 |
with \<open>distinct xs\<close> show ?thesis by simp |
|
46500
0196966d6d2d
removing unnecessary premises in theorems of List theory
bulwahn
parents:
46448
diff
changeset
|
3842 |
qed (auto) |
36851 | 3843 |
|
39728 | 3844 |
lemma remdups_map_remdups: |
3845 |
"remdups (map f (remdups xs)) = remdups (map f xs)" |
|
58807 | 3846 |
by (induct xs) simp_all |
39728 | 3847 |
|
39915
ecf97cf3d248
turned distinct and sorted into inductive predicates: yields nice induction principles for free; more elegant proofs
haftmann
parents:
39774
diff
changeset
|
3848 |
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
|
3849 |
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
|
3850 |
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
|
3851 |
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
|
3852 |
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
|
3853 |
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
|
3854 |
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
|
3855 |
with assms have "distinct xs'" by simp |
60758 | 3856 |
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
|
3857 |
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
|
3858 |
qed |
ecf97cf3d248
turned distinct and sorted into inductive predicates: yields nice induction principles for free; more elegant proofs
haftmann
parents:
39774
diff
changeset
|
3859 |
|
ecf97cf3d248
turned distinct and sorted into inductive predicates: yields nice induction principles for free; more elegant proofs
haftmann
parents:
39774
diff
changeset
|
3860 |
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
|
3861 |
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
|
3862 |
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
|
3863 |
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
|
3864 |
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
|
3865 |
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
|
3866 |
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
|
3867 |
with assms have "distinct ys'" by simp |
60758 | 3868 |
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
|
3869 |
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
|
3870 |
qed |
ecf97cf3d248
turned distinct and sorted into inductive predicates: yields nice induction principles for free; more elegant proofs
haftmann
parents:
39774
diff
changeset
|
3871 |
|
47122 | 3872 |
lemma set_take_disj_set_drop_if_distinct: |
3873 |
"distinct vs \<Longrightarrow> i \<le> j \<Longrightarrow> set (take i vs) \<inter> set (drop j vs) = {}" |
|
3874 |
by (auto simp: in_set_conv_nth distinct_conv_nth) |
|
3875 |
||
44635
3d046864ebe6
added two lemmas about "distinct" to help Sledgehammer
blanchet
parents:
44619
diff
changeset
|
3876 |
(* The next two lemmas help Sledgehammer. *) |
3d046864ebe6
added two lemmas about "distinct" to help Sledgehammer
blanchet
parents:
44619
diff
changeset
|
3877 |
|
3d046864ebe6
added two lemmas about "distinct" to help Sledgehammer
blanchet
parents:
44619
diff
changeset
|
3878 |
lemma distinct_singleton: "distinct [x]" by simp |
3d046864ebe6
added two lemmas about "distinct" to help Sledgehammer
blanchet
parents:
44619
diff
changeset
|
3879 |
|
3d046864ebe6
added two lemmas about "distinct" to help Sledgehammer
blanchet
parents:
44619
diff
changeset
|
3880 |
lemma distinct_length_2_or_more: |
58807 | 3881 |
"distinct (a # b # xs) \<longleftrightarrow> (a \<noteq> b \<and> distinct (a # xs) \<and> distinct (b # xs))" |
56085 | 3882 |
by force |
44635
3d046864ebe6
added two lemmas about "distinct" to help Sledgehammer
blanchet
parents:
44619
diff
changeset
|
3883 |
|
58969 | 3884 |
lemma remdups_adj_altdef: "(remdups_adj xs = ys) \<longleftrightarrow> |
67091 | 3885 |
(\<exists>f::nat => nat. mono f \<and> f ` {0 ..< size xs} = {0 ..< size ys} |
58969 | 3886 |
\<and> (\<forall>i < size xs. xs!i = ys!(f i)) |
61941 | 3887 |
\<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 | 3888 |
proof |
3889 |
assume ?L |
|
3890 |
then show "\<exists>f. ?p f xs ys" |
|
3891 |
proof (induct xs arbitrary: ys rule: remdups_adj.induct) |
|
3892 |
case (1 ys) |
|
3893 |
thus ?case by (intro exI[of _ id]) (auto simp: mono_def) |
|
3894 |
next |
|
3895 |
case (2 x ys) |
|
3896 |
thus ?case by (intro exI[of _ id]) (auto simp: mono_def) |
|
3897 |
next |
|
3898 |
case (3 x1 x2 xs ys) |
|
3899 |
let ?xs = "x1 # x2 # xs" |
|
3900 |
let ?cond = "x1 = x2" |
|
63040 | 3901 |
define zs where "zs = remdups_adj (x2 # xs)" |
58969 | 3902 |
from 3(1-2)[of zs] |
3903 |
obtain f where p: "?p f (x2 # xs) zs" unfolding zs_def by (cases ?cond) auto |
|
3904 |
then have f0: "f 0 = 0" |
|
3905 |
by (intro mono_image_least[where f=f]) blast+ |
|
3906 |
from p have mono: "mono f" and f_xs_zs: "f ` {0..<length (x2 # xs)} = {0..<length zs}" by auto |
|
3907 |
have ys: "ys = (if x1 = x2 then zs else x1 # zs)" |
|
3908 |
unfolding 3(3)[symmetric] zs_def by auto |
|
3909 |
have zs0: "zs ! 0 = x2" unfolding zs_def by (induct xs) auto |
|
3910 |
have zsne: "zs \<noteq> []" unfolding zs_def by (induct xs) auto |
|
3911 |
let ?Succ = "if ?cond then id else Suc" |
|
3912 |
let ?x1 = "if ?cond then id else Cons x1" |
|
3913 |
let ?f = "\<lambda> i. if i = 0 then 0 else ?Succ (f (i - 1))" |
|
3914 |
have ys: "ys = ?x1 zs" unfolding ys by (cases ?cond, auto) |
|
60758 | 3915 |
have mono: "mono ?f" using \<open>mono f\<close> unfolding mono_def by auto |
58969 | 3916 |
show ?case unfolding ys |
3917 |
proof (intro exI[of _ ?f] conjI allI impI) |
|
3918 |
show "mono ?f" by fact |
|
3919 |
next |
|
3920 |
fix i assume i: "i < length ?xs" |
|
3921 |
with p show "?xs ! i = ?x1 zs ! (?f i)" using zs0 by auto |
|
3922 |
next |
|
3923 |
fix i assume i: "i + 1 < length ?xs" |
|
3924 |
with p show "(?xs ! i = ?xs ! (i + 1)) = (?f i = ?f (i + 1))" |
|
3925 |
by (cases i) (auto simp: f0) |
|
3926 |
next |
|
3927 |
have id: "{0 ..< length (?x1 zs)} = insert 0 (?Succ ` {0 ..< length zs})" |
|
3928 |
using zsne by (cases ?cond, auto) |
|
3929 |
{ fix i assume "i < Suc (length xs)" |
|
67399 | 3930 |
hence "Suc i \<in> {0..<Suc (Suc (length xs))} \<inter> Collect ((<) 0)" by auto |
58969 | 3931 |
from imageI[OF this, of "\<lambda>i. ?Succ (f (i - Suc 0))"] |
67399 | 3932 |
have "?Succ (f i) \<in> (\<lambda>i. ?Succ (f (i - Suc 0))) ` ({0..<Suc (Suc (length xs))} \<inter> Collect ((<) 0))" by auto |
58969 | 3933 |
} |
3934 |
then show "?f ` {0 ..< length ?xs} = {0 ..< length (?x1 zs)}" |
|
3935 |
unfolding id f_xs_zs[symmetric] by auto |
|
3936 |
qed |
|
3937 |
qed |
|
3938 |
next |
|
3939 |
assume "\<exists> f. ?p f xs ys" |
|
3940 |
then show ?L |
|
3941 |
proof (induct xs arbitrary: ys rule: remdups_adj.induct) |
|
3942 |
case 1 then show ?case by auto |
|
3943 |
next |
|
3944 |
case (2 x) then obtain f where f_img: "f ` {0 ..< size [x]} = {0 ..< size ys}" |
|
3945 |
and f_nth: "\<And>i. i < size [x] \<Longrightarrow> [x]!i = ys!(f i)" |
|
3946 |
by blast |
|
3947 |
||
3948 |
have "length ys = card (f ` {0 ..< size [x]})" |
|
3949 |
using f_img by auto |
|
63540 | 3950 |
then have *: "length ys = 1" by auto |
58969 | 3951 |
then have "f 0 = 0" using f_img by auto |
63540 | 3952 |
with * show ?case using f_nth by (cases ys) auto |
58969 | 3953 |
next |
3954 |
case (3 x1 x2 xs) |
|
3955 |
from "3.prems" obtain f where f_mono: "mono f" |
|
3956 |
and f_img: "f ` {0..<length (x1 # x2 # xs)} = {0..<length ys}" |
|
3957 |
and f_nth: |
|
3958 |
"\<And>i. i < length (x1 # x2 # xs) \<Longrightarrow> (x1 # x2 # xs) ! i = ys ! f i" |
|
3959 |
"\<And>i. i + 1 < length (x1 # x2 #xs) \<Longrightarrow> |
|
3960 |
((x1 # x2 # xs) ! i = (x1 # x2 # xs) ! (i + 1)) = (f i = f (i + 1))" |
|
3961 |
by blast |
|
3962 |
||
3963 |
show ?case |
|
3964 |
proof cases |
|
3965 |
assume "x1 = x2" |
|
3966 |
||
67091 | 3967 |
let ?f' = "f \<circ> Suc" |
58969 | 3968 |
|
3969 |
have "remdups_adj (x1 # xs) = ys" |
|
3970 |
proof (intro "3.hyps" exI conjI impI allI) |
|
3971 |
show "mono ?f'" |
|
3972 |
using f_mono by (simp add: mono_iff_le_Suc) |
|
3973 |
next |
|
3974 |
have "?f' ` {0 ..< length (x1 # xs)} = f ` {Suc 0 ..< length (x1 # x2 # xs)}" |
|
71585 | 3975 |
using less_Suc_eq_0_disj by auto |
58969 | 3976 |
also have "\<dots> = f ` {0 ..< length (x1 # x2 # xs)}" |
3977 |
proof - |
|
3978 |
have "f 0 = f (Suc 0)" using \<open>x1 = x2\<close> f_nth[of 0] by simp |
|
69850 | 3979 |
then show ?thesis |
71585 | 3980 |
using less_Suc_eq_0_disj by auto |
58969 | 3981 |
qed |
3982 |
also have "\<dots> = {0 ..< length ys}" by fact |
|
3983 |
finally show "?f' ` {0 ..< length (x1 # xs)} = {0 ..< length ys}" . |
|
3984 |
qed (insert f_nth[of "Suc i" for i], auto simp: \<open>x1 = x2\<close>) |
|
3985 |
then show ?thesis using \<open>x1 = x2\<close> by simp |
|
3986 |
next |
|
3987 |
assume "x1 \<noteq> x2" |
|
3988 |
||
71585 | 3989 |
have two: "Suc (Suc 0) \<le> length ys" |
58969 | 3990 |
proof - |
3991 |
have "2 = card {f 0, f 1}" using \<open>x1 \<noteq> x2\<close> f_nth[of 0] by auto |
|
3992 |
also have "\<dots> \<le> card (f ` {0..< length (x1 # x2 # xs)})" |
|
3993 |
by (rule card_mono) auto |
|
3994 |
finally show ?thesis using f_img by simp |
|
3995 |
qed |
|
3996 |
||
3997 |
have "f 0 = 0" using f_mono f_img by (rule mono_image_least) simp |
|
3998 |
||
3999 |
have "f (Suc 0) = Suc 0" |
|
4000 |
proof (rule ccontr) |
|
4001 |
assume "f (Suc 0) \<noteq> Suc 0" |
|
4002 |
then have "Suc 0 < f (Suc 0)" using f_nth[of 0] \<open>x1 \<noteq> x2\<close> \<open>f 0 = 0\<close> by auto |
|
4003 |
then have "\<And>i. Suc 0 < f (Suc i)" |
|
4004 |
using f_mono |
|
4005 |
by (meson Suc_le_mono le0 less_le_trans monoD) |
|
69850 | 4006 |
then have "Suc 0 \<noteq> f i" for i using \<open>f 0 = 0\<close> |
4007 |
by (cases i) fastforce+ |
|
58969 | 4008 |
then have "Suc 0 \<notin> f ` {0 ..< length (x1 # x2 # xs)}" by auto |
71585 | 4009 |
then show False using f_img two by auto |
58969 | 4010 |
qed |
4011 |
obtain ys' where "ys = x1 # x2 # ys'" |
|
71585 | 4012 |
using two f_nth[of 0] f_nth[of 1] |
4013 |
by (auto simp: Suc_le_length_iff \<open>f 0 = 0\<close> \<open>f (Suc 0) = Suc 0\<close>) |
|
4014 |
||
4015 |
have Suc0_le_f_Suc: "Suc 0 \<le> f (Suc i)" for i |
|
4016 |
by (metis Suc_le_mono \<open>f (Suc 0) = Suc 0\<close> f_mono le0 mono_def) |
|
58969 | 4017 |
|
63040 | 4018 |
define f' where "f' x = f (Suc x) - 1" for x |
71585 | 4019 |
have f_Suc: "f (Suc i) = Suc (f' i)" for i |
58969 | 4020 |
using Suc0_le_f_Suc[of i] by (auto simp: f'_def) |
4021 |
||
4022 |
have "remdups_adj (x2 # xs) = (x2 # ys')" |
|
4023 |
proof (intro "3.hyps" exI conjI impI allI) |
|
4024 |
show "mono f'" |
|
4025 |
using Suc0_le_f_Suc f_mono by (auto simp: f'_def mono_iff_le_Suc le_diff_iff) |
|
4026 |
next |
|
4027 |
have "f' ` {0 ..< length (x2 # xs)} = (\<lambda>x. f x - 1) ` {0 ..< length (x1 # x2 #xs)}" |
|
68719 | 4028 |
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 | 4029 |
also have "\<dots> = (\<lambda>x. x - 1) ` f ` {0 ..< length (x1 # x2 #xs)}" |
4030 |
by (auto simp: image_comp) |
|
4031 |
also have "\<dots> = (\<lambda>x. x - 1) ` {0 ..< length ys}" |
|
4032 |
by (simp only: f_img) |
|
4033 |
also have "\<dots> = {0 ..< length (x2 # ys')}" |
|
4034 |
using \<open>ys = _\<close> by (fastforce intro: rev_image_eqI) |
|
4035 |
finally show "f' ` {0 ..< length (x2 # xs)} = {0 ..< length (x2 # ys')}" . |
|
4036 |
qed (insert f_nth[of "Suc i" for i] \<open>x1 \<noteq> x2\<close>, auto simp add: f_Suc \<open>ys = _\<close>) |
|
4037 |
then show ?case using \<open>ys = _\<close> \<open>x1 \<noteq> x2\<close> by simp |
|
4038 |
qed |
|
4039 |
qed |
|
4040 |
qed |
|
4041 |
||
58041 | 4042 |
lemma hd_remdups_adj[simp]: "hd (remdups_adj xs) = hd xs" |
71585 | 4043 |
by (induction xs rule: remdups_adj.induct) simp_all |
58041 | 4044 |
|
53721
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
traytel
parents:
53689
diff
changeset
|
4045 |
lemma remdups_adj_Cons: "remdups_adj (x # xs) = |
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
traytel
parents:
53689
diff
changeset
|
4046 |
(case remdups_adj xs of [] \<Rightarrow> [x] | y # xs \<Rightarrow> if x = y then y # xs else x # y # xs)" |
71585 | 4047 |
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
|
4048 |
|
64963 | 4049 |
lemma remdups_adj_append_two: |
53721
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
traytel
parents:
53689
diff
changeset
|
4050 |
"remdups_adj (xs @ [x,y]) = remdups_adj (xs @ [x]) @ (if x = y then [] else [y])" |
71585 | 4051 |
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
|
4052 |
|
58041 | 4053 |
lemma remdups_adj_adjacent: |
4054 |
"Suc i < length (remdups_adj xs) \<Longrightarrow> remdups_adj xs ! i \<noteq> remdups_adj xs ! Suc i" |
|
4055 |
proof (induction xs arbitrary: i rule: remdups_adj.induct) |
|
4056 |
case (3 x y xs i) |
|
4057 |
thus ?case by (cases i, cases "x = y") (simp, auto simp: hd_conv_nth[symmetric]) |
|
4058 |
qed simp_all |
|
4059 |
||
53721
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
traytel
parents:
53689
diff
changeset
|
4060 |
lemma remdups_adj_rev[simp]: "remdups_adj (rev xs) = rev (remdups_adj xs)" |
58807 | 4061 |
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
|
4062 |
|
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
traytel
parents:
53689
diff
changeset
|
4063 |
lemma remdups_adj_length[simp]: "length (remdups_adj xs) \<le> length xs" |
58807 | 4064 |
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
|
4065 |
|
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
traytel
parents:
53689
diff
changeset
|
4066 |
lemma remdups_adj_length_ge1[simp]: "xs \<noteq> [] \<Longrightarrow> length (remdups_adj xs) \<ge> Suc 0" |
58807 | 4067 |
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
|
4068 |
|
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
traytel
parents:
53689
diff
changeset
|
4069 |
lemma remdups_adj_Nil_iff[simp]: "remdups_adj xs = [] \<longleftrightarrow> xs = []" |
58807 | 4070 |
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
|
4071 |
|
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
traytel
parents:
53689
diff
changeset
|
4072 |
lemma remdups_adj_set[simp]: "set (remdups_adj xs) = set xs" |
58807 | 4073 |
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
|
4074 |
|
71778 | 4075 |
lemma last_remdups_adj [simp]: "last (remdups_adj xs) = last xs" |
4076 |
by (induction xs rule: remdups_adj.induct) auto |
|
4077 |
||
53721
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
traytel
parents:
53689
diff
changeset
|
4078 |
lemma remdups_adj_Cons_alt[simp]: "x # tl (remdups_adj (x # xs)) = remdups_adj (x # xs)" |
58807 | 4079 |
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
|
4080 |
|
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
traytel
parents:
53689
diff
changeset
|
4081 |
lemma remdups_adj_distinct: "distinct xs \<Longrightarrow> remdups_adj xs = xs" |
58807 | 4082 |
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
|
4083 |
|
64963 | 4084 |
lemma remdups_adj_append: |
53721
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
traytel
parents:
53689
diff
changeset
|
4085 |
"remdups_adj (xs\<^sub>1 @ x # xs\<^sub>2) = remdups_adj (xs\<^sub>1 @ [x]) @ tl (remdups_adj (x # xs\<^sub>2))" |
58807 | 4086 |
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
|
4087 |
|
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
traytel
parents:
53689
diff
changeset
|
4088 |
lemma remdups_adj_singleton: |
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
traytel
parents:
53689
diff
changeset
|
4089 |
"remdups_adj xs = [x] \<Longrightarrow> xs = replicate (length xs) x" |
62390 | 4090 |
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
|
4091 |
|
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
traytel
parents:
53689
diff
changeset
|
4092 |
lemma remdups_adj_map_injective: |
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
traytel
parents:
53689
diff
changeset
|
4093 |
assumes "inj f" |
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
traytel
parents:
53689
diff
changeset
|
4094 |
shows "remdups_adj (map f xs) = map f (remdups_adj xs)" |
58807 | 4095 |
by (induct xs rule: remdups_adj.induct) (auto simp add: injD[OF assms]) |
4096 |
||
62065 | 4097 |
lemma remdups_adj_replicate: |
4098 |
"remdups_adj (replicate n x) = (if n = 0 then [] else [x])" |
|
4099 |
by (induction n) (auto simp: remdups_adj_Cons) |
|
4100 |
||
58807 | 4101 |
lemma remdups_upt [simp]: "remdups [m..<n] = [m..<n]" |
58437 | 4102 |
proof (cases "m \<le> n") |
4103 |
case False then show ?thesis by simp |
|
4104 |
next |
|
4105 |
case True then obtain q where "n = m + q" |
|
4106 |
by (auto simp add: le_iff_add) |
|
4107 |
moreover have "remdups [m..<m + q] = [m..<m + q]" |
|
4108 |
by (induct q) simp_all |
|
4109 |
ultimately show ?thesis by simp |
|
4110 |
qed |
|
4111 |
||
71779 | 4112 |
lemma successively_remdups_adjI: |
4113 |
"successively P xs \<Longrightarrow> successively P (remdups_adj xs)" |
|
4114 |
by (induction xs rule: remdups_adj.induct) (auto simp: successively_Cons) |
|
4115 |
||
4116 |
lemma successively_remdups_adj_iff: |
|
4117 |
"(\<And>x. x \<in> set xs \<Longrightarrow> P x x) \<Longrightarrow> |
|
4118 |
successively P (remdups_adj xs) \<longleftrightarrow> successively P xs" |
|
4119 |
by (induction xs rule: remdups_adj.induct)(auto simp: successively_Cons) |
|
4120 |
||
71789 | 4121 |
lemma remdups_adj_Cons': |
4122 |
"remdups_adj (x # xs) = x # remdups_adj (dropWhile (\<lambda>y. y = x) xs)" |
|
4123 |
by (induction xs) auto |
|
4124 |
||
4125 |
lemma remdups_adj_singleton_iff: |
|
4126 |
"length (remdups_adj xs) = Suc 0 \<longleftrightarrow> xs \<noteq> [] \<and> xs = replicate (length xs) (hd xs)" |
|
4127 |
proof safe |
|
4128 |
assume *: "xs = replicate (length xs) (hd xs)" and [simp]: "xs \<noteq> []" |
|
4129 |
show "length (remdups_adj xs) = Suc 0" |
|
4130 |
by (subst *) (auto simp: remdups_adj_replicate) |
|
4131 |
next |
|
4132 |
assume "length (remdups_adj xs) = Suc 0" |
|
4133 |
thus "xs = replicate (length xs) (hd xs)" |
|
4134 |
by (induction xs rule: remdups_adj.induct) (auto split: if_splits) |
|
4135 |
qed auto |
|
4136 |
||
4137 |
lemma tl_remdups_adj: |
|
4138 |
"ys \<noteq> [] \<Longrightarrow> tl (remdups_adj ys) = remdups_adj (dropWhile (\<lambda>x. x = hd ys) (tl ys))" |
|
4139 |
by (cases ys) (simp_all add: remdups_adj_Cons') |
|
4140 |
||
4141 |
lemma remdups_adj_append_dropWhile: |
|
4142 |
"remdups_adj (xs @ y # ys) = remdups_adj (xs @ [y]) @ remdups_adj (dropWhile (\<lambda>x. x = y) ys)" |
|
4143 |
by (subst remdups_adj_append) (simp add: tl_remdups_adj) |
|
4144 |
||
4145 |
lemma remdups_adj_append': |
|
4146 |
assumes "xs = [] \<or> ys = [] \<or> last xs \<noteq> hd ys" |
|
4147 |
shows "remdups_adj (xs @ ys) = remdups_adj xs @ remdups_adj ys" |
|
4148 |
proof - |
|
4149 |
have ?thesis if [simp]: "xs \<noteq> []" "ys \<noteq> []" and "last xs \<noteq> hd ys" |
|
4150 |
proof - |
|
4151 |
obtain x xs' where xs: "xs = xs' @ [x]" |
|
4152 |
by (cases xs rule: rev_cases) auto |
|
4153 |
have "remdups_adj (xs' @ x # ys) = remdups_adj (xs' @ [x]) @ remdups_adj ys" |
|
4154 |
using \<open>last xs \<noteq> hd ys\<close> unfolding xs |
|
4155 |
by (metis (full_types) dropWhile_eq_self_iff last_snoc remdups_adj_append_dropWhile) |
|
4156 |
thus ?thesis by (simp add: xs) |
|
4157 |
qed |
|
74424 | 4158 |
thus ?thesis using assms |
71789 | 4159 |
by (cases "xs = []"; cases "ys = []") auto |
4160 |
qed |
|
4161 |
||
4162 |
lemma remdups_adj_append'': "xs \<noteq> [] |
|
4163 |
\<Longrightarrow> remdups_adj (xs @ ys) = remdups_adj xs @ remdups_adj (dropWhile (\<lambda>y. y = last xs) ys)" |
|
4164 |
by (induction xs rule: remdups_adj.induct) (auto simp: remdups_adj_Cons') |
|
4165 |
||
71779 | 4166 |
|
4167 |
subsection \<open>@{const distinct_adj}\<close> |
|
4168 |
||
4169 |
lemma distinct_adj_Nil [simp]: "distinct_adj []" |
|
4170 |
and distinct_adj_singleton [simp]: "distinct_adj [x]" |
|
4171 |
and distinct_adj_Cons_Cons [simp]: "distinct_adj (x # y # xs) \<longleftrightarrow> x \<noteq> y \<and> distinct_adj (y # xs)" |
|
4172 |
by (auto simp: distinct_adj_def) |
|
4173 |
||
4174 |
lemma distinct_adj_Cons: "distinct_adj (x # xs) \<longleftrightarrow> xs = [] \<or> x \<noteq> hd xs \<and> distinct_adj xs" |
|
4175 |
by (cases xs) auto |
|
4176 |
||
4177 |
lemma distinct_adj_ConsD: "distinct_adj (x # xs) \<Longrightarrow> distinct_adj xs" |
|
4178 |
by (cases xs) auto |
|
4179 |
||
4180 |
lemma distinct_adj_remdups_adj[simp]: "distinct_adj (remdups_adj xs)" |
|
4181 |
by (induction xs rule: remdups_adj.induct) (auto simp: distinct_adj_Cons) |
|
4182 |
||
4183 |
lemma distinct_adj_altdef: "distinct_adj xs \<longleftrightarrow> remdups_adj xs = xs" |
|
4184 |
proof |
|
4185 |
assume "remdups_adj xs = xs" |
|
4186 |
with distinct_adj_remdups_adj[of xs] show "distinct_adj xs" |
|
4187 |
by simp |
|
4188 |
next |
|
4189 |
assume "distinct_adj xs" |
|
4190 |
thus "remdups_adj xs = xs" |
|
4191 |
by (induction xs rule: induct_list012) auto |
|
4192 |
qed |
|
4193 |
||
4194 |
lemma distinct_adj_rev [simp]: "distinct_adj (rev xs) \<longleftrightarrow> distinct_adj xs" |
|
4195 |
by (simp add: distinct_adj_def eq_commute) |
|
4196 |
||
4197 |
lemma distinct_adj_append_iff: |
|
4198 |
"distinct_adj (xs @ ys) \<longleftrightarrow> |
|
4199 |
distinct_adj xs \<and> distinct_adj ys \<and> (xs = [] \<or> ys = [] \<or> last xs \<noteq> hd ys)" |
|
4200 |
by (auto simp: distinct_adj_def successively_append_iff) |
|
4201 |
||
4202 |
lemma distinct_adj_appendD1 [dest]: "distinct_adj (xs @ ys) \<Longrightarrow> distinct_adj xs" |
|
4203 |
and distinct_adj_appendD2 [dest]: "distinct_adj (xs @ ys) \<Longrightarrow> distinct_adj ys" |
|
4204 |
by (auto simp: distinct_adj_append_iff) |
|
4205 |
||
4206 |
lemma distinct_adj_mapI: "distinct_adj xs \<Longrightarrow> inj_on f (set xs) \<Longrightarrow> distinct_adj (map f xs)" |
|
4207 |
unfolding distinct_adj_def successively_map |
|
4208 |
by (erule successively_mono) (auto simp: inj_on_def) |
|
4209 |
||
4210 |
lemma distinct_adj_mapD: "distinct_adj (map f xs) \<Longrightarrow> distinct_adj xs" |
|
4211 |
unfolding distinct_adj_def successively_map by (erule successively_mono) auto |
|
4212 |
||
4213 |
lemma distinct_adj_map_iff: "inj_on f (set xs) \<Longrightarrow> distinct_adj (map f xs) \<longleftrightarrow> distinct_adj xs" |
|
4214 |
using distinct_adj_mapD distinct_adj_mapI by blast |
|
4215 |
||
49948
744934b818c7
moved quite generic material from theory Enum to more appropriate places
haftmann
parents:
49808
diff
changeset
|
4216 |
|
69593 | 4217 |
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
|
4218 |
|
874150ddd50a
canonical insert operation; generalized lemma foldl_apply_inv to foldl_apply
haftmann
parents:
34942
diff
changeset
|
4219 |
lemma in_set_insert [simp]: |
874150ddd50a
canonical insert operation; generalized lemma foldl_apply_inv to foldl_apply
haftmann
parents:
34942
diff
changeset
|
4220 |
"x \<in> set xs \<Longrightarrow> List.insert x xs = xs" |
58807 | 4221 |
by (simp add: List.insert_def) |
34978
874150ddd50a
canonical insert operation; generalized lemma foldl_apply_inv to foldl_apply
haftmann
parents:
34942
diff
changeset
|
4222 |
|
874150ddd50a
canonical insert operation; generalized lemma foldl_apply_inv to foldl_apply
haftmann
parents:
34942
diff
changeset
|
4223 |
lemma not_in_set_insert [simp]: |
874150ddd50a
canonical insert operation; generalized lemma foldl_apply_inv to foldl_apply
haftmann
parents:
34942
diff
changeset
|
4224 |
"x \<notin> set xs \<Longrightarrow> List.insert x xs = x # xs" |
58807 | 4225 |
by (simp add: List.insert_def) |
4226 |
||
4227 |
lemma insert_Nil [simp]: "List.insert x [] = [x]" |
|
4228 |
by simp |
|
4229 |
||
4230 |
lemma set_insert [simp]: "set (List.insert x xs) = insert x (set xs)" |
|
4231 |
by (auto simp add: List.insert_def) |
|
4232 |
||
4233 |
lemma distinct_insert [simp]: "distinct (List.insert x xs) = distinct xs" |
|
4234 |
by (simp add: List.insert_def) |
|
35295 | 4235 |
|
36275 | 4236 |
lemma insert_remdups: |
4237 |
"List.insert x (remdups xs) = remdups (List.insert x xs)" |
|
58807 | 4238 |
by (simp add: List.insert_def) |
36275 | 4239 |
|
34978
874150ddd50a
canonical insert operation; generalized lemma foldl_apply_inv to foldl_apply
haftmann
parents:
34942
diff
changeset
|
4240 |
|
69593 | 4241 |
subsubsection \<open>\<^const>\<open>List.union\<close>\<close> |
60758 | 4242 |
|
4243 |
text\<open>This is all one should need to know about union:\<close> |
|
57198 | 4244 |
lemma set_union[simp]: "set (List.union xs ys) = set xs \<union> set ys" |
4245 |
unfolding List.union_def |
|
4246 |
by(induct xs arbitrary: ys) simp_all |
|
4247 |
||
4248 |
lemma distinct_union[simp]: "distinct(List.union xs ys) = distinct ys" |
|
4249 |
unfolding List.union_def |
|
4250 |
by(induct xs arbitrary: ys) simp_all |
|
4251 |
||
4252 |
||
69593 | 4253 |
subsubsection \<open>\<^const>\<open>List.find\<close>\<close> |
47122 | 4254 |
|
4255 |
lemma find_None_iff: "List.find P xs = None \<longleftrightarrow> \<not> (\<exists>x. x \<in> set xs \<and> P x)" |
|
4256 |
proof (induction xs) |
|
4257 |
case Nil thus ?case by simp |
|
4258 |
next |
|
4259 |
case (Cons x xs) thus ?case by (fastforce split: if_splits) |
|
4260 |
qed |
|
4261 |
||
74802 | 4262 |
lemmas find_None_iff2 = find_None_iff[THEN eq_iff_swap] |
4263 |
||
47122 | 4264 |
lemma find_Some_iff: |
4265 |
"List.find P xs = Some x \<longleftrightarrow> |
|
4266 |
(\<exists>i<length xs. P (xs!i) \<and> x = xs!i \<and> (\<forall>j<i. \<not> P (xs!j)))" |
|
4267 |
proof (induction xs) |
|
4268 |
case Nil thus ?case by simp |
|
4269 |
next |
|
4270 |
case (Cons x xs) thus ?case |
|
56085 | 4271 |
apply(auto simp: nth_Cons' split: if_splits) |
4272 |
using diff_Suc_1[unfolded One_nat_def] less_Suc_eq_0_disj by fastforce |
|
47122 | 4273 |
qed |
4274 |
||
74802 | 4275 |
lemmas find_Some_iff2 = find_Some_iff[THEN eq_iff_swap] |
4276 |
||
47122 | 4277 |
lemma find_cong[fundef_cong]: |
64963 | 4278 |
assumes "xs = ys" and "\<And>x. x \<in> set ys \<Longrightarrow> P x = Q x" |
47122 | 4279 |
shows "List.find P xs = List.find Q ys" |
4280 |
proof (cases "List.find P xs") |
|
4281 |
case None thus ?thesis by (metis find_None_iff assms) |
|
4282 |
next |
|
4283 |
case (Some x) |
|
4284 |
hence "List.find Q ys = Some x" using assms |
|
4285 |
by (auto simp add: find_Some_iff) |
|
4286 |
thus ?thesis using Some by auto |
|
4287 |
qed |
|
4288 |
||
52379 | 4289 |
lemma find_dropWhile: |
4290 |
"List.find P xs = (case dropWhile (Not \<circ> P) xs |
|
4291 |
of [] \<Rightarrow> None |
|
4292 |
| x # _ \<Rightarrow> Some x)" |
|
58807 | 4293 |
by (induct xs) simp_all |
52379 | 4294 |
|
47122 | 4295 |
|
69593 | 4296 |
subsubsection \<open>\<^const>\<open>count_list\<close>\<close> |
60541 | 4297 |
|
75241 | 4298 |
text \<open>This library is intentionally minimal. See the remark about multisets at the point above where @{const count_list} is defined.\<close> |
4299 |
||
75233 | 4300 |
lemma count_list_append[simp]: "count_list (xs @ ys) x = count_list xs x + count_list ys x" |
4301 |
by (induction xs) simp_all |
|
4302 |
||
4303 |
lemma count_list_0_iff: "count_list xs x = 0 \<longleftrightarrow> x \<notin> set xs" |
|
4304 |
by (induction xs) simp_all |
|
4305 |
||
60541 | 4306 |
lemma count_notin[simp]: "x \<notin> set xs \<Longrightarrow> count_list xs x = 0" |
75233 | 4307 |
by(simp add: count_list_0_iff) |
59728 | 4308 |
|
60541 | 4309 |
lemma count_le_length: "count_list xs x \<le> length xs" |
59728 | 4310 |
by (induction xs) auto |
4311 |
||
75241 | 4312 |
lemma count_list_map_ge: "count_list xs x \<le> count_list (map f xs) (f x)" |
4313 |
by (induction xs) auto |
|
4314 |
||
75233 | 4315 |
lemma count_list_inj_map: |
4316 |
"\<lbrakk> inj_on f (set xs); x \<in> set xs \<rbrakk> \<Longrightarrow> count_list (map f xs) (f x) = count_list xs x" |
|
4317 |
by (induction xs) (simp, fastforce) |
|
4318 |
||
75241 | 4319 |
lemma count_list_rev[simp]: "count_list (rev xs) x = count_list xs x" |
4320 |
by (induction xs) auto |
|
4321 |
||
64267 | 4322 |
lemma sum_count_set: |
4323 |
"set xs \<subseteq> X \<Longrightarrow> finite X \<Longrightarrow> sum (count_list xs) X = length xs" |
|
68719 | 4324 |
proof (induction xs arbitrary: X) |
4325 |
case (Cons x xs) |
|
4326 |
then show ?case |
|
71585 | 4327 |
using sum.remove [of X x "count_list xs"] |
4328 |
by (auto simp: sum.If_cases simp flip: diff_eq) |
|
68719 | 4329 |
qed simp |
59728 | 4330 |
|
4331 |
||
69593 | 4332 |
subsubsection \<open>\<^const>\<open>List.extract\<close>\<close> |
55807 | 4333 |
|
4334 |
lemma extract_None_iff: "List.extract P xs = None \<longleftrightarrow> \<not> (\<exists> x\<in>set xs. P x)" |
|
4335 |
by(auto simp: extract_def dropWhile_eq_Cons_conv split: list.splits) |
|
4336 |
(metis in_set_conv_decomp) |
|
4337 |
||
4338 |
lemma extract_SomeE: |
|
4339 |
"List.extract P xs = Some (ys, y, zs) \<Longrightarrow> |
|
64963 | 4340 |
xs = ys @ y # zs \<and> P y \<and> \<not> (\<exists> y \<in> set ys. P y)" |
55807 | 4341 |
by(auto simp: extract_def dropWhile_eq_Cons_conv split: list.splits) |
4342 |
||
4343 |
lemma extract_Some_iff: |
|
4344 |
"List.extract P xs = Some (ys, y, zs) \<longleftrightarrow> |
|
64963 | 4345 |
xs = ys @ y # zs \<and> P y \<and> \<not> (\<exists> y \<in> set ys. P y)" |
55807 | 4346 |
by(auto simp: extract_def dropWhile_eq_Cons_conv dest: set_takeWhileD split: list.splits) |
4347 |
||
4348 |
lemma extract_Nil_code[code]: "List.extract P [] = None" |
|
4349 |
by(simp add: extract_def) |
|
4350 |
||
4351 |
lemma extract_Cons_code[code]: |
|
4352 |
"List.extract P (x # xs) = (if P x then Some ([], x, xs) else |
|
4353 |
(case List.extract P xs of |
|
4354 |
None \<Rightarrow> None | |
|
4355 |
Some (ys, y, zs) \<Rightarrow> Some (x#ys, y, zs)))" |
|
56085 | 4356 |
by(auto simp add: extract_def comp_def split: list.splits) |
4357 |
(metis dropWhile_eq_Nil_conv list.distinct(1)) |
|
55807 | 4358 |
|
4359 |
||
69593 | 4360 |
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
|
4361 |
|
18049 | 4362 |
lemma remove1_append: |
4363 |
"remove1 x (xs @ ys) = |
|
4364 |
(if x \<in> set xs then remove1 x xs @ ys else xs @ remove1 x ys)" |
|
4365 |
by (induct xs) auto |
|
4366 |
||
36903 | 4367 |
lemma remove1_commute: "remove1 x (remove1 y zs) = remove1 y (remove1 x zs)" |
4368 |
by (induct zs) auto |
|
4369 |
||
23479 | 4370 |
lemma in_set_remove1[simp]: |
67613 | 4371 |
"a \<noteq> b \<Longrightarrow> a \<in> set(remove1 b xs) = (a \<in> set xs)" |
68719 | 4372 |
by (induct xs) auto |
23479 | 4373 |
|
67613 | 4374 |
lemma set_remove1_subset: "set(remove1 x xs) \<subseteq> set xs" |
68719 | 4375 |
by (induct xs) auto |
15110
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
4376 |
|
67613 | 4377 |
lemma set_remove1_eq [simp]: "distinct xs \<Longrightarrow> set(remove1 x xs) = set xs - {x}" |
68719 | 4378 |
by (induct xs) auto |
15110
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
4379 |
|
23479 | 4380 |
lemma length_remove1: |
67613 | 4381 |
"length(remove1 x xs) = (if x \<in> set xs then length xs - 1 else length xs)" |
58807 | 4382 |
by (induct xs) (auto dest!:length_pos_if_in_set) |
23479 | 4383 |
|
18049 | 4384 |
lemma remove1_filter_not[simp]: |
4385 |
"\<not> P x \<Longrightarrow> remove1 x (filter P xs) = filter P xs" |
|
4386 |
by(induct xs) auto |
|
4387 |
||
39073 | 4388 |
lemma filter_remove1: |
4389 |
"filter Q (remove1 x xs) = remove1 x (filter Q xs)" |
|
4390 |
by (induct xs) auto |
|
4391 |
||
67613 | 4392 |
lemma notin_set_remove1[simp]: "x \<notin> set xs \<Longrightarrow> x \<notin> set(remove1 y xs)" |
58807 | 4393 |
by(insert set_remove1_subset) fast |
15110
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
4394 |
|
67613 | 4395 |
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
|
4396 |
by (induct xs) simp_all |
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
4397 |
|
36275 | 4398 |
lemma remove1_remdups: |
4399 |
"distinct xs \<Longrightarrow> remove1 x (remdups xs) = remdups (remove1 x xs)" |
|
58807 | 4400 |
by (induct xs) simp_all |
4401 |
||
4402 |
lemma remove1_idem: "x \<notin> set xs \<Longrightarrow> remove1 x xs = xs" |
|
4403 |
by (induct xs) simp_all |
|
37107 | 4404 |
|
73442 | 4405 |
lemma remove1_split: |
73443 | 4406 |
"a \<in> set xs \<Longrightarrow> remove1 a xs = ys \<longleftrightarrow> (\<exists>ls rs. xs = ls @ a # rs \<and> a \<notin> set ls \<and> ys = ls @ rs)" |
73442 | 4407 |
by (metis remove1.simps(2) remove1_append split_list_first) |
4408 |
||
13114 | 4409 |
|
69593 | 4410 |
subsubsection \<open>\<^const>\<open>removeAll\<close>\<close> |
27693 | 4411 |
|
34978
874150ddd50a
canonical insert operation; generalized lemma foldl_apply_inv to foldl_apply
haftmann
parents:
34942
diff
changeset
|
4412 |
lemma removeAll_filter_not_eq: |
874150ddd50a
canonical insert operation; generalized lemma foldl_apply_inv to foldl_apply
haftmann
parents:
34942
diff
changeset
|
4413 |
"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
|
4414 |
proof |
874150ddd50a
canonical insert operation; generalized lemma foldl_apply_inv to foldl_apply
haftmann
parents:
34942
diff
changeset
|
4415 |
fix xs |
874150ddd50a
canonical insert operation; generalized lemma foldl_apply_inv to foldl_apply
haftmann
parents:
34942
diff
changeset
|
4416 |
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
|
4417 |
by (induct xs) auto |
874150ddd50a
canonical insert operation; generalized lemma foldl_apply_inv to foldl_apply
haftmann
parents:
34942
diff
changeset
|
4418 |
qed |
874150ddd50a
canonical insert operation; generalized lemma foldl_apply_inv to foldl_apply
haftmann
parents:
34942
diff
changeset
|
4419 |
|
27693 | 4420 |
lemma removeAll_append[simp]: |
4421 |
"removeAll x (xs @ ys) = removeAll x xs @ removeAll x ys" |
|
4422 |
by (induct xs) auto |
|
4423 |
||
4424 |
lemma set_removeAll[simp]: "set(removeAll x xs) = set xs - {x}" |
|
4425 |
by (induct xs) auto |
|
4426 |
||
4427 |
lemma removeAll_id[simp]: "x \<notin> set xs \<Longrightarrow> removeAll x xs = xs" |
|
4428 |
by (induct xs) auto |
|
4429 |
||
46448
f1201fac7398
more specification of the quotient package in IsarRef
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
46440
diff
changeset
|
4430 |
(* Needs count:: 'a \<Rightarrow> 'a list \<Rightarrow> nat |
27693 | 4431 |
lemma length_removeAll: |
4432 |
"length(removeAll x xs) = length xs - count x xs" |
|
4433 |
*) |
|
4434 |
||
4435 |
lemma removeAll_filter_not[simp]: |
|
4436 |
"\<not> P x \<Longrightarrow> removeAll x (filter P xs) = filter P xs" |
|
4437 |
by(induct xs) auto |
|
4438 |
||
34978
874150ddd50a
canonical insert operation; generalized lemma foldl_apply_inv to foldl_apply
haftmann
parents:
34942
diff
changeset
|
4439 |
lemma distinct_removeAll: |
874150ddd50a
canonical insert operation; generalized lemma foldl_apply_inv to foldl_apply
haftmann
parents:
34942
diff
changeset
|
4440 |
"distinct xs \<Longrightarrow> distinct (removeAll x xs)" |
58807 | 4441 |
by (simp add: removeAll_filter_not_eq) |
27693 | 4442 |
|
4443 |
lemma distinct_remove1_removeAll: |
|
67613 | 4444 |
"distinct xs \<Longrightarrow> remove1 x xs = removeAll x xs" |
27693 | 4445 |
by (induct xs) simp_all |
4446 |
||
4447 |
lemma map_removeAll_inj_on: "inj_on f (insert x (set xs)) \<Longrightarrow> |
|
4448 |
map f (removeAll x xs) = removeAll (f x) (map f xs)" |
|
4449 |
by (induct xs) (simp_all add:inj_on_def) |
|
4450 |
||
4451 |
lemma map_removeAll_inj: "inj f \<Longrightarrow> |
|
4452 |
map f (removeAll x xs) = removeAll (f x) (map f xs)" |
|
56085 | 4453 |
by (rule map_removeAll_inj_on, erule subset_inj_on, rule subset_UNIV) |
27693 | 4454 |
|
63365 | 4455 |
lemma length_removeAll_less_eq [simp]: |
4456 |
"length (removeAll x xs) \<le> length xs" |
|
4457 |
by (simp add: removeAll_filter_not_eq) |
|
4458 |
||
4459 |
lemma length_removeAll_less [termination_simp]: |
|
4460 |
"x \<in> set xs \<Longrightarrow> length (removeAll x xs) < length xs" |
|
4461 |
by (auto dest: length_filter_less simp add: removeAll_filter_not_eq) |
|
4462 |
||
71593 | 4463 |
lemma distinct_concat_iff: "distinct (concat xs) \<longleftrightarrow> |
74424 | 4464 |
distinct (removeAll [] xs) \<and> |
71593 | 4465 |
(\<forall>ys. ys \<in> set xs \<longrightarrow> distinct ys) \<and> |
4466 |
(\<forall>ys zs. ys \<in> set xs \<and> zs \<in> set xs \<and> ys \<noteq> zs \<longrightarrow> set ys \<inter> set zs = {})" |
|
4467 |
apply (induct xs) |
|
4468 |
apply(simp_all, safe, auto) |
|
4469 |
by (metis Int_iff UN_I empty_iff equals0I set_empty) |
|
4470 |
||
27693 | 4471 |
|
69593 | 4472 |
subsubsection \<open>\<^const>\<open>replicate\<close>\<close> |
13114 | 4473 |
|
13142 | 4474 |
lemma length_replicate [simp]: "length (replicate n x) = n" |
13145 | 4475 |
by (induct n) auto |
13124 | 4476 |
|
58437 | 4477 |
lemma replicate_eqI: |
4478 |
assumes "length xs = n" and "\<And>y. y \<in> set xs \<Longrightarrow> y = x" |
|
4479 |
shows "xs = replicate n x" |
|
67613 | 4480 |
using assms |
4481 |
proof (induct xs arbitrary: n) |
|
58437 | 4482 |
case Nil then show ?case by simp |
4483 |
next |
|
4484 |
case (Cons x xs) then show ?case by (cases n) simp_all |
|
4485 |
qed |
|
4486 |
||
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
|
4487 |
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
|
4488 |
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
|
4489 |
|
13142 | 4490 |
lemma map_replicate [simp]: "map f (replicate n x) = replicate n (f x)" |
13145 | 4491 |
by (induct n) auto |
13114 | 4492 |
|
31363
7493b571b37d
Added theorems about distinct & concat, map & replicate and concat & replicate
hoelzl
parents:
31264
diff
changeset
|
4493 |
lemma map_replicate_const: |
7493b571b37d
Added theorems about distinct & concat, map & replicate and concat & replicate
hoelzl
parents:
31264
diff
changeset
|
4494 |
"map (\<lambda> x. k) lst = replicate (length lst) k" |
66853 | 4495 |
by (induct lst) auto |
31363
7493b571b37d
Added theorems about distinct & concat, map & replicate and concat & replicate
hoelzl
parents:
31264
diff
changeset
|
4496 |
|
13114 | 4497 |
lemma replicate_app_Cons_same: |
13145 | 4498 |
"(replicate n x) @ (x # xs) = x # replicate n x @ xs" |
4499 |
by (induct n) auto |
|
13114 | 4500 |
|
13142 | 4501 |
lemma rev_replicate [simp]: "rev (replicate n x) = replicate n x" |
58807 | 4502 |
by (induct n) (auto simp: replicate_app_Cons_same) |
13114 | 4503 |
|
13142 | 4504 |
lemma replicate_add: "replicate (n + m) x = replicate n x @ replicate m x" |
13145 | 4505 |
by (induct n) auto |
13114 | 4506 |
|
60758 | 4507 |
text\<open>Courtesy of Matthias Daum:\<close> |
16397 | 4508 |
lemma append_replicate_commute: |
4509 |
"replicate n x @ replicate k x = replicate k x @ replicate n x" |
|
68719 | 4510 |
by (metis add.commute replicate_add) |
16397 | 4511 |
|
60758 | 4512 |
text\<open>Courtesy of Andreas Lochbihler:\<close> |
31080 | 4513 |
lemma filter_replicate: |
4514 |
"filter P (replicate n x) = (if P x then replicate n x else [])" |
|
4515 |
by(induct n) auto |
|
4516 |
||
71585 | 4517 |
lemma hd_replicate [simp]: "n \<noteq> 0 \<Longrightarrow> hd (replicate n x) = x" |
13145 | 4518 |
by (induct n) auto |
13114 | 4519 |
|
46500
0196966d6d2d
removing unnecessary premises in theorems of List theory
bulwahn
parents:
46448
diff
changeset
|
4520 |
lemma tl_replicate [simp]: "tl (replicate n x) = replicate (n - 1) x" |
13145 | 4521 |
by (induct n) auto |
13114 | 4522 |
|
71585 | 4523 |
lemma last_replicate [simp]: "n \<noteq> 0 \<Longrightarrow> last (replicate n x) = x" |
13145 | 4524 |
by (atomize (full), induct n) auto |
13114 | 4525 |
|
71585 | 4526 |
lemma nth_replicate[simp]: "i < n \<Longrightarrow> (replicate n x)!i = x" |
58807 | 4527 |
by (induct n arbitrary: i)(auto simp: nth_Cons split: nat.split) |
13114 | 4528 |
|
60758 | 4529 |
text\<open>Courtesy of Matthias Daum (2 lemmas):\<close> |
16397 | 4530 |
lemma take_replicate[simp]: "take i (replicate k x) = replicate (min i k) x" |
68719 | 4531 |
proof (cases "k \<le> i") |
4532 |
case True |
|
4533 |
then show ?thesis |
|
4534 |
by (simp add: min_def) |
|
4535 |
next |
|
4536 |
case False |
|
4537 |
then have "replicate k x = replicate i x @ replicate (k - i) x" |
|
4538 |
by (simp add: replicate_add [symmetric]) |
|
4539 |
then show ?thesis |
|
4540 |
by (simp add: min_def) |
|
4541 |
qed |
|
16397 | 4542 |
|
24526 | 4543 |
lemma drop_replicate[simp]: "drop i (replicate k x) = replicate (k-i) x" |
68719 | 4544 |
proof (induct k arbitrary: i) |
4545 |
case (Suc k) |
|
4546 |
then show ?case |
|
4547 |
by (simp add: drop_Cons') |
|
4548 |
qed simp |
|
16397 | 4549 |
|
13142 | 4550 |
lemma set_replicate_Suc: "set (replicate (Suc n) x) = {x}" |
13145 | 4551 |
by (induct n) auto |
13114 | 4552 |
|
71585 | 4553 |
lemma set_replicate [simp]: "n \<noteq> 0 \<Longrightarrow> set (replicate n x) = {x}" |
13145 | 4554 |
by (fast dest!: not0_implies_Suc intro!: set_replicate_Suc) |
13114 | 4555 |
|
13142 | 4556 |
lemma set_replicate_conv_if: "set (replicate n x) = (if n = 0 then {} else {x})" |
13145 | 4557 |
by auto |
13114 | 4558 |
|
67091 | 4559 |
lemma in_set_replicate[simp]: "(x \<in> set (replicate n y)) = (x = y \<and> n \<noteq> 0)" |
37456 | 4560 |
by (simp add: set_replicate_conv_if) |
4561 |
||
76294 | 4562 |
lemma card_set_1_iff_replicate: |
4563 |
"card(set xs) = Suc 0 \<longleftrightarrow> xs \<noteq> [] \<and> (\<exists>x. xs = replicate (length xs) x)" |
|
4564 |
by (metis card_1_singleton_iff empty_iff insert_iff replicate_eqI set_empty2 set_replicate) |
|
4565 |
||
37454 | 4566 |
lemma Ball_set_replicate[simp]: |
67091 | 4567 |
"(\<forall>x \<in> set(replicate n a). P x) = (P a \<or> n=0)" |
37454 | 4568 |
by(simp add: set_replicate_conv_if) |
4569 |
||
4570 |
lemma Bex_set_replicate[simp]: |
|
67091 | 4571 |
"(\<exists>x \<in> set(replicate n a). P x) = (P a \<and> n\<noteq>0)" |
37454 | 4572 |
by(simp add: set_replicate_conv_if) |
13114 | 4573 |
|
24796 | 4574 |
lemma replicate_append_same: |
4575 |
"replicate i x @ [x] = x # replicate i x" |
|
4576 |
by (induct i) simp_all |
|
4577 |
||
4578 |
lemma map_replicate_trivial: |
|
4579 |
"map (\<lambda>i. x) [0..<i] = replicate i x" |
|
66853 | 4580 |
by (induct i) (simp_all add: replicate_append_same) |
24796 | 4581 |
|
31363
7493b571b37d
Added theorems about distinct & concat, map & replicate and concat & replicate
hoelzl
parents:
31264
diff
changeset
|
4582 |
lemma concat_replicate_trivial[simp]: |
7493b571b37d
Added theorems about distinct & concat, map & replicate and concat & replicate
hoelzl
parents:
31264
diff
changeset
|
4583 |
"concat (replicate i []) = []" |
7493b571b37d
Added theorems about distinct & concat, map & replicate and concat & replicate
hoelzl
parents:
31264
diff
changeset
|
4584 |
by (induct i) (auto simp add: map_replicate_const) |
13114 | 4585 |
|
28642 | 4586 |
lemma replicate_empty[simp]: "(replicate n x = []) \<longleftrightarrow> n=0" |
4587 |
by (induct n) auto |
|
4588 |
||
74741
6e1fad4f602b
added eq_iff_swap for creating symmetric variants of thms; applied it in List.
nipkow
parents:
74424
diff
changeset
|
4589 |
lemmas empty_replicate[simp] = replicate_empty[THEN eq_iff_swap] |
28642 | 4590 |
|
4591 |
lemma replicate_eq_replicate[simp]: |
|
67091 | 4592 |
"(replicate m x = replicate n y) \<longleftrightarrow> (m=n \<and> (m\<noteq>0 \<longrightarrow> x=y))" |
68719 | 4593 |
proof (induct m arbitrary: n) |
4594 |
case (Suc m n) |
|
4595 |
then show ?case |
|
4596 |
by (induct n) auto |
|
4597 |
qed simp |
|
28642 | 4598 |
|
71778 | 4599 |
lemma takeWhile_replicate[simp]: |
4600 |
"takeWhile P (replicate n x) = (if P x then replicate n x else [])" |
|
4601 |
using takeWhile_eq_Nil_iff by fastforce |
|
4602 |
||
4603 |
lemma dropWhile_replicate[simp]: |
|
4604 |
"dropWhile P (replicate n x) = (if P x then [] else replicate n x)" |
|
4605 |
using dropWhile_eq_self_iff by fastforce |
|
4606 |
||
39534
c798d4f1b682
generalized lemma insort_remove1 to insort_key_remove1
haftmann
parents:
39302
diff
changeset
|
4607 |
lemma replicate_length_filter: |
c798d4f1b682
generalized lemma insort_remove1 to insort_key_remove1
haftmann
parents:
39302
diff
changeset
|
4608 |
"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
|
4609 |
by (induct xs) auto |
c798d4f1b682
generalized lemma insort_remove1 to insort_key_remove1
haftmann
parents:
39302
diff
changeset
|
4610 |
|
42714
fcba668b0839
add a lemma about commutative append to List.thy
noschinl
parents:
42713
diff
changeset
|
4611 |
lemma comm_append_are_replicate: |
72732 | 4612 |
"xs @ ys = ys @ xs \<Longrightarrow> \<exists>m n zs. concat (replicate m zs) = xs \<and> concat (replicate n zs) = ys" |
72735
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72555
diff
changeset
|
4613 |
proof (induction "length (xs @ ys) + length xs" arbitrary: xs ys rule: less_induct) |
42714
fcba668b0839
add a lemma about commutative append to List.thy
noschinl
parents:
42713
diff
changeset
|
4614 |
case less |
72732 | 4615 |
consider (1) "length ys < length xs" | (2) "xs = []" | (3) "length xs \<le> length ys \<and> xs \<noteq> []" |
72735
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72555
diff
changeset
|
4616 |
by linarith |
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72555
diff
changeset
|
4617 |
then show ?case |
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72555
diff
changeset
|
4618 |
proof (cases) |
72732 | 4619 |
case 1 |
72735
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72555
diff
changeset
|
4620 |
then show ?thesis |
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72555
diff
changeset
|
4621 |
using less.hyps[OF _ less.prems[symmetric]] nat_add_left_cancel_less by auto |
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72555
diff
changeset
|
4622 |
next |
72732 | 4623 |
case 2 |
72735
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72555
diff
changeset
|
4624 |
then have "concat (replicate 0 ys) = xs \<and> concat (replicate 1 ys) = ys" |
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72555
diff
changeset
|
4625 |
by simp |
72732 | 4626 |
then show ?thesis |
72735
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72555
diff
changeset
|
4627 |
by blast |
42714
fcba668b0839
add a lemma about commutative append to List.thy
noschinl
parents:
42713
diff
changeset
|
4628 |
next |
72732 | 4629 |
case 3 |
72735
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72555
diff
changeset
|
4630 |
then have "length xs \<le> length ys" and "xs \<noteq> []" |
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72555
diff
changeset
|
4631 |
by blast+ |
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72555
diff
changeset
|
4632 |
from \<open>length xs \<le> length ys\<close> and \<open>xs @ ys = ys @ xs\<close> |
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72555
diff
changeset
|
4633 |
obtain ws where "ys = xs @ ws" |
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72555
diff
changeset
|
4634 |
by (auto simp: append_eq_append_conv2) |
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72555
diff
changeset
|
4635 |
from this and \<open>xs \<noteq> []\<close> |
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72555
diff
changeset
|
4636 |
have "length ws < length ys" |
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72555
diff
changeset
|
4637 |
by simp |
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72555
diff
changeset
|
4638 |
from \<open>xs @ ys = ys @ xs\<close>[unfolded \<open>ys = xs @ ws\<close>] |
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72555
diff
changeset
|
4639 |
have "xs @ ws = ws @ xs" |
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72555
diff
changeset
|
4640 |
by simp |
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72555
diff
changeset
|
4641 |
from less.hyps[OF _ this] \<open>length ws < length ys\<close> |
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72555
diff
changeset
|
4642 |
obtain m n' zs where "concat (replicate m zs) = xs" and "concat (replicate n' zs) = ws" |
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72555
diff
changeset
|
4643 |
by auto |
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72555
diff
changeset
|
4644 |
then have "concat (replicate (m+n') zs) = ys" |
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72555
diff
changeset
|
4645 |
using \<open>ys = xs @ ws\<close> |
42714
fcba668b0839
add a lemma about commutative append to List.thy
noschinl
parents:
42713
diff
changeset
|
4646 |
by (simp add: replicate_add) |
72732 | 4647 |
then show ?thesis |
72735
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72555
diff
changeset
|
4648 |
using \<open>concat (replicate m zs) = xs\<close> by blast |
42714
fcba668b0839
add a lemma about commutative append to List.thy
noschinl
parents:
42713
diff
changeset
|
4649 |
qed |
fcba668b0839
add a lemma about commutative append to List.thy
noschinl
parents:
42713
diff
changeset
|
4650 |
qed |
fcba668b0839
add a lemma about commutative append to List.thy
noschinl
parents:
42713
diff
changeset
|
4651 |
|
fcba668b0839
add a lemma about commutative append to List.thy
noschinl
parents:
42713
diff
changeset
|
4652 |
lemma comm_append_is_replicate: |
fcba668b0839
add a lemma about commutative append to List.thy
noschinl
parents:
42713
diff
changeset
|
4653 |
fixes xs ys :: "'a list" |
fcba668b0839
add a lemma about commutative append to List.thy
noschinl
parents:
42713
diff
changeset
|
4654 |
assumes "xs \<noteq> []" "ys \<noteq> []" |
fcba668b0839
add a lemma about commutative append to List.thy
noschinl
parents:
42713
diff
changeset
|
4655 |
assumes "xs @ ys = ys @ xs" |
fcba668b0839
add a lemma about commutative append to List.thy
noschinl
parents:
42713
diff
changeset
|
4656 |
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
|
4657 |
proof - |
fcba668b0839
add a lemma about commutative append to List.thy
noschinl
parents:
42713
diff
changeset
|
4658 |
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
|
4659 |
and "concat (replicate n zs) = ys" |
72732 | 4660 |
using comm_append_are_replicate[OF assms(3)] by blast |
42714
fcba668b0839
add a lemma about commutative append to List.thy
noschinl
parents:
42713
diff
changeset
|
4661 |
then have "m + n > 1" and "concat (replicate (m+n) zs) = xs @ ys" |
60758 | 4662 |
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
|
4663 |
by (auto simp: replicate_add) |
fcba668b0839
add a lemma about commutative append to List.thy
noschinl
parents:
42713
diff
changeset
|
4664 |
then show ?thesis by blast |
fcba668b0839
add a lemma about commutative append to List.thy
noschinl
parents:
42713
diff
changeset
|
4665 |
qed |
fcba668b0839
add a lemma about commutative append to List.thy
noschinl
parents:
42713
diff
changeset
|
4666 |
|
52380 | 4667 |
lemma Cons_replicate_eq: |
4668 |
"x # xs = replicate n y \<longleftrightarrow> x = y \<and> n > 0 \<and> xs = replicate (n - 1) x" |
|
66853 | 4669 |
by (induct n) auto |
52380 | 4670 |
|
4671 |
lemma replicate_length_same: |
|
4672 |
"(\<forall>y\<in>set xs. y = x) \<Longrightarrow> replicate (length xs) x = xs" |
|
66853 | 4673 |
by (induct xs) simp_all |
52380 | 4674 |
|
4675 |
lemma foldr_replicate [simp]: |
|
4676 |
"foldr f (replicate n x) = f x ^^ n" |
|
66853 | 4677 |
by (induct n) (simp_all) |
52380 | 4678 |
|
4679 |
lemma fold_replicate [simp]: |
|
4680 |
"fold f (replicate n x) = f x ^^ n" |
|
66853 | 4681 |
by (subst foldr_fold [symmetric]) simp_all |
52380 | 4682 |
|
28642 | 4683 |
|
69593 | 4684 |
subsubsection \<open>\<^const>\<open>enumerate\<close>\<close> |
51173 | 4685 |
|
4686 |
lemma enumerate_simps [simp, code]: |
|
4687 |
"enumerate n [] = []" |
|
4688 |
"enumerate n (x # xs) = (n, x) # enumerate (Suc n) xs" |
|
68719 | 4689 |
by (simp_all add: enumerate_eq_zip upt_rec) |
51173 | 4690 |
|
4691 |
lemma length_enumerate [simp]: |
|
4692 |
"length (enumerate n xs) = length xs" |
|
66853 | 4693 |
by (simp add: enumerate_eq_zip) |
51173 | 4694 |
|
4695 |
lemma map_fst_enumerate [simp]: |
|
4696 |
"map fst (enumerate n xs) = [n..<n + length xs]" |
|
66853 | 4697 |
by (simp add: enumerate_eq_zip) |
51173 | 4698 |
|
4699 |
lemma map_snd_enumerate [simp]: |
|
4700 |
"map snd (enumerate n xs) = xs" |
|
66853 | 4701 |
by (simp add: enumerate_eq_zip) |
64963 | 4702 |
|
51173 | 4703 |
lemma in_set_enumerate_eq: |
4704 |
"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" |
|
4705 |
proof - |
|
4706 |
{ fix m |
|
4707 |
assume "n \<le> m" |
|
4708 |
moreover assume "m < length xs + n" |
|
4709 |
ultimately have "[n..<n + length xs] ! (m - n) = m \<and> |
|
4710 |
xs ! (m - n) = xs ! (m - n) \<and> m - n < length xs" by auto |
|
4711 |
then have "\<exists>q. [n..<n + length xs] ! q = m \<and> |
|
4712 |
xs ! q = xs ! (m - n) \<and> q < length xs" .. |
|
4713 |
} then show ?thesis by (cases p) (auto simp add: enumerate_eq_zip in_set_zip) |
|
4714 |
qed |
|
4715 |
||
66853 | 4716 |
lemma nth_enumerate_eq: "m < length xs \<Longrightarrow> enumerate n xs ! m = (n + m, xs ! m)" |
4717 |
by (simp add: enumerate_eq_zip) |
|
51173 | 4718 |
|
4719 |
lemma enumerate_replicate_eq: |
|
4720 |
"enumerate n (replicate m a) = map (\<lambda>q. (q, a)) [n..<n + m]" |
|
66853 | 4721 |
by (rule pair_list_eqI) |
4722 |
(simp_all add: enumerate_eq_zip comp_def map_replicate_const) |
|
51173 | 4723 |
|
4724 |
lemma enumerate_Suc_eq: |
|
4725 |
"enumerate (Suc n) xs = map (apfst Suc) (enumerate n xs)" |
|
66853 | 4726 |
by (rule pair_list_eqI) |
4727 |
(simp_all add: not_le, simp del: map_map add: map_Suc_upt map_map [symmetric]) |
|
51173 | 4728 |
|
52379 | 4729 |
lemma distinct_enumerate [simp]: |
4730 |
"distinct (enumerate n xs)" |
|
66853 | 4731 |
by (simp add: enumerate_eq_zip distinct_zipI1) |
52379 | 4732 |
|
58437 | 4733 |
lemma enumerate_append_eq: |
4734 |
"enumerate n (xs @ ys) = enumerate n xs @ enumerate (n + length xs) ys" |
|
68719 | 4735 |
by (simp add: enumerate_eq_zip add.assoc zip_append2) |
58437 | 4736 |
|
4737 |
lemma enumerate_map_upt: |
|
4738 |
"enumerate n (map f [n..<m]) = map (\<lambda>k. (k, f k)) [n..<m]" |
|
66853 | 4739 |
by (cases "n \<le> m") (simp_all add: zip_map2 zip_same_conv_map enumerate_eq_zip) |
64963 | 4740 |
|
51173 | 4741 |
|
69593 | 4742 |
subsubsection \<open>\<^const>\<open>rotate1\<close> and \<^const>\<open>rotate\<close>\<close> |
15302 | 4743 |
|
4744 |
lemma rotate0[simp]: "rotate 0 = id" |
|
4745 |
by(simp add:rotate_def) |
|
4746 |
||
4747 |
lemma rotate_Suc[simp]: "rotate (Suc n) xs = rotate1(rotate n xs)" |
|
4748 |
by(simp add:rotate_def) |
|
4749 |
||
4750 |
lemma rotate_add: |
|
67091 | 4751 |
"rotate (m+n) = rotate m \<circ> rotate n" |
15302 | 4752 |
by(simp add:rotate_def funpow_add) |
4753 |
||
4754 |
lemma rotate_rotate: "rotate m (rotate n xs) = rotate (m+n) xs" |
|
4755 |
by(simp add:rotate_add) |
|
4756 |
||
71393
fce780f9c9c6
new examples of BNF lifting across quotients using a new theory of confluence,
traytel
parents:
70911
diff
changeset
|
4757 |
lemma rotate1_map: "rotate1 (map f xs) = map f (rotate1 xs)" |
fce780f9c9c6
new examples of BNF lifting across quotients using a new theory of confluence,
traytel
parents:
70911
diff
changeset
|
4758 |
by(cases xs) simp_all |
fce780f9c9c6
new examples of BNF lifting across quotients using a new theory of confluence,
traytel
parents:
70911
diff
changeset
|
4759 |
|
18049 | 4760 |
lemma rotate1_rotate_swap: "rotate1 (rotate n xs) = rotate n (rotate1 xs)" |
4761 |
by(simp add:rotate_def funpow_swap1) |
|
4762 |
||
68709 | 4763 |
lemma rotate1_length01[simp]: "length xs \<le> 1 \<Longrightarrow> rotate1 xs = xs" |
15302 | 4764 |
by(cases xs) simp_all |
4765 |
||
68709 | 4766 |
lemma rotate_length01[simp]: "length xs \<le> 1 \<Longrightarrow> rotate n xs = xs" |
68719 | 4767 |
by (induct n) (simp_all add:rotate_def) |
13114 | 4768 |
|
15302 | 4769 |
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
|
4770 |
by (cases xs) simp_all |
15302 | 4771 |
|
4772 |
lemma rotate_drop_take: |
|
4773 |
"rotate n xs = drop (n mod length xs) xs @ take (n mod length xs) xs" |
|
68719 | 4774 |
proof (induct n) |
4775 |
case (Suc n) |
|
4776 |
show ?case |
|
4777 |
proof (cases "xs = []") |
|
4778 |
case False |
|
4779 |
then show ?thesis |
|
4780 |
proof (cases "n mod length xs = 0") |
|
4781 |
case True |
|
4782 |
then show ?thesis |
|
71585 | 4783 |
by (auto simp add: mod_Suc False Suc.hyps drop_Suc rotate1_hd_tl take_Suc Suc_length_conv) |
68719 | 4784 |
next |
4785 |
case False |
|
4786 |
with \<open>xs \<noteq> []\<close> Suc |
|
4787 |
show ?thesis |
|
4788 |
by (simp add: rotate_def mod_Suc rotate1_hd_tl drop_Suc[symmetric] drop_tl[symmetric] |
|
4789 |
take_hd_drop linorder_not_le) |
|
4790 |
qed |
|
4791 |
qed simp |
|
4792 |
qed simp |
|
13114 | 4793 |
|
15302 | 4794 |
lemma rotate_conv_mod: "rotate n xs = rotate (n mod length xs) xs" |
71585 | 4795 |
by(simp add:rotate_drop_take) |
15302 | 4796 |
|
4797 |
lemma rotate_id[simp]: "n mod length xs = 0 \<Longrightarrow> rotate n xs = xs" |
|
71585 | 4798 |
by(simp add:rotate_drop_take) |
15302 | 4799 |
|
4800 |
lemma length_rotate1[simp]: "length(rotate1 xs) = length xs" |
|
71585 | 4801 |
by (cases xs) simp_all |
15302 | 4802 |
|
24526 | 4803 |
lemma length_rotate[simp]: "length(rotate n xs) = length xs" |
71585 | 4804 |
by (induct n arbitrary: xs) (simp_all add:rotate_def) |
15302 | 4805 |
|
4806 |
lemma distinct1_rotate[simp]: "distinct(rotate1 xs) = distinct xs" |
|
71585 | 4807 |
by (cases xs) auto |
15302 | 4808 |
|
4809 |
lemma distinct_rotate[simp]: "distinct(rotate n xs) = distinct xs" |
|
71585 | 4810 |
by (induct n) (simp_all add:rotate_def) |
15302 | 4811 |
|
4812 |
lemma rotate_map: "rotate n (map f xs) = map f (rotate n xs)" |
|
71585 | 4813 |
by(simp add:rotate_drop_take take_map drop_map) |
15302 | 4814 |
|
4815 |
lemma set_rotate1[simp]: "set(rotate1 xs) = set xs" |
|
71585 | 4816 |
by (cases xs) auto |
15302 | 4817 |
|
4818 |
lemma set_rotate[simp]: "set(rotate n xs) = set xs" |
|
71585 | 4819 |
by (induct n) (simp_all add:rotate_def) |
15302 | 4820 |
|
76294 | 4821 |
lemma rotate1_replicate[simp]: "rotate1 (replicate n a) = replicate n a" |
4822 |
by (cases n) (simp_all add: replicate_append_same) |
|
4823 |
||
15302 | 4824 |
lemma rotate1_is_Nil_conv[simp]: "(rotate1 xs = []) = (xs = [])" |
71585 | 4825 |
by (cases xs) auto |
15302 | 4826 |
|
4827 |
lemma rotate_is_Nil_conv[simp]: "(rotate n xs = []) = (xs = [])" |
|
71585 | 4828 |
by (induct n) (simp_all add:rotate_def) |
13114 | 4829 |
|
15439 | 4830 |
lemma rotate_rev: |
4831 |
"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
|
4832 |
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
|
4833 |
case False |
2f4e2aab190a
Generalising and renaming some basic results
paulson <lp15@cam.ac.uk>
parents:
68362
diff
changeset
|
4834 |
then show ?thesis |
2f4e2aab190a
Generalising and renaming some basic results
paulson <lp15@cam.ac.uk>
parents:
68362
diff
changeset
|
4835 |
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
|
4836 |
qed force |
15439 | 4837 |
|
68719 | 4838 |
lemma hd_rotate_conv_nth: |
4839 |
assumes "xs \<noteq> []" shows "hd(rotate n xs) = xs!(n mod length xs)" |
|
4840 |
proof - |
|
4841 |
have "n mod length xs < length xs" |
|
4842 |
using assms by simp |
|
4843 |
then show ?thesis |
|
4844 |
by (metis drop_eq_Nil hd_append2 hd_drop_conv_nth leD rotate_drop_take) |
|
4845 |
qed |
|
18423 | 4846 |
|
68527
2f4e2aab190a
Generalising and renaming some basic results
paulson <lp15@cam.ac.uk>
parents:
68362
diff
changeset
|
4847 |
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
|
4848 |
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
|
4849 |
|
71991 | 4850 |
lemma nth_rotate: |
4851 |
\<open>rotate m xs ! n = xs ! ((m + n) mod length xs)\<close> if \<open>n < length xs\<close> |
|
4852 |
using that apply (auto simp add: rotate_drop_take nth_append not_less less_diff_conv ac_simps dest!: le_Suc_ex) |
|
4853 |
apply (metis add.commute mod_add_right_eq mod_less) |
|
4854 |
apply (metis (no_types, lifting) Nat.diff_diff_right add.commute add_diff_cancel_right' diff_le_self dual_order.strict_trans2 length_greater_0_conv less_nat_zero_code list.size(3) mod_add_right_eq mod_add_self2 mod_le_divisor mod_less) |
|
4855 |
done |
|
4856 |
||
4857 |
lemma nth_rotate1: |
|
4858 |
\<open>rotate1 xs ! n = xs ! (Suc n mod length xs)\<close> if \<open>n < length xs\<close> |
|
4859 |
using that nth_rotate [of n xs 1] by simp |
|
4860 |
||
76294 | 4861 |
lemma inj_rotate1: "inj rotate1" |
4862 |
proof |
|
4863 |
fix xs ys :: "'a list" show "rotate1 xs = rotate1 ys \<Longrightarrow> xs = ys" |
|
4864 |
by (cases xs; cases ys; simp) |
|
4865 |
qed |
|
4866 |
||
4867 |
lemma surj_rotate1: "surj rotate1" |
|
4868 |
proof (safe, simp_all) |
|
4869 |
fix xs :: "'a list" show "xs \<in> range rotate1" |
|
4870 |
proof (cases xs rule: rev_exhaust) |
|
4871 |
case Nil |
|
4872 |
hence "xs = rotate1 []" by auto |
|
4873 |
thus ?thesis by fast |
|
4874 |
next |
|
4875 |
case (snoc as a) |
|
4876 |
hence "xs = rotate1 (a#as)" by force |
|
4877 |
thus ?thesis by fast |
|
4878 |
qed |
|
4879 |
qed |
|
4880 |
||
4881 |
lemma bij_rotate1: "bij (rotate1 :: 'a list \<Rightarrow> 'a list)" |
|
4882 |
using bijI inj_rotate1 surj_rotate1 by blast |
|
4883 |
||
4884 |
lemma rotate1_fixpoint_card: "rotate1 xs = xs \<Longrightarrow> xs = [] \<or> card(set xs) = 1" |
|
4885 |
by(induction xs) (auto simp: card_insert_if[OF finite_set] append_eq_Cons_conv) |
|
4886 |
||
13114 | 4887 |
|
69593 | 4888 |
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
|
4889 |
|
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65350
diff
changeset
|
4890 |
lemma nths_empty [simp]: "nths xs {} = []" |
71585 | 4891 |
by (auto simp add: nths_def) |
65956
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65350
diff
changeset
|
4892 |
|
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65350
diff
changeset
|
4893 |
lemma nths_nil [simp]: "nths [] A = []" |
71585 | 4894 |
by (auto simp add: nths_def) |
65956
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65350
diff
changeset
|
4895 |
|
69203 | 4896 |
lemma nths_all: "\<forall>i < length xs. i \<in> I \<Longrightarrow> nths xs I = xs" |
4897 |
apply (simp add: nths_def) |
|
4898 |
apply (subst filter_True) |
|
71585 | 4899 |
apply (auto simp: in_set_zip subset_iff) |
69203 | 4900 |
done |
4901 |
||
65956
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65350
diff
changeset
|
4902 |
lemma length_nths: |
67613 | 4903 |
"length (nths xs I) = card{i. i < length xs \<and> i \<in> I}" |
71585 | 4904 |
by(simp add: nths_def length_filter_conv_card cong:conj_cong) |
65956
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65350
diff
changeset
|
4905 |
|
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65350
diff
changeset
|
4906 |
lemma nths_shift_lemma_Suc: |
68719 | 4907 |
"map fst (filter (\<lambda>p. P(Suc(snd p))) (zip xs is)) = |
4908 |
map fst (filter (\<lambda>p. P(snd p)) (zip xs (map Suc is)))" |
|
4909 |
proof (induct xs arbitrary: "is") |
|
4910 |
case (Cons x xs "is") |
|
4911 |
show ?case |
|
4912 |
by (cases "is") (auto simp add: Cons.hyps) |
|
4913 |
qed simp |
|
15281 | 4914 |
|
65956
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65350
diff
changeset
|
4915 |
lemma nths_shift_lemma: |
68249
949d93804740
First step to remove nonstandard "[x <- xs. P]" syntax: only input
nipkow
parents:
68215
diff
changeset
|
4916 |
"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
|
4917 |
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
|
4918 |
by (induct xs rule: rev_induct) (simp_all add: add.commute) |
13114 | 4919 |
|
65956
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65350
diff
changeset
|
4920 |
lemma nths_append: |
67613 | 4921 |
"nths (l @ l') A = nths l A @ nths l' {j. j + length l \<in> A}" |
68719 | 4922 |
unfolding nths_def |
4923 |
proof (induct l' rule: rev_induct) |
|
4924 |
case (snoc x xs) |
|
4925 |
then show ?case |
|
4926 |
by (simp add: upt_add_eq_append[of 0] nths_shift_lemma add.commute) |
|
4927 |
qed auto |
|
13114 | 4928 |
|
65956
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65350
diff
changeset
|
4929 |
lemma nths_Cons: |
67613 | 4930 |
"nths (x # l) A = (if 0 \<in> A then [x] else []) @ nths l {j. Suc j \<in> A}" |
68719 | 4931 |
proof (induct l rule: rev_induct) |
4932 |
case (snoc x xs) |
|
4933 |
then show ?case |
|
4934 |
by (simp flip: append_Cons add: nths_append) |
|
4935 |
qed (auto simp: nths_def) |
|
13114 | 4936 |
|
66853 | 4937 |
lemma nths_map: "nths (map f xs) I = map f (nths xs I)" |
71585 | 4938 |
by(induction xs arbitrary: I) (simp_all add: nths_Cons) |
66853 | 4939 |
|
65956
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65350
diff
changeset
|
4940 |
lemma set_nths: "set(nths xs I) = {xs!i|i. i<size xs \<and> i \<in> I}" |
71585 | 4941 |
by (induct xs arbitrary: I) (auto simp: nths_Cons nth_Cons split:nat.split dest!: gr0_implies_Suc) |
15281 | 4942 |
|
65956
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65350
diff
changeset
|
4943 |
lemma set_nths_subset: "set(nths xs I) \<subseteq> set xs" |
71585 | 4944 |
by(auto simp add:set_nths) |
65956
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65350
diff
changeset
|
4945 |
|
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65350
diff
changeset
|
4946 |
lemma notin_set_nthsI[simp]: "x \<notin> set xs \<Longrightarrow> x \<notin> set(nths xs I)" |
71585 | 4947 |
by(auto simp add:set_nths) |
65956
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65350
diff
changeset
|
4948 |
|
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65350
diff
changeset
|
4949 |
lemma in_set_nthsD: "x \<in> set(nths xs I) \<Longrightarrow> x \<in> set xs" |
71585 | 4950 |
by(auto simp add:set_nths) |
65956
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65350
diff
changeset
|
4951 |
|
67613 | 4952 |
lemma nths_singleton [simp]: "nths [x] A = (if 0 \<in> A then [x] else [])" |
71585 | 4953 |
by (simp add: nths_Cons) |
65956
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65350
diff
changeset
|
4954 |
|
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65350
diff
changeset
|
4955 |
lemma distinct_nthsI[simp]: "distinct xs \<Longrightarrow> distinct (nths xs I)" |
71585 | 4956 |
by (induct xs arbitrary: I) (auto simp: nths_Cons) |
65956
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65350
diff
changeset
|
4957 |
|
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65350
diff
changeset
|
4958 |
lemma nths_upt_eq_take [simp]: "nths l {..<n} = take n l" |
71585 | 4959 |
by (induct l rule: rev_induct) (simp_all split: nat_diff_split add: nths_append) |
66656 | 4960 |
|
69203 | 4961 |
lemma nths_nths: "nths (nths xs A) B = nths xs {i \<in> A. \<exists>j \<in> B. card {i' \<in> A. i' < i} = j}" |
71585 | 4962 |
by (induction xs arbitrary: A B) (auto simp add: nths_Cons card_less_Suc card_less_Suc2) |
69203 | 4963 |
|
4964 |
lemma drop_eq_nths: "drop n xs = nths xs {i. i \<ge> n}" |
|
71585 | 4965 |
by (induction xs arbitrary: n) (auto simp add: nths_Cons nths_all drop_Cons' intro: arg_cong2[where f=nths, OF refl]) |
69203 | 4966 |
|
4967 |
lemma nths_drop: "nths (drop n xs) I = nths xs ((+) n ` I)" |
|
4968 |
by(force simp: drop_eq_nths nths_nths simp flip: atLeastLessThan_iff |
|
4969 |
intro: arg_cong2[where f=nths, OF refl]) |
|
4970 |
||
66656 | 4971 |
lemma filter_eq_nths: "filter P xs = nths xs {i. i<length xs \<and> P(xs!i)}" |
71585 | 4972 |
by(induction xs) (auto simp: nths_Cons) |
65956
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65350
diff
changeset
|
4973 |
|
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65350
diff
changeset
|
4974 |
lemma filter_in_nths: |
66656 | 4975 |
"distinct xs \<Longrightarrow> filter (%x. x \<in> set (nths xs s)) xs = nths xs s" |
24526 | 4976 |
proof (induct xs arbitrary: s) |
17501 | 4977 |
case Nil thus ?case by simp |
4978 |
next |
|
4979 |
case (Cons a xs) |
|
67091 | 4980 |
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
|
4981 |
with Cons show ?case by(simp add: nths_Cons cong:filter_cong) |
17501 | 4982 |
qed |
4983 |
||
13114 | 4984 |
|
69593 | 4985 |
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
|
4986 |
|
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65350
diff
changeset
|
4987 |
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
|
4988 |
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
|
4989 |
|
65956
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65350
diff
changeset
|
4990 |
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
|
4991 |
proof - |
744934b818c7
moved quite generic material from theory Enum to more appropriate places
haftmann
parents:
49808
diff
changeset
|
4992 |
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
|
4993 |
by (auto simp add: image_def) |
65956
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65350
diff
changeset
|
4994 |
have "set (map set (subseqs xs)) = Pow (set xs)" |
64963 | 4995 |
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
|
4996 |
then show ?thesis by simp |
744934b818c7
moved quite generic material from theory Enum to more appropriate places
haftmann
parents:
49808
diff
changeset
|
4997 |
qed |
744934b818c7
moved quite generic material from theory Enum to more appropriate places
haftmann
parents:
49808
diff
changeset
|
4998 |
|
65956
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65350
diff
changeset
|
4999 |
lemma distinct_set_subseqs: |
49948
744934b818c7
moved quite generic material from theory Enum to more appropriate places
haftmann
parents:
49808
diff
changeset
|
5000 |
assumes "distinct xs" |
65956
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65350
diff
changeset
|
5001 |
shows "distinct (map set (subseqs xs))" |
77433 | 5002 |
by (simp add: assms card_Pow card_distinct distinct_card length_subseqs subseqs_powset) |
49948
744934b818c7
moved quite generic material from theory Enum to more appropriate places
haftmann
parents:
49808
diff
changeset
|
5003 |
|
744934b818c7
moved quite generic material from theory Enum to more appropriate places
haftmann
parents:
49808
diff
changeset
|
5004 |
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
|
5005 |
by (induct n) simp_all |
744934b818c7
moved quite generic material from theory Enum to more appropriate places
haftmann
parents:
49808
diff
changeset
|
5006 |
|
744934b818c7
moved quite generic material from theory Enum to more appropriate places
haftmann
parents:
49808
diff
changeset
|
5007 |
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
|
5008 |
by (induct n arbitrary: ys) auto |
744934b818c7
moved quite generic material from theory Enum to more appropriate places
haftmann
parents:
49808
diff
changeset
|
5009 |
|
744934b818c7
moved quite generic material from theory Enum to more appropriate places
haftmann
parents:
49808
diff
changeset
|
5010 |
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
|
5011 |
proof (rule set_eqI) |
744934b818c7
moved quite generic material from theory Enum to more appropriate places
haftmann
parents:
49808
diff
changeset
|
5012 |
fix ys :: "'a list" |
744934b818c7
moved quite generic material from theory Enum to more appropriate places
haftmann
parents:
49808
diff
changeset
|
5013 |
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
|
5014 |
proof - |
744934b818c7
moved quite generic material from theory Enum to more appropriate places
haftmann
parents:
49808
diff
changeset
|
5015 |
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
|
5016 |
by (induct n arbitrary: ys) auto |
744934b818c7
moved quite generic material from theory Enum to more appropriate places
haftmann
parents:
49808
diff
changeset
|
5017 |
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
|
5018 |
by (induct n arbitrary: ys) auto |
744934b818c7
moved quite generic material from theory Enum to more appropriate places
haftmann
parents:
49808
diff
changeset
|
5019 |
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
|
5020 |
by (induct ys) auto |
744934b818c7
moved quite generic material from theory Enum to more appropriate places
haftmann
parents:
49808
diff
changeset
|
5021 |
ultimately show ?thesis by auto |
744934b818c7
moved quite generic material from theory Enum to more appropriate places
haftmann
parents:
49808
diff
changeset
|
5022 |
qed |
744934b818c7
moved quite generic material from theory Enum to more appropriate places
haftmann
parents:
49808
diff
changeset
|
5023 |
qed |
744934b818c7
moved quite generic material from theory Enum to more appropriate places
haftmann
parents:
49808
diff
changeset
|
5024 |
|
65956
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65350
diff
changeset
|
5025 |
lemma subseqs_refl: "xs \<in> set (subseqs xs)" |
64963 | 5026 |
by (induct xs) (simp_all add: Let_def) |
63561
fba08009ff3e
add lemmas contributed by Peter Gammie
Andreas Lochbihler
parents:
63540
diff
changeset
|
5027 |
|
65956
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65350
diff
changeset
|
5028 |
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
|
5029 |
unfolding subseqs_powset by simp |
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65350
diff
changeset
|
5030 |
|
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65350
diff
changeset
|
5031 |
lemma Cons_in_subseqsD: "y # ys \<in> set (subseqs xs) \<Longrightarrow> ys \<in> set (subseqs xs)" |
64963 | 5032 |
by (induct xs) (auto simp: Let_def) |
63561
fba08009ff3e
add lemmas contributed by Peter Gammie
Andreas Lochbihler
parents:
63540
diff
changeset
|
5033 |
|
65956
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65350
diff
changeset
|
5034 |
lemma subseqs_distinctD: "\<lbrakk> ys \<in> set (subseqs xs); distinct xs \<rbrakk> \<Longrightarrow> distinct ys" |
64963 | 5035 |
proof (induct xs arbitrary: ys) |
63561
fba08009ff3e
add lemmas contributed by Peter Gammie
Andreas Lochbihler
parents:
63540
diff
changeset
|
5036 |
case (Cons x xs ys) |
fba08009ff3e
add lemmas contributed by Peter Gammie
Andreas Lochbihler
parents:
63540
diff
changeset
|
5037 |
then show ?case |
65956
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65350
diff
changeset
|
5038 |
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
|
5039 |
qed simp |
fba08009ff3e
add lemmas contributed by Peter Gammie
Andreas Lochbihler
parents:
63540
diff
changeset
|
5040 |
|
49948
744934b818c7
moved quite generic material from theory Enum to more appropriate places
haftmann
parents:
49808
diff
changeset
|
5041 |
|
69593 | 5042 |
subsubsection \<open>\<^const>\<open>splice\<close>\<close> |
19390 | 5043 |
|
69075 | 5044 |
lemma splice_Nil2 [simp]: "splice xs [] = xs" |
71585 | 5045 |
by (cases xs) simp_all |
19390 | 5046 |
|
24526 | 5047 |
lemma length_splice[simp]: "length(splice xs ys) = length xs + length ys" |
71585 | 5048 |
by (induct xs ys rule: splice.induct) auto |
69136 | 5049 |
|
5050 |
lemma split_Nil_iff[simp]: "splice xs ys = [] \<longleftrightarrow> xs = [] \<and> ys = []" |
|
71585 | 5051 |
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
|
5052 |
|
69140 | 5053 |
lemma splice_replicate[simp]: "splice (replicate m x) (replicate n x) = replicate (m+n) x" |
71585 | 5054 |
proof (induction "replicate m x" "replicate n x" arbitrary: m n rule: splice.induct) |
5055 |
case (2 x xs) |
|
74424 | 5056 |
then show ?case |
71585 | 5057 |
by (auto simp add: Cons_replicate_eq dest: gr0_implies_Suc) |
5058 |
qed auto |
|
65350
b149abe619f7
added shuffle product to HOL/List
eberlm <eberlm@in.tum.de>
parents:
64966
diff
changeset
|
5059 |
|
69593 | 5060 |
subsubsection \<open>\<^const>\<open>shuffles\<close>\<close> |
69107 | 5061 |
|
5062 |
lemma shuffles_commutes: "shuffles xs ys = shuffles ys xs" |
|
5063 |
by (induction xs ys rule: shuffles.induct) (simp_all add: Un_commute) |
|
5064 |
||
5065 |
lemma Nil_in_shuffles[simp]: "[] \<in> shuffles xs ys \<longleftrightarrow> xs = [] \<and> ys = []" |
|
5066 |
by (induct xs ys rule: shuffles.induct) auto |
|
5067 |
||
5068 |
lemma shufflesE: |
|
5069 |
"zs \<in> shuffles xs ys \<Longrightarrow> |
|
65350
b149abe619f7
added shuffle product to HOL/List
eberlm <eberlm@in.tum.de>
parents:
64966
diff
changeset
|
5070 |
(zs = xs \<Longrightarrow> ys = [] \<Longrightarrow> P) \<Longrightarrow> |
b149abe619f7
added shuffle product to HOL/List
eberlm <eberlm@in.tum.de>
parents:
64966
diff
changeset
|
5071 |
(zs = ys \<Longrightarrow> xs = [] \<Longrightarrow> P) \<Longrightarrow> |
69107 | 5072 |
(\<And>x xs' z zs'. xs = x # xs' \<Longrightarrow> zs = z # zs' \<Longrightarrow> x = z \<Longrightarrow> zs' \<in> shuffles xs' ys \<Longrightarrow> P) \<Longrightarrow> |
5073 |
(\<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" |
|
5074 |
by (induct xs ys rule: shuffles.induct) auto |
|
5075 |
||
5076 |
lemma Cons_in_shuffles_iff: |
|
5077 |
"z # zs \<in> shuffles xs ys \<longleftrightarrow> |
|
5078 |
(xs \<noteq> [] \<and> hd xs = z \<and> zs \<in> shuffles (tl xs) ys \<or> |
|
5079 |
ys \<noteq> [] \<and> hd ys = z \<and> zs \<in> shuffles xs (tl ys))" |
|
5080 |
by (induct xs ys rule: shuffles.induct) auto |
|
5081 |
||
5082 |
lemma splice_in_shuffles [simp, intro]: "splice xs ys \<in> shuffles xs ys" |
|
71585 | 5083 |
by (induction xs ys rule: splice.induct) (simp_all add: Cons_in_shuffles_iff shuffles_commutes) |
69107 | 5084 |
|
5085 |
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
|
5086 |
by simp |
66892 | 5087 |
|
69107 | 5088 |
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
|
5089 |
by (cases ys) auto |
b149abe619f7
added shuffle product to HOL/List
eberlm <eberlm@in.tum.de>
parents:
64966
diff
changeset
|
5090 |
|
69107 | 5091 |
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
|
5092 |
by (cases xs) auto |
b149abe619f7
added shuffle product to HOL/List
eberlm <eberlm@in.tum.de>
parents:
64966
diff
changeset
|
5093 |
|
69107 | 5094 |
lemma finite_shuffles [simp, intro]: "finite (shuffles xs ys)" |
5095 |
by (induction xs ys rule: shuffles.induct) simp_all |
|
5096 |
||
5097 |
lemma length_shuffles: "zs \<in> shuffles xs ys \<Longrightarrow> length zs = length xs + length ys" |
|
5098 |
by (induction xs ys arbitrary: zs rule: shuffles.induct) auto |
|
5099 |
||
5100 |
lemma set_shuffles: "zs \<in> shuffles xs ys \<Longrightarrow> set zs = set xs \<union> set ys" |
|
5101 |
by (induction xs ys arbitrary: zs rule: shuffles.induct) auto |
|
5102 |
||
5103 |
lemma distinct_disjoint_shuffles: |
|
5104 |
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
|
5105 |
shows "distinct zs" |
b149abe619f7
added shuffle product to HOL/List
eberlm <eberlm@in.tum.de>
parents:
64966
diff
changeset
|
5106 |
using assms |
69107 | 5107 |
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
|
5108 |
case (3 x xs y ys) |
b149abe619f7
added shuffle product to HOL/List
eberlm <eberlm@in.tum.de>
parents:
64966
diff
changeset
|
5109 |
show ?case |
b149abe619f7
added shuffle product to HOL/List
eberlm <eberlm@in.tum.de>
parents:
64966
diff
changeset
|
5110 |
proof (cases zs) |
b149abe619f7
added shuffle product to HOL/List
eberlm <eberlm@in.tum.de>
parents:
64966
diff
changeset
|
5111 |
case (Cons z zs') |
69107 | 5112 |
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
|
5113 |
qed simp_all |
b149abe619f7
added shuffle product to HOL/List
eberlm <eberlm@in.tum.de>
parents:
64966
diff
changeset
|
5114 |
qed simp_all |
b149abe619f7
added shuffle product to HOL/List
eberlm <eberlm@in.tum.de>
parents:
64966
diff
changeset
|
5115 |
|
69107 | 5116 |
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
|
5117 |
by (cases ys) auto |
66892 | 5118 |
|
69107 | 5119 |
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
|
5120 |
by (cases xs) auto |
b149abe619f7
added shuffle product to HOL/List
eberlm <eberlm@in.tum.de>
parents:
64966
diff
changeset
|
5121 |
|
69107 | 5122 |
lemma filter_shuffles: |
5123 |
"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
|
5124 |
proof - |
67399 | 5125 |
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
|
5126 |
by (auto simp: image_image) |
b149abe619f7
added shuffle product to HOL/List
eberlm <eberlm@in.tum.de>
parents:
64966
diff
changeset
|
5127 |
show ?thesis |
69107 | 5128 |
by (induction xs ys rule: shuffles.induct) |
66892 | 5129 |
(simp_all split: if_splits add: image_Un * Un_absorb1 Un_absorb2 |
69107 | 5130 |
Cons_shuffles_subset1 Cons_shuffles_subset2) |
65350
b149abe619f7
added shuffle product to HOL/List
eberlm <eberlm@in.tum.de>
parents:
64966
diff
changeset
|
5131 |
qed |
b149abe619f7
added shuffle product to HOL/List
eberlm <eberlm@in.tum.de>
parents:
64966
diff
changeset
|
5132 |
|
69107 | 5133 |
lemma filter_shuffles_disjoint1: |
5134 |
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
|
5135 |
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
|
5136 |
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
|
5137 |
using assms |
b149abe619f7
added shuffle product to HOL/List
eberlm <eberlm@in.tum.de>
parents:
64966
diff
changeset
|
5138 |
proof - |
69107 | 5139 |
from assms have "filter ?P zs \<in> filter ?P ` shuffles xs ys" by blast |
5140 |
also have "filter ?P ` shuffles xs ys = shuffles (filter ?P xs) (filter ?P ys)" |
|
5141 |
by (rule filter_shuffles) |
|
65350
b149abe619f7
added shuffle product to HOL/List
eberlm <eberlm@in.tum.de>
parents:
64966
diff
changeset
|
5142 |
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
|
5143 |
also have "filter ?P ys = []" by (rule filter_False) (insert assms(1), auto) |
69107 | 5144 |
also have "shuffles xs [] = {xs}" by simp |
65350
b149abe619f7
added shuffle product to HOL/List
eberlm <eberlm@in.tum.de>
parents:
64966
diff
changeset
|
5145 |
finally show "filter ?P zs = xs" by simp |
b149abe619f7
added shuffle product to HOL/List
eberlm <eberlm@in.tum.de>
parents:
64966
diff
changeset
|
5146 |
next |
69107 | 5147 |
from assms have "filter ?Q zs \<in> filter ?Q ` shuffles xs ys" by blast |
5148 |
also have "filter ?Q ` shuffles xs ys = shuffles (filter ?Q xs) (filter ?Q ys)" |
|
5149 |
by (rule filter_shuffles) |
|
65350
b149abe619f7
added shuffle product to HOL/List
eberlm <eberlm@in.tum.de>
parents:
64966
diff
changeset
|
5150 |
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
|
5151 |
also have "filter ?Q xs = []" by (rule filter_False) (insert assms(1), auto) |
69107 | 5152 |
also have "shuffles [] ys = {ys}" by simp |
65350
b149abe619f7
added shuffle product to HOL/List
eberlm <eberlm@in.tum.de>
parents:
64966
diff
changeset
|
5153 |
finally show "filter ?Q zs = ys" by simp |
b149abe619f7
added shuffle product to HOL/List
eberlm <eberlm@in.tum.de>
parents:
64966
diff
changeset
|
5154 |
qed |
b149abe619f7
added shuffle product to HOL/List
eberlm <eberlm@in.tum.de>
parents:
64966
diff
changeset
|
5155 |
|
69107 | 5156 |
lemma filter_shuffles_disjoint2: |
5157 |
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
|
5158 |
shows "filter (\<lambda>x. x \<in> set ys) zs = ys" "filter (\<lambda>x. x \<notin> set ys) zs = xs" |
69107 | 5159 |
using filter_shuffles_disjoint1[of ys xs zs] assms |
5160 |
by (simp_all add: shuffles_commutes Int_commute) |
|
5161 |
||
5162 |
lemma partition_in_shuffles: |
|
5163 |
"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
|
5164 |
proof (induction xs) |
b149abe619f7
added shuffle product to HOL/List
eberlm <eberlm@in.tum.de>
parents:
64966
diff
changeset
|
5165 |
case (Cons x xs) |
b149abe619f7
added shuffle product to HOL/List
eberlm <eberlm@in.tum.de>
parents:
64966
diff
changeset
|
5166 |
show ?case |
b149abe619f7
added shuffle product to HOL/List
eberlm <eberlm@in.tum.de>
parents:
64966
diff
changeset
|
5167 |
proof (cases "P x") |
b149abe619f7
added shuffle product to HOL/List
eberlm <eberlm@in.tum.de>
parents:
64966
diff
changeset
|
5168 |
case True |
69107 | 5169 |
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
|
5170 |
by (intro imageI Cons.IH) |
69107 | 5171 |
also have "\<dots> \<subseteq> shuffles (filter P (x # xs)) (filter (\<lambda>x. \<not>P x) (x # xs))" |
5172 |
by (simp add: True Cons_shuffles_subset1) |
|
65350
b149abe619f7
added shuffle product to HOL/List
eberlm <eberlm@in.tum.de>
parents:
64966
diff
changeset
|
5173 |
finally show ?thesis . |
b149abe619f7
added shuffle product to HOL/List
eberlm <eberlm@in.tum.de>
parents:
64966
diff
changeset
|
5174 |
next |
b149abe619f7
added shuffle product to HOL/List
eberlm <eberlm@in.tum.de>
parents:
64966
diff
changeset
|
5175 |
case False |
69107 | 5176 |
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
|
5177 |
by (intro imageI Cons.IH) |
69107 | 5178 |
also have "\<dots> \<subseteq> shuffles (filter P (x # xs)) (filter (\<lambda>x. \<not>P x) (x # xs))" |
5179 |
by (simp add: False Cons_shuffles_subset2) |
|
65350
b149abe619f7
added shuffle product to HOL/List
eberlm <eberlm@in.tum.de>
parents:
64966
diff
changeset
|
5180 |
finally show ?thesis . |
b149abe619f7
added shuffle product to HOL/List
eberlm <eberlm@in.tum.de>
parents:
64966
diff
changeset
|
5181 |
qed |
b149abe619f7
added shuffle product to HOL/List
eberlm <eberlm@in.tum.de>
parents:
64966
diff
changeset
|
5182 |
qed auto |
b149abe619f7
added shuffle product to HOL/List
eberlm <eberlm@in.tum.de>
parents:
64966
diff
changeset
|
5183 |
|
b149abe619f7
added shuffle product to HOL/List
eberlm <eberlm@in.tum.de>
parents:
64966
diff
changeset
|
5184 |
lemma inv_image_partition: |
b149abe619f7
added shuffle product to HOL/List
eberlm <eberlm@in.tum.de>
parents:
64966
diff
changeset
|
5185 |
assumes "\<And>x. x \<in> set xs \<Longrightarrow> P x" "\<And>y. y \<in> set ys \<Longrightarrow> \<not>P y" |
69107 | 5186 |
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
|
5187 |
proof (intro equalityI subsetI) |
69107 | 5188 |
fix zs assume zs: "zs \<in> shuffles xs ys" |
5189 |
hence [simp]: "set zs = set xs \<union> set ys" by (rule set_shuffles) |
|
66892 | 5190 |
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
|
5191 |
"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
|
5192 |
by (intro filter_cong refl; force)+ |
b149abe619f7
added shuffle product to HOL/List
eberlm <eberlm@in.tum.de>
parents:
64966
diff
changeset
|
5193 |
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
|
5194 |
ultimately show "zs \<in> partition P -` {(xs, ys)}" using zs |
69107 | 5195 |
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
|
5196 |
next |
b149abe619f7
added shuffle product to HOL/List
eberlm <eberlm@in.tum.de>
parents:
64966
diff
changeset
|
5197 |
fix zs assume "zs \<in> partition P -` {(xs, ys)}" |
69107 | 5198 |
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
|
5199 |
qed |
22793 | 5200 |
|
35115 | 5201 |
|
60758 | 5202 |
subsubsection \<open>Transpose\<close> |
34933 | 5203 |
|
5204 |
function transpose where |
|
5205 |
"transpose [] = []" | |
|
5206 |
"transpose ([] # xss) = transpose xss" | |
|
5207 |
"transpose ((x#xs) # xss) = |
|
5208 |
(x # [h. (h#t) \<leftarrow> xss]) # transpose (xs # [t. (h#t) \<leftarrow> xss])" |
|
5209 |
by pat_completeness auto |
|
5210 |
||
5211 |
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
|
5212 |
"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
|
5213 |
map (\<lambda>xs. hd xs) (filter (\<lambda>ys. ys \<noteq> []) xss)" |
34933 | 5214 |
by (induct xss) (auto split: list.split) |
5215 |
||
5216 |
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
|
5217 |
"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
|
5218 |
map (\<lambda>xs. tl xs) (filter (\<lambda>ys. ys \<noteq> []) xss)" |
34933 | 5219 |
by (induct xss) (auto split: list.split) |
5220 |
||
5221 |
lemma transpose_aux_max: |
|
5222 |
"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
|
5223 |
Suc (max (length xs) (foldr (\<lambda>x. max (length x - Suc 0)) (filter (\<lambda>ys. ys \<noteq> []) xss) 0))" |
34933 | 5224 |
(is "max _ ?foldB = Suc (max _ ?foldA)") |
68249
949d93804740
First step to remove nonstandard "[x <- xs. P]" syntax: only input
nipkow
parents:
68215
diff
changeset
|
5225 |
proof (cases "(filter (\<lambda>ys. ys \<noteq> []) xss) = []") |
34933 | 5226 |
case True |
5227 |
hence "foldr (\<lambda>xs. max (length xs)) xss 0 = 0" |
|
5228 |
proof (induct xss) |
|
5229 |
case (Cons x xs) |
|
53374
a14d2a854c02
tuned proofs -- clarified flow of facts wrt. calculation;
wenzelm
parents:
53017
diff
changeset
|
5230 |
then have "x = []" by (cases x) auto |
a14d2a854c02
tuned proofs -- clarified flow of facts wrt. calculation;
wenzelm
parents:
53017
diff
changeset
|
5231 |
with Cons show ?case by auto |
34933 | 5232 |
qed simp |
5233 |
thus ?thesis using True by simp |
|
5234 |
next |
|
5235 |
case False |
|
5236 |
||
68249
949d93804740
First step to remove nonstandard "[x <- xs. P]" syntax: only input
nipkow
parents:
68215
diff
changeset
|
5237 |
have foldA: "?foldA = foldr (\<lambda>x. max (length x)) (filter (\<lambda>ys. ys \<noteq> []) xss) 0 - 1" |
34933 | 5238 |
by (induct xss) auto |
68249
949d93804740
First step to remove nonstandard "[x <- xs. P]" syntax: only input
nipkow
parents:
68215
diff
changeset
|
5239 |
have foldB: "?foldB = foldr (\<lambda>x. max (length x)) (filter (\<lambda>ys. ys \<noteq> []) xss) 0" |
34933 | 5240 |
by (induct xss) auto |
5241 |
||
5242 |
have "0 < ?foldB" |
|
5243 |
proof - |
|
5244 |
from False |
|
68249
949d93804740
First step to remove nonstandard "[x <- xs. P]" syntax: only input
nipkow
parents:
68215
diff
changeset
|
5245 |
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
|
5246 |
hence "z \<in> set (filter (\<lambda>ys. ys \<noteq> []) xss)" by auto |
34933 | 5247 |
hence "z \<noteq> []" by auto |
5248 |
thus ?thesis |
|
5249 |
unfolding foldB zs |
|
5250 |
by (auto simp: max_def intro: less_le_trans) |
|
5251 |
qed |
|
5252 |
thus ?thesis |
|
5253 |
unfolding foldA foldB max_Suc_Suc[symmetric] |
|
5254 |
by simp |
|
5255 |
qed |
|
5256 |
||
5257 |
termination transpose |
|
5258 |
by (relation "measure (\<lambda>xs. foldr (\<lambda>xs. max (length xs)) xs 0 + length xs)") |
|
5259 |
(auto simp: transpose_aux_filter_tail foldr_map comp_def transpose_aux_max less_Suc_eq_le) |
|
5260 |
||
5261 |
lemma transpose_empty: "(transpose xs = []) \<longleftrightarrow> (\<forall>x \<in> set xs. x = [])" |
|
5262 |
by (induct rule: transpose.induct) simp_all |
|
5263 |
||
5264 |
lemma length_transpose: |
|
5265 |
fixes xs :: "'a list list" |
|
5266 |
shows "length (transpose xs) = foldr (\<lambda>xs. max (length xs)) xs 0" |
|
5267 |
by (induct rule: transpose.induct) |
|
5268 |
(auto simp: transpose_aux_filter_tail foldr_map comp_def transpose_aux_max |
|
5269 |
max_Suc_Suc[symmetric] simp del: max_Suc_Suc) |
|
5270 |
||
5271 |
lemma nth_transpose: |
|
5272 |
fixes xs :: "'a list list" |
|
5273 |
assumes "i < length (transpose xs)" |
|
68249
949d93804740
First step to remove nonstandard "[x <- xs. P]" syntax: only input
nipkow
parents:
68215
diff
changeset
|
5274 |
shows "transpose xs ! i = map (\<lambda>xs. xs ! i) (filter (\<lambda>ys. i < length ys) xs)" |
34933 | 5275 |
using assms proof (induct arbitrary: i rule: transpose.induct) |
5276 |
case (3 x xs xss) |
|
63040 | 5277 |
define XS where "XS = (x # xs) # xss" |
34933 | 5278 |
hence [simp]: "XS \<noteq> []" by auto |
5279 |
thus ?case |
|
5280 |
proof (cases i) |
|
5281 |
case 0 |
|
5282 |
thus ?thesis by (simp add: transpose_aux_filter_head hd_conv_nth) |
|
5283 |
next |
|
5284 |
case (Suc j) |
|
5285 |
have *: "\<And>xss. xs # map tl xss = map tl ((x#xs)#xss)" by simp |
|
5286 |
have **: "\<And>xss. (x#xs) # filter (\<lambda>ys. ys \<noteq> []) xss = filter (\<lambda>ys. ys \<noteq> []) ((x#xs)#xss)" by simp |
|
75598 | 5287 |
{ fix xs :: \<open>'a list\<close> have "Suc j < length xs \<longleftrightarrow> xs \<noteq> [] \<and> j < length xs - Suc 0" |
5288 |
by (cases xs) simp_all |
|
34933 | 5289 |
} note *** = this |
5290 |
||
55404
5cb95b79a51f
transformed 'option' and 'list' into new-style datatypes (but register them as old-style as well)
blanchet
parents:
55148
diff
changeset
|
5291 |
have j_less: "j < length (transpose (xs # concat (map (case_list [] (\<lambda>h t. [t])) xss)))" |
34933 | 5292 |
using "3.prems" by (simp add: transpose_aux_filter_tail length_transpose Suc) |
5293 |
||
5294 |
show ?thesis |
|
60758 | 5295 |
unfolding transpose.simps \<open>i = Suc j\<close> nth_Cons_Suc "3.hyps"[OF j_less] |
34933 | 5296 |
apply (auto simp: transpose_aux_filter_tail filter_map comp_def length_transpose * ** *** XS_def[symmetric]) |
68719 | 5297 |
by (simp add: nth_tl) |
34933 | 5298 |
qed |
5299 |
qed simp_all |
|
5300 |
||
5301 |
lemma transpose_map_map: |
|
5302 |
"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
|
5303 |
proof (rule nth_equalityI) |
34933 | 5304 |
have [simp]: "length (transpose (map (map f) xs)) = length (transpose xs)" |
5305 |
by (simp add: length_transpose foldr_map comp_def) |
|
5306 |
show "length (transpose (map (map f) xs)) = length (map (map f) (transpose xs))" by simp |
|
5307 |
||
5308 |
fix i assume "i < length (transpose (map (map f) xs))" |
|
5309 |
thus "transpose (map (map f) xs) ! i = map (map f) (transpose xs) ! i" |
|
5310 |
by (simp add: nth_transpose filter_map comp_def) |
|
5311 |
qed |
|
24616 | 5312 |
|
75598 | 5313 |
|
69593 | 5314 |
subsubsection \<open>\<^const>\<open>min\<close> and \<^const>\<open>arg_min\<close>\<close> |
67170 | 5315 |
|
5316 |
lemma min_list_Min: "xs \<noteq> [] \<Longrightarrow> min_list xs = Min (set xs)" |
|
71585 | 5317 |
by (induction xs rule: induct_list012)(auto) |
67170 | 5318 |
|
5319 |
lemma f_arg_min_list_f: "xs \<noteq> [] \<Longrightarrow> f (arg_min_list f xs) = Min (f ` (set xs))" |
|
71585 | 5320 |
by(induction f xs rule: arg_min_list.induct) (auto simp: min_def intro!: antisym) |
67170 | 5321 |
|
67171 | 5322 |
lemma arg_min_list_in: "xs \<noteq> [] \<Longrightarrow> arg_min_list f xs \<in> set xs" |
71585 | 5323 |
by(induction xs rule: induct_list012) (auto simp: Let_def) |
67171 | 5324 |
|
35115 | 5325 |
|
60758 | 5326 |
subsubsection \<open>(In)finiteness\<close> |
28642 | 5327 |
|
78833 | 5328 |
lemma finite_list_length: "finite {xs::('a::finite) list. length xs = n}" |
5329 |
proof(induction n) |
|
5330 |
case (Suc n) |
|
5331 |
have "{xs::'a list. length xs = Suc n} = (\<Union>x. (#) x ` {xs. length xs = n})" |
|
5332 |
by (auto simp: length_Suc_conv) |
|
5333 |
then show ?case using Suc by simp |
|
5334 |
qed simp |
|
5335 |
||
28642 | 5336 |
lemma finite_maxlen: |
67091 | 5337 |
"finite (M::'a list set) \<Longrightarrow> \<exists>n. \<forall>s\<in>M. size s < n" |
28642 | 5338 |
proof (induct rule: finite.induct) |
5339 |
case emptyI show ?case by simp |
|
5340 |
next |
|
5341 |
case (insertI M xs) |
|
5342 |
then obtain n where "\<forall>s\<in>M. length s < n" by blast |
|
67613 | 5343 |
hence "\<forall>s\<in>insert xs M. size s < max n (size xs) + 1" by auto |
28642 | 5344 |
thus ?case .. |
5345 |
qed |
|
5346 |
||
45714 | 5347 |
lemma lists_length_Suc_eq: |
5348 |
"{xs. set xs \<subseteq> A \<and> length xs = Suc n} = |
|
5349 |
(\<lambda>(xs, n). n#xs) ` ({xs. set xs \<subseteq> A \<and> length xs = n} \<times> A)" |
|
5350 |
by (auto simp: length_Suc_conv) |
|
5351 |
||
5352 |
lemma |
|
5353 |
assumes "finite A" |
|
5354 |
shows finite_lists_length_eq: "finite {xs. set xs \<subseteq> A \<and> length xs = n}" |
|
5355 |
and card_lists_length_eq: "card {xs. set xs \<subseteq> A \<and> length xs = n} = (card A)^n" |
|
60758 | 5356 |
using \<open>finite A\<close> |
45714 | 5357 |
by (induct n) |
5358 |
(auto simp: card_image inj_split_Cons lists_length_Suc_eq cong: conj_cong) |
|
31557 | 5359 |
|
5360 |
lemma finite_lists_length_le: |
|
5361 |
assumes "finite A" shows "finite {xs. set xs \<subseteq> A \<and> length xs \<le> n}" |
|
5362 |
(is "finite ?S") |
|
5363 |
proof- |
|
5364 |
have "?S = (\<Union>n\<in>{0..n}. {xs. set xs \<subseteq> A \<and> length xs = n})" by auto |
|
60758 | 5365 |
thus ?thesis by (auto intro!: finite_lists_length_eq[OF \<open>finite A\<close>] simp only:) |
31557 | 5366 |
qed |
5367 |
||
45714 | 5368 |
lemma card_lists_length_le: |
5369 |
assumes "finite A" shows "card {xs. set xs \<subseteq> A \<and> length xs \<le> n} = (\<Sum>i\<le>n. card A^i)" |
|
5370 |
proof - |
|
5371 |
have "(\<Sum>i\<le>n. card A^i) = card (\<Union>i\<le>n. {xs. set xs \<subseteq> A \<and> length xs = i})" |
|
60758 | 5372 |
using \<open>finite A\<close> |
45714 | 5373 |
by (subst card_UN_disjoint) |
5374 |
(auto simp add: card_lists_length_eq finite_lists_length_eq) |
|
5375 |
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}" |
|
5376 |
by auto |
|
5377 |
finally show ?thesis by simp |
|
5378 |
qed |
|
5379 |
||
67478
14d3163588ae
add lemma on lists from Falling_Factorial_Sum entry
bulwahn
parents:
67443
diff
changeset
|
5380 |
lemma finite_lists_distinct_length_eq [intro]: |
14d3163588ae
add lemma on lists from Falling_Factorial_Sum entry
bulwahn
parents:
67443
diff
changeset
|
5381 |
assumes "finite A" |
14d3163588ae
add lemma on lists from Falling_Factorial_Sum entry
bulwahn
parents:
67443
diff
changeset
|
5382 |
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
|
5383 |
proof - |
14d3163588ae
add lemma on lists from Falling_Factorial_Sum entry
bulwahn
parents:
67443
diff
changeset
|
5384 |
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
|
5385 |
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
|
5386 |
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
|
5387 |
ultimately show ?thesis using finite_subset by auto |
14d3163588ae
add lemma on lists from Falling_Factorial_Sum entry
bulwahn
parents:
67443
diff
changeset
|
5388 |
qed |
14d3163588ae
add lemma on lists from Falling_Factorial_Sum entry
bulwahn
parents:
67443
diff
changeset
|
5389 |
|
45932 | 5390 |
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
|
5391 |
assumes "finite A" "k \<le> card A" |
45932 | 5392 |
shows "card {xs. length xs = k \<and> distinct xs \<and> set xs \<subseteq> A} = \<Prod>{card A - k + 1 .. card A}" |
5393 |
using assms |
|
5394 |
proof (induct k) |
|
5395 |
case 0 |
|
5396 |
then have "{xs. length xs = 0 \<and> distinct xs \<and> set xs \<subseteq> A} = {[]}" by auto |
|
5397 |
then show ?case by simp |
|
5398 |
next |
|
5399 |
case (Suc k) |
|
5400 |
let "?k_list" = "\<lambda>k xs. length xs = k \<and> distinct xs \<and> set xs \<subseteq> A" |
|
5401 |
have inj_Cons: "\<And>A. inj_on (\<lambda>(xs, n). n # xs) A" by (rule inj_onI) auto |
|
5402 |
||
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
|
5403 |
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
|
5404 |
moreover note \<open>finite A\<close> |
45932 | 5405 |
moreover have "finite {xs. ?k_list k xs}" |
63834 | 5406 |
by (rule finite_subset) (use finite_lists_length_eq[OF \<open>finite A\<close>, of k] in auto) |
45932 | 5407 |
moreover have "\<And>i j. i \<noteq> j \<longrightarrow> {i} \<times> (A - set i) \<inter> {j} \<times> (A - set j) = {}" |
5408 |
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
|
5409 |
moreover have "\<And>i. i \<in> {xs. ?k_list k xs} \<Longrightarrow> card (A - set i) = card A - k" |
45932 | 5410 |
by (simp add: card_Diff_subset distinct_card) |
5411 |
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
|
5412 |
(\<lambda>(xs, n). n#xs) ` \<Union>((\<lambda>xs. {xs} \<times> (A - set xs)) ` {xs. ?k_list k xs})" |
45932 | 5413 |
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
|
5414 |
moreover have "Suc (card A - Suc k) = card A - k" using Suc.prems by simp |
45932 | 5415 |
then have "(card A - k) * \<Prod>{Suc (card A - k)..card A} = \<Prod>{Suc (card A - Suc k)..card A}" |
64272 | 5416 |
by (subst prod.insert[symmetric]) (simp add: atLeastAtMost_insertL)+ |
45932 | 5417 |
ultimately show ?case |
5418 |
by (simp add: card_image inj_Cons card_UN_disjoint Suc.hyps algebra_simps) |
|
5419 |
qed |
|
5420 |
||
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
|
5421 |
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
|
5422 |
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
|
5423 |
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
|
5424 |
proof - |
72302
d7d90ed4c74e
fixed some remarkably ugly proofs
paulson <lp15@cam.ac.uk>
parents:
72184
diff
changeset
|
5425 |
from \<open>k < card A\<close> have "finite A" and "k \<le> card A" using card.infinite by force+ |
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
|
5426 |
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
|
5427 |
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
|
5428 |
|
67091 | 5429 |
lemma infinite_UNIV_listI: "\<not> finite(UNIV::'a list set)" |
68719 | 5430 |
by (metis UNIV_I finite_maxlen length_replicate less_irrefl) |
28642 | 5431 |
|
74424 | 5432 |
lemma same_length_different: |
71404
f2b783abfbe7
A few lemmas connected with orderings
paulson <lp15@cam.ac.uk>
parents:
71393
diff
changeset
|
5433 |
assumes "xs \<noteq> ys" and "length xs = length ys" |
f2b783abfbe7
A few lemmas connected with orderings
paulson <lp15@cam.ac.uk>
parents:
71393
diff
changeset
|
5434 |
shows "\<exists>pre x xs' y ys'. x\<noteq>y \<and> xs = pre @ [x] @ xs' \<and> ys = pre @ [y] @ ys'" |
f2b783abfbe7
A few lemmas connected with orderings
paulson <lp15@cam.ac.uk>
parents:
71393
diff
changeset
|
5435 |
using assms |
f2b783abfbe7
A few lemmas connected with orderings
paulson <lp15@cam.ac.uk>
parents:
71393
diff
changeset
|
5436 |
proof (induction xs arbitrary: ys) |
f2b783abfbe7
A few lemmas connected with orderings
paulson <lp15@cam.ac.uk>
parents:
71393
diff
changeset
|
5437 |
case Nil |
f2b783abfbe7
A few lemmas connected with orderings
paulson <lp15@cam.ac.uk>
parents:
71393
diff
changeset
|
5438 |
then show ?case by auto |
f2b783abfbe7
A few lemmas connected with orderings
paulson <lp15@cam.ac.uk>
parents:
71393
diff
changeset
|
5439 |
next |
f2b783abfbe7
A few lemmas connected with orderings
paulson <lp15@cam.ac.uk>
parents:
71393
diff
changeset
|
5440 |
case (Cons x xs) |
f2b783abfbe7
A few lemmas connected with orderings
paulson <lp15@cam.ac.uk>
parents:
71393
diff
changeset
|
5441 |
then obtain z zs where ys: "ys = Cons z zs" |
f2b783abfbe7
A few lemmas connected with orderings
paulson <lp15@cam.ac.uk>
parents:
71393
diff
changeset
|
5442 |
by (metis length_Suc_conv) |
f2b783abfbe7
A few lemmas connected with orderings
paulson <lp15@cam.ac.uk>
parents:
71393
diff
changeset
|
5443 |
show ?case |
f2b783abfbe7
A few lemmas connected with orderings
paulson <lp15@cam.ac.uk>
parents:
71393
diff
changeset
|
5444 |
proof (cases "x=z") |
f2b783abfbe7
A few lemmas connected with orderings
paulson <lp15@cam.ac.uk>
parents:
71393
diff
changeset
|
5445 |
case True |
f2b783abfbe7
A few lemmas connected with orderings
paulson <lp15@cam.ac.uk>
parents:
71393
diff
changeset
|
5446 |
then have "xs \<noteq> zs" "length xs = length zs" |
f2b783abfbe7
A few lemmas connected with orderings
paulson <lp15@cam.ac.uk>
parents:
71393
diff
changeset
|
5447 |
using Cons.prems ys by auto |
f2b783abfbe7
A few lemmas connected with orderings
paulson <lp15@cam.ac.uk>
parents:
71393
diff
changeset
|
5448 |
then obtain pre u xs' v ys' where "u\<noteq>v" and xs: "xs = pre @ [u] @ xs'" and zs: "zs = pre @ [v] @ys'" |
f2b783abfbe7
A few lemmas connected with orderings
paulson <lp15@cam.ac.uk>
parents:
71393
diff
changeset
|
5449 |
using Cons.IH by meson |
f2b783abfbe7
A few lemmas connected with orderings
paulson <lp15@cam.ac.uk>
parents:
71393
diff
changeset
|
5450 |
then have "x # xs = (z#pre) @ [u] @ xs' \<and> ys = (z#pre) @ [v] @ ys'" |
f2b783abfbe7
A few lemmas connected with orderings
paulson <lp15@cam.ac.uk>
parents:
71393
diff
changeset
|
5451 |
by (simp add: True ys) |
f2b783abfbe7
A few lemmas connected with orderings
paulson <lp15@cam.ac.uk>
parents:
71393
diff
changeset
|
5452 |
with \<open>u\<noteq>v\<close> show ?thesis |
f2b783abfbe7
A few lemmas connected with orderings
paulson <lp15@cam.ac.uk>
parents:
71393
diff
changeset
|
5453 |
by blast |
f2b783abfbe7
A few lemmas connected with orderings
paulson <lp15@cam.ac.uk>
parents:
71393
diff
changeset
|
5454 |
next |
f2b783abfbe7
A few lemmas connected with orderings
paulson <lp15@cam.ac.uk>
parents:
71393
diff
changeset
|
5455 |
case False |
f2b783abfbe7
A few lemmas connected with orderings
paulson <lp15@cam.ac.uk>
parents:
71393
diff
changeset
|
5456 |
then have "x # xs = [] @ [x] @ xs \<and> ys = [] @ [z] @ zs" |
f2b783abfbe7
A few lemmas connected with orderings
paulson <lp15@cam.ac.uk>
parents:
71393
diff
changeset
|
5457 |
by (simp add: ys) |
f2b783abfbe7
A few lemmas connected with orderings
paulson <lp15@cam.ac.uk>
parents:
71393
diff
changeset
|
5458 |
then show ?thesis |
f2b783abfbe7
A few lemmas connected with orderings
paulson <lp15@cam.ac.uk>
parents:
71393
diff
changeset
|
5459 |
using False by blast |
f2b783abfbe7
A few lemmas connected with orderings
paulson <lp15@cam.ac.uk>
parents:
71393
diff
changeset
|
5460 |
qed |
f2b783abfbe7
A few lemmas connected with orderings
paulson <lp15@cam.ac.uk>
parents:
71393
diff
changeset
|
5461 |
qed |
28642 | 5462 |
|
60758 | 5463 |
subsection \<open>Sorting\<close> |
5464 |
||
69593 | 5465 |
subsubsection \<open>\<^const>\<open>sorted_wrt\<close>\<close> |
5466 |
||
5467 |
text \<open>Sometimes the second equation in the definition of \<^const>\<open>sorted_wrt\<close> is too aggressive |
|
68125 | 5468 |
because it relates each list element to \emph{all} its successors. Then this equation |
5469 |
should be removed and \<open>sorted_wrt2_simps\<close> should be added instead.\<close> |
|
5470 |
||
5471 |
lemma sorted_wrt1: "sorted_wrt P [x] = True" |
|
5472 |
by(simp) |
|
5473 |
||
5474 |
lemma sorted_wrt2: "transp P \<Longrightarrow> sorted_wrt P (x # y # zs) = (P x y \<and> sorted_wrt P (y # zs))" |
|
68719 | 5475 |
proof (induction zs arbitrary: x y) |
5476 |
case (Cons z zs) |
|
5477 |
then show ?case |
|
5478 |
by simp (meson transpD)+ |
|
5479 |
qed auto |
|
68125 | 5480 |
|
5481 |
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
|
5482 |
|
68141 | 5483 |
lemma sorted_wrt_true [simp]: |
5484 |
"sorted_wrt (\<lambda>_ _. True) xs" |
|
5485 |
by (induction xs) simp_all |
|
5486 |
||
66434
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66358
diff
changeset
|
5487 |
lemma sorted_wrt_append: |
68109 | 5488 |
"sorted_wrt P (xs @ ys) \<longleftrightarrow> |
66892 | 5489 |
sorted_wrt P xs \<and> sorted_wrt P ys \<and> (\<forall>x\<in>set xs. \<forall>y\<in>set ys. P x y)" |
68109 | 5490 |
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
|
5491 |
|
68141 | 5492 |
lemma sorted_wrt_map: |
5493 |
"sorted_wrt R (map f xs) = sorted_wrt (\<lambda>x y. R (f x) (f y)) xs" |
|
5494 |
by (induction xs) simp_all |
|
5495 |
||
71444 | 5496 |
lemma |
5497 |
assumes "sorted_wrt f xs" |
|
5498 |
shows sorted_wrt_take: "sorted_wrt f (take n xs)" |
|
5499 |
and sorted_wrt_drop: "sorted_wrt f (drop n xs)" |
|
5500 |
proof - |
|
5501 |
from assms have "sorted_wrt f (take n xs @ drop n xs)" by simp |
|
5502 |
thus "sorted_wrt f (take n xs)" and "sorted_wrt f (drop n xs)" |
|
5503 |
unfolding sorted_wrt_append by simp_all |
|
5504 |
qed |
|
5505 |
||
5506 |
lemma sorted_wrt_filter: |
|
5507 |
"sorted_wrt f xs \<Longrightarrow> sorted_wrt f (filter P xs)" |
|
5508 |
by (induction xs) auto |
|
5509 |
||
66890
e92d48a42a01
drop a superfluous assumption that was found by the find_unused_assms command and tune proof
bulwahn
parents:
66889
diff
changeset
|
5510 |
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
|
5511 |
"sorted_wrt P (rev xs) = sorted_wrt (\<lambda>x y. P y x) xs" |
68109 | 5512 |
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
|
5513 |
|
68141 | 5514 |
lemma sorted_wrt_mono_rel: |
5515 |
"(\<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 | 5516 |
by(induction xs)(auto) |
66442 | 5517 |
|
67973 | 5518 |
lemma sorted_wrt01: "length xs \<le> 1 \<Longrightarrow> sorted_wrt P xs" |
5519 |
by(auto simp: le_Suc_eq length_Suc_conv) |
|
5520 |
||
68175 | 5521 |
lemma sorted_wrt_iff_nth_less: |
68156 | 5522 |
"sorted_wrt P xs = (\<forall>i j. i < j \<longrightarrow> j < length xs \<longrightarrow> P (xs ! i) (xs ! j))" |
68719 | 5523 |
by (induction xs) (auto simp add: in_set_conv_nth Ball_def nth_Cons split: nat.split) |
68156 | 5524 |
|
5525 |
lemma sorted_wrt_nth_less: |
|
5526 |
"\<lbrakk> sorted_wrt P xs; i < j; j < length xs \<rbrakk> \<Longrightarrow> P (xs ! i) (xs ! j)" |
|
68175 | 5527 |
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
|
5528 |
|
73017 | 5529 |
lemma sorted_wrt_iff_nth_Suc_transp: assumes "transp P" |
5530 |
shows "sorted_wrt P xs \<longleftrightarrow> (\<forall>i. Suc i < length xs \<longrightarrow> P (xs!i) (xs!(Suc i)))" (is "?L = ?R") |
|
5531 |
proof |
|
5532 |
assume ?L |
|
5533 |
thus ?R |
|
74424 | 5534 |
by (simp add: sorted_wrt_iff_nth_less) |
73017 | 5535 |
next |
5536 |
assume ?R |
|
5537 |
have "i < j \<Longrightarrow> j < length xs \<Longrightarrow> P (xs ! i) (xs ! j)" for i j |
|
76749
11a24dab1880
strengthened and renamed lemmas preorder.transp_(ge|gr|le|less)
desharna
parents:
76682
diff
changeset
|
5538 |
by(induct i j rule: less_Suc_induct)(simp add: \<open>?R\<close>, meson assms transpE transp_on_less) |
73017 | 5539 |
thus ?L |
5540 |
by (simp add: sorted_wrt_iff_nth_less) |
|
5541 |
qed |
|
5542 |
||
68141 | 5543 |
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
|
5544 |
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
|
5545 |
|
68191 | 5546 |
lemma sorted_wrt_upto[simp]: "sorted_wrt (<) [i..j]" |
68719 | 5547 |
proof(induct i j rule:upto.induct) |
5548 |
case (1 i j) |
|
5549 |
from this show ?case |
|
5550 |
unfolding upto.simps[of i j] by auto |
|
5551 |
qed |
|
68175 | 5552 |
|
66434
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66358
diff
changeset
|
5553 |
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
|
5554 |
|
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66358
diff
changeset
|
5555 |
lemma sorted_wrt_less_idx: |
67399 | 5556 |
"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
|
5557 |
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
|
5558 |
case Nil thus ?case by simp |
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66358
diff
changeset
|
5559 |
next |
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66358
diff
changeset
|
5560 |
case snoc |
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66358
diff
changeset
|
5561 |
thus ?case |
77433 | 5562 |
by (simp add: nth_append sorted_wrt_append) |
66892 | 5563 |
(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
|
5564 |
qed |
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66358
diff
changeset
|
5565 |
|
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66358
diff
changeset
|
5566 |
|
69593 | 5567 |
subsubsection \<open>\<^const>\<open>sorted\<close>\<close> |
24617 | 5568 |
|
24616 | 5569 |
context linorder |
5570 |
begin |
|
5571 |
||
69593 | 5572 |
text \<open>Sometimes the second equation in the definition of \<^const>\<open>sorted\<close> is too aggressive |
68111 | 5573 |
because it relates each list element to \emph{all} its successors. Then this equation |
68118 | 5574 |
should be removed and \<open>sorted2_simps\<close> should be added instead. |
5575 |
Executable code is one such use case.\<close> |
|
5576 |
||
73707 | 5577 |
lemma sorted0: "sorted [] = True" |
5578 |
by simp |
|
5579 |
||
68118 | 5580 |
lemma sorted1: "sorted [x] = True" |
73707 | 5581 |
by simp |
68111 | 5582 |
|
5583 |
lemma sorted2: "sorted (x # y # zs) = (x \<le> y \<and> sorted (y # zs))" |
|
77433 | 5584 |
by auto |
68111 | 5585 |
|
5586 |
lemmas sorted2_simps = sorted1 sorted2 |
|
5587 |
||
68175 | 5588 |
lemma sorted_append: |
5589 |
"sorted (xs@ys) = (sorted xs \<and> sorted ys \<and> (\<forall>x \<in> set xs. \<forall>y \<in> set ys. x\<le>y))" |
|
73707 | 5590 |
by (simp add: sorted_wrt_append) |
68175 | 5591 |
|
5592 |
lemma sorted_map: |
|
5593 |
"sorted (map f xs) = sorted_wrt (\<lambda>x y. f x \<le> f y) xs" |
|
73707 | 5594 |
by (simp add: sorted_wrt_map) |
68175 | 5595 |
|
68160 | 5596 |
lemma sorted01: "length xs \<le> 1 \<Longrightarrow> sorted xs" |
73707 | 5597 |
by (simp add: sorted_wrt01) |
68160 | 5598 |
|
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
|
5599 |
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
|
5600 |
"sorted xs \<Longrightarrow> sorted (tl xs)" |
73707 | 5601 |
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
|
5602 |
|
68186 | 5603 |
lemma sorted_iff_nth_mono_less: |
68175 | 5604 |
"sorted xs = (\<forall>i j. i < j \<longrightarrow> j < length xs \<longrightarrow> xs ! i \<le> xs ! j)" |
73707 | 5605 |
by (simp add: sorted_wrt_iff_nth_less) |
24616 | 5606 |
|
68186 | 5607 |
lemma sorted_iff_nth_mono: |
5608 |
"sorted xs = (\<forall>i j. i \<le> j \<longrightarrow> j < length xs \<longrightarrow> xs ! i \<le> xs ! j)" |
|
73707 | 5609 |
by (auto simp: sorted_iff_nth_mono_less nat_less_le) |
68186 | 5610 |
|
31201 | 5611 |
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
|
5612 |
"sorted xs \<Longrightarrow> i \<le> j \<Longrightarrow> j < length xs \<Longrightarrow> xs!i \<le> xs!j" |
73707 | 5613 |
by (auto simp: sorted_iff_nth_mono) |
31201 | 5614 |
|
74424 | 5615 |
lemma sorted_iff_nth_Suc: |
73017 | 5616 |
"sorted xs \<longleftrightarrow> (\<forall>i. Suc i < length xs \<longrightarrow> xs!i \<le> xs!(Suc i))" |
73707 | 5617 |
by(simp add: sorted_wrt_iff_nth_Suc_transp) |
73017 | 5618 |
|
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
|
5619 |
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
|
5620 |
"sorted (rev xs) \<Longrightarrow> i \<le> j \<Longrightarrow> j < length xs \<Longrightarrow> xs!j \<le> xs!i" |
77433 | 5621 |
by (metis local.nle_le order_class.antisym_conv1 sorted_wrt_iff_nth_less sorted_wrt_rev) |
68176 | 5622 |
|
73017 | 5623 |
lemma sorted_rev_iff_nth_mono: |
5624 |
"sorted (rev xs) \<longleftrightarrow> (\<forall> i j. i \<le> j \<longrightarrow> j < length xs \<longrightarrow> xs!j \<le> xs!i)" (is "?L = ?R") |
|
5625 |
proof |
|
5626 |
assume ?L thus ?R |
|
5627 |
by (blast intro: sorted_rev_nth_mono) |
|
5628 |
next |
|
5629 |
assume ?R |
|
5630 |
have "rev xs ! k \<le> rev xs ! l" if asms: "k \<le> l" "l < length(rev xs)" for k l |
|
5631 |
proof - |
|
5632 |
have "k < length xs" "l < length xs" |
|
5633 |
"length xs - Suc l \<le> length xs - Suc k" "length xs - Suc k < length xs" |
|
5634 |
using asms by auto |
|
5635 |
thus "rev xs ! k \<le> rev xs ! l" |
|
77433 | 5636 |
by (simp add: \<open>?R\<close> rev_nth) |
73017 | 5637 |
qed |
5638 |
thus ?L by (simp add: sorted_iff_nth_mono) |
|
5639 |
qed |
|
5640 |
||
74424 | 5641 |
lemma sorted_rev_iff_nth_Suc: |
73017 | 5642 |
"sorted (rev xs) \<longleftrightarrow> (\<forall>i. Suc i < length xs \<longrightarrow> xs!(Suc i) \<le> xs!i)" |
5643 |
proof- |
|
5644 |
interpret dual: linorder "(\<lambda>x y. y \<le> x)" "(\<lambda>x y. y < x)" |
|
5645 |
using dual_linorder . |
|
5646 |
show ?thesis |
|
74424 | 5647 |
using dual_linorder dual.sorted_iff_nth_Suc dual.sorted_iff_nth_mono |
73017 | 5648 |
unfolding sorted_rev_iff_nth_mono by simp |
5649 |
qed |
|
5650 |
||
66434
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66358
diff
changeset
|
5651 |
lemma sorted_map_remove1: |
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66358
diff
changeset
|
5652 |
"sorted (map f xs) \<Longrightarrow> sorted (map f (remove1 x xs))" |
68109 | 5653 |
by (induct xs) (auto) |
66434
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66358
diff
changeset
|
5654 |
|
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66358
diff
changeset
|
5655 |
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
|
5656 |
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
|
5657 |
|
36851 | 5658 |
lemma sorted_butlast: |
77433 | 5659 |
assumes "sorted xs" |
36851 | 5660 |
shows "sorted (butlast xs)" |
77433 | 5661 |
by (simp add: assms butlast_conv_take sorted_wrt_take) |
64963 | 5662 |
|
66905 | 5663 |
lemma sorted_replicate [simp]: "sorted(replicate n x)" |
68109 | 5664 |
by(induction n) (auto) |
66905 | 5665 |
|
26143
314c0bcb7df7
Added useful general lemmas from the work with the HeapMonad
bulwahn
parents:
26073
diff
changeset
|
5666 |
lemma sorted_remdups[simp]: |
68109 | 5667 |
"sorted xs \<Longrightarrow> sorted (remdups xs)" |
5668 |
by (induct xs) (auto) |
|
26143
314c0bcb7df7
Added useful general lemmas from the work with the HeapMonad
bulwahn
parents:
26073
diff
changeset
|
5669 |
|
53721
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
traytel
parents:
53689
diff
changeset
|
5670 |
lemma sorted_remdups_adj[simp]: |
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
traytel
parents:
53689
diff
changeset
|
5671 |
"sorted xs \<Longrightarrow> sorted (remdups_adj xs)" |
68109 | 5672 |
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
|
5673 |
|
66853 | 5674 |
lemma sorted_nths: "sorted xs \<Longrightarrow> sorted (nths xs I)" |
68109 | 5675 |
by(induction xs arbitrary: I)(auto simp: nths_Cons) |
66853 | 5676 |
|
24645 | 5677 |
lemma sorted_distinct_set_unique: |
5678 |
assumes "sorted xs" "distinct xs" "sorted ys" "distinct ys" "set xs = set ys" |
|
5679 |
shows "xs = ys" |
|
5680 |
proof - |
|
26734 | 5681 |
from assms have 1: "length xs = length ys" by (auto dest!: distinct_card) |
24645 | 5682 |
from assms show ?thesis |
5683 |
proof(induct rule:list_induct2[OF 1]) |
|
5684 |
case 1 show ?case by simp |
|
5685 |
next |
|
73411 | 5686 |
case (2 x xs y ys) |
5687 |
then show ?case |
|
5688 |
by (cases \<open>x = y\<close>) (auto simp add: insert_eq_iff) |
|
24645 | 5689 |
qed |
5690 |
qed |
|
5691 |
||
35603 | 5692 |
lemma map_sorted_distinct_set_unique: |
5693 |
assumes "inj_on f (set xs \<union> set ys)" |
|
5694 |
assumes "sorted (map f xs)" "distinct (map f xs)" |
|
5695 |
"sorted (map f ys)" "distinct (map f ys)" |
|
5696 |
assumes "set xs = set ys" |
|
5697 |
shows "xs = ys" |
|
77433 | 5698 |
using assms map_inj_on sorted_distinct_set_unique by fastforce |
29626 | 5699 |
|
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
|
5700 |
lemma sorted_dropWhile: "sorted xs \<Longrightarrow> sorted (dropWhile P xs)" |
77433 | 5701 |
by (auto dest: sorted_wrt_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
|
5702 |
|
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
hoelzl
parents:
33593
diff
changeset
|
5703 |
lemma sorted_takeWhile: "sorted xs \<Longrightarrow> sorted (takeWhile P xs)" |
77433 | 5704 |
by (subst takeWhile_eq_take) (auto dest: sorted_wrt_take) |
29626 | 5705 |
|
34933 | 5706 |
lemma sorted_filter: |
5707 |
"sorted (map f xs) \<Longrightarrow> sorted (map f (filter P xs))" |
|
68109 | 5708 |
by (induct xs) simp_all |
34933 | 5709 |
|
5710 |
lemma foldr_max_sorted: |
|
5711 |
assumes "sorted (rev xs)" |
|
5712 |
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
|
5713 |
using assms |
a14d2a854c02
tuned proofs -- clarified flow of facts wrt. calculation;
wenzelm
parents:
53017
diff
changeset
|
5714 |
proof (induct xs) |
34933 | 5715 |
case (Cons x xs) |
53374
a14d2a854c02
tuned proofs -- clarified flow of facts wrt. calculation;
wenzelm
parents:
53017
diff
changeset
|
5716 |
then have "sorted (rev xs)" using sorted_append by auto |
a14d2a854c02
tuned proofs -- clarified flow of facts wrt. calculation;
wenzelm
parents:
53017
diff
changeset
|
5717 |
with Cons show ?case |
a14d2a854c02
tuned proofs -- clarified flow of facts wrt. calculation;
wenzelm
parents:
53017
diff
changeset
|
5718 |
by (cases xs) (auto simp add: sorted_append max_def) |
34933 | 5719 |
qed simp |
5720 |
||
5721 |
lemma filter_equals_takeWhile_sorted_rev: |
|
5722 |
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
|
5723 |
shows "filter (\<lambda>x. t < f x) xs = takeWhile (\<lambda> x. t < f x) xs" |
34933 | 5724 |
(is "filter ?P xs = ?tW") |
5725 |
proof (rule takeWhile_eq_filter[symmetric]) |
|
5726 |
let "?dW" = "dropWhile ?P xs" |
|
77433 | 5727 |
fix x assume x: "x \<in> set ?dW" |
34933 | 5728 |
then obtain i where i: "i < length ?dW" and nth_i: "x = ?dW ! i" |
5729 |
unfolding in_set_conv_nth by auto |
|
5730 |
hence "length ?tW + i < length (?tW @ ?dW)" |
|
5731 |
unfolding length_append by simp |
|
5732 |
hence i': "length (map f ?tW) + i < length (map f xs)" by simp |
|
5733 |
have "(map f ?tW @ map f ?dW) ! (length (map f ?tW) + i) \<le> |
|
5734 |
(map f ?tW @ map f ?dW) ! (length (map f ?tW) + 0)" |
|
5735 |
using sorted_rev_nth_mono[OF sorted _ i', of "length ?tW"] |
|
5736 |
unfolding map_append[symmetric] by simp |
|
5737 |
hence "f x \<le> f (?dW ! 0)" |
|
5738 |
unfolding nth_append_length_plus nth_i |
|
5739 |
using i preorder_class.le_less_trans[OF le0 i] by simp |
|
5740 |
also have "... \<le> t" |
|
77433 | 5741 |
by (metis hd_conv_nth hd_dropWhile length_greater_0_conv length_pos_if_in_set local.leI x) |
34933 | 5742 |
finally show "\<not> t < f x" by simp |
5743 |
qed |
|
5744 |
||
66434
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66358
diff
changeset
|
5745 |
lemma sorted_map_same: |
68249
949d93804740
First step to remove nonstandard "[x <- xs. P]" syntax: only input
nipkow
parents:
68215
diff
changeset
|
5746 |
"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
|
5747 |
proof (induct xs arbitrary: g) |
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66358
diff
changeset
|
5748 |
case Nil then show ?case by simp |
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66358
diff
changeset
|
5749 |
next |
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66358
diff
changeset
|
5750 |
case (Cons x xs) |
68249
949d93804740
First step to remove nonstandard "[x <- xs. P]" syntax: only input
nipkow
parents:
68215
diff
changeset
|
5751 |
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
|
5752 |
moreover from Cons have "sorted (map f (filter (\<lambda>y. f y = (g \<circ> Cons x) xs) xs))" . |
68109 | 5753 |
ultimately show ?case by simp_all |
66434
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66358
diff
changeset
|
5754 |
qed |
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66358
diff
changeset
|
5755 |
|
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66358
diff
changeset
|
5756 |
lemma sorted_same: |
68249
949d93804740
First step to remove nonstandard "[x <- xs. P]" syntax: only input
nipkow
parents:
68215
diff
changeset
|
5757 |
"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
|
5758 |
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
|
5759 |
|
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66358
diff
changeset
|
5760 |
end |
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66358
diff
changeset
|
5761 |
|
68190 | 5762 |
lemma sorted_upt[simp]: "sorted [m..<n]" |
73707 | 5763 |
by(simp add: sorted_wrt_mono_rel[OF _ sorted_wrt_upt]) |
68190 | 5764 |
|
5765 |
lemma sorted_upto[simp]: "sorted [m..n]" |
|
73707 | 5766 |
by(simp add: sorted_wrt_mono_rel[OF _ sorted_wrt_upto]) |
68190 | 5767 |
|
66434
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66358
diff
changeset
|
5768 |
|
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66358
diff
changeset
|
5769 |
subsubsection \<open>Sorting functions\<close> |
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66358
diff
changeset
|
5770 |
|
69593 | 5771 |
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
|
5772 |
permutation of its input because the nicest proof is via multisets, |
66654 | 5773 |
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
|
5774 |
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
|
5775 |
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
|
5776 |
|
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66358
diff
changeset
|
5777 |
context linorder |
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66358
diff
changeset
|
5778 |
begin |
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66358
diff
changeset
|
5779 |
|
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66358
diff
changeset
|
5780 |
lemma set_insort_key: |
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66358
diff
changeset
|
5781 |
"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
|
5782 |
by (induct xs) auto |
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66358
diff
changeset
|
5783 |
|
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66358
diff
changeset
|
5784 |
lemma length_insort [simp]: |
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66358
diff
changeset
|
5785 |
"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
|
5786 |
by (induct xs) simp_all |
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66358
diff
changeset
|
5787 |
|
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66358
diff
changeset
|
5788 |
lemma insort_key_left_comm: |
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66358
diff
changeset
|
5789 |
assumes "f x \<noteq> f y" |
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66358
diff
changeset
|
5790 |
shows "insort_key f y (insort_key f x xs) = insort_key f x (insort_key f y xs)" |
73411 | 5791 |
by (induct xs) (auto simp add: assms dest: order.antisym) |
66434
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66358
diff
changeset
|
5792 |
|
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66358
diff
changeset
|
5793 |
lemma insort_left_comm: |
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66358
diff
changeset
|
5794 |
"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
|
5795 |
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
|
5796 |
|
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66358
diff
changeset
|
5797 |
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
|
5798 |
proof |
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66358
diff
changeset
|
5799 |
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
|
5800 |
|
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66358
diff
changeset
|
5801 |
lemma sort_key_simps [simp]: |
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66358
diff
changeset
|
5802 |
"sort_key f [] = []" |
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66358
diff
changeset
|
5803 |
"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
|
5804 |
by (simp_all add: sort_key_def) |
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66358
diff
changeset
|
5805 |
|
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66358
diff
changeset
|
5806 |
lemma sort_key_conv_fold: |
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66358
diff
changeset
|
5807 |
assumes "inj_on f (set xs)" |
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66358
diff
changeset
|
5808 |
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
|
5809 |
proof - |
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66358
diff
changeset
|
5810 |
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
|
5811 |
proof (rule fold_rev, rule ext) |
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66358
diff
changeset
|
5812 |
fix zs |
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66358
diff
changeset
|
5813 |
fix x y |
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66358
diff
changeset
|
5814 |
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
|
5815 |
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
|
5816 |
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
|
5817 |
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
|
5818 |
by (induct zs) (auto intro: * simp add: **) |
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66358
diff
changeset
|
5819 |
qed |
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66358
diff
changeset
|
5820 |
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
|
5821 |
qed |
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66358
diff
changeset
|
5822 |
|
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66358
diff
changeset
|
5823 |
lemma sort_conv_fold: |
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66358
diff
changeset
|
5824 |
"sort xs = fold insort xs []" |
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66358
diff
changeset
|
5825 |
by (rule sort_key_conv_fold) simp |
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66358
diff
changeset
|
5826 |
|
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66358
diff
changeset
|
5827 |
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
|
5828 |
by (induct xs, auto) |
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66358
diff
changeset
|
5829 |
|
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66358
diff
changeset
|
5830 |
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
|
5831 |
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
|
5832 |
|
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66358
diff
changeset
|
5833 |
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
|
5834 |
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
|
5835 |
|
73832 | 5836 |
lemma distinct_insort_key: |
5837 |
"distinct (map f (insort_key f x xs)) = (f x \<notin> f ` set xs \<and> (distinct (map f xs)))" |
|
5838 |
by (induct xs) (auto simp: set_insort_key) |
|
5839 |
||
66434
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66358
diff
changeset
|
5840 |
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
|
5841 |
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
|
5842 |
|
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66358
diff
changeset
|
5843 |
lemma sorted_insort_key: "sorted (map f (insort_key f x xs)) = sorted (map f xs)" |
68109 | 5844 |
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
|
5845 |
|
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66358
diff
changeset
|
5846 |
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
|
5847 |
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
|
5848 |
|
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66358
diff
changeset
|
5849 |
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
|
5850 |
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
|
5851 |
|
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66358
diff
changeset
|
5852 |
theorem sorted_sort [simp]: "sorted (sort xs)" |
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66358
diff
changeset
|
5853 |
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
|
5854 |
|
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66358
diff
changeset
|
5855 |
lemma insort_not_Nil [simp]: |
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66358
diff
changeset
|
5856 |
"insort_key f a xs \<noteq> []" |
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66358
diff
changeset
|
5857 |
by (induction xs) simp_all |
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66358
diff
changeset
|
5858 |
|
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66358
diff
changeset
|
5859 |
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
|
5860 |
by (cases xs) auto |
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66358
diff
changeset
|
5861 |
|
76484
defaa0b17424
generalized sorted_sort_id to sort_key_id_if_sorted
nipkow
parents:
76376
diff
changeset
|
5862 |
lemma sort_key_id_if_sorted: "sorted (map f xs) \<Longrightarrow> sort_key f xs = xs" |
76485 | 5863 |
by (induction xs) (auto simp add: insort_is_Cons) |
5864 |
||
5865 |
text \<open>Subsumed by @{thm sort_key_id_if_sorted} but easier to find:\<close> |
|
5866 |
lemma sorted_sort_id: "sorted xs \<Longrightarrow> sort xs = xs" |
|
5867 |
by (simp add: sort_key_id_if_sorted) |
|
66434
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66358
diff
changeset
|
5868 |
|
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66358
diff
changeset
|
5869 |
lemma insort_key_remove1: |
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66358
diff
changeset
|
5870 |
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
|
5871 |
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
|
5872 |
using assms proof (induct xs) |
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66358
diff
changeset
|
5873 |
case (Cons x xs) |
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66358
diff
changeset
|
5874 |
then show ?case |
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66358
diff
changeset
|
5875 |
proof (cases "x = a") |
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66358
diff
changeset
|
5876 |
case False |
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66358
diff
changeset
|
5877 |
then have "f x \<noteq> f a" using Cons.prems by auto |
68109 | 5878 |
then have "f x < f a" using Cons.prems by auto |
5879 |
with \<open>f x \<noteq> f a\<close> show ?thesis using Cons by (auto simp: insort_is_Cons) |
|
5880 |
qed (auto simp: insort_is_Cons) |
|
66434
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66358
diff
changeset
|
5881 |
qed simp |
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66358
diff
changeset
|
5882 |
|
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66358
diff
changeset
|
5883 |
lemma insort_remove1: |
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66358
diff
changeset
|
5884 |
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
|
5885 |
shows "insort a (remove1 a xs) = xs" |
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66358
diff
changeset
|
5886 |
proof (rule insort_key_remove1) |
67399 | 5887 |
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
|
5888 |
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
|
5889 |
from \<open>sorted xs\<close> show "sorted (map (\<lambda>x. x) xs)" by simp |
67399 | 5890 |
from \<open>a \<in> set xs\<close> have "a \<in> set (filter ((=) a) xs)" by auto |
5891 |
then have "set (filter ((=) a) xs) \<noteq> {}" by auto |
|
5892 |
then have "filter ((=) a) xs \<noteq> []" by (auto simp only: set_empty) |
|
5893 |
then have "length (filter ((=) a) xs) > 0" by simp |
|
5894 |
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
|
5895 |
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
|
5896 |
by simp |
67399 | 5897 |
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
|
5898 |
qed |
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66358
diff
changeset
|
5899 |
|
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66358
diff
changeset
|
5900 |
lemma finite_sorted_distinct_unique: |
68719 | 5901 |
assumes "finite A" shows "\<exists>!xs. set xs = A \<and> sorted xs \<and> distinct xs" |
5902 |
proof - |
|
5903 |
obtain xs where "distinct xs" "A = set xs" |
|
5904 |
using finite_distinct_list [OF assms] by metis |
|
5905 |
then show ?thesis |
|
5906 |
by (rule_tac a="sort xs" in ex1I) (auto simp: sorted_distinct_set_unique) |
|
5907 |
qed |
|
66434
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66358
diff
changeset
|
5908 |
|
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
|
5909 |
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
|
5910 |
"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
|
5911 |
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
|
5912 |
|
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
|
5913 |
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
|
5914 |
"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
|
5915 |
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
|
5916 |
|
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
|
5917 |
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
|
5918 |
"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
|
5919 |
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
|
5920 |
|
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
|
5921 |
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
|
5922 |
"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
|
5923 |
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
|
5924 |
|
35608 | 5925 |
lemma set_insort_insert: |
5926 |
"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
|
5927 |
by (auto simp add: insort_insert_key_def set_insort_key) |
35608 | 5928 |
|
5929 |
lemma distinct_insort_insert: |
|
5930 |
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
|
5931 |
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
|
5932 |
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
|
5933 |
|
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
|
5934 |
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
|
5935 |
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
|
5936 |
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
|
5937 |
using assms by (simp add: insort_insert_key_def sorted_insort_key) |
35608 | 5938 |
|
5939 |
lemma sorted_insort_insert: |
|
5940 |
assumes "sorted xs" |
|
5941 |
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
|
5942 |
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
|
5943 |
|
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
|
5944 |
lemma filter_insort_triv: |
37107 | 5945 |
"\<not> P x \<Longrightarrow> filter P (insort_key f x xs) = filter P xs" |
5946 |
by (induct xs) simp_all |
|
5947 |
||
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
|
5948 |
lemma filter_insort: |
37107 | 5949 |
"sorted (map f xs) \<Longrightarrow> P x \<Longrightarrow> filter P (insort_key f x xs) = insort_key f x (filter P xs)" |
68109 | 5950 |
by (induct xs) (auto, subst insort_is_Cons, auto) |
37107 | 5951 |
|
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
|
5952 |
lemma filter_sort: |
37107 | 5953 |
"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
|
5954 |
by (induct xs) (simp_all add: filter_insort_triv filter_insort) |
37107 | 5955 |
|
73832 | 5956 |
lemma remove1_insort_key [simp]: |
5957 |
"remove1 x (insort_key f x xs) = xs" |
|
37107 | 5958 |
by (induct xs) simp_all |
5959 |
||
24616 | 5960 |
end |
5961 |
||
68191 | 5962 |
lemma sort_upt [simp]: "sort [m..<n] = [m..<n]" |
76484
defaa0b17424
generalized sorted_sort_id to sort_key_id_if_sorted
nipkow
parents:
76376
diff
changeset
|
5963 |
by (rule sort_key_id_if_sorted) simp |
68191 | 5964 |
|
5965 |
lemma sort_upto [simp]: "sort [i..j] = [i..j]" |
|
76484
defaa0b17424
generalized sorted_sort_id to sort_key_id_if_sorted
nipkow
parents:
76376
diff
changeset
|
5966 |
by (rule sort_key_id_if_sorted) 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
|
5967 |
|
52379 | 5968 |
lemma sorted_find_Min: |
67479 | 5969 |
"sorted xs \<Longrightarrow> \<exists>x \<in> set xs. P x \<Longrightarrow> List.find P xs = Some (Min {x\<in>set xs. P x})" |
5970 |
proof (induct xs) |
|
52379 | 5971 |
case Nil then show ?case by simp |
5972 |
next |
|
67479 | 5973 |
case (Cons x xs) show ?case proof (cases "P x") |
5974 |
case True |
|
68109 | 5975 |
with Cons show ?thesis by (auto intro: Min_eqI [symmetric]) |
52379 | 5976 |
next |
5977 |
case False then have "{y. (y = x \<or> y \<in> set xs) \<and> P y} = {y \<in> set xs. P y}" |
|
5978 |
by auto |
|
68109 | 5979 |
with Cons False show ?thesis by (simp_all) |
52379 | 5980 |
qed |
5981 |
qed |
|
5982 |
||
66853 | 5983 |
lemma sorted_enumerate [simp]: "sorted (map fst (enumerate n xs))" |
5984 |
by (simp add: enumerate_eq_zip) |
|
58437 | 5985 |
|
75599 | 5986 |
lemma sorted_insort_is_snoc: "sorted xs \<Longrightarrow> \<forall>x \<in> set xs. a \<ge> x \<Longrightarrow> insort a xs = xs @ [a]" |
5987 |
by (induct xs) (auto dest!: insort_is_Cons) |
|
5988 |
||
69593 | 5989 |
text \<open>Stability of \<^const>\<open>sort_key\<close>:\<close> |
67684 | 5990 |
|
68249
949d93804740
First step to remove nonstandard "[x <- xs. P]" syntax: only input
nipkow
parents:
68215
diff
changeset
|
5991 |
lemma sort_key_stable: "filter (\<lambda>y. f y = k) (sort_key f xs) = filter (\<lambda>y. f y = k) xs" |
70296 | 5992 |
by (induction xs) (auto simp: filter_insort insort_is_Cons filter_insort_triv) |
66654 | 5993 |
|
67684 | 5994 |
corollary stable_sort_key_sort_key: "stable_sort_key sort_key" |
5995 |
by(simp add: stable_sort_key_def sort_key_stable) |
|
5996 |
||
67606 | 5997 |
lemma sort_key_const: "sort_key (\<lambda>x. c) xs = xs" |
67684 | 5998 |
by (metis (mono_tags) filter_True sort_key_stable) |
67606 | 5999 |
|
35115 | 6000 |
|
69593 | 6001 |
subsubsection \<open>\<^const>\<open>transpose\<close> on sorted lists\<close> |
34933 | 6002 |
|
66853 | 6003 |
lemma sorted_transpose[simp]: "sorted (rev (map length (transpose xs)))" |
68175 | 6004 |
by (auto simp: sorted_iff_nth_mono rev_nth nth_transpose |
34933 | 6005 |
length_filter_conv_card intro: card_mono) |
6006 |
||
6007 |
lemma transpose_max_length: |
|
68249
949d93804740
First step to remove nonstandard "[x <- xs. P]" syntax: only input
nipkow
parents:
68215
diff
changeset
|
6008 |
"foldr (\<lambda>xs. max (length xs)) (transpose xs) 0 = length (filter (\<lambda>x. x \<noteq> []) xs)" |
34933 | 6009 |
(is "?L = ?R") |
6010 |
proof (cases "transpose xs = []") |
|
6011 |
case False |
|
6012 |
have "?L = foldr max (map length (transpose xs)) 0" |
|
6013 |
by (simp add: foldr_map comp_def) |
|
6014 |
also have "... = length (transpose xs ! 0)" |
|
6015 |
using False sorted_transpose by (simp add: foldr_max_sorted) |
|
6016 |
finally show ?thesis |
|
6017 |
using False by (simp add: nth_transpose) |
|
6018 |
next |
|
6019 |
case True |
|
68249
949d93804740
First step to remove nonstandard "[x <- xs. P]" syntax: only input
nipkow
parents:
68215
diff
changeset
|
6020 |
hence "filter (\<lambda>x. x \<noteq> []) xs = []" |
34933 | 6021 |
by (auto intro!: filter_False simp: transpose_empty) |
6022 |
thus ?thesis by (simp add: transpose_empty True) |
|
6023 |
qed |
|
6024 |
||
6025 |
lemma length_transpose_sorted: |
|
6026 |
fixes xs :: "'a list list" |
|
6027 |
assumes sorted: "sorted (rev (map length xs))" |
|
6028 |
shows "length (transpose xs) = (if xs = [] then 0 else length (xs ! 0))" |
|
6029 |
proof (cases "xs = []") |
|
6030 |
case False |
|
6031 |
thus ?thesis |
|
6032 |
using foldr_max_sorted[OF sorted] False |
|
6033 |
unfolding length_transpose foldr_map comp_def |
|
6034 |
by simp |
|
6035 |
qed simp |
|
6036 |
||
6037 |
lemma nth_nth_transpose_sorted[simp]: |
|
6038 |
fixes xs :: "'a list list" |
|
6039 |
assumes sorted: "sorted (rev (map length xs))" |
|
6040 |
and i: "i < length (transpose xs)" |
|
68249
949d93804740
First step to remove nonstandard "[x <- xs. P]" syntax: only input
nipkow
parents:
68215
diff
changeset
|
6041 |
and j: "j < length (filter (\<lambda>ys. i < length ys) xs)" |
34933 | 6042 |
shows "transpose xs ! i ! j = xs ! j ! i" |
66853 | 6043 |
using j filter_equals_takeWhile_sorted_rev[OF sorted, of i] |
34933 | 6044 |
nth_transpose[OF i] nth_map[OF j] |
66853 | 6045 |
by (simp add: takeWhile_nth) |
34933 | 6046 |
|
6047 |
lemma transpose_column_length: |
|
6048 |
fixes xs :: "'a list list" |
|
6049 |
assumes sorted: "sorted (rev (map length xs))" and "i < length xs" |
|
6050 |
shows "length (filter (\<lambda>ys. i < length ys) (transpose xs)) = length (xs ! i)" |
|
6051 |
proof - |
|
60758 | 6052 |
have "xs \<noteq> []" using \<open>i < length xs\<close> by auto |
34933 | 6053 |
note filter_equals_takeWhile_sorted_rev[OF sorted, simp] |
6054 |
{ fix j assume "j \<le> i" |
|
60758 | 6055 |
note sorted_rev_nth_mono[OF sorted, of j i, simplified, OF this \<open>i < length xs\<close>] |
34933 | 6056 |
} note sortedE = this[consumes 1] |
6057 |
||
6058 |
have "{j. j < length (transpose xs) \<and> i < length (transpose xs ! j)} |
|
6059 |
= {..< length (xs ! i)}" |
|
6060 |
proof safe |
|
6061 |
fix j |
|
6062 |
assume "j < length (transpose xs)" and "i < length (transpose xs ! j)" |
|
6063 |
with this(2) nth_transpose[OF this(1)] |
|
6064 |
have "i < length (takeWhile (\<lambda>ys. j < length ys) xs)" by simp |
|
6065 |
from nth_mem[OF this] takeWhile_nth[OF this] |
|
6066 |
show "j < length (xs ! i)" by (auto dest: set_takeWhileD) |
|
6067 |
next |
|
6068 |
fix j assume "j < length (xs ! i)" |
|
6069 |
thus "j < length (transpose xs)" |
|
60758 | 6070 |
using foldr_max_sorted[OF sorted] \<open>xs \<noteq> []\<close> sortedE[OF le0] |
34933 | 6071 |
by (auto simp: length_transpose comp_def foldr_map) |
6072 |
||
6073 |
have "Suc i \<le> length (takeWhile (\<lambda>ys. j < length ys) xs)" |
|
60758 | 6074 |
using \<open>i < length xs\<close> \<open>j < length (xs ! i)\<close> less_Suc_eq_le |
34933 | 6075 |
by (auto intro!: length_takeWhile_less_P_nth dest!: sortedE) |
60758 | 6076 |
with nth_transpose[OF \<open>j < length (transpose xs)\<close>] |
34933 | 6077 |
show "i < length (transpose xs ! j)" by simp |
6078 |
qed |
|
6079 |
thus ?thesis by (simp add: length_filter_conv_card) |
|
6080 |
qed |
|
6081 |
||
6082 |
lemma transpose_column: |
|
6083 |
fixes xs :: "'a list list" |
|
6084 |
assumes sorted: "sorted (rev (map length xs))" and "i < length xs" |
|
6085 |
shows "map (\<lambda>ys. ys ! i) (filter (\<lambda>ys. i < length ys) (transpose xs)) |
|
6086 |
= 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
|
6087 |
proof (rule nth_equalityI) |
34933 | 6088 |
show length: "length ?R = length (xs ! i)" |
6089 |
using transpose_column_length[OF assms] by simp |
|
6090 |
||
6091 |
fix j assume j: "j < length ?R" |
|
6092 |
note * = less_le_trans[OF this, unfolded length_map, OF length_filter_le] |
|
6093 |
from j have j_less: "j < length (xs ! i)" using length by simp |
|
6094 |
have i_less_tW: "Suc i \<le> length (takeWhile (\<lambda>ys. Suc j \<le> length ys) xs)" |
|
6095 |
proof (rule length_takeWhile_less_P_nth) |
|
60758 | 6096 |
show "Suc i \<le> length xs" using \<open>i < length xs\<close> by simp |
34933 | 6097 |
fix k assume "k < Suc i" |
6098 |
hence "k \<le> i" by auto |
|
60758 | 6099 |
with sorted_rev_nth_mono[OF sorted this] \<open>i < length xs\<close> |
34933 | 6100 |
have "length (xs ! i) \<le> length (xs ! k)" by simp |
6101 |
thus "Suc j \<le> length (xs ! k)" using j_less by simp |
|
6102 |
qed |
|
68249
949d93804740
First step to remove nonstandard "[x <- xs. P]" syntax: only input
nipkow
parents:
68215
diff
changeset
|
6103 |
have i_less_filter: "i < length (filter (\<lambda>ys. j < length ys) xs) " |
34933 | 6104 |
unfolding filter_equals_takeWhile_sorted_rev[OF sorted, of j] |
6105 |
using i_less_tW by (simp_all add: Suc_le_eq) |
|
6106 |
from j show "?R ! j = xs ! i ! j" |
|
6107 |
unfolding filter_equals_takeWhile_sorted_rev[OF sorted_transpose, of i] |
|
6108 |
by (simp add: takeWhile_nth nth_nth_transpose_sorted[OF sorted * i_less_filter]) |
|
6109 |
qed |
|
6110 |
||
6111 |
lemma transpose_transpose: |
|
6112 |
fixes xs :: "'a list list" |
|
6113 |
assumes sorted: "sorted (rev (map length xs))" |
|
6114 |
shows "transpose (transpose xs) = takeWhile (\<lambda>x. x \<noteq> []) xs" (is "?L = ?R") |
|
6115 |
proof - |
|
6116 |
have len: "length ?L = length ?R" |
|
6117 |
unfolding length_transpose transpose_max_length |
|
6118 |
using filter_equals_takeWhile_sorted_rev[OF sorted, of 0] |
|
6119 |
by simp |
|
6120 |
||
6121 |
{ fix i assume "i < length ?R" |
|
6122 |
with less_le_trans[OF _ length_takeWhile_le[of _ xs]] |
|
6123 |
have "i < length xs" by simp |
|
6124 |
} note * = this |
|
6125 |
show ?thesis |
|
6126 |
by (rule nth_equalityI) |
|
6127 |
(simp_all add: len nth_transpose transpose_column[OF sorted] * takeWhile_nth) |
|
6128 |
qed |
|
24616 | 6129 |
|
34934
440605046777
Added transpose_rectangle, when the input list is rectangular.
hoelzl
parents:
34933
diff
changeset
|
6130 |
theorem transpose_rectangle: |
440605046777
Added transpose_rectangle, when the input list is rectangular.
hoelzl
parents:
34933
diff
changeset
|
6131 |
assumes "xs = [] \<Longrightarrow> n = 0" |
440605046777
Added transpose_rectangle, when the input list is rectangular.
hoelzl
parents:
34933
diff
changeset
|
6132 |
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
|
6133 |
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
|
6134 |
(is "?trans = ?map") |
440605046777
Added transpose_rectangle, when the input list is rectangular.
hoelzl
parents:
34933
diff
changeset
|
6135 |
proof (rule nth_equalityI) |
440605046777
Added transpose_rectangle, when the input list is rectangular.
hoelzl
parents:
34933
diff
changeset
|
6136 |
have "sorted (rev (map length xs))" |
68175 | 6137 |
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
|
6138 |
from foldr_max_sorted[OF this] assms |
440605046777
Added transpose_rectangle, when the input list is rectangular.
hoelzl
parents:
34933
diff
changeset
|
6139 |
show len: "length ?trans = length ?map" |
440605046777
Added transpose_rectangle, when the input list is rectangular.
hoelzl
parents:
34933
diff
changeset
|
6140 |
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
|
6141 |
moreover |
68249
949d93804740
First step to remove nonstandard "[x <- xs. P]" syntax: only input
nipkow
parents:
68215
diff
changeset
|
6142 |
{ 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
|
6143 |
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
|
6144 |
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
|
6145 |
by (auto simp: nth_transpose intro: nth_equalityI) |
440605046777
Added transpose_rectangle, when the input list is rectangular.
hoelzl
parents:
34933
diff
changeset
|
6146 |
qed |
24616 | 6147 |
|
35115 | 6148 |
|
73832 | 6149 |
subsubsection \<open>\<open>sorted_key_list_of_set\<close>\<close> |
6150 |
||
6151 |
text\<open> |
|
6152 |
This function maps (finite) linearly ordered sets to sorted lists. |
|
6153 |
The linear order is obtained by a key function that maps the elements of the set to a type |
|
6154 |
that is linearly ordered. |
|
6155 |
Warning: in most cases it is not a good idea to convert from |
|
6156 |
sets to lists but one should convert in the other direction (via \<^const>\<open>set\<close>). |
|
6157 |
||
6158 |
Note: this is a generalisation of the older \<open>sorted_list_of_set\<close> that is obtained by setting |
|
6159 |
the key function to the identity. Consequently, new theorems should be added to the locale |
|
6160 |
below. They should also be aliased to more convenient names for use with \<open>sorted_list_of_set\<close> |
|
6161 |
as seen further below. |
|
6162 |
\<close> |
|
6163 |
||
6164 |
definition (in linorder) sorted_key_list_of_set :: "('b \<Rightarrow> 'a) \<Rightarrow> 'b set \<Rightarrow> 'b list" |
|
6165 |
where "sorted_key_list_of_set f \<equiv> folding_on.F (insort_key f) []" |
|
6166 |
||
6167 |
locale folding_insort_key = lo?: linorder "less_eq :: 'a \<Rightarrow> 'a \<Rightarrow> bool" less |
|
6168 |
for less_eq (infix "\<preceq>" 50) and less (infix "\<prec>" 50) + |
|
6169 |
fixes S |
|
6170 |
fixes f :: "'b \<Rightarrow> 'a" |
|
6171 |
assumes inj_on: "inj_on f S" |
|
54868 | 6172 |
begin |
6173 |
||
73832 | 6174 |
lemma insort_key_commute: |
6175 |
"x \<in> S \<Longrightarrow> y \<in> S \<Longrightarrow> insort_key f y o insort_key f x = insort_key f x o insort_key f y" |
|
6176 |
proof(rule ext, goal_cases) |
|
6177 |
case (1 xs) |
|
6178 |
with inj_on show ?case by (induction xs) (auto simp: inj_onD) |
|
6179 |
qed |
|
6180 |
||
6181 |
sublocale fold_insort_key: folding_on S "insort_key f" "[]" |
|
6182 |
rewrites "folding_on.F (insort_key f) [] = sorted_key_list_of_set f" |
|
51489 | 6183 |
proof - |
73832 | 6184 |
show "folding_on S (insort_key f)" |
6185 |
by standard (simp add: insort_key_commute) |
|
6186 |
qed (simp add: sorted_key_list_of_set_def) |
|
6187 |
||
6188 |
lemma idem_if_sorted_distinct: |
|
6189 |
assumes "set xs \<subseteq> S" and "sorted (map f xs)" "distinct xs" |
|
6190 |
shows "sorted_key_list_of_set f (set xs) = xs" |
|
6191 |
proof(cases "S = {}") |
|
6192 |
case True |
|
6193 |
then show ?thesis using \<open>set xs \<subseteq> S\<close> by auto |
|
6194 |
next |
|
6195 |
case False |
|
6196 |
with assms show ?thesis |
|
6197 |
proof(induction xs) |
|
6198 |
case (Cons a xs) |
|
6199 |
with Cons show ?case by (cases xs) auto |
|
6200 |
qed simp |
|
6201 |
qed |
|
6202 |
||
6203 |
lemma sorted_key_list_of_set_empty: |
|
6204 |
"sorted_key_list_of_set f {} = []" |
|
6205 |
by (fact fold_insort_key.empty) |
|
6206 |
||
6207 |
lemma sorted_key_list_of_set_insert: |
|
6208 |
assumes "insert x A \<subseteq> S" and "finite A" "x \<notin> A" |
|
6209 |
shows "sorted_key_list_of_set f (insert x A) |
|
6210 |
= insort_key f x (sorted_key_list_of_set f A)" |
|
6211 |
using assms by (fact fold_insort_key.insert) |
|
6212 |
||
6213 |
lemma sorted_key_list_of_set_insert_remove [simp]: |
|
6214 |
assumes "insert x A \<subseteq> S" and "finite A" |
|
6215 |
shows "sorted_key_list_of_set f (insert x A) |
|
6216 |
= insort_key f x (sorted_key_list_of_set f (A - {x}))" |
|
6217 |
using assms by (fact fold_insort_key.insert_remove) |
|
6218 |
||
6219 |
lemma sorted_key_list_of_set_eq_Nil_iff [simp]: |
|
6220 |
assumes "A \<subseteq> S" and "finite A" |
|
6221 |
shows "sorted_key_list_of_set f A = [] \<longleftrightarrow> A = {}" |
|
6222 |
using assms by (auto simp: fold_insort_key.remove) |
|
6223 |
||
6224 |
lemma set_sorted_key_list_of_set [simp]: |
|
6225 |
assumes "A \<subseteq> S" and "finite A" |
|
6226 |
shows "set (sorted_key_list_of_set f A) = A" |
|
6227 |
using assms(2,1) |
|
6228 |
by (induct A rule: finite_induct) (simp_all add: set_insort_key) |
|
6229 |
||
6230 |
lemma sorted_sorted_key_list_of_set [simp]: |
|
6231 |
assumes "A \<subseteq> S" |
|
6232 |
shows "sorted (map f (sorted_key_list_of_set f A))" |
|
68083 | 6233 |
proof (cases "finite A") |
73832 | 6234 |
case True thus ?thesis using \<open>A \<subseteq> S\<close> |
6235 |
by (induction A) (simp_all add: sorted_insort_key) |
|
68083 | 6236 |
next |
6237 |
case False thus ?thesis by simp |
|
6238 |
qed |
|
6239 |
||
73832 | 6240 |
lemma distinct_if_distinct_map: "distinct (map f xs) \<Longrightarrow> distinct xs" |
6241 |
using inj_on by (simp add: distinct_map) |
|
6242 |
||
6243 |
lemma distinct_sorted_key_list_of_set [simp]: |
|
6244 |
assumes "A \<subseteq> S" |
|
6245 |
shows "distinct (map f (sorted_key_list_of_set f A))" |
|
68083 | 6246 |
proof (cases "finite A") |
73832 | 6247 |
case True thus ?thesis using \<open>A \<subseteq> S\<close> inj_on |
6248 |
by (induction A) (force simp: distinct_insort_key dest: inj_onD)+ |
|
6249 |
next |
|
68083 | 6250 |
case False thus ?thesis by simp |
6251 |
qed |
|
52122 | 6252 |
|
73832 | 6253 |
lemma length_sorted_key_list_of_set [simp]: |
6254 |
assumes "A \<subseteq> S" |
|
6255 |
shows "length (sorted_key_list_of_set f A) = card A" |
|
71860 | 6256 |
proof (cases "finite A") |
6257 |
case True |
|
73832 | 6258 |
with assms inj_on show ?thesis |
6259 |
using distinct_card[symmetric, OF distinct_sorted_key_list_of_set] |
|
6260 |
by (auto simp: subset_inj_on intro!: card_image) |
|
71860 | 6261 |
qed auto |
71855 | 6262 |
|
73832 | 6263 |
lemmas sorted_key_list_of_set = |
6264 |
set_sorted_key_list_of_set sorted_sorted_key_list_of_set distinct_sorted_key_list_of_set |
|
6265 |
||
6266 |
lemma sorted_key_list_of_set_remove: |
|
6267 |
assumes "insert x A \<subseteq> S" and "finite A" |
|
6268 |
shows "sorted_key_list_of_set f (A - {x}) = remove1 x (sorted_key_list_of_set f A)" |
|
37107 | 6269 |
proof (cases "x \<in> A") |
73832 | 6270 |
case False with assms have "x \<notin> set (sorted_key_list_of_set f A)" by simp |
37107 | 6271 |
with False show ?thesis by (simp add: remove1_idem) |
6272 |
next |
|
6273 |
case True then obtain B where A: "A = insert x B" by (rule Set.set_insert) |
|
6274 |
with assms show ?thesis by simp |
|
6275 |
qed |
|
6276 |
||
73832 | 6277 |
lemma strict_sorted_key_list_of_set [simp]: |
6278 |
"A \<subseteq> S \<Longrightarrow> sorted_wrt (\<prec>) (map f (sorted_key_list_of_set f A))" |
|
6279 |
by (cases "finite A") (auto simp: strict_sorted_iff subset_inj_on[OF inj_on]) |
|
71404
f2b783abfbe7
A few lemmas connected with orderings
paulson <lp15@cam.ac.uk>
parents:
71393
diff
changeset
|
6280 |
|
f2b783abfbe7
A few lemmas connected with orderings
paulson <lp15@cam.ac.uk>
parents:
71393
diff
changeset
|
6281 |
lemma finite_set_strict_sorted: |
73832 | 6282 |
assumes "A \<subseteq> S" and "finite A" |
6283 |
obtains l where "sorted_wrt (\<prec>) (map f l)" "set l = A" "length l = card A" |
|
6284 |
using assms |
|
6285 |
by (meson length_sorted_key_list_of_set set_sorted_key_list_of_set strict_sorted_key_list_of_set) |
|
6286 |
||
6287 |
lemma (in linorder) strict_sorted_equal: |
|
73709 | 6288 |
assumes "sorted_wrt (<) xs" |
73832 | 6289 |
and "sorted_wrt (<) ys" |
6290 |
and "set ys = set xs" |
|
6291 |
shows "ys = xs" |
|
71827 | 6292 |
using assms |
6293 |
proof (induction xs arbitrary: ys) |
|
6294 |
case (Cons x xs) |
|
6295 |
show ?case |
|
6296 |
proof (cases ys) |
|
6297 |
case Nil |
|
6298 |
then show ?thesis |
|
74424 | 6299 |
using Cons.prems by auto |
71827 | 6300 |
next |
6301 |
case (Cons y ys') |
|
6302 |
then have "xs = ys'" |
|
6303 |
by (metis Cons.prems list.inject sorted_distinct_set_unique strict_sorted_iff) |
|
6304 |
moreover have "x = y" |
|
6305 |
using Cons.prems \<open>xs = ys'\<close> local.Cons by fastforce |
|
6306 |
ultimately show ?thesis |
|
6307 |
using local.Cons by blast |
|
6308 |
qed |
|
6309 |
qed auto |
|
74424 | 6310 |
|
73832 | 6311 |
lemma (in linorder) strict_sorted_equal_Uniq: "\<exists>\<^sub>\<le>\<^sub>1xs. sorted_wrt (<) xs \<and> set xs = A" |
71827 | 6312 |
by (simp add: Uniq_def strict_sorted_equal) |
6313 |
||
73832 | 6314 |
lemma sorted_key_list_of_set_inject: |
6315 |
assumes "A \<subseteq> S" "B \<subseteq> S" |
|
74424 | 6316 |
assumes "sorted_key_list_of_set f A = sorted_key_list_of_set f B" "finite A" "finite B" |
71857
d73955442df5
a few new lemmas about functions
paulson <lp15@cam.ac.uk>
parents:
71856
diff
changeset
|
6317 |
shows "A = B" |
73832 | 6318 |
using assms set_sorted_key_list_of_set by metis |
6319 |
||
6320 |
lemma sorted_key_list_of_set_unique: |
|
6321 |
assumes "A \<subseteq> S" and "finite A" |
|
6322 |
shows "sorted_wrt (\<prec>) (map f l) \<and> set l = A \<and> length l = card A |
|
6323 |
\<longleftrightarrow> sorted_key_list_of_set f A = l" |
|
6324 |
using assms |
|
6325 |
by (auto simp: strict_sorted_iff card_distinct idem_if_sorted_distinct) |
|
71857
d73955442df5
a few new lemmas about functions
paulson <lp15@cam.ac.uk>
parents:
71856
diff
changeset
|
6326 |
|
71860 | 6327 |
end |
6328 |
||
73832 | 6329 |
context linorder |
6330 |
begin |
|
6331 |
||
6332 |
definition "sorted_list_of_set \<equiv> sorted_key_list_of_set (\<lambda>x::'a. x)" |
|
6333 |
||
6334 |
text \<open> |
|
6335 |
We abuse the \<open>rewrites\<close> functionality of locales to remove trivial assumptions that result |
|
6336 |
from instantiating the key function to the identity. |
|
6337 |
\<close> |
|
6338 |
sublocale sorted_list_of_set: folding_insort_key "(\<le>)" "(<)" UNIV "(\<lambda>x. x)" |
|
6339 |
rewrites "sorted_key_list_of_set (\<lambda>x. x) = sorted_list_of_set" |
|
6340 |
and "\<And>xs. map (\<lambda>x. x) xs \<equiv> xs" |
|
6341 |
and "\<And>X. (X \<subseteq> UNIV) \<equiv> True" |
|
6342 |
and "\<And>x. x \<in> UNIV \<equiv> True" |
|
6343 |
and "\<And>P. (True \<Longrightarrow> P) \<equiv> Trueprop P" |
|
6344 |
and "\<And>P Q. (True \<Longrightarrow> PROP P \<Longrightarrow> PROP Q) \<equiv> (PROP P \<Longrightarrow> True \<Longrightarrow> PROP Q)" |
|
6345 |
proof - |
|
6346 |
show "folding_insort_key (\<le>) (<) UNIV (\<lambda>x. x)" |
|
6347 |
by standard simp |
|
6348 |
qed (simp_all add: sorted_list_of_set_def) |
|
6349 |
||
6350 |
text \<open>Alias theorems for backwards compatibility and ease of use.\<close> |
|
6351 |
lemmas sorted_list_of_set = sorted_list_of_set.sorted_key_list_of_set and |
|
6352 |
sorted_list_of_set_empty = sorted_list_of_set.sorted_key_list_of_set_empty and |
|
6353 |
sorted_list_of_set_insert = sorted_list_of_set.sorted_key_list_of_set_insert and |
|
6354 |
sorted_list_of_set_insert_remove = sorted_list_of_set.sorted_key_list_of_set_insert_remove and |
|
6355 |
sorted_list_of_set_eq_Nil_iff = sorted_list_of_set.sorted_key_list_of_set_eq_Nil_iff and |
|
6356 |
set_sorted_list_of_set = sorted_list_of_set.set_sorted_key_list_of_set and |
|
6357 |
sorted_sorted_list_of_set = sorted_list_of_set.sorted_sorted_key_list_of_set and |
|
6358 |
distinct_sorted_list_of_set = sorted_list_of_set.distinct_sorted_key_list_of_set and |
|
6359 |
length_sorted_list_of_set = sorted_list_of_set.length_sorted_key_list_of_set and |
|
6360 |
sorted_list_of_set_remove = sorted_list_of_set.sorted_key_list_of_set_remove and |
|
6361 |
strict_sorted_list_of_set = sorted_list_of_set.strict_sorted_key_list_of_set and |
|
6362 |
sorted_list_of_set_inject = sorted_list_of_set.sorted_key_list_of_set_inject and |
|
6363 |
sorted_list_of_set_unique = sorted_list_of_set.sorted_key_list_of_set_unique and |
|
6364 |
finite_set_strict_sorted = sorted_list_of_set.finite_set_strict_sorted |
|
6365 |
||
6366 |
lemma sorted_list_of_set_sort_remdups [code]: |
|
6367 |
"sorted_list_of_set (set xs) = sort (remdups xs)" |
|
6368 |
proof - |
|
6369 |
interpret comp_fun_commute insort by (fact comp_fun_commute_insort) |
|
6370 |
show ?thesis |
|
6371 |
by (simp add: sorted_list_of_set.fold_insort_key.eq_fold sort_conv_fold fold_set_fold_remdups) |
|
6372 |
qed |
|
6373 |
||
6374 |
end |
|
6375 |
||
6376 |
||
71860 | 6377 |
lemma sorted_list_of_set_range [simp]: |
6378 |
"sorted_list_of_set {m..<n} = [m..<n]" |
|
6379 |
by (rule sorted_distinct_set_unique) simp_all |
|
71857
d73955442df5
a few new lemmas about functions
paulson <lp15@cam.ac.uk>
parents:
71856
diff
changeset
|
6380 |
|
74424 | 6381 |
lemma sorted_list_of_set_lessThan_Suc [simp]: |
71857
d73955442df5
a few new lemmas about functions
paulson <lp15@cam.ac.uk>
parents:
71856
diff
changeset
|
6382 |
"sorted_list_of_set {..<Suc k} = sorted_list_of_set {..<k} @ [k]" |
73683
60a788467639
strict_sorted now an abbreviation
paulson <lp15@cam.ac.uk>
parents:
73510
diff
changeset
|
6383 |
using le0 lessThan_atLeast0 sorted_list_of_set_range upt_Suc_append by presburger |
71857
d73955442df5
a few new lemmas about functions
paulson <lp15@cam.ac.uk>
parents:
71856
diff
changeset
|
6384 |
|
d73955442df5
a few new lemmas about functions
paulson <lp15@cam.ac.uk>
parents:
71856
diff
changeset
|
6385 |
lemma sorted_list_of_set_atMost_Suc [simp]: |
d73955442df5
a few new lemmas about functions
paulson <lp15@cam.ac.uk>
parents:
71856
diff
changeset
|
6386 |
"sorted_list_of_set {..Suc k} = sorted_list_of_set {..k} @ [Suc k]" |
d73955442df5
a few new lemmas about functions
paulson <lp15@cam.ac.uk>
parents:
71856
diff
changeset
|
6387 |
using lessThan_Suc_atMost sorted_list_of_set_lessThan_Suc by fastforce |
d73955442df5
a few new lemmas about functions
paulson <lp15@cam.ac.uk>
parents:
71856
diff
changeset
|
6388 |
|
72095
cfb6c22a5636
lemmas about sets and the enumerate operator
paulson <lp15@cam.ac.uk>
parents:
71991
diff
changeset
|
6389 |
lemma sorted_list_of_set_nonempty: |
cfb6c22a5636
lemmas about sets and the enumerate operator
paulson <lp15@cam.ac.uk>
parents:
71991
diff
changeset
|
6390 |
assumes "finite A" "A \<noteq> {}" |
cfb6c22a5636
lemmas about sets and the enumerate operator
paulson <lp15@cam.ac.uk>
parents:
71991
diff
changeset
|
6391 |
shows "sorted_list_of_set A = Min A # sorted_list_of_set (A - {Min A})" |
73832 | 6392 |
using assms |
6393 |
by (auto simp: less_le simp flip: sorted_list_of_set.sorted_key_list_of_set_unique intro: Min_in) |
|
72095
cfb6c22a5636
lemmas about sets and the enumerate operator
paulson <lp15@cam.ac.uk>
parents:
71991
diff
changeset
|
6394 |
|
71857
d73955442df5
a few new lemmas about functions
paulson <lp15@cam.ac.uk>
parents:
71856
diff
changeset
|
6395 |
lemma sorted_list_of_set_greaterThanLessThan: |
74424 | 6396 |
assumes "Suc i < j" |
71857
d73955442df5
a few new lemmas about functions
paulson <lp15@cam.ac.uk>
parents:
71856
diff
changeset
|
6397 |
shows "sorted_list_of_set {i<..<j} = Suc i # sorted_list_of_set {Suc i<..<j}" |
d73955442df5
a few new lemmas about functions
paulson <lp15@cam.ac.uk>
parents:
71856
diff
changeset
|
6398 |
proof - |
d73955442df5
a few new lemmas about functions
paulson <lp15@cam.ac.uk>
parents:
71856
diff
changeset
|
6399 |
have "{i<..<j} = insert (Suc i) {Suc i<..<j}" |
d73955442df5
a few new lemmas about functions
paulson <lp15@cam.ac.uk>
parents:
71856
diff
changeset
|
6400 |
using assms by auto |
d73955442df5
a few new lemmas about functions
paulson <lp15@cam.ac.uk>
parents:
71856
diff
changeset
|
6401 |
then show ?thesis |
d73955442df5
a few new lemmas about functions
paulson <lp15@cam.ac.uk>
parents:
71856
diff
changeset
|
6402 |
by (metis assms atLeastSucLessThan_greaterThanLessThan sorted_list_of_set_range upt_conv_Cons) |
d73955442df5
a few new lemmas about functions
paulson <lp15@cam.ac.uk>
parents:
71856
diff
changeset
|
6403 |
qed |
d73955442df5
a few new lemmas about functions
paulson <lp15@cam.ac.uk>
parents:
71856
diff
changeset
|
6404 |
|
d73955442df5
a few new lemmas about functions
paulson <lp15@cam.ac.uk>
parents:
71856
diff
changeset
|
6405 |
lemma sorted_list_of_set_greaterThanAtMost: |
74424 | 6406 |
assumes "Suc i \<le> j" |
71857
d73955442df5
a few new lemmas about functions
paulson <lp15@cam.ac.uk>
parents:
71856
diff
changeset
|
6407 |
shows "sorted_list_of_set {i<..j} = Suc i # sorted_list_of_set {Suc i<..j}" |
d73955442df5
a few new lemmas about functions
paulson <lp15@cam.ac.uk>
parents:
71856
diff
changeset
|
6408 |
using sorted_list_of_set_greaterThanLessThan [of i "Suc j"] |
d73955442df5
a few new lemmas about functions
paulson <lp15@cam.ac.uk>
parents:
71856
diff
changeset
|
6409 |
by (metis assms greaterThanAtMost_def greaterThanLessThan_eq le_imp_less_Suc lessThan_Suc_atMost) |
d73955442df5
a few new lemmas about functions
paulson <lp15@cam.ac.uk>
parents:
71856
diff
changeset
|
6410 |
|
74424 | 6411 |
lemma nth_sorted_list_of_set_greaterThanLessThan: |
71857
d73955442df5
a few new lemmas about functions
paulson <lp15@cam.ac.uk>
parents:
71856
diff
changeset
|
6412 |
"n < j - Suc i \<Longrightarrow> sorted_list_of_set {i<..<j} ! n = Suc (i+n)" |
d73955442df5
a few new lemmas about functions
paulson <lp15@cam.ac.uk>
parents:
71856
diff
changeset
|
6413 |
by (induction n arbitrary: i) (auto simp: sorted_list_of_set_greaterThanLessThan) |
d73955442df5
a few new lemmas about functions
paulson <lp15@cam.ac.uk>
parents:
71856
diff
changeset
|
6414 |
|
74424 | 6415 |
lemma nth_sorted_list_of_set_greaterThanAtMost: |
71857
d73955442df5
a few new lemmas about functions
paulson <lp15@cam.ac.uk>
parents:
71856
diff
changeset
|
6416 |
"n < j - i \<Longrightarrow> sorted_list_of_set {i<..j} ! n = Suc (i+n)" |
d73955442df5
a few new lemmas about functions
paulson <lp15@cam.ac.uk>
parents:
71856
diff
changeset
|
6417 |
using nth_sorted_list_of_set_greaterThanLessThan [of n "Suc j" i] |
d73955442df5
a few new lemmas about functions
paulson <lp15@cam.ac.uk>
parents:
71856
diff
changeset
|
6418 |
by (simp add: greaterThanAtMost_def greaterThanLessThan_eq lessThan_Suc_atMost) |
d73955442df5
a few new lemmas about functions
paulson <lp15@cam.ac.uk>
parents:
71856
diff
changeset
|
6419 |
|
d73955442df5
a few new lemmas about functions
paulson <lp15@cam.ac.uk>
parents:
71856
diff
changeset
|
6420 |
|
61799 | 6421 |
subsubsection \<open>\<open>lists\<close>: the list-forming operator over sets\<close> |
15302 | 6422 |
|
23740 | 6423 |
inductive_set |
22262 | 6424 |
lists :: "'a set => 'a list set" |
23740 | 6425 |
for A :: "'a set" |
6426 |
where |
|
67613 | 6427 |
Nil [intro!, simp]: "[] \<in> lists A" |
6428 |
| Cons [intro!, simp]: "\<lbrakk>a \<in> A; l \<in> lists A\<rbrakk> \<Longrightarrow> a#l \<in> lists A" |
|
6429 |
||
6430 |
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
|
6431 |
inductive_cases listspE [elim!]: "listsp A (x # l)" |
23740 | 6432 |
|
46313 | 6433 |
inductive_simps listsp_simps[code]: |
6434 |
"listsp A []" |
|
6435 |
"listsp A (x # xs)" |
|
6436 |
||
71585 | 6437 |
lemma listsp_mono [mono]: "A \<le> B \<Longrightarrow> listsp A \<le> listsp B" |
46884 | 6438 |
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
|
6439 |
|
46176
1898e61e89c4
pred_subset/equals_eq are now standard pred_set_conv rules
berghofe
parents:
46156
diff
changeset
|
6440 |
lemmas lists_mono = listsp_mono [to_set] |
22262 | 6441 |
|
22422
ee19cdb07528
stepping towards uniform lattice theory development in HOL
haftmann
parents:
22262
diff
changeset
|
6442 |
lemma listsp_infI: |
71585 | 6443 |
assumes l: "listsp A l" shows "listsp B l \<Longrightarrow> listsp (inf A B) l" using l |
24349 | 6444 |
by induct blast+ |
15302 | 6445 |
|
22422
ee19cdb07528
stepping towards uniform lattice theory development in HOL
haftmann
parents:
22262
diff
changeset
|
6446 |
lemmas lists_IntI = listsp_infI [to_set] |
ee19cdb07528
stepping towards uniform lattice theory development in HOL
haftmann
parents:
22262
diff
changeset
|
6447 |
|
ee19cdb07528
stepping towards uniform lattice theory development in HOL
haftmann
parents:
22262
diff
changeset
|
6448 |
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
|
6449 |
proof (rule mono_inf [where f=listsp, THEN order_antisym]) |
22262 | 6450 |
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
|
6451 |
show "inf (listsp A) (listsp B) \<le> listsp (inf A B)" by (blast intro!: listsp_infI) |
14388 | 6452 |
qed |
6453 |
||
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
|
6454 |
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
|
6455 |
|
46176
1898e61e89c4
pred_subset/equals_eq are now standard pred_set_conv rules
berghofe
parents:
46156
diff
changeset
|
6456 |
lemmas lists_Int_eq [simp] = listsp_inf_eq [to_set] |
22262 | 6457 |
|
67613 | 6458 |
lemma Cons_in_lists_iff[simp]: "x#xs \<in> lists A \<longleftrightarrow> x \<in> A \<and> xs \<in> lists A" |
39613 | 6459 |
by auto |
6460 |
||
22262 | 6461 |
lemma append_in_listsp_conv [iff]: |
6462 |
"(listsp A (xs @ ys)) = (listsp A xs \<and> listsp A ys)" |
|
15302 | 6463 |
by (induct xs) auto |
6464 |
||
22262 | 6465 |
lemmas append_in_lists_conv [iff] = append_in_listsp_conv [to_set] |
6466 |
||
6467 |
lemma in_listsp_conv_set: "(listsp A xs) = (\<forall>x \<in> set xs. A x)" |
|
61799 | 6468 |
\<comment> \<open>eliminate \<open>listsp\<close> in favour of \<open>set\<close>\<close> |
15302 | 6469 |
by (induct xs) auto |
6470 |
||
46313 | 6471 |
lemmas in_lists_conv_set [code_unfold] = in_listsp_conv_set [to_set] |
22262 | 6472 |
|
71585 | 6473 |
lemma in_listspD [dest!]: "listsp A xs \<Longrightarrow> \<forall>x\<in>set xs. A x" |
22262 | 6474 |
by (rule in_listsp_conv_set [THEN iffD1]) |
6475 |
||
54147
97a8ff4e4ac9
killed most "no_atp", to make Sledgehammer more complete
blanchet
parents:
53954
diff
changeset
|
6476 |
lemmas in_listsD [dest!] = in_listspD [to_set] |
97a8ff4e4ac9
killed most "no_atp", to make Sledgehammer more complete
blanchet
parents:
53954
diff
changeset
|
6477 |
|
71585 | 6478 |
lemma in_listspI [intro!]: "\<forall>x\<in>set xs. A x \<Longrightarrow> listsp A xs" |
22262 | 6479 |
by (rule in_listsp_conv_set [THEN iffD2]) |
6480 |
||
54147
97a8ff4e4ac9
killed most "no_atp", to make Sledgehammer more complete
blanchet
parents:
53954
diff
changeset
|
6481 |
lemmas in_listsI [intro!] = in_listspI [to_set] |
15302 | 6482 |
|
68709 | 6483 |
lemma lists_eq_set: "lists A = {xs. set xs \<le> A}" |
39597 | 6484 |
by auto |
6485 |
||
39613 | 6486 |
lemma lists_empty [simp]: "lists {} = {[]}" |
6487 |
by auto |
|
6488 |
||
15302 | 6489 |
lemma lists_UNIV [simp]: "lists UNIV = UNIV" |
6490 |
by auto |
|
6491 |
||
50134 | 6492 |
lemma lists_image: "lists (f`A) = map f ` lists A" |
6493 |
proof - |
|
6494 |
{ fix xs have "\<forall>x\<in>set xs. x \<in> f ` A \<Longrightarrow> xs \<in> map f ` lists A" |
|
55465 | 6495 |
by (induct xs) (auto simp del: list.map simp add: list.map[symmetric] intro!: imageI) } |
50134 | 6496 |
then show ?thesis by auto |
6497 |
qed |
|
17086 | 6498 |
|
60758 | 6499 |
subsubsection \<open>Inductive definition for membership\<close> |
17086 | 6500 |
|
23740 | 6501 |
inductive ListMem :: "'a \<Rightarrow> 'a list \<Rightarrow> bool" |
22262 | 6502 |
where |
6503 |
elem: "ListMem x (x # xs)" |
|
6504 |
| insert: "ListMem x xs \<Longrightarrow> ListMem x (y # xs)" |
|
6505 |
||
6506 |
lemma ListMem_iff: "(ListMem x xs) = (x \<in> set xs)" |
|
68719 | 6507 |
proof |
6508 |
show "ListMem x xs \<Longrightarrow> x \<in> set xs" |
|
6509 |
by (induct set: ListMem) auto |
|
6510 |
show "x \<in> set xs \<Longrightarrow> ListMem x xs" |
|
6511 |
by (induct xs) (auto intro: ListMem.intros) |
|
6512 |
qed |
|
17086 | 6513 |
|
6514 |
||
60758 | 6515 |
subsubsection \<open>Lists as Cartesian products\<close> |
6516 |
||
61799 | 6517 |
text\<open>\<open>set_Cons A Xs\<close>: the set of lists with head drawn from |
69593 | 6518 |
\<^term>\<open>A\<close> and tail drawn from \<^term>\<open>Xs\<close>.\<close> |
15302 | 6519 |
|
50548 | 6520 |
definition set_Cons :: "'a set \<Rightarrow> 'a list set \<Rightarrow> 'a list set" where |
6521 |
"set_Cons A XS = {z. \<exists>x xs. z = x # xs \<and> x \<in> A \<and> xs \<in> XS}" |
|
15302 | 6522 |
|
17724 | 6523 |
lemma set_Cons_sing_Nil [simp]: "set_Cons A {[]} = (%x. [x])`A" |
15302 | 6524 |
by (auto simp add: set_Cons_def) |
6525 |
||
60758 | 6526 |
text\<open>Yields the set of lists, all of the same length as the argument and |
6527 |
with elements drawn from the corresponding element of the argument.\<close> |
|
15302 | 6528 |
|
50548 | 6529 |
primrec listset :: "'a set list \<Rightarrow> 'a list set" where |
6530 |
"listset [] = {[]}" | |
|
6531 |
"listset (A # As) = set_Cons A (listset As)" |
|
15302 | 6532 |
|
6533 |
||
60758 | 6534 |
subsection \<open>Relations on Lists\<close> |
6535 |
||
6536 |
subsubsection \<open>Length Lexicographic Ordering\<close> |
|
6537 |
||
64963 | 6538 |
text\<open>These orderings preserve well-foundedness: shorter lists |
60758 | 6539 |
precede longer lists. These ordering are not used in dictionaries.\<close> |
64963 | 6540 |
|
61799 | 6541 |
primrec \<comment> \<open>The lexicographic ordering for lists of the specified length\<close> |
34941 | 6542 |
lexn :: "('a \<times> 'a) set \<Rightarrow> nat \<Rightarrow> ('a list \<times> 'a list) set" where |
50548 | 6543 |
"lexn r 0 = {}" | |
6544 |
"lexn r (Suc n) = |
|
55932 | 6545 |
(map_prod (%(x, xs). x#xs) (%(x, xs). x#xs) ` (r <*lex*> lexn r n)) Int |
50548 | 6546 |
{(xs, ys). length xs = Suc n \<and> length ys = Suc n}" |
6547 |
||
6548 |
definition lex :: "('a \<times> 'a) set \<Rightarrow> ('a list \<times> 'a list) set" where |
|
61799 | 6549 |
"lex r = (\<Union>n. lexn r n)" \<comment> \<open>Holds only between lists of the same length\<close> |
50548 | 6550 |
|
6551 |
definition lenlex :: "('a \<times> 'a) set => ('a list \<times> 'a list) set" where |
|
6552 |
"lenlex r = inv_image (less_than <*lex*> lex r) (\<lambda>xs. (length xs, xs))" |
|
61799 | 6553 |
\<comment> \<open>Compares lists by their length and then lexicographically\<close> |
15302 | 6554 |
|
68719 | 6555 |
lemma wf_lexn: assumes "wf r" shows "wf (lexn r n)" |
6556 |
proof (induct n) |
|
6557 |
case (Suc n) |
|
6558 |
have inj: "inj (\<lambda>(x, xs). x # xs)" |
|
6559 |
using assms by (auto simp: inj_on_def) |
|
6560 |
have wf: "wf (map_prod (\<lambda>(x, xs). x # xs) (\<lambda>(x, xs). x # xs) ` (r <*lex*> lexn r n))" |
|
6561 |
by (simp add: Suc.hyps assms wf_lex_prod wf_map_prod_image [OF _ inj]) |
|
6562 |
then show ?case |
|
6563 |
by (rule wf_subset) auto |
|
6564 |
qed auto |
|
15302 | 6565 |
|
6566 |
lemma lexn_length: |
|
67613 | 6567 |
"(xs, ys) \<in> lexn r n \<Longrightarrow> length xs = n \<and> length ys = n" |
71585 | 6568 |
by (induct n arbitrary: xs ys) auto |
6569 |
||
74424 | 6570 |
lemma wf_lex [intro!]: |
71585 | 6571 |
assumes "wf r" shows "wf (lex r)" |
68719 | 6572 |
unfolding lex_def |
71585 | 6573 |
proof (rule wf_UN) |
6574 |
show "wf (lexn r i)" for i |
|
6575 |
by (simp add: assms wf_lexn) |
|
6576 |
show "\<And>i j. lexn r i \<noteq> lexn r j \<Longrightarrow> Domain (lexn r i) \<inter> Range (lexn r j) = {}" |
|
6577 |
by (metis DomainE Int_emptyI RangeE lexn_length) |
|
6578 |
qed |
|
6579 |
||
15302 | 6580 |
lemma lexn_conv: |
15656 | 6581 |
"lexn r n = |
6582 |
{(xs,ys). length xs = n \<and> length ys = n \<and> |
|
72184 | 6583 |
(\<exists>xys x y xs' ys'. xs= xys @ x#xs' \<and> ys= xys @ y # ys' \<and> (x, y) \<in> r)}" |
71585 | 6584 |
proof (induction n) |
6585 |
case (Suc n) |
|
72184 | 6586 |
then show ?case |
6587 |
apply (simp add: image_Collect lex_prod_def, safe, blast) |
|
71585 | 6588 |
apply (rule_tac x = "ab # xys" in exI, simp) |
6589 |
apply (case_tac xys; force) |
|
6590 |
done |
|
6591 |
qed auto |
|
15302 | 6592 |
|
62175 | 6593 |
text\<open>By Mathias Fleury:\<close> |
71435
d8fb621fea02
some lemmas about the lex ordering on lists, etc.
paulson <lp15@cam.ac.uk>
parents:
71423
diff
changeset
|
6594 |
proposition lexn_transI: |
72184 | 6595 |
assumes "trans r" shows "trans (lexn r n)" |
62090 | 6596 |
unfolding trans_def |
6597 |
proof (intro allI impI) |
|
6598 |
fix as bs cs |
|
6599 |
assume asbs: "(as, bs) \<in> lexn r n" and bscs: "(bs, cs) \<in> lexn r n" |
|
6600 |
obtain abs a b as' bs' where |
|
6601 |
n: "length as = n" and "length bs = n" and |
|
6602 |
as: "as = abs @ a # as'" and |
|
6603 |
bs: "bs = abs @ b # bs'" and |
|
72184 | 6604 |
abr: "(a, b) \<in> r" |
62090 | 6605 |
using asbs unfolding lexn_conv by blast |
6606 |
obtain bcs b' c' cs' bs' where |
|
6607 |
n': "length cs = n" and "length bs = n" and |
|
6608 |
bs': "bs = bcs @ b' # bs'" and |
|
6609 |
cs: "cs = bcs @ c' # cs'" and |
|
72184 | 6610 |
b'c'r: "(b', c') \<in> r" |
62090 | 6611 |
using bscs unfolding lexn_conv by blast |
6612 |
consider (le) "length bcs < length abs" |
|
6613 |
| (eq) "length bcs = length abs" |
|
6614 |
| (ge) "length bcs > length abs" by linarith |
|
6615 |
thus "(as, cs) \<in> lexn r n" |
|
6616 |
proof cases |
|
6617 |
let ?k = "length bcs" |
|
6618 |
case le |
|
6619 |
hence "as ! ?k = bs ! ?k" unfolding as bs by (simp add: nth_append) |
|
72184 | 6620 |
hence "(as ! ?k, cs ! ?k) \<in> r" using b'c'r unfolding bs' cs by auto |
62090 | 6621 |
moreover |
6622 |
have "length bcs < length as" using le unfolding as by simp |
|
6623 |
from id_take_nth_drop[OF this] |
|
6624 |
have "as = take ?k as @ as ! ?k # drop (Suc ?k) as" . |
|
6625 |
moreover |
|
6626 |
have "length bcs < length cs" unfolding cs by simp |
|
6627 |
from id_take_nth_drop[OF this] |
|
6628 |
have "cs = take ?k cs @ cs ! ?k # drop (Suc ?k) cs" . |
|
6629 |
moreover have "take ?k as = take ?k cs" |
|
6630 |
using le arg_cong[OF bs, of "take (length bcs)"] |
|
6631 |
unfolding cs as bs' by auto |
|
72184 | 6632 |
ultimately show ?thesis using n n' unfolding lexn_conv by auto |
62090 | 6633 |
next |
6634 |
let ?k = "length abs" |
|
6635 |
case ge |
|
6636 |
hence "bs ! ?k = cs ! ?k" unfolding bs' cs by (simp add: nth_append) |
|
72184 | 6637 |
hence "(as ! ?k, cs ! ?k) \<in> r" using abr unfolding as bs by auto |
62090 | 6638 |
moreover |
6639 |
have "length abs < length as" using ge unfolding as by simp |
|
6640 |
from id_take_nth_drop[OF this] |
|
6641 |
have "as = take ?k as @ as ! ?k # drop (Suc ?k) as" . |
|
6642 |
moreover have "length abs < length cs" using n n' unfolding as by simp |
|
6643 |
from id_take_nth_drop[OF this] |
|
6644 |
have "cs = take ?k cs @ cs ! ?k # drop (Suc ?k) cs" . |
|
6645 |
moreover have "take ?k as = take ?k cs" |
|
6646 |
using ge arg_cong[OF bs', of "take (length abs)"] |
|
6647 |
unfolding cs as bs by auto |
|
6648 |
ultimately show ?thesis using n n' unfolding lexn_conv by auto |
|
6649 |
next |
|
6650 |
let ?k = "length abs" |
|
6651 |
case eq |
|
63540 | 6652 |
hence *: "abs = bcs" "b = b'" using bs bs' by auto |
72184 | 6653 |
hence "(a, c') \<in> r" |
6654 |
using abr b'c'r assms unfolding trans_def by blast |
|
6655 |
with * show ?thesis using n n' unfolding lexn_conv as bs cs by auto |
|
62090 | 6656 |
qed |
6657 |
qed |
|
6658 |
||
71435
d8fb621fea02
some lemmas about the lex ordering on lists, etc.
paulson <lp15@cam.ac.uk>
parents:
71423
diff
changeset
|
6659 |
corollary lex_transI: |
72184 | 6660 |
assumes "trans r" shows "trans (lex r)" |
71435
d8fb621fea02
some lemmas about the lex ordering on lists, etc.
paulson <lp15@cam.ac.uk>
parents:
71423
diff
changeset
|
6661 |
using lexn_transI [OF assms] |
d8fb621fea02
some lemmas about the lex ordering on lists, etc.
paulson <lp15@cam.ac.uk>
parents:
71423
diff
changeset
|
6662 |
by (clarsimp simp add: lex_def trans_def) (metis lexn_length) |
d8fb621fea02
some lemmas about the lex ordering on lists, etc.
paulson <lp15@cam.ac.uk>
parents:
71423
diff
changeset
|
6663 |
|
15302 | 6664 |
lemma lex_conv: |
15656 | 6665 |
"lex r = |
6666 |
{(xs,ys). length xs = length ys \<and> |
|
72184 | 6667 |
(\<exists>xys x y xs' ys'. xs = xys @ x # xs' \<and> ys = xys @ y # ys' \<and> (x, y) \<in> r)}" |
15302 | 6668 |
by (force simp add: lex_def lexn_conv) |
6669 |
||
71585 | 6670 |
lemma wf_lenlex [intro!]: "wf r \<Longrightarrow> wf (lenlex r)" |
15693 | 6671 |
by (unfold lenlex_def) blast |
6672 |
||
6673 |
lemma lenlex_conv: |
|
67091 | 6674 |
"lenlex r = {(xs,ys). length xs < length ys \<or> |
6675 |
length xs = length ys \<and> (xs, ys) \<in> lex r}" |
|
72164
b7c54ff7f2dd
S Holub's proposed generalisation of the lexicographic product of two orderings
paulson <lp15@cam.ac.uk>
parents:
72095
diff
changeset
|
6676 |
by (auto simp add: lenlex_def Id_on_def lex_prod_def inv_image_def) |
15302 | 6677 |
|
71404
f2b783abfbe7
A few lemmas connected with orderings
paulson <lp15@cam.ac.uk>
parents:
71393
diff
changeset
|
6678 |
lemma total_lenlex: |
f2b783abfbe7
A few lemmas connected with orderings
paulson <lp15@cam.ac.uk>
parents:
71393
diff
changeset
|
6679 |
assumes "total r" |
f2b783abfbe7
A few lemmas connected with orderings
paulson <lp15@cam.ac.uk>
parents:
71393
diff
changeset
|
6680 |
shows "total (lenlex r)" |
f2b783abfbe7
A few lemmas connected with orderings
paulson <lp15@cam.ac.uk>
parents:
71393
diff
changeset
|
6681 |
proof - |
f2b783abfbe7
A few lemmas connected with orderings
paulson <lp15@cam.ac.uk>
parents:
71393
diff
changeset
|
6682 |
have "(xs,ys) \<in> lexn r (length xs) \<or> (ys,xs) \<in> lexn r (length xs)" |
f2b783abfbe7
A few lemmas connected with orderings
paulson <lp15@cam.ac.uk>
parents:
71393
diff
changeset
|
6683 |
if "xs \<noteq> ys" and len: "length xs = length ys" for xs ys |
f2b783abfbe7
A few lemmas connected with orderings
paulson <lp15@cam.ac.uk>
parents:
71393
diff
changeset
|
6684 |
proof - |
f2b783abfbe7
A few lemmas connected with orderings
paulson <lp15@cam.ac.uk>
parents:
71393
diff
changeset
|
6685 |
obtain pre x xs' y ys' where "x\<noteq>y" and xs: "xs = pre @ [x] @ xs'" and ys: "ys = pre @ [y] @ys'" |
74424 | 6686 |
by (meson len \<open>xs \<noteq> ys\<close> same_length_different) |
71404
f2b783abfbe7
A few lemmas connected with orderings
paulson <lp15@cam.ac.uk>
parents:
71393
diff
changeset
|
6687 |
then consider "(x,y) \<in> r" | "(y,x) \<in> r" |
f2b783abfbe7
A few lemmas connected with orderings
paulson <lp15@cam.ac.uk>
parents:
71393
diff
changeset
|
6688 |
by (meson UNIV_I assms total_on_def) |
f2b783abfbe7
A few lemmas connected with orderings
paulson <lp15@cam.ac.uk>
parents:
71393
diff
changeset
|
6689 |
then show ?thesis |
72184 | 6690 |
by cases (use len in \<open>(force simp add: lexn_conv xs ys)+\<close>) |
71404
f2b783abfbe7
A few lemmas connected with orderings
paulson <lp15@cam.ac.uk>
parents:
71393
diff
changeset
|
6691 |
qed |
f2b783abfbe7
A few lemmas connected with orderings
paulson <lp15@cam.ac.uk>
parents:
71393
diff
changeset
|
6692 |
then show ?thesis |
f2b783abfbe7
A few lemmas connected with orderings
paulson <lp15@cam.ac.uk>
parents:
71393
diff
changeset
|
6693 |
by (fastforce simp: lenlex_def total_on_def lex_def) |
f2b783abfbe7
A few lemmas connected with orderings
paulson <lp15@cam.ac.uk>
parents:
71393
diff
changeset
|
6694 |
qed |
f2b783abfbe7
A few lemmas connected with orderings
paulson <lp15@cam.ac.uk>
parents:
71393
diff
changeset
|
6695 |
|
72184 | 6696 |
lemma lenlex_transI [intro]: "trans r \<Longrightarrow> trans (lenlex r)" |
71435
d8fb621fea02
some lemmas about the lex ordering on lists, etc.
paulson <lp15@cam.ac.uk>
parents:
71423
diff
changeset
|
6697 |
unfolding lenlex_def |
72184 | 6698 |
by (meson lex_transI trans_inv_image trans_less_than trans_lex_prod) |
71435
d8fb621fea02
some lemmas about the lex ordering on lists, etc.
paulson <lp15@cam.ac.uk>
parents:
71423
diff
changeset
|
6699 |
|
15302 | 6700 |
lemma Nil_notin_lex [iff]: "([], ys) \<notin> lex r" |
71585 | 6701 |
by (simp add: lex_conv) |
15302 | 6702 |
|
6703 |
lemma Nil2_notin_lex [iff]: "(xs, []) \<notin> lex r" |
|
71585 | 6704 |
by (simp add:lex_conv) |
15302 | 6705 |
|
18447 | 6706 |
lemma Cons_in_lex [simp]: |
72184 | 6707 |
"(x # xs, y # ys) \<in> lex r \<longleftrightarrow> (x, y) \<in> r \<and> length xs = length ys \<or> x = y \<and> (xs, ys) \<in> lex r" |
6708 |
(is "?lhs = ?rhs") |
|
71585 | 6709 |
proof |
6710 |
assume ?lhs then show ?rhs |
|
6711 |
by (simp add: lex_conv) (metis hd_append list.sel(1) list.sel(3) tl_append2) |
|
6712 |
next |
|
6713 |
assume ?rhs then show ?lhs |
|
6714 |
by (simp add: lex_conv) (blast intro: Cons_eq_appendI) |
|
6715 |
qed |
|
66502 | 6716 |
|
74424 | 6717 |
lemma Nil_lenlex_iff1 [simp]: "([], ns) \<in> lenlex r \<longleftrightarrow> ns \<noteq> []" |
71435
d8fb621fea02
some lemmas about the lex ordering on lists, etc.
paulson <lp15@cam.ac.uk>
parents:
71423
diff
changeset
|
6718 |
and Nil_lenlex_iff2 [simp]: "(ns,[]) \<notin> lenlex r" |
d8fb621fea02
some lemmas about the lex ordering on lists, etc.
paulson <lp15@cam.ac.uk>
parents:
71423
diff
changeset
|
6719 |
by (auto simp: lenlex_def) |
d8fb621fea02
some lemmas about the lex ordering on lists, etc.
paulson <lp15@cam.ac.uk>
parents:
71423
diff
changeset
|
6720 |
|
74424 | 6721 |
lemma Cons_lenlex_iff: |
6722 |
"((m # ms, n # ns) \<in> lenlex r) \<longleftrightarrow> |
|
6723 |
length ms < length ns |
|
6724 |
\<or> length ms = length ns \<and> (m,n) \<in> r |
|
71435
d8fb621fea02
some lemmas about the lex ordering on lists, etc.
paulson <lp15@cam.ac.uk>
parents:
71423
diff
changeset
|
6725 |
\<or> (m = n \<and> (ms,ns) \<in> lenlex r)" |
d8fb621fea02
some lemmas about the lex ordering on lists, etc.
paulson <lp15@cam.ac.uk>
parents:
71423
diff
changeset
|
6726 |
by (auto simp: lenlex_def) |
d8fb621fea02
some lemmas about the lex ordering on lists, etc.
paulson <lp15@cam.ac.uk>
parents:
71423
diff
changeset
|
6727 |
|
71766
1249b998e377
New theory Library/List_Lenlexorder.thy, a type class instantiation for well-ordering lists
paulson <lp15@cam.ac.uk>
parents:
71593
diff
changeset
|
6728 |
lemma lenlex_irreflexive: "(\<And>x. (x,x) \<notin> r) \<Longrightarrow> (xs,xs) \<notin> lenlex r" |
1249b998e377
New theory Library/List_Lenlexorder.thy, a type class instantiation for well-ordering lists
paulson <lp15@cam.ac.uk>
parents:
71593
diff
changeset
|
6729 |
by (induction xs) (auto simp add: Cons_lenlex_iff) |
1249b998e377
New theory Library/List_Lenlexorder.thy, a type class instantiation for well-ordering lists
paulson <lp15@cam.ac.uk>
parents:
71593
diff
changeset
|
6730 |
|
1249b998e377
New theory Library/List_Lenlexorder.thy, a type class instantiation for well-ordering lists
paulson <lp15@cam.ac.uk>
parents:
71593
diff
changeset
|
6731 |
lemma lenlex_trans: |
72184 | 6732 |
"\<lbrakk>(x,y) \<in> lenlex r; (y,z) \<in> lenlex r; trans r\<rbrakk> \<Longrightarrow> (x,z) \<in> lenlex r" |
71766
1249b998e377
New theory Library/List_Lenlexorder.thy, a type class instantiation for well-ordering lists
paulson <lp15@cam.ac.uk>
parents:
71593
diff
changeset
|
6733 |
by (meson lenlex_transI transD) |
1249b998e377
New theory Library/List_Lenlexorder.thy, a type class instantiation for well-ordering lists
paulson <lp15@cam.ac.uk>
parents:
71593
diff
changeset
|
6734 |
|
71435
d8fb621fea02
some lemmas about the lex ordering on lists, etc.
paulson <lp15@cam.ac.uk>
parents:
71423
diff
changeset
|
6735 |
lemma lenlex_length: "(ms, ns) \<in> lenlex r \<Longrightarrow> length ms \<le> length ns" |
d8fb621fea02
some lemmas about the lex ordering on lists, etc.
paulson <lp15@cam.ac.uk>
parents:
71423
diff
changeset
|
6736 |
by (auto simp: lenlex_def) |
d8fb621fea02
some lemmas about the lex ordering on lists, etc.
paulson <lp15@cam.ac.uk>
parents:
71423
diff
changeset
|
6737 |
|
66502 | 6738 |
lemma lex_append_rightI: |
6739 |
"(xs, ys) \<in> lex r \<Longrightarrow> length vs = length us \<Longrightarrow> (xs @ us, ys @ vs) \<in> lex r" |
|
71585 | 6740 |
by (fastforce simp: lex_def lexn_conv) |
66502 | 6741 |
|
6742 |
lemma lex_append_leftI: |
|
6743 |
"(ys, zs) \<in> lex r \<Longrightarrow> (xs @ ys, xs @ zs) \<in> lex r" |
|
71585 | 6744 |
by (induct xs) auto |
66502 | 6745 |
|
6746 |
lemma lex_append_leftD: |
|
6747 |
"\<forall>x. (x,x) \<notin> r \<Longrightarrow> (xs @ ys, xs @ zs) \<in> lex r \<Longrightarrow> (ys, zs) \<in> lex r" |
|
71585 | 6748 |
by (induct xs) auto |
66502 | 6749 |
|
6750 |
lemma lex_append_left_iff: |
|
6751 |
"\<forall>x. (x,x) \<notin> r \<Longrightarrow> (xs @ ys, xs @ zs) \<in> lex r \<longleftrightarrow> (ys, zs) \<in> lex r" |
|
71585 | 6752 |
by(metis lex_append_leftD lex_append_leftI) |
66502 | 6753 |
|
6754 |
lemma lex_take_index: |
|
6755 |
assumes "(xs, ys) \<in> lex r" |
|
71585 | 6756 |
obtains i where "i < length xs" and "i < length ys" and "take i xs = take i ys" |
66502 | 6757 |
and "(xs ! i, ys ! i) \<in> r" |
6758 |
proof - |
|
6759 |
obtain n us x xs' y ys' where "(xs, ys) \<in> lexn r n" and "length xs = n" and "length ys = n" |
|
6760 |
and "xs = us @ x # xs'" and "ys = us @ y # ys'" and "(x, y) \<in> r" |
|
6761 |
using assms by (fastforce simp: lex_def lexn_conv) |
|
6762 |
then show ?thesis by (intro that [of "length us"]) auto |
|
6763 |
qed |
|
15302 | 6764 |
|
71848
3c7852327787
A few new theorems, plus some tidying up
paulson <lp15@cam.ac.uk>
parents:
71827
diff
changeset
|
6765 |
lemma irrefl_lex: "irrefl r \<Longrightarrow> irrefl (lex r)" |
3c7852327787
A few new theorems, plus some tidying up
paulson <lp15@cam.ac.uk>
parents:
71827
diff
changeset
|
6766 |
by (meson irrefl_def lex_take_index) |
3c7852327787
A few new theorems, plus some tidying up
paulson <lp15@cam.ac.uk>
parents:
71827
diff
changeset
|
6767 |
|
72095
cfb6c22a5636
lemmas about sets and the enumerate operator
paulson <lp15@cam.ac.uk>
parents:
71991
diff
changeset
|
6768 |
lemma lexl_not_refl [simp]: "irrefl r \<Longrightarrow> (x,x) \<notin> lex r" |
cfb6c22a5636
lemmas about sets and the enumerate operator
paulson <lp15@cam.ac.uk>
parents:
71991
diff
changeset
|
6769 |
by (meson irrefl_def lex_take_index) |
cfb6c22a5636
lemmas about sets and the enumerate operator
paulson <lp15@cam.ac.uk>
parents:
71991
diff
changeset
|
6770 |
|
71848
3c7852327787
A few new theorems, plus some tidying up
paulson <lp15@cam.ac.uk>
parents:
71827
diff
changeset
|
6771 |
|
60758 | 6772 |
subsubsection \<open>Lexicographic Ordering\<close> |
6773 |
||
6774 |
text \<open>Classical lexicographic ordering on lists, ie. "a" < "ab" < "b". |
|
15656 | 6775 |
This ordering does \emph{not} preserve well-foundedness. |
64963 | 6776 |
Author: N. Voelker, March 2005.\<close> |
15656 | 6777 |
|
50548 | 6778 |
definition lexord :: "('a \<times> 'a) set \<Rightarrow> ('a list \<times> 'a list) set" where |
6779 |
"lexord r = {(x,y). \<exists> a v. y = x @ a # v \<or> |
|
72184 | 6780 |
(\<exists> u a b v w. (a,b) \<in> r \<and> x = u @ (a # v) \<and> y = u @ (b # w))}" |
15656 | 6781 |
|
6782 |
lemma lexord_Nil_left[simp]: "([],y) \<in> lexord r = (\<exists> a x. y = a # x)" |
|
72184 | 6783 |
by (unfold lexord_def, induct_tac y, auto) |
15656 | 6784 |
|
6785 |
lemma lexord_Nil_right[simp]: "(x,[]) \<notin> lexord r" |
|
72184 | 6786 |
by (unfold lexord_def, induct_tac x, auto) |
15656 | 6787 |
|
6788 |
lemma lexord_cons_cons[simp]: |
|
72184 | 6789 |
"(a # x, b # y) \<in> lexord r \<longleftrightarrow> (a,b)\<in> r \<or> (a = b \<and> (x,y)\<in> lexord r)" (is "?lhs = ?rhs") |
71585 | 6790 |
proof |
6791 |
assume ?lhs |
|
6792 |
then show ?rhs |
|
6793 |
apply (simp add: lexord_def) |
|
68719 | 6794 |
apply (metis hd_append list.sel(1) list.sel(3) tl_append2) |
71585 | 6795 |
done |
72184 | 6796 |
qed (auto simp add: lexord_def; (blast | meson Cons_eq_appendI)) |
15656 | 6797 |
|
6798 |
lemmas lexord_simps = lexord_Nil_left lexord_Nil_right lexord_cons_cons |
|
6799 |
||
73018 | 6800 |
lemma lexord_same_pref_iff: |
6801 |
"(xs @ ys, xs @ zs) \<in> lexord r \<longleftrightarrow> (\<exists>x \<in> set xs. (x,x) \<in> r) \<or> (ys, zs) \<in> lexord r" |
|
6802 |
by(induction xs) auto |
|
6803 |
||
6804 |
lemma lexord_same_pref_if_irrefl[simp]: |
|
6805 |
"irrefl r \<Longrightarrow> (xs @ ys, xs @ zs) \<in> lexord r \<longleftrightarrow> (ys, zs) \<in> lexord r" |
|
6806 |
by (simp add: irrefl_def lexord_same_pref_iff) |
|
6807 |
||
15656 | 6808 |
lemma lexord_append_rightI: "\<exists> b z. y = b # z \<Longrightarrow> (x, x @ y) \<in> lexord r" |
73018 | 6809 |
by (metis append_Nil2 lexord_Nil_left lexord_same_pref_iff) |
15656 | 6810 |
|
6811 |
lemma lexord_append_left_rightI: |
|
72184 | 6812 |
"(a,b) \<in> r \<Longrightarrow> (u @ a # x, u @ b # y) \<in> lexord r" |
73018 | 6813 |
by (simp add: lexord_same_pref_iff) |
6814 |
||
6815 |
lemma lexord_append_leftI: "(u,v) \<in> lexord r \<Longrightarrow> (x @ u, x @ v) \<in> lexord r" |
|
6816 |
by (simp add: lexord_same_pref_iff) |
|
15656 | 6817 |
|
6818 |
lemma lexord_append_leftD: |
|
71585 | 6819 |
"\<lbrakk>(x @ u, x @ v) \<in> lexord r; (\<forall>a. (a,a) \<notin> r) \<rbrakk> \<Longrightarrow> (u,v) \<in> lexord r" |
73018 | 6820 |
by (simp add: lexord_same_pref_iff) |
15656 | 6821 |
|
64963 | 6822 |
lemma lexord_take_index_conv: |
67091 | 6823 |
"((x,y) \<in> lexord r) = |
64963 | 6824 |
((length x < length y \<and> take (length x) y = x) \<or> |
72184 | 6825 |
(\<exists>i. i < min(length x)(length y) \<and> take i x = take i y \<and> (x!i,y!i) \<in> r))" |
71585 | 6826 |
proof - |
6827 |
have "(\<exists>a v. y = x @ a # v) = (length x < length y \<and> take (length x) y = x)" |
|
6828 |
by (metis Cons_nth_drop_Suc append_eq_conv_conj drop_all list.simps(3) not_le) |
|
6829 |
moreover |
|
72184 | 6830 |
have "(\<exists>u a b. (a, b) \<in> r \<and> (\<exists>v. x = u @ a # v) \<and> (\<exists>w. y = u @ b # w)) = |
6831 |
(\<exists>i<length x. i < length y \<and> take i x = take i y \<and> (x ! i, y ! i) \<in> r)" |
|
71585 | 6832 |
apply safe |
6833 |
using less_iff_Suc_add apply auto[1] |
|
6834 |
by (metis id_take_nth_drop) |
|
6835 |
ultimately show ?thesis |
|
6836 |
by (auto simp: lexord_def Let_def) |
|
6837 |
qed |
|
64963 | 6838 |
|
6839 |
\<comment> \<open>lexord is extension of partial ordering List.lex\<close> |
|
41986 | 6840 |
lemma lexord_lex: "(x,y) \<in> lex r = ((x,y) \<in> lexord r \<and> length x = length y)" |
68719 | 6841 |
proof (induction x arbitrary: y) |
6842 |
case (Cons a x y) then show ?case |
|
6843 |
by (cases y) (force+) |
|
6844 |
qed auto |
|
15656 | 6845 |
|
73018 | 6846 |
lemma lexord_sufI: |
6847 |
assumes "(u,w) \<in> lexord r" "length w \<le> length u" |
|
6848 |
shows "(u@v,w@z) \<in> lexord r" |
|
6849 |
proof- |
|
6850 |
from leD[OF assms(2)] assms(1)[unfolded lexord_take_index_conv[of u w r] min_absorb2[OF assms(2)]] |
|
6851 |
obtain i where "take i u = take i w" and "(u!i,w!i) \<in> r" and "i < length w" |
|
74424 | 6852 |
by blast |
73018 | 6853 |
hence "((u@v)!i, (w@z)!i) \<in> r" |
6854 |
unfolding nth_append using less_le_trans[OF \<open>i < length w\<close> assms(2)] \<open>(u!i,w!i) \<in> r\<close> |
|
6855 |
by presburger |
|
6856 |
moreover have "i < min (length (u@v)) (length (w@z))" |
|
6857 |
using assms(2) \<open>i < length w\<close> by simp |
|
6858 |
moreover have "take i (u@v) = take i (w@z)" |
|
74424 | 6859 |
using assms(2) \<open>i < length w\<close> \<open>take i u = take i w\<close> by simp |
73018 | 6860 |
ultimately show ?thesis |
6861 |
using lexord_take_index_conv by blast |
|
6862 |
qed |
|
6863 |
||
74424 | 6864 |
lemma lexord_sufE: |
73018 | 6865 |
assumes "(xs@zs,ys@qs) \<in> lexord r" "xs \<noteq> ys" "length xs = length ys" "length zs = length qs" |
6866 |
shows "(xs,ys) \<in> lexord r" |
|
6867 |
proof- |
|
6868 |
obtain i where "i < length (xs@zs)" and "i < length (ys@qs)" and "take i (xs@zs) = take i (ys@qs)" |
|
6869 |
and "((xs@zs) ! i, (ys@qs) ! i) \<in> r" |
|
6870 |
using assms(1) lex_take_index[unfolded lexord_lex,of "xs @ zs" "ys @ qs" r] |
|
6871 |
length_append[of xs zs, unfolded assms(3,4), folded length_append[of ys qs]] |
|
6872 |
by blast |
|
6873 |
have "length (take i xs) = length (take i ys)" |
|
6874 |
by (simp add: assms(3)) |
|
6875 |
have "i < length xs" |
|
6876 |
using assms(2,3) le_less_linear take_all[of xs i] take_all[of ys i] |
|
6877 |
\<open>take i (xs @ zs) = take i (ys @ qs)\<close> append_eq_append_conv take_append |
|
6878 |
by metis |
|
6879 |
hence "(xs ! i, ys ! i) \<in> r" |
|
6880 |
using \<open>((xs @ zs) ! i, (ys @ qs) ! i) \<in> r\<close> assms(3) by (simp add: nth_append) |
|
6881 |
moreover have "take i xs = take i ys" |
|
6882 |
using assms(3) \<open>take i (xs @ zs) = take i (ys @ qs)\<close> by auto |
|
6883 |
ultimately show ?thesis |
|
6884 |
unfolding lexord_take_index_conv using \<open>i < length xs\<close> assms(3) by fastforce |
|
6885 |
qed |
|
6886 |
||
67091 | 6887 |
lemma lexord_irreflexive: "\<forall>x. (x,x) \<notin> r \<Longrightarrow> (xs,xs) \<notin> lexord r" |
72184 | 6888 |
by (induct xs) auto |
41986 | 6889 |
|
60758 | 6890 |
text\<open>By Ren\'e Thiemann:\<close> |
64963 | 6891 |
lemma lexord_partial_trans: |
41986 | 6892 |
"(\<And>x y z. x \<in> set xs \<Longrightarrow> (x,y) \<in> r \<Longrightarrow> (y,z) \<in> r \<Longrightarrow> (x,z) \<in> r) |
6893 |
\<Longrightarrow> (xs,ys) \<in> lexord r \<Longrightarrow> (ys,zs) \<in> lexord r \<Longrightarrow> (xs,zs) \<in> lexord r" |
|
6894 |
proof (induct xs arbitrary: ys zs) |
|
6895 |
case Nil |
|
6896 |
from Nil(3) show ?case unfolding lexord_def by (cases zs, auto) |
|
6897 |
next |
|
6898 |
case (Cons x xs yys zzs) |
|
6899 |
from Cons(3) obtain y ys where yys: "yys = y # ys" unfolding lexord_def |
|
6900 |
by (cases yys, auto) |
|
6901 |
note Cons = Cons[unfolded yys] |
|
72184 | 6902 |
from Cons(3) have one: "(x,y) \<in> r \<or> x = y \<and> (xs,ys) \<in> lexord r" by auto |
41986 | 6903 |
from Cons(4) obtain z zs where zzs: "zzs = z # zs" unfolding lexord_def |
6904 |
by (cases zzs, auto) |
|
6905 |
note Cons = Cons[unfolded zzs] |
|
72184 | 6906 |
from Cons(4) have two: "(y,z) \<in> r \<or> y = z \<and> (ys,zs) \<in> lexord r" by auto |
41986 | 6907 |
{ |
6908 |
assume "(xs,ys) \<in> lexord r" and "(ys,zs) \<in> lexord r" |
|
6909 |
from Cons(1)[OF _ this] Cons(2) |
|
6910 |
have "(xs,zs) \<in> lexord r" by auto |
|
6911 |
} note ind1 = this |
|
6912 |
{ |
|
6913 |
assume "(x,y) \<in> r" and "(y,z) \<in> r" |
|
6914 |
from Cons(2)[OF _ this] have "(x,z) \<in> r" by auto |
|
6915 |
} note ind2 = this |
|
6916 |
from one two ind1 ind2 |
|
72184 | 6917 |
have "(x,z) \<in> r \<or> x = z \<and> (xs,zs) \<in> lexord r" by blast |
6918 |
thus ?case unfolding zzs by auto |
|
41986 | 6919 |
qed |
15656 | 6920 |
|
64963 | 6921 |
lemma lexord_trans: |
72184 | 6922 |
"\<lbrakk> (x, y) \<in> lexord r; (y, z) \<in> lexord r; trans r \<rbrakk> \<Longrightarrow> (x, z) \<in> lexord r" |
6923 |
by(auto simp: trans_def intro:lexord_partial_trans) |
|
6924 |
||
6925 |
lemma lexord_transI: "trans r \<Longrightarrow> trans (lexord r)" |
|
71935
82b00b8f1871
fixed the utterly weird definitions of asym / asymp, and added many asym lemmas
paulson <lp15@cam.ac.uk>
parents:
71860
diff
changeset
|
6926 |
by (meson lexord_trans transI) |
15656 | 6927 |
|
71766
1249b998e377
New theory Library/List_Lenlexorder.thy, a type class instantiation for well-ordering lists
paulson <lp15@cam.ac.uk>
parents:
71593
diff
changeset
|
6928 |
lemma total_lexord: "total r \<Longrightarrow> total (lexord r)" |
1249b998e377
New theory Library/List_Lenlexorder.thy, a type class instantiation for well-ordering lists
paulson <lp15@cam.ac.uk>
parents:
71593
diff
changeset
|
6929 |
unfolding total_on_def |
1249b998e377
New theory Library/List_Lenlexorder.thy, a type class instantiation for well-ordering lists
paulson <lp15@cam.ac.uk>
parents:
71593
diff
changeset
|
6930 |
proof clarsimp |
1249b998e377
New theory Library/List_Lenlexorder.thy, a type class instantiation for well-ordering lists
paulson <lp15@cam.ac.uk>
parents:
71593
diff
changeset
|
6931 |
fix x y |
1249b998e377
New theory Library/List_Lenlexorder.thy, a type class instantiation for well-ordering lists
paulson <lp15@cam.ac.uk>
parents:
71593
diff
changeset
|
6932 |
assume "\<forall>x y. x \<noteq> y \<longrightarrow> (x, y) \<in> r \<or> (y, x) \<in> r" |
1249b998e377
New theory Library/List_Lenlexorder.thy, a type class instantiation for well-ordering lists
paulson <lp15@cam.ac.uk>
parents:
71593
diff
changeset
|
6933 |
and "(x::'a list) \<noteq> y" |
1249b998e377
New theory Library/List_Lenlexorder.thy, a type class instantiation for well-ordering lists
paulson <lp15@cam.ac.uk>
parents:
71593
diff
changeset
|
6934 |
and "(y, x) \<notin> lexord r" |
1249b998e377
New theory Library/List_Lenlexorder.thy, a type class instantiation for well-ordering lists
paulson <lp15@cam.ac.uk>
parents:
71593
diff
changeset
|
6935 |
then |
1249b998e377
New theory Library/List_Lenlexorder.thy, a type class instantiation for well-ordering lists
paulson <lp15@cam.ac.uk>
parents:
71593
diff
changeset
|
6936 |
show "(x, y) \<in> lexord r" |
1249b998e377
New theory Library/List_Lenlexorder.thy, a type class instantiation for well-ordering lists
paulson <lp15@cam.ac.uk>
parents:
71593
diff
changeset
|
6937 |
proof (induction x arbitrary: y) |
1249b998e377
New theory Library/List_Lenlexorder.thy, a type class instantiation for well-ordering lists
paulson <lp15@cam.ac.uk>
parents:
71593
diff
changeset
|
6938 |
case Nil |
1249b998e377
New theory Library/List_Lenlexorder.thy, a type class instantiation for well-ordering lists
paulson <lp15@cam.ac.uk>
parents:
71593
diff
changeset
|
6939 |
then show ?case |
1249b998e377
New theory Library/List_Lenlexorder.thy, a type class instantiation for well-ordering lists
paulson <lp15@cam.ac.uk>
parents:
71593
diff
changeset
|
6940 |
by (metis lexord_Nil_left list.exhaust) |
1249b998e377
New theory Library/List_Lenlexorder.thy, a type class instantiation for well-ordering lists
paulson <lp15@cam.ac.uk>
parents:
71593
diff
changeset
|
6941 |
next |
1249b998e377
New theory Library/List_Lenlexorder.thy, a type class instantiation for well-ordering lists
paulson <lp15@cam.ac.uk>
parents:
71593
diff
changeset
|
6942 |
case (Cons a x y) then show ?case |
72184 | 6943 |
by (cases y) (force+) |
71766
1249b998e377
New theory Library/List_Lenlexorder.thy, a type class instantiation for well-ordering lists
paulson <lp15@cam.ac.uk>
parents:
71593
diff
changeset
|
6944 |
qed |
1249b998e377
New theory Library/List_Lenlexorder.thy, a type class instantiation for well-ordering lists
paulson <lp15@cam.ac.uk>
parents:
71593
diff
changeset
|
6945 |
qed |
1249b998e377
New theory Library/List_Lenlexorder.thy, a type class instantiation for well-ordering lists
paulson <lp15@cam.ac.uk>
parents:
71593
diff
changeset
|
6946 |
|
1249b998e377
New theory Library/List_Lenlexorder.thy, a type class instantiation for well-ordering lists
paulson <lp15@cam.ac.uk>
parents:
71593
diff
changeset
|
6947 |
corollary 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" |
74424 | 6948 |
using total_lexord by (metis UNIV_I total_on_def) |
15656 | 6949 |
|
56545 | 6950 |
lemma lexord_irrefl: |
6951 |
"irrefl R \<Longrightarrow> irrefl (lexord R)" |
|
68085 | 6952 |
by (simp add: irrefl_def lexord_irreflexive) |
64963 | 6953 |
|
56545 | 6954 |
lemma lexord_asym: |
6955 |
assumes "asym R" |
|
6956 |
shows "asym (lexord R)" |
|
74424 | 6957 |
proof |
56545 | 6958 |
fix xs ys |
6959 |
assume "(xs, ys) \<in> lexord R" |
|
6960 |
then show "(ys, xs) \<notin> lexord R" |
|
6961 |
proof (induct xs arbitrary: ys) |
|
6962 |
case Nil |
|
6963 |
then show ?case by simp |
|
6964 |
next |
|
6965 |
case (Cons x xs) |
|
6966 |
then obtain z zs where ys: "ys = z # zs" by (cases ys) auto |
|
76682
e260dabc88e6
added predicates asym_on and asymp_on and redefined asym and asymp to be abbreviations
desharna
parents:
76485
diff
changeset
|
6967 |
with assms Cons show ?case by (auto dest: asymD) |
56545 | 6968 |
qed |
6969 |
qed |
|
64963 | 6970 |
|
56545 | 6971 |
lemma lexord_asymmetric: |
6972 |
assumes "asym R" |
|
6973 |
assumes hyp: "(a, b) \<in> lexord R" |
|
6974 |
shows "(b, a) \<notin> lexord R" |
|
6975 |
proof - |
|
60758 | 6976 |
from \<open>asym R\<close> have "asym (lexord R)" by (rule lexord_asym) |
76682
e260dabc88e6
added predicates asym_on and asymp_on and redefined asym and asymp to be abbreviations
desharna
parents:
76485
diff
changeset
|
6977 |
then show ?thesis by (auto simp: hyp dest: asymD) |
56545 | 6978 |
qed |
6979 |
||
71935
82b00b8f1871
fixed the utterly weird definitions of asym / asymp, and added many asym lemmas
paulson <lp15@cam.ac.uk>
parents:
71860
diff
changeset
|
6980 |
lemma asym_lex: "asym R \<Longrightarrow> asym (lex R)" |
76682
e260dabc88e6
added predicates asym_on and asymp_on and redefined asym and asymp to be abbreviations
desharna
parents:
76485
diff
changeset
|
6981 |
by (meson asymI asymD irrefl_lex lexord_asym lexord_lex) |
71935
82b00b8f1871
fixed the utterly weird definitions of asym / asymp, and added many asym lemmas
paulson <lp15@cam.ac.uk>
parents:
71860
diff
changeset
|
6982 |
|
82b00b8f1871
fixed the utterly weird definitions of asym / asymp, and added many asym lemmas
paulson <lp15@cam.ac.uk>
parents:
71860
diff
changeset
|
6983 |
lemma asym_lenlex: "asym R \<Longrightarrow> asym (lenlex R)" |
82b00b8f1871
fixed the utterly weird definitions of asym / asymp, and added many asym lemmas
paulson <lp15@cam.ac.uk>
parents:
71860
diff
changeset
|
6984 |
by (simp add: lenlex_def asym_inv_image asym_less_than asym_lex asym_lex_prod) |
82b00b8f1871
fixed the utterly weird definitions of asym / asymp, and added many asym lemmas
paulson <lp15@cam.ac.uk>
parents:
71860
diff
changeset
|
6985 |
|
82b00b8f1871
fixed the utterly weird definitions of asym / asymp, and added many asym lemmas
paulson <lp15@cam.ac.uk>
parents:
71860
diff
changeset
|
6986 |
lemma lenlex_append1: |
74424 | 6987 |
assumes len: "(us,xs) \<in> lenlex R" and eq: "length vs = length ys" |
71935
82b00b8f1871
fixed the utterly weird definitions of asym / asymp, and added many asym lemmas
paulson <lp15@cam.ac.uk>
parents:
71860
diff
changeset
|
6988 |
shows "(us @ vs, xs @ ys) \<in> lenlex R" |
82b00b8f1871
fixed the utterly weird definitions of asym / asymp, and added many asym lemmas
paulson <lp15@cam.ac.uk>
parents:
71860
diff
changeset
|
6989 |
using len |
82b00b8f1871
fixed the utterly weird definitions of asym / asymp, and added many asym lemmas
paulson <lp15@cam.ac.uk>
parents:
71860
diff
changeset
|
6990 |
proof (induction us) |
82b00b8f1871
fixed the utterly weird definitions of asym / asymp, and added many asym lemmas
paulson <lp15@cam.ac.uk>
parents:
71860
diff
changeset
|
6991 |
case Nil |
74424 | 6992 |
then show ?case |
71935
82b00b8f1871
fixed the utterly weird definitions of asym / asymp, and added many asym lemmas
paulson <lp15@cam.ac.uk>
parents:
71860
diff
changeset
|
6993 |
by (simp add: lenlex_def eq) |
82b00b8f1871
fixed the utterly weird definitions of asym / asymp, and added many asym lemmas
paulson <lp15@cam.ac.uk>
parents:
71860
diff
changeset
|
6994 |
next |
82b00b8f1871
fixed the utterly weird definitions of asym / asymp, and added many asym lemmas
paulson <lp15@cam.ac.uk>
parents:
71860
diff
changeset
|
6995 |
case (Cons u us) |
82b00b8f1871
fixed the utterly weird definitions of asym / asymp, and added many asym lemmas
paulson <lp15@cam.ac.uk>
parents:
71860
diff
changeset
|
6996 |
with lex_append_rightI show ?case |
82b00b8f1871
fixed the utterly weird definitions of asym / asymp, and added many asym lemmas
paulson <lp15@cam.ac.uk>
parents:
71860
diff
changeset
|
6997 |
by (fastforce simp add: lenlex_def eq) |
82b00b8f1871
fixed the utterly weird definitions of asym / asymp, and added many asym lemmas
paulson <lp15@cam.ac.uk>
parents:
71860
diff
changeset
|
6998 |
qed |
82b00b8f1871
fixed the utterly weird definitions of asym / asymp, and added many asym lemmas
paulson <lp15@cam.ac.uk>
parents:
71860
diff
changeset
|
6999 |
|
82b00b8f1871
fixed the utterly weird definitions of asym / asymp, and added many asym lemmas
paulson <lp15@cam.ac.uk>
parents:
71860
diff
changeset
|
7000 |
lemma lenlex_append2 [simp]: |
82b00b8f1871
fixed the utterly weird definitions of asym / asymp, and added many asym lemmas
paulson <lp15@cam.ac.uk>
parents:
71860
diff
changeset
|
7001 |
assumes "irrefl R" |
82b00b8f1871
fixed the utterly weird definitions of asym / asymp, and added many asym lemmas
paulson <lp15@cam.ac.uk>
parents:
71860
diff
changeset
|
7002 |
shows "(us @ xs, us @ ys) \<in> lenlex R \<longleftrightarrow> (xs, ys) \<in> lenlex R" |
82b00b8f1871
fixed the utterly weird definitions of asym / asymp, and added many asym lemmas
paulson <lp15@cam.ac.uk>
parents:
71860
diff
changeset
|
7003 |
proof (induction us) |
82b00b8f1871
fixed the utterly weird definitions of asym / asymp, and added many asym lemmas
paulson <lp15@cam.ac.uk>
parents:
71860
diff
changeset
|
7004 |
case Nil |
74424 | 7005 |
then show ?case |
71935
82b00b8f1871
fixed the utterly weird definitions of asym / asymp, and added many asym lemmas
paulson <lp15@cam.ac.uk>
parents:
71860
diff
changeset
|
7006 |
by (simp add: lenlex_def) |
82b00b8f1871
fixed the utterly weird definitions of asym / asymp, and added many asym lemmas
paulson <lp15@cam.ac.uk>
parents:
71860
diff
changeset
|
7007 |
next |
82b00b8f1871
fixed the utterly weird definitions of asym / asymp, and added many asym lemmas
paulson <lp15@cam.ac.uk>
parents:
71860
diff
changeset
|
7008 |
case (Cons u us) |
82b00b8f1871
fixed the utterly weird definitions of asym / asymp, and added many asym lemmas
paulson <lp15@cam.ac.uk>
parents:
71860
diff
changeset
|
7009 |
with assms show ?case |
82b00b8f1871
fixed the utterly weird definitions of asym / asymp, and added many asym lemmas
paulson <lp15@cam.ac.uk>
parents:
71860
diff
changeset
|
7010 |
by (auto simp: lenlex_def irrefl_def) |
82b00b8f1871
fixed the utterly weird definitions of asym / asymp, and added many asym lemmas
paulson <lp15@cam.ac.uk>
parents:
71860
diff
changeset
|
7011 |
qed |
56545 | 7012 |
|
60758 | 7013 |
text \<open> |
54593
8c0a27b9c1bd
add predicate version of lexicographic order on lists
Andreas Lochbihler
parents:
54498
diff
changeset
|
7014 |
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
|
7015 |
Author: Andreas Lochbihler |
60758 | 7016 |
\<close> |
54593
8c0a27b9c1bd
add predicate version of lexicographic order on lists
Andreas Lochbihler
parents:
54498
diff
changeset
|
7017 |
|
72170
7fa9605b226c
Another go with lex: now lexordp back in class ord
paulson <lp15@cam.ac.uk>
parents:
72164
diff
changeset
|
7018 |
context ord |
61681
ca53150406c9
option "inductive_defs" controls exposure of def and mono facts;
wenzelm
parents:
61630
diff
changeset
|
7019 |
begin |
ca53150406c9
option "inductive_defs" controls exposure of def and mono facts;
wenzelm
parents:
61630
diff
changeset
|
7020 |
|
ca53150406c9
option "inductive_defs" controls exposure of def and mono facts;
wenzelm
parents:
61630
diff
changeset
|
7021 |
context |
62093 | 7022 |
notes [[inductive_internals]] |
61681
ca53150406c9
option "inductive_defs" controls exposure of def and mono facts;
wenzelm
parents:
61630
diff
changeset
|
7023 |
begin |
54593
8c0a27b9c1bd
add predicate version of lexicographic order on lists
Andreas Lochbihler
parents:
54498
diff
changeset
|
7024 |
|
8c0a27b9c1bd
add predicate version of lexicographic order on lists
Andreas Lochbihler
parents:
54498
diff
changeset
|
7025 |
inductive lexordp :: "'a list \<Rightarrow> 'a list \<Rightarrow> bool" |
8c0a27b9c1bd
add predicate version of lexicographic order on lists
Andreas Lochbihler
parents:
54498
diff
changeset
|
7026 |
where |
8c0a27b9c1bd
add predicate version of lexicographic order on lists
Andreas Lochbihler
parents:
54498
diff
changeset
|
7027 |
Nil: "lexordp [] (y # ys)" |
8c0a27b9c1bd
add predicate version of lexicographic order on lists
Andreas Lochbihler
parents:
54498
diff
changeset
|
7028 |
| Cons: "x < y \<Longrightarrow> lexordp (x # xs) (y # ys)" |
8c0a27b9c1bd
add predicate version of lexicographic order on lists
Andreas Lochbihler
parents:
54498
diff
changeset
|
7029 |
| Cons_eq: |
8c0a27b9c1bd
add predicate version of lexicographic order on lists
Andreas Lochbihler
parents:
54498
diff
changeset
|
7030 |
"\<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
|
7031 |
|
61681
ca53150406c9
option "inductive_defs" controls exposure of def and mono facts;
wenzelm
parents:
61630
diff
changeset
|
7032 |
end |
ca53150406c9
option "inductive_defs" controls exposure of def and mono facts;
wenzelm
parents:
61630
diff
changeset
|
7033 |
|
75599 | 7034 |
lemma lexordp_simps [simp, code]: |
54593
8c0a27b9c1bd
add predicate version of lexicographic order on lists
Andreas Lochbihler
parents:
54498
diff
changeset
|
7035 |
"lexordp [] ys = (ys \<noteq> [])" |
8c0a27b9c1bd
add predicate version of lexicographic order on lists
Andreas Lochbihler
parents:
54498
diff
changeset
|
7036 |
"lexordp xs [] = False" |
8c0a27b9c1bd
add predicate version of lexicographic order on lists
Andreas Lochbihler
parents:
54498
diff
changeset
|
7037 |
"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
|
7038 |
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
|
7039 |
|
8c0a27b9c1bd
add predicate version of lexicographic order on lists
Andreas Lochbihler
parents:
54498
diff
changeset
|
7040 |
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
|
7041 |
Nil: "lexordp_eq [] ys" |
8c0a27b9c1bd
add predicate version of lexicographic order on lists
Andreas Lochbihler
parents:
54498
diff
changeset
|
7042 |
| 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
|
7043 |
| 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
|
7044 |
|
75599 | 7045 |
lemma lexordp_eq_simps [simp, code]: |
54593
8c0a27b9c1bd
add predicate version of lexicographic order on lists
Andreas Lochbihler
parents:
54498
diff
changeset
|
7046 |
"lexordp_eq [] ys = True" |
8c0a27b9c1bd
add predicate version of lexicographic order on lists
Andreas Lochbihler
parents:
54498
diff
changeset
|
7047 |
"lexordp_eq xs [] \<longleftrightarrow> xs = []" |
8c0a27b9c1bd
add predicate version of lexicographic order on lists
Andreas Lochbihler
parents:
54498
diff
changeset
|
7048 |
"lexordp_eq (x # xs) [] = False" |
8c0a27b9c1bd
add predicate version of lexicographic order on lists
Andreas Lochbihler
parents:
54498
diff
changeset
|
7049 |
"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
|
7050 |
by(subst lexordp_eq.simps, fastforce)+ |
8c0a27b9c1bd
add predicate version of lexicographic order on lists
Andreas Lochbihler
parents:
54498
diff
changeset
|
7051 |
|
8c0a27b9c1bd
add predicate version of lexicographic order on lists
Andreas Lochbihler
parents:
54498
diff
changeset
|
7052 |
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
|
7053 |
by(induct xs)(auto simp add: neq_Nil_conv) |
8c0a27b9c1bd
add predicate version of lexicographic order on lists
Andreas Lochbihler
parents:
54498
diff
changeset
|
7054 |
|
8c0a27b9c1bd
add predicate version of lexicographic order on lists
Andreas Lochbihler
parents:
54498
diff
changeset
|
7055 |
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
|
7056 |
by(induct us) auto |
8c0a27b9c1bd
add predicate version of lexicographic order on lists
Andreas Lochbihler
parents:
54498
diff
changeset
|
7057 |
|
8c0a27b9c1bd
add predicate version of lexicographic order on lists
Andreas Lochbihler
parents:
54498
diff
changeset
|
7058 |
lemma lexordp_eq_refl: "lexordp_eq xs xs" |
8c0a27b9c1bd
add predicate version of lexicographic order on lists
Andreas Lochbihler
parents:
54498
diff
changeset
|
7059 |
by(induct xs) simp_all |
8c0a27b9c1bd
add predicate version of lexicographic order on lists
Andreas Lochbihler
parents:
54498
diff
changeset
|
7060 |
|
8c0a27b9c1bd
add predicate version of lexicographic order on lists
Andreas Lochbihler
parents:
54498
diff
changeset
|
7061 |
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
|
7062 |
by(induct xs) auto |
8c0a27b9c1bd
add predicate version of lexicographic order on lists
Andreas Lochbihler
parents:
54498
diff
changeset
|
7063 |
|
8c0a27b9c1bd
add predicate version of lexicographic order on lists
Andreas Lochbihler
parents:
54498
diff
changeset
|
7064 |
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
|
7065 |
by(induct xs) auto |
8c0a27b9c1bd
add predicate version of lexicographic order on lists
Andreas Lochbihler
parents:
54498
diff
changeset
|
7066 |
|
64963 | 7067 |
lemma lexordp_irreflexive: |
54593
8c0a27b9c1bd
add predicate version of lexicographic order on lists
Andreas Lochbihler
parents:
54498
diff
changeset
|
7068 |
assumes irrefl: "\<forall>x. \<not> x < x" |
8c0a27b9c1bd
add predicate version of lexicographic order on lists
Andreas Lochbihler
parents:
54498
diff
changeset
|
7069 |
shows "\<not> lexordp xs xs" |
8c0a27b9c1bd
add predicate version of lexicographic order on lists
Andreas Lochbihler
parents:
54498
diff
changeset
|
7070 |
proof |
8c0a27b9c1bd
add predicate version of lexicographic order on lists
Andreas Lochbihler
parents:
54498
diff
changeset
|
7071 |
assume "lexordp xs xs" |
8c0a27b9c1bd
add predicate version of lexicographic order on lists
Andreas Lochbihler
parents:
54498
diff
changeset
|
7072 |
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
|
7073 |
qed |
8c0a27b9c1bd
add predicate version of lexicographic order on lists
Andreas Lochbihler
parents:
54498
diff
changeset
|
7074 |
|
8c0a27b9c1bd
add predicate version of lexicographic order on lists
Andreas Lochbihler
parents:
54498
diff
changeset
|
7075 |
lemma lexordp_into_lexordp_eq: |
73018 | 7076 |
"lexordp xs ys \<Longrightarrow> lexordp_eq xs ys" |
7077 |
by (induction rule: lexordp.induct) simp_all |
|
7078 |
||
7079 |
lemma lexordp_eq_pref: "lexordp_eq u (u @ v)" |
|
7080 |
by (metis append_Nil2 lexordp_append_rightI lexordp_eq_refl lexordp_into_lexordp_eq) |
|
54593
8c0a27b9c1bd
add predicate version of lexicographic order on lists
Andreas Lochbihler
parents:
54498
diff
changeset
|
7081 |
|
8c0a27b9c1bd
add predicate version of lexicographic order on lists
Andreas Lochbihler
parents:
54498
diff
changeset
|
7082 |
end |
8c0a27b9c1bd
add predicate version of lexicographic order on lists
Andreas Lochbihler
parents:
54498
diff
changeset
|
7083 |
|
72170
7fa9605b226c
Another go with lex: now lexordp back in class ord
paulson <lp15@cam.ac.uk>
parents:
72164
diff
changeset
|
7084 |
declare ord.lexordp_simps [simp, code] |
75599 | 7085 |
declare ord.lexordp_eq_simps [simp, code] |
54593
8c0a27b9c1bd
add predicate version of lexicographic order on lists
Andreas Lochbihler
parents:
54498
diff
changeset
|
7086 |
|
61681
ca53150406c9
option "inductive_defs" controls exposure of def and mono facts;
wenzelm
parents:
61630
diff
changeset
|
7087 |
context order |
ca53150406c9
option "inductive_defs" controls exposure of def and mono facts;
wenzelm
parents:
61630
diff
changeset
|
7088 |
begin |
54593
8c0a27b9c1bd
add predicate version of lexicographic order on lists
Andreas Lochbihler
parents:
54498
diff
changeset
|
7089 |
|
8c0a27b9c1bd
add predicate version of lexicographic order on lists
Andreas Lochbihler
parents:
54498
diff
changeset
|
7090 |
lemma lexordp_antisym: |
8c0a27b9c1bd
add predicate version of lexicographic order on lists
Andreas Lochbihler
parents:
54498
diff
changeset
|
7091 |
assumes "lexordp xs ys" "lexordp ys xs" |
8c0a27b9c1bd
add predicate version of lexicographic order on lists
Andreas Lochbihler
parents:
54498
diff
changeset
|
7092 |
shows False |
72164
b7c54ff7f2dd
S Holub's proposed generalisation of the lexicographic product of two orderings
paulson <lp15@cam.ac.uk>
parents:
72095
diff
changeset
|
7093 |
using assms by induct auto |
54593
8c0a27b9c1bd
add predicate version of lexicographic order on lists
Andreas Lochbihler
parents:
54498
diff
changeset
|
7094 |
|
8c0a27b9c1bd
add predicate version of lexicographic order on lists
Andreas Lochbihler
parents:
54498
diff
changeset
|
7095 |
lemma lexordp_irreflexive': "\<not> lexordp xs xs" |
8c0a27b9c1bd
add predicate version of lexicographic order on lists
Andreas Lochbihler
parents:
54498
diff
changeset
|
7096 |
by(rule lexordp_irreflexive) simp |
8c0a27b9c1bd
add predicate version of lexicographic order on lists
Andreas Lochbihler
parents:
54498
diff
changeset
|
7097 |
|
8c0a27b9c1bd
add predicate version of lexicographic order on lists
Andreas Lochbihler
parents:
54498
diff
changeset
|
7098 |
end |
8c0a27b9c1bd
add predicate version of lexicographic order on lists
Andreas Lochbihler
parents:
54498
diff
changeset
|
7099 |
|
8c0a27b9c1bd
add predicate version of lexicographic order on lists
Andreas Lochbihler
parents:
54498
diff
changeset
|
7100 |
context linorder begin |
8c0a27b9c1bd
add predicate version of lexicographic order on lists
Andreas Lochbihler
parents:
54498
diff
changeset
|
7101 |
|
8c0a27b9c1bd
add predicate version of lexicographic order on lists
Andreas Lochbihler
parents:
54498
diff
changeset
|
7102 |
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
|
7103 |
assumes "lexordp xs ys" |
8c0a27b9c1bd
add predicate version of lexicographic order on lists
Andreas Lochbihler
parents:
54498
diff
changeset
|
7104 |
obtains (Nil) y ys' where "xs = []" "ys = y # ys'" |
8c0a27b9c1bd
add predicate version of lexicographic order on lists
Andreas Lochbihler
parents:
54498
diff
changeset
|
7105 |
| (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
|
7106 |
| (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
|
7107 |
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
|
7108 |
|
8c0a27b9c1bd
add predicate version of lexicographic order on lists
Andreas Lochbihler
parents:
54498
diff
changeset
|
7109 |
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
|
7110 |
assumes major: "lexordp xs ys" |
8c0a27b9c1bd
add predicate version of lexicographic order on lists
Andreas Lochbihler
parents:
54498
diff
changeset
|
7111 |
and Nil: "\<And>y ys. P [] (y # ys)" |
8c0a27b9c1bd
add predicate version of lexicographic order on lists
Andreas Lochbihler
parents:
54498
diff
changeset
|
7112 |
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
|
7113 |
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
|
7114 |
shows "P xs ys" |
8c0a27b9c1bd
add predicate version of lexicographic order on lists
Andreas Lochbihler
parents:
54498
diff
changeset
|
7115 |
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
|
7116 |
|
8c0a27b9c1bd
add predicate version of lexicographic order on lists
Andreas Lochbihler
parents:
54498
diff
changeset
|
7117 |
lemma lexordp_iff: |
8c0a27b9c1bd
add predicate version of lexicographic order on lists
Andreas Lochbihler
parents:
54498
diff
changeset
|
7118 |
"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
|
7119 |
(is "?lhs = ?rhs") |
8c0a27b9c1bd
add predicate version of lexicographic order on lists
Andreas Lochbihler
parents:
54498
diff
changeset
|
7120 |
proof |
8c0a27b9c1bd
add predicate version of lexicographic order on lists
Andreas Lochbihler
parents:
54498
diff
changeset
|
7121 |
assume ?lhs thus ?rhs |
8c0a27b9c1bd
add predicate version of lexicographic order on lists
Andreas Lochbihler
parents:
54498
diff
changeset
|
7122 |
proof induct |
8c0a27b9c1bd
add predicate version of lexicographic order on lists
Andreas Lochbihler
parents:
54498
diff
changeset
|
7123 |
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
|
7124 |
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
|
7125 |
next |
8c0a27b9c1bd
add predicate version of lexicographic order on lists
Andreas Lochbihler
parents:
54498
diff
changeset
|
7126 |
assume ?rhs thus ?lhs |
8c0a27b9c1bd
add predicate version of lexicographic order on lists
Andreas Lochbihler
parents:
54498
diff
changeset
|
7127 |
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
|
7128 |
qed |
8c0a27b9c1bd
add predicate version of lexicographic order on lists
Andreas Lochbihler
parents:
54498
diff
changeset
|
7129 |
|
8c0a27b9c1bd
add predicate version of lexicographic order on lists
Andreas Lochbihler
parents:
54498
diff
changeset
|
7130 |
lemma lexordp_conv_lexord: |
8c0a27b9c1bd
add predicate version of lexicographic order on lists
Andreas Lochbihler
parents:
54498
diff
changeset
|
7131 |
"lexordp xs ys \<longleftrightarrow> (xs, ys) \<in> lexord {(x, y). x < y}" |
72184 | 7132 |
by(simp add: lexordp_iff lexord_def) |
54593
8c0a27b9c1bd
add predicate version of lexicographic order on lists
Andreas Lochbihler
parents:
54498
diff
changeset
|
7133 |
|
64963 | 7134 |
lemma lexordp_eq_antisym: |
7135 |
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
|
7136 |
shows "xs = ys" |
8c0a27b9c1bd
add predicate version of lexicographic order on lists
Andreas Lochbihler
parents:
54498
diff
changeset
|
7137 |
using assms by induct simp_all |
8c0a27b9c1bd
add predicate version of lexicographic order on lists
Andreas Lochbihler
parents:
54498
diff
changeset
|
7138 |
|
8c0a27b9c1bd
add predicate version of lexicographic order on lists
Andreas Lochbihler
parents:
54498
diff
changeset
|
7139 |
lemma lexordp_eq_trans: |
8c0a27b9c1bd
add predicate version of lexicographic order on lists
Andreas Lochbihler
parents:
54498
diff
changeset
|
7140 |
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
|
7141 |
shows "lexordp_eq xs zs" |
68719 | 7142 |
using assms |
7143 |
by (induct arbitrary: zs) (case_tac zs; auto)+ |
|
54593
8c0a27b9c1bd
add predicate version of lexicographic order on lists
Andreas Lochbihler
parents:
54498
diff
changeset
|
7144 |
|
8c0a27b9c1bd
add predicate version of lexicographic order on lists
Andreas Lochbihler
parents:
54498
diff
changeset
|
7145 |
lemma lexordp_trans: |
8c0a27b9c1bd
add predicate version of lexicographic order on lists
Andreas Lochbihler
parents:
54498
diff
changeset
|
7146 |
assumes "lexordp xs ys" "lexordp ys zs" |
8c0a27b9c1bd
add predicate version of lexicographic order on lists
Andreas Lochbihler
parents:
54498
diff
changeset
|
7147 |
shows "lexordp xs zs" |
68719 | 7148 |
using assms |
7149 |
by (induct arbitrary: zs) (case_tac zs; auto)+ |
|
54593
8c0a27b9c1bd
add predicate version of lexicographic order on lists
Andreas Lochbihler
parents:
54498
diff
changeset
|
7150 |
|
8c0a27b9c1bd
add predicate version of lexicographic order on lists
Andreas Lochbihler
parents:
54498
diff
changeset
|
7151 |
lemma lexordp_linear: "lexordp xs ys \<or> xs = ys \<or> lexordp ys xs" |
68719 | 7152 |
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
|
7153 |
|
8c0a27b9c1bd
add predicate version of lexicographic order on lists
Andreas Lochbihler
parents:
54498
diff
changeset
|
7154 |
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
|
7155 |
(is "?lhs \<longleftrightarrow> ?rhs") |
8c0a27b9c1bd
add predicate version of lexicographic order on lists
Andreas Lochbihler
parents:
54498
diff
changeset
|
7156 |
proof |
8c0a27b9c1bd
add predicate version of lexicographic order on lists
Andreas Lochbihler
parents:
54498
diff
changeset
|
7157 |
assume ?lhs |
63540 | 7158 |
hence "\<not> lexordp_eq ys xs" by induct simp_all |
7159 |
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
|
7160 |
next |
8c0a27b9c1bd
add predicate version of lexicographic order on lists
Andreas Lochbihler
parents:
54498
diff
changeset
|
7161 |
assume ?rhs |
8c0a27b9c1bd
add predicate version of lexicographic order on lists
Andreas Lochbihler
parents:
54498
diff
changeset
|
7162 |
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
|
7163 |
thus ?lhs by induct simp_all |
8c0a27b9c1bd
add predicate version of lexicographic order on lists
Andreas Lochbihler
parents:
54498
diff
changeset
|
7164 |
qed |
8c0a27b9c1bd
add predicate version of lexicographic order on lists
Andreas Lochbihler
parents:
54498
diff
changeset
|
7165 |
|
8c0a27b9c1bd
add predicate version of lexicographic order on lists
Andreas Lochbihler
parents:
54498
diff
changeset
|
7166 |
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
|
7167 |
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
|
7168 |
|
8c0a27b9c1bd
add predicate version of lexicographic order on lists
Andreas Lochbihler
parents:
54498
diff
changeset
|
7169 |
lemma lexordp_eq_linear: "lexordp_eq xs ys \<or> lexordp_eq ys xs" |
68719 | 7170 |
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
|
7171 |
|
8c0a27b9c1bd
add predicate version of lexicographic order on lists
Andreas Lochbihler
parents:
54498
diff
changeset
|
7172 |
lemma lexordp_linorder: "class.linorder lexordp_eq lexordp" |
68719 | 7173 |
by unfold_locales |
7174 |
(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
|
7175 |
|
8c0a27b9c1bd
add predicate version of lexicographic order on lists
Andreas Lochbihler
parents:
54498
diff
changeset
|
7176 |
end |
15656 | 7177 |
|
64886 | 7178 |
|
60758 | 7179 |
subsubsection \<open>Lexicographic combination of measure functions\<close> |
7180 |
||
7181 |
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
|
7182 |
|
50548 | 7183 |
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
|
7184 |
|
44013
5cfc1c36ae97
moved recdef package to HOL/Library/Old_Recdef.thy
krauss
parents:
43594
diff
changeset
|
7185 |
lemma wf_measures[simp]: "wf (measures fs)" |
24349 | 7186 |
unfolding measures_def |
7187 |
by blast |
|
21103
367b4ad7c7cc
Added "measures" combinator for lexicographic combinations of multiple measures.
krauss
parents:
21079
diff
changeset
|
7188 |
|
64963 | 7189 |
lemma in_measures[simp]: |
21103
367b4ad7c7cc
Added "measures" combinator for lexicographic combinations of multiple measures.
krauss
parents:
21079
diff
changeset
|
7190 |
"(x, y) \<in> measures [] = False" |
367b4ad7c7cc
Added "measures" combinator for lexicographic combinations of multiple measures.
krauss
parents:
21079
diff
changeset
|
7191 |
"(x, y) \<in> measures (f # fs) |
64963 | 7192 |
= (f x < f y \<or> (f x = f y \<and> (x, y) \<in> measures fs))" |
24349 | 7193 |
unfolding measures_def |
7194 |
by auto |
|
21103
367b4ad7c7cc
Added "measures" combinator for lexicographic combinations of multiple measures.
krauss
parents:
21079
diff
changeset
|
7195 |
|
71585 | 7196 |
lemma measures_less: "f x < f y \<Longrightarrow> (x, y) \<in> measures (f#fs)" |
24349 | 7197 |
by simp |
21103
367b4ad7c7cc
Added "measures" combinator for lexicographic combinations of multiple measures.
krauss
parents:
21079
diff
changeset
|
7198 |
|
71585 | 7199 |
lemma measures_lesseq: "f x \<le> f y \<Longrightarrow> (x, y) \<in> measures fs \<Longrightarrow> (x, y) \<in> measures (f#fs)" |
24349 | 7200 |
by auto |
21103
367b4ad7c7cc
Added "measures" combinator for lexicographic combinations of multiple measures.
krauss
parents:
21079
diff
changeset
|
7201 |
|
367b4ad7c7cc
Added "measures" combinator for lexicographic combinations of multiple measures.
krauss
parents:
21079
diff
changeset
|
7202 |
|
60758 | 7203 |
subsubsection \<open>Lifting Relations to Lists: one element\<close> |
40230 | 7204 |
|
7205 |
definition listrel1 :: "('a \<times> 'a) set \<Rightarrow> ('a list \<times> 'a list) set" where |
|
7206 |
"listrel1 r = {(xs,ys). |
|
7207 |
\<exists>us z z' vs. xs = us @ z # vs \<and> (z,z') \<in> r \<and> ys = us @ z' # vs}" |
|
7208 |
||
7209 |
lemma listrel1I: |
|
7210 |
"\<lbrakk> (x, y) \<in> r; xs = us @ x # vs; ys = us @ y # vs \<rbrakk> \<Longrightarrow> |
|
7211 |
(xs, ys) \<in> listrel1 r" |
|
7212 |
unfolding listrel1_def by auto |
|
7213 |
||
7214 |
lemma listrel1E: |
|
7215 |
"\<lbrakk> (xs, ys) \<in> listrel1 r; |
|
7216 |
!!x y us vs. \<lbrakk> (x, y) \<in> r; xs = us @ x # vs; ys = us @ y # vs \<rbrakk> \<Longrightarrow> P |
|
7217 |
\<rbrakk> \<Longrightarrow> P" |
|
7218 |
unfolding listrel1_def by auto |
|
7219 |
||
7220 |
lemma not_Nil_listrel1 [iff]: "([], xs) \<notin> listrel1 r" |
|
7221 |
unfolding listrel1_def by blast |
|
7222 |
||
7223 |
lemma not_listrel1_Nil [iff]: "(xs, []) \<notin> listrel1 r" |
|
7224 |
unfolding listrel1_def by blast |
|
7225 |
||
7226 |
lemma Cons_listrel1_Cons [iff]: |
|
7227 |
"(x # xs, y # ys) \<in> listrel1 r \<longleftrightarrow> |
|
7228 |
(x,y) \<in> r \<and> xs = ys \<or> x = y \<and> (xs, ys) \<in> listrel1 r" |
|
7229 |
by (simp add: listrel1_def Cons_eq_append_conv) (blast) |
|
7230 |
||
7231 |
lemma listrel1I1: "(x,y) \<in> r \<Longrightarrow> (x # xs, y # xs) \<in> listrel1 r" |
|
56085 | 7232 |
by fast |
40230 | 7233 |
|
7234 |
lemma listrel1I2: "(xs, ys) \<in> listrel1 r \<Longrightarrow> (x # xs, x # ys) \<in> listrel1 r" |
|
56085 | 7235 |
by fast |
40230 | 7236 |
|
7237 |
lemma append_listrel1I: |
|
7238 |
"(xs, ys) \<in> listrel1 r \<and> us = vs \<or> xs = ys \<and> (us, vs) \<in> listrel1 r |
|
7239 |
\<Longrightarrow> (xs @ us, ys @ vs) \<in> listrel1 r" |
|
7240 |
unfolding listrel1_def |
|
7241 |
by auto (blast intro: append_eq_appendI)+ |
|
7242 |
||
7243 |
lemma Cons_listrel1E1[elim!]: |
|
7244 |
assumes "(x # xs, ys) \<in> listrel1 r" |
|
7245 |
and "\<And>y. ys = y # xs \<Longrightarrow> (x, y) \<in> r \<Longrightarrow> R" |
|
7246 |
and "\<And>zs. ys = x # zs \<Longrightarrow> (xs, zs) \<in> listrel1 r \<Longrightarrow> R" |
|
7247 |
shows R |
|
7248 |
using assms by (cases ys) blast+ |
|
7249 |
||
7250 |
lemma Cons_listrel1E2[elim!]: |
|
7251 |
assumes "(xs, y # ys) \<in> listrel1 r" |
|
7252 |
and "\<And>x. xs = x # ys \<Longrightarrow> (x, y) \<in> r \<Longrightarrow> R" |
|
7253 |
and "\<And>zs. xs = y # zs \<Longrightarrow> (zs, ys) \<in> listrel1 r \<Longrightarrow> R" |
|
7254 |
shows R |
|
7255 |
using assms by (cases xs) blast+ |
|
7256 |
||
7257 |
lemma snoc_listrel1_snoc_iff: |
|
7258 |
"(xs @ [x], ys @ [y]) \<in> listrel1 r |
|
7259 |
\<longleftrightarrow> (xs, ys) \<in> listrel1 r \<and> x = y \<or> xs = ys \<and> (x,y) \<in> r" (is "?L \<longleftrightarrow> ?R") |
|
7260 |
proof |
|
7261 |
assume ?L thus ?R |
|
44890
22f665a2e91c
new fastforce replacing fastsimp - less confusing name
nipkow
parents:
44635
diff
changeset
|
7262 |
by (fastforce simp: listrel1_def snoc_eq_iff_butlast butlast_append) |
40230 | 7263 |
next |
7264 |
assume ?R then show ?L unfolding listrel1_def by force |
|
7265 |
qed |
|
7266 |
||
7267 |
lemma listrel1_eq_len: "(xs,ys) \<in> listrel1 r \<Longrightarrow> length xs = length ys" |
|
7268 |
unfolding listrel1_def by auto |
|
7269 |
||
7270 |
lemma listrel1_mono: |
|
7271 |
"r \<subseteq> s \<Longrightarrow> listrel1 r \<subseteq> listrel1 s" |
|
7272 |
unfolding listrel1_def by blast |
|
7273 |
||
7274 |
||
67613 | 7275 |
lemma listrel1_converse: "listrel1 (r\<inverse>) = (listrel1 r)\<inverse>" |
40230 | 7276 |
unfolding listrel1_def by blast |
7277 |
||
7278 |
lemma in_listrel1_converse: |
|
67613 | 7279 |
"(x,y) \<in> listrel1 (r\<inverse>) \<longleftrightarrow> (x,y) \<in> (listrel1 r)\<inverse>" |
40230 | 7280 |
unfolding listrel1_def by blast |
7281 |
||
7282 |
lemma listrel1_iff_update: |
|
7283 |
"(xs,ys) \<in> (listrel1 r) |
|
7284 |
\<longleftrightarrow> (\<exists>y n. (xs ! n, y) \<in> r \<and> n < length xs \<and> ys = xs[n:=y])" (is "?L \<longleftrightarrow> ?R") |
|
7285 |
proof |
|
7286 |
assume "?L" |
|
7287 |
then obtain x y u v where "xs = u @ x # v" "ys = u @ y # v" "(x,y) \<in> r" |
|
7288 |
unfolding listrel1_def by auto |
|
7289 |
then have "ys = xs[length u := y]" and "length u < length xs" |
|
7290 |
and "(xs ! length u, y) \<in> r" by auto |
|
7291 |
then show "?R" by auto |
|
7292 |
next |
|
7293 |
assume "?R" |
|
7294 |
then obtain x y n where "(xs!n, y) \<in> r" "n < size xs" "ys = xs[n:=y]" "x = xs!n" |
|
7295 |
by auto |
|
7296 |
then obtain u v where "xs = u @ x # v" and "ys = u @ y # v" and "(x, y) \<in> r" |
|
7297 |
by (auto intro: upd_conv_take_nth_drop id_take_nth_drop) |
|
7298 |
then show "?L" by (auto simp: listrel1_def) |
|
7299 |
qed |
|
7300 |
||
7301 |
||
60758 | 7302 |
text\<open>Accessible part and wellfoundedness:\<close> |
40230 | 7303 |
|
7304 |
lemma Cons_acc_listrel1I [intro!]: |
|
54295 | 7305 |
"x \<in> Wellfounded.acc r \<Longrightarrow> xs \<in> Wellfounded.acc (listrel1 r) \<Longrightarrow> (x # xs) \<in> Wellfounded.acc (listrel1 r)" |
77433 | 7306 |
proof (induction arbitrary: xs set: Wellfounded.acc) |
7307 |
case outer: (1 u) |
|
7308 |
show ?case |
|
7309 |
proof (induct xs rule: acc_induct) |
|
7310 |
case 1 |
|
7311 |
show "xs \<in> Wellfounded.acc (listrel1 r)" |
|
7312 |
by (simp add: outer.prems) |
|
7313 |
qed (metis (no_types, lifting) Cons_listrel1E2 acc.simps outer.IH) |
|
7314 |
qed |
|
40230 | 7315 |
|
54295 | 7316 |
lemma lists_accD: "xs \<in> lists (Wellfounded.acc r) \<Longrightarrow> xs \<in> Wellfounded.acc (listrel1 r)" |
68719 | 7317 |
proof (induct set: lists) |
7318 |
case Nil |
|
7319 |
then show ?case |
|
7320 |
by (meson acc.intros not_listrel1_Nil) |
|
7321 |
next |
|
7322 |
case (Cons a l) |
|
7323 |
then show ?case |
|
7324 |
by blast |
|
7325 |
qed |
|
40230 | 7326 |
|
54295 | 7327 |
lemma lists_accI: "xs \<in> Wellfounded.acc (listrel1 r) \<Longrightarrow> xs \<in> lists (Wellfounded.acc r)" |
77433 | 7328 |
proof (induction set: Wellfounded.acc) |
7329 |
case (1 x) |
|
7330 |
then have "\<And>u v. \<lbrakk>u \<in> set x; (v, u) \<in> r\<rbrakk> \<Longrightarrow> v \<in> Wellfounded.acc r" |
|
7331 |
by (metis in_lists_conv_set in_set_conv_decomp listrel1I) |
|
7332 |
then show ?case |
|
7333 |
by (meson acc.intros in_listsI) |
|
7334 |
qed |
|
40230 | 7335 |
|
44510 | 7336 |
lemma wf_listrel1_iff[simp]: "wf(listrel1 r) = wf r" |
56085 | 7337 |
by (auto simp: wf_acc_iff |
7338 |
intro: lists_accD lists_accI[THEN Cons_in_lists_iff[THEN iffD1, THEN conjunct1]]) |
|
40230 | 7339 |
|
60758 | 7340 |
subsubsection \<open>Lifting Relations to Lists: all elements\<close> |
15302 | 7341 |
|
23740 | 7342 |
inductive_set |
46317 | 7343 |
listrel :: "('a \<times> 'b) set \<Rightarrow> ('a list \<times> 'b list) set" |
7344 |
for r :: "('a \<times> 'b) set" |
|
22262 | 7345 |
where |
23740 | 7346 |
Nil: "([],[]) \<in> listrel r" |
71585 | 7347 |
| Cons: "\<lbrakk>(x,y) \<in> r; (xs,ys) \<in> listrel r\<rbrakk> \<Longrightarrow> (x#xs, y#ys) \<in> listrel r" |
23740 | 7348 |
|
7349 |
inductive_cases listrel_Nil1 [elim!]: "([],xs) \<in> listrel r" |
|
7350 |
inductive_cases listrel_Nil2 [elim!]: "(xs,[]) \<in> listrel r" |
|
7351 |
inductive_cases listrel_Cons1 [elim!]: "(y#ys,xs) \<in> listrel r" |
|
7352 |
inductive_cases listrel_Cons2 [elim!]: "(xs,y#ys) \<in> listrel r" |
|
15302 | 7353 |
|
7354 |
||
40230 | 7355 |
lemma listrel_eq_len: "(xs, ys) \<in> listrel r \<Longrightarrow> length xs = length ys" |
71813 | 7356 |
by(induct rule: listrel.induct) auto |
40230 | 7357 |
|
67091 | 7358 |
lemma listrel_iff_zip [code_unfold]: "(xs,ys) \<in> listrel r \<longleftrightarrow> |
7359 |
length xs = length ys \<and> (\<forall>(x,y) \<in> set(zip xs ys). (x,y) \<in> r)" (is "?L \<longleftrightarrow> ?R") |
|
40230 | 7360 |
proof |
7361 |
assume ?L thus ?R by induct (auto intro: listrel_eq_len) |
|
7362 |
next |
|
7363 |
assume ?R thus ?L |
|
7364 |
apply (clarify) |
|
7365 |
by (induct rule: list_induct2) (auto intro: listrel.intros) |
|
7366 |
qed |
|
7367 |
||
67091 | 7368 |
lemma listrel_iff_nth: "(xs,ys) \<in> listrel r \<longleftrightarrow> |
7369 |
length xs = length ys \<and> (\<forall>n < length xs. (xs!n, ys!n) \<in> r)" (is "?L \<longleftrightarrow> ?R") |
|
71813 | 7370 |
by (auto simp add: all_set_conv_all_nth listrel_iff_zip) |
40230 | 7371 |
|
15302 | 7372 |
lemma listrel_mono: "r \<subseteq> s \<Longrightarrow> listrel r \<subseteq> listrel s" |
68719 | 7373 |
by (meson listrel_iff_nth subrelI subset_eq) |
15302 | 7374 |
|
71813 | 7375 |
lemma listrel_subset: |
7376 |
assumes "r \<subseteq> A \<times> A" shows "listrel r \<subseteq> lists A \<times> lists A" |
|
7377 |
proof clarify |
|
7378 |
show "a \<in> lists A \<and> b \<in> lists A" if "(a, b) \<in> listrel r" for a b |
|
7379 |
using that assms by (induction rule: listrel.induct, auto) |
|
7380 |
qed |
|
7381 |
||
7382 |
lemma listrel_refl_on: |
|
7383 |
assumes "refl_on A r" shows "refl_on (lists A) (listrel r)" |
|
7384 |
proof - |
|
7385 |
have "l \<in> lists A \<Longrightarrow> (l, l) \<in> listrel r" for l |
|
7386 |
using assms unfolding refl_on_def |
|
7387 |
by (induction l, auto intro: listrel.intros) |
|
7388 |
then show ?thesis |
|
7389 |
by (meson assms listrel_subset refl_on_def) |
|
7390 |
qed |
|
15302 | 7391 |
|
64963 | 7392 |
lemma listrel_sym: "sym r \<Longrightarrow> sym (listrel r)" |
68719 | 7393 |
by (simp add: listrel_iff_nth sym_def) |
15302 | 7394 |
|
71813 | 7395 |
lemma listrel_trans: |
7396 |
assumes "trans r" shows "trans (listrel r)" |
|
7397 |
proof - |
|
7398 |
have "(x, z) \<in> listrel r" if "(x, y) \<in> listrel r" "(y, z) \<in> listrel r" for x y z |
|
7399 |
using that |
|
7400 |
proof induction |
|
7401 |
case (Cons x y xs ys) |
|
7402 |
then show ?case |
|
7403 |
by clarsimp (metis assms listrel.Cons listrel_iff_nth transD) |
|
7404 |
qed auto |
|
7405 |
then show ?thesis |
|
7406 |
using transI by blast |
|
7407 |
qed |
|
15302 | 7408 |
|
7409 |
theorem equiv_listrel: "equiv A r \<Longrightarrow> equiv (lists A) (listrel r)" |
|
71813 | 7410 |
by (simp add: equiv_def listrel_refl_on listrel_sym listrel_trans) |
15302 | 7411 |
|
67613 | 7412 |
lemma listrel_rtrancl_refl[iff]: "(xs,xs) \<in> listrel(r\<^sup>*)" |
71813 | 7413 |
using listrel_refl_on[of UNIV, OF refl_rtrancl] |
7414 |
by(auto simp: refl_on_def) |
|
40230 | 7415 |
|
7416 |
lemma listrel_rtrancl_trans: |
|
71813 | 7417 |
"\<lbrakk>(xs,ys) \<in> listrel(r\<^sup>*); (ys,zs) \<in> listrel(r\<^sup>*)\<rbrakk> \<Longrightarrow> (xs,zs) \<in> listrel(r\<^sup>*)" |
7418 |
by (metis listrel_trans trans_def trans_rtrancl) |
|
40230 | 7419 |
|
15302 | 7420 |
lemma listrel_Nil [simp]: "listrel r `` {[]} = {[]}" |
71813 | 7421 |
by (blast intro: listrel.intros) |
15302 | 7422 |
|
7423 |
lemma listrel_Cons: |
|
71813 | 7424 |
"listrel r `` {x#xs} = set_Cons (r``{x}) (listrel r `` {xs})" |
7425 |
by (auto simp add: set_Cons_def intro: listrel.intros) |
|
15302 | 7426 |
|
69593 | 7427 |
text \<open>Relating \<^term>\<open>listrel1\<close>, \<^term>\<open>listrel\<close> and closures:\<close> |
40230 | 7428 |
|
71813 | 7429 |
lemma listrel1_rtrancl_subset_rtrancl_listrel1: "listrel1 (r\<^sup>*) \<subseteq> (listrel1 r)\<^sup>*" |
40230 | 7430 |
proof (rule subrelI) |
67613 | 7431 |
fix xs ys assume 1: "(xs,ys) \<in> listrel1 (r\<^sup>*)" |
40230 | 7432 |
{ fix x y us vs |
67613 | 7433 |
have "(x,y) \<in> r\<^sup>* \<Longrightarrow> (us @ x # vs, us @ y # vs) \<in> (listrel1 r)\<^sup>*" |
40230 | 7434 |
proof(induct rule: rtrancl.induct) |
7435 |
case rtrancl_refl show ?case by simp |
|
7436 |
next |
|
7437 |
case rtrancl_into_rtrancl thus ?case |
|
7438 |
by (metis listrel1I rtrancl.rtrancl_into_rtrancl) |
|
7439 |
qed } |
|
67613 | 7440 |
thus "(xs,ys) \<in> (listrel1 r)\<^sup>*" using 1 by(blast elim: listrel1E) |
40230 | 7441 |
qed |
7442 |
||
67613 | 7443 |
lemma rtrancl_listrel1_eq_len: "(x,y) \<in> (listrel1 r)\<^sup>* \<Longrightarrow> length x = length y" |
71813 | 7444 |
by (induct rule: rtrancl.induct) (auto intro: listrel1_eq_len) |
40230 | 7445 |
|
7446 |
lemma rtrancl_listrel1_ConsI1: |
|
67613 | 7447 |
"(xs,ys) \<in> (listrel1 r)\<^sup>* \<Longrightarrow> (x#xs,x#ys) \<in> (listrel1 r)\<^sup>*" |
71813 | 7448 |
proof (induction rule: rtrancl.induct) |
7449 |
case (rtrancl_into_rtrancl a b c) |
|
7450 |
then show ?case |
|
7451 |
by (metis listrel1I2 rtrancl.rtrancl_into_rtrancl) |
|
7452 |
qed auto |
|
40230 | 7453 |
|
7454 |
lemma rtrancl_listrel1_ConsI2: |
|
71813 | 7455 |
"(x,y) \<in> r\<^sup>* \<Longrightarrow> (xs, ys) \<in> (listrel1 r)\<^sup>* \<Longrightarrow> (x # xs, y # ys) \<in> (listrel1 r)\<^sup>*" |
7456 |
by (meson in_mono listrel1I1 listrel1_rtrancl_subset_rtrancl_listrel1 rtrancl_listrel1_ConsI1 rtrancl_trans) |
|
40230 | 7457 |
|
7458 |
lemma listrel1_subset_listrel: |
|
7459 |
"r \<subseteq> r' \<Longrightarrow> refl r' \<Longrightarrow> listrel1 r \<subseteq> listrel(r')" |
|
71813 | 7460 |
by(auto elim!: listrel1E simp add: listrel_iff_zip set_zip refl_on_def) |
40230 | 7461 |
|
7462 |
lemma listrel_reflcl_if_listrel1: |
|
67613 | 7463 |
"(xs,ys) \<in> listrel1 r \<Longrightarrow> (xs,ys) \<in> listrel(r\<^sup>*)" |
71813 | 7464 |
by(erule listrel1E)(auto simp add: listrel_iff_zip set_zip) |
40230 | 7465 |
|
67613 | 7466 |
lemma listrel_rtrancl_eq_rtrancl_listrel1: "listrel (r\<^sup>*) = (listrel1 r)\<^sup>*" |
40230 | 7467 |
proof |
67613 | 7468 |
{ fix x y assume "(x,y) \<in> listrel (r\<^sup>*)" |
7469 |
then have "(x,y) \<in> (listrel1 r)\<^sup>*" |
|
40230 | 7470 |
by induct (auto intro: rtrancl_listrel1_ConsI2) } |
67613 | 7471 |
then show "listrel (r\<^sup>*) \<subseteq> (listrel1 r)\<^sup>*" |
40230 | 7472 |
by (rule subrelI) |
7473 |
next |
|
67613 | 7474 |
show "listrel (r\<^sup>*) \<supseteq> (listrel1 r)\<^sup>*" |
40230 | 7475 |
proof(rule subrelI) |
67613 | 7476 |
fix xs ys assume "(xs,ys) \<in> (listrel1 r)\<^sup>*" |
7477 |
then show "(xs,ys) \<in> listrel (r\<^sup>*)" |
|
40230 | 7478 |
proof induct |
7479 |
case base show ?case by(auto simp add: listrel_iff_zip set_zip) |
|
7480 |
next |
|
7481 |
case (step ys zs) |
|
56085 | 7482 |
thus ?case by (metis listrel_reflcl_if_listrel1 listrel_rtrancl_trans) |
40230 | 7483 |
qed |
7484 |
qed |
|
7485 |
qed |
|
7486 |
||
7487 |
lemma rtrancl_listrel1_if_listrel: |
|
67613 | 7488 |
"(xs,ys) \<in> listrel r \<Longrightarrow> (xs,ys) \<in> (listrel1 r)\<^sup>*" |
71813 | 7489 |
by(metis listrel_rtrancl_eq_rtrancl_listrel1 subsetD[OF listrel_mono] r_into_rtrancl subsetI) |
40230 | 7490 |
|
67613 | 7491 |
lemma listrel_subset_rtrancl_listrel1: "listrel r \<subseteq> (listrel1 r)\<^sup>*" |
71813 | 7492 |
by(fast intro:rtrancl_listrel1_if_listrel) |
40230 | 7493 |
|
15302 | 7494 |
|
60758 | 7495 |
subsection \<open>Size function\<close> |
26749
397a1aeede7d
* New attribute "termination_simp": Simp rules for termination proofs
krauss
parents:
26734
diff
changeset
|
7496 |
|
56643
41d3596d8a64
move size hooks together, with new one preceding old one and sharing same theory data
blanchet
parents:
56545
diff
changeset
|
7497 |
lemma [measure_function]: "is_measure f \<Longrightarrow> is_measure (size_list f)" |
71813 | 7498 |
by (rule is_measure_trivial) |
26875
e18574413bc4
Measure functions can now be declared via special rules, allowing for a
krauss
parents:
26795
diff
changeset
|
7499 |
|
56643
41d3596d8a64
move size hooks together, with new one preceding old one and sharing same theory data
blanchet
parents:
56545
diff
changeset
|
7500 |
lemma [measure_function]: "is_measure f \<Longrightarrow> is_measure (size_option f)" |
71813 | 7501 |
by (rule is_measure_trivial) |
26875
e18574413bc4
Measure functions can now be declared via special rules, allowing for a
krauss
parents:
26795
diff
changeset
|
7502 |
|
64963 | 7503 |
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
|
7504 |
"x \<in> set xs \<Longrightarrow> y < f x \<Longrightarrow> y < size_list f xs" |
71813 | 7505 |
by (induct xs) auto |
26749
397a1aeede7d
* New attribute "termination_simp": Simp rules for termination proofs
krauss
parents:
26734
diff
changeset
|
7506 |
|
64963 | 7507 |
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
|
7508 |
"x \<in> set xs \<Longrightarrow> y \<le> f x \<Longrightarrow> y \<le> size_list f xs" |
71813 | 7509 |
by (induct xs) auto |
26875
e18574413bc4
Measure functions can now be declared via special rules, allowing for a
krauss
parents:
26795
diff
changeset
|
7510 |
|
67091 | 7511 |
lemma size_list_map[simp]: "size_list f (map g xs) = size_list (f \<circ> g) xs" |
71813 | 7512 |
by (induct xs) auto |
26875
e18574413bc4
Measure functions can now be declared via special rules, allowing for a
krauss
parents:
26795
diff
changeset
|
7513 |
|
56643
41d3596d8a64
move size hooks together, with new one preceding old one and sharing same theory data
blanchet
parents:
56545
diff
changeset
|
7514 |
lemma size_list_append[simp]: "size_list f (xs @ ys) = size_list f xs + size_list f ys" |
71813 | 7515 |
by (induct xs, auto) |
44619
fd520fa2fb09
adding list_size_append (thanks to Rene Thiemann)
bulwahn
parents:
44618
diff
changeset
|
7516 |
|
64963 | 7517 |
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
|
7518 |
"(\<And>x. x \<in> set xs \<Longrightarrow> f x \<le> g x) \<Longrightarrow> size_list f xs \<le> size_list g xs" |
71813 | 7519 |
by (induct xs) force+ |
26749
397a1aeede7d
* New attribute "termination_simp": Simp rules for termination proofs
krauss
parents:
26734
diff
changeset
|
7520 |
|
31048
ac146fc38b51
refined HOL string theories and corresponding ML fragments
haftmann
parents:
31022
diff
changeset
|
7521 |
|
60758 | 7522 |
subsection \<open>Monad operation\<close> |
46143 | 7523 |
|
7524 |
definition bind :: "'a list \<Rightarrow> ('a \<Rightarrow> 'b list) \<Rightarrow> 'b list" where |
|
50548 | 7525 |
"bind xs f = concat (map f xs)" |
46143 | 7526 |
|
7527 |
hide_const (open) bind |
|
7528 |
||
7529 |
lemma bind_simps [simp]: |
|
7530 |
"List.bind [] f = []" |
|
7531 |
"List.bind (x # xs) f = f x @ List.bind xs f" |
|
7532 |
by (simp_all add: bind_def) |
|
7533 |
||
63099
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents:
63092
diff
changeset
|
7534 |
lemma list_bind_cong [fundef_cong]: |
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents:
63092
diff
changeset
|
7535 |
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
|
7536 |
shows "List.bind xs f = List.bind ys g" |
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents:
63092
diff
changeset
|
7537 |
proof - |
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents:
63092
diff
changeset
|
7538 |
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
|
7539 |
by (induction xs) simp_all |
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents:
63092
diff
changeset
|
7540 |
with assms(1) show ?thesis by simp |
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents:
63092
diff
changeset
|
7541 |
qed |
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents:
63092
diff
changeset
|
7542 |
|
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents:
63092
diff
changeset
|
7543 |
lemma set_list_bind: "set (List.bind xs f) = (\<Union>x\<in>set xs. set (f x))" |
64963 | 7544 |
by (induction xs) simp_all |
63099
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents:
63092
diff
changeset
|
7545 |
|
46143 | 7546 |
|
60758 | 7547 |
subsection \<open>Code generation\<close> |
7548 |
||
69593 | 7549 |
text\<open>Optional tail recursive version of \<^const>\<open>map\<close>. Can avoid |
60758 | 7550 |
stack overflow in some target languages.\<close> |
51875
dafd097dd1f4
tail recursive version of map, for code generation, optionally
nipkow
parents:
51738
diff
changeset
|
7551 |
|
dafd097dd1f4
tail recursive version of map, for code generation, optionally
nipkow
parents:
51738
diff
changeset
|
7552 |
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
|
7553 |
"map_tailrec_rev f [] bs = bs" | |
dafd097dd1f4
tail recursive version of map, for code generation, optionally
nipkow
parents:
51738
diff
changeset
|
7554 |
"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
|
7555 |
|
dafd097dd1f4
tail recursive version of map, for code generation, optionally
nipkow
parents:
51738
diff
changeset
|
7556 |
lemma map_tailrec_rev: |
dafd097dd1f4
tail recursive version of map, for code generation, optionally
nipkow
parents:
51738
diff
changeset
|
7557 |
"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
|
7558 |
by(induction as arbitrary: bs) simp_all |
dafd097dd1f4
tail recursive version of map, for code generation, optionally
nipkow
parents:
51738
diff
changeset
|
7559 |
|
dafd097dd1f4
tail recursive version of map, for code generation, optionally
nipkow
parents:
51738
diff
changeset
|
7560 |
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
|
7561 |
"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
|
7562 |
|
60758 | 7563 |
text\<open>Code equation:\<close> |
51875
dafd097dd1f4
tail recursive version of map, for code generation, optionally
nipkow
parents:
51738
diff
changeset
|
7564 |
lemma map_eq_map_tailrec: "map = map_tailrec" |
dafd097dd1f4
tail recursive version of map, for code generation, optionally
nipkow
parents:
51738
diff
changeset
|
7565 |
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
|
7566 |
|
dafd097dd1f4
tail recursive version of map, for code generation, optionally
nipkow
parents:
51738
diff
changeset
|
7567 |
|
60758 | 7568 |
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
|
7569 |
|
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
7570 |
definition member :: "'a list \<Rightarrow> 'a \<Rightarrow> bool" where |
50548 | 7571 |
[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
|
7572 |
|
60758 | 7573 |
text \<open> |
61799 | 7574 |
Use \<open>member\<close> only for generating executable code. Otherwise use |
69593 | 7575 |
\<^prop>\<open>x \<in> set xs\<close> instead --- it is much easier to reason about. |
60758 | 7576 |
\<close> |
37605
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
7577 |
|
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
7578 |
lemma member_rec [code]: |
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
7579 |
"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
|
7580 |
"member [] y \<longleftrightarrow> False" |
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
7581 |
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
|
7582 |
|
46030
51b2f3412a03
attribute code_abbrev superseedes code_unfold_post; tuned text
haftmann
parents:
45993
diff
changeset
|
7583 |
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
|
7584 |
"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
|
7585 |
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
|
7586 |
|
62328 | 7587 |
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
|
7588 |
|
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
7589 |
definition list_ex :: "('a \<Rightarrow> bool) \<Rightarrow> 'a list \<Rightarrow> bool" where |
50548 | 7590 |
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
|
7591 |
|
51b2f3412a03
attribute code_abbrev superseedes code_unfold_post; tuned text
haftmann
parents:
45993
diff
changeset
|
7592 |
definition list_ex1 :: "('a \<Rightarrow> bool) \<Rightarrow> 'a list \<Rightarrow> bool" where |
50548 | 7593 |
list_ex1_iff [code_abbrev]: "list_ex1 P xs \<longleftrightarrow> (\<exists>! x. x \<in> set xs \<and> P x)" |
40652 | 7594 |
|
60758 | 7595 |
text \<open> |
61799 | 7596 |
Usually you should prefer \<open>\<forall>x\<in>set xs\<close>, \<open>\<exists>x\<in>set xs\<close> |
69593 | 7597 |
and \<open>\<exists>!x. x\<in>set xs \<and> _\<close> over \<^const>\<open>list_all\<close>, \<^const>\<open>list_ex\<close> |
7598 |
and \<^const>\<open>list_ex1\<close> in specifications. |
|
60758 | 7599 |
\<close> |
37605
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
7600 |
|
56527
907f04603177
make list_all an abbreviation of pred_list - prevent duplication
kuncar
parents:
56525
diff
changeset
|
7601 |
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
|
7602 |
"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
|
7603 |
"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
|
7604 |
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
|
7605 |
|
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
7606 |
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
|
7607 |
"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
|
7608 |
"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
|
7609 |
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
|
7610 |
|
40652 | 7611 |
lemma list_ex1_simps [simp, code]: |
7612 |
"list_ex1 P [] = False" |
|
7613 |
"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
|
7614 |
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
|
7615 |
|
51b2f3412a03
attribute code_abbrev superseedes code_unfold_post; tuned text
haftmann
parents:
45993
diff
changeset
|
7616 |
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
|
7617 |
"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
|
7618 |
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
|
7619 |
|
46030
51b2f3412a03
attribute code_abbrev superseedes code_unfold_post; tuned text
haftmann
parents:
45993
diff
changeset
|
7620 |
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
|
7621 |
"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
|
7622 |
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
|
7623 |
|
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
7624 |
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
|
7625 |
"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
|
7626 |
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
|
7627 |
|
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
7628 |
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
|
7629 |
"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
|
7630 |
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
|
7631 |
|
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
7632 |
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
|
7633 |
"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
|
7634 |
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
|
7635 |
|
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
7636 |
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
|
7637 |
"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
|
7638 |
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
|
7639 |
|
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
7640 |
lemma list_all_length: |
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
7641 |
"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
|
7642 |
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
|
7643 |
|
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
7644 |
lemma list_ex_length: |
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
7645 |
"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
|
7646 |
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
|
7647 |
|
62328 | 7648 |
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
|
7649 |
|
47131 | 7650 |
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
|
7651 |
"xs = ys \<Longrightarrow> (\<And>x. x \<in> set ys \<Longrightarrow> f x = g x) \<Longrightarrow> list_ex f xs = list_ex g ys" |
47131 | 7652 |
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
|
7653 |
|
50548 | 7654 |
definition can_select :: "('a \<Rightarrow> bool) \<Rightarrow> 'a set \<Rightarrow> bool" where |
7655 |
[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
|
7656 |
|
744934b818c7
moved quite generic material from theory Enum to more appropriate places
haftmann
parents:
49808
diff
changeset
|
7657 |
lemma can_select_set_list_ex1 [code]: |
744934b818c7
moved quite generic material from theory Enum to more appropriate places
haftmann
parents:
49808
diff
changeset
|
7658 |
"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
|
7659 |
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
|
7660 |
|
744934b818c7
moved quite generic material from theory Enum to more appropriate places
haftmann
parents:
49808
diff
changeset
|
7661 |
|
60758 | 7662 |
text \<open>Executable checks for relations on sets\<close> |
46313 | 7663 |
|
7664 |
definition listrel1p :: "('a \<Rightarrow> 'a \<Rightarrow> bool) \<Rightarrow> 'a list \<Rightarrow> 'a list \<Rightarrow> bool" where |
|
7665 |
"listrel1p r xs ys = ((xs, ys) \<in> listrel1 {(x, y). r x y})" |
|
7666 |
||
7667 |
lemma [code_unfold]: |
|
7668 |
"(xs, ys) \<in> listrel1 r = listrel1p (\<lambda>x y. (x, y) \<in> r) xs ys" |
|
7669 |
unfolding listrel1p_def by auto |
|
7670 |
||
7671 |
lemma [code]: |
|
7672 |
"listrel1p r [] xs = False" |
|
7673 |
"listrel1p r xs [] = False" |
|
7674 |
"listrel1p r (x # xs) (y # ys) \<longleftrightarrow> |
|
7675 |
r x y \<and> xs = ys \<or> x = y \<and> listrel1p r xs ys" |
|
7676 |
by (simp add: listrel1p_def)+ |
|
7677 |
||
7678 |
definition |
|
7679 |
lexordp :: "('a \<Rightarrow> 'a \<Rightarrow> bool) \<Rightarrow> 'a list \<Rightarrow> 'a list \<Rightarrow> bool" where |
|
7680 |
"lexordp r xs ys = ((xs, ys) \<in> lexord {(x, y). r x y})" |
|
7681 |
||
7682 |
lemma [code_unfold]: |
|
7683 |
"(xs, ys) \<in> lexord r = lexordp (\<lambda>x y. (x, y) \<in> r) xs ys" |
|
7684 |
unfolding lexordp_def by auto |
|
7685 |
||
7686 |
lemma [code]: |
|
7687 |
"lexordp r xs [] = False" |
|
7688 |
"lexordp r [] (y#ys) = True" |
|
72184 | 7689 |
"lexordp r (x # xs) (y # ys) = (r x y \<or> (x = y \<and> lexordp r xs ys))" |
7690 |
unfolding lexordp_def by auto |
|
46313 | 7691 |
|
60758 | 7692 |
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
|
7693 |
|
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
7694 |
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
|
7695 |
"{..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
|
7696 |
by auto |
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
7697 |
|
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
7698 |
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
|
7699 |
"{..<n} = set [0..<n]" |
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
7700 |
by auto |
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
7701 |
|
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
7702 |
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
|
7703 |
"{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
|
7704 |
by auto |
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
7705 |
|
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
7706 |
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
|
7707 |
|
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
7708 |
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
|
7709 |
"{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
|
7710 |
by auto |
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
7711 |
|
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
7712 |
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
|
7713 |
"{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
|
7714 |
by auto |
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
7715 |
|
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
7716 |
lemma all_nat_less_eq [code_unfold]: |
61076 | 7717 |
"(\<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
|
7718 |
by auto |
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
7719 |
|
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
7720 |
lemma ex_nat_less_eq [code_unfold]: |
61076 | 7721 |
"(\<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
|
7722 |
by auto |
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
7723 |
|
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
7724 |
lemma all_nat_less [code_unfold]: |
61076 | 7725 |
"(\<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
|
7726 |
by auto |
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
7727 |
|
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
7728 |
lemma ex_nat_less [code_unfold]: |
61076 | 7729 |
"(\<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
|
7730 |
by auto |
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
7731 |
|
61799 | 7732 |
text\<open>Bounded \<open>LEAST\<close> operator:\<close> |
53954 | 7733 |
|
7734 |
definition "Bleast S P = (LEAST x. x \<in> S \<and> P x)" |
|
7735 |
||
7736 |
definition "abort_Bleast S P = (LEAST x. x \<in> S \<and> P x)" |
|
7737 |
||
54890
cb892d835803
fundamental treatment of undefined vs. universally partial replaces code_abort
haftmann
parents:
54885
diff
changeset
|
7738 |
declare [[code abort: abort_Bleast]] |
53954 | 7739 |
|
7740 |
lemma Bleast_code [code]: |
|
7741 |
"Bleast (set xs) P = (case filter P (sort xs) of |
|
7742 |
x#xs \<Rightarrow> x | |
|
7743 |
[] \<Rightarrow> abort_Bleast (set xs) P)" |
|
7744 |
proof (cases "filter P (sort xs)") |
|
7745 |
case Nil thus ?thesis by (simp add: Bleast_def abort_Bleast_def) |
|
7746 |
next |
|
7747 |
case (Cons x ys) |
|
7748 |
have "(LEAST x. x \<in> set xs \<and> P x) = x" |
|
7749 |
proof (rule Least_equality) |
|
7750 |
show "x \<in> set xs \<and> P x" |
|
7751 |
by (metis Cons Cons_eq_filter_iff in_set_conv_decomp set_sort) |
|
7752 |
next |
|
67613 | 7753 |
fix y assume "y \<in> set xs \<and> P y" |
7754 |
hence "y \<in> set (filter P xs)" by auto |
|
53954 | 7755 |
thus "x \<le> y" |
73707 | 7756 |
by (metis Cons eq_iff filter_sort set_ConsD set_sort sorted_wrt.simps(2) sorted_sort) |
53954 | 7757 |
qed |
7758 |
thus ?thesis using Cons by (simp add: Bleast_def) |
|
7759 |
qed |
|
7760 |
||
7761 |
declare Bleast_def[symmetric, code_unfold] |
|
7762 |
||
60758 | 7763 |
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
|
7764 |
|
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
7765 |
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
|
7766 |
"{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
|
7767 |
by auto |
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
7768 |
|
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
7769 |
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
|
7770 |
"{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
|
7771 |
by auto |
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
7772 |
|
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
7773 |
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
|
7774 |
"{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
|
7775 |
by auto |
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
7776 |
|
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
7777 |
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
|
7778 |
|
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
7779 |
|
60758 | 7780 |
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
|
7781 |
|
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
7782 |
definition null :: "'a list \<Rightarrow> bool" where |
46030
51b2f3412a03
attribute code_abbrev superseedes code_unfold_post; tuned text
haftmann
parents:
45993
diff
changeset
|
7783 |
[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
|
7784 |
|
60758 | 7785 |
text \<open> |
69593 | 7786 |
Efficient emptyness check is implemented by \<^const>\<open>null\<close>. |
60758 | 7787 |
\<close> |
37605
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
7788 |
|
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
7789 |
lemma null_rec [code]: |
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
7790 |
"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
|
7791 |
"null [] \<longleftrightarrow> True" |
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
7792 |
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
|
7793 |
|
46030
51b2f3412a03
attribute code_abbrev superseedes code_unfold_post; tuned text
haftmann
parents:
45993
diff
changeset
|
7794 |
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
|
7795 |
"xs = [] \<longleftrightarrow> null xs" |
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
7796 |
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
|
7797 |
|
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
7798 |
lemma equal_Nil_null [code_unfold]: |
38857
97775f3e8722
renamed class/constant eq to equal; tuned some instantiations
haftmann
parents:
38715
diff
changeset
|
7799 |
"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
|
7800 |
"HOL.equal [] = null" |
36cf426cb1c6
Added symmetric code_unfold-lemmas for null and is_none
lammich <lammich@in.tum.de>
parents:
53721
diff
changeset
|
7801 |
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
|
7802 |
|
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
7803 |
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
|
7804 |
[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
|
7805 |
|
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
7806 |
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
|
7807 |
[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
|
7808 |
|
60758 | 7809 |
text \<open> |
69593 | 7810 |
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
|
7811 |
intermediate lists on execution -- do not use for proving. |
60758 | 7812 |
\<close> |
37605
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
7813 |
|
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
7814 |
lemma maps_simps [code]: |
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
7815 |
"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
|
7816 |
"maps f [] = []" |
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
7817 |
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
|
7818 |
|
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
7819 |
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
|
7820 |
"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
|
7821 |
"map_filter f [] = []" |
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
7822 |
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
|
7823 |
|
46030
51b2f3412a03
attribute code_abbrev superseedes code_unfold_post; tuned text
haftmann
parents:
45993
diff
changeset
|
7824 |
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
|
7825 |
"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
|
7826 |
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
|
7827 |
|
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
7828 |
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
|
7829 |
"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
|
7830 |
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
|
7831 |
|
61799 | 7832 |
text \<open>Optimized code for \<open>\<forall>i\<in>{a..b::int}\<close> and \<open>\<forall>n:{a..<b::nat}\<close> |
7833 |
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
|
7834 |
|
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
7835 |
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
|
7836 |
"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
|
7837 |
|
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
7838 |
lemma [code]: |
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
7839 |
"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
|
7840 |
proof - |
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
7841 |
have *: "\<And>n. P i \<Longrightarrow> \<forall>n\<in>{Suc i..<j}. P n \<Longrightarrow> i \<le> n \<Longrightarrow> n < j \<Longrightarrow> P n" |
77433 | 7842 |
using le_less_Suc_eq by fastforce |
37605
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
7843 |
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
|
7844 |
qed |
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
7845 |
|
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
7846 |
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
|
7847 |
"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
|
7848 |
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
|
7849 |
|
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
7850 |
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
|
7851 |
"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
|
7852 |
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
|
7853 |
|
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
7854 |
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
|
7855 |
"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
|
7856 |
|
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
7857 |
lemma [code]: |
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
7858 |
"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
|
7859 |
proof - |
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
7860 |
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" |
77433 | 7861 |
by (smt (verit, best) atLeastAtMost_iff) |
37605
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
7862 |
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
|
7863 |
qed |
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
7864 |
|
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
7865 |
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
|
7866 |
"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
|
7867 |
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
|
7868 |
|
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
7869 |
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
|
7870 |
"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
|
7871 |
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
|
7872 |
|
69593 | 7873 |
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
|
7874 |
|
418991ce7567
tail-recursive implementation for length
Andreas Lochbihler
parents:
49757
diff
changeset
|
7875 |
definition gen_length :: "nat \<Rightarrow> 'a list \<Rightarrow> nat" |
418991ce7567
tail-recursive implementation for length
Andreas Lochbihler
parents:
49757
diff
changeset
|
7876 |
where "gen_length n xs = n + length xs" |
418991ce7567
tail-recursive implementation for length
Andreas Lochbihler
parents:
49757
diff
changeset
|
7877 |
|
418991ce7567
tail-recursive implementation for length
Andreas Lochbihler
parents:
49757
diff
changeset
|
7878 |
lemma gen_length_code [code]: |
418991ce7567
tail-recursive implementation for length
Andreas Lochbihler
parents:
49757
diff
changeset
|
7879 |
"gen_length n [] = n" |
418991ce7567
tail-recursive implementation for length
Andreas Lochbihler
parents:
49757
diff
changeset
|
7880 |
"gen_length n (x # xs) = gen_length (Suc n) xs" |
418991ce7567
tail-recursive implementation for length
Andreas Lochbihler
parents:
49757
diff
changeset
|
7881 |
by(simp_all add: gen_length_def) |
418991ce7567
tail-recursive implementation for length
Andreas Lochbihler
parents:
49757
diff
changeset
|
7882 |
|
418991ce7567
tail-recursive implementation for length
Andreas Lochbihler
parents:
49757
diff
changeset
|
7883 |
declare list.size(3-4)[code del] |
418991ce7567
tail-recursive implementation for length
Andreas Lochbihler
parents:
49757
diff
changeset
|
7884 |
|
418991ce7567
tail-recursive implementation for length
Andreas Lochbihler
parents:
49757
diff
changeset
|
7885 |
lemma length_code [code]: "length = gen_length 0" |
418991ce7567
tail-recursive implementation for length
Andreas Lochbihler
parents:
49757
diff
changeset
|
7886 |
by(simp add: gen_length_def fun_eq_iff) |
418991ce7567
tail-recursive implementation for length
Andreas Lochbihler
parents:
49757
diff
changeset
|
7887 |
|
418991ce7567
tail-recursive implementation for length
Andreas Lochbihler
parents:
49757
diff
changeset
|
7888 |
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
|
7889 |
|
49948
744934b818c7
moved quite generic material from theory Enum to more appropriate places
haftmann
parents:
49808
diff
changeset
|
7890 |
|
60758 | 7891 |
subsubsection \<open>Pretty lists\<close> |
7892 |
||
7893 |
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
|
7894 |
(* 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
|
7895 |
|
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
|
7896 |
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
|
7897 |
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
|
7898 |
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
|
7899 |
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
|
7900 |
|
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
|
7901 |
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
|
7902 |
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
|
7903 |
|
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
|
7904 |
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
|
7905 |
|
55148
7e1b7cb54114
avoid (now superfluous) indirect passing of constant names
haftmann
parents:
55147
diff
changeset
|
7906 |
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
|
7907 |
let |
69593 | 7908 |
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
|
7909 |
| 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
|
7910 |
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
|
7911 |
in case t' |
69593 | 7912 |
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
|
7913 |
| _ => 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
|
7914 |
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
|
7915 |
|
68028 | 7916 |
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
|
7917 |
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
|
7918 |
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
|
7919 |
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
|
7920 |
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
|
7921 |
); |
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
|
7922 |
|
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
|
7923 |
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
|
7924 |
let |
55148
7e1b7cb54114
avoid (now superfluous) indirect passing of constant names
haftmann
parents:
55147
diff
changeset
|
7925 |
fun pretty literals pr _ vars fxy [(t1, _), (t2, _)] = |
7e1b7cb54114
avoid (now superfluous) indirect passing of constant names
haftmann
parents:
55147
diff
changeset
|
7926 |
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
|
7927 |
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
|
7928 |
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
|
7929 |
| NONE => |
68028 | 7930 |
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
|
7931 |
in |
69593 | 7932 |
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
|
7933 |
[(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
|
7934 |
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
|
7935 |
|
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
|
7936 |
end; |
60758 | 7937 |
\<close> |
31055
2cf6efca6c71
proper structures for list and string code generation stuff
haftmann
parents:
31048
diff
changeset
|
7938 |
|
52435
6646bb548c6b
migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
haftmann
parents:
52380
diff
changeset
|
7939 |
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
|
7940 |
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
|
7941 |
(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
|
7942 |
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
|
7943 |
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
|
7944 |
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
|
7945 |
| 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
|
7946 |
(SML) "[]" |
6646bb548c6b
migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
haftmann
parents:
52380
diff
changeset
|
7947 |
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
|
7948 |
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
|
7949 |
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
|
7950 |
| 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
|
7951 |
(Haskell) - |
6646bb548c6b
migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
haftmann
parents:
52380
diff
changeset
|
7952 |
| 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
|
7953 |
(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
|
7954 |
|
60758 | 7955 |
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
|
7956 |
|
ac146fc38b51
refined HOL string theories and corresponding ML fragments
haftmann
parents:
31022
diff
changeset
|
7957 |
code_reserved SML |
ac146fc38b51
refined HOL string theories and corresponding ML fragments
haftmann
parents:
31022
diff
changeset
|
7958 |
list |
ac146fc38b51
refined HOL string theories and corresponding ML fragments
haftmann
parents:
31022
diff
changeset
|
7959 |
|
ac146fc38b51
refined HOL string theories and corresponding ML fragments
haftmann
parents:
31022
diff
changeset
|
7960 |
code_reserved OCaml |
ac146fc38b51
refined HOL string theories and corresponding ML fragments
haftmann
parents:
31022
diff
changeset
|
7961 |
list |
ac146fc38b51
refined HOL string theories and corresponding ML fragments
haftmann
parents:
31022
diff
changeset
|
7962 |
|
21061
580dfc999ef6
added normal post setup; cleaned up "execution" constants
haftmann
parents:
21046
diff
changeset
|
7963 |
|
60758 | 7964 |
subsubsection \<open>Use convenient predefined operations\<close> |
37424
ed431cc99f17
use various predefined Haskell operations when generating code
haftmann
parents:
37408
diff
changeset
|
7965 |
|
52435
6646bb548c6b
migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
haftmann
parents:
52380
diff
changeset
|
7966 |
code_printing |
67399 | 7967 |
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
|
7968 |
(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
|
7969 |
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
|
7970 |
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
|
7971 |
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
|
7972 |
| 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
|
7973 |
(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
|
7974 |
| 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
|
7975 |
(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
|
7976 |
| 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
|
7977 |
(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
|
7978 |
| 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
|
7979 |
(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
|
7980 |
| 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
|
7981 |
(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
|
7982 |
| 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
|
7983 |
(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
|
7984 |
| 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
|
7985 |
(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
|
7986 |
| 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
|
7987 |
(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
|
7988 |
| 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
|
7989 |
(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
|
7990 |
| 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
|
7991 |
(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
|
7992 |
| 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
|
7993 |
(Haskell) "any" |
37605
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
haftmann
parents:
37465
diff
changeset
|
7994 |
|
46147
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
haftmann
parents:
46143
diff
changeset
|
7995 |
|
60758 | 7996 |
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
|
7997 |
|
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
haftmann
parents:
46143
diff
changeset
|
7998 |
lemma is_empty_set [code]: |
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
haftmann
parents:
46143
diff
changeset
|
7999 |
"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
|
8000 |
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
|
8001 |
|
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
haftmann
parents:
46143
diff
changeset
|
8002 |
lemma empty_set [code]: |
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
haftmann
parents:
46143
diff
changeset
|
8003 |
"{} = set []" |
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
haftmann
parents:
46143
diff
changeset
|
8004 |
by simp |
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
haftmann
parents:
46143
diff
changeset
|
8005 |
|
46156 | 8006 |
lemma UNIV_coset [code]: |
8007 |
"UNIV = List.coset []" |
|
8008 |
by simp |
|
8009 |
||
8010 |
lemma compl_set [code]: |
|
8011 |
"- set xs = List.coset xs" |
|
8012 |
by simp |
|
8013 |
||
8014 |
lemma compl_coset [code]: |
|
8015 |
"- List.coset xs = set xs" |
|
8016 |
by simp |
|
8017 |
||
46147
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
haftmann
parents:
46143
diff
changeset
|
8018 |
lemma [code]: |
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
haftmann
parents:
46143
diff
changeset
|
8019 |
"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
|
8020 |
"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
|
8021 |
by (simp_all add: member_def) |
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
haftmann
parents:
46143
diff
changeset
|
8022 |
|
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
haftmann
parents:
46143
diff
changeset
|
8023 |
lemma insert_code [code]: |
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
haftmann
parents:
46143
diff
changeset
|
8024 |
"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
|
8025 |
"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
|
8026 |
by simp_all |
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
haftmann
parents:
46143
diff
changeset
|
8027 |
|
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
haftmann
parents:
46143
diff
changeset
|
8028 |
lemma remove_code [code]: |
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
haftmann
parents:
46143
diff
changeset
|
8029 |
"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
|
8030 |
"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
|
8031 |
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
|
8032 |
|
49757
73ab6d4a9236
rename Set.project to Set.filter - more appropriate name
kuncar
parents:
49739
diff
changeset
|
8033 |
lemma filter_set [code]: |
73ab6d4a9236
rename Set.project to Set.filter - more appropriate name
kuncar
parents:
49739
diff
changeset
|
8034 |
"Set.filter P (set xs) = set (filter P xs)" |
46156 | 8035 |
by auto |
8036 |
||
8037 |
lemma image_set [code]: |
|
8038 |
"image f (set xs) = set (map f xs)" |
|
8039 |
by simp |
|
8040 |
||
47398 | 8041 |
lemma subset_code [code]: |
8042 |
"set xs \<le> B \<longleftrightarrow> (\<forall>x\<in>set xs. x \<in> B)" |
|
8043 |
"A \<le> List.coset ys \<longleftrightarrow> (\<forall>y\<in>set ys. y \<notin> A)" |
|
68709 | 8044 |
"List.coset [] \<subseteq> set [] \<longleftrightarrow> False" |
47398 | 8045 |
by auto |
8046 |
||
60758 | 8047 |
text \<open>A frequent case -- avoid intermediate sets\<close> |
47398 | 8048 |
lemma [code_unfold]: |
8049 |
"set xs \<subseteq> set ys \<longleftrightarrow> list_all (\<lambda>x. x \<in> set ys) xs" |
|
8050 |
by (auto simp: list_all_iff) |
|
8051 |
||
46147
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
haftmann
parents:
46143
diff
changeset
|
8052 |
lemma Ball_set [code]: |
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
haftmann
parents:
46143
diff
changeset
|
8053 |
"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
|
8054 |
by (simp add: list_all_iff) |
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
haftmann
parents:
46143
diff
changeset
|
8055 |
|
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
haftmann
parents:
46143
diff
changeset
|
8056 |
lemma Bex_set [code]: |
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
haftmann
parents:
46143
diff
changeset
|
8057 |
"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
|
8058 |
by (simp add: list_ex_iff) |
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
haftmann
parents:
46143
diff
changeset
|
8059 |
|
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
haftmann
parents:
46143
diff
changeset
|
8060 |
lemma card_set [code]: |
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
haftmann
parents:
46143
diff
changeset
|
8061 |
"card (set xs) = length (remdups xs)" |
77433 | 8062 |
by (simp add: length_remdups_card_conv) |
46147
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
haftmann
parents:
46143
diff
changeset
|
8063 |
|
46156 | 8064 |
lemma the_elem_set [code]: |
8065 |
"the_elem (set [x]) = x" |
|
8066 |
by simp |
|
8067 |
||
8068 |
lemma Pow_set [code]: |
|
8069 |
"Pow (set []) = {{}}" |
|
8070 |
"Pow (set (x # xs)) = (let A = Pow (set xs) in A \<union> insert x ` A)" |
|
8071 |
by (simp_all add: Pow_insert Let_def) |
|
8072 |
||
46424
b447318e5e1a
adding code equation for Relation.image; adding map_project as correspondence to map_filter on lists
bulwahn
parents:
46418
diff
changeset
|
8073 |
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
|
8074 |
"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
|
8075 |
|
b447318e5e1a
adding code equation for Relation.image; adding map_project as correspondence to map_filter on lists
bulwahn
parents:
46418
diff
changeset
|
8076 |
lemma [code]: |
b447318e5e1a
adding code equation for Relation.image; adding map_project as correspondence to map_filter on lists
bulwahn
parents:
46418
diff
changeset
|
8077 |
"map_project f (set xs) = set (List.map_filter f xs)" |
47398 | 8078 |
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
|
8079 |
|
b447318e5e1a
adding code equation for Relation.image; adding map_project as correspondence to map_filter on lists
bulwahn
parents:
46418
diff
changeset
|
8080 |
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
|
8081 |
|
49948
744934b818c7
moved quite generic material from theory Enum to more appropriate places
haftmann
parents:
49808
diff
changeset
|
8082 |
|
60758 | 8083 |
text \<open>Operations on relations\<close> |
46147
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
haftmann
parents:
46143
diff
changeset
|
8084 |
|
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
haftmann
parents:
46143
diff
changeset
|
8085 |
lemma product_code [code]: |
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
haftmann
parents:
46143
diff
changeset
|
8086 |
"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
|
8087 |
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
|
8088 |
|
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
haftmann
parents:
46143
diff
changeset
|
8089 |
lemma Id_on_set [code]: |
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
haftmann
parents:
46143
diff
changeset
|
8090 |
"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
|
8091 |
by (auto simp add: Id_on_def) |
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
haftmann
parents:
46143
diff
changeset
|
8092 |
|
46424
b447318e5e1a
adding code equation for Relation.image; adding map_project as correspondence to map_filter on lists
bulwahn
parents:
46418
diff
changeset
|
8093 |
lemma [code]: |
67613 | 8094 |
"R `` S = List.map_project (\<lambda>(x, y). if x \<in> S then Some y else None) R" |
62390 | 8095 |
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
|
8096 |
|
46147
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
haftmann
parents:
46143
diff
changeset
|
8097 |
lemma trancl_set_ntrancl [code]: |
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
haftmann
parents:
46143
diff
changeset
|
8098 |
"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
|
8099 |
by (simp add: finite_trancl_ntranl) |
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
haftmann
parents:
46143
diff
changeset
|
8100 |
|
47433
07f4bf913230
renamed "rel_comp" to "relcomp" (to be consistent with, e.g., "relpow")
griff
parents:
47131
diff
changeset
|
8101 |
lemma set_relcomp [code]: |
46147
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
haftmann
parents:
46143
diff
changeset
|
8102 |
"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
|
8103 |
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
|
8104 |
|
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
haftmann
parents:
46143
diff
changeset
|
8105 |
lemma wf_set [code]: |
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
haftmann
parents:
46143
diff
changeset
|
8106 |
"wf (set xs) = acyclic (set xs)" |
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
haftmann
parents:
46143
diff
changeset
|
8107 |
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
|
8108 |
|
55129
26bd1cba3ab5
killed 'More_BNFs' by moving its various bits where they (now) belong
blanchet
parents:
54890
diff
changeset
|
8109 |
|
60758 | 8110 |
subsection \<open>Setup for Lifting/Transfer\<close> |
8111 |
||
8112 |
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
|
8113 |
|
63343 | 8114 |
context includes lifting_syntax |
53012
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
kuncar
parents:
52435
diff
changeset
|
8115 |
begin |
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
kuncar
parents:
52435
diff
changeset
|
8116 |
|
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
kuncar
parents:
52435
diff
changeset
|
8117 |
lemma tl_transfer [transfer_rule]: |
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
kuncar
parents:
52435
diff
changeset
|
8118 |
"(list_all2 A ===> list_all2 A) tl tl" |
55405
0a155051bd9d
use new selector support to define 'the', 'hd', 'tl'
blanchet
parents:
55404
diff
changeset
|
8119 |
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
|
8120 |
|
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
kuncar
parents:
52435
diff
changeset
|
8121 |
lemma butlast_transfer [transfer_rule]: |
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
kuncar
parents:
52435
diff
changeset
|
8122 |
"(list_all2 A ===> list_all2 A) butlast butlast" |
55945 | 8123 |
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
|
8124 |
|
55465 | 8125 |
lemma map_rec: "map f xs = rec_list Nil (%x _ y. Cons (f x) y) xs" |
8126 |
by (induct xs) auto |
|
8127 |
||
53012
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
kuncar
parents:
52435
diff
changeset
|
8128 |
lemma append_transfer [transfer_rule]: |
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
kuncar
parents:
52435
diff
changeset
|
8129 |
"(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
|
8130 |
unfolding List.append_def by transfer_prover |
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
kuncar
parents:
52435
diff
changeset
|
8131 |
|
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
kuncar
parents:
52435
diff
changeset
|
8132 |
lemma rev_transfer [transfer_rule]: |
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
kuncar
parents:
52435
diff
changeset
|
8133 |
"(list_all2 A ===> list_all2 A) rev rev" |
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
kuncar
parents:
52435
diff
changeset
|
8134 |
unfolding List.rev_def by transfer_prover |
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
kuncar
parents:
52435
diff
changeset
|
8135 |
|
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
kuncar
parents:
52435
diff
changeset
|
8136 |
lemma filter_transfer [transfer_rule]: |
67399 | 8137 |
"((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
|
8138 |
unfolding List.filter_def by transfer_prover |
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
kuncar
parents:
52435
diff
changeset
|
8139 |
|
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
kuncar
parents:
52435
diff
changeset
|
8140 |
lemma fold_transfer [transfer_rule]: |
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
kuncar
parents:
52435
diff
changeset
|
8141 |
"((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
|
8142 |
unfolding List.fold_def by transfer_prover |
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
kuncar
parents:
52435
diff
changeset
|
8143 |
|
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
kuncar
parents:
52435
diff
changeset
|
8144 |
lemma foldr_transfer [transfer_rule]: |
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
kuncar
parents:
52435
diff
changeset
|
8145 |
"((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
|
8146 |
unfolding List.foldr_def by transfer_prover |
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
kuncar
parents:
52435
diff
changeset
|
8147 |
|
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
kuncar
parents:
52435
diff
changeset
|
8148 |
lemma foldl_transfer [transfer_rule]: |
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
kuncar
parents:
52435
diff
changeset
|
8149 |
"((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
|
8150 |
unfolding List.foldl_def by transfer_prover |
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
kuncar
parents:
52435
diff
changeset
|
8151 |
|
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
kuncar
parents:
52435
diff
changeset
|
8152 |
lemma concat_transfer [transfer_rule]: |
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
kuncar
parents:
52435
diff
changeset
|
8153 |
"(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
|
8154 |
unfolding List.concat_def by transfer_prover |
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
kuncar
parents:
52435
diff
changeset
|
8155 |
|
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
kuncar
parents:
52435
diff
changeset
|
8156 |
lemma drop_transfer [transfer_rule]: |
67399 | 8157 |
"((=) ===> 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
|
8158 |
unfolding List.drop_def by transfer_prover |
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
kuncar
parents:
52435
diff
changeset
|
8159 |
|
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
kuncar
parents:
52435
diff
changeset
|
8160 |
lemma take_transfer [transfer_rule]: |
67399 | 8161 |
"((=) ===> 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
|
8162 |
unfolding List.take_def by transfer_prover |
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
kuncar
parents:
52435
diff
changeset
|
8163 |
|
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
kuncar
parents:
52435
diff
changeset
|
8164 |
lemma list_update_transfer [transfer_rule]: |
67399 | 8165 |
"(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
|
8166 |
unfolding list_update_def by transfer_prover |
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
kuncar
parents:
52435
diff
changeset
|
8167 |
|
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
kuncar
parents:
52435
diff
changeset
|
8168 |
lemma takeWhile_transfer [transfer_rule]: |
67399 | 8169 |
"((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
|
8170 |
unfolding takeWhile_def by transfer_prover |
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
kuncar
parents:
52435
diff
changeset
|
8171 |
|
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
kuncar
parents:
52435
diff
changeset
|
8172 |
lemma dropWhile_transfer [transfer_rule]: |
67399 | 8173 |
"((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
|
8174 |
unfolding dropWhile_def by transfer_prover |
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
kuncar
parents:
52435
diff
changeset
|
8175 |
|
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
kuncar
parents:
52435
diff
changeset
|
8176 |
lemma zip_transfer [transfer_rule]: |
55944 | 8177 |
"(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
|
8178 |
unfolding zip_def by transfer_prover |
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
kuncar
parents:
52435
diff
changeset
|
8179 |
|
53721
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
traytel
parents:
53689
diff
changeset
|
8180 |
lemma product_transfer [transfer_rule]: |
55944 | 8181 |
"(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
|
8182 |
unfolding List.product_def by transfer_prover |
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
traytel
parents:
53689
diff
changeset
|
8183 |
|
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
traytel
parents:
53689
diff
changeset
|
8184 |
lemma product_lists_transfer [transfer_rule]: |
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
traytel
parents:
53689
diff
changeset
|
8185 |
"(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
|
8186 |
unfolding product_lists_def by transfer_prover |
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
traytel
parents:
53689
diff
changeset
|
8187 |
|
53012
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
kuncar
parents:
52435
diff
changeset
|
8188 |
lemma insert_transfer [transfer_rule]: |
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
kuncar
parents:
52435
diff
changeset
|
8189 |
assumes [transfer_rule]: "bi_unique A" |
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
kuncar
parents:
52435
diff
changeset
|
8190 |
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
|
8191 |
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
|
8192 |
|
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
kuncar
parents:
52435
diff
changeset
|
8193 |
lemma find_transfer [transfer_rule]: |
67399 | 8194 |
"((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
|
8195 |
unfolding List.find_def by transfer_prover |
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
kuncar
parents:
52435
diff
changeset
|
8196 |
|
63521
32da860241b8
added missing transfer rule
Lars Hupel <lars.hupel@mytum.de>
parents:
63365
diff
changeset
|
8197 |
lemma those_transfer [transfer_rule]: |
32da860241b8
added missing transfer rule
Lars Hupel <lars.hupel@mytum.de>
parents:
63365
diff
changeset
|
8198 |
"(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
|
8199 |
unfolding List.those_def by transfer_prover |
32da860241b8
added missing transfer rule
Lars Hupel <lars.hupel@mytum.de>
parents:
63365
diff
changeset
|
8200 |
|
53012
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
kuncar
parents:
52435
diff
changeset
|
8201 |
lemma remove1_transfer [transfer_rule]: |
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
kuncar
parents:
52435
diff
changeset
|
8202 |
assumes [transfer_rule]: "bi_unique A" |
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
kuncar
parents:
52435
diff
changeset
|
8203 |
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
|
8204 |
unfolding remove1_def by transfer_prover |
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
kuncar
parents:
52435
diff
changeset
|
8205 |
|
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
kuncar
parents:
52435
diff
changeset
|
8206 |
lemma removeAll_transfer [transfer_rule]: |
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
kuncar
parents:
52435
diff
changeset
|
8207 |
assumes [transfer_rule]: "bi_unique A" |
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
kuncar
parents:
52435
diff
changeset
|
8208 |
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
|
8209 |
unfolding removeAll_def by transfer_prover |
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
kuncar
parents:
52435
diff
changeset
|
8210 |
|
71779 | 8211 |
lemma successively_transfer [transfer_rule]: |
8212 |
"((A ===> A ===> (=)) ===> list_all2 A ===> (=)) successively successively" |
|
8213 |
unfolding successively_altdef by transfer_prover |
|
8214 |
||
53012
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
kuncar
parents:
52435
diff
changeset
|
8215 |
lemma distinct_transfer [transfer_rule]: |
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
kuncar
parents:
52435
diff
changeset
|
8216 |
assumes [transfer_rule]: "bi_unique A" |
67399 | 8217 |
shows "(list_all2 A ===> (=)) distinct distinct" |
53012
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
kuncar
parents:
52435
diff
changeset
|
8218 |
unfolding distinct_def by transfer_prover |
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
kuncar
parents:
52435
diff
changeset
|
8219 |
|
71779 | 8220 |
lemma distinct_adj_transfer [transfer_rule]: |
8221 |
assumes "bi_unique A" |
|
8222 |
shows "(list_all2 A ===> (=)) distinct_adj distinct_adj" |
|
8223 |
unfolding rel_fun_def |
|
8224 |
proof (intro allI impI) |
|
8225 |
fix xs ys assume "list_all2 A xs ys" |
|
8226 |
thus "distinct_adj xs \<longleftrightarrow> distinct_adj ys" |
|
8227 |
proof (induction rule: list_all2_induct) |
|
8228 |
case (Cons x xs y ys) |
|
8229 |
show ?case |
|
77433 | 8230 |
by (metis Cons assms bi_unique_def distinct_adj_Cons list.rel_sel) |
71779 | 8231 |
qed auto |
8232 |
qed |
|
8233 |
||
53012
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
kuncar
parents:
52435
diff
changeset
|
8234 |
lemma remdups_transfer [transfer_rule]: |
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
kuncar
parents:
52435
diff
changeset
|
8235 |
assumes [transfer_rule]: "bi_unique A" |
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
kuncar
parents:
52435
diff
changeset
|
8236 |
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
|
8237 |
unfolding remdups_def by transfer_prover |
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
kuncar
parents:
52435
diff
changeset
|
8238 |
|
53721
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
traytel
parents:
53689
diff
changeset
|
8239 |
lemma remdups_adj_transfer [transfer_rule]: |
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
traytel
parents:
53689
diff
changeset
|
8240 |
assumes [transfer_rule]: "bi_unique A" |
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
traytel
parents:
53689
diff
changeset
|
8241 |
shows "(list_all2 A ===> list_all2 A) remdups_adj remdups_adj" |
55945 | 8242 |
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
|
8243 |
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
|
8244 |
|
53012
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
kuncar
parents:
52435
diff
changeset
|
8245 |
lemma replicate_transfer [transfer_rule]: |
67399 | 8246 |
"((=) ===> A ===> list_all2 A) replicate replicate" |
53012
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
kuncar
parents:
52435
diff
changeset
|
8247 |
unfolding replicate_def by transfer_prover |
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
kuncar
parents:
52435
diff
changeset
|
8248 |
|
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
kuncar
parents:
52435
diff
changeset
|
8249 |
lemma length_transfer [transfer_rule]: |
67399 | 8250 |
"(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
|
8251 |
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
|
8252 |
|
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
kuncar
parents:
52435
diff
changeset
|
8253 |
lemma rotate1_transfer [transfer_rule]: |
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
kuncar
parents:
52435
diff
changeset
|
8254 |
"(list_all2 A ===> list_all2 A) rotate1 rotate1" |
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
kuncar
parents:
52435
diff
changeset
|
8255 |
unfolding rotate1_def by transfer_prover |
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
kuncar
parents:
52435
diff
changeset
|
8256 |
|
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
kuncar
parents:
52435
diff
changeset
|
8257 |
lemma rotate_transfer [transfer_rule]: |
67399 | 8258 |
"((=) ===> 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
|
8259 |
unfolding rotate_def [abs_def] by transfer_prover |
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
kuncar
parents:
52435
diff
changeset
|
8260 |
|
65956
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65350
diff
changeset
|
8261 |
lemma nths_transfer [transfer_rule]: |
67399 | 8262 |
"(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
|
8263 |
unfolding nths_def [abs_def] by transfer_prover |
66892 | 8264 |
|
65956
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65350
diff
changeset
|
8265 |
lemma subseqs_transfer [transfer_rule]: |
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65350
diff
changeset
|
8266 |
"(list_all2 A ===> list_all2 (list_all2 A)) subseqs subseqs" |
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65350
diff
changeset
|
8267 |
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
|
8268 |
|
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
kuncar
parents:
52435
diff
changeset
|
8269 |
lemma partition_transfer [transfer_rule]: |
67399 | 8270 |
"((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
|
8271 |
partition partition" |
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
kuncar
parents:
52435
diff
changeset
|
8272 |
unfolding partition_def by transfer_prover |
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
kuncar
parents:
52435
diff
changeset
|
8273 |
|
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
kuncar
parents:
52435
diff
changeset
|
8274 |
lemma lists_transfer [transfer_rule]: |
55938 | 8275 |
"(rel_set A ===> rel_set (list_all2 A)) lists lists" |
71813 | 8276 |
proof (rule rel_funI, rule rel_setI) |
8277 |
show "\<lbrakk>l \<in> lists X; rel_set A X Y\<rbrakk> \<Longrightarrow> \<exists>y\<in>lists Y. list_all2 A l y" for X Y l |
|
8278 |
proof (induction l rule: lists.induct) |
|
8279 |
case (Cons a l) |
|
8280 |
then show ?case |
|
8281 |
by (simp only: rel_set_def list_all2_Cons1, metis lists.Cons) |
|
8282 |
qed auto |
|
8283 |
show "\<lbrakk>l \<in> lists Y; rel_set A X Y\<rbrakk> \<Longrightarrow> \<exists>x\<in>lists X. list_all2 A x l" for X Y l |
|
8284 |
proof (induction l rule: lists.induct) |
|
8285 |
case (Cons a l) |
|
8286 |
then show ?case |
|
8287 |
by (simp only: rel_set_def list_all2_Cons2, metis lists.Cons) |
|
8288 |
qed auto |
|
8289 |
qed |
|
53012
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
kuncar
parents:
52435
diff
changeset
|
8290 |
|
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
kuncar
parents:
52435
diff
changeset
|
8291 |
lemma set_Cons_transfer [transfer_rule]: |
55938 | 8292 |
"(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
|
8293 |
set_Cons set_Cons" |
55945 | 8294 |
unfolding rel_fun_def rel_set_def set_Cons_def |
68719 | 8295 |
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
|
8296 |
|
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
kuncar
parents:
52435
diff
changeset
|
8297 |
lemma listset_transfer [transfer_rule]: |
55938 | 8298 |
"(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
|
8299 |
unfolding listset_def by transfer_prover |
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
kuncar
parents:
52435
diff
changeset
|
8300 |
|
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
kuncar
parents:
52435
diff
changeset
|
8301 |
lemma null_transfer [transfer_rule]: |
67399 | 8302 |
"(list_all2 A ===> (=)) List.null List.null" |
55945 | 8303 |
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
|
8304 |
|
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
kuncar
parents:
52435
diff
changeset
|
8305 |
lemma list_all_transfer [transfer_rule]: |
67399 | 8306 |
"((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
|
8307 |
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
|
8308 |
|
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
kuncar
parents:
52435
diff
changeset
|
8309 |
lemma list_ex_transfer [transfer_rule]: |
67399 | 8310 |
"((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
|
8311 |
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
|
8312 |
|
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
kuncar
parents:
52435
diff
changeset
|
8313 |
lemma splice_transfer [transfer_rule]: |
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
kuncar
parents:
52435
diff
changeset
|
8314 |
"(list_all2 A ===> list_all2 A ===> list_all2 A) splice splice" |
55945 | 8315 |
apply (rule rel_funI, erule list_all2_induct, simp add: rel_fun_def, simp) |
8316 |
apply (rule rel_funI) |
|
8317 |
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
|
8318 |
done |
66892 | 8319 |
|
69107 | 8320 |
lemma shuffles_transfer [transfer_rule]: |
8321 |
"(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
|
8322 |
proof (intro rel_funI, goal_cases) |
b149abe619f7
added shuffle product to HOL/List
eberlm <eberlm@in.tum.de>
parents:
64966
diff
changeset
|
8323 |
case (1 xs xs' ys ys') |
b149abe619f7
added shuffle product to HOL/List
eberlm <eberlm@in.tum.de>
parents:
64966
diff
changeset
|
8324 |
thus ?case |
69107 | 8325 |
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
|
8326 |
case (3 x xs y ys xs' ys') |
b149abe619f7
added shuffle product to HOL/List
eberlm <eberlm@in.tum.de>
parents:
64966
diff
changeset
|
8327 |
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
|
8328 |
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
|
8329 |
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
|
8330 |
using "3.prems" by (simp_all add: xs' ys') |
69107 | 8331 |
have [transfer_rule]: "rel_set (list_all2 A) (shuffles xs (y # ys)) (shuffles xs'' ys')" and |
8332 |
[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
|
8333 |
using "3.prems" by (auto intro!: "3.IH" simp: xs' ys') |
69107 | 8334 |
have "rel_set (list_all2 A) ((#) x ` shuffles xs (y # ys) \<union> (#) y ` shuffles (x # xs) ys) |
8335 |
((#) 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
|
8336 |
thus ?case by (simp add: xs' ys') |
b149abe619f7
added shuffle product to HOL/List
eberlm <eberlm@in.tum.de>
parents:
64966
diff
changeset
|
8337 |
qed (auto simp: rel_set_def) |
66892 | 8338 |
qed |
53012
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
kuncar
parents:
52435
diff
changeset
|
8339 |
|
57599 | 8340 |
lemma rtrancl_parametric [transfer_rule]: |
8341 |
assumes [transfer_rule]: "bi_unique A" "bi_total A" |
|
8342 |
shows "(rel_set (rel_prod A A) ===> rel_set (rel_prod A A)) rtrancl rtrancl" |
|
8343 |
unfolding rtrancl_def by transfer_prover |
|
8344 |
||
59516
d92b74f3f6e3
add parametricity rules for monotone, fun_lub, and fun_ord
Andreas Lochbihler
parents:
59199
diff
changeset
|
8345 |
lemma monotone_parametric [transfer_rule]: |
d92b74f3f6e3
add parametricity rules for monotone, fun_lub, and fun_ord
Andreas Lochbihler
parents:
59199
diff
changeset
|
8346 |
assumes [transfer_rule]: "bi_total A" |
67399 | 8347 |
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
|
8348 |
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
|
8349 |
|
d92b74f3f6e3
add parametricity rules for monotone, fun_lub, and fun_ord
Andreas Lochbihler
parents:
59199
diff
changeset
|
8350 |
lemma fun_ord_parametric [transfer_rule]: |
d92b74f3f6e3
add parametricity rules for monotone, fun_lub, and fun_ord
Andreas Lochbihler
parents:
59199
diff
changeset
|
8351 |
assumes [transfer_rule]: "bi_total C" |
67399 | 8352 |
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
|
8353 |
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
|
8354 |
|
d92b74f3f6e3
add parametricity rules for monotone, fun_lub, and fun_ord
Andreas Lochbihler
parents:
59199
diff
changeset
|
8355 |
lemma fun_lub_parametric [transfer_rule]: |
d92b74f3f6e3
add parametricity rules for monotone, fun_lub, and fun_ord
Andreas Lochbihler
parents:
59199
diff
changeset
|
8356 |
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
|
8357 |
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
|
8358 |
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
|
8359 |
|
23388 | 8360 |
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
|
8361 |
|
53012
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
kuncar
parents:
52435
diff
changeset
|
8362 |
end |