| author | Andreas Lochbihler | 
| Tue, 19 Aug 2014 15:19:16 +0200 | |
| changeset 57998 | 8b7508f848ef | 
| parent 57816 | d8bbb97689d3 | 
| child 58041 | 41ceac4450dc | 
| permissions | -rw-r--r-- | 
| 13462 | 1  | 
(* Title: HOL/List.thy  | 
2  | 
Author: Tobias Nipkow  | 
|
| 923 | 3  | 
*)  | 
4  | 
||
| 13114 | 5  | 
header {* The datatype of finite lists *}
 | 
| 13122 | 6  | 
|
| 15131 | 7  | 
theory List  | 
| 57243 | 8  | 
imports Sledgehammer Code_Numeral Lifting_Set Lifting_Option Lifting_Product  | 
| 15131 | 9  | 
begin  | 
| 923 | 10  | 
|
| 
57206
 
d9be905d6283
changed syntax of map: and rel: arguments to BNF-based datatypes
 
blanchet 
parents: 
57200 
diff
changeset
 | 
11  | 
datatype_new (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  | 
| 
57200
 
aab87ffa60cc
use 'where' clause for selector default value syntax
 
blanchet 
parents: 
57198 
diff
changeset
 | 
17  | 
where  | 
| 
 
aab87ffa60cc
use 'where' clause for selector default value syntax
 
blanchet 
parents: 
57198 
diff
changeset
 | 
18  | 
"tl [] = []"  | 
| 
57123
 
b5324647e0f1
tuned whitespace, to make datatype definitions slightly less intimidating
 
blanchet 
parents: 
57091 
diff
changeset
 | 
19  | 
|
| 
55531
 
601ca8efa000
renamed 'datatype_new_compat' to 'datatype_compat'
 
blanchet 
parents: 
55525 
diff
changeset
 | 
20  | 
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
 | 
21  | 
|
| 55406 | 22  | 
lemma [case_names Nil Cons, cases type: list]:  | 
23  | 
  -- {* for backward compatibility -- names of variables differ *}
 | 
|
24  | 
"(y = [] \<Longrightarrow> P) \<Longrightarrow> (\<And>a list. y = a # list \<Longrightarrow> P) \<Longrightarrow> P"  | 
|
25  | 
by (rule list.exhaust)  | 
|
26  | 
||
27  | 
lemma [case_names Nil Cons, induct type: list]:  | 
|
28  | 
  -- {* for backward compatibility -- names of variables differ *}
 | 
|
29  | 
"P [] \<Longrightarrow> (\<And>a list. P list \<Longrightarrow> P (a # list)) \<Longrightarrow> P list"  | 
|
30  | 
by (rule list.induct)  | 
|
31  | 
||
| 55442 | 32  | 
text {* Compatibility: *}
 | 
33  | 
||
| 
55404
 
5cb95b79a51f
transformed 'option' and 'list' into new-style datatypes (but register them as old-style as well)
 
blanchet 
parents: 
55148 
diff
changeset
 | 
34  | 
setup {* Sign.mandatory_path "list" *}
 | 
| 
 
5cb95b79a51f
transformed 'option' and 'list' into new-style datatypes (but register them as old-style as well)
 
blanchet 
parents: 
55148 
diff
changeset
 | 
35  | 
|
| 
 
5cb95b79a51f
transformed 'option' and 'list' into new-style datatypes (but register them as old-style as well)
 
blanchet 
parents: 
55148 
diff
changeset
 | 
36  | 
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
 | 
37  | 
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
 | 
38  | 
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
 | 
39  | 
|
| 
 
5cb95b79a51f
transformed 'option' and 'list' into new-style datatypes (but register them as old-style as well)
 
blanchet 
parents: 
55148 
diff
changeset
 | 
40  | 
setup {* Sign.parent_path *}
 | 
| 
 
5cb95b79a51f
transformed 'option' and 'list' into new-style datatypes (but register them as old-style as well)
 
blanchet 
parents: 
55148 
diff
changeset
 | 
41  | 
|
| 
57816
 
d8bbb97689d3
no need for 'set_simps' now that 'datatype_new' generates the desired 'set' property
 
blanchet 
parents: 
57599 
diff
changeset
 | 
42  | 
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
 | 
43  | 
|
| 34941 | 44  | 
syntax  | 
45  | 
  -- {* list Enumeration *}
 | 
|
| 35115 | 46  | 
  "_list" :: "args => 'a list"    ("[(_)]")
 | 
| 34941 | 47  | 
|
48  | 
translations  | 
|
49  | 
"[x, xs]" == "x#[xs]"  | 
|
50  | 
"[x]" == "x#[]"  | 
|
51  | 
||
| 35115 | 52  | 
|
53  | 
subsection {* Basic list processing functions *}
 | 
|
| 15302 | 54  | 
|
| 50548 | 55  | 
primrec last :: "'a list \<Rightarrow> 'a" where  | 
56  | 
"last (x # xs) = (if xs = [] then x else last xs)"  | 
|
57  | 
||
58  | 
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
 | 
59  | 
"butlast [] = []" |  | 
| 50548 | 60  | 
"butlast (x # xs) = (if xs = [] then [] else x # butlast xs)"  | 
61  | 
||
| 55584 | 62  | 
lemma set_rec: "set xs = rec_list {} (\<lambda>x _. insert x) xs"
 | 
63  | 
by (induct xs) auto  | 
|
| 50548 | 64  | 
|
65  | 
definition coset :: "'a list \<Rightarrow> 'a set" where  | 
|
66  | 
[simp]: "coset xs = - set xs"  | 
|
67  | 
||
68  | 
primrec append :: "'a list \<Rightarrow> 'a list \<Rightarrow> 'a list" (infixr "@" 65) where  | 
|
69  | 
append_Nil: "[] @ ys = ys" |  | 
|
70  | 
append_Cons: "(x#xs) @ ys = x # xs @ ys"  | 
|
71  | 
||
72  | 
primrec rev :: "'a list \<Rightarrow> 'a list" where  | 
|
73  | 
"rev [] = []" |  | 
|
74  | 
"rev (x # xs) = rev xs @ [x]"  | 
|
75  | 
||
76  | 
primrec filter:: "('a \<Rightarrow> bool) \<Rightarrow> 'a list \<Rightarrow> 'a list" where
 | 
|
77  | 
"filter P [] = []" |  | 
|
78  | 
"filter P (x # xs) = (if P x then x # filter P xs else filter P xs)"  | 
|
| 34941 | 79  | 
|
80  | 
syntax  | 
|
81  | 
  -- {* Special syntax for filter *}
 | 
|
| 35115 | 82  | 
  "_filter" :: "[pttrn, 'a list, bool] => 'a list"    ("(1[_<-_./ _])")
 | 
| 34941 | 83  | 
|
84  | 
translations  | 
|
85  | 
"[x<-xs . P]"== "CONST filter (%x. P) xs"  | 
|
86  | 
||
87  | 
syntax (xsymbols)  | 
|
| 35115 | 88  | 
  "_filter" :: "[pttrn, 'a list, bool] => 'a list"("(1[_\<leftarrow>_ ./ _])")
 | 
| 34941 | 89  | 
syntax (HTML output)  | 
| 35115 | 90  | 
  "_filter" :: "[pttrn, 'a list, bool] => 'a list"("(1[_\<leftarrow>_ ./ _])")
 | 
| 34941 | 91  | 
|
| 50548 | 92  | 
primrec fold :: "('a \<Rightarrow> 'b \<Rightarrow> 'b) \<Rightarrow> 'a list \<Rightarrow> 'b \<Rightarrow> 'b" where
 | 
93  | 
fold_Nil: "fold f [] = id" |  | 
|
94  | 
fold_Cons: "fold f (x # xs) = fold f xs \<circ> f x"  | 
|
95  | 
||
96  | 
primrec foldr :: "('a \<Rightarrow> 'b \<Rightarrow> 'b) \<Rightarrow> 'a list \<Rightarrow> 'b \<Rightarrow> 'b" where
 | 
|
97  | 
foldr_Nil: "foldr f [] = id" |  | 
|
98  | 
foldr_Cons: "foldr f (x # xs) = f x \<circ> foldr f xs"  | 
|
99  | 
||
100  | 
primrec foldl :: "('b \<Rightarrow> 'a \<Rightarrow> 'b) \<Rightarrow> 'b \<Rightarrow> 'a list \<Rightarrow> 'b" where
 | 
|
101  | 
foldl_Nil: "foldl f a [] = a" |  | 
|
102  | 
foldl_Cons: "foldl f a (x # xs) = foldl f (f a x) xs"  | 
|
103  | 
||
104  | 
primrec concat:: "'a list list \<Rightarrow> 'a list" where  | 
|
105  | 
"concat [] = []" |  | 
|
106  | 
"concat (x # xs) = x @ concat xs"  | 
|
107  | 
||
108  | 
definition (in monoid_add) listsum :: "'a list \<Rightarrow> 'a" where  | 
|
109  | 
"listsum xs = foldr plus xs 0"  | 
|
110  | 
||
111  | 
primrec drop:: "nat \<Rightarrow> 'a list \<Rightarrow> 'a list" where  | 
|
112  | 
drop_Nil: "drop n [] = []" |  | 
|
113  | 
drop_Cons: "drop n (x # xs) = (case n of 0 \<Rightarrow> x # xs | Suc m \<Rightarrow> drop m xs)"  | 
|
| 34941 | 114  | 
  -- {*Warning: simpset does not contain this definition, but separate
 | 
115  | 
       theorems for @{text "n = 0"} and @{text "n = Suc k"} *}
 | 
|
116  | 
||
| 50548 | 117  | 
primrec take:: "nat \<Rightarrow> 'a list \<Rightarrow> 'a list" where  | 
118  | 
take_Nil:"take n [] = []" |  | 
|
119  | 
take_Cons: "take n (x # xs) = (case n of 0 \<Rightarrow> [] | Suc m \<Rightarrow> x # take m xs)"  | 
|
| 34941 | 120  | 
  -- {*Warning: simpset does not contain this definition, but separate
 | 
121  | 
       theorems for @{text "n = 0"} and @{text "n = Suc k"} *}
 | 
|
122  | 
||
| 50548 | 123  | 
primrec nth :: "'a list => nat => 'a" (infixl "!" 100) where  | 
124  | 
nth_Cons: "(x # xs) ! n = (case n of 0 \<Rightarrow> x | Suc k \<Rightarrow> xs ! k)"  | 
|
| 34941 | 125  | 
  -- {*Warning: simpset does not contain this definition, but separate
 | 
126  | 
       theorems for @{text "n = 0"} and @{text "n = Suc k"} *}
 | 
|
127  | 
||
| 50548 | 128  | 
primrec list_update :: "'a list \<Rightarrow> nat \<Rightarrow> 'a \<Rightarrow> 'a list" where  | 
129  | 
"list_update [] i v = []" |  | 
|
130  | 
"list_update (x # xs) i v =  | 
|
131  | 
(case i of 0 \<Rightarrow> v # xs | Suc j \<Rightarrow> x # list_update xs j v)"  | 
|
| 923 | 132  | 
|
| 
41229
 
d797baa3d57c
replaced command 'nonterminals' by slightly modernized version 'nonterminal';
 
wenzelm 
parents: 
41075 
diff
changeset
 | 
133  | 
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
 | 
134  | 
|
| 923 | 135  | 
syntax  | 
| 13366 | 136  | 
  "_lupdbind":: "['a, 'a] => lupdbind"    ("(2_ :=/ _)")
 | 
137  | 
  "" :: "lupdbind => lupdbinds"    ("_")
 | 
|
138  | 
  "_lupdbinds" :: "[lupdbind, lupdbinds] => lupdbinds"    ("_,/ _")
 | 
|
139  | 
  "_LUpdate" :: "['a, lupdbinds] => 'a"    ("_/[(_)]" [900,0] 900)
 | 
|
| 
5077
 
71043526295f
* HOL/List: new function list_update written xs[i:=v] that updates the i-th
 
nipkow 
parents: 
4643 
diff
changeset
 | 
140  | 
|
| 923 | 141  | 
translations  | 
| 35115 | 142  | 
"_LUpdate xs (_lupdbinds b bs)" == "_LUpdate (_LUpdate xs b) bs"  | 
| 34941 | 143  | 
"xs[i:=x]" == "CONST list_update xs i x"  | 
144  | 
||
| 50548 | 145  | 
primrec takeWhile :: "('a \<Rightarrow> bool) \<Rightarrow> 'a list \<Rightarrow> 'a list" where
 | 
146  | 
"takeWhile P [] = []" |  | 
|
147  | 
"takeWhile P (x # xs) = (if P x then x # takeWhile P xs else [])"  | 
|
148  | 
||
149  | 
primrec dropWhile :: "('a \<Rightarrow> bool) \<Rightarrow> 'a list \<Rightarrow> 'a list" where
 | 
|
150  | 
"dropWhile P [] = []" |  | 
|
151  | 
"dropWhile P (x # xs) = (if P x then dropWhile P xs else x # xs)"  | 
|
152  | 
||
153  | 
primrec zip :: "'a list \<Rightarrow> 'b list \<Rightarrow> ('a \<times> 'b) list" where
 | 
|
154  | 
"zip xs [] = []" |  | 
|
155  | 
zip_Cons: "zip xs (y # ys) =  | 
|
156  | 
(case xs of [] => [] | z # zs => (z, y) # zip zs ys)"  | 
|
| 34941 | 157  | 
  -- {*Warning: simpset does not contain this definition, but separate
 | 
158  | 
       theorems for @{text "xs = []"} and @{text "xs = z # zs"} *}
 | 
|
159  | 
||
| 50548 | 160  | 
primrec product :: "'a list \<Rightarrow> 'b list \<Rightarrow> ('a \<times> 'b) list" where
 | 
161  | 
"product [] _ = []" |  | 
|
162  | 
"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
 | 
163  | 
|
| 
 
744934b818c7
moved quite generic material from theory Enum to more appropriate places
 
haftmann 
parents: 
49808 
diff
changeset
 | 
164  | 
hide_const (open) product  | 
| 
 
744934b818c7
moved quite generic material from theory Enum to more appropriate places
 
haftmann 
parents: 
49808 
diff
changeset
 | 
165  | 
|
| 
53721
 
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
 
traytel 
parents: 
53689 
diff
changeset
 | 
166  | 
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
 | 
167  | 
"product_lists [] = [[]]" |  | 
| 
 
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
 
traytel 
parents: 
53689 
diff
changeset
 | 
168  | 
"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
 | 
169  | 
|
| 50548 | 170  | 
primrec upt :: "nat \<Rightarrow> nat \<Rightarrow> nat list" ("(1[_..</_'])") where
 | 
171  | 
upt_0: "[i..<0] = []" |  | 
|
172  | 
upt_Suc: "[i..<(Suc j)] = (if i <= j then [i..<j] @ [j] else [])"  | 
|
173  | 
||
174  | 
definition insert :: "'a \<Rightarrow> 'a list \<Rightarrow> 'a list" where  | 
|
175  | 
"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
 | 
176  | 
|
| 57198 | 177  | 
definition union :: "'a list \<Rightarrow> 'a list \<Rightarrow> 'a list" where  | 
178  | 
"union = fold insert"  | 
|
179  | 
||
180  | 
hide_const (open) insert union  | 
|
181  | 
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
 | 
182  | 
|
| 47122 | 183  | 
primrec find :: "('a \<Rightarrow> bool) \<Rightarrow> 'a list \<Rightarrow> 'a option" where
 | 
| 50548 | 184  | 
"find _ [] = None" |  | 
185  | 
"find P (x#xs) = (if P x then Some x else find P xs)"  | 
|
| 47122 | 186  | 
|
187  | 
hide_const (open) find  | 
|
188  | 
||
| 55807 | 189  | 
definition  | 
190  | 
   "extract" :: "('a \<Rightarrow> bool) \<Rightarrow> 'a list \<Rightarrow> ('a list * 'a * 'a list) option"
 | 
|
191  | 
where "extract P xs =  | 
|
192  | 
(case dropWhile (Not o P) xs of  | 
|
193  | 
[] \<Rightarrow> None |  | 
|
194  | 
y#ys \<Rightarrow> Some(takeWhile (Not o P) xs, y, ys))"  | 
|
195  | 
||
196  | 
hide_const (open) "extract"  | 
|
197  | 
||
| 51096 | 198  | 
primrec those :: "'a option list \<Rightarrow> 'a list option"  | 
199  | 
where  | 
|
200  | 
"those [] = Some []" |  | 
|
201  | 
"those (x # xs) = (case x of  | 
|
202  | 
None \<Rightarrow> None  | 
|
| 55466 | 203  | 
| Some y \<Rightarrow> map_option (Cons y) (those xs))"  | 
| 51096 | 204  | 
|
| 50548 | 205  | 
primrec remove1 :: "'a \<Rightarrow> 'a list \<Rightarrow> 'a list" where  | 
206  | 
"remove1 x [] = []" |  | 
|
207  | 
"remove1 x (y # xs) = (if x = y then xs else y # remove1 x xs)"  | 
|
208  | 
||
209  | 
primrec removeAll :: "'a \<Rightarrow> 'a list \<Rightarrow> 'a list" where  | 
|
210  | 
"removeAll x [] = []" |  | 
|
211  | 
"removeAll x (y # xs) = (if x = y then removeAll x xs else y # removeAll x xs)"  | 
|
212  | 
||
213  | 
primrec distinct :: "'a list \<Rightarrow> bool" where  | 
|
214  | 
"distinct [] \<longleftrightarrow> True" |  | 
|
215  | 
"distinct (x # xs) \<longleftrightarrow> x \<notin> set xs \<and> distinct xs"  | 
|
216  | 
||
217  | 
primrec remdups :: "'a list \<Rightarrow> 'a list" where  | 
|
218  | 
"remdups [] = []" |  | 
|
219  | 
"remdups (x # xs) = (if x \<in> set xs then remdups xs else x # remdups xs)"  | 
|
220  | 
||
| 
53721
 
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
 
traytel 
parents: 
53689 
diff
changeset
 | 
221  | 
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
 | 
222  | 
"remdups_adj [] = []" |  | 
| 
 
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
 
traytel 
parents: 
53689 
diff
changeset
 | 
223  | 
"remdups_adj [x] = [x]" |  | 
| 
 
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
 
traytel 
parents: 
53689 
diff
changeset
 | 
224  | 
"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
 | 
225  | 
|
| 50548 | 226  | 
primrec replicate :: "nat \<Rightarrow> 'a \<Rightarrow> 'a list" where  | 
227  | 
replicate_0: "replicate 0 x = []" |  | 
|
228  | 
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
 | 
229  | 
|
| 13142 | 230  | 
text {*
 | 
| 14589 | 231  | 
  Function @{text size} is overloaded for all datatypes. Users may
 | 
| 13366 | 232  | 
  refer to the list version as @{text length}. *}
 | 
| 13142 | 233  | 
|
| 50548 | 234  | 
abbreviation length :: "'a list \<Rightarrow> nat" where  | 
235  | 
"length \<equiv> size"  | 
|
| 15307 | 236  | 
|
| 51173 | 237  | 
definition enumerate :: "nat \<Rightarrow> 'a list \<Rightarrow> (nat \<times> 'a) list" where  | 
238  | 
enumerate_eq_zip: "enumerate n xs = zip [n..<n + length xs] xs"  | 
|
239  | 
||
| 
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
 | 
240  | 
primrec rotate1 :: "'a list \<Rightarrow> 'a list" where  | 
| 50548 | 241  | 
"rotate1 [] = []" |  | 
242  | 
"rotate1 (x # xs) = xs @ [x]"  | 
|
243  | 
||
244  | 
definition rotate :: "nat \<Rightarrow> 'a list \<Rightarrow> 'a list" where  | 
|
245  | 
"rotate n = rotate1 ^^ n"  | 
|
246  | 
||
247  | 
definition sublist :: "'a list => nat set => 'a list" where  | 
|
248  | 
"sublist xs A = map fst (filter (\<lambda>p. snd p \<in> A) (zip xs [0..<size xs]))"  | 
|
249  | 
||
250  | 
primrec sublists :: "'a list \<Rightarrow> 'a list list" where  | 
|
251  | 
"sublists [] = [[]]" |  | 
|
252  | 
"sublists (x#xs) = (let xss = sublists xs in map (Cons x) xss @ xss)"  | 
|
253  | 
||
254  | 
primrec n_lists :: "nat \<Rightarrow> 'a list \<Rightarrow> 'a list list" where  | 
|
255  | 
"n_lists 0 xs = [[]]" |  | 
|
256  | 
"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
 | 
257  | 
|
| 
 
744934b818c7
moved quite generic material from theory Enum to more appropriate places
 
haftmann 
parents: 
49808 
diff
changeset
 | 
258  | 
hide_const (open) n_lists  | 
| 
 
744934b818c7
moved quite generic material from theory Enum to more appropriate places
 
haftmann 
parents: 
49808 
diff
changeset
 | 
259  | 
|
| 
40593
 
1e57b18d27b1
code eqn for slice was missing; redefined splice with fun
 
nipkow 
parents: 
40365 
diff
changeset
 | 
260  | 
fun splice :: "'a list \<Rightarrow> 'a list \<Rightarrow> 'a list" where  | 
| 
 
1e57b18d27b1
code eqn for slice was missing; redefined splice with fun
 
nipkow 
parents: 
40365 
diff
changeset
 | 
261  | 
"splice [] ys = ys" |  | 
| 
 
1e57b18d27b1
code eqn for slice was missing; redefined splice with fun
 
nipkow 
parents: 
40365 
diff
changeset
 | 
262  | 
"splice xs [] = xs" |  | 
| 
 
1e57b18d27b1
code eqn for slice was missing; redefined splice with fun
 
nipkow 
parents: 
40365 
diff
changeset
 | 
263  | 
"splice (x#xs) (y#ys) = x # y # splice xs ys"  | 
| 
21061
 
580dfc999ef6
added normal post setup; cleaned up "execution" constants
 
haftmann 
parents: 
21046 
diff
changeset
 | 
264  | 
|
| 26771 | 265  | 
text{*
 | 
266  | 
\begin{figure}[htbp]
 | 
|
267  | 
\fbox{
 | 
|
268  | 
\begin{tabular}{l}
 | 
|
| 27381 | 269  | 
@{lemma "[a,b]@[c,d] = [a,b,c,d]" by simp}\\
 | 
270  | 
@{lemma "length [a,b,c] = 3" by simp}\\
 | 
|
271  | 
@{lemma "set [a,b,c] = {a,b,c}" by simp}\\
 | 
|
272  | 
@{lemma "map f [a,b,c] = [f a, f b, f c]" by simp}\\
 | 
|
273  | 
@{lemma "rev [a,b,c] = [c,b,a]" by simp}\\
 | 
|
274  | 
@{lemma "hd [a,b,c,d] = a" by simp}\\
 | 
|
275  | 
@{lemma "tl [a,b,c,d] = [b,c,d]" by simp}\\
 | 
|
276  | 
@{lemma "last [a,b,c,d] = d" by simp}\\
 | 
|
277  | 
@{lemma "butlast [a,b,c,d] = [a,b,c]" by simp}\\
 | 
|
278  | 
@{lemma[source] "filter (\<lambda>n::nat. n<2) [0,2,1] = [0,1]" by simp}\\
 | 
|
279  | 
@{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
 | 
280  | 
@{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
 | 
281  | 
@{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
 | 
282  | 
@{lemma "foldl f x [a,b,c] = f (f (f x a) b) c" by simp}\\
 | 
| 27381 | 283  | 
@{lemma "zip [a,b,c] [x,y,z] = [(a,x),(b,y),(c,z)]" by simp}\\
 | 
284  | 
@{lemma "zip [a,b] [x,y,z] = [(a,x),(b,y)]" by simp}\\
 | 
|
| 51173 | 285  | 
@{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
 | 
286  | 
@{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
 | 
287  | 
@{lemma "product_lists [[a,b], [c], [d,e]] = [[a,c,d], [a,c,e], [b,c,d], [b,c,e]]" by simp}\\
 | 
| 27381 | 288  | 
@{lemma "splice [a,b,c] [x,y,z] = [a,x,b,y,c,z]" by simp}\\
 | 
289  | 
@{lemma "splice [a,b,c,d] [x,y] = [a,x,b,y,c,d]" by simp}\\
 | 
|
290  | 
@{lemma "take 2 [a,b,c,d] = [a,b]" by simp}\\
 | 
|
291  | 
@{lemma "take 6 [a,b,c,d] = [a,b,c,d]" by simp}\\
 | 
|
292  | 
@{lemma "drop 2 [a,b,c,d] = [c,d]" by simp}\\
 | 
|
293  | 
@{lemma "drop 6 [a,b,c,d] = []" by simp}\\
 | 
|
294  | 
@{lemma "takeWhile (%n::nat. n<3) [1,2,3,0] = [1,2]" by simp}\\
 | 
|
295  | 
@{lemma "dropWhile (%n::nat. n<3) [1,2,3,0] = [3,0]" by simp}\\
 | 
|
296  | 
@{lemma "distinct [2,0,1::nat]" by simp}\\
 | 
|
297  | 
@{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
 | 
298  | 
@{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
 | 
299  | 
@{lemma "List.insert 2 [0::nat,1,2] = [0,1,2]" by (simp add: List.insert_def)}\\
 | 
| 35295 | 300  | 
@{lemma "List.insert 3 [0::nat,1,2] = [3,0,1,2]" by (simp add: List.insert_def)}\\
 | 
| 57198 | 301  | 
@{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 | 302  | 
@{lemma "List.find (%i::int. i>0) [0,0] = None" by simp}\\
 | 
303  | 
@{lemma "List.find (%i::int. i>0) [0,1,0,2] = Some 1" by simp}\\
 | 
|
| 55807 | 304  | 
@{lemma "List.extract (%i::int. i>0) [0,0] = None" by(simp add: extract_def)}\\
 | 
305  | 
@{lemma "List.extract (%i::int. i>0) [0,1,0,2] = Some([0], 1, [0,2])" by(simp add: extract_def)}\\
 | 
|
| 27381 | 306  | 
@{lemma "remove1 2 [2,0,2,1::nat,2] = [0,2,1,2]" by simp}\\
 | 
| 27693 | 307  | 
@{lemma "removeAll 2 [2,0,2,1::nat,2] = [0,1]" by simp}\\
 | 
| 27381 | 308  | 
@{lemma "nth [a,b,c,d] 2 = c" by simp}\\
 | 
309  | 
@{lemma "[a,b,c,d][2 := x] = [a,b,x,d]" by simp}\\
 | 
|
310  | 
@{lemma "sublist [a,b,c,d,e] {0,2,3} = [a,c,d]" by (simp add:sublist_def)}\\
 | 
|
| 
49948
 
744934b818c7
moved quite generic material from theory Enum to more appropriate places
 
haftmann 
parents: 
49808 
diff
changeset
 | 
311  | 
@{lemma "sublists [a,b] = [[a, b], [a], [b], []]" by simp}\\
 | 
| 
 
744934b818c7
moved quite generic material from theory Enum to more appropriate places
 
haftmann 
parents: 
49808 
diff
changeset
 | 
312  | 
@{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
 | 
313  | 
@{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
 | 
314  | 
@{lemma "rotate 3 [a,b,c,d] = [d,a,b,c]" by (simp add:rotate_def eval_nat_numeral)}\\
 | 
| 40077 | 315  | 
@{lemma "replicate 4 a = [a,a,a,a]" by (simp add:eval_nat_numeral)}\\
 | 
316  | 
@{lemma "[2..<5] = [2,3,4]" by (simp add:eval_nat_numeral)}\\
 | 
|
| 
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
 | 
317  | 
@{lemma "listsum [1,2,3::nat] = 6" by (simp add: listsum_def)}
 | 
| 26771 | 318  | 
\end{tabular}}
 | 
319  | 
\caption{Characteristic examples}
 | 
|
320  | 
\label{fig:Characteristic}
 | 
|
321  | 
\end{figure}
 | 
|
| 29927 | 322  | 
Figure~\ref{fig:Characteristic} shows characteristic examples
 | 
| 26771 | 323  | 
that should give an intuitive understanding of the above functions.  | 
324  | 
*}  | 
|
325  | 
||
| 24616 | 326  | 
text{* The following simple sort functions are intended for proofs,
 | 
327  | 
not for efficient implementations. *}  | 
|
328  | 
||
| 
25221
 
5ded95dda5df
append/member: more light-weight way to declare authentic syntax;
 
wenzelm 
parents: 
25215 
diff
changeset
 | 
329  | 
context linorder  | 
| 
 
5ded95dda5df
append/member: more light-weight way to declare authentic syntax;
 
wenzelm 
parents: 
25215 
diff
changeset
 | 
330  | 
begin  | 
| 
 
5ded95dda5df
append/member: more light-weight way to declare authentic syntax;
 
wenzelm 
parents: 
25215 
diff
changeset
 | 
331  | 
|
| 
39915
 
ecf97cf3d248
turned distinct and sorted into inductive predicates: yields nice induction principles for free; more elegant proofs
 
haftmann 
parents: 
39774 
diff
changeset
 | 
332  | 
inductive sorted :: "'a list \<Rightarrow> bool" where  | 
| 
 
ecf97cf3d248
turned distinct and sorted into inductive predicates: yields nice induction principles for free; more elegant proofs
 
haftmann 
parents: 
39774 
diff
changeset
 | 
333  | 
Nil [iff]: "sorted []"  | 
| 
 
ecf97cf3d248
turned distinct and sorted into inductive predicates: yields nice induction principles for free; more elegant proofs
 
haftmann 
parents: 
39774 
diff
changeset
 | 
334  | 
| Cons: "\<forall>y\<in>set xs. x \<le> y \<Longrightarrow> sorted xs \<Longrightarrow> sorted (x # xs)"  | 
| 
 
ecf97cf3d248
turned distinct and sorted into inductive predicates: yields nice induction principles for free; more elegant proofs
 
haftmann 
parents: 
39774 
diff
changeset
 | 
335  | 
|
| 
 
ecf97cf3d248
turned distinct and sorted into inductive predicates: yields nice induction principles for free; more elegant proofs
 
haftmann 
parents: 
39774 
diff
changeset
 | 
336  | 
lemma sorted_single [iff]:  | 
| 
 
ecf97cf3d248
turned distinct and sorted into inductive predicates: yields nice induction principles for free; more elegant proofs
 
haftmann 
parents: 
39774 
diff
changeset
 | 
337  | 
"sorted [x]"  | 
| 
 
ecf97cf3d248
turned distinct and sorted into inductive predicates: yields nice induction principles for free; more elegant proofs
 
haftmann 
parents: 
39774 
diff
changeset
 | 
338  | 
by (rule sorted.Cons) auto  | 
| 
 
ecf97cf3d248
turned distinct and sorted into inductive predicates: yields nice induction principles for free; more elegant proofs
 
haftmann 
parents: 
39774 
diff
changeset
 | 
339  | 
|
| 
 
ecf97cf3d248
turned distinct and sorted into inductive predicates: yields nice induction principles for free; more elegant proofs
 
haftmann 
parents: 
39774 
diff
changeset
 | 
340  | 
lemma sorted_many:  | 
| 
 
ecf97cf3d248
turned distinct and sorted into inductive predicates: yields nice induction principles for free; more elegant proofs
 
haftmann 
parents: 
39774 
diff
changeset
 | 
341  | 
"x \<le> y \<Longrightarrow> sorted (y # zs) \<Longrightarrow> sorted (x # y # zs)"  | 
| 
 
ecf97cf3d248
turned distinct and sorted into inductive predicates: yields nice induction principles for free; more elegant proofs
 
haftmann 
parents: 
39774 
diff
changeset
 | 
342  | 
by (rule sorted.Cons) (cases "y # zs" rule: sorted.cases, auto)  | 
| 
 
ecf97cf3d248
turned distinct and sorted into inductive predicates: yields nice induction principles for free; more elegant proofs
 
haftmann 
parents: 
39774 
diff
changeset
 | 
343  | 
|
| 
 
ecf97cf3d248
turned distinct and sorted into inductive predicates: yields nice induction principles for free; more elegant proofs
 
haftmann 
parents: 
39774 
diff
changeset
 | 
344  | 
lemma sorted_many_eq [simp, code]:  | 
| 
 
ecf97cf3d248
turned distinct and sorted into inductive predicates: yields nice induction principles for free; more elegant proofs
 
haftmann 
parents: 
39774 
diff
changeset
 | 
345  | 
"sorted (x # y # zs) \<longleftrightarrow> x \<le> y \<and> sorted (y # zs)"  | 
| 
 
ecf97cf3d248
turned distinct and sorted into inductive predicates: yields nice induction principles for free; more elegant proofs
 
haftmann 
parents: 
39774 
diff
changeset
 | 
346  | 
by (auto intro: sorted_many elim: sorted.cases)  | 
| 
 
ecf97cf3d248
turned distinct and sorted into inductive predicates: yields nice induction principles for free; more elegant proofs
 
haftmann 
parents: 
39774 
diff
changeset
 | 
347  | 
|
| 
 
ecf97cf3d248
turned distinct and sorted into inductive predicates: yields nice induction principles for free; more elegant proofs
 
haftmann 
parents: 
39774 
diff
changeset
 | 
348  | 
lemma [code]:  | 
| 
 
ecf97cf3d248
turned distinct and sorted into inductive predicates: yields nice induction principles for free; more elegant proofs
 
haftmann 
parents: 
39774 
diff
changeset
 | 
349  | 
"sorted [] \<longleftrightarrow> True"  | 
| 
 
ecf97cf3d248
turned distinct and sorted into inductive predicates: yields nice induction principles for free; more elegant proofs
 
haftmann 
parents: 
39774 
diff
changeset
 | 
350  | 
"sorted [x] \<longleftrightarrow> True"  | 
| 
 
ecf97cf3d248
turned distinct and sorted into inductive predicates: yields nice induction principles for free; more elegant proofs
 
haftmann 
parents: 
39774 
diff
changeset
 | 
351  | 
by simp_all  | 
| 24697 | 352  | 
|
| 
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
 | 
353  | 
primrec insort_key :: "('b \<Rightarrow> 'a) \<Rightarrow> 'b \<Rightarrow> 'b list \<Rightarrow> 'b list" where
 | 
| 50548 | 354  | 
"insort_key f x [] = [x]" |  | 
355  | 
"insort_key f x (y#ys) =  | 
|
356  | 
(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
 | 
357  | 
|
| 35195 | 358  | 
definition sort_key :: "('b \<Rightarrow> 'a) \<Rightarrow> 'b list \<Rightarrow> 'b list" where
 | 
| 50548 | 359  | 
"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
 | 
360  | 
|
| 
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
 | 
361  | 
definition insort_insert_key :: "('b \<Rightarrow> 'a) \<Rightarrow> 'b \<Rightarrow> 'b list \<Rightarrow> 'b list" where
 | 
| 50548 | 362  | 
"insort_insert_key f x xs =  | 
363  | 
(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
 | 
364  | 
|
| 
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
 | 
365  | 
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
 | 
366  | 
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
 | 
367  | 
abbreviation "insort_insert \<equiv> insort_insert_key (\<lambda>x. x)"  | 
| 35608 | 368  | 
|
| 
25221
 
5ded95dda5df
append/member: more light-weight way to declare authentic syntax;
 
wenzelm 
parents: 
25215 
diff
changeset
 | 
369  | 
end  | 
| 
 
5ded95dda5df
append/member: more light-weight way to declare authentic syntax;
 
wenzelm 
parents: 
25215 
diff
changeset
 | 
370  | 
|
| 24616 | 371  | 
|
| 23388 | 372  | 
subsubsection {* List comprehension *}
 | 
| 23192 | 373  | 
|
| 24349 | 374  | 
text{* Input syntax for Haskell-like list comprehension notation.
 | 
375  | 
Typical example: @{text"[(x,y). x \<leftarrow> xs, y \<leftarrow> ys, x \<noteq> y]"},
 | 
|
376  | 
the list of all pairs of distinct elements from @{text xs} and @{text ys}.
 | 
|
377  | 
The syntax is as in Haskell, except that @{text"|"} becomes a dot
 | 
|
378  | 
(like in Isabelle's set comprehension): @{text"[e. x \<leftarrow> xs, \<dots>]"} rather than
 | 
|
379  | 
\verb![e| x <- xs, ...]!.  | 
|
380  | 
||
381  | 
The qualifiers after the dot are  | 
|
382  | 
\begin{description}
 | 
|
383  | 
\item[generators] @{text"p \<leftarrow> xs"},
 | 
|
| 
24476
 
f7ad9fbbeeaa
turned list comprehension translations into ML to optimize base case
 
nipkow 
parents: 
24471 
diff
changeset
 | 
384  | 
 where @{text p} is a pattern and @{text xs} an expression of list type, or
 | 
| 
 
f7ad9fbbeeaa
turned list comprehension translations into ML to optimize base case
 
nipkow 
parents: 
24471 
diff
changeset
 | 
385  | 
\item[guards] @{text"b"}, where @{text b} is a boolean expression.
 | 
| 
 
f7ad9fbbeeaa
turned list comprehension translations into ML to optimize base case
 
nipkow 
parents: 
24471 
diff
changeset
 | 
386  | 
%\item[local bindings] @ {text"let x = e"}.
 | 
| 24349 | 387  | 
\end{description}
 | 
| 23240 | 388  | 
|
| 
24476
 
f7ad9fbbeeaa
turned list comprehension translations into ML to optimize base case
 
nipkow 
parents: 
24471 
diff
changeset
 | 
389  | 
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
 | 
390  | 
misunderstandings, the translation into desugared form is not reversed  | 
| 
 
f7ad9fbbeeaa
turned list comprehension translations into ML to optimize base case
 
nipkow 
parents: 
24471 
diff
changeset
 | 
391  | 
upon output. Note that the translation of @{text"[e. x \<leftarrow> xs]"} is
 | 
| 
 
f7ad9fbbeeaa
turned list comprehension translations into ML to optimize base case
 
nipkow 
parents: 
24471 
diff
changeset
 | 
392  | 
optmized to @{term"map (%x. e) xs"}.
 | 
| 23240 | 393  | 
|
| 24349 | 394  | 
It is easy to write short list comprehensions which stand for complex  | 
395  | 
expressions. During proofs, they may become unreadable (and  | 
|
396  | 
mangled). In such cases it can be advisable to introduce separate  | 
|
397  | 
definitions for the list comprehensions in question. *}  | 
|
398  | 
||
| 
46138
 
85f8d8a8c711
improved list comprehension syntax: more careful treatment of position constraints, which enables PIDE markup;
 
wenzelm 
parents: 
46133 
diff
changeset
 | 
399  | 
nonterminal lc_qual and lc_quals  | 
| 23192 | 400  | 
|
401  | 
syntax  | 
|
| 
46138
 
85f8d8a8c711
improved list comprehension syntax: more careful treatment of position constraints, which enables PIDE markup;
 
wenzelm 
parents: 
46133 
diff
changeset
 | 
402  | 
  "_listcompr" :: "'a \<Rightarrow> lc_qual \<Rightarrow> lc_quals \<Rightarrow> 'a list"  ("[_ . __")
 | 
| 
 
85f8d8a8c711
improved list comprehension syntax: more careful treatment of position constraints, which enables PIDE markup;
 
wenzelm 
parents: 
46133 
diff
changeset
 | 
403  | 
  "_lc_gen" :: "'a \<Rightarrow> 'a list \<Rightarrow> lc_qual"  ("_ <- _")
 | 
| 
 
85f8d8a8c711
improved list comprehension syntax: more careful treatment of position constraints, which enables PIDE markup;
 
wenzelm 
parents: 
46133 
diff
changeset
 | 
404  | 
  "_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
 | 
405  | 
  (*"_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
 | 
406  | 
  "_lc_end" :: "lc_quals" ("]")
 | 
| 
 
85f8d8a8c711
improved list comprehension syntax: more careful treatment of position constraints, which enables PIDE markup;
 
wenzelm 
parents: 
46133 
diff
changeset
 | 
407  | 
  "_lc_quals" :: "lc_qual \<Rightarrow> lc_quals \<Rightarrow> lc_quals"  (", __")
 | 
| 
 
85f8d8a8c711
improved list comprehension syntax: more careful treatment of position constraints, which enables PIDE markup;
 
wenzelm 
parents: 
46133 
diff
changeset
 | 
408  | 
"_lc_abs" :: "'a => 'b list => 'b list"  | 
| 23192 | 409  | 
|
| 
24476
 
f7ad9fbbeeaa
turned list comprehension translations into ML to optimize base case
 
nipkow 
parents: 
24471 
diff
changeset
 | 
410  | 
(* These are easier than ML code but cannot express the optimized  | 
| 
 
f7ad9fbbeeaa
turned list comprehension translations into ML to optimize base case
 
nipkow 
parents: 
24471 
diff
changeset
 | 
411  | 
translation of [e. p<-xs]  | 
| 23192 | 412  | 
translations  | 
| 
46138
 
85f8d8a8c711
improved list comprehension syntax: more careful treatment of position constraints, which enables PIDE markup;
 
wenzelm 
parents: 
46133 
diff
changeset
 | 
413  | 
"[e. p<-xs]" => "concat(map (_lc_abs p [e]) xs)"  | 
| 
 
85f8d8a8c711
improved list comprehension syntax: more careful treatment of position constraints, which enables PIDE markup;
 
wenzelm 
parents: 
46133 
diff
changeset
 | 
414  | 
"_listcompr e (_lc_gen p xs) (_lc_quals Q Qs)"  | 
| 
 
85f8d8a8c711
improved list comprehension syntax: more careful treatment of position constraints, which enables PIDE markup;
 
wenzelm 
parents: 
46133 
diff
changeset
 | 
415  | 
=> "concat (map (_lc_abs p (_listcompr e Q Qs)) xs)"  | 
| 
 
85f8d8a8c711
improved list comprehension syntax: more careful treatment of position constraints, which enables PIDE markup;
 
wenzelm 
parents: 
46133 
diff
changeset
 | 
416  | 
"[e. P]" => "if P then [e] else []"  | 
| 
 
85f8d8a8c711
improved list comprehension syntax: more careful treatment of position constraints, which enables PIDE markup;
 
wenzelm 
parents: 
46133 
diff
changeset
 | 
417  | 
"_listcompr e (_lc_test P) (_lc_quals Q Qs)"  | 
| 
 
85f8d8a8c711
improved list comprehension syntax: more careful treatment of position constraints, which enables PIDE markup;
 
wenzelm 
parents: 
46133 
diff
changeset
 | 
418  | 
=> "if P then (_listcompr e Q Qs) else []"  | 
| 
 
85f8d8a8c711
improved list comprehension syntax: more careful treatment of position constraints, which enables PIDE markup;
 
wenzelm 
parents: 
46133 
diff
changeset
 | 
419  | 
"_listcompr e (_lc_let b) (_lc_quals Q Qs)"  | 
| 
 
85f8d8a8c711
improved list comprehension syntax: more careful treatment of position constraints, which enables PIDE markup;
 
wenzelm 
parents: 
46133 
diff
changeset
 | 
420  | 
=> "_Let b (_listcompr e Q Qs)"  | 
| 
24476
 
f7ad9fbbeeaa
turned list comprehension translations into ML to optimize base case
 
nipkow 
parents: 
24471 
diff
changeset
 | 
421  | 
*)  | 
| 23240 | 422  | 
|
| 
23279
 
e39dd93161d9
tuned list comprehension, changed filter syntax from : to <-
 
nipkow 
parents: 
23246 
diff
changeset
 | 
423  | 
syntax (xsymbols)  | 
| 
46138
 
85f8d8a8c711
improved list comprehension syntax: more careful treatment of position constraints, which enables PIDE markup;
 
wenzelm 
parents: 
46133 
diff
changeset
 | 
424  | 
  "_lc_gen" :: "'a \<Rightarrow> 'a list \<Rightarrow> lc_qual"  ("_ \<leftarrow> _")
 | 
| 
23279
 
e39dd93161d9
tuned list comprehension, changed filter syntax from : to <-
 
nipkow 
parents: 
23246 
diff
changeset
 | 
425  | 
syntax (HTML output)  | 
| 
46138
 
85f8d8a8c711
improved list comprehension syntax: more careful treatment of position constraints, which enables PIDE markup;
 
wenzelm 
parents: 
46133 
diff
changeset
 | 
426  | 
  "_lc_gen" :: "'a \<Rightarrow> 'a list \<Rightarrow> lc_qual"  ("_ \<leftarrow> _")
 | 
| 24349 | 427  | 
|
| 52143 | 428  | 
parse_translation {*
 | 
| 
46138
 
85f8d8a8c711
improved list comprehension syntax: more careful treatment of position constraints, which enables PIDE markup;
 
wenzelm 
parents: 
46133 
diff
changeset
 | 
429  | 
let  | 
| 
 
85f8d8a8c711
improved list comprehension syntax: more careful treatment of position constraints, which enables PIDE markup;
 
wenzelm 
parents: 
46133 
diff
changeset
 | 
430  | 
    val NilC = Syntax.const @{const_syntax Nil};
 | 
| 
 
85f8d8a8c711
improved list comprehension syntax: more careful treatment of position constraints, which enables PIDE markup;
 
wenzelm 
parents: 
46133 
diff
changeset
 | 
431  | 
    val ConsC = Syntax.const @{const_syntax Cons};
 | 
| 
 
85f8d8a8c711
improved list comprehension syntax: more careful treatment of position constraints, which enables PIDE markup;
 
wenzelm 
parents: 
46133 
diff
changeset
 | 
432  | 
    val mapC = Syntax.const @{const_syntax map};
 | 
| 
 
85f8d8a8c711
improved list comprehension syntax: more careful treatment of position constraints, which enables PIDE markup;
 
wenzelm 
parents: 
46133 
diff
changeset
 | 
433  | 
    val concatC = Syntax.const @{const_syntax concat};
 | 
| 
 
85f8d8a8c711
improved list comprehension syntax: more careful treatment of position constraints, which enables PIDE markup;
 
wenzelm 
parents: 
46133 
diff
changeset
 | 
434  | 
    val IfC = Syntax.const @{const_syntax If};
 | 
| 
 
85f8d8a8c711
improved list comprehension syntax: more careful treatment of position constraints, which enables PIDE markup;
 
wenzelm 
parents: 
46133 
diff
changeset
 | 
435  | 
|
| 
 
85f8d8a8c711
improved list comprehension syntax: more careful treatment of position constraints, which enables PIDE markup;
 
wenzelm 
parents: 
46133 
diff
changeset
 | 
436  | 
fun single x = ConsC $ x $ NilC;  | 
| 
 
85f8d8a8c711
improved list comprehension syntax: more careful treatment of position constraints, which enables PIDE markup;
 
wenzelm 
parents: 
46133 
diff
changeset
 | 
437  | 
|
| 
 
85f8d8a8c711
improved list comprehension syntax: more careful treatment of position constraints, which enables PIDE markup;
 
wenzelm 
parents: 
46133 
diff
changeset
 | 
438  | 
fun pat_tr ctxt p e opti = (* %x. case x of p => e | _ => [] *)  | 
| 
 
85f8d8a8c711
improved list comprehension syntax: more careful treatment of position constraints, which enables PIDE markup;
 
wenzelm 
parents: 
46133 
diff
changeset
 | 
439  | 
let  | 
| 
 
85f8d8a8c711
improved list comprehension syntax: more careful treatment of position constraints, which enables PIDE markup;
 
wenzelm 
parents: 
46133 
diff
changeset
 | 
440  | 
(* FIXME proper name context!? *)  | 
| 
 
85f8d8a8c711
improved list comprehension syntax: more careful treatment of position constraints, which enables PIDE markup;
 
wenzelm 
parents: 
46133 
diff
changeset
 | 
441  | 
val x =  | 
| 
 
85f8d8a8c711
improved list comprehension syntax: more careful treatment of position constraints, which enables PIDE markup;
 
wenzelm 
parents: 
46133 
diff
changeset
 | 
442  | 
Free (singleton (Name.variant_list (fold Term.add_free_names [p, e] [])) "x", dummyT);  | 
| 
 
85f8d8a8c711
improved list comprehension syntax: more careful treatment of position constraints, which enables PIDE markup;
 
wenzelm 
parents: 
46133 
diff
changeset
 | 
443  | 
val e = if opti then single e else e;  | 
| 
 
85f8d8a8c711
improved list comprehension syntax: more careful treatment of position constraints, which enables PIDE markup;
 
wenzelm 
parents: 
46133 
diff
changeset
 | 
444  | 
        val case1 = Syntax.const @{syntax_const "_case1"} $ p $ e;
 | 
| 
 
85f8d8a8c711
improved list comprehension syntax: more careful treatment of position constraints, which enables PIDE markup;
 
wenzelm 
parents: 
46133 
diff
changeset
 | 
445  | 
val case2 =  | 
| 
 
85f8d8a8c711
improved list comprehension syntax: more careful treatment of position constraints, which enables PIDE markup;
 
wenzelm 
parents: 
46133 
diff
changeset
 | 
446  | 
          Syntax.const @{syntax_const "_case1"} $
 | 
| 56241 | 447  | 
            Syntax.const @{const_syntax Pure.dummy_pattern} $ NilC;
 | 
| 
46138
 
85f8d8a8c711
improved list comprehension syntax: more careful treatment of position constraints, which enables PIDE markup;
 
wenzelm 
parents: 
46133 
diff
changeset
 | 
448  | 
        val cs = Syntax.const @{syntax_const "_case2"} $ case1 $ case2;
 | 
| 
51678
 
1e33b81c328a
allow redundant cases in the list comprehension translation
 
traytel 
parents: 
51673 
diff
changeset
 | 
449  | 
in Syntax_Trans.abs_tr [x, Case_Translation.case_tr false ctxt [x, cs]] end;  | 
| 
46138
 
85f8d8a8c711
improved list comprehension syntax: more careful treatment of position constraints, which enables PIDE markup;
 
wenzelm 
parents: 
46133 
diff
changeset
 | 
450  | 
|
| 
 
85f8d8a8c711
improved list comprehension syntax: more careful treatment of position constraints, which enables PIDE markup;
 
wenzelm 
parents: 
46133 
diff
changeset
 | 
451  | 
fun abs_tr ctxt p e opti =  | 
| 
 
85f8d8a8c711
improved list comprehension syntax: more careful treatment of position constraints, which enables PIDE markup;
 
wenzelm 
parents: 
46133 
diff
changeset
 | 
452  | 
(case Term_Position.strip_positions p of  | 
| 
 
85f8d8a8c711
improved list comprehension syntax: more careful treatment of position constraints, which enables PIDE markup;
 
wenzelm 
parents: 
46133 
diff
changeset
 | 
453  | 
Free (s, T) =>  | 
| 
 
85f8d8a8c711
improved list comprehension syntax: more careful treatment of position constraints, which enables PIDE markup;
 
wenzelm 
parents: 
46133 
diff
changeset
 | 
454  | 
let  | 
| 
 
85f8d8a8c711
improved list comprehension syntax: more careful treatment of position constraints, which enables PIDE markup;
 
wenzelm 
parents: 
46133 
diff
changeset
 | 
455  | 
val thy = Proof_Context.theory_of ctxt;  | 
| 
 
85f8d8a8c711
improved list comprehension syntax: more careful treatment of position constraints, which enables PIDE markup;
 
wenzelm 
parents: 
46133 
diff
changeset
 | 
456  | 
val s' = Proof_Context.intern_const ctxt s;  | 
| 
 
85f8d8a8c711
improved list comprehension syntax: more careful treatment of position constraints, which enables PIDE markup;
 
wenzelm 
parents: 
46133 
diff
changeset
 | 
457  | 
in  | 
| 
 
85f8d8a8c711
improved list comprehension syntax: more careful treatment of position constraints, which enables PIDE markup;
 
wenzelm 
parents: 
46133 
diff
changeset
 | 
458  | 
if Sign.declared_const thy s'  | 
| 
 
85f8d8a8c711
improved list comprehension syntax: more careful treatment of position constraints, which enables PIDE markup;
 
wenzelm 
parents: 
46133 
diff
changeset
 | 
459  | 
then (pat_tr ctxt p e opti, false)  | 
| 
 
85f8d8a8c711
improved list comprehension syntax: more careful treatment of position constraints, which enables PIDE markup;
 
wenzelm 
parents: 
46133 
diff
changeset
 | 
460  | 
else (Syntax_Trans.abs_tr [p, e], true)  | 
| 
 
85f8d8a8c711
improved list comprehension syntax: more careful treatment of position constraints, which enables PIDE markup;
 
wenzelm 
parents: 
46133 
diff
changeset
 | 
461  | 
end  | 
| 
 
85f8d8a8c711
improved list comprehension syntax: more careful treatment of position constraints, which enables PIDE markup;
 
wenzelm 
parents: 
46133 
diff
changeset
 | 
462  | 
| _ => (pat_tr ctxt p e opti, false));  | 
| 
 
85f8d8a8c711
improved list comprehension syntax: more careful treatment of position constraints, which enables PIDE markup;
 
wenzelm 
parents: 
46133 
diff
changeset
 | 
463  | 
|
| 
 
85f8d8a8c711
improved list comprehension syntax: more careful treatment of position constraints, which enables PIDE markup;
 
wenzelm 
parents: 
46133 
diff
changeset
 | 
464  | 
    fun lc_tr ctxt [e, Const (@{syntax_const "_lc_test"}, _) $ b, qs] =
 | 
| 
 
85f8d8a8c711
improved list comprehension syntax: more careful treatment of position constraints, which enables PIDE markup;
 
wenzelm 
parents: 
46133 
diff
changeset
 | 
465  | 
let  | 
| 
 
85f8d8a8c711
improved list comprehension syntax: more careful treatment of position constraints, which enables PIDE markup;
 
wenzelm 
parents: 
46133 
diff
changeset
 | 
466  | 
val res =  | 
| 
 
85f8d8a8c711
improved list comprehension syntax: more careful treatment of position constraints, which enables PIDE markup;
 
wenzelm 
parents: 
46133 
diff
changeset
 | 
467  | 
(case qs of  | 
| 
 
85f8d8a8c711
improved list comprehension syntax: more careful treatment of position constraints, which enables PIDE markup;
 
wenzelm 
parents: 
46133 
diff
changeset
 | 
468  | 
                Const (@{syntax_const "_lc_end"}, _) => single e
 | 
| 
 
85f8d8a8c711
improved list comprehension syntax: more careful treatment of position constraints, which enables PIDE markup;
 
wenzelm 
parents: 
46133 
diff
changeset
 | 
469  | 
              | Const (@{syntax_const "_lc_quals"}, _) $ q $ qs => lc_tr ctxt [e, q, qs]);
 | 
| 
 
85f8d8a8c711
improved list comprehension syntax: more careful treatment of position constraints, which enables PIDE markup;
 
wenzelm 
parents: 
46133 
diff
changeset
 | 
470  | 
in IfC $ b $ res $ NilC end  | 
| 
 
85f8d8a8c711
improved list comprehension syntax: more careful treatment of position constraints, which enables PIDE markup;
 
wenzelm 
parents: 
46133 
diff
changeset
 | 
471  | 
| lc_tr ctxt  | 
| 
 
85f8d8a8c711
improved list comprehension syntax: more careful treatment of position constraints, which enables PIDE markup;
 
wenzelm 
parents: 
46133 
diff
changeset
 | 
472  | 
            [e, Const (@{syntax_const "_lc_gen"}, _) $ p $ es,
 | 
| 
 
85f8d8a8c711
improved list comprehension syntax: more careful treatment of position constraints, which enables PIDE markup;
 
wenzelm 
parents: 
46133 
diff
changeset
 | 
473  | 
              Const(@{syntax_const "_lc_end"}, _)] =
 | 
| 
 
85f8d8a8c711
improved list comprehension syntax: more careful treatment of position constraints, which enables PIDE markup;
 
wenzelm 
parents: 
46133 
diff
changeset
 | 
474  | 
(case abs_tr ctxt p e true of  | 
| 
 
85f8d8a8c711
improved list comprehension syntax: more careful treatment of position constraints, which enables PIDE markup;
 
wenzelm 
parents: 
46133 
diff
changeset
 | 
475  | 
(f, true) => mapC $ f $ es  | 
| 
 
85f8d8a8c711
improved list comprehension syntax: more careful treatment of position constraints, which enables PIDE markup;
 
wenzelm 
parents: 
46133 
diff
changeset
 | 
476  | 
| (f, false) => concatC $ (mapC $ f $ es))  | 
| 
 
85f8d8a8c711
improved list comprehension syntax: more careful treatment of position constraints, which enables PIDE markup;
 
wenzelm 
parents: 
46133 
diff
changeset
 | 
477  | 
| lc_tr ctxt  | 
| 
 
85f8d8a8c711
improved list comprehension syntax: more careful treatment of position constraints, which enables PIDE markup;
 
wenzelm 
parents: 
46133 
diff
changeset
 | 
478  | 
            [e, Const (@{syntax_const "_lc_gen"}, _) $ p $ es,
 | 
| 
 
85f8d8a8c711
improved list comprehension syntax: more careful treatment of position constraints, which enables PIDE markup;
 
wenzelm 
parents: 
46133 
diff
changeset
 | 
479  | 
              Const (@{syntax_const "_lc_quals"}, _) $ q $ qs] =
 | 
| 
 
85f8d8a8c711
improved list comprehension syntax: more careful treatment of position constraints, which enables PIDE markup;
 
wenzelm 
parents: 
46133 
diff
changeset
 | 
480  | 
let val e' = lc_tr ctxt [e, q, qs];  | 
| 
 
85f8d8a8c711
improved list comprehension syntax: more careful treatment of position constraints, which enables PIDE markup;
 
wenzelm 
parents: 
46133 
diff
changeset
 | 
481  | 
in concatC $ (mapC $ (fst (abs_tr ctxt p e' false)) $ es) end;  | 
| 
 
85f8d8a8c711
improved list comprehension syntax: more careful treatment of position constraints, which enables PIDE markup;
 
wenzelm 
parents: 
46133 
diff
changeset
 | 
482  | 
|
| 
 
85f8d8a8c711
improved list comprehension syntax: more careful treatment of position constraints, which enables PIDE markup;
 
wenzelm 
parents: 
46133 
diff
changeset
 | 
483  | 
  in [(@{syntax_const "_listcompr"}, lc_tr)] end
 | 
| 24349 | 484  | 
*}  | 
| 
23279
 
e39dd93161d9
tuned list comprehension, changed filter syntax from : to <-
 
nipkow 
parents: 
23246 
diff
changeset
 | 
485  | 
|
| 51272 | 486  | 
ML_val {*
 | 
| 42167 | 487  | 
let  | 
488  | 
    val read = Syntax.read_term @{context};
 | 
|
489  | 
    fun check s1 s2 = read s1 aconv read s2 orelse error ("Check failed: " ^ quote s1);
 | 
|
490  | 
in  | 
|
491  | 
check "[(x,y,z). b]" "if b then [(x, y, z)] else []";  | 
|
492  | 
check "[(x,y,z). x\<leftarrow>xs]" "map (\<lambda>x. (x, y, z)) xs";  | 
|
493  | 
check "[e x y. x\<leftarrow>xs, y\<leftarrow>ys]" "concat (map (\<lambda>x. map (\<lambda>y. e x y) ys) xs)";  | 
|
494  | 
check "[(x,y,z). x<a, x>b]" "if x < a then if b < x then [(x, y, z)] else [] else []";  | 
|
495  | 
check "[(x,y,z). x\<leftarrow>xs, x>b]" "concat (map (\<lambda>x. if b < x then [(x, y, z)] else []) xs)";  | 
|
496  | 
check "[(x,y,z). x<a, x\<leftarrow>xs]" "if x < a then map (\<lambda>x. (x, y, z)) xs else []";  | 
|
497  | 
check "[(x,y). Cons True x \<leftarrow> xs]"  | 
|
498  | 
"concat (map (\<lambda>xa. case xa of [] \<Rightarrow> [] | True # x \<Rightarrow> [(x, y)] | False # x \<Rightarrow> []) xs)";  | 
|
499  | 
check "[(x,y,z). Cons x [] \<leftarrow> xs]"  | 
|
500  | 
"concat (map (\<lambda>xa. case xa of [] \<Rightarrow> [] | [x] \<Rightarrow> [(x, y, z)] | x # aa # lista \<Rightarrow> []) xs)";  | 
|
501  | 
check "[(x,y,z). x<a, x>b, x=d]"  | 
|
502  | 
"if x < a then if b < x then if x = d then [(x, y, z)] else [] else [] else []";  | 
|
503  | 
check "[(x,y,z). x<a, x>b, y\<leftarrow>ys]"  | 
|
504  | 
"if x < a then if b < x then map (\<lambda>y. (x, y, z)) ys else [] else []";  | 
|
505  | 
check "[(x,y,z). x<a, x\<leftarrow>xs,y>b]"  | 
|
506  | 
"if x < a then concat (map (\<lambda>x. if b < y then [(x, y, z)] else []) xs) else []";  | 
|
507  | 
check "[(x,y,z). x<a, x\<leftarrow>xs, y\<leftarrow>ys]"  | 
|
508  | 
"if x < a then concat (map (\<lambda>x. map (\<lambda>y. (x, y, z)) ys) xs) else []";  | 
|
509  | 
check "[(x,y,z). x\<leftarrow>xs, x>b, y<a]"  | 
|
510  | 
"concat (map (\<lambda>x. if b < x then if y < a then [(x, y, z)] else [] else []) xs)";  | 
|
511  | 
check "[(x,y,z). x\<leftarrow>xs, x>b, y\<leftarrow>ys]"  | 
|
512  | 
"concat (map (\<lambda>x. if b < x then map (\<lambda>y. (x, y, z)) ys else []) xs)";  | 
|
513  | 
check "[(x,y,z). x\<leftarrow>xs, y\<leftarrow>ys,y>x]"  | 
|
514  | 
"concat (map (\<lambda>x. concat (map (\<lambda>y. if x < y then [(x, y, z)] else []) ys)) xs)";  | 
|
515  | 
check "[(x,y,z). x\<leftarrow>xs, y\<leftarrow>ys,z\<leftarrow>zs]"  | 
|
516  | 
"concat (map (\<lambda>x. concat (map (\<lambda>y. map (\<lambda>z. (x, y, z)) zs) ys)) xs)"  | 
|
517  | 
end;  | 
|
518  | 
*}  | 
|
519  | 
||
| 35115 | 520  | 
(*  | 
| 24349 | 521  | 
term "[(x,y). x\<leftarrow>xs, let xx = x+x, y\<leftarrow>ys, y \<noteq> xx]"  | 
| 23192 | 522  | 
*)  | 
523  | 
||
| 42167 | 524  | 
|
| 
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
 | 
525  | 
ML {*
 | 
| 
 
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
 | 
526  | 
(* 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
 | 
527  | 
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
 | 
528  | 
|
| 
 
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
 | 
529  | 
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
 | 
530  | 
sig  | 
| 
51717
 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 
wenzelm 
parents: 
51678 
diff
changeset
 | 
531  | 
val simproc : Proof.context -> cterm -> thm option  | 
| 
50422
 
ee729dbd1b7f
avoid ML_file in large theory files to improve performance of dependency discovery of main HOL (approx. 1s CPU time) -- relevant for any application using it, e.g. small paper sessions;
 
wenzelm 
parents: 
50134 
diff
changeset
 | 
532  | 
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
 | 
533  | 
|
| 
 
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
 | 
534  | 
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
 | 
535  | 
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
 | 
536  | 
|
| 
 
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
 | 
537  | 
(* 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
 | 
538  | 
|
| 
 
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
 | 
539  | 
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
 | 
540  | 
(case Thm.term_of ct 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
 | 
541  | 
    Const (@{const_name HOL.Ex}, _) $ Abs _ =>
 | 
| 
 
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
 | 
542  | 
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
 | 
543  | 
| _ => 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
 | 
544  | 
|
| 
 
ee729dbd1b7f
avoid ML_file in large theory files to improve performance of dependency discovery of main HOL (approx. 1s CPU time) -- relevant for any application using it, e.g. small paper sessions;
 
wenzelm 
parents: 
50134 
diff
changeset
 | 
545  | 
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
 | 
546  | 
(case Thm.term_of ct 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
 | 
547  | 
    Const (@{const_name HOL.Ex}, _) $ Abs (_, _, Const (@{const_name HOL.Ex}, _) $ _) =>
 | 
| 
 
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
 | 
548  | 
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
 | 
549  | 
| _ => 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
 | 
550  | 
|
| 
 
ee729dbd1b7f
avoid ML_file in large theory files to improve performance of dependency discovery of main HOL (approx. 1s CPU time) -- relevant for any application using it, e.g. small paper sessions;
 
wenzelm 
parents: 
50134 
diff
changeset
 | 
551  | 
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
 | 
552  | 
(case Thm.term_of ct 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
 | 
553  | 
    Const (@{const_name Set.Collect}, _) $ Abs _ => Conv.arg_conv (Conv.abs_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
 | 
554  | 
  | _ => 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
 | 
555  | 
|
| 
 
ee729dbd1b7f
avoid ML_file in large theory files to improve performance of dependency discovery of main HOL (approx. 1s CPU time) -- relevant for any application using it, e.g. small paper sessions;
 
wenzelm 
parents: 
50134 
diff
changeset
 | 
556  | 
fun 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
 | 
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  | 
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
 | 
559  | 
Conv.try_conv  | 
| 
51315
 
536a5603a138
provide common HOLogic.conj_conv and HOLogic.eq_conv;
 
wenzelm 
parents: 
51314 
diff
changeset
 | 
560  | 
    (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
 | 
561  | 
|
| 
 
ee729dbd1b7f
avoid ML_file in large theory files to improve performance of dependency discovery of main HOL (approx. 1s CPU time) -- relevant for any application using it, e.g. small paper sessions;
 
wenzelm 
parents: 
50134 
diff
changeset
 | 
562  | 
fun right_hand_set_comprehension_conv conv ctxt =  | 
| 
51315
 
536a5603a138
provide common HOLogic.conj_conv and HOLogic.eq_conv;
 
wenzelm 
parents: 
51314 
diff
changeset
 | 
563  | 
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
 | 
564  | 
(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
 | 
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  | 
|
| 
 
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  | 
(* 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
 | 
568  | 
|
| 
 
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  | 
datatype termlets = If | Case of (typ * int)  | 
| 
 
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
 | 
570  | 
|
| 
51717
 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 
wenzelm 
parents: 
51678 
diff
changeset
 | 
571  | 
fun simproc ctxt 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
 | 
572  | 
let  | 
| 
57816
 
d8bbb97689d3
no need for 'set_simps' now that 'datatype_new' generates the desired 'set' property
 
blanchet 
parents: 
57599 
diff
changeset
 | 
573  | 
    val set_Nil_I = @{thm trans} OF [@{thm list.set(1)}, @{thm empty_def}]
 | 
| 
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
 | 
574  | 
    val set_singleton = @{lemma "set [a] = {x. x = a}" by simp}
 | 
| 
 
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  | 
    val inst_Collect_mem_eq = @{lemma "set A = {x. x : set A}" by simp}
 | 
| 
 
ee729dbd1b7f
avoid ML_file in large theory files to improve performance of dependency discovery of main HOL (approx. 1s CPU time) -- relevant for any application using it, e.g. small paper sessions;
 
wenzelm 
parents: 
50134 
diff
changeset
 | 
576  | 
    val del_refl_eq = @{lemma "(t = t & P) == P" by simp}
 | 
| 
 
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  | 
    fun mk_set T = Const (@{const_name List.set}, HOLogic.listT T --> HOLogic.mk_setT 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
 | 
578  | 
    fun dest_set (Const (@{const_name List.set}, _) $ xs) = xs
 | 
| 
 
ee729dbd1b7f
avoid ML_file in large theory files to improve performance of dependency discovery of main HOL (approx. 1s CPU time) -- relevant for any application using it, e.g. small paper sessions;
 
wenzelm 
parents: 
50134 
diff
changeset
 | 
579  | 
    fun dest_singleton_list (Const (@{const_name List.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
 | 
580  | 
          $ t $ (Const (@{const_name List.Nil}, _))) = 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
 | 
581  | 
      | dest_singleton_list t = raise TERM ("dest_singleton_list", [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
 | 
582  | 
(* We check that one case returns a singleton list and all other cases  | 
| 
 
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  | 
return [], and return the index of the one singleton list case *)  | 
| 
 
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  | 
fun possible_index_of_singleton_case cases =  | 
| 
 
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  | 
let  | 
| 
 
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  | 
fun check (i, case_t) s =  | 
| 
 
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  | 
(case strip_abs_body case_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
 | 
588  | 
            (Const (@{const_name List.Nil}, _)) => s
 | 
| 
53412
 
01b804df0a30
list_to_set_comprehension: don't crash in case distinctions on datatypes with even number of constructors
 
traytel 
parents: 
53374 
diff
changeset
 | 
589  | 
| _ => (case s of SOME NONE => SOME (SOME i) | _ => NONE))  | 
| 
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  | 
in  | 
| 
53412
 
01b804df0a30
list_to_set_comprehension: don't crash in case distinctions on datatypes with even number of constructors
 
traytel 
parents: 
53374 
diff
changeset
 | 
591  | 
fold_index check cases (SOME NONE) |> the_default NONE  | 
| 
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
 | 
592  | 
end  | 
| 
54404
 
9f0f1152c875
port list comprehension simproc to 'Ctr_Sugar' abstraction
 
blanchet 
parents: 
54295 
diff
changeset
 | 
593  | 
(* returns (case_expr type index chosen_case constr_name) option *)  | 
| 
50422
 
ee729dbd1b7f
avoid ML_file in large theory files to improve performance of dependency discovery of main HOL (approx. 1s CPU time) -- relevant for any application using it, e.g. small paper sessions;
 
wenzelm 
parents: 
50134 
diff
changeset
 | 
594  | 
fun dest_case case_term =  | 
| 
 
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  | 
let  | 
| 
 
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  | 
val (case_const, args) = strip_comb case_term  | 
| 
 
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  | 
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
 | 
598  | 
(case try dest_Const case_const 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
 | 
599  | 
SOME (c, T) =>  | 
| 
54404
 
9f0f1152c875
port list comprehension simproc to 'Ctr_Sugar' abstraction
 
blanchet 
parents: 
54295 
diff
changeset
 | 
600  | 
(case Ctr_Sugar.ctr_sugar_of_case ctxt c of  | 
| 
 
9f0f1152c875
port list comprehension simproc to 'Ctr_Sugar' abstraction
 
blanchet 
parents: 
54295 
diff
changeset
 | 
601  | 
              SOME {ctrs, ...} =>
 | 
| 
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
 | 
602  | 
(case possible_index_of_singleton_case (fst (split_last args)) 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
 | 
603  | 
SOME i =>  | 
| 
 
ee729dbd1b7f
avoid ML_file in large theory files to improve performance of dependency discovery of main HOL (approx. 1s CPU time) -- relevant for any application using it, e.g. small paper sessions;
 
wenzelm 
parents: 
50134 
diff
changeset
 | 
604  | 
let  | 
| 
54404
 
9f0f1152c875
port list comprehension simproc to 'Ctr_Sugar' abstraction
 
blanchet 
parents: 
54295 
diff
changeset
 | 
605  | 
val constr_names = map (fst o dest_Const) ctrs  | 
| 
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
 | 
606  | 
val (Ts, _) = strip_type 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
 | 
607  | 
val T' = List.last Ts  | 
| 
54404
 
9f0f1152c875
port list comprehension simproc to 'Ctr_Sugar' abstraction
 
blanchet 
parents: 
54295 
diff
changeset
 | 
608  | 
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
 | 
609  | 
| 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
 | 
610  | 
| 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
 | 
611  | 
| 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
 | 
612  | 
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
 | 
613  | 
(* returns condition continuing term option *)  | 
| 
 
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
 | 
614  | 
    fun dest_if (Const (@{const_name If}, _) $ cond $ then_t $ Const (@{const_name Nil}, _)) =
 | 
| 
 
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
 | 
615  | 
SOME (cond, then_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
 | 
616  | 
| dest_if _ = 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
 | 
617  | 
fun tac _ [] = rtac set_singleton 1 ORELSE rtac inst_Collect_mem_eq 1  | 
| 
 
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
 | 
618  | 
| tac ctxt (If :: 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
 | 
619  | 
          Splitter.split_tac [@{thm split_if}] 1
 | 
| 
 
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
 | 
620  | 
          THEN rtac @{thm conjI} 1
 | 
| 
 
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
 | 
621  | 
          THEN rtac @{thm impI} 1
 | 
| 
 
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
 | 
622  | 
          THEN Subgoal.FOCUS (fn {prems, context, ...} =>
 | 
| 
 
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
 | 
623  | 
CONVERSION (right_hand_set_comprehension_conv (K  | 
| 
51315
 
536a5603a138
provide common HOLogic.conj_conv and HOLogic.eq_conv;
 
wenzelm 
parents: 
51314 
diff
changeset
 | 
624  | 
              (HOLogic.conj_conv (Conv.rewr_conv (List.last prems RS @{thm Eq_TrueI})) 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
 | 
625  | 
then_conv  | 
| 
 
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
 | 
626  | 
               rewr_conv' @{lemma "(True & P) = P" by simp})) context) 1) ctxt 1
 | 
| 
 
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
 | 
627  | 
THEN tac ctxt 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
 | 
628  | 
          THEN rtac @{thm impI} 1
 | 
| 
 
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
 | 
629  | 
          THEN Subgoal.FOCUS (fn {prems, context, ...} =>
 | 
| 
 
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
 | 
630  | 
CONVERSION (right_hand_set_comprehension_conv (K  | 
| 
51315
 
536a5603a138
provide common HOLogic.conj_conv and HOLogic.eq_conv;
 
wenzelm 
parents: 
51314 
diff
changeset
 | 
631  | 
                (HOLogic.conj_conv (Conv.rewr_conv (List.last prems RS @{thm Eq_FalseI})) 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
 | 
632  | 
                 then_conv rewr_conv' @{lemma "(False & P) = False" by simp})) context) 1) ctxt 1
 | 
| 
 
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
 | 
633  | 
THEN rtac set_Nil_I 1  | 
| 
 
ee729dbd1b7f
avoid ML_file in large theory files to improve performance of dependency discovery of main HOL (approx. 1s CPU time) -- relevant for any application using it, e.g. small paper sessions;
 
wenzelm 
parents: 
50134 
diff
changeset
 | 
634  | 
| tac ctxt (Case (T, i) :: 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
 | 
635  | 
let  | 
| 
54404
 
9f0f1152c875
port list comprehension simproc to 'Ctr_Sugar' abstraction
 
blanchet 
parents: 
54295 
diff
changeset
 | 
636  | 
            val SOME {injects, distincts, case_thms, split, ...} =
 | 
| 
 
9f0f1152c875
port list comprehension simproc to 'Ctr_Sugar' abstraction
 
blanchet 
parents: 
54295 
diff
changeset
 | 
637  | 
Ctr_Sugar.ctr_sugar_of ctxt (fst (dest_Type 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
 | 
638  | 
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
 | 
639  | 
(* do case distinction *)  | 
| 
54404
 
9f0f1152c875
port list comprehension simproc to 'Ctr_Sugar' abstraction
 
blanchet 
parents: 
54295 
diff
changeset
 | 
640  | 
Splitter.split_tac [split] 1  | 
| 
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
 | 
641  | 
THEN EVERY (map_index (fn (i', _) =>  | 
| 
54404
 
9f0f1152c875
port list comprehension simproc to 'Ctr_Sugar' abstraction
 
blanchet 
parents: 
54295 
diff
changeset
 | 
642  | 
              (if i' < length case_thms - 1 then rtac @{thm conjI} 1 else all_tac)
 | 
| 
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
 | 
643  | 
              THEN REPEAT_DETERM (rtac @{thm allI} 1)
 | 
| 
 
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
 | 
644  | 
              THEN rtac @{thm impI} 1
 | 
| 
 
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
 | 
645  | 
THEN (if i' = i then  | 
| 
 
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  | 
(* continue recursively *)  | 
| 
 
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  | 
                Subgoal.FOCUS (fn {prems, context, ...} =>
 | 
| 
 
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
 | 
648  | 
CONVERSION (Thm.eta_conversion then_conv right_hand_set_comprehension_conv (K  | 
| 
51315
 
536a5603a138
provide common HOLogic.conj_conv and HOLogic.eq_conv;
 
wenzelm 
parents: 
51314 
diff
changeset
 | 
649  | 
((HOLogic.conj_conv  | 
| 
 
536a5603a138
provide common HOLogic.conj_conv and HOLogic.eq_conv;
 
wenzelm 
parents: 
51314 
diff
changeset
 | 
650  | 
(HOLogic.eq_conv Conv.all_conv (rewr_conv' (List.last prems)) then_conv  | 
| 
54404
 
9f0f1152c875
port list comprehension simproc to 'Ctr_Sugar' abstraction
 
blanchet 
parents: 
54295 
diff
changeset
 | 
651  | 
(Conv.try_conv (Conv.rewrs_conv (map mk_meta_eq injects))))  | 
| 
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
 | 
652  | 
Conv.all_conv)  | 
| 
 
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
 | 
653  | 
then_conv (Conv.try_conv (Conv.rewr_conv del_refl_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
 | 
654  | 
then_conv conjunct_assoc_conv)) context  | 
| 
51315
 
536a5603a138
provide common HOLogic.conj_conv and HOLogic.eq_conv;
 
wenzelm 
parents: 
51314 
diff
changeset
 | 
655  | 
then_conv (HOLogic.Trueprop_conv (HOLogic.eq_conv Conv.all_conv (Collect_conv (fn (_, ctxt) =>  | 
| 
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
 | 
656  | 
Conv.repeat_conv  | 
| 
 
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
 | 
657  | 
(all_but_last_exists_conv  | 
| 
 
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
 | 
658  | 
(K (rewr_conv'  | 
| 
 
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
 | 
659  | 
                            @{lemma "(EX x. x = t & P x) = P t" by simp})) ctxt)) context)))) 1) ctxt 1
 | 
| 
 
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
 | 
660  | 
THEN tac ctxt 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
 | 
661  | 
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
 | 
662  | 
                Subgoal.FOCUS (fn {prems, context, ...} =>
 | 
| 
 
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
 | 
663  | 
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
 | 
664  | 
(right_hand_set_comprehension_conv (K  | 
| 
51315
 
536a5603a138
provide common HOLogic.conj_conv and HOLogic.eq_conv;
 
wenzelm 
parents: 
51314 
diff
changeset
 | 
665  | 
(HOLogic.conj_conv  | 
| 
 
536a5603a138
provide common HOLogic.conj_conv and HOLogic.eq_conv;
 
wenzelm 
parents: 
51314 
diff
changeset
 | 
666  | 
((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
 | 
667  | 
(rewr_conv' (List.last prems))) then_conv  | 
| 
54404
 
9f0f1152c875
port list comprehension simproc to 'Ctr_Sugar' abstraction
 
blanchet 
parents: 
54295 
diff
changeset
 | 
668  | 
                          (Conv.rewrs_conv (map (fn th => th RS @{thm Eq_FalseI}) distincts)))
 | 
| 
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
 | 
669  | 
Conv.all_conv then_conv  | 
| 
 
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
 | 
670  | 
                        (rewr_conv' @{lemma "(False & P) = False" by simp}))) context then_conv
 | 
| 
51314
 
eac4bb5adbf9
just one HOLogic.Trueprop_conv, with regular exception CTERM;
 
wenzelm 
parents: 
51272 
diff
changeset
 | 
671  | 
HOLogic.Trueprop_conv  | 
| 
51315
 
536a5603a138
provide common HOLogic.conj_conv and HOLogic.eq_conv;
 
wenzelm 
parents: 
51314 
diff
changeset
 | 
672  | 
(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
 | 
673  | 
(Collect_conv (fn (_, 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
 | 
674  | 
Conv.repeat_conv  | 
| 
 
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
 | 
675  | 
(Conv.bottom_conv  | 
| 
 
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
 | 
676  | 
(K (rewr_conv'  | 
| 
 
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
 | 
677  | 
                                  @{lemma "(EX x. P) = P" by simp})) ctxt)) context))) 1) ctxt 1
 | 
| 
54404
 
9f0f1152c875
port list comprehension simproc to 'Ctr_Sugar' abstraction
 
blanchet 
parents: 
54295 
diff
changeset
 | 
678  | 
THEN rtac set_Nil_I 1)) case_thms)  | 
| 
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
 | 
679  | 
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
 | 
680  | 
fun make_inner_eqs bound_vs Tis eqs 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
 | 
681  | 
(case dest_case t of  | 
| 
54404
 
9f0f1152c875
port list comprehension simproc to 'Ctr_Sugar' abstraction
 
blanchet 
parents: 
54295 
diff
changeset
 | 
682  | 
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
 | 
683  | 
let  | 
| 52131 | 684  | 
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
 | 
685  | 
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
 | 
686  | 
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
 | 
687  | 
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
 | 
688  | 
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
 | 
689  | 
(Const (constr_name, map snd vs ---> T), map Bound (((length vs) - 1) downto 0))  | 
| 
 
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
 | 
690  | 
            val constr_eq = Const (@{const_name HOL.eq}, T --> T --> @{typ bool}) $ constr_t $ 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
 | 
691  | 
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
 | 
692  | 
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
 | 
693  | 
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
 | 
694  | 
| 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
 | 
695  | 
(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
 | 
696  | 
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
 | 
697  | 
| 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
 | 
698  | 
if eqs = [] then NONE (* no rewriting, nothing to be done *)  | 
| 
 
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
 | 
699  | 
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
 | 
700  | 
let  | 
| 
 
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
 | 
701  | 
                val Type (@{type_name List.list}, [rT]) = fastype_of1 (map snd 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
 | 
702  | 
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
 | 
703  | 
(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
 | 
704  | 
SOME 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
 | 
705  | 
                      Const (@{const_name HOL.eq}, rT --> rT --> @{typ bool}) $
 | 
| 
 
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
 | 
706  | 
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
 | 
707  | 
| 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
 | 
708  | 
                      Const (@{const_name Set.member}, rT --> HOLogic.mk_setT rT --> @{typ bool}) $
 | 
| 
 
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
 | 
709  | 
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
 | 
710  | 
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
 | 
711  | 
((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
 | 
712  | 
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
 | 
713  | 
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
 | 
714  | 
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
 | 
715  | 
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
 | 
716  | 
(rev bound_vs) (fold (curry HOLogic.mk_conj) eqs' 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
 | 
717  | 
val lhs = term_of redex  | 
| 
 
ee729dbd1b7f
avoid ML_file in large theory files to improve performance of dependency discovery of main HOL (approx. 1s CPU time) -- relevant for any application using it, e.g. small paper sessions;
 
wenzelm 
parents: 
50134 
diff
changeset
 | 
718  | 
                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
 | 
719  | 
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
 | 
720  | 
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
 | 
721  | 
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
 | 
722  | 
((Goal.prove ctxt [] [] rewrite_rule_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
 | 
723  | 
                    (fn {context, ...} => tac context (rev Tis))) RS @{thm eq_reflection})
 | 
| 
 
ee729dbd1b7f
avoid ML_file in large theory files to improve performance of dependency discovery of main HOL (approx. 1s CPU time) -- relevant for any application using it, e.g. small paper sessions;
 
wenzelm 
parents: 
50134 
diff
changeset
 | 
724  | 
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
 | 
725  | 
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
 | 
726  | 
make_inner_eqs [] [] [] (dest_set (term_of redex))  | 
| 
 
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  | 
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
 | 
728  | 
|
| 
 
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  | 
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
 | 
730  | 
*}  | 
| 
41463
 
edbf0a86fb1c
adding simproc to rewrite list comprehensions to set comprehensions; adopting proofs
 
bulwahn 
parents: 
41372 
diff
changeset
 | 
731  | 
|
| 
 
edbf0a86fb1c
adding simproc to rewrite list comprehensions to set comprehensions; adopting proofs
 
bulwahn 
parents: 
41372 
diff
changeset
 | 
732  | 
simproc_setup list_to_set_comprehension ("set xs") = {* K List_to_Set_Comprehension.simproc *}
 | 
| 
 
edbf0a86fb1c
adding simproc to rewrite list comprehensions to set comprehensions; adopting proofs
 
bulwahn 
parents: 
41372 
diff
changeset
 | 
733  | 
|
| 
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
 | 
734  | 
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
 | 
735  | 
|
| 
 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 
haftmann 
parents: 
46125 
diff
changeset
 | 
736  | 
hide_const (open) coset  | 
| 35115 | 737  | 
|
| 
49948
 
744934b818c7
moved quite generic material from theory Enum to more appropriate places
 
haftmann 
parents: 
49808 
diff
changeset
 | 
738  | 
|
| 
21061
 
580dfc999ef6
added normal post setup; cleaned up "execution" constants
 
haftmann 
parents: 
21046 
diff
changeset
 | 
739  | 
subsubsection {* @{const Nil} and @{const Cons} *}
 | 
| 
 
580dfc999ef6
added normal post setup; cleaned up "execution" constants
 
haftmann 
parents: 
21046 
diff
changeset
 | 
740  | 
|
| 
 
580dfc999ef6
added normal post setup; cleaned up "execution" constants
 
haftmann 
parents: 
21046 
diff
changeset
 | 
741  | 
lemma not_Cons_self [simp]:  | 
| 
 
580dfc999ef6
added normal post setup; cleaned up "execution" constants
 
haftmann 
parents: 
21046 
diff
changeset
 | 
742  | 
"xs \<noteq> x # xs"  | 
| 13145 | 743  | 
by (induct xs) auto  | 
| 13114 | 744  | 
|
| 41697 | 745  | 
lemma not_Cons_self2 [simp]:  | 
746  | 
"x # xs \<noteq> xs"  | 
|
747  | 
by (rule not_Cons_self [symmetric])  | 
|
| 13114 | 748  | 
|
| 13142 | 749  | 
lemma neq_Nil_conv: "(xs \<noteq> []) = (\<exists>y ys. xs = y # ys)"  | 
| 13145 | 750  | 
by (induct xs) auto  | 
| 13114 | 751  | 
|
| 53689 | 752  | 
lemma tl_Nil: "tl xs = [] \<longleftrightarrow> xs = [] \<or> (EX x. xs = [x])"  | 
753  | 
by (cases xs) auto  | 
|
754  | 
||
755  | 
lemma Nil_tl: "[] = tl xs \<longleftrightarrow> xs = [] \<or> (EX x. xs = [x])"  | 
|
756  | 
by (cases xs) auto  | 
|
757  | 
||
| 13142 | 758  | 
lemma length_induct:  | 
| 
21061
 
580dfc999ef6
added normal post setup; cleaned up "execution" constants
 
haftmann 
parents: 
21046 
diff
changeset
 | 
759  | 
"(\<And>xs. \<forall>ys. length ys < length xs \<longrightarrow> P ys \<Longrightarrow> P xs) \<Longrightarrow> P xs"  | 
| 53689 | 760  | 
by (fact measure_induct)  | 
| 13114 | 761  | 
|
| 37289 | 762  | 
lemma list_nonempty_induct [consumes 1, case_names single cons]:  | 
763  | 
assumes "xs \<noteq> []"  | 
|
764  | 
assumes single: "\<And>x. P [x]"  | 
|
765  | 
assumes cons: "\<And>x xs. xs \<noteq> [] \<Longrightarrow> P xs \<Longrightarrow> P (x # xs)"  | 
|
766  | 
shows "P xs"  | 
|
767  | 
using `xs \<noteq> []` proof (induct xs)  | 
|
768  | 
case Nil then show ?case by simp  | 
|
769  | 
next  | 
|
| 
53374
 
a14d2a854c02
tuned proofs -- clarified flow of facts wrt. calculation;
 
wenzelm 
parents: 
53017 
diff
changeset
 | 
770  | 
case (Cons x xs)  | 
| 
 
a14d2a854c02
tuned proofs -- clarified flow of facts wrt. calculation;
 
wenzelm 
parents: 
53017 
diff
changeset
 | 
771  | 
show ?case  | 
| 
 
a14d2a854c02
tuned proofs -- clarified flow of facts wrt. calculation;
 
wenzelm 
parents: 
53017 
diff
changeset
 | 
772  | 
proof (cases xs)  | 
| 
 
a14d2a854c02
tuned proofs -- clarified flow of facts wrt. calculation;
 
wenzelm 
parents: 
53017 
diff
changeset
 | 
773  | 
case Nil  | 
| 
 
a14d2a854c02
tuned proofs -- clarified flow of facts wrt. calculation;
 
wenzelm 
parents: 
53017 
diff
changeset
 | 
774  | 
with single show ?thesis by simp  | 
| 37289 | 775  | 
next  | 
| 
53374
 
a14d2a854c02
tuned proofs -- clarified flow of facts wrt. calculation;
 
wenzelm 
parents: 
53017 
diff
changeset
 | 
776  | 
case Cons  | 
| 
 
a14d2a854c02
tuned proofs -- clarified flow of facts wrt. calculation;
 
wenzelm 
parents: 
53017 
diff
changeset
 | 
777  | 
show ?thesis  | 
| 
 
a14d2a854c02
tuned proofs -- clarified flow of facts wrt. calculation;
 
wenzelm 
parents: 
53017 
diff
changeset
 | 
778  | 
proof (rule cons)  | 
| 
 
a14d2a854c02
tuned proofs -- clarified flow of facts wrt. calculation;
 
wenzelm 
parents: 
53017 
diff
changeset
 | 
779  | 
from Cons show "xs \<noteq> []" by simp  | 
| 
 
a14d2a854c02
tuned proofs -- clarified flow of facts wrt. calculation;
 
wenzelm 
parents: 
53017 
diff
changeset
 | 
780  | 
with Cons.hyps show "P xs" .  | 
| 
 
a14d2a854c02
tuned proofs -- clarified flow of facts wrt. calculation;
 
wenzelm 
parents: 
53017 
diff
changeset
 | 
781  | 
qed  | 
| 37289 | 782  | 
qed  | 
783  | 
qed  | 
|
784  | 
||
| 45714 | 785  | 
lemma inj_split_Cons: "inj_on (\<lambda>(xs, n). n#xs) X"  | 
786  | 
by (auto intro!: inj_onI)  | 
|
| 13114 | 787  | 
|
| 
49948
 
744934b818c7
moved quite generic material from theory Enum to more appropriate places
 
haftmann 
parents: 
49808 
diff
changeset
 | 
788  | 
|
| 
21061
 
580dfc999ef6
added normal post setup; cleaned up "execution" constants
 
haftmann 
parents: 
21046 
diff
changeset
 | 
789  | 
subsubsection {* @{const length} *}
 | 
| 13114 | 790  | 
|
| 13142 | 791  | 
text {*
 | 
| 
21061
 
580dfc999ef6
added normal post setup; cleaned up "execution" constants
 
haftmann 
parents: 
21046 
diff
changeset
 | 
792  | 
  Needs to come before @{text "@"} because of theorem @{text
 | 
| 
 
580dfc999ef6
added normal post setup; cleaned up "execution" constants
 
haftmann 
parents: 
21046 
diff
changeset
 | 
793  | 
append_eq_append_conv}.  | 
| 13142 | 794  | 
*}  | 
| 13114 | 795  | 
|
| 13142 | 796  | 
lemma length_append [simp]: "length (xs @ ys) = length xs + length ys"  | 
| 13145 | 797  | 
by (induct xs) auto  | 
| 13114 | 798  | 
|
| 13142 | 799  | 
lemma length_map [simp]: "length (map f xs) = length xs"  | 
| 13145 | 800  | 
by (induct xs) auto  | 
| 13114 | 801  | 
|
| 13142 | 802  | 
lemma length_rev [simp]: "length (rev xs) = length xs"  | 
| 13145 | 803  | 
by (induct xs) auto  | 
| 13114 | 804  | 
|
| 13142 | 805  | 
lemma length_tl [simp]: "length (tl xs) = length xs - 1"  | 
| 13145 | 806  | 
by (cases xs) auto  | 
| 13114 | 807  | 
|
| 13142 | 808  | 
lemma length_0_conv [iff]: "(length xs = 0) = (xs = [])"  | 
| 13145 | 809  | 
by (induct xs) auto  | 
| 13114 | 810  | 
|
| 13142 | 811  | 
lemma length_greater_0_conv [iff]: "(0 < length xs) = (xs \<noteq> [])"  | 
| 13145 | 812  | 
by (induct xs) auto  | 
| 13114 | 813  | 
|
| 23479 | 814  | 
lemma length_pos_if_in_set: "x : set xs \<Longrightarrow> length xs > 0"  | 
815  | 
by auto  | 
|
816  | 
||
| 13114 | 817  | 
lemma length_Suc_conv:  | 
| 13145 | 818  | 
"(length xs = Suc n) = (\<exists>y ys. xs = y # ys \<and> length ys = n)"  | 
819  | 
by (induct xs) auto  | 
|
| 13142 | 820  | 
|
| 14025 | 821  | 
lemma Suc_length_conv:  | 
822  | 
"(Suc n = length xs) = (\<exists>y ys. xs = y # ys \<and> length ys = n)"  | 
|
| 14208 | 823  | 
apply (induct xs, simp, simp)  | 
| 14025 | 824  | 
apply blast  | 
825  | 
done  | 
|
826  | 
||
| 
25221
 
5ded95dda5df
append/member: more light-weight way to declare authentic syntax;
 
wenzelm 
parents: 
25215 
diff
changeset
 | 
827  | 
lemma impossible_Cons: "length xs <= length ys ==> xs = x # ys = False"  | 
| 
 
5ded95dda5df
append/member: more light-weight way to declare authentic syntax;
 
wenzelm 
parents: 
25215 
diff
changeset
 | 
828  | 
by (induct xs) auto  | 
| 
 
5ded95dda5df
append/member: more light-weight way to declare authentic syntax;
 
wenzelm 
parents: 
25215 
diff
changeset
 | 
829  | 
|
| 
26442
 
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
 
haftmann 
parents: 
26300 
diff
changeset
 | 
830  | 
lemma list_induct2 [consumes 1, case_names Nil Cons]:  | 
| 
 
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
 
haftmann 
parents: 
26300 
diff
changeset
 | 
831  | 
"length xs = length ys \<Longrightarrow> P [] [] \<Longrightarrow>  | 
| 
 
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
 
haftmann 
parents: 
26300 
diff
changeset
 | 
832  | 
(\<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
 | 
833  | 
\<Longrightarrow> P xs ys"  | 
| 
 
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
 
haftmann 
parents: 
26300 
diff
changeset
 | 
834  | 
proof (induct xs arbitrary: ys)  | 
| 
 
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
 
haftmann 
parents: 
26300 
diff
changeset
 | 
835  | 
case Nil then show ?case by simp  | 
| 
 
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
 
haftmann 
parents: 
26300 
diff
changeset
 | 
836  | 
next  | 
| 
 
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
 
haftmann 
parents: 
26300 
diff
changeset
 | 
837  | 
case (Cons x xs ys) then show ?case by (cases ys) simp_all  | 
| 
 
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
 
haftmann 
parents: 
26300 
diff
changeset
 | 
838  | 
qed  | 
| 
 
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
 
haftmann 
parents: 
26300 
diff
changeset
 | 
839  | 
|
| 
 
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
 
haftmann 
parents: 
26300 
diff
changeset
 | 
840  | 
lemma list_induct3 [consumes 2, case_names Nil Cons]:  | 
| 
 
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
 
haftmann 
parents: 
26300 
diff
changeset
 | 
841  | 
"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
 | 
842  | 
(\<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
 | 
843  | 
\<Longrightarrow> P xs ys zs"  | 
| 
 
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
 
haftmann 
parents: 
26300 
diff
changeset
 | 
844  | 
proof (induct xs arbitrary: ys zs)  | 
| 
 
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
 
haftmann 
parents: 
26300 
diff
changeset
 | 
845  | 
case Nil then show ?case by simp  | 
| 
 
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
 
haftmann 
parents: 
26300 
diff
changeset
 | 
846  | 
next  | 
| 
 
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
 
haftmann 
parents: 
26300 
diff
changeset
 | 
847  | 
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
 | 
848  | 
(cases zs, simp_all)  | 
| 
 
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
 
haftmann 
parents: 
26300 
diff
changeset
 | 
849  | 
qed  | 
| 13114 | 850  | 
|
| 
36154
 
11c6106d7787
Respectfullness and preservation of list_rel
 
Cezary Kaliszyk <kaliszyk@in.tum.de> 
parents: 
35828 
diff
changeset
 | 
851  | 
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
 | 
852  | 
"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
 | 
853  | 
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
 | 
854  | 
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
 | 
855  | 
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
 | 
856  | 
proof (induct xs arbitrary: ys zs ws)  | 
| 
 
11c6106d7787
Respectfullness and preservation of list_rel
 
Cezary Kaliszyk <kaliszyk@in.tum.de> 
parents: 
35828 
diff
changeset
 | 
857  | 
case Nil then show ?case by simp  | 
| 
 
11c6106d7787
Respectfullness and preservation of list_rel
 
Cezary Kaliszyk <kaliszyk@in.tum.de> 
parents: 
35828 
diff
changeset
 | 
858  | 
next  | 
| 
 
11c6106d7787
Respectfullness and preservation of list_rel
 
Cezary Kaliszyk <kaliszyk@in.tum.de> 
parents: 
35828 
diff
changeset
 | 
859  | 
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
 | 
860  | 
qed  | 
| 
 
11c6106d7787
Respectfullness and preservation of list_rel
 
Cezary Kaliszyk <kaliszyk@in.tum.de> 
parents: 
35828 
diff
changeset
 | 
861  | 
|
| 
22493
 
db930e490fe5
added another rule for simultaneous induction, and lemmas for zip
 
krauss 
parents: 
22422 
diff
changeset
 | 
862  | 
lemma list_induct2':  | 
| 
 
db930e490fe5
added another rule for simultaneous induction, and lemmas for zip
 
krauss 
parents: 
22422 
diff
changeset
 | 
863  | 
"\<lbrakk> P [] [];  | 
| 
 
db930e490fe5
added another rule for simultaneous induction, and lemmas for zip
 
krauss 
parents: 
22422 
diff
changeset
 | 
864  | 
\<And>x xs. P (x#xs) [];  | 
| 
 
db930e490fe5
added another rule for simultaneous induction, and lemmas for zip
 
krauss 
parents: 
22422 
diff
changeset
 | 
865  | 
\<And>y ys. P [] (y#ys);  | 
| 
 
db930e490fe5
added another rule for simultaneous induction, and lemmas for zip
 
krauss 
parents: 
22422 
diff
changeset
 | 
866  | 
\<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
 | 
867  | 
\<Longrightarrow> P xs ys"  | 
| 
 
db930e490fe5
added another rule for simultaneous induction, and lemmas for zip
 
krauss 
parents: 
22422 
diff
changeset
 | 
868  | 
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
 | 
869  | 
|
| 
55524
 
f41ef840f09d
folded 'list_all2' with the relator generated by 'datatype_new'
 
blanchet 
parents: 
55473 
diff
changeset
 | 
870  | 
lemma list_all2_iff:  | 
| 
 
f41ef840f09d
folded 'list_all2' with the relator generated by 'datatype_new'
 
blanchet 
parents: 
55473 
diff
changeset
 | 
871  | 
"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
 | 
872  | 
by (induct xs ys rule: list_induct2') auto  | 
| 
 
f41ef840f09d
folded 'list_all2' with the relator generated by 'datatype_new'
 
blanchet 
parents: 
55473 
diff
changeset
 | 
873  | 
|
| 
22143
 
cf58486ca11b
Added simproc list_neq (prompted by an application)
 
nipkow 
parents: 
21911 
diff
changeset
 | 
874  | 
lemma neq_if_length_neq: "length xs \<noteq> length ys \<Longrightarrow> (xs = ys) == False"  | 
| 24349 | 875  | 
by (rule Eq_FalseI) auto  | 
| 24037 | 876  | 
|
877  | 
simproc_setup list_neq ("(xs::'a list) = ys") = {*
 | 
|
| 
22143
 
cf58486ca11b
Added simproc list_neq (prompted by an application)
 
nipkow 
parents: 
21911 
diff
changeset
 | 
878  | 
(*  | 
| 
 
cf58486ca11b
Added simproc list_neq (prompted by an application)
 
nipkow 
parents: 
21911 
diff
changeset
 | 
879  | 
Reduces xs=ys to False if xs and ys cannot be of the same length.  | 
| 
 
cf58486ca11b
Added simproc list_neq (prompted by an application)
 
nipkow 
parents: 
21911 
diff
changeset
 | 
880  | 
This is the case if the atomic sublists of one are a submultiset  | 
| 
 
cf58486ca11b
Added simproc list_neq (prompted by an application)
 
nipkow 
parents: 
21911 
diff
changeset
 | 
881  | 
of those of the other list and there are fewer Cons's in one than the other.  | 
| 
 
cf58486ca11b
Added simproc list_neq (prompted by an application)
 
nipkow 
parents: 
21911 
diff
changeset
 | 
882  | 
*)  | 
| 24037 | 883  | 
|
884  | 
let  | 
|
| 
22143
 
cf58486ca11b
Added simproc list_neq (prompted by an application)
 
nipkow 
parents: 
21911 
diff
changeset
 | 
885  | 
|
| 29856 | 886  | 
fun len (Const(@{const_name Nil},_)) acc = acc
 | 
887  | 
  | len (Const(@{const_name Cons},_) $ _ $ xs) (ts,n) = len xs (ts,n+1)
 | 
|
888  | 
  | len (Const(@{const_name append},_) $ xs $ ys) acc = len xs (len ys acc)
 | 
|
889  | 
  | len (Const(@{const_name rev},_) $ xs) acc = len xs acc
 | 
|
890  | 
  | len (Const(@{const_name map},_) $ _ $ xs) acc = len xs acc
 | 
|
| 
22143
 
cf58486ca11b
Added simproc list_neq (prompted by an application)
 
nipkow 
parents: 
21911 
diff
changeset
 | 
891  | 
| len t (ts,n) = (t::ts,n);  | 
| 
 
cf58486ca11b
Added simproc list_neq (prompted by an application)
 
nipkow 
parents: 
21911 
diff
changeset
 | 
892  | 
|
| 
51717
 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 
wenzelm 
parents: 
51678 
diff
changeset
 | 
893  | 
val ss = simpset_of @{context};
 | 
| 
 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 
wenzelm 
parents: 
51678 
diff
changeset
 | 
894  | 
|
| 
 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 
wenzelm 
parents: 
51678 
diff
changeset
 | 
895  | 
fun list_neq ctxt ct =  | 
| 
22143
 
cf58486ca11b
Added simproc list_neq (prompted by an application)
 
nipkow 
parents: 
21911 
diff
changeset
 | 
896  | 
let  | 
| 24037 | 897  | 
val (Const(_,eqT) $ lhs $ rhs) = Thm.term_of ct;  | 
| 
22143
 
cf58486ca11b
Added simproc list_neq (prompted by an application)
 
nipkow 
parents: 
21911 
diff
changeset
 | 
898  | 
val (ls,m) = len lhs ([],0) and (rs,n) = len rhs ([],0);  | 
| 
 
cf58486ca11b
Added simproc list_neq (prompted by an application)
 
nipkow 
parents: 
21911 
diff
changeset
 | 
899  | 
fun prove_neq() =  | 
| 
 
cf58486ca11b
Added simproc list_neq (prompted by an application)
 
nipkow 
parents: 
21911 
diff
changeset
 | 
900  | 
let  | 
| 
 
cf58486ca11b
Added simproc list_neq (prompted by an application)
 
nipkow 
parents: 
21911 
diff
changeset
 | 
901  | 
val Type(_,listT::_) = eqT;  | 
| 22994 | 902  | 
val size = HOLogic.size_const listT;  | 
| 
22143
 
cf58486ca11b
Added simproc list_neq (prompted by an application)
 
nipkow 
parents: 
21911 
diff
changeset
 | 
903  | 
val eq_len = HOLogic.mk_eq (size $ lhs, size $ rhs);  | 
| 
 
cf58486ca11b
Added simproc list_neq (prompted by an application)
 
nipkow 
parents: 
21911 
diff
changeset
 | 
904  | 
val neq_len = HOLogic.mk_Trueprop (HOLogic.Not $ eq_len);  | 
| 
51717
 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 
wenzelm 
parents: 
51678 
diff
changeset
 | 
905  | 
val thm = Goal.prove ctxt [] [] neq_len  | 
| 
 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 
wenzelm 
parents: 
51678 
diff
changeset
 | 
906  | 
(K (simp_tac (put_simpset ss ctxt) 1));  | 
| 22633 | 907  | 
      in SOME (thm RS @{thm neq_if_length_neq}) end
 | 
| 
22143
 
cf58486ca11b
Added simproc list_neq (prompted by an application)
 
nipkow 
parents: 
21911 
diff
changeset
 | 
908  | 
in  | 
| 23214 | 909  | 
if m < n andalso submultiset (op aconv) (ls,rs) orelse  | 
910  | 
n < m andalso submultiset (op aconv) (rs,ls)  | 
|
| 
22143
 
cf58486ca11b
Added simproc list_neq (prompted by an application)
 
nipkow 
parents: 
21911 
diff
changeset
 | 
911  | 
then prove_neq() else NONE  | 
| 
 
cf58486ca11b
Added simproc list_neq (prompted by an application)
 
nipkow 
parents: 
21911 
diff
changeset
 | 
912  | 
end;  | 
| 
51717
 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 
wenzelm 
parents: 
51678 
diff
changeset
 | 
913  | 
in K list_neq end;  | 
| 
22143
 
cf58486ca11b
Added simproc list_neq (prompted by an application)
 
nipkow 
parents: 
21911 
diff
changeset
 | 
914  | 
*}  | 
| 
 
cf58486ca11b
Added simproc list_neq (prompted by an application)
 
nipkow 
parents: 
21911 
diff
changeset
 | 
915  | 
|
| 
 
cf58486ca11b
Added simproc list_neq (prompted by an application)
 
nipkow 
parents: 
21911 
diff
changeset
 | 
916  | 
|
| 15392 | 917  | 
subsubsection {* @{text "@"} -- append *}
 | 
| 13114 | 918  | 
|
| 13142 | 919  | 
lemma append_assoc [simp]: "(xs @ ys) @ zs = xs @ (ys @ zs)"  | 
| 13145 | 920  | 
by (induct xs) auto  | 
| 13114 | 921  | 
|
| 13142 | 922  | 
lemma append_Nil2 [simp]: "xs @ [] = xs"  | 
| 13145 | 923  | 
by (induct xs) auto  | 
| 3507 | 924  | 
|
| 13142 | 925  | 
lemma append_is_Nil_conv [iff]: "(xs @ ys = []) = (xs = [] \<and> ys = [])"  | 
| 13145 | 926  | 
by (induct xs) auto  | 
| 13114 | 927  | 
|
| 13142 | 928  | 
lemma Nil_is_append_conv [iff]: "([] = xs @ ys) = (xs = [] \<and> ys = [])"  | 
| 13145 | 929  | 
by (induct xs) auto  | 
| 13114 | 930  | 
|
| 13142 | 931  | 
lemma append_self_conv [iff]: "(xs @ ys = xs) = (ys = [])"  | 
| 13145 | 932  | 
by (induct xs) auto  | 
| 13114 | 933  | 
|
| 13142 | 934  | 
lemma self_append_conv [iff]: "(xs = xs @ ys) = (ys = [])"  | 
| 13145 | 935  | 
by (induct xs) auto  | 
| 13114 | 936  | 
|
| 
54147
 
97a8ff4e4ac9
killed most "no_atp", to make Sledgehammer more complete
 
blanchet 
parents: 
53954 
diff
changeset
 | 
937  | 
lemma append_eq_append_conv [simp]:  | 
| 24526 | 938  | 
"length xs = length ys \<or> length us = length vs  | 
| 
13883
 
0451e0fb3f22
Re-structured some proofs in order to get rid of rule_format attribute.
 
berghofe 
parents: 
13863 
diff
changeset
 | 
939  | 
==> (xs@us = ys@vs) = (xs=ys \<and> us=vs)"  | 
| 24526 | 940  | 
apply (induct xs arbitrary: ys)  | 
| 14208 | 941  | 
apply (case_tac ys, simp, force)  | 
942  | 
apply (case_tac ys, force, simp)  | 
|
| 13145 | 943  | 
done  | 
| 13142 | 944  | 
|
| 24526 | 945  | 
lemma append_eq_append_conv2: "(xs @ ys = zs @ ts) =  | 
946  | 
(EX us. xs = zs @ us & us @ ys = ts | xs @ us = zs & ys = us@ ts)"  | 
|
947  | 
apply (induct xs arbitrary: ys zs ts)  | 
|
| 
44890
 
22f665a2e91c
new fastforce replacing fastsimp - less confusing name
 
nipkow 
parents: 
44635 
diff
changeset
 | 
948  | 
apply fastforce  | 
| 14495 | 949  | 
apply(case_tac zs)  | 
950  | 
apply simp  | 
|
| 
44890
 
22f665a2e91c
new fastforce replacing fastsimp - less confusing name
 
nipkow 
parents: 
44635 
diff
changeset
 | 
951  | 
apply fastforce  | 
| 14495 | 952  | 
done  | 
953  | 
||
| 
34910
 
b23bd3ee4813
same_append_eq / append_same_eq are now used for simplifying induction rules.
 
berghofe 
parents: 
34064 
diff
changeset
 | 
954  | 
lemma same_append_eq [iff, induct_simp]: "(xs @ ys = xs @ zs) = (ys = zs)"  | 
| 13145 | 955  | 
by simp  | 
| 13142 | 956  | 
|
957  | 
lemma append1_eq_conv [iff]: "(xs @ [x] = ys @ [y]) = (xs = ys \<and> x = y)"  | 
|
| 13145 | 958  | 
by simp  | 
| 13114 | 959  | 
|
| 
34910
 
b23bd3ee4813
same_append_eq / append_same_eq are now used for simplifying induction rules.
 
berghofe 
parents: 
34064 
diff
changeset
 | 
960  | 
lemma append_same_eq [iff, induct_simp]: "(ys @ xs = zs @ xs) = (ys = zs)"  | 
| 13145 | 961  | 
by simp  | 
| 13114 | 962  | 
|
| 13142 | 963  | 
lemma append_self_conv2 [iff]: "(xs @ ys = ys) = (xs = [])"  | 
| 13145 | 964  | 
using append_same_eq [of _ _ "[]"] by auto  | 
| 3507 | 965  | 
|
| 13142 | 966  | 
lemma self_append_conv2 [iff]: "(ys = xs @ ys) = (xs = [])"  | 
| 13145 | 967  | 
using append_same_eq [of "[]"] by auto  | 
| 13114 | 968  | 
|
| 
54147
 
97a8ff4e4ac9
killed most "no_atp", to make Sledgehammer more complete
 
blanchet 
parents: 
53954 
diff
changeset
 | 
969  | 
lemma hd_Cons_tl [simp]: "xs \<noteq> [] ==> hd xs # tl xs = xs"  | 
| 13145 | 970  | 
by (induct xs) auto  | 
| 13114 | 971  | 
|
| 13142 | 972  | 
lemma hd_append: "hd (xs @ ys) = (if xs = [] then hd ys else hd xs)"  | 
| 13145 | 973  | 
by (induct xs) auto  | 
| 13114 | 974  | 
|
| 13142 | 975  | 
lemma hd_append2 [simp]: "xs \<noteq> [] ==> hd (xs @ ys) = hd xs"  | 
| 13145 | 976  | 
by (simp add: hd_append split: list.split)  | 
| 13114 | 977  | 
|
| 13142 | 978  | 
lemma tl_append: "tl (xs @ ys) = (case xs of [] => tl ys | z#zs => zs @ ys)"  | 
| 13145 | 979  | 
by (simp split: list.split)  | 
| 13114 | 980  | 
|
| 13142 | 981  | 
lemma tl_append2 [simp]: "xs \<noteq> [] ==> tl (xs @ ys) = tl xs @ ys"  | 
| 13145 | 982  | 
by (simp add: tl_append split: list.split)  | 
| 13114 | 983  | 
|
984  | 
||
| 14300 | 985  | 
lemma Cons_eq_append_conv: "x#xs = ys@zs =  | 
986  | 
(ys = [] & x#xs = zs | (EX ys'. x#ys' = ys & xs = ys'@zs))"  | 
|
987  | 
by(cases ys) auto  | 
|
988  | 
||
| 15281 | 989  | 
lemma append_eq_Cons_conv: "(ys@zs = x#xs) =  | 
990  | 
(ys = [] & zs = x#xs | (EX ys'. ys = x#ys' & ys'@zs = xs))"  | 
|
991  | 
by(cases ys) auto  | 
|
992  | 
||
| 14300 | 993  | 
|
| 13142 | 994  | 
text {* Trivial rules for solving @{text "@"}-equations automatically. *}
 | 
| 13114 | 995  | 
|
996  | 
lemma eq_Nil_appendI: "xs = ys ==> xs = [] @ ys"  | 
|
| 13145 | 997  | 
by simp  | 
| 13114 | 998  | 
|
| 13142 | 999  | 
lemma Cons_eq_appendI:  | 
| 13145 | 1000  | 
"[| x # xs1 = ys; xs = xs1 @ zs |] ==> x # xs = ys @ zs"  | 
1001  | 
by (drule sym) simp  | 
|
| 13114 | 1002  | 
|
| 13142 | 1003  | 
lemma append_eq_appendI:  | 
| 13145 | 1004  | 
"[| xs @ xs1 = zs; ys = xs1 @ us |] ==> xs @ ys = zs @ us"  | 
1005  | 
by (drule sym) simp  | 
|
| 13114 | 1006  | 
|
1007  | 
||
| 13142 | 1008  | 
text {*
 | 
| 13145 | 1009  | 
Simplification procedure for all list equalities.  | 
1010  | 
Currently only tries to rearrange @{text "@"} to see if
 | 
|
1011  | 
- both lists end in a singleton list,  | 
|
1012  | 
- or both lists end in the same list.  | 
|
| 13142 | 1013  | 
*}  | 
1014  | 
||
| 43594 | 1015  | 
simproc_setup list_eq ("(xs::'a list) = ys")  = {*
 | 
| 13462 | 1016  | 
let  | 
| 43594 | 1017  | 
    fun last (cons as Const (@{const_name Cons}, _) $ _ $ xs) =
 | 
1018  | 
          (case xs of Const (@{const_name Nil}, _) => cons | _ => last xs)
 | 
|
1019  | 
      | last (Const(@{const_name append},_) $ _ $ ys) = last ys
 | 
|
1020  | 
| last t = t;  | 
|
1021  | 
||
1022  | 
    fun list1 (Const(@{const_name Cons},_) $ _ $ Const(@{const_name Nil},_)) = true
 | 
|
1023  | 
| list1 _ = false;  | 
|
1024  | 
||
1025  | 
    fun butlast ((cons as Const(@{const_name Cons},_) $ x) $ xs) =
 | 
|
1026  | 
          (case xs of Const (@{const_name Nil}, _) => xs | _ => cons $ butlast xs)
 | 
|
1027  | 
      | butlast ((app as Const (@{const_name append}, _) $ xs) $ ys) = app $ butlast ys
 | 
|
1028  | 
      | butlast xs = Const(@{const_name Nil}, fastype_of xs);
 | 
|
1029  | 
||
1030  | 
val rearr_ss =  | 
|
| 
51717
 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 
wenzelm 
parents: 
51678 
diff
changeset
 | 
1031  | 
      simpset_of (put_simpset HOL_basic_ss @{context}
 | 
| 
 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 
wenzelm 
parents: 
51678 
diff
changeset
 | 
1032  | 
        addsimps [@{thm append_assoc}, @{thm append_Nil}, @{thm append_Cons}]);
 | 
| 43594 | 1033  | 
|
| 
51717
 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 
wenzelm 
parents: 
51678 
diff
changeset
 | 
1034  | 
fun list_eq ctxt (F as (eq as Const(_,eqT)) $ lhs $ rhs) =  | 
| 13462 | 1035  | 
let  | 
| 43594 | 1036  | 
val lastl = last lhs and lastr = last rhs;  | 
1037  | 
fun rearr conv =  | 
|
1038  | 
let  | 
|
1039  | 
val lhs1 = butlast lhs and rhs1 = butlast rhs;  | 
|
1040  | 
val Type(_,listT::_) = eqT  | 
|
1041  | 
val appT = [listT,listT] ---> listT  | 
|
1042  | 
            val app = Const(@{const_name append},appT)
 | 
|
1043  | 
val F2 = eq $ (app$lhs1$lastl) $ (app$rhs1$lastr)  | 
|
1044  | 
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
 | 
1045  | 
val thm = Goal.prove ctxt [] [] eq  | 
| 
 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 
wenzelm 
parents: 
51678 
diff
changeset
 | 
1046  | 
(K (simp_tac (put_simpset rearr_ss ctxt) 1));  | 
| 43594 | 1047  | 
in SOME ((conv RS (thm RS trans)) RS eq_reflection) end;  | 
1048  | 
in  | 
|
1049  | 
        if list1 lastl andalso list1 lastr then rearr @{thm append1_eq_conv}
 | 
|
1050  | 
        else if lastl aconv lastr then rearr @{thm append_same_eq}
 | 
|
1051  | 
else NONE  | 
|
1052  | 
end;  | 
|
| 
51717
 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 
wenzelm 
parents: 
51678 
diff
changeset
 | 
1053  | 
in fn _ => fn ctxt => fn ct => list_eq ctxt (term_of ct) end;  | 
| 13114 | 1054  | 
*}  | 
1055  | 
||
1056  | 
||
| 
49948
 
744934b818c7
moved quite generic material from theory Enum to more appropriate places
 
haftmann 
parents: 
49808 
diff
changeset
 | 
1057  | 
subsubsection {* @{const map} *}
 | 
| 13114 | 1058  | 
|
| 
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  | 
lemma hd_map:  | 
| 
 
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
 | 
1060  | 
"xs \<noteq> [] \<Longrightarrow> hd (map f xs) = f (hd 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
 | 
1061  | 
by (cases xs) simp_all  | 
| 
 
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
 | 
1062  | 
|
| 
 
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
 | 
1063  | 
lemma map_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
 | 
1064  | 
"map f (tl xs) = tl (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
 | 
1065  | 
by (cases xs) simp_all  | 
| 
 
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
 | 
1066  | 
|
| 13142 | 1067  | 
lemma map_ext: "(!!x. x : set xs --> f x = g x) ==> map f xs = map g xs"  | 
| 13145 | 1068  | 
by (induct xs) simp_all  | 
| 13114 | 1069  | 
|
| 13142 | 1070  | 
lemma map_ident [simp]: "map (\<lambda>x. x) = (\<lambda>xs. xs)"  | 
| 13145 | 1071  | 
by (rule ext, induct_tac xs) auto  | 
| 13114 | 1072  | 
|
| 13142 | 1073  | 
lemma map_append [simp]: "map f (xs @ ys) = map f xs @ map f ys"  | 
| 13145 | 1074  | 
by (induct xs) auto  | 
| 13114 | 1075  | 
|
| 
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
 | 
1076  | 
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
 | 
1077  | 
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
 | 
1078  | 
|
| 35208 | 1079  | 
lemma map_comp_map[simp]: "((map f) o (map g)) = map(f o g)"  | 
1080  | 
apply(rule ext)  | 
|
1081  | 
apply(simp)  | 
|
1082  | 
done  | 
|
1083  | 
||
| 13142 | 1084  | 
lemma rev_map: "rev (map f xs) = map f (rev xs)"  | 
| 13145 | 1085  | 
by (induct xs) auto  | 
| 13114 | 1086  | 
|
| 13737 | 1087  | 
lemma map_eq_conv[simp]: "(map f xs = map g xs) = (!x : set xs. f x = g x)"  | 
1088  | 
by (induct xs) auto  | 
|
1089  | 
||
| 
44013
 
5cfc1c36ae97
moved recdef package to HOL/Library/Old_Recdef.thy
 
krauss 
parents: 
43594 
diff
changeset
 | 
1090  | 
lemma map_cong [fundef_cong]:  | 
| 
40122
 
1d8ad2ff3e01
dropped (almost) redundant distinct.induct rule; distinct_simps again named distinct.simps
 
haftmann 
parents: 
40077 
diff
changeset
 | 
1091  | 
"xs = ys \<Longrightarrow> (\<And>x. x \<in> set ys \<Longrightarrow> f x = g x) \<Longrightarrow> map f xs = map g ys"  | 
| 
 
1d8ad2ff3e01
dropped (almost) redundant distinct.induct rule; distinct_simps again named distinct.simps
 
haftmann 
parents: 
40077 
diff
changeset
 | 
1092  | 
by simp  | 
| 13114 | 1093  | 
|
| 13142 | 1094  | 
lemma map_is_Nil_conv [iff]: "(map f xs = []) = (xs = [])"  | 
| 13145 | 1095  | 
by (cases xs) auto  | 
| 13114 | 1096  | 
|
| 13142 | 1097  | 
lemma Nil_is_map_conv [iff]: "([] = map f xs) = (xs = [])"  | 
| 13145 | 1098  | 
by (cases xs) auto  | 
| 13114 | 1099  | 
|
| 18447 | 1100  | 
lemma map_eq_Cons_conv:  | 
| 14025 | 1101  | 
"(map f xs = y#ys) = (\<exists>z zs. xs = z#zs \<and> f z = y \<and> map f zs = ys)"  | 
| 13145 | 1102  | 
by (cases xs) auto  | 
| 13114 | 1103  | 
|
| 18447 | 1104  | 
lemma Cons_eq_map_conv:  | 
| 14025 | 1105  | 
"(x#xs = map f ys) = (\<exists>z zs. ys = z#zs \<and> x = f z \<and> xs = map f zs)"  | 
1106  | 
by (cases ys) auto  | 
|
1107  | 
||
| 18447 | 1108  | 
lemmas map_eq_Cons_D = map_eq_Cons_conv [THEN iffD1]  | 
1109  | 
lemmas Cons_eq_map_D = Cons_eq_map_conv [THEN iffD1]  | 
|
1110  | 
declare map_eq_Cons_D [dest!] Cons_eq_map_D [dest!]  | 
|
1111  | 
||
| 14111 | 1112  | 
lemma ex_map_conv:  | 
1113  | 
"(EX xs. ys = map f xs) = (ALL y : set ys. EX x. y = f x)"  | 
|
| 18447 | 1114  | 
by(induct ys, auto simp add: Cons_eq_map_conv)  | 
| 14111 | 1115  | 
|
| 
15110
 
78b5636eabc7
Added a number of new thms and the new function remove1
 
nipkow 
parents: 
15072 
diff
changeset
 | 
1116  | 
lemma map_eq_imp_length_eq:  | 
| 35510 | 1117  | 
assumes "map f xs = map g ys"  | 
| 26734 | 1118  | 
shows "length xs = length ys"  | 
| 
53374
 
a14d2a854c02
tuned proofs -- clarified flow of facts wrt. calculation;
 
wenzelm 
parents: 
53017 
diff
changeset
 | 
1119  | 
using assms  | 
| 
 
a14d2a854c02
tuned proofs -- clarified flow of facts wrt. calculation;
 
wenzelm 
parents: 
53017 
diff
changeset
 | 
1120  | 
proof (induct ys arbitrary: xs)  | 
| 26734 | 1121  | 
case Nil then show ?case by simp  | 
1122  | 
next  | 
|
1123  | 
case (Cons y ys) then obtain z zs where xs: "xs = z # zs" by auto  | 
|
| 35510 | 1124  | 
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
 | 
1125  | 
with Cons have "length zs = length ys" by blast  | 
| 26734 | 1126  | 
with xs show ?case by simp  | 
1127  | 
qed  | 
|
1128  | 
||
| 
15110
 
78b5636eabc7
Added a number of new thms and the new function remove1
 
nipkow 
parents: 
15072 
diff
changeset
 | 
1129  | 
lemma map_inj_on:  | 
| 
 
78b5636eabc7
Added a number of new thms and the new function remove1
 
nipkow 
parents: 
15072 
diff
changeset
 | 
1130  | 
"[| map f xs = map f ys; inj_on f (set xs Un set ys) |]  | 
| 
 
78b5636eabc7
Added a number of new thms and the new function remove1
 
nipkow 
parents: 
15072 
diff
changeset
 | 
1131  | 
==> xs = ys"  | 
| 
 
78b5636eabc7
Added a number of new thms and the new function remove1
 
nipkow 
parents: 
15072 
diff
changeset
 | 
1132  | 
apply(frule map_eq_imp_length_eq)  | 
| 
 
78b5636eabc7
Added a number of new thms and the new function remove1
 
nipkow 
parents: 
15072 
diff
changeset
 | 
1133  | 
apply(rotate_tac -1)  | 
| 
 
78b5636eabc7
Added a number of new thms and the new function remove1
 
nipkow 
parents: 
15072 
diff
changeset
 | 
1134  | 
apply(induct rule:list_induct2)  | 
| 
 
78b5636eabc7
Added a number of new thms and the new function remove1
 
nipkow 
parents: 
15072 
diff
changeset
 | 
1135  | 
apply simp  | 
| 
 
78b5636eabc7
Added a number of new thms and the new function remove1
 
nipkow 
parents: 
15072 
diff
changeset
 | 
1136  | 
apply(simp)  | 
| 
 
78b5636eabc7
Added a number of new thms and the new function remove1
 
nipkow 
parents: 
15072 
diff
changeset
 | 
1137  | 
apply (blast intro:sym)  | 
| 
 
78b5636eabc7
Added a number of new thms and the new function remove1
 
nipkow 
parents: 
15072 
diff
changeset
 | 
1138  | 
done  | 
| 
 
78b5636eabc7
Added a number of new thms and the new function remove1
 
nipkow 
parents: 
15072 
diff
changeset
 | 
1139  | 
|
| 
 
78b5636eabc7
Added a number of new thms and the new function remove1
 
nipkow 
parents: 
15072 
diff
changeset
 | 
1140  | 
lemma inj_on_map_eq_map:  | 
| 
 
78b5636eabc7
Added a number of new thms and the new function remove1
 
nipkow 
parents: 
15072 
diff
changeset
 | 
1141  | 
"inj_on f (set xs Un set ys) \<Longrightarrow> (map f xs = map f ys) = (xs = ys)"  | 
| 
 
78b5636eabc7
Added a number of new thms and the new function remove1
 
nipkow 
parents: 
15072 
diff
changeset
 | 
1142  | 
by(blast dest:map_inj_on)  | 
| 
 
78b5636eabc7
Added a number of new thms and the new function remove1
 
nipkow 
parents: 
15072 
diff
changeset
 | 
1143  | 
|
| 13114 | 1144  | 
lemma map_injective:  | 
| 24526 | 1145  | 
"map f xs = map f ys ==> inj f ==> xs = ys"  | 
1146  | 
by (induct ys arbitrary: xs) (auto dest!:injD)  | 
|
| 13114 | 1147  | 
|
| 14339 | 1148  | 
lemma inj_map_eq_map[simp]: "inj f \<Longrightarrow> (map f xs = map f ys) = (xs = ys)"  | 
1149  | 
by(blast dest:map_injective)  | 
|
1150  | 
||
| 13114 | 1151  | 
lemma inj_mapI: "inj f ==> inj (map f)"  | 
| 17589 | 1152  | 
by (iprover dest: map_injective injD intro: inj_onI)  | 
| 13114 | 1153  | 
|
1154  | 
lemma inj_mapD: "inj (map f) ==> inj f"  | 
|
| 14208 | 1155  | 
apply (unfold inj_on_def, clarify)  | 
| 13145 | 1156  | 
apply (erule_tac x = "[x]" in ballE)  | 
| 14208 | 1157  | 
apply (erule_tac x = "[y]" in ballE, simp, blast)  | 
| 13145 | 1158  | 
apply blast  | 
1159  | 
done  | 
|
| 13114 | 1160  | 
|
| 14339 | 1161  | 
lemma inj_map[iff]: "inj (map f) = inj f"  | 
| 13145 | 1162  | 
by (blast dest: inj_mapD intro: inj_mapI)  | 
| 13114 | 1163  | 
|
| 15303 | 1164  | 
lemma inj_on_mapI: "inj_on f (\<Union>(set ` A)) \<Longrightarrow> inj_on (map f) A"  | 
1165  | 
apply(rule inj_onI)  | 
|
1166  | 
apply(erule map_inj_on)  | 
|
1167  | 
apply(blast intro:inj_onI dest:inj_onD)  | 
|
1168  | 
done  | 
|
1169  | 
||
| 14343 | 1170  | 
lemma map_idI: "(\<And>x. x \<in> set xs \<Longrightarrow> f x = x) \<Longrightarrow> map f xs = xs"  | 
1171  | 
by (induct xs, auto)  | 
|
| 13114 | 1172  | 
|
| 
14402
 
4201e1916482
moved lemmas from MicroJava/Comp/AuxLemmas.thy to List.thy
 
nipkow 
parents: 
14395 
diff
changeset
 | 
1173  | 
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
 | 
1174  | 
by (induct xs) auto  | 
| 
 
4201e1916482
moved lemmas from MicroJava/Comp/AuxLemmas.thy to List.thy
 
nipkow 
parents: 
14395 
diff
changeset
 | 
1175  | 
|
| 
15110
 
78b5636eabc7
Added a number of new thms and the new function remove1
 
nipkow 
parents: 
15072 
diff
changeset
 | 
1176  | 
lemma map_fst_zip[simp]:  | 
| 
 
78b5636eabc7
Added a number of new thms and the new function remove1
 
nipkow 
parents: 
15072 
diff
changeset
 | 
1177  | 
"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
 | 
1178  | 
by (induct rule:list_induct2, simp_all)  | 
| 
 
78b5636eabc7
Added a number of new thms and the new function remove1
 
nipkow 
parents: 
15072 
diff
changeset
 | 
1179  | 
|
| 
 
78b5636eabc7
Added a number of new thms and the new function remove1
 
nipkow 
parents: 
15072 
diff
changeset
 | 
1180  | 
lemma map_snd_zip[simp]:  | 
| 
 
78b5636eabc7
Added a number of new thms and the new function remove1
 
nipkow 
parents: 
15072 
diff
changeset
 | 
1181  | 
"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
 | 
1182  | 
by (induct rule:list_induct2, simp_all)  | 
| 
 
78b5636eabc7
Added a number of new thms and the new function remove1
 
nipkow 
parents: 
15072 
diff
changeset
 | 
1183  | 
|
| 
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
 | 
1184  | 
functor map: map  | 
| 47122 | 1185  | 
by (simp_all add: id_def)  | 
1186  | 
||
| 
49948
 
744934b818c7
moved quite generic material from theory Enum to more appropriate places
 
haftmann 
parents: 
49808 
diff
changeset
 | 
1187  | 
declare map.id [simp]  | 
| 
 
744934b818c7
moved quite generic material from theory Enum to more appropriate places
 
haftmann 
parents: 
49808 
diff
changeset
 | 
1188  | 
|
| 
 
744934b818c7
moved quite generic material from theory Enum to more appropriate places
 
haftmann 
parents: 
49808 
diff
changeset
 | 
1189  | 
|
| 
 
744934b818c7
moved quite generic material from theory Enum to more appropriate places
 
haftmann 
parents: 
49808 
diff
changeset
 | 
1190  | 
subsubsection {* @{const rev} *}
 | 
| 13114 | 1191  | 
|
| 13142 | 1192  | 
lemma rev_append [simp]: "rev (xs @ ys) = rev ys @ rev xs"  | 
| 13145 | 1193  | 
by (induct xs) auto  | 
| 13114 | 1194  | 
|
| 13142 | 1195  | 
lemma rev_rev_ident [simp]: "rev (rev xs) = xs"  | 
| 13145 | 1196  | 
by (induct xs) auto  | 
| 13114 | 1197  | 
|
| 15870 | 1198  | 
lemma rev_swap: "(rev xs = ys) = (xs = rev ys)"  | 
1199  | 
by auto  | 
|
1200  | 
||
| 13142 | 1201  | 
lemma rev_is_Nil_conv [iff]: "(rev xs = []) = (xs = [])"  | 
| 13145 | 1202  | 
by (induct xs) auto  | 
| 13114 | 1203  | 
|
| 13142 | 1204  | 
lemma Nil_is_rev_conv [iff]: "([] = rev xs) = (xs = [])"  | 
| 13145 | 1205  | 
by (induct xs) auto  | 
| 13114 | 1206  | 
|
| 15870 | 1207  | 
lemma rev_singleton_conv [simp]: "(rev xs = [x]) = (xs = [x])"  | 
1208  | 
by (cases xs) auto  | 
|
1209  | 
||
1210  | 
lemma singleton_rev_conv [simp]: "([x] = rev xs) = (xs = [x])"  | 
|
1211  | 
by (cases xs) auto  | 
|
1212  | 
||
| 
54147
 
97a8ff4e4ac9
killed most "no_atp", to make Sledgehammer more complete
 
blanchet 
parents: 
53954 
diff
changeset
 | 
1213  | 
lemma rev_is_rev_conv [iff]: "(rev xs = rev ys) = (xs = ys)"  | 
| 
21061
 
580dfc999ef6
added normal post setup; cleaned up "execution" constants
 
haftmann 
parents: 
21046 
diff
changeset
 | 
1214  | 
apply (induct xs arbitrary: ys, force)  | 
| 14208 | 1215  | 
apply (case_tac ys, simp, force)  | 
| 13145 | 1216  | 
done  | 
| 13114 | 1217  | 
|
| 15439 | 1218  | 
lemma inj_on_rev[iff]: "inj_on rev A"  | 
1219  | 
by(simp add:inj_on_def)  | 
|
1220  | 
||
| 13366 | 1221  | 
lemma rev_induct [case_names Nil snoc]:  | 
1222  | 
"[| P []; !!x xs. P xs ==> P (xs @ [x]) |] ==> P xs"  | 
|
| 
15489
 
d136af442665
Replaced application of subst by simplesubst in proof of rev_induct
 
berghofe 
parents: 
15439 
diff
changeset
 | 
1223  | 
apply(simplesubst rev_rev_ident[symmetric])  | 
| 13145 | 1224  | 
apply(rule_tac list = "rev xs" in list.induct, simp_all)  | 
1225  | 
done  | 
|
| 13114 | 1226  | 
|
| 13366 | 1227  | 
lemma rev_exhaust [case_names Nil snoc]:  | 
1228  | 
"(xs = [] ==> P) ==>(!!ys y. xs = ys @ [y] ==> P) ==> P"  | 
|
| 13145 | 1229  | 
by (induct xs rule: rev_induct) auto  | 
| 13114 | 1230  | 
|
| 13366 | 1231  | 
lemmas rev_cases = rev_exhaust  | 
1232  | 
||
| 57577 | 1233  | 
lemma rev_nonempty_induct [consumes 1, case_names single snoc]:  | 
1234  | 
assumes "xs \<noteq> []"  | 
|
1235  | 
and single: "\<And>x. P [x]"  | 
|
1236  | 
and snoc': "\<And>x xs. xs \<noteq> [] \<Longrightarrow> P xs \<Longrightarrow> P (xs@[x])"  | 
|
1237  | 
shows "P xs"  | 
|
1238  | 
using `xs \<noteq> []` proof (induct xs rule: rev_induct)  | 
|
1239  | 
case (snoc x xs) then show ?case  | 
|
1240  | 
proof (cases xs)  | 
|
1241  | 
case Nil thus ?thesis by (simp add: single)  | 
|
1242  | 
next  | 
|
1243  | 
case Cons with snoc show ?thesis by (fastforce intro!: snoc')  | 
|
1244  | 
qed  | 
|
1245  | 
qed simp  | 
|
1246  | 
||
| 18423 | 1247  | 
lemma rev_eq_Cons_iff[iff]: "(rev xs = y#ys) = (xs = rev ys @ [y])"  | 
1248  | 
by(rule rev_cases[of xs]) auto  | 
|
1249  | 
||
| 13114 | 1250  | 
|
| 
49948
 
744934b818c7
moved quite generic material from theory Enum to more appropriate places
 
haftmann 
parents: 
49808 
diff
changeset
 | 
1251  | 
subsubsection {* @{const set} *}
 | 
| 13114 | 1252  | 
|
| 
57816
 
d8bbb97689d3
no need for 'set_simps' now that 'datatype_new' generates the desired 'set' property
 
blanchet 
parents: 
57599 
diff
changeset
 | 
1253  | 
declare list.set[code_post] --"pretty output"  | 
| 
 
d8bbb97689d3
no need for 'set_simps' now that 'datatype_new' generates the desired 'set' property
 
blanchet 
parents: 
57599 
diff
changeset
 | 
1254  | 
|
| 13142 | 1255  | 
lemma finite_set [iff]: "finite (set xs)"  | 
| 13145 | 1256  | 
by (induct xs) auto  | 
| 13114 | 1257  | 
|
| 13142 | 1258  | 
lemma set_append [simp]: "set (xs @ ys) = (set xs \<union> set ys)"  | 
| 13145 | 1259  | 
by (induct xs) auto  | 
| 13114 | 1260  | 
|
| 17830 | 1261  | 
lemma hd_in_set[simp]: "xs \<noteq> [] \<Longrightarrow> hd xs : set xs"  | 
1262  | 
by(cases xs) auto  | 
|
| 14099 | 1263  | 
|
| 13142 | 1264  | 
lemma set_subset_Cons: "set xs \<subseteq> set (x # xs)"  | 
| 13145 | 1265  | 
by auto  | 
| 13114 | 1266  | 
|
| 14099 | 1267  | 
lemma set_ConsD: "y \<in> set (x # xs) \<Longrightarrow> y=x \<or> y \<in> set xs"  | 
1268  | 
by auto  | 
|
1269  | 
||
| 13142 | 1270  | 
lemma set_empty [iff]: "(set xs = {}) = (xs = [])"
 | 
| 13145 | 1271  | 
by (induct xs) auto  | 
| 13114 | 1272  | 
|
| 15245 | 1273  | 
lemma set_empty2[iff]: "({} = set xs) = (xs = [])"
 | 
1274  | 
by(induct xs) auto  | 
|
1275  | 
||
| 13142 | 1276  | 
lemma set_rev [simp]: "set (rev xs) = set xs"  | 
| 13145 | 1277  | 
by (induct xs) auto  | 
| 13114 | 1278  | 
|
| 13142 | 1279  | 
lemma set_map [simp]: "set (map f xs) = f`(set xs)"  | 
| 13145 | 1280  | 
by (induct xs) auto  | 
| 13114 | 1281  | 
|
| 13142 | 1282  | 
lemma set_filter [simp]: "set (filter P xs) = {x. x : set xs \<and> P x}"
 | 
| 13145 | 1283  | 
by (induct xs) auto  | 
| 13114 | 1284  | 
|
| 32417 | 1285  | 
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
 | 
1286  | 
by (induct j) auto  | 
| 13114 | 1287  | 
|
| 13142 | 1288  | 
|
| 
25221
 
5ded95dda5df
append/member: more light-weight way to declare authentic syntax;
 
wenzelm 
parents: 
25215 
diff
changeset
 | 
1289  | 
lemma split_list: "x : set xs \<Longrightarrow> \<exists>ys zs. xs = ys @ x # zs"  | 
| 18049 | 1290  | 
proof (induct xs)  | 
| 26073 | 1291  | 
case Nil thus ?case by simp  | 
1292  | 
next  | 
|
1293  | 
case Cons thus ?case by (auto intro: Cons_eq_appendI)  | 
|
1294  | 
qed  | 
|
1295  | 
||
| 26734 | 1296  | 
lemma in_set_conv_decomp: "x \<in> set xs \<longleftrightarrow> (\<exists>ys zs. xs = ys @ x # zs)"  | 
1297  | 
by (auto elim: split_list)  | 
|
| 26073 | 1298  | 
|
1299  | 
lemma split_list_first: "x : set xs \<Longrightarrow> \<exists>ys zs. xs = ys @ x # zs \<and> x \<notin> set ys"  | 
|
1300  | 
proof (induct xs)  | 
|
1301  | 
case Nil thus ?case by simp  | 
|
| 18049 | 1302  | 
next  | 
1303  | 
case (Cons a xs)  | 
|
1304  | 
show ?case  | 
|
1305  | 
proof cases  | 
|
| 
44890
 
22f665a2e91c
new fastforce replacing fastsimp - less confusing name
 
nipkow 
parents: 
44635 
diff
changeset
 | 
1306  | 
assume "x = a" thus ?case using Cons by fastforce  | 
| 18049 | 1307  | 
next  | 
| 
44890
 
22f665a2e91c
new fastforce replacing fastsimp - less confusing name
 
nipkow 
parents: 
44635 
diff
changeset
 | 
1308  | 
assume "x \<noteq> a" thus ?case using Cons by(fastforce intro!: Cons_eq_appendI)  | 
| 26073 | 1309  | 
qed  | 
1310  | 
qed  | 
|
1311  | 
||
1312  | 
lemma in_set_conv_decomp_first:  | 
|
1313  | 
"(x : set xs) = (\<exists>ys zs. xs = ys @ x # zs \<and> x \<notin> set ys)"  | 
|
| 26734 | 1314  | 
by (auto dest!: split_list_first)  | 
| 26073 | 1315  | 
|
| 
40122
 
1d8ad2ff3e01
dropped (almost) redundant distinct.induct rule; distinct_simps again named distinct.simps
 
haftmann 
parents: 
40077 
diff
changeset
 | 
1316  | 
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
 | 
1317  | 
proof (induct xs rule: rev_induct)  | 
| 26073 | 1318  | 
case Nil thus ?case by simp  | 
1319  | 
next  | 
|
1320  | 
case (snoc a xs)  | 
|
1321  | 
show ?case  | 
|
1322  | 
proof cases  | 
|
| 56085 | 1323  | 
assume "x = a" thus ?case using snoc by (auto intro!: exI)  | 
| 26073 | 1324  | 
next  | 
| 
44890
 
22f665a2e91c
new fastforce replacing fastsimp - less confusing name
 
nipkow 
parents: 
44635 
diff
changeset
 | 
1325  | 
assume "x \<noteq> a" thus ?case using snoc by fastforce  | 
| 18049 | 1326  | 
qed  | 
1327  | 
qed  | 
|
1328  | 
||
| 26073 | 1329  | 
lemma in_set_conv_decomp_last:  | 
1330  | 
"(x : set xs) = (\<exists>ys zs. xs = ys @ x # zs \<and> x \<notin> set zs)"  | 
|
| 26734 | 1331  | 
by (auto dest!: split_list_last)  | 
| 26073 | 1332  | 
|
1333  | 
lemma split_list_prop: "\<exists>x \<in> set xs. P x \<Longrightarrow> \<exists>ys x zs. xs = ys @ x # zs & P x"  | 
|
1334  | 
proof (induct xs)  | 
|
1335  | 
case Nil thus ?case by simp  | 
|
1336  | 
next  | 
|
1337  | 
case Cons thus ?case  | 
|
1338  | 
by(simp add:Bex_def)(metis append_Cons append.simps(1))  | 
|
1339  | 
qed  | 
|
1340  | 
||
1341  | 
lemma split_list_propE:  | 
|
| 26734 | 1342  | 
assumes "\<exists>x \<in> set xs. P x"  | 
1343  | 
obtains ys x zs where "xs = ys @ x # zs" and "P x"  | 
|
1344  | 
using split_list_prop [OF assms] by blast  | 
|
| 26073 | 1345  | 
|
1346  | 
lemma split_list_first_prop:  | 
|
1347  | 
"\<exists>x \<in> set xs. P x \<Longrightarrow>  | 
|
1348  | 
\<exists>ys x zs. xs = ys@x#zs \<and> P x \<and> (\<forall>y \<in> set ys. \<not> P y)"  | 
|
| 26734 | 1349  | 
proof (induct xs)  | 
| 26073 | 1350  | 
case Nil thus ?case by simp  | 
1351  | 
next  | 
|
1352  | 
case (Cons x xs)  | 
|
1353  | 
show ?case  | 
|
1354  | 
proof cases  | 
|
1355  | 
assume "P x"  | 
|
| 56085 | 1356  | 
hence "x # xs = [] @ x # xs \<and> P x \<and> (\<forall>y\<in>set []. \<not> P y)" by simp  | 
1357  | 
thus ?thesis by fast  | 
|
| 26073 | 1358  | 
next  | 
1359  | 
assume "\<not> P x"  | 
|
1360  | 
hence "\<exists>x\<in>set xs. P x" using Cons(2) by simp  | 
|
1361  | 
thus ?thesis using `\<not> P x` Cons(1) by (metis append_Cons set_ConsD)  | 
|
1362  | 
qed  | 
|
1363  | 
qed  | 
|
1364  | 
||
1365  | 
lemma split_list_first_propE:  | 
|
| 26734 | 1366  | 
assumes "\<exists>x \<in> set xs. P x"  | 
1367  | 
obtains ys x zs where "xs = ys @ x # zs" and "P x" and "\<forall>y \<in> set ys. \<not> P y"  | 
|
1368  | 
using split_list_first_prop [OF assms] by blast  | 
|
| 26073 | 1369  | 
|
1370  | 
lemma split_list_first_prop_iff:  | 
|
1371  | 
"(\<exists>x \<in> set xs. P x) \<longleftrightarrow>  | 
|
1372  | 
(\<exists>ys x zs. xs = ys@x#zs \<and> P x \<and> (\<forall>y \<in> set ys. \<not> P y))"  | 
|
| 26734 | 1373  | 
by (rule, erule split_list_first_prop) auto  | 
| 26073 | 1374  | 
|
1375  | 
lemma split_list_last_prop:  | 
|
1376  | 
"\<exists>x \<in> set xs. P x \<Longrightarrow>  | 
|
1377  | 
\<exists>ys x zs. xs = ys@x#zs \<and> P x \<and> (\<forall>z \<in> set zs. \<not> P z)"  | 
|
1378  | 
proof(induct xs rule:rev_induct)  | 
|
1379  | 
case Nil thus ?case by simp  | 
|
1380  | 
next  | 
|
1381  | 
case (snoc x xs)  | 
|
1382  | 
show ?case  | 
|
1383  | 
proof cases  | 
|
| 56085 | 1384  | 
assume "P x" thus ?thesis by (auto intro!: exI)  | 
| 26073 | 1385  | 
next  | 
1386  | 
assume "\<not> P x"  | 
|
1387  | 
hence "\<exists>x\<in>set xs. P x" using snoc(2) by simp  | 
|
| 
44890
 
22f665a2e91c
new fastforce replacing fastsimp - less confusing name
 
nipkow 
parents: 
44635 
diff
changeset
 | 
1388  | 
thus ?thesis using `\<not> P x` snoc(1) by fastforce  | 
| 26073 | 1389  | 
qed  | 
1390  | 
qed  | 
|
1391  | 
||
1392  | 
lemma split_list_last_propE:  | 
|
| 26734 | 1393  | 
assumes "\<exists>x \<in> set xs. P x"  | 
1394  | 
obtains ys x zs where "xs = ys @ x # zs" and "P x" and "\<forall>z \<in> set zs. \<not> P z"  | 
|
1395  | 
using split_list_last_prop [OF assms] by blast  | 
|
| 26073 | 1396  | 
|
1397  | 
lemma split_list_last_prop_iff:  | 
|
1398  | 
"(\<exists>x \<in> set xs. P x) \<longleftrightarrow>  | 
|
1399  | 
(\<exists>ys x zs. xs = ys@x#zs \<and> P x \<and> (\<forall>z \<in> set zs. \<not> P z))"  | 
|
| 56085 | 1400  | 
by rule (erule split_list_last_prop, auto)  | 
1401  | 
||
| 26073 | 1402  | 
|
1403  | 
lemma finite_list: "finite A ==> EX 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
 | 
1404  | 
by (erule finite_induct) (auto simp add: list.set(2)[symmetric] simp del: list.set(2))  | 
| 13508 | 1405  | 
|
| 14388 | 1406  | 
lemma card_length: "card (set xs) \<le> length xs"  | 
1407  | 
by (induct xs) (auto simp add: card_insert_if)  | 
|
| 13114 | 1408  | 
|
| 
26442
 
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
 
haftmann 
parents: 
26300 
diff
changeset
 | 
1409  | 
lemma set_minus_filter_out:  | 
| 
 
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
 
haftmann 
parents: 
26300 
diff
changeset
 | 
1410  | 
  "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
 | 
1411  | 
by (induct xs) auto  | 
| 15168 | 1412  | 
|
| 35115 | 1413  | 
|
| 
49948
 
744934b818c7
moved quite generic material from theory Enum to more appropriate places
 
haftmann 
parents: 
49808 
diff
changeset
 | 
1414  | 
subsubsection {* @{const filter} *}
 | 
| 13114 | 1415  | 
|
| 13142 | 1416  | 
lemma filter_append [simp]: "filter P (xs @ ys) = filter P xs @ filter P ys"  | 
| 13145 | 1417  | 
by (induct xs) auto  | 
| 13114 | 1418  | 
|
| 15305 | 1419  | 
lemma rev_filter: "rev (filter P xs) = filter P (rev xs)"  | 
1420  | 
by (induct xs) simp_all  | 
|
1421  | 
||
| 13142 | 1422  | 
lemma filter_filter [simp]: "filter P (filter Q xs) = filter (\<lambda>x. Q x \<and> P x) xs"  | 
| 13145 | 1423  | 
by (induct xs) auto  | 
| 13114 | 1424  | 
|
| 16998 | 1425  | 
lemma length_filter_le [simp]: "length (filter P xs) \<le> length xs"  | 
1426  | 
by (induct xs) (auto simp add: le_SucI)  | 
|
1427  | 
||
| 18423 | 1428  | 
lemma sum_length_filter_compl:  | 
1429  | 
"length(filter P xs) + length(filter (%x. ~P x) xs) = length xs"  | 
|
1430  | 
by(induct xs) simp_all  | 
|
1431  | 
||
| 13142 | 1432  | 
lemma filter_True [simp]: "\<forall>x \<in> set xs. P x ==> filter P xs = xs"  | 
| 13145 | 1433  | 
by (induct xs) auto  | 
| 13114 | 1434  | 
|
| 13142 | 1435  | 
lemma filter_False [simp]: "\<forall>x \<in> set xs. \<not> P x ==> filter P xs = []"  | 
| 13145 | 1436  | 
by (induct xs) auto  | 
| 13114 | 1437  | 
|
| 16998 | 1438  | 
lemma filter_empty_conv: "(filter P xs = []) = (\<forall>x\<in>set xs. \<not> P x)"  | 
| 24349 | 1439  | 
by (induct xs) simp_all  | 
| 16998 | 1440  | 
|
1441  | 
lemma filter_id_conv: "(filter P xs = xs) = (\<forall>x\<in>set xs. P x)"  | 
|
1442  | 
apply (induct xs)  | 
|
1443  | 
apply auto  | 
|
1444  | 
apply(cut_tac P=P and xs=xs in length_filter_le)  | 
|
1445  | 
apply simp  | 
|
1446  | 
done  | 
|
| 13114 | 1447  | 
|
| 16965 | 1448  | 
lemma filter_map:  | 
1449  | 
"filter P (map f xs) = map f (filter (P o f) xs)"  | 
|
1450  | 
by (induct xs) simp_all  | 
|
1451  | 
||
1452  | 
lemma length_filter_map[simp]:  | 
|
1453  | 
"length (filter P (map f xs)) = length(filter (P o f) xs)"  | 
|
1454  | 
by (simp add:filter_map)  | 
|
1455  | 
||
| 13142 | 1456  | 
lemma filter_is_subset [simp]: "set (filter P xs) \<le> set xs"  | 
| 13145 | 1457  | 
by auto  | 
| 13114 | 1458  | 
|
| 15246 | 1459  | 
lemma length_filter_less:  | 
1460  | 
"\<lbrakk> x : set xs; ~ P x \<rbrakk> \<Longrightarrow> length(filter P xs) < length xs"  | 
|
1461  | 
proof (induct xs)  | 
|
1462  | 
case Nil thus ?case by simp  | 
|
1463  | 
next  | 
|
1464  | 
case (Cons x xs) thus ?case  | 
|
1465  | 
apply (auto split:split_if_asm)  | 
|
1466  | 
using length_filter_le[of P xs] apply arith  | 
|
1467  | 
done  | 
|
1468  | 
qed  | 
|
| 13114 | 1469  | 
|
| 15281 | 1470  | 
lemma length_filter_conv_card:  | 
1471  | 
 "length(filter p xs) = card{i. i < length xs & p(xs!i)}"
 | 
|
1472  | 
proof (induct xs)  | 
|
1473  | 
case Nil thus ?case by simp  | 
|
1474  | 
next  | 
|
1475  | 
case (Cons x xs)  | 
|
1476  | 
  let ?S = "{i. i < length xs & p(xs!i)}"
 | 
|
1477  | 
have fin: "finite ?S" by(fast intro: bounded_nat_set_is_finite)  | 
|
1478  | 
show ?case (is "?l = card ?S'")  | 
|
1479  | 
proof (cases)  | 
|
1480  | 
assume "p x"  | 
|
1481  | 
hence eq: "?S' = insert 0 (Suc ` ?S)"  | 
|
| 25162 | 1482  | 
by(auto simp: image_def split:nat.split dest:gr0_implies_Suc)  | 
| 15281 | 1483  | 
have "length (filter p (x # xs)) = Suc(card ?S)"  | 
| 23388 | 1484  | 
using Cons `p x` by simp  | 
| 15281 | 1485  | 
also have "\<dots> = Suc(card(Suc ` ?S))" using fin  | 
| 44921 | 1486  | 
by (simp add: card_image)  | 
| 15281 | 1487  | 
also have "\<dots> = card ?S'" using eq fin  | 
1488  | 
by (simp add:card_insert_if) (simp add:image_def)  | 
|
1489  | 
finally show ?thesis .  | 
|
1490  | 
next  | 
|
1491  | 
assume "\<not> p x"  | 
|
1492  | 
hence eq: "?S' = Suc ` ?S"  | 
|
| 25162 | 1493  | 
by(auto simp add: image_def split:nat.split elim:lessE)  | 
| 15281 | 1494  | 
have "length (filter p (x # xs)) = card ?S"  | 
| 23388 | 1495  | 
using Cons `\<not> p x` by simp  | 
| 15281 | 1496  | 
also have "\<dots> = card(Suc ` ?S)" using fin  | 
| 44921 | 1497  | 
by (simp add: card_image)  | 
| 15281 | 1498  | 
also have "\<dots> = card ?S'" using eq fin  | 
1499  | 
by (simp add:card_insert_if)  | 
|
1500  | 
finally show ?thesis .  | 
|
1501  | 
qed  | 
|
1502  | 
qed  | 
|
1503  | 
||
| 17629 | 1504  | 
lemma Cons_eq_filterD:  | 
1505  | 
"x#xs = filter P ys \<Longrightarrow>  | 
|
1506  | 
\<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 | 1507  | 
(is "_ \<Longrightarrow> \<exists>us vs. ?P ys us vs")  | 
| 17629 | 1508  | 
proof(induct ys)  | 
1509  | 
case Nil thus ?case by simp  | 
|
1510  | 
next  | 
|
1511  | 
case (Cons y ys)  | 
|
1512  | 
show ?case (is "\<exists>x. ?Q x")  | 
|
1513  | 
proof cases  | 
|
1514  | 
assume Py: "P y"  | 
|
1515  | 
show ?thesis  | 
|
1516  | 
proof cases  | 
|
| 
25221
 
5ded95dda5df
append/member: more light-weight way to declare authentic syntax;
 
wenzelm 
parents: 
25215 
diff
changeset
 | 
1517  | 
assume "x = y"  | 
| 
 
5ded95dda5df
append/member: more light-weight way to declare authentic syntax;
 
wenzelm 
parents: 
25215 
diff
changeset
 | 
1518  | 
with Py Cons.prems have "?Q []" by simp  | 
| 
 
5ded95dda5df
append/member: more light-weight way to declare authentic syntax;
 
wenzelm 
parents: 
25215 
diff
changeset
 | 
1519  | 
then show ?thesis ..  | 
| 17629 | 1520  | 
next  | 
| 
25221
 
5ded95dda5df
append/member: more light-weight way to declare authentic syntax;
 
wenzelm 
parents: 
25215 
diff
changeset
 | 
1521  | 
assume "x \<noteq> y"  | 
| 
 
5ded95dda5df
append/member: more light-weight way to declare authentic syntax;
 
wenzelm 
parents: 
25215 
diff
changeset
 | 
1522  | 
with Py Cons.prems show ?thesis by simp  | 
| 17629 | 1523  | 
qed  | 
1524  | 
next  | 
|
| 
25221
 
5ded95dda5df
append/member: more light-weight way to declare authentic syntax;
 
wenzelm 
parents: 
25215 
diff
changeset
 | 
1525  | 
assume "\<not> P y"  | 
| 
44890
 
22f665a2e91c
new fastforce replacing fastsimp - less confusing name
 
nipkow 
parents: 
44635 
diff
changeset
 | 
1526  | 
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
 | 
1527  | 
then have "?Q (y#us)" by simp  | 
| 
 
5ded95dda5df
append/member: more light-weight way to declare authentic syntax;
 
wenzelm 
parents: 
25215 
diff
changeset
 | 
1528  | 
then show ?thesis ..  | 
| 17629 | 1529  | 
qed  | 
1530  | 
qed  | 
|
1531  | 
||
1532  | 
lemma filter_eq_ConsD:  | 
|
1533  | 
"filter P ys = x#xs \<Longrightarrow>  | 
|
1534  | 
\<exists>us vs. ys = us @ x # vs \<and> (\<forall>u\<in>set us. \<not> P u) \<and> P x \<and> xs = filter P vs"  | 
|
1535  | 
by(rule Cons_eq_filterD) simp  | 
|
1536  | 
||
1537  | 
lemma filter_eq_Cons_iff:  | 
|
1538  | 
"(filter P ys = x#xs) =  | 
|
1539  | 
(\<exists>us vs. ys = us @ x # vs \<and> (\<forall>u\<in>set us. \<not> P u) \<and> P x \<and> xs = filter P vs)"  | 
|
1540  | 
by(auto dest:filter_eq_ConsD)  | 
|
1541  | 
||
1542  | 
lemma Cons_eq_filter_iff:  | 
|
1543  | 
"(x#xs = filter P ys) =  | 
|
1544  | 
(\<exists>us vs. ys = us @ x # vs \<and> (\<forall>u\<in>set us. \<not> P u) \<and> P x \<and> xs = filter P vs)"  | 
|
1545  | 
by(auto dest:Cons_eq_filterD)  | 
|
1546  | 
||
| 
44013
 
5cfc1c36ae97
moved recdef package to HOL/Library/Old_Recdef.thy
 
krauss 
parents: 
43594 
diff
changeset
 | 
1547  | 
lemma filter_cong[fundef_cong]:  | 
| 17501 | 1548  | 
"xs = ys \<Longrightarrow> (\<And>x. x \<in> set ys \<Longrightarrow> P x = Q x) \<Longrightarrow> filter P xs = filter Q ys"  | 
1549  | 
apply simp  | 
|
1550  | 
apply(erule thin_rl)  | 
|
1551  | 
by (induct ys) simp_all  | 
|
1552  | 
||
| 15281 | 1553  | 
|
| 
26442
 
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
 
haftmann 
parents: 
26300 
diff
changeset
 | 
1554  | 
subsubsection {* List partitioning *}
 | 
| 
 
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
 
haftmann 
parents: 
26300 
diff
changeset
 | 
1555  | 
|
| 
 
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
 
haftmann 
parents: 
26300 
diff
changeset
 | 
1556  | 
primrec partition :: "('a \<Rightarrow> bool) \<Rightarrow>'a list \<Rightarrow> 'a list \<times> 'a list" where
 | 
| 50548 | 1557  | 
"partition P [] = ([], [])" |  | 
1558  | 
"partition P (x # xs) =  | 
|
1559  | 
(let (yes, no) = partition P xs  | 
|
1560  | 
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
 | 
1561  | 
|
| 
 
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
 
haftmann 
parents: 
26300 
diff
changeset
 | 
1562  | 
lemma partition_filter1:  | 
| 
 
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
 
haftmann 
parents: 
26300 
diff
changeset
 | 
1563  | 
"fst (partition P xs) = filter P xs"  | 
| 
 
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
 
haftmann 
parents: 
26300 
diff
changeset
 | 
1564  | 
by (induct xs) (auto simp add: Let_def split_def)  | 
| 
 
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
 
haftmann 
parents: 
26300 
diff
changeset
 | 
1565  | 
|
| 
 
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
 
haftmann 
parents: 
26300 
diff
changeset
 | 
1566  | 
lemma partition_filter2:  | 
| 
 
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
 
haftmann 
parents: 
26300 
diff
changeset
 | 
1567  | 
"snd (partition P xs) = filter (Not o P) xs"  | 
| 
 
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
 
haftmann 
parents: 
26300 
diff
changeset
 | 
1568  | 
by (induct xs) (auto simp add: Let_def split_def)  | 
| 
 
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
 
haftmann 
parents: 
26300 
diff
changeset
 | 
1569  | 
|
| 
 
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
 
haftmann 
parents: 
26300 
diff
changeset
 | 
1570  | 
lemma partition_P:  | 
| 
 
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
 
haftmann 
parents: 
26300 
diff
changeset
 | 
1571  | 
assumes "partition P xs = (yes, no)"  | 
| 
 
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
 
haftmann 
parents: 
26300 
diff
changeset
 | 
1572  | 
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
 | 
1573  | 
proof -  | 
| 
 
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
 
haftmann 
parents: 
26300 
diff
changeset
 | 
1574  | 
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
 | 
1575  | 
by simp_all  | 
| 
 
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
 
haftmann 
parents: 
26300 
diff
changeset
 | 
1576  | 
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
 | 
1577  | 
qed  | 
| 
 
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
 
haftmann 
parents: 
26300 
diff
changeset
 | 
1578  | 
|
| 
 
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
 
haftmann 
parents: 
26300 
diff
changeset
 | 
1579  | 
lemma partition_set:  | 
| 
 
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
 
haftmann 
parents: 
26300 
diff
changeset
 | 
1580  | 
assumes "partition P xs = (yes, no)"  | 
| 
 
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
 
haftmann 
parents: 
26300 
diff
changeset
 | 
1581  | 
shows "set yes \<union> set no = set xs"  | 
| 
 
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
 
haftmann 
parents: 
26300 
diff
changeset
 | 
1582  | 
proof -  | 
| 
 
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
 
haftmann 
parents: 
26300 
diff
changeset
 | 
1583  | 
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
 | 
1584  | 
by simp_all  | 
| 
 
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
 
haftmann 
parents: 
26300 
diff
changeset
 | 
1585  | 
then show ?thesis by (auto simp add: partition_filter1 partition_filter2)  | 
| 
 
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
 
haftmann 
parents: 
26300 
diff
changeset
 | 
1586  | 
qed  | 
| 
 
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
 
haftmann 
parents: 
26300 
diff
changeset
 | 
1587  | 
|
| 
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
 | 
1588  | 
lemma partition_filter_conv[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
 | 
1589  | 
"partition f xs = (filter f xs,filter (Not o f) 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
 | 
1590  | 
unfolding partition_filter2[symmetric]  | 
| 
 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 
hoelzl 
parents: 
33593 
diff
changeset
 | 
1591  | 
unfolding partition_filter1[symmetric] 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
 | 
1592  | 
|
| 
 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 
hoelzl 
parents: 
33593 
diff
changeset
 | 
1593  | 
declare partition.simps[simp del]  | 
| 
26442
 
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
 
haftmann 
parents: 
26300 
diff
changeset
 | 
1594  | 
|
| 35115 | 1595  | 
|
| 
49948
 
744934b818c7
moved quite generic material from theory Enum to more appropriate places
 
haftmann 
parents: 
49808 
diff
changeset
 | 
1596  | 
subsubsection {* @{const concat} *}
 | 
| 13114 | 1597  | 
|
| 13142 | 1598  | 
lemma concat_append [simp]: "concat (xs @ ys) = concat xs @ concat ys"  | 
| 13145 | 1599  | 
by (induct xs) auto  | 
| 13114 | 1600  | 
|
| 18447 | 1601  | 
lemma concat_eq_Nil_conv [simp]: "(concat xss = []) = (\<forall>xs \<in> set xss. xs = [])"  | 
| 13145 | 1602  | 
by (induct xss) auto  | 
| 13114 | 1603  | 
|
| 18447 | 1604  | 
lemma Nil_eq_concat_conv [simp]: "([] = concat xss) = (\<forall>xs \<in> set xss. xs = [])"  | 
| 13145 | 1605  | 
by (induct xss) auto  | 
| 13114 | 1606  | 
|
| 24308 | 1607  | 
lemma set_concat [simp]: "set (concat xs) = (UN x:set xs. set x)"  | 
| 13145 | 1608  | 
by (induct xs) auto  | 
| 13114 | 1609  | 
|
| 
24476
 
f7ad9fbbeeaa
turned list comprehension translations into ML to optimize base case
 
nipkow 
parents: 
24471 
diff
changeset
 | 
1610  | 
lemma concat_map_singleton[simp]: "concat(map (%x. [f x]) xs) = map f xs"  | 
| 24349 | 1611  | 
by (induct xs) auto  | 
1612  | 
||
| 13142 | 1613  | 
lemma map_concat: "map f (concat xs) = concat (map (map f) xs)"  | 
| 13145 | 1614  | 
by (induct xs) auto  | 
| 13114 | 1615  | 
|
| 13142 | 1616  | 
lemma filter_concat: "filter p (concat xs) = concat (map (filter p) xs)"  | 
| 13145 | 1617  | 
by (induct xs) auto  | 
| 13114 | 1618  | 
|
| 13142 | 1619  | 
lemma rev_concat: "rev (concat xs) = concat (map rev (rev xs))"  | 
| 13145 | 1620  | 
by (induct xs) auto  | 
| 13114 | 1621  | 
|
| 
40365
 
a1456f2e1c9d
added two lemmas about injectivity of concat to the list theory
 
bulwahn 
parents: 
40304 
diff
changeset
 | 
1622  | 
lemma concat_eq_concat_iff: "\<forall>(x, y) \<in> set (zip xs ys). length x = length y ==> length xs = length ys ==> (concat xs = concat ys) = (xs = ys)"  | 
| 
 
a1456f2e1c9d
added two lemmas about injectivity of concat to the list theory
 
bulwahn 
parents: 
40304 
diff
changeset
 | 
1623  | 
proof (induct xs arbitrary: ys)  | 
| 
 
a1456f2e1c9d
added two lemmas about injectivity of concat to the list theory
 
bulwahn 
parents: 
40304 
diff
changeset
 | 
1624  | 
case (Cons x xs ys)  | 
| 
 
a1456f2e1c9d
added two lemmas about injectivity of concat to the list theory
 
bulwahn 
parents: 
40304 
diff
changeset
 | 
1625  | 
thus ?case by (cases ys) auto  | 
| 
 
a1456f2e1c9d
added two lemmas about injectivity of concat to the list theory
 
bulwahn 
parents: 
40304 
diff
changeset
 | 
1626  | 
qed (auto)  | 
| 
 
a1456f2e1c9d
added two lemmas about injectivity of concat to the list theory
 
bulwahn 
parents: 
40304 
diff
changeset
 | 
1627  | 
|
| 
 
a1456f2e1c9d
added two lemmas about injectivity of concat to the list theory
 
bulwahn 
parents: 
40304 
diff
changeset
 | 
1628  | 
lemma concat_injective: "concat xs = concat ys ==> length xs = length ys ==> \<forall>(x, y) \<in> set (zip xs ys). length x = length y ==> xs = ys"  | 
| 
 
a1456f2e1c9d
added two lemmas about injectivity of concat to the list theory
 
bulwahn 
parents: 
40304 
diff
changeset
 | 
1629  | 
by (simp add: concat_eq_concat_iff)  | 
| 
 
a1456f2e1c9d
added two lemmas about injectivity of concat to the list theory
 
bulwahn 
parents: 
40304 
diff
changeset
 | 
1630  | 
|
| 13114 | 1631  | 
|
| 
49948
 
744934b818c7
moved quite generic material from theory Enum to more appropriate places
 
haftmann 
parents: 
49808 
diff
changeset
 | 
1632  | 
subsubsection {* @{const nth} *}
 | 
| 13114 | 1633  | 
|
| 29827 | 1634  | 
lemma nth_Cons_0 [simp, code]: "(x # xs)!0 = x"  | 
| 13145 | 1635  | 
by auto  | 
| 13114 | 1636  | 
|
| 29827 | 1637  | 
lemma nth_Cons_Suc [simp, code]: "(x # xs)!(Suc n) = xs!n"  | 
| 13145 | 1638  | 
by auto  | 
| 13114 | 1639  | 
|
| 13142 | 1640  | 
declare nth.simps [simp del]  | 
| 13114 | 1641  | 
|
| 41842 | 1642  | 
lemma nth_Cons_pos[simp]: "0 < n \<Longrightarrow> (x#xs) ! n = xs ! (n - 1)"  | 
1643  | 
by(auto simp: Nat.gr0_conv_Suc)  | 
|
1644  | 
||
| 13114 | 1645  | 
lemma nth_append:  | 
| 24526 | 1646  | 
"(xs @ ys)!n = (if n < length xs then xs!n else ys!(n - length xs))"  | 
1647  | 
apply (induct xs arbitrary: n, simp)  | 
|
| 14208 | 1648  | 
apply (case_tac n, auto)  | 
| 13145 | 1649  | 
done  | 
| 13114 | 1650  | 
|
| 
14402
 
4201e1916482
moved lemmas from MicroJava/Comp/AuxLemmas.thy to List.thy
 
nipkow 
parents: 
14395 
diff
changeset
 | 
1651  | 
lemma nth_append_length [simp]: "(xs @ x # ys) ! length xs = x"  | 
| 
25221
 
5ded95dda5df
append/member: more light-weight way to declare authentic syntax;
 
wenzelm 
parents: 
25215 
diff
changeset
 | 
1652  | 
by (induct xs) auto  | 
| 
14402
 
4201e1916482
moved lemmas from MicroJava/Comp/AuxLemmas.thy to List.thy
 
nipkow 
parents: 
14395 
diff
changeset
 | 
1653  | 
|
| 
 
4201e1916482
moved lemmas from MicroJava/Comp/AuxLemmas.thy to List.thy
 
nipkow 
parents: 
14395 
diff
changeset
 | 
1654  | 
lemma nth_append_length_plus[simp]: "(xs @ ys) ! (length xs + n) = ys ! n"  | 
| 
25221
 
5ded95dda5df
append/member: more light-weight way to declare authentic syntax;
 
wenzelm 
parents: 
25215 
diff
changeset
 | 
1655  | 
by (induct xs) auto  | 
| 
14402
 
4201e1916482
moved lemmas from MicroJava/Comp/AuxLemmas.thy to List.thy
 
nipkow 
parents: 
14395 
diff
changeset
 | 
1656  | 
|
| 24526 | 1657  | 
lemma nth_map [simp]: "n < length xs ==> (map f xs)!n = f(xs!n)"  | 
1658  | 
apply (induct xs arbitrary: n, simp)  | 
|
| 14208 | 1659  | 
apply (case_tac n, auto)  | 
| 13145 | 1660  | 
done  | 
| 13114 | 1661  | 
|
| 45841 | 1662  | 
lemma nth_tl:  | 
1663  | 
assumes "n < length (tl x)" shows "tl x ! n = x ! Suc n"  | 
|
1664  | 
using assms by (induct x) auto  | 
|
1665  | 
||
| 18423 | 1666  | 
lemma hd_conv_nth: "xs \<noteq> [] \<Longrightarrow> hd xs = xs!0"  | 
1667  | 
by(cases xs) simp_all  | 
|
1668  | 
||
| 18049 | 1669  | 
|
1670  | 
lemma list_eq_iff_nth_eq:  | 
|
| 24526 | 1671  | 
"(xs = ys) = (length xs = length ys \<and> (ALL i<length xs. xs!i = ys!i))"  | 
1672  | 
apply(induct xs arbitrary: ys)  | 
|
| 24632 | 1673  | 
apply force  | 
| 18049 | 1674  | 
apply(case_tac ys)  | 
1675  | 
apply simp  | 
|
1676  | 
apply(simp add:nth_Cons split:nat.split)apply blast  | 
|
1677  | 
done  | 
|
1678  | 
||
| 13142 | 1679  | 
lemma set_conv_nth: "set xs = {xs!i | i. i < length xs}"
 | 
| 15251 | 1680  | 
apply (induct xs, simp, simp)  | 
| 13145 | 1681  | 
apply safe  | 
| 
55642
 
63beb38e9258
adapted to renaming of datatype 'cases' and 'recs' to 'case' and 'rec'
 
blanchet 
parents: 
55584 
diff
changeset
 | 
1682  | 
apply (metis nat.case(1) nth.simps zero_less_Suc)  | 
| 24632 | 1683  | 
apply (metis less_Suc_eq_0_disj nth_Cons_Suc)  | 
| 14208 | 1684  | 
apply (case_tac i, simp)  | 
| 
55642
 
63beb38e9258
adapted to renaming of datatype 'cases' and 'recs' to 'case' and 'rec'
 
blanchet 
parents: 
55584 
diff
changeset
 | 
1685  | 
apply (metis diff_Suc_Suc nat.case(2) nth.simps zero_less_diff)  | 
| 13145 | 1686  | 
done  | 
| 13114 | 1687  | 
|
| 17501 | 1688  | 
lemma in_set_conv_nth: "(x \<in> set xs) = (\<exists>i < length xs. xs!i = x)"  | 
1689  | 
by(auto simp:set_conv_nth)  | 
|
1690  | 
||
| 
51160
 
599ff65b85e2
systematic conversions between nat and nibble/char;
 
haftmann 
parents: 
51112 
diff
changeset
 | 
1691  | 
lemma nth_equal_first_eq:  | 
| 
 
599ff65b85e2
systematic conversions between nat and nibble/char;
 
haftmann 
parents: 
51112 
diff
changeset
 | 
1692  | 
assumes "x \<notin> set xs"  | 
| 
 
599ff65b85e2
systematic conversions between nat and nibble/char;
 
haftmann 
parents: 
51112 
diff
changeset
 | 
1693  | 
assumes "n \<le> length xs"  | 
| 
 
599ff65b85e2
systematic conversions between nat and nibble/char;
 
haftmann 
parents: 
51112 
diff
changeset
 | 
1694  | 
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
 | 
1695  | 
proof  | 
| 
 
599ff65b85e2
systematic conversions between nat and nibble/char;
 
haftmann 
parents: 
51112 
diff
changeset
 | 
1696  | 
assume ?lhs  | 
| 
 
599ff65b85e2
systematic conversions between nat and nibble/char;
 
haftmann 
parents: 
51112 
diff
changeset
 | 
1697  | 
show ?rhs  | 
| 
 
599ff65b85e2
systematic conversions between nat and nibble/char;
 
haftmann 
parents: 
51112 
diff
changeset
 | 
1698  | 
proof (rule ccontr)  | 
| 
 
599ff65b85e2
systematic conversions between nat and nibble/char;
 
haftmann 
parents: 
51112 
diff
changeset
 | 
1699  | 
assume "n \<noteq> 0"  | 
| 
 
599ff65b85e2
systematic conversions between nat and nibble/char;
 
haftmann 
parents: 
51112 
diff
changeset
 | 
1700  | 
then have "n > 0" by simp  | 
| 
 
599ff65b85e2
systematic conversions between nat and nibble/char;
 
haftmann 
parents: 
51112 
diff
changeset
 | 
1701  | 
with `?lhs` have "xs ! (n - 1) = x" by simp  | 
| 
 
599ff65b85e2
systematic conversions between nat and nibble/char;
 
haftmann 
parents: 
51112 
diff
changeset
 | 
1702  | 
moreover from `n > 0` `n \<le> length xs` have "n - 1 < length xs" by simp  | 
| 
 
599ff65b85e2
systematic conversions between nat and nibble/char;
 
haftmann 
parents: 
51112 
diff
changeset
 | 
1703  | 
ultimately have "\<exists>i<length xs. xs ! i = x" by auto  | 
| 
 
599ff65b85e2
systematic conversions between nat and nibble/char;
 
haftmann 
parents: 
51112 
diff
changeset
 | 
1704  | 
with `x \<notin> set xs` in_set_conv_nth [of x xs] show False by simp  | 
| 
 
599ff65b85e2
systematic conversions between nat and nibble/char;
 
haftmann 
parents: 
51112 
diff
changeset
 | 
1705  | 
qed  | 
| 
 
599ff65b85e2
systematic conversions between nat and nibble/char;
 
haftmann 
parents: 
51112 
diff
changeset
 | 
1706  | 
next  | 
| 
 
599ff65b85e2
systematic conversions between nat and nibble/char;
 
haftmann 
parents: 
51112 
diff
changeset
 | 
1707  | 
assume ?rhs then show ?lhs by simp  | 
| 
 
599ff65b85e2
systematic conversions between nat and nibble/char;
 
haftmann 
parents: 
51112 
diff
changeset
 | 
1708  | 
qed  | 
| 
 
599ff65b85e2
systematic conversions between nat and nibble/char;
 
haftmann 
parents: 
51112 
diff
changeset
 | 
1709  | 
|
| 
 
599ff65b85e2
systematic conversions between nat and nibble/char;
 
haftmann 
parents: 
51112 
diff
changeset
 | 
1710  | 
lemma nth_non_equal_first_eq:  | 
| 
 
599ff65b85e2
systematic conversions between nat and nibble/char;
 
haftmann 
parents: 
51112 
diff
changeset
 | 
1711  | 
assumes "x \<noteq> y"  | 
| 
 
599ff65b85e2
systematic conversions between nat and nibble/char;
 
haftmann 
parents: 
51112 
diff
changeset
 | 
1712  | 
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
 | 
1713  | 
proof  | 
| 
 
599ff65b85e2
systematic conversions between nat and nibble/char;
 
haftmann 
parents: 
51112 
diff
changeset
 | 
1714  | 
assume "?lhs" with assms have "n > 0" by (cases n) simp_all  | 
| 
 
599ff65b85e2
systematic conversions between nat and nibble/char;
 
haftmann 
parents: 
51112 
diff
changeset
 | 
1715  | 
with `?lhs` show ?rhs by simp  | 
| 
 
599ff65b85e2
systematic conversions between nat and nibble/char;
 
haftmann 
parents: 
51112 
diff
changeset
 | 
1716  | 
next  | 
| 
 
599ff65b85e2
systematic conversions between nat and nibble/char;
 
haftmann 
parents: 
51112 
diff
changeset
 | 
1717  | 
assume "?rhs" then show "?lhs" by simp  | 
| 
 
599ff65b85e2
systematic conversions between nat and nibble/char;
 
haftmann 
parents: 
51112 
diff
changeset
 | 
1718  | 
qed  | 
| 
 
599ff65b85e2
systematic conversions between nat and nibble/char;
 
haftmann 
parents: 
51112 
diff
changeset
 | 
1719  | 
|
| 13145 | 1720  | 
lemma list_ball_nth: "[| n < length xs; !x : set xs. P x|] ==> P(xs!n)"  | 
1721  | 
by (auto simp add: set_conv_nth)  | 
|
| 13114 | 1722  | 
|
| 13142 | 1723  | 
lemma nth_mem [simp]: "n < length xs ==> xs!n : set xs"  | 
| 13145 | 1724  | 
by (auto simp add: set_conv_nth)  | 
| 13114 | 1725  | 
|
1726  | 
lemma all_nth_imp_all_set:  | 
|
| 13145 | 1727  | 
"[| !i < length xs. P(xs!i); x : set xs|] ==> P x"  | 
1728  | 
by (auto simp add: set_conv_nth)  | 
|
| 13114 | 1729  | 
|
1730  | 
lemma all_set_conv_all_nth:  | 
|
| 13145 | 1731  | 
"(\<forall>x \<in> set xs. P x) = (\<forall>i. i < length xs --> P (xs ! i))"  | 
1732  | 
by (auto simp add: set_conv_nth)  | 
|
| 13114 | 1733  | 
|
| 25296 | 1734  | 
lemma rev_nth:  | 
1735  | 
"n < size xs \<Longrightarrow> rev xs ! n = xs ! (length xs - Suc n)"  | 
|
1736  | 
proof (induct xs arbitrary: n)  | 
|
1737  | 
case Nil thus ?case by simp  | 
|
1738  | 
next  | 
|
1739  | 
case (Cons x xs)  | 
|
1740  | 
hence n: "n < Suc (length xs)" by simp  | 
|
1741  | 
moreover  | 
|
1742  | 
  { assume "n < length xs"
 | 
|
| 
53374
 
a14d2a854c02
tuned proofs -- clarified flow of facts wrt. calculation;
 
wenzelm 
parents: 
53017 
diff
changeset
 | 
1743  | 
with n obtain n' where n': "length xs - n = Suc n'"  | 
| 25296 | 1744  | 
by (cases "length xs - n", auto)  | 
1745  | 
moreover  | 
|
| 
53374
 
a14d2a854c02
tuned proofs -- clarified flow of facts wrt. calculation;
 
wenzelm 
parents: 
53017 
diff
changeset
 | 
1746  | 
from n' have "length xs - Suc n = n'" by simp  | 
| 25296 | 1747  | 
ultimately  | 
1748  | 
have "xs ! (length xs - Suc n) = (x # xs) ! (length xs - n)" by simp  | 
|
1749  | 
}  | 
|
1750  | 
ultimately  | 
|
1751  | 
show ?case by (clarsimp simp add: Cons nth_append)  | 
|
1752  | 
qed  | 
|
| 13114 | 1753  | 
|
| 31159 | 1754  | 
lemma Skolem_list_nth:  | 
1755  | 
"(ALL i<k. EX x. P i x) = (EX xs. size xs = k & (ALL i<k. P i (xs!i)))"  | 
|
1756  | 
(is "_ = (EX xs. ?P k xs)")  | 
|
1757  | 
proof(induct k)  | 
|
1758  | 
case 0 show ?case by simp  | 
|
1759  | 
next  | 
|
1760  | 
case (Suc k)  | 
|
1761  | 
show ?case (is "?L = ?R" is "_ = (EX xs. ?P' xs)")  | 
|
1762  | 
proof  | 
|
1763  | 
assume "?R" thus "?L" using Suc by auto  | 
|
1764  | 
next  | 
|
1765  | 
assume "?L"  | 
|
1766  | 
with Suc obtain x xs where "?P k xs & P k x" by (metis less_Suc_eq)  | 
|
1767  | 
hence "?P'(xs@[x])" by(simp add:nth_append less_Suc_eq)  | 
|
1768  | 
thus "?R" ..  | 
|
1769  | 
qed  | 
|
1770  | 
qed  | 
|
1771  | 
||
1772  | 
||
| 
49948
 
744934b818c7
moved quite generic material from theory Enum to more appropriate places
 
haftmann 
parents: 
49808 
diff
changeset
 | 
1773  | 
subsubsection {* @{const list_update} *}
 | 
| 13114 | 1774  | 
|
| 24526 | 1775  | 
lemma length_list_update [simp]: "length(xs[i:=x]) = length xs"  | 
1776  | 
by (induct xs arbitrary: i) (auto split: nat.split)  | 
|
| 13114 | 1777  | 
|
1778  | 
lemma nth_list_update:  | 
|
| 24526 | 1779  | 
"i < length xs==> (xs[i:=x])!j = (if i = j then x else xs!j)"  | 
1780  | 
by (induct xs arbitrary: i j) (auto simp add: nth_Cons split: nat.split)  | 
|
| 13114 | 1781  | 
|
| 13142 | 1782  | 
lemma nth_list_update_eq [simp]: "i < length xs ==> (xs[i:=x])!i = x"  | 
| 13145 | 1783  | 
by (simp add: nth_list_update)  | 
| 13114 | 1784  | 
|
| 24526 | 1785  | 
lemma nth_list_update_neq [simp]: "i \<noteq> j ==> xs[i:=x]!j = xs!j"  | 
1786  | 
by (induct xs arbitrary: i j) (auto simp add: nth_Cons split: nat.split)  | 
|
| 13114 | 1787  | 
|
| 24526 | 1788  | 
lemma list_update_id[simp]: "xs[i := xs!i] = xs"  | 
1789  | 
by (induct xs arbitrary: i) (simp_all split:nat.splits)  | 
|
1790  | 
||
1791  | 
lemma list_update_beyond[simp]: "length xs \<le> i \<Longrightarrow> xs[i:=x] = xs"  | 
|
1792  | 
apply (induct xs arbitrary: i)  | 
|
| 17501 | 1793  | 
apply simp  | 
1794  | 
apply (case_tac i)  | 
|
1795  | 
apply simp_all  | 
|
1796  | 
done  | 
|
1797  | 
||
| 31077 | 1798  | 
lemma list_update_nonempty[simp]: "xs[k:=x] = [] \<longleftrightarrow> xs=[]"  | 
| 56085 | 1799  | 
by (simp only: length_0_conv[symmetric] length_list_update)  | 
| 31077 | 1800  | 
|
| 13114 | 1801  | 
lemma list_update_same_conv:  | 
| 24526 | 1802  | 
"i < length xs ==> (xs[i := x] = xs) = (xs!i = x)"  | 
1803  | 
by (induct xs arbitrary: i) (auto split: nat.split)  | 
|
| 13114 | 1804  | 
|
| 14187 | 1805  | 
lemma list_update_append1:  | 
| 24526 | 1806  | 
"i < size xs \<Longrightarrow> (xs @ ys)[i:=x] = xs[i:=x] @ ys"  | 
1807  | 
apply (induct xs arbitrary: i, simp)  | 
|
| 14187 | 1808  | 
apply(simp split:nat.split)  | 
1809  | 
done  | 
|
1810  | 
||
| 15868 | 1811  | 
lemma list_update_append:  | 
| 24526 | 1812  | 
"(xs @ ys) [n:= x] =  | 
| 15868 | 1813  | 
(if n < length xs then xs[n:= x] @ ys else xs @ (ys [n-length xs:= x]))"  | 
| 24526 | 1814  | 
by (induct xs arbitrary: n) (auto split:nat.splits)  | 
| 15868 | 1815  | 
|
| 
14402
 
4201e1916482
moved lemmas from MicroJava/Comp/AuxLemmas.thy to List.thy
 
nipkow 
parents: 
14395 
diff
changeset
 | 
1816  | 
lemma list_update_length [simp]:  | 
| 
 
4201e1916482
moved lemmas from MicroJava/Comp/AuxLemmas.thy to List.thy
 
nipkow 
parents: 
14395 
diff
changeset
 | 
1817  | 
"(xs @ x # ys)[length xs := y] = (xs @ y # ys)"  | 
| 
 
4201e1916482
moved lemmas from MicroJava/Comp/AuxLemmas.thy to List.thy
 
nipkow 
parents: 
14395 
diff
changeset
 | 
1818  | 
by (induct xs, auto)  | 
| 
 
4201e1916482
moved lemmas from MicroJava/Comp/AuxLemmas.thy to List.thy
 
nipkow 
parents: 
14395 
diff
changeset
 | 
1819  | 
|
| 31264 | 1820  | 
lemma map_update: "map f (xs[k:= y]) = (map f xs)[k := f y]"  | 
1821  | 
by(induct xs arbitrary: k)(auto split:nat.splits)  | 
|
1822  | 
||
1823  | 
lemma rev_update:  | 
|
1824  | 
"k < length xs \<Longrightarrow> rev (xs[k:= y]) = (rev xs)[length xs - k - 1 := y]"  | 
|
1825  | 
by (induct xs arbitrary: k) (auto simp: list_update_append split:nat.splits)  | 
|
1826  | 
||
| 13114 | 1827  | 
lemma update_zip:  | 
| 31080 | 1828  | 
"(zip xs ys)[i:=xy] = zip (xs[i:=fst xy]) (ys[i:=snd xy])"  | 
| 24526 | 1829  | 
by (induct ys arbitrary: i xy xs) (auto, case_tac xs, auto split: nat.split)  | 
1830  | 
||
1831  | 
lemma set_update_subset_insert: "set(xs[i:=x]) <= insert x (set xs)"  | 
|
1832  | 
by (induct xs arbitrary: i) (auto split: nat.split)  | 
|
| 13114 | 1833  | 
|
1834  | 
lemma set_update_subsetI: "[| set xs <= A; x:A |] ==> set(xs[i := x]) <= A"  | 
|
| 13145 | 1835  | 
by (blast dest!: set_update_subset_insert [THEN subsetD])  | 
| 13114 | 1836  | 
|
| 24526 | 1837  | 
lemma set_update_memI: "n < length xs \<Longrightarrow> x \<in> set (xs[n := x])"  | 
1838  | 
by (induct xs arbitrary: n) (auto split:nat.splits)  | 
|
| 15868 | 1839  | 
|
| 31077 | 1840  | 
lemma list_update_overwrite[simp]:  | 
| 24796 | 1841  | 
"xs [i := x, i := y] = xs [i := y]"  | 
| 31077 | 1842  | 
apply (induct xs arbitrary: i) apply simp  | 
1843  | 
apply (case_tac i, simp_all)  | 
|
| 24796 | 1844  | 
done  | 
1845  | 
||
1846  | 
lemma list_update_swap:  | 
|
1847  | 
"i \<noteq> i' \<Longrightarrow> xs [i := x, i' := x'] = xs [i' := x', i := x]"  | 
|
1848  | 
apply (induct xs arbitrary: i i')  | 
|
| 57537 | 1849  | 
apply simp  | 
| 24796 | 1850  | 
apply (case_tac i, case_tac i')  | 
| 57537 | 1851  | 
apply auto  | 
| 24796 | 1852  | 
apply (case_tac i')  | 
1853  | 
apply auto  | 
|
1854  | 
done  | 
|
1855  | 
||
| 29827 | 1856  | 
lemma list_update_code [code]:  | 
1857  | 
"[][i := y] = []"  | 
|
1858  | 
"(x # xs)[0 := y] = y # xs"  | 
|
1859  | 
"(x # xs)[Suc i := y] = x # xs[i := y]"  | 
|
1860  | 
by simp_all  | 
|
1861  | 
||
| 13114 | 1862  | 
|
| 
49948
 
744934b818c7
moved quite generic material from theory Enum to more appropriate places
 
haftmann 
parents: 
49808 
diff
changeset
 | 
1863  | 
subsubsection {* @{const last} and @{const butlast} *}
 | 
| 13114 | 1864  | 
|
| 13142 | 1865  | 
lemma last_snoc [simp]: "last (xs @ [x]) = x"  | 
| 13145 | 1866  | 
by (induct xs) auto  | 
| 13114 | 1867  | 
|
| 13142 | 1868  | 
lemma butlast_snoc [simp]: "butlast (xs @ [x]) = xs"  | 
| 13145 | 1869  | 
by (induct xs) auto  | 
| 13114 | 1870  | 
|
| 14302 | 1871  | 
lemma last_ConsL: "xs = [] \<Longrightarrow> last(x#xs) = x"  | 
| 44921 | 1872  | 
by simp  | 
| 14302 | 1873  | 
|
1874  | 
lemma last_ConsR: "xs \<noteq> [] \<Longrightarrow> last(x#xs) = last xs"  | 
|
| 44921 | 1875  | 
by simp  | 
| 14302 | 1876  | 
|
1877  | 
lemma last_append: "last(xs @ ys) = (if ys = [] then last xs else last ys)"  | 
|
1878  | 
by (induct xs) (auto)  | 
|
1879  | 
||
1880  | 
lemma last_appendL[simp]: "ys = [] \<Longrightarrow> last(xs @ ys) = last xs"  | 
|
1881  | 
by(simp add:last_append)  | 
|
1882  | 
||
1883  | 
lemma last_appendR[simp]: "ys \<noteq> [] \<Longrightarrow> last(xs @ ys) = last ys"  | 
|
1884  | 
by(simp add:last_append)  | 
|
1885  | 
||
| 45841 | 1886  | 
lemma last_tl: "xs = [] \<or> tl xs \<noteq> [] \<Longrightarrow>last (tl xs) = last xs"  | 
1887  | 
by (induct xs) simp_all  | 
|
1888  | 
||
1889  | 
lemma butlast_tl: "butlast (tl xs) = tl (butlast xs)"  | 
|
1890  | 
by (induct xs) simp_all  | 
|
1891  | 
||
| 17762 | 1892  | 
lemma hd_rev: "xs \<noteq> [] \<Longrightarrow> hd(rev xs) = last xs"  | 
1893  | 
by(rule rev_exhaust[of xs]) simp_all  | 
|
1894  | 
||
1895  | 
lemma last_rev: "xs \<noteq> [] \<Longrightarrow> last(rev xs) = hd xs"  | 
|
1896  | 
by(cases xs) simp_all  | 
|
1897  | 
||
| 17765 | 1898  | 
lemma last_in_set[simp]: "as \<noteq> [] \<Longrightarrow> last as \<in> set as"  | 
1899  | 
by (induct as) auto  | 
|
| 17762 | 1900  | 
|
| 13142 | 1901  | 
lemma length_butlast [simp]: "length (butlast xs) = length xs - 1"  | 
| 13145 | 1902  | 
by (induct xs rule: rev_induct) auto  | 
| 13114 | 1903  | 
|
1904  | 
lemma butlast_append:  | 
|
| 24526 | 1905  | 
"butlast (xs @ ys) = (if ys = [] then butlast xs else xs @ butlast ys)"  | 
1906  | 
by (induct xs arbitrary: ys) auto  | 
|
| 13114 | 1907  | 
|
| 13142 | 1908  | 
lemma append_butlast_last_id [simp]:  | 
| 13145 | 1909  | 
"xs \<noteq> [] ==> butlast xs @ [last xs] = xs"  | 
1910  | 
by (induct xs) auto  | 
|
| 13114 | 1911  | 
|
| 13142 | 1912  | 
lemma in_set_butlastD: "x : set (butlast xs) ==> x : set xs"  | 
| 13145 | 1913  | 
by (induct xs) (auto split: split_if_asm)  | 
| 13114 | 1914  | 
|
1915  | 
lemma in_set_butlast_appendI:  | 
|
| 13145 | 1916  | 
"x : set (butlast xs) | x : set (butlast ys) ==> x : set (butlast (xs @ ys))"  | 
1917  | 
by (auto dest: in_set_butlastD simp add: butlast_append)  | 
|
| 13114 | 1918  | 
|
| 24526 | 1919  | 
lemma last_drop[simp]: "n < length xs \<Longrightarrow> last (drop n xs) = last xs"  | 
1920  | 
apply (induct xs arbitrary: n)  | 
|
| 17501 | 1921  | 
apply simp  | 
1922  | 
apply (auto split:nat.split)  | 
|
1923  | 
done  | 
|
1924  | 
||
| 45841 | 1925  | 
lemma nth_butlast:  | 
1926  | 
assumes "n < length (butlast xs)" shows "butlast xs ! n = xs ! n"  | 
|
1927  | 
proof (cases xs)  | 
|
1928  | 
case (Cons y ys)  | 
|
1929  | 
moreover from assms have "butlast xs ! n = (butlast xs @ [last xs]) ! n"  | 
|
1930  | 
by (simp add: nth_append)  | 
|
1931  | 
ultimately show ?thesis using append_butlast_last_id by simp  | 
|
1932  | 
qed simp  | 
|
1933  | 
||
| 
30128
 
365ee7319b86
revert some Suc 0 lemmas back to their original forms; added some simp rules for (1::nat)
 
huffman 
parents: 
30079 
diff
changeset
 | 
1934  | 
lemma last_conv_nth: "xs\<noteq>[] \<Longrightarrow> last xs = xs!(length xs - 1)"  | 
| 17589 | 1935  | 
by(induct xs)(auto simp:neq_Nil_conv)  | 
1936  | 
||
| 
30128
 
365ee7319b86
revert some Suc 0 lemmas back to their original forms; added some simp rules for (1::nat)
 
huffman 
parents: 
30079 
diff
changeset
 | 
1937  | 
lemma butlast_conv_take: "butlast xs = take (length xs - 1) xs"  | 
| 
26584
 
46f3b89b2445
move lemmas from Word/BinBoolList.thy to List.thy
 
huffman 
parents: 
26480 
diff
changeset
 | 
1938  | 
by (induct xs, simp, case_tac xs, simp_all)  | 
| 
 
46f3b89b2445
move lemmas from Word/BinBoolList.thy to List.thy
 
huffman 
parents: 
26480 
diff
changeset
 | 
1939  | 
|
| 31077 | 1940  | 
lemma last_list_update:  | 
1941  | 
"xs \<noteq> [] \<Longrightarrow> last(xs[k:=x]) = (if k = size xs - 1 then x else last xs)"  | 
|
1942  | 
by (auto simp: last_conv_nth)  | 
|
1943  | 
||
1944  | 
lemma butlast_list_update:  | 
|
1945  | 
"butlast(xs[k:=x]) =  | 
|
1946  | 
(if k = size xs - 1 then butlast xs else (butlast xs)[k:=x])"  | 
|
1947  | 
apply(cases xs rule:rev_cases)  | 
|
1948  | 
apply simp  | 
|
1949  | 
apply(simp add:list_update_append split:nat.splits)  | 
|
1950  | 
done  | 
|
1951  | 
||
| 36851 | 1952  | 
lemma last_map:  | 
1953  | 
"xs \<noteq> [] \<Longrightarrow> last (map f xs) = f (last xs)"  | 
|
1954  | 
by (cases xs rule: rev_cases) simp_all  | 
|
1955  | 
||
1956  | 
lemma map_butlast:  | 
|
1957  | 
"map f (butlast xs) = butlast (map f xs)"  | 
|
1958  | 
by (induct xs) simp_all  | 
|
1959  | 
||
| 40230 | 1960  | 
lemma snoc_eq_iff_butlast:  | 
1961  | 
"xs @ [x] = ys \<longleftrightarrow> (ys \<noteq> [] & butlast ys = xs & last ys = x)"  | 
|
| 56085 | 1962  | 
by fastforce  | 
| 40230 | 1963  | 
|
| 24796 | 1964  | 
|
| 
49948
 
744934b818c7
moved quite generic material from theory Enum to more appropriate places
 
haftmann 
parents: 
49808 
diff
changeset
 | 
1965  | 
subsubsection {* @{const take} and @{const drop} *}
 | 
| 13114 | 1966  | 
|
| 13142 | 1967  | 
lemma take_0 [simp]: "take 0 xs = []"  | 
| 13145 | 1968  | 
by (induct xs) auto  | 
| 13114 | 1969  | 
|
| 13142 | 1970  | 
lemma drop_0 [simp]: "drop 0 xs = xs"  | 
| 13145 | 1971  | 
by (induct xs) auto  | 
| 13114 | 1972  | 
|
| 13142 | 1973  | 
lemma take_Suc_Cons [simp]: "take (Suc n) (x # xs) = x # take n xs"  | 
| 13145 | 1974  | 
by simp  | 
| 13114 | 1975  | 
|
| 13142 | 1976  | 
lemma drop_Suc_Cons [simp]: "drop (Suc n) (x # xs) = drop n xs"  | 
| 13145 | 1977  | 
by simp  | 
| 13114 | 1978  | 
|
| 13142 | 1979  | 
declare take_Cons [simp del] and drop_Cons [simp del]  | 
| 13114 | 1980  | 
|
| 
30128
 
365ee7319b86
revert some Suc 0 lemmas back to their original forms; added some simp rules for (1::nat)
 
huffman 
parents: 
30079 
diff
changeset
 | 
1981  | 
lemma take_1_Cons [simp]: "take 1 (x # xs) = [x]"  | 
| 
 
365ee7319b86
revert some Suc 0 lemmas back to their original forms; added some simp rules for (1::nat)
 
huffman 
parents: 
30079 
diff
changeset
 | 
1982  | 
unfolding One_nat_def by simp  | 
| 
 
365ee7319b86
revert some Suc 0 lemmas back to their original forms; added some simp rules for (1::nat)
 
huffman 
parents: 
30079 
diff
changeset
 | 
1983  | 
|
| 
 
365ee7319b86
revert some Suc 0 lemmas back to their original forms; added some simp rules for (1::nat)
 
huffman 
parents: 
30079 
diff
changeset
 | 
1984  | 
lemma drop_1_Cons [simp]: "drop 1 (x # xs) = xs"  | 
| 
 
365ee7319b86
revert some Suc 0 lemmas back to their original forms; added some simp rules for (1::nat)
 
huffman 
parents: 
30079 
diff
changeset
 | 
1985  | 
unfolding One_nat_def by simp  | 
| 
 
365ee7319b86
revert some Suc 0 lemmas back to their original forms; added some simp rules for (1::nat)
 
huffman 
parents: 
30079 
diff
changeset
 | 
1986  | 
|
| 
15110
 
78b5636eabc7
Added a number of new thms and the new function remove1
 
nipkow 
parents: 
15072 
diff
changeset
 | 
1987  | 
lemma take_Suc: "xs ~= [] ==> take (Suc n) xs = hd xs # take n (tl xs)"  | 
| 
 
78b5636eabc7
Added a number of new thms and the new function remove1
 
nipkow 
parents: 
15072 
diff
changeset
 | 
1988  | 
by(clarsimp simp add:neq_Nil_conv)  | 
| 
 
78b5636eabc7
Added a number of new thms and the new function remove1
 
nipkow 
parents: 
15072 
diff
changeset
 | 
1989  | 
|
| 14187 | 1990  | 
lemma drop_Suc: "drop (Suc n) xs = drop n (tl xs)"  | 
1991  | 
by(cases xs, simp_all)  | 
|
1992  | 
||
| 
26584
 
46f3b89b2445
move lemmas from Word/BinBoolList.thy to List.thy
 
huffman 
parents: 
26480 
diff
changeset
 | 
1993  | 
lemma take_tl: "take n (tl xs) = tl (take (Suc n) xs)"  | 
| 
 
46f3b89b2445
move lemmas from Word/BinBoolList.thy to List.thy
 
huffman 
parents: 
26480 
diff
changeset
 | 
1994  | 
by (induct xs arbitrary: n) simp_all  | 
| 
 
46f3b89b2445
move lemmas from Word/BinBoolList.thy to List.thy
 
huffman 
parents: 
26480 
diff
changeset
 | 
1995  | 
|
| 24526 | 1996  | 
lemma drop_tl: "drop n (tl xs) = tl(drop n xs)"  | 
1997  | 
by(induct xs arbitrary: n, simp_all add:drop_Cons drop_Suc split:nat.split)  | 
|
1998  | 
||
| 
26584
 
46f3b89b2445
move lemmas from Word/BinBoolList.thy to List.thy
 
huffman 
parents: 
26480 
diff
changeset
 | 
1999  | 
lemma tl_take: "tl (take n xs) = take (n - 1) (tl xs)"  | 
| 
 
46f3b89b2445
move lemmas from Word/BinBoolList.thy to List.thy
 
huffman 
parents: 
26480 
diff
changeset
 | 
2000  | 
by (cases n, simp, cases xs, auto)  | 
| 
 
46f3b89b2445
move lemmas from Word/BinBoolList.thy to List.thy
 
huffman 
parents: 
26480 
diff
changeset
 | 
2001  | 
|
| 
 
46f3b89b2445
move lemmas from Word/BinBoolList.thy to List.thy
 
huffman 
parents: 
26480 
diff
changeset
 | 
2002  | 
lemma tl_drop: "tl (drop n xs) = drop n (tl xs)"  | 
| 
 
46f3b89b2445
move lemmas from Word/BinBoolList.thy to List.thy
 
huffman 
parents: 
26480 
diff
changeset
 | 
2003  | 
by (simp only: drop_tl)  | 
| 
 
46f3b89b2445
move lemmas from Word/BinBoolList.thy to List.thy
 
huffman 
parents: 
26480 
diff
changeset
 | 
2004  | 
|
| 24526 | 2005  | 
lemma nth_via_drop: "drop n xs = y#ys \<Longrightarrow> xs!n = y"  | 
2006  | 
apply (induct xs arbitrary: n, simp)  | 
|
| 14187 | 2007  | 
apply(simp add:drop_Cons nth_Cons split:nat.splits)  | 
2008  | 
done  | 
|
2009  | 
||
| 13913 | 2010  | 
lemma take_Suc_conv_app_nth:  | 
| 24526 | 2011  | 
"i < length xs \<Longrightarrow> take (Suc i) xs = take i xs @ [xs!i]"  | 
2012  | 
apply (induct xs arbitrary: i, simp)  | 
|
| 14208 | 2013  | 
apply (case_tac i, auto)  | 
| 13913 | 2014  | 
done  | 
2015  | 
||
| 14591 | 2016  | 
lemma drop_Suc_conv_tl:  | 
| 24526 | 2017  | 
"i < length xs \<Longrightarrow> (xs!i) # (drop (Suc i) xs) = drop i xs"  | 
2018  | 
apply (induct xs arbitrary: i, simp)  | 
|
| 14591 | 2019  | 
apply (case_tac i, auto)  | 
2020  | 
done  | 
|
2021  | 
||
| 24526 | 2022  | 
lemma length_take [simp]: "length (take n xs) = min (length xs) n"  | 
2023  | 
by (induct n arbitrary: xs) (auto, case_tac xs, auto)  | 
|
2024  | 
||
2025  | 
lemma length_drop [simp]: "length (drop n xs) = (length xs - n)"  | 
|
2026  | 
by (induct n arbitrary: xs) (auto, case_tac xs, auto)  | 
|
2027  | 
||
2028  | 
lemma take_all [simp]: "length xs <= n ==> take n xs = xs"  | 
|
2029  | 
by (induct n arbitrary: xs) (auto, case_tac xs, auto)  | 
|
2030  | 
||
2031  | 
lemma drop_all [simp]: "length xs <= n ==> drop n xs = []"  | 
|
2032  | 
by (induct n arbitrary: xs) (auto, case_tac xs, auto)  | 
|
| 13114 | 2033  | 
|
| 13142 | 2034  | 
lemma take_append [simp]:  | 
| 24526 | 2035  | 
"take n (xs @ ys) = (take n xs @ take (n - length xs) ys)"  | 
2036  | 
by (induct n arbitrary: xs) (auto, case_tac xs, auto)  | 
|
| 13114 | 2037  | 
|
| 13142 | 2038  | 
lemma drop_append [simp]:  | 
| 24526 | 2039  | 
"drop n (xs @ ys) = drop n xs @ drop (n - length xs) ys"  | 
2040  | 
by (induct n arbitrary: xs) (auto, case_tac xs, auto)  | 
|
2041  | 
||
2042  | 
lemma take_take [simp]: "take n (take m xs) = take (min n m) xs"  | 
|
2043  | 
apply (induct m arbitrary: xs n, auto)  | 
|
| 14208 | 2044  | 
apply (case_tac xs, auto)  | 
| 
15236
 
f289e8ba2bb3
Proofs needed to be updated because induction now preserves name of
 
nipkow 
parents: 
15176 
diff
changeset
 | 
2045  | 
apply (case_tac n, auto)  | 
| 13145 | 2046  | 
done  | 
| 13114 | 2047  | 
|
| 24526 | 2048  | 
lemma drop_drop [simp]: "drop n (drop m xs) = drop (n + m) xs"  | 
2049  | 
apply (induct m arbitrary: xs, auto)  | 
|
| 14208 | 2050  | 
apply (case_tac xs, auto)  | 
| 13145 | 2051  | 
done  | 
| 13114 | 2052  | 
|
| 24526 | 2053  | 
lemma take_drop: "take n (drop m xs) = drop m (take (n + m) xs)"  | 
2054  | 
apply (induct m arbitrary: xs n, auto)  | 
|
| 14208 | 2055  | 
apply (case_tac xs, auto)  | 
| 13145 | 2056  | 
done  | 
| 13114 | 2057  | 
|
| 24526 | 2058  | 
lemma drop_take: "drop n (take m xs) = take (m-n) (drop n xs)"  | 
2059  | 
apply(induct xs arbitrary: m n)  | 
|
| 14802 | 2060  | 
apply simp  | 
2061  | 
apply(simp add: take_Cons drop_Cons split:nat.split)  | 
|
2062  | 
done  | 
|
2063  | 
||
| 24526 | 2064  | 
lemma append_take_drop_id [simp]: "take n xs @ drop n xs = xs"  | 
2065  | 
apply (induct n arbitrary: xs, auto)  | 
|
| 14208 | 2066  | 
apply (case_tac xs, auto)  | 
| 13145 | 2067  | 
done  | 
| 13114 | 2068  | 
|
| 24526 | 2069  | 
lemma take_eq_Nil[simp]: "(take n xs = []) = (n = 0 \<or> xs = [])"  | 
2070  | 
apply(induct xs arbitrary: n)  | 
|
| 
15110
 
78b5636eabc7
Added a number of new thms and the new function remove1
 
nipkow 
parents: 
15072 
diff
changeset
 | 
2071  | 
apply simp  | 
| 
 
78b5636eabc7
Added a number of new thms and the new function remove1
 
nipkow 
parents: 
15072 
diff
changeset
 | 
2072  | 
apply(simp add:take_Cons split:nat.split)  | 
| 
 
78b5636eabc7
Added a number of new thms and the new function remove1
 
nipkow 
parents: 
15072 
diff
changeset
 | 
2073  | 
done  | 
| 
 
78b5636eabc7
Added a number of new thms and the new function remove1
 
nipkow 
parents: 
15072 
diff
changeset
 | 
2074  | 
|
| 24526 | 2075  | 
lemma drop_eq_Nil[simp]: "(drop n xs = []) = (length xs <= n)"  | 
2076  | 
apply(induct xs arbitrary: n)  | 
|
| 
15110
 
78b5636eabc7
Added a number of new thms and the new function remove1
 
nipkow 
parents: 
15072 
diff
changeset
 | 
2077  | 
apply simp  | 
| 
 
78b5636eabc7
Added a number of new thms and the new function remove1
 
nipkow 
parents: 
15072 
diff
changeset
 | 
2078  | 
apply(simp add:drop_Cons split:nat.split)  | 
| 
 
78b5636eabc7
Added a number of new thms and the new function remove1
 
nipkow 
parents: 
15072 
diff
changeset
 | 
2079  | 
done  | 
| 
 
78b5636eabc7
Added a number of new thms and the new function remove1
 
nipkow 
parents: 
15072 
diff
changeset
 | 
2080  | 
|
| 24526 | 2081  | 
lemma take_map: "take n (map f xs) = map f (take n xs)"  | 
2082  | 
apply (induct n arbitrary: xs, auto)  | 
|
| 14208 | 2083  | 
apply (case_tac xs, auto)  | 
| 13145 | 2084  | 
done  | 
| 13114 | 2085  | 
|
| 24526 | 2086  | 
lemma drop_map: "drop n (map f xs) = map f (drop n xs)"  | 
2087  | 
apply (induct n arbitrary: xs, auto)  | 
|
| 14208 | 2088  | 
apply (case_tac xs, auto)  | 
| 13145 | 2089  | 
done  | 
| 13114 | 2090  | 
|
| 24526 | 2091  | 
lemma rev_take: "rev (take i xs) = drop (length xs - i) (rev xs)"  | 
2092  | 
apply (induct xs arbitrary: i, auto)  | 
|
| 14208 | 2093  | 
apply (case_tac i, auto)  | 
| 13145 | 2094  | 
done  | 
| 13114 | 2095  | 
|
| 24526 | 2096  | 
lemma rev_drop: "rev (drop i xs) = take (length xs - i) (rev xs)"  | 
2097  | 
apply (induct xs arbitrary: i, auto)  | 
|
| 14208 | 2098  | 
apply (case_tac i, auto)  | 
| 13145 | 2099  | 
done  | 
| 13114 | 2100  | 
|
| 24526 | 2101  | 
lemma nth_take [simp]: "i < n ==> (take n xs)!i = xs!i"  | 
2102  | 
apply (induct xs arbitrary: i n, auto)  | 
|
| 14208 | 2103  | 
apply (case_tac n, blast)  | 
2104  | 
apply (case_tac i, auto)  | 
|
| 13145 | 2105  | 
done  | 
| 13114 | 2106  | 
|
| 13142 | 2107  | 
lemma nth_drop [simp]:  | 
| 24526 | 2108  | 
"n + i <= length xs ==> (drop n xs)!i = xs!(n + i)"  | 
2109  | 
apply (induct n arbitrary: xs i, auto)  | 
|
| 14208 | 2110  | 
apply (case_tac xs, auto)  | 
| 13145 | 2111  | 
done  | 
| 3507 | 2112  | 
|
| 
26584
 
46f3b89b2445
move lemmas from Word/BinBoolList.thy to List.thy
 
huffman 
parents: 
26480 
diff
changeset
 | 
2113  | 
lemma butlast_take:  | 
| 
30128
 
365ee7319b86
revert some Suc 0 lemmas back to their original forms; added some simp rules for (1::nat)
 
huffman 
parents: 
30079 
diff
changeset
 | 
2114  | 
"n <= length xs ==> butlast (take n xs) = take (n - 1) xs"  | 
| 
54863
 
82acc20ded73
prefer more canonical names for lemmas on min/max
 
haftmann 
parents: 
54600 
diff
changeset
 | 
2115  | 
by (simp add: butlast_conv_take min.absorb1 min.absorb2)  | 
| 
26584
 
46f3b89b2445
move lemmas from Word/BinBoolList.thy to List.thy
 
huffman 
parents: 
26480 
diff
changeset
 | 
2116  | 
|
| 
 
46f3b89b2445
move lemmas from Word/BinBoolList.thy to List.thy
 
huffman 
parents: 
26480 
diff
changeset
 | 
2117  | 
lemma butlast_drop: "butlast (drop n xs) = drop n (butlast xs)"  | 
| 
57514
 
bdc2c6b40bf2
prefer ac_simps collections over separate name bindings for add and mult
 
haftmann 
parents: 
57512 
diff
changeset
 | 
2118  | 
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
 | 
2119  | 
|
| 
 
46f3b89b2445
move lemmas from Word/BinBoolList.thy to List.thy
 
huffman 
parents: 
26480 
diff
changeset
 | 
2120  | 
lemma take_butlast: "n < length xs ==> take n (butlast xs) = take n xs"  | 
| 
54863
 
82acc20ded73
prefer more canonical names for lemmas on min/max
 
haftmann 
parents: 
54600 
diff
changeset
 | 
2121  | 
by (simp add: butlast_conv_take min.absorb1)  | 
| 
26584
 
46f3b89b2445
move lemmas from Word/BinBoolList.thy to List.thy
 
huffman 
parents: 
26480 
diff
changeset
 | 
2122  | 
|
| 
 
46f3b89b2445
move lemmas from Word/BinBoolList.thy to List.thy
 
huffman 
parents: 
26480 
diff
changeset
 | 
2123  | 
lemma drop_butlast: "drop n (butlast xs) = butlast (drop n xs)"  | 
| 
57514
 
bdc2c6b40bf2
prefer ac_simps collections over separate name bindings for add and mult
 
haftmann 
parents: 
57512 
diff
changeset
 | 
2124  | 
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
 | 
2125  | 
|
| 
46500
 
0196966d6d2d
removing unnecessary premises in theorems of List theory
 
bulwahn 
parents: 
46448 
diff
changeset
 | 
2126  | 
lemma hd_drop_conv_nth: "n < length xs \<Longrightarrow> hd(drop n xs) = xs!n"  | 
| 18423 | 2127  | 
by(simp add: hd_conv_nth)  | 
2128  | 
||
| 35248 | 2129  | 
lemma set_take_subset_set_take:  | 
2130  | 
"m <= n \<Longrightarrow> set(take m xs) <= set(take n xs)"  | 
|
| 
41463
 
edbf0a86fb1c
adding simproc to rewrite list comprehensions to set comprehensions; adopting proofs
 
bulwahn 
parents: 
41372 
diff
changeset
 | 
2131  | 
apply (induct xs arbitrary: m n)  | 
| 
 
edbf0a86fb1c
adding simproc to rewrite list comprehensions to set comprehensions; adopting proofs
 
bulwahn 
parents: 
41372 
diff
changeset
 | 
2132  | 
apply simp  | 
| 
 
edbf0a86fb1c
adding simproc to rewrite list comprehensions to set comprehensions; adopting proofs
 
bulwahn 
parents: 
41372 
diff
changeset
 | 
2133  | 
apply (case_tac n)  | 
| 
 
edbf0a86fb1c
adding simproc to rewrite list comprehensions to set comprehensions; adopting proofs
 
bulwahn 
parents: 
41372 
diff
changeset
 | 
2134  | 
apply (auto simp: take_Cons)  | 
| 
 
edbf0a86fb1c
adding simproc to rewrite list comprehensions to set comprehensions; adopting proofs
 
bulwahn 
parents: 
41372 
diff
changeset
 | 
2135  | 
done  | 
| 35248 | 2136  | 
|
| 24526 | 2137  | 
lemma set_take_subset: "set(take n xs) \<subseteq> set xs"  | 
2138  | 
by(induct xs arbitrary: n)(auto simp:take_Cons split:nat.split)  | 
|
2139  | 
||
2140  | 
lemma set_drop_subset: "set(drop n xs) \<subseteq> set xs"  | 
|
2141  | 
by(induct xs arbitrary: n)(auto simp:drop_Cons split:nat.split)  | 
|
| 14025 | 2142  | 
|
| 35248 | 2143  | 
lemma set_drop_subset_set_drop:  | 
2144  | 
"m >= n \<Longrightarrow> set(drop m xs) <= set(drop n xs)"  | 
|
2145  | 
apply(induct xs arbitrary: m n)  | 
|
2146  | 
apply(auto simp:drop_Cons split:nat.split)  | 
|
| 56085 | 2147  | 
by (metis set_drop_subset subset_iff)  | 
| 35248 | 2148  | 
|
| 14187 | 2149  | 
lemma in_set_takeD: "x : set(take n xs) \<Longrightarrow> x : set xs"  | 
2150  | 
using set_take_subset by fast  | 
|
2151  | 
||
2152  | 
lemma in_set_dropD: "x : set(drop n xs) \<Longrightarrow> x : set xs"  | 
|
2153  | 
using set_drop_subset by fast  | 
|
2154  | 
||
| 13114 | 2155  | 
lemma append_eq_conv_conj:  | 
| 24526 | 2156  | 
"(xs @ ys = zs) = (xs = take (length xs) zs \<and> ys = drop (length xs) zs)"  | 
2157  | 
apply (induct xs arbitrary: zs, simp, clarsimp)  | 
|
| 14208 | 2158  | 
apply (case_tac zs, auto)  | 
| 13145 | 2159  | 
done  | 
| 13142 | 2160  | 
|
| 24526 | 2161  | 
lemma take_add:  | 
| 42713 | 2162  | 
"take (i+j) xs = take i xs @ take j (drop i xs)"  | 
| 24526 | 2163  | 
apply (induct xs arbitrary: i, auto)  | 
2164  | 
apply (case_tac i, simp_all)  | 
|
| 14050 | 2165  | 
done  | 
2166  | 
||
| 14300 | 2167  | 
lemma append_eq_append_conv_if:  | 
| 
53015
 
a1119cf551e8
standardized symbols via "isabelle update_sub_sup", excluding src/Pure and src/Tools/WWW_Find;
 
wenzelm 
parents: 
52435 
diff
changeset
 | 
2168  | 
"(xs\<^sub>1 @ xs\<^sub>2 = 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
 | 
2169  | 
(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
 | 
2170  | 
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
 | 
2171  | 
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)"  | 
| 
 
a1119cf551e8
standardized symbols via "isabelle update_sub_sup", excluding src/Pure and src/Tools/WWW_Find;
 
wenzelm 
parents: 
52435 
diff
changeset
 | 
2172  | 
apply(induct xs\<^sub>1 arbitrary: ys\<^sub>1)  | 
| 14300 | 2173  | 
apply simp  | 
| 
53015
 
a1119cf551e8
standardized symbols via "isabelle update_sub_sup", excluding src/Pure and src/Tools/WWW_Find;
 
wenzelm 
parents: 
52435 
diff
changeset
 | 
2174  | 
apply(case_tac ys\<^sub>1)  | 
| 14300 | 2175  | 
apply simp_all  | 
2176  | 
done  | 
|
2177  | 
||
| 
15110
 
78b5636eabc7
Added a number of new thms and the new function remove1
 
nipkow 
parents: 
15072 
diff
changeset
 | 
2178  | 
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
 | 
2179  | 
"n < length xs \<Longrightarrow> take n xs @ [hd (drop n xs)] = take (Suc n) xs"  | 
| 24526 | 2180  | 
apply(induct xs arbitrary: n)  | 
| 
15110
 
78b5636eabc7
Added a number of new thms and the new function remove1
 
nipkow 
parents: 
15072 
diff
changeset
 | 
2181  | 
apply simp  | 
| 
 
78b5636eabc7
Added a number of new thms and the new function remove1
 
nipkow 
parents: 
15072 
diff
changeset
 | 
2182  | 
apply(simp add:drop_Cons split:nat.split)  | 
| 
 
78b5636eabc7
Added a number of new thms and the new function remove1
 
nipkow 
parents: 
15072 
diff
changeset
 | 
2183  | 
done  | 
| 
 
78b5636eabc7
Added a number of new thms and the new function remove1
 
nipkow 
parents: 
15072 
diff
changeset
 | 
2184  | 
|
| 17501 | 2185  | 
lemma id_take_nth_drop:  | 
2186  | 
"i < length xs \<Longrightarrow> xs = take i xs @ xs!i # drop (Suc i) xs"  | 
|
2187  | 
proof -  | 
|
2188  | 
assume si: "i < length xs"  | 
|
2189  | 
hence "xs = take (Suc i) xs @ drop (Suc i) xs" by auto  | 
|
2190  | 
moreover  | 
|
2191  | 
from si have "take (Suc i) xs = take i xs @ [xs!i]"  | 
|
2192  | 
apply (rule_tac take_Suc_conv_app_nth) by arith  | 
|
2193  | 
ultimately show ?thesis by auto  | 
|
2194  | 
qed  | 
|
2195  | 
||
2196  | 
lemma upd_conv_take_nth_drop:  | 
|
2197  | 
"i < length xs \<Longrightarrow> xs[i:=a] = take i xs @ a # drop (Suc i) xs"  | 
|
2198  | 
proof -  | 
|
2199  | 
assume i: "i < length xs"  | 
|
2200  | 
have "xs[i:=a] = (take i xs @ xs!i # drop (Suc i) xs)[i:=a]"  | 
|
2201  | 
by(rule arg_cong[OF id_take_nth_drop[OF i]])  | 
|
2202  | 
also have "\<dots> = take i xs @ a # drop (Suc i) xs"  | 
|
2203  | 
using i by (simp add: list_update_append)  | 
|
2204  | 
finally show ?thesis .  | 
|
2205  | 
qed  | 
|
2206  | 
||
| 24796 | 2207  | 
lemma nth_drop':  | 
2208  | 
"i < length xs \<Longrightarrow> xs ! i # drop (Suc i) xs = drop i xs"  | 
|
2209  | 
apply (induct i arbitrary: xs)  | 
|
2210  | 
apply (simp add: neq_Nil_conv)  | 
|
2211  | 
apply (erule exE)+  | 
|
2212  | 
apply simp  | 
|
2213  | 
apply (case_tac xs)  | 
|
2214  | 
apply simp_all  | 
|
2215  | 
done  | 
|
2216  | 
||
| 13114 | 2217  | 
|
| 
49948
 
744934b818c7
moved quite generic material from theory Enum to more appropriate places
 
haftmann 
parents: 
49808 
diff
changeset
 | 
2218  | 
subsubsection {* @{const takeWhile} and @{const dropWhile} *}
 | 
| 13114 | 2219  | 
|
| 
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
 | 
2220  | 
lemma length_takeWhile_le: "length (takeWhile P xs) \<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
 | 
2221  | 
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
 | 
2222  | 
|
| 13142 | 2223  | 
lemma takeWhile_dropWhile_id [simp]: "takeWhile P xs @ dropWhile P xs = xs"  | 
| 13145 | 2224  | 
by (induct xs) auto  | 
| 13114 | 2225  | 
|
| 13142 | 2226  | 
lemma takeWhile_append1 [simp]:  | 
| 13145 | 2227  | 
"[| x:set xs; ~P(x)|] ==> takeWhile P (xs @ ys) = takeWhile P xs"  | 
2228  | 
by (induct xs) auto  | 
|
| 13114 | 2229  | 
|
| 13142 | 2230  | 
lemma takeWhile_append2 [simp]:  | 
| 13145 | 2231  | 
"(!!x. x : set xs ==> P x) ==> takeWhile P (xs @ ys) = xs @ takeWhile P ys"  | 
2232  | 
by (induct xs) auto  | 
|
| 13114 | 2233  | 
|
| 13142 | 2234  | 
lemma takeWhile_tail: "\<not> P x ==> takeWhile P (xs @ (x#l)) = takeWhile P xs"  | 
| 13145 | 2235  | 
by (induct xs) auto  | 
| 13114 | 2236  | 
|
| 
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
 | 
2237  | 
lemma takeWhile_nth: "j < length (takeWhile P xs) \<Longrightarrow> takeWhile P xs ! j = xs ! j"  | 
| 
 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 
hoelzl 
parents: 
33593 
diff
changeset
 | 
2238  | 
apply (subst (3) takeWhile_dropWhile_id[symmetric]) unfolding nth_append by 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
 | 
2239  | 
|
| 
 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 
hoelzl 
parents: 
33593 
diff
changeset
 | 
2240  | 
lemma dropWhile_nth: "j < length (dropWhile P xs) \<Longrightarrow> dropWhile P xs ! j = xs ! (j + 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
 | 
2241  | 
apply (subst (3) takeWhile_dropWhile_id[symmetric]) unfolding nth_append by 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
 | 
2242  | 
|
| 
 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 
hoelzl 
parents: 
33593 
diff
changeset
 | 
2243  | 
lemma length_dropWhile_le: "length (dropWhile P xs) \<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
 | 
2244  | 
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
 | 
2245  | 
|
| 13142 | 2246  | 
lemma dropWhile_append1 [simp]:  | 
| 13145 | 2247  | 
"[| x : set xs; ~P(x)|] ==> dropWhile P (xs @ ys) = (dropWhile P xs)@ys"  | 
2248  | 
by (induct xs) auto  | 
|
| 13114 | 2249  | 
|
| 13142 | 2250  | 
lemma dropWhile_append2 [simp]:  | 
| 13145 | 2251  | 
"(!!x. x:set xs ==> P(x)) ==> dropWhile P (xs @ ys) = dropWhile P ys"  | 
2252  | 
by (induct xs) auto  | 
|
| 13114 | 2253  | 
|
| 45841 | 2254  | 
lemma dropWhile_append3:  | 
2255  | 
"\<not> P y \<Longrightarrow>dropWhile P (xs @ y # ys) = dropWhile P xs @ y # ys"  | 
|
2256  | 
by (induct xs) auto  | 
|
2257  | 
||
2258  | 
lemma dropWhile_last:  | 
|
2259  | 
"x \<in> set xs \<Longrightarrow> \<not> P x \<Longrightarrow> last (dropWhile P xs) = last xs"  | 
|
2260  | 
by (auto simp add: dropWhile_append3 in_set_conv_decomp)  | 
|
2261  | 
||
2262  | 
lemma set_dropWhileD: "x \<in> set (dropWhile P xs) \<Longrightarrow> x \<in> set xs"  | 
|
2263  | 
by (induct xs) (auto split: split_if_asm)  | 
|
2264  | 
||
| 
23971
 
e6d505d5b03d
renamed lemma "set_take_whileD" to "set_takeWhileD"
 
krauss 
parents: 
23740 
diff
changeset
 | 
2265  | 
lemma set_takeWhileD: "x : set (takeWhile P xs) ==> x : set xs \<and> P x"  | 
| 13145 | 2266  | 
by (induct xs) (auto split: split_if_asm)  | 
| 13114 | 2267  | 
|
| 13913 | 2268  | 
lemma takeWhile_eq_all_conv[simp]:  | 
2269  | 
"(takeWhile P xs = xs) = (\<forall>x \<in> set xs. P x)"  | 
|
2270  | 
by(induct xs, auto)  | 
|
2271  | 
||
2272  | 
lemma dropWhile_eq_Nil_conv[simp]:  | 
|
2273  | 
"(dropWhile P xs = []) = (\<forall>x \<in> set xs. P x)"  | 
|
2274  | 
by(induct xs, auto)  | 
|
2275  | 
||
2276  | 
lemma dropWhile_eq_Cons_conv:  | 
|
2277  | 
"(dropWhile P xs = y#ys) = (xs = takeWhile P xs @ y # ys & \<not> P y)"  | 
|
2278  | 
by(induct xs, auto)  | 
|
2279  | 
||
| 31077 | 2280  | 
lemma distinct_takeWhile[simp]: "distinct xs ==> distinct (takeWhile P xs)"  | 
2281  | 
by (induct xs) (auto dest: set_takeWhileD)  | 
|
2282  | 
||
2283  | 
lemma distinct_dropWhile[simp]: "distinct xs ==> distinct (dropWhile P xs)"  | 
|
2284  | 
by (induct xs) auto  | 
|
2285  | 
||
| 
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
 | 
2286  | 
lemma takeWhile_map: "takeWhile P (map f xs) = map f (takeWhile (P \<circ> f) 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
 | 
2287  | 
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
 | 
2288  | 
|
| 
 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 
hoelzl 
parents: 
33593 
diff
changeset
 | 
2289  | 
lemma dropWhile_map: "dropWhile P (map f xs) = map f (dropWhile (P \<circ> f) 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
 | 
2290  | 
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
 | 
2291  | 
|
| 
 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 
hoelzl 
parents: 
33593 
diff
changeset
 | 
2292  | 
lemma takeWhile_eq_take: "takeWhile P xs = take (length (takeWhile P xs)) 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
 | 
2293  | 
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
 | 
2294  | 
|
| 
 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 
hoelzl 
parents: 
33593 
diff
changeset
 | 
2295  | 
lemma dropWhile_eq_drop: "dropWhile P xs = drop (length (takeWhile P xs)) 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
 | 
2296  | 
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
 | 
2297  | 
|
| 
 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 
hoelzl 
parents: 
33593 
diff
changeset
 | 
2298  | 
lemma hd_dropWhile:  | 
| 
 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 
hoelzl 
parents: 
33593 
diff
changeset
 | 
2299  | 
"dropWhile P xs \<noteq> [] \<Longrightarrow> \<not> P (hd (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
 | 
2300  | 
using assms 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
 | 
2301  | 
|
| 
 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 
hoelzl 
parents: 
33593 
diff
changeset
 | 
2302  | 
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
 | 
2303  | 
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
 | 
2304  | 
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
 | 
2305  | 
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
 | 
2306  | 
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
 | 
2307  | 
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
 | 
2308  | 
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
 | 
2309  | 
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
 | 
2310  | 
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
 | 
2311  | 
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
 | 
2312  | 
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
 | 
2313  | 
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
 | 
2314  | 
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
 | 
2315  | 
|
| 
 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 
hoelzl 
parents: 
33593 
diff
changeset
 | 
2316  | 
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
 | 
2317  | 
"\<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
 | 
2318  | 
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
 | 
2319  | 
proof (induct xs arbitrary: n)  | 
| 
 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 
hoelzl 
parents: 
33593 
diff
changeset
 | 
2320  | 
case (Cons x 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
 | 
2321  | 
thus ?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
 | 
2322  | 
proof (cases n)  | 
| 
 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 
hoelzl 
parents: 
33593 
diff
changeset
 | 
2323  | 
case (Suc n') note this[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
 | 
2324  | 
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
 | 
2325  | 
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
 | 
2326  | 
proof (rule 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
 | 
2327  | 
case goal1 thus "P (xs ! i)" using Cons.prems(1)[of "Suc i"] 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
 | 
2328  | 
next case goal2 thus ?case using Cons by 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
 | 
2329  | 
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
 | 
2330  | 
ultimately show ?thesis 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
 | 
2331  | 
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
 | 
2332  | 
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
 | 
2333  | 
|
| 
 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 
hoelzl 
parents: 
33593 
diff
changeset
 | 
2334  | 
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
 | 
2335  | 
"length (takeWhile P xs) < length xs \<Longrightarrow> \<not> P (xs ! 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
 | 
2336  | 
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
 | 
2337  | 
|
| 
 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 
hoelzl 
parents: 
33593 
diff
changeset
 | 
2338  | 
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
 | 
2339  | 
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
 | 
2340  | 
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
 | 
2341  | 
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
 | 
2342  | 
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
 | 
2343  | 
hence "length (takeWhile P xs) < length xs" using assms 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
 | 
2344  | 
thus ?thesis using all `\<not> ?thesis` nth_length_takeWhile[of P xs] by 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
 | 
2345  | 
qed  | 
| 31077 | 2346  | 
|
| 17501 | 2347  | 
text{* The following two lemmmas could be generalized to an arbitrary
 | 
2348  | 
property. *}  | 
|
2349  | 
||
2350  | 
lemma takeWhile_neq_rev: "\<lbrakk>distinct xs; x \<in> set xs\<rbrakk> \<Longrightarrow>  | 
|
2351  | 
takeWhile (\<lambda>y. y \<noteq> x) (rev xs) = rev (tl (dropWhile (\<lambda>y. y \<noteq> x) xs))"  | 
|
2352  | 
by(induct xs) (auto simp: takeWhile_tail[where l="[]"])  | 
|
2353  | 
||
2354  | 
lemma dropWhile_neq_rev: "\<lbrakk>distinct xs; x \<in> set xs\<rbrakk> \<Longrightarrow>  | 
|
2355  | 
dropWhile (\<lambda>y. y \<noteq> x) (rev xs) = x # rev (takeWhile (\<lambda>y. y \<noteq> x) xs)"  | 
|
2356  | 
apply(induct xs)  | 
|
2357  | 
apply simp  | 
|
2358  | 
apply auto  | 
|
2359  | 
apply(subst dropWhile_append2)  | 
|
2360  | 
apply auto  | 
|
2361  | 
done  | 
|
2362  | 
||
| 18423 | 2363  | 
lemma takeWhile_not_last:  | 
| 
46500
 
0196966d6d2d
removing unnecessary premises in theorems of List theory
 
bulwahn 
parents: 
46448 
diff
changeset
 | 
2364  | 
"distinct xs \<Longrightarrow> takeWhile (\<lambda>y. y \<noteq> last xs) xs = butlast xs"  | 
| 18423 | 2365  | 
apply(induct xs)  | 
2366  | 
apply simp  | 
|
2367  | 
apply(case_tac xs)  | 
|
2368  | 
apply(auto)  | 
|
2369  | 
done  | 
|
2370  | 
||
| 
44013
 
5cfc1c36ae97
moved recdef package to HOL/Library/Old_Recdef.thy
 
krauss 
parents: 
43594 
diff
changeset
 | 
2371  | 
lemma takeWhile_cong [fundef_cong]:  | 
| 
18336
 
1a2e30b37ed3
Added recdef congruence rules for bounded quantifiers and commonly used
 
krauss 
parents: 
18049 
diff
changeset
 | 
2372  | 
"[| l = k; !!x. x : set l ==> P x = Q x |]  | 
| 
 
1a2e30b37ed3
Added recdef congruence rules for bounded quantifiers and commonly used
 
krauss 
parents: 
18049 
diff
changeset
 | 
2373  | 
==> takeWhile P l = takeWhile Q k"  | 
| 24349 | 2374  | 
by (induct k arbitrary: l) (simp_all)  | 
| 
18336
 
1a2e30b37ed3
Added recdef congruence rules for bounded quantifiers and commonly used
 
krauss 
parents: 
18049 
diff
changeset
 | 
2375  | 
|
| 
44013
 
5cfc1c36ae97
moved recdef package to HOL/Library/Old_Recdef.thy
 
krauss 
parents: 
43594 
diff
changeset
 | 
2376  | 
lemma dropWhile_cong [fundef_cong]:  | 
| 
18336
 
1a2e30b37ed3
Added recdef congruence rules for bounded quantifiers and commonly used
 
krauss 
parents: 
18049 
diff
changeset
 | 
2377  | 
"[| l = k; !!x. x : set l ==> P x = Q x |]  | 
| 
 
1a2e30b37ed3
Added recdef congruence rules for bounded quantifiers and commonly used
 
krauss 
parents: 
18049 
diff
changeset
 | 
2378  | 
==> dropWhile P l = dropWhile Q k"  | 
| 24349 | 2379  | 
by (induct k arbitrary: l, simp_all)  | 
| 
18336
 
1a2e30b37ed3
Added recdef congruence rules for bounded quantifiers and commonly used
 
krauss 
parents: 
18049 
diff
changeset
 | 
2380  | 
|
| 52380 | 2381  | 
lemma takeWhile_idem [simp]:  | 
2382  | 
"takeWhile P (takeWhile P xs) = takeWhile P xs"  | 
|
2383  | 
by (induct xs) auto  | 
|
2384  | 
||
2385  | 
lemma dropWhile_idem [simp]:  | 
|
2386  | 
"dropWhile P (dropWhile P xs) = dropWhile P xs"  | 
|
2387  | 
by (induct xs) auto  | 
|
2388  | 
||
| 13114 | 2389  | 
|
| 
49948
 
744934b818c7
moved quite generic material from theory Enum to more appropriate places
 
haftmann 
parents: 
49808 
diff
changeset
 | 
2390  | 
subsubsection {* @{const zip} *}
 | 
| 13114 | 2391  | 
|
| 13142 | 2392  | 
lemma zip_Nil [simp]: "zip [] ys = []"  | 
| 13145 | 2393  | 
by (induct ys) auto  | 
| 13114 | 2394  | 
|
| 13142 | 2395  | 
lemma zip_Cons_Cons [simp]: "zip (x # xs) (y # ys) = (x, y) # zip xs ys"  | 
| 13145 | 2396  | 
by simp  | 
| 13114 | 2397  | 
|
| 13142 | 2398  | 
declare zip_Cons [simp del]  | 
| 13114 | 2399  | 
|
| 36198 | 2400  | 
lemma [code]:  | 
2401  | 
"zip [] ys = []"  | 
|
2402  | 
"zip xs [] = []"  | 
|
2403  | 
"zip (x # xs) (y # ys) = (x, y) # zip xs ys"  | 
|
2404  | 
by (fact zip_Nil zip.simps(1) zip_Cons_Cons)+  | 
|
2405  | 
||
| 15281 | 2406  | 
lemma zip_Cons1:  | 
2407  | 
"zip (x#xs) ys = (case ys of [] \<Rightarrow> [] | y#ys \<Rightarrow> (x,y)#zip xs ys)"  | 
|
2408  | 
by(auto split:list.split)  | 
|
2409  | 
||
| 13142 | 2410  | 
lemma length_zip [simp]:  | 
| 
22493
 
db930e490fe5
added another rule for simultaneous induction, and lemmas for zip
 
krauss 
parents: 
22422 
diff
changeset
 | 
2411  | 
"length (zip xs ys) = min (length xs) (length ys)"  | 
| 
 
db930e490fe5
added another rule for simultaneous induction, and lemmas for zip
 
krauss 
parents: 
22422 
diff
changeset
 | 
2412  | 
by (induct xs ys rule:list_induct2') auto  | 
| 13114 | 2413  | 
|
| 
34978
 
874150ddd50a
canonical insert operation; generalized lemma foldl_apply_inv to foldl_apply
 
haftmann 
parents: 
34942 
diff
changeset
 | 
2414  | 
lemma zip_obtain_same_length:  | 
| 
 
874150ddd50a
canonical insert operation; generalized lemma foldl_apply_inv to foldl_apply
 
haftmann 
parents: 
34942 
diff
changeset
 | 
2415  | 
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
 | 
2416  | 
\<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
 | 
2417  | 
shows "P (zip xs ys)"  | 
| 
 
874150ddd50a
canonical insert operation; generalized lemma foldl_apply_inv to foldl_apply
 
haftmann 
parents: 
34942 
diff
changeset
 | 
2418  | 
proof -  | 
| 
 
874150ddd50a
canonical insert operation; generalized lemma foldl_apply_inv to foldl_apply
 
haftmann 
parents: 
34942 
diff
changeset
 | 
2419  | 
let ?n = "min (length xs) (length ys)"  | 
| 
 
874150ddd50a
canonical insert operation; generalized lemma foldl_apply_inv to foldl_apply
 
haftmann 
parents: 
34942 
diff
changeset
 | 
2420  | 
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
 | 
2421  | 
by (rule assms) simp_all  | 
| 
 
874150ddd50a
canonical insert operation; generalized lemma foldl_apply_inv to foldl_apply
 
haftmann 
parents: 
34942 
diff
changeset
 | 
2422  | 
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
 | 
2423  | 
proof (induct xs arbitrary: ys)  | 
| 
 
874150ddd50a
canonical insert operation; generalized lemma foldl_apply_inv to foldl_apply
 
haftmann 
parents: 
34942 
diff
changeset
 | 
2424  | 
case Nil then show ?case by simp  | 
| 
 
874150ddd50a
canonical insert operation; generalized lemma foldl_apply_inv to foldl_apply
 
haftmann 
parents: 
34942 
diff
changeset
 | 
2425  | 
next  | 
| 
 
874150ddd50a
canonical insert operation; generalized lemma foldl_apply_inv to foldl_apply
 
haftmann 
parents: 
34942 
diff
changeset
 | 
2426  | 
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
 | 
2427  | 
qed  | 
| 
 
874150ddd50a
canonical insert operation; generalized lemma foldl_apply_inv to foldl_apply
 
haftmann 
parents: 
34942 
diff
changeset
 | 
2428  | 
ultimately show ?thesis by simp  | 
| 
 
874150ddd50a
canonical insert operation; generalized lemma foldl_apply_inv to foldl_apply
 
haftmann 
parents: 
34942 
diff
changeset
 | 
2429  | 
qed  | 
| 
 
874150ddd50a
canonical insert operation; generalized lemma foldl_apply_inv to foldl_apply
 
haftmann 
parents: 
34942 
diff
changeset
 | 
2430  | 
|
| 13114 | 2431  | 
lemma zip_append1:  | 
| 
22493
 
db930e490fe5
added another rule for simultaneous induction, and lemmas for zip
 
krauss 
parents: 
22422 
diff
changeset
 | 
2432  | 
"zip (xs @ ys) zs =  | 
| 13145 | 2433  | 
zip xs (take (length xs) zs) @ zip ys (drop (length xs) zs)"  | 
| 
22493
 
db930e490fe5
added another rule for simultaneous induction, and lemmas for zip
 
krauss 
parents: 
22422 
diff
changeset
 | 
2434  | 
by (induct xs zs rule:list_induct2') auto  | 
| 13114 | 2435  | 
|
2436  | 
lemma zip_append2:  | 
|
| 
22493
 
db930e490fe5
added another rule for simultaneous induction, and lemmas for zip
 
krauss 
parents: 
22422 
diff
changeset
 | 
2437  | 
"zip xs (ys @ zs) =  | 
| 13145 | 2438  | 
zip (take (length ys) xs) ys @ zip (drop (length ys) xs) zs"  | 
| 
22493
 
db930e490fe5
added another rule for simultaneous induction, and lemmas for zip
 
krauss 
parents: 
22422 
diff
changeset
 | 
2439  | 
by (induct xs ys rule:list_induct2') auto  | 
| 13114 | 2440  | 
|
| 13142 | 2441  | 
lemma zip_append [simp]:  | 
| 
46500
 
0196966d6d2d
removing unnecessary premises in theorems of List theory
 
bulwahn 
parents: 
46448 
diff
changeset
 | 
2442  | 
"[| length xs = length us |] ==>  | 
| 13145 | 2443  | 
zip (xs@ys) (us@vs) = zip xs us @ zip ys vs"  | 
2444  | 
by (simp add: zip_append1)  | 
|
| 13114 | 2445  | 
|
2446  | 
lemma zip_rev:  | 
|
| 14247 | 2447  | 
"length xs = length ys ==> zip (rev xs) (rev ys) = rev (zip xs ys)"  | 
2448  | 
by (induct rule:list_induct2, simp_all)  | 
|
| 13114 | 2449  | 
|
| 
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
 | 
2450  | 
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
 | 
2451  | 
"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
 | 
2452  | 
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
 | 
2453  | 
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
 | 
2454  | 
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
 | 
2455  | 
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
 | 
2456  | 
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
 | 
2457  | 
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
 | 
2458  | 
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
 | 
2459  | 
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
 | 
2460  | 
|
| 
 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 
hoelzl 
parents: 
33593 
diff
changeset
 | 
2461  | 
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
 | 
2462  | 
"zip (map f xs) ys = map (\<lambda>(x, y). (f x, 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
 | 
2463  | 
using zip_map_map[of f xs "\<lambda>x. x" ys] 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
 | 
2464  | 
|
| 
 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 
hoelzl 
parents: 
33593 
diff
changeset
 | 
2465  | 
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
 | 
2466  | 
"zip xs (map f ys) = map (\<lambda>(x, y). (x, f 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
 | 
2467  | 
using zip_map_map[of "\<lambda>x. x" xs f ys] 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
 | 
2468  | 
|
| 23096 | 2469  | 
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
 | 
2470  | 
"map f (zip (map g xs) ys) = map (%(x,y). f(g x, 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
 | 
2471  | 
unfolding zip_map1 by auto  | 
| 23096 | 2472  | 
|
2473  | 
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
 | 
2474  | 
"map f (zip xs (map g ys)) = map (%(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
 | 
2475  | 
unfolding zip_map2 by auto  | 
| 23096 | 2476  | 
|
| 31080 | 2477  | 
text{* Courtesy of Andreas Lochbihler: *}
 | 
2478  | 
lemma zip_same_conv_map: "zip xs xs = map (\<lambda>x. (x, x)) xs"  | 
|
2479  | 
by(induct xs) auto  | 
|
2480  | 
||
| 13142 | 2481  | 
lemma nth_zip [simp]:  | 
| 24526 | 2482  | 
"[| i < length xs; i < length ys|] ==> (zip xs ys)!i = (xs!i, ys!i)"  | 
2483  | 
apply (induct ys arbitrary: i xs, simp)  | 
|
| 13145 | 2484  | 
apply (case_tac xs)  | 
2485  | 
apply (simp_all add: nth.simps split: nat.split)  | 
|
2486  | 
done  | 
|
| 13114 | 2487  | 
|
2488  | 
lemma set_zip:  | 
|
| 13145 | 2489  | 
"set (zip xs ys) = {(xs!i, ys!i) | i. i < min (length xs) (length ys)}"
 | 
| 31080 | 2490  | 
by(simp add: set_conv_nth cong: rev_conj_cong)  | 
| 13114 | 2491  | 
|
| 
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  | 
lemma zip_same: "((a,b) \<in> set (zip xs xs)) = (a \<in> set xs \<and> a = 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
 | 
2493  | 
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
 | 
2494  | 
|
| 13114 | 2495  | 
lemma zip_update:  | 
| 31080 | 2496  | 
"zip (xs[i:=x]) (ys[i:=y]) = (zip xs ys)[i:=(x,y)]"  | 
2497  | 
by(rule sym, simp add: update_zip)  | 
|
| 13114 | 2498  | 
|
| 13142 | 2499  | 
lemma zip_replicate [simp]:  | 
| 24526 | 2500  | 
"zip (replicate i x) (replicate j y) = replicate (min i j) (x,y)"  | 
2501  | 
apply (induct i arbitrary: j, auto)  | 
|
| 14208 | 2502  | 
apply (case_tac j, auto)  | 
| 13145 | 2503  | 
done  | 
| 13114 | 2504  | 
|
| 19487 | 2505  | 
lemma take_zip:  | 
| 24526 | 2506  | 
"take n (zip xs ys) = zip (take n xs) (take n ys)"  | 
2507  | 
apply (induct n arbitrary: xs ys)  | 
|
| 19487 | 2508  | 
apply simp  | 
2509  | 
apply (case_tac xs, simp)  | 
|
2510  | 
apply (case_tac ys, simp_all)  | 
|
2511  | 
done  | 
|
2512  | 
||
2513  | 
lemma drop_zip:  | 
|
| 24526 | 2514  | 
"drop n (zip xs ys) = zip (drop n xs) (drop n ys)"  | 
2515  | 
apply (induct n arbitrary: xs ys)  | 
|
| 19487 | 2516  | 
apply simp  | 
2517  | 
apply (case_tac xs, simp)  | 
|
2518  | 
apply (case_tac ys, simp_all)  | 
|
2519  | 
done  | 
|
2520  | 
||
| 
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  | 
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
 | 
2522  | 
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
 | 
2523  | 
case (Cons x xs) thus ?case by (cases ys) 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
 | 
2524  | 
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
 | 
2525  | 
|
| 
 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 
hoelzl 
parents: 
33593 
diff
changeset
 | 
2526  | 
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
 | 
2527  | 
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
 | 
2528  | 
case (Cons x xs) thus ?case by (cases ys) 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
 | 
2529  | 
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
 | 
2530  | 
|
| 
22493
 
db930e490fe5
added another rule for simultaneous induction, and lemmas for zip
 
krauss 
parents: 
22422 
diff
changeset
 | 
2531  | 
lemma set_zip_leftD:  | 
| 
 
db930e490fe5
added another rule for simultaneous induction, and lemmas for zip
 
krauss 
parents: 
22422 
diff
changeset
 | 
2532  | 
"(x,y)\<in> set (zip xs ys) \<Longrightarrow> x \<in> set xs"  | 
| 
 
db930e490fe5
added another rule for simultaneous induction, and lemmas for zip
 
krauss 
parents: 
22422 
diff
changeset
 | 
2533  | 
by (induct xs ys rule:list_induct2') auto  | 
| 
 
db930e490fe5
added another rule for simultaneous induction, and lemmas for zip
 
krauss 
parents: 
22422 
diff
changeset
 | 
2534  | 
|
| 
 
db930e490fe5
added another rule for simultaneous induction, and lemmas for zip
 
krauss 
parents: 
22422 
diff
changeset
 | 
2535  | 
lemma set_zip_rightD:  | 
| 
 
db930e490fe5
added another rule for simultaneous induction, and lemmas for zip
 
krauss 
parents: 
22422 
diff
changeset
 | 
2536  | 
"(x,y)\<in> set (zip xs ys) \<Longrightarrow> y \<in> set ys"  | 
| 
 
db930e490fe5
added another rule for simultaneous induction, and lemmas for zip
 
krauss 
parents: 
22422 
diff
changeset
 | 
2537  | 
by (induct xs ys rule:list_induct2') auto  | 
| 13142 | 2538  | 
|
| 23983 | 2539  | 
lemma in_set_zipE:  | 
2540  | 
"(x,y) : set(zip xs ys) \<Longrightarrow> (\<lbrakk> x : set xs; y : set ys \<rbrakk> \<Longrightarrow> R) \<Longrightarrow> R"  | 
|
2541  | 
by(blast dest: set_zip_leftD set_zip_rightD)  | 
|
2542  | 
||
| 29829 | 2543  | 
lemma zip_map_fst_snd:  | 
2544  | 
"zip (map fst zs) (map snd zs) = zs"  | 
|
2545  | 
by (induct zs) simp_all  | 
|
2546  | 
||
2547  | 
lemma zip_eq_conv:  | 
|
2548  | 
"length xs = length ys \<Longrightarrow> zip xs ys = zs \<longleftrightarrow> map fst zs = xs \<and> map snd zs = ys"  | 
|
2549  | 
by (auto simp add: zip_map_fst_snd)  | 
|
2550  | 
||
| 51173 | 2551  | 
lemma in_set_zip:  | 
2552  | 
"p \<in> set (zip xs ys) \<longleftrightarrow> (\<exists>n. xs ! n = fst p \<and> ys ! n = snd p  | 
|
2553  | 
\<and> n < length xs \<and> n < length ys)"  | 
|
2554  | 
by (cases p) (auto simp add: set_zip)  | 
|
2555  | 
||
2556  | 
lemma pair_list_eqI:  | 
|
2557  | 
assumes "map fst xs = map fst ys" and "map snd xs = map snd ys"  | 
|
2558  | 
shows "xs = ys"  | 
|
2559  | 
proof -  | 
|
2560  | 
from assms(1) have "length xs = length ys" by (rule map_eq_imp_length_eq)  | 
|
2561  | 
from this assms show ?thesis  | 
|
2562  | 
by (induct xs ys rule: list_induct2) (simp_all add: prod_eqI)  | 
|
2563  | 
qed  | 
|
2564  | 
||
| 35115 | 2565  | 
|
| 
49948
 
744934b818c7
moved quite generic material from theory Enum to more appropriate places
 
haftmann 
parents: 
49808 
diff
changeset
 | 
2566  | 
subsubsection {* @{const list_all2} *}
 | 
| 13114 | 2567  | 
|
| 
14316
 
91b897b9a2dc
added some [intro?] and [trans] for list_all2 lemmas
 
kleing 
parents: 
14302 
diff
changeset
 | 
2568  | 
lemma list_all2_lengthD [intro?]:  | 
| 
 
91b897b9a2dc
added some [intro?] and [trans] for list_all2 lemmas
 
kleing 
parents: 
14302 
diff
changeset
 | 
2569  | 
"list_all2 P xs ys ==> length xs = length ys"  | 
| 
55524
 
f41ef840f09d
folded 'list_all2' with the relator generated by 'datatype_new'
 
blanchet 
parents: 
55473 
diff
changeset
 | 
2570  | 
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
 | 
2571  | 
|
| 19787 | 2572  | 
lemma list_all2_Nil [iff, code]: "list_all2 P [] ys = (ys = [])"  | 
| 
55524
 
f41ef840f09d
folded 'list_all2' with the relator generated by 'datatype_new'
 
blanchet 
parents: 
55473 
diff
changeset
 | 
2573  | 
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
 | 
2574  | 
|
| 19787 | 2575  | 
lemma list_all2_Nil2 [iff, code]: "list_all2 P xs [] = (xs = [])"  | 
| 
55524
 
f41ef840f09d
folded 'list_all2' with the relator generated by 'datatype_new'
 
blanchet 
parents: 
55473 
diff
changeset
 | 
2576  | 
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
 | 
2577  | 
|
| 
 
07eeb832f28d
introduced characters for code generator; some improved code lemmas for some list functions
 
haftmann 
parents: 
19585 
diff
changeset
 | 
2578  | 
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
 | 
2579  | 
"list_all2 P (x # xs) (y # ys) = (P x y \<and> list_all2 P xs ys)"  | 
| 
55524
 
f41ef840f09d
folded 'list_all2' with the relator generated by 'datatype_new'
 
blanchet 
parents: 
55473 
diff
changeset
 | 
2580  | 
by (auto simp add: list_all2_iff)  | 
| 13114 | 2581  | 
|
2582  | 
lemma list_all2_Cons1:  | 
|
| 13145 | 2583  | 
"list_all2 P (x # xs) ys = (\<exists>z zs. ys = z # zs \<and> P x z \<and> list_all2 P xs zs)"  | 
2584  | 
by (cases ys) auto  | 
|
| 13114 | 2585  | 
|
2586  | 
lemma list_all2_Cons2:  | 
|
| 13145 | 2587  | 
"list_all2 P xs (y # ys) = (\<exists>z zs. xs = z # zs \<and> P z y \<and> list_all2 P zs ys)"  | 
2588  | 
by (cases xs) auto  | 
|
| 13114 | 2589  | 
|
| 45794 | 2590  | 
lemma list_all2_induct  | 
2591  | 
[consumes 1, case_names Nil Cons, induct set: list_all2]:  | 
|
2592  | 
assumes P: "list_all2 P xs ys"  | 
|
2593  | 
assumes Nil: "R [] []"  | 
|
| 47640 | 2594  | 
assumes Cons: "\<And>x xs y ys.  | 
2595  | 
\<lbrakk>P x y; list_all2 P xs ys; R xs ys\<rbrakk> \<Longrightarrow> R (x # xs) (y # ys)"  | 
|
| 45794 | 2596  | 
shows "R xs ys"  | 
2597  | 
using P  | 
|
2598  | 
by (induct xs arbitrary: ys) (auto simp add: list_all2_Cons1 Nil Cons)  | 
|
2599  | 
||
| 13142 | 2600  | 
lemma list_all2_rev [iff]:  | 
| 13145 | 2601  | 
"list_all2 P (rev xs) (rev ys) = list_all2 P xs ys"  | 
| 
55524
 
f41ef840f09d
folded 'list_all2' with the relator generated by 'datatype_new'
 
blanchet 
parents: 
55473 
diff
changeset
 | 
2602  | 
by (simp add: list_all2_iff zip_rev cong: conj_cong)  | 
| 13114 | 2603  | 
|
| 13863 | 2604  | 
lemma list_all2_rev1:  | 
2605  | 
"list_all2 P (rev xs) ys = list_all2 P xs (rev ys)"  | 
|
2606  | 
by (subst list_all2_rev [symmetric]) simp  | 
|
2607  | 
||
| 13114 | 2608  | 
lemma list_all2_append1:  | 
| 13145 | 2609  | 
"list_all2 P (xs @ ys) zs =  | 
2610  | 
(EX us vs. zs = us @ vs \<and> length us = length xs \<and> length vs = length ys \<and>  | 
|
2611  | 
list_all2 P xs us \<and> list_all2 P ys vs)"  | 
|
| 
55524
 
f41ef840f09d
folded 'list_all2' with the relator generated by 'datatype_new'
 
blanchet 
parents: 
55473 
diff
changeset
 | 
2612  | 
apply (simp add: list_all2_iff zip_append1)  | 
| 13145 | 2613  | 
apply (rule iffI)  | 
2614  | 
apply (rule_tac x = "take (length xs) zs" in exI)  | 
|
2615  | 
apply (rule_tac x = "drop (length xs) zs" in exI)  | 
|
| 14208 | 2616  | 
apply (force split: nat_diff_split simp add: min_def, clarify)  | 
| 13145 | 2617  | 
apply (simp add: ball_Un)  | 
2618  | 
done  | 
|
| 13114 | 2619  | 
|
2620  | 
lemma list_all2_append2:  | 
|
| 13145 | 2621  | 
"list_all2 P xs (ys @ zs) =  | 
2622  | 
(EX us vs. xs = us @ vs \<and> length us = length ys \<and> length vs = length zs \<and>  | 
|
2623  | 
list_all2 P us ys \<and> list_all2 P vs zs)"  | 
|
| 
55524
 
f41ef840f09d
folded 'list_all2' with the relator generated by 'datatype_new'
 
blanchet 
parents: 
55473 
diff
changeset
 | 
2624  | 
apply (simp add: list_all2_iff zip_append2)  | 
| 13145 | 2625  | 
apply (rule iffI)  | 
2626  | 
apply (rule_tac x = "take (length ys) xs" in exI)  | 
|
2627  | 
apply (rule_tac x = "drop (length ys) xs" in exI)  | 
|
| 14208 | 2628  | 
apply (force split: nat_diff_split simp add: min_def, clarify)  | 
| 13145 | 2629  | 
apply (simp add: ball_Un)  | 
2630  | 
done  | 
|
| 13114 | 2631  | 
|
| 13863 | 2632  | 
lemma list_all2_append:  | 
| 14247 | 2633  | 
"length xs = length ys \<Longrightarrow>  | 
2634  | 
list_all2 P (xs@us) (ys@vs) = (list_all2 P xs ys \<and> list_all2 P us vs)"  | 
|
2635  | 
by (induct rule:list_induct2, simp_all)  | 
|
| 13863 | 2636  | 
|
2637  | 
lemma list_all2_appendI [intro?, trans]:  | 
|
2638  | 
"\<lbrakk> list_all2 P a b; list_all2 P c d \<rbrakk> \<Longrightarrow> list_all2 P (a@c) (b@d)"  | 
|
| 24349 | 2639  | 
by (simp add: list_all2_append list_all2_lengthD)  | 
| 13863 | 2640  | 
|
| 13114 | 2641  | 
lemma list_all2_conv_all_nth:  | 
| 13145 | 2642  | 
"list_all2 P xs ys =  | 
2643  | 
(length xs = length ys \<and> (\<forall>i < length xs. P (xs!i) (ys!i)))"  | 
|
| 
55524
 
f41ef840f09d
folded 'list_all2' with the relator generated by 'datatype_new'
 
blanchet 
parents: 
55473 
diff
changeset
 | 
2644  | 
by (force simp add: list_all2_iff set_zip)  | 
| 13114 | 2645  | 
|
| 
13883
 
0451e0fb3f22
Re-structured some proofs in order to get rid of rule_format attribute.
 
berghofe 
parents: 
13863 
diff
changeset
 | 
2646  | 
lemma list_all2_trans:  | 
| 
 
0451e0fb3f22
Re-structured some proofs in order to get rid of rule_format attribute.
 
berghofe 
parents: 
13863 
diff
changeset
 | 
2647  | 
assumes tr: "!!a b c. P1 a b ==> P2 b c ==> P3 a c"  | 
| 
 
0451e0fb3f22
Re-structured some proofs in order to get rid of rule_format attribute.
 
berghofe 
parents: 
13863 
diff
changeset
 | 
2648  | 
shows "!!bs cs. list_all2 P1 as bs ==> list_all2 P2 bs cs ==> list_all2 P3 as cs"  | 
| 
 
0451e0fb3f22
Re-structured some proofs in order to get rid of rule_format attribute.
 
berghofe 
parents: 
13863 
diff
changeset
 | 
2649  | 
(is "!!bs cs. PROP ?Q as bs cs")  | 
| 
 
0451e0fb3f22
Re-structured some proofs in order to get rid of rule_format attribute.
 
berghofe 
parents: 
13863 
diff
changeset
 | 
2650  | 
proof (induct as)  | 
| 
 
0451e0fb3f22
Re-structured some proofs in order to get rid of rule_format attribute.
 
berghofe 
parents: 
13863 
diff
changeset
 | 
2651  | 
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
 | 
2652  | 
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
 | 
2653  | 
proof (induct bs)  | 
| 
 
0451e0fb3f22
Re-structured some proofs in order to get rid of rule_format attribute.
 
berghofe 
parents: 
13863 
diff
changeset
 | 
2654  | 
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
 | 
2655  | 
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
 | 
2656  | 
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
 | 
2657  | 
qed simp  | 
| 
 
0451e0fb3f22
Re-structured some proofs in order to get rid of rule_format attribute.
 
berghofe 
parents: 
13863 
diff
changeset
 | 
2658  | 
qed simp  | 
| 
 
0451e0fb3f22
Re-structured some proofs in order to get rid of rule_format attribute.
 
berghofe 
parents: 
13863 
diff
changeset
 | 
2659  | 
|
| 13863 | 2660  | 
lemma list_all2_all_nthI [intro?]:  | 
2661  | 
"length a = length b \<Longrightarrow> (\<And>n. n < length a \<Longrightarrow> P (a!n) (b!n)) \<Longrightarrow> list_all2 P a b"  | 
|
| 24349 | 2662  | 
by (simp add: list_all2_conv_all_nth)  | 
| 13863 | 2663  | 
|
| 14395 | 2664  | 
lemma list_all2I:  | 
2665  | 
"\<forall>x \<in> set (zip a b). split P x \<Longrightarrow> length a = length b \<Longrightarrow> list_all2 P a b"  | 
|
| 
55524
 
f41ef840f09d
folded 'list_all2' with the relator generated by 'datatype_new'
 
blanchet 
parents: 
55473 
diff
changeset
 | 
2666  | 
by (simp add: list_all2_iff)  | 
| 14395 | 2667  | 
|
| 14328 | 2668  | 
lemma list_all2_nthD:  | 
| 13863 | 2669  | 
"\<lbrakk> list_all2 P xs ys; p < size xs \<rbrakk> \<Longrightarrow> P (xs!p) (ys!p)"  | 
| 24349 | 2670  | 
by (simp add: list_all2_conv_all_nth)  | 
| 13863 | 2671  | 
|
| 14302 | 2672  | 
lemma list_all2_nthD2:  | 
2673  | 
"\<lbrakk>list_all2 P xs ys; p < size ys\<rbrakk> \<Longrightarrow> P (xs!p) (ys!p)"  | 
|
| 24349 | 2674  | 
by (frule list_all2_lengthD) (auto intro: list_all2_nthD)  | 
| 14302 | 2675  | 
|
| 13863 | 2676  | 
lemma list_all2_map1:  | 
2677  | 
"list_all2 P (map f as) bs = list_all2 (\<lambda>x y. P (f x) y) as bs"  | 
|
| 24349 | 2678  | 
by (simp add: list_all2_conv_all_nth)  | 
| 13863 | 2679  | 
|
2680  | 
lemma list_all2_map2:  | 
|
2681  | 
"list_all2 P as (map f bs) = list_all2 (\<lambda>x y. P x (f y)) as bs"  | 
|
| 24349 | 2682  | 
by (auto simp add: list_all2_conv_all_nth)  | 
| 13863 | 2683  | 
|
| 
14316
 
91b897b9a2dc
added some [intro?] and [trans] for list_all2 lemmas
 
kleing 
parents: 
14302 
diff
changeset
 | 
2684  | 
lemma list_all2_refl [intro?]:  | 
| 13863 | 2685  | 
"(\<And>x. P x x) \<Longrightarrow> list_all2 P xs xs"  | 
| 24349 | 2686  | 
by (simp add: list_all2_conv_all_nth)  | 
| 13863 | 2687  | 
|
2688  | 
lemma list_all2_update_cong:  | 
|
| 
46669
 
c1d2ab32174a
one general list_all2_update_cong instead of two special ones
 
bulwahn 
parents: 
46664 
diff
changeset
 | 
2689  | 
"\<lbrakk> list_all2 P xs ys; P x y \<rbrakk> \<Longrightarrow> list_all2 P (xs[i:=x]) (ys[i:=y])"  | 
| 
 
c1d2ab32174a
one general list_all2_update_cong instead of two special ones
 
bulwahn 
parents: 
46664 
diff
changeset
 | 
2690  | 
by (cases "i < length ys") (auto simp add: list_all2_conv_all_nth nth_list_update)  | 
| 13863 | 2691  | 
|
| 14302 | 2692  | 
lemma list_all2_takeI [simp,intro?]:  | 
| 24526 | 2693  | 
"list_all2 P xs ys \<Longrightarrow> list_all2 P (take n xs) (take n ys)"  | 
2694  | 
apply (induct xs arbitrary: n ys)  | 
|
2695  | 
apply simp  | 
|
2696  | 
apply (clarsimp simp add: list_all2_Cons1)  | 
|
2697  | 
apply (case_tac n)  | 
|
2698  | 
apply auto  | 
|
2699  | 
done  | 
|
| 14302 | 2700  | 
|
2701  | 
lemma list_all2_dropI [simp,intro?]:  | 
|
| 24526 | 2702  | 
"list_all2 P as bs \<Longrightarrow> list_all2 P (drop n as) (drop n bs)"  | 
2703  | 
apply (induct as arbitrary: n bs, simp)  | 
|
2704  | 
apply (clarsimp simp add: list_all2_Cons1)  | 
|
2705  | 
apply (case_tac n, simp, simp)  | 
|
2706  | 
done  | 
|
| 13863 | 2707  | 
|
| 14327 | 2708  | 
lemma list_all2_mono [intro?]:  | 
| 24526 | 2709  | 
"list_all2 P xs ys \<Longrightarrow> (\<And>xs ys. P xs ys \<Longrightarrow> Q xs ys) \<Longrightarrow> list_all2 Q xs ys"  | 
2710  | 
apply (induct xs arbitrary: ys, simp)  | 
|
2711  | 
apply (case_tac ys, auto)  | 
|
2712  | 
done  | 
|
| 13863 | 2713  | 
|
| 22551 | 2714  | 
lemma list_all2_eq:  | 
2715  | 
"xs = ys \<longleftrightarrow> list_all2 (op =) xs ys"  | 
|
| 24349 | 2716  | 
by (induct xs ys rule: list_induct2') auto  | 
| 22551 | 2717  | 
|
| 40230 | 2718  | 
lemma list_eq_iff_zip_eq:  | 
2719  | 
"xs = ys \<longleftrightarrow> length xs = length ys \<and> (\<forall>(x,y) \<in> set (zip xs ys). x = y)"  | 
|
2720  | 
by(auto simp add: set_zip list_all2_eq list_all2_conv_all_nth cong: conj_cong)  | 
|
2721  | 
||
| 57308 | 2722  | 
lemma list_all2_same: "list_all2 P xs xs \<longleftrightarrow> (\<forall>x\<in>set xs. P x x)"  | 
2723  | 
by(auto simp add: list_all2_conv_all_nth set_conv_nth)  | 
|
| 13142 | 2724  | 
|
| 
53721
 
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
 
traytel 
parents: 
53689 
diff
changeset
 | 
2725  | 
subsubsection {* @{const List.product} and @{const product_lists} *}
 | 
| 
49948
 
744934b818c7
moved quite generic material from theory Enum to more appropriate places
 
haftmann 
parents: 
49808 
diff
changeset
 | 
2726  | 
|
| 57247 | 2727  | 
lemma set_product[simp]:  | 
| 
49948
 
744934b818c7
moved quite generic material from theory Enum to more appropriate places
 
haftmann 
parents: 
49808 
diff
changeset
 | 
2728  | 
"set (List.product xs ys) = set xs \<times> set ys"  | 
| 
 
744934b818c7
moved quite generic material from theory Enum to more appropriate places
 
haftmann 
parents: 
49808 
diff
changeset
 | 
2729  | 
by (induct xs) auto  | 
| 
 
744934b818c7
moved quite generic material from theory Enum to more appropriate places
 
haftmann 
parents: 
49808 
diff
changeset
 | 
2730  | 
|
| 
51160
 
599ff65b85e2
systematic conversions between nat and nibble/char;
 
haftmann 
parents: 
51112 
diff
changeset
 | 
2731  | 
lemma length_product [simp]:  | 
| 
 
599ff65b85e2
systematic conversions between nat and nibble/char;
 
haftmann 
parents: 
51112 
diff
changeset
 | 
2732  | 
"length (List.product xs ys) = length xs * length ys"  | 
| 
 
599ff65b85e2
systematic conversions between nat and nibble/char;
 
haftmann 
parents: 
51112 
diff
changeset
 | 
2733  | 
by (induct xs) simp_all  | 
| 
 
599ff65b85e2
systematic conversions between nat and nibble/char;
 
haftmann 
parents: 
51112 
diff
changeset
 | 
2734  | 
|
| 
 
599ff65b85e2
systematic conversions between nat and nibble/char;
 
haftmann 
parents: 
51112 
diff
changeset
 | 
2735  | 
lemma product_nth:  | 
| 
 
599ff65b85e2
systematic conversions between nat and nibble/char;
 
haftmann 
parents: 
51112 
diff
changeset
 | 
2736  | 
assumes "n < length xs * length ys"  | 
| 
 
599ff65b85e2
systematic conversions between nat and nibble/char;
 
haftmann 
parents: 
51112 
diff
changeset
 | 
2737  | 
shows "List.product xs ys ! n = (xs ! (n div length ys), ys ! (n mod length ys))"  | 
| 
 
599ff65b85e2
systematic conversions between nat and nibble/char;
 
haftmann 
parents: 
51112 
diff
changeset
 | 
2738  | 
using assms proof (induct xs arbitrary: n)  | 
| 
 
599ff65b85e2
systematic conversions between nat and nibble/char;
 
haftmann 
parents: 
51112 
diff
changeset
 | 
2739  | 
case Nil then show ?case by simp  | 
| 
 
599ff65b85e2
systematic conversions between nat and nibble/char;
 
haftmann 
parents: 
51112 
diff
changeset
 | 
2740  | 
next  | 
| 
 
599ff65b85e2
systematic conversions between nat and nibble/char;
 
haftmann 
parents: 
51112 
diff
changeset
 | 
2741  | 
case (Cons x xs n)  | 
| 
 
599ff65b85e2
systematic conversions between nat and nibble/char;
 
haftmann 
parents: 
51112 
diff
changeset
 | 
2742  | 
then have "length ys > 0" by auto  | 
| 
 
599ff65b85e2
systematic conversions between nat and nibble/char;
 
haftmann 
parents: 
51112 
diff
changeset
 | 
2743  | 
with Cons show ?case  | 
| 
 
599ff65b85e2
systematic conversions between nat and nibble/char;
 
haftmann 
parents: 
51112 
diff
changeset
 | 
2744  | 
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
 | 
2745  | 
qed  | 
| 
 
599ff65b85e2
systematic conversions between nat and nibble/char;
 
haftmann 
parents: 
51112 
diff
changeset
 | 
2746  | 
|
| 
53721
 
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
 
traytel 
parents: 
53689 
diff
changeset
 | 
2747  | 
lemma in_set_product_lists_length:  | 
| 
 
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
 
traytel 
parents: 
53689 
diff
changeset
 | 
2748  | 
"xs \<in> set (product_lists xss) \<Longrightarrow> length xs = length xss"  | 
| 
 
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
 
traytel 
parents: 
53689 
diff
changeset
 | 
2749  | 
by (induct xss arbitrary: xs) auto  | 
| 
 
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
 
traytel 
parents: 
53689 
diff
changeset
 | 
2750  | 
|
| 
 
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
 
traytel 
parents: 
53689 
diff
changeset
 | 
2751  | 
lemma product_lists_set:  | 
| 
 
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
 
traytel 
parents: 
53689 
diff
changeset
 | 
2752  | 
  "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
 | 
2753  | 
proof (intro equalityI subsetI, unfold mem_Collect_eq)  | 
| 
 
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
 
traytel 
parents: 
53689 
diff
changeset
 | 
2754  | 
fix xs assume "xs \<in> ?L"  | 
| 
 
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
 
traytel 
parents: 
53689 
diff
changeset
 | 
2755  | 
then have "length xs = length xss" by (rule in_set_product_lists_length)  | 
| 
 
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
 
traytel 
parents: 
53689 
diff
changeset
 | 
2756  | 
from this `xs \<in> ?L` show "?R xs" by (induct xs xss rule: list_induct2) auto  | 
| 
 
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
 
traytel 
parents: 
53689 
diff
changeset
 | 
2757  | 
next  | 
| 
 
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
 
traytel 
parents: 
53689 
diff
changeset
 | 
2758  | 
fix xs assume "?R xs"  | 
| 
 
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
 
traytel 
parents: 
53689 
diff
changeset
 | 
2759  | 
then show "xs \<in> ?L" by induct auto  | 
| 
 
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
 
traytel 
parents: 
53689 
diff
changeset
 | 
2760  | 
qed  | 
| 
 
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
 
traytel 
parents: 
53689 
diff
changeset
 | 
2761  | 
|
| 
49948
 
744934b818c7
moved quite generic material from theory Enum to more appropriate places
 
haftmann 
parents: 
49808 
diff
changeset
 | 
2762  | 
|
| 
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
 | 
2763  | 
subsubsection {* @{const fold} with natural argument order *}
 | 
| 
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
 | 
2764  | 
|
| 
48828
 
441a4eed7823
prefer eta-expanded code equations for fold, to accomodate tail recursion optimisation in Scala
 
haftmann 
parents: 
48619 
diff
changeset
 | 
2765  | 
lemma fold_simps [code]: -- {* eta-expanded variant for generated code -- enables tail-recursion optimisation in Scala *}
 | 
| 
 
441a4eed7823
prefer eta-expanded code equations for fold, to accomodate tail recursion optimisation in Scala
 
haftmann 
parents: 
48619 
diff
changeset
 | 
2766  | 
"fold f [] s = s"  | 
| 
 
441a4eed7823
prefer eta-expanded code equations for fold, to accomodate tail recursion optimisation in Scala
 
haftmann 
parents: 
48619 
diff
changeset
 | 
2767  | 
"fold f (x # xs) s = fold f xs (f x s)"  | 
| 
 
441a4eed7823
prefer eta-expanded code equations for fold, to accomodate tail recursion optimisation in Scala
 
haftmann 
parents: 
48619 
diff
changeset
 | 
2768  | 
by simp_all  | 
| 
 
441a4eed7823
prefer eta-expanded code equations for fold, to accomodate tail recursion optimisation in Scala
 
haftmann 
parents: 
48619 
diff
changeset
 | 
2769  | 
|
| 
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
 | 
2770  | 
lemma fold_remove1_split:  | 
| 
 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 
haftmann 
parents: 
46125 
diff
changeset
 | 
2771  | 
assumes f: "\<And>x y. x \<in> set xs \<Longrightarrow> y \<in> set xs \<Longrightarrow> f x \<circ> f y = f y \<circ> f 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
 | 
2772  | 
and x: "x \<in> set xs"  | 
| 
 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 
haftmann 
parents: 
46125 
diff
changeset
 | 
2773  | 
shows "fold f xs = fold f (remove1 x xs) \<circ> f x"  | 
| 49739 | 2774  | 
using assms 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
 | 
2775  | 
|
| 
 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 
haftmann 
parents: 
46125 
diff
changeset
 | 
2776  | 
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
 | 
2777  | 
"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
 | 
2778  | 
\<Longrightarrow> fold f xs a = fold g ys b"  | 
| 
 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 
haftmann 
parents: 
46125 
diff
changeset
 | 
2779  | 
by (induct ys arbitrary: a b xs) simp_all  | 
| 
 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 
haftmann 
parents: 
46125 
diff
changeset
 | 
2780  | 
|
| 
 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 
haftmann 
parents: 
46125 
diff
changeset
 | 
2781  | 
lemma fold_id:  | 
| 
 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 
haftmann 
parents: 
46125 
diff
changeset
 | 
2782  | 
assumes "\<And>x. x \<in> set xs \<Longrightarrow> f x = id"  | 
| 
 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 
haftmann 
parents: 
46125 
diff
changeset
 | 
2783  | 
shows "fold f xs = id"  | 
| 
 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 
haftmann 
parents: 
46125 
diff
changeset
 | 
2784  | 
using assms by (induct xs) simp_all  | 
| 
 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 
haftmann 
parents: 
46125 
diff
changeset
 | 
2785  | 
|
| 
 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 
haftmann 
parents: 
46125 
diff
changeset
 | 
2786  | 
lemma 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
 | 
2787  | 
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
 | 
2788  | 
shows "h \<circ> fold g xs = fold f xs \<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
 | 
2789  | 
using assms by (induct xs) (simp_all add: fun_eq_iff)  | 
| 
 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 
haftmann 
parents: 
46125 
diff
changeset
 | 
2790  | 
|
| 
 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 
haftmann 
parents: 
46125 
diff
changeset
 | 
2791  | 
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
 | 
2792  | 
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
 | 
2793  | 
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
 | 
2794  | 
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
 | 
2795  | 
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
 | 
2796  | 
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
 | 
2797  | 
qed  | 
| 
 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 
haftmann 
parents: 
37465 
diff
changeset
 | 
2798  | 
|
| 
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
 | 
2799  | 
lemma fold_invariant:  | 
| 
 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 
haftmann 
parents: 
46125 
diff
changeset
 | 
2800  | 
assumes "\<And>x. x \<in> set xs \<Longrightarrow> Q x" and "P 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
 | 
2801  | 
and "\<And>x s. Q x \<Longrightarrow> P s \<Longrightarrow> P (f x 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
 | 
2802  | 
shows "P (fold f xs s)"  | 
| 
34978
 
874150ddd50a
canonical insert operation; generalized lemma foldl_apply_inv to foldl_apply
 
haftmann 
parents: 
34942 
diff
changeset
 | 
2803  | 
using assms by (induct xs arbitrary: s) simp_all  | 
| 
 
874150ddd50a
canonical insert operation; generalized lemma foldl_apply_inv to foldl_apply
 
haftmann 
parents: 
34942 
diff
changeset
 | 
2804  | 
|
| 
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
 | 
2805  | 
lemma fold_append [simp]:  | 
| 
 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 
haftmann 
parents: 
46125 
diff
changeset
 | 
2806  | 
"fold f (xs @ ys) = fold f ys \<circ> fold f xs"  | 
| 
 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 
haftmann 
parents: 
46125 
diff
changeset
 | 
2807  | 
by (induct xs) simp_all  | 
| 
 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 
haftmann 
parents: 
46125 
diff
changeset
 | 
2808  | 
|
| 
 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 
haftmann 
parents: 
46125 
diff
changeset
 | 
2809  | 
lemma fold_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
 | 
2810  | 
"fold g (map f xs) = fold (g o f) xs"  | 
| 
 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 
haftmann 
parents: 
46125 
diff
changeset
 | 
2811  | 
by (induct xs) simp_all  | 
| 
 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 
haftmann 
parents: 
46125 
diff
changeset
 | 
2812  | 
|
| 
 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 
haftmann 
parents: 
46125 
diff
changeset
 | 
2813  | 
lemma fold_rev:  | 
| 
 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 
haftmann 
parents: 
46125 
diff
changeset
 | 
2814  | 
assumes "\<And>x y. x \<in> set xs \<Longrightarrow> y \<in> set xs \<Longrightarrow> f y \<circ> f x = f x \<circ> f y"  | 
| 
 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 
haftmann 
parents: 
46125 
diff
changeset
 | 
2815  | 
shows "fold f (rev xs) = fold f xs"  | 
| 
 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 
haftmann 
parents: 
46125 
diff
changeset
 | 
2816  | 
using assms by (induct xs) (simp_all add: fold_commute_apply fun_eq_iff)  | 
| 
 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 
haftmann 
parents: 
46125 
diff
changeset
 | 
2817  | 
|
| 
 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 
haftmann 
parents: 
46125 
diff
changeset
 | 
2818  | 
lemma fold_Cons_rev:  | 
| 
 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 
haftmann 
parents: 
46125 
diff
changeset
 | 
2819  | 
"fold Cons xs = append (rev xs)"  | 
| 
 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 
haftmann 
parents: 
46125 
diff
changeset
 | 
2820  | 
by (induct xs) simp_all  | 
| 
 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 
haftmann 
parents: 
46125 
diff
changeset
 | 
2821  | 
|
| 
 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 
haftmann 
parents: 
46125 
diff
changeset
 | 
2822  | 
lemma rev_conv_fold [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
 | 
2823  | 
"rev xs = fold Cons xs []"  | 
| 
 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 
haftmann 
parents: 
46125 
diff
changeset
 | 
2824  | 
by (simp add: fold_Cons_rev)  | 
| 
 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 
haftmann 
parents: 
46125 
diff
changeset
 | 
2825  | 
|
| 
 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 
haftmann 
parents: 
46125 
diff
changeset
 | 
2826  | 
lemma fold_append_concat_rev:  | 
| 
 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 
haftmann 
parents: 
46125 
diff
changeset
 | 
2827  | 
"fold append xss = append (concat (rev xss))"  | 
| 
 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 
haftmann 
parents: 
46125 
diff
changeset
 | 
2828  | 
by (induct xss) simp_all  | 
| 
 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 
haftmann 
parents: 
46125 
diff
changeset
 | 
2829  | 
|
| 
 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 
haftmann 
parents: 
46125 
diff
changeset
 | 
2830  | 
text {* @{const Finite_Set.fold} and @{const 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
 | 
2831  | 
|
| 
 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 
haftmann 
parents: 
46125 
diff
changeset
 | 
2832  | 
lemma (in comp_fun_commute) fold_set_fold_remdups:  | 
| 
 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 
haftmann 
parents: 
46125 
diff
changeset
 | 
2833  | 
"Finite_Set.fold f y (set xs) = fold f (remdups xs) y"  | 
| 51489 | 2834  | 
by (rule sym, induct xs arbitrary: y) (simp_all add: fold_fun_left_comm insert_absorb)  | 
| 48619 | 2835  | 
|
| 
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
 | 
2836  | 
lemma (in comp_fun_idem) fold_set_fold:  | 
| 
 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 
haftmann 
parents: 
46125 
diff
changeset
 | 
2837  | 
"Finite_Set.fold f y (set xs) = fold f xs y"  | 
| 51489 | 2838  | 
by (rule sym, induct xs arbitrary: y) (simp_all add: fold_fun_left_comm)  | 
| 
32681
 
adeac3cbb659
lemma relating fold1 and foldl; code_unfold rules for Inf_fin, Sup_fin, Min, Max, Inf, Sup
 
haftmann 
parents: 
32422 
diff
changeset
 | 
2839  | 
|
| 
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
 | 
2840  | 
lemma union_set_fold [code]:  | 
| 
46147
 
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
 
haftmann 
parents: 
46143 
diff
changeset
 | 
2841  | 
"set xs \<union> A = fold Set.insert xs A"  | 
| 
 
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
 
haftmann 
parents: 
46143 
diff
changeset
 | 
2842  | 
proof -  | 
| 
 
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
 
haftmann 
parents: 
46143 
diff
changeset
 | 
2843  | 
interpret comp_fun_idem Set.insert  | 
| 
 
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
 
haftmann 
parents: 
46143 
diff
changeset
 | 
2844  | 
by (fact comp_fun_idem_insert)  | 
| 
 
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
 
haftmann 
parents: 
46143 
diff
changeset
 | 
2845  | 
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
 | 
2846  | 
qed  | 
| 
 
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
 
haftmann 
parents: 
46143 
diff
changeset
 | 
2847  | 
|
| 
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
 | 
2848  | 
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
 | 
2849  | 
"List.coset xs \<union> A = List.coset (List.filter (\<lambda>x. x \<notin> A) xs)"  | 
| 
 
d654c73e4b12
no preference wrt. fold(l/r); prefer fold rather than foldr for iterating over lists in generated code
 
haftmann 
parents: 
47131 
diff
changeset
 | 
2850  | 
by auto  | 
| 
 
d654c73e4b12
no preference wrt. fold(l/r); prefer fold rather than foldr for iterating over lists in generated code
 
haftmann 
parents: 
47131 
diff
changeset
 | 
2851  | 
|
| 
 
d654c73e4b12
no preference wrt. fold(l/r); prefer fold rather than foldr for iterating over lists in generated code
 
haftmann 
parents: 
47131 
diff
changeset
 | 
2852  | 
lemma minus_set_fold [code]:  | 
| 
46147
 
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
 
haftmann 
parents: 
46143 
diff
changeset
 | 
2853  | 
"A - set xs = fold Set.remove xs A"  | 
| 
 
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
 
haftmann 
parents: 
46143 
diff
changeset
 | 
2854  | 
proof -  | 
| 
 
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
 
haftmann 
parents: 
46143 
diff
changeset
 | 
2855  | 
interpret comp_fun_idem Set.remove  | 
| 
 
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
 
haftmann 
parents: 
46143 
diff
changeset
 | 
2856  | 
by (fact comp_fun_idem_remove)  | 
| 
 
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
 
haftmann 
parents: 
46143 
diff
changeset
 | 
2857  | 
show ?thesis  | 
| 
 
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
 
haftmann 
parents: 
46143 
diff
changeset
 | 
2858  | 
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
 | 
2859  | 
qed  | 
| 
 
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
 
haftmann 
parents: 
46143 
diff
changeset
 | 
2860  | 
|
| 
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
 | 
2861  | 
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
 | 
2862  | 
"A - List.coset xs = set (List.filter (\<lambda>x. x \<in> A) xs)"  | 
| 
 
d654c73e4b12
no preference wrt. fold(l/r); prefer fold rather than foldr for iterating over lists in generated code
 
haftmann 
parents: 
47131 
diff
changeset
 | 
2863  | 
by auto  | 
| 
 
d654c73e4b12
no preference wrt. fold(l/r); prefer fold rather than foldr for iterating over lists in generated code
 
haftmann 
parents: 
47131 
diff
changeset
 | 
2864  | 
|
| 
 
d654c73e4b12
no preference wrt. fold(l/r); prefer fold rather than foldr for iterating over lists in generated code
 
haftmann 
parents: 
47131 
diff
changeset
 | 
2865  | 
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
 | 
2866  | 
"A \<inter> set xs = set (List.filter (\<lambda>x. x \<in> A) xs)"  | 
| 
 
d654c73e4b12
no preference wrt. fold(l/r); prefer fold rather than foldr for iterating over lists in generated code
 
haftmann 
parents: 
47131 
diff
changeset
 | 
2867  | 
by auto  | 
| 
 
d654c73e4b12
no preference wrt. fold(l/r); prefer fold rather than foldr for iterating over lists in generated code
 
haftmann 
parents: 
47131 
diff
changeset
 | 
2868  | 
|
| 
 
d654c73e4b12
no preference wrt. fold(l/r); prefer fold rather than foldr for iterating over lists in generated code
 
haftmann 
parents: 
47131 
diff
changeset
 | 
2869  | 
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
 | 
2870  | 
"A \<inter> List.coset xs = fold Set.remove xs A"  | 
| 
 
d654c73e4b12
no preference wrt. fold(l/r); prefer fold rather than foldr for iterating over lists in generated code
 
haftmann 
parents: 
47131 
diff
changeset
 | 
2871  | 
by (simp add: Diff_eq [symmetric] minus_set_fold)  | 
| 
 
d654c73e4b12
no preference wrt. fold(l/r); prefer fold rather than foldr for iterating over lists in generated code
 
haftmann 
parents: 
47131 
diff
changeset
 | 
2872  | 
|
| 54885 | 2873  | 
lemma (in semilattice_set) set_eq_fold [code]:  | 
| 51489 | 2874  | 
"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
 | 
2875  | 
proof -  | 
| 51489 | 2876  | 
interpret comp_fun_idem f  | 
2877  | 
by default (simp_all add: fun_eq_iff left_commute)  | 
|
2878  | 
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
 | 
2879  | 
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
 | 
2880  | 
|
| 
 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 
haftmann 
parents: 
46125 
diff
changeset
 | 
2881  | 
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
 | 
2882  | 
"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
 | 
2883  | 
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
 | 
2884  | 
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
 | 
2885  | 
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
 | 
2886  | 
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
 | 
2887  | 
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
 | 
2888  | 
|
| 
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
 | 
2889  | 
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
 | 
2890  | 
|
| 
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
 | 
2891  | 
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
 | 
2892  | 
"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
 | 
2893  | 
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
 | 
2894  | 
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
 | 
2895  | 
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
 | 
2896  | 
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
 | 
2897  | 
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
 | 
2898  | 
|
| 
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
 | 
2899  | 
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
 | 
2900  | 
|
| 
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
 | 
2901  | 
lemma (in complete_lattice) INF_set_fold:  | 
| 
56218
 
1c3f1f2431f9
elongated INFI and SUPR, to reduced risk of confusing theorems names in the future while still being consistent with INTER and UNION
 
haftmann 
parents: 
56166 
diff
changeset
 | 
2902  | 
"INFIMUM (set xs) f = fold (inf \<circ> f) xs top"  | 
| 56166 | 2903  | 
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
 | 
2904  | 
|
| 
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
 | 
2905  | 
declare INF_set_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
 | 
2906  | 
|
| 
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
 | 
2907  | 
lemma (in complete_lattice) SUP_set_fold:  | 
| 
56218
 
1c3f1f2431f9
elongated INFI and SUPR, to reduced risk of confusing theorems names in the future while still being consistent with INTER and UNION
 
haftmann 
parents: 
56166 
diff
changeset
 | 
2908  | 
"SUPREMUM (set xs) f = fold (sup \<circ> f) xs bot"  | 
| 56166 | 2909  | 
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
 | 
2910  | 
|
| 
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
 | 
2911  | 
declare SUP_set_fold [code]  | 
| 
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
 | 
2912  | 
|
| 
49948
 
744934b818c7
moved quite generic material from theory Enum to more appropriate places
 
haftmann 
parents: 
49808 
diff
changeset
 | 
2913  | 
|
| 
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
 | 
2914  | 
subsubsection {* Fold variants: @{const foldr} and @{const foldl} *}
 | 
| 
 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 
haftmann 
parents: 
46125 
diff
changeset
 | 
2915  | 
|
| 
 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 
haftmann 
parents: 
46125 
diff
changeset
 | 
2916  | 
text {* Correspondence *}
 | 
| 
 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 
haftmann 
parents: 
46125 
diff
changeset
 | 
2917  | 
|
| 
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
 | 
2918  | 
lemma foldr_conv_fold [code_abbrev]:  | 
| 
 
d654c73e4b12
no preference wrt. fold(l/r); prefer fold rather than foldr for iterating over lists in generated code
 
haftmann 
parents: 
47131 
diff
changeset
 | 
2919  | 
"foldr f xs = fold f (rev xs)"  | 
| 
 
d654c73e4b12
no preference wrt. fold(l/r); prefer fold rather than foldr for iterating over lists in generated code
 
haftmann 
parents: 
47131 
diff
changeset
 | 
2920  | 
by (induct xs) simp_all  | 
| 
 
d654c73e4b12
no preference wrt. fold(l/r); prefer fold rather than foldr for iterating over lists in generated code
 
haftmann 
parents: 
47131 
diff
changeset
 | 
2921  | 
|
| 
 
d654c73e4b12
no preference wrt. fold(l/r); prefer fold rather than foldr for iterating over lists in generated code
 
haftmann 
parents: 
47131 
diff
changeset
 | 
2922  | 
lemma foldl_conv_fold:  | 
| 
 
d654c73e4b12
no preference wrt. fold(l/r); prefer fold rather than foldr for iterating over lists in generated code
 
haftmann 
parents: 
47131 
diff
changeset
 | 
2923  | 
"foldl f s xs = fold (\<lambda>x s. f s x) xs s"  | 
| 
 
d654c73e4b12
no preference wrt. fold(l/r); prefer fold rather than foldr for iterating over lists in generated code
 
haftmann 
parents: 
47131 
diff
changeset
 | 
2924  | 
by (induct xs arbitrary: s) simp_all  | 
| 
 
d654c73e4b12
no preference wrt. fold(l/r); prefer fold rather than foldr for iterating over lists in generated code
 
haftmann 
parents: 
47131 
diff
changeset
 | 
2925  | 
|
| 
 
d654c73e4b12
no preference wrt. fold(l/r); prefer fold rather than foldr for iterating over lists in generated code
 
haftmann 
parents: 
47131 
diff
changeset
 | 
2926  | 
lemma foldr_conv_foldl: -- {* The ``Third Duality Theorem'' in Bird \& Wadler: *}
 | 
| 
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
 | 
2927  | 
"foldr f xs a = foldl (\<lambda>x y. f y x) a (rev xs)"  | 
| 
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
 | 
2928  | 
by (simp add: foldr_conv_fold foldl_conv_fold)  | 
| 
 
d654c73e4b12
no preference wrt. fold(l/r); prefer fold rather than foldr for iterating over lists in generated code
 
haftmann 
parents: 
47131 
diff
changeset
 | 
2929  | 
|
| 
 
d654c73e4b12
no preference wrt. fold(l/r); prefer fold rather than foldr for iterating over lists in generated code
 
haftmann 
parents: 
47131 
diff
changeset
 | 
2930  | 
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
 | 
2931  | 
"foldl f a xs = foldr (\<lambda>x y. f y x) (rev xs) a"  | 
| 
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
 | 
2932  | 
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
 | 
2933  | 
|
| 
 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 
haftmann 
parents: 
46125 
diff
changeset
 | 
2934  | 
lemma foldr_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
 | 
2935  | 
assumes "\<And>x y. x \<in> set xs \<Longrightarrow> y \<in> set xs \<Longrightarrow> f y \<circ> f x = f x \<circ> f y"  | 
| 
 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 
haftmann 
parents: 
46125 
diff
changeset
 | 
2936  | 
shows "foldr f xs = fold f xs"  | 
| 
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
 | 
2937  | 
using assms 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
 | 
2938  | 
|
| 
 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 
haftmann 
parents: 
46125 
diff
changeset
 | 
2939  | 
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
 | 
2940  | 
"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"  | 
| 
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
 | 
2941  | 
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
 | 
2942  | 
|
| 
 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 
haftmann 
parents: 
46125 
diff
changeset
 | 
2943  | 
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
 | 
2944  | 
"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"  | 
| 
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
 | 
2945  | 
by (auto simp add: foldl_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
 | 
2946  | 
|
| 
 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 
haftmann 
parents: 
46125 
diff
changeset
 | 
2947  | 
lemma foldr_append [simp]:  | 
| 
 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 
haftmann 
parents: 
46125 
diff
changeset
 | 
2948  | 
"foldr f (xs @ ys) a = foldr f xs (foldr f ys a)"  | 
| 
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
 | 
2949  | 
by (simp add: 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
 | 
2950  | 
|
| 
 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 
haftmann 
parents: 
46125 
diff
changeset
 | 
2951  | 
lemma foldl_append [simp]:  | 
| 
 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 
haftmann 
parents: 
46125 
diff
changeset
 | 
2952  | 
"foldl f a (xs @ ys) = foldl f (foldl f a xs) ys"  | 
| 
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
 | 
2953  | 
by (simp add: 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
 | 
2954  | 
|
| 
 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 
haftmann 
parents: 
46125 
diff
changeset
 | 
2955  | 
lemma foldr_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
 | 
2956  | 
"foldr g (map f xs) a = foldr (g o f) xs a"  | 
| 
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
 | 
2957  | 
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
 | 
2958  | 
|
| 
 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 
haftmann 
parents: 
46125 
diff
changeset
 | 
2959  | 
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
 | 
2960  | 
"foldl g a (map f xs) = foldl (\<lambda>a x. g a (f x)) a xs"  | 
| 
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
 | 
2961  | 
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
 | 
2962  | 
|
| 
 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 
haftmann 
parents: 
46125 
diff
changeset
 | 
2963  | 
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
 | 
2964  | 
"concat xss = foldr append xss []"  | 
| 
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
 | 
2965  | 
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
 | 
2966  | 
|
| 35115 | 2967  | 
|
| 
49948
 
744934b818c7
moved quite generic material from theory Enum to more appropriate places
 
haftmann 
parents: 
49808 
diff
changeset
 | 
2968  | 
subsubsection {* @{const upt} *}
 | 
| 13114 | 2969  | 
|
| 17090 | 2970  | 
lemma upt_rec[code]: "[i..<j] = (if i<j then i#[Suc i..<j] else [])"  | 
2971  | 
-- {* simp does not terminate! *}
 | 
|
| 13145 | 2972  | 
by (induct j) auto  | 
| 13142 | 2973  | 
|
| 
47108
 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
 
huffman 
parents: 
46898 
diff
changeset
 | 
2974  | 
lemmas upt_rec_numeral[simp] = upt_rec[of "numeral m" "numeral n"] for m n  | 
| 32005 | 2975  | 
|
| 15425 | 2976  | 
lemma upt_conv_Nil [simp]: "j <= i ==> [i..<j] = []"  | 
| 13145 | 2977  | 
by (subst upt_rec) simp  | 
| 13114 | 2978  | 
|
| 15425 | 2979  | 
lemma upt_eq_Nil_conv[simp]: "([i..<j] = []) = (j = 0 \<or> j <= i)"  | 
| 15281 | 2980  | 
by(induct j)simp_all  | 
2981  | 
||
2982  | 
lemma upt_eq_Cons_conv:  | 
|
| 24526 | 2983  | 
"([i..<j] = x#xs) = (i < j & i = x & [i+1..<j] = xs)"  | 
2984  | 
apply(induct j arbitrary: x xs)  | 
|
| 15281 | 2985  | 
apply simp  | 
2986  | 
apply(clarsimp simp add: append_eq_Cons_conv)  | 
|
2987  | 
apply arith  | 
|
2988  | 
done  | 
|
2989  | 
||
| 15425 | 2990  | 
lemma upt_Suc_append: "i <= j ==> [i..<(Suc j)] = [i..<j]@[j]"  | 
| 13145 | 2991  | 
-- {* Only needed if @{text upt_Suc} is deleted from the simpset. *}
 | 
2992  | 
by simp  | 
|
| 13114 | 2993  | 
|
| 15425 | 2994  | 
lemma upt_conv_Cons: "i < j ==> [i..<j] = i # [Suc i..<j]"  | 
| 26734 | 2995  | 
by (simp add: upt_rec)  | 
| 13114 | 2996  | 
|
| 15425 | 2997  | 
lemma upt_add_eq_append: "i<=j ==> [i..<j+k] = [i..<j]@[j..<j+k]"  | 
| 13145 | 2998  | 
-- {* LOOPS as a simprule, since @{text "j <= j"}. *}
 | 
2999  | 
by (induct k) auto  | 
|
| 13114 | 3000  | 
|
| 15425 | 3001  | 
lemma length_upt [simp]: "length [i..<j] = j - i"  | 
| 13145 | 3002  | 
by (induct j) (auto simp add: Suc_diff_le)  | 
| 13114 | 3003  | 
|
| 15425 | 3004  | 
lemma nth_upt [simp]: "i + k < j ==> [i..<j] ! k = i + k"  | 
| 13145 | 3005  | 
apply (induct j)  | 
3006  | 
apply (auto simp add: less_Suc_eq nth_append split: nat_diff_split)  | 
|
3007  | 
done  | 
|
| 13114 | 3008  | 
|
| 17906 | 3009  | 
|
3010  | 
lemma hd_upt[simp]: "i < j \<Longrightarrow> hd[i..<j] = i"  | 
|
3011  | 
by(simp add:upt_conv_Cons)  | 
|
3012  | 
||
3013  | 
lemma last_upt[simp]: "i < j \<Longrightarrow> last[i..<j] = j - 1"  | 
|
3014  | 
apply(cases j)  | 
|
3015  | 
apply simp  | 
|
3016  | 
by(simp add:upt_Suc_append)  | 
|
3017  | 
||
| 24526 | 3018  | 
lemma take_upt [simp]: "i+m <= n ==> take m [i..<n] = [i..<i+m]"  | 
3019  | 
apply (induct m arbitrary: i, simp)  | 
|
| 13145 | 3020  | 
apply (subst upt_rec)  | 
3021  | 
apply (rule sym)  | 
|
3022  | 
apply (subst upt_rec)  | 
|
3023  | 
apply (simp del: upt.simps)  | 
|
3024  | 
done  | 
|
| 3507 | 3025  | 
|
| 17501 | 3026  | 
lemma drop_upt[simp]: "drop m [i..<j] = [i+m..<j]"  | 
3027  | 
apply(induct j)  | 
|
3028  | 
apply auto  | 
|
3029  | 
done  | 
|
3030  | 
||
| 24645 | 3031  | 
lemma map_Suc_upt: "map Suc [m..<n] = [Suc m..<Suc n]"  | 
| 13145 | 3032  | 
by (induct n) auto  | 
| 13114 | 3033  | 
|
| 
54496
 
178922b63b58
add lemmas Suc_funpow and id_funpow to simpset; add lemma map_add_upt
 
hoelzl 
parents: 
54404 
diff
changeset
 | 
3034  | 
lemma map_add_upt: "map (\<lambda>i. i + n) [0..<m] = [n..<m + n]"  | 
| 
 
178922b63b58
add lemmas Suc_funpow and id_funpow to simpset; add lemma map_add_upt
 
hoelzl 
parents: 
54404 
diff
changeset
 | 
3035  | 
by (induct m) simp_all  | 
| 
 
178922b63b58
add lemmas Suc_funpow and id_funpow to simpset; add lemma map_add_upt
 
hoelzl 
parents: 
54404 
diff
changeset
 | 
3036  | 
|
| 24526 | 3037  | 
lemma nth_map_upt: "i < n-m ==> (map f [m..<n]) ! i = f(m+i)"  | 
3038  | 
apply (induct n m arbitrary: i rule: diff_induct)  | 
|
| 13145 | 3039  | 
prefer 3 apply (subst map_Suc_upt[symmetric])  | 
| 44921 | 3040  | 
apply (auto simp add: less_diff_conv)  | 
| 13145 | 3041  | 
done  | 
| 13114 | 3042  | 
|
| 52380 | 3043  | 
lemma map_decr_upt:  | 
3044  | 
"map (\<lambda>n. n - Suc 0) [Suc m..<Suc n] = [m..<n]"  | 
|
3045  | 
by (induct n) simp_all  | 
|
3046  | 
||
| 
13883
 
0451e0fb3f22
Re-structured some proofs in order to get rid of rule_format attribute.
 
berghofe 
parents: 
13863 
diff
changeset
 | 
3047  | 
lemma nth_take_lemma:  | 
| 24526 | 3048  | 
"k <= length xs ==> k <= length ys ==>  | 
| 
13883
 
0451e0fb3f22
Re-structured some proofs in order to get rid of rule_format attribute.
 
berghofe 
parents: 
13863 
diff
changeset
 | 
3049  | 
(!!i. i < k --> xs!i = ys!i) ==> take k xs = take k ys"  | 
| 24526 | 3050  | 
apply (atomize, induct k arbitrary: xs ys)  | 
| 14208 | 3051  | 
apply (simp_all add: less_Suc_eq_0_disj all_conj_distrib, clarify)  | 
| 13145 | 3052  | 
txt {* Both lists must be non-empty *}
 | 
| 14208 | 3053  | 
apply (case_tac xs, simp)  | 
3054  | 
apply (case_tac ys, clarify)  | 
|
| 13145 | 3055  | 
apply (simp (no_asm_use))  | 
3056  | 
apply clarify  | 
|
3057  | 
txt {* prenexing's needed, not miniscoping *}
 | 
|
3058  | 
apply (simp (no_asm_use) add: all_simps [symmetric] del: all_simps)  | 
|
3059  | 
apply blast  | 
|
3060  | 
done  | 
|
| 13114 | 3061  | 
|
3062  | 
lemma nth_equalityI:  | 
|
3063  | 
"[| length xs = length ys; ALL i < length xs. xs!i = ys!i |] ==> xs = ys"  | 
|
| 44921 | 3064  | 
by (frule nth_take_lemma [OF le_refl eq_imp_le]) simp_all  | 
| 13142 | 3065  | 
|
| 24796 | 3066  | 
lemma map_nth:  | 
3067  | 
"map (\<lambda>i. xs ! i) [0..<length xs] = xs"  | 
|
3068  | 
by (rule nth_equalityI, auto)  | 
|
3069  | 
||
| 13863 | 3070  | 
(* needs nth_equalityI *)  | 
3071  | 
lemma list_all2_antisym:  | 
|
3072  | 
"\<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>  | 
|
3073  | 
\<Longrightarrow> xs = ys"  | 
|
3074  | 
apply (simp add: list_all2_conv_all_nth)  | 
|
| 14208 | 3075  | 
apply (rule nth_equalityI, blast, simp)  | 
| 13863 | 3076  | 
done  | 
3077  | 
||
| 13142 | 3078  | 
lemma take_equalityI: "(\<forall>i. take i xs = take i ys) ==> xs = ys"  | 
| 13145 | 3079  | 
-- {* The famous take-lemma. *}
 | 
3080  | 
apply (drule_tac x = "max (length xs) (length ys)" in spec)  | 
|
| 44921 | 3081  | 
apply (simp add: le_max_iff_disj)  | 
| 13145 | 3082  | 
done  | 
| 13142 | 3083  | 
|
3084  | 
||
| 15302 | 3085  | 
lemma take_Cons':  | 
3086  | 
"take n (x # xs) = (if n = 0 then [] else x # take (n - 1) xs)"  | 
|
3087  | 
by (cases n) simp_all  | 
|
3088  | 
||
3089  | 
lemma drop_Cons':  | 
|
3090  | 
"drop n (x # xs) = (if n = 0 then x # xs else drop (n - 1) xs)"  | 
|
3091  | 
by (cases n) simp_all  | 
|
3092  | 
||
3093  | 
lemma nth_Cons': "(x # xs)!n = (if n = 0 then x else xs!(n - 1))"  | 
|
3094  | 
by (cases n) simp_all  | 
|
3095  | 
||
| 
47108
 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
 
huffman 
parents: 
46898 
diff
changeset
 | 
3096  | 
lemma take_Cons_numeral [simp]:  | 
| 
 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
 
huffman 
parents: 
46898 
diff
changeset
 | 
3097  | 
"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
 | 
3098  | 
by (simp add: take_Cons')  | 
| 
 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
 
huffman 
parents: 
46898 
diff
changeset
 | 
3099  | 
|
| 
 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
 
huffman 
parents: 
46898 
diff
changeset
 | 
3100  | 
lemma drop_Cons_numeral [simp]:  | 
| 
 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
 
huffman 
parents: 
46898 
diff
changeset
 | 
3101  | 
"drop (numeral v) (x # xs) = drop (numeral v - 1) xs"  | 
| 
 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
 
huffman 
parents: 
46898 
diff
changeset
 | 
3102  | 
by (simp add: drop_Cons')  | 
| 
 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
 
huffman 
parents: 
46898 
diff
changeset
 | 
3103  | 
|
| 
 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
 
huffman 
parents: 
46898 
diff
changeset
 | 
3104  | 
lemma nth_Cons_numeral [simp]:  | 
| 
 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
 
huffman 
parents: 
46898 
diff
changeset
 | 
3105  | 
"(x # xs) ! numeral v = xs ! (numeral v - 1)"  | 
| 
 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
 
huffman 
parents: 
46898 
diff
changeset
 | 
3106  | 
by (simp add: nth_Cons')  | 
| 15302 | 3107  | 
|
3108  | 
||
| 
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
 | 
3109  | 
subsubsection {* @{text upto}: interval-list on @{typ int} *}
 | 
| 
 
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
 | 
3110  | 
|
| 
 
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
 | 
3111  | 
function upto :: "int \<Rightarrow> int \<Rightarrow> int list" ("(1[_../_])") where
 | 
| 51166 | 3112  | 
"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
 | 
3113  | 
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
 | 
3114  | 
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
 | 
3115  | 
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
 | 
3116  | 
|
| 51166 | 3117  | 
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
 | 
3118  | 
|
| 
47108
 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
 
huffman 
parents: 
46898 
diff
changeset
 | 
3119  | 
lemmas upto_rec_numeral [simp] =  | 
| 
 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
 
huffman 
parents: 
46898 
diff
changeset
 | 
3120  | 
upto.simps[of "numeral m" "numeral n"]  | 
| 
54489
 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 
haftmann 
parents: 
54404 
diff
changeset
 | 
3121  | 
upto.simps[of "numeral m" "- numeral n"]  | 
| 
 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 
haftmann 
parents: 
54404 
diff
changeset
 | 
3122  | 
upto.simps[of "- numeral m" "numeral n"]  | 
| 
 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 
haftmann 
parents: 
54404 
diff
changeset
 | 
3123  | 
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
 | 
3124  | 
|
| 
 
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
 | 
3125  | 
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
 | 
3126  | 
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
 | 
3127  | 
|
| 51166 | 3128  | 
lemma upto_rec1: "i \<le> j \<Longrightarrow> [i..j] = i#[i+1..j]"  | 
3129  | 
by(simp add: upto.simps)  | 
|
3130  | 
||
3131  | 
lemma upto_rec2: "i \<le> j \<Longrightarrow> [i..j] = [i..j - 1]@[j]"  | 
|
3132  | 
proof(induct "nat(j-i)" arbitrary: i j)  | 
|
3133  | 
case 0 thus ?case by(simp add: upto.simps)  | 
|
3134  | 
next  | 
|
3135  | 
case (Suc n)  | 
|
3136  | 
hence "n = nat (j - (i + 1))" "i < j" by linarith+  | 
|
3137  | 
from this(2) Suc.hyps(1)[OF this(1)] Suc(2,3) upto_rec1 show ?case by simp  | 
|
3138  | 
qed  | 
|
3139  | 
||
| 
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
 | 
3140  | 
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
 | 
3141  | 
proof(induct i j rule:upto.induct)  | 
| 
 
edbf0a86fb1c
adding simproc to rewrite list comprehensions to set comprehensions; adopting proofs
 
bulwahn 
parents: 
41372 
diff
changeset
 | 
3142  | 
case (1 i j)  | 
| 
 
edbf0a86fb1c
adding simproc to rewrite list comprehensions to set comprehensions; adopting proofs
 
bulwahn 
parents: 
41372 
diff
changeset
 | 
3143  | 
from this show ?case  | 
| 55811 | 3144  | 
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
 | 
3145  | 
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
 | 
3146  | 
|
| 51166 | 3147  | 
text{* Tail recursive version for code generation: *}
 | 
3148  | 
||
| 51170 | 3149  | 
definition upto_aux :: "int \<Rightarrow> int \<Rightarrow> int list \<Rightarrow> int list" where  | 
3150  | 
"upto_aux i j js = [i..j] @ js"  | 
|
3151  | 
||
3152  | 
lemma upto_aux_rec [code]:  | 
|
| 51166 | 3153  | 
"upto_aux i j js = (if j<i then js else upto_aux i (j - 1) (j#js))"  | 
| 51170 | 3154  | 
by (simp add: upto_aux_def upto_rec2)  | 
| 51166 | 3155  | 
|
3156  | 
lemma upto_code[code]: "[i..j] = upto_aux i j []"  | 
|
| 51170 | 3157  | 
by(simp add: upto_aux_def)  | 
| 51166 | 3158  | 
|
| 
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
 | 
3159  | 
|
| 
53721
 
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
 
traytel 
parents: 
53689 
diff
changeset
 | 
3160  | 
subsubsection {* @{const distinct} and @{const remdups} and @{const remdups_adj} *}
 | 
| 13142 | 3161  | 
|
| 
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
 | 
3162  | 
lemma distinct_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
 | 
3163  | 
"distinct xs \<Longrightarrow> distinct (tl 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
 | 
3164  | 
by (cases xs) simp_all  | 
| 
 
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
 | 
3165  | 
|
| 13142 | 3166  | 
lemma distinct_append [simp]:  | 
| 13145 | 3167  | 
"distinct (xs @ ys) = (distinct xs \<and> distinct ys \<and> set xs \<inter> set ys = {})"
 | 
3168  | 
by (induct xs) auto  | 
|
| 13142 | 3169  | 
|
| 15305 | 3170  | 
lemma distinct_rev[simp]: "distinct(rev xs) = distinct xs"  | 
3171  | 
by(induct xs) auto  | 
|
3172  | 
||
| 13142 | 3173  | 
lemma set_remdups [simp]: "set (remdups xs) = set xs"  | 
| 13145 | 3174  | 
by (induct xs) (auto simp add: insert_absorb)  | 
| 13142 | 3175  | 
|
3176  | 
lemma distinct_remdups [iff]: "distinct (remdups xs)"  | 
|
| 13145 | 3177  | 
by (induct xs) auto  | 
| 13142 | 3178  | 
|
| 25287 | 3179  | 
lemma distinct_remdups_id: "distinct xs ==> remdups xs = xs"  | 
3180  | 
by (induct xs, auto)  | 
|
3181  | 
||
| 26734 | 3182  | 
lemma remdups_id_iff_distinct [simp]: "remdups xs = xs \<longleftrightarrow> distinct xs"  | 
3183  | 
by (metis distinct_remdups distinct_remdups_id)  | 
|
| 25287 | 3184  | 
|
| 24566 | 3185  | 
lemma finite_distinct_list: "finite A \<Longrightarrow> EX xs. set xs = A & distinct xs"  | 
| 24632 | 3186  | 
by (metis distinct_remdups finite_list set_remdups)  | 
| 24566 | 3187  | 
|
| 15072 | 3188  | 
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
 | 
3189  | 
by (induct x, auto)  | 
| 15072 | 3190  | 
|
3191  | 
lemma remdups_eq_nil_right_iff [simp]: "([] = remdups x) = (x = [])"  | 
|
| 24349 | 3192  | 
by (induct x, auto)  | 
| 15072 | 3193  | 
|
| 15245 | 3194  | 
lemma length_remdups_leq[iff]: "length(remdups xs) <= length xs"  | 
3195  | 
by (induct xs) auto  | 
|
3196  | 
||
3197  | 
lemma length_remdups_eq[iff]:  | 
|
3198  | 
"(length (remdups xs) = length xs) = (remdups xs = xs)"  | 
|
3199  | 
apply(induct xs)  | 
|
3200  | 
apply auto  | 
|
3201  | 
apply(subgoal_tac "length (remdups xs) <= length xs")  | 
|
3202  | 
apply arith  | 
|
3203  | 
apply(rule length_remdups_leq)  | 
|
3204  | 
done  | 
|
3205  | 
||
| 33945 | 3206  | 
lemma remdups_filter: "remdups(filter P xs) = filter P (remdups xs)"  | 
3207  | 
apply(induct xs)  | 
|
3208  | 
apply auto  | 
|
3209  | 
done  | 
|
| 18490 | 3210  | 
|
3211  | 
lemma distinct_map:  | 
|
3212  | 
"distinct(map f xs) = (distinct xs & inj_on f (set xs))"  | 
|
3213  | 
by (induct xs) auto  | 
|
3214  | 
||
| 13142 | 3215  | 
lemma distinct_filter [simp]: "distinct xs ==> distinct (filter P xs)"  | 
| 13145 | 3216  | 
by (induct xs) auto  | 
| 13114 | 3217  | 
|
| 17501 | 3218  | 
lemma distinct_upt[simp]: "distinct[i..<j]"  | 
3219  | 
by (induct j) auto  | 
|
3220  | 
||
| 
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
 | 
3221  | 
lemma distinct_upto[simp]: "distinct[i..j]"  | 
| 
 
1dddf2f64266
got rid of complicated class finite_intvl_succ and defined "upto" directly on int, the only instance of the class.
 
nipkow 
parents: 
32078 
diff
changeset
 | 
3222  | 
apply(induct i j rule:upto.induct)  | 
| 
 
1dddf2f64266
got rid of complicated class finite_intvl_succ and defined "upto" directly on int, the only instance of the class.
 
nipkow 
parents: 
32078 
diff
changeset
 | 
3223  | 
apply(subst upto.simps)  | 
| 
 
1dddf2f64266
got rid of complicated class finite_intvl_succ and defined "upto" directly on int, the only instance of the class.
 
nipkow 
parents: 
32078 
diff
changeset
 | 
3224  | 
apply(simp)  | 
| 
 
1dddf2f64266
got rid of complicated class finite_intvl_succ and defined "upto" directly on int, the only instance of the class.
 
nipkow 
parents: 
32078 
diff
changeset
 | 
3225  | 
done  | 
| 
 
1dddf2f64266
got rid of complicated class finite_intvl_succ and defined "upto" directly on int, the only instance of the class.
 
nipkow 
parents: 
32078 
diff
changeset
 | 
3226  | 
|
| 24526 | 3227  | 
lemma distinct_take[simp]: "distinct xs \<Longrightarrow> distinct (take i xs)"  | 
3228  | 
apply(induct xs arbitrary: i)  | 
|
| 17501 | 3229  | 
apply simp  | 
3230  | 
apply (case_tac i)  | 
|
3231  | 
apply simp_all  | 
|
3232  | 
apply(blast dest:in_set_takeD)  | 
|
3233  | 
done  | 
|
3234  | 
||
| 24526 | 3235  | 
lemma distinct_drop[simp]: "distinct xs \<Longrightarrow> distinct (drop i xs)"  | 
3236  | 
apply(induct xs arbitrary: i)  | 
|
| 17501 | 3237  | 
apply simp  | 
3238  | 
apply (case_tac i)  | 
|
3239  | 
apply simp_all  | 
|
3240  | 
done  | 
|
3241  | 
||
3242  | 
lemma distinct_list_update:  | 
|
3243  | 
assumes d: "distinct xs" and a: "a \<notin> set xs - {xs!i}"
 | 
|
3244  | 
shows "distinct (xs[i:=a])"  | 
|
3245  | 
proof (cases "i < length xs")  | 
|
3246  | 
case True  | 
|
3247  | 
  with a have "a \<notin> set (take i xs @ xs ! i # drop (Suc i) xs) - {xs!i}"
 | 
|
3248  | 
apply (drule_tac id_take_nth_drop) by simp  | 
|
3249  | 
with d True show ?thesis  | 
|
3250  | 
apply (simp add: upd_conv_take_nth_drop)  | 
|
3251  | 
apply (drule subst [OF id_take_nth_drop]) apply assumption  | 
|
3252  | 
apply simp apply (cases "a = xs!i") apply simp by blast  | 
|
3253  | 
next  | 
|
3254  | 
case False with d show ?thesis by auto  | 
|
3255  | 
qed  | 
|
3256  | 
||
| 
31363
 
7493b571b37d
Added theorems about distinct & concat, map & replicate and concat & replicate
 
hoelzl 
parents: 
31264 
diff
changeset
 | 
3257  | 
lemma distinct_concat:  | 
| 
 
7493b571b37d
Added theorems about distinct & concat, map & replicate and concat & replicate
 
hoelzl 
parents: 
31264 
diff
changeset
 | 
3258  | 
assumes "distinct xs"  | 
| 
 
7493b571b37d
Added theorems about distinct & concat, map & replicate and concat & replicate
 
hoelzl 
parents: 
31264 
diff
changeset
 | 
3259  | 
and "\<And> ys. ys \<in> set xs \<Longrightarrow> distinct ys"  | 
| 
 
7493b571b37d
Added theorems about distinct & concat, map & replicate and concat & replicate
 
hoelzl 
parents: 
31264 
diff
changeset
 | 
3260  | 
  and "\<And> ys zs. \<lbrakk> ys \<in> set xs ; zs \<in> set xs ; ys \<noteq> zs \<rbrakk> \<Longrightarrow> set ys \<inter> set zs = {}"
 | 
| 
 
7493b571b37d
Added theorems about distinct & concat, map & replicate and concat & replicate
 
hoelzl 
parents: 
31264 
diff
changeset
 | 
3261  | 
shows "distinct (concat xs)"  | 
| 
 
7493b571b37d
Added theorems about distinct & concat, map & replicate and concat & replicate
 
hoelzl 
parents: 
31264 
diff
changeset
 | 
3262  | 
using assms by (induct xs) auto  | 
| 17501 | 3263  | 
|
3264  | 
text {* It is best to avoid this indexed version of distinct, but
 | 
|
3265  | 
sometimes it is useful. *}  | 
|
3266  | 
||
| 13142 | 3267  | 
lemma distinct_conv_nth:  | 
| 17501 | 3268  | 
"distinct xs = (\<forall>i < size xs. \<forall>j < size xs. i \<noteq> j --> xs!i \<noteq> xs!j)"  | 
| 15251 | 3269  | 
apply (induct xs, simp, simp)  | 
| 14208 | 3270  | 
apply (rule iffI, clarsimp)  | 
| 13145 | 3271  | 
apply (case_tac i)  | 
| 14208 | 3272  | 
apply (case_tac j, simp)  | 
| 13145 | 3273  | 
apply (simp add: set_conv_nth)  | 
3274  | 
apply (case_tac j)  | 
|
| 
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
 | 
3275  | 
apply (clarsimp simp add: set_conv_nth, simp)  | 
| 13145 | 3276  | 
apply (rule conjI)  | 
| 24648 | 3277  | 
apply (clarsimp simp add: set_conv_nth)  | 
3278  | 
apply (erule_tac x = 0 in allE, simp)  | 
|
3279  | 
apply (erule_tac x = "Suc i" in allE, simp, clarsimp)  | 
|
| 25130 | 3280  | 
apply (erule_tac x = "Suc i" in allE, simp)  | 
3281  | 
apply (erule_tac x = "Suc j" in allE, simp)  | 
|
| 13145 | 3282  | 
done  | 
| 13114 | 3283  | 
|
| 18490 | 3284  | 
lemma nth_eq_iff_index_eq:  | 
3285  | 
"\<lbrakk> distinct xs; i < length xs; j < length xs \<rbrakk> \<Longrightarrow> (xs!i = xs!j) = (i = j)"  | 
|
3286  | 
by(auto simp: distinct_conv_nth)  | 
|
3287  | 
||
| 56953 | 3288  | 
lemma set_update_distinct: "\<lbrakk> distinct xs; n < length xs \<rbrakk> \<Longrightarrow>  | 
3289  | 
  set(xs[n := x]) = insert x (set xs - {xs!n})"
 | 
|
3290  | 
by(auto simp: set_eq_iff in_set_conv_nth nth_list_update nth_eq_iff_index_eq)  | 
|
3291  | 
||
| 57537 | 3292  | 
lemma distinct_swap[simp]: "\<lbrakk> i < size xs; j < size xs \<rbrakk> \<Longrightarrow>  | 
3293  | 
distinct(xs[i := xs!j, j := xs!i]) = distinct xs"  | 
|
3294  | 
apply (simp add: distinct_conv_nth nth_list_update)  | 
|
3295  | 
apply safe  | 
|
3296  | 
apply metis+  | 
|
3297  | 
done  | 
|
3298  | 
||
3299  | 
lemma set_swap[simp]:  | 
|
3300  | 
"\<lbrakk> i < size xs; j < size xs \<rbrakk> \<Longrightarrow> set(xs[i := xs!j, j := xs!i]) = set xs"  | 
|
3301  | 
by(simp add: set_conv_nth nth_list_update) metis  | 
|
3302  | 
||
| 
15110
 
78b5636eabc7
Added a number of new thms and the new function remove1
 
nipkow 
parents: 
15072 
diff
changeset
 | 
3303  | 
lemma distinct_card: "distinct xs ==> card (set xs) = size xs"  | 
| 24349 | 3304  | 
by (induct xs) auto  | 
| 14388 | 3305  | 
|
| 
15110
 
78b5636eabc7
Added a number of new thms and the new function remove1
 
nipkow 
parents: 
15072 
diff
changeset
 | 
3306  | 
lemma card_distinct: "card (set xs) = size xs ==> distinct xs"  | 
| 14388 | 3307  | 
proof (induct xs)  | 
3308  | 
case Nil thus ?case by simp  | 
|
3309  | 
next  | 
|
3310  | 
case (Cons x xs)  | 
|
3311  | 
show ?case  | 
|
3312  | 
proof (cases "x \<in> set xs")  | 
|
3313  | 
case False with Cons show ?thesis by simp  | 
|
3314  | 
next  | 
|
3315  | 
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
 | 
3316  | 
have "card (set xs) = Suc (length xs)"  | 
| 14388 | 3317  | 
by (simp add: card_insert_if split: split_if_asm)  | 
3318  | 
moreover have "card (set xs) \<le> length xs" by (rule card_length)  | 
|
3319  | 
ultimately have False by simp  | 
|
3320  | 
thus ?thesis ..  | 
|
3321  | 
qed  | 
|
3322  | 
qed  | 
|
3323  | 
||
| 
45115
 
93c1ac6727a3
adding lemma to List library for executable equation of the (refl) transitive closure
 
bulwahn 
parents: 
44928 
diff
changeset
 | 
3324  | 
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
 | 
3325  | 
by (induct xs) (auto)  | 
| 
 
93c1ac6727a3
adding lemma to List library for executable equation of the (refl) transitive closure
 
bulwahn 
parents: 
44928 
diff
changeset
 | 
3326  | 
|
| 25287 | 3327  | 
lemma not_distinct_decomp: "~ distinct ws ==> EX xs ys zs y. ws = xs@[y]@ys@[y]@zs"  | 
3328  | 
apply (induct n == "length ws" arbitrary:ws) apply simp  | 
|
3329  | 
apply(case_tac ws) apply simp  | 
|
3330  | 
apply (simp split:split_if_asm)  | 
|
3331  | 
apply (metis Cons_eq_appendI eq_Nil_appendI split_list)  | 
|
3332  | 
done  | 
|
| 18490 | 3333  | 
|
| 45841 | 3334  | 
lemma not_distinct_conv_prefix:  | 
3335  | 
defines "dec as xs y ys \<equiv> y \<in> set xs \<and> distinct xs \<and> as = xs @ y # ys"  | 
|
3336  | 
shows "\<not>distinct as \<longleftrightarrow> (\<exists>xs y ys. dec as xs y ys)" (is "?L = ?R")  | 
|
3337  | 
proof  | 
|
3338  | 
assume "?L" then show "?R"  | 
|
3339  | 
proof (induct "length as" arbitrary: as rule: less_induct)  | 
|
3340  | 
case less  | 
|
3341  | 
obtain xs ys zs y where decomp: "as = (xs @ y # ys) @ y # zs"  | 
|
3342  | 
using not_distinct_decomp[OF less.prems] by auto  | 
|
3343  | 
show ?case  | 
|
3344  | 
proof (cases "distinct (xs @ y # ys)")  | 
|
3345  | 
case True  | 
|
3346  | 
with decomp have "dec as (xs @ y # ys) y zs" by (simp add: dec_def)  | 
|
3347  | 
then show ?thesis by blast  | 
|
3348  | 
next  | 
|
3349  | 
case False  | 
|
3350  | 
with less decomp obtain xs' y' ys' where "dec (xs @ y # ys) xs' y' ys'"  | 
|
3351  | 
by atomize_elim auto  | 
|
3352  | 
with decomp have "dec as xs' y' (ys' @ y # zs)" by (simp add: dec_def)  | 
|
3353  | 
then show ?thesis by blast  | 
|
3354  | 
qed  | 
|
3355  | 
qed  | 
|
3356  | 
qed (auto simp: dec_def)  | 
|
3357  | 
||
| 
49948
 
744934b818c7
moved quite generic material from theory Enum to more appropriate places
 
haftmann 
parents: 
49808 
diff
changeset
 | 
3358  | 
lemma distinct_product:  | 
| 57247 | 3359  | 
"distinct xs \<Longrightarrow> distinct ys \<Longrightarrow> distinct (List.product xs ys)"  | 
3360  | 
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
 | 
3361  | 
|
| 
53721
 
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
 
traytel 
parents: 
53689 
diff
changeset
 | 
3362  | 
lemma distinct_product_lists:  | 
| 
 
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
 
traytel 
parents: 
53689 
diff
changeset
 | 
3363  | 
assumes "\<forall>xs \<in> set xss. distinct xs"  | 
| 
 
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
 
traytel 
parents: 
53689 
diff
changeset
 | 
3364  | 
shows "distinct (product_lists xss)"  | 
| 
 
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
 
traytel 
parents: 
53689 
diff
changeset
 | 
3365  | 
using assms proof (induction xss)  | 
| 
 
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
 
traytel 
parents: 
53689 
diff
changeset
 | 
3366  | 
case (Cons xs xss) note * = this  | 
| 
 
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
 
traytel 
parents: 
53689 
diff
changeset
 | 
3367  | 
then show ?case  | 
| 
 
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
 
traytel 
parents: 
53689 
diff
changeset
 | 
3368  | 
proof (cases "product_lists xss")  | 
| 
 
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
 
traytel 
parents: 
53689 
diff
changeset
 | 
3369  | 
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
 | 
3370  | 
next  | 
| 
 
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
 
traytel 
parents: 
53689 
diff
changeset
 | 
3371  | 
case (Cons ps pss) with * show ?thesis  | 
| 
 
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
 
traytel 
parents: 
53689 
diff
changeset
 | 
3372  | 
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
 | 
3373  | 
qed  | 
| 
 
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
 
traytel 
parents: 
53689 
diff
changeset
 | 
3374  | 
qed simp  | 
| 
 
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
 
traytel 
parents: 
53689 
diff
changeset
 | 
3375  | 
|
| 18490 | 3376  | 
lemma length_remdups_concat:  | 
| 44921 | 3377  | 
"length (remdups (concat xss)) = card (\<Union>xs\<in>set xss. set xs)"  | 
3378  | 
by (simp add: distinct_card [symmetric])  | 
|
| 17906 | 3379  | 
|
| 
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
 | 
3380  | 
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
 | 
3381  | 
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
 | 
3382  | 
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
 | 
3383  | 
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
 | 
3384  | 
qed  | 
| 17906 | 3385  | 
|
| 36275 | 3386  | 
lemma remdups_remdups:  | 
3387  | 
"remdups (remdups xs) = remdups xs"  | 
|
3388  | 
by (induct xs) simp_all  | 
|
3389  | 
||
| 36851 | 3390  | 
lemma distinct_butlast:  | 
| 
46500
 
0196966d6d2d
removing unnecessary premises in theorems of List theory
 
bulwahn 
parents: 
46448 
diff
changeset
 | 
3391  | 
assumes "distinct xs"  | 
| 36851 | 3392  | 
shows "distinct (butlast xs)"  | 
| 
46500
 
0196966d6d2d
removing unnecessary premises in theorems of List theory
 
bulwahn 
parents: 
46448 
diff
changeset
 | 
3393  | 
proof (cases "xs = []")  | 
| 
 
0196966d6d2d
removing unnecessary premises in theorems of List theory
 
bulwahn 
parents: 
46448 
diff
changeset
 | 
3394  | 
case False  | 
| 
 
0196966d6d2d
removing unnecessary premises in theorems of List theory
 
bulwahn 
parents: 
46448 
diff
changeset
 | 
3395  | 
from `xs \<noteq> []` obtain ys y where "xs = ys @ [y]" by (cases xs rule: rev_cases) auto  | 
| 
 
0196966d6d2d
removing unnecessary premises in theorems of List theory
 
bulwahn 
parents: 
46448 
diff
changeset
 | 
3396  | 
with `distinct xs` show ?thesis by simp  | 
| 
 
0196966d6d2d
removing unnecessary premises in theorems of List theory
 
bulwahn 
parents: 
46448 
diff
changeset
 | 
3397  | 
qed (auto)  | 
| 36851 | 3398  | 
|
| 39728 | 3399  | 
lemma remdups_map_remdups:  | 
3400  | 
"remdups (map f (remdups xs)) = remdups (map f xs)"  | 
|
3401  | 
by (induct xs) simp_all  | 
|
3402  | 
||
| 
39915
 
ecf97cf3d248
turned distinct and sorted into inductive predicates: yields nice induction principles for free; more elegant proofs
 
haftmann 
parents: 
39774 
diff
changeset
 | 
3403  | 
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
 | 
3404  | 
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
 | 
3405  | 
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
 | 
3406  | 
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
 | 
3407  | 
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
 | 
3408  | 
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
 | 
3409  | 
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
 | 
3410  | 
with assms have "distinct xs'" by simp  | 
| 
 
ecf97cf3d248
turned distinct and sorted into inductive predicates: yields nice induction principles for free; more elegant proofs
 
haftmann 
parents: 
39774 
diff
changeset
 | 
3411  | 
with `length xs' = length ys'` show "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
 | 
3412  | 
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
 | 
3413  | 
qed  | 
| 
 
ecf97cf3d248
turned distinct and sorted into inductive predicates: yields nice induction principles for free; more elegant proofs
 
haftmann 
parents: 
39774 
diff
changeset
 | 
3414  | 
|
| 
 
ecf97cf3d248
turned distinct and sorted into inductive predicates: yields nice induction principles for free; more elegant proofs
 
haftmann 
parents: 
39774 
diff
changeset
 | 
3415  | 
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
 | 
3416  | 
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
 | 
3417  | 
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
 | 
3418  | 
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
 | 
3419  | 
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
 | 
3420  | 
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
 | 
3421  | 
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
 | 
3422  | 
with assms have "distinct ys'" by simp  | 
| 
 
ecf97cf3d248
turned distinct and sorted into inductive predicates: yields nice induction principles for free; more elegant proofs
 
haftmann 
parents: 
39774 
diff
changeset
 | 
3423  | 
with `length xs' = length ys'` show "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
 | 
3424  | 
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
 | 
3425  | 
qed  | 
| 
 
ecf97cf3d248
turned distinct and sorted into inductive predicates: yields nice induction principles for free; more elegant proofs
 
haftmann 
parents: 
39774 
diff
changeset
 | 
3426  | 
|
| 47122 | 3427  | 
lemma set_take_disj_set_drop_if_distinct:  | 
3428  | 
  "distinct vs \<Longrightarrow> i \<le> j \<Longrightarrow> set (take i vs) \<inter> set (drop j vs) = {}"
 | 
|
3429  | 
by (auto simp: in_set_conv_nth distinct_conv_nth)  | 
|
3430  | 
||
| 
44635
 
3d046864ebe6
added two lemmas about "distinct" to help Sledgehammer
 
blanchet 
parents: 
44619 
diff
changeset
 | 
3431  | 
(* The next two lemmas help Sledgehammer. *)  | 
| 
 
3d046864ebe6
added two lemmas about "distinct" to help Sledgehammer
 
blanchet 
parents: 
44619 
diff
changeset
 | 
3432  | 
|
| 
 
3d046864ebe6
added two lemmas about "distinct" to help Sledgehammer
 
blanchet 
parents: 
44619 
diff
changeset
 | 
3433  | 
lemma distinct_singleton: "distinct [x]" by simp  | 
| 
 
3d046864ebe6
added two lemmas about "distinct" to help Sledgehammer
 
blanchet 
parents: 
44619 
diff
changeset
 | 
3434  | 
|
| 
 
3d046864ebe6
added two lemmas about "distinct" to help Sledgehammer
 
blanchet 
parents: 
44619 
diff
changeset
 | 
3435  | 
lemma distinct_length_2_or_more:  | 
| 
 
3d046864ebe6
added two lemmas about "distinct" to help Sledgehammer
 
blanchet 
parents: 
44619 
diff
changeset
 | 
3436  | 
"distinct (a # b # xs) \<longleftrightarrow> (a \<noteq> b \<and> distinct (a # xs) \<and> distinct (b # xs))"  | 
| 56085 | 3437  | 
by force  | 
| 
44635
 
3d046864ebe6
added two lemmas about "distinct" to help Sledgehammer
 
blanchet 
parents: 
44619 
diff
changeset
 | 
3438  | 
|
| 
53721
 
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
 
traytel 
parents: 
53689 
diff
changeset
 | 
3439  | 
lemma remdups_adj_Cons: "remdups_adj (x # xs) =  | 
| 
 
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
 
traytel 
parents: 
53689 
diff
changeset
 | 
3440  | 
(case remdups_adj xs of [] \<Rightarrow> [x] | y # xs \<Rightarrow> if x = y then y # xs else x # y # xs)"  | 
| 
 
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
 
traytel 
parents: 
53689 
diff
changeset
 | 
3441  | 
by (induct xs arbitrary: x) (auto split: list.splits)  | 
| 
 
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
 
traytel 
parents: 
53689 
diff
changeset
 | 
3442  | 
|
| 
 
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
 
traytel 
parents: 
53689 
diff
changeset
 | 
3443  | 
lemma remdups_adj_append_two:  | 
| 
 
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
 
traytel 
parents: 
53689 
diff
changeset
 | 
3444  | 
"remdups_adj (xs @ [x,y]) = remdups_adj (xs @ [x]) @ (if x = y then [] else [y])"  | 
| 
 
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
 
traytel 
parents: 
53689 
diff
changeset
 | 
3445  | 
by (induct xs rule: remdups_adj.induct, simp_all)  | 
| 
 
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
 
traytel 
parents: 
53689 
diff
changeset
 | 
3446  | 
|
| 
 
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
 
traytel 
parents: 
53689 
diff
changeset
 | 
3447  | 
lemma remdups_adj_rev[simp]: "remdups_adj (rev xs) = rev (remdups_adj xs)"  | 
| 
 
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
 
traytel 
parents: 
53689 
diff
changeset
 | 
3448  | 
by (induct xs rule: remdups_adj.induct, simp_all add: remdups_adj_append_two)  | 
| 
 
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
 
traytel 
parents: 
53689 
diff
changeset
 | 
3449  | 
|
| 
 
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
 
traytel 
parents: 
53689 
diff
changeset
 | 
3450  | 
lemma remdups_adj_length[simp]: "length (remdups_adj xs) \<le> length xs"  | 
| 
 
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
 
traytel 
parents: 
53689 
diff
changeset
 | 
3451  | 
by (induct xs rule: remdups_adj.induct, auto)  | 
| 
 
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
 
traytel 
parents: 
53689 
diff
changeset
 | 
3452  | 
|
| 
 
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
 
traytel 
parents: 
53689 
diff
changeset
 | 
3453  | 
lemma remdups_adj_length_ge1[simp]: "xs \<noteq> [] \<Longrightarrow> length (remdups_adj xs) \<ge> Suc 0"  | 
| 
 
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
 
traytel 
parents: 
53689 
diff
changeset
 | 
3454  | 
by (induct xs rule: remdups_adj.induct, simp_all)  | 
| 
 
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
 
traytel 
parents: 
53689 
diff
changeset
 | 
3455  | 
|
| 
 
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
 
traytel 
parents: 
53689 
diff
changeset
 | 
3456  | 
lemma remdups_adj_Nil_iff[simp]: "remdups_adj xs = [] \<longleftrightarrow> xs = []"  | 
| 
 
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
 
traytel 
parents: 
53689 
diff
changeset
 | 
3457  | 
by (induct xs rule: remdups_adj.induct, simp_all)  | 
| 
 
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
 
traytel 
parents: 
53689 
diff
changeset
 | 
3458  | 
|
| 
 
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
 
traytel 
parents: 
53689 
diff
changeset
 | 
3459  | 
lemma remdups_adj_set[simp]: "set (remdups_adj xs) = set xs"  | 
| 
 
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
 
traytel 
parents: 
53689 
diff
changeset
 | 
3460  | 
by (induct xs rule: remdups_adj.induct, simp_all)  | 
| 
 
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
 
traytel 
parents: 
53689 
diff
changeset
 | 
3461  | 
|
| 
 
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
 
traytel 
parents: 
53689 
diff
changeset
 | 
3462  | 
lemma remdups_adj_Cons_alt[simp]: "x # tl (remdups_adj (x # xs)) = remdups_adj (x # xs)"  | 
| 
 
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
 
traytel 
parents: 
53689 
diff
changeset
 | 
3463  | 
by (induct xs rule: remdups_adj.induct, auto)  | 
| 
 
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
 
traytel 
parents: 
53689 
diff
changeset
 | 
3464  | 
|
| 
 
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
 
traytel 
parents: 
53689 
diff
changeset
 | 
3465  | 
lemma remdups_adj_distinct: "distinct xs \<Longrightarrow> remdups_adj xs = xs"  | 
| 
 
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
 
traytel 
parents: 
53689 
diff
changeset
 | 
3466  | 
by (induct xs rule: remdups_adj.induct, simp_all)  | 
| 
 
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
 
traytel 
parents: 
53689 
diff
changeset
 | 
3467  | 
|
| 
 
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
 
traytel 
parents: 
53689 
diff
changeset
 | 
3468  | 
lemma remdups_adj_append:  | 
| 
 
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
 
traytel 
parents: 
53689 
diff
changeset
 | 
3469  | 
"remdups_adj (xs\<^sub>1 @ x # xs\<^sub>2) = remdups_adj (xs\<^sub>1 @ [x]) @ tl (remdups_adj (x # xs\<^sub>2))"  | 
| 
 
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
 
traytel 
parents: 
53689 
diff
changeset
 | 
3470  | 
by (induct xs\<^sub>1 rule: remdups_adj.induct, simp_all)  | 
| 
 
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
 
traytel 
parents: 
53689 
diff
changeset
 | 
3471  | 
|
| 
 
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
 
traytel 
parents: 
53689 
diff
changeset
 | 
3472  | 
lemma remdups_adj_singleton:  | 
| 
 
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
 
traytel 
parents: 
53689 
diff
changeset
 | 
3473  | 
"remdups_adj xs = [x] \<Longrightarrow> xs = replicate (length xs) x"  | 
| 
 
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
 
traytel 
parents: 
53689 
diff
changeset
 | 
3474  | 
by (induct xs rule: remdups_adj.induct, auto split: split_if_asm)  | 
| 
 
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
 
traytel 
parents: 
53689 
diff
changeset
 | 
3475  | 
|
| 
 
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
 
traytel 
parents: 
53689 
diff
changeset
 | 
3476  | 
lemma remdups_adj_map_injective:  | 
| 
 
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
 
traytel 
parents: 
53689 
diff
changeset
 | 
3477  | 
assumes "inj f"  | 
| 
 
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
 
traytel 
parents: 
53689 
diff
changeset
 | 
3478  | 
shows "remdups_adj (map f xs) = map f (remdups_adj xs)"  | 
| 
 
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
 
traytel 
parents: 
53689 
diff
changeset
 | 
3479  | 
by (induct xs rule: remdups_adj.induct,  | 
| 
 
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
 
traytel 
parents: 
53689 
diff
changeset
 | 
3480  | 
auto simp add: injD[OF assms])  | 
| 
 
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
 
traytel 
parents: 
53689 
diff
changeset
 | 
3481  | 
|
| 
49948
 
744934b818c7
moved quite generic material from theory Enum to more appropriate places
 
haftmann 
parents: 
49808 
diff
changeset
 | 
3482  | 
|
| 
37605
 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 
haftmann 
parents: 
37465 
diff
changeset
 | 
3483  | 
subsubsection {* List summation: @{const listsum} and @{text"\<Sum>"}*}
 | 
| 
 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 
haftmann 
parents: 
37465 
diff
changeset
 | 
3484  | 
|
| 39774 | 3485  | 
lemma (in monoid_add) listsum_simps [simp]:  | 
3486  | 
"listsum [] = 0"  | 
|
| 
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
 | 
3487  | 
"listsum (x # xs) = x + listsum xs"  | 
| 39774 | 3488  | 
by (simp_all add: listsum_def)  | 
3489  | 
||
3490  | 
lemma (in monoid_add) listsum_append [simp]:  | 
|
3491  | 
"listsum (xs @ ys) = listsum xs + listsum ys"  | 
|
3492  | 
by (induct xs) (simp_all add: add.assoc)  | 
|
3493  | 
||
3494  | 
lemma (in comm_monoid_add) listsum_rev [simp]:  | 
|
3495  | 
"listsum (rev xs) = listsum xs"  | 
|
| 
57514
 
bdc2c6b40bf2
prefer ac_simps collections over separate name bindings for add and mult
 
haftmann 
parents: 
57512 
diff
changeset
 | 
3496  | 
by (simp add: listsum_def foldr_fold fold_rev fun_eq_iff ac_simps)  | 
| 
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
 | 
3497  | 
|
| 
 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 
haftmann 
parents: 
46125 
diff
changeset
 | 
3498  | 
lemma (in monoid_add) fold_plus_listsum_rev:  | 
| 
 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 
haftmann 
parents: 
46125 
diff
changeset
 | 
3499  | 
"fold plus xs = plus (listsum (rev xs))"  | 
| 
 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 
haftmann 
parents: 
46125 
diff
changeset
 | 
3500  | 
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
 | 
3501  | 
fix 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
 | 
3502  | 
have "fold plus xs x = fold plus xs (x + 0)" by simp  | 
| 
 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 
haftmann 
parents: 
46125 
diff
changeset
 | 
3503  | 
also have "\<dots> = fold plus (x # xs) 0" 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
 | 
3504  | 
also have "\<dots> = foldr plus (rev xs @ [x]) 0" by (simp add: 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
 | 
3505  | 
also have "\<dots> = listsum (rev xs @ [x])" by (simp add: listsum_def)  | 
| 
 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 
haftmann 
parents: 
46125 
diff
changeset
 | 
3506  | 
also have "\<dots> = listsum (rev xs) + listsum [x]" by simp  | 
| 
 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 
haftmann 
parents: 
46125 
diff
changeset
 | 
3507  | 
finally show "fold plus xs x = listsum (rev xs) + x" by simp  | 
| 
 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 
haftmann 
parents: 
46125 
diff
changeset
 | 
3508  | 
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
 | 
3509  | 
|
| 
 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 
haftmann 
parents: 
46125 
diff
changeset
 | 
3510  | 
text{* Some syntactic sugar for summing a function over a list: *}
 | 
| 
 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 
haftmann 
parents: 
46125 
diff
changeset
 | 
3511  | 
|
| 
 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 
haftmann 
parents: 
46125 
diff
changeset
 | 
3512  | 
syntax  | 
| 
 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 
haftmann 
parents: 
46125 
diff
changeset
 | 
3513  | 
  "_listsum" :: "pttrn => 'a list => 'b => 'b"    ("(3SUM _<-_. _)" [0, 51, 10] 10)
 | 
| 
 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 
haftmann 
parents: 
46125 
diff
changeset
 | 
3514  | 
syntax (xsymbols)  | 
| 
 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 
haftmann 
parents: 
46125 
diff
changeset
 | 
3515  | 
  "_listsum" :: "pttrn => 'a list => 'b => 'b"    ("(3\<Sum>_\<leftarrow>_. _)" [0, 51, 10] 10)
 | 
| 
 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 
haftmann 
parents: 
46125 
diff
changeset
 | 
3516  | 
syntax (HTML output)  | 
| 
 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 
haftmann 
parents: 
46125 
diff
changeset
 | 
3517  | 
  "_listsum" :: "pttrn => 'a list => 'b => 'b"    ("(3\<Sum>_\<leftarrow>_. _)" [0, 51, 10] 10)
 | 
| 
 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 
haftmann 
parents: 
46125 
diff
changeset
 | 
3518  | 
|
| 
 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 
haftmann 
parents: 
46125 
diff
changeset
 | 
3519  | 
translations -- {* Beware of argument permutation! *}
 | 
| 
 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 
haftmann 
parents: 
46125 
diff
changeset
 | 
3520  | 
"SUM x<-xs. b" == "CONST listsum (CONST map (%x. b) xs)"  | 
| 
 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 
haftmann 
parents: 
46125 
diff
changeset
 | 
3521  | 
"\<Sum>x\<leftarrow>xs. b" == "CONST listsum (CONST map (%x. b) xs)"  | 
| 39774 | 3522  | 
|
3523  | 
lemma (in comm_monoid_add) listsum_map_remove1:  | 
|
3524  | 
"x \<in> set xs \<Longrightarrow> listsum (map f xs) = f x + listsum (map f (remove1 x xs))"  | 
|
3525  | 
by (induct xs) (auto simp add: ac_simps)  | 
|
3526  | 
||
| 
56643
 
41d3596d8a64
move size hooks together, with new one preceding old one and sharing same theory data
 
blanchet 
parents: 
56545 
diff
changeset
 | 
3527  | 
lemma (in monoid_add) size_list_conv_listsum:  | 
| 
 
41d3596d8a64
move size hooks together, with new one preceding old one and sharing same theory data
 
blanchet 
parents: 
56545 
diff
changeset
 | 
3528  | 
"size_list f xs = listsum (map f xs) + size xs"  | 
| 39774 | 3529  | 
by (induct xs) auto  | 
3530  | 
||
3531  | 
lemma (in monoid_add) length_concat:  | 
|
3532  | 
"length (concat xss) = listsum (map length xss)"  | 
|
3533  | 
by (induct xss) simp_all  | 
|
3534  | 
||
| 
53721
 
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
 
traytel 
parents: 
53689 
diff
changeset
 | 
3535  | 
lemma (in monoid_add) length_product_lists:  | 
| 
 
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
 
traytel 
parents: 
53689 
diff
changeset
 | 
3536  | 
"length (product_lists xss) = foldr op * (map length xss) 1"  | 
| 
 
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
 
traytel 
parents: 
53689 
diff
changeset
 | 
3537  | 
proof (induct xss)  | 
| 
 
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
 
traytel 
parents: 
53689 
diff
changeset
 | 
3538  | 
case (Cons xs xss) then show ?case by (induct xs) (auto simp: length_concat o_def)  | 
| 
 
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
 
traytel 
parents: 
53689 
diff
changeset
 | 
3539  | 
qed simp  | 
| 
 
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
 
traytel 
parents: 
53689 
diff
changeset
 | 
3540  | 
|
| 39774 | 3541  | 
lemma (in monoid_add) listsum_map_filter:  | 
3542  | 
assumes "\<And>x. x \<in> set xs \<Longrightarrow> \<not> P x \<Longrightarrow> f x = 0"  | 
|
| 
37605
 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 
haftmann 
parents: 
37465 
diff
changeset
 | 
3543  | 
shows "listsum (map f (filter P xs)) = listsum (map f xs)"  | 
| 39774 | 3544  | 
using assms by (induct xs) auto  | 
3545  | 
||
| 
51738
 
9e4220605179
tuned: unnamed contexts, interpretation and sublocale in locale target;
 
haftmann 
parents: 
51717 
diff
changeset
 | 
3546  | 
lemma (in comm_monoid_add) distinct_listsum_conv_Setsum:  | 
| 39774 | 3547  | 
"distinct xs \<Longrightarrow> listsum xs = Setsum (set xs)"  | 
3548  | 
by (induct xs) simp_all  | 
|
3549  | 
||
3550  | 
lemma listsum_eq_0_nat_iff_nat [simp]:  | 
|
3551  | 
"listsum ns = (0::nat) \<longleftrightarrow> (\<forall>n \<in> set ns. n = 0)"  | 
|
| 
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
 | 
3552  | 
by (induct ns) simp_all  | 
| 
 
d654c73e4b12
no preference wrt. fold(l/r); prefer fold rather than foldr for iterating over lists in generated code
 
haftmann 
parents: 
47131 
diff
changeset
 | 
3553  | 
|
| 
 
d654c73e4b12
no preference wrt. fold(l/r); prefer fold rather than foldr for iterating over lists in generated code
 
haftmann 
parents: 
47131 
diff
changeset
 | 
3554  | 
lemma member_le_listsum_nat:  | 
| 
 
d654c73e4b12
no preference wrt. fold(l/r); prefer fold rather than foldr for iterating over lists in generated code
 
haftmann 
parents: 
47131 
diff
changeset
 | 
3555  | 
"(n :: nat) \<in> set ns \<Longrightarrow> n \<le> listsum ns"  | 
| 
 
d654c73e4b12
no preference wrt. fold(l/r); prefer fold rather than foldr for iterating over lists in generated code
 
haftmann 
parents: 
47131 
diff
changeset
 | 
3556  | 
by (induct ns) auto  | 
| 39774 | 3557  | 
|
3558  | 
lemma elem_le_listsum_nat:  | 
|
3559  | 
"k < size ns \<Longrightarrow> ns ! k \<le> listsum (ns::nat list)"  | 
|
| 
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
 | 
3560  | 
by (rule member_le_listsum_nat) simp  | 
| 
37605
 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 
haftmann 
parents: 
37465 
diff
changeset
 | 
3561  | 
|
| 39774 | 3562  | 
lemma listsum_update_nat:  | 
| 
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
 | 
3563  | 
"k < size ns \<Longrightarrow> listsum (ns[k := (n::nat)]) = listsum ns + n - ns ! k"  | 
| 
37605
 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 
haftmann 
parents: 
37465 
diff
changeset
 | 
3564  | 
apply(induct ns arbitrary:k)  | 
| 
 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 
haftmann 
parents: 
37465 
diff
changeset
 | 
3565  | 
apply (auto split:nat.split)  | 
| 
 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 
haftmann 
parents: 
37465 
diff
changeset
 | 
3566  | 
apply(drule elem_le_listsum_nat)  | 
| 
 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 
haftmann 
parents: 
37465 
diff
changeset
 | 
3567  | 
apply arith  | 
| 
 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 
haftmann 
parents: 
37465 
diff
changeset
 | 
3568  | 
done  | 
| 
 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 
haftmann 
parents: 
37465 
diff
changeset
 | 
3569  | 
|
| 39774 | 3570  | 
lemma (in monoid_add) listsum_triv:  | 
3571  | 
"(\<Sum>x\<leftarrow>xs. r) = of_nat (length xs) * r"  | 
|
| 
49962
 
a8cc904a6820
Renamed {left,right}_distrib to distrib_{right,left}.
 
webertj 
parents: 
49808 
diff
changeset
 | 
3572  | 
by (induct xs) (simp_all add: distrib_right)  | 
| 
37605
 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 
haftmann 
parents: 
37465 
diff
changeset
 | 
3573  | 
|
| 39774 | 3574  | 
lemma (in monoid_add) listsum_0 [simp]:  | 
3575  | 
"(\<Sum>x\<leftarrow>xs. 0) = 0"  | 
|
| 
49962
 
a8cc904a6820
Renamed {left,right}_distrib to distrib_{right,left}.
 
webertj 
parents: 
49808 
diff
changeset
 | 
3576  | 
by (induct xs) (simp_all add: distrib_right)  | 
| 
37605
 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 
haftmann 
parents: 
37465 
diff
changeset
 | 
3577  | 
|
| 
 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 
haftmann 
parents: 
37465 
diff
changeset
 | 
3578  | 
text{* For non-Abelian groups @{text xs} needs to be reversed on one side: *}
 | 
| 39774 | 3579  | 
lemma (in ab_group_add) uminus_listsum_map:  | 
3580  | 
"- listsum (map f xs) = listsum (map (uminus \<circ> f) xs)"  | 
|
3581  | 
by (induct xs) simp_all  | 
|
3582  | 
||
3583  | 
lemma (in comm_monoid_add) listsum_addf:  | 
|
3584  | 
"(\<Sum>x\<leftarrow>xs. f x + g x) = listsum (map f xs) + listsum (map g xs)"  | 
|
3585  | 
by (induct xs) (simp_all add: algebra_simps)  | 
|
3586  | 
||
3587  | 
lemma (in ab_group_add) listsum_subtractf:  | 
|
3588  | 
"(\<Sum>x\<leftarrow>xs. f x - g x) = listsum (map f xs) - listsum (map g xs)"  | 
|
3589  | 
by (induct xs) (simp_all add: algebra_simps)  | 
|
3590  | 
||
3591  | 
lemma (in semiring_0) listsum_const_mult:  | 
|
3592  | 
"(\<Sum>x\<leftarrow>xs. c * f x) = c * (\<Sum>x\<leftarrow>xs. f x)"  | 
|
3593  | 
by (induct xs) (simp_all add: algebra_simps)  | 
|
3594  | 
||
3595  | 
lemma (in semiring_0) listsum_mult_const:  | 
|
3596  | 
"(\<Sum>x\<leftarrow>xs. f x * c) = (\<Sum>x\<leftarrow>xs. f x) * c"  | 
|
3597  | 
by (induct xs) (simp_all add: algebra_simps)  | 
|
3598  | 
||
3599  | 
lemma (in ordered_ab_group_add_abs) listsum_abs:  | 
|
3600  | 
"\<bar>listsum xs\<bar> \<le> listsum (map abs xs)"  | 
|
3601  | 
by (induct xs) (simp_all add: order_trans [OF abs_triangle_ineq])  | 
|
| 
37605
 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 
haftmann 
parents: 
37465 
diff
changeset
 | 
3602  | 
|
| 
 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 
haftmann 
parents: 
37465 
diff
changeset
 | 
3603  | 
lemma listsum_mono:  | 
| 39774 | 3604  | 
  fixes f g :: "'a \<Rightarrow> 'b::{monoid_add, ordered_ab_semigroup_add}"
 | 
| 
37605
 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 
haftmann 
parents: 
37465 
diff
changeset
 | 
3605  | 
shows "(\<And>x. x \<in> set xs \<Longrightarrow> f x \<le> g x) \<Longrightarrow> (\<Sum>x\<leftarrow>xs. f x) \<le> (\<Sum>x\<leftarrow>xs. g x)"  | 
| 39774 | 3606  | 
by (induct xs) (simp, simp add: add_mono)  | 
3607  | 
||
3608  | 
lemma (in monoid_add) listsum_distinct_conv_setsum_set:  | 
|
| 
37605
 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 
haftmann 
parents: 
37465 
diff
changeset
 | 
3609  | 
"distinct xs \<Longrightarrow> listsum (map f xs) = setsum f (set xs)"  | 
| 
 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 
haftmann 
parents: 
37465 
diff
changeset
 | 
3610  | 
by (induct xs) simp_all  | 
| 
 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 
haftmann 
parents: 
37465 
diff
changeset
 | 
3611  | 
|
| 39774 | 3612  | 
lemma (in monoid_add) interv_listsum_conv_setsum_set_nat:  | 
| 
37605
 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 
haftmann 
parents: 
37465 
diff
changeset
 | 
3613  | 
"listsum (map f [m..<n]) = setsum f (set [m..<n])"  | 
| 
 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 
haftmann 
parents: 
37465 
diff
changeset
 | 
3614  | 
by (simp add: listsum_distinct_conv_setsum_set)  | 
| 
 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 
haftmann 
parents: 
37465 
diff
changeset
 | 
3615  | 
|
| 39774 | 3616  | 
lemma (in monoid_add) interv_listsum_conv_setsum_set_int:  | 
| 
37605
 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 
haftmann 
parents: 
37465 
diff
changeset
 | 
3617  | 
"listsum (map f [k..l]) = setsum f (set [k..l])"  | 
| 
 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 
haftmann 
parents: 
37465 
diff
changeset
 | 
3618  | 
by (simp add: listsum_distinct_conv_setsum_set)  | 
| 
 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 
haftmann 
parents: 
37465 
diff
changeset
 | 
3619  | 
|
| 
 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 
haftmann 
parents: 
37465 
diff
changeset
 | 
3620  | 
text {* General equivalence between @{const listsum} and @{const setsum} *}
 | 
| 39774 | 3621  | 
lemma (in monoid_add) listsum_setsum_nth:  | 
| 
37605
 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 
haftmann 
parents: 
37465 
diff
changeset
 | 
3622  | 
"listsum xs = (\<Sum> i = 0 ..< length xs. xs ! i)"  | 
| 
 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 
haftmann 
parents: 
37465 
diff
changeset
 | 
3623  | 
using interv_listsum_conv_setsum_set_nat [of "op ! xs" 0 "length xs"] by (simp add: map_nth)  | 
| 
 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 
haftmann 
parents: 
37465 
diff
changeset
 | 
3624  | 
|
| 
 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 
haftmann 
parents: 
37465 
diff
changeset
 | 
3625  | 
|
| 
34978
 
874150ddd50a
canonical insert operation; generalized lemma foldl_apply_inv to foldl_apply
 
haftmann 
parents: 
34942 
diff
changeset
 | 
3626  | 
subsubsection {* @{const insert} *}
 | 
| 
 
874150ddd50a
canonical insert operation; generalized lemma foldl_apply_inv to foldl_apply
 
haftmann 
parents: 
34942 
diff
changeset
 | 
3627  | 
|
| 
 
874150ddd50a
canonical insert operation; generalized lemma foldl_apply_inv to foldl_apply
 
haftmann 
parents: 
34942 
diff
changeset
 | 
3628  | 
lemma in_set_insert [simp]:  | 
| 
 
874150ddd50a
canonical insert operation; generalized lemma foldl_apply_inv to foldl_apply
 
haftmann 
parents: 
34942 
diff
changeset
 | 
3629  | 
"x \<in> set xs \<Longrightarrow> List.insert x xs = xs"  | 
| 
 
874150ddd50a
canonical insert operation; generalized lemma foldl_apply_inv to foldl_apply
 
haftmann 
parents: 
34942 
diff
changeset
 | 
3630  | 
by (simp add: List.insert_def)  | 
| 
 
874150ddd50a
canonical insert operation; generalized lemma foldl_apply_inv to foldl_apply
 
haftmann 
parents: 
34942 
diff
changeset
 | 
3631  | 
|
| 
 
874150ddd50a
canonical insert operation; generalized lemma foldl_apply_inv to foldl_apply
 
haftmann 
parents: 
34942 
diff
changeset
 | 
3632  | 
lemma not_in_set_insert [simp]:  | 
| 
 
874150ddd50a
canonical insert operation; generalized lemma foldl_apply_inv to foldl_apply
 
haftmann 
parents: 
34942 
diff
changeset
 | 
3633  | 
"x \<notin> set xs \<Longrightarrow> List.insert x xs = x # xs"  | 
| 
 
874150ddd50a
canonical insert operation; generalized lemma foldl_apply_inv to foldl_apply
 
haftmann 
parents: 
34942 
diff
changeset
 | 
3634  | 
by (simp add: List.insert_def)  | 
| 
 
874150ddd50a
canonical insert operation; generalized lemma foldl_apply_inv to foldl_apply
 
haftmann 
parents: 
34942 
diff
changeset
 | 
3635  | 
|
| 
 
874150ddd50a
canonical insert operation; generalized lemma foldl_apply_inv to foldl_apply
 
haftmann 
parents: 
34942 
diff
changeset
 | 
3636  | 
lemma insert_Nil [simp]:  | 
| 
 
874150ddd50a
canonical insert operation; generalized lemma foldl_apply_inv to foldl_apply
 
haftmann 
parents: 
34942 
diff
changeset
 | 
3637  | 
"List.insert x [] = [x]"  | 
| 
 
874150ddd50a
canonical insert operation; generalized lemma foldl_apply_inv to foldl_apply
 
haftmann 
parents: 
34942 
diff
changeset
 | 
3638  | 
by simp  | 
| 
 
874150ddd50a
canonical insert operation; generalized lemma foldl_apply_inv to foldl_apply
 
haftmann 
parents: 
34942 
diff
changeset
 | 
3639  | 
|
| 35295 | 3640  | 
lemma set_insert [simp]:  | 
| 
34978
 
874150ddd50a
canonical insert operation; generalized lemma foldl_apply_inv to foldl_apply
 
haftmann 
parents: 
34942 
diff
changeset
 | 
3641  | 
"set (List.insert x xs) = insert x (set xs)"  | 
| 
 
874150ddd50a
canonical insert operation; generalized lemma foldl_apply_inv to foldl_apply
 
haftmann 
parents: 
34942 
diff
changeset
 | 
3642  | 
by (auto simp add: List.insert_def)  | 
| 
 
874150ddd50a
canonical insert operation; generalized lemma foldl_apply_inv to foldl_apply
 
haftmann 
parents: 
34942 
diff
changeset
 | 
3643  | 
|
| 35295 | 3644  | 
lemma distinct_insert [simp]:  | 
| 57198 | 3645  | 
"distinct (List.insert x xs) = distinct xs"  | 
| 35295 | 3646  | 
by (simp add: List.insert_def)  | 
3647  | 
||
| 36275 | 3648  | 
lemma insert_remdups:  | 
3649  | 
"List.insert x (remdups xs) = remdups (List.insert x xs)"  | 
|
3650  | 
by (simp add: List.insert_def)  | 
|
3651  | 
||
| 
34978
 
874150ddd50a
canonical insert operation; generalized lemma foldl_apply_inv to foldl_apply
 
haftmann 
parents: 
34942 
diff
changeset
 | 
3652  | 
|
| 57198 | 3653  | 
subsubsection {* @{const List.union} *}
 | 
3654  | 
||
3655  | 
text{* This is all one should need to know about union: *}
 | 
|
3656  | 
lemma set_union[simp]: "set (List.union xs ys) = set xs \<union> set ys"  | 
|
3657  | 
unfolding List.union_def  | 
|
3658  | 
by(induct xs arbitrary: ys) simp_all  | 
|
3659  | 
||
3660  | 
lemma distinct_union[simp]: "distinct(List.union xs ys) = distinct ys"  | 
|
3661  | 
unfolding List.union_def  | 
|
3662  | 
by(induct xs arbitrary: ys) simp_all  | 
|
3663  | 
||
3664  | 
||
| 47122 | 3665  | 
subsubsection {* @{const List.find} *}
 | 
3666  | 
||
3667  | 
lemma find_None_iff: "List.find P xs = None \<longleftrightarrow> \<not> (\<exists>x. x \<in> set xs \<and> P x)"  | 
|
3668  | 
proof (induction xs)  | 
|
3669  | 
case Nil thus ?case by simp  | 
|
3670  | 
next  | 
|
3671  | 
case (Cons x xs) thus ?case by (fastforce split: if_splits)  | 
|
3672  | 
qed  | 
|
3673  | 
||
3674  | 
lemma find_Some_iff:  | 
|
3675  | 
"List.find P xs = Some x \<longleftrightarrow>  | 
|
3676  | 
(\<exists>i<length xs. P (xs!i) \<and> x = xs!i \<and> (\<forall>j<i. \<not> P (xs!j)))"  | 
|
3677  | 
proof (induction xs)  | 
|
3678  | 
case Nil thus ?case by simp  | 
|
3679  | 
next  | 
|
3680  | 
case (Cons x xs) thus ?case  | 
|
| 56085 | 3681  | 
apply(auto simp: nth_Cons' split: if_splits)  | 
3682  | 
using diff_Suc_1[unfolded One_nat_def] less_Suc_eq_0_disj by fastforce  | 
|
| 47122 | 3683  | 
qed  | 
3684  | 
||
3685  | 
lemma find_cong[fundef_cong]:  | 
|
3686  | 
assumes "xs = ys" and "\<And>x. x \<in> set ys \<Longrightarrow> P x = Q x"  | 
|
3687  | 
shows "List.find P xs = List.find Q ys"  | 
|
3688  | 
proof (cases "List.find P xs")  | 
|
3689  | 
case None thus ?thesis by (metis find_None_iff assms)  | 
|
3690  | 
next  | 
|
3691  | 
case (Some x)  | 
|
3692  | 
hence "List.find Q ys = Some x" using assms  | 
|
3693  | 
by (auto simp add: find_Some_iff)  | 
|
3694  | 
thus ?thesis using Some by auto  | 
|
3695  | 
qed  | 
|
3696  | 
||
| 52379 | 3697  | 
lemma find_dropWhile:  | 
3698  | 
"List.find P xs = (case dropWhile (Not \<circ> P) xs  | 
|
3699  | 
of [] \<Rightarrow> None  | 
|
3700  | 
| x # _ \<Rightarrow> Some x)"  | 
|
3701  | 
by (induct xs) simp_all  | 
|
3702  | 
||
| 47122 | 3703  | 
|
| 55807 | 3704  | 
subsubsection {* @{const List.extract} *}
 | 
3705  | 
||
3706  | 
lemma extract_None_iff: "List.extract P xs = None \<longleftrightarrow> \<not> (\<exists> x\<in>set xs. P x)"  | 
|
3707  | 
by(auto simp: extract_def dropWhile_eq_Cons_conv split: list.splits)  | 
|
3708  | 
(metis in_set_conv_decomp)  | 
|
3709  | 
||
3710  | 
lemma extract_SomeE:  | 
|
3711  | 
"List.extract P xs = Some (ys, y, zs) \<Longrightarrow>  | 
|
3712  | 
xs = ys @ y # zs \<and> P y \<and> \<not> (\<exists> y \<in> set ys. P y)"  | 
|
3713  | 
by(auto simp: extract_def dropWhile_eq_Cons_conv split: list.splits)  | 
|
3714  | 
||
3715  | 
lemma extract_Some_iff:  | 
|
3716  | 
"List.extract P xs = Some (ys, y, zs) \<longleftrightarrow>  | 
|
3717  | 
xs = ys @ y # zs \<and> P y \<and> \<not> (\<exists> y \<in> set ys. P y)"  | 
|
3718  | 
by(auto simp: extract_def dropWhile_eq_Cons_conv dest: set_takeWhileD split: list.splits)  | 
|
3719  | 
||
3720  | 
lemma extract_Nil_code[code]: "List.extract P [] = None"  | 
|
3721  | 
by(simp add: extract_def)  | 
|
3722  | 
||
3723  | 
lemma extract_Cons_code[code]:  | 
|
3724  | 
"List.extract P (x # xs) = (if P x then Some ([], x, xs) else  | 
|
3725  | 
(case List.extract P xs of  | 
|
3726  | 
None \<Rightarrow> None |  | 
|
3727  | 
Some (ys, y, zs) \<Rightarrow> Some (x#ys, y, zs)))"  | 
|
| 56085 | 3728  | 
by(auto simp add: extract_def comp_def split: list.splits)  | 
3729  | 
(metis dropWhile_eq_Nil_conv list.distinct(1))  | 
|
| 55807 | 3730  | 
|
3731  | 
||
| 47122 | 3732  | 
subsubsection {* @{const remove1} *}
 | 
| 
15110
 
78b5636eabc7
Added a number of new thms and the new function remove1
 
nipkow 
parents: 
15072 
diff
changeset
 | 
3733  | 
|
| 18049 | 3734  | 
lemma remove1_append:  | 
3735  | 
"remove1 x (xs @ ys) =  | 
|
3736  | 
(if x \<in> set xs then remove1 x xs @ ys else xs @ remove1 x ys)"  | 
|
3737  | 
by (induct xs) auto  | 
|
3738  | 
||
| 36903 | 3739  | 
lemma remove1_commute: "remove1 x (remove1 y zs) = remove1 y (remove1 x zs)"  | 
3740  | 
by (induct zs) auto  | 
|
3741  | 
||
| 23479 | 3742  | 
lemma in_set_remove1[simp]:  | 
3743  | 
"a \<noteq> b \<Longrightarrow> a : set(remove1 b xs) = (a : set xs)"  | 
|
3744  | 
apply (induct xs)  | 
|
3745  | 
apply auto  | 
|
3746  | 
done  | 
|
3747  | 
||
| 
15110
 
78b5636eabc7
Added a number of new thms and the new function remove1
 
nipkow 
parents: 
15072 
diff
changeset
 | 
3748  | 
lemma set_remove1_subset: "set(remove1 x xs) <= set xs"  | 
| 
 
78b5636eabc7
Added a number of new thms and the new function remove1
 
nipkow 
parents: 
15072 
diff
changeset
 | 
3749  | 
apply(induct xs)  | 
| 
 
78b5636eabc7
Added a number of new thms and the new function remove1
 
nipkow 
parents: 
15072 
diff
changeset
 | 
3750  | 
apply simp  | 
| 
 
78b5636eabc7
Added a number of new thms and the new function remove1
 
nipkow 
parents: 
15072 
diff
changeset
 | 
3751  | 
apply simp  | 
| 
 
78b5636eabc7
Added a number of new thms and the new function remove1
 
nipkow 
parents: 
15072 
diff
changeset
 | 
3752  | 
apply blast  | 
| 
 
78b5636eabc7
Added a number of new thms and the new function remove1
 
nipkow 
parents: 
15072 
diff
changeset
 | 
3753  | 
done  | 
| 
 
78b5636eabc7
Added a number of new thms and the new function remove1
 
nipkow 
parents: 
15072 
diff
changeset
 | 
3754  | 
|
| 17724 | 3755  | 
lemma set_remove1_eq [simp]: "distinct xs ==> set(remove1 x xs) = set xs - {x}"
 | 
| 
15110
 
78b5636eabc7
Added a number of new thms and the new function remove1
 
nipkow 
parents: 
15072 
diff
changeset
 | 
3756  | 
apply(induct xs)  | 
| 
 
78b5636eabc7
Added a number of new thms and the new function remove1
 
nipkow 
parents: 
15072 
diff
changeset
 | 
3757  | 
apply simp  | 
| 
 
78b5636eabc7
Added a number of new thms and the new function remove1
 
nipkow 
parents: 
15072 
diff
changeset
 | 
3758  | 
apply simp  | 
| 
 
78b5636eabc7
Added a number of new thms and the new function remove1
 
nipkow 
parents: 
15072 
diff
changeset
 | 
3759  | 
apply blast  | 
| 
 
78b5636eabc7
Added a number of new thms and the new function remove1
 
nipkow 
parents: 
15072 
diff
changeset
 | 
3760  | 
done  | 
| 
 
78b5636eabc7
Added a number of new thms and the new function remove1
 
nipkow 
parents: 
15072 
diff
changeset
 | 
3761  | 
|
| 23479 | 3762  | 
lemma length_remove1:  | 
| 
30128
 
365ee7319b86
revert some Suc 0 lemmas back to their original forms; added some simp rules for (1::nat)
 
huffman 
parents: 
30079 
diff
changeset
 | 
3763  | 
"length(remove1 x xs) = (if x : set xs then length xs - 1 else length xs)"  | 
| 23479 | 3764  | 
apply (induct xs)  | 
3765  | 
apply (auto dest!:length_pos_if_in_set)  | 
|
3766  | 
done  | 
|
3767  | 
||
| 18049 | 3768  | 
lemma remove1_filter_not[simp]:  | 
3769  | 
"\<not> P x \<Longrightarrow> remove1 x (filter P xs) = filter P xs"  | 
|
3770  | 
by(induct xs) auto  | 
|
3771  | 
||
| 39073 | 3772  | 
lemma filter_remove1:  | 
3773  | 
"filter Q (remove1 x xs) = remove1 x (filter Q xs)"  | 
|
3774  | 
by (induct xs) auto  | 
|
3775  | 
||
| 
15110
 
78b5636eabc7
Added a number of new thms and the new function remove1
 
nipkow 
parents: 
15072 
diff
changeset
 | 
3776  | 
lemma notin_set_remove1[simp]: "x ~: set xs ==> x ~: set(remove1 y xs)"  | 
| 
 
78b5636eabc7
Added a number of new thms and the new function remove1
 
nipkow 
parents: 
15072 
diff
changeset
 | 
3777  | 
apply(insert set_remove1_subset)  | 
| 
 
78b5636eabc7
Added a number of new thms and the new function remove1
 
nipkow 
parents: 
15072 
diff
changeset
 | 
3778  | 
apply fast  | 
| 
 
78b5636eabc7
Added a number of new thms and the new function remove1
 
nipkow 
parents: 
15072 
diff
changeset
 | 
3779  | 
done  | 
| 
 
78b5636eabc7
Added a number of new thms and the new function remove1
 
nipkow 
parents: 
15072 
diff
changeset
 | 
3780  | 
|
| 
 
78b5636eabc7
Added a number of new thms and the new function remove1
 
nipkow 
parents: 
15072 
diff
changeset
 | 
3781  | 
lemma distinct_remove1[simp]: "distinct xs ==> distinct(remove1 x xs)"  | 
| 
 
78b5636eabc7
Added a number of new thms and the new function remove1
 
nipkow 
parents: 
15072 
diff
changeset
 | 
3782  | 
by (induct xs) simp_all  | 
| 
 
78b5636eabc7
Added a number of new thms and the new function remove1
 
nipkow 
parents: 
15072 
diff
changeset
 | 
3783  | 
|
| 36275 | 3784  | 
lemma remove1_remdups:  | 
3785  | 
"distinct xs \<Longrightarrow> remove1 x (remdups xs) = remdups (remove1 x xs)"  | 
|
3786  | 
by (induct xs) simp_all  | 
|
3787  | 
||
| 37107 | 3788  | 
lemma remove1_idem:  | 
3789  | 
assumes "x \<notin> set xs"  | 
|
3790  | 
shows "remove1 x xs = xs"  | 
|
3791  | 
using assms by (induct xs) simp_all  | 
|
3792  | 
||
| 13114 | 3793  | 
|
| 
49948
 
744934b818c7
moved quite generic material from theory Enum to more appropriate places
 
haftmann 
parents: 
49808 
diff
changeset
 | 
3794  | 
subsubsection {* @{const removeAll} *}
 | 
| 27693 | 3795  | 
|
| 
34978
 
874150ddd50a
canonical insert operation; generalized lemma foldl_apply_inv to foldl_apply
 
haftmann 
parents: 
34942 
diff
changeset
 | 
3796  | 
lemma removeAll_filter_not_eq:  | 
| 
 
874150ddd50a
canonical insert operation; generalized lemma foldl_apply_inv to foldl_apply
 
haftmann 
parents: 
34942 
diff
changeset
 | 
3797  | 
"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
 | 
3798  | 
proof  | 
| 
 
874150ddd50a
canonical insert operation; generalized lemma foldl_apply_inv to foldl_apply
 
haftmann 
parents: 
34942 
diff
changeset
 | 
3799  | 
fix xs  | 
| 
 
874150ddd50a
canonical insert operation; generalized lemma foldl_apply_inv to foldl_apply
 
haftmann 
parents: 
34942 
diff
changeset
 | 
3800  | 
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
 | 
3801  | 
by (induct xs) auto  | 
| 
 
874150ddd50a
canonical insert operation; generalized lemma foldl_apply_inv to foldl_apply
 
haftmann 
parents: 
34942 
diff
changeset
 | 
3802  | 
qed  | 
| 
 
874150ddd50a
canonical insert operation; generalized lemma foldl_apply_inv to foldl_apply
 
haftmann 
parents: 
34942 
diff
changeset
 | 
3803  | 
|
| 27693 | 3804  | 
lemma removeAll_append[simp]:  | 
3805  | 
"removeAll x (xs @ ys) = removeAll x xs @ removeAll x ys"  | 
|
3806  | 
by (induct xs) auto  | 
|
3807  | 
||
3808  | 
lemma set_removeAll[simp]: "set(removeAll x xs) = set xs - {x}"
 | 
|
3809  | 
by (induct xs) auto  | 
|
3810  | 
||
3811  | 
lemma removeAll_id[simp]: "x \<notin> set xs \<Longrightarrow> removeAll x xs = xs"  | 
|
3812  | 
by (induct xs) auto  | 
|
3813  | 
||
| 
46448
 
f1201fac7398
more specification of the quotient package in IsarRef
 
Cezary Kaliszyk <cezarykaliszyk@gmail.com> 
parents: 
46440 
diff
changeset
 | 
3814  | 
(* Needs count:: 'a \<Rightarrow> 'a list \<Rightarrow> nat  | 
| 27693 | 3815  | 
lemma length_removeAll:  | 
3816  | 
"length(removeAll x xs) = length xs - count x xs"  | 
|
3817  | 
*)  | 
|
3818  | 
||
3819  | 
lemma removeAll_filter_not[simp]:  | 
|
3820  | 
"\<not> P x \<Longrightarrow> removeAll x (filter P xs) = filter P xs"  | 
|
3821  | 
by(induct xs) auto  | 
|
3822  | 
||
| 
34978
 
874150ddd50a
canonical insert operation; generalized lemma foldl_apply_inv to foldl_apply
 
haftmann 
parents: 
34942 
diff
changeset
 | 
3823  | 
lemma distinct_removeAll:  | 
| 
 
874150ddd50a
canonical insert operation; generalized lemma foldl_apply_inv to foldl_apply
 
haftmann 
parents: 
34942 
diff
changeset
 | 
3824  | 
"distinct xs \<Longrightarrow> distinct (removeAll x xs)"  | 
| 
 
874150ddd50a
canonical insert operation; generalized lemma foldl_apply_inv to foldl_apply
 
haftmann 
parents: 
34942 
diff
changeset
 | 
3825  | 
by (simp add: removeAll_filter_not_eq)  | 
| 27693 | 3826  | 
|
3827  | 
lemma distinct_remove1_removeAll:  | 
|
3828  | 
"distinct xs ==> remove1 x xs = removeAll x xs"  | 
|
3829  | 
by (induct xs) simp_all  | 
|
3830  | 
||
3831  | 
lemma map_removeAll_inj_on: "inj_on f (insert x (set xs)) \<Longrightarrow>  | 
|
3832  | 
map f (removeAll x xs) = removeAll (f x) (map f xs)"  | 
|
3833  | 
by (induct xs) (simp_all add:inj_on_def)  | 
|
3834  | 
||
3835  | 
lemma map_removeAll_inj: "inj f \<Longrightarrow>  | 
|
3836  | 
map f (removeAll x xs) = removeAll (f x) (map f xs)"  | 
|
| 56085 | 3837  | 
by (rule map_removeAll_inj_on, erule subset_inj_on, rule subset_UNIV)  | 
| 27693 | 3838  | 
|
3839  | 
||
| 
49948
 
744934b818c7
moved quite generic material from theory Enum to more appropriate places
 
haftmann 
parents: 
49808 
diff
changeset
 | 
3840  | 
subsubsection {* @{const replicate} *}
 | 
| 13114 | 3841  | 
|
| 13142 | 3842  | 
lemma length_replicate [simp]: "length (replicate n x) = n"  | 
| 13145 | 3843  | 
by (induct n) auto  | 
| 13124 | 3844  | 
|
| 
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
 | 
3845  | 
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
 | 
3846  | 
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
 | 
3847  | 
|
| 13142 | 3848  | 
lemma map_replicate [simp]: "map f (replicate n x) = replicate n (f x)"  | 
| 13145 | 3849  | 
by (induct n) auto  | 
| 13114 | 3850  | 
|
| 
31363
 
7493b571b37d
Added theorems about distinct & concat, map & replicate and concat & replicate
 
hoelzl 
parents: 
31264 
diff
changeset
 | 
3851  | 
lemma map_replicate_const:  | 
| 
 
7493b571b37d
Added theorems about distinct & concat, map & replicate and concat & replicate
 
hoelzl 
parents: 
31264 
diff
changeset
 | 
3852  | 
"map (\<lambda> x. k) lst = replicate (length lst) k"  | 
| 
 
7493b571b37d
Added theorems about distinct & concat, map & replicate and concat & replicate
 
hoelzl 
parents: 
31264 
diff
changeset
 | 
3853  | 
by (induct lst) auto  | 
| 
 
7493b571b37d
Added theorems about distinct & concat, map & replicate and concat & replicate
 
hoelzl 
parents: 
31264 
diff
changeset
 | 
3854  | 
|
| 13114 | 3855  | 
lemma replicate_app_Cons_same:  | 
| 13145 | 3856  | 
"(replicate n x) @ (x # xs) = x # replicate n x @ xs"  | 
3857  | 
by (induct n) auto  | 
|
| 13114 | 3858  | 
|
| 13142 | 3859  | 
lemma rev_replicate [simp]: "rev (replicate n x) = replicate n x"  | 
| 14208 | 3860  | 
apply (induct n, simp)  | 
| 13145 | 3861  | 
apply (simp add: replicate_app_Cons_same)  | 
3862  | 
done  | 
|
| 13114 | 3863  | 
|
| 13142 | 3864  | 
lemma replicate_add: "replicate (n + m) x = replicate n x @ replicate m x"  | 
| 13145 | 3865  | 
by (induct n) auto  | 
| 13114 | 3866  | 
|
| 16397 | 3867  | 
text{* Courtesy of Matthias Daum: *}
 | 
3868  | 
lemma append_replicate_commute:  | 
|
3869  | 
"replicate n x @ replicate k x = replicate k x @ replicate n x"  | 
|
3870  | 
apply (simp add: replicate_add [THEN sym])  | 
|
| 
57512
 
cc97b347b301
reduced name variants for assoc and commute on plus and mult
 
haftmann 
parents: 
57418 
diff
changeset
 | 
3871  | 
apply (simp add: add.commute)  | 
| 16397 | 3872  | 
done  | 
3873  | 
||
| 31080 | 3874  | 
text{* Courtesy of Andreas Lochbihler: *}
 | 
3875  | 
lemma filter_replicate:  | 
|
3876  | 
"filter P (replicate n x) = (if P x then replicate n x else [])"  | 
|
3877  | 
by(induct n) auto  | 
|
3878  | 
||
| 13142 | 3879  | 
lemma hd_replicate [simp]: "n \<noteq> 0 ==> hd (replicate n x) = x"  | 
| 13145 | 3880  | 
by (induct n) auto  | 
| 13114 | 3881  | 
|
| 
46500
 
0196966d6d2d
removing unnecessary premises in theorems of List theory
 
bulwahn 
parents: 
46448 
diff
changeset
 | 
3882  | 
lemma tl_replicate [simp]: "tl (replicate n x) = replicate (n - 1) x"  | 
| 13145 | 3883  | 
by (induct n) auto  | 
| 13114 | 3884  | 
|
| 13142 | 3885  | 
lemma last_replicate [simp]: "n \<noteq> 0 ==> last (replicate n x) = x"  | 
| 13145 | 3886  | 
by (atomize (full), induct n) auto  | 
| 13114 | 3887  | 
|
| 24526 | 3888  | 
lemma nth_replicate[simp]: "i < n ==> (replicate n x)!i = x"  | 
3889  | 
apply (induct n arbitrary: i, simp)  | 
|
| 13145 | 3890  | 
apply (simp add: nth_Cons split: nat.split)  | 
3891  | 
done  | 
|
| 13114 | 3892  | 
|
| 16397 | 3893  | 
text{* Courtesy of Matthias Daum (2 lemmas): *}
 | 
3894  | 
lemma take_replicate[simp]: "take i (replicate k x) = replicate (min i k) x"  | 
|
3895  | 
apply (case_tac "k \<le> i")  | 
|
3896  | 
apply (simp add: min_def)  | 
|
3897  | 
apply (drule not_leE)  | 
|
3898  | 
apply (simp add: min_def)  | 
|
3899  | 
apply (subgoal_tac "replicate k x = replicate i x @ replicate (k - i) x")  | 
|
3900  | 
apply simp  | 
|
3901  | 
apply (simp add: replicate_add [symmetric])  | 
|
3902  | 
done  | 
|
3903  | 
||
| 24526 | 3904  | 
lemma drop_replicate[simp]: "drop i (replicate k x) = replicate (k-i) x"  | 
3905  | 
apply (induct k arbitrary: i)  | 
|
| 16397 | 3906  | 
apply simp  | 
3907  | 
apply clarsimp  | 
|
3908  | 
apply (case_tac i)  | 
|
3909  | 
apply simp  | 
|
3910  | 
apply clarsimp  | 
|
3911  | 
done  | 
|
3912  | 
||
| 13142 | 3913  | 
lemma set_replicate_Suc: "set (replicate (Suc n) x) = {x}"
 | 
| 13145 | 3914  | 
by (induct n) auto  | 
| 13114 | 3915  | 
|
| 13142 | 3916  | 
lemma set_replicate [simp]: "n \<noteq> 0 ==> set (replicate n x) = {x}"
 | 
| 13145 | 3917  | 
by (fast dest!: not0_implies_Suc intro!: set_replicate_Suc)  | 
| 13114 | 3918  | 
|
| 13142 | 3919  | 
lemma set_replicate_conv_if: "set (replicate n x) = (if n = 0 then {} else {x})"
 | 
| 13145 | 3920  | 
by auto  | 
| 13114 | 3921  | 
|
| 37456 | 3922  | 
lemma in_set_replicate[simp]: "(x : set (replicate n y)) = (x = y & n \<noteq> 0)"  | 
3923  | 
by (simp add: set_replicate_conv_if)  | 
|
3924  | 
||
| 37454 | 3925  | 
lemma Ball_set_replicate[simp]:  | 
3926  | 
"(ALL x : set(replicate n a). P x) = (P a | n=0)"  | 
|
3927  | 
by(simp add: set_replicate_conv_if)  | 
|
3928  | 
||
3929  | 
lemma Bex_set_replicate[simp]:  | 
|
3930  | 
"(EX x : set(replicate n a). P x) = (P a & n\<noteq>0)"  | 
|
3931  | 
by(simp add: set_replicate_conv_if)  | 
|
| 13114 | 3932  | 
|
| 24796 | 3933  | 
lemma replicate_append_same:  | 
3934  | 
"replicate i x @ [x] = x # replicate i x"  | 
|
3935  | 
by (induct i) simp_all  | 
|
3936  | 
||
3937  | 
lemma map_replicate_trivial:  | 
|
3938  | 
"map (\<lambda>i. x) [0..<i] = replicate i x"  | 
|
3939  | 
by (induct i) (simp_all add: replicate_append_same)  | 
|
3940  | 
||
| 
31363
 
7493b571b37d
Added theorems about distinct & concat, map & replicate and concat & replicate
 
hoelzl 
parents: 
31264 
diff
changeset
 | 
3941  | 
lemma concat_replicate_trivial[simp]:  | 
| 
 
7493b571b37d
Added theorems about distinct & concat, map & replicate and concat & replicate
 
hoelzl 
parents: 
31264 
diff
changeset
 | 
3942  | 
"concat (replicate i []) = []"  | 
| 
 
7493b571b37d
Added theorems about distinct & concat, map & replicate and concat & replicate
 
hoelzl 
parents: 
31264 
diff
changeset
 | 
3943  | 
by (induct i) (auto simp add: map_replicate_const)  | 
| 13114 | 3944  | 
|
| 28642 | 3945  | 
lemma replicate_empty[simp]: "(replicate n x = []) \<longleftrightarrow> n=0"  | 
3946  | 
by (induct n) auto  | 
|
3947  | 
||
3948  | 
lemma empty_replicate[simp]: "([] = replicate n x) \<longleftrightarrow> n=0"  | 
|
3949  | 
by (induct n) auto  | 
|
3950  | 
||
3951  | 
lemma replicate_eq_replicate[simp]:  | 
|
3952  | 
"(replicate m x = replicate n y) \<longleftrightarrow> (m=n & (m\<noteq>0 \<longrightarrow> x=y))"  | 
|
3953  | 
apply(induct m arbitrary: n)  | 
|
3954  | 
apply simp  | 
|
3955  | 
apply(induct_tac n)  | 
|
3956  | 
apply auto  | 
|
3957  | 
done  | 
|
3958  | 
||
| 
39534
 
c798d4f1b682
generalized lemma insort_remove1 to insort_key_remove1
 
haftmann 
parents: 
39302 
diff
changeset
 | 
3959  | 
lemma replicate_length_filter:  | 
| 
 
c798d4f1b682
generalized lemma insort_remove1 to insort_key_remove1
 
haftmann 
parents: 
39302 
diff
changeset
 | 
3960  | 
"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
 | 
3961  | 
by (induct xs) auto  | 
| 
 
c798d4f1b682
generalized lemma insort_remove1 to insort_key_remove1
 
haftmann 
parents: 
39302 
diff
changeset
 | 
3962  | 
|
| 
42714
 
fcba668b0839
add a lemma about commutative append to List.thy
 
noschinl 
parents: 
42713 
diff
changeset
 | 
3963  | 
lemma comm_append_are_replicate:  | 
| 
 
fcba668b0839
add a lemma about commutative append to List.thy
 
noschinl 
parents: 
42713 
diff
changeset
 | 
3964  | 
fixes xs ys :: "'a list"  | 
| 
 
fcba668b0839
add a lemma about commutative append to List.thy
 
noschinl 
parents: 
42713 
diff
changeset
 | 
3965  | 
assumes "xs \<noteq> []" "ys \<noteq> []"  | 
| 
 
fcba668b0839
add a lemma about commutative append to List.thy
 
noschinl 
parents: 
42713 
diff
changeset
 | 
3966  | 
assumes "xs @ ys = ys @ xs"  | 
| 
 
fcba668b0839
add a lemma about commutative append to List.thy
 
noschinl 
parents: 
42713 
diff
changeset
 | 
3967  | 
shows "\<exists>m n zs. concat (replicate m zs) = xs \<and> concat (replicate n zs) = ys"  | 
| 
 
fcba668b0839
add a lemma about commutative append to List.thy
 
noschinl 
parents: 
42713 
diff
changeset
 | 
3968  | 
using assms  | 
| 
 
fcba668b0839
add a lemma about commutative append to List.thy
 
noschinl 
parents: 
42713 
diff
changeset
 | 
3969  | 
proof (induct "length (xs @ ys)" arbitrary: xs ys rule: less_induct)  | 
| 
 
fcba668b0839
add a lemma about commutative append to List.thy
 
noschinl 
parents: 
42713 
diff
changeset
 | 
3970  | 
case less  | 
| 
 
fcba668b0839
add a lemma about commutative append to List.thy
 
noschinl 
parents: 
42713 
diff
changeset
 | 
3971  | 
|
| 
 
fcba668b0839
add a lemma about commutative append to List.thy
 
noschinl 
parents: 
42713 
diff
changeset
 | 
3972  | 
def xs' \<equiv> "if (length xs \<le> length ys) then xs else ys"  | 
| 
 
fcba668b0839
add a lemma about commutative append to List.thy
 
noschinl 
parents: 
42713 
diff
changeset
 | 
3973  | 
and ys' \<equiv> "if (length xs \<le> length ys) then ys else xs"  | 
| 
 
fcba668b0839
add a lemma about commutative append to List.thy
 
noschinl 
parents: 
42713 
diff
changeset
 | 
3974  | 
then have  | 
| 
 
fcba668b0839
add a lemma about commutative append to List.thy
 
noschinl 
parents: 
42713 
diff
changeset
 | 
3975  | 
prems': "length xs' \<le> length ys'"  | 
| 
 
fcba668b0839
add a lemma about commutative append to List.thy
 
noschinl 
parents: 
42713 
diff
changeset
 | 
3976  | 
"xs' @ ys' = ys' @ xs'"  | 
| 
 
fcba668b0839
add a lemma about commutative append to List.thy
 
noschinl 
parents: 
42713 
diff
changeset
 | 
3977  | 
and "xs' \<noteq> []"  | 
| 
 
fcba668b0839
add a lemma about commutative append to List.thy
 
noschinl 
parents: 
42713 
diff
changeset
 | 
3978  | 
and len: "length (xs @ ys) = length (xs' @ ys')"  | 
| 
 
fcba668b0839
add a lemma about commutative append to List.thy
 
noschinl 
parents: 
42713 
diff
changeset
 | 
3979  | 
using less by (auto intro: less.hyps)  | 
| 
 
fcba668b0839
add a lemma about commutative append to List.thy
 
noschinl 
parents: 
42713 
diff
changeset
 | 
3980  | 
|
| 
 
fcba668b0839
add a lemma about commutative append to List.thy
 
noschinl 
parents: 
42713 
diff
changeset
 | 
3981  | 
from prems'  | 
| 
 
fcba668b0839
add a lemma about commutative append to List.thy
 
noschinl 
parents: 
42713 
diff
changeset
 | 
3982  | 
obtain ws where "ys' = xs' @ ws"  | 
| 
 
fcba668b0839
add a lemma about commutative append to List.thy
 
noschinl 
parents: 
42713 
diff
changeset
 | 
3983  | 
by (auto simp: append_eq_append_conv2)  | 
| 
 
fcba668b0839
add a lemma about commutative append to List.thy
 
noschinl 
parents: 
42713 
diff
changeset
 | 
3984  | 
|
| 
 
fcba668b0839
add a lemma about commutative append to List.thy
 
noschinl 
parents: 
42713 
diff
changeset
 | 
3985  | 
have "\<exists>m n zs. concat (replicate m zs) = xs' \<and> concat (replicate n zs) = ys'"  | 
| 
 
fcba668b0839
add a lemma about commutative append to List.thy
 
noschinl 
parents: 
42713 
diff
changeset
 | 
3986  | 
proof (cases "ws = []")  | 
| 
 
fcba668b0839
add a lemma about commutative append to List.thy
 
noschinl 
parents: 
42713 
diff
changeset
 | 
3987  | 
case True  | 
| 
 
fcba668b0839
add a lemma about commutative append to List.thy
 
noschinl 
parents: 
42713 
diff
changeset
 | 
3988  | 
then have "concat (replicate 1 xs') = xs'"  | 
| 
 
fcba668b0839
add a lemma about commutative append to List.thy
 
noschinl 
parents: 
42713 
diff
changeset
 | 
3989  | 
and "concat (replicate 1 xs') = ys'"  | 
| 
 
fcba668b0839
add a lemma about commutative append to List.thy
 
noschinl 
parents: 
42713 
diff
changeset
 | 
3990  | 
using `ys' = xs' @ ws` by auto  | 
| 
 
fcba668b0839
add a lemma about commutative append to List.thy
 
noschinl 
parents: 
42713 
diff
changeset
 | 
3991  | 
then show ?thesis by blast  | 
| 
 
fcba668b0839
add a lemma about commutative append to List.thy
 
noschinl 
parents: 
42713 
diff
changeset
 | 
3992  | 
next  | 
| 
 
fcba668b0839
add a lemma about commutative append to List.thy
 
noschinl 
parents: 
42713 
diff
changeset
 | 
3993  | 
case False  | 
| 
 
fcba668b0839
add a lemma about commutative append to List.thy
 
noschinl 
parents: 
42713 
diff
changeset
 | 
3994  | 
from `ys' = xs' @ ws` and `xs' @ ys' = ys' @ xs'`  | 
| 
 
fcba668b0839
add a lemma about commutative append to List.thy
 
noschinl 
parents: 
42713 
diff
changeset
 | 
3995  | 
have "xs' @ ws = ws @ xs'" by simp  | 
| 
 
fcba668b0839
add a lemma about commutative append to List.thy
 
noschinl 
parents: 
42713 
diff
changeset
 | 
3996  | 
then have "\<exists>m n zs. concat (replicate m zs) = xs' \<and> concat (replicate n zs) = ws"  | 
| 
 
fcba668b0839
add a lemma about commutative append to List.thy
 
noschinl 
parents: 
42713 
diff
changeset
 | 
3997  | 
using False and `xs' \<noteq> []` and `ys' = xs' @ ws` and len  | 
| 
 
fcba668b0839
add a lemma about commutative append to List.thy
 
noschinl 
parents: 
42713 
diff
changeset
 | 
3998  | 
by (intro less.hyps) auto  | 
| 
53374
 
a14d2a854c02
tuned proofs -- clarified flow of facts wrt. calculation;
 
wenzelm 
parents: 
53017 
diff
changeset
 | 
3999  | 
then obtain m n zs where *: "concat (replicate m zs) = xs'"  | 
| 
42714
 
fcba668b0839
add a lemma about commutative append to List.thy
 
noschinl 
parents: 
42713 
diff
changeset
 | 
4000  | 
and "concat (replicate n zs) = ws" by blast  | 
| 
 
fcba668b0839
add a lemma about commutative append to List.thy
 
noschinl 
parents: 
42713 
diff
changeset
 | 
4001  | 
then have "concat (replicate (m + n) zs) = ys'"  | 
| 
 
fcba668b0839
add a lemma about commutative append to List.thy
 
noschinl 
parents: 
42713 
diff
changeset
 | 
4002  | 
using `ys' = xs' @ ws`  | 
| 
 
fcba668b0839
add a lemma about commutative append to List.thy
 
noschinl 
parents: 
42713 
diff
changeset
 | 
4003  | 
by (simp add: replicate_add)  | 
| 
53374
 
a14d2a854c02
tuned proofs -- clarified flow of facts wrt. calculation;
 
wenzelm 
parents: 
53017 
diff
changeset
 | 
4004  | 
with * show ?thesis by blast  | 
| 
42714
 
fcba668b0839
add a lemma about commutative append to List.thy
 
noschinl 
parents: 
42713 
diff
changeset
 | 
4005  | 
qed  | 
| 
 
fcba668b0839
add a lemma about commutative append to List.thy
 
noschinl 
parents: 
42713 
diff
changeset
 | 
4006  | 
then show ?case  | 
| 56085 | 4007  | 
using xs'_def ys'_def by meson  | 
| 
42714
 
fcba668b0839
add a lemma about commutative append to List.thy
 
noschinl 
parents: 
42713 
diff
changeset
 | 
4008  | 
qed  | 
| 
 
fcba668b0839
add a lemma about commutative append to List.thy
 
noschinl 
parents: 
42713 
diff
changeset
 | 
4009  | 
|
| 
 
fcba668b0839
add a lemma about commutative append to List.thy
 
noschinl 
parents: 
42713 
diff
changeset
 | 
4010  | 
lemma comm_append_is_replicate:  | 
| 
 
fcba668b0839
add a lemma about commutative append to List.thy
 
noschinl 
parents: 
42713 
diff
changeset
 | 
4011  | 
fixes xs ys :: "'a list"  | 
| 
 
fcba668b0839
add a lemma about commutative append to List.thy
 
noschinl 
parents: 
42713 
diff
changeset
 | 
4012  | 
assumes "xs \<noteq> []" "ys \<noteq> []"  | 
| 
 
fcba668b0839
add a lemma about commutative append to List.thy
 
noschinl 
parents: 
42713 
diff
changeset
 | 
4013  | 
assumes "xs @ ys = ys @ xs"  | 
| 
 
fcba668b0839
add a lemma about commutative append to List.thy
 
noschinl 
parents: 
42713 
diff
changeset
 | 
4014  | 
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
 | 
4015  | 
|
| 
 
fcba668b0839
add a lemma about commutative append to List.thy
 
noschinl 
parents: 
42713 
diff
changeset
 | 
4016  | 
proof -  | 
| 
 
fcba668b0839
add a lemma about commutative append to List.thy
 
noschinl 
parents: 
42713 
diff
changeset
 | 
4017  | 
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
 | 
4018  | 
and "concat (replicate n zs) = ys"  | 
| 56085 | 4019  | 
using comm_append_are_replicate[of xs ys, OF assms] by blast  | 
| 
42714
 
fcba668b0839
add a lemma about commutative append to List.thy
 
noschinl 
parents: 
42713 
diff
changeset
 | 
4020  | 
then have "m + n > 1" and "concat (replicate (m+n) zs) = xs @ ys"  | 
| 
 
fcba668b0839
add a lemma about commutative append to List.thy
 
noschinl 
parents: 
42713 
diff
changeset
 | 
4021  | 
using `xs \<noteq> []` and `ys \<noteq> []`  | 
| 
 
fcba668b0839
add a lemma about commutative append to List.thy
 
noschinl 
parents: 
42713 
diff
changeset
 | 
4022  | 
by (auto simp: replicate_add)  | 
| 
 
fcba668b0839
add a lemma about commutative append to List.thy
 
noschinl 
parents: 
42713 
diff
changeset
 | 
4023  | 
then show ?thesis by blast  | 
| 
 
fcba668b0839
add a lemma about commutative append to List.thy
 
noschinl 
parents: 
42713 
diff
changeset
 | 
4024  | 
qed  | 
| 
 
fcba668b0839
add a lemma about commutative append to List.thy
 
noschinl 
parents: 
42713 
diff
changeset
 | 
4025  | 
|
| 52380 | 4026  | 
lemma Cons_replicate_eq:  | 
4027  | 
"x # xs = replicate n y \<longleftrightarrow> x = y \<and> n > 0 \<and> xs = replicate (n - 1) x"  | 
|
4028  | 
by (induct n) auto  | 
|
4029  | 
||
4030  | 
lemma replicate_length_same:  | 
|
4031  | 
"(\<forall>y\<in>set xs. y = x) \<Longrightarrow> replicate (length xs) x = xs"  | 
|
4032  | 
by (induct xs) simp_all  | 
|
4033  | 
||
4034  | 
lemma foldr_replicate [simp]:  | 
|
4035  | 
"foldr f (replicate n x) = f x ^^ n"  | 
|
4036  | 
by (induct n) (simp_all)  | 
|
4037  | 
||
4038  | 
lemma fold_replicate [simp]:  | 
|
4039  | 
"fold f (replicate n x) = f x ^^ n"  | 
|
4040  | 
by (subst foldr_fold [symmetric]) simp_all  | 
|
4041  | 
||
| 28642 | 4042  | 
|
| 51173 | 4043  | 
subsubsection {* @{const enumerate} *}
 | 
4044  | 
||
4045  | 
lemma enumerate_simps [simp, code]:  | 
|
4046  | 
"enumerate n [] = []"  | 
|
4047  | 
"enumerate n (x # xs) = (n, x) # enumerate (Suc n) xs"  | 
|
4048  | 
apply (auto simp add: enumerate_eq_zip not_le)  | 
|
4049  | 
apply (cases "n < n + length xs")  | 
|
4050  | 
apply (auto simp add: upt_conv_Cons)  | 
|
4051  | 
done  | 
|
4052  | 
||
4053  | 
lemma length_enumerate [simp]:  | 
|
4054  | 
"length (enumerate n xs) = length xs"  | 
|
4055  | 
by (simp add: enumerate_eq_zip)  | 
|
4056  | 
||
4057  | 
lemma map_fst_enumerate [simp]:  | 
|
4058  | 
"map fst (enumerate n xs) = [n..<n + length xs]"  | 
|
4059  | 
by (simp add: enumerate_eq_zip)  | 
|
4060  | 
||
4061  | 
lemma map_snd_enumerate [simp]:  | 
|
4062  | 
"map snd (enumerate n xs) = xs"  | 
|
4063  | 
by (simp add: enumerate_eq_zip)  | 
|
4064  | 
||
4065  | 
lemma in_set_enumerate_eq:  | 
|
4066  | 
"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"  | 
|
4067  | 
proof -  | 
|
4068  | 
  { fix m
 | 
|
4069  | 
assume "n \<le> m"  | 
|
4070  | 
moreover assume "m < length xs + n"  | 
|
4071  | 
ultimately have "[n..<n + length xs] ! (m - n) = m \<and>  | 
|
4072  | 
xs ! (m - n) = xs ! (m - n) \<and> m - n < length xs" by auto  | 
|
4073  | 
then have "\<exists>q. [n..<n + length xs] ! q = m \<and>  | 
|
4074  | 
xs ! q = xs ! (m - n) \<and> q < length xs" ..  | 
|
4075  | 
} then show ?thesis by (cases p) (auto simp add: enumerate_eq_zip in_set_zip)  | 
|
4076  | 
qed  | 
|
4077  | 
||
4078  | 
lemma nth_enumerate_eq:  | 
|
4079  | 
assumes "m < length xs"  | 
|
4080  | 
shows "enumerate n xs ! m = (n + m, xs ! m)"  | 
|
4081  | 
using assms by (simp add: enumerate_eq_zip)  | 
|
4082  | 
||
4083  | 
lemma enumerate_replicate_eq:  | 
|
4084  | 
"enumerate n (replicate m a) = map (\<lambda>q. (q, a)) [n..<n + m]"  | 
|
4085  | 
by (rule pair_list_eqI)  | 
|
4086  | 
(simp_all add: enumerate_eq_zip comp_def map_replicate_const)  | 
|
4087  | 
||
4088  | 
lemma enumerate_Suc_eq:  | 
|
4089  | 
"enumerate (Suc n) xs = map (apfst Suc) (enumerate n xs)"  | 
|
4090  | 
by (rule pair_list_eqI)  | 
|
4091  | 
(simp_all add: not_le, simp del: map_map [simp del] add: map_Suc_upt map_map [symmetric])  | 
|
4092  | 
||
| 52379 | 4093  | 
lemma distinct_enumerate [simp]:  | 
4094  | 
"distinct (enumerate n xs)"  | 
|
4095  | 
by (simp add: enumerate_eq_zip distinct_zipI1)  | 
|
4096  | 
||
| 51173 | 4097  | 
|
| 
49948
 
744934b818c7
moved quite generic material from theory Enum to more appropriate places
 
haftmann 
parents: 
49808 
diff
changeset
 | 
4098  | 
subsubsection {* @{const rotate1} and @{const rotate} *}
 | 
| 15302 | 4099  | 
|
4100  | 
lemma rotate0[simp]: "rotate 0 = id"  | 
|
4101  | 
by(simp add:rotate_def)  | 
|
4102  | 
||
4103  | 
lemma rotate_Suc[simp]: "rotate (Suc n) xs = rotate1(rotate n xs)"  | 
|
4104  | 
by(simp add:rotate_def)  | 
|
4105  | 
||
4106  | 
lemma rotate_add:  | 
|
4107  | 
"rotate (m+n) = rotate m o rotate n"  | 
|
4108  | 
by(simp add:rotate_def funpow_add)  | 
|
4109  | 
||
4110  | 
lemma rotate_rotate: "rotate m (rotate n xs) = rotate (m+n) xs"  | 
|
4111  | 
by(simp add:rotate_add)  | 
|
4112  | 
||
| 18049 | 4113  | 
lemma rotate1_rotate_swap: "rotate1 (rotate n xs) = rotate n (rotate1 xs)"  | 
4114  | 
by(simp add:rotate_def funpow_swap1)  | 
|
4115  | 
||
| 15302 | 4116  | 
lemma rotate1_length01[simp]: "length xs <= 1 \<Longrightarrow> rotate1 xs = xs"  | 
4117  | 
by(cases xs) simp_all  | 
|
4118  | 
||
4119  | 
lemma rotate_length01[simp]: "length xs <= 1 \<Longrightarrow> rotate n xs = xs"  | 
|
4120  | 
apply(induct n)  | 
|
4121  | 
apply simp  | 
|
4122  | 
apply (simp add:rotate_def)  | 
|
| 13145 | 4123  | 
done  | 
| 13114 | 4124  | 
|
| 15302 | 4125  | 
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
 | 
4126  | 
by (cases xs) simp_all  | 
| 15302 | 4127  | 
|
4128  | 
lemma rotate_drop_take:  | 
|
4129  | 
"rotate n xs = drop (n mod length xs) xs @ take (n mod length xs) xs"  | 
|
4130  | 
apply(induct n)  | 
|
4131  | 
apply simp  | 
|
4132  | 
apply(simp add:rotate_def)  | 
|
4133  | 
apply(cases "xs = []")  | 
|
4134  | 
apply (simp)  | 
|
4135  | 
apply(case_tac "n mod length xs = 0")  | 
|
4136  | 
apply(simp add:mod_Suc)  | 
|
4137  | 
apply(simp add: rotate1_hd_tl drop_Suc take_Suc)  | 
|
4138  | 
apply(simp add:mod_Suc rotate1_hd_tl drop_Suc[symmetric] drop_tl[symmetric]  | 
|
4139  | 
take_hd_drop linorder_not_le)  | 
|
| 13145 | 4140  | 
done  | 
| 13114 | 4141  | 
|
| 15302 | 4142  | 
lemma rotate_conv_mod: "rotate n xs = rotate (n mod length xs) xs"  | 
4143  | 
by(simp add:rotate_drop_take)  | 
|
4144  | 
||
4145  | 
lemma rotate_id[simp]: "n mod length xs = 0 \<Longrightarrow> rotate n xs = xs"  | 
|
4146  | 
by(simp add:rotate_drop_take)  | 
|
4147  | 
||
4148  | 
lemma length_rotate1[simp]: "length(rotate1 xs) = length xs"  | 
|
| 
46440
 
d4994e2e7364
use 'primrec' to define "rotate1", for uniformity (and to help first-order tools that rely on "Spec_Rules")
 
blanchet 
parents: 
46439 
diff
changeset
 | 
4149  | 
by (cases xs) simp_all  | 
| 15302 | 4150  | 
|
| 24526 | 4151  | 
lemma length_rotate[simp]: "length(rotate n xs) = length xs"  | 
4152  | 
by (induct n arbitrary: xs) (simp_all add:rotate_def)  | 
|
| 15302 | 4153  | 
|
4154  | 
lemma distinct1_rotate[simp]: "distinct(rotate1 xs) = distinct xs"  | 
|
| 
46440
 
d4994e2e7364
use 'primrec' to define "rotate1", for uniformity (and to help first-order tools that rely on "Spec_Rules")
 
blanchet 
parents: 
46439 
diff
changeset
 | 
4155  | 
by (cases xs) auto  | 
| 15302 | 4156  | 
|
4157  | 
lemma distinct_rotate[simp]: "distinct(rotate n xs) = distinct xs"  | 
|
4158  | 
by (induct n) (simp_all add:rotate_def)  | 
|
4159  | 
||
4160  | 
lemma rotate_map: "rotate n (map f xs) = map f (rotate n xs)"  | 
|
4161  | 
by(simp add:rotate_drop_take take_map drop_map)  | 
|
4162  | 
||
4163  | 
lemma set_rotate1[simp]: "set(rotate1 xs) = set xs"  | 
|
| 
46440
 
d4994e2e7364
use 'primrec' to define "rotate1", for uniformity (and to help first-order tools that rely on "Spec_Rules")
 
blanchet 
parents: 
46439 
diff
changeset
 | 
4164  | 
by (cases xs) auto  | 
| 15302 | 4165  | 
|
4166  | 
lemma set_rotate[simp]: "set(rotate n xs) = set xs"  | 
|
4167  | 
by (induct n) (simp_all add:rotate_def)  | 
|
4168  | 
||
4169  | 
lemma rotate1_is_Nil_conv[simp]: "(rotate1 xs = []) = (xs = [])"  | 
|
| 
46440
 
d4994e2e7364
use 'primrec' to define "rotate1", for uniformity (and to help first-order tools that rely on "Spec_Rules")
 
blanchet 
parents: 
46439 
diff
changeset
 | 
4170  | 
by (cases xs) auto  | 
| 15302 | 4171  | 
|
4172  | 
lemma rotate_is_Nil_conv[simp]: "(rotate n xs = []) = (xs = [])"  | 
|
4173  | 
by (induct n) (simp_all add:rotate_def)  | 
|
| 13114 | 4174  | 
|
| 15439 | 4175  | 
lemma rotate_rev:  | 
4176  | 
"rotate n (rev xs) = rev(rotate (length xs - (n mod length xs)) xs)"  | 
|
4177  | 
apply(simp add:rotate_drop_take rev_drop rev_take)  | 
|
4178  | 
apply(cases "length xs = 0")  | 
|
4179  | 
apply simp  | 
|
4180  | 
apply(cases "n mod length xs = 0")  | 
|
4181  | 
apply simp  | 
|
4182  | 
apply(simp add:rotate_drop_take rev_drop rev_take)  | 
|
4183  | 
done  | 
|
4184  | 
||
| 18423 | 4185  | 
lemma hd_rotate_conv_nth: "xs \<noteq> [] \<Longrightarrow> hd(rotate n xs) = xs!(n mod length xs)"  | 
4186  | 
apply(simp add:rotate_drop_take hd_append hd_drop_conv_nth hd_conv_nth)  | 
|
4187  | 
apply(subgoal_tac "length xs \<noteq> 0")  | 
|
4188  | 
prefer 2 apply simp  | 
|
4189  | 
using mod_less_divisor[of "length xs" n] by arith  | 
|
4190  | 
||
| 13114 | 4191  | 
|
| 
49948
 
744934b818c7
moved quite generic material from theory Enum to more appropriate places
 
haftmann 
parents: 
49808 
diff
changeset
 | 
4192  | 
subsubsection {* @{const sublist} --- a generalization of @{const nth} to sets *}
 | 
| 13114 | 4193  | 
|
| 13142 | 4194  | 
lemma sublist_empty [simp]: "sublist xs {} = []"
 | 
| 13145 | 4195  | 
by (auto simp add: sublist_def)  | 
| 13114 | 4196  | 
|
| 13142 | 4197  | 
lemma sublist_nil [simp]: "sublist [] A = []"  | 
| 13145 | 4198  | 
by (auto simp add: sublist_def)  | 
| 13114 | 4199  | 
|
| 15281 | 4200  | 
lemma length_sublist:  | 
4201  | 
  "length(sublist xs I) = card{i. i < length xs \<and> i : I}"
 | 
|
4202  | 
by(simp add: sublist_def length_filter_conv_card cong:conj_cong)  | 
|
4203  | 
||
4204  | 
lemma sublist_shift_lemma_Suc:  | 
|
| 24526 | 4205  | 
"map fst (filter (%p. P(Suc(snd p))) (zip xs is)) =  | 
4206  | 
map fst (filter (%p. P(snd p)) (zip xs (map Suc is)))"  | 
|
4207  | 
apply(induct xs arbitrary: "is")  | 
|
| 15281 | 4208  | 
apply simp  | 
4209  | 
apply (case_tac "is")  | 
|
4210  | 
apply simp  | 
|
4211  | 
apply simp  | 
|
4212  | 
done  | 
|
4213  | 
||
| 13114 | 4214  | 
lemma sublist_shift_lemma:  | 
| 
23279
 
e39dd93161d9
tuned list comprehension, changed filter syntax from : to <-
 
nipkow 
parents: 
23246 
diff
changeset
 | 
4215  | 
"map fst [p<-zip xs [i..<i + length xs] . snd p : A] =  | 
| 
 
e39dd93161d9
tuned list comprehension, changed filter syntax from : to <-
 
nipkow 
parents: 
23246 
diff
changeset
 | 
4216  | 
map fst [p<-zip xs [0..<length xs] . snd p + i : A]"  | 
| 
57512
 
cc97b347b301
reduced name variants for assoc and commute on plus and mult
 
haftmann 
parents: 
57418 
diff
changeset
 | 
4217  | 
by (induct xs rule: rev_induct) (simp_all add: add.commute)  | 
| 13114 | 4218  | 
|
4219  | 
lemma sublist_append:  | 
|
| 15168 | 4220  | 
     "sublist (l @ l') A = sublist l A @ sublist l' {j. j + length l : A}"
 | 
| 13145 | 4221  | 
apply (unfold sublist_def)  | 
| 14208 | 4222  | 
apply (induct l' rule: rev_induct, simp)  | 
| 44921 | 4223  | 
apply (simp add: upt_add_eq_append[of 0] sublist_shift_lemma)  | 
| 
57512
 
cc97b347b301
reduced name variants for assoc and commute on plus and mult
 
haftmann 
parents: 
57418 
diff
changeset
 | 
4224  | 
apply (simp add: add.commute)  | 
| 13145 | 4225  | 
done  | 
| 13114 | 4226  | 
|
4227  | 
lemma sublist_Cons:  | 
|
| 13145 | 4228  | 
"sublist (x # l) A = (if 0:A then [x] else []) @ sublist l {j. Suc j : A}"
 | 
4229  | 
apply (induct l rule: rev_induct)  | 
|
4230  | 
apply (simp add: sublist_def)  | 
|
4231  | 
apply (simp del: append_Cons add: append_Cons[symmetric] sublist_append)  | 
|
4232  | 
done  | 
|
| 13114 | 4233  | 
|
| 24526 | 4234  | 
lemma set_sublist: "set(sublist xs I) = {xs!i|i. i<size xs \<and> i \<in> I}"
 | 
4235  | 
apply(induct xs arbitrary: I)  | 
|
| 25162 | 4236  | 
apply(auto simp: sublist_Cons nth_Cons split:nat.split dest!: gr0_implies_Suc)  | 
| 15281 | 4237  | 
done  | 
4238  | 
||
4239  | 
lemma set_sublist_subset: "set(sublist xs I) \<subseteq> set xs"  | 
|
4240  | 
by(auto simp add:set_sublist)  | 
|
4241  | 
||
4242  | 
lemma notin_set_sublistI[simp]: "x \<notin> set xs \<Longrightarrow> x \<notin> set(sublist xs I)"  | 
|
4243  | 
by(auto simp add:set_sublist)  | 
|
4244  | 
||
4245  | 
lemma in_set_sublistD: "x \<in> set(sublist xs I) \<Longrightarrow> x \<in> set xs"  | 
|
4246  | 
by(auto simp add:set_sublist)  | 
|
4247  | 
||
| 13142 | 4248  | 
lemma sublist_singleton [simp]: "sublist [x] A = (if 0 : A then [x] else [])"  | 
| 13145 | 4249  | 
by (simp add: sublist_Cons)  | 
| 13114 | 4250  | 
|
| 15281 | 4251  | 
|
| 24526 | 4252  | 
lemma distinct_sublistI[simp]: "distinct xs \<Longrightarrow> distinct(sublist xs I)"  | 
4253  | 
apply(induct xs arbitrary: I)  | 
|
| 15281 | 4254  | 
apply simp  | 
4255  | 
apply(auto simp add:sublist_Cons)  | 
|
4256  | 
done  | 
|
4257  | 
||
4258  | 
||
| 15045 | 4259  | 
lemma sublist_upt_eq_take [simp]: "sublist l {..<n} = take n l"
 | 
| 14208 | 4260  | 
apply (induct l rule: rev_induct, simp)  | 
| 13145 | 4261  | 
apply (simp split: nat_diff_split add: sublist_append)  | 
4262  | 
done  | 
|
| 13114 | 4263  | 
|
| 24526 | 4264  | 
lemma filter_in_sublist:  | 
4265  | 
"distinct xs \<Longrightarrow> filter (%x. x \<in> set(sublist xs s)) xs = sublist xs s"  | 
|
4266  | 
proof (induct xs arbitrary: s)  | 
|
| 17501 | 4267  | 
case Nil thus ?case by simp  | 
4268  | 
next  | 
|
4269  | 
case (Cons a xs)  | 
|
| 
53374
 
a14d2a854c02
tuned proofs -- clarified flow of facts wrt. calculation;
 
wenzelm 
parents: 
53017 
diff
changeset
 | 
4270  | 
then have "!x. x: set xs \<longrightarrow> x \<noteq> a" by auto  | 
| 
 
a14d2a854c02
tuned proofs -- clarified flow of facts wrt. calculation;
 
wenzelm 
parents: 
53017 
diff
changeset
 | 
4271  | 
with Cons show ?case by(simp add: sublist_Cons cong:filter_cong)  | 
| 17501 | 4272  | 
qed  | 
4273  | 
||
| 13114 | 4274  | 
|
| 
49948
 
744934b818c7
moved quite generic material from theory Enum to more appropriate places
 
haftmann 
parents: 
49808 
diff
changeset
 | 
4275  | 
subsubsection {* @{const sublists} and @{const List.n_lists} *}
 | 
| 
 
744934b818c7
moved quite generic material from theory Enum to more appropriate places
 
haftmann 
parents: 
49808 
diff
changeset
 | 
4276  | 
|
| 
 
744934b818c7
moved quite generic material from theory Enum to more appropriate places
 
haftmann 
parents: 
49808 
diff
changeset
 | 
4277  | 
lemma length_sublists:  | 
| 
 
744934b818c7
moved quite generic material from theory Enum to more appropriate places
 
haftmann 
parents: 
49808 
diff
changeset
 | 
4278  | 
"length (sublists xs) = 2 ^ length xs"  | 
| 
 
744934b818c7
moved quite generic material from theory Enum to more appropriate places
 
haftmann 
parents: 
49808 
diff
changeset
 | 
4279  | 
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
 | 
4280  | 
|
| 
 
744934b818c7
moved quite generic material from theory Enum to more appropriate places
 
haftmann 
parents: 
49808 
diff
changeset
 | 
4281  | 
lemma sublists_powset:  | 
| 
 
744934b818c7
moved quite generic material from theory Enum to more appropriate places
 
haftmann 
parents: 
49808 
diff
changeset
 | 
4282  | 
"set ` set (sublists xs) = Pow (set xs)"  | 
| 
 
744934b818c7
moved quite generic material from theory Enum to more appropriate places
 
haftmann 
parents: 
49808 
diff
changeset
 | 
4283  | 
proof -  | 
| 
 
744934b818c7
moved quite generic material from theory Enum to more appropriate places
 
haftmann 
parents: 
49808 
diff
changeset
 | 
4284  | 
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
 | 
4285  | 
by (auto simp add: image_def)  | 
| 
 
744934b818c7
moved quite generic material from theory Enum to more appropriate places
 
haftmann 
parents: 
49808 
diff
changeset
 | 
4286  | 
have "set (map set (sublists xs)) = Pow (set xs)"  | 
| 
 
744934b818c7
moved quite generic material from theory Enum to more appropriate places
 
haftmann 
parents: 
49808 
diff
changeset
 | 
4287  | 
by (induct xs)  | 
| 
 
744934b818c7
moved quite generic material from theory Enum to more appropriate places
 
haftmann 
parents: 
49808 
diff
changeset
 | 
4288  | 
(simp_all add: aux Let_def Pow_insert Un_commute comp_def del: map_map)  | 
| 
 
744934b818c7
moved quite generic material from theory Enum to more appropriate places
 
haftmann 
parents: 
49808 
diff
changeset
 | 
4289  | 
then show ?thesis by simp  | 
| 
 
744934b818c7
moved quite generic material from theory Enum to more appropriate places
 
haftmann 
parents: 
49808 
diff
changeset
 | 
4290  | 
qed  | 
| 
 
744934b818c7
moved quite generic material from theory Enum to more appropriate places
 
haftmann 
parents: 
49808 
diff
changeset
 | 
4291  | 
|
| 
 
744934b818c7
moved quite generic material from theory Enum to more appropriate places
 
haftmann 
parents: 
49808 
diff
changeset
 | 
4292  | 
lemma distinct_set_sublists:  | 
| 
 
744934b818c7
moved quite generic material from theory Enum to more appropriate places
 
haftmann 
parents: 
49808 
diff
changeset
 | 
4293  | 
assumes "distinct xs"  | 
| 
 
744934b818c7
moved quite generic material from theory Enum to more appropriate places
 
haftmann 
parents: 
49808 
diff
changeset
 | 
4294  | 
shows "distinct (map set (sublists xs))"  | 
| 
 
744934b818c7
moved quite generic material from theory Enum to more appropriate places
 
haftmann 
parents: 
49808 
diff
changeset
 | 
4295  | 
proof (rule card_distinct)  | 
| 
 
744934b818c7
moved quite generic material from theory Enum to more appropriate places
 
haftmann 
parents: 
49808 
diff
changeset
 | 
4296  | 
have "finite (set xs)" by rule  | 
| 
 
744934b818c7
moved quite generic material from theory Enum to more appropriate places
 
haftmann 
parents: 
49808 
diff
changeset
 | 
4297  | 
then have "card (Pow (set xs)) = 2 ^ card (set xs)" by (rule card_Pow)  | 
| 
 
744934b818c7
moved quite generic material from theory Enum to more appropriate places
 
haftmann 
parents: 
49808 
diff
changeset
 | 
4298  | 
with assms distinct_card [of xs]  | 
| 
 
744934b818c7
moved quite generic material from theory Enum to more appropriate places
 
haftmann 
parents: 
49808 
diff
changeset
 | 
4299  | 
have "card (Pow (set xs)) = 2 ^ length xs" by simp  | 
| 
 
744934b818c7
moved quite generic material from theory Enum to more appropriate places
 
haftmann 
parents: 
49808 
diff
changeset
 | 
4300  | 
then show "card (set (map set (sublists xs))) = length (map set (sublists xs))"  | 
| 
 
744934b818c7
moved quite generic material from theory Enum to more appropriate places
 
haftmann 
parents: 
49808 
diff
changeset
 | 
4301  | 
by (simp add: sublists_powset length_sublists)  | 
| 
 
744934b818c7
moved quite generic material from theory Enum to more appropriate places
 
haftmann 
parents: 
49808 
diff
changeset
 | 
4302  | 
qed  | 
| 
 
744934b818c7
moved quite generic material from theory Enum to more appropriate places
 
haftmann 
parents: 
49808 
diff
changeset
 | 
4303  | 
|
| 
 
744934b818c7
moved quite generic material from theory Enum to more appropriate places
 
haftmann 
parents: 
49808 
diff
changeset
 | 
4304  | 
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
 | 
4305  | 
by (induct n) simp_all  | 
| 
 
744934b818c7
moved quite generic material from theory Enum to more appropriate places
 
haftmann 
parents: 
49808 
diff
changeset
 | 
4306  | 
|
| 
 
744934b818c7
moved quite generic material from theory Enum to more appropriate places
 
haftmann 
parents: 
49808 
diff
changeset
 | 
4307  | 
lemma length_n_lists: "length (List.n_lists n xs) = length xs ^ n"  | 
| 
 
744934b818c7
moved quite generic material from theory Enum to more appropriate places
 
haftmann 
parents: 
49808 
diff
changeset
 | 
4308  | 
by (induct n) (auto simp add: length_concat o_def listsum_triv)  | 
| 
 
744934b818c7
moved quite generic material from theory Enum to more appropriate places
 
haftmann 
parents: 
49808 
diff
changeset
 | 
4309  | 
|
| 
 
744934b818c7
moved quite generic material from theory Enum to more appropriate places
 
haftmann 
parents: 
49808 
diff
changeset
 | 
4310  | 
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
 | 
4311  | 
by (induct n arbitrary: ys) auto  | 
| 
 
744934b818c7
moved quite generic material from theory Enum to more appropriate places
 
haftmann 
parents: 
49808 
diff
changeset
 | 
4312  | 
|
| 
 
744934b818c7
moved quite generic material from theory Enum to more appropriate places
 
haftmann 
parents: 
49808 
diff
changeset
 | 
4313  | 
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
 | 
4314  | 
proof (rule set_eqI)  | 
| 
 
744934b818c7
moved quite generic material from theory Enum to more appropriate places
 
haftmann 
parents: 
49808 
diff
changeset
 | 
4315  | 
fix ys :: "'a list"  | 
| 
 
744934b818c7
moved quite generic material from theory Enum to more appropriate places
 
haftmann 
parents: 
49808 
diff
changeset
 | 
4316  | 
  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
 | 
4317  | 
proof -  | 
| 
 
744934b818c7
moved quite generic material from theory Enum to more appropriate places
 
haftmann 
parents: 
49808 
diff
changeset
 | 
4318  | 
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
 | 
4319  | 
by (induct n arbitrary: ys) auto  | 
| 
 
744934b818c7
moved quite generic material from theory Enum to more appropriate places
 
haftmann 
parents: 
49808 
diff
changeset
 | 
4320  | 
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
 | 
4321  | 
by (induct n arbitrary: ys) auto  | 
| 
 
744934b818c7
moved quite generic material from theory Enum to more appropriate places
 
haftmann 
parents: 
49808 
diff
changeset
 | 
4322  | 
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
 | 
4323  | 
by (induct ys) auto  | 
| 
 
744934b818c7
moved quite generic material from theory Enum to more appropriate places
 
haftmann 
parents: 
49808 
diff
changeset
 | 
4324  | 
ultimately show ?thesis by auto  | 
| 
 
744934b818c7
moved quite generic material from theory Enum to more appropriate places
 
haftmann 
parents: 
49808 
diff
changeset
 | 
4325  | 
qed  | 
| 
 
744934b818c7
moved quite generic material from theory Enum to more appropriate places
 
haftmann 
parents: 
49808 
diff
changeset
 | 
4326  | 
qed  | 
| 
 
744934b818c7
moved quite generic material from theory Enum to more appropriate places
 
haftmann 
parents: 
49808 
diff
changeset
 | 
4327  | 
|
| 
 
744934b818c7
moved quite generic material from theory Enum to more appropriate places
 
haftmann 
parents: 
49808 
diff
changeset
 | 
4328  | 
lemma distinct_n_lists:  | 
| 
 
744934b818c7
moved quite generic material from theory Enum to more appropriate places
 
haftmann 
parents: 
49808 
diff
changeset
 | 
4329  | 
assumes "distinct xs"  | 
| 
 
744934b818c7
moved quite generic material from theory Enum to more appropriate places
 
haftmann 
parents: 
49808 
diff
changeset
 | 
4330  | 
shows "distinct (List.n_lists n xs)"  | 
| 
 
744934b818c7
moved quite generic material from theory Enum to more appropriate places
 
haftmann 
parents: 
49808 
diff
changeset
 | 
4331  | 
proof (rule card_distinct)  | 
| 
 
744934b818c7
moved quite generic material from theory Enum to more appropriate places
 
haftmann 
parents: 
49808 
diff
changeset
 | 
4332  | 
from assms have card_length: "card (set xs) = length xs" by (rule distinct_card)  | 
| 
 
744934b818c7
moved quite generic material from theory Enum to more appropriate places
 
haftmann 
parents: 
49808 
diff
changeset
 | 
4333  | 
have "card (set (List.n_lists n xs)) = card (set xs) ^ n"  | 
| 
 
744934b818c7
moved quite generic material from theory Enum to more appropriate places
 
haftmann 
parents: 
49808 
diff
changeset
 | 
4334  | 
proof (induct n)  | 
| 
 
744934b818c7
moved quite generic material from theory Enum to more appropriate places
 
haftmann 
parents: 
49808 
diff
changeset
 | 
4335  | 
case 0 then show ?case by simp  | 
| 
 
744934b818c7
moved quite generic material from theory Enum to more appropriate places
 
haftmann 
parents: 
49808 
diff
changeset
 | 
4336  | 
next  | 
| 
 
744934b818c7
moved quite generic material from theory Enum to more appropriate places
 
haftmann 
parents: 
49808 
diff
changeset
 | 
4337  | 
case (Suc n)  | 
| 
 
744934b818c7
moved quite generic material from theory Enum to more appropriate places
 
haftmann 
parents: 
49808 
diff
changeset
 | 
4338  | 
moreover have "card (\<Union>ys\<in>set (List.n_lists n xs). (\<lambda>y. y # ys) ` set xs)  | 
| 
 
744934b818c7
moved quite generic material from theory Enum to more appropriate places
 
haftmann 
parents: 
49808 
diff
changeset
 | 
4339  | 
= (\<Sum>ys\<in>set (List.n_lists n xs). card ((\<lambda>y. y # ys) ` set xs))"  | 
| 
 
744934b818c7
moved quite generic material from theory Enum to more appropriate places
 
haftmann 
parents: 
49808 
diff
changeset
 | 
4340  | 
by (rule card_UN_disjoint) auto  | 
| 
 
744934b818c7
moved quite generic material from theory Enum to more appropriate places
 
haftmann 
parents: 
49808 
diff
changeset
 | 
4341  | 
moreover have "\<And>ys. card ((\<lambda>y. y # ys) ` set xs) = card (set xs)"  | 
| 
 
744934b818c7
moved quite generic material from theory Enum to more appropriate places
 
haftmann 
parents: 
49808 
diff
changeset
 | 
4342  | 
by (rule card_image) (simp add: inj_on_def)  | 
| 
 
744934b818c7
moved quite generic material from theory Enum to more appropriate places
 
haftmann 
parents: 
49808 
diff
changeset
 | 
4343  | 
ultimately show ?case by auto  | 
| 
 
744934b818c7
moved quite generic material from theory Enum to more appropriate places
 
haftmann 
parents: 
49808 
diff
changeset
 | 
4344  | 
qed  | 
| 
 
744934b818c7
moved quite generic material from theory Enum to more appropriate places
 
haftmann 
parents: 
49808 
diff
changeset
 | 
4345  | 
also have "\<dots> = length xs ^ n" by (simp add: card_length)  | 
| 
 
744934b818c7
moved quite generic material from theory Enum to more appropriate places
 
haftmann 
parents: 
49808 
diff
changeset
 | 
4346  | 
finally show "card (set (List.n_lists n xs)) = length (List.n_lists n xs)"  | 
| 
 
744934b818c7
moved quite generic material from theory Enum to more appropriate places
 
haftmann 
parents: 
49808 
diff
changeset
 | 
4347  | 
by (simp add: length_n_lists)  | 
| 
 
744934b818c7
moved quite generic material from theory Enum to more appropriate places
 
haftmann 
parents: 
49808 
diff
changeset
 | 
4348  | 
qed  | 
| 
 
744934b818c7
moved quite generic material from theory Enum to more appropriate places
 
haftmann 
parents: 
49808 
diff
changeset
 | 
4349  | 
|
| 
 
744934b818c7
moved quite generic material from theory Enum to more appropriate places
 
haftmann 
parents: 
49808 
diff
changeset
 | 
4350  | 
|
| 19390 | 4351  | 
subsubsection {* @{const splice} *}
 | 
4352  | 
||
| 
40593
 
1e57b18d27b1
code eqn for slice was missing; redefined splice with fun
 
nipkow 
parents: 
40365 
diff
changeset
 | 
4353  | 
lemma splice_Nil2 [simp, code]: "splice xs [] = xs"  | 
| 19390 | 4354  | 
by (cases xs) simp_all  | 
4355  | 
||
| 
40593
 
1e57b18d27b1
code eqn for slice was missing; redefined splice with fun
 
nipkow 
parents: 
40365 
diff
changeset
 | 
4356  | 
declare splice.simps(1,3)[code]  | 
| 
 
1e57b18d27b1
code eqn for slice was missing; redefined splice with fun
 
nipkow 
parents: 
40365 
diff
changeset
 | 
4357  | 
declare splice.simps(2)[simp del]  | 
| 19390 | 4358  | 
|
| 24526 | 4359  | 
lemma length_splice[simp]: "length(splice xs ys) = length xs + length ys"  | 
| 
40593
 
1e57b18d27b1
code eqn for slice was missing; redefined splice with fun
 
nipkow 
parents: 
40365 
diff
changeset
 | 
4360  | 
by (induct xs ys rule: splice.induct) auto  | 
| 22793 | 4361  | 
|
| 35115 | 4362  | 
|
4363  | 
subsubsection {* Transpose *}
 | 
|
| 34933 | 4364  | 
|
4365  | 
function transpose where  | 
|
4366  | 
"transpose [] = []" |  | 
|
4367  | 
"transpose ([] # xss) = transpose xss" |  | 
|
4368  | 
"transpose ((x#xs) # xss) =  | 
|
4369  | 
(x # [h. (h#t) \<leftarrow> xss]) # transpose (xs # [t. (h#t) \<leftarrow> xss])"  | 
|
4370  | 
by pat_completeness auto  | 
|
4371  | 
||
4372  | 
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
 | 
4373  | 
"concat (map (case_list [] (\<lambda>h t. [h])) xss) =  | 
| 34933 | 4374  | 
map (\<lambda>xs. hd xs) [ys\<leftarrow>xss . ys \<noteq> []]"  | 
4375  | 
by (induct xss) (auto split: list.split)  | 
|
4376  | 
||
4377  | 
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
 | 
4378  | 
"concat (map (case_list [] (\<lambda>h t. [t])) xss) =  | 
| 34933 | 4379  | 
map (\<lambda>xs. tl xs) [ys\<leftarrow>xss . ys \<noteq> []]"  | 
4380  | 
by (induct xss) (auto split: list.split)  | 
|
4381  | 
||
4382  | 
lemma transpose_aux_max:  | 
|
4383  | 
"max (Suc (length xs)) (foldr (\<lambda>xs. max (length xs)) xss 0) =  | 
|
4384  | 
Suc (max (length xs) (foldr (\<lambda>x. max (length x - Suc 0)) [ys\<leftarrow>xss . ys\<noteq>[]] 0))"  | 
|
4385  | 
(is "max _ ?foldB = Suc (max _ ?foldA)")  | 
|
4386  | 
proof (cases "[ys\<leftarrow>xss . ys\<noteq>[]] = []")  | 
|
4387  | 
case True  | 
|
4388  | 
hence "foldr (\<lambda>xs. max (length xs)) xss 0 = 0"  | 
|
4389  | 
proof (induct xss)  | 
|
4390  | 
case (Cons x xs)  | 
|
| 
53374
 
a14d2a854c02
tuned proofs -- clarified flow of facts wrt. calculation;
 
wenzelm 
parents: 
53017 
diff
changeset
 | 
4391  | 
then have "x = []" by (cases x) auto  | 
| 
 
a14d2a854c02
tuned proofs -- clarified flow of facts wrt. calculation;
 
wenzelm 
parents: 
53017 
diff
changeset
 | 
4392  | 
with Cons show ?case by auto  | 
| 34933 | 4393  | 
qed simp  | 
4394  | 
thus ?thesis using True by simp  | 
|
4395  | 
next  | 
|
4396  | 
case False  | 
|
4397  | 
||
4398  | 
have foldA: "?foldA = foldr (\<lambda>x. max (length x)) [ys\<leftarrow>xss . ys \<noteq> []] 0 - 1"  | 
|
4399  | 
by (induct xss) auto  | 
|
4400  | 
have foldB: "?foldB = foldr (\<lambda>x. max (length x)) [ys\<leftarrow>xss . ys \<noteq> []] 0"  | 
|
4401  | 
by (induct xss) auto  | 
|
4402  | 
||
4403  | 
have "0 < ?foldB"  | 
|
4404  | 
proof -  | 
|
4405  | 
from False  | 
|
4406  | 
obtain z zs where zs: "[ys\<leftarrow>xss . ys \<noteq> []] = z#zs" by (auto simp: neq_Nil_conv)  | 
|
4407  | 
hence "z \<in> set ([ys\<leftarrow>xss . ys \<noteq> []])" by auto  | 
|
4408  | 
hence "z \<noteq> []" by auto  | 
|
4409  | 
thus ?thesis  | 
|
4410  | 
unfolding foldB zs  | 
|
4411  | 
by (auto simp: max_def intro: less_le_trans)  | 
|
4412  | 
qed  | 
|
4413  | 
thus ?thesis  | 
|
4414  | 
unfolding foldA foldB max_Suc_Suc[symmetric]  | 
|
4415  | 
by simp  | 
|
4416  | 
qed  | 
|
4417  | 
||
4418  | 
termination transpose  | 
|
4419  | 
by (relation "measure (\<lambda>xs. foldr (\<lambda>xs. max (length xs)) xs 0 + length xs)")  | 
|
4420  | 
(auto simp: transpose_aux_filter_tail foldr_map comp_def transpose_aux_max less_Suc_eq_le)  | 
|
4421  | 
||
4422  | 
lemma transpose_empty: "(transpose xs = []) \<longleftrightarrow> (\<forall>x \<in> set xs. x = [])"  | 
|
4423  | 
by (induct rule: transpose.induct) simp_all  | 
|
4424  | 
||
4425  | 
lemma length_transpose:  | 
|
4426  | 
fixes xs :: "'a list list"  | 
|
4427  | 
shows "length (transpose xs) = foldr (\<lambda>xs. max (length xs)) xs 0"  | 
|
4428  | 
by (induct rule: transpose.induct)  | 
|
4429  | 
(auto simp: transpose_aux_filter_tail foldr_map comp_def transpose_aux_max  | 
|
4430  | 
max_Suc_Suc[symmetric] simp del: max_Suc_Suc)  | 
|
4431  | 
||
4432  | 
lemma nth_transpose:  | 
|
4433  | 
fixes xs :: "'a list list"  | 
|
4434  | 
assumes "i < length (transpose xs)"  | 
|
4435  | 
shows "transpose xs ! i = map (\<lambda>xs. xs ! i) [ys \<leftarrow> xs. i < length ys]"  | 
|
4436  | 
using assms proof (induct arbitrary: i rule: transpose.induct)  | 
|
4437  | 
case (3 x xs xss)  | 
|
4438  | 
def XS == "(x # xs) # xss"  | 
|
4439  | 
hence [simp]: "XS \<noteq> []" by auto  | 
|
4440  | 
thus ?case  | 
|
4441  | 
proof (cases i)  | 
|
4442  | 
case 0  | 
|
4443  | 
thus ?thesis by (simp add: transpose_aux_filter_head hd_conv_nth)  | 
|
4444  | 
next  | 
|
4445  | 
case (Suc j)  | 
|
4446  | 
have *: "\<And>xss. xs # map tl xss = map tl ((x#xs)#xss)" by simp  | 
|
4447  | 
have **: "\<And>xss. (x#xs) # filter (\<lambda>ys. ys \<noteq> []) xss = filter (\<lambda>ys. ys \<noteq> []) ((x#xs)#xss)" by simp  | 
|
4448  | 
    { fix x have "Suc j < length x \<longleftrightarrow> x \<noteq> [] \<and> j < length x - Suc 0"
 | 
|
4449  | 
by (cases x) simp_all  | 
|
4450  | 
} note *** = this  | 
|
4451  | 
||
| 
55404
 
5cb95b79a51f
transformed 'option' and 'list' into new-style datatypes (but register them as old-style as well)
 
blanchet 
parents: 
55148 
diff
changeset
 | 
4452  | 
have j_less: "j < length (transpose (xs # concat (map (case_list [] (\<lambda>h t. [t])) xss)))"  | 
| 34933 | 4453  | 
using "3.prems" by (simp add: transpose_aux_filter_tail length_transpose Suc)  | 
4454  | 
||
4455  | 
show ?thesis  | 
|
4456  | 
unfolding transpose.simps `i = Suc j` nth_Cons_Suc "3.hyps"[OF j_less]  | 
|
4457  | 
apply (auto simp: transpose_aux_filter_tail filter_map comp_def length_transpose * ** *** XS_def[symmetric])  | 
|
| 
55404
 
5cb95b79a51f
transformed 'option' and 'list' into new-style datatypes (but register them as old-style as well)
 
blanchet 
parents: 
55148 
diff
changeset
 | 
4458  | 
apply (rule list.exhaust)  | 
| 34933 | 4459  | 
by auto  | 
4460  | 
qed  | 
|
4461  | 
qed simp_all  | 
|
4462  | 
||
4463  | 
lemma transpose_map_map:  | 
|
4464  | 
"transpose (map (map f) xs) = map (map f) (transpose xs)"  | 
|
4465  | 
proof (rule nth_equalityI, safe)  | 
|
4466  | 
have [simp]: "length (transpose (map (map f) xs)) = length (transpose xs)"  | 
|
4467  | 
by (simp add: length_transpose foldr_map comp_def)  | 
|
4468  | 
show "length (transpose (map (map f) xs)) = length (map (map f) (transpose xs))" by simp  | 
|
4469  | 
||
4470  | 
fix i assume "i < length (transpose (map (map f) xs))"  | 
|
4471  | 
thus "transpose (map (map f) xs) ! i = map (map f) (transpose xs) ! i"  | 
|
4472  | 
by (simp add: nth_transpose filter_map comp_def)  | 
|
4473  | 
qed  | 
|
| 24616 | 4474  | 
|
| 35115 | 4475  | 
|
| 31557 | 4476  | 
subsubsection {* (In)finiteness *}
 | 
| 28642 | 4477  | 
|
4478  | 
lemma finite_maxlen:  | 
|
4479  | 
"finite (M::'a list set) ==> EX n. ALL s:M. size s < n"  | 
|
4480  | 
proof (induct rule: finite.induct)  | 
|
4481  | 
case emptyI show ?case by simp  | 
|
4482  | 
next  | 
|
4483  | 
case (insertI M xs)  | 
|
4484  | 
then obtain n where "\<forall>s\<in>M. length s < n" by blast  | 
|
4485  | 
hence "ALL s:insert xs M. size s < max n (size xs) + 1" by auto  | 
|
4486  | 
thus ?case ..  | 
|
4487  | 
qed  | 
|
4488  | 
||
| 45714 | 4489  | 
lemma lists_length_Suc_eq:  | 
4490  | 
  "{xs. set xs \<subseteq> A \<and> length xs = Suc n} =
 | 
|
4491  | 
    (\<lambda>(xs, n). n#xs) ` ({xs. set xs \<subseteq> A \<and> length xs = n} \<times> A)"
 | 
|
4492  | 
by (auto simp: length_Suc_conv)  | 
|
4493  | 
||
4494  | 
lemma  | 
|
4495  | 
assumes "finite A"  | 
|
4496  | 
  shows finite_lists_length_eq: "finite {xs. set xs \<subseteq> A \<and> length xs = n}"
 | 
|
4497  | 
  and card_lists_length_eq: "card {xs. set xs \<subseteq> A \<and> length xs = n} = (card A)^n"
 | 
|
4498  | 
using `finite A`  | 
|
4499  | 
by (induct n)  | 
|
4500  | 
(auto simp: card_image inj_split_Cons lists_length_Suc_eq cong: conj_cong)  | 
|
| 31557 | 4501  | 
|
4502  | 
lemma finite_lists_length_le:  | 
|
4503  | 
  assumes "finite A" shows "finite {xs. set xs \<subseteq> A \<and> length xs \<le> n}"
 | 
|
4504  | 
(is "finite ?S")  | 
|
4505  | 
proof-  | 
|
4506  | 
  have "?S = (\<Union>n\<in>{0..n}. {xs. set xs \<subseteq> A \<and> length xs = n})" by auto
 | 
|
| 
50027
 
7747a9f4c358
adjusting proofs as the set_comprehension_pointfree simproc breaks some existing proofs
 
bulwahn 
parents: 
49963 
diff
changeset
 | 
4507  | 
thus ?thesis by (auto intro!: finite_lists_length_eq[OF `finite A`] simp only:)  | 
| 31557 | 4508  | 
qed  | 
4509  | 
||
| 45714 | 4510  | 
lemma card_lists_length_le:  | 
4511  | 
  assumes "finite A" shows "card {xs. set xs \<subseteq> A \<and> length xs \<le> n} = (\<Sum>i\<le>n. card A^i)"
 | 
|
4512  | 
proof -  | 
|
4513  | 
  have "(\<Sum>i\<le>n. card A^i) = card (\<Union>i\<le>n. {xs. set xs \<subseteq> A \<and> length xs = i})"
 | 
|
4514  | 
using `finite A`  | 
|
4515  | 
by (subst card_UN_disjoint)  | 
|
4516  | 
(auto simp add: card_lists_length_eq finite_lists_length_eq)  | 
|
4517  | 
  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}"
 | 
|
4518  | 
by auto  | 
|
4519  | 
finally show ?thesis by simp  | 
|
4520  | 
qed  | 
|
4521  | 
||
| 45932 | 4522  | 
lemma card_lists_distinct_length_eq:  | 
4523  | 
assumes "k < card A"  | 
|
4524  | 
  shows "card {xs. length xs = k \<and> distinct xs \<and> set xs \<subseteq> A} = \<Prod>{card A - k + 1 .. card A}"
 | 
|
4525  | 
using assms  | 
|
4526  | 
proof (induct k)  | 
|
4527  | 
case 0  | 
|
4528  | 
  then have "{xs. length xs = 0 \<and> distinct xs \<and> set xs \<subseteq> A} = {[]}" by auto
 | 
|
4529  | 
then show ?case by simp  | 
|
4530  | 
next  | 
|
4531  | 
case (Suc k)  | 
|
4532  | 
let "?k_list" = "\<lambda>k xs. length xs = k \<and> distinct xs \<and> set xs \<subseteq> A"  | 
|
4533  | 
have inj_Cons: "\<And>A. inj_on (\<lambda>(xs, n). n # xs) A" by (rule inj_onI) auto  | 
|
4534  | 
||
4535  | 
from Suc have "k < card A" by simp  | 
|
4536  | 
moreover have "finite A" using assms by (simp add: card_ge_0_finite)  | 
|
4537  | 
  moreover have "finite {xs. ?k_list k xs}"
 | 
|
4538  | 
using finite_lists_length_eq[OF `finite A`, of k]  | 
|
4539  | 
by - (rule finite_subset, auto)  | 
|
4540  | 
  moreover have "\<And>i j. i \<noteq> j \<longrightarrow> {i} \<times> (A - set i) \<inter> {j} \<times> (A - set j) = {}"
 | 
|
4541  | 
by auto  | 
|
4542  | 
moreover have "\<And>i. i \<in>Collect (?k_list k) \<Longrightarrow> card (A - set i) = card A - k"  | 
|
4543  | 
by (simp add: card_Diff_subset distinct_card)  | 
|
4544  | 
  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
 | 
4545  | 
      (\<lambda>(xs, n). n#xs) ` \<Union>((\<lambda>xs. {xs} \<times> (A - set xs)) ` {xs. ?k_list k xs})"
 | 
| 45932 | 4546  | 
by (auto simp: length_Suc_conv)  | 
4547  | 
moreover  | 
|
4548  | 
have "Suc (card A - Suc k) = card A - k" using Suc.prems by simp  | 
|
4549  | 
  then have "(card A - k) * \<Prod>{Suc (card A - k)..card A} = \<Prod>{Suc (card A - Suc k)..card A}"
 | 
|
| 57418 | 4550  | 
by (subst setprod.insert[symmetric]) (simp add: atLeastAtMost_insertL)+  | 
| 45932 | 4551  | 
ultimately show ?case  | 
4552  | 
by (simp add: card_image inj_Cons card_UN_disjoint Suc.hyps algebra_simps)  | 
|
4553  | 
qed  | 
|
4554  | 
||
| 28642 | 4555  | 
lemma infinite_UNIV_listI: "~ finite(UNIV::'a list set)"  | 
| 56085 | 4556  | 
apply (rule notI)  | 
4557  | 
apply (drule finite_maxlen)  | 
|
4558  | 
apply clarsimp  | 
|
4559  | 
apply (erule_tac x = "replicate n undefined" in allE)  | 
|
4560  | 
by simp  | 
|
| 28642 | 4561  | 
|
4562  | 
||
| 35115 | 4563  | 
subsection {* Sorting *}
 | 
| 24616 | 4564  | 
|
| 24617 | 4565  | 
text{* Currently it is not shown that @{const sort} returns a
 | 
4566  | 
permutation of its input because the nicest proof is via multisets,  | 
|
4567  | 
which are not yet available. Alternatively one could define a function  | 
|
4568  | 
that counts the number of occurrences of an element in a list and use  | 
|
4569  | 
that instead of multisets to state the correctness property. *}  | 
|
4570  | 
||
| 24616 | 4571  | 
context linorder  | 
4572  | 
begin  | 
|
4573  | 
||
| 
51548
 
757fa47af981
centralized various multiset operations in theory multiset;
 
haftmann 
parents: 
51540 
diff
changeset
 | 
4574  | 
lemma set_insort_key:  | 
| 
 
757fa47af981
centralized various multiset operations in theory multiset;
 
haftmann 
parents: 
51540 
diff
changeset
 | 
4575  | 
"set (insort_key f x xs) = insert x (set xs)"  | 
| 
 
757fa47af981
centralized various multiset operations in theory multiset;
 
haftmann 
parents: 
51540 
diff
changeset
 | 
4576  | 
by (induct xs) auto  | 
| 
 
757fa47af981
centralized various multiset operations in theory multiset;
 
haftmann 
parents: 
51540 
diff
changeset
 | 
4577  | 
|
| 
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
 | 
4578  | 
lemma length_insort [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
 | 
4579  | 
"length (insort_key f x xs) = Suc (length 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
 | 
4580  | 
by (induct xs) simp_all  | 
| 
 
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
 | 
4581  | 
|
| 
 
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
 | 
4582  | 
lemma insort_key_left_comm:  | 
| 
 
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
 | 
4583  | 
assumes "f x \<noteq> f y"  | 
| 
 
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
 | 
4584  | 
shows "insort_key f y (insort_key f x xs) = insort_key f x (insort_key f y 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
 | 
4585  | 
by (induct xs) (auto simp add: assms dest: antisym)  | 
| 
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
 | 
4586  | 
|
| 35195 | 4587  | 
lemma insort_left_comm:  | 
4588  | 
"insort x (insort y xs) = insort y (insort 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
 | 
4589  | 
by (cases "x = y") (auto intro: insort_key_left_comm)  | 
| 35195 | 4590  | 
|
| 
42871
 
1c0b99f950d9
names of fold_set locales resemble name of characteristic property more closely
 
haftmann 
parents: 
42809 
diff
changeset
 | 
4591  | 
lemma comp_fun_commute_insort:  | 
| 
 
1c0b99f950d9
names of fold_set locales resemble name of characteristic property more closely
 
haftmann 
parents: 
42809 
diff
changeset
 | 
4592  | 
"comp_fun_commute insort"  | 
| 35195 | 4593  | 
proof  | 
| 
42809
 
5b45125b15ba
use pointfree characterisation for fold_set locale
 
haftmann 
parents: 
42714 
diff
changeset
 | 
4594  | 
qed (simp add: insort_left_comm fun_eq_iff)  | 
| 35195 | 4595  | 
|
4596  | 
lemma sort_key_simps [simp]:  | 
|
4597  | 
"sort_key f [] = []"  | 
|
4598  | 
"sort_key f (x#xs) = insort_key f x (sort_key f xs)"  | 
|
4599  | 
by (simp_all add: sort_key_def)  | 
|
4600  | 
||
| 
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
 | 
4601  | 
lemma (in linorder) sort_key_conv_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
 | 
4602  | 
assumes "inj_on f (set xs)"  | 
| 
 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 
haftmann 
parents: 
46125 
diff
changeset
 | 
4603  | 
shows "sort_key f xs = fold (insort_key f) xs []"  | 
| 
 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 
haftmann 
parents: 
46125 
diff
changeset
 | 
4604  | 
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
 | 
4605  | 
have "fold (insort_key f) (rev xs) = fold (insort_key f) xs"  | 
| 
 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 
haftmann 
parents: 
46125 
diff
changeset
 | 
4606  | 
proof (rule fold_rev, rule ext)  | 
| 
 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 
haftmann 
parents: 
46125 
diff
changeset
 | 
4607  | 
fix zs  | 
| 
 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 
haftmann 
parents: 
46125 
diff
changeset
 | 
4608  | 
fix x y  | 
| 
 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 
haftmann 
parents: 
46125 
diff
changeset
 | 
4609  | 
assume "x \<in> set xs" "y \<in> set xs"  | 
| 
 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 
haftmann 
parents: 
46125 
diff
changeset
 | 
4610  | 
with assms have *: "f y = f x \<Longrightarrow> y = x" by (auto dest: inj_onD)  | 
| 
 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 
haftmann 
parents: 
46125 
diff
changeset
 | 
4611  | 
have **: "x = y \<longleftrightarrow> y = x" by auto  | 
| 
 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 
haftmann 
parents: 
46125 
diff
changeset
 | 
4612  | 
show "(insort_key f y \<circ> insort_key f x) zs = (insort_key f x \<circ> insort_key f y) zs"  | 
| 
 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 
haftmann 
parents: 
46125 
diff
changeset
 | 
4613  | 
by (induct zs) (auto intro: * simp add: **)  | 
| 
 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 
haftmann 
parents: 
46125 
diff
changeset
 | 
4614  | 
qed  | 
| 
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
 | 
4615  | 
then show ?thesis by (simp add: sort_key_def 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
 | 
4616  | 
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
 | 
4617  | 
|
| 
 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 
haftmann 
parents: 
46125 
diff
changeset
 | 
4618  | 
lemma (in linorder) sort_conv_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
 | 
4619  | 
"sort xs = fold insort xs []"  | 
| 
 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 
haftmann 
parents: 
46125 
diff
changeset
 | 
4620  | 
by (rule sort_key_conv_fold) simp  | 
| 35195 | 4621  | 
|
| 
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
 | 
4622  | 
lemma length_sort[simp]: "length (sort_key f xs) = 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
 | 
4623  | 
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
 | 
4624  | 
|
| 25062 | 4625  | 
lemma sorted_Cons: "sorted (x#xs) = (sorted xs & (ALL y:set xs. x <= y))"  | 
| 24616 | 4626  | 
apply(induct xs arbitrary: x) apply simp  | 
4627  | 
by simp (blast intro: order_trans)  | 
|
4628  | 
||
| 
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
 | 
4629  | 
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
 | 
4630  | 
"sorted xs \<Longrightarrow> sorted (tl 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
 | 
4631  | 
by (cases xs) (simp_all add: sorted_Cons)  | 
| 
 
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
 | 
4632  | 
|
| 24616 | 4633  | 
lemma sorted_append:  | 
| 25062 | 4634  | 
"sorted (xs@ys) = (sorted xs & sorted ys & (\<forall>x \<in> set xs. \<forall>y \<in> set ys. x\<le>y))"  | 
| 24616 | 4635  | 
by (induct xs) (auto simp add:sorted_Cons)  | 
4636  | 
||
| 31201 | 4637  | 
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
 | 
4638  | 
"sorted xs \<Longrightarrow> i \<le> j \<Longrightarrow> j < length xs \<Longrightarrow> xs!i \<le> xs!j"  | 
| 31201 | 4639  | 
by (induct xs arbitrary: i j) (auto simp:nth_Cons' sorted_Cons)  | 
4640  | 
||
| 
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
 | 
4641  | 
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
 | 
4642  | 
"sorted (rev xs) \<Longrightarrow> i \<le> j \<Longrightarrow> j < length xs \<Longrightarrow> xs!j \<le> xs!i"  | 
| 
 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 
hoelzl 
parents: 
33593 
diff
changeset
 | 
4643  | 
using sorted_nth_mono[ of "rev xs" "length xs - j - 1" "length xs - i - 1"]  | 
| 
 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 
hoelzl 
parents: 
33593 
diff
changeset
 | 
4644  | 
rev_nth[of "length xs - i - 1" "xs"] rev_nth[of "length xs - j - 1" "xs"]  | 
| 
 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 
hoelzl 
parents: 
33593 
diff
changeset
 | 
4645  | 
by 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
 | 
4646  | 
|
| 
 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 
hoelzl 
parents: 
33593 
diff
changeset
 | 
4647  | 
lemma sorted_nth_monoI:  | 
| 
 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 
hoelzl 
parents: 
33593 
diff
changeset
 | 
4648  | 
"(\<And> i j. \<lbrakk> i \<le> j ; j < length xs \<rbrakk> \<Longrightarrow> xs ! i \<le> xs ! j) \<Longrightarrow> sorted 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
 | 
4649  | 
proof (induct 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
 | 
4650  | 
case (Cons x 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
 | 
4651  | 
have "sorted 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
 | 
4652  | 
proof (rule 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
 | 
4653  | 
fix i j assume "i \<le> j" and "j < 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
 | 
4654  | 
with Cons.prems[of "Suc i" "Suc j"]  | 
| 
 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 
hoelzl 
parents: 
33593 
diff
changeset
 | 
4655  | 
show "xs ! i \<le> xs ! j" by 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
 | 
4656  | 
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
 | 
4657  | 
moreover  | 
| 
 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 
hoelzl 
parents: 
33593 
diff
changeset
 | 
4658  | 
  {
 | 
| 
 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 
hoelzl 
parents: 
33593 
diff
changeset
 | 
4659  | 
fix y assume "y \<in> 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
 | 
4660  | 
then obtain j where "j < length xs" and "xs ! j = y"  | 
| 
 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 
hoelzl 
parents: 
33593 
diff
changeset
 | 
4661  | 
unfolding in_set_conv_nth 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
 | 
4662  | 
with Cons.prems[of 0 "Suc j"]  | 
| 
 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 
hoelzl 
parents: 
33593 
diff
changeset
 | 
4663  | 
have "x \<le> y"  | 
| 
 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 
hoelzl 
parents: 
33593 
diff
changeset
 | 
4664  | 
by 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
 | 
4665  | 
}  | 
| 
 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 
hoelzl 
parents: 
33593 
diff
changeset
 | 
4666  | 
ultimately  | 
| 
 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 
hoelzl 
parents: 
33593 
diff
changeset
 | 
4667  | 
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
 | 
4668  | 
unfolding sorted_Cons by 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
 | 
4669  | 
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
 | 
4670  | 
|
| 
 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 
hoelzl 
parents: 
33593 
diff
changeset
 | 
4671  | 
lemma sorted_equals_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
 | 
4672  | 
"sorted xs = (\<forall>j < length xs. \<forall>i \<le> j. xs ! i \<le> xs ! j)"  | 
| 
 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 
hoelzl 
parents: 
33593 
diff
changeset
 | 
4673  | 
by (auto intro: sorted_nth_monoI sorted_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
 | 
4674  | 
|
| 
 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 
hoelzl 
parents: 
33593 
diff
changeset
 | 
4675  | 
lemma set_insort: "set(insort_key f x xs) = insert x (set xs)"  | 
| 24616 | 4676  | 
by (induct xs) auto  | 
4677  | 
||
| 
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
 | 
4678  | 
lemma set_sort[simp]: "set(sort_key f xs) = set xs"  | 
| 24616 | 4679  | 
by (induct xs) (simp_all add:set_insort)  | 
4680  | 
||
| 
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
 | 
4681  | 
lemma distinct_insort: "distinct (insort_key f x xs) = (x \<notin> set xs \<and> distinct xs)"  | 
| 24616 | 4682  | 
by(induct xs)(auto simp:set_insort)  | 
4683  | 
||
| 
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
 | 
4684  | 
lemma distinct_sort[simp]: "distinct (sort_key f xs) = distinct xs"  | 
| 44921 | 4685  | 
by (induct xs) (simp_all add: distinct_insort)  | 
| 24616 | 4686  | 
|
| 
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
 | 
4687  | 
lemma sorted_insort_key: "sorted (map f (insort_key f x xs)) = sorted (map f 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
 | 
4688  | 
by (induct xs) (auto simp:sorted_Cons set_insort)  | 
| 
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
 | 
4689  | 
|
| 24616 | 4690  | 
lemma sorted_insort: "sorted (insort x xs) = sorted 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
 | 
4691  | 
using sorted_insort_key [where f="\<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
 | 
4692  | 
|
| 
 
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
 | 
4693  | 
theorem sorted_sort_key [simp]: "sorted (map f (sort_key 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
 | 
4694  | 
by (induct xs) (auto simp:sorted_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
 | 
4695  | 
|
| 
 
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
 | 
4696  | 
theorem sorted_sort [simp]: "sorted (sort 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
 | 
4697  | 
using sorted_sort_key [where f="\<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
 | 
4698  | 
|
| 36851 | 4699  | 
lemma sorted_butlast:  | 
4700  | 
assumes "xs \<noteq> []" and "sorted xs"  | 
|
4701  | 
shows "sorted (butlast xs)"  | 
|
4702  | 
proof -  | 
|
4703  | 
from `xs \<noteq> []` obtain ys y where "xs = ys @ [y]" by (cases xs rule: rev_cases) auto  | 
|
4704  | 
with `sorted xs` show ?thesis by (simp add: sorted_append)  | 
|
4705  | 
qed  | 
|
4706  | 
||
4707  | 
lemma insort_not_Nil [simp]:  | 
|
4708  | 
"insort_key f a xs \<noteq> []"  | 
|
4709  | 
by (induct xs) simp_all  | 
|
4710  | 
||
| 
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
 | 
4711  | 
lemma insort_is_Cons: "\<forall>x\<in>set xs. f a \<le> f x \<Longrightarrow> insort_key f a xs = a # xs"  | 
| 
26143
 
314c0bcb7df7
Added useful general lemmas from the work with the HeapMonad
 
bulwahn 
parents: 
26073 
diff
changeset
 | 
4712  | 
by (cases xs) auto  | 
| 
 
314c0bcb7df7
Added useful general lemmas from the work with the HeapMonad
 
bulwahn 
parents: 
26073 
diff
changeset
 | 
4713  | 
|
| 
44916
 
840d8c3d9113
added lemma motivated by a more specific lemma in the AFP-KBPs theories
 
bulwahn 
parents: 
44890 
diff
changeset
 | 
4714  | 
lemma sorted_sort_id: "sorted xs \<Longrightarrow> sort xs = xs"  | 
| 
 
840d8c3d9113
added lemma motivated by a more specific lemma in the AFP-KBPs theories
 
bulwahn 
parents: 
44890 
diff
changeset
 | 
4715  | 
by (induct xs) (auto simp add: sorted_Cons insort_is_Cons)  | 
| 
 
840d8c3d9113
added lemma motivated by a more specific lemma in the AFP-KBPs theories
 
bulwahn 
parents: 
44890 
diff
changeset
 | 
4716  | 
|
| 
39534
 
c798d4f1b682
generalized lemma insort_remove1 to insort_key_remove1
 
haftmann 
parents: 
39302 
diff
changeset
 | 
4717  | 
lemma sorted_map_remove1:  | 
| 
 
c798d4f1b682
generalized lemma insort_remove1 to insort_key_remove1
 
haftmann 
parents: 
39302 
diff
changeset
 | 
4718  | 
"sorted (map f xs) \<Longrightarrow> sorted (map f (remove1 x xs))"  | 
| 
 
c798d4f1b682
generalized lemma insort_remove1 to insort_key_remove1
 
haftmann 
parents: 
39302 
diff
changeset
 | 
4719  | 
by (induct xs) (auto simp add: sorted_Cons)  | 
| 
 
c798d4f1b682
generalized lemma insort_remove1 to insort_key_remove1
 
haftmann 
parents: 
39302 
diff
changeset
 | 
4720  | 
|
| 
26143
 
314c0bcb7df7
Added useful general lemmas from the work with the HeapMonad
 
bulwahn 
parents: 
26073 
diff
changeset
 | 
4721  | 
lemma sorted_remove1: "sorted xs \<Longrightarrow> sorted (remove1 a xs)"  | 
| 
39534
 
c798d4f1b682
generalized lemma insort_remove1 to insort_key_remove1
 
haftmann 
parents: 
39302 
diff
changeset
 | 
4722  | 
using sorted_map_remove1 [of "\<lambda>x. x"] by simp  | 
| 
 
c798d4f1b682
generalized lemma insort_remove1 to insort_key_remove1
 
haftmann 
parents: 
39302 
diff
changeset
 | 
4723  | 
|
| 
 
c798d4f1b682
generalized lemma insort_remove1 to insort_key_remove1
 
haftmann 
parents: 
39302 
diff
changeset
 | 
4724  | 
lemma insort_key_remove1:  | 
| 
 
c798d4f1b682
generalized lemma insort_remove1 to insort_key_remove1
 
haftmann 
parents: 
39302 
diff
changeset
 | 
4725  | 
assumes "a \<in> set xs" and "sorted (map f xs)" and "hd (filter (\<lambda>x. f a = f x) xs) = a"  | 
| 
 
c798d4f1b682
generalized lemma insort_remove1 to insort_key_remove1
 
haftmann 
parents: 
39302 
diff
changeset
 | 
4726  | 
shows "insort_key f a (remove1 a xs) = xs"  | 
| 
 
c798d4f1b682
generalized lemma insort_remove1 to insort_key_remove1
 
haftmann 
parents: 
39302 
diff
changeset
 | 
4727  | 
using assms proof (induct 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
 | 
4728  | 
case (Cons x xs)  | 
| 
39534
 
c798d4f1b682
generalized lemma insort_remove1 to insort_key_remove1
 
haftmann 
parents: 
39302 
diff
changeset
 | 
4729  | 
then 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
 | 
4730  | 
proof (cases "x = a")  | 
| 
 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 
hoelzl 
parents: 
33593 
diff
changeset
 | 
4731  | 
case False  | 
| 
39534
 
c798d4f1b682
generalized lemma insort_remove1 to insort_key_remove1
 
haftmann 
parents: 
39302 
diff
changeset
 | 
4732  | 
then have "f x \<noteq> f a" using Cons.prems by auto  | 
| 
 
c798d4f1b682
generalized lemma insort_remove1 to insort_key_remove1
 
haftmann 
parents: 
39302 
diff
changeset
 | 
4733  | 
then have "f x < f a" using Cons.prems by (auto simp: sorted_Cons)  | 
| 
 
c798d4f1b682
generalized lemma insort_remove1 to insort_key_remove1
 
haftmann 
parents: 
39302 
diff
changeset
 | 
4734  | 
with `f x \<noteq> f a` show ?thesis using Cons by (auto simp: sorted_Cons insort_is_Cons)  | 
| 
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
 | 
4735  | 
qed (auto simp: sorted_Cons insort_is_Cons)  | 
| 
 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 
hoelzl 
parents: 
33593 
diff
changeset
 | 
4736  | 
qed simp  | 
| 
26143
 
314c0bcb7df7
Added useful general lemmas from the work with the HeapMonad
 
bulwahn 
parents: 
26073 
diff
changeset
 | 
4737  | 
|
| 
39534
 
c798d4f1b682
generalized lemma insort_remove1 to insort_key_remove1
 
haftmann 
parents: 
39302 
diff
changeset
 | 
4738  | 
lemma insort_remove1:  | 
| 
 
c798d4f1b682
generalized lemma insort_remove1 to insort_key_remove1
 
haftmann 
parents: 
39302 
diff
changeset
 | 
4739  | 
assumes "a \<in> set xs" and "sorted xs"  | 
| 
 
c798d4f1b682
generalized lemma insort_remove1 to insort_key_remove1
 
haftmann 
parents: 
39302 
diff
changeset
 | 
4740  | 
shows "insort a (remove1 a xs) = xs"  | 
| 
 
c798d4f1b682
generalized lemma insort_remove1 to insort_key_remove1
 
haftmann 
parents: 
39302 
diff
changeset
 | 
4741  | 
proof (rule insort_key_remove1)  | 
| 
 
c798d4f1b682
generalized lemma insort_remove1 to insort_key_remove1
 
haftmann 
parents: 
39302 
diff
changeset
 | 
4742  | 
from `a \<in> set xs` show "a \<in> set xs" .  | 
| 
 
c798d4f1b682
generalized lemma insort_remove1 to insort_key_remove1
 
haftmann 
parents: 
39302 
diff
changeset
 | 
4743  | 
from `sorted xs` show "sorted (map (\<lambda>x. x) xs)" by simp  | 
| 
 
c798d4f1b682
generalized lemma insort_remove1 to insort_key_remove1
 
haftmann 
parents: 
39302 
diff
changeset
 | 
4744  | 
from `a \<in> set xs` have "a \<in> set (filter (op = a) xs)" by auto  | 
| 
 
c798d4f1b682
generalized lemma insort_remove1 to insort_key_remove1
 
haftmann 
parents: 
39302 
diff
changeset
 | 
4745  | 
  then have "set (filter (op = a) xs) \<noteq> {}" by auto
 | 
| 
 
c798d4f1b682
generalized lemma insort_remove1 to insort_key_remove1
 
haftmann 
parents: 
39302 
diff
changeset
 | 
4746  | 
then have "filter (op = a) xs \<noteq> []" by (auto simp only: set_empty)  | 
| 
 
c798d4f1b682
generalized lemma insort_remove1 to insort_key_remove1
 
haftmann 
parents: 
39302 
diff
changeset
 | 
4747  | 
then have "length (filter (op = a) xs) > 0" by simp  | 
| 
 
c798d4f1b682
generalized lemma insort_remove1 to insort_key_remove1
 
haftmann 
parents: 
39302 
diff
changeset
 | 
4748  | 
then obtain n where n: "Suc n = length (filter (op = a) xs)"  | 
| 
 
c798d4f1b682
generalized lemma insort_remove1 to insort_key_remove1
 
haftmann 
parents: 
39302 
diff
changeset
 | 
4749  | 
by (cases "length (filter (op = a) xs)") simp_all  | 
| 
 
c798d4f1b682
generalized lemma insort_remove1 to insort_key_remove1
 
haftmann 
parents: 
39302 
diff
changeset
 | 
4750  | 
moreover have "replicate (Suc n) a = a # replicate n a"  | 
| 
 
c798d4f1b682
generalized lemma insort_remove1 to insort_key_remove1
 
haftmann 
parents: 
39302 
diff
changeset
 | 
4751  | 
by simp  | 
| 
 
c798d4f1b682
generalized lemma insort_remove1 to insort_key_remove1
 
haftmann 
parents: 
39302 
diff
changeset
 | 
4752  | 
ultimately show "hd (filter (op = a) xs) = a" by (simp add: replicate_length_filter)  | 
| 
 
c798d4f1b682
generalized lemma insort_remove1 to insort_key_remove1
 
haftmann 
parents: 
39302 
diff
changeset
 | 
4753  | 
qed  | 
| 
26143
 
314c0bcb7df7
Added useful general lemmas from the work with the HeapMonad
 
bulwahn 
parents: 
26073 
diff
changeset
 | 
4754  | 
|
| 
 
314c0bcb7df7
Added useful general lemmas from the work with the HeapMonad
 
bulwahn 
parents: 
26073 
diff
changeset
 | 
4755  | 
lemma sorted_remdups[simp]:  | 
| 
 
314c0bcb7df7
Added useful general lemmas from the work with the HeapMonad
 
bulwahn 
parents: 
26073 
diff
changeset
 | 
4756  | 
"sorted l \<Longrightarrow> sorted (remdups l)"  | 
| 
 
314c0bcb7df7
Added useful general lemmas from the work with the HeapMonad
 
bulwahn 
parents: 
26073 
diff
changeset
 | 
4757  | 
by (induct l) (auto simp: sorted_Cons)  | 
| 
 
314c0bcb7df7
Added useful general lemmas from the work with the HeapMonad
 
bulwahn 
parents: 
26073 
diff
changeset
 | 
4758  | 
|
| 
53721
 
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
 
traytel 
parents: 
53689 
diff
changeset
 | 
4759  | 
lemma sorted_remdups_adj[simp]:  | 
| 
 
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
 
traytel 
parents: 
53689 
diff
changeset
 | 
4760  | 
"sorted xs \<Longrightarrow> sorted (remdups_adj xs)"  | 
| 
 
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
 
traytel 
parents: 
53689 
diff
changeset
 | 
4761  | 
by (induct xs rule: remdups_adj.induct, simp_all split: split_if_asm add: sorted_Cons)  | 
| 
 
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
 
traytel 
parents: 
53689 
diff
changeset
 | 
4762  | 
|
| 24645 | 4763  | 
lemma sorted_distinct_set_unique:  | 
4764  | 
assumes "sorted xs" "distinct xs" "sorted ys" "distinct ys" "set xs = set ys"  | 
|
4765  | 
shows "xs = ys"  | 
|
4766  | 
proof -  | 
|
| 26734 | 4767  | 
from assms have 1: "length xs = length ys" by (auto dest!: distinct_card)  | 
| 24645 | 4768  | 
from assms show ?thesis  | 
4769  | 
proof(induct rule:list_induct2[OF 1])  | 
|
4770  | 
case 1 show ?case by simp  | 
|
4771  | 
next  | 
|
| 56085 | 4772  | 
case 2 thus ?case by (simp add: sorted_Cons)  | 
| 24645 | 4773  | 
(metis Diff_insert_absorb antisym insertE insert_iff)  | 
4774  | 
qed  | 
|
4775  | 
qed  | 
|
4776  | 
||
| 35603 | 4777  | 
lemma map_sorted_distinct_set_unique:  | 
4778  | 
assumes "inj_on f (set xs \<union> set ys)"  | 
|
4779  | 
assumes "sorted (map f xs)" "distinct (map f xs)"  | 
|
4780  | 
"sorted (map f ys)" "distinct (map f ys)"  | 
|
4781  | 
assumes "set xs = set ys"  | 
|
4782  | 
shows "xs = ys"  | 
|
4783  | 
proof -  | 
|
4784  | 
from assms have "map f xs = map f ys"  | 
|
4785  | 
by (simp add: sorted_distinct_set_unique)  | 
|
| 
53374
 
a14d2a854c02
tuned proofs -- clarified flow of facts wrt. calculation;
 
wenzelm 
parents: 
53017 
diff
changeset
 | 
4786  | 
with `inj_on f (set xs \<union> set ys)` show "xs = ys"  | 
| 35603 | 4787  | 
by (blast intro: map_inj_on)  | 
4788  | 
qed  | 
|
4789  | 
||
| 24645 | 4790  | 
lemma finite_sorted_distinct_unique:  | 
4791  | 
shows "finite A \<Longrightarrow> EX! xs. set xs = A & sorted xs & distinct xs"  | 
|
4792  | 
apply(drule finite_distinct_list)  | 
|
4793  | 
apply clarify  | 
|
4794  | 
apply(rule_tac a="sort xs" in ex1I)  | 
|
4795  | 
apply (auto simp: sorted_distinct_set_unique)  | 
|
4796  | 
done  | 
|
4797  | 
||
| 
39915
 
ecf97cf3d248
turned distinct and sorted into inductive predicates: yields nice induction principles for free; more elegant proofs
 
haftmann 
parents: 
39774 
diff
changeset
 | 
4798  | 
lemma  | 
| 
 
ecf97cf3d248
turned distinct and sorted into inductive predicates: yields nice induction principles for free; more elegant proofs
 
haftmann 
parents: 
39774 
diff
changeset
 | 
4799  | 
assumes "sorted xs"  | 
| 
 
ecf97cf3d248
turned distinct and sorted into inductive predicates: yields nice induction principles for free; more elegant proofs
 
haftmann 
parents: 
39774 
diff
changeset
 | 
4800  | 
shows sorted_take: "sorted (take n xs)"  | 
| 
 
ecf97cf3d248
turned distinct and sorted into inductive predicates: yields nice induction principles for free; more elegant proofs
 
haftmann 
parents: 
39774 
diff
changeset
 | 
4801  | 
and sorted_drop: "sorted (drop n xs)"  | 
| 
 
ecf97cf3d248
turned distinct and sorted into inductive predicates: yields nice induction principles for free; more elegant proofs
 
haftmann 
parents: 
39774 
diff
changeset
 | 
4802  | 
proof -  | 
| 
 
ecf97cf3d248
turned distinct and sorted into inductive predicates: yields nice induction principles for free; more elegant proofs
 
haftmann 
parents: 
39774 
diff
changeset
 | 
4803  | 
from assms have "sorted (take n xs @ drop n xs)" by simp  | 
| 
 
ecf97cf3d248
turned distinct and sorted into inductive predicates: yields nice induction principles for free; more elegant proofs
 
haftmann 
parents: 
39774 
diff
changeset
 | 
4804  | 
then show "sorted (take n xs)" and "sorted (drop n xs)"  | 
| 
 
ecf97cf3d248
turned distinct and sorted into inductive predicates: yields nice induction principles for free; more elegant proofs
 
haftmann 
parents: 
39774 
diff
changeset
 | 
4805  | 
unfolding sorted_append by simp_all  | 
| 29626 | 4806  | 
qed  | 
4807  | 
||
| 
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
 | 
4808  | 
lemma sorted_dropWhile: "sorted xs \<Longrightarrow> sorted (dropWhile P xs)"  | 
| 
39915
 
ecf97cf3d248
turned distinct and sorted into inductive predicates: yields nice induction principles for free; more elegant proofs
 
haftmann 
parents: 
39774 
diff
changeset
 | 
4809  | 
by (auto dest: sorted_drop simp add: dropWhile_eq_drop)  | 
| 
33639
 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 
hoelzl 
parents: 
33593 
diff
changeset
 | 
4810  | 
|
| 
 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 
hoelzl 
parents: 
33593 
diff
changeset
 | 
4811  | 
lemma sorted_takeWhile: "sorted xs \<Longrightarrow> sorted (takeWhile P xs)"  | 
| 
39915
 
ecf97cf3d248
turned distinct and sorted into inductive predicates: yields nice induction principles for free; more elegant proofs
 
haftmann 
parents: 
39774 
diff
changeset
 | 
4812  | 
by (subst takeWhile_eq_take) (auto dest: sorted_take)  | 
| 29626 | 4813  | 
|
| 34933 | 4814  | 
lemma sorted_filter:  | 
4815  | 
"sorted (map f xs) \<Longrightarrow> sorted (map f (filter P xs))"  | 
|
4816  | 
by (induct xs) (simp_all add: sorted_Cons)  | 
|
4817  | 
||
4818  | 
lemma foldr_max_sorted:  | 
|
4819  | 
assumes "sorted (rev xs)"  | 
|
4820  | 
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
 | 
4821  | 
using assms  | 
| 
 
a14d2a854c02
tuned proofs -- clarified flow of facts wrt. calculation;
 
wenzelm 
parents: 
53017 
diff
changeset
 | 
4822  | 
proof (induct xs)  | 
| 34933 | 4823  | 
case (Cons x xs)  | 
| 
53374
 
a14d2a854c02
tuned proofs -- clarified flow of facts wrt. calculation;
 
wenzelm 
parents: 
53017 
diff
changeset
 | 
4824  | 
then have "sorted (rev xs)" using sorted_append by auto  | 
| 
 
a14d2a854c02
tuned proofs -- clarified flow of facts wrt. calculation;
 
wenzelm 
parents: 
53017 
diff
changeset
 | 
4825  | 
with Cons show ?case  | 
| 
 
a14d2a854c02
tuned proofs -- clarified flow of facts wrt. calculation;
 
wenzelm 
parents: 
53017 
diff
changeset
 | 
4826  | 
by (cases xs) (auto simp add: sorted_append max_def)  | 
| 34933 | 4827  | 
qed simp  | 
4828  | 
||
4829  | 
lemma filter_equals_takeWhile_sorted_rev:  | 
|
4830  | 
assumes sorted: "sorted (rev (map f xs))"  | 
|
4831  | 
shows "[x \<leftarrow> xs. t < f x] = takeWhile (\<lambda> x. t < f x) xs"  | 
|
4832  | 
(is "filter ?P xs = ?tW")  | 
|
4833  | 
proof (rule takeWhile_eq_filter[symmetric])  | 
|
4834  | 
let "?dW" = "dropWhile ?P xs"  | 
|
4835  | 
fix x assume "x \<in> set ?dW"  | 
|
4836  | 
then obtain i where i: "i < length ?dW" and nth_i: "x = ?dW ! i"  | 
|
4837  | 
unfolding in_set_conv_nth by auto  | 
|
4838  | 
hence "length ?tW + i < length (?tW @ ?dW)"  | 
|
4839  | 
unfolding length_append by simp  | 
|
4840  | 
hence i': "length (map f ?tW) + i < length (map f xs)" by simp  | 
|
4841  | 
have "(map f ?tW @ map f ?dW) ! (length (map f ?tW) + i) \<le>  | 
|
4842  | 
(map f ?tW @ map f ?dW) ! (length (map f ?tW) + 0)"  | 
|
4843  | 
using sorted_rev_nth_mono[OF sorted _ i', of "length ?tW"]  | 
|
4844  | 
unfolding map_append[symmetric] by simp  | 
|
4845  | 
hence "f x \<le> f (?dW ! 0)"  | 
|
4846  | 
unfolding nth_append_length_plus nth_i  | 
|
4847  | 
using i preorder_class.le_less_trans[OF le0 i] by simp  | 
|
4848  | 
also have "... \<le> t"  | 
|
4849  | 
using hd_dropWhile[of "?P" xs] le0[THEN preorder_class.le_less_trans, OF i]  | 
|
4850  | 
using hd_conv_nth[of "?dW"] by simp  | 
|
4851  | 
finally show "\<not> t < f x" by simp  | 
|
4852  | 
qed  | 
|
4853  | 
||
| 
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
 | 
4854  | 
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
 | 
4855  | 
"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
 | 
4856  | 
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
 | 
4857  | 
|
| 
 
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
 | 
4858  | 
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
 | 
4859  | 
"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
 | 
4860  | 
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
 | 
4861  | 
|
| 
 
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
 | 
4862  | 
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
 | 
4863  | 
"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
 | 
4864  | 
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
 | 
4865  | 
|
| 
 
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
 | 
4866  | 
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
 | 
4867  | 
"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
 | 
4868  | 
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
 | 
4869  | 
|
| 35608 | 4870  | 
lemma set_insort_insert:  | 
4871  | 
"set (insort_insert x xs) = insert x (set 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
 | 
4872  | 
by (auto simp add: insort_insert_key_def set_insort)  | 
| 35608 | 4873  | 
|
4874  | 
lemma distinct_insort_insert:  | 
|
4875  | 
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
 | 
4876  | 
shows "distinct (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
 | 
4877  | 
using assms by (induct xs) (auto simp add: insort_insert_key_def set_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
 | 
4878  | 
|
| 
 
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
 | 
4879  | 
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
 | 
4880  | 
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
 | 
4881  | 
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
 | 
4882  | 
using assms by (simp add: insort_insert_key_def sorted_insort_key)  | 
| 35608 | 4883  | 
|
4884  | 
lemma sorted_insort_insert:  | 
|
4885  | 
assumes "sorted xs"  | 
|
4886  | 
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
 | 
4887  | 
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
 | 
4888  | 
|
| 
 
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
 | 
4889  | 
lemma filter_insort_triv:  | 
| 37107 | 4890  | 
"\<not> P x \<Longrightarrow> filter P (insort_key f x xs) = filter P xs"  | 
4891  | 
by (induct xs) simp_all  | 
|
4892  | 
||
| 
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
 | 
4893  | 
lemma filter_insort:  | 
| 37107 | 4894  | 
"sorted (map f xs) \<Longrightarrow> P x \<Longrightarrow> filter P (insort_key f x xs) = insort_key f x (filter P xs)"  | 
4895  | 
using assms by (induct xs)  | 
|
4896  | 
(auto simp add: sorted_Cons, subst insort_is_Cons, auto)  | 
|
4897  | 
||
| 
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
 | 
4898  | 
lemma filter_sort:  | 
| 37107 | 4899  | 
"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
 | 
4900  | 
by (induct xs) (simp_all add: filter_insort_triv filter_insort)  | 
| 37107 | 4901  | 
|
| 40304 | 4902  | 
lemma sorted_map_same:  | 
4903  | 
"sorted (map f [x\<leftarrow>xs. f x = g xs])"  | 
|
4904  | 
proof (induct xs arbitrary: g)  | 
|
| 37107 | 4905  | 
case Nil then show ?case by simp  | 
4906  | 
next  | 
|
4907  | 
case (Cons x xs)  | 
|
| 40304 | 4908  | 
then have "sorted (map f [y\<leftarrow>xs . f y = (\<lambda>xs. f x) xs])" .  | 
4909  | 
moreover from Cons have "sorted (map f [y\<leftarrow>xs . f y = (g \<circ> Cons x) xs])" .  | 
|
| 37107 | 4910  | 
ultimately show ?case by (simp_all add: sorted_Cons)  | 
4911  | 
qed  | 
|
4912  | 
||
| 40304 | 4913  | 
lemma sorted_same:  | 
4914  | 
"sorted [x\<leftarrow>xs. x = g xs]"  | 
|
4915  | 
using sorted_map_same [of "\<lambda>x. x"] by simp  | 
|
4916  | 
||
| 37107 | 4917  | 
lemma remove1_insort [simp]:  | 
4918  | 
"remove1 x (insort x xs) = xs"  | 
|
4919  | 
by (induct xs) simp_all  | 
|
4920  | 
||
| 24616 | 4921  | 
end  | 
4922  | 
||
| 25277 | 4923  | 
lemma sorted_upt[simp]: "sorted[i..<j]"  | 
4924  | 
by (induct j) (simp_all add:sorted_append)  | 
|
4925  | 
||
| 
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
 | 
4926  | 
lemma sorted_upto[simp]: "sorted[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
 | 
4927  | 
apply(induct i j rule:upto.induct)  | 
| 
 
1dddf2f64266
got rid of complicated class finite_intvl_succ and defined "upto" directly on int, the only instance of the class.
 
nipkow 
parents: 
32078 
diff
changeset
 | 
4928  | 
apply(subst upto.simps)  | 
| 
 
1dddf2f64266
got rid of complicated class finite_intvl_succ and defined "upto" directly on int, the only instance of the class.
 
nipkow 
parents: 
32078 
diff
changeset
 | 
4929  | 
apply(simp add:sorted_Cons)  | 
| 
 
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
 | 
4930  | 
done  | 
| 
 
1dddf2f64266
got rid of complicated class finite_intvl_succ and defined "upto" directly on int, the only instance of the class.
 
nipkow 
parents: 
32078 
diff
changeset
 | 
4931  | 
|
| 52379 | 4932  | 
lemma sorted_find_Min:  | 
4933  | 
assumes "sorted xs"  | 
|
4934  | 
assumes "\<exists>x \<in> set xs. P x"  | 
|
4935  | 
  shows "List.find P xs = Some (Min {x\<in>set xs. P x})"
 | 
|
4936  | 
using assms proof (induct xs rule: sorted.induct)  | 
|
4937  | 
case Nil then show ?case by simp  | 
|
4938  | 
next  | 
|
4939  | 
case (Cons xs x) show ?case proof (cases "P x")  | 
|
4940  | 
case True with Cons show ?thesis by (auto intro: Min_eqI [symmetric])  | 
|
4941  | 
next  | 
|
4942  | 
    case False then have "{y. (y = x \<or> y \<in> set xs) \<and> P y} = {y \<in> set xs. P y}"
 | 
|
4943  | 
by auto  | 
|
4944  | 
with Cons False show ?thesis by simp_all  | 
|
4945  | 
qed  | 
|
4946  | 
qed  | 
|
4947  | 
||
| 35115 | 4948  | 
|
4949  | 
subsubsection {* @{const transpose} on sorted lists *}
 | 
|
| 34933 | 4950  | 
|
4951  | 
lemma sorted_transpose[simp]:  | 
|
4952  | 
shows "sorted (rev (map length (transpose xs)))"  | 
|
4953  | 
by (auto simp: sorted_equals_nth_mono rev_nth nth_transpose  | 
|
4954  | 
length_filter_conv_card intro: card_mono)  | 
|
4955  | 
||
4956  | 
lemma transpose_max_length:  | 
|
4957  | 
"foldr (\<lambda>xs. max (length xs)) (transpose xs) 0 = length [x \<leftarrow> xs. x \<noteq> []]"  | 
|
4958  | 
(is "?L = ?R")  | 
|
4959  | 
proof (cases "transpose xs = []")  | 
|
4960  | 
case False  | 
|
4961  | 
have "?L = foldr max (map length (transpose xs)) 0"  | 
|
4962  | 
by (simp add: foldr_map comp_def)  | 
|
4963  | 
also have "... = length (transpose xs ! 0)"  | 
|
4964  | 
using False sorted_transpose by (simp add: foldr_max_sorted)  | 
|
4965  | 
finally show ?thesis  | 
|
4966  | 
using False by (simp add: nth_transpose)  | 
|
4967  | 
next  | 
|
4968  | 
case True  | 
|
4969  | 
hence "[x \<leftarrow> xs. x \<noteq> []] = []"  | 
|
4970  | 
by (auto intro!: filter_False simp: transpose_empty)  | 
|
4971  | 
thus ?thesis by (simp add: transpose_empty True)  | 
|
4972  | 
qed  | 
|
4973  | 
||
4974  | 
lemma length_transpose_sorted:  | 
|
4975  | 
fixes xs :: "'a list list"  | 
|
4976  | 
assumes sorted: "sorted (rev (map length xs))"  | 
|
4977  | 
shows "length (transpose xs) = (if xs = [] then 0 else length (xs ! 0))"  | 
|
4978  | 
proof (cases "xs = []")  | 
|
4979  | 
case False  | 
|
4980  | 
thus ?thesis  | 
|
4981  | 
using foldr_max_sorted[OF sorted] False  | 
|
4982  | 
unfolding length_transpose foldr_map comp_def  | 
|
4983  | 
by simp  | 
|
4984  | 
qed simp  | 
|
4985  | 
||
4986  | 
lemma nth_nth_transpose_sorted[simp]:  | 
|
4987  | 
fixes xs :: "'a list list"  | 
|
4988  | 
assumes sorted: "sorted (rev (map length xs))"  | 
|
4989  | 
and i: "i < length (transpose xs)"  | 
|
4990  | 
and j: "j < length [ys \<leftarrow> xs. i < length ys]"  | 
|
4991  | 
shows "transpose xs ! i ! j = xs ! j ! i"  | 
|
4992  | 
using j filter_equals_takeWhile_sorted_rev[OF sorted, of i]  | 
|
4993  | 
nth_transpose[OF i] nth_map[OF j]  | 
|
4994  | 
by (simp add: takeWhile_nth)  | 
|
4995  | 
||
4996  | 
lemma transpose_column_length:  | 
|
4997  | 
fixes xs :: "'a list list"  | 
|
4998  | 
assumes sorted: "sorted (rev (map length xs))" and "i < length xs"  | 
|
4999  | 
shows "length (filter (\<lambda>ys. i < length ys) (transpose xs)) = length (xs ! i)"  | 
|
5000  | 
proof -  | 
|
5001  | 
have "xs \<noteq> []" using `i < length xs` by auto  | 
|
5002  | 
note filter_equals_takeWhile_sorted_rev[OF sorted, simp]  | 
|
5003  | 
  { fix j assume "j \<le> i"
 | 
|
5004  | 
note sorted_rev_nth_mono[OF sorted, of j i, simplified, OF this `i < length xs`]  | 
|
5005  | 
} note sortedE = this[consumes 1]  | 
|
5006  | 
||
5007  | 
  have "{j. j < length (transpose xs) \<and> i < length (transpose xs ! j)}
 | 
|
5008  | 
    = {..< length (xs ! i)}"
 | 
|
5009  | 
proof safe  | 
|
5010  | 
fix j  | 
|
5011  | 
assume "j < length (transpose xs)" and "i < length (transpose xs ! j)"  | 
|
5012  | 
with this(2) nth_transpose[OF this(1)]  | 
|
5013  | 
have "i < length (takeWhile (\<lambda>ys. j < length ys) xs)" by simp  | 
|
5014  | 
from nth_mem[OF this] takeWhile_nth[OF this]  | 
|
5015  | 
show "j < length (xs ! i)" by (auto dest: set_takeWhileD)  | 
|
5016  | 
next  | 
|
5017  | 
fix j assume "j < length (xs ! i)"  | 
|
5018  | 
thus "j < length (transpose xs)"  | 
|
5019  | 
using foldr_max_sorted[OF sorted] `xs \<noteq> []` sortedE[OF le0]  | 
|
5020  | 
by (auto simp: length_transpose comp_def foldr_map)  | 
|
5021  | 
||
5022  | 
have "Suc i \<le> length (takeWhile (\<lambda>ys. j < length ys) xs)"  | 
|
5023  | 
using `i < length xs` `j < length (xs ! i)` less_Suc_eq_le  | 
|
5024  | 
by (auto intro!: length_takeWhile_less_P_nth dest!: sortedE)  | 
|
5025  | 
with nth_transpose[OF `j < length (transpose xs)`]  | 
|
5026  | 
show "i < length (transpose xs ! j)" by simp  | 
|
5027  | 
qed  | 
|
5028  | 
thus ?thesis by (simp add: length_filter_conv_card)  | 
|
5029  | 
qed  | 
|
5030  | 
||
5031  | 
lemma transpose_column:  | 
|
5032  | 
fixes xs :: "'a list list"  | 
|
5033  | 
assumes sorted: "sorted (rev (map length xs))" and "i < length xs"  | 
|
5034  | 
shows "map (\<lambda>ys. ys ! i) (filter (\<lambda>ys. i < length ys) (transpose xs))  | 
|
5035  | 
= xs ! i" (is "?R = _")  | 
|
5036  | 
proof (rule nth_equalityI, safe)  | 
|
5037  | 
show length: "length ?R = length (xs ! i)"  | 
|
5038  | 
using transpose_column_length[OF assms] by simp  | 
|
5039  | 
||
5040  | 
fix j assume j: "j < length ?R"  | 
|
5041  | 
note * = less_le_trans[OF this, unfolded length_map, OF length_filter_le]  | 
|
5042  | 
from j have j_less: "j < length (xs ! i)" using length by simp  | 
|
5043  | 
have i_less_tW: "Suc i \<le> length (takeWhile (\<lambda>ys. Suc j \<le> length ys) xs)"  | 
|
5044  | 
proof (rule length_takeWhile_less_P_nth)  | 
|
5045  | 
show "Suc i \<le> length xs" using `i < length xs` by simp  | 
|
5046  | 
fix k assume "k < Suc i"  | 
|
5047  | 
hence "k \<le> i" by auto  | 
|
5048  | 
with sorted_rev_nth_mono[OF sorted this] `i < length xs`  | 
|
5049  | 
have "length (xs ! i) \<le> length (xs ! k)" by simp  | 
|
5050  | 
thus "Suc j \<le> length (xs ! k)" using j_less by simp  | 
|
5051  | 
qed  | 
|
5052  | 
have i_less_filter: "i < length [ys\<leftarrow>xs . j < length ys]"  | 
|
5053  | 
unfolding filter_equals_takeWhile_sorted_rev[OF sorted, of j]  | 
|
5054  | 
using i_less_tW by (simp_all add: Suc_le_eq)  | 
|
5055  | 
from j show "?R ! j = xs ! i ! j"  | 
|
5056  | 
unfolding filter_equals_takeWhile_sorted_rev[OF sorted_transpose, of i]  | 
|
5057  | 
by (simp add: takeWhile_nth nth_nth_transpose_sorted[OF sorted * i_less_filter])  | 
|
5058  | 
qed  | 
|
5059  | 
||
5060  | 
lemma transpose_transpose:  | 
|
5061  | 
fixes xs :: "'a list list"  | 
|
5062  | 
assumes sorted: "sorted (rev (map length xs))"  | 
|
5063  | 
shows "transpose (transpose xs) = takeWhile (\<lambda>x. x \<noteq> []) xs" (is "?L = ?R")  | 
|
5064  | 
proof -  | 
|
5065  | 
have len: "length ?L = length ?R"  | 
|
5066  | 
unfolding length_transpose transpose_max_length  | 
|
5067  | 
using filter_equals_takeWhile_sorted_rev[OF sorted, of 0]  | 
|
5068  | 
by simp  | 
|
5069  | 
||
5070  | 
  { fix i assume "i < length ?R"
 | 
|
5071  | 
with less_le_trans[OF _ length_takeWhile_le[of _ xs]]  | 
|
5072  | 
have "i < length xs" by simp  | 
|
5073  | 
} note * = this  | 
|
5074  | 
show ?thesis  | 
|
5075  | 
by (rule nth_equalityI)  | 
|
5076  | 
(simp_all add: len nth_transpose transpose_column[OF sorted] * takeWhile_nth)  | 
|
5077  | 
qed  | 
|
| 24616 | 5078  | 
|
| 
34934
 
440605046777
Added transpose_rectangle, when the input list is rectangular.
 
hoelzl 
parents: 
34933 
diff
changeset
 | 
5079  | 
theorem transpose_rectangle:  | 
| 
 
440605046777
Added transpose_rectangle, when the input list is rectangular.
 
hoelzl 
parents: 
34933 
diff
changeset
 | 
5080  | 
assumes "xs = [] \<Longrightarrow> n = 0"  | 
| 
 
440605046777
Added transpose_rectangle, when the input list is rectangular.
 
hoelzl 
parents: 
34933 
diff
changeset
 | 
5081  | 
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
 | 
5082  | 
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
 | 
5083  | 
(is "?trans = ?map")  | 
| 
 
440605046777
Added transpose_rectangle, when the input list is rectangular.
 
hoelzl 
parents: 
34933 
diff
changeset
 | 
5084  | 
proof (rule nth_equalityI)  | 
| 
 
440605046777
Added transpose_rectangle, when the input list is rectangular.
 
hoelzl 
parents: 
34933 
diff
changeset
 | 
5085  | 
have "sorted (rev (map length xs))"  | 
| 
 
440605046777
Added transpose_rectangle, when the input list is rectangular.
 
hoelzl 
parents: 
34933 
diff
changeset
 | 
5086  | 
by (auto simp: rev_nth rect intro!: sorted_nth_monoI)  | 
| 
 
440605046777
Added transpose_rectangle, when the input list is rectangular.
 
hoelzl 
parents: 
34933 
diff
changeset
 | 
5087  | 
from foldr_max_sorted[OF this] assms  | 
| 
 
440605046777
Added transpose_rectangle, when the input list is rectangular.
 
hoelzl 
parents: 
34933 
diff
changeset
 | 
5088  | 
show len: "length ?trans = length ?map"  | 
| 
 
440605046777
Added transpose_rectangle, when the input list is rectangular.
 
hoelzl 
parents: 
34933 
diff
changeset
 | 
5089  | 
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
 | 
5090  | 
moreover  | 
| 
 
440605046777
Added transpose_rectangle, when the input list is rectangular.
 
hoelzl 
parents: 
34933 
diff
changeset
 | 
5091  | 
  { fix i assume "i < n" hence "[ys\<leftarrow>xs . i < length ys] = xs"
 | 
| 
 
440605046777
Added transpose_rectangle, when the input list is rectangular.
 
hoelzl 
parents: 
34933 
diff
changeset
 | 
5092  | 
using rect by (auto simp: in_set_conv_nth intro!: filter_True) }  | 
| 
 
440605046777
Added transpose_rectangle, when the input list is rectangular.
 
hoelzl 
parents: 
34933 
diff
changeset
 | 
5093  | 
ultimately show "\<forall>i < length ?trans. ?trans ! i = ?map ! i"  | 
| 
 
440605046777
Added transpose_rectangle, when the input list is rectangular.
 
hoelzl 
parents: 
34933 
diff
changeset
 | 
5094  | 
by (auto simp: nth_transpose intro: nth_equalityI)  | 
| 
 
440605046777
Added transpose_rectangle, when the input list is rectangular.
 
hoelzl 
parents: 
34933 
diff
changeset
 | 
5095  | 
qed  | 
| 24616 | 5096  | 
|
| 35115 | 5097  | 
|
| 25069 | 5098  | 
subsubsection {* @{text sorted_list_of_set} *}
 | 
5099  | 
||
5100  | 
text{* This function maps (finite) linearly ordered sets to sorted
 | 
|
5101  | 
lists. Warning: in most cases it is not a good idea to convert from  | 
|
5102  | 
sets to lists but one should convert in the other direction (via  | 
|
5103  | 
@{const set}). *}
 | 
|
5104  | 
||
| 51489 | 5105  | 
subsubsection {* @{text sorted_list_of_set} *}
 | 
5106  | 
||
5107  | 
text{* This function maps (finite) linearly ordered sets to sorted
 | 
|
5108  | 
lists. Warning: in most cases it is not a good idea to convert from  | 
|
5109  | 
sets to lists but one should convert in the other direction (via  | 
|
5110  | 
@{const set}). *}
 | 
|
5111  | 
||
| 54868 | 5112  | 
context linorder  | 
5113  | 
begin  | 
|
5114  | 
||
5115  | 
definition sorted_list_of_set :: "'a set \<Rightarrow> 'a list" where  | 
|
| 51489 | 5116  | 
"sorted_list_of_set = folding.F insort []"  | 
5117  | 
||
| 54868 | 5118  | 
sublocale sorted_list_of_set!: folding insort Nil  | 
| 51489 | 5119  | 
where  | 
5120  | 
"folding.F insort [] = sorted_list_of_set"  | 
|
5121  | 
proof -  | 
|
5122  | 
interpret comp_fun_commute insort by (fact comp_fun_commute_insort)  | 
|
5123  | 
show "folding insort" by default (fact comp_fun_commute)  | 
|
5124  | 
show "folding.F insort [] = sorted_list_of_set" by (simp only: sorted_list_of_set_def)  | 
|
5125  | 
qed  | 
|
5126  | 
||
5127  | 
lemma sorted_list_of_set_empty:  | 
|
| 35195 | 5128  | 
  "sorted_list_of_set {} = []"
 | 
| 51489 | 5129  | 
by (fact sorted_list_of_set.empty)  | 
| 35195 | 5130  | 
|
5131  | 
lemma sorted_list_of_set_insert [simp]:  | 
|
| 54868 | 5132  | 
  "finite A \<Longrightarrow> sorted_list_of_set (insert x A) = insort x (sorted_list_of_set (A - {x}))"
 | 
5133  | 
by (fact sorted_list_of_set.insert_remove)  | 
|
| 35195 | 5134  | 
|
| 52122 | 5135  | 
lemma sorted_list_of_set_eq_Nil_iff [simp]:  | 
5136  | 
  "finite A \<Longrightarrow> sorted_list_of_set A = [] \<longleftrightarrow> A = {}"
 | 
|
| 54868 | 5137  | 
by (auto simp: sorted_list_of_set.remove)  | 
| 52122 | 5138  | 
|
| 35195 | 5139  | 
lemma sorted_list_of_set [simp]:  | 
5140  | 
"finite A \<Longrightarrow> set (sorted_list_of_set A) = A \<and> sorted (sorted_list_of_set A)  | 
|
5141  | 
\<and> distinct (sorted_list_of_set A)"  | 
|
5142  | 
by (induct A rule: finite_induct) (simp_all add: set_insort sorted_insort distinct_insort)  | 
|
5143  | 
||
| 52122 | 5144  | 
lemma distinct_sorted_list_of_set:  | 
5145  | 
"distinct (sorted_list_of_set A)"  | 
|
5146  | 
using sorted_list_of_set by (cases "finite A") auto  | 
|
5147  | 
||
| 47841 | 5148  | 
lemma sorted_list_of_set_sort_remdups [code]:  | 
| 35195 | 5149  | 
"sorted_list_of_set (set xs) = sort (remdups xs)"  | 
5150  | 
proof -  | 
|
| 
42871
 
1c0b99f950d9
names of fold_set locales resemble name of characteristic property more closely
 
haftmann 
parents: 
42809 
diff
changeset
 | 
5151  | 
interpret comp_fun_commute insort by (fact comp_fun_commute_insort)  | 
| 51489 | 5152  | 
show ?thesis by (simp add: sorted_list_of_set.eq_fold sort_conv_fold fold_set_fold_remdups)  | 
| 35195 | 5153  | 
qed  | 
| 25069 | 5154  | 
|
| 37107 | 5155  | 
lemma sorted_list_of_set_remove:  | 
5156  | 
assumes "finite A"  | 
|
5157  | 
  shows "sorted_list_of_set (A - {x}) = remove1 x (sorted_list_of_set A)"
 | 
|
5158  | 
proof (cases "x \<in> A")  | 
|
5159  | 
case False with assms have "x \<notin> set (sorted_list_of_set A)" by simp  | 
|
5160  | 
with False show ?thesis by (simp add: remove1_idem)  | 
|
5161  | 
next  | 
|
5162  | 
case True then obtain B where A: "A = insert x B" by (rule Set.set_insert)  | 
|
5163  | 
with assms show ?thesis by simp  | 
|
5164  | 
qed  | 
|
5165  | 
||
| 25069 | 5166  | 
end  | 
5167  | 
||
| 37107 | 5168  | 
lemma sorted_list_of_set_range [simp]:  | 
5169  | 
  "sorted_list_of_set {m..<n} = [m..<n]"
 | 
|
5170  | 
by (rule sorted_distinct_set_unique) simp_all  | 
|
5171  | 
||
5172  | 
||
| 15392 | 5173  | 
subsubsection {* @{text lists}: the list-forming operator over sets *}
 | 
| 15302 | 5174  | 
|
| 23740 | 5175  | 
inductive_set  | 
| 22262 | 5176  | 
lists :: "'a set => 'a list set"  | 
| 23740 | 5177  | 
for A :: "'a set"  | 
5178  | 
where  | 
|
| 39613 | 5179  | 
Nil [intro!, simp]: "[]: lists A"  | 
| 
54147
 
97a8ff4e4ac9
killed most "no_atp", to make Sledgehammer more complete
 
blanchet 
parents: 
53954 
diff
changeset
 | 
5180  | 
| Cons [intro!, simp]: "[| a: A; l: lists A|] ==> a#l : lists A"  | 
| 
 
97a8ff4e4ac9
killed most "no_atp", to make Sledgehammer more complete
 
blanchet 
parents: 
53954 
diff
changeset
 | 
5181  | 
|
| 
 
97a8ff4e4ac9
killed most "no_atp", to make Sledgehammer more complete
 
blanchet 
parents: 
53954 
diff
changeset
 | 
5182  | 
inductive_cases listsE [elim!]: "x#l : lists A"  | 
| 
 
97a8ff4e4ac9
killed most "no_atp", to make Sledgehammer more complete
 
blanchet 
parents: 
53954 
diff
changeset
 | 
5183  | 
inductive_cases listspE [elim!]: "listsp A (x # l)"  | 
| 23740 | 5184  | 
|
| 46313 | 5185  | 
inductive_simps listsp_simps[code]:  | 
5186  | 
"listsp A []"  | 
|
5187  | 
"listsp A (x # xs)"  | 
|
5188  | 
||
| 23740 | 5189  | 
lemma listsp_mono [mono]: "A \<le> B ==> listsp A \<le> listsp B"  | 
| 46884 | 5190  | 
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
 | 
5191  | 
|
| 
46176
 
1898e61e89c4
pred_subset/equals_eq are now standard pred_set_conv rules
 
berghofe 
parents: 
46156 
diff
changeset
 | 
5192  | 
lemmas lists_mono = listsp_mono [to_set]  | 
| 22262 | 5193  | 
|
| 
22422
 
ee19cdb07528
stepping towards uniform lattice theory development in HOL
 
haftmann 
parents: 
22262 
diff
changeset
 | 
5194  | 
lemma listsp_infI:  | 
| 
 
ee19cdb07528
stepping towards uniform lattice theory development in HOL
 
haftmann 
parents: 
22262 
diff
changeset
 | 
5195  | 
assumes l: "listsp A l" shows "listsp B l ==> listsp (inf A B) l" using l  | 
| 24349 | 5196  | 
by induct blast+  | 
| 15302 | 5197  | 
|
| 
22422
 
ee19cdb07528
stepping towards uniform lattice theory development in HOL
 
haftmann 
parents: 
22262 
diff
changeset
 | 
5198  | 
lemmas lists_IntI = listsp_infI [to_set]  | 
| 
 
ee19cdb07528
stepping towards uniform lattice theory development in HOL
 
haftmann 
parents: 
22262 
diff
changeset
 | 
5199  | 
|
| 
 
ee19cdb07528
stepping towards uniform lattice theory development in HOL
 
haftmann 
parents: 
22262 
diff
changeset
 | 
5200  | 
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
 | 
5201  | 
proof (rule mono_inf [where f=listsp, THEN order_antisym])  | 
| 22262 | 5202  | 
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
 | 
5203  | 
show "inf (listsp A) (listsp B) \<le> listsp (inf A B)" by (blast intro!: listsp_infI)  | 
| 14388 | 5204  | 
qed  | 
5205  | 
||
| 
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
 | 
5206  | 
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
 | 
5207  | 
|
| 
46176
 
1898e61e89c4
pred_subset/equals_eq are now standard pred_set_conv rules
 
berghofe 
parents: 
46156 
diff
changeset
 | 
5208  | 
lemmas lists_Int_eq [simp] = listsp_inf_eq [to_set]  | 
| 22262 | 5209  | 
|
| 39613 | 5210  | 
lemma Cons_in_lists_iff[simp]: "x#xs : lists A \<longleftrightarrow> x:A \<and> xs : lists A"  | 
5211  | 
by auto  | 
|
5212  | 
||
| 22262 | 5213  | 
lemma append_in_listsp_conv [iff]:  | 
5214  | 
"(listsp A (xs @ ys)) = (listsp A xs \<and> listsp A ys)"  | 
|
| 15302 | 5215  | 
by (induct xs) auto  | 
5216  | 
||
| 22262 | 5217  | 
lemmas append_in_lists_conv [iff] = append_in_listsp_conv [to_set]  | 
5218  | 
||
5219  | 
lemma in_listsp_conv_set: "(listsp A xs) = (\<forall>x \<in> set xs. A x)"  | 
|
5220  | 
-- {* eliminate @{text listsp} in favour of @{text set} *}
 | 
|
| 15302 | 5221  | 
by (induct xs) auto  | 
5222  | 
||
| 46313 | 5223  | 
lemmas in_lists_conv_set [code_unfold] = in_listsp_conv_set [to_set]  | 
| 22262 | 5224  | 
|
| 
54147
 
97a8ff4e4ac9
killed most "no_atp", to make Sledgehammer more complete
 
blanchet 
parents: 
53954 
diff
changeset
 | 
5225  | 
lemma in_listspD [dest!]: "listsp A xs ==> \<forall>x\<in>set xs. A x"  | 
| 22262 | 5226  | 
by (rule in_listsp_conv_set [THEN iffD1])  | 
5227  | 
||
| 
54147
 
97a8ff4e4ac9
killed most "no_atp", to make Sledgehammer more complete
 
blanchet 
parents: 
53954 
diff
changeset
 | 
5228  | 
lemmas in_listsD [dest!] = in_listspD [to_set]  | 
| 
 
97a8ff4e4ac9
killed most "no_atp", to make Sledgehammer more complete
 
blanchet 
parents: 
53954 
diff
changeset
 | 
5229  | 
|
| 
 
97a8ff4e4ac9
killed most "no_atp", to make Sledgehammer more complete
 
blanchet 
parents: 
53954 
diff
changeset
 | 
5230  | 
lemma in_listspI [intro!]: "\<forall>x\<in>set xs. A x ==> listsp A xs"  | 
| 22262 | 5231  | 
by (rule in_listsp_conv_set [THEN iffD2])  | 
5232  | 
||
| 
54147
 
97a8ff4e4ac9
killed most "no_atp", to make Sledgehammer more complete
 
blanchet 
parents: 
53954 
diff
changeset
 | 
5233  | 
lemmas in_listsI [intro!] = in_listspI [to_set]  | 
| 15302 | 5234  | 
|
| 39597 | 5235  | 
lemma lists_eq_set: "lists A = {xs. set xs <= A}"
 | 
5236  | 
by auto  | 
|
5237  | 
||
| 39613 | 5238  | 
lemma lists_empty [simp]: "lists {} = {[]}"
 | 
5239  | 
by auto  | 
|
5240  | 
||
| 15302 | 5241  | 
lemma lists_UNIV [simp]: "lists UNIV = UNIV"  | 
5242  | 
by auto  | 
|
5243  | 
||
| 50134 | 5244  | 
lemma lists_image: "lists (f`A) = map f ` lists A"  | 
5245  | 
proof -  | 
|
5246  | 
  { fix xs have "\<forall>x\<in>set xs. x \<in> f ` A \<Longrightarrow> xs \<in> map f ` lists A"
 | 
|
| 55465 | 5247  | 
by (induct xs) (auto simp del: list.map simp add: list.map[symmetric] intro!: imageI) }  | 
| 50134 | 5248  | 
then show ?thesis by auto  | 
5249  | 
qed  | 
|
| 17086 | 5250  | 
|
| 35115 | 5251  | 
subsubsection {* Inductive definition for membership *}
 | 
| 17086 | 5252  | 
|
| 23740 | 5253  | 
inductive ListMem :: "'a \<Rightarrow> 'a list \<Rightarrow> bool"  | 
| 22262 | 5254  | 
where  | 
5255  | 
elem: "ListMem x (x # xs)"  | 
|
5256  | 
| insert: "ListMem x xs \<Longrightarrow> ListMem x (y # xs)"  | 
|
5257  | 
||
5258  | 
lemma ListMem_iff: "(ListMem x xs) = (x \<in> set xs)"  | 
|
| 17086 | 5259  | 
apply (rule iffI)  | 
5260  | 
apply (induct set: ListMem)  | 
|
5261  | 
apply auto  | 
|
5262  | 
apply (induct xs)  | 
|
5263  | 
apply (auto intro: ListMem.intros)  | 
|
5264  | 
done  | 
|
5265  | 
||
5266  | 
||
| 35115 | 5267  | 
subsubsection {* Lists as Cartesian products *}
 | 
| 15302 | 5268  | 
|
5269  | 
text{*@{text"set_Cons A Xs"}: the set of lists with head drawn from
 | 
|
5270  | 
@{term A} and tail drawn from @{term Xs}.*}
 | 
|
5271  | 
||
| 50548 | 5272  | 
definition set_Cons :: "'a set \<Rightarrow> 'a list set \<Rightarrow> 'a list set" where  | 
5273  | 
"set_Cons A XS = {z. \<exists>x xs. z = x # xs \<and> x \<in> A \<and> xs \<in> XS}"
 | 
|
| 15302 | 5274  | 
|
| 17724 | 5275  | 
lemma set_Cons_sing_Nil [simp]: "set_Cons A {[]} = (%x. [x])`A"
 | 
| 15302 | 5276  | 
by (auto simp add: set_Cons_def)  | 
5277  | 
||
5278  | 
text{*Yields the set of lists, all of the same length as the argument and
 | 
|
5279  | 
with elements drawn from the corresponding element of the argument.*}  | 
|
5280  | 
||
| 50548 | 5281  | 
primrec listset :: "'a set list \<Rightarrow> 'a list set" where  | 
5282  | 
"listset [] = {[]}" |
 | 
|
5283  | 
"listset (A # As) = set_Cons A (listset As)"  | 
|
| 15302 | 5284  | 
|
5285  | 
||
| 35115 | 5286  | 
subsection {* Relations on Lists *}
 | 
| 15656 | 5287  | 
|
5288  | 
subsubsection {* Length Lexicographic Ordering *}
 | 
|
5289  | 
||
5290  | 
text{*These orderings preserve well-foundedness: shorter lists 
 | 
|
5291  | 
precede longer lists. These ordering are not used in dictionaries.*}  | 
|
| 34941 | 5292  | 
|
5293  | 
primrec -- {*The lexicographic ordering for lists of the specified length*}
 | 
|
5294  | 
  lexn :: "('a \<times> 'a) set \<Rightarrow> nat \<Rightarrow> ('a list \<times> 'a list) set" where
 | 
|
| 50548 | 5295  | 
"lexn r 0 = {}" |
 | 
5296  | 
"lexn r (Suc n) =  | 
|
| 55932 | 5297  | 
(map_prod (%(x, xs). x#xs) (%(x, xs). x#xs) ` (r <*lex*> lexn r n)) Int  | 
| 50548 | 5298  | 
  {(xs, ys). length xs = Suc n \<and> length ys = Suc n}"
 | 
5299  | 
||
5300  | 
definition lex :: "('a \<times> 'a) set \<Rightarrow> ('a list \<times> 'a list) set" where
 | 
|
5301  | 
"lex r = (\<Union>n. lexn r n)" -- {*Holds only between lists of the same length*}
 | 
|
5302  | 
||
5303  | 
definition lenlex :: "('a \<times> 'a) set => ('a list \<times> 'a list) set" where
 | 
|
5304  | 
"lenlex r = inv_image (less_than <*lex*> lex r) (\<lambda>xs. (length xs, xs))"  | 
|
| 34941 | 5305  | 
        -- {*Compares lists by their length and then lexicographically*}
 | 
| 15302 | 5306  | 
|
5307  | 
lemma wf_lexn: "wf r ==> wf (lexn r n)"  | 
|
5308  | 
apply (induct n, simp, simp)  | 
|
5309  | 
apply(rule wf_subset)  | 
|
5310  | 
prefer 2 apply (rule Int_lower1)  | 
|
| 55932 | 5311  | 
apply(rule wf_map_prod_image)  | 
| 15302 | 5312  | 
prefer 2 apply (rule inj_onI, auto)  | 
5313  | 
done  | 
|
5314  | 
||
5315  | 
lemma lexn_length:  | 
|
| 24526 | 5316  | 
"(xs, ys) : lexn r n ==> length xs = n \<and> length ys = n"  | 
5317  | 
by (induct n arbitrary: xs ys) auto  | 
|
| 15302 | 5318  | 
|
5319  | 
lemma wf_lex [intro!]: "wf r ==> wf (lex r)"  | 
|
5320  | 
apply (unfold lex_def)  | 
|
5321  | 
apply (rule wf_UN)  | 
|
5322  | 
apply (blast intro: wf_lexn, clarify)  | 
|
5323  | 
apply (rename_tac m n)  | 
|
5324  | 
apply (subgoal_tac "m \<noteq> n")  | 
|
5325  | 
prefer 2 apply blast  | 
|
5326  | 
apply (blast dest: lexn_length not_sym)  | 
|
5327  | 
done  | 
|
5328  | 
||
5329  | 
lemma lexn_conv:  | 
|
| 15656 | 5330  | 
"lexn r n =  | 
5331  | 
    {(xs,ys). length xs = n \<and> length ys = n \<and>
 | 
|
5332  | 
(\<exists>xys x y xs' ys'. xs= xys @ x#xs' \<and> ys= xys @ y # ys' \<and> (x, y):r)}"  | 
|
| 18423 | 5333  | 
apply (induct n, simp)  | 
| 15302 | 5334  | 
apply (simp add: image_Collect lex_prod_def, safe, blast)  | 
5335  | 
apply (rule_tac x = "ab # xys" in exI, simp)  | 
|
5336  | 
apply (case_tac xys, simp_all, blast)  | 
|
5337  | 
done  | 
|
5338  | 
||
5339  | 
lemma lex_conv:  | 
|
| 15656 | 5340  | 
"lex r =  | 
5341  | 
    {(xs,ys). length xs = length ys \<and>
 | 
|
5342  | 
(\<exists>xys x y xs' ys'. xs = xys @ x # xs' \<and> ys = xys @ y # ys' \<and> (x, y):r)}"  | 
|
| 15302 | 5343  | 
by (force simp add: lex_def lexn_conv)  | 
5344  | 
||
| 15693 | 5345  | 
lemma wf_lenlex [intro!]: "wf r ==> wf (lenlex r)"  | 
5346  | 
by (unfold lenlex_def) blast  | 
|
5347  | 
||
5348  | 
lemma lenlex_conv:  | 
|
5349  | 
    "lenlex r = {(xs,ys). length xs < length ys |
 | 
|
| 15656 | 5350  | 
length xs = length ys \<and> (xs, ys) : lex r}"  | 
| 30198 | 5351  | 
by (simp add: lenlex_def Id_on_def lex_prod_def inv_image_def)  | 
| 15302 | 5352  | 
|
5353  | 
lemma Nil_notin_lex [iff]: "([], ys) \<notin> lex r"  | 
|
5354  | 
by (simp add: lex_conv)  | 
|
5355  | 
||
5356  | 
lemma Nil2_notin_lex [iff]: "(xs, []) \<notin> lex r"  | 
|
5357  | 
by (simp add:lex_conv)  | 
|
5358  | 
||
| 18447 | 5359  | 
lemma Cons_in_lex [simp]:  | 
| 15656 | 5360  | 
"((x # xs, y # ys) : lex r) =  | 
5361  | 
((x, y) : r \<and> length xs = length ys | x = y \<and> (xs, ys) : lex r)"  | 
|
| 15302 | 5362  | 
apply (simp add: lex_conv)  | 
5363  | 
apply (rule iffI)  | 
|
5364  | 
prefer 2 apply (blast intro: Cons_eq_appendI, clarify)  | 
|
5365  | 
apply (case_tac xys, simp, simp)  | 
|
5366  | 
apply blast  | 
|
5367  | 
done  | 
|
5368  | 
||
5369  | 
||
| 15656 | 5370  | 
subsubsection {* Lexicographic Ordering *}
 | 
5371  | 
||
5372  | 
text {* Classical lexicographic ordering on lists, ie. "a" < "ab" < "b".
 | 
|
5373  | 
    This ordering does \emph{not} preserve well-foundedness.
 | 
|
| 17090 | 5374  | 
Author: N. Voelker, March 2005. *}  | 
| 15656 | 5375  | 
|
| 50548 | 5376  | 
definition lexord :: "('a \<times> 'a) set \<Rightarrow> ('a list \<times> 'a list) set" where
 | 
5377  | 
"lexord r = {(x,y). \<exists> a v. y = x @ a # v \<or>
 | 
|
| 15656 | 5378  | 
(\<exists> u a b v w. (a,b) \<in> r \<and> x = u @ (a # v) \<and> y = u @ (b # w))}"  | 
5379  | 
||
5380  | 
lemma lexord_Nil_left[simp]: "([],y) \<in> lexord r = (\<exists> a x. y = a # x)"  | 
|
| 24349 | 5381  | 
by (unfold lexord_def, induct_tac y, auto)  | 
| 15656 | 5382  | 
|
5383  | 
lemma lexord_Nil_right[simp]: "(x,[]) \<notin> lexord r"  | 
|
| 24349 | 5384  | 
by (unfold lexord_def, induct_tac x, auto)  | 
| 15656 | 5385  | 
|
5386  | 
lemma lexord_cons_cons[simp]:  | 
|
5387  | 
"((a # x, b # y) \<in> lexord r) = ((a,b)\<in> r | (a = b & (x,y)\<in> lexord r))"  | 
|
5388  | 
apply (unfold lexord_def, safe, simp_all)  | 
|
5389  | 
apply (case_tac u, simp, simp)  | 
|
5390  | 
apply (case_tac u, simp, clarsimp, blast, blast, clarsimp)  | 
|
5391  | 
apply (erule_tac x="b # u" in allE)  | 
|
5392  | 
by force  | 
|
5393  | 
||
5394  | 
lemmas lexord_simps = lexord_Nil_left lexord_Nil_right lexord_cons_cons  | 
|
5395  | 
||
5396  | 
lemma lexord_append_rightI: "\<exists> b z. y = b # z \<Longrightarrow> (x, x @ y) \<in> lexord r"  | 
|
| 24349 | 5397  | 
by (induct_tac x, auto)  | 
| 15656 | 5398  | 
|
5399  | 
lemma lexord_append_left_rightI:  | 
|
5400  | 
"(a,b) \<in> r \<Longrightarrow> (u @ a # x, u @ b # y) \<in> lexord r"  | 
|
| 24349 | 5401  | 
by (induct_tac u, auto)  | 
| 15656 | 5402  | 
|
5403  | 
lemma lexord_append_leftI: " (u,v) \<in> lexord r \<Longrightarrow> (x @ u, x @ v) \<in> lexord r"  | 
|
| 24349 | 5404  | 
by (induct x, auto)  | 
| 15656 | 5405  | 
|
5406  | 
lemma lexord_append_leftD:  | 
|
5407  | 
"\<lbrakk> (x @ u, x @ v) \<in> lexord r; (! a. (a,a) \<notin> r) \<rbrakk> \<Longrightarrow> (u,v) \<in> lexord r"  | 
|
| 24349 | 5408  | 
by (erule rev_mp, induct_tac x, auto)  | 
| 15656 | 5409  | 
|
5410  | 
lemma lexord_take_index_conv:  | 
|
5411  | 
"((x,y) : lexord r) =  | 
|
5412  | 
((length x < length y \<and> take (length x) y = x) \<or>  | 
|
5413  | 
(\<exists>i. i < min(length x)(length y) & take i x = take i y & (x!i,y!i) \<in> r))"  | 
|
5414  | 
apply (unfold lexord_def Let_def, clarsimp)  | 
|
5415  | 
apply (rule_tac f = "(% a b. a \<or> b)" in arg_cong2)  | 
|
5416  | 
apply auto  | 
|
5417  | 
apply (rule_tac x="hd (drop (length x) y)" in exI)  | 
|
5418  | 
apply (rule_tac x="tl (drop (length x) y)" in exI)  | 
|
5419  | 
apply (erule subst, simp add: min_def)  | 
|
5420  | 
apply (rule_tac x ="length u" in exI, simp)  | 
|
5421  | 
apply (rule_tac x ="take i x" in exI)  | 
|
5422  | 
apply (rule_tac x ="x ! i" in exI)  | 
|
5423  | 
apply (rule_tac x ="y ! i" in exI, safe)  | 
|
5424  | 
apply (rule_tac x="drop (Suc i) x" in exI)  | 
|
5425  | 
apply (drule sym, simp add: drop_Suc_conv_tl)  | 
|
5426  | 
apply (rule_tac x="drop (Suc i) y" in exI)  | 
|
5427  | 
by (simp add: drop_Suc_conv_tl)  | 
|
5428  | 
||
5429  | 
-- {* lexord is extension of partial ordering List.lex *} 
 | 
|
| 41986 | 5430  | 
lemma lexord_lex: "(x,y) \<in> lex r = ((x,y) \<in> lexord r \<and> length x = length y)"  | 
| 15656 | 5431  | 
apply (rule_tac x = y in spec)  | 
5432  | 
apply (induct_tac x, clarsimp)  | 
|
5433  | 
by (clarify, case_tac x, simp, force)  | 
|
5434  | 
||
| 41986 | 5435  | 
lemma lexord_irreflexive: "ALL x. (x,x) \<notin> r \<Longrightarrow> (xs,xs) \<notin> lexord r"  | 
5436  | 
by (induct xs) auto  | 
|
5437  | 
||
5438  | 
text{* By Ren\'e Thiemann: *}
 | 
|
5439  | 
lemma lexord_partial_trans:  | 
|
5440  | 
"(\<And>x y z. x \<in> set xs \<Longrightarrow> (x,y) \<in> r \<Longrightarrow> (y,z) \<in> r \<Longrightarrow> (x,z) \<in> r)  | 
|
5441  | 
\<Longrightarrow> (xs,ys) \<in> lexord r \<Longrightarrow> (ys,zs) \<in> lexord r \<Longrightarrow> (xs,zs) \<in> lexord r"  | 
|
5442  | 
proof (induct xs arbitrary: ys zs)  | 
|
5443  | 
case Nil  | 
|
5444  | 
from Nil(3) show ?case unfolding lexord_def by (cases zs, auto)  | 
|
5445  | 
next  | 
|
5446  | 
case (Cons x xs yys zzs)  | 
|
5447  | 
from Cons(3) obtain y ys where yys: "yys = y # ys" unfolding lexord_def  | 
|
5448  | 
by (cases yys, auto)  | 
|
5449  | 
note Cons = Cons[unfolded yys]  | 
|
5450  | 
from Cons(3) have one: "(x,y) \<in> r \<or> x = y \<and> (xs,ys) \<in> lexord r" by auto  | 
|
5451  | 
from Cons(4) obtain z zs where zzs: "zzs = z # zs" unfolding lexord_def  | 
|
5452  | 
by (cases zzs, auto)  | 
|
5453  | 
note Cons = Cons[unfolded zzs]  | 
|
5454  | 
from Cons(4) have two: "(y,z) \<in> r \<or> y = z \<and> (ys,zs) \<in> lexord r" by auto  | 
|
5455  | 
  {
 | 
|
5456  | 
assume "(xs,ys) \<in> lexord r" and "(ys,zs) \<in> lexord r"  | 
|
5457  | 
from Cons(1)[OF _ this] Cons(2)  | 
|
5458  | 
have "(xs,zs) \<in> lexord r" by auto  | 
|
5459  | 
} note ind1 = this  | 
|
5460  | 
  {
 | 
|
5461  | 
assume "(x,y) \<in> r" and "(y,z) \<in> r"  | 
|
5462  | 
from Cons(2)[OF _ this] have "(x,z) \<in> r" by auto  | 
|
5463  | 
} note ind2 = this  | 
|
5464  | 
from one two ind1 ind2  | 
|
5465  | 
have "(x,z) \<in> r \<or> x = z \<and> (xs,zs) \<in> lexord r" by blast  | 
|
5466  | 
thus ?case unfolding zzs by auto  | 
|
5467  | 
qed  | 
|
| 15656 | 5468  | 
|
5469  | 
lemma lexord_trans:  | 
|
5470  | 
"\<lbrakk> (x, y) \<in> lexord r; (y, z) \<in> lexord r; trans r \<rbrakk> \<Longrightarrow> (x, z) \<in> lexord r"  | 
|
| 41986 | 5471  | 
by(auto simp: trans_def intro:lexord_partial_trans)  | 
| 15656 | 5472  | 
|
5473  | 
lemma lexord_transI: "trans r \<Longrightarrow> trans (lexord r)"  | 
|
| 24349 | 5474  | 
by (rule transI, drule lexord_trans, blast)  | 
| 15656 | 5475  | 
|
5476  | 
lemma lexord_linear: "(! a b. (a,b)\<in> r | a = b | (b,a) \<in> r) \<Longrightarrow> (x,y) : lexord r | x = y | (y,x) : lexord r"  | 
|
5477  | 
apply (rule_tac x = y in spec)  | 
|
5478  | 
apply (induct_tac x, rule allI)  | 
|
5479  | 
apply (case_tac x, simp, simp)  | 
|
5480  | 
apply (rule allI, case_tac x, simp, simp)  | 
|
5481  | 
by blast  | 
|
5482  | 
||
| 56545 | 5483  | 
lemma lexord_irrefl:  | 
5484  | 
"irrefl R \<Longrightarrow> irrefl (lexord R)"  | 
|
5485  | 
by (simp add: irrefl_def lexord_irreflexive)  | 
|
5486  | 
||
5487  | 
lemma lexord_asym:  | 
|
5488  | 
assumes "asym R"  | 
|
5489  | 
shows "asym (lexord R)"  | 
|
5490  | 
proof  | 
|
5491  | 
from assms obtain "irrefl R" by (blast elim: asym.cases)  | 
|
5492  | 
then show "irrefl (lexord R)" by (rule lexord_irrefl)  | 
|
5493  | 
next  | 
|
5494  | 
fix xs ys  | 
|
5495  | 
assume "(xs, ys) \<in> lexord R"  | 
|
5496  | 
then show "(ys, xs) \<notin> lexord R"  | 
|
5497  | 
proof (induct xs arbitrary: ys)  | 
|
5498  | 
case Nil  | 
|
5499  | 
then show ?case by simp  | 
|
5500  | 
next  | 
|
5501  | 
case (Cons x xs)  | 
|
5502  | 
then obtain z zs where ys: "ys = z # zs" by (cases ys) auto  | 
|
5503  | 
with assms Cons show ?case by (auto elim: asym.cases)  | 
|
5504  | 
qed  | 
|
5505  | 
qed  | 
|
5506  | 
||
5507  | 
lemma lexord_asymmetric:  | 
|
5508  | 
assumes "asym R"  | 
|
5509  | 
assumes hyp: "(a, b) \<in> lexord R"  | 
|
5510  | 
shows "(b, a) \<notin> lexord R"  | 
|
5511  | 
proof -  | 
|
5512  | 
from `asym R` have "asym (lexord R)" by (rule lexord_asym)  | 
|
5513  | 
then show ?thesis by (rule asym.cases) (auto simp add: hyp)  | 
|
5514  | 
qed  | 
|
5515  | 
||
5516  | 
||
| 
54593
 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 
Andreas Lochbihler 
parents: 
54498 
diff
changeset
 | 
5517  | 
text {*
 | 
| 
 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 
Andreas Lochbihler 
parents: 
54498 
diff
changeset
 | 
5518  | 
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
 | 
5519  | 
Author: Andreas Lochbihler  | 
| 
 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 
Andreas Lochbihler 
parents: 
54498 
diff
changeset
 | 
5520  | 
*}  | 
| 
 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 
Andreas Lochbihler 
parents: 
54498 
diff
changeset
 | 
5521  | 
|
| 
 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 
Andreas Lochbihler 
parents: 
54498 
diff
changeset
 | 
5522  | 
context ord begin  | 
| 
 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 
Andreas Lochbihler 
parents: 
54498 
diff
changeset
 | 
5523  | 
|
| 
 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 
Andreas Lochbihler 
parents: 
54498 
diff
changeset
 | 
5524  | 
inductive lexordp :: "'a list \<Rightarrow> 'a list \<Rightarrow> bool"  | 
| 
 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 
Andreas Lochbihler 
parents: 
54498 
diff
changeset
 | 
5525  | 
where  | 
| 
 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 
Andreas Lochbihler 
parents: 
54498 
diff
changeset
 | 
5526  | 
Nil: "lexordp [] (y # ys)"  | 
| 
 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 
Andreas Lochbihler 
parents: 
54498 
diff
changeset
 | 
5527  | 
| Cons: "x < y \<Longrightarrow> lexordp (x # xs) (y # ys)"  | 
| 
 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 
Andreas Lochbihler 
parents: 
54498 
diff
changeset
 | 
5528  | 
| Cons_eq:  | 
| 
 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 
Andreas Lochbihler 
parents: 
54498 
diff
changeset
 | 
5529  | 
"\<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
 | 
5530  | 
|
| 
 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 
Andreas Lochbihler 
parents: 
54498 
diff
changeset
 | 
5531  | 
lemma lexordp_simps [simp]:  | 
| 
 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 
Andreas Lochbihler 
parents: 
54498 
diff
changeset
 | 
5532  | 
"lexordp [] ys = (ys \<noteq> [])"  | 
| 
 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 
Andreas Lochbihler 
parents: 
54498 
diff
changeset
 | 
5533  | 
"lexordp xs [] = False"  | 
| 
 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 
Andreas Lochbihler 
parents: 
54498 
diff
changeset
 | 
5534  | 
"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
 | 
5535  | 
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
 | 
5536  | 
|
| 
 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 
Andreas Lochbihler 
parents: 
54498 
diff
changeset
 | 
5537  | 
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
 | 
5538  | 
Nil: "lexordp_eq [] ys"  | 
| 
 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 
Andreas Lochbihler 
parents: 
54498 
diff
changeset
 | 
5539  | 
| 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
 | 
5540  | 
| 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
 | 
5541  | 
|
| 
 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 
Andreas Lochbihler 
parents: 
54498 
diff
changeset
 | 
5542  | 
lemma lexordp_eq_simps [simp]:  | 
| 
 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 
Andreas Lochbihler 
parents: 
54498 
diff
changeset
 | 
5543  | 
"lexordp_eq [] ys = True"  | 
| 
 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 
Andreas Lochbihler 
parents: 
54498 
diff
changeset
 | 
5544  | 
"lexordp_eq xs [] \<longleftrightarrow> xs = []"  | 
| 
 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 
Andreas Lochbihler 
parents: 
54498 
diff
changeset
 | 
5545  | 
"lexordp_eq (x # xs) [] = False"  | 
| 
 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 
Andreas Lochbihler 
parents: 
54498 
diff
changeset
 | 
5546  | 
"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
 | 
5547  | 
by(subst lexordp_eq.simps, fastforce)+  | 
| 
 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 
Andreas Lochbihler 
parents: 
54498 
diff
changeset
 | 
5548  | 
|
| 
 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 
Andreas Lochbihler 
parents: 
54498 
diff
changeset
 | 
5549  | 
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
 | 
5550  | 
by(induct xs)(auto simp add: neq_Nil_conv)  | 
| 
 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 
Andreas Lochbihler 
parents: 
54498 
diff
changeset
 | 
5551  | 
|
| 
 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 
Andreas Lochbihler 
parents: 
54498 
diff
changeset
 | 
5552  | 
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
 | 
5553  | 
by(induct us) auto  | 
| 
 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 
Andreas Lochbihler 
parents: 
54498 
diff
changeset
 | 
5554  | 
|
| 
 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 
Andreas Lochbihler 
parents: 
54498 
diff
changeset
 | 
5555  | 
lemma lexordp_eq_refl: "lexordp_eq xs xs"  | 
| 
 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 
Andreas Lochbihler 
parents: 
54498 
diff
changeset
 | 
5556  | 
by(induct xs) simp_all  | 
| 
 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 
Andreas Lochbihler 
parents: 
54498 
diff
changeset
 | 
5557  | 
|
| 
 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 
Andreas Lochbihler 
parents: 
54498 
diff
changeset
 | 
5558  | 
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
 | 
5559  | 
by(induct xs) auto  | 
| 
 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 
Andreas Lochbihler 
parents: 
54498 
diff
changeset
 | 
5560  | 
|
| 
 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 
Andreas Lochbihler 
parents: 
54498 
diff
changeset
 | 
5561  | 
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
 | 
5562  | 
by(induct xs) auto  | 
| 
 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 
Andreas Lochbihler 
parents: 
54498 
diff
changeset
 | 
5563  | 
|
| 
 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 
Andreas Lochbihler 
parents: 
54498 
diff
changeset
 | 
5564  | 
lemma lexordp_irreflexive:  | 
| 
 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 
Andreas Lochbihler 
parents: 
54498 
diff
changeset
 | 
5565  | 
assumes irrefl: "\<forall>x. \<not> x < x"  | 
| 
 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 
Andreas Lochbihler 
parents: 
54498 
diff
changeset
 | 
5566  | 
shows "\<not> lexordp xs xs"  | 
| 
 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 
Andreas Lochbihler 
parents: 
54498 
diff
changeset
 | 
5567  | 
proof  | 
| 
 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 
Andreas Lochbihler 
parents: 
54498 
diff
changeset
 | 
5568  | 
assume "lexordp xs xs"  | 
| 
 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 
Andreas Lochbihler 
parents: 
54498 
diff
changeset
 | 
5569  | 
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
 | 
5570  | 
qed  | 
| 
 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 
Andreas Lochbihler 
parents: 
54498 
diff
changeset
 | 
5571  | 
|
| 
 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 
Andreas Lochbihler 
parents: 
54498 
diff
changeset
 | 
5572  | 
lemma lexordp_into_lexordp_eq:  | 
| 
 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 
Andreas Lochbihler 
parents: 
54498 
diff
changeset
 | 
5573  | 
assumes "lexordp xs ys"  | 
| 
 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 
Andreas Lochbihler 
parents: 
54498 
diff
changeset
 | 
5574  | 
shows "lexordp_eq xs ys"  | 
| 
 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 
Andreas Lochbihler 
parents: 
54498 
diff
changeset
 | 
5575  | 
using assms by induct simp_all  | 
| 
 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 
Andreas Lochbihler 
parents: 
54498 
diff
changeset
 | 
5576  | 
|
| 
 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 
Andreas Lochbihler 
parents: 
54498 
diff
changeset
 | 
5577  | 
end  | 
| 
 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 
Andreas Lochbihler 
parents: 
54498 
diff
changeset
 | 
5578  | 
|
| 
 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 
Andreas Lochbihler 
parents: 
54498 
diff
changeset
 | 
5579  | 
declare ord.lexordp_simps [simp, code]  | 
| 
 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 
Andreas Lochbihler 
parents: 
54498 
diff
changeset
 | 
5580  | 
declare ord.lexordp_eq_simps [code, simp]  | 
| 
 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 
Andreas Lochbihler 
parents: 
54498 
diff
changeset
 | 
5581  | 
|
| 
 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 
Andreas Lochbihler 
parents: 
54498 
diff
changeset
 | 
5582  | 
lemma lexord_code [code, code_unfold]: "lexordp = ord.lexordp less"  | 
| 
 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 
Andreas Lochbihler 
parents: 
54498 
diff
changeset
 | 
5583  | 
unfolding lexordp_def ord.lexordp_def ..  | 
| 
 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 
Andreas Lochbihler 
parents: 
54498 
diff
changeset
 | 
5584  | 
|
| 
 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 
Andreas Lochbihler 
parents: 
54498 
diff
changeset
 | 
5585  | 
context order begin  | 
| 
 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 
Andreas Lochbihler 
parents: 
54498 
diff
changeset
 | 
5586  | 
|
| 
 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 
Andreas Lochbihler 
parents: 
54498 
diff
changeset
 | 
5587  | 
lemma lexordp_antisym:  | 
| 
 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 
Andreas Lochbihler 
parents: 
54498 
diff
changeset
 | 
5588  | 
assumes "lexordp xs ys" "lexordp ys xs"  | 
| 
 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 
Andreas Lochbihler 
parents: 
54498 
diff
changeset
 | 
5589  | 
shows False  | 
| 
 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 
Andreas Lochbihler 
parents: 
54498 
diff
changeset
 | 
5590  | 
using assms by induct auto  | 
| 
 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 
Andreas Lochbihler 
parents: 
54498 
diff
changeset
 | 
5591  | 
|
| 
 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 
Andreas Lochbihler 
parents: 
54498 
diff
changeset
 | 
5592  | 
lemma lexordp_irreflexive': "\<not> lexordp xs xs"  | 
| 
 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 
Andreas Lochbihler 
parents: 
54498 
diff
changeset
 | 
5593  | 
by(rule lexordp_irreflexive) simp  | 
| 
 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 
Andreas Lochbihler 
parents: 
54498 
diff
changeset
 | 
5594  | 
|
| 
 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 
Andreas Lochbihler 
parents: 
54498 
diff
changeset
 | 
5595  | 
end  | 
| 
 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 
Andreas Lochbihler 
parents: 
54498 
diff
changeset
 | 
5596  | 
|
| 
 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 
Andreas Lochbihler 
parents: 
54498 
diff
changeset
 | 
5597  | 
context linorder begin  | 
| 
 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 
Andreas Lochbihler 
parents: 
54498 
diff
changeset
 | 
5598  | 
|
| 
 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 
Andreas Lochbihler 
parents: 
54498 
diff
changeset
 | 
5599  | 
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
 | 
5600  | 
assumes "lexordp xs ys"  | 
| 
 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 
Andreas Lochbihler 
parents: 
54498 
diff
changeset
 | 
5601  | 
obtains (Nil) y ys' where "xs = []" "ys = y # ys'"  | 
| 
 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 
Andreas Lochbihler 
parents: 
54498 
diff
changeset
 | 
5602  | 
| (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
 | 
5603  | 
| (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
 | 
5604  | 
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
 | 
5605  | 
|
| 
 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 
Andreas Lochbihler 
parents: 
54498 
diff
changeset
 | 
5606  | 
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
 | 
5607  | 
assumes major: "lexordp xs ys"  | 
| 
 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 
Andreas Lochbihler 
parents: 
54498 
diff
changeset
 | 
5608  | 
and Nil: "\<And>y ys. P [] (y # ys)"  | 
| 
 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 
Andreas Lochbihler 
parents: 
54498 
diff
changeset
 | 
5609  | 
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
 | 
5610  | 
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
 | 
5611  | 
shows "P xs ys"  | 
| 
 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 
Andreas Lochbihler 
parents: 
54498 
diff
changeset
 | 
5612  | 
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
 | 
5613  | 
|
| 
 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 
Andreas Lochbihler 
parents: 
54498 
diff
changeset
 | 
5614  | 
lemma lexordp_iff:  | 
| 
 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 
Andreas Lochbihler 
parents: 
54498 
diff
changeset
 | 
5615  | 
"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
 | 
5616  | 
(is "?lhs = ?rhs")  | 
| 
 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 
Andreas Lochbihler 
parents: 
54498 
diff
changeset
 | 
5617  | 
proof  | 
| 
 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 
Andreas Lochbihler 
parents: 
54498 
diff
changeset
 | 
5618  | 
assume ?lhs thus ?rhs  | 
| 
 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 
Andreas Lochbihler 
parents: 
54498 
diff
changeset
 | 
5619  | 
proof induct  | 
| 
 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 
Andreas Lochbihler 
parents: 
54498 
diff
changeset
 | 
5620  | 
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
 | 
5621  | 
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
 | 
5622  | 
next  | 
| 
 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 
Andreas Lochbihler 
parents: 
54498 
diff
changeset
 | 
5623  | 
assume ?rhs thus ?lhs  | 
| 
 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 
Andreas Lochbihler 
parents: 
54498 
diff
changeset
 | 
5624  | 
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
 | 
5625  | 
qed  | 
| 
 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 
Andreas Lochbihler 
parents: 
54498 
diff
changeset
 | 
5626  | 
|
| 
 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 
Andreas Lochbihler 
parents: 
54498 
diff
changeset
 | 
5627  | 
lemma lexordp_conv_lexord:  | 
| 
 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 
Andreas Lochbihler 
parents: 
54498 
diff
changeset
 | 
5628  | 
  "lexordp xs ys \<longleftrightarrow> (xs, ys) \<in> lexord {(x, y). x < y}"
 | 
| 
 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 
Andreas Lochbihler 
parents: 
54498 
diff
changeset
 | 
5629  | 
by(simp add: lexordp_iff lexord_def)  | 
| 
 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 
Andreas Lochbihler 
parents: 
54498 
diff
changeset
 | 
5630  | 
|
| 
 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 
Andreas Lochbihler 
parents: 
54498 
diff
changeset
 | 
5631  | 
lemma lexordp_eq_antisym:  | 
| 
 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 
Andreas Lochbihler 
parents: 
54498 
diff
changeset
 | 
5632  | 
assumes "lexordp_eq xs ys" "lexordp_eq ys xs"  | 
| 
 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 
Andreas Lochbihler 
parents: 
54498 
diff
changeset
 | 
5633  | 
shows "xs = ys"  | 
| 
 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 
Andreas Lochbihler 
parents: 
54498 
diff
changeset
 | 
5634  | 
using assms by induct simp_all  | 
| 
 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 
Andreas Lochbihler 
parents: 
54498 
diff
changeset
 | 
5635  | 
|
| 
 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 
Andreas Lochbihler 
parents: 
54498 
diff
changeset
 | 
5636  | 
lemma lexordp_eq_trans:  | 
| 
 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 
Andreas Lochbihler 
parents: 
54498 
diff
changeset
 | 
5637  | 
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
 | 
5638  | 
shows "lexordp_eq xs zs"  | 
| 
 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 
Andreas Lochbihler 
parents: 
54498 
diff
changeset
 | 
5639  | 
using assms  | 
| 
 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 
Andreas Lochbihler 
parents: 
54498 
diff
changeset
 | 
5640  | 
apply(induct arbitrary: zs)  | 
| 
 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 
Andreas Lochbihler 
parents: 
54498 
diff
changeset
 | 
5641  | 
apply(case_tac [2-3] zs)  | 
| 
 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 
Andreas Lochbihler 
parents: 
54498 
diff
changeset
 | 
5642  | 
apply auto  | 
| 
 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 
Andreas Lochbihler 
parents: 
54498 
diff
changeset
 | 
5643  | 
done  | 
| 
 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 
Andreas Lochbihler 
parents: 
54498 
diff
changeset
 | 
5644  | 
|
| 
 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 
Andreas Lochbihler 
parents: 
54498 
diff
changeset
 | 
5645  | 
lemma lexordp_trans:  | 
| 
 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 
Andreas Lochbihler 
parents: 
54498 
diff
changeset
 | 
5646  | 
assumes "lexordp xs ys" "lexordp ys zs"  | 
| 
 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 
Andreas Lochbihler 
parents: 
54498 
diff
changeset
 | 
5647  | 
shows "lexordp xs zs"  | 
| 
 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 
Andreas Lochbihler 
parents: 
54498 
diff
changeset
 | 
5648  | 
using assms  | 
| 
 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 
Andreas Lochbihler 
parents: 
54498 
diff
changeset
 | 
5649  | 
apply(induct arbitrary: zs)  | 
| 
 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 
Andreas Lochbihler 
parents: 
54498 
diff
changeset
 | 
5650  | 
apply(case_tac [2-3] zs)  | 
| 
 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 
Andreas Lochbihler 
parents: 
54498 
diff
changeset
 | 
5651  | 
apply auto  | 
| 
 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 
Andreas Lochbihler 
parents: 
54498 
diff
changeset
 | 
5652  | 
done  | 
| 
 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 
Andreas Lochbihler 
parents: 
54498 
diff
changeset
 | 
5653  | 
|
| 
 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 
Andreas Lochbihler 
parents: 
54498 
diff
changeset
 | 
5654  | 
lemma lexordp_linear: "lexordp xs ys \<or> xs = ys \<or> lexordp ys xs"  | 
| 
 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 
Andreas Lochbihler 
parents: 
54498 
diff
changeset
 | 
5655  | 
proof(induct xs arbitrary: ys)  | 
| 
 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 
Andreas Lochbihler 
parents: 
54498 
diff
changeset
 | 
5656  | 
case Nil thus ?case by(cases ys) simp_all  | 
| 
 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 
Andreas Lochbihler 
parents: 
54498 
diff
changeset
 | 
5657  | 
next  | 
| 
 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 
Andreas Lochbihler 
parents: 
54498 
diff
changeset
 | 
5658  | 
case Cons thus ?case by(cases ys) auto  | 
| 
 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 
Andreas Lochbihler 
parents: 
54498 
diff
changeset
 | 
5659  | 
qed  | 
| 
 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 
Andreas Lochbihler 
parents: 
54498 
diff
changeset
 | 
5660  | 
|
| 
 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 
Andreas Lochbihler 
parents: 
54498 
diff
changeset
 | 
5661  | 
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
 | 
5662  | 
(is "?lhs \<longleftrightarrow> ?rhs")  | 
| 
 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 
Andreas Lochbihler 
parents: 
54498 
diff
changeset
 | 
5663  | 
proof  | 
| 
 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 
Andreas Lochbihler 
parents: 
54498 
diff
changeset
 | 
5664  | 
assume ?lhs  | 
| 
 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 
Andreas Lochbihler 
parents: 
54498 
diff
changeset
 | 
5665  | 
moreover hence "\<not> lexordp_eq ys xs" by induct simp_all  | 
| 
 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 
Andreas Lochbihler 
parents: 
54498 
diff
changeset
 | 
5666  | 
ultimately show ?rhs by(simp add: lexordp_into_lexordp_eq)  | 
| 
 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 
Andreas Lochbihler 
parents: 
54498 
diff
changeset
 | 
5667  | 
next  | 
| 
 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 
Andreas Lochbihler 
parents: 
54498 
diff
changeset
 | 
5668  | 
assume ?rhs  | 
| 
 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 
Andreas Lochbihler 
parents: 
54498 
diff
changeset
 | 
5669  | 
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
 | 
5670  | 
thus ?lhs by induct simp_all  | 
| 
 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 
Andreas Lochbihler 
parents: 
54498 
diff
changeset
 | 
5671  | 
qed  | 
| 
 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 
Andreas Lochbihler 
parents: 
54498 
diff
changeset
 | 
5672  | 
|
| 
 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 
Andreas Lochbihler 
parents: 
54498 
diff
changeset
 | 
5673  | 
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
 | 
5674  | 
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
 | 
5675  | 
|
| 
 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 
Andreas Lochbihler 
parents: 
54498 
diff
changeset
 | 
5676  | 
lemma lexordp_eq_linear: "lexordp_eq xs ys \<or> lexordp_eq ys xs"  | 
| 
 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 
Andreas Lochbihler 
parents: 
54498 
diff
changeset
 | 
5677  | 
apply(induct xs arbitrary: ys)  | 
| 
 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 
Andreas Lochbihler 
parents: 
54498 
diff
changeset
 | 
5678  | 
apply(case_tac [!] ys)  | 
| 
 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 
Andreas Lochbihler 
parents: 
54498 
diff
changeset
 | 
5679  | 
apply auto  | 
| 
 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 
Andreas Lochbihler 
parents: 
54498 
diff
changeset
 | 
5680  | 
done  | 
| 
 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 
Andreas Lochbihler 
parents: 
54498 
diff
changeset
 | 
5681  | 
|
| 
 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 
Andreas Lochbihler 
parents: 
54498 
diff
changeset
 | 
5682  | 
lemma lexordp_linorder: "class.linorder lexordp_eq lexordp"  | 
| 
 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 
Andreas Lochbihler 
parents: 
54498 
diff
changeset
 | 
5683  | 
by unfold_locales(auto simp add: lexordp_conv_lexordp_eq lexordp_eq_refl lexordp_eq_antisym intro: lexordp_eq_trans del: disjCI intro: lexordp_eq_linear)  | 
| 
 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 
Andreas Lochbihler 
parents: 
54498 
diff
changeset
 | 
5684  | 
|
| 
 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 
Andreas Lochbihler 
parents: 
54498 
diff
changeset
 | 
5685  | 
end  | 
| 15656 | 5686  | 
|
| 40230 | 5687  | 
subsubsection {* Lexicographic combination of measure functions *}
 | 
| 
21103
 
367b4ad7c7cc
Added "measures" combinator for lexicographic combinations of multiple measures.
 
krauss 
parents: 
21079 
diff
changeset
 | 
5688  | 
|
| 
 
367b4ad7c7cc
Added "measures" combinator for lexicographic combinations of multiple measures.
 
krauss 
parents: 
21079 
diff
changeset
 | 
5689  | 
text {* These are useful for termination proofs *}
 | 
| 
 
367b4ad7c7cc
Added "measures" combinator for lexicographic combinations of multiple measures.
 
krauss 
parents: 
21079 
diff
changeset
 | 
5690  | 
|
| 50548 | 5691  | 
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
 | 
5692  | 
|
| 
44013
 
5cfc1c36ae97
moved recdef package to HOL/Library/Old_Recdef.thy
 
krauss 
parents: 
43594 
diff
changeset
 | 
5693  | 
lemma wf_measures[simp]: "wf (measures fs)"  | 
| 24349 | 5694  | 
unfolding measures_def  | 
5695  | 
by blast  | 
|
| 
21103
 
367b4ad7c7cc
Added "measures" combinator for lexicographic combinations of multiple measures.
 
krauss 
parents: 
21079 
diff
changeset
 | 
5696  | 
|
| 
 
367b4ad7c7cc
Added "measures" combinator for lexicographic combinations of multiple measures.
 
krauss 
parents: 
21079 
diff
changeset
 | 
5697  | 
lemma in_measures[simp]:  | 
| 
 
367b4ad7c7cc
Added "measures" combinator for lexicographic combinations of multiple measures.
 
krauss 
parents: 
21079 
diff
changeset
 | 
5698  | 
"(x, y) \<in> measures [] = False"  | 
| 
 
367b4ad7c7cc
Added "measures" combinator for lexicographic combinations of multiple measures.
 
krauss 
parents: 
21079 
diff
changeset
 | 
5699  | 
"(x, y) \<in> measures (f # fs)  | 
| 
 
367b4ad7c7cc
Added "measures" combinator for lexicographic combinations of multiple measures.
 
krauss 
parents: 
21079 
diff
changeset
 | 
5700  | 
= (f x < f y \<or> (f x = f y \<and> (x, y) \<in> measures fs))"  | 
| 24349 | 5701  | 
unfolding measures_def  | 
5702  | 
by auto  | 
|
| 
21103
 
367b4ad7c7cc
Added "measures" combinator for lexicographic combinations of multiple measures.
 
krauss 
parents: 
21079 
diff
changeset
 | 
5703  | 
|
| 
 
367b4ad7c7cc
Added "measures" combinator for lexicographic combinations of multiple measures.
 
krauss 
parents: 
21079 
diff
changeset
 | 
5704  | 
lemma measures_less: "f x < f y ==> (x, y) \<in> measures (f#fs)"  | 
| 24349 | 5705  | 
by simp  | 
| 
21103
 
367b4ad7c7cc
Added "measures" combinator for lexicographic combinations of multiple measures.
 
krauss 
parents: 
21079 
diff
changeset
 | 
5706  | 
|
| 
 
367b4ad7c7cc
Added "measures" combinator for lexicographic combinations of multiple measures.
 
krauss 
parents: 
21079 
diff
changeset
 | 
5707  | 
lemma measures_lesseq: "f x <= f y ==> (x, y) \<in> measures fs ==> (x, y) \<in> measures (f#fs)"  | 
| 24349 | 5708  | 
by auto  | 
| 
21103
 
367b4ad7c7cc
Added "measures" combinator for lexicographic combinations of multiple measures.
 
krauss 
parents: 
21079 
diff
changeset
 | 
5709  | 
|
| 
 
367b4ad7c7cc
Added "measures" combinator for lexicographic combinations of multiple measures.
 
krauss 
parents: 
21079 
diff
changeset
 | 
5710  | 
|
| 40230 | 5711  | 
subsubsection {* Lifting Relations to Lists: one element *}
 | 
5712  | 
||
5713  | 
definition listrel1 :: "('a \<times> 'a) set \<Rightarrow> ('a list \<times> 'a list) set" where
 | 
|
5714  | 
"listrel1 r = {(xs,ys).
 | 
|
5715  | 
\<exists>us z z' vs. xs = us @ z # vs \<and> (z,z') \<in> r \<and> ys = us @ z' # vs}"  | 
|
5716  | 
||
5717  | 
lemma listrel1I:  | 
|
5718  | 
"\<lbrakk> (x, y) \<in> r; xs = us @ x # vs; ys = us @ y # vs \<rbrakk> \<Longrightarrow>  | 
|
5719  | 
(xs, ys) \<in> listrel1 r"  | 
|
5720  | 
unfolding listrel1_def by auto  | 
|
5721  | 
||
5722  | 
lemma listrel1E:  | 
|
5723  | 
"\<lbrakk> (xs, ys) \<in> listrel1 r;  | 
|
5724  | 
!!x y us vs. \<lbrakk> (x, y) \<in> r; xs = us @ x # vs; ys = us @ y # vs \<rbrakk> \<Longrightarrow> P  | 
|
5725  | 
\<rbrakk> \<Longrightarrow> P"  | 
|
5726  | 
unfolding listrel1_def by auto  | 
|
5727  | 
||
5728  | 
lemma not_Nil_listrel1 [iff]: "([], xs) \<notin> listrel1 r"  | 
|
5729  | 
unfolding listrel1_def by blast  | 
|
5730  | 
||
5731  | 
lemma not_listrel1_Nil [iff]: "(xs, []) \<notin> listrel1 r"  | 
|
5732  | 
unfolding listrel1_def by blast  | 
|
5733  | 
||
5734  | 
lemma Cons_listrel1_Cons [iff]:  | 
|
5735  | 
"(x # xs, y # ys) \<in> listrel1 r \<longleftrightarrow>  | 
|
5736  | 
(x,y) \<in> r \<and> xs = ys \<or> x = y \<and> (xs, ys) \<in> listrel1 r"  | 
|
5737  | 
by (simp add: listrel1_def Cons_eq_append_conv) (blast)  | 
|
5738  | 
||
5739  | 
lemma listrel1I1: "(x,y) \<in> r \<Longrightarrow> (x # xs, y # xs) \<in> listrel1 r"  | 
|
| 56085 | 5740  | 
by fast  | 
| 40230 | 5741  | 
|
5742  | 
lemma listrel1I2: "(xs, ys) \<in> listrel1 r \<Longrightarrow> (x # xs, x # ys) \<in> listrel1 r"  | 
|
| 56085 | 5743  | 
by fast  | 
| 40230 | 5744  | 
|
5745  | 
lemma append_listrel1I:  | 
|
5746  | 
"(xs, ys) \<in> listrel1 r \<and> us = vs \<or> xs = ys \<and> (us, vs) \<in> listrel1 r  | 
|
5747  | 
\<Longrightarrow> (xs @ us, ys @ vs) \<in> listrel1 r"  | 
|
5748  | 
unfolding listrel1_def  | 
|
5749  | 
by auto (blast intro: append_eq_appendI)+  | 
|
5750  | 
||
5751  | 
lemma Cons_listrel1E1[elim!]:  | 
|
5752  | 
assumes "(x # xs, ys) \<in> listrel1 r"  | 
|
5753  | 
and "\<And>y. ys = y # xs \<Longrightarrow> (x, y) \<in> r \<Longrightarrow> R"  | 
|
5754  | 
and "\<And>zs. ys = x # zs \<Longrightarrow> (xs, zs) \<in> listrel1 r \<Longrightarrow> R"  | 
|
5755  | 
shows R  | 
|
5756  | 
using assms by (cases ys) blast+  | 
|
5757  | 
||
5758  | 
lemma Cons_listrel1E2[elim!]:  | 
|
5759  | 
assumes "(xs, y # ys) \<in> listrel1 r"  | 
|
5760  | 
and "\<And>x. xs = x # ys \<Longrightarrow> (x, y) \<in> r \<Longrightarrow> R"  | 
|
5761  | 
and "\<And>zs. xs = y # zs \<Longrightarrow> (zs, ys) \<in> listrel1 r \<Longrightarrow> R"  | 
|
5762  | 
shows R  | 
|
5763  | 
using assms by (cases xs) blast+  | 
|
5764  | 
||
5765  | 
lemma snoc_listrel1_snoc_iff:  | 
|
5766  | 
"(xs @ [x], ys @ [y]) \<in> listrel1 r  | 
|
5767  | 
\<longleftrightarrow> (xs, ys) \<in> listrel1 r \<and> x = y \<or> xs = ys \<and> (x,y) \<in> r" (is "?L \<longleftrightarrow> ?R")  | 
|
5768  | 
proof  | 
|
5769  | 
assume ?L thus ?R  | 
|
| 
44890
 
22f665a2e91c
new fastforce replacing fastsimp - less confusing name
 
nipkow 
parents: 
44635 
diff
changeset
 | 
5770  | 
by (fastforce simp: listrel1_def snoc_eq_iff_butlast butlast_append)  | 
| 40230 | 5771  | 
next  | 
5772  | 
assume ?R then show ?L unfolding listrel1_def by force  | 
|
5773  | 
qed  | 
|
5774  | 
||
5775  | 
lemma listrel1_eq_len: "(xs,ys) \<in> listrel1 r \<Longrightarrow> length xs = length ys"  | 
|
5776  | 
unfolding listrel1_def by auto  | 
|
5777  | 
||
5778  | 
lemma listrel1_mono:  | 
|
5779  | 
"r \<subseteq> s \<Longrightarrow> listrel1 r \<subseteq> listrel1 s"  | 
|
5780  | 
unfolding listrel1_def by blast  | 
|
5781  | 
||
5782  | 
||
5783  | 
lemma listrel1_converse: "listrel1 (r^-1) = (listrel1 r)^-1"  | 
|
5784  | 
unfolding listrel1_def by blast  | 
|
5785  | 
||
5786  | 
lemma in_listrel1_converse:  | 
|
5787  | 
"(x,y) : listrel1 (r^-1) \<longleftrightarrow> (x,y) : (listrel1 r)^-1"  | 
|
5788  | 
unfolding listrel1_def by blast  | 
|
5789  | 
||
5790  | 
lemma listrel1_iff_update:  | 
|
5791  | 
"(xs,ys) \<in> (listrel1 r)  | 
|
5792  | 
\<longleftrightarrow> (\<exists>y n. (xs ! n, y) \<in> r \<and> n < length xs \<and> ys = xs[n:=y])" (is "?L \<longleftrightarrow> ?R")  | 
|
5793  | 
proof  | 
|
5794  | 
assume "?L"  | 
|
5795  | 
then obtain x y u v where "xs = u @ x # v" "ys = u @ y # v" "(x,y) \<in> r"  | 
|
5796  | 
unfolding listrel1_def by auto  | 
|
5797  | 
then have "ys = xs[length u := y]" and "length u < length xs"  | 
|
5798  | 
and "(xs ! length u, y) \<in> r" by auto  | 
|
5799  | 
then show "?R" by auto  | 
|
5800  | 
next  | 
|
5801  | 
assume "?R"  | 
|
5802  | 
then obtain x y n where "(xs!n, y) \<in> r" "n < size xs" "ys = xs[n:=y]" "x = xs!n"  | 
|
5803  | 
by auto  | 
|
5804  | 
then obtain u v where "xs = u @ x # v" and "ys = u @ y # v" and "(x, y) \<in> r"  | 
|
5805  | 
by (auto intro: upd_conv_take_nth_drop id_take_nth_drop)  | 
|
5806  | 
then show "?L" by (auto simp: listrel1_def)  | 
|
5807  | 
qed  | 
|
5808  | 
||
5809  | 
||
| 44510 | 5810  | 
text{* Accessible part and wellfoundedness: *}
 | 
| 40230 | 5811  | 
|
5812  | 
lemma Cons_acc_listrel1I [intro!]:  | 
|
| 54295 | 5813  | 
"x \<in> Wellfounded.acc r \<Longrightarrow> xs \<in> Wellfounded.acc (listrel1 r) \<Longrightarrow> (x # xs) \<in> Wellfounded.acc (listrel1 r)"  | 
5814  | 
apply (induct arbitrary: xs set: Wellfounded.acc)  | 
|
| 40230 | 5815  | 
apply (erule thin_rl)  | 
5816  | 
apply (erule acc_induct)  | 
|
5817  | 
apply (rule accI)  | 
|
5818  | 
apply (blast)  | 
|
5819  | 
done  | 
|
5820  | 
||
| 54295 | 5821  | 
lemma lists_accD: "xs \<in> lists (Wellfounded.acc r) \<Longrightarrow> xs \<in> Wellfounded.acc (listrel1 r)"  | 
| 40230 | 5822  | 
apply (induct set: lists)  | 
5823  | 
apply (rule accI)  | 
|
5824  | 
apply simp  | 
|
5825  | 
apply (rule accI)  | 
|
5826  | 
apply (fast dest: acc_downward)  | 
|
5827  | 
done  | 
|
5828  | 
||
| 54295 | 5829  | 
lemma lists_accI: "xs \<in> Wellfounded.acc (listrel1 r) \<Longrightarrow> xs \<in> lists (Wellfounded.acc r)"  | 
5830  | 
apply (induct set: Wellfounded.acc)  | 
|
| 40230 | 5831  | 
apply clarify  | 
5832  | 
apply (rule accI)  | 
|
| 
44890
 
22f665a2e91c
new fastforce replacing fastsimp - less confusing name
 
nipkow 
parents: 
44635 
diff
changeset
 | 
5833  | 
apply (fastforce dest!: in_set_conv_decomp[THEN iffD1] simp: listrel1_def)  | 
| 40230 | 5834  | 
done  | 
5835  | 
||
| 44510 | 5836  | 
lemma wf_listrel1_iff[simp]: "wf(listrel1 r) = wf r"  | 
| 56085 | 5837  | 
by (auto simp: wf_acc_iff  | 
5838  | 
intro: lists_accD lists_accI[THEN Cons_in_lists_iff[THEN iffD1, THEN conjunct1]])  | 
|
| 40230 | 5839  | 
|
5840  | 
subsubsection {* Lifting Relations to Lists: all elements *}
 | 
|
| 15302 | 5841  | 
|
| 23740 | 5842  | 
inductive_set  | 
| 46317 | 5843  | 
  listrel :: "('a \<times> 'b) set \<Rightarrow> ('a list \<times> 'b list) set"
 | 
5844  | 
  for r :: "('a \<times> 'b) set"
 | 
|
| 22262 | 5845  | 
where  | 
| 23740 | 5846  | 
Nil: "([],[]) \<in> listrel r"  | 
5847  | 
| Cons: "[| (x,y) \<in> r; (xs,ys) \<in> listrel r |] ==> (x#xs, y#ys) \<in> listrel r"  | 
|
5848  | 
||
5849  | 
inductive_cases listrel_Nil1 [elim!]: "([],xs) \<in> listrel r"  | 
|
5850  | 
inductive_cases listrel_Nil2 [elim!]: "(xs,[]) \<in> listrel r"  | 
|
5851  | 
inductive_cases listrel_Cons1 [elim!]: "(y#ys,xs) \<in> listrel r"  | 
|
5852  | 
inductive_cases listrel_Cons2 [elim!]: "(xs,y#ys) \<in> listrel r"  | 
|
| 15302 | 5853  | 
|
5854  | 
||
| 40230 | 5855  | 
lemma listrel_eq_len: "(xs, ys) \<in> listrel r \<Longrightarrow> length xs = length ys"  | 
5856  | 
by(induct rule: listrel.induct) auto  | 
|
5857  | 
||
| 46313 | 5858  | 
lemma listrel_iff_zip [code_unfold]: "(xs,ys) : listrel r \<longleftrightarrow>  | 
| 40230 | 5859  | 
length xs = length ys & (\<forall>(x,y) \<in> set(zip xs ys). (x,y) \<in> r)" (is "?L \<longleftrightarrow> ?R")  | 
5860  | 
proof  | 
|
5861  | 
assume ?L thus ?R by induct (auto intro: listrel_eq_len)  | 
|
5862  | 
next  | 
|
5863  | 
assume ?R thus ?L  | 
|
5864  | 
apply (clarify)  | 
|
5865  | 
by (induct rule: list_induct2) (auto intro: listrel.intros)  | 
|
5866  | 
qed  | 
|
5867  | 
||
5868  | 
lemma listrel_iff_nth: "(xs,ys) : listrel r \<longleftrightarrow>  | 
|
5869  | 
length xs = length ys & (\<forall>n < length xs. (xs!n, ys!n) \<in> r)" (is "?L \<longleftrightarrow> ?R")  | 
|
5870  | 
by (auto simp add: all_set_conv_all_nth listrel_iff_zip)  | 
|
5871  | 
||
5872  | 
||
| 15302 | 5873  | 
lemma listrel_mono: "r \<subseteq> s \<Longrightarrow> listrel r \<subseteq> listrel s"  | 
5874  | 
apply clarify  | 
|
| 23740 | 5875  | 
apply (erule listrel.induct)  | 
5876  | 
apply (blast intro: listrel.intros)+  | 
|
| 15302 | 5877  | 
done  | 
5878  | 
||
5879  | 
lemma listrel_subset: "r \<subseteq> A \<times> A \<Longrightarrow> listrel r \<subseteq> lists A \<times> lists A"  | 
|
5880  | 
apply clarify  | 
|
| 23740 | 5881  | 
apply (erule listrel.induct, auto)  | 
| 15302 | 5882  | 
done  | 
5883  | 
||
| 30198 | 5884  | 
lemma listrel_refl_on: "refl_on A r \<Longrightarrow> refl_on (lists A) (listrel r)"  | 
5885  | 
apply (simp add: refl_on_def listrel_subset Ball_def)  | 
|
| 15302 | 5886  | 
apply (rule allI)  | 
5887  | 
apply (induct_tac x)  | 
|
| 23740 | 5888  | 
apply (auto intro: listrel.intros)  | 
| 15302 | 5889  | 
done  | 
5890  | 
||
5891  | 
lemma listrel_sym: "sym r \<Longrightarrow> sym (listrel r)"  | 
|
5892  | 
apply (auto simp add: sym_def)  | 
|
| 23740 | 5893  | 
apply (erule listrel.induct)  | 
5894  | 
apply (blast intro: listrel.intros)+  | 
|
| 15302 | 5895  | 
done  | 
5896  | 
||
5897  | 
lemma listrel_trans: "trans r \<Longrightarrow> trans (listrel r)"  | 
|
5898  | 
apply (simp add: trans_def)  | 
|
5899  | 
apply (intro allI)  | 
|
5900  | 
apply (rule impI)  | 
|
| 23740 | 5901  | 
apply (erule listrel.induct)  | 
5902  | 
apply (blast intro: listrel.intros)+  | 
|
| 15302 | 5903  | 
done  | 
5904  | 
||
5905  | 
theorem equiv_listrel: "equiv A r \<Longrightarrow> equiv (lists A) (listrel r)"  | 
|
| 30198 | 5906  | 
by (simp add: equiv_def listrel_refl_on listrel_sym listrel_trans)  | 
| 15302 | 5907  | 
|
| 40230 | 5908  | 
lemma listrel_rtrancl_refl[iff]: "(xs,xs) : listrel(r^*)"  | 
5909  | 
using listrel_refl_on[of UNIV, OF refl_rtrancl]  | 
|
5910  | 
by(auto simp: refl_on_def)  | 
|
5911  | 
||
5912  | 
lemma listrel_rtrancl_trans:  | 
|
5913  | 
"\<lbrakk> (xs,ys) : listrel(r^*); (ys,zs) : listrel(r^*) \<rbrakk>  | 
|
5914  | 
\<Longrightarrow> (xs,zs) : listrel(r^*)"  | 
|
5915  | 
by (metis listrel_trans trans_def trans_rtrancl)  | 
|
5916  | 
||
5917  | 
||
| 15302 | 5918  | 
lemma listrel_Nil [simp]: "listrel r `` {[]} = {[]}"
 | 
| 23740 | 5919  | 
by (blast intro: listrel.intros)  | 
| 15302 | 5920  | 
|
5921  | 
lemma listrel_Cons:  | 
|
| 
33318
 
ddd97d9dfbfb
moved Nat_Transfer before Divides; distributed Nat_Transfer setup accordingly
 
haftmann 
parents: 
32960 
diff
changeset
 | 
5922  | 
     "listrel r `` {x#xs} = set_Cons (r``{x}) (listrel r `` {xs})"
 | 
| 
 
ddd97d9dfbfb
moved Nat_Transfer before Divides; distributed Nat_Transfer setup accordingly
 
haftmann 
parents: 
32960 
diff
changeset
 | 
5923  | 
by (auto simp add: set_Cons_def intro: listrel.intros)  | 
| 15302 | 5924  | 
|
| 40230 | 5925  | 
text {* Relating @{term listrel1}, @{term listrel} and closures: *}
 | 
5926  | 
||
5927  | 
lemma listrel1_rtrancl_subset_rtrancl_listrel1:  | 
|
5928  | 
"listrel1 (r^*) \<subseteq> (listrel1 r)^*"  | 
|
5929  | 
proof (rule subrelI)  | 
|
5930  | 
fix xs ys assume 1: "(xs,ys) \<in> listrel1 (r^*)"  | 
|
5931  | 
  { fix x y us vs
 | 
|
5932  | 
have "(x,y) : r^* \<Longrightarrow> (us @ x # vs, us @ y # vs) : (listrel1 r)^*"  | 
|
5933  | 
proof(induct rule: rtrancl.induct)  | 
|
5934  | 
case rtrancl_refl show ?case by simp  | 
|
5935  | 
next  | 
|
5936  | 
case rtrancl_into_rtrancl thus ?case  | 
|
5937  | 
by (metis listrel1I rtrancl.rtrancl_into_rtrancl)  | 
|
5938  | 
qed }  | 
|
5939  | 
thus "(xs,ys) \<in> (listrel1 r)^*" using 1 by(blast elim: listrel1E)  | 
|
5940  | 
qed  | 
|
5941  | 
||
5942  | 
lemma rtrancl_listrel1_eq_len: "(x,y) \<in> (listrel1 r)^* \<Longrightarrow> length x = length y"  | 
|
5943  | 
by (induct rule: rtrancl.induct) (auto intro: listrel1_eq_len)  | 
|
5944  | 
||
5945  | 
lemma rtrancl_listrel1_ConsI1:  | 
|
5946  | 
"(xs,ys) : (listrel1 r)^* \<Longrightarrow> (x#xs,x#ys) : (listrel1 r)^*"  | 
|
5947  | 
apply(induct rule: rtrancl.induct)  | 
|
5948  | 
apply simp  | 
|
5949  | 
by (metis listrel1I2 rtrancl.rtrancl_into_rtrancl)  | 
|
5950  | 
||
5951  | 
lemma rtrancl_listrel1_ConsI2:  | 
|
5952  | 
"(x,y) \<in> r^* \<Longrightarrow> (xs, ys) \<in> (listrel1 r)^*  | 
|
5953  | 
\<Longrightarrow> (x # xs, y # ys) \<in> (listrel1 r)^*"  | 
|
5954  | 
by (blast intro: rtrancl_trans rtrancl_listrel1_ConsI1  | 
|
5955  | 
subsetD[OF listrel1_rtrancl_subset_rtrancl_listrel1 listrel1I1])  | 
|
5956  | 
||
5957  | 
lemma listrel1_subset_listrel:  | 
|
5958  | 
"r \<subseteq> r' \<Longrightarrow> refl r' \<Longrightarrow> listrel1 r \<subseteq> listrel(r')"  | 
|
5959  | 
by(auto elim!: listrel1E simp add: listrel_iff_zip set_zip refl_on_def)  | 
|
5960  | 
||
5961  | 
lemma listrel_reflcl_if_listrel1:  | 
|
5962  | 
"(xs,ys) : listrel1 r \<Longrightarrow> (xs,ys) : listrel(r^*)"  | 
|
5963  | 
by(erule listrel1E)(auto simp add: listrel_iff_zip set_zip)  | 
|
5964  | 
||
5965  | 
lemma listrel_rtrancl_eq_rtrancl_listrel1: "listrel (r^*) = (listrel1 r)^*"  | 
|
5966  | 
proof  | 
|
5967  | 
  { fix x y assume "(x,y) \<in> listrel (r^*)"
 | 
|
5968  | 
then have "(x,y) \<in> (listrel1 r)^*"  | 
|
5969  | 
by induct (auto intro: rtrancl_listrel1_ConsI2) }  | 
|
5970  | 
then show "listrel (r^*) \<subseteq> (listrel1 r)^*"  | 
|
5971  | 
by (rule subrelI)  | 
|
5972  | 
next  | 
|
5973  | 
show "listrel (r^*) \<supseteq> (listrel1 r)^*"  | 
|
5974  | 
proof(rule subrelI)  | 
|
5975  | 
fix xs ys assume "(xs,ys) \<in> (listrel1 r)^*"  | 
|
5976  | 
then show "(xs,ys) \<in> listrel (r^*)"  | 
|
5977  | 
proof induct  | 
|
5978  | 
case base show ?case by(auto simp add: listrel_iff_zip set_zip)  | 
|
5979  | 
next  | 
|
5980  | 
case (step ys zs)  | 
|
| 56085 | 5981  | 
thus ?case by (metis listrel_reflcl_if_listrel1 listrel_rtrancl_trans)  | 
| 40230 | 5982  | 
qed  | 
5983  | 
qed  | 
|
5984  | 
qed  | 
|
5985  | 
||
5986  | 
lemma rtrancl_listrel1_if_listrel:  | 
|
5987  | 
"(xs,ys) : listrel r \<Longrightarrow> (xs,ys) : (listrel1 r)^*"  | 
|
5988  | 
by(metis listrel_rtrancl_eq_rtrancl_listrel1 subsetD[OF listrel_mono] r_into_rtrancl subsetI)  | 
|
5989  | 
||
5990  | 
lemma listrel_subset_rtrancl_listrel1: "listrel r \<subseteq> (listrel1 r)^*"  | 
|
5991  | 
by(fast intro:rtrancl_listrel1_if_listrel)  | 
|
5992  | 
||
| 15302 | 5993  | 
|
| 
26749
 
397a1aeede7d
* New attribute "termination_simp": Simp rules for termination proofs
 
krauss 
parents: 
26734 
diff
changeset
 | 
5994  | 
subsection {* Size function *}
 | 
| 
 
397a1aeede7d
* New attribute "termination_simp": Simp rules for termination proofs
 
krauss 
parents: 
26734 
diff
changeset
 | 
5995  | 
|
| 
56643
 
41d3596d8a64
move size hooks together, with new one preceding old one and sharing same theory data
 
blanchet 
parents: 
56545 
diff
changeset
 | 
5996  | 
lemma [measure_function]: "is_measure f \<Longrightarrow> is_measure (size_list f)"  | 
| 
26875
 
e18574413bc4
Measure functions can now be declared via special rules, allowing for a
 
krauss 
parents: 
26795 
diff
changeset
 | 
5997  | 
by (rule is_measure_trivial)  | 
| 
 
e18574413bc4
Measure functions can now be declared via special rules, allowing for a
 
krauss 
parents: 
26795 
diff
changeset
 | 
5998  | 
|
| 
56643
 
41d3596d8a64
move size hooks together, with new one preceding old one and sharing same theory data
 
blanchet 
parents: 
56545 
diff
changeset
 | 
5999  | 
lemma [measure_function]: "is_measure f \<Longrightarrow> is_measure (size_option f)"  | 
| 
26875
 
e18574413bc4
Measure functions can now be declared via special rules, allowing for a
 
krauss 
parents: 
26795 
diff
changeset
 | 
6000  | 
by (rule is_measure_trivial)  | 
| 
 
e18574413bc4
Measure functions can now be declared via special rules, allowing for a
 
krauss 
parents: 
26795 
diff
changeset
 | 
6001  | 
|
| 
56643
 
41d3596d8a64
move size hooks together, with new one preceding old one and sharing same theory data
 
blanchet 
parents: 
56545 
diff
changeset
 | 
6002  | 
lemma size_list_estimation[termination_simp]:  | 
| 
 
41d3596d8a64
move size hooks together, with new one preceding old one and sharing same theory data
 
blanchet 
parents: 
56545 
diff
changeset
 | 
6003  | 
"x \<in> set xs \<Longrightarrow> y < f x \<Longrightarrow> y < size_list f xs"  | 
| 
26749
 
397a1aeede7d
* New attribute "termination_simp": Simp rules for termination proofs
 
krauss 
parents: 
26734 
diff
changeset
 | 
6004  | 
by (induct xs) auto  | 
| 
 
397a1aeede7d
* New attribute "termination_simp": Simp rules for termination proofs
 
krauss 
parents: 
26734 
diff
changeset
 | 
6005  | 
|
| 
56643
 
41d3596d8a64
move size hooks together, with new one preceding old one and sharing same theory data
 
blanchet 
parents: 
56545 
diff
changeset
 | 
6006  | 
lemma size_list_estimation'[termination_simp]:  | 
| 
 
41d3596d8a64
move size hooks together, with new one preceding old one and sharing same theory data
 
blanchet 
parents: 
56545 
diff
changeset
 | 
6007  | 
"x \<in> set xs \<Longrightarrow> y \<le> f x \<Longrightarrow> y \<le> size_list f xs"  | 
| 
26875
 
e18574413bc4
Measure functions can now be declared via special rules, allowing for a
 
krauss 
parents: 
26795 
diff
changeset
 | 
6008  | 
by (induct xs) auto  | 
| 
 
e18574413bc4
Measure functions can now be declared via special rules, allowing for a
 
krauss 
parents: 
26795 
diff
changeset
 | 
6009  | 
|
| 
56643
 
41d3596d8a64
move size hooks together, with new one preceding old one and sharing same theory data
 
blanchet 
parents: 
56545 
diff
changeset
 | 
6010  | 
lemma size_list_map[simp]: "size_list f (map g xs) = size_list (f o g) xs"  | 
| 
26875
 
e18574413bc4
Measure functions can now be declared via special rules, allowing for a
 
krauss 
parents: 
26795 
diff
changeset
 | 
6011  | 
by (induct xs) auto  | 
| 
 
e18574413bc4
Measure functions can now be declared via special rules, allowing for a
 
krauss 
parents: 
26795 
diff
changeset
 | 
6012  | 
|
| 
56643
 
41d3596d8a64
move size hooks together, with new one preceding old one and sharing same theory data
 
blanchet 
parents: 
56545 
diff
changeset
 | 
6013  | 
lemma size_list_append[simp]: "size_list f (xs @ ys) = size_list f xs + size_list f ys"  | 
| 
44619
 
fd520fa2fb09
adding list_size_append (thanks to Rene Thiemann)
 
bulwahn 
parents: 
44618 
diff
changeset
 | 
6014  | 
by (induct xs, auto)  | 
| 
 
fd520fa2fb09
adding list_size_append (thanks to Rene Thiemann)
 
bulwahn 
parents: 
44618 
diff
changeset
 | 
6015  | 
|
| 
56643
 
41d3596d8a64
move size hooks together, with new one preceding old one and sharing same theory data
 
blanchet 
parents: 
56545 
diff
changeset
 | 
6016  | 
lemma size_list_pointwise[termination_simp]:  | 
| 
 
41d3596d8a64
move size hooks together, with new one preceding old one and sharing same theory data
 
blanchet 
parents: 
56545 
diff
changeset
 | 
6017  | 
"(\<And>x. x \<in> set xs \<Longrightarrow> f x \<le> g x) \<Longrightarrow> size_list f xs \<le> size_list g xs"  | 
| 
26875
 
e18574413bc4
Measure functions can now be declared via special rules, allowing for a
 
krauss 
parents: 
26795 
diff
changeset
 | 
6018  | 
by (induct xs) force+  | 
| 
26749
 
397a1aeede7d
* New attribute "termination_simp": Simp rules for termination proofs
 
krauss 
parents: 
26734 
diff
changeset
 | 
6019  | 
|
| 
31048
 
ac146fc38b51
refined HOL string theories and corresponding ML fragments
 
haftmann 
parents: 
31022 
diff
changeset
 | 
6020  | 
|
| 46143 | 6021  | 
subsection {* Monad operation *}
 | 
6022  | 
||
6023  | 
definition bind :: "'a list \<Rightarrow> ('a \<Rightarrow> 'b list) \<Rightarrow> 'b list" where
 | 
|
| 50548 | 6024  | 
"bind xs f = concat (map f xs)"  | 
| 46143 | 6025  | 
|
6026  | 
hide_const (open) bind  | 
|
6027  | 
||
6028  | 
lemma bind_simps [simp]:  | 
|
6029  | 
"List.bind [] f = []"  | 
|
6030  | 
"List.bind (x # xs) f = f x @ List.bind xs f"  | 
|
6031  | 
by (simp_all add: bind_def)  | 
|
6032  | 
||
6033  | 
||
| 
33318
 
ddd97d9dfbfb
moved Nat_Transfer before Divides; distributed Nat_Transfer setup accordingly
 
haftmann 
parents: 
32960 
diff
changeset
 | 
6034  | 
subsection {* Transfer *}
 | 
| 
 
ddd97d9dfbfb
moved Nat_Transfer before Divides; distributed Nat_Transfer setup accordingly
 
haftmann 
parents: 
32960 
diff
changeset
 | 
6035  | 
|
| 50548 | 6036  | 
definition embed_list :: "nat list \<Rightarrow> int list" where  | 
6037  | 
"embed_list l = map int l"  | 
|
6038  | 
||
6039  | 
definition nat_list :: "int list \<Rightarrow> bool" where  | 
|
6040  | 
"nat_list l = nat_set (set l)"  | 
|
6041  | 
||
6042  | 
definition return_list :: "int list \<Rightarrow> nat list" where  | 
|
6043  | 
"return_list l = map nat l"  | 
|
| 
33318
 
ddd97d9dfbfb
moved Nat_Transfer before Divides; distributed Nat_Transfer setup accordingly
 
haftmann 
parents: 
32960 
diff
changeset
 | 
6044  | 
|
| 
 
ddd97d9dfbfb
moved Nat_Transfer before Divides; distributed Nat_Transfer setup accordingly
 
haftmann 
parents: 
32960 
diff
changeset
 | 
6045  | 
lemma transfer_nat_int_list_return_embed: "nat_list l \<longrightarrow>  | 
| 
 
ddd97d9dfbfb
moved Nat_Transfer before Divides; distributed Nat_Transfer setup accordingly
 
haftmann 
parents: 
32960 
diff
changeset
 | 
6046  | 
embed_list (return_list l) = l"  | 
| 
 
ddd97d9dfbfb
moved Nat_Transfer before Divides; distributed Nat_Transfer setup accordingly
 
haftmann 
parents: 
32960 
diff
changeset
 | 
6047  | 
unfolding embed_list_def return_list_def nat_list_def nat_set_def  | 
| 
 
ddd97d9dfbfb
moved Nat_Transfer before Divides; distributed Nat_Transfer setup accordingly
 
haftmann 
parents: 
32960 
diff
changeset
 | 
6048  | 
apply (induct l)  | 
| 
 
ddd97d9dfbfb
moved Nat_Transfer before Divides; distributed Nat_Transfer setup accordingly
 
haftmann 
parents: 
32960 
diff
changeset
 | 
6049  | 
apply auto  | 
| 
 
ddd97d9dfbfb
moved Nat_Transfer before Divides; distributed Nat_Transfer setup accordingly
 
haftmann 
parents: 
32960 
diff
changeset
 | 
6050  | 
done  | 
| 
 
ddd97d9dfbfb
moved Nat_Transfer before Divides; distributed Nat_Transfer setup accordingly
 
haftmann 
parents: 
32960 
diff
changeset
 | 
6051  | 
|
| 
 
ddd97d9dfbfb
moved Nat_Transfer before Divides; distributed Nat_Transfer setup accordingly
 
haftmann 
parents: 
32960 
diff
changeset
 | 
6052  | 
lemma transfer_nat_int_list_functions:  | 
| 
 
ddd97d9dfbfb
moved Nat_Transfer before Divides; distributed Nat_Transfer setup accordingly
 
haftmann 
parents: 
32960 
diff
changeset
 | 
6053  | 
"l @ m = return_list (embed_list l @ embed_list m)"  | 
| 
 
ddd97d9dfbfb
moved Nat_Transfer before Divides; distributed Nat_Transfer setup accordingly
 
haftmann 
parents: 
32960 
diff
changeset
 | 
6054  | 
"[] = return_list []"  | 
| 
 
ddd97d9dfbfb
moved Nat_Transfer before Divides; distributed Nat_Transfer setup accordingly
 
haftmann 
parents: 
32960 
diff
changeset
 | 
6055  | 
unfolding return_list_def embed_list_def  | 
| 
 
ddd97d9dfbfb
moved Nat_Transfer before Divides; distributed Nat_Transfer setup accordingly
 
haftmann 
parents: 
32960 
diff
changeset
 | 
6056  | 
apply auto  | 
| 
 
ddd97d9dfbfb
moved Nat_Transfer before Divides; distributed Nat_Transfer setup accordingly
 
haftmann 
parents: 
32960 
diff
changeset
 | 
6057  | 
apply (induct l, auto)  | 
| 
 
ddd97d9dfbfb
moved Nat_Transfer before Divides; distributed Nat_Transfer setup accordingly
 
haftmann 
parents: 
32960 
diff
changeset
 | 
6058  | 
apply (induct m, auto)  | 
| 
 
ddd97d9dfbfb
moved Nat_Transfer before Divides; distributed Nat_Transfer setup accordingly
 
haftmann 
parents: 
32960 
diff
changeset
 | 
6059  | 
done  | 
| 
 
ddd97d9dfbfb
moved Nat_Transfer before Divides; distributed Nat_Transfer setup accordingly
 
haftmann 
parents: 
32960 
diff
changeset
 | 
6060  | 
|
| 
 
ddd97d9dfbfb
moved Nat_Transfer before Divides; distributed Nat_Transfer setup accordingly
 
haftmann 
parents: 
32960 
diff
changeset
 | 
6061  | 
(*  | 
| 
 
ddd97d9dfbfb
moved Nat_Transfer before Divides; distributed Nat_Transfer setup accordingly
 
haftmann 
parents: 
32960 
diff
changeset
 | 
6062  | 
lemma transfer_nat_int_fold1: "fold f l x =  | 
| 
 
ddd97d9dfbfb
moved Nat_Transfer before Divides; distributed Nat_Transfer setup accordingly
 
haftmann 
parents: 
32960 
diff
changeset
 | 
6063  | 
fold (%x. f (nat x)) (embed_list l) x";  | 
| 
 
ddd97d9dfbfb
moved Nat_Transfer before Divides; distributed Nat_Transfer setup accordingly
 
haftmann 
parents: 
32960 
diff
changeset
 | 
6064  | 
*)  | 
| 
 
ddd97d9dfbfb
moved Nat_Transfer before Divides; distributed Nat_Transfer setup accordingly
 
haftmann 
parents: 
32960 
diff
changeset
 | 
6065  | 
|
| 
 
ddd97d9dfbfb
moved Nat_Transfer before Divides; distributed Nat_Transfer setup accordingly
 
haftmann 
parents: 
32960 
diff
changeset
 | 
6066  | 
|
| 
37605
 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 
haftmann 
parents: 
37465 
diff
changeset
 | 
6067  | 
subsection {* Code generation *}
 | 
| 
 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 
haftmann 
parents: 
37465 
diff
changeset
 | 
6068  | 
|
| 
51875
 
dafd097dd1f4
tail recursive version of map, for code generation, optionally
 
nipkow 
parents: 
51738 
diff
changeset
 | 
6069  | 
text{* Optional tail recursive version of @{const map}. Can avoid
 | 
| 
 
dafd097dd1f4
tail recursive version of map, for code generation, optionally
 
nipkow 
parents: 
51738 
diff
changeset
 | 
6070  | 
stack overflow in some target languages. *}  | 
| 
 
dafd097dd1f4
tail recursive version of map, for code generation, optionally
 
nipkow 
parents: 
51738 
diff
changeset
 | 
6071  | 
|
| 
 
dafd097dd1f4
tail recursive version of map, for code generation, optionally
 
nipkow 
parents: 
51738 
diff
changeset
 | 
6072  | 
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
 | 
6073  | 
"map_tailrec_rev f [] bs = bs" |  | 
| 
 
dafd097dd1f4
tail recursive version of map, for code generation, optionally
 
nipkow 
parents: 
51738 
diff
changeset
 | 
6074  | 
"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
 | 
6075  | 
|
| 
 
dafd097dd1f4
tail recursive version of map, for code generation, optionally
 
nipkow 
parents: 
51738 
diff
changeset
 | 
6076  | 
lemma map_tailrec_rev:  | 
| 
 
dafd097dd1f4
tail recursive version of map, for code generation, optionally
 
nipkow 
parents: 
51738 
diff
changeset
 | 
6077  | 
"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
 | 
6078  | 
by(induction as arbitrary: bs) simp_all  | 
| 
 
dafd097dd1f4
tail recursive version of map, for code generation, optionally
 
nipkow 
parents: 
51738 
diff
changeset
 | 
6079  | 
|
| 
 
dafd097dd1f4
tail recursive version of map, for code generation, optionally
 
nipkow 
parents: 
51738 
diff
changeset
 | 
6080  | 
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
 | 
6081  | 
"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
 | 
6082  | 
|
| 
 
dafd097dd1f4
tail recursive version of map, for code generation, optionally
 
nipkow 
parents: 
51738 
diff
changeset
 | 
6083  | 
text{* Code equation: *}
 | 
| 
 
dafd097dd1f4
tail recursive version of map, for code generation, optionally
 
nipkow 
parents: 
51738 
diff
changeset
 | 
6084  | 
lemma map_eq_map_tailrec: "map = map_tailrec"  | 
| 
 
dafd097dd1f4
tail recursive version of map, for code generation, optionally
 
nipkow 
parents: 
51738 
diff
changeset
 | 
6085  | 
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
 | 
6086  | 
|
| 
 
dafd097dd1f4
tail recursive version of map, for code generation, optionally
 
nipkow 
parents: 
51738 
diff
changeset
 | 
6087  | 
|
| 
37605
 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 
haftmann 
parents: 
37465 
diff
changeset
 | 
6088  | 
subsubsection {* Counterparts for set-related operations *}
 | 
| 
 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 
haftmann 
parents: 
37465 
diff
changeset
 | 
6089  | 
|
| 
 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 
haftmann 
parents: 
37465 
diff
changeset
 | 
6090  | 
definition member :: "'a list \<Rightarrow> 'a \<Rightarrow> bool" where  | 
| 50548 | 6091  | 
[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
 | 
6092  | 
|
| 
 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 
haftmann 
parents: 
37465 
diff
changeset
 | 
6093  | 
text {*
 | 
| 
46030
 
51b2f3412a03
attribute code_abbrev superseedes code_unfold_post; tuned text
 
haftmann 
parents: 
45993 
diff
changeset
 | 
6094  | 
  Use @{text member} only for generating executable code.  Otherwise use
 | 
| 
37605
 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 
haftmann 
parents: 
37465 
diff
changeset
 | 
6095  | 
  @{prop "x \<in> set xs"} instead --- it is much easier to reason about.
 | 
| 
 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 
haftmann 
parents: 
37465 
diff
changeset
 | 
6096  | 
*}  | 
| 
 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 
haftmann 
parents: 
37465 
diff
changeset
 | 
6097  | 
|
| 
 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 
haftmann 
parents: 
37465 
diff
changeset
 | 
6098  | 
lemma member_rec [code]:  | 
| 
 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 
haftmann 
parents: 
37465 
diff
changeset
 | 
6099  | 
"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
 | 
6100  | 
"member [] y \<longleftrightarrow> False"  | 
| 
 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 
haftmann 
parents: 
37465 
diff
changeset
 | 
6101  | 
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
 | 
6102  | 
|
| 
46030
 
51b2f3412a03
attribute code_abbrev superseedes code_unfold_post; tuned text
 
haftmann 
parents: 
45993 
diff
changeset
 | 
6103  | 
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
 | 
6104  | 
"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
 | 
6105  | 
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
 | 
6106  | 
|
| 
56527
 
907f04603177
make list_all an abbreviation of pred_list - prevent duplication
 
kuncar 
parents: 
56525 
diff
changeset
 | 
6107  | 
abbreviation "list_all == pred_list"  | 
| 
 
907f04603177
make list_all an abbreviation of pred_list - prevent duplication
 
kuncar 
parents: 
56525 
diff
changeset
 | 
6108  | 
|
| 
 
907f04603177
make list_all an abbreviation of pred_list - prevent duplication
 
kuncar 
parents: 
56525 
diff
changeset
 | 
6109  | 
lemma list_all_iff [code_abbrev]: "list_all P xs \<longleftrightarrow> Ball (set xs) P"  | 
| 
 
907f04603177
make list_all an abbreviation of pred_list - prevent duplication
 
kuncar 
parents: 
56525 
diff
changeset
 | 
6110  | 
unfolding pred_list_def ..  | 
| 
37605
 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 
haftmann 
parents: 
37465 
diff
changeset
 | 
6111  | 
|
| 
 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 
haftmann 
parents: 
37465 
diff
changeset
 | 
6112  | 
definition list_ex :: "('a \<Rightarrow> bool) \<Rightarrow> 'a list \<Rightarrow> bool" where
 | 
| 50548 | 6113  | 
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
 | 
6114  | 
|
| 
 
51b2f3412a03
attribute code_abbrev superseedes code_unfold_post; tuned text
 
haftmann 
parents: 
45993 
diff
changeset
 | 
6115  | 
definition list_ex1 :: "('a \<Rightarrow> bool) \<Rightarrow> 'a list \<Rightarrow> bool" where
 | 
| 50548 | 6116  | 
list_ex1_iff [code_abbrev]: "list_ex1 P xs \<longleftrightarrow> (\<exists>! x. x \<in> set xs \<and> P x)"  | 
| 40652 | 6117  | 
|
| 
37605
 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 
haftmann 
parents: 
37465 
diff
changeset
 | 
6118  | 
text {*
 | 
| 
46030
 
51b2f3412a03
attribute code_abbrev superseedes code_unfold_post; tuned text
 
haftmann 
parents: 
45993 
diff
changeset
 | 
6119  | 
  Usually you should prefer @{text "\<forall>x\<in>set xs"}, @{text "\<exists>x\<in>set xs"}
 | 
| 
 
51b2f3412a03
attribute code_abbrev superseedes code_unfold_post; tuned text
 
haftmann 
parents: 
45993 
diff
changeset
 | 
6120  | 
  and @{text "\<exists>!x. x\<in>set xs \<and> _"} over @{const list_all}, @{const list_ex}
 | 
| 
 
51b2f3412a03
attribute code_abbrev superseedes code_unfold_post; tuned text
 
haftmann 
parents: 
45993 
diff
changeset
 | 
6121  | 
  and @{const list_ex1} in specifications.
 | 
| 
37605
 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 
haftmann 
parents: 
37465 
diff
changeset
 | 
6122  | 
*}  | 
| 
 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 
haftmann 
parents: 
37465 
diff
changeset
 | 
6123  | 
|
| 
56527
 
907f04603177
make list_all an abbreviation of pred_list - prevent duplication
 
kuncar 
parents: 
56525 
diff
changeset
 | 
6124  | 
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
 | 
6125  | 
"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
 | 
6126  | 
"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
 | 
6127  | 
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
 | 
6128  | 
|
| 
 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 
haftmann 
parents: 
37465 
diff
changeset
 | 
6129  | 
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
 | 
6130  | 
"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
 | 
6131  | 
"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
 | 
6132  | 
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
 | 
6133  | 
|
| 40652 | 6134  | 
lemma list_ex1_simps [simp, code]:  | 
6135  | 
"list_ex1 P [] = False"  | 
|
6136  | 
"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
 | 
6137  | 
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
 | 
6138  | 
|
| 
 
51b2f3412a03
attribute code_abbrev superseedes code_unfold_post; tuned text
 
haftmann 
parents: 
45993 
diff
changeset
 | 
6139  | 
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
 | 
6140  | 
"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
 | 
6141  | 
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
 | 
6142  | 
|
| 
46030
 
51b2f3412a03
attribute code_abbrev superseedes code_unfold_post; tuned text
 
haftmann 
parents: 
45993 
diff
changeset
 | 
6143  | 
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
 | 
6144  | 
"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
 | 
6145  | 
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
 | 
6146  | 
|
| 
 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 
haftmann 
parents: 
37465 
diff
changeset
 | 
6147  | 
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
 | 
6148  | 
"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
 | 
6149  | 
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
 | 
6150  | 
|
| 
 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 
haftmann 
parents: 
37465 
diff
changeset
 | 
6151  | 
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
 | 
6152  | 
"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
 | 
6153  | 
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
 | 
6154  | 
|
| 
 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 
haftmann 
parents: 
37465 
diff
changeset
 | 
6155  | 
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
 | 
6156  | 
"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
 | 
6157  | 
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
 | 
6158  | 
|
| 
 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 
haftmann 
parents: 
37465 
diff
changeset
 | 
6159  | 
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
 | 
6160  | 
"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
 | 
6161  | 
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
 | 
6162  | 
|
| 
 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 
haftmann 
parents: 
37465 
diff
changeset
 | 
6163  | 
lemma list_all_length:  | 
| 
 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 
haftmann 
parents: 
37465 
diff
changeset
 | 
6164  | 
"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
 | 
6165  | 
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
 | 
6166  | 
|
| 
 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 
haftmann 
parents: 
37465 
diff
changeset
 | 
6167  | 
lemma list_ex_length:  | 
| 
 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 
haftmann 
parents: 
37465 
diff
changeset
 | 
6168  | 
"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
 | 
6169  | 
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
 | 
6170  | 
|
| 
 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 
haftmann 
parents: 
37465 
diff
changeset
 | 
6171  | 
lemma list_all_cong [fundef_cong]:  | 
| 
 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 
haftmann 
parents: 
37465 
diff
changeset
 | 
6172  | 
"xs = ys \<Longrightarrow> (\<And>x. x \<in> set ys \<Longrightarrow> f x = g x) \<Longrightarrow> list_all f xs = list_all g ys"  | 
| 
 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 
haftmann 
parents: 
37465 
diff
changeset
 | 
6173  | 
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
 | 
6174  | 
|
| 47131 | 6175  | 
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
 | 
6176  | 
"xs = ys \<Longrightarrow> (\<And>x. x \<in> set ys \<Longrightarrow> f x = g x) \<Longrightarrow> list_ex f xs = list_ex g ys"  | 
| 47131 | 6177  | 
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
 | 
6178  | 
|
| 50548 | 6179  | 
definition can_select :: "('a \<Rightarrow> bool) \<Rightarrow> 'a set \<Rightarrow> bool" where
 | 
6180  | 
[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
 | 
6181  | 
|
| 
 
744934b818c7
moved quite generic material from theory Enum to more appropriate places
 
haftmann 
parents: 
49808 
diff
changeset
 | 
6182  | 
lemma can_select_set_list_ex1 [code]:  | 
| 
 
744934b818c7
moved quite generic material from theory Enum to more appropriate places
 
haftmann 
parents: 
49808 
diff
changeset
 | 
6183  | 
"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
 | 
6184  | 
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
 | 
6185  | 
|
| 
 
744934b818c7
moved quite generic material from theory Enum to more appropriate places
 
haftmann 
parents: 
49808 
diff
changeset
 | 
6186  | 
|
| 46313 | 6187  | 
text {* Executable checks for relations on sets *}
 | 
6188  | 
||
6189  | 
definition listrel1p :: "('a \<Rightarrow> 'a \<Rightarrow> bool) \<Rightarrow> 'a list \<Rightarrow> 'a list \<Rightarrow> bool" where
 | 
|
6190  | 
"listrel1p r xs ys = ((xs, ys) \<in> listrel1 {(x, y). r x y})"
 | 
|
6191  | 
||
6192  | 
lemma [code_unfold]:  | 
|
6193  | 
"(xs, ys) \<in> listrel1 r = listrel1p (\<lambda>x y. (x, y) \<in> r) xs ys"  | 
|
6194  | 
unfolding listrel1p_def by auto  | 
|
6195  | 
||
6196  | 
lemma [code]:  | 
|
6197  | 
"listrel1p r [] xs = False"  | 
|
6198  | 
"listrel1p r xs [] = False"  | 
|
6199  | 
"listrel1p r (x # xs) (y # ys) \<longleftrightarrow>  | 
|
6200  | 
r x y \<and> xs = ys \<or> x = y \<and> listrel1p r xs ys"  | 
|
6201  | 
by (simp add: listrel1p_def)+  | 
|
6202  | 
||
6203  | 
definition  | 
|
6204  | 
  lexordp :: "('a \<Rightarrow> 'a \<Rightarrow> bool) \<Rightarrow> 'a list \<Rightarrow> 'a list \<Rightarrow> bool" where
 | 
|
6205  | 
  "lexordp r xs ys = ((xs, ys) \<in> lexord {(x, y). r x y})"
 | 
|
6206  | 
||
6207  | 
lemma [code_unfold]:  | 
|
6208  | 
"(xs, ys) \<in> lexord r = lexordp (\<lambda>x y. (x, y) \<in> r) xs ys"  | 
|
6209  | 
unfolding lexordp_def by auto  | 
|
6210  | 
||
6211  | 
lemma [code]:  | 
|
6212  | 
"lexordp r xs [] = False"  | 
|
6213  | 
"lexordp r [] (y#ys) = True"  | 
|
6214  | 
"lexordp r (x # xs) (y # ys) = (r x y | (x = y & lexordp r xs ys))"  | 
|
6215  | 
unfolding lexordp_def by auto  | 
|
6216  | 
||
| 
37605
 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 
haftmann 
parents: 
37465 
diff
changeset
 | 
6217  | 
text {* Bounded quantification and summation over nats. *}
 | 
| 
 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 
haftmann 
parents: 
37465 
diff
changeset
 | 
6218  | 
|
| 
 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 
haftmann 
parents: 
37465 
diff
changeset
 | 
6219  | 
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
 | 
6220  | 
  "{..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
 | 
6221  | 
by auto  | 
| 
 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 
haftmann 
parents: 
37465 
diff
changeset
 | 
6222  | 
|
| 
 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 
haftmann 
parents: 
37465 
diff
changeset
 | 
6223  | 
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
 | 
6224  | 
  "{..<n} = set [0..<n]"
 | 
| 
 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 
haftmann 
parents: 
37465 
diff
changeset
 | 
6225  | 
by auto  | 
| 
 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 
haftmann 
parents: 
37465 
diff
changeset
 | 
6226  | 
|
| 
 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 
haftmann 
parents: 
37465 
diff
changeset
 | 
6227  | 
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
 | 
6228  | 
  "{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
 | 
6229  | 
by auto  | 
| 
 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 
haftmann 
parents: 
37465 
diff
changeset
 | 
6230  | 
|
| 
 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 
haftmann 
parents: 
37465 
diff
changeset
 | 
6231  | 
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
 | 
6232  | 
|
| 
 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 
haftmann 
parents: 
37465 
diff
changeset
 | 
6233  | 
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
 | 
6234  | 
  "{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
 | 
6235  | 
by auto  | 
| 
 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 
haftmann 
parents: 
37465 
diff
changeset
 | 
6236  | 
|
| 
 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 
haftmann 
parents: 
37465 
diff
changeset
 | 
6237  | 
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
 | 
6238  | 
  "{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
 | 
6239  | 
by auto  | 
| 
 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 
haftmann 
parents: 
37465 
diff
changeset
 | 
6240  | 
|
| 
 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 
haftmann 
parents: 
37465 
diff
changeset
 | 
6241  | 
lemma all_nat_less_eq [code_unfold]:  | 
| 
 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 
haftmann 
parents: 
37465 
diff
changeset
 | 
6242  | 
  "(\<forall>m<n\<Colon>nat. P m) \<longleftrightarrow> (\<forall>m \<in> {0..<n}. P m)"
 | 
| 
 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 
haftmann 
parents: 
37465 
diff
changeset
 | 
6243  | 
by auto  | 
| 
 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 
haftmann 
parents: 
37465 
diff
changeset
 | 
6244  | 
|
| 
 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 
haftmann 
parents: 
37465 
diff
changeset
 | 
6245  | 
lemma ex_nat_less_eq [code_unfold]:  | 
| 
 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 
haftmann 
parents: 
37465 
diff
changeset
 | 
6246  | 
  "(\<exists>m<n\<Colon>nat. P m) \<longleftrightarrow> (\<exists>m \<in> {0..<n}. P m)"
 | 
| 
 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 
haftmann 
parents: 
37465 
diff
changeset
 | 
6247  | 
by auto  | 
| 
 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 
haftmann 
parents: 
37465 
diff
changeset
 | 
6248  | 
|
| 
 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 
haftmann 
parents: 
37465 
diff
changeset
 | 
6249  | 
lemma all_nat_less [code_unfold]:  | 
| 
 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 
haftmann 
parents: 
37465 
diff
changeset
 | 
6250  | 
  "(\<forall>m\<le>n\<Colon>nat. P m) \<longleftrightarrow> (\<forall>m \<in> {0..n}. P m)"
 | 
| 
 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 
haftmann 
parents: 
37465 
diff
changeset
 | 
6251  | 
by auto  | 
| 
 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 
haftmann 
parents: 
37465 
diff
changeset
 | 
6252  | 
|
| 
 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 
haftmann 
parents: 
37465 
diff
changeset
 | 
6253  | 
lemma ex_nat_less [code_unfold]:  | 
| 
 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 
haftmann 
parents: 
37465 
diff
changeset
 | 
6254  | 
  "(\<exists>m\<le>n\<Colon>nat. P m) \<longleftrightarrow> (\<exists>m \<in> {0..n}. P m)"
 | 
| 
 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 
haftmann 
parents: 
37465 
diff
changeset
 | 
6255  | 
by auto  | 
| 
 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 
haftmann 
parents: 
37465 
diff
changeset
 | 
6256  | 
|
| 
 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 
haftmann 
parents: 
37465 
diff
changeset
 | 
6257  | 
lemma setsum_set_upt_conv_listsum_nat [code_unfold]:  | 
| 
 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 
haftmann 
parents: 
37465 
diff
changeset
 | 
6258  | 
"setsum f (set [m..<n]) = listsum (map f [m..<n])"  | 
| 
 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 
haftmann 
parents: 
37465 
diff
changeset
 | 
6259  | 
by (simp add: interv_listsum_conv_setsum_set_nat)  | 
| 
 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 
haftmann 
parents: 
37465 
diff
changeset
 | 
6260  | 
|
| 53954 | 6261  | 
text{* Bounded @{text LEAST} operator: *}
 | 
6262  | 
||
6263  | 
definition "Bleast S P = (LEAST x. x \<in> S \<and> P x)"  | 
|
6264  | 
||
6265  | 
definition "abort_Bleast S P = (LEAST x. x \<in> S \<and> P x)"  | 
|
6266  | 
||
| 
54890
 
cb892d835803
fundamental treatment of undefined vs. universally partial replaces code_abort
 
haftmann 
parents: 
54885 
diff
changeset
 | 
6267  | 
declare [[code abort: abort_Bleast]]  | 
| 53954 | 6268  | 
|
6269  | 
lemma Bleast_code [code]:  | 
|
6270  | 
"Bleast (set xs) P = (case filter P (sort xs) of  | 
|
6271  | 
x#xs \<Rightarrow> x |  | 
|
6272  | 
[] \<Rightarrow> abort_Bleast (set xs) P)"  | 
|
6273  | 
proof (cases "filter P (sort xs)")  | 
|
6274  | 
case Nil thus ?thesis by (simp add: Bleast_def abort_Bleast_def)  | 
|
6275  | 
next  | 
|
6276  | 
case (Cons x ys)  | 
|
6277  | 
have "(LEAST x. x \<in> set xs \<and> P x) = x"  | 
|
6278  | 
proof (rule Least_equality)  | 
|
6279  | 
show "x \<in> set xs \<and> P x"  | 
|
6280  | 
by (metis Cons Cons_eq_filter_iff in_set_conv_decomp set_sort)  | 
|
6281  | 
next  | 
|
6282  | 
fix y assume "y : set xs \<and> P y"  | 
|
6283  | 
hence "y : set (filter P xs)" by auto  | 
|
6284  | 
thus "x \<le> y"  | 
|
6285  | 
by (metis Cons eq_iff filter_sort set_ConsD set_sort sorted_Cons sorted_sort)  | 
|
6286  | 
qed  | 
|
6287  | 
thus ?thesis using Cons by (simp add: Bleast_def)  | 
|
6288  | 
qed  | 
|
6289  | 
||
6290  | 
declare Bleast_def[symmetric, code_unfold]  | 
|
6291  | 
||
| 
37605
 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 
haftmann 
parents: 
37465 
diff
changeset
 | 
6292  | 
text {* Summation over ints. *}
 | 
| 
 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 
haftmann 
parents: 
37465 
diff
changeset
 | 
6293  | 
|
| 
 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 
haftmann 
parents: 
37465 
diff
changeset
 | 
6294  | 
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
 | 
6295  | 
  "{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
 | 
6296  | 
by auto  | 
| 
 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 
haftmann 
parents: 
37465 
diff
changeset
 | 
6297  | 
|
| 
 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 
haftmann 
parents: 
37465 
diff
changeset
 | 
6298  | 
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
 | 
6299  | 
  "{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
 | 
6300  | 
by auto  | 
| 
 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 
haftmann 
parents: 
37465 
diff
changeset
 | 
6301  | 
|
| 
 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 
haftmann 
parents: 
37465 
diff
changeset
 | 
6302  | 
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
 | 
6303  | 
  "{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
 | 
6304  | 
by auto  | 
| 
 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 
haftmann 
parents: 
37465 
diff
changeset
 | 
6305  | 
|
| 
 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 
haftmann 
parents: 
37465 
diff
changeset
 | 
6306  | 
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
 | 
6307  | 
|
| 
 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 
haftmann 
parents: 
37465 
diff
changeset
 | 
6308  | 
lemma setsum_set_upto_conv_listsum_int [code_unfold]:  | 
| 
 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 
haftmann 
parents: 
37465 
diff
changeset
 | 
6309  | 
"setsum f (set [i..j::int]) = listsum (map f [i..j])"  | 
| 
 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 
haftmann 
parents: 
37465 
diff
changeset
 | 
6310  | 
by (simp add: interv_listsum_conv_setsum_set_int)  | 
| 
 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 
haftmann 
parents: 
37465 
diff
changeset
 | 
6311  | 
|
| 
 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 
haftmann 
parents: 
37465 
diff
changeset
 | 
6312  | 
|
| 
 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 
haftmann 
parents: 
37465 
diff
changeset
 | 
6313  | 
subsubsection {* Optimizing by rewriting *}
 | 
| 
 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 
haftmann 
parents: 
37465 
diff
changeset
 | 
6314  | 
|
| 
 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 
haftmann 
parents: 
37465 
diff
changeset
 | 
6315  | 
definition null :: "'a list \<Rightarrow> bool" where  | 
| 
46030
 
51b2f3412a03
attribute code_abbrev superseedes code_unfold_post; tuned text
 
haftmann 
parents: 
45993 
diff
changeset
 | 
6316  | 
[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
 | 
6317  | 
|
| 
 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 
haftmann 
parents: 
37465 
diff
changeset
 | 
6318  | 
text {*
 | 
| 
 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 
haftmann 
parents: 
37465 
diff
changeset
 | 
6319  | 
  Efficient emptyness check is implemented by @{const null}.
 | 
| 
 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 
haftmann 
parents: 
37465 
diff
changeset
 | 
6320  | 
*}  | 
| 
 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 
haftmann 
parents: 
37465 
diff
changeset
 | 
6321  | 
|
| 
 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 
haftmann 
parents: 
37465 
diff
changeset
 | 
6322  | 
lemma null_rec [code]:  | 
| 
 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 
haftmann 
parents: 
37465 
diff
changeset
 | 
6323  | 
"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
 | 
6324  | 
"null [] \<longleftrightarrow> True"  | 
| 
 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 
haftmann 
parents: 
37465 
diff
changeset
 | 
6325  | 
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
 | 
6326  | 
|
| 
46030
 
51b2f3412a03
attribute code_abbrev superseedes code_unfold_post; tuned text
 
haftmann 
parents: 
45993 
diff
changeset
 | 
6327  | 
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
 | 
6328  | 
"xs = [] \<longleftrightarrow> null xs"  | 
| 
 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 
haftmann 
parents: 
37465 
diff
changeset
 | 
6329  | 
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
 | 
6330  | 
|
| 
 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 
haftmann 
parents: 
37465 
diff
changeset
 | 
6331  | 
lemma equal_Nil_null [code_unfold]:  | 
| 
38857
 
97775f3e8722
renamed class/constant eq to equal; tuned some instantiations
 
haftmann 
parents: 
38715 
diff
changeset
 | 
6332  | 
"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
 | 
6333  | 
"HOL.equal [] = null"  | 
| 
 
36cf426cb1c6
Added symmetric code_unfold-lemmas for null and is_none
 
lammich <lammich@in.tum.de> 
parents: 
53721 
diff
changeset
 | 
6334  | 
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
 | 
6335  | 
|
| 
 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 
haftmann 
parents: 
37465 
diff
changeset
 | 
6336  | 
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
 | 
6337  | 
[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
 | 
6338  | 
|
| 
 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 
haftmann 
parents: 
37465 
diff
changeset
 | 
6339  | 
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
 | 
6340  | 
[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
 | 
6341  | 
|
| 
 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 
haftmann 
parents: 
37465 
diff
changeset
 | 
6342  | 
text {*
 | 
| 
 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 
haftmann 
parents: 
37465 
diff
changeset
 | 
6343  | 
  Operations @{const maps} and @{const map_filter} avoid
 | 
| 
 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 
haftmann 
parents: 
37465 
diff
changeset
 | 
6344  | 
intermediate lists on execution -- do not use for proving.  | 
| 
 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 
haftmann 
parents: 
37465 
diff
changeset
 | 
6345  | 
*}  | 
| 
 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 
haftmann 
parents: 
37465 
diff
changeset
 | 
6346  | 
|
| 
 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 
haftmann 
parents: 
37465 
diff
changeset
 | 
6347  | 
lemma maps_simps [code]:  | 
| 
 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 
haftmann 
parents: 
37465 
diff
changeset
 | 
6348  | 
"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
 | 
6349  | 
"maps f [] = []"  | 
| 
 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 
haftmann 
parents: 
37465 
diff
changeset
 | 
6350  | 
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
 | 
6351  | 
|
| 
 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 
haftmann 
parents: 
37465 
diff
changeset
 | 
6352  | 
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
 | 
6353  | 
"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
 | 
6354  | 
"map_filter f [] = []"  | 
| 
 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 
haftmann 
parents: 
37465 
diff
changeset
 | 
6355  | 
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
 | 
6356  | 
|
| 
46030
 
51b2f3412a03
attribute code_abbrev superseedes code_unfold_post; tuned text
 
haftmann 
parents: 
45993 
diff
changeset
 | 
6357  | 
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
 | 
6358  | 
"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
 | 
6359  | 
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
 | 
6360  | 
|
| 
 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 
haftmann 
parents: 
37465 
diff
changeset
 | 
6361  | 
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
 | 
6362  | 
"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
 | 
6363  | 
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
 | 
6364  | 
|
| 
 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 
haftmann 
parents: 
37465 
diff
changeset
 | 
6365  | 
text {* Optimized code for @{text"\<forall>i\<in>{a..b::int}"} and @{text"\<forall>n:{a..<b::nat}"}
 | 
| 
 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 
haftmann 
parents: 
37465 
diff
changeset
 | 
6366  | 
and similiarly for @{text"\<exists>"}. *}
 | 
| 
 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 
haftmann 
parents: 
37465 
diff
changeset
 | 
6367  | 
|
| 
 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 
haftmann 
parents: 
37465 
diff
changeset
 | 
6368  | 
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
 | 
6369  | 
  "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
 | 
6370  | 
|
| 
 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 
haftmann 
parents: 
37465 
diff
changeset
 | 
6371  | 
lemma [code]:  | 
| 
 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 
haftmann 
parents: 
37465 
diff
changeset
 | 
6372  | 
"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
 | 
6373  | 
proof -  | 
| 
 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 
haftmann 
parents: 
37465 
diff
changeset
 | 
6374  | 
  have *: "\<And>n. P i \<Longrightarrow> \<forall>n\<in>{Suc i..<j}. P n \<Longrightarrow> i \<le> n \<Longrightarrow> n < j \<Longrightarrow> P n"
 | 
| 
 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 
haftmann 
parents: 
37465 
diff
changeset
 | 
6375  | 
proof -  | 
| 
 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 
haftmann 
parents: 
37465 
diff
changeset
 | 
6376  | 
fix n  | 
| 
 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 
haftmann 
parents: 
37465 
diff
changeset
 | 
6377  | 
    assume "P i" "\<forall>n\<in>{Suc i..<j}. P n" "i \<le> n" "n < j"
 | 
| 
 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 
haftmann 
parents: 
37465 
diff
changeset
 | 
6378  | 
then show "P n" by (cases "n = i") simp_all  | 
| 
 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 
haftmann 
parents: 
37465 
diff
changeset
 | 
6379  | 
qed  | 
| 
 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 
haftmann 
parents: 
37465 
diff
changeset
 | 
6380  | 
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
 | 
6381  | 
qed  | 
| 
 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 
haftmann 
parents: 
37465 
diff
changeset
 | 
6382  | 
|
| 
 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 
haftmann 
parents: 
37465 
diff
changeset
 | 
6383  | 
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
 | 
6384  | 
"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
 | 
6385  | 
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
 | 
6386  | 
|
| 
 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 
haftmann 
parents: 
37465 
diff
changeset
 | 
6387  | 
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
 | 
6388  | 
"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
 | 
6389  | 
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
 | 
6390  | 
|
| 
 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 
haftmann 
parents: 
37465 
diff
changeset
 | 
6391  | 
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
 | 
6392  | 
  "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
 | 
6393  | 
|
| 
 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 
haftmann 
parents: 
37465 
diff
changeset
 | 
6394  | 
lemma [code]:  | 
| 
 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 
haftmann 
parents: 
37465 
diff
changeset
 | 
6395  | 
"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
 | 
6396  | 
proof -  | 
| 
 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 
haftmann 
parents: 
37465 
diff
changeset
 | 
6397  | 
  have *: "\<And>k. P i \<Longrightarrow> \<forall>k\<in>{i+1..j}. P k \<Longrightarrow> i \<le> k \<Longrightarrow> k \<le> j \<Longrightarrow> P k"
 | 
| 
 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 
haftmann 
parents: 
37465 
diff
changeset
 | 
6398  | 
proof -  | 
| 
 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 
haftmann 
parents: 
37465 
diff
changeset
 | 
6399  | 
fix k  | 
| 
 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 
haftmann 
parents: 
37465 
diff
changeset
 | 
6400  | 
    assume "P i" "\<forall>k\<in>{i+1..j}. P k" "i \<le> k" "k \<le> j"
 | 
| 
 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 
haftmann 
parents: 
37465 
diff
changeset
 | 
6401  | 
then show "P k" by (cases "k = i") simp_all  | 
| 
 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 
haftmann 
parents: 
37465 
diff
changeset
 | 
6402  | 
qed  | 
| 
 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 
haftmann 
parents: 
37465 
diff
changeset
 | 
6403  | 
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
 | 
6404  | 
qed  | 
| 
 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 
haftmann 
parents: 
37465 
diff
changeset
 | 
6405  | 
|
| 
 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 
haftmann 
parents: 
37465 
diff
changeset
 | 
6406  | 
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
 | 
6407  | 
"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
 | 
6408  | 
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
 | 
6409  | 
|
| 
 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 
haftmann 
parents: 
37465 
diff
changeset
 | 
6410  | 
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
 | 
6411  | 
"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
 | 
6412  | 
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
 | 
6413  | 
|
| 
49808
 
418991ce7567
tail-recursive implementation for length
 
Andreas Lochbihler 
parents: 
49757 
diff
changeset
 | 
6414  | 
text {* optimized code (tail-recursive) for @{term length} *}
 | 
| 
 
418991ce7567
tail-recursive implementation for length
 
Andreas Lochbihler 
parents: 
49757 
diff
changeset
 | 
6415  | 
|
| 
 
418991ce7567
tail-recursive implementation for length
 
Andreas Lochbihler 
parents: 
49757 
diff
changeset
 | 
6416  | 
definition gen_length :: "nat \<Rightarrow> 'a list \<Rightarrow> nat"  | 
| 
 
418991ce7567
tail-recursive implementation for length
 
Andreas Lochbihler 
parents: 
49757 
diff
changeset
 | 
6417  | 
where "gen_length n xs = n + length xs"  | 
| 
 
418991ce7567
tail-recursive implementation for length
 
Andreas Lochbihler 
parents: 
49757 
diff
changeset
 | 
6418  | 
|
| 
 
418991ce7567
tail-recursive implementation for length
 
Andreas Lochbihler 
parents: 
49757 
diff
changeset
 | 
6419  | 
lemma gen_length_code [code]:  | 
| 
 
418991ce7567
tail-recursive implementation for length
 
Andreas Lochbihler 
parents: 
49757 
diff
changeset
 | 
6420  | 
"gen_length n [] = n"  | 
| 
 
418991ce7567
tail-recursive implementation for length
 
Andreas Lochbihler 
parents: 
49757 
diff
changeset
 | 
6421  | 
"gen_length n (x # xs) = gen_length (Suc n) xs"  | 
| 
 
418991ce7567
tail-recursive implementation for length
 
Andreas Lochbihler 
parents: 
49757 
diff
changeset
 | 
6422  | 
by(simp_all add: gen_length_def)  | 
| 
 
418991ce7567
tail-recursive implementation for length
 
Andreas Lochbihler 
parents: 
49757 
diff
changeset
 | 
6423  | 
|
| 
 
418991ce7567
tail-recursive implementation for length
 
Andreas Lochbihler 
parents: 
49757 
diff
changeset
 | 
6424  | 
declare list.size(3-4)[code del]  | 
| 
 
418991ce7567
tail-recursive implementation for length
 
Andreas Lochbihler 
parents: 
49757 
diff
changeset
 | 
6425  | 
|
| 
 
418991ce7567
tail-recursive implementation for length
 
Andreas Lochbihler 
parents: 
49757 
diff
changeset
 | 
6426  | 
lemma length_code [code]: "length = gen_length 0"  | 
| 
 
418991ce7567
tail-recursive implementation for length
 
Andreas Lochbihler 
parents: 
49757 
diff
changeset
 | 
6427  | 
by(simp add: gen_length_def fun_eq_iff)  | 
| 
 
418991ce7567
tail-recursive implementation for length
 
Andreas Lochbihler 
parents: 
49757 
diff
changeset
 | 
6428  | 
|
| 
 
418991ce7567
tail-recursive implementation for length
 
Andreas Lochbihler 
parents: 
49757 
diff
changeset
 | 
6429  | 
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
 | 
6430  | 
|
| 
49948
 
744934b818c7
moved quite generic material from theory Enum to more appropriate places
 
haftmann 
parents: 
49808 
diff
changeset
 | 
6431  | 
|
| 
37605
 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 
haftmann 
parents: 
37465 
diff
changeset
 | 
6432  | 
subsubsection {* Pretty lists *}
 | 
| 
15064
 
4f3102b50197
- Moved code generator setup for lists from Main.thy to List.thy
 
berghofe 
parents: 
15045 
diff
changeset
 | 
6433  | 
|
| 
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
 | 
6434  | 
ML {*
 | 
| 
 
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
 | 
6435  | 
(* 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
 | 
6436  | 
|
| 
 
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
 | 
6437  | 
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
 | 
6438  | 
sig  | 
| 
55148
 
7e1b7cb54114
avoid (now superfluous) indirect passing of constant names
 
haftmann 
parents: 
55147 
diff
changeset
 | 
6439  | 
val implode_list: Code_Thingol.iterm -> Code_Thingol.iterm list option  | 
| 
50422
 
ee729dbd1b7f
avoid ML_file in large theory files to improve performance of dependency discovery of main HOL (approx. 1s CPU time) -- relevant for any application using it, e.g. small paper sessions;
 
wenzelm 
parents: 
50134 
diff
changeset
 | 
6440  | 
val default_list: int * string  | 
| 
 
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
 | 
6441  | 
-> (Code_Printer.fixity -> Code_Thingol.iterm -> Pretty.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
 | 
6442  | 
-> Code_Printer.fixity -> Code_Thingol.iterm -> Code_Thingol.iterm -> Pretty.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
 | 
6443  | 
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
 | 
6444  | 
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
 | 
6445  | 
|
| 
 
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
 | 
6446  | 
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
 | 
6447  | 
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
 | 
6448  | 
|
| 
 
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
 | 
6449  | 
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
 | 
6450  | 
|
| 
55148
 
7e1b7cb54114
avoid (now superfluous) indirect passing of constant names
 
haftmann 
parents: 
55147 
diff
changeset
 | 
6451  | 
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
 | 
6452  | 
let  | 
| 
55148
 
7e1b7cb54114
avoid (now superfluous) indirect passing of constant names
 
haftmann 
parents: 
55147 
diff
changeset
 | 
6453  | 
    fun dest_cons (IConst { sym = Code_Symbol.Constant @{const_name Cons}, ... } `$ 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
 | 
6454  | 
| 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
 | 
6455  | 
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
 | 
6456  | 
in case t'  | 
| 
55148
 
7e1b7cb54114
avoid (now superfluous) indirect passing of constant names
 
haftmann 
parents: 
55147 
diff
changeset
 | 
6457  | 
   of IConst { sym = Code_Symbol.Constant @{const_name Nil}, ... } => 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
 | 
6458  | 
| _ => 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
 | 
6459  | 
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
 | 
6460  | 
|
| 
 
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
 | 
6461  | 
fun default_list (target_fxy, target_cons) pr fxy t1 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
 | 
6462  | 
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
 | 
6463  | 
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
 | 
6464  | 
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
 | 
6465  | 
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
 | 
6466  | 
);  | 
| 
 
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
 | 
6467  | 
|
| 
 
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
 | 
6468  | 
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
 | 
6469  | 
let  | 
| 
55148
 
7e1b7cb54114
avoid (now superfluous) indirect passing of constant names
 
haftmann 
parents: 
55147 
diff
changeset
 | 
6470  | 
fun pretty literals pr _ vars fxy [(t1, _), (t2, _)] =  | 
| 
 
7e1b7cb54114
avoid (now superfluous) indirect passing of constant names
 
haftmann 
parents: 
55147 
diff
changeset
 | 
6471  | 
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
 | 
6472  | 
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
 | 
6473  | 
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
 | 
6474  | 
| 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
 | 
6475  | 
default_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
 | 
6476  | 
in  | 
| 
 
6646bb548c6b
migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
 
haftmann 
parents: 
52380 
diff
changeset
 | 
6477  | 
    Code_Target.set_printings (Code_Symbol.Constant (@{const_name Cons},
 | 
| 
55148
 
7e1b7cb54114
avoid (now superfluous) indirect passing of constant names
 
haftmann 
parents: 
55147 
diff
changeset
 | 
6478  | 
[(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
 | 
6479  | 
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
 | 
6480  | 
|
| 
 
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
 | 
6481  | 
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
 | 
6482  | 
*}  | 
| 
31055
 
2cf6efca6c71
proper structures for list and string code generation stuff
 
haftmann 
parents: 
31048 
diff
changeset
 | 
6483  | 
|
| 
52435
 
6646bb548c6b
migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
 
haftmann 
parents: 
52380 
diff
changeset
 | 
6484  | 
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
 | 
6485  | 
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
 | 
6486  | 
(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
 | 
6487  | 
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
 | 
6488  | 
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
 | 
6489  | 
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
 | 
6490  | 
| 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
 | 
6491  | 
(SML) "[]"  | 
| 
 
6646bb548c6b
migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
 
haftmann 
parents: 
52380 
diff
changeset
 | 
6492  | 
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
 | 
6493  | 
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
 | 
6494  | 
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
 | 
6495  | 
| 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
 | 
6496  | 
(Haskell) -  | 
| 
 
6646bb548c6b
migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
 
haftmann 
parents: 
52380 
diff
changeset
 | 
6497  | 
| 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
 | 
6498  | 
(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
 | 
6499  | 
|
| 
 
6646bb548c6b
migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
 
haftmann 
parents: 
52380 
diff
changeset
 | 
6500  | 
setup {* fold (List_Code.add_literal_list) ["SML", "OCaml", "Haskell", "Scala"] *}
 | 
| 
31048
 
ac146fc38b51
refined HOL string theories and corresponding ML fragments
 
haftmann 
parents: 
31022 
diff
changeset
 | 
6501  | 
|
| 
 
ac146fc38b51
refined HOL string theories and corresponding ML fragments
 
haftmann 
parents: 
31022 
diff
changeset
 | 
6502  | 
code_reserved SML  | 
| 
 
ac146fc38b51
refined HOL string theories and corresponding ML fragments
 
haftmann 
parents: 
31022 
diff
changeset
 | 
6503  | 
list  | 
| 
 
ac146fc38b51
refined HOL string theories and corresponding ML fragments
 
haftmann 
parents: 
31022 
diff
changeset
 | 
6504  | 
|
| 
 
ac146fc38b51
refined HOL string theories and corresponding ML fragments
 
haftmann 
parents: 
31022 
diff
changeset
 | 
6505  | 
code_reserved OCaml  | 
| 
 
ac146fc38b51
refined HOL string theories and corresponding ML fragments
 
haftmann 
parents: 
31022 
diff
changeset
 | 
6506  | 
list  | 
| 
 
ac146fc38b51
refined HOL string theories and corresponding ML fragments
 
haftmann 
parents: 
31022 
diff
changeset
 | 
6507  | 
|
| 
21061
 
580dfc999ef6
added normal post setup; cleaned up "execution" constants
 
haftmann 
parents: 
21046 
diff
changeset
 | 
6508  | 
|
| 
37424
 
ed431cc99f17
use various predefined Haskell operations when generating code
 
haftmann 
parents: 
37408 
diff
changeset
 | 
6509  | 
subsubsection {* Use convenient predefined operations *}
 | 
| 
 
ed431cc99f17
use various predefined Haskell operations when generating code
 
haftmann 
parents: 
37408 
diff
changeset
 | 
6510  | 
|
| 
52435
 
6646bb548c6b
migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
 
haftmann 
parents: 
52380 
diff
changeset
 | 
6511  | 
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
 | 
6512  | 
constant "op @" \<rightharpoonup>  | 
| 
 
6646bb548c6b
migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
 
haftmann 
parents: 
52380 
diff
changeset
 | 
6513  | 
(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
 | 
6514  | 
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
 | 
6515  | 
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
 | 
6516  | 
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
 | 
6517  | 
| 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
 | 
6518  | 
(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
 | 
6519  | 
| 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
 | 
6520  | 
(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
 | 
6521  | 
| 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
 | 
6522  | 
(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
 | 
6523  | 
| 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
 | 
6524  | 
(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
 | 
6525  | 
| 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
 | 
6526  | 
(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
 | 
6527  | 
| 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
 | 
6528  | 
(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
 | 
6529  | 
| 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
 | 
6530  | 
(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
 | 
6531  | 
| 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
 | 
6532  | 
(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
 | 
6533  | 
| 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
 | 
6534  | 
(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
 | 
6535  | 
| 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
 | 
6536  | 
(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
 | 
6537  | 
| 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
 | 
6538  | 
(Haskell) "any"  | 
| 
37605
 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 
haftmann 
parents: 
37465 
diff
changeset
 | 
6539  | 
|
| 
46147
 
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
 
haftmann 
parents: 
46143 
diff
changeset
 | 
6540  | 
|
| 
 
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
 
haftmann 
parents: 
46143 
diff
changeset
 | 
6541  | 
subsubsection {* Implementation of sets by lists *}
 | 
| 
 
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
 
haftmann 
parents: 
46143 
diff
changeset
 | 
6542  | 
|
| 
 
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
 
haftmann 
parents: 
46143 
diff
changeset
 | 
6543  | 
lemma is_empty_set [code]:  | 
| 
 
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
 
haftmann 
parents: 
46143 
diff
changeset
 | 
6544  | 
"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
 | 
6545  | 
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
 | 
6546  | 
|
| 
 
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
 
haftmann 
parents: 
46143 
diff
changeset
 | 
6547  | 
lemma empty_set [code]:  | 
| 
 
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
 
haftmann 
parents: 
46143 
diff
changeset
 | 
6548  | 
  "{} = set []"
 | 
| 
 
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
 
haftmann 
parents: 
46143 
diff
changeset
 | 
6549  | 
by simp  | 
| 
 
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
 
haftmann 
parents: 
46143 
diff
changeset
 | 
6550  | 
|
| 46156 | 6551  | 
lemma UNIV_coset [code]:  | 
6552  | 
"UNIV = List.coset []"  | 
|
6553  | 
by simp  | 
|
6554  | 
||
6555  | 
lemma compl_set [code]:  | 
|
6556  | 
"- set xs = List.coset xs"  | 
|
6557  | 
by simp  | 
|
6558  | 
||
6559  | 
lemma compl_coset [code]:  | 
|
6560  | 
"- List.coset xs = set xs"  | 
|
6561  | 
by simp  | 
|
6562  | 
||
| 
46147
 
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
 
haftmann 
parents: 
46143 
diff
changeset
 | 
6563  | 
lemma [code]:  | 
| 
 
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
 
haftmann 
parents: 
46143 
diff
changeset
 | 
6564  | 
"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
 | 
6565  | 
"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
 | 
6566  | 
by (simp_all add: member_def)  | 
| 
 
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
 
haftmann 
parents: 
46143 
diff
changeset
 | 
6567  | 
|
| 
 
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
 
haftmann 
parents: 
46143 
diff
changeset
 | 
6568  | 
lemma insert_code [code]:  | 
| 
 
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
 
haftmann 
parents: 
46143 
diff
changeset
 | 
6569  | 
"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
 | 
6570  | 
"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
 | 
6571  | 
by simp_all  | 
| 
 
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
 
haftmann 
parents: 
46143 
diff
changeset
 | 
6572  | 
|
| 
 
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
 
haftmann 
parents: 
46143 
diff
changeset
 | 
6573  | 
lemma remove_code [code]:  | 
| 
 
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
 
haftmann 
parents: 
46143 
diff
changeset
 | 
6574  | 
"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
 | 
6575  | 
"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
 | 
6576  | 
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
 | 
6577  | 
|
| 
49757
 
73ab6d4a9236
rename Set.project to Set.filter - more appropriate name
 
kuncar 
parents: 
49739 
diff
changeset
 | 
6578  | 
lemma filter_set [code]:  | 
| 
 
73ab6d4a9236
rename Set.project to Set.filter - more appropriate name
 
kuncar 
parents: 
49739 
diff
changeset
 | 
6579  | 
"Set.filter P (set xs) = set (filter P xs)"  | 
| 46156 | 6580  | 
by auto  | 
6581  | 
||
6582  | 
lemma image_set [code]:  | 
|
6583  | 
"image f (set xs) = set (map f xs)"  | 
|
6584  | 
by simp  | 
|
6585  | 
||
| 47398 | 6586  | 
lemma subset_code [code]:  | 
6587  | 
"set xs \<le> B \<longleftrightarrow> (\<forall>x\<in>set xs. x \<in> B)"  | 
|
6588  | 
"A \<le> List.coset ys \<longleftrightarrow> (\<forall>y\<in>set ys. y \<notin> A)"  | 
|
6589  | 
"List.coset [] \<le> set [] \<longleftrightarrow> False"  | 
|
6590  | 
by auto  | 
|
6591  | 
||
| 
56790
 
f54097170704
prefer plain ASCII / latex over not-so-universal Unicode;
 
wenzelm 
parents: 
56643 
diff
changeset
 | 
6592  | 
text {* A frequent case -- avoid intermediate sets *}
 | 
| 47398 | 6593  | 
lemma [code_unfold]:  | 
6594  | 
"set xs \<subseteq> set ys \<longleftrightarrow> list_all (\<lambda>x. x \<in> set ys) xs"  | 
|
6595  | 
by (auto simp: list_all_iff)  | 
|
6596  | 
||
| 
46147
 
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
 
haftmann 
parents: 
46143 
diff
changeset
 | 
6597  | 
lemma Ball_set [code]:  | 
| 
 
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
 
haftmann 
parents: 
46143 
diff
changeset
 | 
6598  | 
"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
 | 
6599  | 
by (simp add: list_all_iff)  | 
| 
 
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
 
haftmann 
parents: 
46143 
diff
changeset
 | 
6600  | 
|
| 
 
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
 
haftmann 
parents: 
46143 
diff
changeset
 | 
6601  | 
lemma Bex_set [code]:  | 
| 
 
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
 
haftmann 
parents: 
46143 
diff
changeset
 | 
6602  | 
"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
 | 
6603  | 
by (simp add: list_ex_iff)  | 
| 
 
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
 
haftmann 
parents: 
46143 
diff
changeset
 | 
6604  | 
|
| 
 
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
 
haftmann 
parents: 
46143 
diff
changeset
 | 
6605  | 
lemma card_set [code]:  | 
| 
 
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
 
haftmann 
parents: 
46143 
diff
changeset
 | 
6606  | 
"card (set xs) = length (remdups xs)"  | 
| 
 
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
 
haftmann 
parents: 
46143 
diff
changeset
 | 
6607  | 
proof -  | 
| 
 
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
 
haftmann 
parents: 
46143 
diff
changeset
 | 
6608  | 
have "card (set (remdups xs)) = length (remdups xs)"  | 
| 
 
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
 
haftmann 
parents: 
46143 
diff
changeset
 | 
6609  | 
by (rule distinct_card) simp  | 
| 
 
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
 
haftmann 
parents: 
46143 
diff
changeset
 | 
6610  | 
then show ?thesis by simp  | 
| 
 
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
 
haftmann 
parents: 
46143 
diff
changeset
 | 
6611  | 
qed  | 
| 
 
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
 
haftmann 
parents: 
46143 
diff
changeset
 | 
6612  | 
|
| 46156 | 6613  | 
lemma the_elem_set [code]:  | 
6614  | 
"the_elem (set [x]) = x"  | 
|
6615  | 
by simp  | 
|
6616  | 
||
6617  | 
lemma Pow_set [code]:  | 
|
6618  | 
  "Pow (set []) = {{}}"
 | 
|
6619  | 
"Pow (set (x # xs)) = (let A = Pow (set xs) in A \<union> insert x ` A)"  | 
|
6620  | 
by (simp_all add: Pow_insert Let_def)  | 
|
6621  | 
||
| 46383 | 6622  | 
lemma setsum_code [code]:  | 
6623  | 
"setsum f (set xs) = listsum (map f (remdups xs))"  | 
|
6624  | 
by (simp add: listsum_distinct_conv_setsum_set)  | 
|
| 
46147
 
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
 
haftmann 
parents: 
46143 
diff
changeset
 | 
6625  | 
|
| 
46424
 
b447318e5e1a
adding code equation for Relation.image; adding map_project as correspondence to map_filter on lists
 
bulwahn 
parents: 
46418 
diff
changeset
 | 
6626  | 
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
 | 
6627  | 
  "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
 | 
6628  | 
|
| 
 
b447318e5e1a
adding code equation for Relation.image; adding map_project as correspondence to map_filter on lists
 
bulwahn 
parents: 
46418 
diff
changeset
 | 
6629  | 
lemma [code]:  | 
| 
 
b447318e5e1a
adding code equation for Relation.image; adding map_project as correspondence to map_filter on lists
 
bulwahn 
parents: 
46418 
diff
changeset
 | 
6630  | 
"map_project f (set xs) = set (List.map_filter f xs)"  | 
| 47398 | 6631  | 
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
 | 
6632  | 
|
| 
 
b447318e5e1a
adding code equation for Relation.image; adding map_project as correspondence to map_filter on lists
 
bulwahn 
parents: 
46418 
diff
changeset
 | 
6633  | 
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
 | 
6634  | 
|
| 
49948
 
744934b818c7
moved quite generic material from theory Enum to more appropriate places
 
haftmann 
parents: 
49808 
diff
changeset
 | 
6635  | 
|
| 
46147
 
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
 
haftmann 
parents: 
46143 
diff
changeset
 | 
6636  | 
text {* Operations on relations *}
 | 
| 
 
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
 
haftmann 
parents: 
46143 
diff
changeset
 | 
6637  | 
|
| 
 
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
 
haftmann 
parents: 
46143 
diff
changeset
 | 
6638  | 
lemma product_code [code]:  | 
| 
 
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
 
haftmann 
parents: 
46143 
diff
changeset
 | 
6639  | 
"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
 | 
6640  | 
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
 | 
6641  | 
|
| 
 
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
 
haftmann 
parents: 
46143 
diff
changeset
 | 
6642  | 
lemma Id_on_set [code]:  | 
| 
 
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
 
haftmann 
parents: 
46143 
diff
changeset
 | 
6643  | 
"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
 | 
6644  | 
by (auto simp add: Id_on_def)  | 
| 
 
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
 
haftmann 
parents: 
46143 
diff
changeset
 | 
6645  | 
|
| 
46424
 
b447318e5e1a
adding code equation for Relation.image; adding map_project as correspondence to map_filter on lists
 
bulwahn 
parents: 
46418 
diff
changeset
 | 
6646  | 
lemma [code]:  | 
| 
 
b447318e5e1a
adding code equation for Relation.image; adding map_project as correspondence to map_filter on lists
 
bulwahn 
parents: 
46418 
diff
changeset
 | 
6647  | 
"R `` S = List.map_project (%(x, y). if x : S then Some y else None) R"  | 
| 
 
b447318e5e1a
adding code equation for Relation.image; adding map_project as correspondence to map_filter on lists
 
bulwahn 
parents: 
46418 
diff
changeset
 | 
6648  | 
unfolding map_project_def by (auto split: prod.split split_if_asm)  | 
| 
 
b447318e5e1a
adding code equation for Relation.image; adding map_project as correspondence to map_filter on lists
 
bulwahn 
parents: 
46418 
diff
changeset
 | 
6649  | 
|
| 
46147
 
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
 
haftmann 
parents: 
46143 
diff
changeset
 | 
6650  | 
lemma trancl_set_ntrancl [code]:  | 
| 
 
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
 
haftmann 
parents: 
46143 
diff
changeset
 | 
6651  | 
"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
 | 
6652  | 
by (simp add: finite_trancl_ntranl)  | 
| 
 
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
 
haftmann 
parents: 
46143 
diff
changeset
 | 
6653  | 
|
| 
47433
 
07f4bf913230
renamed "rel_comp" to "relcomp" (to be consistent with, e.g., "relpow")
 
griff 
parents: 
47131 
diff
changeset
 | 
6654  | 
lemma set_relcomp [code]:  | 
| 
46147
 
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
 
haftmann 
parents: 
46143 
diff
changeset
 | 
6655  | 
"set xys O set yzs = set ([(fst xy, snd yz). xy \<leftarrow> xys, yz \<leftarrow> yzs, snd xy = fst yz])"  | 
| 
 
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
 
haftmann 
parents: 
46143 
diff
changeset
 | 
6656  | 
by (auto simp add: Bex_def)  | 
| 
 
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
 
haftmann 
parents: 
46143 
diff
changeset
 | 
6657  | 
|
| 
 
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
 
haftmann 
parents: 
46143 
diff
changeset
 | 
6658  | 
lemma wf_set [code]:  | 
| 
 
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
 
haftmann 
parents: 
46143 
diff
changeset
 | 
6659  | 
"wf (set xs) = acyclic (set xs)"  | 
| 
 
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
 
haftmann 
parents: 
46143 
diff
changeset
 | 
6660  | 
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
 | 
6661  | 
|
| 
55129
 
26bd1cba3ab5
killed 'More_BNFs' by moving its various bits where they (now) belong
 
blanchet 
parents: 
54890 
diff
changeset
 | 
6662  | 
|
| 
53012
 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 
kuncar 
parents: 
52435 
diff
changeset
 | 
6663  | 
subsection {* Setup for Lifting/Transfer *}
 | 
| 
 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 
kuncar 
parents: 
52435 
diff
changeset
 | 
6664  | 
|
| 
 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 
kuncar 
parents: 
52435 
diff
changeset
 | 
6665  | 
subsubsection {* Transfer rules for the Transfer package *}
 | 
| 
 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 
kuncar 
parents: 
52435 
diff
changeset
 | 
6666  | 
|
| 
 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 
kuncar 
parents: 
52435 
diff
changeset
 | 
6667  | 
context  | 
| 
 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 
kuncar 
parents: 
52435 
diff
changeset
 | 
6668  | 
begin  | 
| 
 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 
kuncar 
parents: 
52435 
diff
changeset
 | 
6669  | 
interpretation lifting_syntax .  | 
| 
 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 
kuncar 
parents: 
52435 
diff
changeset
 | 
6670  | 
|
| 
 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 
kuncar 
parents: 
52435 
diff
changeset
 | 
6671  | 
lemma Nil_transfer [transfer_rule]: "(list_all2 A) [] []"  | 
| 
 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 
kuncar 
parents: 
52435 
diff
changeset
 | 
6672  | 
by simp  | 
| 
 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 
kuncar 
parents: 
52435 
diff
changeset
 | 
6673  | 
|
| 
 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 
kuncar 
parents: 
52435 
diff
changeset
 | 
6674  | 
lemma Cons_transfer [transfer_rule]:  | 
| 
 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 
kuncar 
parents: 
52435 
diff
changeset
 | 
6675  | 
"(A ===> list_all2 A ===> list_all2 A) Cons Cons"  | 
| 55945 | 6676  | 
unfolding rel_fun_def by simp  | 
| 
53012
 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 
kuncar 
parents: 
52435 
diff
changeset
 | 
6677  | 
|
| 
55404
 
5cb95b79a51f
transformed 'option' and 'list' into new-style datatypes (but register them as old-style as well)
 
blanchet 
parents: 
55148 
diff
changeset
 | 
6678  | 
lemma case_list_transfer [transfer_rule]:  | 
| 
53012
 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 
kuncar 
parents: 
52435 
diff
changeset
 | 
6679  | 
"(B ===> (A ===> list_all2 A ===> B) ===> list_all2 A ===> B)  | 
| 
55404
 
5cb95b79a51f
transformed 'option' and 'list' into new-style datatypes (but register them as old-style as well)
 
blanchet 
parents: 
55148 
diff
changeset
 | 
6680  | 
case_list case_list"  | 
| 55945 | 6681  | 
unfolding rel_fun_def by (simp split: list.split)  | 
| 
53012
 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 
kuncar 
parents: 
52435 
diff
changeset
 | 
6682  | 
|
| 
55404
 
5cb95b79a51f
transformed 'option' and 'list' into new-style datatypes (but register them as old-style as well)
 
blanchet 
parents: 
55148 
diff
changeset
 | 
6683  | 
lemma rec_list_transfer [transfer_rule]:  | 
| 
53012
 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 
kuncar 
parents: 
52435 
diff
changeset
 | 
6684  | 
"(B ===> (A ===> list_all2 A ===> B ===> B) ===> list_all2 A ===> B)  | 
| 
55404
 
5cb95b79a51f
transformed 'option' and 'list' into new-style datatypes (but register them as old-style as well)
 
blanchet 
parents: 
55148 
diff
changeset
 | 
6685  | 
rec_list rec_list"  | 
| 55945 | 6686  | 
unfolding rel_fun_def by (clarify, erule list_all2_induct, simp_all)  | 
| 
53012
 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 
kuncar 
parents: 
52435 
diff
changeset
 | 
6687  | 
|
| 
 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 
kuncar 
parents: 
52435 
diff
changeset
 | 
6688  | 
lemma tl_transfer [transfer_rule]:  | 
| 
 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 
kuncar 
parents: 
52435 
diff
changeset
 | 
6689  | 
"(list_all2 A ===> list_all2 A) tl tl"  | 
| 
55405
 
0a155051bd9d
use new selector support to define 'the', 'hd', 'tl'
 
blanchet 
parents: 
55404 
diff
changeset
 | 
6690  | 
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
 | 
6691  | 
|
| 
 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 
kuncar 
parents: 
52435 
diff
changeset
 | 
6692  | 
lemma butlast_transfer [transfer_rule]:  | 
| 
 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 
kuncar 
parents: 
52435 
diff
changeset
 | 
6693  | 
"(list_all2 A ===> list_all2 A) butlast butlast"  | 
| 55945 | 6694  | 
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
 | 
6695  | 
|
| 
 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 
kuncar 
parents: 
52435 
diff
changeset
 | 
6696  | 
lemma set_transfer [transfer_rule]:  | 
| 55938 | 6697  | 
"(list_all2 A ===> rel_set A) set set"  | 
| 55584 | 6698  | 
unfolding set_rec[abs_def] by transfer_prover  | 
| 
53012
 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 
kuncar 
parents: 
52435 
diff
changeset
 | 
6699  | 
|
| 55465 | 6700  | 
lemma map_rec: "map f xs = rec_list Nil (%x _ y. Cons (f x) y) xs"  | 
6701  | 
by (induct xs) auto  | 
|
6702  | 
||
| 
53012
 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 
kuncar 
parents: 
52435 
diff
changeset
 | 
6703  | 
lemma map_transfer [transfer_rule]:  | 
| 
 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 
kuncar 
parents: 
52435 
diff
changeset
 | 
6704  | 
"((A ===> B) ===> list_all2 A ===> list_all2 B) map map"  | 
| 55465 | 6705  | 
unfolding map_rec[abs_def] by transfer_prover  | 
| 
53012
 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 
kuncar 
parents: 
52435 
diff
changeset
 | 
6706  | 
|
| 
 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 
kuncar 
parents: 
52435 
diff
changeset
 | 
6707  | 
lemma append_transfer [transfer_rule]:  | 
| 
 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 
kuncar 
parents: 
52435 
diff
changeset
 | 
6708  | 
"(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
 | 
6709  | 
unfolding List.append_def by transfer_prover  | 
| 
 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 
kuncar 
parents: 
52435 
diff
changeset
 | 
6710  | 
|
| 
 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 
kuncar 
parents: 
52435 
diff
changeset
 | 
6711  | 
lemma rev_transfer [transfer_rule]:  | 
| 
 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 
kuncar 
parents: 
52435 
diff
changeset
 | 
6712  | 
"(list_all2 A ===> list_all2 A) rev rev"  | 
| 
 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 
kuncar 
parents: 
52435 
diff
changeset
 | 
6713  | 
unfolding List.rev_def by transfer_prover  | 
| 
 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 
kuncar 
parents: 
52435 
diff
changeset
 | 
6714  | 
|
| 
 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 
kuncar 
parents: 
52435 
diff
changeset
 | 
6715  | 
lemma filter_transfer [transfer_rule]:  | 
| 
 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 
kuncar 
parents: 
52435 
diff
changeset
 | 
6716  | 
"((A ===> op =) ===> list_all2 A ===> list_all2 A) filter filter"  | 
| 
 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 
kuncar 
parents: 
52435 
diff
changeset
 | 
6717  | 
unfolding List.filter_def by transfer_prover  | 
| 
 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 
kuncar 
parents: 
52435 
diff
changeset
 | 
6718  | 
|
| 
 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 
kuncar 
parents: 
52435 
diff
changeset
 | 
6719  | 
lemma fold_transfer [transfer_rule]:  | 
| 
 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 
kuncar 
parents: 
52435 
diff
changeset
 | 
6720  | 
"((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
 | 
6721  | 
unfolding List.fold_def by transfer_prover  | 
| 
 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 
kuncar 
parents: 
52435 
diff
changeset
 | 
6722  | 
|
| 
 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 
kuncar 
parents: 
52435 
diff
changeset
 | 
6723  | 
lemma foldr_transfer [transfer_rule]:  | 
| 
 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 
kuncar 
parents: 
52435 
diff
changeset
 | 
6724  | 
"((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
 | 
6725  | 
unfolding List.foldr_def by transfer_prover  | 
| 
 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 
kuncar 
parents: 
52435 
diff
changeset
 | 
6726  | 
|
| 
 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 
kuncar 
parents: 
52435 
diff
changeset
 | 
6727  | 
lemma foldl_transfer [transfer_rule]:  | 
| 
 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 
kuncar 
parents: 
52435 
diff
changeset
 | 
6728  | 
"((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
 | 
6729  | 
unfolding List.foldl_def by transfer_prover  | 
| 
 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 
kuncar 
parents: 
52435 
diff
changeset
 | 
6730  | 
|
| 
 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 
kuncar 
parents: 
52435 
diff
changeset
 | 
6731  | 
lemma concat_transfer [transfer_rule]:  | 
| 
 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 
kuncar 
parents: 
52435 
diff
changeset
 | 
6732  | 
"(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
 | 
6733  | 
unfolding List.concat_def by transfer_prover  | 
| 
 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 
kuncar 
parents: 
52435 
diff
changeset
 | 
6734  | 
|
| 
 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 
kuncar 
parents: 
52435 
diff
changeset
 | 
6735  | 
lemma drop_transfer [transfer_rule]:  | 
| 
 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 
kuncar 
parents: 
52435 
diff
changeset
 | 
6736  | 
"(op = ===> list_all2 A ===> list_all2 A) drop drop"  | 
| 
 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 
kuncar 
parents: 
52435 
diff
changeset
 | 
6737  | 
unfolding List.drop_def by transfer_prover  | 
| 
 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 
kuncar 
parents: 
52435 
diff
changeset
 | 
6738  | 
|
| 
 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 
kuncar 
parents: 
52435 
diff
changeset
 | 
6739  | 
lemma take_transfer [transfer_rule]:  | 
| 
 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 
kuncar 
parents: 
52435 
diff
changeset
 | 
6740  | 
"(op = ===> list_all2 A ===> list_all2 A) take take"  | 
| 
 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 
kuncar 
parents: 
52435 
diff
changeset
 | 
6741  | 
unfolding List.take_def by transfer_prover  | 
| 
 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 
kuncar 
parents: 
52435 
diff
changeset
 | 
6742  | 
|
| 
 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 
kuncar 
parents: 
52435 
diff
changeset
 | 
6743  | 
lemma list_update_transfer [transfer_rule]:  | 
| 
 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 
kuncar 
parents: 
52435 
diff
changeset
 | 
6744  | 
"(list_all2 A ===> op = ===> A ===> list_all2 A) list_update list_update"  | 
| 
 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 
kuncar 
parents: 
52435 
diff
changeset
 | 
6745  | 
unfolding list_update_def by transfer_prover  | 
| 
 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 
kuncar 
parents: 
52435 
diff
changeset
 | 
6746  | 
|
| 
 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 
kuncar 
parents: 
52435 
diff
changeset
 | 
6747  | 
lemma takeWhile_transfer [transfer_rule]:  | 
| 
 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 
kuncar 
parents: 
52435 
diff
changeset
 | 
6748  | 
"((A ===> op =) ===> list_all2 A ===> list_all2 A) takeWhile takeWhile"  | 
| 
 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 
kuncar 
parents: 
52435 
diff
changeset
 | 
6749  | 
unfolding takeWhile_def by transfer_prover  | 
| 
 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 
kuncar 
parents: 
52435 
diff
changeset
 | 
6750  | 
|
| 
 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 
kuncar 
parents: 
52435 
diff
changeset
 | 
6751  | 
lemma dropWhile_transfer [transfer_rule]:  | 
| 
 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 
kuncar 
parents: 
52435 
diff
changeset
 | 
6752  | 
"((A ===> op =) ===> list_all2 A ===> list_all2 A) dropWhile dropWhile"  | 
| 
 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 
kuncar 
parents: 
52435 
diff
changeset
 | 
6753  | 
unfolding dropWhile_def by transfer_prover  | 
| 
 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 
kuncar 
parents: 
52435 
diff
changeset
 | 
6754  | 
|
| 
 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 
kuncar 
parents: 
52435 
diff
changeset
 | 
6755  | 
lemma zip_transfer [transfer_rule]:  | 
| 55944 | 6756  | 
"(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
 | 
6757  | 
unfolding zip_def by transfer_prover  | 
| 
 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 
kuncar 
parents: 
52435 
diff
changeset
 | 
6758  | 
|
| 
53721
 
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
 
traytel 
parents: 
53689 
diff
changeset
 | 
6759  | 
lemma product_transfer [transfer_rule]:  | 
| 55944 | 6760  | 
"(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
 | 
6761  | 
unfolding List.product_def by transfer_prover  | 
| 
 
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
 
traytel 
parents: 
53689 
diff
changeset
 | 
6762  | 
|
| 
 
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
 
traytel 
parents: 
53689 
diff
changeset
 | 
6763  | 
lemma product_lists_transfer [transfer_rule]:  | 
| 
 
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
 
traytel 
parents: 
53689 
diff
changeset
 | 
6764  | 
"(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
 | 
6765  | 
unfolding product_lists_def by transfer_prover  | 
| 
 
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
 
traytel 
parents: 
53689 
diff
changeset
 | 
6766  | 
|
| 
53012
 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 
kuncar 
parents: 
52435 
diff
changeset
 | 
6767  | 
lemma insert_transfer [transfer_rule]:  | 
| 
 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 
kuncar 
parents: 
52435 
diff
changeset
 | 
6768  | 
assumes [transfer_rule]: "bi_unique A"  | 
| 
 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 
kuncar 
parents: 
52435 
diff
changeset
 | 
6769  | 
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
 | 
6770  | 
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
 | 
6771  | 
|
| 
 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 
kuncar 
parents: 
52435 
diff
changeset
 | 
6772  | 
lemma find_transfer [transfer_rule]:  | 
| 55525 | 6773  | 
"((A ===> op =) ===> 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
 | 
6774  | 
unfolding List.find_def by transfer_prover  | 
| 
 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 
kuncar 
parents: 
52435 
diff
changeset
 | 
6775  | 
|
| 
 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 
kuncar 
parents: 
52435 
diff
changeset
 | 
6776  | 
lemma remove1_transfer [transfer_rule]:  | 
| 
 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 
kuncar 
parents: 
52435 
diff
changeset
 | 
6777  | 
assumes [transfer_rule]: "bi_unique A"  | 
| 
 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 
kuncar 
parents: 
52435 
diff
changeset
 | 
6778  | 
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
 | 
6779  | 
unfolding remove1_def by transfer_prover  | 
| 
 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 
kuncar 
parents: 
52435 
diff
changeset
 | 
6780  | 
|
| 
 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 
kuncar 
parents: 
52435 
diff
changeset
 | 
6781  | 
lemma removeAll_transfer [transfer_rule]:  | 
| 
 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 
kuncar 
parents: 
52435 
diff
changeset
 | 
6782  | 
assumes [transfer_rule]: "bi_unique A"  | 
| 
 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 
kuncar 
parents: 
52435 
diff
changeset
 | 
6783  | 
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
 | 
6784  | 
unfolding removeAll_def by transfer_prover  | 
| 
 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 
kuncar 
parents: 
52435 
diff
changeset
 | 
6785  | 
|
| 
 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 
kuncar 
parents: 
52435 
diff
changeset
 | 
6786  | 
lemma distinct_transfer [transfer_rule]:  | 
| 
 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 
kuncar 
parents: 
52435 
diff
changeset
 | 
6787  | 
assumes [transfer_rule]: "bi_unique A"  | 
| 
 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 
kuncar 
parents: 
52435 
diff
changeset
 | 
6788  | 
shows "(list_all2 A ===> op =) distinct distinct"  | 
| 
 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 
kuncar 
parents: 
52435 
diff
changeset
 | 
6789  | 
unfolding distinct_def by transfer_prover  | 
| 
 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 
kuncar 
parents: 
52435 
diff
changeset
 | 
6790  | 
|
| 
 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 
kuncar 
parents: 
52435 
diff
changeset
 | 
6791  | 
lemma remdups_transfer [transfer_rule]:  | 
| 
 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 
kuncar 
parents: 
52435 
diff
changeset
 | 
6792  | 
assumes [transfer_rule]: "bi_unique A"  | 
| 
 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 
kuncar 
parents: 
52435 
diff
changeset
 | 
6793  | 
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
 | 
6794  | 
unfolding remdups_def by transfer_prover  | 
| 
 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 
kuncar 
parents: 
52435 
diff
changeset
 | 
6795  | 
|
| 
53721
 
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
 
traytel 
parents: 
53689 
diff
changeset
 | 
6796  | 
lemma remdups_adj_transfer [transfer_rule]:  | 
| 
 
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
 
traytel 
parents: 
53689 
diff
changeset
 | 
6797  | 
assumes [transfer_rule]: "bi_unique A"  | 
| 
 
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
 
traytel 
parents: 
53689 
diff
changeset
 | 
6798  | 
shows "(list_all2 A ===> list_all2 A) remdups_adj remdups_adj"  | 
| 55945 | 6799  | 
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
 | 
6800  | 
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
 | 
6801  | 
|
| 
53012
 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 
kuncar 
parents: 
52435 
diff
changeset
 | 
6802  | 
lemma replicate_transfer [transfer_rule]:  | 
| 
 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 
kuncar 
parents: 
52435 
diff
changeset
 | 
6803  | 
"(op = ===> A ===> list_all2 A) replicate replicate"  | 
| 
 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 
kuncar 
parents: 
52435 
diff
changeset
 | 
6804  | 
unfolding replicate_def by transfer_prover  | 
| 
 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 
kuncar 
parents: 
52435 
diff
changeset
 | 
6805  | 
|
| 
 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 
kuncar 
parents: 
52435 
diff
changeset
 | 
6806  | 
lemma length_transfer [transfer_rule]:  | 
| 
 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 
kuncar 
parents: 
52435 
diff
changeset
 | 
6807  | 
"(list_all2 A ===> op =) length length"  | 
| 
56643
 
41d3596d8a64
move size hooks together, with new one preceding old one and sharing same theory data
 
blanchet 
parents: 
56545 
diff
changeset
 | 
6808  | 
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
 | 
6809  | 
|
| 
 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 
kuncar 
parents: 
52435 
diff
changeset
 | 
6810  | 
lemma rotate1_transfer [transfer_rule]:  | 
| 
 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 
kuncar 
parents: 
52435 
diff
changeset
 | 
6811  | 
"(list_all2 A ===> list_all2 A) rotate1 rotate1"  | 
| 
 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 
kuncar 
parents: 
52435 
diff
changeset
 | 
6812  | 
unfolding rotate1_def by transfer_prover  | 
| 
 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 
kuncar 
parents: 
52435 
diff
changeset
 | 
6813  | 
|
| 
 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 
kuncar 
parents: 
52435 
diff
changeset
 | 
6814  | 
lemma rotate_transfer [transfer_rule]:  | 
| 
 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 
kuncar 
parents: 
52435 
diff
changeset
 | 
6815  | 
"(op = ===> list_all2 A ===> list_all2 A) rotate rotate"  | 
| 
 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 
kuncar 
parents: 
52435 
diff
changeset
 | 
6816  | 
unfolding rotate_def [abs_def] by transfer_prover  | 
| 
 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 
kuncar 
parents: 
52435 
diff
changeset
 | 
6817  | 
|
| 
 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 
kuncar 
parents: 
52435 
diff
changeset
 | 
6818  | 
lemma list_all2_transfer [transfer_rule]:  | 
| 
 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 
kuncar 
parents: 
52435 
diff
changeset
 | 
6819  | 
"((A ===> B ===> op =) ===> list_all2 A ===> list_all2 B ===> op =)  | 
| 
 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 
kuncar 
parents: 
52435 
diff
changeset
 | 
6820  | 
list_all2 list_all2"  | 
| 
55524
 
f41ef840f09d
folded 'list_all2' with the relator generated by 'datatype_new'
 
blanchet 
parents: 
55473 
diff
changeset
 | 
6821  | 
apply (subst (4) list_all2_iff [abs_def])  | 
| 
 
f41ef840f09d
folded 'list_all2' with the relator generated by 'datatype_new'
 
blanchet 
parents: 
55473 
diff
changeset
 | 
6822  | 
apply (subst (3) list_all2_iff [abs_def])  | 
| 
53012
 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 
kuncar 
parents: 
52435 
diff
changeset
 | 
6823  | 
apply transfer_prover  | 
| 
 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 
kuncar 
parents: 
52435 
diff
changeset
 | 
6824  | 
done  | 
| 
 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 
kuncar 
parents: 
52435 
diff
changeset
 | 
6825  | 
|
| 
 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 
kuncar 
parents: 
52435 
diff
changeset
 | 
6826  | 
lemma sublist_transfer [transfer_rule]:  | 
| 55938 | 6827  | 
"(list_all2 A ===> rel_set (op =) ===> list_all2 A) sublist sublist"  | 
| 
53012
 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 
kuncar 
parents: 
52435 
diff
changeset
 | 
6828  | 
unfolding sublist_def [abs_def] by transfer_prover  | 
| 
 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 
kuncar 
parents: 
52435 
diff
changeset
 | 
6829  | 
|
| 
 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 
kuncar 
parents: 
52435 
diff
changeset
 | 
6830  | 
lemma partition_transfer [transfer_rule]:  | 
| 55944 | 6831  | 
"((A ===> op =) ===> 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
 | 
6832  | 
partition partition"  | 
| 
 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 
kuncar 
parents: 
52435 
diff
changeset
 | 
6833  | 
unfolding partition_def by transfer_prover  | 
| 
 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 
kuncar 
parents: 
52435 
diff
changeset
 | 
6834  | 
|
| 
 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 
kuncar 
parents: 
52435 
diff
changeset
 | 
6835  | 
lemma lists_transfer [transfer_rule]:  | 
| 55938 | 6836  | 
"(rel_set A ===> rel_set (list_all2 A)) lists lists"  | 
| 55945 | 6837  | 
apply (rule rel_funI, rule rel_setI)  | 
| 
53012
 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 
kuncar 
parents: 
52435 
diff
changeset
 | 
6838  | 
apply (erule lists.induct, simp)  | 
| 55938 | 6839  | 
apply (simp only: rel_set_def list_all2_Cons1, metis lists.Cons)  | 
| 
53012
 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 
kuncar 
parents: 
52435 
diff
changeset
 | 
6840  | 
apply (erule lists.induct, simp)  | 
| 55938 | 6841  | 
apply (simp only: rel_set_def list_all2_Cons2, metis lists.Cons)  | 
| 
53012
 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 
kuncar 
parents: 
52435 
diff
changeset
 | 
6842  | 
done  | 
| 
 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 
kuncar 
parents: 
52435 
diff
changeset
 | 
6843  | 
|
| 
 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 
kuncar 
parents: 
52435 
diff
changeset
 | 
6844  | 
lemma set_Cons_transfer [transfer_rule]:  | 
| 55938 | 6845  | 
"(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
 | 
6846  | 
set_Cons set_Cons"  | 
| 55945 | 6847  | 
unfolding rel_fun_def rel_set_def set_Cons_def  | 
| 
53012
 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 
kuncar 
parents: 
52435 
diff
changeset
 | 
6848  | 
apply safe  | 
| 
 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 
kuncar 
parents: 
52435 
diff
changeset
 | 
6849  | 
apply (simp add: list_all2_Cons1, fast)  | 
| 
 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 
kuncar 
parents: 
52435 
diff
changeset
 | 
6850  | 
apply (simp add: list_all2_Cons2, fast)  | 
| 
 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 
kuncar 
parents: 
52435 
diff
changeset
 | 
6851  | 
done  | 
| 
 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 
kuncar 
parents: 
52435 
diff
changeset
 | 
6852  | 
|
| 
 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 
kuncar 
parents: 
52435 
diff
changeset
 | 
6853  | 
lemma listset_transfer [transfer_rule]:  | 
| 55938 | 6854  | 
"(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
 | 
6855  | 
unfolding listset_def by transfer_prover  | 
| 
 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 
kuncar 
parents: 
52435 
diff
changeset
 | 
6856  | 
|
| 
 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 
kuncar 
parents: 
52435 
diff
changeset
 | 
6857  | 
lemma null_transfer [transfer_rule]:  | 
| 
 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 
kuncar 
parents: 
52435 
diff
changeset
 | 
6858  | 
"(list_all2 A ===> op =) List.null List.null"  | 
| 55945 | 6859  | 
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
 | 
6860  | 
|
| 
 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 
kuncar 
parents: 
52435 
diff
changeset
 | 
6861  | 
lemma list_all_transfer [transfer_rule]:  | 
| 
 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 
kuncar 
parents: 
52435 
diff
changeset
 | 
6862  | 
"((A ===> op =) ===> list_all2 A ===> op =) list_all list_all"  | 
| 
 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 
kuncar 
parents: 
52435 
diff
changeset
 | 
6863  | 
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
 | 
6864  | 
|
| 
 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 
kuncar 
parents: 
52435 
diff
changeset
 | 
6865  | 
lemma list_ex_transfer [transfer_rule]:  | 
| 
 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 
kuncar 
parents: 
52435 
diff
changeset
 | 
6866  | 
"((A ===> op =) ===> list_all2 A ===> op =) list_ex list_ex"  | 
| 
 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 
kuncar 
parents: 
52435 
diff
changeset
 | 
6867  | 
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
 | 
6868  | 
|
| 
 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 
kuncar 
parents: 
52435 
diff
changeset
 | 
6869  | 
lemma splice_transfer [transfer_rule]:  | 
| 
 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 
kuncar 
parents: 
52435 
diff
changeset
 | 
6870  | 
"(list_all2 A ===> list_all2 A ===> list_all2 A) splice splice"  | 
| 55945 | 6871  | 
apply (rule rel_funI, erule list_all2_induct, simp add: rel_fun_def, simp)  | 
6872  | 
apply (rule rel_funI)  | 
|
6873  | 
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
 | 
6874  | 
done  | 
| 
 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 
kuncar 
parents: 
52435 
diff
changeset
 | 
6875  | 
|
| 
 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 
kuncar 
parents: 
52435 
diff
changeset
 | 
6876  | 
lemma listsum_transfer[transfer_rule]:  | 
| 
 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 
kuncar 
parents: 
52435 
diff
changeset
 | 
6877  | 
assumes [transfer_rule]: "A 0 0"  | 
| 
 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 
kuncar 
parents: 
52435 
diff
changeset
 | 
6878  | 
assumes [transfer_rule]: "(A ===> A ===> A) op + op +"  | 
| 
 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 
kuncar 
parents: 
52435 
diff
changeset
 | 
6879  | 
shows "(list_all2 A ===> A) listsum listsum"  | 
| 
 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 
kuncar 
parents: 
52435 
diff
changeset
 | 
6880  | 
unfolding listsum_def[abs_def]  | 
| 
 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 
kuncar 
parents: 
52435 
diff
changeset
 | 
6881  | 
by transfer_prover  | 
| 
 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 
kuncar 
parents: 
52435 
diff
changeset
 | 
6882  | 
|
| 57599 | 6883  | 
lemma rtrancl_parametric [transfer_rule]:  | 
6884  | 
assumes [transfer_rule]: "bi_unique A" "bi_total A"  | 
|
6885  | 
shows "(rel_set (rel_prod A A) ===> rel_set (rel_prod A A)) rtrancl rtrancl"  | 
|
6886  | 
unfolding rtrancl_def by transfer_prover  | 
|
6887  | 
||
| 23388 | 6888  | 
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
 | 
6889  | 
|
| 
53012
 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 
kuncar 
parents: 
52435 
diff
changeset
 | 
6890  | 
end  |