| author | wenzelm | 
| Mon, 24 Feb 2014 13:10:33 +0100 | |
| changeset 55714 | ed1b789d0b21 | 
| parent 55642 | 63beb38e9258 | 
| child 55807 | fd31d0e70eb8 | 
| 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 | 
| 54555 | 8 | imports Presburger Code_Numeral Quotient Lifting_Set Lifting_Option Lifting_Product | 
| 15131 | 9 | begin | 
| 923 | 10 | |
| 55584 | 11 | datatype_new (set: 'a) list (map: map rel: list_all2) = | 
| 55405 
0a155051bd9d
use new selector support to define 'the', 'hd', 'tl'
 blanchet parents: 
55404diff
changeset | 12 |     =: Nil (defaults tl: "[]")  ("[]")
 | 
| 
0a155051bd9d
use new selector support to define 'the', 'hd', 'tl'
 blanchet parents: 
55404diff
changeset | 13 | | Cons (hd: 'a) (tl: "'a list") (infixr "#" 65) | 
| 55531 
601ca8efa000
renamed 'datatype_new_compat' to 'datatype_compat'
 blanchet parents: 
55525diff
changeset | 14 | datatype_compat list | 
| 55404 
5cb95b79a51f
transformed 'option' and 'list' into new-style datatypes (but register them as old-style as well)
 blanchet parents: 
55148diff
changeset | 15 | |
| 55406 | 16 | lemma [case_names Nil Cons, cases type: list]: | 
| 17 |   -- {* for backward compatibility -- names of variables differ *}
 | |
| 18 | "(y = [] \<Longrightarrow> P) \<Longrightarrow> (\<And>a list. y = a # list \<Longrightarrow> P) \<Longrightarrow> P" | |
| 19 | by (rule list.exhaust) | |
| 20 | ||
| 21 | lemma [case_names Nil Cons, induct type: list]: | |
| 22 |   -- {* for backward compatibility -- names of variables differ *}
 | |
| 23 | "P [] \<Longrightarrow> (\<And>a list. P list \<Longrightarrow> P (a # list)) \<Longrightarrow> P list" | |
| 24 | by (rule list.induct) | |
| 25 | ||
| 55442 | 26 | text {* Compatibility: *}
 | 
| 27 | ||
| 55404 
5cb95b79a51f
transformed 'option' and 'list' into new-style datatypes (but register them as old-style as well)
 blanchet parents: 
55148diff
changeset | 28 | setup {* Sign.mandatory_path "list" *}
 | 
| 
5cb95b79a51f
transformed 'option' and 'list' into new-style datatypes (but register them as old-style as well)
 blanchet parents: 
55148diff
changeset | 29 | |
| 
5cb95b79a51f
transformed 'option' and 'list' into new-style datatypes (but register them as old-style as well)
 blanchet parents: 
55148diff
changeset | 30 | lemmas inducts = list.induct | 
| 
5cb95b79a51f
transformed 'option' and 'list' into new-style datatypes (but register them as old-style as well)
 blanchet parents: 
55148diff
changeset | 31 | lemmas recs = list.rec | 
| 
5cb95b79a51f
transformed 'option' and 'list' into new-style datatypes (but register them as old-style as well)
 blanchet parents: 
55148diff
changeset | 32 | lemmas cases = list.case | 
| 
5cb95b79a51f
transformed 'option' and 'list' into new-style datatypes (but register them as old-style as well)
 blanchet parents: 
55148diff
changeset | 33 | |
| 
5cb95b79a51f
transformed 'option' and 'list' into new-style datatypes (but register them as old-style as well)
 blanchet parents: 
55148diff
changeset | 34 | setup {* Sign.parent_path *}
 | 
| 
5cb95b79a51f
transformed 'option' and 'list' into new-style datatypes (but register them as old-style as well)
 blanchet parents: 
55148diff
changeset | 35 | |
| 34941 | 36 | syntax | 
| 37 |   -- {* list Enumeration *}
 | |
| 35115 | 38 |   "_list" :: "args => 'a list"    ("[(_)]")
 | 
| 34941 | 39 | |
| 40 | translations | |
| 41 | "[x, xs]" == "x#[xs]" | |
| 42 | "[x]" == "x#[]" | |
| 43 | ||
| 35115 | 44 | |
| 45 | subsection {* Basic list processing functions *}
 | |
| 15302 | 46 | |
| 50548 | 47 | primrec last :: "'a list \<Rightarrow> 'a" where | 
| 48 | "last (x # xs) = (if xs = [] then x else last xs)" | |
| 49 | ||
| 50 | primrec butlast :: "'a list \<Rightarrow> 'a list" where | |
| 51 | "butlast []= []" | | |
| 52 | "butlast (x # xs) = (if xs = [] then [] else x # butlast xs)" | |
| 53 | ||
| 55584 | 54 | declare list.set[simp del, code del] | 
| 55 | ||
| 56 | lemma set_simps[simp, code, code_post]: | |
| 57 |   "set [] = {}"
 | |
| 58 | "set (x # xs) = insert x (set xs)" | |
| 59 | by (simp_all add: list.set) | |
| 60 | ||
| 61 | lemma set_rec: "set xs = rec_list {} (\<lambda>x _. insert x) xs"
 | |
| 62 | by (induct xs) auto | |
| 50548 | 63 | |
| 64 | definition coset :: "'a list \<Rightarrow> 'a set" where | |
| 65 | [simp]: "coset xs = - set xs" | |
| 66 | ||
| 67 | primrec append :: "'a list \<Rightarrow> 'a list \<Rightarrow> 'a list" (infixr "@" 65) where | |
| 68 | append_Nil: "[] @ ys = ys" | | |
| 69 | append_Cons: "(x#xs) @ ys = x # xs @ ys" | |
| 70 | ||
| 71 | primrec rev :: "'a list \<Rightarrow> 'a list" where | |
| 72 | "rev [] = []" | | |
| 73 | "rev (x # xs) = rev xs @ [x]" | |
| 74 | ||
| 75 | primrec filter:: "('a \<Rightarrow> bool) \<Rightarrow> 'a list \<Rightarrow> 'a list" where
 | |
| 76 | "filter P [] = []" | | |
| 77 | "filter P (x # xs) = (if P x then x # filter P xs else filter P xs)" | |
| 34941 | 78 | |
| 79 | syntax | |
| 80 |   -- {* Special syntax for filter *}
 | |
| 35115 | 81 |   "_filter" :: "[pttrn, 'a list, bool] => 'a list"    ("(1[_<-_./ _])")
 | 
| 34941 | 82 | |
| 83 | translations | |
| 84 | "[x<-xs . P]"== "CONST filter (%x. P) xs" | |
| 85 | ||
| 86 | syntax (xsymbols) | |
| 35115 | 87 |   "_filter" :: "[pttrn, 'a list, bool] => 'a list"("(1[_\<leftarrow>_ ./ _])")
 | 
| 34941 | 88 | syntax (HTML output) | 
| 35115 | 89 |   "_filter" :: "[pttrn, 'a list, bool] => 'a list"("(1[_\<leftarrow>_ ./ _])")
 | 
| 34941 | 90 | |
| 50548 | 91 | primrec fold :: "('a \<Rightarrow> 'b \<Rightarrow> 'b) \<Rightarrow> 'a list \<Rightarrow> 'b \<Rightarrow> 'b" where
 | 
| 92 | fold_Nil: "fold f [] = id" | | |
| 93 | fold_Cons: "fold f (x # xs) = fold f xs \<circ> f x" | |
| 94 | ||
| 95 | primrec foldr :: "('a \<Rightarrow> 'b \<Rightarrow> 'b) \<Rightarrow> 'a list \<Rightarrow> 'b \<Rightarrow> 'b" where
 | |
| 96 | foldr_Nil: "foldr f [] = id" | | |
| 97 | foldr_Cons: "foldr f (x # xs) = f x \<circ> foldr f xs" | |
| 98 | ||
| 99 | primrec foldl :: "('b \<Rightarrow> 'a \<Rightarrow> 'b) \<Rightarrow> 'b \<Rightarrow> 'a list \<Rightarrow> 'b" where
 | |
| 100 | foldl_Nil: "foldl f a [] = a" | | |
| 101 | foldl_Cons: "foldl f a (x # xs) = foldl f (f a x) xs" | |
| 102 | ||
| 103 | primrec concat:: "'a list list \<Rightarrow> 'a list" where | |
| 104 | "concat [] = []" | | |
| 105 | "concat (x # xs) = x @ concat xs" | |
| 106 | ||
| 107 | definition (in monoid_add) listsum :: "'a list \<Rightarrow> 'a" where | |
| 108 | "listsum xs = foldr plus xs 0" | |
| 109 | ||
| 110 | primrec drop:: "nat \<Rightarrow> 'a list \<Rightarrow> 'a list" where | |
| 111 | drop_Nil: "drop n [] = []" | | |
| 112 | drop_Cons: "drop n (x # xs) = (case n of 0 \<Rightarrow> x # xs | Suc m \<Rightarrow> drop m xs)" | |
| 34941 | 113 |   -- {*Warning: simpset does not contain this definition, but separate
 | 
| 114 |        theorems for @{text "n = 0"} and @{text "n = Suc k"} *}
 | |
| 115 | ||
| 50548 | 116 | primrec take:: "nat \<Rightarrow> 'a list \<Rightarrow> 'a list" where | 
| 117 | take_Nil:"take n [] = []" | | |
| 118 | take_Cons: "take n (x # xs) = (case n of 0 \<Rightarrow> [] | Suc m \<Rightarrow> x # take m xs)" | |
| 34941 | 119 |   -- {*Warning: simpset does not contain this definition, but separate
 | 
| 120 |        theorems for @{text "n = 0"} and @{text "n = Suc k"} *}
 | |
| 121 | ||
| 50548 | 122 | primrec nth :: "'a list => nat => 'a" (infixl "!" 100) where | 
| 123 | nth_Cons: "(x # xs) ! n = (case n of 0 \<Rightarrow> x | Suc k \<Rightarrow> xs ! k)" | |
| 34941 | 124 |   -- {*Warning: simpset does not contain this definition, but separate
 | 
| 125 |        theorems for @{text "n = 0"} and @{text "n = Suc k"} *}
 | |
| 126 | ||
| 50548 | 127 | primrec list_update :: "'a list \<Rightarrow> nat \<Rightarrow> 'a \<Rightarrow> 'a list" where | 
| 128 | "list_update [] i v = []" | | |
| 129 | "list_update (x # xs) i v = | |
| 130 | (case i of 0 \<Rightarrow> v # xs | Suc j \<Rightarrow> x # list_update xs j v)" | |
| 923 | 131 | |
| 41229 
d797baa3d57c
replaced command 'nonterminals' by slightly modernized version 'nonterminal';
 wenzelm parents: 
41075diff
changeset | 132 | nonterminal lupdbinds and lupdbind | 
| 5077 
71043526295f
* HOL/List: new function list_update written xs[i:=v] that updates the i-th
 nipkow parents: 
4643diff
changeset | 133 | |
| 923 | 134 | syntax | 
| 13366 | 135 |   "_lupdbind":: "['a, 'a] => lupdbind"    ("(2_ :=/ _)")
 | 
| 136 |   "" :: "lupdbind => lupdbinds"    ("_")
 | |
| 137 |   "_lupdbinds" :: "[lupdbind, lupdbinds] => lupdbinds"    ("_,/ _")
 | |
| 138 |   "_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: 
4643diff
changeset | 139 | |
| 923 | 140 | translations | 
| 35115 | 141 | "_LUpdate xs (_lupdbinds b bs)" == "_LUpdate (_LUpdate xs b) bs" | 
| 34941 | 142 | "xs[i:=x]" == "CONST list_update xs i x" | 
| 143 | ||
| 50548 | 144 | primrec takeWhile :: "('a \<Rightarrow> bool) \<Rightarrow> 'a list \<Rightarrow> 'a list" where
 | 
| 145 | "takeWhile P [] = []" | | |
| 146 | "takeWhile P (x # xs) = (if P x then x # takeWhile P xs else [])" | |
| 147 | ||
| 148 | primrec dropWhile :: "('a \<Rightarrow> bool) \<Rightarrow> 'a list \<Rightarrow> 'a list" where
 | |
| 149 | "dropWhile P [] = []" | | |
| 150 | "dropWhile P (x # xs) = (if P x then dropWhile P xs else x # xs)" | |
| 151 | ||
| 152 | primrec zip :: "'a list \<Rightarrow> 'b list \<Rightarrow> ('a \<times> 'b) list" where
 | |
| 153 | "zip xs [] = []" | | |
| 154 | zip_Cons: "zip xs (y # ys) = | |
| 155 | (case xs of [] => [] | z # zs => (z, y) # zip zs ys)" | |
| 34941 | 156 |   -- {*Warning: simpset does not contain this definition, but separate
 | 
| 157 |        theorems for @{text "xs = []"} and @{text "xs = z # zs"} *}
 | |
| 158 | ||
| 50548 | 159 | primrec product :: "'a list \<Rightarrow> 'b list \<Rightarrow> ('a \<times> 'b) list" where
 | 
| 160 | "product [] _ = []" | | |
| 161 | "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: 
49808diff
changeset | 162 | |
| 
744934b818c7
moved quite generic material from theory Enum to more appropriate places
 haftmann parents: 
49808diff
changeset | 163 | hide_const (open) product | 
| 
744934b818c7
moved quite generic material from theory Enum to more appropriate places
 haftmann parents: 
49808diff
changeset | 164 | |
| 53721 
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
 traytel parents: 
53689diff
changeset | 165 | primrec product_lists :: "'a list list \<Rightarrow> 'a list list" where | 
| 
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
 traytel parents: 
53689diff
changeset | 166 | "product_lists [] = [[]]" | | 
| 
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
 traytel parents: 
53689diff
changeset | 167 | "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: 
53689diff
changeset | 168 | |
| 50548 | 169 | primrec upt :: "nat \<Rightarrow> nat \<Rightarrow> nat list" ("(1[_..</_'])") where
 | 
| 170 | upt_0: "[i..<0] = []" | | |
| 171 | upt_Suc: "[i..<(Suc j)] = (if i <= j then [i..<j] @ [j] else [])" | |
| 172 | ||
| 173 | definition insert :: "'a \<Rightarrow> 'a list \<Rightarrow> 'a list" where | |
| 174 | "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: 
34942diff
changeset | 175 | |
| 36176 
3fe7e97ccca8
replaced generic 'hide' command by more conventional 'hide_class', 'hide_type', 'hide_const', 'hide_fact' -- frees some popular keywords;
 wenzelm parents: 
36154diff
changeset | 176 | hide_const (open) insert | 
| 
3fe7e97ccca8
replaced generic 'hide' command by more conventional 'hide_class', 'hide_type', 'hide_const', 'hide_fact' -- frees some popular keywords;
 wenzelm parents: 
36154diff
changeset | 177 | hide_fact (open) insert_def | 
| 34978 
874150ddd50a
canonical insert operation; generalized lemma foldl_apply_inv to foldl_apply
 haftmann parents: 
34942diff
changeset | 178 | |
| 47122 | 179 | primrec find :: "('a \<Rightarrow> bool) \<Rightarrow> 'a list \<Rightarrow> 'a option" where
 | 
| 50548 | 180 | "find _ [] = None" | | 
| 181 | "find P (x#xs) = (if P x then Some x else find P xs)" | |
| 47122 | 182 | |
| 183 | hide_const (open) find | |
| 184 | ||
| 51096 | 185 | primrec those :: "'a option list \<Rightarrow> 'a list option" | 
| 186 | where | |
| 187 | "those [] = Some []" | | |
| 188 | "those (x # xs) = (case x of | |
| 189 | None \<Rightarrow> None | |
| 55466 | 190 | | Some y \<Rightarrow> map_option (Cons y) (those xs))" | 
| 51096 | 191 | |
| 50548 | 192 | primrec remove1 :: "'a \<Rightarrow> 'a list \<Rightarrow> 'a list" where | 
| 193 | "remove1 x [] = []" | | |
| 194 | "remove1 x (y # xs) = (if x = y then xs else y # remove1 x xs)" | |
| 195 | ||
| 196 | primrec removeAll :: "'a \<Rightarrow> 'a list \<Rightarrow> 'a list" where | |
| 197 | "removeAll x [] = []" | | |
| 198 | "removeAll x (y # xs) = (if x = y then removeAll x xs else y # removeAll x xs)" | |
| 199 | ||
| 200 | primrec distinct :: "'a list \<Rightarrow> bool" where | |
| 201 | "distinct [] \<longleftrightarrow> True" | | |
| 202 | "distinct (x # xs) \<longleftrightarrow> x \<notin> set xs \<and> distinct xs" | |
| 203 | ||
| 204 | primrec remdups :: "'a list \<Rightarrow> 'a list" where | |
| 205 | "remdups [] = []" | | |
| 206 | "remdups (x # xs) = (if x \<in> set xs then remdups xs else x # remdups xs)" | |
| 207 | ||
| 53721 
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
 traytel parents: 
53689diff
changeset | 208 | fun remdups_adj :: "'a list \<Rightarrow> 'a list" where | 
| 
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
 traytel parents: 
53689diff
changeset | 209 | "remdups_adj [] = []" | | 
| 
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
 traytel parents: 
53689diff
changeset | 210 | "remdups_adj [x] = [x]" | | 
| 
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
 traytel parents: 
53689diff
changeset | 211 | "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: 
53689diff
changeset | 212 | |
| 50548 | 213 | primrec replicate :: "nat \<Rightarrow> 'a \<Rightarrow> 'a list" where | 
| 214 | replicate_0: "replicate 0 x = []" | | |
| 215 | replicate_Suc: "replicate (Suc n) x = x # replicate n x" | |
| 3342 
ec3b55fcb165
New operator "lists" for formalizing sets of lists
 paulson parents: 
3320diff
changeset | 216 | |
| 13142 | 217 | text {*
 | 
| 14589 | 218 |   Function @{text size} is overloaded for all datatypes. Users may
 | 
| 13366 | 219 |   refer to the list version as @{text length}. *}
 | 
| 13142 | 220 | |
| 50548 | 221 | abbreviation length :: "'a list \<Rightarrow> nat" where | 
| 222 | "length \<equiv> size" | |
| 15307 | 223 | |
| 51173 | 224 | definition enumerate :: "nat \<Rightarrow> 'a list \<Rightarrow> (nat \<times> 'a) list" where | 
| 225 | enumerate_eq_zip: "enumerate n xs = zip [n..<n + length xs] xs" | |
| 226 | ||
| 46440 
d4994e2e7364
use 'primrec' to define "rotate1", for uniformity (and to help first-order tools that rely on "Spec_Rules")
 blanchet parents: 
46439diff
changeset | 227 | primrec rotate1 :: "'a list \<Rightarrow> 'a list" where | 
| 50548 | 228 | "rotate1 [] = []" | | 
| 229 | "rotate1 (x # xs) = xs @ [x]" | |
| 230 | ||
| 231 | definition rotate :: "nat \<Rightarrow> 'a list \<Rightarrow> 'a list" where | |
| 232 | "rotate n = rotate1 ^^ n" | |
| 233 | ||
| 234 | definition sublist :: "'a list => nat set => 'a list" where | |
| 235 | "sublist xs A = map fst (filter (\<lambda>p. snd p \<in> A) (zip xs [0..<size xs]))" | |
| 236 | ||
| 237 | primrec sublists :: "'a list \<Rightarrow> 'a list list" where | |
| 238 | "sublists [] = [[]]" | | |
| 239 | "sublists (x#xs) = (let xss = sublists xs in map (Cons x) xss @ xss)" | |
| 240 | ||
| 241 | primrec n_lists :: "nat \<Rightarrow> 'a list \<Rightarrow> 'a list list" where | |
| 242 | "n_lists 0 xs = [[]]" | | |
| 243 | "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: 
49808diff
changeset | 244 | |
| 
744934b818c7
moved quite generic material from theory Enum to more appropriate places
 haftmann parents: 
49808diff
changeset | 245 | hide_const (open) n_lists | 
| 
744934b818c7
moved quite generic material from theory Enum to more appropriate places
 haftmann parents: 
49808diff
changeset | 246 | |
| 40593 
1e57b18d27b1
code eqn for slice was missing; redefined splice with fun
 nipkow parents: 
40365diff
changeset | 247 | fun splice :: "'a list \<Rightarrow> 'a list \<Rightarrow> 'a list" where | 
| 
1e57b18d27b1
code eqn for slice was missing; redefined splice with fun
 nipkow parents: 
40365diff
changeset | 248 | "splice [] ys = ys" | | 
| 
1e57b18d27b1
code eqn for slice was missing; redefined splice with fun
 nipkow parents: 
40365diff
changeset | 249 | "splice xs [] = xs" | | 
| 
1e57b18d27b1
code eqn for slice was missing; redefined splice with fun
 nipkow parents: 
40365diff
changeset | 250 | "splice (x#xs) (y#ys) = x # y # splice xs ys" | 
| 21061 
580dfc999ef6
added normal post setup; cleaned up "execution" constants
 haftmann parents: 
21046diff
changeset | 251 | |
| 26771 | 252 | text{*
 | 
| 253 | \begin{figure}[htbp]
 | |
| 254 | \fbox{
 | |
| 255 | \begin{tabular}{l}
 | |
| 27381 | 256 | @{lemma "[a,b]@[c,d] = [a,b,c,d]" by simp}\\
 | 
| 257 | @{lemma "length [a,b,c] = 3" by simp}\\
 | |
| 258 | @{lemma "set [a,b,c] = {a,b,c}" by simp}\\
 | |
| 259 | @{lemma "map f [a,b,c] = [f a, f b, f c]" by simp}\\
 | |
| 260 | @{lemma "rev [a,b,c] = [c,b,a]" by simp}\\
 | |
| 261 | @{lemma "hd [a,b,c,d] = a" by simp}\\
 | |
| 262 | @{lemma "tl [a,b,c,d] = [b,c,d]" by simp}\\
 | |
| 263 | @{lemma "last [a,b,c,d] = d" by simp}\\
 | |
| 264 | @{lemma "butlast [a,b,c,d] = [a,b,c]" by simp}\\
 | |
| 265 | @{lemma[source] "filter (\<lambda>n::nat. n<2) [0,2,1] = [0,1]" by simp}\\
 | |
| 266 | @{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: 
46125diff
changeset | 267 | @{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: 
47131diff
changeset | 268 | @{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: 
47131diff
changeset | 269 | @{lemma "foldl f x [a,b,c] = f (f (f x a) b) c" by simp}\\
 | 
| 27381 | 270 | @{lemma "zip [a,b,c] [x,y,z] = [(a,x),(b,y),(c,z)]" by simp}\\
 | 
| 271 | @{lemma "zip [a,b] [x,y,z] = [(a,x),(b,y)]" by simp}\\
 | |
| 51173 | 272 | @{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: 
49808diff
changeset | 273 | @{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: 
53689diff
changeset | 274 | @{lemma "product_lists [[a,b], [c], [d,e]] = [[a,c,d], [a,c,e], [b,c,d], [b,c,e]]" by simp}\\
 | 
| 27381 | 275 | @{lemma "splice [a,b,c] [x,y,z] = [a,x,b,y,c,z]" by simp}\\
 | 
| 276 | @{lemma "splice [a,b,c,d] [x,y] = [a,x,b,y,c,d]" by simp}\\
 | |
| 277 | @{lemma "take 2 [a,b,c,d] = [a,b]" by simp}\\
 | |
| 278 | @{lemma "take 6 [a,b,c,d] = [a,b,c,d]" by simp}\\
 | |
| 279 | @{lemma "drop 2 [a,b,c,d] = [c,d]" by simp}\\
 | |
| 280 | @{lemma "drop 6 [a,b,c,d] = []" by simp}\\
 | |
| 281 | @{lemma "takeWhile (%n::nat. n<3) [1,2,3,0] = [1,2]" by simp}\\
 | |
| 282 | @{lemma "dropWhile (%n::nat. n<3) [1,2,3,0] = [3,0]" by simp}\\
 | |
| 283 | @{lemma "distinct [2,0,1::nat]" by simp}\\
 | |
| 284 | @{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: 
53689diff
changeset | 285 | @{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: 
34942diff
changeset | 286 | @{lemma "List.insert 2 [0::nat,1,2] = [0,1,2]" by (simp add: List.insert_def)}\\
 | 
| 35295 | 287 | @{lemma "List.insert 3 [0::nat,1,2] = [3,0,1,2]" by (simp add: List.insert_def)}\\
 | 
| 47122 | 288 | @{lemma "List.find (%i::int. i>0) [0,0] = None" by simp}\\
 | 
| 289 | @{lemma "List.find (%i::int. i>0) [0,1,0,2] = Some 1" by simp}\\
 | |
| 27381 | 290 | @{lemma "remove1 2 [2,0,2,1::nat,2] = [0,2,1,2]" by simp}\\
 | 
| 27693 | 291 | @{lemma "removeAll 2 [2,0,2,1::nat,2] = [0,1]" by simp}\\
 | 
| 27381 | 292 | @{lemma "nth [a,b,c,d] 2 = c" by simp}\\
 | 
| 293 | @{lemma "[a,b,c,d][2 := x] = [a,b,x,d]" by simp}\\
 | |
| 294 | @{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: 
49808diff
changeset | 295 | @{lemma "sublists [a,b] = [[a, b], [a], [b], []]" by simp}\\
 | 
| 
744934b818c7
moved quite generic material from theory Enum to more appropriate places
 haftmann parents: 
49808diff
changeset | 296 | @{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: 
46439diff
changeset | 297 | @{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: 
46439diff
changeset | 298 | @{lemma "rotate 3 [a,b,c,d] = [d,a,b,c]" by (simp add:rotate_def eval_nat_numeral)}\\
 | 
| 40077 | 299 | @{lemma "replicate 4 a = [a,a,a,a]" by (simp add:eval_nat_numeral)}\\
 | 
| 300 | @{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: 
47131diff
changeset | 301 | @{lemma "listsum [1,2,3::nat] = 6" by (simp add: listsum_def)}
 | 
| 26771 | 302 | \end{tabular}}
 | 
| 303 | \caption{Characteristic examples}
 | |
| 304 | \label{fig:Characteristic}
 | |
| 305 | \end{figure}
 | |
| 29927 | 306 | Figure~\ref{fig:Characteristic} shows characteristic examples
 | 
| 26771 | 307 | that should give an intuitive understanding of the above functions. | 
| 308 | *} | |
| 309 | ||
| 24616 | 310 | text{* The following simple sort functions are intended for proofs,
 | 
| 311 | not for efficient implementations. *} | |
| 312 | ||
| 25221 
5ded95dda5df
append/member: more light-weight way to declare authentic syntax;
 wenzelm parents: 
25215diff
changeset | 313 | context linorder | 
| 
5ded95dda5df
append/member: more light-weight way to declare authentic syntax;
 wenzelm parents: 
25215diff
changeset | 314 | begin | 
| 
5ded95dda5df
append/member: more light-weight way to declare authentic syntax;
 wenzelm parents: 
25215diff
changeset | 315 | |
| 39915 
ecf97cf3d248
turned distinct and sorted into inductive predicates: yields nice induction principles for free; more elegant proofs
 haftmann parents: 
39774diff
changeset | 316 | 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: 
39774diff
changeset | 317 | Nil [iff]: "sorted []" | 
| 
ecf97cf3d248
turned distinct and sorted into inductive predicates: yields nice induction principles for free; more elegant proofs
 haftmann parents: 
39774diff
changeset | 318 | | 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: 
39774diff
changeset | 319 | |
| 
ecf97cf3d248
turned distinct and sorted into inductive predicates: yields nice induction principles for free; more elegant proofs
 haftmann parents: 
39774diff
changeset | 320 | lemma sorted_single [iff]: | 
| 
ecf97cf3d248
turned distinct and sorted into inductive predicates: yields nice induction principles for free; more elegant proofs
 haftmann parents: 
39774diff
changeset | 321 | "sorted [x]" | 
| 
ecf97cf3d248
turned distinct and sorted into inductive predicates: yields nice induction principles for free; more elegant proofs
 haftmann parents: 
39774diff
changeset | 322 | by (rule sorted.Cons) auto | 
| 
ecf97cf3d248
turned distinct and sorted into inductive predicates: yields nice induction principles for free; more elegant proofs
 haftmann parents: 
39774diff
changeset | 323 | |
| 
ecf97cf3d248
turned distinct and sorted into inductive predicates: yields nice induction principles for free; more elegant proofs
 haftmann parents: 
39774diff
changeset | 324 | lemma sorted_many: | 
| 
ecf97cf3d248
turned distinct and sorted into inductive predicates: yields nice induction principles for free; more elegant proofs
 haftmann parents: 
39774diff
changeset | 325 | "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: 
39774diff
changeset | 326 | 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: 
39774diff
changeset | 327 | |
| 
ecf97cf3d248
turned distinct and sorted into inductive predicates: yields nice induction principles for free; more elegant proofs
 haftmann parents: 
39774diff
changeset | 328 | 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: 
39774diff
changeset | 329 | "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: 
39774diff
changeset | 330 | 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: 
39774diff
changeset | 331 | |
| 
ecf97cf3d248
turned distinct and sorted into inductive predicates: yields nice induction principles for free; more elegant proofs
 haftmann parents: 
39774diff
changeset | 332 | lemma [code]: | 
| 
ecf97cf3d248
turned distinct and sorted into inductive predicates: yields nice induction principles for free; more elegant proofs
 haftmann parents: 
39774diff
changeset | 333 | "sorted [] \<longleftrightarrow> True" | 
| 
ecf97cf3d248
turned distinct and sorted into inductive predicates: yields nice induction principles for free; more elegant proofs
 haftmann parents: 
39774diff
changeset | 334 | "sorted [x] \<longleftrightarrow> True" | 
| 
ecf97cf3d248
turned distinct and sorted into inductive predicates: yields nice induction principles for free; more elegant proofs
 haftmann parents: 
39774diff
changeset | 335 | by simp_all | 
| 24697 | 336 | |
| 33639 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 337 | primrec insort_key :: "('b \<Rightarrow> 'a) \<Rightarrow> 'b \<Rightarrow> 'b list \<Rightarrow> 'b list" where
 | 
| 50548 | 338 | "insort_key f x [] = [x]" | | 
| 339 | "insort_key f x (y#ys) = | |
| 340 | (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: 
33593diff
changeset | 341 | |
| 35195 | 342 | definition sort_key :: "('b \<Rightarrow> 'a) \<Rightarrow> 'b list \<Rightarrow> 'b list" where
 | 
| 50548 | 343 | "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: 
33593diff
changeset | 344 | |
| 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: 
40195diff
changeset | 345 | definition insort_insert_key :: "('b \<Rightarrow> 'a) \<Rightarrow> 'b \<Rightarrow> 'b list \<Rightarrow> 'b list" where
 | 
| 50548 | 346 | "insort_insert_key f x xs = | 
| 347 | (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: 
40195diff
changeset | 348 | |
| 33639 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 349 | 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: 
33593diff
changeset | 350 | 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: 
40195diff
changeset | 351 | abbreviation "insort_insert \<equiv> insort_insert_key (\<lambda>x. x)" | 
| 35608 | 352 | |
| 25221 
5ded95dda5df
append/member: more light-weight way to declare authentic syntax;
 wenzelm parents: 
25215diff
changeset | 353 | end | 
| 
5ded95dda5df
append/member: more light-weight way to declare authentic syntax;
 wenzelm parents: 
25215diff
changeset | 354 | |
| 24616 | 355 | |
| 23388 | 356 | subsubsection {* List comprehension *}
 | 
| 23192 | 357 | |
| 24349 | 358 | text{* Input syntax for Haskell-like list comprehension notation.
 | 
| 359 | Typical example: @{text"[(x,y). x \<leftarrow> xs, y \<leftarrow> ys, x \<noteq> y]"},
 | |
| 360 | the list of all pairs of distinct elements from @{text xs} and @{text ys}.
 | |
| 361 | The syntax is as in Haskell, except that @{text"|"} becomes a dot
 | |
| 362 | (like in Isabelle's set comprehension): @{text"[e. x \<leftarrow> xs, \<dots>]"} rather than
 | |
| 363 | \verb![e| x <- xs, ...]!. | |
| 364 | ||
| 365 | The qualifiers after the dot are | |
| 366 | \begin{description}
 | |
| 367 | \item[generators] @{text"p \<leftarrow> xs"},
 | |
| 24476 
f7ad9fbbeeaa
turned list comprehension translations into ML to optimize base case
 nipkow parents: 
24471diff
changeset | 368 |  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: 
24471diff
changeset | 369 | \item[guards] @{text"b"}, where @{text b} is a boolean expression.
 | 
| 
f7ad9fbbeeaa
turned list comprehension translations into ML to optimize base case
 nipkow parents: 
24471diff
changeset | 370 | %\item[local bindings] @ {text"let x = e"}.
 | 
| 24349 | 371 | \end{description}
 | 
| 23240 | 372 | |
| 24476 
f7ad9fbbeeaa
turned list comprehension translations into ML to optimize base case
 nipkow parents: 
24471diff
changeset | 373 | 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: 
24471diff
changeset | 374 | misunderstandings, the translation into desugared form is not reversed | 
| 
f7ad9fbbeeaa
turned list comprehension translations into ML to optimize base case
 nipkow parents: 
24471diff
changeset | 375 | 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: 
24471diff
changeset | 376 | optmized to @{term"map (%x. e) xs"}.
 | 
| 23240 | 377 | |
| 24349 | 378 | It is easy to write short list comprehensions which stand for complex | 
| 379 | expressions. During proofs, they may become unreadable (and | |
| 380 | mangled). In such cases it can be advisable to introduce separate | |
| 381 | definitions for the list comprehensions in question. *} | |
| 382 | ||
| 46138 
85f8d8a8c711
improved list comprehension syntax: more careful treatment of position constraints, which enables PIDE markup;
 wenzelm parents: 
46133diff
changeset | 383 | nonterminal lc_qual and lc_quals | 
| 23192 | 384 | |
| 385 | syntax | |
| 46138 
85f8d8a8c711
improved list comprehension syntax: more careful treatment of position constraints, which enables PIDE markup;
 wenzelm parents: 
46133diff
changeset | 386 |   "_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: 
46133diff
changeset | 387 |   "_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: 
46133diff
changeset | 388 |   "_lc_test" :: "bool \<Rightarrow> lc_qual" ("_")
 | 
| 
85f8d8a8c711
improved list comprehension syntax: more careful treatment of position constraints, which enables PIDE markup;
 wenzelm parents: 
46133diff
changeset | 389 |   (*"_lc_let" :: "letbinds => lc_qual"  ("let _")*)
 | 
| 
85f8d8a8c711
improved list comprehension syntax: more careful treatment of position constraints, which enables PIDE markup;
 wenzelm parents: 
46133diff
changeset | 390 |   "_lc_end" :: "lc_quals" ("]")
 | 
| 
85f8d8a8c711
improved list comprehension syntax: more careful treatment of position constraints, which enables PIDE markup;
 wenzelm parents: 
46133diff
changeset | 391 |   "_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: 
46133diff
changeset | 392 | "_lc_abs" :: "'a => 'b list => 'b list" | 
| 23192 | 393 | |
| 24476 
f7ad9fbbeeaa
turned list comprehension translations into ML to optimize base case
 nipkow parents: 
24471diff
changeset | 394 | (* These are easier than ML code but cannot express the optimized | 
| 
f7ad9fbbeeaa
turned list comprehension translations into ML to optimize base case
 nipkow parents: 
24471diff
changeset | 395 | translation of [e. p<-xs] | 
| 23192 | 396 | translations | 
| 46138 
85f8d8a8c711
improved list comprehension syntax: more careful treatment of position constraints, which enables PIDE markup;
 wenzelm parents: 
46133diff
changeset | 397 | "[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: 
46133diff
changeset | 398 | "_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: 
46133diff
changeset | 399 | => "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: 
46133diff
changeset | 400 | "[e. P]" => "if P then [e] else []" | 
| 
85f8d8a8c711
improved list comprehension syntax: more careful treatment of position constraints, which enables PIDE markup;
 wenzelm parents: 
46133diff
changeset | 401 | "_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: 
46133diff
changeset | 402 | => "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: 
46133diff
changeset | 403 | "_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: 
46133diff
changeset | 404 | => "_Let b (_listcompr e Q Qs)" | 
| 24476 
f7ad9fbbeeaa
turned list comprehension translations into ML to optimize base case
 nipkow parents: 
24471diff
changeset | 405 | *) | 
| 23240 | 406 | |
| 23279 
e39dd93161d9
tuned list comprehension, changed filter syntax from : to <-
 nipkow parents: 
23246diff
changeset | 407 | syntax (xsymbols) | 
| 46138 
85f8d8a8c711
improved list comprehension syntax: more careful treatment of position constraints, which enables PIDE markup;
 wenzelm parents: 
46133diff
changeset | 408 |   "_lc_gen" :: "'a \<Rightarrow> 'a list \<Rightarrow> lc_qual"  ("_ \<leftarrow> _")
 | 
| 23279 
e39dd93161d9
tuned list comprehension, changed filter syntax from : to <-
 nipkow parents: 
23246diff
changeset | 409 | syntax (HTML output) | 
| 46138 
85f8d8a8c711
improved list comprehension syntax: more careful treatment of position constraints, which enables PIDE markup;
 wenzelm parents: 
46133diff
changeset | 410 |   "_lc_gen" :: "'a \<Rightarrow> 'a list \<Rightarrow> lc_qual"  ("_ \<leftarrow> _")
 | 
| 24349 | 411 | |
| 52143 | 412 | parse_translation {*
 | 
| 46138 
85f8d8a8c711
improved list comprehension syntax: more careful treatment of position constraints, which enables PIDE markup;
 wenzelm parents: 
46133diff
changeset | 413 | let | 
| 
85f8d8a8c711
improved list comprehension syntax: more careful treatment of position constraints, which enables PIDE markup;
 wenzelm parents: 
46133diff
changeset | 414 |     val NilC = Syntax.const @{const_syntax Nil};
 | 
| 
85f8d8a8c711
improved list comprehension syntax: more careful treatment of position constraints, which enables PIDE markup;
 wenzelm parents: 
46133diff
changeset | 415 |     val ConsC = Syntax.const @{const_syntax Cons};
 | 
| 
85f8d8a8c711
improved list comprehension syntax: more careful treatment of position constraints, which enables PIDE markup;
 wenzelm parents: 
46133diff
changeset | 416 |     val mapC = Syntax.const @{const_syntax map};
 | 
| 
85f8d8a8c711
improved list comprehension syntax: more careful treatment of position constraints, which enables PIDE markup;
 wenzelm parents: 
46133diff
changeset | 417 |     val concatC = Syntax.const @{const_syntax concat};
 | 
| 
85f8d8a8c711
improved list comprehension syntax: more careful treatment of position constraints, which enables PIDE markup;
 wenzelm parents: 
46133diff
changeset | 418 |     val IfC = Syntax.const @{const_syntax If};
 | 
| 
85f8d8a8c711
improved list comprehension syntax: more careful treatment of position constraints, which enables PIDE markup;
 wenzelm parents: 
46133diff
changeset | 419 | |
| 
85f8d8a8c711
improved list comprehension syntax: more careful treatment of position constraints, which enables PIDE markup;
 wenzelm parents: 
46133diff
changeset | 420 | fun single x = ConsC $ x $ NilC; | 
| 
85f8d8a8c711
improved list comprehension syntax: more careful treatment of position constraints, which enables PIDE markup;
 wenzelm parents: 
46133diff
changeset | 421 | |
| 
85f8d8a8c711
improved list comprehension syntax: more careful treatment of position constraints, which enables PIDE markup;
 wenzelm parents: 
46133diff
changeset | 422 | 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: 
46133diff
changeset | 423 | let | 
| 
85f8d8a8c711
improved list comprehension syntax: more careful treatment of position constraints, which enables PIDE markup;
 wenzelm parents: 
46133diff
changeset | 424 | (* FIXME proper name context!? *) | 
| 
85f8d8a8c711
improved list comprehension syntax: more careful treatment of position constraints, which enables PIDE markup;
 wenzelm parents: 
46133diff
changeset | 425 | val x = | 
| 
85f8d8a8c711
improved list comprehension syntax: more careful treatment of position constraints, which enables PIDE markup;
 wenzelm parents: 
46133diff
changeset | 426 | 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: 
46133diff
changeset | 427 | 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: 
46133diff
changeset | 428 |         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: 
46133diff
changeset | 429 | val case2 = | 
| 
85f8d8a8c711
improved list comprehension syntax: more careful treatment of position constraints, which enables PIDE markup;
 wenzelm parents: 
46133diff
changeset | 430 |           Syntax.const @{syntax_const "_case1"} $
 | 
| 
85f8d8a8c711
improved list comprehension syntax: more careful treatment of position constraints, which enables PIDE markup;
 wenzelm parents: 
46133diff
changeset | 431 |             Syntax.const @{const_syntax dummy_pattern} $ NilC;
 | 
| 
85f8d8a8c711
improved list comprehension syntax: more careful treatment of position constraints, which enables PIDE markup;
 wenzelm parents: 
46133diff
changeset | 432 |         val cs = Syntax.const @{syntax_const "_case2"} $ case1 $ case2;
 | 
| 51678 
1e33b81c328a
allow redundant cases in the list comprehension translation
 traytel parents: 
51673diff
changeset | 433 | 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: 
46133diff
changeset | 434 | |
| 
85f8d8a8c711
improved list comprehension syntax: more careful treatment of position constraints, which enables PIDE markup;
 wenzelm parents: 
46133diff
changeset | 435 | fun abs_tr ctxt p e opti = | 
| 
85f8d8a8c711
improved list comprehension syntax: more careful treatment of position constraints, which enables PIDE markup;
 wenzelm parents: 
46133diff
changeset | 436 | (case Term_Position.strip_positions p of | 
| 
85f8d8a8c711
improved list comprehension syntax: more careful treatment of position constraints, which enables PIDE markup;
 wenzelm parents: 
46133diff
changeset | 437 | Free (s, T) => | 
| 
85f8d8a8c711
improved list comprehension syntax: more careful treatment of position constraints, which enables PIDE markup;
 wenzelm parents: 
46133diff
changeset | 438 | let | 
| 
85f8d8a8c711
improved list comprehension syntax: more careful treatment of position constraints, which enables PIDE markup;
 wenzelm parents: 
46133diff
changeset | 439 | val thy = Proof_Context.theory_of ctxt; | 
| 
85f8d8a8c711
improved list comprehension syntax: more careful treatment of position constraints, which enables PIDE markup;
 wenzelm parents: 
46133diff
changeset | 440 | 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: 
46133diff
changeset | 441 | in | 
| 
85f8d8a8c711
improved list comprehension syntax: more careful treatment of position constraints, which enables PIDE markup;
 wenzelm parents: 
46133diff
changeset | 442 | if Sign.declared_const thy s' | 
| 
85f8d8a8c711
improved list comprehension syntax: more careful treatment of position constraints, which enables PIDE markup;
 wenzelm parents: 
46133diff
changeset | 443 | 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: 
46133diff
changeset | 444 | 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: 
46133diff
changeset | 445 | end | 
| 
85f8d8a8c711
improved list comprehension syntax: more careful treatment of position constraints, which enables PIDE markup;
 wenzelm parents: 
46133diff
changeset | 446 | | _ => (pat_tr ctxt p e opti, false)); | 
| 
85f8d8a8c711
improved list comprehension syntax: more careful treatment of position constraints, which enables PIDE markup;
 wenzelm parents: 
46133diff
changeset | 447 | |
| 
85f8d8a8c711
improved list comprehension syntax: more careful treatment of position constraints, which enables PIDE markup;
 wenzelm parents: 
46133diff
changeset | 448 |     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: 
46133diff
changeset | 449 | let | 
| 
85f8d8a8c711
improved list comprehension syntax: more careful treatment of position constraints, which enables PIDE markup;
 wenzelm parents: 
46133diff
changeset | 450 | val res = | 
| 
85f8d8a8c711
improved list comprehension syntax: more careful treatment of position constraints, which enables PIDE markup;
 wenzelm parents: 
46133diff
changeset | 451 | (case qs of | 
| 
85f8d8a8c711
improved list comprehension syntax: more careful treatment of position constraints, which enables PIDE markup;
 wenzelm parents: 
46133diff
changeset | 452 |                 Const (@{syntax_const "_lc_end"}, _) => single e
 | 
| 
85f8d8a8c711
improved list comprehension syntax: more careful treatment of position constraints, which enables PIDE markup;
 wenzelm parents: 
46133diff
changeset | 453 |               | 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: 
46133diff
changeset | 454 | in IfC $ b $ res $ NilC end | 
| 
85f8d8a8c711
improved list comprehension syntax: more careful treatment of position constraints, which enables PIDE markup;
 wenzelm parents: 
46133diff
changeset | 455 | | lc_tr ctxt | 
| 
85f8d8a8c711
improved list comprehension syntax: more careful treatment of position constraints, which enables PIDE markup;
 wenzelm parents: 
46133diff
changeset | 456 |             [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: 
46133diff
changeset | 457 |               Const(@{syntax_const "_lc_end"}, _)] =
 | 
| 
85f8d8a8c711
improved list comprehension syntax: more careful treatment of position constraints, which enables PIDE markup;
 wenzelm parents: 
46133diff
changeset | 458 | (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: 
46133diff
changeset | 459 | (f, true) => mapC $ f $ es | 
| 
85f8d8a8c711
improved list comprehension syntax: more careful treatment of position constraints, which enables PIDE markup;
 wenzelm parents: 
46133diff
changeset | 460 | | (f, false) => concatC $ (mapC $ f $ es)) | 
| 
85f8d8a8c711
improved list comprehension syntax: more careful treatment of position constraints, which enables PIDE markup;
 wenzelm parents: 
46133diff
changeset | 461 | | lc_tr ctxt | 
| 
85f8d8a8c711
improved list comprehension syntax: more careful treatment of position constraints, which enables PIDE markup;
 wenzelm parents: 
46133diff
changeset | 462 |             [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: 
46133diff
changeset | 463 |               Const (@{syntax_const "_lc_quals"}, _) $ q $ qs] =
 | 
| 
85f8d8a8c711
improved list comprehension syntax: more careful treatment of position constraints, which enables PIDE markup;
 wenzelm parents: 
46133diff
changeset | 464 | 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: 
46133diff
changeset | 465 | 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: 
46133diff
changeset | 466 | |
| 
85f8d8a8c711
improved list comprehension syntax: more careful treatment of position constraints, which enables PIDE markup;
 wenzelm parents: 
46133diff
changeset | 467 |   in [(@{syntax_const "_listcompr"}, lc_tr)] end
 | 
| 24349 | 468 | *} | 
| 23279 
e39dd93161d9
tuned list comprehension, changed filter syntax from : to <-
 nipkow parents: 
23246diff
changeset | 469 | |
| 51272 | 470 | ML_val {*
 | 
| 42167 | 471 | let | 
| 472 |     val read = Syntax.read_term @{context};
 | |
| 473 |     fun check s1 s2 = read s1 aconv read s2 orelse error ("Check failed: " ^ quote s1);
 | |
| 474 | in | |
| 475 | check "[(x,y,z). b]" "if b then [(x, y, z)] else []"; | |
| 476 | check "[(x,y,z). x\<leftarrow>xs]" "map (\<lambda>x. (x, y, z)) xs"; | |
| 477 | check "[e x y. x\<leftarrow>xs, y\<leftarrow>ys]" "concat (map (\<lambda>x. map (\<lambda>y. e x y) ys) xs)"; | |
| 478 | check "[(x,y,z). x<a, x>b]" "if x < a then if b < x then [(x, y, z)] else [] else []"; | |
| 479 | check "[(x,y,z). x\<leftarrow>xs, x>b]" "concat (map (\<lambda>x. if b < x then [(x, y, z)] else []) xs)"; | |
| 480 | check "[(x,y,z). x<a, x\<leftarrow>xs]" "if x < a then map (\<lambda>x. (x, y, z)) xs else []"; | |
| 481 | check "[(x,y). Cons True x \<leftarrow> xs]" | |
| 482 | "concat (map (\<lambda>xa. case xa of [] \<Rightarrow> [] | True # x \<Rightarrow> [(x, y)] | False # x \<Rightarrow> []) xs)"; | |
| 483 | check "[(x,y,z). Cons x [] \<leftarrow> xs]" | |
| 484 | "concat (map (\<lambda>xa. case xa of [] \<Rightarrow> [] | [x] \<Rightarrow> [(x, y, z)] | x # aa # lista \<Rightarrow> []) xs)"; | |
| 485 | check "[(x,y,z). x<a, x>b, x=d]" | |
| 486 | "if x < a then if b < x then if x = d then [(x, y, z)] else [] else [] else []"; | |
| 487 | check "[(x,y,z). x<a, x>b, y\<leftarrow>ys]" | |
| 488 | "if x < a then if b < x then map (\<lambda>y. (x, y, z)) ys else [] else []"; | |
| 489 | check "[(x,y,z). x<a, x\<leftarrow>xs,y>b]" | |
| 490 | "if x < a then concat (map (\<lambda>x. if b < y then [(x, y, z)] else []) xs) else []"; | |
| 491 | check "[(x,y,z). x<a, x\<leftarrow>xs, y\<leftarrow>ys]" | |
| 492 | "if x < a then concat (map (\<lambda>x. map (\<lambda>y. (x, y, z)) ys) xs) else []"; | |
| 493 | check "[(x,y,z). x\<leftarrow>xs, x>b, y<a]" | |
| 494 | "concat (map (\<lambda>x. if b < x then if y < a then [(x, y, z)] else [] else []) xs)"; | |
| 495 | check "[(x,y,z). x\<leftarrow>xs, x>b, y\<leftarrow>ys]" | |
| 496 | "concat (map (\<lambda>x. if b < x then map (\<lambda>y. (x, y, z)) ys else []) xs)"; | |
| 497 | check "[(x,y,z). x\<leftarrow>xs, y\<leftarrow>ys,y>x]" | |
| 498 | "concat (map (\<lambda>x. concat (map (\<lambda>y. if x < y then [(x, y, z)] else []) ys)) xs)"; | |
| 499 | check "[(x,y,z). x\<leftarrow>xs, y\<leftarrow>ys,z\<leftarrow>zs]" | |
| 500 | "concat (map (\<lambda>x. concat (map (\<lambda>y. map (\<lambda>z. (x, y, z)) zs) ys)) xs)" | |
| 501 | end; | |
| 502 | *} | |
| 503 | ||
| 35115 | 504 | (* | 
| 24349 | 505 | term "[(x,y). x\<leftarrow>xs, let xx = x+x, y\<leftarrow>ys, y \<noteq> xx]" | 
| 23192 | 506 | *) | 
| 507 | ||
| 42167 | 508 | |
| 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: 
50134diff
changeset | 509 | 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: 
50134diff
changeset | 510 | (* 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: 
50134diff
changeset | 511 | 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: 
50134diff
changeset | 512 | |
| 
ee729dbd1b7f
avoid ML_file in large theory files to improve performance of dependency discovery of main HOL (approx. 1s CPU time) -- relevant for any application using it, e.g. small paper sessions;
 wenzelm parents: 
50134diff
changeset | 513 | 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: 
50134diff
changeset | 514 | sig | 
| 51717 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51678diff
changeset | 515 | 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: 
50134diff
changeset | 516 | 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: 
50134diff
changeset | 517 | |
| 
ee729dbd1b7f
avoid ML_file in large theory files to improve performance of dependency discovery of main HOL (approx. 1s CPU time) -- relevant for any application using it, e.g. small paper sessions;
 wenzelm parents: 
50134diff
changeset | 518 | 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: 
50134diff
changeset | 519 | 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: 
50134diff
changeset | 520 | |
| 
ee729dbd1b7f
avoid ML_file in large theory files to improve performance of dependency discovery of main HOL (approx. 1s CPU time) -- relevant for any application using it, e.g. small paper sessions;
 wenzelm parents: 
50134diff
changeset | 521 | (* 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: 
50134diff
changeset | 522 | |
| 
ee729dbd1b7f
avoid ML_file in large theory files to improve performance of dependency discovery of main HOL (approx. 1s CPU time) -- relevant for any application using it, e.g. small paper sessions;
 wenzelm parents: 
50134diff
changeset | 523 | 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: 
50134diff
changeset | 524 | (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: 
50134diff
changeset | 525 |     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: 
50134diff
changeset | 526 | 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: 
50134diff
changeset | 527 | | _ => 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: 
50134diff
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: 
50134diff
changeset | 529 | 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: 
50134diff
changeset | 530 | (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: 
50134diff
changeset | 531 |     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: 
50134diff
changeset | 532 | 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: 
50134diff
changeset | 533 | | _ => 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: 
50134diff
changeset | 534 | |
| 
ee729dbd1b7f
avoid ML_file in large theory files to improve performance of dependency discovery of main HOL (approx. 1s CPU time) -- relevant for any application using it, e.g. small paper sessions;
 wenzelm parents: 
50134diff
changeset | 535 | 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: 
50134diff
changeset | 536 | (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: 
50134diff
changeset | 537 |     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: 
50134diff
changeset | 538 |   | _ => 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: 
50134diff
changeset | 539 | |
| 
ee729dbd1b7f
avoid ML_file in large theory files to improve performance of dependency discovery of main HOL (approx. 1s CPU time) -- relevant for any application using it, e.g. small paper sessions;
 wenzelm parents: 
50134diff
changeset | 540 | 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: 
50134diff
changeset | 541 | |
| 
ee729dbd1b7f
avoid ML_file in large theory files to improve performance of dependency discovery of main HOL (approx. 1s CPU time) -- relevant for any application using it, e.g. small paper sessions;
 wenzelm parents: 
50134diff
changeset | 542 | 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: 
50134diff
changeset | 543 | Conv.try_conv | 
| 51315 
536a5603a138
provide common HOLogic.conj_conv and HOLogic.eq_conv;
 wenzelm parents: 
51314diff
changeset | 544 |     (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: 
50134diff
changeset | 545 | |
| 
ee729dbd1b7f
avoid ML_file in large theory files to improve performance of dependency discovery of main HOL (approx. 1s CPU time) -- relevant for any application using it, e.g. small paper sessions;
 wenzelm parents: 
50134diff
changeset | 546 | fun right_hand_set_comprehension_conv conv ctxt = | 
| 51315 
536a5603a138
provide common HOLogic.conj_conv and HOLogic.eq_conv;
 wenzelm parents: 
51314diff
changeset | 547 | 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: 
50134diff
changeset | 548 | (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: 
50134diff
changeset | 549 | |
| 
ee729dbd1b7f
avoid ML_file in large theory files to improve performance of dependency discovery of main HOL (approx. 1s CPU time) -- relevant for any application using it, e.g. small paper sessions;
 wenzelm parents: 
50134diff
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: 
50134diff
changeset | 551 | (* 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: 
50134diff
changeset | 552 | |
| 
ee729dbd1b7f
avoid ML_file in large theory files to improve performance of dependency discovery of main HOL (approx. 1s CPU time) -- relevant for any application using it, e.g. small paper sessions;
 wenzelm parents: 
50134diff
changeset | 553 | 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: 
50134diff
changeset | 554 | |
| 51717 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51678diff
changeset | 555 | 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: 
50134diff
changeset | 556 | let | 
| 55584 | 557 |     val set_Nil_I = @{thm trans} OF [@{thm set_simps(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: 
50134diff
changeset | 558 |     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: 
50134diff
changeset | 559 |     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: 
50134diff
changeset | 560 |     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: 
50134diff
changeset | 561 |     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: 
50134diff
changeset | 562 |     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: 
50134diff
changeset | 563 |     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: 
50134diff
changeset | 564 |           $ 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: 
50134diff
changeset | 565 |       | 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: 
50134diff
changeset | 566 | (* 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: 
50134diff
changeset | 567 | 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: 
50134diff
changeset | 568 | 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: 
50134diff
changeset | 569 | 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: 
50134diff
changeset | 570 | 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: 
50134diff
changeset | 571 | (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: 
50134diff
changeset | 572 |             (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: 
53374diff
changeset | 573 | | _ => (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: 
50134diff
changeset | 574 | in | 
| 53412 
01b804df0a30
list_to_set_comprehension: don't crash in case distinctions on datatypes with even number of constructors
 traytel parents: 
53374diff
changeset | 575 | 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: 
50134diff
changeset | 576 | end | 
| 54404 
9f0f1152c875
port list comprehension simproc to 'Ctr_Sugar' abstraction
 blanchet parents: 
54295diff
changeset | 577 | (* 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: 
50134diff
changeset | 578 | 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: 
50134diff
changeset | 579 | 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: 
50134diff
changeset | 580 | 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: 
50134diff
changeset | 581 | 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: 
50134diff
changeset | 582 | (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: 
50134diff
changeset | 583 | SOME (c, T) => | 
| 54404 
9f0f1152c875
port list comprehension simproc to 'Ctr_Sugar' abstraction
 blanchet parents: 
54295diff
changeset | 584 | (case Ctr_Sugar.ctr_sugar_of_case ctxt c of | 
| 
9f0f1152c875
port list comprehension simproc to 'Ctr_Sugar' abstraction
 blanchet parents: 
54295diff
changeset | 585 |               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: 
50134diff
changeset | 586 | (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: 
50134diff
changeset | 587 | 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: 
50134diff
changeset | 588 | let | 
| 54404 
9f0f1152c875
port list comprehension simproc to 'Ctr_Sugar' abstraction
 blanchet parents: 
54295diff
changeset | 589 | 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: 
50134diff
changeset | 590 | 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: 
50134diff
changeset | 591 | val T' = List.last Ts | 
| 54404 
9f0f1152c875
port list comprehension simproc to 'Ctr_Sugar' abstraction
 blanchet parents: 
54295diff
changeset | 592 | 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: 
50134diff
changeset | 593 | | 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: 
50134diff
changeset | 594 | | 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: 
50134diff
changeset | 595 | | 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: 
50134diff
changeset | 596 | 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: 
50134diff
changeset | 597 | (* 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: 
50134diff
changeset | 598 |     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: 
50134diff
changeset | 599 | 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: 
50134diff
changeset | 600 | | 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: 
50134diff
changeset | 601 | 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: 
50134diff
changeset | 602 | | 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: 
50134diff
changeset | 603 |           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: 
50134diff
changeset | 604 |           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: 
50134diff
changeset | 605 |           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: 
50134diff
changeset | 606 |           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: 
50134diff
changeset | 607 | CONVERSION (right_hand_set_comprehension_conv (K | 
| 51315 
536a5603a138
provide common HOLogic.conj_conv and HOLogic.eq_conv;
 wenzelm parents: 
51314diff
changeset | 608 |               (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: 
50134diff
changeset | 609 | 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: 
50134diff
changeset | 610 |                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: 
50134diff
changeset | 611 | 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: 
50134diff
changeset | 612 |           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: 
50134diff
changeset | 613 |           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: 
50134diff
changeset | 614 | CONVERSION (right_hand_set_comprehension_conv (K | 
| 51315 
536a5603a138
provide common HOLogic.conj_conv and HOLogic.eq_conv;
 wenzelm parents: 
51314diff
changeset | 615 |                 (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: 
50134diff
changeset | 616 |                  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: 
50134diff
changeset | 617 | 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: 
50134diff
changeset | 618 | | 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: 
50134diff
changeset | 619 | let | 
| 54404 
9f0f1152c875
port list comprehension simproc to 'Ctr_Sugar' abstraction
 blanchet parents: 
54295diff
changeset | 620 |             val SOME {injects, distincts, case_thms, split, ...} =
 | 
| 
9f0f1152c875
port list comprehension simproc to 'Ctr_Sugar' abstraction
 blanchet parents: 
54295diff
changeset | 621 | 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: 
50134diff
changeset | 622 | 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: 
50134diff
changeset | 623 | (* do case distinction *) | 
| 54404 
9f0f1152c875
port list comprehension simproc to 'Ctr_Sugar' abstraction
 blanchet parents: 
54295diff
changeset | 624 | 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: 
50134diff
changeset | 625 | THEN EVERY (map_index (fn (i', _) => | 
| 54404 
9f0f1152c875
port list comprehension simproc to 'Ctr_Sugar' abstraction
 blanchet parents: 
54295diff
changeset | 626 |               (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: 
50134diff
changeset | 627 |               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: 
50134diff
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: 
50134diff
changeset | 629 | 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: 
50134diff
changeset | 630 | (* 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: 
50134diff
changeset | 631 |                 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: 
50134diff
changeset | 632 | 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: 
51314diff
changeset | 633 | ((HOLogic.conj_conv | 
| 
536a5603a138
provide common HOLogic.conj_conv and HOLogic.eq_conv;
 wenzelm parents: 
51314diff
changeset | 634 | (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: 
54295diff
changeset | 635 | (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: 
50134diff
changeset | 636 | 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: 
50134diff
changeset | 637 | 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: 
50134diff
changeset | 638 | then_conv conjunct_assoc_conv)) context | 
| 51315 
536a5603a138
provide common HOLogic.conj_conv and HOLogic.eq_conv;
 wenzelm parents: 
51314diff
changeset | 639 | 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: 
50134diff
changeset | 640 | 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: 
50134diff
changeset | 641 | (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: 
50134diff
changeset | 642 | (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: 
50134diff
changeset | 643 |                             @{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: 
50134diff
changeset | 644 | 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: 
50134diff
changeset | 645 | 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: 
50134diff
changeset | 646 |                 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: 
50134diff
changeset | 647 | 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: 
50134diff
changeset | 648 | (right_hand_set_comprehension_conv (K | 
| 51315 
536a5603a138
provide common HOLogic.conj_conv and HOLogic.eq_conv;
 wenzelm parents: 
51314diff
changeset | 649 | (HOLogic.conj_conv | 
| 
536a5603a138
provide common HOLogic.conj_conv and HOLogic.eq_conv;
 wenzelm parents: 
51314diff
changeset | 650 | ((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: 
50134diff
changeset | 651 | (rewr_conv' (List.last prems))) then_conv | 
| 54404 
9f0f1152c875
port list comprehension simproc to 'Ctr_Sugar' abstraction
 blanchet parents: 
54295diff
changeset | 652 |                           (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: 
50134diff
changeset | 653 | 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: 
50134diff
changeset | 654 |                         (rewr_conv' @{lemma "(False & P) = False" by simp}))) context then_conv
 | 
| 51314 
eac4bb5adbf9
just one HOLogic.Trueprop_conv, with regular exception CTERM;
 wenzelm parents: 
51272diff
changeset | 655 | HOLogic.Trueprop_conv | 
| 51315 
536a5603a138
provide common HOLogic.conj_conv and HOLogic.eq_conv;
 wenzelm parents: 
51314diff
changeset | 656 | (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: 
50134diff
changeset | 657 | (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: 
50134diff
changeset | 658 | 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: 
50134diff
changeset | 659 | (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: 
50134diff
changeset | 660 | (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: 
50134diff
changeset | 661 |                                   @{lemma "(EX x. P) = P" by simp})) ctxt)) context))) 1) ctxt 1
 | 
| 54404 
9f0f1152c875
port list comprehension simproc to 'Ctr_Sugar' abstraction
 blanchet parents: 
54295diff
changeset | 662 | 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: 
50134diff
changeset | 663 | 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: 
50134diff
changeset | 664 | 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: 
50134diff
changeset | 665 | (case dest_case t of | 
| 54404 
9f0f1152c875
port list comprehension simproc to 'Ctr_Sugar' abstraction
 blanchet parents: 
54295diff
changeset | 666 | 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: 
50134diff
changeset | 667 | let | 
| 52131 | 668 | 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: 
50134diff
changeset | 669 | 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: 
50134diff
changeset | 670 | 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: 
50134diff
changeset | 671 | 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: 
50134diff
changeset | 672 | 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: 
50134diff
changeset | 673 | (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: 
50134diff
changeset | 674 |             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: 
50134diff
changeset | 675 | 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: 
50134diff
changeset | 676 | 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: 
50134diff
changeset | 677 | 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: 
50134diff
changeset | 678 | | 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: 
50134diff
changeset | 679 | (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: 
50134diff
changeset | 680 | 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: 
50134diff
changeset | 681 | | 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: 
50134diff
changeset | 682 | 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: 
50134diff
changeset | 683 | 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: 
50134diff
changeset | 684 | 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: 
50134diff
changeset | 685 |                 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: 
50134diff
changeset | 686 | 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: 
50134diff
changeset | 687 | (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: 
50134diff
changeset | 688 | 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: 
50134diff
changeset | 689 |                       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: 
50134diff
changeset | 690 | 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: 
50134diff
changeset | 691 | | 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: 
50134diff
changeset | 692 |                       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: 
50134diff
changeset | 693 | 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: 
50134diff
changeset | 694 | 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: 
50134diff
changeset | 695 | ((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: 
50134diff
changeset | 696 | 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: 
50134diff
changeset | 697 | 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: 
50134diff
changeset | 698 | 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: 
50134diff
changeset | 699 | 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: 
50134diff
changeset | 700 | (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: 
50134diff
changeset | 701 | 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: 
50134diff
changeset | 702 |                 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: 
50134diff
changeset | 703 | 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: 
50134diff
changeset | 704 | 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: 
50134diff
changeset | 705 | 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: 
50134diff
changeset | 706 | ((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: 
50134diff
changeset | 707 |                     (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: 
50134diff
changeset | 708 | 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: 
50134diff
changeset | 709 | 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: 
50134diff
changeset | 710 | 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: 
50134diff
changeset | 711 | 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: 
50134diff
changeset | 712 | |
| 
ee729dbd1b7f
avoid ML_file in large theory files to improve performance of dependency discovery of main HOL (approx. 1s CPU time) -- relevant for any application using it, e.g. small paper sessions;
 wenzelm parents: 
50134diff
changeset | 713 | 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: 
50134diff
changeset | 714 | *} | 
| 41463 
edbf0a86fb1c
adding simproc to rewrite list comprehensions to set comprehensions; adopting proofs
 bulwahn parents: 
41372diff
changeset | 715 | |
| 
edbf0a86fb1c
adding simproc to rewrite list comprehensions to set comprehensions; adopting proofs
 bulwahn parents: 
41372diff
changeset | 716 | 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: 
41372diff
changeset | 717 | |
| 46133 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 haftmann parents: 
46125diff
changeset | 718 | 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: 
46125diff
changeset | 719 | |
| 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 haftmann parents: 
46125diff
changeset | 720 | hide_const (open) coset | 
| 35115 | 721 | |
| 49948 
744934b818c7
moved quite generic material from theory Enum to more appropriate places
 haftmann parents: 
49808diff
changeset | 722 | |
| 21061 
580dfc999ef6
added normal post setup; cleaned up "execution" constants
 haftmann parents: 
21046diff
changeset | 723 | subsubsection {* @{const Nil} and @{const Cons} *}
 | 
| 
580dfc999ef6
added normal post setup; cleaned up "execution" constants
 haftmann parents: 
21046diff
changeset | 724 | |
| 
580dfc999ef6
added normal post setup; cleaned up "execution" constants
 haftmann parents: 
21046diff
changeset | 725 | lemma not_Cons_self [simp]: | 
| 
580dfc999ef6
added normal post setup; cleaned up "execution" constants
 haftmann parents: 
21046diff
changeset | 726 | "xs \<noteq> x # xs" | 
| 13145 | 727 | by (induct xs) auto | 
| 13114 | 728 | |
| 41697 | 729 | lemma not_Cons_self2 [simp]: | 
| 730 | "x # xs \<noteq> xs" | |
| 731 | by (rule not_Cons_self [symmetric]) | |
| 13114 | 732 | |
| 13142 | 733 | lemma neq_Nil_conv: "(xs \<noteq> []) = (\<exists>y ys. xs = y # ys)" | 
| 13145 | 734 | by (induct xs) auto | 
| 13114 | 735 | |
| 53689 | 736 | lemma tl_Nil: "tl xs = [] \<longleftrightarrow> xs = [] \<or> (EX x. xs = [x])" | 
| 737 | by (cases xs) auto | |
| 738 | ||
| 739 | lemma Nil_tl: "[] = tl xs \<longleftrightarrow> xs = [] \<or> (EX x. xs = [x])" | |
| 740 | by (cases xs) auto | |
| 741 | ||
| 13142 | 742 | lemma length_induct: | 
| 21061 
580dfc999ef6
added normal post setup; cleaned up "execution" constants
 haftmann parents: 
21046diff
changeset | 743 | "(\<And>xs. \<forall>ys. length ys < length xs \<longrightarrow> P ys \<Longrightarrow> P xs) \<Longrightarrow> P xs" | 
| 53689 | 744 | by (fact measure_induct) | 
| 13114 | 745 | |
| 37289 | 746 | lemma list_nonempty_induct [consumes 1, case_names single cons]: | 
| 747 | assumes "xs \<noteq> []" | |
| 748 | assumes single: "\<And>x. P [x]" | |
| 749 | assumes cons: "\<And>x xs. xs \<noteq> [] \<Longrightarrow> P xs \<Longrightarrow> P (x # xs)" | |
| 750 | shows "P xs" | |
| 751 | using `xs \<noteq> []` proof (induct xs) | |
| 752 | case Nil then show ?case by simp | |
| 753 | next | |
| 53374 
a14d2a854c02
tuned proofs -- clarified flow of facts wrt. calculation;
 wenzelm parents: 
53017diff
changeset | 754 | case (Cons x xs) | 
| 
a14d2a854c02
tuned proofs -- clarified flow of facts wrt. calculation;
 wenzelm parents: 
53017diff
changeset | 755 | show ?case | 
| 
a14d2a854c02
tuned proofs -- clarified flow of facts wrt. calculation;
 wenzelm parents: 
53017diff
changeset | 756 | proof (cases xs) | 
| 
a14d2a854c02
tuned proofs -- clarified flow of facts wrt. calculation;
 wenzelm parents: 
53017diff
changeset | 757 | case Nil | 
| 
a14d2a854c02
tuned proofs -- clarified flow of facts wrt. calculation;
 wenzelm parents: 
53017diff
changeset | 758 | with single show ?thesis by simp | 
| 37289 | 759 | next | 
| 53374 
a14d2a854c02
tuned proofs -- clarified flow of facts wrt. calculation;
 wenzelm parents: 
53017diff
changeset | 760 | case Cons | 
| 
a14d2a854c02
tuned proofs -- clarified flow of facts wrt. calculation;
 wenzelm parents: 
53017diff
changeset | 761 | show ?thesis | 
| 
a14d2a854c02
tuned proofs -- clarified flow of facts wrt. calculation;
 wenzelm parents: 
53017diff
changeset | 762 | proof (rule cons) | 
| 
a14d2a854c02
tuned proofs -- clarified flow of facts wrt. calculation;
 wenzelm parents: 
53017diff
changeset | 763 | from Cons show "xs \<noteq> []" by simp | 
| 
a14d2a854c02
tuned proofs -- clarified flow of facts wrt. calculation;
 wenzelm parents: 
53017diff
changeset | 764 | with Cons.hyps show "P xs" . | 
| 
a14d2a854c02
tuned proofs -- clarified flow of facts wrt. calculation;
 wenzelm parents: 
53017diff
changeset | 765 | qed | 
| 37289 | 766 | qed | 
| 767 | qed | |
| 768 | ||
| 45714 | 769 | lemma inj_split_Cons: "inj_on (\<lambda>(xs, n). n#xs) X" | 
| 770 | by (auto intro!: inj_onI) | |
| 13114 | 771 | |
| 49948 
744934b818c7
moved quite generic material from theory Enum to more appropriate places
 haftmann parents: 
49808diff
changeset | 772 | |
| 21061 
580dfc999ef6
added normal post setup; cleaned up "execution" constants
 haftmann parents: 
21046diff
changeset | 773 | subsubsection {* @{const length} *}
 | 
| 13114 | 774 | |
| 13142 | 775 | text {*
 | 
| 21061 
580dfc999ef6
added normal post setup; cleaned up "execution" constants
 haftmann parents: 
21046diff
changeset | 776 |   Needs to come before @{text "@"} because of theorem @{text
 | 
| 
580dfc999ef6
added normal post setup; cleaned up "execution" constants
 haftmann parents: 
21046diff
changeset | 777 | append_eq_append_conv}. | 
| 13142 | 778 | *} | 
| 13114 | 779 | |
| 13142 | 780 | lemma length_append [simp]: "length (xs @ ys) = length xs + length ys" | 
| 13145 | 781 | by (induct xs) auto | 
| 13114 | 782 | |
| 13142 | 783 | lemma length_map [simp]: "length (map f xs) = length xs" | 
| 13145 | 784 | by (induct xs) auto | 
| 13114 | 785 | |
| 13142 | 786 | lemma length_rev [simp]: "length (rev xs) = length xs" | 
| 13145 | 787 | by (induct xs) auto | 
| 13114 | 788 | |
| 13142 | 789 | lemma length_tl [simp]: "length (tl xs) = length xs - 1" | 
| 13145 | 790 | by (cases xs) auto | 
| 13114 | 791 | |
| 13142 | 792 | lemma length_0_conv [iff]: "(length xs = 0) = (xs = [])" | 
| 13145 | 793 | by (induct xs) auto | 
| 13114 | 794 | |
| 13142 | 795 | lemma length_greater_0_conv [iff]: "(0 < length xs) = (xs \<noteq> [])" | 
| 13145 | 796 | by (induct xs) auto | 
| 13114 | 797 | |
| 23479 | 798 | lemma length_pos_if_in_set: "x : set xs \<Longrightarrow> length xs > 0" | 
| 799 | by auto | |
| 800 | ||
| 13114 | 801 | lemma length_Suc_conv: | 
| 13145 | 802 | "(length xs = Suc n) = (\<exists>y ys. xs = y # ys \<and> length ys = n)" | 
| 803 | by (induct xs) auto | |
| 13142 | 804 | |
| 14025 | 805 | lemma Suc_length_conv: | 
| 806 | "(Suc n = length xs) = (\<exists>y ys. xs = y # ys \<and> length ys = n)" | |
| 14208 | 807 | apply (induct xs, simp, simp) | 
| 14025 | 808 | apply blast | 
| 809 | done | |
| 810 | ||
| 25221 
5ded95dda5df
append/member: more light-weight way to declare authentic syntax;
 wenzelm parents: 
25215diff
changeset | 811 | lemma impossible_Cons: "length xs <= length ys ==> xs = x # ys = False" | 
| 
5ded95dda5df
append/member: more light-weight way to declare authentic syntax;
 wenzelm parents: 
25215diff
changeset | 812 | by (induct xs) auto | 
| 
5ded95dda5df
append/member: more light-weight way to declare authentic syntax;
 wenzelm parents: 
25215diff
changeset | 813 | |
| 26442 
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
 haftmann parents: 
26300diff
changeset | 814 | lemma list_induct2 [consumes 1, case_names Nil Cons]: | 
| 
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
 haftmann parents: 
26300diff
changeset | 815 | "length xs = length ys \<Longrightarrow> P [] [] \<Longrightarrow> | 
| 
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
 haftmann parents: 
26300diff
changeset | 816 | (\<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: 
26300diff
changeset | 817 | \<Longrightarrow> P xs ys" | 
| 
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
 haftmann parents: 
26300diff
changeset | 818 | proof (induct xs arbitrary: ys) | 
| 
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
 haftmann parents: 
26300diff
changeset | 819 | case Nil then show ?case by simp | 
| 
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
 haftmann parents: 
26300diff
changeset | 820 | next | 
| 
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
 haftmann parents: 
26300diff
changeset | 821 | case (Cons x xs ys) then show ?case by (cases ys) simp_all | 
| 
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
 haftmann parents: 
26300diff
changeset | 822 | qed | 
| 
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
 haftmann parents: 
26300diff
changeset | 823 | |
| 
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
 haftmann parents: 
26300diff
changeset | 824 | lemma list_induct3 [consumes 2, case_names Nil Cons]: | 
| 
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
 haftmann parents: 
26300diff
changeset | 825 | "length xs = length ys \<Longrightarrow> length ys = length zs \<Longrightarrow> P [] [] [] \<Longrightarrow> | 
| 
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
 haftmann parents: 
26300diff
changeset | 826 | (\<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: 
26300diff
changeset | 827 | \<Longrightarrow> P xs ys zs" | 
| 
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
 haftmann parents: 
26300diff
changeset | 828 | proof (induct xs arbitrary: ys zs) | 
| 
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
 haftmann parents: 
26300diff
changeset | 829 | case Nil then show ?case by simp | 
| 
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
 haftmann parents: 
26300diff
changeset | 830 | next | 
| 
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
 haftmann parents: 
26300diff
changeset | 831 | 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: 
26300diff
changeset | 832 | (cases zs, simp_all) | 
| 
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
 haftmann parents: 
26300diff
changeset | 833 | qed | 
| 13114 | 834 | |
| 36154 
11c6106d7787
Respectfullness and preservation of list_rel
 Cezary Kaliszyk <kaliszyk@in.tum.de> parents: 
35828diff
changeset | 835 | lemma list_induct4 [consumes 3, case_names Nil Cons]: | 
| 
11c6106d7787
Respectfullness and preservation of list_rel
 Cezary Kaliszyk <kaliszyk@in.tum.de> parents: 
35828diff
changeset | 836 | "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: 
35828diff
changeset | 837 | 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: 
35828diff
changeset | 838 | 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: 
35828diff
changeset | 839 | 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: 
35828diff
changeset | 840 | proof (induct xs arbitrary: ys zs ws) | 
| 
11c6106d7787
Respectfullness and preservation of list_rel
 Cezary Kaliszyk <kaliszyk@in.tum.de> parents: 
35828diff
changeset | 841 | case Nil then show ?case by simp | 
| 
11c6106d7787
Respectfullness and preservation of list_rel
 Cezary Kaliszyk <kaliszyk@in.tum.de> parents: 
35828diff
changeset | 842 | next | 
| 
11c6106d7787
Respectfullness and preservation of list_rel
 Cezary Kaliszyk <kaliszyk@in.tum.de> parents: 
35828diff
changeset | 843 | 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: 
35828diff
changeset | 844 | qed | 
| 
11c6106d7787
Respectfullness and preservation of list_rel
 Cezary Kaliszyk <kaliszyk@in.tum.de> parents: 
35828diff
changeset | 845 | |
| 22493 
db930e490fe5
added another rule for simultaneous induction, and lemmas for zip
 krauss parents: 
22422diff
changeset | 846 | lemma list_induct2': | 
| 
db930e490fe5
added another rule for simultaneous induction, and lemmas for zip
 krauss parents: 
22422diff
changeset | 847 | "\<lbrakk> P [] []; | 
| 
db930e490fe5
added another rule for simultaneous induction, and lemmas for zip
 krauss parents: 
22422diff
changeset | 848 | \<And>x xs. P (x#xs) []; | 
| 
db930e490fe5
added another rule for simultaneous induction, and lemmas for zip
 krauss parents: 
22422diff
changeset | 849 | \<And>y ys. P [] (y#ys); | 
| 
db930e490fe5
added another rule for simultaneous induction, and lemmas for zip
 krauss parents: 
22422diff
changeset | 850 | \<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: 
22422diff
changeset | 851 | \<Longrightarrow> P xs ys" | 
| 
db930e490fe5
added another rule for simultaneous induction, and lemmas for zip
 krauss parents: 
22422diff
changeset | 852 | by (induct xs arbitrary: ys) (case_tac x, auto)+ | 
| 
db930e490fe5
added another rule for simultaneous induction, and lemmas for zip
 krauss parents: 
22422diff
changeset | 853 | |
| 55524 
f41ef840f09d
folded 'list_all2' with the relator generated by 'datatype_new'
 blanchet parents: 
55473diff
changeset | 854 | lemma list_all2_iff: | 
| 
f41ef840f09d
folded 'list_all2' with the relator generated by 'datatype_new'
 blanchet parents: 
55473diff
changeset | 855 | "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: 
55473diff
changeset | 856 | by (induct xs ys rule: list_induct2') auto | 
| 
f41ef840f09d
folded 'list_all2' with the relator generated by 'datatype_new'
 blanchet parents: 
55473diff
changeset | 857 | |
| 22143 
cf58486ca11b
Added simproc list_neq (prompted by an application)
 nipkow parents: 
21911diff
changeset | 858 | lemma neq_if_length_neq: "length xs \<noteq> length ys \<Longrightarrow> (xs = ys) == False" | 
| 24349 | 859 | by (rule Eq_FalseI) auto | 
| 24037 | 860 | |
| 861 | simproc_setup list_neq ("(xs::'a list) = ys") = {*
 | |
| 22143 
cf58486ca11b
Added simproc list_neq (prompted by an application)
 nipkow parents: 
21911diff
changeset | 862 | (* | 
| 
cf58486ca11b
Added simproc list_neq (prompted by an application)
 nipkow parents: 
21911diff
changeset | 863 | 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: 
21911diff
changeset | 864 | This is the case if the atomic sublists of one are a submultiset | 
| 
cf58486ca11b
Added simproc list_neq (prompted by an application)
 nipkow parents: 
21911diff
changeset | 865 | 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: 
21911diff
changeset | 866 | *) | 
| 24037 | 867 | |
| 868 | let | |
| 22143 
cf58486ca11b
Added simproc list_neq (prompted by an application)
 nipkow parents: 
21911diff
changeset | 869 | |
| 29856 | 870 | fun len (Const(@{const_name Nil},_)) acc = acc
 | 
| 871 |   | len (Const(@{const_name Cons},_) $ _ $ xs) (ts,n) = len xs (ts,n+1)
 | |
| 872 |   | len (Const(@{const_name append},_) $ xs $ ys) acc = len xs (len ys acc)
 | |
| 873 |   | len (Const(@{const_name rev},_) $ xs) acc = len xs acc
 | |
| 874 |   | len (Const(@{const_name map},_) $ _ $ xs) acc = len xs acc
 | |
| 22143 
cf58486ca11b
Added simproc list_neq (prompted by an application)
 nipkow parents: 
21911diff
changeset | 875 | | len t (ts,n) = (t::ts,n); | 
| 
cf58486ca11b
Added simproc list_neq (prompted by an application)
 nipkow parents: 
21911diff
changeset | 876 | |
| 51717 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51678diff
changeset | 877 | val ss = simpset_of @{context};
 | 
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51678diff
changeset | 878 | |
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51678diff
changeset | 879 | fun list_neq ctxt ct = | 
| 22143 
cf58486ca11b
Added simproc list_neq (prompted by an application)
 nipkow parents: 
21911diff
changeset | 880 | let | 
| 24037 | 881 | val (Const(_,eqT) $ lhs $ rhs) = Thm.term_of ct; | 
| 22143 
cf58486ca11b
Added simproc list_neq (prompted by an application)
 nipkow parents: 
21911diff
changeset | 882 | val (ls,m) = len lhs ([],0) and (rs,n) = len rhs ([],0); | 
| 
cf58486ca11b
Added simproc list_neq (prompted by an application)
 nipkow parents: 
21911diff
changeset | 883 | fun prove_neq() = | 
| 
cf58486ca11b
Added simproc list_neq (prompted by an application)
 nipkow parents: 
21911diff
changeset | 884 | let | 
| 
cf58486ca11b
Added simproc list_neq (prompted by an application)
 nipkow parents: 
21911diff
changeset | 885 | val Type(_,listT::_) = eqT; | 
| 22994 | 886 | val size = HOLogic.size_const listT; | 
| 22143 
cf58486ca11b
Added simproc list_neq (prompted by an application)
 nipkow parents: 
21911diff
changeset | 887 | val eq_len = HOLogic.mk_eq (size $ lhs, size $ rhs); | 
| 
cf58486ca11b
Added simproc list_neq (prompted by an application)
 nipkow parents: 
21911diff
changeset | 888 | val neq_len = HOLogic.mk_Trueprop (HOLogic.Not $ eq_len); | 
| 51717 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51678diff
changeset | 889 | val thm = Goal.prove ctxt [] [] neq_len | 
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51678diff
changeset | 890 | (K (simp_tac (put_simpset ss ctxt) 1)); | 
| 22633 | 891 |       in SOME (thm RS @{thm neq_if_length_neq}) end
 | 
| 22143 
cf58486ca11b
Added simproc list_neq (prompted by an application)
 nipkow parents: 
21911diff
changeset | 892 | in | 
| 23214 | 893 | if m < n andalso submultiset (op aconv) (ls,rs) orelse | 
| 894 | n < m andalso submultiset (op aconv) (rs,ls) | |
| 22143 
cf58486ca11b
Added simproc list_neq (prompted by an application)
 nipkow parents: 
21911diff
changeset | 895 | then prove_neq() else NONE | 
| 
cf58486ca11b
Added simproc list_neq (prompted by an application)
 nipkow parents: 
21911diff
changeset | 896 | end; | 
| 51717 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51678diff
changeset | 897 | in K list_neq end; | 
| 22143 
cf58486ca11b
Added simproc list_neq (prompted by an application)
 nipkow parents: 
21911diff
changeset | 898 | *} | 
| 
cf58486ca11b
Added simproc list_neq (prompted by an application)
 nipkow parents: 
21911diff
changeset | 899 | |
| 
cf58486ca11b
Added simproc list_neq (prompted by an application)
 nipkow parents: 
21911diff
changeset | 900 | |
| 15392 | 901 | subsubsection {* @{text "@"} -- append *}
 | 
| 13114 | 902 | |
| 13142 | 903 | lemma append_assoc [simp]: "(xs @ ys) @ zs = xs @ (ys @ zs)" | 
| 13145 | 904 | by (induct xs) auto | 
| 13114 | 905 | |
| 13142 | 906 | lemma append_Nil2 [simp]: "xs @ [] = xs" | 
| 13145 | 907 | by (induct xs) auto | 
| 3507 | 908 | |
| 13142 | 909 | lemma append_is_Nil_conv [iff]: "(xs @ ys = []) = (xs = [] \<and> ys = [])" | 
| 13145 | 910 | by (induct xs) auto | 
| 13114 | 911 | |
| 13142 | 912 | lemma Nil_is_append_conv [iff]: "([] = xs @ ys) = (xs = [] \<and> ys = [])" | 
| 13145 | 913 | by (induct xs) auto | 
| 13114 | 914 | |
| 13142 | 915 | lemma append_self_conv [iff]: "(xs @ ys = xs) = (ys = [])" | 
| 13145 | 916 | by (induct xs) auto | 
| 13114 | 917 | |
| 13142 | 918 | lemma self_append_conv [iff]: "(xs = xs @ ys) = (ys = [])" | 
| 13145 | 919 | by (induct xs) auto | 
| 13114 | 920 | |
| 54147 
97a8ff4e4ac9
killed most "no_atp", to make Sledgehammer more complete
 blanchet parents: 
53954diff
changeset | 921 | lemma append_eq_append_conv [simp]: | 
| 24526 | 922 | "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: 
13863diff
changeset | 923 | ==> (xs@us = ys@vs) = (xs=ys \<and> us=vs)" | 
| 24526 | 924 | apply (induct xs arbitrary: ys) | 
| 14208 | 925 | apply (case_tac ys, simp, force) | 
| 926 | apply (case_tac ys, force, simp) | |
| 13145 | 927 | done | 
| 13142 | 928 | |
| 24526 | 929 | lemma append_eq_append_conv2: "(xs @ ys = zs @ ts) = | 
| 930 | (EX us. xs = zs @ us & us @ ys = ts | xs @ us = zs & ys = us@ ts)" | |
| 931 | apply (induct xs arbitrary: ys zs ts) | |
| 44890 
22f665a2e91c
new fastforce replacing fastsimp - less confusing name
 nipkow parents: 
44635diff
changeset | 932 | apply fastforce | 
| 14495 | 933 | apply(case_tac zs) | 
| 934 | apply simp | |
| 44890 
22f665a2e91c
new fastforce replacing fastsimp - less confusing name
 nipkow parents: 
44635diff
changeset | 935 | apply fastforce | 
| 14495 | 936 | done | 
| 937 | ||
| 34910 
b23bd3ee4813
same_append_eq / append_same_eq are now used for simplifying induction rules.
 berghofe parents: 
34064diff
changeset | 938 | lemma same_append_eq [iff, induct_simp]: "(xs @ ys = xs @ zs) = (ys = zs)" | 
| 13145 | 939 | by simp | 
| 13142 | 940 | |
| 941 | lemma append1_eq_conv [iff]: "(xs @ [x] = ys @ [y]) = (xs = ys \<and> x = y)" | |
| 13145 | 942 | by simp | 
| 13114 | 943 | |
| 34910 
b23bd3ee4813
same_append_eq / append_same_eq are now used for simplifying induction rules.
 berghofe parents: 
34064diff
changeset | 944 | lemma append_same_eq [iff, induct_simp]: "(ys @ xs = zs @ xs) = (ys = zs)" | 
| 13145 | 945 | by simp | 
| 13114 | 946 | |
| 13142 | 947 | lemma append_self_conv2 [iff]: "(xs @ ys = ys) = (xs = [])" | 
| 13145 | 948 | using append_same_eq [of _ _ "[]"] by auto | 
| 3507 | 949 | |
| 13142 | 950 | lemma self_append_conv2 [iff]: "(ys = xs @ ys) = (xs = [])" | 
| 13145 | 951 | using append_same_eq [of "[]"] by auto | 
| 13114 | 952 | |
| 54147 
97a8ff4e4ac9
killed most "no_atp", to make Sledgehammer more complete
 blanchet parents: 
53954diff
changeset | 953 | lemma hd_Cons_tl [simp]: "xs \<noteq> [] ==> hd xs # tl xs = xs" | 
| 13145 | 954 | by (induct xs) auto | 
| 13114 | 955 | |
| 13142 | 956 | lemma hd_append: "hd (xs @ ys) = (if xs = [] then hd ys else hd xs)" | 
| 13145 | 957 | by (induct xs) auto | 
| 13114 | 958 | |
| 13142 | 959 | lemma hd_append2 [simp]: "xs \<noteq> [] ==> hd (xs @ ys) = hd xs" | 
| 13145 | 960 | by (simp add: hd_append split: list.split) | 
| 13114 | 961 | |
| 13142 | 962 | lemma tl_append: "tl (xs @ ys) = (case xs of [] => tl ys | z#zs => zs @ ys)" | 
| 13145 | 963 | by (simp split: list.split) | 
| 13114 | 964 | |
| 13142 | 965 | lemma tl_append2 [simp]: "xs \<noteq> [] ==> tl (xs @ ys) = tl xs @ ys" | 
| 13145 | 966 | by (simp add: tl_append split: list.split) | 
| 13114 | 967 | |
| 968 | ||
| 14300 | 969 | lemma Cons_eq_append_conv: "x#xs = ys@zs = | 
| 970 | (ys = [] & x#xs = zs | (EX ys'. x#ys' = ys & xs = ys'@zs))" | |
| 971 | by(cases ys) auto | |
| 972 | ||
| 15281 | 973 | lemma append_eq_Cons_conv: "(ys@zs = x#xs) = | 
| 974 | (ys = [] & zs = x#xs | (EX ys'. ys = x#ys' & ys'@zs = xs))" | |
| 975 | by(cases ys) auto | |
| 976 | ||
| 14300 | 977 | |
| 13142 | 978 | text {* Trivial rules for solving @{text "@"}-equations automatically. *}
 | 
| 13114 | 979 | |
| 980 | lemma eq_Nil_appendI: "xs = ys ==> xs = [] @ ys" | |
| 13145 | 981 | by simp | 
| 13114 | 982 | |
| 13142 | 983 | lemma Cons_eq_appendI: | 
| 13145 | 984 | "[| x # xs1 = ys; xs = xs1 @ zs |] ==> x # xs = ys @ zs" | 
| 985 | by (drule sym) simp | |
| 13114 | 986 | |
| 13142 | 987 | lemma append_eq_appendI: | 
| 13145 | 988 | "[| xs @ xs1 = zs; ys = xs1 @ us |] ==> xs @ ys = zs @ us" | 
| 989 | by (drule sym) simp | |
| 13114 | 990 | |
| 991 | ||
| 13142 | 992 | text {*
 | 
| 13145 | 993 | Simplification procedure for all list equalities. | 
| 994 | Currently only tries to rearrange @{text "@"} to see if
 | |
| 995 | - both lists end in a singleton list, | |
| 996 | - or both lists end in the same list. | |
| 13142 | 997 | *} | 
| 998 | ||
| 43594 | 999 | simproc_setup list_eq ("(xs::'a list) = ys")  = {*
 | 
| 13462 | 1000 | let | 
| 43594 | 1001 |     fun last (cons as Const (@{const_name Cons}, _) $ _ $ xs) =
 | 
| 1002 |           (case xs of Const (@{const_name Nil}, _) => cons | _ => last xs)
 | |
| 1003 |       | last (Const(@{const_name append},_) $ _ $ ys) = last ys
 | |
| 1004 | | last t = t; | |
| 1005 | ||
| 1006 |     fun list1 (Const(@{const_name Cons},_) $ _ $ Const(@{const_name Nil},_)) = true
 | |
| 1007 | | list1 _ = false; | |
| 1008 | ||
| 1009 |     fun butlast ((cons as Const(@{const_name Cons},_) $ x) $ xs) =
 | |
| 1010 |           (case xs of Const (@{const_name Nil}, _) => xs | _ => cons $ butlast xs)
 | |
| 1011 |       | butlast ((app as Const (@{const_name append}, _) $ xs) $ ys) = app $ butlast ys
 | |
| 1012 |       | butlast xs = Const(@{const_name Nil}, fastype_of xs);
 | |
| 1013 | ||
| 1014 | val rearr_ss = | |
| 51717 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51678diff
changeset | 1015 |       simpset_of (put_simpset HOL_basic_ss @{context}
 | 
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51678diff
changeset | 1016 |         addsimps [@{thm append_assoc}, @{thm append_Nil}, @{thm append_Cons}]);
 | 
| 43594 | 1017 | |
| 51717 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51678diff
changeset | 1018 | fun list_eq ctxt (F as (eq as Const(_,eqT)) $ lhs $ rhs) = | 
| 13462 | 1019 | let | 
| 43594 | 1020 | val lastl = last lhs and lastr = last rhs; | 
| 1021 | fun rearr conv = | |
| 1022 | let | |
| 1023 | val lhs1 = butlast lhs and rhs1 = butlast rhs; | |
| 1024 | val Type(_,listT::_) = eqT | |
| 1025 | val appT = [listT,listT] ---> listT | |
| 1026 |             val app = Const(@{const_name append},appT)
 | |
| 1027 | val F2 = eq $ (app$lhs1$lastl) $ (app$rhs1$lastr) | |
| 1028 | val eq = HOLogic.mk_Trueprop (HOLogic.mk_eq (F,F2)); | |
| 51717 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51678diff
changeset | 1029 | val thm = Goal.prove ctxt [] [] eq | 
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51678diff
changeset | 1030 | (K (simp_tac (put_simpset rearr_ss ctxt) 1)); | 
| 43594 | 1031 | in SOME ((conv RS (thm RS trans)) RS eq_reflection) end; | 
| 1032 | in | |
| 1033 |         if list1 lastl andalso list1 lastr then rearr @{thm append1_eq_conv}
 | |
| 1034 |         else if lastl aconv lastr then rearr @{thm append_same_eq}
 | |
| 1035 | else NONE | |
| 1036 | end; | |
| 51717 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51678diff
changeset | 1037 | in fn _ => fn ctxt => fn ct => list_eq ctxt (term_of ct) end; | 
| 13114 | 1038 | *} | 
| 1039 | ||
| 1040 | ||
| 49948 
744934b818c7
moved quite generic material from theory Enum to more appropriate places
 haftmann parents: 
49808diff
changeset | 1041 | subsubsection {* @{const map} *}
 | 
| 13114 | 1042 | |
| 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: 
40195diff
changeset | 1043 | 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: 
40195diff
changeset | 1044 | "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: 
40195diff
changeset | 1045 | 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: 
40195diff
changeset | 1046 | |
| 
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: 
40195diff
changeset | 1047 | 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: 
40195diff
changeset | 1048 | "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: 
40195diff
changeset | 1049 | 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: 
40195diff
changeset | 1050 | |
| 13142 | 1051 | lemma map_ext: "(!!x. x : set xs --> f x = g x) ==> map f xs = map g xs" | 
| 13145 | 1052 | by (induct xs) simp_all | 
| 13114 | 1053 | |
| 13142 | 1054 | lemma map_ident [simp]: "map (\<lambda>x. x) = (\<lambda>xs. xs)" | 
| 13145 | 1055 | by (rule ext, induct_tac xs) auto | 
| 13114 | 1056 | |
| 13142 | 1057 | lemma map_append [simp]: "map f (xs @ ys) = map f xs @ map f ys" | 
| 13145 | 1058 | by (induct xs) auto | 
| 13114 | 1059 | |
| 33639 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 1060 | 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: 
33593diff
changeset | 1061 | 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: 
33593diff
changeset | 1062 | |
| 35208 | 1063 | lemma map_comp_map[simp]: "((map f) o (map g)) = map(f o g)" | 
| 1064 | apply(rule ext) | |
| 1065 | apply(simp) | |
| 1066 | done | |
| 1067 | ||
| 13142 | 1068 | lemma rev_map: "rev (map f xs) = map f (rev xs)" | 
| 13145 | 1069 | by (induct xs) auto | 
| 13114 | 1070 | |
| 13737 | 1071 | lemma map_eq_conv[simp]: "(map f xs = map g xs) = (!x : set xs. f x = g x)" | 
| 1072 | by (induct xs) auto | |
| 1073 | ||
| 44013 
5cfc1c36ae97
moved recdef package to HOL/Library/Old_Recdef.thy
 krauss parents: 
43594diff
changeset | 1074 | lemma map_cong [fundef_cong]: | 
| 40122 
1d8ad2ff3e01
dropped (almost) redundant distinct.induct rule; distinct_simps again named distinct.simps
 haftmann parents: 
40077diff
changeset | 1075 | "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: 
40077diff
changeset | 1076 | by simp | 
| 13114 | 1077 | |
| 13142 | 1078 | lemma map_is_Nil_conv [iff]: "(map f xs = []) = (xs = [])" | 
| 13145 | 1079 | by (cases xs) auto | 
| 13114 | 1080 | |
| 13142 | 1081 | lemma Nil_is_map_conv [iff]: "([] = map f xs) = (xs = [])" | 
| 13145 | 1082 | by (cases xs) auto | 
| 13114 | 1083 | |
| 18447 | 1084 | lemma map_eq_Cons_conv: | 
| 14025 | 1085 | "(map f xs = y#ys) = (\<exists>z zs. xs = z#zs \<and> f z = y \<and> map f zs = ys)" | 
| 13145 | 1086 | by (cases xs) auto | 
| 13114 | 1087 | |
| 18447 | 1088 | lemma Cons_eq_map_conv: | 
| 14025 | 1089 | "(x#xs = map f ys) = (\<exists>z zs. ys = z#zs \<and> x = f z \<and> xs = map f zs)" | 
| 1090 | by (cases ys) auto | |
| 1091 | ||
| 18447 | 1092 | lemmas map_eq_Cons_D = map_eq_Cons_conv [THEN iffD1] | 
| 1093 | lemmas Cons_eq_map_D = Cons_eq_map_conv [THEN iffD1] | |
| 1094 | declare map_eq_Cons_D [dest!] Cons_eq_map_D [dest!] | |
| 1095 | ||
| 14111 | 1096 | lemma ex_map_conv: | 
| 1097 | "(EX xs. ys = map f xs) = (ALL y : set ys. EX x. y = f x)" | |
| 18447 | 1098 | by(induct ys, auto simp add: Cons_eq_map_conv) | 
| 14111 | 1099 | |
| 15110 
78b5636eabc7
Added a number of new thms and the new function remove1
 nipkow parents: 
15072diff
changeset | 1100 | lemma map_eq_imp_length_eq: | 
| 35510 | 1101 | assumes "map f xs = map g ys" | 
| 26734 | 1102 | shows "length xs = length ys" | 
| 53374 
a14d2a854c02
tuned proofs -- clarified flow of facts wrt. calculation;
 wenzelm parents: 
53017diff
changeset | 1103 | using assms | 
| 
a14d2a854c02
tuned proofs -- clarified flow of facts wrt. calculation;
 wenzelm parents: 
53017diff
changeset | 1104 | proof (induct ys arbitrary: xs) | 
| 26734 | 1105 | case Nil then show ?case by simp | 
| 1106 | next | |
| 1107 | case (Cons y ys) then obtain z zs where xs: "xs = z # zs" by auto | |
| 35510 | 1108 | from Cons xs have "map f zs = map g ys" by simp | 
| 53374 
a14d2a854c02
tuned proofs -- clarified flow of facts wrt. calculation;
 wenzelm parents: 
53017diff
changeset | 1109 | with Cons have "length zs = length ys" by blast | 
| 26734 | 1110 | with xs show ?case by simp | 
| 1111 | qed | |
| 1112 | ||
| 15110 
78b5636eabc7
Added a number of new thms and the new function remove1
 nipkow parents: 
15072diff
changeset | 1113 | lemma map_inj_on: | 
| 
78b5636eabc7
Added a number of new thms and the new function remove1
 nipkow parents: 
15072diff
changeset | 1114 | "[| 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: 
15072diff
changeset | 1115 | ==> xs = ys" | 
| 
78b5636eabc7
Added a number of new thms and the new function remove1
 nipkow parents: 
15072diff
changeset | 1116 | apply(frule map_eq_imp_length_eq) | 
| 
78b5636eabc7
Added a number of new thms and the new function remove1
 nipkow parents: 
15072diff
changeset | 1117 | apply(rotate_tac -1) | 
| 
78b5636eabc7
Added a number of new thms and the new function remove1
 nipkow parents: 
15072diff
changeset | 1118 | apply(induct rule:list_induct2) | 
| 
78b5636eabc7
Added a number of new thms and the new function remove1
 nipkow parents: 
15072diff
changeset | 1119 | apply simp | 
| 
78b5636eabc7
Added a number of new thms and the new function remove1
 nipkow parents: 
15072diff
changeset | 1120 | apply(simp) | 
| 
78b5636eabc7
Added a number of new thms and the new function remove1
 nipkow parents: 
15072diff
changeset | 1121 | apply (blast intro:sym) | 
| 
78b5636eabc7
Added a number of new thms and the new function remove1
 nipkow parents: 
15072diff
changeset | 1122 | done | 
| 
78b5636eabc7
Added a number of new thms and the new function remove1
 nipkow parents: 
15072diff
changeset | 1123 | |
| 
78b5636eabc7
Added a number of new thms and the new function remove1
 nipkow parents: 
15072diff
changeset | 1124 | lemma inj_on_map_eq_map: | 
| 
78b5636eabc7
Added a number of new thms and the new function remove1
 nipkow parents: 
15072diff
changeset | 1125 | "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: 
15072diff
changeset | 1126 | by(blast dest:map_inj_on) | 
| 
78b5636eabc7
Added a number of new thms and the new function remove1
 nipkow parents: 
15072diff
changeset | 1127 | |
| 13114 | 1128 | lemma map_injective: | 
| 24526 | 1129 | "map f xs = map f ys ==> inj f ==> xs = ys" | 
| 1130 | by (induct ys arbitrary: xs) (auto dest!:injD) | |
| 13114 | 1131 | |
| 14339 | 1132 | lemma inj_map_eq_map[simp]: "inj f \<Longrightarrow> (map f xs = map f ys) = (xs = ys)" | 
| 1133 | by(blast dest:map_injective) | |
| 1134 | ||
| 13114 | 1135 | lemma inj_mapI: "inj f ==> inj (map f)" | 
| 17589 | 1136 | by (iprover dest: map_injective injD intro: inj_onI) | 
| 13114 | 1137 | |
| 1138 | lemma inj_mapD: "inj (map f) ==> inj f" | |
| 14208 | 1139 | apply (unfold inj_on_def, clarify) | 
| 13145 | 1140 | apply (erule_tac x = "[x]" in ballE) | 
| 14208 | 1141 | apply (erule_tac x = "[y]" in ballE, simp, blast) | 
| 13145 | 1142 | apply blast | 
| 1143 | done | |
| 13114 | 1144 | |
| 14339 | 1145 | lemma inj_map[iff]: "inj (map f) = inj f" | 
| 13145 | 1146 | by (blast dest: inj_mapD intro: inj_mapI) | 
| 13114 | 1147 | |
| 15303 | 1148 | lemma inj_on_mapI: "inj_on f (\<Union>(set ` A)) \<Longrightarrow> inj_on (map f) A" | 
| 1149 | apply(rule inj_onI) | |
| 1150 | apply(erule map_inj_on) | |
| 1151 | apply(blast intro:inj_onI dest:inj_onD) | |
| 1152 | done | |
| 1153 | ||
| 14343 | 1154 | lemma map_idI: "(\<And>x. x \<in> set xs \<Longrightarrow> f x = x) \<Longrightarrow> map f xs = xs" | 
| 1155 | by (induct xs, auto) | |
| 13114 | 1156 | |
| 14402 
4201e1916482
moved lemmas from MicroJava/Comp/AuxLemmas.thy to List.thy
 nipkow parents: 
14395diff
changeset | 1157 | 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: 
14395diff
changeset | 1158 | by (induct xs) auto | 
| 
4201e1916482
moved lemmas from MicroJava/Comp/AuxLemmas.thy to List.thy
 nipkow parents: 
14395diff
changeset | 1159 | |
| 15110 
78b5636eabc7
Added a number of new thms and the new function remove1
 nipkow parents: 
15072diff
changeset | 1160 | lemma map_fst_zip[simp]: | 
| 
78b5636eabc7
Added a number of new thms and the new function remove1
 nipkow parents: 
15072diff
changeset | 1161 | "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: 
15072diff
changeset | 1162 | by (induct rule:list_induct2, simp_all) | 
| 
78b5636eabc7
Added a number of new thms and the new function remove1
 nipkow parents: 
15072diff
changeset | 1163 | |
| 
78b5636eabc7
Added a number of new thms and the new function remove1
 nipkow parents: 
15072diff
changeset | 1164 | lemma map_snd_zip[simp]: | 
| 
78b5636eabc7
Added a number of new thms and the new function remove1
 nipkow parents: 
15072diff
changeset | 1165 | "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: 
15072diff
changeset | 1166 | by (induct rule:list_induct2, simp_all) | 
| 
78b5636eabc7
Added a number of new thms and the new function remove1
 nipkow parents: 
15072diff
changeset | 1167 | |
| 55467 
a5c9002bc54d
renamed 'enriched_type' to more informative 'functor' (following the renaming of enriched type constructors to bounded natural functors)
 blanchet parents: 
55466diff
changeset | 1168 | functor map: map | 
| 47122 | 1169 | by (simp_all add: id_def) | 
| 1170 | ||
| 49948 
744934b818c7
moved quite generic material from theory Enum to more appropriate places
 haftmann parents: 
49808diff
changeset | 1171 | declare map.id [simp] | 
| 
744934b818c7
moved quite generic material from theory Enum to more appropriate places
 haftmann parents: 
49808diff
changeset | 1172 | |
| 
744934b818c7
moved quite generic material from theory Enum to more appropriate places
 haftmann parents: 
49808diff
changeset | 1173 | |
| 
744934b818c7
moved quite generic material from theory Enum to more appropriate places
 haftmann parents: 
49808diff
changeset | 1174 | subsubsection {* @{const rev} *}
 | 
| 13114 | 1175 | |
| 13142 | 1176 | lemma rev_append [simp]: "rev (xs @ ys) = rev ys @ rev xs" | 
| 13145 | 1177 | by (induct xs) auto | 
| 13114 | 1178 | |
| 13142 | 1179 | lemma rev_rev_ident [simp]: "rev (rev xs) = xs" | 
| 13145 | 1180 | by (induct xs) auto | 
| 13114 | 1181 | |
| 15870 | 1182 | lemma rev_swap: "(rev xs = ys) = (xs = rev ys)" | 
| 1183 | by auto | |
| 1184 | ||
| 13142 | 1185 | lemma rev_is_Nil_conv [iff]: "(rev xs = []) = (xs = [])" | 
| 13145 | 1186 | by (induct xs) auto | 
| 13114 | 1187 | |
| 13142 | 1188 | lemma Nil_is_rev_conv [iff]: "([] = rev xs) = (xs = [])" | 
| 13145 | 1189 | by (induct xs) auto | 
| 13114 | 1190 | |
| 15870 | 1191 | lemma rev_singleton_conv [simp]: "(rev xs = [x]) = (xs = [x])" | 
| 1192 | by (cases xs) auto | |
| 1193 | ||
| 1194 | lemma singleton_rev_conv [simp]: "([x] = rev xs) = (xs = [x])" | |
| 1195 | by (cases xs) auto | |
| 1196 | ||
| 54147 
97a8ff4e4ac9
killed most "no_atp", to make Sledgehammer more complete
 blanchet parents: 
53954diff
changeset | 1197 | lemma rev_is_rev_conv [iff]: "(rev xs = rev ys) = (xs = ys)" | 
| 21061 
580dfc999ef6
added normal post setup; cleaned up "execution" constants
 haftmann parents: 
21046diff
changeset | 1198 | apply (induct xs arbitrary: ys, force) | 
| 14208 | 1199 | apply (case_tac ys, simp, force) | 
| 13145 | 1200 | done | 
| 13114 | 1201 | |
| 15439 | 1202 | lemma inj_on_rev[iff]: "inj_on rev A" | 
| 1203 | by(simp add:inj_on_def) | |
| 1204 | ||
| 13366 | 1205 | lemma rev_induct [case_names Nil snoc]: | 
| 1206 | "[| 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: 
15439diff
changeset | 1207 | apply(simplesubst rev_rev_ident[symmetric]) | 
| 13145 | 1208 | apply(rule_tac list = "rev xs" in list.induct, simp_all) | 
| 1209 | done | |
| 13114 | 1210 | |
| 13366 | 1211 | lemma rev_exhaust [case_names Nil snoc]: | 
| 1212 | "(xs = [] ==> P) ==>(!!ys y. xs = ys @ [y] ==> P) ==> P" | |
| 13145 | 1213 | by (induct xs rule: rev_induct) auto | 
| 13114 | 1214 | |
| 13366 | 1215 | lemmas rev_cases = rev_exhaust | 
| 1216 | ||
| 18423 | 1217 | lemma rev_eq_Cons_iff[iff]: "(rev xs = y#ys) = (xs = rev ys @ [y])" | 
| 1218 | by(rule rev_cases[of xs]) auto | |
| 1219 | ||
| 13114 | 1220 | |
| 49948 
744934b818c7
moved quite generic material from theory Enum to more appropriate places
 haftmann parents: 
49808diff
changeset | 1221 | subsubsection {* @{const set} *}
 | 
| 13114 | 1222 | |
| 13142 | 1223 | lemma finite_set [iff]: "finite (set xs)" | 
| 13145 | 1224 | by (induct xs) auto | 
| 13114 | 1225 | |
| 13142 | 1226 | lemma set_append [simp]: "set (xs @ ys) = (set xs \<union> set ys)" | 
| 13145 | 1227 | by (induct xs) auto | 
| 13114 | 1228 | |
| 17830 | 1229 | lemma hd_in_set[simp]: "xs \<noteq> [] \<Longrightarrow> hd xs : set xs" | 
| 1230 | by(cases xs) auto | |
| 14099 | 1231 | |
| 13142 | 1232 | lemma set_subset_Cons: "set xs \<subseteq> set (x # xs)" | 
| 13145 | 1233 | by auto | 
| 13114 | 1234 | |
| 14099 | 1235 | lemma set_ConsD: "y \<in> set (x # xs) \<Longrightarrow> y=x \<or> y \<in> set xs" | 
| 1236 | by auto | |
| 1237 | ||
| 13142 | 1238 | lemma set_empty [iff]: "(set xs = {}) = (xs = [])"
 | 
| 13145 | 1239 | by (induct xs) auto | 
| 13114 | 1240 | |
| 15245 | 1241 | lemma set_empty2[iff]: "({} = set xs) = (xs = [])"
 | 
| 1242 | by(induct xs) auto | |
| 1243 | ||
| 13142 | 1244 | lemma set_rev [simp]: "set (rev xs) = set xs" | 
| 13145 | 1245 | by (induct xs) auto | 
| 13114 | 1246 | |
| 13142 | 1247 | lemma set_map [simp]: "set (map f xs) = f`(set xs)" | 
| 13145 | 1248 | by (induct xs) auto | 
| 13114 | 1249 | |
| 13142 | 1250 | lemma set_filter [simp]: "set (filter P xs) = {x. x : set xs \<and> P x}"
 | 
| 13145 | 1251 | by (induct xs) auto | 
| 13114 | 1252 | |
| 32417 | 1253 | lemma set_upt [simp]: "set[i..<j] = {i..<j}"
 | 
| 41463 
edbf0a86fb1c
adding simproc to rewrite list comprehensions to set comprehensions; adopting proofs
 bulwahn parents: 
41372diff
changeset | 1254 | by (induct j) auto | 
| 13114 | 1255 | |
| 13142 | 1256 | |
| 25221 
5ded95dda5df
append/member: more light-weight way to declare authentic syntax;
 wenzelm parents: 
25215diff
changeset | 1257 | lemma split_list: "x : set xs \<Longrightarrow> \<exists>ys zs. xs = ys @ x # zs" | 
| 18049 | 1258 | proof (induct xs) | 
| 26073 | 1259 | case Nil thus ?case by simp | 
| 1260 | next | |
| 1261 | case Cons thus ?case by (auto intro: Cons_eq_appendI) | |
| 1262 | qed | |
| 1263 | ||
| 26734 | 1264 | lemma in_set_conv_decomp: "x \<in> set xs \<longleftrightarrow> (\<exists>ys zs. xs = ys @ x # zs)" | 
| 1265 | by (auto elim: split_list) | |
| 26073 | 1266 | |
| 1267 | lemma split_list_first: "x : set xs \<Longrightarrow> \<exists>ys zs. xs = ys @ x # zs \<and> x \<notin> set ys" | |
| 1268 | proof (induct xs) | |
| 1269 | case Nil thus ?case by simp | |
| 18049 | 1270 | next | 
| 1271 | case (Cons a xs) | |
| 1272 | show ?case | |
| 1273 | proof cases | |
| 44890 
22f665a2e91c
new fastforce replacing fastsimp - less confusing name
 nipkow parents: 
44635diff
changeset | 1274 | assume "x = a" thus ?case using Cons by fastforce | 
| 18049 | 1275 | next | 
| 44890 
22f665a2e91c
new fastforce replacing fastsimp - less confusing name
 nipkow parents: 
44635diff
changeset | 1276 | assume "x \<noteq> a" thus ?case using Cons by(fastforce intro!: Cons_eq_appendI) | 
| 26073 | 1277 | qed | 
| 1278 | qed | |
| 1279 | ||
| 1280 | lemma in_set_conv_decomp_first: | |
| 1281 | "(x : set xs) = (\<exists>ys zs. xs = ys @ x # zs \<and> x \<notin> set ys)" | |
| 26734 | 1282 | by (auto dest!: split_list_first) | 
| 26073 | 1283 | |
| 40122 
1d8ad2ff3e01
dropped (almost) redundant distinct.induct rule; distinct_simps again named distinct.simps
 haftmann parents: 
40077diff
changeset | 1284 | 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: 
40077diff
changeset | 1285 | proof (induct xs rule: rev_induct) | 
| 26073 | 1286 | case Nil thus ?case by simp | 
| 1287 | next | |
| 1288 | case (snoc a xs) | |
| 1289 | show ?case | |
| 1290 | proof cases | |
| 55584 | 1291 | assume "x = a" thus ?case using snoc by (metis set_simps(1) emptyE) | 
| 26073 | 1292 | next | 
| 44890 
22f665a2e91c
new fastforce replacing fastsimp - less confusing name
 nipkow parents: 
44635diff
changeset | 1293 | assume "x \<noteq> a" thus ?case using snoc by fastforce | 
| 18049 | 1294 | qed | 
| 1295 | qed | |
| 1296 | ||
| 26073 | 1297 | lemma in_set_conv_decomp_last: | 
| 1298 | "(x : set xs) = (\<exists>ys zs. xs = ys @ x # zs \<and> x \<notin> set zs)" | |
| 26734 | 1299 | by (auto dest!: split_list_last) | 
| 26073 | 1300 | |
| 1301 | lemma split_list_prop: "\<exists>x \<in> set xs. P x \<Longrightarrow> \<exists>ys x zs. xs = ys @ x # zs & P x" | |
| 1302 | proof (induct xs) | |
| 1303 | case Nil thus ?case by simp | |
| 1304 | next | |
| 1305 | case Cons thus ?case | |
| 1306 | by(simp add:Bex_def)(metis append_Cons append.simps(1)) | |
| 1307 | qed | |
| 1308 | ||
| 1309 | lemma split_list_propE: | |
| 26734 | 1310 | assumes "\<exists>x \<in> set xs. P x" | 
| 1311 | obtains ys x zs where "xs = ys @ x # zs" and "P x" | |
| 1312 | using split_list_prop [OF assms] by blast | |
| 26073 | 1313 | |
| 1314 | lemma split_list_first_prop: | |
| 1315 | "\<exists>x \<in> set xs. P x \<Longrightarrow> | |
| 1316 | \<exists>ys x zs. xs = ys@x#zs \<and> P x \<and> (\<forall>y \<in> set ys. \<not> P y)" | |
| 26734 | 1317 | proof (induct xs) | 
| 26073 | 1318 | case Nil thus ?case by simp | 
| 1319 | next | |
| 1320 | case (Cons x xs) | |
| 1321 | show ?case | |
| 1322 | proof cases | |
| 1323 | assume "P x" | |
| 40122 
1d8ad2ff3e01
dropped (almost) redundant distinct.induct rule; distinct_simps again named distinct.simps
 haftmann parents: 
40077diff
changeset | 1324 | thus ?thesis by simp (metis Un_upper1 contra_subsetD in_set_conv_decomp_first self_append_conv2 set_append) | 
| 26073 | 1325 | next | 
| 1326 | assume "\<not> P x" | |
| 1327 | hence "\<exists>x\<in>set xs. P x" using Cons(2) by simp | |
| 1328 | thus ?thesis using `\<not> P x` Cons(1) by (metis append_Cons set_ConsD) | |
| 1329 | qed | |
| 1330 | qed | |
| 1331 | ||
| 1332 | lemma split_list_first_propE: | |
| 26734 | 1333 | assumes "\<exists>x \<in> set xs. P x" | 
| 1334 | obtains ys x zs where "xs = ys @ x # zs" and "P x" and "\<forall>y \<in> set ys. \<not> P y" | |
| 1335 | using split_list_first_prop [OF assms] by blast | |
| 26073 | 1336 | |
| 1337 | lemma split_list_first_prop_iff: | |
| 1338 | "(\<exists>x \<in> set xs. P x) \<longleftrightarrow> | |
| 1339 | (\<exists>ys x zs. xs = ys@x#zs \<and> P x \<and> (\<forall>y \<in> set ys. \<not> P y))" | |
| 26734 | 1340 | by (rule, erule split_list_first_prop) auto | 
| 26073 | 1341 | |
| 1342 | lemma split_list_last_prop: | |
| 1343 | "\<exists>x \<in> set xs. P x \<Longrightarrow> | |
| 1344 | \<exists>ys x zs. xs = ys@x#zs \<and> P x \<and> (\<forall>z \<in> set zs. \<not> P z)" | |
| 1345 | proof(induct xs rule:rev_induct) | |
| 1346 | case Nil thus ?case by simp | |
| 1347 | next | |
| 1348 | case (snoc x xs) | |
| 1349 | show ?case | |
| 1350 | proof cases | |
| 1351 | assume "P x" thus ?thesis by (metis emptyE set_empty) | |
| 1352 | next | |
| 1353 | assume "\<not> P x" | |
| 1354 | hence "\<exists>x\<in>set xs. P x" using snoc(2) by simp | |
| 44890 
22f665a2e91c
new fastforce replacing fastsimp - less confusing name
 nipkow parents: 
44635diff
changeset | 1355 | thus ?thesis using `\<not> P x` snoc(1) by fastforce | 
| 26073 | 1356 | qed | 
| 1357 | qed | |
| 1358 | ||
| 1359 | lemma split_list_last_propE: | |
| 26734 | 1360 | assumes "\<exists>x \<in> set xs. P x" | 
| 1361 | obtains ys x zs where "xs = ys @ x # zs" and "P x" and "\<forall>z \<in> set zs. \<not> P z" | |
| 1362 | using split_list_last_prop [OF assms] by blast | |
| 26073 | 1363 | |
| 1364 | lemma split_list_last_prop_iff: | |
| 1365 | "(\<exists>x \<in> set xs. P x) \<longleftrightarrow> | |
| 1366 | (\<exists>ys x zs. xs = ys@x#zs \<and> P x \<and> (\<forall>z \<in> set zs. \<not> P z))" | |
| 26734 | 1367 | by (metis split_list_last_prop [where P=P] in_set_conv_decomp) | 
| 26073 | 1368 | |
| 1369 | lemma finite_list: "finite A ==> EX xs. set xs = A" | |
| 55584 | 1370 | by (erule finite_induct) (auto simp add: set_simps(2) [symmetric] simp del: set_simps(2)) | 
| 13508 | 1371 | |
| 14388 | 1372 | lemma card_length: "card (set xs) \<le> length xs" | 
| 1373 | by (induct xs) (auto simp add: card_insert_if) | |
| 13114 | 1374 | |
| 26442 
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
 haftmann parents: 
26300diff
changeset | 1375 | lemma set_minus_filter_out: | 
| 
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
 haftmann parents: 
26300diff
changeset | 1376 |   "set xs - {y} = set (filter (\<lambda>x. \<not> (x = y)) xs)"
 | 
| 
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
 haftmann parents: 
26300diff
changeset | 1377 | by (induct xs) auto | 
| 15168 | 1378 | |
| 35115 | 1379 | |
| 49948 
744934b818c7
moved quite generic material from theory Enum to more appropriate places
 haftmann parents: 
49808diff
changeset | 1380 | subsubsection {* @{const filter} *}
 | 
| 13114 | 1381 | |
| 13142 | 1382 | lemma filter_append [simp]: "filter P (xs @ ys) = filter P xs @ filter P ys" | 
| 13145 | 1383 | by (induct xs) auto | 
| 13114 | 1384 | |
| 15305 | 1385 | lemma rev_filter: "rev (filter P xs) = filter P (rev xs)" | 
| 1386 | by (induct xs) simp_all | |
| 1387 | ||
| 13142 | 1388 | lemma filter_filter [simp]: "filter P (filter Q xs) = filter (\<lambda>x. Q x \<and> P x) xs" | 
| 13145 | 1389 | by (induct xs) auto | 
| 13114 | 1390 | |
| 16998 | 1391 | lemma length_filter_le [simp]: "length (filter P xs) \<le> length xs" | 
| 1392 | by (induct xs) (auto simp add: le_SucI) | |
| 1393 | ||
| 18423 | 1394 | lemma sum_length_filter_compl: | 
| 1395 | "length(filter P xs) + length(filter (%x. ~P x) xs) = length xs" | |
| 1396 | by(induct xs) simp_all | |
| 1397 | ||
| 13142 | 1398 | lemma filter_True [simp]: "\<forall>x \<in> set xs. P x ==> filter P xs = xs" | 
| 13145 | 1399 | by (induct xs) auto | 
| 13114 | 1400 | |
| 13142 | 1401 | lemma filter_False [simp]: "\<forall>x \<in> set xs. \<not> P x ==> filter P xs = []" | 
| 13145 | 1402 | by (induct xs) auto | 
| 13114 | 1403 | |
| 16998 | 1404 | lemma filter_empty_conv: "(filter P xs = []) = (\<forall>x\<in>set xs. \<not> P x)" | 
| 24349 | 1405 | by (induct xs) simp_all | 
| 16998 | 1406 | |
| 1407 | lemma filter_id_conv: "(filter P xs = xs) = (\<forall>x\<in>set xs. P x)" | |
| 1408 | apply (induct xs) | |
| 1409 | apply auto | |
| 1410 | apply(cut_tac P=P and xs=xs in length_filter_le) | |
| 1411 | apply simp | |
| 1412 | done | |
| 13114 | 1413 | |
| 16965 | 1414 | lemma filter_map: | 
| 1415 | "filter P (map f xs) = map f (filter (P o f) xs)" | |
| 1416 | by (induct xs) simp_all | |
| 1417 | ||
| 1418 | lemma length_filter_map[simp]: | |
| 1419 | "length (filter P (map f xs)) = length(filter (P o f) xs)" | |
| 1420 | by (simp add:filter_map) | |
| 1421 | ||
| 13142 | 1422 | lemma filter_is_subset [simp]: "set (filter P xs) \<le> set xs" | 
| 13145 | 1423 | by auto | 
| 13114 | 1424 | |
| 15246 | 1425 | lemma length_filter_less: | 
| 1426 | "\<lbrakk> x : set xs; ~ P x \<rbrakk> \<Longrightarrow> length(filter P xs) < length xs" | |
| 1427 | proof (induct xs) | |
| 1428 | case Nil thus ?case by simp | |
| 1429 | next | |
| 1430 | case (Cons x xs) thus ?case | |
| 1431 | apply (auto split:split_if_asm) | |
| 1432 | using length_filter_le[of P xs] apply arith | |
| 1433 | done | |
| 1434 | qed | |
| 13114 | 1435 | |
| 15281 | 1436 | lemma length_filter_conv_card: | 
| 1437 |  "length(filter p xs) = card{i. i < length xs & p(xs!i)}"
 | |
| 1438 | proof (induct xs) | |
| 1439 | case Nil thus ?case by simp | |
| 1440 | next | |
| 1441 | case (Cons x xs) | |
| 1442 |   let ?S = "{i. i < length xs & p(xs!i)}"
 | |
| 1443 | have fin: "finite ?S" by(fast intro: bounded_nat_set_is_finite) | |
| 1444 | show ?case (is "?l = card ?S'") | |
| 1445 | proof (cases) | |
| 1446 | assume "p x" | |
| 1447 | hence eq: "?S' = insert 0 (Suc ` ?S)" | |
| 25162 | 1448 | by(auto simp: image_def split:nat.split dest:gr0_implies_Suc) | 
| 15281 | 1449 | have "length (filter p (x # xs)) = Suc(card ?S)" | 
| 23388 | 1450 | using Cons `p x` by simp | 
| 15281 | 1451 | also have "\<dots> = Suc(card(Suc ` ?S))" using fin | 
| 44921 | 1452 | by (simp add: card_image) | 
| 15281 | 1453 | also have "\<dots> = card ?S'" using eq fin | 
| 1454 | by (simp add:card_insert_if) (simp add:image_def) | |
| 1455 | finally show ?thesis . | |
| 1456 | next | |
| 1457 | assume "\<not> p x" | |
| 1458 | hence eq: "?S' = Suc ` ?S" | |
| 25162 | 1459 | by(auto simp add: image_def split:nat.split elim:lessE) | 
| 15281 | 1460 | have "length (filter p (x # xs)) = card ?S" | 
| 23388 | 1461 | using Cons `\<not> p x` by simp | 
| 15281 | 1462 | also have "\<dots> = card(Suc ` ?S)" using fin | 
| 44921 | 1463 | by (simp add: card_image) | 
| 15281 | 1464 | also have "\<dots> = card ?S'" using eq fin | 
| 1465 | by (simp add:card_insert_if) | |
| 1466 | finally show ?thesis . | |
| 1467 | qed | |
| 1468 | qed | |
| 1469 | ||
| 17629 | 1470 | lemma Cons_eq_filterD: | 
| 1471 | "x#xs = filter P ys \<Longrightarrow> | |
| 1472 | \<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 | 1473 | (is "_ \<Longrightarrow> \<exists>us vs. ?P ys us vs") | 
| 17629 | 1474 | proof(induct ys) | 
| 1475 | case Nil thus ?case by simp | |
| 1476 | next | |
| 1477 | case (Cons y ys) | |
| 1478 | show ?case (is "\<exists>x. ?Q x") | |
| 1479 | proof cases | |
| 1480 | assume Py: "P y" | |
| 1481 | show ?thesis | |
| 1482 | proof cases | |
| 25221 
5ded95dda5df
append/member: more light-weight way to declare authentic syntax;
 wenzelm parents: 
25215diff
changeset | 1483 | assume "x = y" | 
| 
5ded95dda5df
append/member: more light-weight way to declare authentic syntax;
 wenzelm parents: 
25215diff
changeset | 1484 | with Py Cons.prems have "?Q []" by simp | 
| 
5ded95dda5df
append/member: more light-weight way to declare authentic syntax;
 wenzelm parents: 
25215diff
changeset | 1485 | then show ?thesis .. | 
| 17629 | 1486 | next | 
| 25221 
5ded95dda5df
append/member: more light-weight way to declare authentic syntax;
 wenzelm parents: 
25215diff
changeset | 1487 | assume "x \<noteq> y" | 
| 
5ded95dda5df
append/member: more light-weight way to declare authentic syntax;
 wenzelm parents: 
25215diff
changeset | 1488 | with Py Cons.prems show ?thesis by simp | 
| 17629 | 1489 | qed | 
| 1490 | next | |
| 25221 
5ded95dda5df
append/member: more light-weight way to declare authentic syntax;
 wenzelm parents: 
25215diff
changeset | 1491 | assume "\<not> P y" | 
| 44890 
22f665a2e91c
new fastforce replacing fastsimp - less confusing name
 nipkow parents: 
44635diff
changeset | 1492 | 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: 
25215diff
changeset | 1493 | then have "?Q (y#us)" by simp | 
| 
5ded95dda5df
append/member: more light-weight way to declare authentic syntax;
 wenzelm parents: 
25215diff
changeset | 1494 | then show ?thesis .. | 
| 17629 | 1495 | qed | 
| 1496 | qed | |
| 1497 | ||
| 1498 | lemma filter_eq_ConsD: | |
| 1499 | "filter P ys = x#xs \<Longrightarrow> | |
| 1500 | \<exists>us vs. ys = us @ x # vs \<and> (\<forall>u\<in>set us. \<not> P u) \<and> P x \<and> xs = filter P vs" | |
| 1501 | by(rule Cons_eq_filterD) simp | |
| 1502 | ||
| 1503 | lemma filter_eq_Cons_iff: | |
| 1504 | "(filter P ys = x#xs) = | |
| 1505 | (\<exists>us vs. ys = us @ x # vs \<and> (\<forall>u\<in>set us. \<not> P u) \<and> P x \<and> xs = filter P vs)" | |
| 1506 | by(auto dest:filter_eq_ConsD) | |
| 1507 | ||
| 1508 | lemma Cons_eq_filter_iff: | |
| 1509 | "(x#xs = filter P ys) = | |
| 1510 | (\<exists>us vs. ys = us @ x # vs \<and> (\<forall>u\<in>set us. \<not> P u) \<and> P x \<and> xs = filter P vs)" | |
| 1511 | by(auto dest:Cons_eq_filterD) | |
| 1512 | ||
| 44013 
5cfc1c36ae97
moved recdef package to HOL/Library/Old_Recdef.thy
 krauss parents: 
43594diff
changeset | 1513 | lemma filter_cong[fundef_cong]: | 
| 17501 | 1514 | "xs = ys \<Longrightarrow> (\<And>x. x \<in> set ys \<Longrightarrow> P x = Q x) \<Longrightarrow> filter P xs = filter Q ys" | 
| 1515 | apply simp | |
| 1516 | apply(erule thin_rl) | |
| 1517 | by (induct ys) simp_all | |
| 1518 | ||
| 15281 | 1519 | |
| 26442 
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
 haftmann parents: 
26300diff
changeset | 1520 | subsubsection {* List partitioning *}
 | 
| 
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
 haftmann parents: 
26300diff
changeset | 1521 | |
| 
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
 haftmann parents: 
26300diff
changeset | 1522 | primrec partition :: "('a \<Rightarrow> bool) \<Rightarrow>'a list \<Rightarrow> 'a list \<times> 'a list" where
 | 
| 50548 | 1523 | "partition P [] = ([], [])" | | 
| 1524 | "partition P (x # xs) = | |
| 1525 | (let (yes, no) = partition P xs | |
| 1526 | in if P x then (x # yes, no) else (yes, x # no))" | |
| 26442 
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
 haftmann parents: 
26300diff
changeset | 1527 | |
| 
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
 haftmann parents: 
26300diff
changeset | 1528 | lemma partition_filter1: | 
| 
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
 haftmann parents: 
26300diff
changeset | 1529 | "fst (partition P xs) = filter P xs" | 
| 
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
 haftmann parents: 
26300diff
changeset | 1530 | by (induct xs) (auto simp add: Let_def split_def) | 
| 
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
 haftmann parents: 
26300diff
changeset | 1531 | |
| 
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
 haftmann parents: 
26300diff
changeset | 1532 | lemma partition_filter2: | 
| 
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
 haftmann parents: 
26300diff
changeset | 1533 | "snd (partition P xs) = filter (Not o P) xs" | 
| 
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
 haftmann parents: 
26300diff
changeset | 1534 | by (induct xs) (auto simp add: Let_def split_def) | 
| 
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
 haftmann parents: 
26300diff
changeset | 1535 | |
| 
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
 haftmann parents: 
26300diff
changeset | 1536 | lemma partition_P: | 
| 
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
 haftmann parents: 
26300diff
changeset | 1537 | assumes "partition P xs = (yes, no)" | 
| 
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
 haftmann parents: 
26300diff
changeset | 1538 | 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: 
26300diff
changeset | 1539 | proof - | 
| 
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
 haftmann parents: 
26300diff
changeset | 1540 | 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: 
26300diff
changeset | 1541 | by simp_all | 
| 
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
 haftmann parents: 
26300diff
changeset | 1542 | then show ?thesis by (simp_all add: partition_filter1 partition_filter2) | 
| 
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
 haftmann parents: 
26300diff
changeset | 1543 | qed | 
| 
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
 haftmann parents: 
26300diff
changeset | 1544 | |
| 
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
 haftmann parents: 
26300diff
changeset | 1545 | lemma partition_set: | 
| 
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
 haftmann parents: 
26300diff
changeset | 1546 | assumes "partition P xs = (yes, no)" | 
| 
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
 haftmann parents: 
26300diff
changeset | 1547 | shows "set yes \<union> set no = set xs" | 
| 
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
 haftmann parents: 
26300diff
changeset | 1548 | proof - | 
| 
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
 haftmann parents: 
26300diff
changeset | 1549 | 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: 
26300diff
changeset | 1550 | by simp_all | 
| 
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
 haftmann parents: 
26300diff
changeset | 1551 | then show ?thesis by (auto simp add: partition_filter1 partition_filter2) | 
| 
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
 haftmann parents: 
26300diff
changeset | 1552 | qed | 
| 
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
 haftmann parents: 
26300diff
changeset | 1553 | |
| 33639 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 1554 | 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: 
33593diff
changeset | 1555 | "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: 
33593diff
changeset | 1556 | 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: 
33593diff
changeset | 1557 | 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: 
33593diff
changeset | 1558 | |
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 1559 | declare partition.simps[simp del] | 
| 26442 
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
 haftmann parents: 
26300diff
changeset | 1560 | |
| 35115 | 1561 | |
| 49948 
744934b818c7
moved quite generic material from theory Enum to more appropriate places
 haftmann parents: 
49808diff
changeset | 1562 | subsubsection {* @{const concat} *}
 | 
| 13114 | 1563 | |
| 13142 | 1564 | lemma concat_append [simp]: "concat (xs @ ys) = concat xs @ concat ys" | 
| 13145 | 1565 | by (induct xs) auto | 
| 13114 | 1566 | |
| 18447 | 1567 | lemma concat_eq_Nil_conv [simp]: "(concat xss = []) = (\<forall>xs \<in> set xss. xs = [])" | 
| 13145 | 1568 | by (induct xss) auto | 
| 13114 | 1569 | |
| 18447 | 1570 | lemma Nil_eq_concat_conv [simp]: "([] = concat xss) = (\<forall>xs \<in> set xss. xs = [])" | 
| 13145 | 1571 | by (induct xss) auto | 
| 13114 | 1572 | |
| 24308 | 1573 | lemma set_concat [simp]: "set (concat xs) = (UN x:set xs. set x)" | 
| 13145 | 1574 | by (induct xs) auto | 
| 13114 | 1575 | |
| 24476 
f7ad9fbbeeaa
turned list comprehension translations into ML to optimize base case
 nipkow parents: 
24471diff
changeset | 1576 | lemma concat_map_singleton[simp]: "concat(map (%x. [f x]) xs) = map f xs" | 
| 24349 | 1577 | by (induct xs) auto | 
| 1578 | ||
| 13142 | 1579 | lemma map_concat: "map f (concat xs) = concat (map (map f) xs)" | 
| 13145 | 1580 | by (induct xs) auto | 
| 13114 | 1581 | |
| 13142 | 1582 | lemma filter_concat: "filter p (concat xs) = concat (map (filter p) xs)" | 
| 13145 | 1583 | by (induct xs) auto | 
| 13114 | 1584 | |
| 13142 | 1585 | lemma rev_concat: "rev (concat xs) = concat (map rev (rev xs))" | 
| 13145 | 1586 | by (induct xs) auto | 
| 13114 | 1587 | |
| 40365 
a1456f2e1c9d
added two lemmas about injectivity of concat to the list theory
 bulwahn parents: 
40304diff
changeset | 1588 | 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: 
40304diff
changeset | 1589 | proof (induct xs arbitrary: ys) | 
| 
a1456f2e1c9d
added two lemmas about injectivity of concat to the list theory
 bulwahn parents: 
40304diff
changeset | 1590 | case (Cons x xs ys) | 
| 
a1456f2e1c9d
added two lemmas about injectivity of concat to the list theory
 bulwahn parents: 
40304diff
changeset | 1591 | thus ?case by (cases ys) auto | 
| 
a1456f2e1c9d
added two lemmas about injectivity of concat to the list theory
 bulwahn parents: 
40304diff
changeset | 1592 | qed (auto) | 
| 
a1456f2e1c9d
added two lemmas about injectivity of concat to the list theory
 bulwahn parents: 
40304diff
changeset | 1593 | |
| 
a1456f2e1c9d
added two lemmas about injectivity of concat to the list theory
 bulwahn parents: 
40304diff
changeset | 1594 | 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: 
40304diff
changeset | 1595 | by (simp add: concat_eq_concat_iff) | 
| 
a1456f2e1c9d
added two lemmas about injectivity of concat to the list theory
 bulwahn parents: 
40304diff
changeset | 1596 | |
| 13114 | 1597 | |
| 49948 
744934b818c7
moved quite generic material from theory Enum to more appropriate places
 haftmann parents: 
49808diff
changeset | 1598 | subsubsection {* @{const nth} *}
 | 
| 13114 | 1599 | |
| 29827 | 1600 | lemma nth_Cons_0 [simp, code]: "(x # xs)!0 = x" | 
| 13145 | 1601 | by auto | 
| 13114 | 1602 | |
| 29827 | 1603 | lemma nth_Cons_Suc [simp, code]: "(x # xs)!(Suc n) = xs!n" | 
| 13145 | 1604 | by auto | 
| 13114 | 1605 | |
| 13142 | 1606 | declare nth.simps [simp del] | 
| 13114 | 1607 | |
| 41842 | 1608 | lemma nth_Cons_pos[simp]: "0 < n \<Longrightarrow> (x#xs) ! n = xs ! (n - 1)" | 
| 1609 | by(auto simp: Nat.gr0_conv_Suc) | |
| 1610 | ||
| 13114 | 1611 | lemma nth_append: | 
| 24526 | 1612 | "(xs @ ys)!n = (if n < length xs then xs!n else ys!(n - length xs))" | 
| 1613 | apply (induct xs arbitrary: n, simp) | |
| 14208 | 1614 | apply (case_tac n, auto) | 
| 13145 | 1615 | done | 
| 13114 | 1616 | |
| 14402 
4201e1916482
moved lemmas from MicroJava/Comp/AuxLemmas.thy to List.thy
 nipkow parents: 
14395diff
changeset | 1617 | 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: 
25215diff
changeset | 1618 | by (induct xs) auto | 
| 14402 
4201e1916482
moved lemmas from MicroJava/Comp/AuxLemmas.thy to List.thy
 nipkow parents: 
14395diff
changeset | 1619 | |
| 
4201e1916482
moved lemmas from MicroJava/Comp/AuxLemmas.thy to List.thy
 nipkow parents: 
14395diff
changeset | 1620 | 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: 
25215diff
changeset | 1621 | by (induct xs) auto | 
| 14402 
4201e1916482
moved lemmas from MicroJava/Comp/AuxLemmas.thy to List.thy
 nipkow parents: 
14395diff
changeset | 1622 | |
| 24526 | 1623 | lemma nth_map [simp]: "n < length xs ==> (map f xs)!n = f(xs!n)" | 
| 1624 | apply (induct xs arbitrary: n, simp) | |
| 14208 | 1625 | apply (case_tac n, auto) | 
| 13145 | 1626 | done | 
| 13114 | 1627 | |
| 45841 | 1628 | lemma nth_tl: | 
| 1629 | assumes "n < length (tl x)" shows "tl x ! n = x ! Suc n" | |
| 1630 | using assms by (induct x) auto | |
| 1631 | ||
| 18423 | 1632 | lemma hd_conv_nth: "xs \<noteq> [] \<Longrightarrow> hd xs = xs!0" | 
| 1633 | by(cases xs) simp_all | |
| 1634 | ||
| 18049 | 1635 | |
| 1636 | lemma list_eq_iff_nth_eq: | |
| 24526 | 1637 | "(xs = ys) = (length xs = length ys \<and> (ALL i<length xs. xs!i = ys!i))" | 
| 1638 | apply(induct xs arbitrary: ys) | |
| 24632 | 1639 | apply force | 
| 18049 | 1640 | apply(case_tac ys) | 
| 1641 | apply simp | |
| 1642 | apply(simp add:nth_Cons split:nat.split)apply blast | |
| 1643 | done | |
| 1644 | ||
| 13142 | 1645 | lemma set_conv_nth: "set xs = {xs!i | i. i < length xs}"
 | 
| 15251 | 1646 | apply (induct xs, simp, simp) | 
| 13145 | 1647 | apply safe | 
| 55642 
63beb38e9258
adapted to renaming of datatype 'cases' and 'recs' to 'case' and 'rec'
 blanchet parents: 
55584diff
changeset | 1648 | apply (metis nat.case(1) nth.simps zero_less_Suc) | 
| 24632 | 1649 | apply (metis less_Suc_eq_0_disj nth_Cons_Suc) | 
| 14208 | 1650 | apply (case_tac i, simp) | 
| 55642 
63beb38e9258
adapted to renaming of datatype 'cases' and 'recs' to 'case' and 'rec'
 blanchet parents: 
55584diff
changeset | 1651 | apply (metis diff_Suc_Suc nat.case(2) nth.simps zero_less_diff) | 
| 13145 | 1652 | done | 
| 13114 | 1653 | |
| 17501 | 1654 | lemma in_set_conv_nth: "(x \<in> set xs) = (\<exists>i < length xs. xs!i = x)" | 
| 1655 | by(auto simp:set_conv_nth) | |
| 1656 | ||
| 51160 
599ff65b85e2
systematic conversions between nat and nibble/char;
 haftmann parents: 
51112diff
changeset | 1657 | lemma nth_equal_first_eq: | 
| 
599ff65b85e2
systematic conversions between nat and nibble/char;
 haftmann parents: 
51112diff
changeset | 1658 | assumes "x \<notin> set xs" | 
| 
599ff65b85e2
systematic conversions between nat and nibble/char;
 haftmann parents: 
51112diff
changeset | 1659 | assumes "n \<le> length xs" | 
| 
599ff65b85e2
systematic conversions between nat and nibble/char;
 haftmann parents: 
51112diff
changeset | 1660 | shows "(x # xs) ! n = x \<longleftrightarrow> n = 0" (is "?lhs \<longleftrightarrow> ?rhs") | 
| 
599ff65b85e2
systematic conversions between nat and nibble/char;
 haftmann parents: 
51112diff
changeset | 1661 | proof | 
| 
599ff65b85e2
systematic conversions between nat and nibble/char;
 haftmann parents: 
51112diff
changeset | 1662 | assume ?lhs | 
| 
599ff65b85e2
systematic conversions between nat and nibble/char;
 haftmann parents: 
51112diff
changeset | 1663 | show ?rhs | 
| 
599ff65b85e2
systematic conversions between nat and nibble/char;
 haftmann parents: 
51112diff
changeset | 1664 | proof (rule ccontr) | 
| 
599ff65b85e2
systematic conversions between nat and nibble/char;
 haftmann parents: 
51112diff
changeset | 1665 | assume "n \<noteq> 0" | 
| 
599ff65b85e2
systematic conversions between nat and nibble/char;
 haftmann parents: 
51112diff
changeset | 1666 | then have "n > 0" by simp | 
| 
599ff65b85e2
systematic conversions between nat and nibble/char;
 haftmann parents: 
51112diff
changeset | 1667 | with `?lhs` have "xs ! (n - 1) = x" by simp | 
| 
599ff65b85e2
systematic conversions between nat and nibble/char;
 haftmann parents: 
51112diff
changeset | 1668 | 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: 
51112diff
changeset | 1669 | ultimately have "\<exists>i<length xs. xs ! i = x" by auto | 
| 
599ff65b85e2
systematic conversions between nat and nibble/char;
 haftmann parents: 
51112diff
changeset | 1670 | 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: 
51112diff
changeset | 1671 | qed | 
| 
599ff65b85e2
systematic conversions between nat and nibble/char;
 haftmann parents: 
51112diff
changeset | 1672 | next | 
| 
599ff65b85e2
systematic conversions between nat and nibble/char;
 haftmann parents: 
51112diff
changeset | 1673 | assume ?rhs then show ?lhs by simp | 
| 
599ff65b85e2
systematic conversions between nat and nibble/char;
 haftmann parents: 
51112diff
changeset | 1674 | qed | 
| 
599ff65b85e2
systematic conversions between nat and nibble/char;
 haftmann parents: 
51112diff
changeset | 1675 | |
| 
599ff65b85e2
systematic conversions between nat and nibble/char;
 haftmann parents: 
51112diff
changeset | 1676 | lemma nth_non_equal_first_eq: | 
| 
599ff65b85e2
systematic conversions between nat and nibble/char;
 haftmann parents: 
51112diff
changeset | 1677 | assumes "x \<noteq> y" | 
| 
599ff65b85e2
systematic conversions between nat and nibble/char;
 haftmann parents: 
51112diff
changeset | 1678 | 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: 
51112diff
changeset | 1679 | proof | 
| 
599ff65b85e2
systematic conversions between nat and nibble/char;
 haftmann parents: 
51112diff
changeset | 1680 | assume "?lhs" with assms have "n > 0" by (cases n) simp_all | 
| 
599ff65b85e2
systematic conversions between nat and nibble/char;
 haftmann parents: 
51112diff
changeset | 1681 | with `?lhs` show ?rhs by simp | 
| 
599ff65b85e2
systematic conversions between nat and nibble/char;
 haftmann parents: 
51112diff
changeset | 1682 | next | 
| 
599ff65b85e2
systematic conversions between nat and nibble/char;
 haftmann parents: 
51112diff
changeset | 1683 | assume "?rhs" then show "?lhs" by simp | 
| 
599ff65b85e2
systematic conversions between nat and nibble/char;
 haftmann parents: 
51112diff
changeset | 1684 | qed | 
| 
599ff65b85e2
systematic conversions between nat and nibble/char;
 haftmann parents: 
51112diff
changeset | 1685 | |
| 13145 | 1686 | lemma list_ball_nth: "[| n < length xs; !x : set xs. P x|] ==> P(xs!n)" | 
| 1687 | by (auto simp add: set_conv_nth) | |
| 13114 | 1688 | |
| 13142 | 1689 | lemma nth_mem [simp]: "n < length xs ==> xs!n : set xs" | 
| 13145 | 1690 | by (auto simp add: set_conv_nth) | 
| 13114 | 1691 | |
| 1692 | lemma all_nth_imp_all_set: | |
| 13145 | 1693 | "[| !i < length xs. P(xs!i); x : set xs|] ==> P x" | 
| 1694 | by (auto simp add: set_conv_nth) | |
| 13114 | 1695 | |
| 1696 | lemma all_set_conv_all_nth: | |
| 13145 | 1697 | "(\<forall>x \<in> set xs. P x) = (\<forall>i. i < length xs --> P (xs ! i))" | 
| 1698 | by (auto simp add: set_conv_nth) | |
| 13114 | 1699 | |
| 25296 | 1700 | lemma rev_nth: | 
| 1701 | "n < size xs \<Longrightarrow> rev xs ! n = xs ! (length xs - Suc n)" | |
| 1702 | proof (induct xs arbitrary: n) | |
| 1703 | case Nil thus ?case by simp | |
| 1704 | next | |
| 1705 | case (Cons x xs) | |
| 1706 | hence n: "n < Suc (length xs)" by simp | |
| 1707 | moreover | |
| 1708 |   { assume "n < length xs"
 | |
| 53374 
a14d2a854c02
tuned proofs -- clarified flow of facts wrt. calculation;
 wenzelm parents: 
53017diff
changeset | 1709 | with n obtain n' where n': "length xs - n = Suc n'" | 
| 25296 | 1710 | by (cases "length xs - n", auto) | 
| 1711 | moreover | |
| 53374 
a14d2a854c02
tuned proofs -- clarified flow of facts wrt. calculation;
 wenzelm parents: 
53017diff
changeset | 1712 | from n' have "length xs - Suc n = n'" by simp | 
| 25296 | 1713 | ultimately | 
| 1714 | have "xs ! (length xs - Suc n) = (x # xs) ! (length xs - n)" by simp | |
| 1715 | } | |
| 1716 | ultimately | |
| 1717 | show ?case by (clarsimp simp add: Cons nth_append) | |
| 1718 | qed | |
| 13114 | 1719 | |
| 31159 | 1720 | lemma Skolem_list_nth: | 
| 1721 | "(ALL i<k. EX x. P i x) = (EX xs. size xs = k & (ALL i<k. P i (xs!i)))" | |
| 1722 | (is "_ = (EX xs. ?P k xs)") | |
| 1723 | proof(induct k) | |
| 1724 | case 0 show ?case by simp | |
| 1725 | next | |
| 1726 | case (Suc k) | |
| 1727 | show ?case (is "?L = ?R" is "_ = (EX xs. ?P' xs)") | |
| 1728 | proof | |
| 1729 | assume "?R" thus "?L" using Suc by auto | |
| 1730 | next | |
| 1731 | assume "?L" | |
| 1732 | with Suc obtain x xs where "?P k xs & P k x" by (metis less_Suc_eq) | |
| 1733 | hence "?P'(xs@[x])" by(simp add:nth_append less_Suc_eq) | |
| 1734 | thus "?R" .. | |
| 1735 | qed | |
| 1736 | qed | |
| 1737 | ||
| 1738 | ||
| 49948 
744934b818c7
moved quite generic material from theory Enum to more appropriate places
 haftmann parents: 
49808diff
changeset | 1739 | subsubsection {* @{const list_update} *}
 | 
| 13114 | 1740 | |
| 24526 | 1741 | lemma length_list_update [simp]: "length(xs[i:=x]) = length xs" | 
| 1742 | by (induct xs arbitrary: i) (auto split: nat.split) | |
| 13114 | 1743 | |
| 1744 | lemma nth_list_update: | |
| 24526 | 1745 | "i < length xs==> (xs[i:=x])!j = (if i = j then x else xs!j)" | 
| 1746 | by (induct xs arbitrary: i j) (auto simp add: nth_Cons split: nat.split) | |
| 13114 | 1747 | |
| 13142 | 1748 | lemma nth_list_update_eq [simp]: "i < length xs ==> (xs[i:=x])!i = x" | 
| 13145 | 1749 | by (simp add: nth_list_update) | 
| 13114 | 1750 | |
| 24526 | 1751 | lemma nth_list_update_neq [simp]: "i \<noteq> j ==> xs[i:=x]!j = xs!j" | 
| 1752 | by (induct xs arbitrary: i j) (auto simp add: nth_Cons split: nat.split) | |
| 13114 | 1753 | |
| 24526 | 1754 | lemma list_update_id[simp]: "xs[i := xs!i] = xs" | 
| 1755 | by (induct xs arbitrary: i) (simp_all split:nat.splits) | |
| 1756 | ||
| 1757 | lemma list_update_beyond[simp]: "length xs \<le> i \<Longrightarrow> xs[i:=x] = xs" | |
| 1758 | apply (induct xs arbitrary: i) | |
| 17501 | 1759 | apply simp | 
| 1760 | apply (case_tac i) | |
| 1761 | apply simp_all | |
| 1762 | done | |
| 1763 | ||
| 31077 | 1764 | lemma list_update_nonempty[simp]: "xs[k:=x] = [] \<longleftrightarrow> xs=[]" | 
| 1765 | by(metis length_0_conv length_list_update) | |
| 1766 | ||
| 13114 | 1767 | lemma list_update_same_conv: | 
| 24526 | 1768 | "i < length xs ==> (xs[i := x] = xs) = (xs!i = x)" | 
| 1769 | by (induct xs arbitrary: i) (auto split: nat.split) | |
| 13114 | 1770 | |
| 14187 | 1771 | lemma list_update_append1: | 
| 24526 | 1772 | "i < size xs \<Longrightarrow> (xs @ ys)[i:=x] = xs[i:=x] @ ys" | 
| 1773 | apply (induct xs arbitrary: i, simp) | |
| 14187 | 1774 | apply(simp split:nat.split) | 
| 1775 | done | |
| 1776 | ||
| 15868 | 1777 | lemma list_update_append: | 
| 24526 | 1778 | "(xs @ ys) [n:= x] = | 
| 15868 | 1779 | (if n < length xs then xs[n:= x] @ ys else xs @ (ys [n-length xs:= x]))" | 
| 24526 | 1780 | by (induct xs arbitrary: n) (auto split:nat.splits) | 
| 15868 | 1781 | |
| 14402 
4201e1916482
moved lemmas from MicroJava/Comp/AuxLemmas.thy to List.thy
 nipkow parents: 
14395diff
changeset | 1782 | lemma list_update_length [simp]: | 
| 
4201e1916482
moved lemmas from MicroJava/Comp/AuxLemmas.thy to List.thy
 nipkow parents: 
14395diff
changeset | 1783 | "(xs @ x # ys)[length xs := y] = (xs @ y # ys)" | 
| 
4201e1916482
moved lemmas from MicroJava/Comp/AuxLemmas.thy to List.thy
 nipkow parents: 
14395diff
changeset | 1784 | by (induct xs, auto) | 
| 
4201e1916482
moved lemmas from MicroJava/Comp/AuxLemmas.thy to List.thy
 nipkow parents: 
14395diff
changeset | 1785 | |
| 31264 | 1786 | lemma map_update: "map f (xs[k:= y]) = (map f xs)[k := f y]" | 
| 1787 | by(induct xs arbitrary: k)(auto split:nat.splits) | |
| 1788 | ||
| 1789 | lemma rev_update: | |
| 1790 | "k < length xs \<Longrightarrow> rev (xs[k:= y]) = (rev xs)[length xs - k - 1 := y]" | |
| 1791 | by (induct xs arbitrary: k) (auto simp: list_update_append split:nat.splits) | |
| 1792 | ||
| 13114 | 1793 | lemma update_zip: | 
| 31080 | 1794 | "(zip xs ys)[i:=xy] = zip (xs[i:=fst xy]) (ys[i:=snd xy])" | 
| 24526 | 1795 | by (induct ys arbitrary: i xy xs) (auto, case_tac xs, auto split: nat.split) | 
| 1796 | ||
| 1797 | lemma set_update_subset_insert: "set(xs[i:=x]) <= insert x (set xs)" | |
| 1798 | by (induct xs arbitrary: i) (auto split: nat.split) | |
| 13114 | 1799 | |
| 1800 | lemma set_update_subsetI: "[| set xs <= A; x:A |] ==> set(xs[i := x]) <= A" | |
| 13145 | 1801 | by (blast dest!: set_update_subset_insert [THEN subsetD]) | 
| 13114 | 1802 | |
| 24526 | 1803 | lemma set_update_memI: "n < length xs \<Longrightarrow> x \<in> set (xs[n := x])" | 
| 1804 | by (induct xs arbitrary: n) (auto split:nat.splits) | |
| 15868 | 1805 | |
| 31077 | 1806 | lemma list_update_overwrite[simp]: | 
| 24796 | 1807 | "xs [i := x, i := y] = xs [i := y]" | 
| 31077 | 1808 | apply (induct xs arbitrary: i) apply simp | 
| 1809 | apply (case_tac i, simp_all) | |
| 24796 | 1810 | done | 
| 1811 | ||
| 1812 | lemma list_update_swap: | |
| 1813 | "i \<noteq> i' \<Longrightarrow> xs [i := x, i' := x'] = xs [i' := x', i := x]" | |
| 1814 | apply (induct xs arbitrary: i i') | |
| 1815 | apply simp | |
| 1816 | apply (case_tac i, case_tac i') | |
| 1817 | apply auto | |
| 1818 | apply (case_tac i') | |
| 1819 | apply auto | |
| 1820 | done | |
| 1821 | ||
| 29827 | 1822 | lemma list_update_code [code]: | 
| 1823 | "[][i := y] = []" | |
| 1824 | "(x # xs)[0 := y] = y # xs" | |
| 1825 | "(x # xs)[Suc i := y] = x # xs[i := y]" | |
| 1826 | by simp_all | |
| 1827 | ||
| 13114 | 1828 | |
| 49948 
744934b818c7
moved quite generic material from theory Enum to more appropriate places
 haftmann parents: 
49808diff
changeset | 1829 | subsubsection {* @{const last} and @{const butlast} *}
 | 
| 13114 | 1830 | |
| 13142 | 1831 | lemma last_snoc [simp]: "last (xs @ [x]) = x" | 
| 13145 | 1832 | by (induct xs) auto | 
| 13114 | 1833 | |
| 13142 | 1834 | lemma butlast_snoc [simp]: "butlast (xs @ [x]) = xs" | 
| 13145 | 1835 | by (induct xs) auto | 
| 13114 | 1836 | |
| 14302 | 1837 | lemma last_ConsL: "xs = [] \<Longrightarrow> last(x#xs) = x" | 
| 44921 | 1838 | by simp | 
| 14302 | 1839 | |
| 1840 | lemma last_ConsR: "xs \<noteq> [] \<Longrightarrow> last(x#xs) = last xs" | |
| 44921 | 1841 | by simp | 
| 14302 | 1842 | |
| 1843 | lemma last_append: "last(xs @ ys) = (if ys = [] then last xs else last ys)" | |
| 1844 | by (induct xs) (auto) | |
| 1845 | ||
| 1846 | lemma last_appendL[simp]: "ys = [] \<Longrightarrow> last(xs @ ys) = last xs" | |
| 1847 | by(simp add:last_append) | |
| 1848 | ||
| 1849 | lemma last_appendR[simp]: "ys \<noteq> [] \<Longrightarrow> last(xs @ ys) = last ys" | |
| 1850 | by(simp add:last_append) | |
| 1851 | ||
| 45841 | 1852 | lemma last_tl: "xs = [] \<or> tl xs \<noteq> [] \<Longrightarrow>last (tl xs) = last xs" | 
| 1853 | by (induct xs) simp_all | |
| 1854 | ||
| 1855 | lemma butlast_tl: "butlast (tl xs) = tl (butlast xs)" | |
| 1856 | by (induct xs) simp_all | |
| 1857 | ||
| 17762 | 1858 | lemma hd_rev: "xs \<noteq> [] \<Longrightarrow> hd(rev xs) = last xs" | 
| 1859 | by(rule rev_exhaust[of xs]) simp_all | |
| 1860 | ||
| 1861 | lemma last_rev: "xs \<noteq> [] \<Longrightarrow> last(rev xs) = hd xs" | |
| 1862 | by(cases xs) simp_all | |
| 1863 | ||
| 17765 | 1864 | lemma last_in_set[simp]: "as \<noteq> [] \<Longrightarrow> last as \<in> set as" | 
| 1865 | by (induct as) auto | |
| 17762 | 1866 | |
| 13142 | 1867 | lemma length_butlast [simp]: "length (butlast xs) = length xs - 1" | 
| 13145 | 1868 | by (induct xs rule: rev_induct) auto | 
| 13114 | 1869 | |
| 1870 | lemma butlast_append: | |
| 24526 | 1871 | "butlast (xs @ ys) = (if ys = [] then butlast xs else xs @ butlast ys)" | 
| 1872 | by (induct xs arbitrary: ys) auto | |
| 13114 | 1873 | |
| 13142 | 1874 | lemma append_butlast_last_id [simp]: | 
| 13145 | 1875 | "xs \<noteq> [] ==> butlast xs @ [last xs] = xs" | 
| 1876 | by (induct xs) auto | |
| 13114 | 1877 | |
| 13142 | 1878 | lemma in_set_butlastD: "x : set (butlast xs) ==> x : set xs" | 
| 13145 | 1879 | by (induct xs) (auto split: split_if_asm) | 
| 13114 | 1880 | |
| 1881 | lemma in_set_butlast_appendI: | |
| 13145 | 1882 | "x : set (butlast xs) | x : set (butlast ys) ==> x : set (butlast (xs @ ys))" | 
| 1883 | by (auto dest: in_set_butlastD simp add: butlast_append) | |
| 13114 | 1884 | |
| 24526 | 1885 | lemma last_drop[simp]: "n < length xs \<Longrightarrow> last (drop n xs) = last xs" | 
| 1886 | apply (induct xs arbitrary: n) | |
| 17501 | 1887 | apply simp | 
| 1888 | apply (auto split:nat.split) | |
| 1889 | done | |
| 1890 | ||
| 45841 | 1891 | lemma nth_butlast: | 
| 1892 | assumes "n < length (butlast xs)" shows "butlast xs ! n = xs ! n" | |
| 1893 | proof (cases xs) | |
| 1894 | case (Cons y ys) | |
| 1895 | moreover from assms have "butlast xs ! n = (butlast xs @ [last xs]) ! n" | |
| 1896 | by (simp add: nth_append) | |
| 1897 | ultimately show ?thesis using append_butlast_last_id by simp | |
| 1898 | qed simp | |
| 1899 | ||
| 30128 
365ee7319b86
revert some Suc 0 lemmas back to their original forms; added some simp rules for (1::nat)
 huffman parents: 
30079diff
changeset | 1900 | lemma last_conv_nth: "xs\<noteq>[] \<Longrightarrow> last xs = xs!(length xs - 1)" | 
| 17589 | 1901 | by(induct xs)(auto simp:neq_Nil_conv) | 
| 1902 | ||
| 30128 
365ee7319b86
revert some Suc 0 lemmas back to their original forms; added some simp rules for (1::nat)
 huffman parents: 
30079diff
changeset | 1903 | lemma butlast_conv_take: "butlast xs = take (length xs - 1) xs" | 
| 26584 
46f3b89b2445
move lemmas from Word/BinBoolList.thy to List.thy
 huffman parents: 
26480diff
changeset | 1904 | by (induct xs, simp, case_tac xs, simp_all) | 
| 
46f3b89b2445
move lemmas from Word/BinBoolList.thy to List.thy
 huffman parents: 
26480diff
changeset | 1905 | |
| 31077 | 1906 | lemma last_list_update: | 
| 1907 | "xs \<noteq> [] \<Longrightarrow> last(xs[k:=x]) = (if k = size xs - 1 then x else last xs)" | |
| 1908 | by (auto simp: last_conv_nth) | |
| 1909 | ||
| 1910 | lemma butlast_list_update: | |
| 1911 | "butlast(xs[k:=x]) = | |
| 1912 | (if k = size xs - 1 then butlast xs else (butlast xs)[k:=x])" | |
| 1913 | apply(cases xs rule:rev_cases) | |
| 1914 | apply simp | |
| 1915 | apply(simp add:list_update_append split:nat.splits) | |
| 1916 | done | |
| 1917 | ||
| 36851 | 1918 | lemma last_map: | 
| 1919 | "xs \<noteq> [] \<Longrightarrow> last (map f xs) = f (last xs)" | |
| 1920 | by (cases xs rule: rev_cases) simp_all | |
| 1921 | ||
| 1922 | lemma map_butlast: | |
| 1923 | "map f (butlast xs) = butlast (map f xs)" | |
| 1924 | by (induct xs) simp_all | |
| 1925 | ||
| 40230 | 1926 | lemma snoc_eq_iff_butlast: | 
| 1927 | "xs @ [x] = ys \<longleftrightarrow> (ys \<noteq> [] & butlast ys = xs & last ys = x)" | |
| 1928 | by (metis append_butlast_last_id append_is_Nil_conv butlast_snoc last_snoc not_Cons_self) | |
| 1929 | ||
| 24796 | 1930 | |
| 49948 
744934b818c7
moved quite generic material from theory Enum to more appropriate places
 haftmann parents: 
49808diff
changeset | 1931 | subsubsection {* @{const take} and @{const drop} *}
 | 
| 13114 | 1932 | |
| 13142 | 1933 | lemma take_0 [simp]: "take 0 xs = []" | 
| 13145 | 1934 | by (induct xs) auto | 
| 13114 | 1935 | |
| 13142 | 1936 | lemma drop_0 [simp]: "drop 0 xs = xs" | 
| 13145 | 1937 | by (induct xs) auto | 
| 13114 | 1938 | |
| 13142 | 1939 | lemma take_Suc_Cons [simp]: "take (Suc n) (x # xs) = x # take n xs" | 
| 13145 | 1940 | by simp | 
| 13114 | 1941 | |
| 13142 | 1942 | lemma drop_Suc_Cons [simp]: "drop (Suc n) (x # xs) = drop n xs" | 
| 13145 | 1943 | by simp | 
| 13114 | 1944 | |
| 13142 | 1945 | declare take_Cons [simp del] and drop_Cons [simp del] | 
| 13114 | 1946 | |
| 30128 
365ee7319b86
revert some Suc 0 lemmas back to their original forms; added some simp rules for (1::nat)
 huffman parents: 
30079diff
changeset | 1947 | 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: 
30079diff
changeset | 1948 | 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: 
30079diff
changeset | 1949 | |
| 
365ee7319b86
revert some Suc 0 lemmas back to their original forms; added some simp rules for (1::nat)
 huffman parents: 
30079diff
changeset | 1950 | 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: 
30079diff
changeset | 1951 | 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: 
30079diff
changeset | 1952 | |
| 15110 
78b5636eabc7
Added a number of new thms and the new function remove1
 nipkow parents: 
15072diff
changeset | 1953 | 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: 
15072diff
changeset | 1954 | by(clarsimp simp add:neq_Nil_conv) | 
| 
78b5636eabc7
Added a number of new thms and the new function remove1
 nipkow parents: 
15072diff
changeset | 1955 | |
| 14187 | 1956 | lemma drop_Suc: "drop (Suc n) xs = drop n (tl xs)" | 
| 1957 | by(cases xs, simp_all) | |
| 1958 | ||
| 26584 
46f3b89b2445
move lemmas from Word/BinBoolList.thy to List.thy
 huffman parents: 
26480diff
changeset | 1959 | lemma take_tl: "take n (tl xs) = tl (take (Suc n) xs)" | 
| 
46f3b89b2445
move lemmas from Word/BinBoolList.thy to List.thy
 huffman parents: 
26480diff
changeset | 1960 | by (induct xs arbitrary: n) simp_all | 
| 
46f3b89b2445
move lemmas from Word/BinBoolList.thy to List.thy
 huffman parents: 
26480diff
changeset | 1961 | |
| 24526 | 1962 | lemma drop_tl: "drop n (tl xs) = tl(drop n xs)" | 
| 1963 | by(induct xs arbitrary: n, simp_all add:drop_Cons drop_Suc split:nat.split) | |
| 1964 | ||
| 26584 
46f3b89b2445
move lemmas from Word/BinBoolList.thy to List.thy
 huffman parents: 
26480diff
changeset | 1965 | lemma tl_take: "tl (take n xs) = take (n - 1) (tl xs)" | 
| 
46f3b89b2445
move lemmas from Word/BinBoolList.thy to List.thy
 huffman parents: 
26480diff
changeset | 1966 | by (cases n, simp, cases xs, auto) | 
| 
46f3b89b2445
move lemmas from Word/BinBoolList.thy to List.thy
 huffman parents: 
26480diff
changeset | 1967 | |
| 
46f3b89b2445
move lemmas from Word/BinBoolList.thy to List.thy
 huffman parents: 
26480diff
changeset | 1968 | lemma tl_drop: "tl (drop n xs) = drop n (tl xs)" | 
| 
46f3b89b2445
move lemmas from Word/BinBoolList.thy to List.thy
 huffman parents: 
26480diff
changeset | 1969 | by (simp only: drop_tl) | 
| 
46f3b89b2445
move lemmas from Word/BinBoolList.thy to List.thy
 huffman parents: 
26480diff
changeset | 1970 | |
| 24526 | 1971 | lemma nth_via_drop: "drop n xs = y#ys \<Longrightarrow> xs!n = y" | 
| 1972 | apply (induct xs arbitrary: n, simp) | |
| 14187 | 1973 | apply(simp add:drop_Cons nth_Cons split:nat.splits) | 
| 1974 | done | |
| 1975 | ||
| 13913 | 1976 | lemma take_Suc_conv_app_nth: | 
| 24526 | 1977 | "i < length xs \<Longrightarrow> take (Suc i) xs = take i xs @ [xs!i]" | 
| 1978 | apply (induct xs arbitrary: i, simp) | |
| 14208 | 1979 | apply (case_tac i, auto) | 
| 13913 | 1980 | done | 
| 1981 | ||
| 14591 | 1982 | lemma drop_Suc_conv_tl: | 
| 24526 | 1983 | "i < length xs \<Longrightarrow> (xs!i) # (drop (Suc i) xs) = drop i xs" | 
| 1984 | apply (induct xs arbitrary: i, simp) | |
| 14591 | 1985 | apply (case_tac i, auto) | 
| 1986 | done | |
| 1987 | ||
| 24526 | 1988 | lemma length_take [simp]: "length (take n xs) = min (length xs) n" | 
| 1989 | by (induct n arbitrary: xs) (auto, case_tac xs, auto) | |
| 1990 | ||
| 1991 | lemma length_drop [simp]: "length (drop n xs) = (length xs - n)" | |
| 1992 | by (induct n arbitrary: xs) (auto, case_tac xs, auto) | |
| 1993 | ||
| 1994 | lemma take_all [simp]: "length xs <= n ==> take n xs = xs" | |
| 1995 | by (induct n arbitrary: xs) (auto, case_tac xs, auto) | |
| 1996 | ||
| 1997 | lemma drop_all [simp]: "length xs <= n ==> drop n xs = []" | |
| 1998 | by (induct n arbitrary: xs) (auto, case_tac xs, auto) | |
| 13114 | 1999 | |
| 13142 | 2000 | lemma take_append [simp]: | 
| 24526 | 2001 | "take n (xs @ ys) = (take n xs @ take (n - length xs) ys)" | 
| 2002 | by (induct n arbitrary: xs) (auto, case_tac xs, auto) | |
| 13114 | 2003 | |
| 13142 | 2004 | lemma drop_append [simp]: | 
| 24526 | 2005 | "drop n (xs @ ys) = drop n xs @ drop (n - length xs) ys" | 
| 2006 | by (induct n arbitrary: xs) (auto, case_tac xs, auto) | |
| 2007 | ||
| 2008 | lemma take_take [simp]: "take n (take m xs) = take (min n m) xs" | |
| 2009 | apply (induct m arbitrary: xs n, auto) | |
| 14208 | 2010 | apply (case_tac xs, auto) | 
| 15236 
f289e8ba2bb3
Proofs needed to be updated because induction now preserves name of
 nipkow parents: 
15176diff
changeset | 2011 | apply (case_tac n, auto) | 
| 13145 | 2012 | done | 
| 13114 | 2013 | |
| 24526 | 2014 | lemma drop_drop [simp]: "drop n (drop m xs) = drop (n + m) xs" | 
| 2015 | apply (induct m arbitrary: xs, auto) | |
| 14208 | 2016 | apply (case_tac xs, auto) | 
| 13145 | 2017 | done | 
| 13114 | 2018 | |
| 24526 | 2019 | lemma take_drop: "take n (drop m xs) = drop m (take (n + m) xs)" | 
| 2020 | apply (induct m arbitrary: xs n, auto) | |
| 14208 | 2021 | apply (case_tac xs, auto) | 
| 13145 | 2022 | done | 
| 13114 | 2023 | |
| 24526 | 2024 | lemma drop_take: "drop n (take m xs) = take (m-n) (drop n xs)" | 
| 2025 | apply(induct xs arbitrary: m n) | |
| 14802 | 2026 | apply simp | 
| 2027 | apply(simp add: take_Cons drop_Cons split:nat.split) | |
| 2028 | done | |
| 2029 | ||
| 24526 | 2030 | lemma append_take_drop_id [simp]: "take n xs @ drop n xs = xs" | 
| 2031 | apply (induct n arbitrary: xs, auto) | |
| 14208 | 2032 | apply (case_tac xs, auto) | 
| 13145 | 2033 | done | 
| 13114 | 2034 | |
| 24526 | 2035 | lemma take_eq_Nil[simp]: "(take n xs = []) = (n = 0 \<or> xs = [])" | 
| 2036 | apply(induct xs arbitrary: n) | |
| 15110 
78b5636eabc7
Added a number of new thms and the new function remove1
 nipkow parents: 
15072diff
changeset | 2037 | apply simp | 
| 
78b5636eabc7
Added a number of new thms and the new function remove1
 nipkow parents: 
15072diff
changeset | 2038 | apply(simp add:take_Cons split:nat.split) | 
| 
78b5636eabc7
Added a number of new thms and the new function remove1
 nipkow parents: 
15072diff
changeset | 2039 | done | 
| 
78b5636eabc7
Added a number of new thms and the new function remove1
 nipkow parents: 
15072diff
changeset | 2040 | |
| 24526 | 2041 | lemma drop_eq_Nil[simp]: "(drop n xs = []) = (length xs <= n)" | 
| 2042 | apply(induct xs arbitrary: n) | |
| 15110 
78b5636eabc7
Added a number of new thms and the new function remove1
 nipkow parents: 
15072diff
changeset | 2043 | apply simp | 
| 
78b5636eabc7
Added a number of new thms and the new function remove1
 nipkow parents: 
15072diff
changeset | 2044 | apply(simp add:drop_Cons split:nat.split) | 
| 
78b5636eabc7
Added a number of new thms and the new function remove1
 nipkow parents: 
15072diff
changeset | 2045 | done | 
| 
78b5636eabc7
Added a number of new thms and the new function remove1
 nipkow parents: 
15072diff
changeset | 2046 | |
| 24526 | 2047 | lemma take_map: "take n (map f xs) = map f (take n xs)" | 
| 2048 | apply (induct n arbitrary: xs, auto) | |
| 14208 | 2049 | apply (case_tac xs, auto) | 
| 13145 | 2050 | done | 
| 13114 | 2051 | |
| 24526 | 2052 | lemma drop_map: "drop n (map f xs) = map f (drop n xs)" | 
| 2053 | apply (induct n arbitrary: xs, auto) | |
| 14208 | 2054 | apply (case_tac xs, auto) | 
| 13145 | 2055 | done | 
| 13114 | 2056 | |
| 24526 | 2057 | lemma rev_take: "rev (take i xs) = drop (length xs - i) (rev xs)" | 
| 2058 | apply (induct xs arbitrary: i, auto) | |
| 14208 | 2059 | apply (case_tac i, auto) | 
| 13145 | 2060 | done | 
| 13114 | 2061 | |
| 24526 | 2062 | lemma rev_drop: "rev (drop i xs) = take (length xs - i) (rev xs)" | 
| 2063 | apply (induct xs arbitrary: i, auto) | |
| 14208 | 2064 | apply (case_tac i, auto) | 
| 13145 | 2065 | done | 
| 13114 | 2066 | |
| 24526 | 2067 | lemma nth_take [simp]: "i < n ==> (take n xs)!i = xs!i" | 
| 2068 | apply (induct xs arbitrary: i n, auto) | |
| 14208 | 2069 | apply (case_tac n, blast) | 
| 2070 | apply (case_tac i, auto) | |
| 13145 | 2071 | done | 
| 13114 | 2072 | |
| 13142 | 2073 | lemma nth_drop [simp]: | 
| 24526 | 2074 | "n + i <= length xs ==> (drop n xs)!i = xs!(n + i)" | 
| 2075 | apply (induct n arbitrary: xs i, auto) | |
| 14208 | 2076 | apply (case_tac xs, auto) | 
| 13145 | 2077 | done | 
| 3507 | 2078 | |
| 26584 
46f3b89b2445
move lemmas from Word/BinBoolList.thy to List.thy
 huffman parents: 
26480diff
changeset | 2079 | lemma butlast_take: | 
| 30128 
365ee7319b86
revert some Suc 0 lemmas back to their original forms; added some simp rules for (1::nat)
 huffman parents: 
30079diff
changeset | 2080 | "n <= length xs ==> butlast (take n xs) = take (n - 1) xs" | 
| 54863 
82acc20ded73
prefer more canonical names for lemmas on min/max
 haftmann parents: 
54600diff
changeset | 2081 | by (simp add: butlast_conv_take min.absorb1 min.absorb2) | 
| 26584 
46f3b89b2445
move lemmas from Word/BinBoolList.thy to List.thy
 huffman parents: 
26480diff
changeset | 2082 | |
| 
46f3b89b2445
move lemmas from Word/BinBoolList.thy to List.thy
 huffman parents: 
26480diff
changeset | 2083 | lemma butlast_drop: "butlast (drop n xs) = drop n (butlast xs)" | 
| 30128 
365ee7319b86
revert some Suc 0 lemmas back to their original forms; added some simp rules for (1::nat)
 huffman parents: 
30079diff
changeset | 2084 | by (simp add: butlast_conv_take drop_take add_ac) | 
| 26584 
46f3b89b2445
move lemmas from Word/BinBoolList.thy to List.thy
 huffman parents: 
26480diff
changeset | 2085 | |
| 
46f3b89b2445
move lemmas from Word/BinBoolList.thy to List.thy
 huffman parents: 
26480diff
changeset | 2086 | 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: 
54600diff
changeset | 2087 | by (simp add: butlast_conv_take min.absorb1) | 
| 26584 
46f3b89b2445
move lemmas from Word/BinBoolList.thy to List.thy
 huffman parents: 
26480diff
changeset | 2088 | |
| 
46f3b89b2445
move lemmas from Word/BinBoolList.thy to List.thy
 huffman parents: 
26480diff
changeset | 2089 | lemma drop_butlast: "drop n (butlast xs) = butlast (drop n xs)" | 
| 30128 
365ee7319b86
revert some Suc 0 lemmas back to their original forms; added some simp rules for (1::nat)
 huffman parents: 
30079diff
changeset | 2090 | by (simp add: butlast_conv_take drop_take add_ac) | 
| 26584 
46f3b89b2445
move lemmas from Word/BinBoolList.thy to List.thy
 huffman parents: 
26480diff
changeset | 2091 | |
| 46500 
0196966d6d2d
removing unnecessary premises in theorems of List theory
 bulwahn parents: 
46448diff
changeset | 2092 | lemma hd_drop_conv_nth: "n < length xs \<Longrightarrow> hd(drop n xs) = xs!n" | 
| 18423 | 2093 | by(simp add: hd_conv_nth) | 
| 2094 | ||
| 35248 | 2095 | lemma set_take_subset_set_take: | 
| 2096 | "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: 
41372diff
changeset | 2097 | apply (induct xs arbitrary: m n) | 
| 
edbf0a86fb1c
adding simproc to rewrite list comprehensions to set comprehensions; adopting proofs
 bulwahn parents: 
41372diff
changeset | 2098 | apply simp | 
| 
edbf0a86fb1c
adding simproc to rewrite list comprehensions to set comprehensions; adopting proofs
 bulwahn parents: 
41372diff
changeset | 2099 | apply (case_tac n) | 
| 
edbf0a86fb1c
adding simproc to rewrite list comprehensions to set comprehensions; adopting proofs
 bulwahn parents: 
41372diff
changeset | 2100 | apply (auto simp: take_Cons) | 
| 
edbf0a86fb1c
adding simproc to rewrite list comprehensions to set comprehensions; adopting proofs
 bulwahn parents: 
41372diff
changeset | 2101 | done | 
| 35248 | 2102 | |
| 24526 | 2103 | lemma set_take_subset: "set(take n xs) \<subseteq> set xs" | 
| 2104 | by(induct xs arbitrary: n)(auto simp:take_Cons split:nat.split) | |
| 2105 | ||
| 2106 | lemma set_drop_subset: "set(drop n xs) \<subseteq> set xs" | |
| 2107 | by(induct xs arbitrary: n)(auto simp:drop_Cons split:nat.split) | |
| 14025 | 2108 | |
| 35248 | 2109 | lemma set_drop_subset_set_drop: | 
| 2110 | "m >= n \<Longrightarrow> set(drop m xs) <= set(drop n xs)" | |
| 2111 | apply(induct xs arbitrary: m n) | |
| 2112 | apply(auto simp:drop_Cons split:nat.split) | |
| 2113 | apply (metis set_drop_subset subset_iff) | |
| 2114 | done | |
| 2115 | ||
| 14187 | 2116 | lemma in_set_takeD: "x : set(take n xs) \<Longrightarrow> x : set xs" | 
| 2117 | using set_take_subset by fast | |
| 2118 | ||
| 2119 | lemma in_set_dropD: "x : set(drop n xs) \<Longrightarrow> x : set xs" | |
| 2120 | using set_drop_subset by fast | |
| 2121 | ||
| 13114 | 2122 | lemma append_eq_conv_conj: | 
| 24526 | 2123 | "(xs @ ys = zs) = (xs = take (length xs) zs \<and> ys = drop (length xs) zs)" | 
| 2124 | apply (induct xs arbitrary: zs, simp, clarsimp) | |
| 14208 | 2125 | apply (case_tac zs, auto) | 
| 13145 | 2126 | done | 
| 13142 | 2127 | |
| 24526 | 2128 | lemma take_add: | 
| 42713 | 2129 | "take (i+j) xs = take i xs @ take j (drop i xs)" | 
| 24526 | 2130 | apply (induct xs arbitrary: i, auto) | 
| 2131 | apply (case_tac i, simp_all) | |
| 14050 | 2132 | done | 
| 2133 | ||
| 14300 | 2134 | 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: 
52435diff
changeset | 2135 | "(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: 
52435diff
changeset | 2136 | (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: 
52435diff
changeset | 2137 | 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: 
52435diff
changeset | 2138 | 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: 
52435diff
changeset | 2139 | apply(induct xs\<^sub>1 arbitrary: ys\<^sub>1) | 
| 14300 | 2140 | apply simp | 
| 53015 
a1119cf551e8
standardized symbols via "isabelle update_sub_sup", excluding src/Pure and src/Tools/WWW_Find;
 wenzelm parents: 
52435diff
changeset | 2141 | apply(case_tac ys\<^sub>1) | 
| 14300 | 2142 | apply simp_all | 
| 2143 | done | |
| 2144 | ||
| 15110 
78b5636eabc7
Added a number of new thms and the new function remove1
 nipkow parents: 
15072diff
changeset | 2145 | 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: 
30008diff
changeset | 2146 | "n < length xs \<Longrightarrow> take n xs @ [hd (drop n xs)] = take (Suc n) xs" | 
| 24526 | 2147 | apply(induct xs arbitrary: n) | 
| 15110 
78b5636eabc7
Added a number of new thms and the new function remove1
 nipkow parents: 
15072diff
changeset | 2148 | apply simp | 
| 
78b5636eabc7
Added a number of new thms and the new function remove1
 nipkow parents: 
15072diff
changeset | 2149 | apply(simp add:drop_Cons split:nat.split) | 
| 
78b5636eabc7
Added a number of new thms and the new function remove1
 nipkow parents: 
15072diff
changeset | 2150 | done | 
| 
78b5636eabc7
Added a number of new thms and the new function remove1
 nipkow parents: 
15072diff
changeset | 2151 | |
| 17501 | 2152 | lemma id_take_nth_drop: | 
| 2153 | "i < length xs \<Longrightarrow> xs = take i xs @ xs!i # drop (Suc i) xs" | |
| 2154 | proof - | |
| 2155 | assume si: "i < length xs" | |
| 2156 | hence "xs = take (Suc i) xs @ drop (Suc i) xs" by auto | |
| 2157 | moreover | |
| 2158 | from si have "take (Suc i) xs = take i xs @ [xs!i]" | |
| 2159 | apply (rule_tac take_Suc_conv_app_nth) by arith | |
| 2160 | ultimately show ?thesis by auto | |
| 2161 | qed | |
| 2162 | ||
| 2163 | lemma upd_conv_take_nth_drop: | |
| 2164 | "i < length xs \<Longrightarrow> xs[i:=a] = take i xs @ a # drop (Suc i) xs" | |
| 2165 | proof - | |
| 2166 | assume i: "i < length xs" | |
| 2167 | have "xs[i:=a] = (take i xs @ xs!i # drop (Suc i) xs)[i:=a]" | |
| 2168 | by(rule arg_cong[OF id_take_nth_drop[OF i]]) | |
| 2169 | also have "\<dots> = take i xs @ a # drop (Suc i) xs" | |
| 2170 | using i by (simp add: list_update_append) | |
| 2171 | finally show ?thesis . | |
| 2172 | qed | |
| 2173 | ||
| 24796 | 2174 | lemma nth_drop': | 
| 2175 | "i < length xs \<Longrightarrow> xs ! i # drop (Suc i) xs = drop i xs" | |
| 2176 | apply (induct i arbitrary: xs) | |
| 2177 | apply (simp add: neq_Nil_conv) | |
| 2178 | apply (erule exE)+ | |
| 2179 | apply simp | |
| 2180 | apply (case_tac xs) | |
| 2181 | apply simp_all | |
| 2182 | done | |
| 2183 | ||
| 13114 | 2184 | |
| 49948 
744934b818c7
moved quite generic material from theory Enum to more appropriate places
 haftmann parents: 
49808diff
changeset | 2185 | subsubsection {* @{const takeWhile} and @{const dropWhile} *}
 | 
| 13114 | 2186 | |
| 33639 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 2187 | 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: 
33593diff
changeset | 2188 | 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: 
33593diff
changeset | 2189 | |
| 13142 | 2190 | lemma takeWhile_dropWhile_id [simp]: "takeWhile P xs @ dropWhile P xs = xs" | 
| 13145 | 2191 | by (induct xs) auto | 
| 13114 | 2192 | |
| 13142 | 2193 | lemma takeWhile_append1 [simp]: | 
| 13145 | 2194 | "[| x:set xs; ~P(x)|] ==> takeWhile P (xs @ ys) = takeWhile P xs" | 
| 2195 | by (induct xs) auto | |
| 13114 | 2196 | |
| 13142 | 2197 | lemma takeWhile_append2 [simp]: | 
| 13145 | 2198 | "(!!x. x : set xs ==> P x) ==> takeWhile P (xs @ ys) = xs @ takeWhile P ys" | 
| 2199 | by (induct xs) auto | |
| 13114 | 2200 | |
| 13142 | 2201 | lemma takeWhile_tail: "\<not> P x ==> takeWhile P (xs @ (x#l)) = takeWhile P xs" | 
| 13145 | 2202 | by (induct xs) auto | 
| 13114 | 2203 | |
| 33639 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 2204 | 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: 
33593diff
changeset | 2205 | 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: 
33593diff
changeset | 2206 | |
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 2207 | 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: 
33593diff
changeset | 2208 | 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: 
33593diff
changeset | 2209 | |
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 2210 | 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: 
33593diff
changeset | 2211 | 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: 
33593diff
changeset | 2212 | |
| 13142 | 2213 | lemma dropWhile_append1 [simp]: | 
| 13145 | 2214 | "[| x : set xs; ~P(x)|] ==> dropWhile P (xs @ ys) = (dropWhile P xs)@ys" | 
| 2215 | by (induct xs) auto | |
| 13114 | 2216 | |
| 13142 | 2217 | lemma dropWhile_append2 [simp]: | 
| 13145 | 2218 | "(!!x. x:set xs ==> P(x)) ==> dropWhile P (xs @ ys) = dropWhile P ys" | 
| 2219 | by (induct xs) auto | |
| 13114 | 2220 | |
| 45841 | 2221 | lemma dropWhile_append3: | 
| 2222 | "\<not> P y \<Longrightarrow>dropWhile P (xs @ y # ys) = dropWhile P xs @ y # ys" | |
| 2223 | by (induct xs) auto | |
| 2224 | ||
| 2225 | lemma dropWhile_last: | |
| 2226 | "x \<in> set xs \<Longrightarrow> \<not> P x \<Longrightarrow> last (dropWhile P xs) = last xs" | |
| 2227 | by (auto simp add: dropWhile_append3 in_set_conv_decomp) | |
| 2228 | ||
| 2229 | lemma set_dropWhileD: "x \<in> set (dropWhile P xs) \<Longrightarrow> x \<in> set xs" | |
| 2230 | by (induct xs) (auto split: split_if_asm) | |
| 2231 | ||
| 23971 
e6d505d5b03d
renamed lemma "set_take_whileD" to "set_takeWhileD"
 krauss parents: 
23740diff
changeset | 2232 | lemma set_takeWhileD: "x : set (takeWhile P xs) ==> x : set xs \<and> P x" | 
| 13145 | 2233 | by (induct xs) (auto split: split_if_asm) | 
| 13114 | 2234 | |
| 13913 | 2235 | lemma takeWhile_eq_all_conv[simp]: | 
| 2236 | "(takeWhile P xs = xs) = (\<forall>x \<in> set xs. P x)" | |
| 2237 | by(induct xs, auto) | |
| 2238 | ||
| 2239 | lemma dropWhile_eq_Nil_conv[simp]: | |
| 2240 | "(dropWhile P xs = []) = (\<forall>x \<in> set xs. P x)" | |
| 2241 | by(induct xs, auto) | |
| 2242 | ||
| 2243 | lemma dropWhile_eq_Cons_conv: | |
| 2244 | "(dropWhile P xs = y#ys) = (xs = takeWhile P xs @ y # ys & \<not> P y)" | |
| 2245 | by(induct xs, auto) | |
| 2246 | ||
| 31077 | 2247 | lemma distinct_takeWhile[simp]: "distinct xs ==> distinct (takeWhile P xs)" | 
| 2248 | by (induct xs) (auto dest: set_takeWhileD) | |
| 2249 | ||
| 2250 | lemma distinct_dropWhile[simp]: "distinct xs ==> distinct (dropWhile P xs)" | |
| 2251 | by (induct xs) auto | |
| 2252 | ||
| 33639 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 2253 | 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: 
33593diff
changeset | 2254 | 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: 
33593diff
changeset | 2255 | |
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 2256 | 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: 
33593diff
changeset | 2257 | 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: 
33593diff
changeset | 2258 | |
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 2259 | 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: 
33593diff
changeset | 2260 | 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: 
33593diff
changeset | 2261 | |
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 2262 | 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: 
33593diff
changeset | 2263 | 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: 
33593diff
changeset | 2264 | |
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 2265 | lemma hd_dropWhile: | 
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 2266 | "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: 
33593diff
changeset | 2267 | 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: 
33593diff
changeset | 2268 | |
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 2269 | 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: 
33593diff
changeset | 2270 | 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: 
33593diff
changeset | 2271 | 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: 
33593diff
changeset | 2272 | proof - | 
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 2273 | 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: 
33593diff
changeset | 2274 | by simp | 
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 2275 | 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: 
33593diff
changeset | 2276 | 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: 
33593diff
changeset | 2277 | 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: 
33593diff
changeset | 2278 | 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: 
33593diff
changeset | 2279 | 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: 
33593diff
changeset | 2280 | thus ?thesis .. | 
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 2281 | qed | 
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 2282 | |
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 2283 | 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: 
33593diff
changeset | 2284 | "\<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: 
33593diff
changeset | 2285 | 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: 
33593diff
changeset | 2286 | 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: 
33593diff
changeset | 2287 | 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: 
33593diff
changeset | 2288 | thus ?case | 
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 2289 | proof (cases n) | 
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 2290 | 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: 
33593diff
changeset | 2291 | 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: 
33593diff
changeset | 2292 | 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: 
33593diff
changeset | 2293 | 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: 
33593diff
changeset | 2294 | 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: 
33593diff
changeset | 2295 | 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: 
33593diff
changeset | 2296 | qed | 
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 2297 | 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: 
33593diff
changeset | 2298 | qed simp | 
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 2299 | qed simp | 
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 2300 | |
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 2301 | 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: 
33593diff
changeset | 2302 | "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: 
33593diff
changeset | 2303 | 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: 
33593diff
changeset | 2304 | |
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 2305 | 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: 
33593diff
changeset | 2306 | 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: 
33593diff
changeset | 2307 | 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: 
33593diff
changeset | 2308 | proof (rule classical) | 
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 2309 | assume "\<not> ?thesis" | 
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 2310 | 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: 
33593diff
changeset | 2311 | 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: 
33593diff
changeset | 2312 | qed | 
| 31077 | 2313 | |
| 17501 | 2314 | text{* The following two lemmmas could be generalized to an arbitrary
 | 
| 2315 | property. *} | |
| 2316 | ||
| 2317 | lemma takeWhile_neq_rev: "\<lbrakk>distinct xs; x \<in> set xs\<rbrakk> \<Longrightarrow> | |
| 2318 | takeWhile (\<lambda>y. y \<noteq> x) (rev xs) = rev (tl (dropWhile (\<lambda>y. y \<noteq> x) xs))" | |
| 2319 | by(induct xs) (auto simp: takeWhile_tail[where l="[]"]) | |
| 2320 | ||
| 2321 | lemma dropWhile_neq_rev: "\<lbrakk>distinct xs; x \<in> set xs\<rbrakk> \<Longrightarrow> | |
| 2322 | dropWhile (\<lambda>y. y \<noteq> x) (rev xs) = x # rev (takeWhile (\<lambda>y. y \<noteq> x) xs)" | |
| 2323 | apply(induct xs) | |
| 2324 | apply simp | |
| 2325 | apply auto | |
| 2326 | apply(subst dropWhile_append2) | |
| 2327 | apply auto | |
| 2328 | done | |
| 2329 | ||
| 18423 | 2330 | lemma takeWhile_not_last: | 
| 46500 
0196966d6d2d
removing unnecessary premises in theorems of List theory
 bulwahn parents: 
46448diff
changeset | 2331 | "distinct xs \<Longrightarrow> takeWhile (\<lambda>y. y \<noteq> last xs) xs = butlast xs" | 
| 18423 | 2332 | apply(induct xs) | 
| 2333 | apply simp | |
| 2334 | apply(case_tac xs) | |
| 2335 | apply(auto) | |
| 2336 | done | |
| 2337 | ||
| 44013 
5cfc1c36ae97
moved recdef package to HOL/Library/Old_Recdef.thy
 krauss parents: 
43594diff
changeset | 2338 | lemma takeWhile_cong [fundef_cong]: | 
| 18336 
1a2e30b37ed3
Added recdef congruence rules for bounded quantifiers and commonly used
 krauss parents: 
18049diff
changeset | 2339 | "[| l = k; !!x. x : set l ==> P x = Q x |] | 
| 
1a2e30b37ed3
Added recdef congruence rules for bounded quantifiers and commonly used
 krauss parents: 
18049diff
changeset | 2340 | ==> takeWhile P l = takeWhile Q k" | 
| 24349 | 2341 | by (induct k arbitrary: l) (simp_all) | 
| 18336 
1a2e30b37ed3
Added recdef congruence rules for bounded quantifiers and commonly used
 krauss parents: 
18049diff
changeset | 2342 | |
| 44013 
5cfc1c36ae97
moved recdef package to HOL/Library/Old_Recdef.thy
 krauss parents: 
43594diff
changeset | 2343 | lemma dropWhile_cong [fundef_cong]: | 
| 18336 
1a2e30b37ed3
Added recdef congruence rules for bounded quantifiers and commonly used
 krauss parents: 
18049diff
changeset | 2344 | "[| l = k; !!x. x : set l ==> P x = Q x |] | 
| 
1a2e30b37ed3
Added recdef congruence rules for bounded quantifiers and commonly used
 krauss parents: 
18049diff
changeset | 2345 | ==> dropWhile P l = dropWhile Q k" | 
| 24349 | 2346 | by (induct k arbitrary: l, simp_all) | 
| 18336 
1a2e30b37ed3
Added recdef congruence rules for bounded quantifiers and commonly used
 krauss parents: 
18049diff
changeset | 2347 | |
| 52380 | 2348 | lemma takeWhile_idem [simp]: | 
| 2349 | "takeWhile P (takeWhile P xs) = takeWhile P xs" | |
| 2350 | by (induct xs) auto | |
| 2351 | ||
| 2352 | lemma dropWhile_idem [simp]: | |
| 2353 | "dropWhile P (dropWhile P xs) = dropWhile P xs" | |
| 2354 | by (induct xs) auto | |
| 2355 | ||
| 13114 | 2356 | |
| 49948 
744934b818c7
moved quite generic material from theory Enum to more appropriate places
 haftmann parents: 
49808diff
changeset | 2357 | subsubsection {* @{const zip} *}
 | 
| 13114 | 2358 | |
| 13142 | 2359 | lemma zip_Nil [simp]: "zip [] ys = []" | 
| 13145 | 2360 | by (induct ys) auto | 
| 13114 | 2361 | |
| 13142 | 2362 | lemma zip_Cons_Cons [simp]: "zip (x # xs) (y # ys) = (x, y) # zip xs ys" | 
| 13145 | 2363 | by simp | 
| 13114 | 2364 | |
| 13142 | 2365 | declare zip_Cons [simp del] | 
| 13114 | 2366 | |
| 36198 | 2367 | lemma [code]: | 
| 2368 | "zip [] ys = []" | |
| 2369 | "zip xs [] = []" | |
| 2370 | "zip (x # xs) (y # ys) = (x, y) # zip xs ys" | |
| 2371 | by (fact zip_Nil zip.simps(1) zip_Cons_Cons)+ | |
| 2372 | ||
| 15281 | 2373 | lemma zip_Cons1: | 
| 2374 | "zip (x#xs) ys = (case ys of [] \<Rightarrow> [] | y#ys \<Rightarrow> (x,y)#zip xs ys)" | |
| 2375 | by(auto split:list.split) | |
| 2376 | ||
| 13142 | 2377 | lemma length_zip [simp]: | 
| 22493 
db930e490fe5
added another rule for simultaneous induction, and lemmas for zip
 krauss parents: 
22422diff
changeset | 2378 | "length (zip xs ys) = min (length xs) (length ys)" | 
| 
db930e490fe5
added another rule for simultaneous induction, and lemmas for zip
 krauss parents: 
22422diff
changeset | 2379 | by (induct xs ys rule:list_induct2') auto | 
| 13114 | 2380 | |
| 34978 
874150ddd50a
canonical insert operation; generalized lemma foldl_apply_inv to foldl_apply
 haftmann parents: 
34942diff
changeset | 2381 | lemma zip_obtain_same_length: | 
| 
874150ddd50a
canonical insert operation; generalized lemma foldl_apply_inv to foldl_apply
 haftmann parents: 
34942diff
changeset | 2382 | 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: 
34942diff
changeset | 2383 | \<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: 
34942diff
changeset | 2384 | shows "P (zip xs ys)" | 
| 
874150ddd50a
canonical insert operation; generalized lemma foldl_apply_inv to foldl_apply
 haftmann parents: 
34942diff
changeset | 2385 | proof - | 
| 
874150ddd50a
canonical insert operation; generalized lemma foldl_apply_inv to foldl_apply
 haftmann parents: 
34942diff
changeset | 2386 | let ?n = "min (length xs) (length ys)" | 
| 
874150ddd50a
canonical insert operation; generalized lemma foldl_apply_inv to foldl_apply
 haftmann parents: 
34942diff
changeset | 2387 | have "P (zip (take ?n xs) (take ?n ys))" | 
| 
874150ddd50a
canonical insert operation; generalized lemma foldl_apply_inv to foldl_apply
 haftmann parents: 
34942diff
changeset | 2388 | by (rule assms) simp_all | 
| 
874150ddd50a
canonical insert operation; generalized lemma foldl_apply_inv to foldl_apply
 haftmann parents: 
34942diff
changeset | 2389 | 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: 
34942diff
changeset | 2390 | proof (induct xs arbitrary: ys) | 
| 
874150ddd50a
canonical insert operation; generalized lemma foldl_apply_inv to foldl_apply
 haftmann parents: 
34942diff
changeset | 2391 | case Nil then show ?case by simp | 
| 
874150ddd50a
canonical insert operation; generalized lemma foldl_apply_inv to foldl_apply
 haftmann parents: 
34942diff
changeset | 2392 | next | 
| 
874150ddd50a
canonical insert operation; generalized lemma foldl_apply_inv to foldl_apply
 haftmann parents: 
34942diff
changeset | 2393 | 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: 
34942diff
changeset | 2394 | qed | 
| 
874150ddd50a
canonical insert operation; generalized lemma foldl_apply_inv to foldl_apply
 haftmann parents: 
34942diff
changeset | 2395 | ultimately show ?thesis by simp | 
| 
874150ddd50a
canonical insert operation; generalized lemma foldl_apply_inv to foldl_apply
 haftmann parents: 
34942diff
changeset | 2396 | qed | 
| 
874150ddd50a
canonical insert operation; generalized lemma foldl_apply_inv to foldl_apply
 haftmann parents: 
34942diff
changeset | 2397 | |
| 13114 | 2398 | lemma zip_append1: | 
| 22493 
db930e490fe5
added another rule for simultaneous induction, and lemmas for zip
 krauss parents: 
22422diff
changeset | 2399 | "zip (xs @ ys) zs = | 
| 13145 | 2400 | 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: 
22422diff
changeset | 2401 | by (induct xs zs rule:list_induct2') auto | 
| 13114 | 2402 | |
| 2403 | lemma zip_append2: | |
| 22493 
db930e490fe5
added another rule for simultaneous induction, and lemmas for zip
 krauss parents: 
22422diff
changeset | 2404 | "zip xs (ys @ zs) = | 
| 13145 | 2405 | 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: 
22422diff
changeset | 2406 | by (induct xs ys rule:list_induct2') auto | 
| 13114 | 2407 | |
| 13142 | 2408 | lemma zip_append [simp]: | 
| 46500 
0196966d6d2d
removing unnecessary premises in theorems of List theory
 bulwahn parents: 
46448diff
changeset | 2409 | "[| length xs = length us |] ==> | 
| 13145 | 2410 | zip (xs@ys) (us@vs) = zip xs us @ zip ys vs" | 
| 2411 | by (simp add: zip_append1) | |
| 13114 | 2412 | |
| 2413 | lemma zip_rev: | |
| 14247 | 2414 | "length xs = length ys ==> zip (rev xs) (rev ys) = rev (zip xs ys)" | 
| 2415 | by (induct rule:list_induct2, simp_all) | |
| 13114 | 2416 | |
| 33639 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 2417 | 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: 
33593diff
changeset | 2418 | "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: 
33593diff
changeset | 2419 | 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: 
33593diff
changeset | 2420 | 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: 
33593diff
changeset | 2421 | show ?case | 
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 2422 | proof (cases ys) | 
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 2423 | 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: 
33593diff
changeset | 2424 | 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: 
33593diff
changeset | 2425 | qed simp | 
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 2426 | qed simp | 
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 2427 | |
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 2428 | lemma zip_map1: | 
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 2429 | "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: 
33593diff
changeset | 2430 | 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: 
33593diff
changeset | 2431 | |
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 2432 | lemma zip_map2: | 
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 2433 | "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: 
33593diff
changeset | 2434 | 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: 
33593diff
changeset | 2435 | |
| 23096 | 2436 | 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: 
33593diff
changeset | 2437 | "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: 
33593diff
changeset | 2438 | unfolding zip_map1 by auto | 
| 23096 | 2439 | |
| 2440 | 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: 
33593diff
changeset | 2441 | "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: 
33593diff
changeset | 2442 | unfolding zip_map2 by auto | 
| 23096 | 2443 | |
| 31080 | 2444 | text{* Courtesy of Andreas Lochbihler: *}
 | 
| 2445 | lemma zip_same_conv_map: "zip xs xs = map (\<lambda>x. (x, x)) xs" | |
| 2446 | by(induct xs) auto | |
| 2447 | ||
| 13142 | 2448 | lemma nth_zip [simp]: | 
| 24526 | 2449 | "[| i < length xs; i < length ys|] ==> (zip xs ys)!i = (xs!i, ys!i)" | 
| 2450 | apply (induct ys arbitrary: i xs, simp) | |
| 13145 | 2451 | apply (case_tac xs) | 
| 2452 | apply (simp_all add: nth.simps split: nat.split) | |
| 2453 | done | |
| 13114 | 2454 | |
| 2455 | lemma set_zip: | |
| 13145 | 2456 | "set (zip xs ys) = {(xs!i, ys!i) | i. i < min (length xs) (length ys)}"
 | 
| 31080 | 2457 | by(simp add: set_conv_nth cong: rev_conj_cong) | 
| 13114 | 2458 | |
| 33639 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 2459 | 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: 
33593diff
changeset | 2460 | 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: 
33593diff
changeset | 2461 | |
| 13114 | 2462 | lemma zip_update: | 
| 31080 | 2463 | "zip (xs[i:=x]) (ys[i:=y]) = (zip xs ys)[i:=(x,y)]" | 
| 2464 | by(rule sym, simp add: update_zip) | |
| 13114 | 2465 | |
| 13142 | 2466 | lemma zip_replicate [simp]: | 
| 24526 | 2467 | "zip (replicate i x) (replicate j y) = replicate (min i j) (x,y)" | 
| 2468 | apply (induct i arbitrary: j, auto) | |
| 14208 | 2469 | apply (case_tac j, auto) | 
| 13145 | 2470 | done | 
| 13114 | 2471 | |
| 19487 | 2472 | lemma take_zip: | 
| 24526 | 2473 | "take n (zip xs ys) = zip (take n xs) (take n ys)" | 
| 2474 | apply (induct n arbitrary: xs ys) | |
| 19487 | 2475 | apply simp | 
| 2476 | apply (case_tac xs, simp) | |
| 2477 | apply (case_tac ys, simp_all) | |
| 2478 | done | |
| 2479 | ||
| 2480 | lemma drop_zip: | |
| 24526 | 2481 | "drop n (zip xs ys) = zip (drop n xs) (drop n ys)" | 
| 2482 | apply (induct n arbitrary: xs ys) | |
| 19487 | 2483 | apply simp | 
| 2484 | apply (case_tac xs, simp) | |
| 2485 | apply (case_tac ys, simp_all) | |
| 2486 | done | |
| 2487 | ||
| 33639 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 2488 | 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: 
33593diff
changeset | 2489 | 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: 
33593diff
changeset | 2490 | 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: 
33593diff
changeset | 2491 | qed simp | 
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 2492 | |
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 2493 | 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: 
33593diff
changeset | 2494 | 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: 
33593diff
changeset | 2495 | 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: 
33593diff
changeset | 2496 | qed simp | 
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 2497 | |
| 22493 
db930e490fe5
added another rule for simultaneous induction, and lemmas for zip
 krauss parents: 
22422diff
changeset | 2498 | lemma set_zip_leftD: | 
| 
db930e490fe5
added another rule for simultaneous induction, and lemmas for zip
 krauss parents: 
22422diff
changeset | 2499 | "(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: 
22422diff
changeset | 2500 | by (induct xs ys rule:list_induct2') auto | 
| 
db930e490fe5
added another rule for simultaneous induction, and lemmas for zip
 krauss parents: 
22422diff
changeset | 2501 | |
| 
db930e490fe5
added another rule for simultaneous induction, and lemmas for zip
 krauss parents: 
22422diff
changeset | 2502 | lemma set_zip_rightD: | 
| 
db930e490fe5
added another rule for simultaneous induction, and lemmas for zip
 krauss parents: 
22422diff
changeset | 2503 | "(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: 
22422diff
changeset | 2504 | by (induct xs ys rule:list_induct2') auto | 
| 13142 | 2505 | |
| 23983 | 2506 | lemma in_set_zipE: | 
| 2507 | "(x,y) : set(zip xs ys) \<Longrightarrow> (\<lbrakk> x : set xs; y : set ys \<rbrakk> \<Longrightarrow> R) \<Longrightarrow> R" | |
| 2508 | by(blast dest: set_zip_leftD set_zip_rightD) | |
| 2509 | ||
| 29829 | 2510 | lemma zip_map_fst_snd: | 
| 2511 | "zip (map fst zs) (map snd zs) = zs" | |
| 2512 | by (induct zs) simp_all | |
| 2513 | ||
| 2514 | lemma zip_eq_conv: | |
| 2515 | "length xs = length ys \<Longrightarrow> zip xs ys = zs \<longleftrightarrow> map fst zs = xs \<and> map snd zs = ys" | |
| 2516 | by (auto simp add: zip_map_fst_snd) | |
| 2517 | ||
| 51173 | 2518 | lemma in_set_zip: | 
| 2519 | "p \<in> set (zip xs ys) \<longleftrightarrow> (\<exists>n. xs ! n = fst p \<and> ys ! n = snd p | |
| 2520 | \<and> n < length xs \<and> n < length ys)" | |
| 2521 | by (cases p) (auto simp add: set_zip) | |
| 2522 | ||
| 2523 | lemma pair_list_eqI: | |
| 2524 | assumes "map fst xs = map fst ys" and "map snd xs = map snd ys" | |
| 2525 | shows "xs = ys" | |
| 2526 | proof - | |
| 2527 | from assms(1) have "length xs = length ys" by (rule map_eq_imp_length_eq) | |
| 2528 | from this assms show ?thesis | |
| 2529 | by (induct xs ys rule: list_induct2) (simp_all add: prod_eqI) | |
| 2530 | qed | |
| 2531 | ||
| 35115 | 2532 | |
| 49948 
744934b818c7
moved quite generic material from theory Enum to more appropriate places
 haftmann parents: 
49808diff
changeset | 2533 | subsubsection {* @{const list_all2} *}
 | 
| 13114 | 2534 | |
| 14316 
91b897b9a2dc
added some [intro?] and [trans] for list_all2 lemmas
 kleing parents: 
14302diff
changeset | 2535 | lemma list_all2_lengthD [intro?]: | 
| 
91b897b9a2dc
added some [intro?] and [trans] for list_all2 lemmas
 kleing parents: 
14302diff
changeset | 2536 | "list_all2 P xs ys ==> length xs = length ys" | 
| 55524 
f41ef840f09d
folded 'list_all2' with the relator generated by 'datatype_new'
 blanchet parents: 
55473diff
changeset | 2537 | by (simp add: list_all2_iff) | 
| 19607 
07eeb832f28d
introduced characters for code generator; some improved code lemmas for some list functions
 haftmann parents: 
19585diff
changeset | 2538 | |
| 19787 | 2539 | 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: 
55473diff
changeset | 2540 | by (simp add: list_all2_iff) | 
| 19607 
07eeb832f28d
introduced characters for code generator; some improved code lemmas for some list functions
 haftmann parents: 
19585diff
changeset | 2541 | |
| 19787 | 2542 | 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: 
55473diff
changeset | 2543 | by (simp add: list_all2_iff) | 
| 19607 
07eeb832f28d
introduced characters for code generator; some improved code lemmas for some list functions
 haftmann parents: 
19585diff
changeset | 2544 | |
| 
07eeb832f28d
introduced characters for code generator; some improved code lemmas for some list functions
 haftmann parents: 
19585diff
changeset | 2545 | lemma list_all2_Cons [iff, code]: | 
| 
07eeb832f28d
introduced characters for code generator; some improved code lemmas for some list functions
 haftmann parents: 
19585diff
changeset | 2546 | "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: 
55473diff
changeset | 2547 | by (auto simp add: list_all2_iff) | 
| 13114 | 2548 | |
| 2549 | lemma list_all2_Cons1: | |
| 13145 | 2550 | "list_all2 P (x # xs) ys = (\<exists>z zs. ys = z # zs \<and> P x z \<and> list_all2 P xs zs)" | 
| 2551 | by (cases ys) auto | |
| 13114 | 2552 | |
| 2553 | lemma list_all2_Cons2: | |
| 13145 | 2554 | "list_all2 P xs (y # ys) = (\<exists>z zs. xs = z # zs \<and> P z y \<and> list_all2 P zs ys)" | 
| 2555 | by (cases xs) auto | |
| 13114 | 2556 | |
| 45794 | 2557 | lemma list_all2_induct | 
| 2558 | [consumes 1, case_names Nil Cons, induct set: list_all2]: | |
| 2559 | assumes P: "list_all2 P xs ys" | |
| 2560 | assumes Nil: "R [] []" | |
| 47640 | 2561 | assumes Cons: "\<And>x xs y ys. | 
| 2562 | \<lbrakk>P x y; list_all2 P xs ys; R xs ys\<rbrakk> \<Longrightarrow> R (x # xs) (y # ys)" | |
| 45794 | 2563 | shows "R xs ys" | 
| 2564 | using P | |
| 2565 | by (induct xs arbitrary: ys) (auto simp add: list_all2_Cons1 Nil Cons) | |
| 2566 | ||
| 13142 | 2567 | lemma list_all2_rev [iff]: | 
| 13145 | 2568 | "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: 
55473diff
changeset | 2569 | by (simp add: list_all2_iff zip_rev cong: conj_cong) | 
| 13114 | 2570 | |
| 13863 | 2571 | lemma list_all2_rev1: | 
| 2572 | "list_all2 P (rev xs) ys = list_all2 P xs (rev ys)" | |
| 2573 | by (subst list_all2_rev [symmetric]) simp | |
| 2574 | ||
| 13114 | 2575 | lemma list_all2_append1: | 
| 13145 | 2576 | "list_all2 P (xs @ ys) zs = | 
| 2577 | (EX us vs. zs = us @ vs \<and> length us = length xs \<and> length vs = length ys \<and> | |
| 2578 | 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: 
55473diff
changeset | 2579 | apply (simp add: list_all2_iff zip_append1) | 
| 13145 | 2580 | apply (rule iffI) | 
| 2581 | apply (rule_tac x = "take (length xs) zs" in exI) | |
| 2582 | apply (rule_tac x = "drop (length xs) zs" in exI) | |
| 14208 | 2583 | apply (force split: nat_diff_split simp add: min_def, clarify) | 
| 13145 | 2584 | apply (simp add: ball_Un) | 
| 2585 | done | |
| 13114 | 2586 | |
| 2587 | lemma list_all2_append2: | |
| 13145 | 2588 | "list_all2 P xs (ys @ zs) = | 
| 2589 | (EX us vs. xs = us @ vs \<and> length us = length ys \<and> length vs = length zs \<and> | |
| 2590 | 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: 
55473diff
changeset | 2591 | apply (simp add: list_all2_iff zip_append2) | 
| 13145 | 2592 | apply (rule iffI) | 
| 2593 | apply (rule_tac x = "take (length ys) xs" in exI) | |
| 2594 | apply (rule_tac x = "drop (length ys) xs" in exI) | |
| 14208 | 2595 | apply (force split: nat_diff_split simp add: min_def, clarify) | 
| 13145 | 2596 | apply (simp add: ball_Un) | 
| 2597 | done | |
| 13114 | 2598 | |
| 13863 | 2599 | lemma list_all2_append: | 
| 14247 | 2600 | "length xs = length ys \<Longrightarrow> | 
| 2601 | list_all2 P (xs@us) (ys@vs) = (list_all2 P xs ys \<and> list_all2 P us vs)" | |
| 2602 | by (induct rule:list_induct2, simp_all) | |
| 13863 | 2603 | |
| 2604 | lemma list_all2_appendI [intro?, trans]: | |
| 2605 | "\<lbrakk> list_all2 P a b; list_all2 P c d \<rbrakk> \<Longrightarrow> list_all2 P (a@c) (b@d)" | |
| 24349 | 2606 | by (simp add: list_all2_append list_all2_lengthD) | 
| 13863 | 2607 | |
| 13114 | 2608 | lemma list_all2_conv_all_nth: | 
| 13145 | 2609 | "list_all2 P xs ys = | 
| 2610 | (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: 
55473diff
changeset | 2611 | by (force simp add: list_all2_iff set_zip) | 
| 13114 | 2612 | |
| 13883 
0451e0fb3f22
Re-structured some proofs in order to get rid of rule_format attribute.
 berghofe parents: 
13863diff
changeset | 2613 | lemma list_all2_trans: | 
| 
0451e0fb3f22
Re-structured some proofs in order to get rid of rule_format attribute.
 berghofe parents: 
13863diff
changeset | 2614 | 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: 
13863diff
changeset | 2615 | 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: 
13863diff
changeset | 2616 | (is "!!bs cs. PROP ?Q as bs cs") | 
| 
0451e0fb3f22
Re-structured some proofs in order to get rid of rule_format attribute.
 berghofe parents: 
13863diff
changeset | 2617 | proof (induct as) | 
| 
0451e0fb3f22
Re-structured some proofs in order to get rid of rule_format attribute.
 berghofe parents: 
13863diff
changeset | 2618 | 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: 
13863diff
changeset | 2619 | show "!!cs. PROP ?Q (x # xs) bs cs" | 
| 
0451e0fb3f22
Re-structured some proofs in order to get rid of rule_format attribute.
 berghofe parents: 
13863diff
changeset | 2620 | proof (induct bs) | 
| 
0451e0fb3f22
Re-structured some proofs in order to get rid of rule_format attribute.
 berghofe parents: 
13863diff
changeset | 2621 | 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: 
13863diff
changeset | 2622 | show "PROP ?Q (x # xs) (y # ys) cs" | 
| 
0451e0fb3f22
Re-structured some proofs in order to get rid of rule_format attribute.
 berghofe parents: 
13863diff
changeset | 2623 | by (induct cs) (auto intro: tr I1 I2) | 
| 
0451e0fb3f22
Re-structured some proofs in order to get rid of rule_format attribute.
 berghofe parents: 
13863diff
changeset | 2624 | qed simp | 
| 
0451e0fb3f22
Re-structured some proofs in order to get rid of rule_format attribute.
 berghofe parents: 
13863diff
changeset | 2625 | qed simp | 
| 
0451e0fb3f22
Re-structured some proofs in order to get rid of rule_format attribute.
 berghofe parents: 
13863diff
changeset | 2626 | |
| 13863 | 2627 | lemma list_all2_all_nthI [intro?]: | 
| 2628 | "length a = length b \<Longrightarrow> (\<And>n. n < length a \<Longrightarrow> P (a!n) (b!n)) \<Longrightarrow> list_all2 P a b" | |
| 24349 | 2629 | by (simp add: list_all2_conv_all_nth) | 
| 13863 | 2630 | |
| 14395 | 2631 | lemma list_all2I: | 
| 2632 | "\<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: 
55473diff
changeset | 2633 | by (simp add: list_all2_iff) | 
| 14395 | 2634 | |
| 14328 | 2635 | lemma list_all2_nthD: | 
| 13863 | 2636 | "\<lbrakk> list_all2 P xs ys; p < size xs \<rbrakk> \<Longrightarrow> P (xs!p) (ys!p)" | 
| 24349 | 2637 | by (simp add: list_all2_conv_all_nth) | 
| 13863 | 2638 | |
| 14302 | 2639 | lemma list_all2_nthD2: | 
| 2640 | "\<lbrakk>list_all2 P xs ys; p < size ys\<rbrakk> \<Longrightarrow> P (xs!p) (ys!p)" | |
| 24349 | 2641 | by (frule list_all2_lengthD) (auto intro: list_all2_nthD) | 
| 14302 | 2642 | |
| 13863 | 2643 | lemma list_all2_map1: | 
| 2644 | "list_all2 P (map f as) bs = list_all2 (\<lambda>x y. P (f x) y) as bs" | |
| 24349 | 2645 | by (simp add: list_all2_conv_all_nth) | 
| 13863 | 2646 | |
| 2647 | lemma list_all2_map2: | |
| 2648 | "list_all2 P as (map f bs) = list_all2 (\<lambda>x y. P x (f y)) as bs" | |
| 24349 | 2649 | by (auto simp add: list_all2_conv_all_nth) | 
| 13863 | 2650 | |
| 14316 
91b897b9a2dc
added some [intro?] and [trans] for list_all2 lemmas
 kleing parents: 
14302diff
changeset | 2651 | lemma list_all2_refl [intro?]: | 
| 13863 | 2652 | "(\<And>x. P x x) \<Longrightarrow> list_all2 P xs xs" | 
| 24349 | 2653 | by (simp add: list_all2_conv_all_nth) | 
| 13863 | 2654 | |
| 2655 | lemma list_all2_update_cong: | |
| 46669 
c1d2ab32174a
one general list_all2_update_cong instead of two special ones
 bulwahn parents: 
46664diff
changeset | 2656 | "\<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: 
46664diff
changeset | 2657 | by (cases "i < length ys") (auto simp add: list_all2_conv_all_nth nth_list_update) | 
| 13863 | 2658 | |
| 14302 | 2659 | lemma list_all2_takeI [simp,intro?]: | 
| 24526 | 2660 | "list_all2 P xs ys \<Longrightarrow> list_all2 P (take n xs) (take n ys)" | 
| 2661 | apply (induct xs arbitrary: n ys) | |
| 2662 | apply simp | |
| 2663 | apply (clarsimp simp add: list_all2_Cons1) | |
| 2664 | apply (case_tac n) | |
| 2665 | apply auto | |
| 2666 | done | |
| 14302 | 2667 | |
| 2668 | lemma list_all2_dropI [simp,intro?]: | |
| 24526 | 2669 | "list_all2 P as bs \<Longrightarrow> list_all2 P (drop n as) (drop n bs)" | 
| 2670 | apply (induct as arbitrary: n bs, simp) | |
| 2671 | apply (clarsimp simp add: list_all2_Cons1) | |
| 2672 | apply (case_tac n, simp, simp) | |
| 2673 | done | |
| 13863 | 2674 | |
| 14327 | 2675 | lemma list_all2_mono [intro?]: | 
| 24526 | 2676 | "list_all2 P xs ys \<Longrightarrow> (\<And>xs ys. P xs ys \<Longrightarrow> Q xs ys) \<Longrightarrow> list_all2 Q xs ys" | 
| 2677 | apply (induct xs arbitrary: ys, simp) | |
| 2678 | apply (case_tac ys, auto) | |
| 2679 | done | |
| 13863 | 2680 | |
| 22551 | 2681 | lemma list_all2_eq: | 
| 2682 | "xs = ys \<longleftrightarrow> list_all2 (op =) xs ys" | |
| 24349 | 2683 | by (induct xs ys rule: list_induct2') auto | 
| 22551 | 2684 | |
| 40230 | 2685 | lemma list_eq_iff_zip_eq: | 
| 2686 | "xs = ys \<longleftrightarrow> length xs = length ys \<and> (\<forall>(x,y) \<in> set (zip xs ys). x = y)" | |
| 2687 | by(auto simp add: set_zip list_all2_eq list_all2_conv_all_nth cong: conj_cong) | |
| 2688 | ||
| 13142 | 2689 | |
| 53721 
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
 traytel parents: 
53689diff
changeset | 2690 | subsubsection {* @{const List.product} and @{const product_lists} *}
 | 
| 49948 
744934b818c7
moved quite generic material from theory Enum to more appropriate places
 haftmann parents: 
49808diff
changeset | 2691 | |
| 
744934b818c7
moved quite generic material from theory Enum to more appropriate places
 haftmann parents: 
49808diff
changeset | 2692 | lemma product_list_set: | 
| 
744934b818c7
moved quite generic material from theory Enum to more appropriate places
 haftmann parents: 
49808diff
changeset | 2693 | "set (List.product xs ys) = set xs \<times> set ys" | 
| 
744934b818c7
moved quite generic material from theory Enum to more appropriate places
 haftmann parents: 
49808diff
changeset | 2694 | by (induct xs) auto | 
| 
744934b818c7
moved quite generic material from theory Enum to more appropriate places
 haftmann parents: 
49808diff
changeset | 2695 | |
| 51160 
599ff65b85e2
systematic conversions between nat and nibble/char;
 haftmann parents: 
51112diff
changeset | 2696 | lemma length_product [simp]: | 
| 
599ff65b85e2
systematic conversions between nat and nibble/char;
 haftmann parents: 
51112diff
changeset | 2697 | "length (List.product xs ys) = length xs * length ys" | 
| 
599ff65b85e2
systematic conversions between nat and nibble/char;
 haftmann parents: 
51112diff
changeset | 2698 | by (induct xs) simp_all | 
| 
599ff65b85e2
systematic conversions between nat and nibble/char;
 haftmann parents: 
51112diff
changeset | 2699 | |
| 
599ff65b85e2
systematic conversions between nat and nibble/char;
 haftmann parents: 
51112diff
changeset | 2700 | lemma product_nth: | 
| 
599ff65b85e2
systematic conversions between nat and nibble/char;
 haftmann parents: 
51112diff
changeset | 2701 | assumes "n < length xs * length ys" | 
| 
599ff65b85e2
systematic conversions between nat and nibble/char;
 haftmann parents: 
51112diff
changeset | 2702 | 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: 
51112diff
changeset | 2703 | using assms proof (induct xs arbitrary: n) | 
| 
599ff65b85e2
systematic conversions between nat and nibble/char;
 haftmann parents: 
51112diff
changeset | 2704 | case Nil then show ?case by simp | 
| 
599ff65b85e2
systematic conversions between nat and nibble/char;
 haftmann parents: 
51112diff
changeset | 2705 | next | 
| 
599ff65b85e2
systematic conversions between nat and nibble/char;
 haftmann parents: 
51112diff
changeset | 2706 | case (Cons x xs n) | 
| 
599ff65b85e2
systematic conversions between nat and nibble/char;
 haftmann parents: 
51112diff
changeset | 2707 | then have "length ys > 0" by auto | 
| 
599ff65b85e2
systematic conversions between nat and nibble/char;
 haftmann parents: 
51112diff
changeset | 2708 | with Cons show ?case | 
| 
599ff65b85e2
systematic conversions between nat and nibble/char;
 haftmann parents: 
51112diff
changeset | 2709 | by (auto simp add: nth_append not_less le_mod_geq le_div_geq) | 
| 
599ff65b85e2
systematic conversions between nat and nibble/char;
 haftmann parents: 
51112diff
changeset | 2710 | qed | 
| 
599ff65b85e2
systematic conversions between nat and nibble/char;
 haftmann parents: 
51112diff
changeset | 2711 | |
| 53721 
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
 traytel parents: 
53689diff
changeset | 2712 | lemma in_set_product_lists_length: | 
| 
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
 traytel parents: 
53689diff
changeset | 2713 | "xs \<in> set (product_lists xss) \<Longrightarrow> length xs = length xss" | 
| 
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
 traytel parents: 
53689diff
changeset | 2714 | by (induct xss arbitrary: xs) auto | 
| 
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
 traytel parents: 
53689diff
changeset | 2715 | |
| 
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
 traytel parents: 
53689diff
changeset | 2716 | lemma product_lists_set: | 
| 
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
 traytel parents: 
53689diff
changeset | 2717 |   "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: 
53689diff
changeset | 2718 | proof (intro equalityI subsetI, unfold mem_Collect_eq) | 
| 
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
 traytel parents: 
53689diff
changeset | 2719 | fix xs assume "xs \<in> ?L" | 
| 
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
 traytel parents: 
53689diff
changeset | 2720 | 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: 
53689diff
changeset | 2721 | 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: 
53689diff
changeset | 2722 | next | 
| 
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
 traytel parents: 
53689diff
changeset | 2723 | fix xs assume "?R xs" | 
| 
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
 traytel parents: 
53689diff
changeset | 2724 | then show "xs \<in> ?L" by induct auto | 
| 
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
 traytel parents: 
53689diff
changeset | 2725 | qed | 
| 
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
 traytel parents: 
53689diff
changeset | 2726 | |
| 49948 
744934b818c7
moved quite generic material from theory Enum to more appropriate places
 haftmann parents: 
49808diff
changeset | 2727 | |
| 47397 
d654c73e4b12
no preference wrt. fold(l/r); prefer fold rather than foldr for iterating over lists in generated code
 haftmann parents: 
47131diff
changeset | 2728 | 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: 
46125diff
changeset | 2729 | |
| 48828 
441a4eed7823
prefer eta-expanded code equations for fold, to accomodate tail recursion optimisation in Scala
 haftmann parents: 
48619diff
changeset | 2730 | 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: 
48619diff
changeset | 2731 | "fold f [] s = s" | 
| 
441a4eed7823
prefer eta-expanded code equations for fold, to accomodate tail recursion optimisation in Scala
 haftmann parents: 
48619diff
changeset | 2732 | "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: 
48619diff
changeset | 2733 | by simp_all | 
| 
441a4eed7823
prefer eta-expanded code equations for fold, to accomodate tail recursion optimisation in Scala
 haftmann parents: 
48619diff
changeset | 2734 | |
| 46133 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 haftmann parents: 
46125diff
changeset | 2735 | 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: 
46125diff
changeset | 2736 | 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: 
46125diff
changeset | 2737 | 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: 
46125diff
changeset | 2738 | shows "fold f xs = fold f (remove1 x xs) \<circ> f x" | 
| 49739 | 2739 | 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: 
46125diff
changeset | 2740 | |
| 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 haftmann parents: 
46125diff
changeset | 2741 | 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: 
46125diff
changeset | 2742 | "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: 
46125diff
changeset | 2743 | \<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: 
46125diff
changeset | 2744 | 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: 
46125diff
changeset | 2745 | |
| 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 haftmann parents: 
46125diff
changeset | 2746 | lemma fold_id: | 
| 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 haftmann parents: 
46125diff
changeset | 2747 | 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: 
46125diff
changeset | 2748 | 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: 
46125diff
changeset | 2749 | 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: 
46125diff
changeset | 2750 | |
| 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 haftmann parents: 
46125diff
changeset | 2751 | lemma fold_commute: | 
| 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 haftmann parents: 
46125diff
changeset | 2752 | 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: 
46125diff
changeset | 2753 | 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: 
46125diff
changeset | 2754 | 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: 
46125diff
changeset | 2755 | |
| 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 haftmann parents: 
46125diff
changeset | 2756 | 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: 
46125diff
changeset | 2757 | 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: 
46125diff
changeset | 2758 | 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: 
46125diff
changeset | 2759 | proof - | 
| 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 haftmann parents: 
46125diff
changeset | 2760 | 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: 
46125diff
changeset | 2761 | 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: 
37465diff
changeset | 2762 | qed | 
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 2763 | |
| 46133 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 haftmann parents: 
46125diff
changeset | 2764 | lemma fold_invariant: | 
| 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 haftmann parents: 
46125diff
changeset | 2765 | 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: 
46125diff
changeset | 2766 | 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: 
46125diff
changeset | 2767 | shows "P (fold f xs s)" | 
| 34978 
874150ddd50a
canonical insert operation; generalized lemma foldl_apply_inv to foldl_apply
 haftmann parents: 
34942diff
changeset | 2768 | using assms by (induct xs arbitrary: s) simp_all | 
| 
874150ddd50a
canonical insert operation; generalized lemma foldl_apply_inv to foldl_apply
 haftmann parents: 
34942diff
changeset | 2769 | |
| 46133 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 haftmann parents: 
46125diff
changeset | 2770 | 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: 
46125diff
changeset | 2771 | "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: 
46125diff
changeset | 2772 | 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: 
46125diff
changeset | 2773 | |
| 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 haftmann parents: 
46125diff
changeset | 2774 | 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: 
46125diff
changeset | 2775 | "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: 
46125diff
changeset | 2776 | 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: 
46125diff
changeset | 2777 | |
| 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 haftmann parents: 
46125diff
changeset | 2778 | lemma fold_rev: | 
| 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 haftmann parents: 
46125diff
changeset | 2779 | 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: 
46125diff
changeset | 2780 | 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: 
46125diff
changeset | 2781 | 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: 
46125diff
changeset | 2782 | |
| 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 haftmann parents: 
46125diff
changeset | 2783 | 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: 
46125diff
changeset | 2784 | "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: 
46125diff
changeset | 2785 | 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: 
46125diff
changeset | 2786 | |
| 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 haftmann parents: 
46125diff
changeset | 2787 | 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: 
46125diff
changeset | 2788 | "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: 
46125diff
changeset | 2789 | 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: 
46125diff
changeset | 2790 | |
| 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 haftmann parents: 
46125diff
changeset | 2791 | 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: 
46125diff
changeset | 2792 | "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: 
46125diff
changeset | 2793 | 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: 
46125diff
changeset | 2794 | |
| 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 haftmann parents: 
46125diff
changeset | 2795 | 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: 
46125diff
changeset | 2796 | |
| 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 haftmann parents: 
46125diff
changeset | 2797 | 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: 
46125diff
changeset | 2798 | "Finite_Set.fold f y (set xs) = fold f (remdups xs) y" | 
| 51489 | 2799 | by (rule sym, induct xs arbitrary: y) (simp_all add: fold_fun_left_comm insert_absorb) | 
| 48619 | 2800 | |
| 46133 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 haftmann parents: 
46125diff
changeset | 2801 | 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: 
46125diff
changeset | 2802 | "Finite_Set.fold f y (set xs) = fold f xs y" | 
| 51489 | 2803 | 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: 
32422diff
changeset | 2804 | |
| 47397 
d654c73e4b12
no preference wrt. fold(l/r); prefer fold rather than foldr for iterating over lists in generated code
 haftmann parents: 
47131diff
changeset | 2805 | lemma union_set_fold [code]: | 
| 46147 
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
 haftmann parents: 
46143diff
changeset | 2806 | "set xs \<union> A = fold Set.insert xs A" | 
| 
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
 haftmann parents: 
46143diff
changeset | 2807 | proof - | 
| 
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
 haftmann parents: 
46143diff
changeset | 2808 | interpret comp_fun_idem Set.insert | 
| 
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
 haftmann parents: 
46143diff
changeset | 2809 | by (fact comp_fun_idem_insert) | 
| 
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
 haftmann parents: 
46143diff
changeset | 2810 | 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: 
46143diff
changeset | 2811 | qed | 
| 
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
 haftmann parents: 
46143diff
changeset | 2812 | |
| 47397 
d654c73e4b12
no preference wrt. fold(l/r); prefer fold rather than foldr for iterating over lists in generated code
 haftmann parents: 
47131diff
changeset | 2813 | 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: 
47131diff
changeset | 2814 | "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: 
47131diff
changeset | 2815 | by auto | 
| 
d654c73e4b12
no preference wrt. fold(l/r); prefer fold rather than foldr for iterating over lists in generated code
 haftmann parents: 
47131diff
changeset | 2816 | |
| 
d654c73e4b12
no preference wrt. fold(l/r); prefer fold rather than foldr for iterating over lists in generated code
 haftmann parents: 
47131diff
changeset | 2817 | lemma minus_set_fold [code]: | 
| 46147 
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
 haftmann parents: 
46143diff
changeset | 2818 | "A - set xs = fold Set.remove xs A" | 
| 
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
 haftmann parents: 
46143diff
changeset | 2819 | proof - | 
| 
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
 haftmann parents: 
46143diff
changeset | 2820 | interpret comp_fun_idem Set.remove | 
| 
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
 haftmann parents: 
46143diff
changeset | 2821 | by (fact comp_fun_idem_remove) | 
| 
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
 haftmann parents: 
46143diff
changeset | 2822 | show ?thesis | 
| 
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
 haftmann parents: 
46143diff
changeset | 2823 | 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: 
46143diff
changeset | 2824 | qed | 
| 
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
 haftmann parents: 
46143diff
changeset | 2825 | |
| 47397 
d654c73e4b12
no preference wrt. fold(l/r); prefer fold rather than foldr for iterating over lists in generated code
 haftmann parents: 
47131diff
changeset | 2826 | 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: 
47131diff
changeset | 2827 | "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: 
47131diff
changeset | 2828 | by auto | 
| 
d654c73e4b12
no preference wrt. fold(l/r); prefer fold rather than foldr for iterating over lists in generated code
 haftmann parents: 
47131diff
changeset | 2829 | |
| 
d654c73e4b12
no preference wrt. fold(l/r); prefer fold rather than foldr for iterating over lists in generated code
 haftmann parents: 
47131diff
changeset | 2830 | 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: 
47131diff
changeset | 2831 | "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: 
47131diff
changeset | 2832 | by auto | 
| 
d654c73e4b12
no preference wrt. fold(l/r); prefer fold rather than foldr for iterating over lists in generated code
 haftmann parents: 
47131diff
changeset | 2833 | |
| 
d654c73e4b12
no preference wrt. fold(l/r); prefer fold rather than foldr for iterating over lists in generated code
 haftmann parents: 
47131diff
changeset | 2834 | 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: 
47131diff
changeset | 2835 | "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: 
47131diff
changeset | 2836 | 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: 
47131diff
changeset | 2837 | |
| 54885 | 2838 | lemma (in semilattice_set) set_eq_fold [code]: | 
| 51489 | 2839 | "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: 
46125diff
changeset | 2840 | proof - | 
| 51489 | 2841 | interpret comp_fun_idem f | 
| 2842 | by default (simp_all add: fun_eq_iff left_commute) | |
| 2843 | 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: 
46125diff
changeset | 2844 | qed | 
| 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 haftmann parents: 
46125diff
changeset | 2845 | |
| 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 haftmann parents: 
46125diff
changeset | 2846 | 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: 
46125diff
changeset | 2847 | "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: 
46125diff
changeset | 2848 | proof - | 
| 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 haftmann parents: 
46125diff
changeset | 2849 | 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: 
46125diff
changeset | 2850 | 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: 
46125diff
changeset | 2851 | 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: 
46125diff
changeset | 2852 | qed | 
| 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 haftmann parents: 
46125diff
changeset | 2853 | |
| 47397 
d654c73e4b12
no preference wrt. fold(l/r); prefer fold rather than foldr for iterating over lists in generated code
 haftmann parents: 
47131diff
changeset | 2854 | 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: 
47131diff
changeset | 2855 | |
| 46133 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 haftmann parents: 
46125diff
changeset | 2856 | 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: 
46125diff
changeset | 2857 | "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: 
46125diff
changeset | 2858 | proof - | 
| 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 haftmann parents: 
46125diff
changeset | 2859 | 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: 
46125diff
changeset | 2860 | 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: 
46125diff
changeset | 2861 | 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: 
46125diff
changeset | 2862 | qed | 
| 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 haftmann parents: 
46125diff
changeset | 2863 | |
| 47397 
d654c73e4b12
no preference wrt. fold(l/r); prefer fold rather than foldr for iterating over lists in generated code
 haftmann parents: 
47131diff
changeset | 2864 | 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: 
47131diff
changeset | 2865 | |
| 46133 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 haftmann parents: 
46125diff
changeset | 2866 | 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: 
46125diff
changeset | 2867 | "INFI (set xs) f = fold (inf \<circ> f) xs top" | 
| 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 haftmann parents: 
46125diff
changeset | 2868 | unfolding INF_def set_map [symmetric] Inf_set_fold fold_map .. | 
| 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 haftmann parents: 
46125diff
changeset | 2869 | |
| 47397 
d654c73e4b12
no preference wrt. fold(l/r); prefer fold rather than foldr for iterating over lists in generated code
 haftmann parents: 
47131diff
changeset | 2870 | 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: 
47131diff
changeset | 2871 | |
| 46133 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 haftmann parents: 
46125diff
changeset | 2872 | 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: 
46125diff
changeset | 2873 | "SUPR (set xs) f = fold (sup \<circ> f) xs bot" | 
| 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 haftmann parents: 
46125diff
changeset | 2874 | unfolding SUP_def set_map [symmetric] Sup_set_fold fold_map .. | 
| 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 haftmann parents: 
46125diff
changeset | 2875 | |
| 47397 
d654c73e4b12
no preference wrt. fold(l/r); prefer fold rather than foldr for iterating over lists in generated code
 haftmann parents: 
47131diff
changeset | 2876 | 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: 
46125diff
changeset | 2877 | |
| 49948 
744934b818c7
moved quite generic material from theory Enum to more appropriate places
 haftmann parents: 
49808diff
changeset | 2878 | |
| 46133 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 haftmann parents: 
46125diff
changeset | 2879 | 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: 
46125diff
changeset | 2880 | |
| 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 haftmann parents: 
46125diff
changeset | 2881 | text {* Correspondence *}
 | 
| 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 haftmann parents: 
46125diff
changeset | 2882 | |
| 47397 
d654c73e4b12
no preference wrt. fold(l/r); prefer fold rather than foldr for iterating over lists in generated code
 haftmann parents: 
47131diff
changeset | 2883 | 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: 
47131diff
changeset | 2884 | "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: 
47131diff
changeset | 2885 | 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: 
47131diff
changeset | 2886 | |
| 
d654c73e4b12
no preference wrt. fold(l/r); prefer fold rather than foldr for iterating over lists in generated code
 haftmann parents: 
47131diff
changeset | 2887 | 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: 
47131diff
changeset | 2888 | "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: 
47131diff
changeset | 2889 | 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: 
47131diff
changeset | 2890 | |
| 
d654c73e4b12
no preference wrt. fold(l/r); prefer fold rather than foldr for iterating over lists in generated code
 haftmann parents: 
47131diff
changeset | 2891 | 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: 
46125diff
changeset | 2892 | "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: 
47131diff
changeset | 2893 | 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: 
47131diff
changeset | 2894 | |
| 
d654c73e4b12
no preference wrt. fold(l/r); prefer fold rather than foldr for iterating over lists in generated code
 haftmann parents: 
47131diff
changeset | 2895 | 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: 
46125diff
changeset | 2896 | "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: 
47131diff
changeset | 2897 | 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: 
46125diff
changeset | 2898 | |
| 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 haftmann parents: 
46125diff
changeset | 2899 | lemma foldr_fold: | 
| 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 haftmann parents: 
46125diff
changeset | 2900 | 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: 
46125diff
changeset | 2901 | 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: 
47131diff
changeset | 2902 | 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: 
46125diff
changeset | 2903 | |
| 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 haftmann parents: 
46125diff
changeset | 2904 | 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: 
46125diff
changeset | 2905 | "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: 
47131diff
changeset | 2906 | 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: 
46125diff
changeset | 2907 | |
| 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 haftmann parents: 
46125diff
changeset | 2908 | 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: 
46125diff
changeset | 2909 | "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: 
47131diff
changeset | 2910 | 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: 
46125diff
changeset | 2911 | |
| 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 haftmann parents: 
46125diff
changeset | 2912 | 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: 
46125diff
changeset | 2913 | "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: 
47131diff
changeset | 2914 | 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: 
46125diff
changeset | 2915 | |
| 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 haftmann parents: 
46125diff
changeset | 2916 | 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: 
46125diff
changeset | 2917 | "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: 
47131diff
changeset | 2918 | 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: 
46125diff
changeset | 2919 | |
| 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 haftmann parents: 
46125diff
changeset | 2920 | 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: 
46125diff
changeset | 2921 | "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: 
47131diff
changeset | 2922 | 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: 
46125diff
changeset | 2923 | |
| 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 haftmann parents: 
46125diff
changeset | 2924 | 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: 
46125diff
changeset | 2925 | "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: 
47131diff
changeset | 2926 | 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: 
46125diff
changeset | 2927 | |
| 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 haftmann parents: 
46125diff
changeset | 2928 | 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: 
46125diff
changeset | 2929 | "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: 
47131diff
changeset | 2930 | 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: 
46125diff
changeset | 2931 | |
| 35115 | 2932 | |
| 49948 
744934b818c7
moved quite generic material from theory Enum to more appropriate places
 haftmann parents: 
49808diff
changeset | 2933 | subsubsection {* @{const upt} *}
 | 
| 13114 | 2934 | |
| 17090 | 2935 | lemma upt_rec[code]: "[i..<j] = (if i<j then i#[Suc i..<j] else [])" | 
| 2936 | -- {* simp does not terminate! *}
 | |
| 13145 | 2937 | by (induct j) auto | 
| 13142 | 2938 | |
| 47108 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
 huffman parents: 
46898diff
changeset | 2939 | lemmas upt_rec_numeral[simp] = upt_rec[of "numeral m" "numeral n"] for m n | 
| 32005 | 2940 | |
| 15425 | 2941 | lemma upt_conv_Nil [simp]: "j <= i ==> [i..<j] = []" | 
| 13145 | 2942 | by (subst upt_rec) simp | 
| 13114 | 2943 | |
| 15425 | 2944 | lemma upt_eq_Nil_conv[simp]: "([i..<j] = []) = (j = 0 \<or> j <= i)" | 
| 15281 | 2945 | by(induct j)simp_all | 
| 2946 | ||
| 2947 | lemma upt_eq_Cons_conv: | |
| 24526 | 2948 | "([i..<j] = x#xs) = (i < j & i = x & [i+1..<j] = xs)" | 
| 2949 | apply(induct j arbitrary: x xs) | |
| 15281 | 2950 | apply simp | 
| 2951 | apply(clarsimp simp add: append_eq_Cons_conv) | |
| 2952 | apply arith | |
| 2953 | done | |
| 2954 | ||
| 15425 | 2955 | lemma upt_Suc_append: "i <= j ==> [i..<(Suc j)] = [i..<j]@[j]" | 
| 13145 | 2956 | -- {* Only needed if @{text upt_Suc} is deleted from the simpset. *}
 | 
| 2957 | by simp | |
| 13114 | 2958 | |
| 15425 | 2959 | lemma upt_conv_Cons: "i < j ==> [i..<j] = i # [Suc i..<j]" | 
| 26734 | 2960 | by (simp add: upt_rec) | 
| 13114 | 2961 | |
| 15425 | 2962 | lemma upt_add_eq_append: "i<=j ==> [i..<j+k] = [i..<j]@[j..<j+k]" | 
| 13145 | 2963 | -- {* LOOPS as a simprule, since @{text "j <= j"}. *}
 | 
| 2964 | by (induct k) auto | |
| 13114 | 2965 | |
| 15425 | 2966 | lemma length_upt [simp]: "length [i..<j] = j - i" | 
| 13145 | 2967 | by (induct j) (auto simp add: Suc_diff_le) | 
| 13114 | 2968 | |
| 15425 | 2969 | lemma nth_upt [simp]: "i + k < j ==> [i..<j] ! k = i + k" | 
| 13145 | 2970 | apply (induct j) | 
| 2971 | apply (auto simp add: less_Suc_eq nth_append split: nat_diff_split) | |
| 2972 | done | |
| 13114 | 2973 | |
| 17906 | 2974 | |
| 2975 | lemma hd_upt[simp]: "i < j \<Longrightarrow> hd[i..<j] = i" | |
| 2976 | by(simp add:upt_conv_Cons) | |
| 2977 | ||
| 2978 | lemma last_upt[simp]: "i < j \<Longrightarrow> last[i..<j] = j - 1" | |
| 2979 | apply(cases j) | |
| 2980 | apply simp | |
| 2981 | by(simp add:upt_Suc_append) | |
| 2982 | ||
| 24526 | 2983 | lemma take_upt [simp]: "i+m <= n ==> take m [i..<n] = [i..<i+m]" | 
| 2984 | apply (induct m arbitrary: i, simp) | |
| 13145 | 2985 | apply (subst upt_rec) | 
| 2986 | apply (rule sym) | |
| 2987 | apply (subst upt_rec) | |
| 2988 | apply (simp del: upt.simps) | |
| 2989 | done | |
| 3507 | 2990 | |
| 17501 | 2991 | lemma drop_upt[simp]: "drop m [i..<j] = [i+m..<j]" | 
| 2992 | apply(induct j) | |
| 2993 | apply auto | |
| 2994 | done | |
| 2995 | ||
| 24645 | 2996 | lemma map_Suc_upt: "map Suc [m..<n] = [Suc m..<Suc n]" | 
| 13145 | 2997 | by (induct n) auto | 
| 13114 | 2998 | |
| 54496 
178922b63b58
add lemmas Suc_funpow and id_funpow to simpset; add lemma map_add_upt
 hoelzl parents: 
54404diff
changeset | 2999 | 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: 
54404diff
changeset | 3000 | by (induct m) simp_all | 
| 
178922b63b58
add lemmas Suc_funpow and id_funpow to simpset; add lemma map_add_upt
 hoelzl parents: 
54404diff
changeset | 3001 | |
| 24526 | 3002 | lemma nth_map_upt: "i < n-m ==> (map f [m..<n]) ! i = f(m+i)" | 
| 3003 | apply (induct n m arbitrary: i rule: diff_induct) | |
| 13145 | 3004 | prefer 3 apply (subst map_Suc_upt[symmetric]) | 
| 44921 | 3005 | apply (auto simp add: less_diff_conv) | 
| 13145 | 3006 | done | 
| 13114 | 3007 | |
| 52380 | 3008 | lemma map_decr_upt: | 
| 3009 | "map (\<lambda>n. n - Suc 0) [Suc m..<Suc n] = [m..<n]" | |
| 3010 | by (induct n) simp_all | |
| 3011 | ||
| 13883 
0451e0fb3f22
Re-structured some proofs in order to get rid of rule_format attribute.
 berghofe parents: 
13863diff
changeset | 3012 | lemma nth_take_lemma: | 
| 24526 | 3013 | "k <= length xs ==> k <= length ys ==> | 
| 13883 
0451e0fb3f22
Re-structured some proofs in order to get rid of rule_format attribute.
 berghofe parents: 
13863diff
changeset | 3014 | (!!i. i < k --> xs!i = ys!i) ==> take k xs = take k ys" | 
| 24526 | 3015 | apply (atomize, induct k arbitrary: xs ys) | 
| 14208 | 3016 | apply (simp_all add: less_Suc_eq_0_disj all_conj_distrib, clarify) | 
| 13145 | 3017 | txt {* Both lists must be non-empty *}
 | 
| 14208 | 3018 | apply (case_tac xs, simp) | 
| 3019 | apply (case_tac ys, clarify) | |
| 13145 | 3020 | apply (simp (no_asm_use)) | 
| 3021 | apply clarify | |
| 3022 | txt {* prenexing's needed, not miniscoping *}
 | |
| 3023 | apply (simp (no_asm_use) add: all_simps [symmetric] del: all_simps) | |
| 3024 | apply blast | |
| 3025 | done | |
| 13114 | 3026 | |
| 3027 | lemma nth_equalityI: | |
| 3028 | "[| length xs = length ys; ALL i < length xs. xs!i = ys!i |] ==> xs = ys" | |
| 44921 | 3029 | by (frule nth_take_lemma [OF le_refl eq_imp_le]) simp_all | 
| 13142 | 3030 | |
| 24796 | 3031 | lemma map_nth: | 
| 3032 | "map (\<lambda>i. xs ! i) [0..<length xs] = xs" | |
| 3033 | by (rule nth_equalityI, auto) | |
| 3034 | ||
| 13863 | 3035 | (* needs nth_equalityI *) | 
| 3036 | lemma list_all2_antisym: | |
| 3037 | "\<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> | |
| 3038 | \<Longrightarrow> xs = ys" | |
| 3039 | apply (simp add: list_all2_conv_all_nth) | |
| 14208 | 3040 | apply (rule nth_equalityI, blast, simp) | 
| 13863 | 3041 | done | 
| 3042 | ||
| 13142 | 3043 | lemma take_equalityI: "(\<forall>i. take i xs = take i ys) ==> xs = ys" | 
| 13145 | 3044 | -- {* The famous take-lemma. *}
 | 
| 3045 | apply (drule_tac x = "max (length xs) (length ys)" in spec) | |
| 44921 | 3046 | apply (simp add: le_max_iff_disj) | 
| 13145 | 3047 | done | 
| 13142 | 3048 | |
| 3049 | ||
| 15302 | 3050 | lemma take_Cons': | 
| 3051 | "take n (x # xs) = (if n = 0 then [] else x # take (n - 1) xs)" | |
| 3052 | by (cases n) simp_all | |
| 3053 | ||
| 3054 | lemma drop_Cons': | |
| 3055 | "drop n (x # xs) = (if n = 0 then x # xs else drop (n - 1) xs)" | |
| 3056 | by (cases n) simp_all | |
| 3057 | ||
| 3058 | lemma nth_Cons': "(x # xs)!n = (if n = 0 then x else xs!(n - 1))" | |
| 3059 | by (cases n) simp_all | |
| 3060 | ||
| 47108 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
 huffman parents: 
46898diff
changeset | 3061 | lemma take_Cons_numeral [simp]: | 
| 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
 huffman parents: 
46898diff
changeset | 3062 | "take (numeral v) (x # xs) = x # take (numeral v - 1) xs" | 
| 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
 huffman parents: 
46898diff
changeset | 3063 | by (simp add: take_Cons') | 
| 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
 huffman parents: 
46898diff
changeset | 3064 | |
| 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
 huffman parents: 
46898diff
changeset | 3065 | lemma drop_Cons_numeral [simp]: | 
| 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
 huffman parents: 
46898diff
changeset | 3066 | "drop (numeral v) (x # xs) = drop (numeral v - 1) xs" | 
| 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
 huffman parents: 
46898diff
changeset | 3067 | by (simp add: drop_Cons') | 
| 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
 huffman parents: 
46898diff
changeset | 3068 | |
| 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
 huffman parents: 
46898diff
changeset | 3069 | lemma nth_Cons_numeral [simp]: | 
| 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
 huffman parents: 
46898diff
changeset | 3070 | "(x # xs) ! numeral v = xs ! (numeral v - 1)" | 
| 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
 huffman parents: 
46898diff
changeset | 3071 | by (simp add: nth_Cons') | 
| 15302 | 3072 | |
| 3073 | ||
| 32415 
1dddf2f64266
got rid of complicated class finite_intvl_succ and defined "upto" directly on int, the only instance of the class.
 nipkow parents: 
32078diff
changeset | 3074 | 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: 
32078diff
changeset | 3075 | |
| 
1dddf2f64266
got rid of complicated class finite_intvl_succ and defined "upto" directly on int, the only instance of the class.
 nipkow parents: 
32078diff
changeset | 3076 | function upto :: "int \<Rightarrow> int \<Rightarrow> int list" ("(1[_../_])") where
 | 
| 51166 | 3077 | "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: 
32078diff
changeset | 3078 | 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: 
32078diff
changeset | 3079 | termination | 
| 
1dddf2f64266
got rid of complicated class finite_intvl_succ and defined "upto" directly on int, the only instance of the class.
 nipkow parents: 
32078diff
changeset | 3080 | 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: 
32078diff
changeset | 3081 | |
| 51166 | 3082 | 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: 
32078diff
changeset | 3083 | |
| 47108 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
 huffman parents: 
46898diff
changeset | 3084 | lemmas upto_rec_numeral [simp] = | 
| 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
 huffman parents: 
46898diff
changeset | 3085 | upto.simps[of "numeral m" "numeral n"] | 
| 54489 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 haftmann parents: 
54404diff
changeset | 3086 | upto.simps[of "numeral m" "- numeral n"] | 
| 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 haftmann parents: 
54404diff
changeset | 3087 | upto.simps[of "- numeral m" "numeral n"] | 
| 
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
 haftmann parents: 
54404diff
changeset | 3088 | 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: 
32078diff
changeset | 3089 | |
| 
1dddf2f64266
got rid of complicated class finite_intvl_succ and defined "upto" directly on int, the only instance of the class.
 nipkow parents: 
32078diff
changeset | 3090 | 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: 
32078diff
changeset | 3091 | 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: 
32078diff
changeset | 3092 | |
| 51166 | 3093 | lemma upto_rec1: "i \<le> j \<Longrightarrow> [i..j] = i#[i+1..j]" | 
| 3094 | by(simp add: upto.simps) | |
| 3095 | ||
| 3096 | lemma upto_rec2: "i \<le> j \<Longrightarrow> [i..j] = [i..j - 1]@[j]" | |
| 3097 | proof(induct "nat(j-i)" arbitrary: i j) | |
| 3098 | case 0 thus ?case by(simp add: upto.simps) | |
| 3099 | next | |
| 3100 | case (Suc n) | |
| 3101 | hence "n = nat (j - (i + 1))" "i < j" by linarith+ | |
| 3102 | from this(2) Suc.hyps(1)[OF this(1)] Suc(2,3) upto_rec1 show ?case by simp | |
| 3103 | qed | |
| 3104 | ||
| 32415 
1dddf2f64266
got rid of complicated class finite_intvl_succ and defined "upto" directly on int, the only instance of the class.
 nipkow parents: 
32078diff
changeset | 3105 | lemma set_upto[simp]: "set[i..j] = {i..j}"
 | 
| 41463 
edbf0a86fb1c
adding simproc to rewrite list comprehensions to set comprehensions; adopting proofs
 bulwahn parents: 
41372diff
changeset | 3106 | proof(induct i j rule:upto.induct) | 
| 
edbf0a86fb1c
adding simproc to rewrite list comprehensions to set comprehensions; adopting proofs
 bulwahn parents: 
41372diff
changeset | 3107 | case (1 i j) | 
| 
edbf0a86fb1c
adding simproc to rewrite list comprehensions to set comprehensions; adopting proofs
 bulwahn parents: 
41372diff
changeset | 3108 | from this show ?case | 
| 
edbf0a86fb1c
adding simproc to rewrite list comprehensions to set comprehensions; adopting proofs
 bulwahn parents: 
41372diff
changeset | 3109 | unfolding upto.simps[of i j] simp_from_to[of i j] by auto | 
| 
edbf0a86fb1c
adding simproc to rewrite list comprehensions to set comprehensions; adopting proofs
 bulwahn parents: 
41372diff
changeset | 3110 | 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: 
32078diff
changeset | 3111 | |
| 51166 | 3112 | text{* Tail recursive version for code generation: *}
 | 
| 3113 | ||
| 51170 | 3114 | definition upto_aux :: "int \<Rightarrow> int \<Rightarrow> int list \<Rightarrow> int list" where | 
| 3115 | "upto_aux i j js = [i..j] @ js" | |
| 3116 | ||
| 3117 | lemma upto_aux_rec [code]: | |
| 51166 | 3118 | "upto_aux i j js = (if j<i then js else upto_aux i (j - 1) (j#js))" | 
| 51170 | 3119 | by (simp add: upto_aux_def upto_rec2) | 
| 51166 | 3120 | |
| 3121 | lemma upto_code[code]: "[i..j] = upto_aux i j []" | |
| 51170 | 3122 | by(simp add: upto_aux_def) | 
| 51166 | 3123 | |
| 32415 
1dddf2f64266
got rid of complicated class finite_intvl_succ and defined "upto" directly on int, the only instance of the class.
 nipkow parents: 
32078diff
changeset | 3124 | |
| 53721 
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
 traytel parents: 
53689diff
changeset | 3125 | subsubsection {* @{const distinct} and @{const remdups} and @{const remdups_adj} *}
 | 
| 13142 | 3126 | |
| 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: 
40195diff
changeset | 3127 | 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: 
40195diff
changeset | 3128 | "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: 
40195diff
changeset | 3129 | 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: 
40195diff
changeset | 3130 | |
| 13142 | 3131 | lemma distinct_append [simp]: | 
| 13145 | 3132 | "distinct (xs @ ys) = (distinct xs \<and> distinct ys \<and> set xs \<inter> set ys = {})"
 | 
| 3133 | by (induct xs) auto | |
| 13142 | 3134 | |
| 15305 | 3135 | lemma distinct_rev[simp]: "distinct(rev xs) = distinct xs" | 
| 3136 | by(induct xs) auto | |
| 3137 | ||
| 13142 | 3138 | lemma set_remdups [simp]: "set (remdups xs) = set xs" | 
| 13145 | 3139 | by (induct xs) (auto simp add: insert_absorb) | 
| 13142 | 3140 | |
| 3141 | lemma distinct_remdups [iff]: "distinct (remdups xs)" | |
| 13145 | 3142 | by (induct xs) auto | 
| 13142 | 3143 | |
| 25287 | 3144 | lemma distinct_remdups_id: "distinct xs ==> remdups xs = xs" | 
| 3145 | by (induct xs, auto) | |
| 3146 | ||
| 26734 | 3147 | lemma remdups_id_iff_distinct [simp]: "remdups xs = xs \<longleftrightarrow> distinct xs" | 
| 3148 | by (metis distinct_remdups distinct_remdups_id) | |
| 25287 | 3149 | |
| 24566 | 3150 | lemma finite_distinct_list: "finite A \<Longrightarrow> EX xs. set xs = A & distinct xs" | 
| 24632 | 3151 | by (metis distinct_remdups finite_list set_remdups) | 
| 24566 | 3152 | |
| 15072 | 3153 | 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: 
46439diff
changeset | 3154 | by (induct x, auto) | 
| 15072 | 3155 | |
| 3156 | lemma remdups_eq_nil_right_iff [simp]: "([] = remdups x) = (x = [])" | |
| 24349 | 3157 | by (induct x, auto) | 
| 15072 | 3158 | |
| 15245 | 3159 | lemma length_remdups_leq[iff]: "length(remdups xs) <= length xs" | 
| 3160 | by (induct xs) auto | |
| 3161 | ||
| 3162 | lemma length_remdups_eq[iff]: | |
| 3163 | "(length (remdups xs) = length xs) = (remdups xs = xs)" | |
| 3164 | apply(induct xs) | |
| 3165 | apply auto | |
| 3166 | apply(subgoal_tac "length (remdups xs) <= length xs") | |
| 3167 | apply arith | |
| 3168 | apply(rule length_remdups_leq) | |
| 3169 | done | |
| 3170 | ||
| 33945 | 3171 | lemma remdups_filter: "remdups(filter P xs) = filter P (remdups xs)" | 
| 3172 | apply(induct xs) | |
| 3173 | apply auto | |
| 3174 | done | |
| 18490 | 3175 | |
| 3176 | lemma distinct_map: | |
| 3177 | "distinct(map f xs) = (distinct xs & inj_on f (set xs))" | |
| 3178 | by (induct xs) auto | |
| 3179 | ||
| 13142 | 3180 | lemma distinct_filter [simp]: "distinct xs ==> distinct (filter P xs)" | 
| 13145 | 3181 | by (induct xs) auto | 
| 13114 | 3182 | |
| 17501 | 3183 | lemma distinct_upt[simp]: "distinct[i..<j]" | 
| 3184 | by (induct j) auto | |
| 3185 | ||
| 32415 
1dddf2f64266
got rid of complicated class finite_intvl_succ and defined "upto" directly on int, the only instance of the class.
 nipkow parents: 
32078diff
changeset | 3186 | 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: 
32078diff
changeset | 3187 | 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: 
32078diff
changeset | 3188 | 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: 
32078diff
changeset | 3189 | 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: 
32078diff
changeset | 3190 | done | 
| 
1dddf2f64266
got rid of complicated class finite_intvl_succ and defined "upto" directly on int, the only instance of the class.
 nipkow parents: 
32078diff
changeset | 3191 | |
| 24526 | 3192 | lemma distinct_take[simp]: "distinct xs \<Longrightarrow> distinct (take i xs)" | 
| 3193 | apply(induct xs arbitrary: i) | |
| 17501 | 3194 | apply simp | 
| 3195 | apply (case_tac i) | |
| 3196 | apply simp_all | |
| 3197 | apply(blast dest:in_set_takeD) | |
| 3198 | done | |
| 3199 | ||
| 24526 | 3200 | lemma distinct_drop[simp]: "distinct xs \<Longrightarrow> distinct (drop i xs)" | 
| 3201 | apply(induct xs arbitrary: i) | |
| 17501 | 3202 | apply simp | 
| 3203 | apply (case_tac i) | |
| 3204 | apply simp_all | |
| 3205 | done | |
| 3206 | ||
| 3207 | lemma distinct_list_update: | |
| 3208 | assumes d: "distinct xs" and a: "a \<notin> set xs - {xs!i}"
 | |
| 3209 | shows "distinct (xs[i:=a])" | |
| 3210 | proof (cases "i < length xs") | |
| 3211 | case True | |
| 3212 |   with a have "a \<notin> set (take i xs @ xs ! i # drop (Suc i) xs) - {xs!i}"
 | |
| 3213 | apply (drule_tac id_take_nth_drop) by simp | |
| 3214 | with d True show ?thesis | |
| 3215 | apply (simp add: upd_conv_take_nth_drop) | |
| 3216 | apply (drule subst [OF id_take_nth_drop]) apply assumption | |
| 3217 | apply simp apply (cases "a = xs!i") apply simp by blast | |
| 3218 | next | |
| 3219 | case False with d show ?thesis by auto | |
| 3220 | qed | |
| 3221 | ||
| 31363 
7493b571b37d
Added theorems about distinct & concat, map & replicate and concat & replicate
 hoelzl parents: 
31264diff
changeset | 3222 | lemma distinct_concat: | 
| 
7493b571b37d
Added theorems about distinct & concat, map & replicate and concat & replicate
 hoelzl parents: 
31264diff
changeset | 3223 | assumes "distinct xs" | 
| 
7493b571b37d
Added theorems about distinct & concat, map & replicate and concat & replicate
 hoelzl parents: 
31264diff
changeset | 3224 | and "\<And> ys. ys \<in> set xs \<Longrightarrow> distinct ys" | 
| 
7493b571b37d
Added theorems about distinct & concat, map & replicate and concat & replicate
 hoelzl parents: 
31264diff
changeset | 3225 |   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: 
31264diff
changeset | 3226 | shows "distinct (concat xs)" | 
| 
7493b571b37d
Added theorems about distinct & concat, map & replicate and concat & replicate
 hoelzl parents: 
31264diff
changeset | 3227 | using assms by (induct xs) auto | 
| 17501 | 3228 | |
| 3229 | text {* It is best to avoid this indexed version of distinct, but
 | |
| 3230 | sometimes it is useful. *} | |
| 3231 | ||
| 13142 | 3232 | lemma distinct_conv_nth: | 
| 17501 | 3233 | "distinct xs = (\<forall>i < size xs. \<forall>j < size xs. i \<noteq> j --> xs!i \<noteq> xs!j)" | 
| 15251 | 3234 | apply (induct xs, simp, simp) | 
| 14208 | 3235 | apply (rule iffI, clarsimp) | 
| 13145 | 3236 | apply (case_tac i) | 
| 14208 | 3237 | apply (case_tac j, simp) | 
| 13145 | 3238 | apply (simp add: set_conv_nth) | 
| 3239 | 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: 
46439diff
changeset | 3240 | apply (clarsimp simp add: set_conv_nth, simp) | 
| 13145 | 3241 | apply (rule conjI) | 
| 24648 | 3242 | (*TOO SLOW | 
| 24632 | 3243 | apply (metis Zero_neq_Suc gr0_conv_Suc in_set_conv_nth lessI less_trans_Suc nth_Cons' nth_Cons_Suc) | 
| 24648 | 3244 | *) | 
| 3245 | apply (clarsimp simp add: set_conv_nth) | |
| 3246 | apply (erule_tac x = 0 in allE, simp) | |
| 3247 | apply (erule_tac x = "Suc i" in allE, simp, clarsimp) | |
| 25130 | 3248 | (*TOO SLOW | 
| 24632 | 3249 | apply (metis Suc_Suc_eq lessI less_trans_Suc nth_Cons_Suc) | 
| 25130 | 3250 | *) | 
| 3251 | apply (erule_tac x = "Suc i" in allE, simp) | |
| 3252 | apply (erule_tac x = "Suc j" in allE, simp) | |
| 13145 | 3253 | done | 
| 13114 | 3254 | |
| 18490 | 3255 | lemma nth_eq_iff_index_eq: | 
| 3256 | "\<lbrakk> distinct xs; i < length xs; j < length xs \<rbrakk> \<Longrightarrow> (xs!i = xs!j) = (i = j)" | |
| 3257 | by(auto simp: distinct_conv_nth) | |
| 3258 | ||
| 15110 
78b5636eabc7
Added a number of new thms and the new function remove1
 nipkow parents: 
15072diff
changeset | 3259 | lemma distinct_card: "distinct xs ==> card (set xs) = size xs" | 
| 24349 | 3260 | by (induct xs) auto | 
| 14388 | 3261 | |
| 15110 
78b5636eabc7
Added a number of new thms and the new function remove1
 nipkow parents: 
15072diff
changeset | 3262 | lemma card_distinct: "card (set xs) = size xs ==> distinct xs" | 
| 14388 | 3263 | proof (induct xs) | 
| 3264 | case Nil thus ?case by simp | |
| 3265 | next | |
| 3266 | case (Cons x xs) | |
| 3267 | show ?case | |
| 3268 | proof (cases "x \<in> set xs") | |
| 3269 | case False with Cons show ?thesis by simp | |
| 3270 | next | |
| 3271 | 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: 
46439diff
changeset | 3272 | have "card (set xs) = Suc (length xs)" | 
| 14388 | 3273 | by (simp add: card_insert_if split: split_if_asm) | 
| 3274 | moreover have "card (set xs) \<le> length xs" by (rule card_length) | |
| 3275 | ultimately have False by simp | |
| 3276 | thus ?thesis .. | |
| 3277 | qed | |
| 3278 | qed | |
| 3279 | ||
| 45115 
93c1ac6727a3
adding lemma to List library for executable equation of the (refl) transitive closure
 bulwahn parents: 
44928diff
changeset | 3280 | 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: 
44928diff
changeset | 3281 | by (induct xs) (auto) | 
| 
93c1ac6727a3
adding lemma to List library for executable equation of the (refl) transitive closure
 bulwahn parents: 
44928diff
changeset | 3282 | |
| 25287 | 3283 | lemma not_distinct_decomp: "~ distinct ws ==> EX xs ys zs y. ws = xs@[y]@ys@[y]@zs" | 
| 3284 | apply (induct n == "length ws" arbitrary:ws) apply simp | |
| 3285 | apply(case_tac ws) apply simp | |
| 3286 | apply (simp split:split_if_asm) | |
| 3287 | apply (metis Cons_eq_appendI eq_Nil_appendI split_list) | |
| 3288 | done | |
| 18490 | 3289 | |
| 45841 | 3290 | lemma not_distinct_conv_prefix: | 
| 3291 | defines "dec as xs y ys \<equiv> y \<in> set xs \<and> distinct xs \<and> as = xs @ y # ys" | |
| 3292 | shows "\<not>distinct as \<longleftrightarrow> (\<exists>xs y ys. dec as xs y ys)" (is "?L = ?R") | |
| 3293 | proof | |
| 3294 | assume "?L" then show "?R" | |
| 3295 | proof (induct "length as" arbitrary: as rule: less_induct) | |
| 3296 | case less | |
| 3297 | obtain xs ys zs y where decomp: "as = (xs @ y # ys) @ y # zs" | |
| 3298 | using not_distinct_decomp[OF less.prems] by auto | |
| 3299 | show ?case | |
| 3300 | proof (cases "distinct (xs @ y # ys)") | |
| 3301 | case True | |
| 3302 | with decomp have "dec as (xs @ y # ys) y zs" by (simp add: dec_def) | |
| 3303 | then show ?thesis by blast | |
| 3304 | next | |
| 3305 | case False | |
| 3306 | with less decomp obtain xs' y' ys' where "dec (xs @ y # ys) xs' y' ys'" | |
| 3307 | by atomize_elim auto | |
| 3308 | with decomp have "dec as xs' y' (ys' @ y # zs)" by (simp add: dec_def) | |
| 3309 | then show ?thesis by blast | |
| 3310 | qed | |
| 3311 | qed | |
| 3312 | qed (auto simp: dec_def) | |
| 3313 | ||
| 49948 
744934b818c7
moved quite generic material from theory Enum to more appropriate places
 haftmann parents: 
49808diff
changeset | 3314 | lemma distinct_product: | 
| 
744934b818c7
moved quite generic material from theory Enum to more appropriate places
 haftmann parents: 
49808diff
changeset | 3315 | assumes "distinct xs" and "distinct ys" | 
| 
744934b818c7
moved quite generic material from theory Enum to more appropriate places
 haftmann parents: 
49808diff
changeset | 3316 | shows "distinct (List.product xs ys)" | 
| 
744934b818c7
moved quite generic material from theory Enum to more appropriate places
 haftmann parents: 
49808diff
changeset | 3317 | using assms by (induct xs) | 
| 
744934b818c7
moved quite generic material from theory Enum to more appropriate places
 haftmann parents: 
49808diff
changeset | 3318 | (auto intro: inj_onI simp add: product_list_set distinct_map) | 
| 
744934b818c7
moved quite generic material from theory Enum to more appropriate places
 haftmann parents: 
49808diff
changeset | 3319 | |
| 53721 
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
 traytel parents: 
53689diff
changeset | 3320 | lemma distinct_product_lists: | 
| 
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
 traytel parents: 
53689diff
changeset | 3321 | assumes "\<forall>xs \<in> set xss. distinct xs" | 
| 
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
 traytel parents: 
53689diff
changeset | 3322 | shows "distinct (product_lists xss)" | 
| 
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
 traytel parents: 
53689diff
changeset | 3323 | using assms proof (induction xss) | 
| 
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
 traytel parents: 
53689diff
changeset | 3324 | case (Cons xs xss) note * = this | 
| 
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
 traytel parents: 
53689diff
changeset | 3325 | then show ?case | 
| 
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
 traytel parents: 
53689diff
changeset | 3326 | proof (cases "product_lists xss") | 
| 
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
 traytel parents: 
53689diff
changeset | 3327 | case Nil then show ?thesis by (induct xs) simp_all | 
| 
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
 traytel parents: 
53689diff
changeset | 3328 | next | 
| 
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
 traytel parents: 
53689diff
changeset | 3329 | case (Cons ps pss) with * show ?thesis | 
| 
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
 traytel parents: 
53689diff
changeset | 3330 | by (auto intro!: inj_onI distinct_concat simp add: distinct_map) | 
| 
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
 traytel parents: 
53689diff
changeset | 3331 | qed | 
| 
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
 traytel parents: 
53689diff
changeset | 3332 | qed simp | 
| 
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
 traytel parents: 
53689diff
changeset | 3333 | |
| 18490 | 3334 | lemma length_remdups_concat: | 
| 44921 | 3335 | "length (remdups (concat xss)) = card (\<Union>xs\<in>set xss. set xs)" | 
| 3336 | by (simp add: distinct_card [symmetric]) | |
| 17906 | 3337 | |
| 33639 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 3338 | 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: 
33593diff
changeset | 3339 | proof - | 
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 3340 | 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: 
33593diff
changeset | 3341 | 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: 
33593diff
changeset | 3342 | qed | 
| 17906 | 3343 | |
| 36275 | 3344 | lemma remdups_remdups: | 
| 3345 | "remdups (remdups xs) = remdups xs" | |
| 3346 | by (induct xs) simp_all | |
| 3347 | ||
| 36851 | 3348 | lemma distinct_butlast: | 
| 46500 
0196966d6d2d
removing unnecessary premises in theorems of List theory
 bulwahn parents: 
46448diff
changeset | 3349 | assumes "distinct xs" | 
| 36851 | 3350 | shows "distinct (butlast xs)" | 
| 46500 
0196966d6d2d
removing unnecessary premises in theorems of List theory
 bulwahn parents: 
46448diff
changeset | 3351 | proof (cases "xs = []") | 
| 
0196966d6d2d
removing unnecessary premises in theorems of List theory
 bulwahn parents: 
46448diff
changeset | 3352 | case False | 
| 
0196966d6d2d
removing unnecessary premises in theorems of List theory
 bulwahn parents: 
46448diff
changeset | 3353 | 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: 
46448diff
changeset | 3354 | with `distinct xs` show ?thesis by simp | 
| 
0196966d6d2d
removing unnecessary premises in theorems of List theory
 bulwahn parents: 
46448diff
changeset | 3355 | qed (auto) | 
| 36851 | 3356 | |
| 39728 | 3357 | lemma remdups_map_remdups: | 
| 3358 | "remdups (map f (remdups xs)) = remdups (map f xs)" | |
| 3359 | by (induct xs) simp_all | |
| 3360 | ||
| 39915 
ecf97cf3d248
turned distinct and sorted into inductive predicates: yields nice induction principles for free; more elegant proofs
 haftmann parents: 
39774diff
changeset | 3361 | lemma distinct_zipI1: | 
| 
ecf97cf3d248
turned distinct and sorted into inductive predicates: yields nice induction principles for free; more elegant proofs
 haftmann parents: 
39774diff
changeset | 3362 | assumes "distinct xs" | 
| 
ecf97cf3d248
turned distinct and sorted into inductive predicates: yields nice induction principles for free; more elegant proofs
 haftmann parents: 
39774diff
changeset | 3363 | shows "distinct (zip xs ys)" | 
| 
ecf97cf3d248
turned distinct and sorted into inductive predicates: yields nice induction principles for free; more elegant proofs
 haftmann parents: 
39774diff
changeset | 3364 | 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: 
39774diff
changeset | 3365 | 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: 
39774diff
changeset | 3366 | assume "length xs' = length ys'" | 
| 
ecf97cf3d248
turned distinct and sorted into inductive predicates: yields nice induction principles for free; more elegant proofs
 haftmann parents: 
39774diff
changeset | 3367 | assume "xs' = take n xs" | 
| 
ecf97cf3d248
turned distinct and sorted into inductive predicates: yields nice induction principles for free; more elegant proofs
 haftmann parents: 
39774diff
changeset | 3368 | 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: 
39774diff
changeset | 3369 | 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: 
39774diff
changeset | 3370 | 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: 
39774diff
changeset | 3371 | qed | 
| 
ecf97cf3d248
turned distinct and sorted into inductive predicates: yields nice induction principles for free; more elegant proofs
 haftmann parents: 
39774diff
changeset | 3372 | |
| 
ecf97cf3d248
turned distinct and sorted into inductive predicates: yields nice induction principles for free; more elegant proofs
 haftmann parents: 
39774diff
changeset | 3373 | lemma distinct_zipI2: | 
| 
ecf97cf3d248
turned distinct and sorted into inductive predicates: yields nice induction principles for free; more elegant proofs
 haftmann parents: 
39774diff
changeset | 3374 | assumes "distinct ys" | 
| 
ecf97cf3d248
turned distinct and sorted into inductive predicates: yields nice induction principles for free; more elegant proofs
 haftmann parents: 
39774diff
changeset | 3375 | shows "distinct (zip xs ys)" | 
| 
ecf97cf3d248
turned distinct and sorted into inductive predicates: yields nice induction principles for free; more elegant proofs
 haftmann parents: 
39774diff
changeset | 3376 | 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: 
39774diff
changeset | 3377 | 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: 
39774diff
changeset | 3378 | assume "length xs' = length ys'" | 
| 
ecf97cf3d248
turned distinct and sorted into inductive predicates: yields nice induction principles for free; more elegant proofs
 haftmann parents: 
39774diff
changeset | 3379 | assume "ys' = take n ys" | 
| 
ecf97cf3d248
turned distinct and sorted into inductive predicates: yields nice induction principles for free; more elegant proofs
 haftmann parents: 
39774diff
changeset | 3380 | 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: 
39774diff
changeset | 3381 | 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: 
39774diff
changeset | 3382 | 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: 
39774diff
changeset | 3383 | qed | 
| 
ecf97cf3d248
turned distinct and sorted into inductive predicates: yields nice induction principles for free; more elegant proofs
 haftmann parents: 
39774diff
changeset | 3384 | |
| 47122 | 3385 | lemma set_take_disj_set_drop_if_distinct: | 
| 3386 |   "distinct vs \<Longrightarrow> i \<le> j \<Longrightarrow> set (take i vs) \<inter> set (drop j vs) = {}"
 | |
| 3387 | by (auto simp: in_set_conv_nth distinct_conv_nth) | |
| 3388 | ||
| 44635 
3d046864ebe6
added two lemmas about "distinct" to help Sledgehammer
 blanchet parents: 
44619diff
changeset | 3389 | (* The next two lemmas help Sledgehammer. *) | 
| 
3d046864ebe6
added two lemmas about "distinct" to help Sledgehammer
 blanchet parents: 
44619diff
changeset | 3390 | |
| 
3d046864ebe6
added two lemmas about "distinct" to help Sledgehammer
 blanchet parents: 
44619diff
changeset | 3391 | lemma distinct_singleton: "distinct [x]" by simp | 
| 
3d046864ebe6
added two lemmas about "distinct" to help Sledgehammer
 blanchet parents: 
44619diff
changeset | 3392 | |
| 
3d046864ebe6
added two lemmas about "distinct" to help Sledgehammer
 blanchet parents: 
44619diff
changeset | 3393 | lemma distinct_length_2_or_more: | 
| 
3d046864ebe6
added two lemmas about "distinct" to help Sledgehammer
 blanchet parents: 
44619diff
changeset | 3394 | "distinct (a # b # xs) \<longleftrightarrow> (a \<noteq> b \<and> distinct (a # xs) \<and> distinct (b # xs))" | 
| 55405 
0a155051bd9d
use new selector support to define 'the', 'hd', 'tl'
 blanchet parents: 
55404diff
changeset | 3395 | by (metis distinct.simps(2) list.sel(1) hd_in_set list.simps(2) set_ConsD set_rev_mp | 
| 
0a155051bd9d
use new selector support to define 'the', 'hd', 'tl'
 blanchet parents: 
55404diff
changeset | 3396 | set_subset_Cons) | 
| 44635 
3d046864ebe6
added two lemmas about "distinct" to help Sledgehammer
 blanchet parents: 
44619diff
changeset | 3397 | |
| 53721 
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
 traytel parents: 
53689diff
changeset | 3398 | lemma remdups_adj_Cons: "remdups_adj (x # xs) = | 
| 
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
 traytel parents: 
53689diff
changeset | 3399 | (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: 
53689diff
changeset | 3400 | by (induct xs arbitrary: x) (auto split: list.splits) | 
| 
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
 traytel parents: 
53689diff
changeset | 3401 | |
| 
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
 traytel parents: 
53689diff
changeset | 3402 | lemma remdups_adj_append_two: | 
| 
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
 traytel parents: 
53689diff
changeset | 3403 | "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: 
53689diff
changeset | 3404 | by (induct xs rule: remdups_adj.induct, simp_all) | 
| 
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
 traytel parents: 
53689diff
changeset | 3405 | |
| 
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
 traytel parents: 
53689diff
changeset | 3406 | 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: 
53689diff
changeset | 3407 | 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: 
53689diff
changeset | 3408 | |
| 
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
 traytel parents: 
53689diff
changeset | 3409 | 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: 
53689diff
changeset | 3410 | by (induct xs rule: remdups_adj.induct, auto) | 
| 
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
 traytel parents: 
53689diff
changeset | 3411 | |
| 
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
 traytel parents: 
53689diff
changeset | 3412 | 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: 
53689diff
changeset | 3413 | by (induct xs rule: remdups_adj.induct, simp_all) | 
| 
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
 traytel parents: 
53689diff
changeset | 3414 | |
| 
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
 traytel parents: 
53689diff
changeset | 3415 | lemma remdups_adj_Nil_iff[simp]: "remdups_adj xs = [] \<longleftrightarrow> xs = []" | 
| 
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
 traytel parents: 
53689diff
changeset | 3416 | by (induct xs rule: remdups_adj.induct, simp_all) | 
| 
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
 traytel parents: 
53689diff
changeset | 3417 | |
| 
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
 traytel parents: 
53689diff
changeset | 3418 | lemma remdups_adj_set[simp]: "set (remdups_adj xs) = set xs" | 
| 
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
 traytel parents: 
53689diff
changeset | 3419 | by (induct xs rule: remdups_adj.induct, simp_all) | 
| 
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
 traytel parents: 
53689diff
changeset | 3420 | |
| 
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
 traytel parents: 
53689diff
changeset | 3421 | 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: 
53689diff
changeset | 3422 | by (induct xs rule: remdups_adj.induct, auto) | 
| 
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
 traytel parents: 
53689diff
changeset | 3423 | |
| 
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
 traytel parents: 
53689diff
changeset | 3424 | lemma remdups_adj_distinct: "distinct xs \<Longrightarrow> remdups_adj xs = xs" | 
| 
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
 traytel parents: 
53689diff
changeset | 3425 | by (induct xs rule: remdups_adj.induct, simp_all) | 
| 
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
 traytel parents: 
53689diff
changeset | 3426 | |
| 
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
 traytel parents: 
53689diff
changeset | 3427 | lemma remdups_adj_append: | 
| 
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
 traytel parents: 
53689diff
changeset | 3428 | "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: 
53689diff
changeset | 3429 | by (induct xs\<^sub>1 rule: remdups_adj.induct, simp_all) | 
| 
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
 traytel parents: 
53689diff
changeset | 3430 | |
| 
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
 traytel parents: 
53689diff
changeset | 3431 | lemma remdups_adj_singleton: | 
| 
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
 traytel parents: 
53689diff
changeset | 3432 | "remdups_adj xs = [x] \<Longrightarrow> xs = replicate (length xs) x" | 
| 
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
 traytel parents: 
53689diff
changeset | 3433 | 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: 
53689diff
changeset | 3434 | |
| 
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
 traytel parents: 
53689diff
changeset | 3435 | lemma remdups_adj_map_injective: | 
| 
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
 traytel parents: 
53689diff
changeset | 3436 | assumes "inj f" | 
| 
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
 traytel parents: 
53689diff
changeset | 3437 | shows "remdups_adj (map f xs) = map f (remdups_adj xs)" | 
| 
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
 traytel parents: 
53689diff
changeset | 3438 | by (induct xs rule: remdups_adj.induct, | 
| 
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
 traytel parents: 
53689diff
changeset | 3439 | auto simp add: injD[OF assms]) | 
| 
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
 traytel parents: 
53689diff
changeset | 3440 | |
| 49948 
744934b818c7
moved quite generic material from theory Enum to more appropriate places
 haftmann parents: 
49808diff
changeset | 3441 | |
| 37605 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 3442 | 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: 
37465diff
changeset | 3443 | |
| 39774 | 3444 | lemma (in monoid_add) listsum_simps [simp]: | 
| 3445 | "listsum [] = 0" | |
| 46133 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 haftmann parents: 
46125diff
changeset | 3446 | "listsum (x # xs) = x + listsum xs" | 
| 39774 | 3447 | by (simp_all add: listsum_def) | 
| 3448 | ||
| 3449 | lemma (in monoid_add) listsum_append [simp]: | |
| 3450 | "listsum (xs @ ys) = listsum xs + listsum ys" | |
| 3451 | by (induct xs) (simp_all add: add.assoc) | |
| 3452 | ||
| 3453 | lemma (in comm_monoid_add) listsum_rev [simp]: | |
| 3454 | "listsum (rev xs) = listsum xs" | |
| 47397 
d654c73e4b12
no preference wrt. fold(l/r); prefer fold rather than foldr for iterating over lists in generated code
 haftmann parents: 
47131diff
changeset | 3455 | by (simp add: listsum_def foldr_fold fold_rev fun_eq_iff add_ac) | 
| 46133 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 haftmann parents: 
46125diff
changeset | 3456 | |
| 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 haftmann parents: 
46125diff
changeset | 3457 | 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: 
46125diff
changeset | 3458 | "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: 
46125diff
changeset | 3459 | proof | 
| 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 haftmann parents: 
46125diff
changeset | 3460 | fix x | 
| 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 haftmann parents: 
46125diff
changeset | 3461 | 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: 
46125diff
changeset | 3462 | 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: 
47131diff
changeset | 3463 | 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: 
46125diff
changeset | 3464 | 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: 
46125diff
changeset | 3465 | 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: 
46125diff
changeset | 3466 | 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: 
46125diff
changeset | 3467 | qed | 
| 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 haftmann parents: 
46125diff
changeset | 3468 | |
| 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 haftmann parents: 
46125diff
changeset | 3469 | 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: 
46125diff
changeset | 3470 | |
| 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 haftmann parents: 
46125diff
changeset | 3471 | syntax | 
| 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 haftmann parents: 
46125diff
changeset | 3472 |   "_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: 
46125diff
changeset | 3473 | syntax (xsymbols) | 
| 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 haftmann parents: 
46125diff
changeset | 3474 |   "_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: 
46125diff
changeset | 3475 | syntax (HTML output) | 
| 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 haftmann parents: 
46125diff
changeset | 3476 |   "_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: 
46125diff
changeset | 3477 | |
| 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 haftmann parents: 
46125diff
changeset | 3478 | 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: 
46125diff
changeset | 3479 | "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: 
46125diff
changeset | 3480 | "\<Sum>x\<leftarrow>xs. b" == "CONST listsum (CONST map (%x. b) xs)" | 
| 39774 | 3481 | |
| 3482 | lemma (in comm_monoid_add) listsum_map_remove1: | |
| 3483 | "x \<in> set xs \<Longrightarrow> listsum (map f xs) = f x + listsum (map f (remove1 x xs))" | |
| 3484 | by (induct xs) (auto simp add: ac_simps) | |
| 3485 | ||
| 3486 | lemma (in monoid_add) list_size_conv_listsum: | |
| 37605 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 3487 | "list_size f xs = listsum (map f xs) + size xs" | 
| 39774 | 3488 | by (induct xs) auto | 
| 3489 | ||
| 3490 | lemma (in monoid_add) length_concat: | |
| 3491 | "length (concat xss) = listsum (map length xss)" | |
| 3492 | by (induct xss) simp_all | |
| 3493 | ||
| 53721 
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
 traytel parents: 
53689diff
changeset | 3494 | lemma (in monoid_add) length_product_lists: | 
| 
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
 traytel parents: 
53689diff
changeset | 3495 | "length (product_lists xss) = foldr op * (map length xss) 1" | 
| 
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
 traytel parents: 
53689diff
changeset | 3496 | proof (induct xss) | 
| 
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
 traytel parents: 
53689diff
changeset | 3497 | 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: 
53689diff
changeset | 3498 | qed simp | 
| 
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
 traytel parents: 
53689diff
changeset | 3499 | |
| 39774 | 3500 | lemma (in monoid_add) listsum_map_filter: | 
| 3501 | 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: 
37465diff
changeset | 3502 | shows "listsum (map f (filter P xs)) = listsum (map f xs)" | 
| 39774 | 3503 | using assms by (induct xs) auto | 
| 3504 | ||
| 51738 
9e4220605179
tuned: unnamed contexts, interpretation and sublocale in locale target;
 haftmann parents: 
51717diff
changeset | 3505 | lemma (in comm_monoid_add) distinct_listsum_conv_Setsum: | 
| 39774 | 3506 | "distinct xs \<Longrightarrow> listsum xs = Setsum (set xs)" | 
| 3507 | by (induct xs) simp_all | |
| 3508 | ||
| 3509 | lemma listsum_eq_0_nat_iff_nat [simp]: | |
| 3510 | "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: 
47131diff
changeset | 3511 | 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: 
47131diff
changeset | 3512 | |
| 
d654c73e4b12
no preference wrt. fold(l/r); prefer fold rather than foldr for iterating over lists in generated code
 haftmann parents: 
47131diff
changeset | 3513 | 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: 
47131diff
changeset | 3514 | "(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: 
47131diff
changeset | 3515 | by (induct ns) auto | 
| 39774 | 3516 | |
| 3517 | lemma elem_le_listsum_nat: | |
| 3518 | "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: 
47131diff
changeset | 3519 | 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: 
37465diff
changeset | 3520 | |
| 39774 | 3521 | 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: 
47131diff
changeset | 3522 | "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: 
37465diff
changeset | 3523 | apply(induct ns arbitrary:k) | 
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 3524 | apply (auto split:nat.split) | 
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 3525 | apply(drule elem_le_listsum_nat) | 
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 3526 | apply arith | 
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 3527 | done | 
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 3528 | |
| 39774 | 3529 | lemma (in monoid_add) listsum_triv: | 
| 3530 | "(\<Sum>x\<leftarrow>xs. r) = of_nat (length xs) * r" | |
| 49962 
a8cc904a6820
Renamed {left,right}_distrib to distrib_{right,left}.
 webertj parents: 
49808diff
changeset | 3531 | 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: 
37465diff
changeset | 3532 | |
| 39774 | 3533 | lemma (in monoid_add) listsum_0 [simp]: | 
| 3534 | "(\<Sum>x\<leftarrow>xs. 0) = 0" | |
| 49962 
a8cc904a6820
Renamed {left,right}_distrib to distrib_{right,left}.
 webertj parents: 
49808diff
changeset | 3535 | 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: 
37465diff
changeset | 3536 | |
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 3537 | text{* For non-Abelian groups @{text xs} needs to be reversed on one side: *}
 | 
| 39774 | 3538 | lemma (in ab_group_add) uminus_listsum_map: | 
| 3539 | "- listsum (map f xs) = listsum (map (uminus \<circ> f) xs)" | |
| 3540 | by (induct xs) simp_all | |
| 3541 | ||
| 3542 | lemma (in comm_monoid_add) listsum_addf: | |
| 3543 | "(\<Sum>x\<leftarrow>xs. f x + g x) = listsum (map f xs) + listsum (map g xs)" | |
| 3544 | by (induct xs) (simp_all add: algebra_simps) | |
| 3545 | ||
| 3546 | lemma (in ab_group_add) listsum_subtractf: | |
| 3547 | "(\<Sum>x\<leftarrow>xs. f x - g x) = listsum (map f xs) - listsum (map g xs)" | |
| 3548 | by (induct xs) (simp_all add: algebra_simps) | |
| 3549 | ||
| 3550 | lemma (in semiring_0) listsum_const_mult: | |
| 3551 | "(\<Sum>x\<leftarrow>xs. c * f x) = c * (\<Sum>x\<leftarrow>xs. f x)" | |
| 3552 | by (induct xs) (simp_all add: algebra_simps) | |
| 3553 | ||
| 3554 | lemma (in semiring_0) listsum_mult_const: | |
| 3555 | "(\<Sum>x\<leftarrow>xs. f x * c) = (\<Sum>x\<leftarrow>xs. f x) * c" | |
| 3556 | by (induct xs) (simp_all add: algebra_simps) | |
| 3557 | ||
| 3558 | lemma (in ordered_ab_group_add_abs) listsum_abs: | |
| 3559 | "\<bar>listsum xs\<bar> \<le> listsum (map abs xs)" | |
| 3560 | 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: 
37465diff
changeset | 3561 | |
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 3562 | lemma listsum_mono: | 
| 39774 | 3563 |   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: 
37465diff
changeset | 3564 | 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 | 3565 | by (induct xs) (simp, simp add: add_mono) | 
| 3566 | ||
| 3567 | 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: 
37465diff
changeset | 3568 | "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: 
37465diff
changeset | 3569 | by (induct xs) simp_all | 
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 3570 | |
| 39774 | 3571 | 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: 
37465diff
changeset | 3572 | "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: 
37465diff
changeset | 3573 | 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: 
37465diff
changeset | 3574 | |
| 39774 | 3575 | 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: 
37465diff
changeset | 3576 | "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: 
37465diff
changeset | 3577 | 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: 
37465diff
changeset | 3578 | |
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 3579 | text {* General equivalence between @{const listsum} and @{const setsum} *}
 | 
| 39774 | 3580 | 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: 
37465diff
changeset | 3581 | "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: 
37465diff
changeset | 3582 | 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: 
37465diff
changeset | 3583 | |
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 3584 | |
| 34978 
874150ddd50a
canonical insert operation; generalized lemma foldl_apply_inv to foldl_apply
 haftmann parents: 
34942diff
changeset | 3585 | subsubsection {* @{const insert} *}
 | 
| 
874150ddd50a
canonical insert operation; generalized lemma foldl_apply_inv to foldl_apply
 haftmann parents: 
34942diff
changeset | 3586 | |
| 
874150ddd50a
canonical insert operation; generalized lemma foldl_apply_inv to foldl_apply
 haftmann parents: 
34942diff
changeset | 3587 | lemma in_set_insert [simp]: | 
| 
874150ddd50a
canonical insert operation; generalized lemma foldl_apply_inv to foldl_apply
 haftmann parents: 
34942diff
changeset | 3588 | "x \<in> set xs \<Longrightarrow> List.insert x xs = xs" | 
| 
874150ddd50a
canonical insert operation; generalized lemma foldl_apply_inv to foldl_apply
 haftmann parents: 
34942diff
changeset | 3589 | by (simp add: List.insert_def) | 
| 
874150ddd50a
canonical insert operation; generalized lemma foldl_apply_inv to foldl_apply
 haftmann parents: 
34942diff
changeset | 3590 | |
| 
874150ddd50a
canonical insert operation; generalized lemma foldl_apply_inv to foldl_apply
 haftmann parents: 
34942diff
changeset | 3591 | lemma not_in_set_insert [simp]: | 
| 
874150ddd50a
canonical insert operation; generalized lemma foldl_apply_inv to foldl_apply
 haftmann parents: 
34942diff
changeset | 3592 | "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: 
34942diff
changeset | 3593 | by (simp add: List.insert_def) | 
| 
874150ddd50a
canonical insert operation; generalized lemma foldl_apply_inv to foldl_apply
 haftmann parents: 
34942diff
changeset | 3594 | |
| 
874150ddd50a
canonical insert operation; generalized lemma foldl_apply_inv to foldl_apply
 haftmann parents: 
34942diff
changeset | 3595 | lemma insert_Nil [simp]: | 
| 
874150ddd50a
canonical insert operation; generalized lemma foldl_apply_inv to foldl_apply
 haftmann parents: 
34942diff
changeset | 3596 | "List.insert x [] = [x]" | 
| 
874150ddd50a
canonical insert operation; generalized lemma foldl_apply_inv to foldl_apply
 haftmann parents: 
34942diff
changeset | 3597 | by simp | 
| 
874150ddd50a
canonical insert operation; generalized lemma foldl_apply_inv to foldl_apply
 haftmann parents: 
34942diff
changeset | 3598 | |
| 35295 | 3599 | lemma set_insert [simp]: | 
| 34978 
874150ddd50a
canonical insert operation; generalized lemma foldl_apply_inv to foldl_apply
 haftmann parents: 
34942diff
changeset | 3600 | "set (List.insert x xs) = insert x (set xs)" | 
| 
874150ddd50a
canonical insert operation; generalized lemma foldl_apply_inv to foldl_apply
 haftmann parents: 
34942diff
changeset | 3601 | by (auto simp add: List.insert_def) | 
| 
874150ddd50a
canonical insert operation; generalized lemma foldl_apply_inv to foldl_apply
 haftmann parents: 
34942diff
changeset | 3602 | |
| 35295 | 3603 | lemma distinct_insert [simp]: | 
| 3604 | "distinct xs \<Longrightarrow> distinct (List.insert x xs)" | |
| 3605 | by (simp add: List.insert_def) | |
| 3606 | ||
| 36275 | 3607 | lemma insert_remdups: | 
| 3608 | "List.insert x (remdups xs) = remdups (List.insert x xs)" | |
| 3609 | by (simp add: List.insert_def) | |
| 3610 | ||
| 34978 
874150ddd50a
canonical insert operation; generalized lemma foldl_apply_inv to foldl_apply
 haftmann parents: 
34942diff
changeset | 3611 | |
| 47122 | 3612 | subsubsection {* @{const List.find} *}
 | 
| 3613 | ||
| 3614 | lemma find_None_iff: "List.find P xs = None \<longleftrightarrow> \<not> (\<exists>x. x \<in> set xs \<and> P x)" | |
| 3615 | proof (induction xs) | |
| 3616 | case Nil thus ?case by simp | |
| 3617 | next | |
| 3618 | case (Cons x xs) thus ?case by (fastforce split: if_splits) | |
| 3619 | qed | |
| 3620 | ||
| 3621 | lemma find_Some_iff: | |
| 3622 | "List.find P xs = Some x \<longleftrightarrow> | |
| 3623 | (\<exists>i<length xs. P (xs!i) \<and> x = xs!i \<and> (\<forall>j<i. \<not> P (xs!j)))" | |
| 3624 | proof (induction xs) | |
| 3625 | case Nil thus ?case by simp | |
| 3626 | next | |
| 3627 | case (Cons x xs) thus ?case | |
| 3628 | by(auto simp: nth_Cons' split: if_splits) | |
| 3629 | (metis One_nat_def diff_Suc_1 less_Suc_eq_0_disj) | |
| 3630 | qed | |
| 3631 | ||
| 3632 | lemma find_cong[fundef_cong]: | |
| 3633 | assumes "xs = ys" and "\<And>x. x \<in> set ys \<Longrightarrow> P x = Q x" | |
| 3634 | shows "List.find P xs = List.find Q ys" | |
| 3635 | proof (cases "List.find P xs") | |
| 3636 | case None thus ?thesis by (metis find_None_iff assms) | |
| 3637 | next | |
| 3638 | case (Some x) | |
| 3639 | hence "List.find Q ys = Some x" using assms | |
| 3640 | by (auto simp add: find_Some_iff) | |
| 3641 | thus ?thesis using Some by auto | |
| 3642 | qed | |
| 3643 | ||
| 52379 | 3644 | lemma find_dropWhile: | 
| 3645 | "List.find P xs = (case dropWhile (Not \<circ> P) xs | |
| 3646 | of [] \<Rightarrow> None | |
| 3647 | | x # _ \<Rightarrow> Some x)" | |
| 3648 | by (induct xs) simp_all | |
| 3649 | ||
| 47122 | 3650 | |
| 3651 | subsubsection {* @{const remove1} *}
 | |
| 15110 
78b5636eabc7
Added a number of new thms and the new function remove1
 nipkow parents: 
15072diff
changeset | 3652 | |
| 18049 | 3653 | lemma remove1_append: | 
| 3654 | "remove1 x (xs @ ys) = | |
| 3655 | (if x \<in> set xs then remove1 x xs @ ys else xs @ remove1 x ys)" | |
| 3656 | by (induct xs) auto | |
| 3657 | ||
| 36903 | 3658 | lemma remove1_commute: "remove1 x (remove1 y zs) = remove1 y (remove1 x zs)" | 
| 3659 | by (induct zs) auto | |
| 3660 | ||
| 23479 | 3661 | lemma in_set_remove1[simp]: | 
| 3662 | "a \<noteq> b \<Longrightarrow> a : set(remove1 b xs) = (a : set xs)" | |
| 3663 | apply (induct xs) | |
| 3664 | apply auto | |
| 3665 | done | |
| 3666 | ||
| 15110 
78b5636eabc7
Added a number of new thms and the new function remove1
 nipkow parents: 
15072diff
changeset | 3667 | lemma set_remove1_subset: "set(remove1 x xs) <= set xs" | 
| 
78b5636eabc7
Added a number of new thms and the new function remove1
 nipkow parents: 
15072diff
changeset | 3668 | apply(induct xs) | 
| 
78b5636eabc7
Added a number of new thms and the new function remove1
 nipkow parents: 
15072diff
changeset | 3669 | apply simp | 
| 
78b5636eabc7
Added a number of new thms and the new function remove1
 nipkow parents: 
15072diff
changeset | 3670 | apply simp | 
| 
78b5636eabc7
Added a number of new thms and the new function remove1
 nipkow parents: 
15072diff
changeset | 3671 | apply blast | 
| 
78b5636eabc7
Added a number of new thms and the new function remove1
 nipkow parents: 
15072diff
changeset | 3672 | done | 
| 
78b5636eabc7
Added a number of new thms and the new function remove1
 nipkow parents: 
15072diff
changeset | 3673 | |
| 17724 | 3674 | 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: 
15072diff
changeset | 3675 | apply(induct xs) | 
| 
78b5636eabc7
Added a number of new thms and the new function remove1
 nipkow parents: 
15072diff
changeset | 3676 | apply simp | 
| 
78b5636eabc7
Added a number of new thms and the new function remove1
 nipkow parents: 
15072diff
changeset | 3677 | apply simp | 
| 
78b5636eabc7
Added a number of new thms and the new function remove1
 nipkow parents: 
15072diff
changeset | 3678 | apply blast | 
| 
78b5636eabc7
Added a number of new thms and the new function remove1
 nipkow parents: 
15072diff
changeset | 3679 | done | 
| 
78b5636eabc7
Added a number of new thms and the new function remove1
 nipkow parents: 
15072diff
changeset | 3680 | |
| 23479 | 3681 | lemma length_remove1: | 
| 30128 
365ee7319b86
revert some Suc 0 lemmas back to their original forms; added some simp rules for (1::nat)
 huffman parents: 
30079diff
changeset | 3682 | "length(remove1 x xs) = (if x : set xs then length xs - 1 else length xs)" | 
| 23479 | 3683 | apply (induct xs) | 
| 3684 | apply (auto dest!:length_pos_if_in_set) | |
| 3685 | done | |
| 3686 | ||
| 18049 | 3687 | lemma remove1_filter_not[simp]: | 
| 3688 | "\<not> P x \<Longrightarrow> remove1 x (filter P xs) = filter P xs" | |
| 3689 | by(induct xs) auto | |
| 3690 | ||
| 39073 | 3691 | lemma filter_remove1: | 
| 3692 | "filter Q (remove1 x xs) = remove1 x (filter Q xs)" | |
| 3693 | by (induct xs) auto | |
| 3694 | ||
| 15110 
78b5636eabc7
Added a number of new thms and the new function remove1
 nipkow parents: 
15072diff
changeset | 3695 | 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: 
15072diff
changeset | 3696 | apply(insert set_remove1_subset) | 
| 
78b5636eabc7
Added a number of new thms and the new function remove1
 nipkow parents: 
15072diff
changeset | 3697 | apply fast | 
| 
78b5636eabc7
Added a number of new thms and the new function remove1
 nipkow parents: 
15072diff
changeset | 3698 | done | 
| 
78b5636eabc7
Added a number of new thms and the new function remove1
 nipkow parents: 
15072diff
changeset | 3699 | |
| 
78b5636eabc7
Added a number of new thms and the new function remove1
 nipkow parents: 
15072diff
changeset | 3700 | lemma distinct_remove1[simp]: "distinct xs ==> distinct(remove1 x xs)" | 
| 
78b5636eabc7
Added a number of new thms and the new function remove1
 nipkow parents: 
15072diff
changeset | 3701 | by (induct xs) simp_all | 
| 
78b5636eabc7
Added a number of new thms and the new function remove1
 nipkow parents: 
15072diff
changeset | 3702 | |
| 36275 | 3703 | lemma remove1_remdups: | 
| 3704 | "distinct xs \<Longrightarrow> remove1 x (remdups xs) = remdups (remove1 x xs)" | |
| 3705 | by (induct xs) simp_all | |
| 3706 | ||
| 37107 | 3707 | lemma remove1_idem: | 
| 3708 | assumes "x \<notin> set xs" | |
| 3709 | shows "remove1 x xs = xs" | |
| 3710 | using assms by (induct xs) simp_all | |
| 3711 | ||
| 13114 | 3712 | |
| 49948 
744934b818c7
moved quite generic material from theory Enum to more appropriate places
 haftmann parents: 
49808diff
changeset | 3713 | subsubsection {* @{const removeAll} *}
 | 
| 27693 | 3714 | |
| 34978 
874150ddd50a
canonical insert operation; generalized lemma foldl_apply_inv to foldl_apply
 haftmann parents: 
34942diff
changeset | 3715 | lemma removeAll_filter_not_eq: | 
| 
874150ddd50a
canonical insert operation; generalized lemma foldl_apply_inv to foldl_apply
 haftmann parents: 
34942diff
changeset | 3716 | "removeAll x = filter (\<lambda>y. x \<noteq> y)" | 
| 
874150ddd50a
canonical insert operation; generalized lemma foldl_apply_inv to foldl_apply
 haftmann parents: 
34942diff
changeset | 3717 | proof | 
| 
874150ddd50a
canonical insert operation; generalized lemma foldl_apply_inv to foldl_apply
 haftmann parents: 
34942diff
changeset | 3718 | fix xs | 
| 
874150ddd50a
canonical insert operation; generalized lemma foldl_apply_inv to foldl_apply
 haftmann parents: 
34942diff
changeset | 3719 | 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: 
34942diff
changeset | 3720 | by (induct xs) auto | 
| 
874150ddd50a
canonical insert operation; generalized lemma foldl_apply_inv to foldl_apply
 haftmann parents: 
34942diff
changeset | 3721 | qed | 
| 
874150ddd50a
canonical insert operation; generalized lemma foldl_apply_inv to foldl_apply
 haftmann parents: 
34942diff
changeset | 3722 | |
| 27693 | 3723 | lemma removeAll_append[simp]: | 
| 3724 | "removeAll x (xs @ ys) = removeAll x xs @ removeAll x ys" | |
| 3725 | by (induct xs) auto | |
| 3726 | ||
| 3727 | lemma set_removeAll[simp]: "set(removeAll x xs) = set xs - {x}"
 | |
| 3728 | by (induct xs) auto | |
| 3729 | ||
| 3730 | lemma removeAll_id[simp]: "x \<notin> set xs \<Longrightarrow> removeAll x xs = xs" | |
| 3731 | by (induct xs) auto | |
| 3732 | ||
| 46448 
f1201fac7398
more specification of the quotient package in IsarRef
 Cezary Kaliszyk <cezarykaliszyk@gmail.com> parents: 
46440diff
changeset | 3733 | (* Needs count:: 'a \<Rightarrow> 'a list \<Rightarrow> nat | 
| 27693 | 3734 | lemma length_removeAll: | 
| 3735 | "length(removeAll x xs) = length xs - count x xs" | |
| 3736 | *) | |
| 3737 | ||
| 3738 | lemma removeAll_filter_not[simp]: | |
| 3739 | "\<not> P x \<Longrightarrow> removeAll x (filter P xs) = filter P xs" | |
| 3740 | by(induct xs) auto | |
| 3741 | ||
| 34978 
874150ddd50a
canonical insert operation; generalized lemma foldl_apply_inv to foldl_apply
 haftmann parents: 
34942diff
changeset | 3742 | lemma distinct_removeAll: | 
| 
874150ddd50a
canonical insert operation; generalized lemma foldl_apply_inv to foldl_apply
 haftmann parents: 
34942diff
changeset | 3743 | "distinct xs \<Longrightarrow> distinct (removeAll x xs)" | 
| 
874150ddd50a
canonical insert operation; generalized lemma foldl_apply_inv to foldl_apply
 haftmann parents: 
34942diff
changeset | 3744 | by (simp add: removeAll_filter_not_eq) | 
| 27693 | 3745 | |
| 3746 | lemma distinct_remove1_removeAll: | |
| 3747 | "distinct xs ==> remove1 x xs = removeAll x xs" | |
| 3748 | by (induct xs) simp_all | |
| 3749 | ||
| 3750 | lemma map_removeAll_inj_on: "inj_on f (insert x (set xs)) \<Longrightarrow> | |
| 3751 | map f (removeAll x xs) = removeAll (f x) (map f xs)" | |
| 3752 | by (induct xs) (simp_all add:inj_on_def) | |
| 3753 | ||
| 3754 | lemma map_removeAll_inj: "inj f \<Longrightarrow> | |
| 3755 | map f (removeAll x xs) = removeAll (f x) (map f xs)" | |
| 3756 | by(metis map_removeAll_inj_on subset_inj_on subset_UNIV) | |
| 3757 | ||
| 3758 | ||
| 49948 
744934b818c7
moved quite generic material from theory Enum to more appropriate places
 haftmann parents: 
49808diff
changeset | 3759 | subsubsection {* @{const replicate} *}
 | 
| 13114 | 3760 | |
| 13142 | 3761 | lemma length_replicate [simp]: "length (replicate n x) = n" | 
| 13145 | 3762 | by (induct n) auto | 
| 13124 | 3763 | |
| 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: 
36275diff
changeset | 3764 | 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: 
36275diff
changeset | 3765 | 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: 
36275diff
changeset | 3766 | |
| 13142 | 3767 | lemma map_replicate [simp]: "map f (replicate n x) = replicate n (f x)" | 
| 13145 | 3768 | by (induct n) auto | 
| 13114 | 3769 | |
| 31363 
7493b571b37d
Added theorems about distinct & concat, map & replicate and concat & replicate
 hoelzl parents: 
31264diff
changeset | 3770 | lemma map_replicate_const: | 
| 
7493b571b37d
Added theorems about distinct & concat, map & replicate and concat & replicate
 hoelzl parents: 
31264diff
changeset | 3771 | "map (\<lambda> x. k) lst = replicate (length lst) k" | 
| 
7493b571b37d
Added theorems about distinct & concat, map & replicate and concat & replicate
 hoelzl parents: 
31264diff
changeset | 3772 | by (induct lst) auto | 
| 
7493b571b37d
Added theorems about distinct & concat, map & replicate and concat & replicate
 hoelzl parents: 
31264diff
changeset | 3773 | |
| 13114 | 3774 | lemma replicate_app_Cons_same: | 
| 13145 | 3775 | "(replicate n x) @ (x # xs) = x # replicate n x @ xs" | 
| 3776 | by (induct n) auto | |
| 13114 | 3777 | |
| 13142 | 3778 | lemma rev_replicate [simp]: "rev (replicate n x) = replicate n x" | 
| 14208 | 3779 | apply (induct n, simp) | 
| 13145 | 3780 | apply (simp add: replicate_app_Cons_same) | 
| 3781 | done | |
| 13114 | 3782 | |
| 13142 | 3783 | lemma replicate_add: "replicate (n + m) x = replicate n x @ replicate m x" | 
| 13145 | 3784 | by (induct n) auto | 
| 13114 | 3785 | |
| 16397 | 3786 | text{* Courtesy of Matthias Daum: *}
 | 
| 3787 | lemma append_replicate_commute: | |
| 3788 | "replicate n x @ replicate k x = replicate k x @ replicate n x" | |
| 3789 | apply (simp add: replicate_add [THEN sym]) | |
| 3790 | apply (simp add: add_commute) | |
| 3791 | done | |
| 3792 | ||
| 31080 | 3793 | text{* Courtesy of Andreas Lochbihler: *}
 | 
| 3794 | lemma filter_replicate: | |
| 3795 | "filter P (replicate n x) = (if P x then replicate n x else [])" | |
| 3796 | by(induct n) auto | |
| 3797 | ||
| 13142 | 3798 | lemma hd_replicate [simp]: "n \<noteq> 0 ==> hd (replicate n x) = x" | 
| 13145 | 3799 | by (induct n) auto | 
| 13114 | 3800 | |
| 46500 
0196966d6d2d
removing unnecessary premises in theorems of List theory
 bulwahn parents: 
46448diff
changeset | 3801 | lemma tl_replicate [simp]: "tl (replicate n x) = replicate (n - 1) x" | 
| 13145 | 3802 | by (induct n) auto | 
| 13114 | 3803 | |
| 13142 | 3804 | lemma last_replicate [simp]: "n \<noteq> 0 ==> last (replicate n x) = x" | 
| 13145 | 3805 | by (atomize (full), induct n) auto | 
| 13114 | 3806 | |
| 24526 | 3807 | lemma nth_replicate[simp]: "i < n ==> (replicate n x)!i = x" | 
| 3808 | apply (induct n arbitrary: i, simp) | |
| 13145 | 3809 | apply (simp add: nth_Cons split: nat.split) | 
| 3810 | done | |
| 13114 | 3811 | |
| 16397 | 3812 | text{* Courtesy of Matthias Daum (2 lemmas): *}
 | 
| 3813 | lemma take_replicate[simp]: "take i (replicate k x) = replicate (min i k) x" | |
| 3814 | apply (case_tac "k \<le> i") | |
| 3815 | apply (simp add: min_def) | |
| 3816 | apply (drule not_leE) | |
| 3817 | apply (simp add: min_def) | |
| 3818 | apply (subgoal_tac "replicate k x = replicate i x @ replicate (k - i) x") | |
| 3819 | apply simp | |
| 3820 | apply (simp add: replicate_add [symmetric]) | |
| 3821 | done | |
| 3822 | ||
| 24526 | 3823 | lemma drop_replicate[simp]: "drop i (replicate k x) = replicate (k-i) x" | 
| 3824 | apply (induct k arbitrary: i) | |
| 16397 | 3825 | apply simp | 
| 3826 | apply clarsimp | |
| 3827 | apply (case_tac i) | |
| 3828 | apply simp | |
| 3829 | apply clarsimp | |
| 3830 | done | |
| 3831 | ||
| 13142 | 3832 | lemma set_replicate_Suc: "set (replicate (Suc n) x) = {x}"
 | 
| 13145 | 3833 | by (induct n) auto | 
| 13114 | 3834 | |
| 13142 | 3835 | lemma set_replicate [simp]: "n \<noteq> 0 ==> set (replicate n x) = {x}"
 | 
| 13145 | 3836 | by (fast dest!: not0_implies_Suc intro!: set_replicate_Suc) | 
| 13114 | 3837 | |
| 13142 | 3838 | lemma set_replicate_conv_if: "set (replicate n x) = (if n = 0 then {} else {x})"
 | 
| 13145 | 3839 | by auto | 
| 13114 | 3840 | |
| 37456 | 3841 | lemma in_set_replicate[simp]: "(x : set (replicate n y)) = (x = y & n \<noteq> 0)" | 
| 3842 | by (simp add: set_replicate_conv_if) | |
| 3843 | ||
| 37454 | 3844 | lemma Ball_set_replicate[simp]: | 
| 3845 | "(ALL x : set(replicate n a). P x) = (P a | n=0)" | |
| 3846 | by(simp add: set_replicate_conv_if) | |
| 3847 | ||
| 3848 | lemma Bex_set_replicate[simp]: | |
| 3849 | "(EX x : set(replicate n a). P x) = (P a & n\<noteq>0)" | |
| 3850 | by(simp add: set_replicate_conv_if) | |
| 13114 | 3851 | |
| 24796 | 3852 | lemma replicate_append_same: | 
| 3853 | "replicate i x @ [x] = x # replicate i x" | |
| 3854 | by (induct i) simp_all | |
| 3855 | ||
| 3856 | lemma map_replicate_trivial: | |
| 3857 | "map (\<lambda>i. x) [0..<i] = replicate i x" | |
| 3858 | by (induct i) (simp_all add: replicate_append_same) | |
| 3859 | ||
| 31363 
7493b571b37d
Added theorems about distinct & concat, map & replicate and concat & replicate
 hoelzl parents: 
31264diff
changeset | 3860 | lemma concat_replicate_trivial[simp]: | 
| 
7493b571b37d
Added theorems about distinct & concat, map & replicate and concat & replicate
 hoelzl parents: 
31264diff
changeset | 3861 | "concat (replicate i []) = []" | 
| 
7493b571b37d
Added theorems about distinct & concat, map & replicate and concat & replicate
 hoelzl parents: 
31264diff
changeset | 3862 | by (induct i) (auto simp add: map_replicate_const) | 
| 13114 | 3863 | |
| 28642 | 3864 | lemma replicate_empty[simp]: "(replicate n x = []) \<longleftrightarrow> n=0" | 
| 3865 | by (induct n) auto | |
| 3866 | ||
| 3867 | lemma empty_replicate[simp]: "([] = replicate n x) \<longleftrightarrow> n=0" | |
| 3868 | by (induct n) auto | |
| 3869 | ||
| 3870 | lemma replicate_eq_replicate[simp]: | |
| 3871 | "(replicate m x = replicate n y) \<longleftrightarrow> (m=n & (m\<noteq>0 \<longrightarrow> x=y))" | |
| 3872 | apply(induct m arbitrary: n) | |
| 3873 | apply simp | |
| 3874 | apply(induct_tac n) | |
| 3875 | apply auto | |
| 3876 | done | |
| 3877 | ||
| 39534 
c798d4f1b682
generalized lemma insort_remove1 to insort_key_remove1
 haftmann parents: 
39302diff
changeset | 3878 | lemma replicate_length_filter: | 
| 
c798d4f1b682
generalized lemma insort_remove1 to insort_key_remove1
 haftmann parents: 
39302diff
changeset | 3879 | "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: 
39302diff
changeset | 3880 | by (induct xs) auto | 
| 
c798d4f1b682
generalized lemma insort_remove1 to insort_key_remove1
 haftmann parents: 
39302diff
changeset | 3881 | |
| 42714 
fcba668b0839
add a lemma about commutative append to List.thy
 noschinl parents: 
42713diff
changeset | 3882 | lemma comm_append_are_replicate: | 
| 
fcba668b0839
add a lemma about commutative append to List.thy
 noschinl parents: 
42713diff
changeset | 3883 | fixes xs ys :: "'a list" | 
| 
fcba668b0839
add a lemma about commutative append to List.thy
 noschinl parents: 
42713diff
changeset | 3884 | assumes "xs \<noteq> []" "ys \<noteq> []" | 
| 
fcba668b0839
add a lemma about commutative append to List.thy
 noschinl parents: 
42713diff
changeset | 3885 | assumes "xs @ ys = ys @ xs" | 
| 
fcba668b0839
add a lemma about commutative append to List.thy
 noschinl parents: 
42713diff
changeset | 3886 | 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: 
42713diff
changeset | 3887 | using assms | 
| 
fcba668b0839
add a lemma about commutative append to List.thy
 noschinl parents: 
42713diff
changeset | 3888 | proof (induct "length (xs @ ys)" arbitrary: xs ys rule: less_induct) | 
| 
fcba668b0839
add a lemma about commutative append to List.thy
 noschinl parents: 
42713diff
changeset | 3889 | case less | 
| 
fcba668b0839
add a lemma about commutative append to List.thy
 noschinl parents: 
42713diff
changeset | 3890 | |
| 
fcba668b0839
add a lemma about commutative append to List.thy
 noschinl parents: 
42713diff
changeset | 3891 | 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: 
42713diff
changeset | 3892 | 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: 
42713diff
changeset | 3893 | then have | 
| 
fcba668b0839
add a lemma about commutative append to List.thy
 noschinl parents: 
42713diff
changeset | 3894 | prems': "length xs' \<le> length ys'" | 
| 
fcba668b0839
add a lemma about commutative append to List.thy
 noschinl parents: 
42713diff
changeset | 3895 | "xs' @ ys' = ys' @ xs'" | 
| 
fcba668b0839
add a lemma about commutative append to List.thy
 noschinl parents: 
42713diff
changeset | 3896 | and "xs' \<noteq> []" | 
| 
fcba668b0839
add a lemma about commutative append to List.thy
 noschinl parents: 
42713diff
changeset | 3897 | and len: "length (xs @ ys) = length (xs' @ ys')" | 
| 
fcba668b0839
add a lemma about commutative append to List.thy
 noschinl parents: 
42713diff
changeset | 3898 | using less by (auto intro: less.hyps) | 
| 
fcba668b0839
add a lemma about commutative append to List.thy
 noschinl parents: 
42713diff
changeset | 3899 | |
| 
fcba668b0839
add a lemma about commutative append to List.thy
 noschinl parents: 
42713diff
changeset | 3900 | from prems' | 
| 
fcba668b0839
add a lemma about commutative append to List.thy
 noschinl parents: 
42713diff
changeset | 3901 | obtain ws where "ys' = xs' @ ws" | 
| 
fcba668b0839
add a lemma about commutative append to List.thy
 noschinl parents: 
42713diff
changeset | 3902 | by (auto simp: append_eq_append_conv2) | 
| 
fcba668b0839
add a lemma about commutative append to List.thy
 noschinl parents: 
42713diff
changeset | 3903 | |
| 
fcba668b0839
add a lemma about commutative append to List.thy
 noschinl parents: 
42713diff
changeset | 3904 | 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: 
42713diff
changeset | 3905 | proof (cases "ws = []") | 
| 
fcba668b0839
add a lemma about commutative append to List.thy
 noschinl parents: 
42713diff
changeset | 3906 | case True | 
| 
fcba668b0839
add a lemma about commutative append to List.thy
 noschinl parents: 
42713diff
changeset | 3907 | then have "concat (replicate 1 xs') = xs'" | 
| 
fcba668b0839
add a lemma about commutative append to List.thy
 noschinl parents: 
42713diff
changeset | 3908 | and "concat (replicate 1 xs') = ys'" | 
| 
fcba668b0839
add a lemma about commutative append to List.thy
 noschinl parents: 
42713diff
changeset | 3909 | using `ys' = xs' @ ws` by auto | 
| 
fcba668b0839
add a lemma about commutative append to List.thy
 noschinl parents: 
42713diff
changeset | 3910 | then show ?thesis by blast | 
| 
fcba668b0839
add a lemma about commutative append to List.thy
 noschinl parents: 
42713diff
changeset | 3911 | next | 
| 
fcba668b0839
add a lemma about commutative append to List.thy
 noschinl parents: 
42713diff
changeset | 3912 | case False | 
| 
fcba668b0839
add a lemma about commutative append to List.thy
 noschinl parents: 
42713diff
changeset | 3913 | from `ys' = xs' @ ws` and `xs' @ ys' = ys' @ xs'` | 
| 
fcba668b0839
add a lemma about commutative append to List.thy
 noschinl parents: 
42713diff
changeset | 3914 | have "xs' @ ws = ws @ xs'" by simp | 
| 
fcba668b0839
add a lemma about commutative append to List.thy
 noschinl parents: 
42713diff
changeset | 3915 | 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: 
42713diff
changeset | 3916 | using False and `xs' \<noteq> []` and `ys' = xs' @ ws` and len | 
| 
fcba668b0839
add a lemma about commutative append to List.thy
 noschinl parents: 
42713diff
changeset | 3917 | by (intro less.hyps) auto | 
| 53374 
a14d2a854c02
tuned proofs -- clarified flow of facts wrt. calculation;
 wenzelm parents: 
53017diff
changeset | 3918 | then obtain m n zs where *: "concat (replicate m zs) = xs'" | 
| 42714 
fcba668b0839
add a lemma about commutative append to List.thy
 noschinl parents: 
42713diff
changeset | 3919 | and "concat (replicate n zs) = ws" by blast | 
| 
fcba668b0839
add a lemma about commutative append to List.thy
 noschinl parents: 
42713diff
changeset | 3920 | then have "concat (replicate (m + n) zs) = ys'" | 
| 
fcba668b0839
add a lemma about commutative append to List.thy
 noschinl parents: 
42713diff
changeset | 3921 | using `ys' = xs' @ ws` | 
| 
fcba668b0839
add a lemma about commutative append to List.thy
 noschinl parents: 
42713diff
changeset | 3922 | by (simp add: replicate_add) | 
| 53374 
a14d2a854c02
tuned proofs -- clarified flow of facts wrt. calculation;
 wenzelm parents: 
53017diff
changeset | 3923 | with * show ?thesis by blast | 
| 42714 
fcba668b0839
add a lemma about commutative append to List.thy
 noschinl parents: 
42713diff
changeset | 3924 | qed | 
| 
fcba668b0839
add a lemma about commutative append to List.thy
 noschinl parents: 
42713diff
changeset | 3925 | then show ?case | 
| 
fcba668b0839
add a lemma about commutative append to List.thy
 noschinl parents: 
42713diff
changeset | 3926 | using xs'_def ys'_def by metis | 
| 
fcba668b0839
add a lemma about commutative append to List.thy
 noschinl parents: 
42713diff
changeset | 3927 | qed | 
| 
fcba668b0839
add a lemma about commutative append to List.thy
 noschinl parents: 
42713diff
changeset | 3928 | |
| 
fcba668b0839
add a lemma about commutative append to List.thy
 noschinl parents: 
42713diff
changeset | 3929 | lemma comm_append_is_replicate: | 
| 
fcba668b0839
add a lemma about commutative append to List.thy
 noschinl parents: 
42713diff
changeset | 3930 | fixes xs ys :: "'a list" | 
| 
fcba668b0839
add a lemma about commutative append to List.thy
 noschinl parents: 
42713diff
changeset | 3931 | assumes "xs \<noteq> []" "ys \<noteq> []" | 
| 
fcba668b0839
add a lemma about commutative append to List.thy
 noschinl parents: 
42713diff
changeset | 3932 | assumes "xs @ ys = ys @ xs" | 
| 
fcba668b0839
add a lemma about commutative append to List.thy
 noschinl parents: 
42713diff
changeset | 3933 | 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: 
42713diff
changeset | 3934 | |
| 
fcba668b0839
add a lemma about commutative append to List.thy
 noschinl parents: 
42713diff
changeset | 3935 | proof - | 
| 
fcba668b0839
add a lemma about commutative append to List.thy
 noschinl parents: 
42713diff
changeset | 3936 | obtain m n zs where "concat (replicate m zs) = xs" | 
| 
fcba668b0839
add a lemma about commutative append to List.thy
 noschinl parents: 
42713diff
changeset | 3937 | and "concat (replicate n zs) = ys" | 
| 
fcba668b0839
add a lemma about commutative append to List.thy
 noschinl parents: 
42713diff
changeset | 3938 | using assms by (metis comm_append_are_replicate) | 
| 
fcba668b0839
add a lemma about commutative append to List.thy
 noschinl parents: 
42713diff
changeset | 3939 | 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: 
42713diff
changeset | 3940 | using `xs \<noteq> []` and `ys \<noteq> []` | 
| 
fcba668b0839
add a lemma about commutative append to List.thy
 noschinl parents: 
42713diff
changeset | 3941 | by (auto simp: replicate_add) | 
| 
fcba668b0839
add a lemma about commutative append to List.thy
 noschinl parents: 
42713diff
changeset | 3942 | then show ?thesis by blast | 
| 
fcba668b0839
add a lemma about commutative append to List.thy
 noschinl parents: 
42713diff
changeset | 3943 | qed | 
| 
fcba668b0839
add a lemma about commutative append to List.thy
 noschinl parents: 
42713diff
changeset | 3944 | |
| 52380 | 3945 | lemma Cons_replicate_eq: | 
| 3946 | "x # xs = replicate n y \<longleftrightarrow> x = y \<and> n > 0 \<and> xs = replicate (n - 1) x" | |
| 3947 | by (induct n) auto | |
| 3948 | ||
| 3949 | lemma replicate_length_same: | |
| 3950 | "(\<forall>y\<in>set xs. y = x) \<Longrightarrow> replicate (length xs) x = xs" | |
| 3951 | by (induct xs) simp_all | |
| 3952 | ||
| 3953 | lemma foldr_replicate [simp]: | |
| 3954 | "foldr f (replicate n x) = f x ^^ n" | |
| 3955 | by (induct n) (simp_all) | |
| 3956 | ||
| 3957 | lemma fold_replicate [simp]: | |
| 3958 | "fold f (replicate n x) = f x ^^ n" | |
| 3959 | by (subst foldr_fold [symmetric]) simp_all | |
| 3960 | ||
| 28642 | 3961 | |
| 51173 | 3962 | subsubsection {* @{const enumerate} *}
 | 
| 3963 | ||
| 3964 | lemma enumerate_simps [simp, code]: | |
| 3965 | "enumerate n [] = []" | |
| 3966 | "enumerate n (x # xs) = (n, x) # enumerate (Suc n) xs" | |
| 3967 | apply (auto simp add: enumerate_eq_zip not_le) | |
| 3968 | apply (cases "n < n + length xs") | |
| 3969 | apply (auto simp add: upt_conv_Cons) | |
| 3970 | done | |
| 3971 | ||
| 3972 | lemma length_enumerate [simp]: | |
| 3973 | "length (enumerate n xs) = length xs" | |
| 3974 | by (simp add: enumerate_eq_zip) | |
| 3975 | ||
| 3976 | lemma map_fst_enumerate [simp]: | |
| 3977 | "map fst (enumerate n xs) = [n..<n + length xs]" | |
| 3978 | by (simp add: enumerate_eq_zip) | |
| 3979 | ||
| 3980 | lemma map_snd_enumerate [simp]: | |
| 3981 | "map snd (enumerate n xs) = xs" | |
| 3982 | by (simp add: enumerate_eq_zip) | |
| 3983 | ||
| 3984 | lemma in_set_enumerate_eq: | |
| 3985 | "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" | |
| 3986 | proof - | |
| 3987 |   { fix m
 | |
| 3988 | assume "n \<le> m" | |
| 3989 | moreover assume "m < length xs + n" | |
| 3990 | ultimately have "[n..<n + length xs] ! (m - n) = m \<and> | |
| 3991 | xs ! (m - n) = xs ! (m - n) \<and> m - n < length xs" by auto | |
| 3992 | then have "\<exists>q. [n..<n + length xs] ! q = m \<and> | |
| 3993 | xs ! q = xs ! (m - n) \<and> q < length xs" .. | |
| 3994 | } then show ?thesis by (cases p) (auto simp add: enumerate_eq_zip in_set_zip) | |
| 3995 | qed | |
| 3996 | ||
| 3997 | lemma nth_enumerate_eq: | |
| 3998 | assumes "m < length xs" | |
| 3999 | shows "enumerate n xs ! m = (n + m, xs ! m)" | |
| 4000 | using assms by (simp add: enumerate_eq_zip) | |
| 4001 | ||
| 4002 | lemma enumerate_replicate_eq: | |
| 4003 | "enumerate n (replicate m a) = map (\<lambda>q. (q, a)) [n..<n + m]" | |
| 4004 | by (rule pair_list_eqI) | |
| 4005 | (simp_all add: enumerate_eq_zip comp_def map_replicate_const) | |
| 4006 | ||
| 4007 | lemma enumerate_Suc_eq: | |
| 4008 | "enumerate (Suc n) xs = map (apfst Suc) (enumerate n xs)" | |
| 4009 | by (rule pair_list_eqI) | |
| 4010 | (simp_all add: not_le, simp del: map_map [simp del] add: map_Suc_upt map_map [symmetric]) | |
| 4011 | ||
| 52379 | 4012 | lemma distinct_enumerate [simp]: | 
| 4013 | "distinct (enumerate n xs)" | |
| 4014 | by (simp add: enumerate_eq_zip distinct_zipI1) | |
| 4015 | ||
| 51173 | 4016 | |
| 49948 
744934b818c7
moved quite generic material from theory Enum to more appropriate places
 haftmann parents: 
49808diff
changeset | 4017 | subsubsection {* @{const rotate1} and @{const rotate} *}
 | 
| 15302 | 4018 | |
| 4019 | lemma rotate0[simp]: "rotate 0 = id" | |
| 4020 | by(simp add:rotate_def) | |
| 4021 | ||
| 4022 | lemma rotate_Suc[simp]: "rotate (Suc n) xs = rotate1(rotate n xs)" | |
| 4023 | by(simp add:rotate_def) | |
| 4024 | ||
| 4025 | lemma rotate_add: | |
| 4026 | "rotate (m+n) = rotate m o rotate n" | |
| 4027 | by(simp add:rotate_def funpow_add) | |
| 4028 | ||
| 4029 | lemma rotate_rotate: "rotate m (rotate n xs) = rotate (m+n) xs" | |
| 4030 | by(simp add:rotate_add) | |
| 4031 | ||
| 18049 | 4032 | lemma rotate1_rotate_swap: "rotate1 (rotate n xs) = rotate n (rotate1 xs)" | 
| 4033 | by(simp add:rotate_def funpow_swap1) | |
| 4034 | ||
| 15302 | 4035 | lemma rotate1_length01[simp]: "length xs <= 1 \<Longrightarrow> rotate1 xs = xs" | 
| 4036 | by(cases xs) simp_all | |
| 4037 | ||
| 4038 | lemma rotate_length01[simp]: "length xs <= 1 \<Longrightarrow> rotate n xs = xs" | |
| 4039 | apply(induct n) | |
| 4040 | apply simp | |
| 4041 | apply (simp add:rotate_def) | |
| 13145 | 4042 | done | 
| 13114 | 4043 | |
| 15302 | 4044 | 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: 
46439diff
changeset | 4045 | by (cases xs) simp_all | 
| 15302 | 4046 | |
| 4047 | lemma rotate_drop_take: | |
| 4048 | "rotate n xs = drop (n mod length xs) xs @ take (n mod length xs) xs" | |
| 4049 | apply(induct n) | |
| 4050 | apply simp | |
| 4051 | apply(simp add:rotate_def) | |
| 4052 | apply(cases "xs = []") | |
| 4053 | apply (simp) | |
| 4054 | apply(case_tac "n mod length xs = 0") | |
| 4055 | apply(simp add:mod_Suc) | |
| 4056 | apply(simp add: rotate1_hd_tl drop_Suc take_Suc) | |
| 4057 | apply(simp add:mod_Suc rotate1_hd_tl drop_Suc[symmetric] drop_tl[symmetric] | |
| 4058 | take_hd_drop linorder_not_le) | |
| 13145 | 4059 | done | 
| 13114 | 4060 | |
| 15302 | 4061 | lemma rotate_conv_mod: "rotate n xs = rotate (n mod length xs) xs" | 
| 4062 | by(simp add:rotate_drop_take) | |
| 4063 | ||
| 4064 | lemma rotate_id[simp]: "n mod length xs = 0 \<Longrightarrow> rotate n xs = xs" | |
| 4065 | by(simp add:rotate_drop_take) | |
| 4066 | ||
| 4067 | 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: 
46439diff
changeset | 4068 | by (cases xs) simp_all | 
| 15302 | 4069 | |
| 24526 | 4070 | lemma length_rotate[simp]: "length(rotate n xs) = length xs" | 
| 4071 | by (induct n arbitrary: xs) (simp_all add:rotate_def) | |
| 15302 | 4072 | |
| 4073 | 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: 
46439diff
changeset | 4074 | by (cases xs) auto | 
| 15302 | 4075 | |
| 4076 | lemma distinct_rotate[simp]: "distinct(rotate n xs) = distinct xs" | |
| 4077 | by (induct n) (simp_all add:rotate_def) | |
| 4078 | ||
| 4079 | lemma rotate_map: "rotate n (map f xs) = map f (rotate n xs)" | |
| 4080 | by(simp add:rotate_drop_take take_map drop_map) | |
| 4081 | ||
| 4082 | 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: 
46439diff
changeset | 4083 | by (cases xs) auto | 
| 15302 | 4084 | |
| 4085 | lemma set_rotate[simp]: "set(rotate n xs) = set xs" | |
| 4086 | by (induct n) (simp_all add:rotate_def) | |
| 4087 | ||
| 4088 | 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: 
46439diff
changeset | 4089 | by (cases xs) auto | 
| 15302 | 4090 | |
| 4091 | lemma rotate_is_Nil_conv[simp]: "(rotate n xs = []) = (xs = [])" | |
| 4092 | by (induct n) (simp_all add:rotate_def) | |
| 13114 | 4093 | |
| 15439 | 4094 | lemma rotate_rev: | 
| 4095 | "rotate n (rev xs) = rev(rotate (length xs - (n mod length xs)) xs)" | |
| 4096 | apply(simp add:rotate_drop_take rev_drop rev_take) | |
| 4097 | apply(cases "length xs = 0") | |
| 4098 | apply simp | |
| 4099 | apply(cases "n mod length xs = 0") | |
| 4100 | apply simp | |
| 4101 | apply(simp add:rotate_drop_take rev_drop rev_take) | |
| 4102 | done | |
| 4103 | ||
| 18423 | 4104 | lemma hd_rotate_conv_nth: "xs \<noteq> [] \<Longrightarrow> hd(rotate n xs) = xs!(n mod length xs)" | 
| 4105 | apply(simp add:rotate_drop_take hd_append hd_drop_conv_nth hd_conv_nth) | |
| 4106 | apply(subgoal_tac "length xs \<noteq> 0") | |
| 4107 | prefer 2 apply simp | |
| 4108 | using mod_less_divisor[of "length xs" n] by arith | |
| 4109 | ||
| 13114 | 4110 | |
| 49948 
744934b818c7
moved quite generic material from theory Enum to more appropriate places
 haftmann parents: 
49808diff
changeset | 4111 | subsubsection {* @{const sublist} --- a generalization of @{const nth} to sets *}
 | 
| 13114 | 4112 | |
| 13142 | 4113 | lemma sublist_empty [simp]: "sublist xs {} = []"
 | 
| 13145 | 4114 | by (auto simp add: sublist_def) | 
| 13114 | 4115 | |
| 13142 | 4116 | lemma sublist_nil [simp]: "sublist [] A = []" | 
| 13145 | 4117 | by (auto simp add: sublist_def) | 
| 13114 | 4118 | |
| 15281 | 4119 | lemma length_sublist: | 
| 4120 |   "length(sublist xs I) = card{i. i < length xs \<and> i : I}"
 | |
| 4121 | by(simp add: sublist_def length_filter_conv_card cong:conj_cong) | |
| 4122 | ||
| 4123 | lemma sublist_shift_lemma_Suc: | |
| 24526 | 4124 | "map fst (filter (%p. P(Suc(snd p))) (zip xs is)) = | 
| 4125 | map fst (filter (%p. P(snd p)) (zip xs (map Suc is)))" | |
| 4126 | apply(induct xs arbitrary: "is") | |
| 15281 | 4127 | apply simp | 
| 4128 | apply (case_tac "is") | |
| 4129 | apply simp | |
| 4130 | apply simp | |
| 4131 | done | |
| 4132 | ||
| 13114 | 4133 | lemma sublist_shift_lemma: | 
| 23279 
e39dd93161d9
tuned list comprehension, changed filter syntax from : to <-
 nipkow parents: 
23246diff
changeset | 4134 | "map fst [p<-zip xs [i..<i + length xs] . snd p : A] = | 
| 
e39dd93161d9
tuned list comprehension, changed filter syntax from : to <-
 nipkow parents: 
23246diff
changeset | 4135 | map fst [p<-zip xs [0..<length xs] . snd p + i : A]" | 
| 13145 | 4136 | by (induct xs rule: rev_induct) (simp_all add: add_commute) | 
| 13114 | 4137 | |
| 4138 | lemma sublist_append: | |
| 15168 | 4139 |      "sublist (l @ l') A = sublist l A @ sublist l' {j. j + length l : A}"
 | 
| 13145 | 4140 | apply (unfold sublist_def) | 
| 14208 | 4141 | apply (induct l' rule: rev_induct, simp) | 
| 44921 | 4142 | apply (simp add: upt_add_eq_append[of 0] sublist_shift_lemma) | 
| 13145 | 4143 | apply (simp add: add_commute) | 
| 4144 | done | |
| 13114 | 4145 | |
| 4146 | lemma sublist_Cons: | |
| 13145 | 4147 | "sublist (x # l) A = (if 0:A then [x] else []) @ sublist l {j. Suc j : A}"
 | 
| 4148 | apply (induct l rule: rev_induct) | |
| 4149 | apply (simp add: sublist_def) | |
| 4150 | apply (simp del: append_Cons add: append_Cons[symmetric] sublist_append) | |
| 4151 | done | |
| 13114 | 4152 | |
| 24526 | 4153 | lemma set_sublist: "set(sublist xs I) = {xs!i|i. i<size xs \<and> i \<in> I}"
 | 
| 4154 | apply(induct xs arbitrary: I) | |
| 25162 | 4155 | apply(auto simp: sublist_Cons nth_Cons split:nat.split dest!: gr0_implies_Suc) | 
| 15281 | 4156 | done | 
| 4157 | ||
| 4158 | lemma set_sublist_subset: "set(sublist xs I) \<subseteq> set xs" | |
| 4159 | by(auto simp add:set_sublist) | |
| 4160 | ||
| 4161 | lemma notin_set_sublistI[simp]: "x \<notin> set xs \<Longrightarrow> x \<notin> set(sublist xs I)" | |
| 4162 | by(auto simp add:set_sublist) | |
| 4163 | ||
| 4164 | lemma in_set_sublistD: "x \<in> set(sublist xs I) \<Longrightarrow> x \<in> set xs" | |
| 4165 | by(auto simp add:set_sublist) | |
| 4166 | ||
| 13142 | 4167 | lemma sublist_singleton [simp]: "sublist [x] A = (if 0 : A then [x] else [])" | 
| 13145 | 4168 | by (simp add: sublist_Cons) | 
| 13114 | 4169 | |
| 15281 | 4170 | |
| 24526 | 4171 | lemma distinct_sublistI[simp]: "distinct xs \<Longrightarrow> distinct(sublist xs I)" | 
| 4172 | apply(induct xs arbitrary: I) | |
| 15281 | 4173 | apply simp | 
| 4174 | apply(auto simp add:sublist_Cons) | |
| 4175 | done | |
| 4176 | ||
| 4177 | ||
| 15045 | 4178 | lemma sublist_upt_eq_take [simp]: "sublist l {..<n} = take n l"
 | 
| 14208 | 4179 | apply (induct l rule: rev_induct, simp) | 
| 13145 | 4180 | apply (simp split: nat_diff_split add: sublist_append) | 
| 4181 | done | |
| 13114 | 4182 | |
| 24526 | 4183 | lemma filter_in_sublist: | 
| 4184 | "distinct xs \<Longrightarrow> filter (%x. x \<in> set(sublist xs s)) xs = sublist xs s" | |
| 4185 | proof (induct xs arbitrary: s) | |
| 17501 | 4186 | case Nil thus ?case by simp | 
| 4187 | next | |
| 4188 | case (Cons a xs) | |
| 53374 
a14d2a854c02
tuned proofs -- clarified flow of facts wrt. calculation;
 wenzelm parents: 
53017diff
changeset | 4189 | then have "!x. x: set xs \<longrightarrow> x \<noteq> a" by auto | 
| 
a14d2a854c02
tuned proofs -- clarified flow of facts wrt. calculation;
 wenzelm parents: 
53017diff
changeset | 4190 | with Cons show ?case by(simp add: sublist_Cons cong:filter_cong) | 
| 17501 | 4191 | qed | 
| 4192 | ||
| 13114 | 4193 | |
| 49948 
744934b818c7
moved quite generic material from theory Enum to more appropriate places
 haftmann parents: 
49808diff
changeset | 4194 | subsubsection {* @{const sublists} and @{const List.n_lists} *}
 | 
| 
744934b818c7
moved quite generic material from theory Enum to more appropriate places
 haftmann parents: 
49808diff
changeset | 4195 | |
| 
744934b818c7
moved quite generic material from theory Enum to more appropriate places
 haftmann parents: 
49808diff
changeset | 4196 | lemma length_sublists: | 
| 
744934b818c7
moved quite generic material from theory Enum to more appropriate places
 haftmann parents: 
49808diff
changeset | 4197 | "length (sublists xs) = 2 ^ length xs" | 
| 
744934b818c7
moved quite generic material from theory Enum to more appropriate places
 haftmann parents: 
49808diff
changeset | 4198 | by (induct xs) (simp_all add: Let_def) | 
| 
744934b818c7
moved quite generic material from theory Enum to more appropriate places
 haftmann parents: 
49808diff
changeset | 4199 | |
| 
744934b818c7
moved quite generic material from theory Enum to more appropriate places
 haftmann parents: 
49808diff
changeset | 4200 | lemma sublists_powset: | 
| 
744934b818c7
moved quite generic material from theory Enum to more appropriate places
 haftmann parents: 
49808diff
changeset | 4201 | "set ` set (sublists xs) = Pow (set xs)" | 
| 
744934b818c7
moved quite generic material from theory Enum to more appropriate places
 haftmann parents: 
49808diff
changeset | 4202 | proof - | 
| 
744934b818c7
moved quite generic material from theory Enum to more appropriate places
 haftmann parents: 
49808diff
changeset | 4203 | 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: 
49808diff
changeset | 4204 | by (auto simp add: image_def) | 
| 
744934b818c7
moved quite generic material from theory Enum to more appropriate places
 haftmann parents: 
49808diff
changeset | 4205 | have "set (map set (sublists xs)) = Pow (set xs)" | 
| 
744934b818c7
moved quite generic material from theory Enum to more appropriate places
 haftmann parents: 
49808diff
changeset | 4206 | by (induct xs) | 
| 
744934b818c7
moved quite generic material from theory Enum to more appropriate places
 haftmann parents: 
49808diff
changeset | 4207 | (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: 
49808diff
changeset | 4208 | then show ?thesis by simp | 
| 
744934b818c7
moved quite generic material from theory Enum to more appropriate places
 haftmann parents: 
49808diff
changeset | 4209 | qed | 
| 
744934b818c7
moved quite generic material from theory Enum to more appropriate places
 haftmann parents: 
49808diff
changeset | 4210 | |
| 
744934b818c7
moved quite generic material from theory Enum to more appropriate places
 haftmann parents: 
49808diff
changeset | 4211 | lemma distinct_set_sublists: | 
| 
744934b818c7
moved quite generic material from theory Enum to more appropriate places
 haftmann parents: 
49808diff
changeset | 4212 | assumes "distinct xs" | 
| 
744934b818c7
moved quite generic material from theory Enum to more appropriate places
 haftmann parents: 
49808diff
changeset | 4213 | shows "distinct (map set (sublists xs))" | 
| 
744934b818c7
moved quite generic material from theory Enum to more appropriate places
 haftmann parents: 
49808diff
changeset | 4214 | proof (rule card_distinct) | 
| 
744934b818c7
moved quite generic material from theory Enum to more appropriate places
 haftmann parents: 
49808diff
changeset | 4215 | have "finite (set xs)" by rule | 
| 
744934b818c7
moved quite generic material from theory Enum to more appropriate places
 haftmann parents: 
49808diff
changeset | 4216 | 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: 
49808diff
changeset | 4217 | with assms distinct_card [of xs] | 
| 
744934b818c7
moved quite generic material from theory Enum to more appropriate places
 haftmann parents: 
49808diff
changeset | 4218 | have "card (Pow (set xs)) = 2 ^ length xs" by simp | 
| 
744934b818c7
moved quite generic material from theory Enum to more appropriate places
 haftmann parents: 
49808diff
changeset | 4219 | 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: 
49808diff
changeset | 4220 | by (simp add: sublists_powset length_sublists) | 
| 
744934b818c7
moved quite generic material from theory Enum to more appropriate places
 haftmann parents: 
49808diff
changeset | 4221 | qed | 
| 
744934b818c7
moved quite generic material from theory Enum to more appropriate places
 haftmann parents: 
49808diff
changeset | 4222 | |
| 
744934b818c7
moved quite generic material from theory Enum to more appropriate places
 haftmann parents: 
49808diff
changeset | 4223 | 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: 
49808diff
changeset | 4224 | by (induct n) simp_all | 
| 
744934b818c7
moved quite generic material from theory Enum to more appropriate places
 haftmann parents: 
49808diff
changeset | 4225 | |
| 
744934b818c7
moved quite generic material from theory Enum to more appropriate places
 haftmann parents: 
49808diff
changeset | 4226 | 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: 
49808diff
changeset | 4227 | 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: 
49808diff
changeset | 4228 | |
| 
744934b818c7
moved quite generic material from theory Enum to more appropriate places
 haftmann parents: 
49808diff
changeset | 4229 | 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: 
49808diff
changeset | 4230 | by (induct n arbitrary: ys) auto | 
| 
744934b818c7
moved quite generic material from theory Enum to more appropriate places
 haftmann parents: 
49808diff
changeset | 4231 | |
| 
744934b818c7
moved quite generic material from theory Enum to more appropriate places
 haftmann parents: 
49808diff
changeset | 4232 | 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: 
49808diff
changeset | 4233 | proof (rule set_eqI) | 
| 
744934b818c7
moved quite generic material from theory Enum to more appropriate places
 haftmann parents: 
49808diff
changeset | 4234 | fix ys :: "'a list" | 
| 
744934b818c7
moved quite generic material from theory Enum to more appropriate places
 haftmann parents: 
49808diff
changeset | 4235 |   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: 
49808diff
changeset | 4236 | proof - | 
| 
744934b818c7
moved quite generic material from theory Enum to more appropriate places
 haftmann parents: 
49808diff
changeset | 4237 | 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: 
49808diff
changeset | 4238 | by (induct n arbitrary: ys) auto | 
| 
744934b818c7
moved quite generic material from theory Enum to more appropriate places
 haftmann parents: 
49808diff
changeset | 4239 | 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: 
49808diff
changeset | 4240 | by (induct n arbitrary: ys) auto | 
| 
744934b818c7
moved quite generic material from theory Enum to more appropriate places
 haftmann parents: 
49808diff
changeset | 4241 | 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: 
49808diff
changeset | 4242 | by (induct ys) auto | 
| 
744934b818c7
moved quite generic material from theory Enum to more appropriate places
 haftmann parents: 
49808diff
changeset | 4243 | ultimately show ?thesis by auto | 
| 
744934b818c7
moved quite generic material from theory Enum to more appropriate places
 haftmann parents: 
49808diff
changeset | 4244 | qed | 
| 
744934b818c7
moved quite generic material from theory Enum to more appropriate places
 haftmann parents: 
49808diff
changeset | 4245 | qed | 
| 
744934b818c7
moved quite generic material from theory Enum to more appropriate places
 haftmann parents: 
49808diff
changeset | 4246 | |
| 
744934b818c7
moved quite generic material from theory Enum to more appropriate places
 haftmann parents: 
49808diff
changeset | 4247 | lemma distinct_n_lists: | 
| 
744934b818c7
moved quite generic material from theory Enum to more appropriate places
 haftmann parents: 
49808diff
changeset | 4248 | assumes "distinct xs" | 
| 
744934b818c7
moved quite generic material from theory Enum to more appropriate places
 haftmann parents: 
49808diff
changeset | 4249 | shows "distinct (List.n_lists n xs)" | 
| 
744934b818c7
moved quite generic material from theory Enum to more appropriate places
 haftmann parents: 
49808diff
changeset | 4250 | proof (rule card_distinct) | 
| 
744934b818c7
moved quite generic material from theory Enum to more appropriate places
 haftmann parents: 
49808diff
changeset | 4251 | 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: 
49808diff
changeset | 4252 | 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: 
49808diff
changeset | 4253 | proof (induct n) | 
| 
744934b818c7
moved quite generic material from theory Enum to more appropriate places
 haftmann parents: 
49808diff
changeset | 4254 | case 0 then show ?case by simp | 
| 
744934b818c7
moved quite generic material from theory Enum to more appropriate places
 haftmann parents: 
49808diff
changeset | 4255 | next | 
| 
744934b818c7
moved quite generic material from theory Enum to more appropriate places
 haftmann parents: 
49808diff
changeset | 4256 | case (Suc n) | 
| 
744934b818c7
moved quite generic material from theory Enum to more appropriate places
 haftmann parents: 
49808diff
changeset | 4257 | 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: 
49808diff
changeset | 4258 | = (\<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: 
49808diff
changeset | 4259 | by (rule card_UN_disjoint) auto | 
| 
744934b818c7
moved quite generic material from theory Enum to more appropriate places
 haftmann parents: 
49808diff
changeset | 4260 | 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: 
49808diff
changeset | 4261 | by (rule card_image) (simp add: inj_on_def) | 
| 
744934b818c7
moved quite generic material from theory Enum to more appropriate places
 haftmann parents: 
49808diff
changeset | 4262 | ultimately show ?case by auto | 
| 
744934b818c7
moved quite generic material from theory Enum to more appropriate places
 haftmann parents: 
49808diff
changeset | 4263 | qed | 
| 
744934b818c7
moved quite generic material from theory Enum to more appropriate places
 haftmann parents: 
49808diff
changeset | 4264 | 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: 
49808diff
changeset | 4265 | 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: 
49808diff
changeset | 4266 | by (simp add: length_n_lists) | 
| 
744934b818c7
moved quite generic material from theory Enum to more appropriate places
 haftmann parents: 
49808diff
changeset | 4267 | qed | 
| 
744934b818c7
moved quite generic material from theory Enum to more appropriate places
 haftmann parents: 
49808diff
changeset | 4268 | |
| 
744934b818c7
moved quite generic material from theory Enum to more appropriate places
 haftmann parents: 
49808diff
changeset | 4269 | |
| 19390 | 4270 | subsubsection {* @{const splice} *}
 | 
| 4271 | ||
| 40593 
1e57b18d27b1
code eqn for slice was missing; redefined splice with fun
 nipkow parents: 
40365diff
changeset | 4272 | lemma splice_Nil2 [simp, code]: "splice xs [] = xs" | 
| 19390 | 4273 | by (cases xs) simp_all | 
| 4274 | ||
| 40593 
1e57b18d27b1
code eqn for slice was missing; redefined splice with fun
 nipkow parents: 
40365diff
changeset | 4275 | declare splice.simps(1,3)[code] | 
| 
1e57b18d27b1
code eqn for slice was missing; redefined splice with fun
 nipkow parents: 
40365diff
changeset | 4276 | declare splice.simps(2)[simp del] | 
| 19390 | 4277 | |
| 24526 | 4278 | 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: 
40365diff
changeset | 4279 | by (induct xs ys rule: splice.induct) auto | 
| 22793 | 4280 | |
| 35115 | 4281 | |
| 4282 | subsubsection {* Transpose *}
 | |
| 34933 | 4283 | |
| 4284 | function transpose where | |
| 4285 | "transpose [] = []" | | |
| 4286 | "transpose ([] # xss) = transpose xss" | | |
| 4287 | "transpose ((x#xs) # xss) = | |
| 4288 | (x # [h. (h#t) \<leftarrow> xss]) # transpose (xs # [t. (h#t) \<leftarrow> xss])" | |
| 4289 | by pat_completeness auto | |
| 4290 | ||
| 4291 | 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: 
55148diff
changeset | 4292 | "concat (map (case_list [] (\<lambda>h t. [h])) xss) = | 
| 34933 | 4293 | map (\<lambda>xs. hd xs) [ys\<leftarrow>xss . ys \<noteq> []]" | 
| 4294 | by (induct xss) (auto split: list.split) | |
| 4295 | ||
| 4296 | 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: 
55148diff
changeset | 4297 | "concat (map (case_list [] (\<lambda>h t. [t])) xss) = | 
| 34933 | 4298 | map (\<lambda>xs. tl xs) [ys\<leftarrow>xss . ys \<noteq> []]" | 
| 4299 | by (induct xss) (auto split: list.split) | |
| 4300 | ||
| 4301 | lemma transpose_aux_max: | |
| 4302 | "max (Suc (length xs)) (foldr (\<lambda>xs. max (length xs)) xss 0) = | |
| 4303 | Suc (max (length xs) (foldr (\<lambda>x. max (length x - Suc 0)) [ys\<leftarrow>xss . ys\<noteq>[]] 0))" | |
| 4304 | (is "max _ ?foldB = Suc (max _ ?foldA)") | |
| 4305 | proof (cases "[ys\<leftarrow>xss . ys\<noteq>[]] = []") | |
| 4306 | case True | |
| 4307 | hence "foldr (\<lambda>xs. max (length xs)) xss 0 = 0" | |
| 4308 | proof (induct xss) | |
| 4309 | case (Cons x xs) | |
| 53374 
a14d2a854c02
tuned proofs -- clarified flow of facts wrt. calculation;
 wenzelm parents: 
53017diff
changeset | 4310 | then have "x = []" by (cases x) auto | 
| 
a14d2a854c02
tuned proofs -- clarified flow of facts wrt. calculation;
 wenzelm parents: 
53017diff
changeset | 4311 | with Cons show ?case by auto | 
| 34933 | 4312 | qed simp | 
| 4313 | thus ?thesis using True by simp | |
| 4314 | next | |
| 4315 | case False | |
| 4316 | ||
| 4317 | have foldA: "?foldA = foldr (\<lambda>x. max (length x)) [ys\<leftarrow>xss . ys \<noteq> []] 0 - 1" | |
| 4318 | by (induct xss) auto | |
| 4319 | have foldB: "?foldB = foldr (\<lambda>x. max (length x)) [ys\<leftarrow>xss . ys \<noteq> []] 0" | |
| 4320 | by (induct xss) auto | |
| 4321 | ||
| 4322 | have "0 < ?foldB" | |
| 4323 | proof - | |
| 4324 | from False | |
| 4325 | obtain z zs where zs: "[ys\<leftarrow>xss . ys \<noteq> []] = z#zs" by (auto simp: neq_Nil_conv) | |
| 4326 | hence "z \<in> set ([ys\<leftarrow>xss . ys \<noteq> []])" by auto | |
| 4327 | hence "z \<noteq> []" by auto | |
| 4328 | thus ?thesis | |
| 4329 | unfolding foldB zs | |
| 4330 | by (auto simp: max_def intro: less_le_trans) | |
| 4331 | qed | |
| 4332 | thus ?thesis | |
| 4333 | unfolding foldA foldB max_Suc_Suc[symmetric] | |
| 4334 | by simp | |
| 4335 | qed | |
| 4336 | ||
| 4337 | termination transpose | |
| 4338 | by (relation "measure (\<lambda>xs. foldr (\<lambda>xs. max (length xs)) xs 0 + length xs)") | |
| 4339 | (auto simp: transpose_aux_filter_tail foldr_map comp_def transpose_aux_max less_Suc_eq_le) | |
| 4340 | ||
| 4341 | lemma transpose_empty: "(transpose xs = []) \<longleftrightarrow> (\<forall>x \<in> set xs. x = [])" | |
| 4342 | by (induct rule: transpose.induct) simp_all | |
| 4343 | ||
| 4344 | lemma length_transpose: | |
| 4345 | fixes xs :: "'a list list" | |
| 4346 | shows "length (transpose xs) = foldr (\<lambda>xs. max (length xs)) xs 0" | |
| 4347 | by (induct rule: transpose.induct) | |
| 4348 | (auto simp: transpose_aux_filter_tail foldr_map comp_def transpose_aux_max | |
| 4349 | max_Suc_Suc[symmetric] simp del: max_Suc_Suc) | |
| 4350 | ||
| 4351 | lemma nth_transpose: | |
| 4352 | fixes xs :: "'a list list" | |
| 4353 | assumes "i < length (transpose xs)" | |
| 4354 | shows "transpose xs ! i = map (\<lambda>xs. xs ! i) [ys \<leftarrow> xs. i < length ys]" | |
| 4355 | using assms proof (induct arbitrary: i rule: transpose.induct) | |
| 4356 | case (3 x xs xss) | |
| 4357 | def XS == "(x # xs) # xss" | |
| 4358 | hence [simp]: "XS \<noteq> []" by auto | |
| 4359 | thus ?case | |
| 4360 | proof (cases i) | |
| 4361 | case 0 | |
| 4362 | thus ?thesis by (simp add: transpose_aux_filter_head hd_conv_nth) | |
| 4363 | next | |
| 4364 | case (Suc j) | |
| 4365 | have *: "\<And>xss. xs # map tl xss = map tl ((x#xs)#xss)" by simp | |
| 4366 | have **: "\<And>xss. (x#xs) # filter (\<lambda>ys. ys \<noteq> []) xss = filter (\<lambda>ys. ys \<noteq> []) ((x#xs)#xss)" by simp | |
| 4367 |     { fix x have "Suc j < length x \<longleftrightarrow> x \<noteq> [] \<and> j < length x - Suc 0"
 | |
| 4368 | by (cases x) simp_all | |
| 4369 | } note *** = this | |
| 4370 | ||
| 55404 
5cb95b79a51f
transformed 'option' and 'list' into new-style datatypes (but register them as old-style as well)
 blanchet parents: 
55148diff
changeset | 4371 | have j_less: "j < length (transpose (xs # concat (map (case_list [] (\<lambda>h t. [t])) xss)))" | 
| 34933 | 4372 | using "3.prems" by (simp add: transpose_aux_filter_tail length_transpose Suc) | 
| 4373 | ||
| 4374 | show ?thesis | |
| 4375 | unfolding transpose.simps `i = Suc j` nth_Cons_Suc "3.hyps"[OF j_less] | |
| 4376 | 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: 
55148diff
changeset | 4377 | apply (rule list.exhaust) | 
| 34933 | 4378 | by auto | 
| 4379 | qed | |
| 4380 | qed simp_all | |
| 4381 | ||
| 4382 | lemma transpose_map_map: | |
| 4383 | "transpose (map (map f) xs) = map (map f) (transpose xs)" | |
| 4384 | proof (rule nth_equalityI, safe) | |
| 4385 | have [simp]: "length (transpose (map (map f) xs)) = length (transpose xs)" | |
| 4386 | by (simp add: length_transpose foldr_map comp_def) | |
| 4387 | show "length (transpose (map (map f) xs)) = length (map (map f) (transpose xs))" by simp | |
| 4388 | ||
| 4389 | fix i assume "i < length (transpose (map (map f) xs))" | |
| 4390 | thus "transpose (map (map f) xs) ! i = map (map f) (transpose xs) ! i" | |
| 4391 | by (simp add: nth_transpose filter_map comp_def) | |
| 4392 | qed | |
| 24616 | 4393 | |
| 35115 | 4394 | |
| 31557 | 4395 | subsubsection {* (In)finiteness *}
 | 
| 28642 | 4396 | |
| 4397 | lemma finite_maxlen: | |
| 4398 | "finite (M::'a list set) ==> EX n. ALL s:M. size s < n" | |
| 4399 | proof (induct rule: finite.induct) | |
| 4400 | case emptyI show ?case by simp | |
| 4401 | next | |
| 4402 | case (insertI M xs) | |
| 4403 | then obtain n where "\<forall>s\<in>M. length s < n" by blast | |
| 4404 | hence "ALL s:insert xs M. size s < max n (size xs) + 1" by auto | |
| 4405 | thus ?case .. | |
| 4406 | qed | |
| 4407 | ||
| 45714 | 4408 | lemma lists_length_Suc_eq: | 
| 4409 |   "{xs. set xs \<subseteq> A \<and> length xs = Suc n} =
 | |
| 4410 |     (\<lambda>(xs, n). n#xs) ` ({xs. set xs \<subseteq> A \<and> length xs = n} \<times> A)"
 | |
| 4411 | by (auto simp: length_Suc_conv) | |
| 4412 | ||
| 4413 | lemma | |
| 4414 | assumes "finite A" | |
| 4415 |   shows finite_lists_length_eq: "finite {xs. set xs \<subseteq> A \<and> length xs = n}"
 | |
| 4416 |   and card_lists_length_eq: "card {xs. set xs \<subseteq> A \<and> length xs = n} = (card A)^n"
 | |
| 4417 | using `finite A` | |
| 4418 | by (induct n) | |
| 4419 | (auto simp: card_image inj_split_Cons lists_length_Suc_eq cong: conj_cong) | |
| 31557 | 4420 | |
| 4421 | lemma finite_lists_length_le: | |
| 4422 |   assumes "finite A" shows "finite {xs. set xs \<subseteq> A \<and> length xs \<le> n}"
 | |
| 4423 | (is "finite ?S") | |
| 4424 | proof- | |
| 4425 |   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: 
49963diff
changeset | 4426 | thus ?thesis by (auto intro!: finite_lists_length_eq[OF `finite A`] simp only:) | 
| 31557 | 4427 | qed | 
| 4428 | ||
| 45714 | 4429 | lemma card_lists_length_le: | 
| 4430 |   assumes "finite A" shows "card {xs. set xs \<subseteq> A \<and> length xs \<le> n} = (\<Sum>i\<le>n. card A^i)"
 | |
| 4431 | proof - | |
| 4432 |   have "(\<Sum>i\<le>n. card A^i) = card (\<Union>i\<le>n. {xs. set xs \<subseteq> A \<and> length xs = i})"
 | |
| 4433 | using `finite A` | |
| 4434 | by (subst card_UN_disjoint) | |
| 4435 | (auto simp add: card_lists_length_eq finite_lists_length_eq) | |
| 4436 |   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}"
 | |
| 4437 | by auto | |
| 4438 | finally show ?thesis by simp | |
| 4439 | qed | |
| 4440 | ||
| 45932 | 4441 | lemma card_lists_distinct_length_eq: | 
| 4442 | assumes "k < card A" | |
| 4443 |   shows "card {xs. length xs = k \<and> distinct xs \<and> set xs \<subseteq> A} = \<Prod>{card A - k + 1 .. card A}"
 | |
| 4444 | using assms | |
| 4445 | proof (induct k) | |
| 4446 | case 0 | |
| 4447 |   then have "{xs. length xs = 0 \<and> distinct xs \<and> set xs \<subseteq> A} = {[]}" by auto
 | |
| 4448 | then show ?case by simp | |
| 4449 | next | |
| 4450 | case (Suc k) | |
| 4451 | let "?k_list" = "\<lambda>k xs. length xs = k \<and> distinct xs \<and> set xs \<subseteq> A" | |
| 4452 | have inj_Cons: "\<And>A. inj_on (\<lambda>(xs, n). n # xs) A" by (rule inj_onI) auto | |
| 4453 | ||
| 4454 | from Suc have "k < card A" by simp | |
| 4455 | moreover have "finite A" using assms by (simp add: card_ge_0_finite) | |
| 4456 |   moreover have "finite {xs. ?k_list k xs}"
 | |
| 4457 | using finite_lists_length_eq[OF `finite A`, of k] | |
| 4458 | by - (rule finite_subset, auto) | |
| 4459 |   moreover have "\<And>i j. i \<noteq> j \<longrightarrow> {i} \<times> (A - set i) \<inter> {j} \<times> (A - set j) = {}"
 | |
| 4460 | by auto | |
| 4461 | moreover have "\<And>i. i \<in>Collect (?k_list k) \<Longrightarrow> card (A - set i) = card A - k" | |
| 4462 | by (simp add: card_Diff_subset distinct_card) | |
| 4463 |   moreover have "{xs. ?k_list (Suc k) xs} =
 | |
| 52141 
eff000cab70f
weaker precendence of syntax for big intersection and union on sets
 haftmann parents: 
52131diff
changeset | 4464 |       (\<lambda>(xs, n). n#xs) ` \<Union>((\<lambda>xs. {xs} \<times> (A - set xs)) ` {xs. ?k_list k xs})"
 | 
| 45932 | 4465 | by (auto simp: length_Suc_conv) | 
| 4466 | moreover | |
| 4467 | have "Suc (card A - Suc k) = card A - k" using Suc.prems by simp | |
| 4468 |   then have "(card A - k) * \<Prod>{Suc (card A - k)..card A} = \<Prod>{Suc (card A - Suc k)..card A}"
 | |
| 4469 | by (subst setprod_insert[symmetric]) (simp add: atLeastAtMost_insertL)+ | |
| 4470 | ultimately show ?case | |
| 4471 | by (simp add: card_image inj_Cons card_UN_disjoint Suc.hyps algebra_simps) | |
| 4472 | qed | |
| 4473 | ||
| 28642 | 4474 | lemma infinite_UNIV_listI: "~ finite(UNIV::'a list set)" | 
| 4475 | apply(rule notI) | |
| 4476 | apply(drule finite_maxlen) | |
| 4477 | apply (metis UNIV_I length_replicate less_not_refl) | |
| 4478 | done | |
| 4479 | ||
| 4480 | ||
| 35115 | 4481 | subsection {* Sorting *}
 | 
| 24616 | 4482 | |
| 24617 | 4483 | text{* Currently it is not shown that @{const sort} returns a
 | 
| 4484 | permutation of its input because the nicest proof is via multisets, | |
| 4485 | which are not yet available. Alternatively one could define a function | |
| 4486 | that counts the number of occurrences of an element in a list and use | |
| 4487 | that instead of multisets to state the correctness property. *} | |
| 4488 | ||
| 24616 | 4489 | context linorder | 
| 4490 | begin | |
| 4491 | ||
| 51548 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51540diff
changeset | 4492 | lemma set_insort_key: | 
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51540diff
changeset | 4493 | "set (insort_key f x xs) = insert x (set xs)" | 
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51540diff
changeset | 4494 | by (induct xs) auto | 
| 
757fa47af981
centralized various multiset operations in theory multiset;
 haftmann parents: 
51540diff
changeset | 4495 | |
| 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: 
40195diff
changeset | 4496 | 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: 
40195diff
changeset | 4497 | "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: 
40195diff
changeset | 4498 | 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: 
40195diff
changeset | 4499 | |
| 
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: 
40195diff
changeset | 4500 | 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: 
40195diff
changeset | 4501 | 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: 
40195diff
changeset | 4502 | 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: 
40195diff
changeset | 4503 | 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: 
33593diff
changeset | 4504 | |
| 35195 | 4505 | lemma insort_left_comm: | 
| 4506 | "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: 
40195diff
changeset | 4507 | by (cases "x = y") (auto intro: insort_key_left_comm) | 
| 35195 | 4508 | |
| 42871 
1c0b99f950d9
names of fold_set locales resemble name of characteristic property more closely
 haftmann parents: 
42809diff
changeset | 4509 | lemma comp_fun_commute_insort: | 
| 
1c0b99f950d9
names of fold_set locales resemble name of characteristic property more closely
 haftmann parents: 
42809diff
changeset | 4510 | "comp_fun_commute insort" | 
| 35195 | 4511 | proof | 
| 42809 
5b45125b15ba
use pointfree characterisation for fold_set locale
 haftmann parents: 
42714diff
changeset | 4512 | qed (simp add: insort_left_comm fun_eq_iff) | 
| 35195 | 4513 | |
| 4514 | lemma sort_key_simps [simp]: | |
| 4515 | "sort_key f [] = []" | |
| 4516 | "sort_key f (x#xs) = insort_key f x (sort_key f xs)" | |
| 4517 | by (simp_all add: sort_key_def) | |
| 4518 | ||
| 46133 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 haftmann parents: 
46125diff
changeset | 4519 | 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: 
46125diff
changeset | 4520 | 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: 
46125diff
changeset | 4521 | 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: 
46125diff
changeset | 4522 | proof - | 
| 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 haftmann parents: 
46125diff
changeset | 4523 | 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: 
46125diff
changeset | 4524 | 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: 
46125diff
changeset | 4525 | fix zs | 
| 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 haftmann parents: 
46125diff
changeset | 4526 | fix x y | 
| 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 haftmann parents: 
46125diff
changeset | 4527 | 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: 
46125diff
changeset | 4528 | 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: 
46125diff
changeset | 4529 | 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: 
46125diff
changeset | 4530 | 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: 
46125diff
changeset | 4531 | 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: 
46125diff
changeset | 4532 | qed | 
| 47397 
d654c73e4b12
no preference wrt. fold(l/r); prefer fold rather than foldr for iterating over lists in generated code
 haftmann parents: 
47131diff
changeset | 4533 | 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: 
46125diff
changeset | 4534 | qed | 
| 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 haftmann parents: 
46125diff
changeset | 4535 | |
| 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 haftmann parents: 
46125diff
changeset | 4536 | 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: 
46125diff
changeset | 4537 | "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: 
46125diff
changeset | 4538 | by (rule sort_key_conv_fold) simp | 
| 35195 | 4539 | |
| 33639 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 4540 | 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: 
33593diff
changeset | 4541 | 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: 
33593diff
changeset | 4542 | |
| 25062 | 4543 | lemma sorted_Cons: "sorted (x#xs) = (sorted xs & (ALL y:set xs. x <= y))" | 
| 24616 | 4544 | apply(induct xs arbitrary: x) apply simp | 
| 4545 | by simp (blast intro: order_trans) | |
| 4546 | ||
| 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: 
40195diff
changeset | 4547 | 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: 
40195diff
changeset | 4548 | "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: 
40195diff
changeset | 4549 | 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: 
40195diff
changeset | 4550 | |
| 24616 | 4551 | lemma sorted_append: | 
| 25062 | 4552 | "sorted (xs@ys) = (sorted xs & sorted ys & (\<forall>x \<in> set xs. \<forall>y \<in> set ys. x\<le>y))" | 
| 24616 | 4553 | by (induct xs) (auto simp add:sorted_Cons) | 
| 4554 | ||
| 31201 | 4555 | 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: 
33593diff
changeset | 4556 | "sorted xs \<Longrightarrow> i \<le> j \<Longrightarrow> j < length xs \<Longrightarrow> xs!i \<le> xs!j" | 
| 31201 | 4557 | by (induct xs arbitrary: i j) (auto simp:nth_Cons' sorted_Cons) | 
| 4558 | ||
| 33639 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 4559 | 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: 
33593diff
changeset | 4560 | "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: 
33593diff
changeset | 4561 | 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: 
33593diff
changeset | 4562 | 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: 
33593diff
changeset | 4563 | by auto | 
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 4564 | |
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 4565 | 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: 
33593diff
changeset | 4566 | "(\<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: 
33593diff
changeset | 4567 | proof (induct xs) | 
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 4568 | 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: 
33593diff
changeset | 4569 | have "sorted xs" | 
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 4570 | 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: 
33593diff
changeset | 4571 | 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: 
33593diff
changeset | 4572 | 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: 
33593diff
changeset | 4573 | 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: 
33593diff
changeset | 4574 | qed | 
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 4575 | moreover | 
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 4576 |   {
 | 
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 4577 | 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: 
33593diff
changeset | 4578 | 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: 
33593diff
changeset | 4579 | 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: 
33593diff
changeset | 4580 | 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: 
33593diff
changeset | 4581 | 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: 
33593diff
changeset | 4582 | by auto | 
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 4583 | } | 
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 4584 | ultimately | 
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 4585 | show ?case | 
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 4586 | 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: 
33593diff
changeset | 4587 | qed simp | 
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 4588 | |
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 4589 | 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: 
33593diff
changeset | 4590 | "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: 
33593diff
changeset | 4591 | 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: 
33593diff
changeset | 4592 | |
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 4593 | lemma set_insort: "set(insort_key f x xs) = insert x (set xs)" | 
| 24616 | 4594 | by (induct xs) auto | 
| 4595 | ||
| 33639 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 4596 | lemma set_sort[simp]: "set(sort_key f xs) = set xs" | 
| 24616 | 4597 | by (induct xs) (simp_all add:set_insort) | 
| 4598 | ||
| 33639 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 4599 | lemma distinct_insort: "distinct (insort_key f x xs) = (x \<notin> set xs \<and> distinct xs)" | 
| 24616 | 4600 | by(induct xs)(auto simp:set_insort) | 
| 4601 | ||
| 33639 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 4602 | lemma distinct_sort[simp]: "distinct (sort_key f xs) = distinct xs" | 
| 44921 | 4603 | by (induct xs) (simp_all add: distinct_insort) | 
| 24616 | 4604 | |
| 33639 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 4605 | 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: 
40195diff
changeset | 4606 | 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: 
33593diff
changeset | 4607 | |
| 24616 | 4608 | 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: 
40195diff
changeset | 4609 | 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: 
40195diff
changeset | 4610 | |
| 
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: 
40195diff
changeset | 4611 | 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: 
40195diff
changeset | 4612 | 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: 
40195diff
changeset | 4613 | |
| 
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: 
40195diff
changeset | 4614 | 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: 
40195diff
changeset | 4615 | 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: 
33593diff
changeset | 4616 | |
| 36851 | 4617 | lemma sorted_butlast: | 
| 4618 | assumes "xs \<noteq> []" and "sorted xs" | |
| 4619 | shows "sorted (butlast xs)" | |
| 4620 | proof - | |
| 4621 | from `xs \<noteq> []` obtain ys y where "xs = ys @ [y]" by (cases xs rule: rev_cases) auto | |
| 4622 | with `sorted xs` show ?thesis by (simp add: sorted_append) | |
| 4623 | qed | |
| 4624 | ||
| 4625 | lemma insort_not_Nil [simp]: | |
| 4626 | "insort_key f a xs \<noteq> []" | |
| 4627 | by (induct xs) simp_all | |
| 4628 | ||
| 33639 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 4629 | 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: 
26073diff
changeset | 4630 | by (cases xs) auto | 
| 
314c0bcb7df7
Added useful general lemmas from the work with the HeapMonad
 bulwahn parents: 
26073diff
changeset | 4631 | |
| 44916 
840d8c3d9113
added lemma motivated by a more specific lemma in the AFP-KBPs theories
 bulwahn parents: 
44890diff
changeset | 4632 | 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: 
44890diff
changeset | 4633 | 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: 
44890diff
changeset | 4634 | |
| 39534 
c798d4f1b682
generalized lemma insort_remove1 to insort_key_remove1
 haftmann parents: 
39302diff
changeset | 4635 | lemma sorted_map_remove1: | 
| 
c798d4f1b682
generalized lemma insort_remove1 to insort_key_remove1
 haftmann parents: 
39302diff
changeset | 4636 | "sorted (map f xs) \<Longrightarrow> sorted (map f (remove1 x xs))" | 
| 
c798d4f1b682
generalized lemma insort_remove1 to insort_key_remove1
 haftmann parents: 
39302diff
changeset | 4637 | by (induct xs) (auto simp add: sorted_Cons) | 
| 
c798d4f1b682
generalized lemma insort_remove1 to insort_key_remove1
 haftmann parents: 
39302diff
changeset | 4638 | |
| 26143 
314c0bcb7df7
Added useful general lemmas from the work with the HeapMonad
 bulwahn parents: 
26073diff
changeset | 4639 | lemma sorted_remove1: "sorted xs \<Longrightarrow> sorted (remove1 a xs)" | 
| 39534 
c798d4f1b682
generalized lemma insort_remove1 to insort_key_remove1
 haftmann parents: 
39302diff
changeset | 4640 | using sorted_map_remove1 [of "\<lambda>x. x"] by simp | 
| 
c798d4f1b682
generalized lemma insort_remove1 to insort_key_remove1
 haftmann parents: 
39302diff
changeset | 4641 | |
| 
c798d4f1b682
generalized lemma insort_remove1 to insort_key_remove1
 haftmann parents: 
39302diff
changeset | 4642 | lemma insort_key_remove1: | 
| 
c798d4f1b682
generalized lemma insort_remove1 to insort_key_remove1
 haftmann parents: 
39302diff
changeset | 4643 | 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: 
39302diff
changeset | 4644 | shows "insort_key f a (remove1 a xs) = xs" | 
| 
c798d4f1b682
generalized lemma insort_remove1 to insort_key_remove1
 haftmann parents: 
39302diff
changeset | 4645 | 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: 
33593diff
changeset | 4646 | case (Cons x xs) | 
| 39534 
c798d4f1b682
generalized lemma insort_remove1 to insort_key_remove1
 haftmann parents: 
39302diff
changeset | 4647 | 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: 
33593diff
changeset | 4648 | 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: 
33593diff
changeset | 4649 | case False | 
| 39534 
c798d4f1b682
generalized lemma insort_remove1 to insort_key_remove1
 haftmann parents: 
39302diff
changeset | 4650 | then have "f x \<noteq> f a" using Cons.prems by auto | 
| 
c798d4f1b682
generalized lemma insort_remove1 to insort_key_remove1
 haftmann parents: 
39302diff
changeset | 4651 | 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: 
39302diff
changeset | 4652 | 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: 
33593diff
changeset | 4653 | 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: 
33593diff
changeset | 4654 | qed simp | 
| 26143 
314c0bcb7df7
Added useful general lemmas from the work with the HeapMonad
 bulwahn parents: 
26073diff
changeset | 4655 | |
| 39534 
c798d4f1b682
generalized lemma insort_remove1 to insort_key_remove1
 haftmann parents: 
39302diff
changeset | 4656 | lemma insort_remove1: | 
| 
c798d4f1b682
generalized lemma insort_remove1 to insort_key_remove1
 haftmann parents: 
39302diff
changeset | 4657 | assumes "a \<in> set xs" and "sorted xs" | 
| 
c798d4f1b682
generalized lemma insort_remove1 to insort_key_remove1
 haftmann parents: 
39302diff
changeset | 4658 | shows "insort a (remove1 a xs) = xs" | 
| 
c798d4f1b682
generalized lemma insort_remove1 to insort_key_remove1
 haftmann parents: 
39302diff
changeset | 4659 | proof (rule insort_key_remove1) | 
| 
c798d4f1b682
generalized lemma insort_remove1 to insort_key_remove1
 haftmann parents: 
39302diff
changeset | 4660 | from `a \<in> set xs` show "a \<in> set xs" . | 
| 
c798d4f1b682
generalized lemma insort_remove1 to insort_key_remove1
 haftmann parents: 
39302diff
changeset | 4661 | from `sorted xs` show "sorted (map (\<lambda>x. x) xs)" by simp | 
| 
c798d4f1b682
generalized lemma insort_remove1 to insort_key_remove1
 haftmann parents: 
39302diff
changeset | 4662 | 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: 
39302diff
changeset | 4663 |   then have "set (filter (op = a) xs) \<noteq> {}" by auto
 | 
| 
c798d4f1b682
generalized lemma insort_remove1 to insort_key_remove1
 haftmann parents: 
39302diff
changeset | 4664 | then have "filter (op = a) xs \<noteq> []" by (auto simp only: set_empty) | 
| 
c798d4f1b682
generalized lemma insort_remove1 to insort_key_remove1
 haftmann parents: 
39302diff
changeset | 4665 | then have "length (filter (op = a) xs) > 0" by simp | 
| 
c798d4f1b682
generalized lemma insort_remove1 to insort_key_remove1
 haftmann parents: 
39302diff
changeset | 4666 | then obtain n where n: "Suc n = length (filter (op = a) xs)" | 
| 
c798d4f1b682
generalized lemma insort_remove1 to insort_key_remove1
 haftmann parents: 
39302diff
changeset | 4667 | by (cases "length (filter (op = a) xs)") simp_all | 
| 
c798d4f1b682
generalized lemma insort_remove1 to insort_key_remove1
 haftmann parents: 
39302diff
changeset | 4668 | moreover have "replicate (Suc n) a = a # replicate n a" | 
| 
c798d4f1b682
generalized lemma insort_remove1 to insort_key_remove1
 haftmann parents: 
39302diff
changeset | 4669 | by simp | 
| 
c798d4f1b682
generalized lemma insort_remove1 to insort_key_remove1
 haftmann parents: 
39302diff
changeset | 4670 | 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: 
39302diff
changeset | 4671 | qed | 
| 26143 
314c0bcb7df7
Added useful general lemmas from the work with the HeapMonad
 bulwahn parents: 
26073diff
changeset | 4672 | |
| 
314c0bcb7df7
Added useful general lemmas from the work with the HeapMonad
 bulwahn parents: 
26073diff
changeset | 4673 | lemma sorted_remdups[simp]: | 
| 
314c0bcb7df7
Added useful general lemmas from the work with the HeapMonad
 bulwahn parents: 
26073diff
changeset | 4674 | "sorted l \<Longrightarrow> sorted (remdups l)" | 
| 
314c0bcb7df7
Added useful general lemmas from the work with the HeapMonad
 bulwahn parents: 
26073diff
changeset | 4675 | by (induct l) (auto simp: sorted_Cons) | 
| 
314c0bcb7df7
Added useful general lemmas from the work with the HeapMonad
 bulwahn parents: 
26073diff
changeset | 4676 | |
| 53721 
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
 traytel parents: 
53689diff
changeset | 4677 | lemma sorted_remdups_adj[simp]: | 
| 
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
 traytel parents: 
53689diff
changeset | 4678 | "sorted xs \<Longrightarrow> sorted (remdups_adj xs)" | 
| 
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
 traytel parents: 
53689diff
changeset | 4679 | 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: 
53689diff
changeset | 4680 | |
| 24645 | 4681 | lemma sorted_distinct_set_unique: | 
| 4682 | assumes "sorted xs" "distinct xs" "sorted ys" "distinct ys" "set xs = set ys" | |
| 4683 | shows "xs = ys" | |
| 4684 | proof - | |
| 26734 | 4685 | from assms have 1: "length xs = length ys" by (auto dest!: distinct_card) | 
| 24645 | 4686 | from assms show ?thesis | 
| 4687 | proof(induct rule:list_induct2[OF 1]) | |
| 4688 | case 1 show ?case by simp | |
| 4689 | next | |
| 4690 | case 2 thus ?case by (simp add:sorted_Cons) | |
| 4691 | (metis Diff_insert_absorb antisym insertE insert_iff) | |
| 4692 | qed | |
| 4693 | qed | |
| 4694 | ||
| 35603 | 4695 | lemma map_sorted_distinct_set_unique: | 
| 4696 | assumes "inj_on f (set xs \<union> set ys)" | |
| 4697 | assumes "sorted (map f xs)" "distinct (map f xs)" | |
| 4698 | "sorted (map f ys)" "distinct (map f ys)" | |
| 4699 | assumes "set xs = set ys" | |
| 4700 | shows "xs = ys" | |
| 4701 | proof - | |
| 4702 | from assms have "map f xs = map f ys" | |
| 4703 | by (simp add: sorted_distinct_set_unique) | |
| 53374 
a14d2a854c02
tuned proofs -- clarified flow of facts wrt. calculation;
 wenzelm parents: 
53017diff
changeset | 4704 | with `inj_on f (set xs \<union> set ys)` show "xs = ys" | 
| 35603 | 4705 | by (blast intro: map_inj_on) | 
| 4706 | qed | |
| 4707 | ||
| 24645 | 4708 | lemma finite_sorted_distinct_unique: | 
| 4709 | shows "finite A \<Longrightarrow> EX! xs. set xs = A & sorted xs & distinct xs" | |
| 4710 | apply(drule finite_distinct_list) | |
| 4711 | apply clarify | |
| 4712 | apply(rule_tac a="sort xs" in ex1I) | |
| 4713 | apply (auto simp: sorted_distinct_set_unique) | |
| 4714 | done | |
| 4715 | ||
| 39915 
ecf97cf3d248
turned distinct and sorted into inductive predicates: yields nice induction principles for free; more elegant proofs
 haftmann parents: 
39774diff
changeset | 4716 | lemma | 
| 
ecf97cf3d248
turned distinct and sorted into inductive predicates: yields nice induction principles for free; more elegant proofs
 haftmann parents: 
39774diff
changeset | 4717 | assumes "sorted xs" | 
| 
ecf97cf3d248
turned distinct and sorted into inductive predicates: yields nice induction principles for free; more elegant proofs
 haftmann parents: 
39774diff
changeset | 4718 | 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: 
39774diff
changeset | 4719 | 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: 
39774diff
changeset | 4720 | proof - | 
| 
ecf97cf3d248
turned distinct and sorted into inductive predicates: yields nice induction principles for free; more elegant proofs
 haftmann parents: 
39774diff
changeset | 4721 | 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: 
39774diff
changeset | 4722 | 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: 
39774diff
changeset | 4723 | unfolding sorted_append by simp_all | 
| 29626 | 4724 | qed | 
| 4725 | ||
| 33639 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 4726 | 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: 
39774diff
changeset | 4727 | 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: 
33593diff
changeset | 4728 | |
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 4729 | 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: 
39774diff
changeset | 4730 | by (subst takeWhile_eq_take) (auto dest: sorted_take) | 
| 29626 | 4731 | |
| 34933 | 4732 | lemma sorted_filter: | 
| 4733 | "sorted (map f xs) \<Longrightarrow> sorted (map f (filter P xs))" | |
| 4734 | by (induct xs) (simp_all add: sorted_Cons) | |
| 4735 | ||
| 4736 | lemma foldr_max_sorted: | |
| 4737 | assumes "sorted (rev xs)" | |
| 4738 | 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: 
53017diff
changeset | 4739 | using assms | 
| 
a14d2a854c02
tuned proofs -- clarified flow of facts wrt. calculation;
 wenzelm parents: 
53017diff
changeset | 4740 | proof (induct xs) | 
| 34933 | 4741 | case (Cons x xs) | 
| 53374 
a14d2a854c02
tuned proofs -- clarified flow of facts wrt. calculation;
 wenzelm parents: 
53017diff
changeset | 4742 | then have "sorted (rev xs)" using sorted_append by auto | 
| 
a14d2a854c02
tuned proofs -- clarified flow of facts wrt. calculation;
 wenzelm parents: 
53017diff
changeset | 4743 | with Cons show ?case | 
| 
a14d2a854c02
tuned proofs -- clarified flow of facts wrt. calculation;
 wenzelm parents: 
53017diff
changeset | 4744 | by (cases xs) (auto simp add: sorted_append max_def) | 
| 34933 | 4745 | qed simp | 
| 4746 | ||
| 4747 | lemma filter_equals_takeWhile_sorted_rev: | |
| 4748 | assumes sorted: "sorted (rev (map f xs))" | |
| 4749 | shows "[x \<leftarrow> xs. t < f x] = takeWhile (\<lambda> x. t < f x) xs" | |
| 4750 | (is "filter ?P xs = ?tW") | |
| 4751 | proof (rule takeWhile_eq_filter[symmetric]) | |
| 4752 | let "?dW" = "dropWhile ?P xs" | |
| 4753 | fix x assume "x \<in> set ?dW" | |
| 4754 | then obtain i where i: "i < length ?dW" and nth_i: "x = ?dW ! i" | |
| 4755 | unfolding in_set_conv_nth by auto | |
| 4756 | hence "length ?tW + i < length (?tW @ ?dW)" | |
| 4757 | unfolding length_append by simp | |
| 4758 | hence i': "length (map f ?tW) + i < length (map f xs)" by simp | |
| 4759 | have "(map f ?tW @ map f ?dW) ! (length (map f ?tW) + i) \<le> | |
| 4760 | (map f ?tW @ map f ?dW) ! (length (map f ?tW) + 0)" | |
| 4761 | using sorted_rev_nth_mono[OF sorted _ i', of "length ?tW"] | |
| 4762 | unfolding map_append[symmetric] by simp | |
| 4763 | hence "f x \<le> f (?dW ! 0)" | |
| 4764 | unfolding nth_append_length_plus nth_i | |
| 4765 | using i preorder_class.le_less_trans[OF le0 i] by simp | |
| 4766 | also have "... \<le> t" | |
| 4767 | using hd_dropWhile[of "?P" xs] le0[THEN preorder_class.le_less_trans, OF i] | |
| 4768 | using hd_conv_nth[of "?dW"] by simp | |
| 4769 | finally show "\<not> t < f x" by simp | |
| 4770 | qed | |
| 4771 | ||
| 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: 
40195diff
changeset | 4772 | 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: 
40195diff
changeset | 4773 | "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: 
40195diff
changeset | 4774 | 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: 
40195diff
changeset | 4775 | |
| 
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: 
40195diff
changeset | 4776 | 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: 
40195diff
changeset | 4777 | "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: 
40195diff
changeset | 4778 | 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: 
40195diff
changeset | 4779 | |
| 
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: 
40195diff
changeset | 4780 | 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: 
40195diff
changeset | 4781 | "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: 
40195diff
changeset | 4782 | 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: 
40195diff
changeset | 4783 | |
| 
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: 
40195diff
changeset | 4784 | 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: 
40195diff
changeset | 4785 | "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: 
40195diff
changeset | 4786 | 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: 
40195diff
changeset | 4787 | |
| 35608 | 4788 | lemma set_insort_insert: | 
| 4789 | "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: 
40195diff
changeset | 4790 | by (auto simp add: insort_insert_key_def set_insort) | 
| 35608 | 4791 | |
| 4792 | lemma distinct_insort_insert: | |
| 4793 | 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: 
40195diff
changeset | 4794 | 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: 
40195diff
changeset | 4795 | 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: 
40195diff
changeset | 4796 | |
| 
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: 
40195diff
changeset | 4797 | 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: 
40195diff
changeset | 4798 | 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: 
40195diff
changeset | 4799 | 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: 
40195diff
changeset | 4800 | using assms by (simp add: insort_insert_key_def sorted_insort_key) | 
| 35608 | 4801 | |
| 4802 | lemma sorted_insort_insert: | |
| 4803 | assumes "sorted xs" | |
| 4804 | 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: 
40195diff
changeset | 4805 | 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: 
40195diff
changeset | 4806 | |
| 
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: 
40195diff
changeset | 4807 | lemma filter_insort_triv: | 
| 37107 | 4808 | "\<not> P x \<Longrightarrow> filter P (insort_key f x xs) = filter P xs" | 
| 4809 | by (induct xs) simp_all | |
| 4810 | ||
| 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: 
40195diff
changeset | 4811 | lemma filter_insort: | 
| 37107 | 4812 | "sorted (map f xs) \<Longrightarrow> P x \<Longrightarrow> filter P (insort_key f x xs) = insort_key f x (filter P xs)" | 
| 4813 | using assms by (induct xs) | |
| 4814 | (auto simp add: sorted_Cons, subst insort_is_Cons, auto) | |
| 4815 | ||
| 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: 
40195diff
changeset | 4816 | lemma filter_sort: | 
| 37107 | 4817 | "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: 
40195diff
changeset | 4818 | by (induct xs) (simp_all add: filter_insort_triv filter_insort) | 
| 37107 | 4819 | |
| 40304 | 4820 | lemma sorted_map_same: | 
| 4821 | "sorted (map f [x\<leftarrow>xs. f x = g xs])" | |
| 4822 | proof (induct xs arbitrary: g) | |
| 37107 | 4823 | case Nil then show ?case by simp | 
| 4824 | next | |
| 4825 | case (Cons x xs) | |
| 40304 | 4826 | then have "sorted (map f [y\<leftarrow>xs . f y = (\<lambda>xs. f x) xs])" . | 
| 4827 | moreover from Cons have "sorted (map f [y\<leftarrow>xs . f y = (g \<circ> Cons x) xs])" . | |
| 37107 | 4828 | ultimately show ?case by (simp_all add: sorted_Cons) | 
| 4829 | qed | |
| 4830 | ||
| 40304 | 4831 | lemma sorted_same: | 
| 4832 | "sorted [x\<leftarrow>xs. x = g xs]" | |
| 4833 | using sorted_map_same [of "\<lambda>x. x"] by simp | |
| 4834 | ||
| 37107 | 4835 | lemma remove1_insort [simp]: | 
| 4836 | "remove1 x (insort x xs) = xs" | |
| 4837 | by (induct xs) simp_all | |
| 4838 | ||
| 24616 | 4839 | end | 
| 4840 | ||
| 25277 | 4841 | lemma sorted_upt[simp]: "sorted[i..<j]" | 
| 4842 | by (induct j) (simp_all add:sorted_append) | |
| 4843 | ||
| 32415 
1dddf2f64266
got rid of complicated class finite_intvl_succ and defined "upto" directly on int, the only instance of the class.
 nipkow parents: 
32078diff
changeset | 4844 | 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: 
32078diff
changeset | 4845 | 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: 
32078diff
changeset | 4846 | 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: 
32078diff
changeset | 4847 | 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: 
32078diff
changeset | 4848 | done | 
| 
1dddf2f64266
got rid of complicated class finite_intvl_succ and defined "upto" directly on int, the only instance of the class.
 nipkow parents: 
32078diff
changeset | 4849 | |
| 52379 | 4850 | lemma sorted_find_Min: | 
| 4851 | assumes "sorted xs" | |
| 4852 | assumes "\<exists>x \<in> set xs. P x" | |
| 4853 |   shows "List.find P xs = Some (Min {x\<in>set xs. P x})"
 | |
| 4854 | using assms proof (induct xs rule: sorted.induct) | |
| 4855 | case Nil then show ?case by simp | |
| 4856 | next | |
| 4857 | case (Cons xs x) show ?case proof (cases "P x") | |
| 4858 | case True with Cons show ?thesis by (auto intro: Min_eqI [symmetric]) | |
| 4859 | next | |
| 4860 |     case False then have "{y. (y = x \<or> y \<in> set xs) \<and> P y} = {y \<in> set xs. P y}"
 | |
| 4861 | by auto | |
| 4862 | with Cons False show ?thesis by simp_all | |
| 4863 | qed | |
| 4864 | qed | |
| 4865 | ||
| 35115 | 4866 | |
| 4867 | subsubsection {* @{const transpose} on sorted lists *}
 | |
| 34933 | 4868 | |
| 4869 | lemma sorted_transpose[simp]: | |
| 4870 | shows "sorted (rev (map length (transpose xs)))" | |
| 4871 | by (auto simp: sorted_equals_nth_mono rev_nth nth_transpose | |
| 4872 | length_filter_conv_card intro: card_mono) | |
| 4873 | ||
| 4874 | lemma transpose_max_length: | |
| 4875 | "foldr (\<lambda>xs. max (length xs)) (transpose xs) 0 = length [x \<leftarrow> xs. x \<noteq> []]" | |
| 4876 | (is "?L = ?R") | |
| 4877 | proof (cases "transpose xs = []") | |
| 4878 | case False | |
| 4879 | have "?L = foldr max (map length (transpose xs)) 0" | |
| 4880 | by (simp add: foldr_map comp_def) | |
| 4881 | also have "... = length (transpose xs ! 0)" | |
| 4882 | using False sorted_transpose by (simp add: foldr_max_sorted) | |
| 4883 | finally show ?thesis | |
| 4884 | using False by (simp add: nth_transpose) | |
| 4885 | next | |
| 4886 | case True | |
| 4887 | hence "[x \<leftarrow> xs. x \<noteq> []] = []" | |
| 4888 | by (auto intro!: filter_False simp: transpose_empty) | |
| 4889 | thus ?thesis by (simp add: transpose_empty True) | |
| 4890 | qed | |
| 4891 | ||
| 4892 | lemma length_transpose_sorted: | |
| 4893 | fixes xs :: "'a list list" | |
| 4894 | assumes sorted: "sorted (rev (map length xs))" | |
| 4895 | shows "length (transpose xs) = (if xs = [] then 0 else length (xs ! 0))" | |
| 4896 | proof (cases "xs = []") | |
| 4897 | case False | |
| 4898 | thus ?thesis | |
| 4899 | using foldr_max_sorted[OF sorted] False | |
| 4900 | unfolding length_transpose foldr_map comp_def | |
| 4901 | by simp | |
| 4902 | qed simp | |
| 4903 | ||
| 4904 | lemma nth_nth_transpose_sorted[simp]: | |
| 4905 | fixes xs :: "'a list list" | |
| 4906 | assumes sorted: "sorted (rev (map length xs))" | |
| 4907 | and i: "i < length (transpose xs)" | |
| 4908 | and j: "j < length [ys \<leftarrow> xs. i < length ys]" | |
| 4909 | shows "transpose xs ! i ! j = xs ! j ! i" | |
| 4910 | using j filter_equals_takeWhile_sorted_rev[OF sorted, of i] | |
| 4911 | nth_transpose[OF i] nth_map[OF j] | |
| 4912 | by (simp add: takeWhile_nth) | |
| 4913 | ||
| 4914 | lemma transpose_column_length: | |
| 4915 | fixes xs :: "'a list list" | |
| 4916 | assumes sorted: "sorted (rev (map length xs))" and "i < length xs" | |
| 4917 | shows "length (filter (\<lambda>ys. i < length ys) (transpose xs)) = length (xs ! i)" | |
| 4918 | proof - | |
| 4919 | have "xs \<noteq> []" using `i < length xs` by auto | |
| 4920 | note filter_equals_takeWhile_sorted_rev[OF sorted, simp] | |
| 4921 |   { fix j assume "j \<le> i"
 | |
| 4922 | note sorted_rev_nth_mono[OF sorted, of j i, simplified, OF this `i < length xs`] | |
| 4923 | } note sortedE = this[consumes 1] | |
| 4924 | ||
| 4925 |   have "{j. j < length (transpose xs) \<and> i < length (transpose xs ! j)}
 | |
| 4926 |     = {..< length (xs ! i)}"
 | |
| 4927 | proof safe | |
| 4928 | fix j | |
| 4929 | assume "j < length (transpose xs)" and "i < length (transpose xs ! j)" | |
| 4930 | with this(2) nth_transpose[OF this(1)] | |
| 4931 | have "i < length (takeWhile (\<lambda>ys. j < length ys) xs)" by simp | |
| 4932 | from nth_mem[OF this] takeWhile_nth[OF this] | |
| 4933 | show "j < length (xs ! i)" by (auto dest: set_takeWhileD) | |
| 4934 | next | |
| 4935 | fix j assume "j < length (xs ! i)" | |
| 4936 | thus "j < length (transpose xs)" | |
| 4937 | using foldr_max_sorted[OF sorted] `xs \<noteq> []` sortedE[OF le0] | |
| 4938 | by (auto simp: length_transpose comp_def foldr_map) | |
| 4939 | ||
| 4940 | have "Suc i \<le> length (takeWhile (\<lambda>ys. j < length ys) xs)" | |
| 4941 | using `i < length xs` `j < length (xs ! i)` less_Suc_eq_le | |
| 4942 | by (auto intro!: length_takeWhile_less_P_nth dest!: sortedE) | |
| 4943 | with nth_transpose[OF `j < length (transpose xs)`] | |
| 4944 | show "i < length (transpose xs ! j)" by simp | |
| 4945 | qed | |
| 4946 | thus ?thesis by (simp add: length_filter_conv_card) | |
| 4947 | qed | |
| 4948 | ||
| 4949 | lemma transpose_column: | |
| 4950 | fixes xs :: "'a list list" | |
| 4951 | assumes sorted: "sorted (rev (map length xs))" and "i < length xs" | |
| 4952 | shows "map (\<lambda>ys. ys ! i) (filter (\<lambda>ys. i < length ys) (transpose xs)) | |
| 4953 | = xs ! i" (is "?R = _") | |
| 4954 | proof (rule nth_equalityI, safe) | |
| 4955 | show length: "length ?R = length (xs ! i)" | |
| 4956 | using transpose_column_length[OF assms] by simp | |
| 4957 | ||
| 4958 | fix j assume j: "j < length ?R" | |
| 4959 | note * = less_le_trans[OF this, unfolded length_map, OF length_filter_le] | |
| 4960 | from j have j_less: "j < length (xs ! i)" using length by simp | |
| 4961 | have i_less_tW: "Suc i \<le> length (takeWhile (\<lambda>ys. Suc j \<le> length ys) xs)" | |
| 4962 | proof (rule length_takeWhile_less_P_nth) | |
| 4963 | show "Suc i \<le> length xs" using `i < length xs` by simp | |
| 4964 | fix k assume "k < Suc i" | |
| 4965 | hence "k \<le> i" by auto | |
| 4966 | with sorted_rev_nth_mono[OF sorted this] `i < length xs` | |
| 4967 | have "length (xs ! i) \<le> length (xs ! k)" by simp | |
| 4968 | thus "Suc j \<le> length (xs ! k)" using j_less by simp | |
| 4969 | qed | |
| 4970 | have i_less_filter: "i < length [ys\<leftarrow>xs . j < length ys]" | |
| 4971 | unfolding filter_equals_takeWhile_sorted_rev[OF sorted, of j] | |
| 4972 | using i_less_tW by (simp_all add: Suc_le_eq) | |
| 4973 | from j show "?R ! j = xs ! i ! j" | |
| 4974 | unfolding filter_equals_takeWhile_sorted_rev[OF sorted_transpose, of i] | |
| 4975 | by (simp add: takeWhile_nth nth_nth_transpose_sorted[OF sorted * i_less_filter]) | |
| 4976 | qed | |
| 4977 | ||
| 4978 | lemma transpose_transpose: | |
| 4979 | fixes xs :: "'a list list" | |
| 4980 | assumes sorted: "sorted (rev (map length xs))" | |
| 4981 | shows "transpose (transpose xs) = takeWhile (\<lambda>x. x \<noteq> []) xs" (is "?L = ?R") | |
| 4982 | proof - | |
| 4983 | have len: "length ?L = length ?R" | |
| 4984 | unfolding length_transpose transpose_max_length | |
| 4985 | using filter_equals_takeWhile_sorted_rev[OF sorted, of 0] | |
| 4986 | by simp | |
| 4987 | ||
| 4988 |   { fix i assume "i < length ?R"
 | |
| 4989 | with less_le_trans[OF _ length_takeWhile_le[of _ xs]] | |
| 4990 | have "i < length xs" by simp | |
| 4991 | } note * = this | |
| 4992 | show ?thesis | |
| 4993 | by (rule nth_equalityI) | |
| 4994 | (simp_all add: len nth_transpose transpose_column[OF sorted] * takeWhile_nth) | |
| 4995 | qed | |
| 24616 | 4996 | |
| 34934 
440605046777
Added transpose_rectangle, when the input list is rectangular.
 hoelzl parents: 
34933diff
changeset | 4997 | theorem transpose_rectangle: | 
| 
440605046777
Added transpose_rectangle, when the input list is rectangular.
 hoelzl parents: 
34933diff
changeset | 4998 | assumes "xs = [] \<Longrightarrow> n = 0" | 
| 
440605046777
Added transpose_rectangle, when the input list is rectangular.
 hoelzl parents: 
34933diff
changeset | 4999 | assumes rect: "\<And> i. i < length xs \<Longrightarrow> length (xs ! i) = n" | 
| 
440605046777
Added transpose_rectangle, when the input list is rectangular.
 hoelzl parents: 
34933diff
changeset | 5000 | 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: 
34933diff
changeset | 5001 | (is "?trans = ?map") | 
| 
440605046777
Added transpose_rectangle, when the input list is rectangular.
 hoelzl parents: 
34933diff
changeset | 5002 | proof (rule nth_equalityI) | 
| 
440605046777
Added transpose_rectangle, when the input list is rectangular.
 hoelzl parents: 
34933diff
changeset | 5003 | have "sorted (rev (map length xs))" | 
| 
440605046777
Added transpose_rectangle, when the input list is rectangular.
 hoelzl parents: 
34933diff
changeset | 5004 | by (auto simp: rev_nth rect intro!: sorted_nth_monoI) | 
| 
440605046777
Added transpose_rectangle, when the input list is rectangular.
 hoelzl parents: 
34933diff
changeset | 5005 | from foldr_max_sorted[OF this] assms | 
| 
440605046777
Added transpose_rectangle, when the input list is rectangular.
 hoelzl parents: 
34933diff
changeset | 5006 | show len: "length ?trans = length ?map" | 
| 
440605046777
Added transpose_rectangle, when the input list is rectangular.
 hoelzl parents: 
34933diff
changeset | 5007 | by (simp_all add: length_transpose foldr_map comp_def) | 
| 
440605046777
Added transpose_rectangle, when the input list is rectangular.
 hoelzl parents: 
34933diff
changeset | 5008 | moreover | 
| 
440605046777
Added transpose_rectangle, when the input list is rectangular.
 hoelzl parents: 
34933diff
changeset | 5009 |   { 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: 
34933diff
changeset | 5010 | using rect by (auto simp: in_set_conv_nth intro!: filter_True) } | 
| 
440605046777
Added transpose_rectangle, when the input list is rectangular.
 hoelzl parents: 
34933diff
changeset | 5011 | ultimately show "\<forall>i < length ?trans. ?trans ! i = ?map ! i" | 
| 
440605046777
Added transpose_rectangle, when the input list is rectangular.
 hoelzl parents: 
34933diff
changeset | 5012 | by (auto simp: nth_transpose intro: nth_equalityI) | 
| 
440605046777
Added transpose_rectangle, when the input list is rectangular.
 hoelzl parents: 
34933diff
changeset | 5013 | qed | 
| 24616 | 5014 | |
| 35115 | 5015 | |
| 25069 | 5016 | subsubsection {* @{text sorted_list_of_set} *}
 | 
| 5017 | ||
| 5018 | text{* This function maps (finite) linearly ordered sets to sorted
 | |
| 5019 | lists. Warning: in most cases it is not a good idea to convert from | |
| 5020 | sets to lists but one should convert in the other direction (via | |
| 5021 | @{const set}). *}
 | |
| 5022 | ||
| 51489 | 5023 | subsubsection {* @{text sorted_list_of_set} *}
 | 
| 5024 | ||
| 5025 | text{* This function maps (finite) linearly ordered sets to sorted
 | |
| 5026 | lists. Warning: in most cases it is not a good idea to convert from | |
| 5027 | sets to lists but one should convert in the other direction (via | |
| 5028 | @{const set}). *}
 | |
| 5029 | ||
| 54868 | 5030 | context linorder | 
| 5031 | begin | |
| 5032 | ||
| 5033 | definition sorted_list_of_set :: "'a set \<Rightarrow> 'a list" where | |
| 51489 | 5034 | "sorted_list_of_set = folding.F insort []" | 
| 5035 | ||
| 54868 | 5036 | sublocale sorted_list_of_set!: folding insort Nil | 
| 51489 | 5037 | where | 
| 5038 | "folding.F insort [] = sorted_list_of_set" | |
| 5039 | proof - | |
| 5040 | interpret comp_fun_commute insort by (fact comp_fun_commute_insort) | |
| 5041 | show "folding insort" by default (fact comp_fun_commute) | |
| 5042 | show "folding.F insort [] = sorted_list_of_set" by (simp only: sorted_list_of_set_def) | |
| 5043 | qed | |
| 5044 | ||
| 5045 | lemma sorted_list_of_set_empty: | |
| 35195 | 5046 |   "sorted_list_of_set {} = []"
 | 
| 51489 | 5047 | by (fact sorted_list_of_set.empty) | 
| 35195 | 5048 | |
| 5049 | lemma sorted_list_of_set_insert [simp]: | |
| 54868 | 5050 |   "finite A \<Longrightarrow> sorted_list_of_set (insert x A) = insort x (sorted_list_of_set (A - {x}))"
 | 
| 5051 | by (fact sorted_list_of_set.insert_remove) | |
| 35195 | 5052 | |
| 52122 | 5053 | lemma sorted_list_of_set_eq_Nil_iff [simp]: | 
| 5054 |   "finite A \<Longrightarrow> sorted_list_of_set A = [] \<longleftrightarrow> A = {}"
 | |
| 54868 | 5055 | by (auto simp: sorted_list_of_set.remove) | 
| 52122 | 5056 | |
| 35195 | 5057 | lemma sorted_list_of_set [simp]: | 
| 5058 | "finite A \<Longrightarrow> set (sorted_list_of_set A) = A \<and> sorted (sorted_list_of_set A) | |
| 5059 | \<and> distinct (sorted_list_of_set A)" | |
| 5060 | by (induct A rule: finite_induct) (simp_all add: set_insort sorted_insort distinct_insort) | |
| 5061 | ||
| 52122 | 5062 | lemma distinct_sorted_list_of_set: | 
| 5063 | "distinct (sorted_list_of_set A)" | |
| 5064 | using sorted_list_of_set by (cases "finite A") auto | |
| 5065 | ||
| 47841 | 5066 | lemma sorted_list_of_set_sort_remdups [code]: | 
| 35195 | 5067 | "sorted_list_of_set (set xs) = sort (remdups xs)" | 
| 5068 | proof - | |
| 42871 
1c0b99f950d9
names of fold_set locales resemble name of characteristic property more closely
 haftmann parents: 
42809diff
changeset | 5069 | interpret comp_fun_commute insort by (fact comp_fun_commute_insort) | 
| 51489 | 5070 | show ?thesis by (simp add: sorted_list_of_set.eq_fold sort_conv_fold fold_set_fold_remdups) | 
| 35195 | 5071 | qed | 
| 25069 | 5072 | |
| 37107 | 5073 | lemma sorted_list_of_set_remove: | 
| 5074 | assumes "finite A" | |
| 5075 |   shows "sorted_list_of_set (A - {x}) = remove1 x (sorted_list_of_set A)"
 | |
| 5076 | proof (cases "x \<in> A") | |
| 5077 | case False with assms have "x \<notin> set (sorted_list_of_set A)" by simp | |
| 5078 | with False show ?thesis by (simp add: remove1_idem) | |
| 5079 | next | |
| 5080 | case True then obtain B where A: "A = insert x B" by (rule Set.set_insert) | |
| 5081 | with assms show ?thesis by simp | |
| 5082 | qed | |
| 5083 | ||
| 25069 | 5084 | end | 
| 5085 | ||
| 37107 | 5086 | lemma sorted_list_of_set_range [simp]: | 
| 5087 |   "sorted_list_of_set {m..<n} = [m..<n]"
 | |
| 5088 | by (rule sorted_distinct_set_unique) simp_all | |
| 5089 | ||
| 5090 | ||
| 15392 | 5091 | subsubsection {* @{text lists}: the list-forming operator over sets *}
 | 
| 15302 | 5092 | |
| 23740 | 5093 | inductive_set | 
| 22262 | 5094 | lists :: "'a set => 'a list set" | 
| 23740 | 5095 | for A :: "'a set" | 
| 5096 | where | |
| 39613 | 5097 | Nil [intro!, simp]: "[]: lists A" | 
| 54147 
97a8ff4e4ac9
killed most "no_atp", to make Sledgehammer more complete
 blanchet parents: 
53954diff
changeset | 5098 | | Cons [intro!, simp]: "[| a: A; l: lists A|] ==> a#l : lists A" | 
| 
97a8ff4e4ac9
killed most "no_atp", to make Sledgehammer more complete
 blanchet parents: 
53954diff
changeset | 5099 | |
| 
97a8ff4e4ac9
killed most "no_atp", to make Sledgehammer more complete
 blanchet parents: 
53954diff
changeset | 5100 | inductive_cases listsE [elim!]: "x#l : lists A" | 
| 
97a8ff4e4ac9
killed most "no_atp", to make Sledgehammer more complete
 blanchet parents: 
53954diff
changeset | 5101 | inductive_cases listspE [elim!]: "listsp A (x # l)" | 
| 23740 | 5102 | |
| 46313 | 5103 | inductive_simps listsp_simps[code]: | 
| 5104 | "listsp A []" | |
| 5105 | "listsp A (x # xs)" | |
| 5106 | ||
| 23740 | 5107 | lemma listsp_mono [mono]: "A \<le> B ==> listsp A \<le> listsp B" | 
| 46884 | 5108 | by (rule predicate1I, erule listsp.induct, blast+) | 
| 26795 
a27607030a1c
- Explicitely applied predicate1I in a few proofs, because it is no longer
 berghofe parents: 
26771diff
changeset | 5109 | |
| 46176 
1898e61e89c4
pred_subset/equals_eq are now standard pred_set_conv rules
 berghofe parents: 
46156diff
changeset | 5110 | lemmas lists_mono = listsp_mono [to_set] | 
| 22262 | 5111 | |
| 22422 
ee19cdb07528
stepping towards uniform lattice theory development in HOL
 haftmann parents: 
22262diff
changeset | 5112 | lemma listsp_infI: | 
| 
ee19cdb07528
stepping towards uniform lattice theory development in HOL
 haftmann parents: 
22262diff
changeset | 5113 | assumes l: "listsp A l" shows "listsp B l ==> listsp (inf A B) l" using l | 
| 24349 | 5114 | by induct blast+ | 
| 15302 | 5115 | |
| 22422 
ee19cdb07528
stepping towards uniform lattice theory development in HOL
 haftmann parents: 
22262diff
changeset | 5116 | lemmas lists_IntI = listsp_infI [to_set] | 
| 
ee19cdb07528
stepping towards uniform lattice theory development in HOL
 haftmann parents: 
22262diff
changeset | 5117 | |
| 
ee19cdb07528
stepping towards uniform lattice theory development in HOL
 haftmann parents: 
22262diff
changeset | 5118 | 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: 
22262diff
changeset | 5119 | proof (rule mono_inf [where f=listsp, THEN order_antisym]) | 
| 22262 | 5120 | 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: 
47131diff
changeset | 5121 | show "inf (listsp A) (listsp B) \<le> listsp (inf A B)" by (blast intro!: listsp_infI) | 
| 14388 | 5122 | qed | 
| 5123 | ||
| 41075 
4bed56dc95fb
primitive definitions of bot/top/inf/sup for bool and fun are named with canonical suffix `_def` rather than `_eq`
 haftmann parents: 
40968diff
changeset | 5124 | 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: 
22262diff
changeset | 5125 | |
| 46176 
1898e61e89c4
pred_subset/equals_eq are now standard pred_set_conv rules
 berghofe parents: 
46156diff
changeset | 5126 | lemmas lists_Int_eq [simp] = listsp_inf_eq [to_set] | 
| 22262 | 5127 | |
| 39613 | 5128 | lemma Cons_in_lists_iff[simp]: "x#xs : lists A \<longleftrightarrow> x:A \<and> xs : lists A" | 
| 5129 | by auto | |
| 5130 | ||
| 22262 | 5131 | lemma append_in_listsp_conv [iff]: | 
| 5132 | "(listsp A (xs @ ys)) = (listsp A xs \<and> listsp A ys)" | |
| 15302 | 5133 | by (induct xs) auto | 
| 5134 | ||
| 22262 | 5135 | lemmas append_in_lists_conv [iff] = append_in_listsp_conv [to_set] | 
| 5136 | ||
| 5137 | lemma in_listsp_conv_set: "(listsp A xs) = (\<forall>x \<in> set xs. A x)" | |
| 5138 | -- {* eliminate @{text listsp} in favour of @{text set} *}
 | |
| 15302 | 5139 | by (induct xs) auto | 
| 5140 | ||
| 46313 | 5141 | lemmas in_lists_conv_set [code_unfold] = in_listsp_conv_set [to_set] | 
| 22262 | 5142 | |
| 54147 
97a8ff4e4ac9
killed most "no_atp", to make Sledgehammer more complete
 blanchet parents: 
53954diff
changeset | 5143 | lemma in_listspD [dest!]: "listsp A xs ==> \<forall>x\<in>set xs. A x" | 
| 22262 | 5144 | by (rule in_listsp_conv_set [THEN iffD1]) | 
| 5145 | ||
| 54147 
97a8ff4e4ac9
killed most "no_atp", to make Sledgehammer more complete
 blanchet parents: 
53954diff
changeset | 5146 | lemmas in_listsD [dest!] = in_listspD [to_set] | 
| 
97a8ff4e4ac9
killed most "no_atp", to make Sledgehammer more complete
 blanchet parents: 
53954diff
changeset | 5147 | |
| 
97a8ff4e4ac9
killed most "no_atp", to make Sledgehammer more complete
 blanchet parents: 
53954diff
changeset | 5148 | lemma in_listspI [intro!]: "\<forall>x\<in>set xs. A x ==> listsp A xs" | 
| 22262 | 5149 | by (rule in_listsp_conv_set [THEN iffD2]) | 
| 5150 | ||
| 54147 
97a8ff4e4ac9
killed most "no_atp", to make Sledgehammer more complete
 blanchet parents: 
53954diff
changeset | 5151 | lemmas in_listsI [intro!] = in_listspI [to_set] | 
| 15302 | 5152 | |
| 39597 | 5153 | lemma lists_eq_set: "lists A = {xs. set xs <= A}"
 | 
| 5154 | by auto | |
| 5155 | ||
| 39613 | 5156 | lemma lists_empty [simp]: "lists {} = {[]}"
 | 
| 5157 | by auto | |
| 5158 | ||
| 15302 | 5159 | lemma lists_UNIV [simp]: "lists UNIV = UNIV" | 
| 5160 | by auto | |
| 5161 | ||
| 50134 | 5162 | lemma lists_image: "lists (f`A) = map f ` lists A" | 
| 5163 | proof - | |
| 5164 |   { fix xs have "\<forall>x\<in>set xs. x \<in> f ` A \<Longrightarrow> xs \<in> map f ` lists A"
 | |
| 55465 | 5165 | by (induct xs) (auto simp del: list.map simp add: list.map[symmetric] intro!: imageI) } | 
| 50134 | 5166 | then show ?thesis by auto | 
| 5167 | qed | |
| 17086 | 5168 | |
| 35115 | 5169 | subsubsection {* Inductive definition for membership *}
 | 
| 17086 | 5170 | |
| 23740 | 5171 | inductive ListMem :: "'a \<Rightarrow> 'a list \<Rightarrow> bool" | 
| 22262 | 5172 | where | 
| 5173 | elem: "ListMem x (x # xs)" | |
| 5174 | | insert: "ListMem x xs \<Longrightarrow> ListMem x (y # xs)" | |
| 5175 | ||
| 5176 | lemma ListMem_iff: "(ListMem x xs) = (x \<in> set xs)" | |
| 17086 | 5177 | apply (rule iffI) | 
| 5178 | apply (induct set: ListMem) | |
| 5179 | apply auto | |
| 5180 | apply (induct xs) | |
| 5181 | apply (auto intro: ListMem.intros) | |
| 5182 | done | |
| 5183 | ||
| 5184 | ||
| 35115 | 5185 | subsubsection {* Lists as Cartesian products *}
 | 
| 15302 | 5186 | |
| 5187 | text{*@{text"set_Cons A Xs"}: the set of lists with head drawn from
 | |
| 5188 | @{term A} and tail drawn from @{term Xs}.*}
 | |
| 5189 | ||
| 50548 | 5190 | definition set_Cons :: "'a set \<Rightarrow> 'a list set \<Rightarrow> 'a list set" where | 
| 5191 | "set_Cons A XS = {z. \<exists>x xs. z = x # xs \<and> x \<in> A \<and> xs \<in> XS}"
 | |
| 15302 | 5192 | |
| 17724 | 5193 | lemma set_Cons_sing_Nil [simp]: "set_Cons A {[]} = (%x. [x])`A"
 | 
| 15302 | 5194 | by (auto simp add: set_Cons_def) | 
| 5195 | ||
| 5196 | text{*Yields the set of lists, all of the same length as the argument and
 | |
| 5197 | with elements drawn from the corresponding element of the argument.*} | |
| 5198 | ||
| 50548 | 5199 | primrec listset :: "'a set list \<Rightarrow> 'a list set" where | 
| 5200 | "listset [] = {[]}" |
 | |
| 5201 | "listset (A # As) = set_Cons A (listset As)" | |
| 15302 | 5202 | |
| 5203 | ||
| 35115 | 5204 | subsection {* Relations on Lists *}
 | 
| 15656 | 5205 | |
| 5206 | subsubsection {* Length Lexicographic Ordering *}
 | |
| 5207 | ||
| 5208 | text{*These orderings preserve well-foundedness: shorter lists 
 | |
| 5209 | precede longer lists. These ordering are not used in dictionaries.*} | |
| 34941 | 5210 | |
| 5211 | primrec -- {*The lexicographic ordering for lists of the specified length*}
 | |
| 5212 |   lexn :: "('a \<times> 'a) set \<Rightarrow> nat \<Rightarrow> ('a list \<times> 'a list) set" where
 | |
| 50548 | 5213 | "lexn r 0 = {}" |
 | 
| 5214 | "lexn r (Suc n) = | |
| 5215 | (map_pair (%(x, xs). x#xs) (%(x, xs). x#xs) ` (r <*lex*> lexn r n)) Int | |
| 5216 |   {(xs, ys). length xs = Suc n \<and> length ys = Suc n}"
 | |
| 5217 | ||
| 5218 | definition lex :: "('a \<times> 'a) set \<Rightarrow> ('a list \<times> 'a list) set" where
 | |
| 5219 | "lex r = (\<Union>n. lexn r n)" -- {*Holds only between lists of the same length*}
 | |
| 5220 | ||
| 5221 | definition lenlex :: "('a \<times> 'a) set => ('a list \<times> 'a list) set" where
 | |
| 5222 | "lenlex r = inv_image (less_than <*lex*> lex r) (\<lambda>xs. (length xs, xs))" | |
| 34941 | 5223 |         -- {*Compares lists by their length and then lexicographically*}
 | 
| 15302 | 5224 | |
| 5225 | lemma wf_lexn: "wf r ==> wf (lexn r n)" | |
| 5226 | apply (induct n, simp, simp) | |
| 5227 | apply(rule wf_subset) | |
| 5228 | prefer 2 apply (rule Int_lower1) | |
| 40608 
6c28ab8b8166
mapper for list type; map_pair replaces prod_fun
 haftmann parents: 
40593diff
changeset | 5229 | apply(rule wf_map_pair_image) | 
| 15302 | 5230 | prefer 2 apply (rule inj_onI, auto) | 
| 5231 | done | |
| 5232 | ||
| 5233 | lemma lexn_length: | |
| 24526 | 5234 | "(xs, ys) : lexn r n ==> length xs = n \<and> length ys = n" | 
| 5235 | by (induct n arbitrary: xs ys) auto | |
| 15302 | 5236 | |
| 5237 | lemma wf_lex [intro!]: "wf r ==> wf (lex r)" | |
| 5238 | apply (unfold lex_def) | |
| 5239 | apply (rule wf_UN) | |
| 5240 | apply (blast intro: wf_lexn, clarify) | |
| 5241 | apply (rename_tac m n) | |
| 5242 | apply (subgoal_tac "m \<noteq> n") | |
| 5243 | prefer 2 apply blast | |
| 5244 | apply (blast dest: lexn_length not_sym) | |
| 5245 | done | |
| 5246 | ||
| 5247 | lemma lexn_conv: | |
| 15656 | 5248 | "lexn r n = | 
| 5249 |     {(xs,ys). length xs = n \<and> length ys = n \<and>
 | |
| 5250 | (\<exists>xys x y xs' ys'. xs= xys @ x#xs' \<and> ys= xys @ y # ys' \<and> (x, y):r)}" | |
| 18423 | 5251 | apply (induct n, simp) | 
| 15302 | 5252 | apply (simp add: image_Collect lex_prod_def, safe, blast) | 
| 5253 | apply (rule_tac x = "ab # xys" in exI, simp) | |
| 5254 | apply (case_tac xys, simp_all, blast) | |
| 5255 | done | |
| 5256 | ||
| 5257 | lemma lex_conv: | |
| 15656 | 5258 | "lex r = | 
| 5259 |     {(xs,ys). length xs = length ys \<and>
 | |
| 5260 | (\<exists>xys x y xs' ys'. xs = xys @ x # xs' \<and> ys = xys @ y # ys' \<and> (x, y):r)}" | |
| 15302 | 5261 | by (force simp add: lex_def lexn_conv) | 
| 5262 | ||
| 15693 | 5263 | lemma wf_lenlex [intro!]: "wf r ==> wf (lenlex r)" | 
| 5264 | by (unfold lenlex_def) blast | |
| 5265 | ||
| 5266 | lemma lenlex_conv: | |
| 5267 |     "lenlex r = {(xs,ys). length xs < length ys |
 | |
| 15656 | 5268 | length xs = length ys \<and> (xs, ys) : lex r}" | 
| 30198 | 5269 | by (simp add: lenlex_def Id_on_def lex_prod_def inv_image_def) | 
| 15302 | 5270 | |
| 5271 | lemma Nil_notin_lex [iff]: "([], ys) \<notin> lex r" | |
| 5272 | by (simp add: lex_conv) | |
| 5273 | ||
| 5274 | lemma Nil2_notin_lex [iff]: "(xs, []) \<notin> lex r" | |
| 5275 | by (simp add:lex_conv) | |
| 5276 | ||
| 18447 | 5277 | lemma Cons_in_lex [simp]: | 
| 15656 | 5278 | "((x # xs, y # ys) : lex r) = | 
| 5279 | ((x, y) : r \<and> length xs = length ys | x = y \<and> (xs, ys) : lex r)" | |
| 15302 | 5280 | apply (simp add: lex_conv) | 
| 5281 | apply (rule iffI) | |
| 5282 | prefer 2 apply (blast intro: Cons_eq_appendI, clarify) | |
| 5283 | apply (case_tac xys, simp, simp) | |
| 5284 | apply blast | |
| 5285 | done | |
| 5286 | ||
| 5287 | ||
| 15656 | 5288 | subsubsection {* Lexicographic Ordering *}
 | 
| 5289 | ||
| 5290 | text {* Classical lexicographic ordering on lists, ie. "a" < "ab" < "b".
 | |
| 5291 |     This ordering does \emph{not} preserve well-foundedness.
 | |
| 17090 | 5292 | Author: N. Voelker, March 2005. *} | 
| 15656 | 5293 | |
| 50548 | 5294 | definition lexord :: "('a \<times> 'a) set \<Rightarrow> ('a list \<times> 'a list) set" where
 | 
| 5295 | "lexord r = {(x,y). \<exists> a v. y = x @ a # v \<or>
 | |
| 15656 | 5296 | (\<exists> u a b v w. (a,b) \<in> r \<and> x = u @ (a # v) \<and> y = u @ (b # w))}" | 
| 5297 | ||
| 5298 | lemma lexord_Nil_left[simp]: "([],y) \<in> lexord r = (\<exists> a x. y = a # x)" | |
| 24349 | 5299 | by (unfold lexord_def, induct_tac y, auto) | 
| 15656 | 5300 | |
| 5301 | lemma lexord_Nil_right[simp]: "(x,[]) \<notin> lexord r" | |
| 24349 | 5302 | by (unfold lexord_def, induct_tac x, auto) | 
| 15656 | 5303 | |
| 5304 | lemma lexord_cons_cons[simp]: | |
| 5305 | "((a # x, b # y) \<in> lexord r) = ((a,b)\<in> r | (a = b & (x,y)\<in> lexord r))" | |
| 5306 | apply (unfold lexord_def, safe, simp_all) | |
| 5307 | apply (case_tac u, simp, simp) | |
| 5308 | apply (case_tac u, simp, clarsimp, blast, blast, clarsimp) | |
| 5309 | apply (erule_tac x="b # u" in allE) | |
| 5310 | by force | |
| 5311 | ||
| 5312 | lemmas lexord_simps = lexord_Nil_left lexord_Nil_right lexord_cons_cons | |
| 5313 | ||
| 5314 | lemma lexord_append_rightI: "\<exists> b z. y = b # z \<Longrightarrow> (x, x @ y) \<in> lexord r" | |
| 24349 | 5315 | by (induct_tac x, auto) | 
| 15656 | 5316 | |
| 5317 | lemma lexord_append_left_rightI: | |
| 5318 | "(a,b) \<in> r \<Longrightarrow> (u @ a # x, u @ b # y) \<in> lexord r" | |
| 24349 | 5319 | by (induct_tac u, auto) | 
| 15656 | 5320 | |
| 5321 | lemma lexord_append_leftI: " (u,v) \<in> lexord r \<Longrightarrow> (x @ u, x @ v) \<in> lexord r" | |
| 24349 | 5322 | by (induct x, auto) | 
| 15656 | 5323 | |
| 5324 | lemma lexord_append_leftD: | |
| 5325 | "\<lbrakk> (x @ u, x @ v) \<in> lexord r; (! a. (a,a) \<notin> r) \<rbrakk> \<Longrightarrow> (u,v) \<in> lexord r" | |
| 24349 | 5326 | by (erule rev_mp, induct_tac x, auto) | 
| 15656 | 5327 | |
| 5328 | lemma lexord_take_index_conv: | |
| 5329 | "((x,y) : lexord r) = | |
| 5330 | ((length x < length y \<and> take (length x) y = x) \<or> | |
| 5331 | (\<exists>i. i < min(length x)(length y) & take i x = take i y & (x!i,y!i) \<in> r))" | |
| 5332 | apply (unfold lexord_def Let_def, clarsimp) | |
| 5333 | apply (rule_tac f = "(% a b. a \<or> b)" in arg_cong2) | |
| 5334 | apply auto | |
| 5335 | apply (rule_tac x="hd (drop (length x) y)" in exI) | |
| 5336 | apply (rule_tac x="tl (drop (length x) y)" in exI) | |
| 5337 | apply (erule subst, simp add: min_def) | |
| 5338 | apply (rule_tac x ="length u" in exI, simp) | |
| 5339 | apply (rule_tac x ="take i x" in exI) | |
| 5340 | apply (rule_tac x ="x ! i" in exI) | |
| 5341 | apply (rule_tac x ="y ! i" in exI, safe) | |
| 5342 | apply (rule_tac x="drop (Suc i) x" in exI) | |
| 5343 | apply (drule sym, simp add: drop_Suc_conv_tl) | |
| 5344 | apply (rule_tac x="drop (Suc i) y" in exI) | |
| 5345 | by (simp add: drop_Suc_conv_tl) | |
| 5346 | ||
| 5347 | -- {* lexord is extension of partial ordering List.lex *} 
 | |
| 41986 | 5348 | lemma lexord_lex: "(x,y) \<in> lex r = ((x,y) \<in> lexord r \<and> length x = length y)" | 
| 15656 | 5349 | apply (rule_tac x = y in spec) | 
| 5350 | apply (induct_tac x, clarsimp) | |
| 5351 | by (clarify, case_tac x, simp, force) | |
| 5352 | ||
| 41986 | 5353 | lemma lexord_irreflexive: "ALL x. (x,x) \<notin> r \<Longrightarrow> (xs,xs) \<notin> lexord r" | 
| 5354 | by (induct xs) auto | |
| 5355 | ||
| 5356 | text{* By Ren\'e Thiemann: *}
 | |
| 5357 | lemma lexord_partial_trans: | |
| 5358 | "(\<And>x y z. x \<in> set xs \<Longrightarrow> (x,y) \<in> r \<Longrightarrow> (y,z) \<in> r \<Longrightarrow> (x,z) \<in> r) | |
| 5359 | \<Longrightarrow> (xs,ys) \<in> lexord r \<Longrightarrow> (ys,zs) \<in> lexord r \<Longrightarrow> (xs,zs) \<in> lexord r" | |
| 5360 | proof (induct xs arbitrary: ys zs) | |
| 5361 | case Nil | |
| 5362 | from Nil(3) show ?case unfolding lexord_def by (cases zs, auto) | |
| 5363 | next | |
| 5364 | case (Cons x xs yys zzs) | |
| 5365 | from Cons(3) obtain y ys where yys: "yys = y # ys" unfolding lexord_def | |
| 5366 | by (cases yys, auto) | |
| 5367 | note Cons = Cons[unfolded yys] | |
| 5368 | from Cons(3) have one: "(x,y) \<in> r \<or> x = y \<and> (xs,ys) \<in> lexord r" by auto | |
| 5369 | from Cons(4) obtain z zs where zzs: "zzs = z # zs" unfolding lexord_def | |
| 5370 | by (cases zzs, auto) | |
| 5371 | note Cons = Cons[unfolded zzs] | |
| 5372 | from Cons(4) have two: "(y,z) \<in> r \<or> y = z \<and> (ys,zs) \<in> lexord r" by auto | |
| 5373 |   {
 | |
| 5374 | assume "(xs,ys) \<in> lexord r" and "(ys,zs) \<in> lexord r" | |
| 5375 | from Cons(1)[OF _ this] Cons(2) | |
| 5376 | have "(xs,zs) \<in> lexord r" by auto | |
| 5377 | } note ind1 = this | |
| 5378 |   {
 | |
| 5379 | assume "(x,y) \<in> r" and "(y,z) \<in> r" | |
| 5380 | from Cons(2)[OF _ this] have "(x,z) \<in> r" by auto | |
| 5381 | } note ind2 = this | |
| 5382 | from one two ind1 ind2 | |
| 5383 | have "(x,z) \<in> r \<or> x = z \<and> (xs,zs) \<in> lexord r" by blast | |
| 5384 | thus ?case unfolding zzs by auto | |
| 5385 | qed | |
| 15656 | 5386 | |
| 5387 | lemma lexord_trans: | |
| 5388 | "\<lbrakk> (x, y) \<in> lexord r; (y, z) \<in> lexord r; trans r \<rbrakk> \<Longrightarrow> (x, z) \<in> lexord r" | |
| 41986 | 5389 | by(auto simp: trans_def intro:lexord_partial_trans) | 
| 15656 | 5390 | |
| 5391 | lemma lexord_transI: "trans r \<Longrightarrow> trans (lexord r)" | |
| 24349 | 5392 | by (rule transI, drule lexord_trans, blast) | 
| 15656 | 5393 | |
| 5394 | 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" | |
| 5395 | apply (rule_tac x = y in spec) | |
| 5396 | apply (induct_tac x, rule allI) | |
| 5397 | apply (case_tac x, simp, simp) | |
| 5398 | apply (rule allI, case_tac x, simp, simp) | |
| 5399 | by blast | |
| 5400 | ||
| 54593 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 Andreas Lochbihler parents: 
54498diff
changeset | 5401 | text {*
 | 
| 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 Andreas Lochbihler parents: 
54498diff
changeset | 5402 | Predicate version of lexicographic order integrated with Isabelle's order type classes. | 
| 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 Andreas Lochbihler parents: 
54498diff
changeset | 5403 | Author: Andreas Lochbihler | 
| 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 Andreas Lochbihler parents: 
54498diff
changeset | 5404 | *} | 
| 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 Andreas Lochbihler parents: 
54498diff
changeset | 5405 | |
| 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 Andreas Lochbihler parents: 
54498diff
changeset | 5406 | context ord begin | 
| 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 Andreas Lochbihler parents: 
54498diff
changeset | 5407 | |
| 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 Andreas Lochbihler parents: 
54498diff
changeset | 5408 | inductive lexordp :: "'a list \<Rightarrow> 'a list \<Rightarrow> bool" | 
| 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 Andreas Lochbihler parents: 
54498diff
changeset | 5409 | where | 
| 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 Andreas Lochbihler parents: 
54498diff
changeset | 5410 | Nil: "lexordp [] (y # ys)" | 
| 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 Andreas Lochbihler parents: 
54498diff
changeset | 5411 | | Cons: "x < y \<Longrightarrow> lexordp (x # xs) (y # ys)" | 
| 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 Andreas Lochbihler parents: 
54498diff
changeset | 5412 | | Cons_eq: | 
| 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 Andreas Lochbihler parents: 
54498diff
changeset | 5413 | "\<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: 
54498diff
changeset | 5414 | |
| 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 Andreas Lochbihler parents: 
54498diff
changeset | 5415 | lemma lexordp_simps [simp]: | 
| 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 Andreas Lochbihler parents: 
54498diff
changeset | 5416 | "lexordp [] ys = (ys \<noteq> [])" | 
| 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 Andreas Lochbihler parents: 
54498diff
changeset | 5417 | "lexordp xs [] = False" | 
| 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 Andreas Lochbihler parents: 
54498diff
changeset | 5418 | "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: 
54498diff
changeset | 5419 | by(subst lexordp.simps, fastforce simp add: neq_Nil_conv)+ | 
| 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 Andreas Lochbihler parents: 
54498diff
changeset | 5420 | |
| 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 Andreas Lochbihler parents: 
54498diff
changeset | 5421 | inductive lexordp_eq :: "'a list \<Rightarrow> 'a list \<Rightarrow> bool" where | 
| 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 Andreas Lochbihler parents: 
54498diff
changeset | 5422 | Nil: "lexordp_eq [] ys" | 
| 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 Andreas Lochbihler parents: 
54498diff
changeset | 5423 | | Cons: "x < y \<Longrightarrow> lexordp_eq (x # xs) (y # ys)" | 
| 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 Andreas Lochbihler parents: 
54498diff
changeset | 5424 | | 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: 
54498diff
changeset | 5425 | |
| 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 Andreas Lochbihler parents: 
54498diff
changeset | 5426 | lemma lexordp_eq_simps [simp]: | 
| 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 Andreas Lochbihler parents: 
54498diff
changeset | 5427 | "lexordp_eq [] ys = True" | 
| 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 Andreas Lochbihler parents: 
54498diff
changeset | 5428 | "lexordp_eq xs [] \<longleftrightarrow> xs = []" | 
| 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 Andreas Lochbihler parents: 
54498diff
changeset | 5429 | "lexordp_eq (x # xs) [] = False" | 
| 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 Andreas Lochbihler parents: 
54498diff
changeset | 5430 | "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: 
54498diff
changeset | 5431 | by(subst lexordp_eq.simps, fastforce)+ | 
| 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 Andreas Lochbihler parents: 
54498diff
changeset | 5432 | |
| 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 Andreas Lochbihler parents: 
54498diff
changeset | 5433 | lemma lexordp_append_rightI: "ys \<noteq> Nil \<Longrightarrow> lexordp xs (xs @ ys)" | 
| 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 Andreas Lochbihler parents: 
54498diff
changeset | 5434 | by(induct xs)(auto simp add: neq_Nil_conv) | 
| 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 Andreas Lochbihler parents: 
54498diff
changeset | 5435 | |
| 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 Andreas Lochbihler parents: 
54498diff
changeset | 5436 | 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: 
54498diff
changeset | 5437 | by(induct us) auto | 
| 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 Andreas Lochbihler parents: 
54498diff
changeset | 5438 | |
| 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 Andreas Lochbihler parents: 
54498diff
changeset | 5439 | lemma lexordp_eq_refl: "lexordp_eq xs xs" | 
| 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 Andreas Lochbihler parents: 
54498diff
changeset | 5440 | by(induct xs) simp_all | 
| 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 Andreas Lochbihler parents: 
54498diff
changeset | 5441 | |
| 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 Andreas Lochbihler parents: 
54498diff
changeset | 5442 | 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: 
54498diff
changeset | 5443 | by(induct xs) auto | 
| 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 Andreas Lochbihler parents: 
54498diff
changeset | 5444 | |
| 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 Andreas Lochbihler parents: 
54498diff
changeset | 5445 | 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: 
54498diff
changeset | 5446 | by(induct xs) auto | 
| 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 Andreas Lochbihler parents: 
54498diff
changeset | 5447 | |
| 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 Andreas Lochbihler parents: 
54498diff
changeset | 5448 | lemma lexordp_irreflexive: | 
| 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 Andreas Lochbihler parents: 
54498diff
changeset | 5449 | assumes irrefl: "\<forall>x. \<not> x < x" | 
| 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 Andreas Lochbihler parents: 
54498diff
changeset | 5450 | shows "\<not> lexordp xs xs" | 
| 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 Andreas Lochbihler parents: 
54498diff
changeset | 5451 | proof | 
| 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 Andreas Lochbihler parents: 
54498diff
changeset | 5452 | assume "lexordp xs xs" | 
| 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 Andreas Lochbihler parents: 
54498diff
changeset | 5453 | thus False by(induct xs ys\<equiv>xs)(simp_all add: irrefl) | 
| 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 Andreas Lochbihler parents: 
54498diff
changeset | 5454 | qed | 
| 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 Andreas Lochbihler parents: 
54498diff
changeset | 5455 | |
| 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 Andreas Lochbihler parents: 
54498diff
changeset | 5456 | lemma lexordp_into_lexordp_eq: | 
| 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 Andreas Lochbihler parents: 
54498diff
changeset | 5457 | assumes "lexordp xs ys" | 
| 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 Andreas Lochbihler parents: 
54498diff
changeset | 5458 | shows "lexordp_eq xs ys" | 
| 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 Andreas Lochbihler parents: 
54498diff
changeset | 5459 | using assms by induct simp_all | 
| 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 Andreas Lochbihler parents: 
54498diff
changeset | 5460 | |
| 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 Andreas Lochbihler parents: 
54498diff
changeset | 5461 | end | 
| 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 Andreas Lochbihler parents: 
54498diff
changeset | 5462 | |
| 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 Andreas Lochbihler parents: 
54498diff
changeset | 5463 | declare ord.lexordp_simps [simp, code] | 
| 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 Andreas Lochbihler parents: 
54498diff
changeset | 5464 | declare ord.lexordp_eq_simps [code, simp] | 
| 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 Andreas Lochbihler parents: 
54498diff
changeset | 5465 | |
| 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 Andreas Lochbihler parents: 
54498diff
changeset | 5466 | lemma lexord_code [code, code_unfold]: "lexordp = ord.lexordp less" | 
| 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 Andreas Lochbihler parents: 
54498diff
changeset | 5467 | unfolding lexordp_def ord.lexordp_def .. | 
| 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 Andreas Lochbihler parents: 
54498diff
changeset | 5468 | |
| 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 Andreas Lochbihler parents: 
54498diff
changeset | 5469 | context order begin | 
| 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 Andreas Lochbihler parents: 
54498diff
changeset | 5470 | |
| 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 Andreas Lochbihler parents: 
54498diff
changeset | 5471 | lemma lexordp_antisym: | 
| 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 Andreas Lochbihler parents: 
54498diff
changeset | 5472 | assumes "lexordp xs ys" "lexordp ys xs" | 
| 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 Andreas Lochbihler parents: 
54498diff
changeset | 5473 | shows False | 
| 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 Andreas Lochbihler parents: 
54498diff
changeset | 5474 | using assms by induct auto | 
| 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 Andreas Lochbihler parents: 
54498diff
changeset | 5475 | |
| 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 Andreas Lochbihler parents: 
54498diff
changeset | 5476 | lemma lexordp_irreflexive': "\<not> lexordp xs xs" | 
| 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 Andreas Lochbihler parents: 
54498diff
changeset | 5477 | by(rule lexordp_irreflexive) simp | 
| 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 Andreas Lochbihler parents: 
54498diff
changeset | 5478 | |
| 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 Andreas Lochbihler parents: 
54498diff
changeset | 5479 | end | 
| 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 Andreas Lochbihler parents: 
54498diff
changeset | 5480 | |
| 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 Andreas Lochbihler parents: 
54498diff
changeset | 5481 | context linorder begin | 
| 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 Andreas Lochbihler parents: 
54498diff
changeset | 5482 | |
| 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 Andreas Lochbihler parents: 
54498diff
changeset | 5483 | 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: 
54498diff
changeset | 5484 | assumes "lexordp xs ys" | 
| 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 Andreas Lochbihler parents: 
54498diff
changeset | 5485 | obtains (Nil) y ys' where "xs = []" "ys = y # ys'" | 
| 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 Andreas Lochbihler parents: 
54498diff
changeset | 5486 | | (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: 
54498diff
changeset | 5487 | | (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: 
54498diff
changeset | 5488 | 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: 
54498diff
changeset | 5489 | |
| 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 Andreas Lochbihler parents: 
54498diff
changeset | 5490 | 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: 
54498diff
changeset | 5491 | assumes major: "lexordp xs ys" | 
| 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 Andreas Lochbihler parents: 
54498diff
changeset | 5492 | and Nil: "\<And>y ys. P [] (y # ys)" | 
| 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 Andreas Lochbihler parents: 
54498diff
changeset | 5493 | 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: 
54498diff
changeset | 5494 | 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: 
54498diff
changeset | 5495 | shows "P xs ys" | 
| 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 Andreas Lochbihler parents: 
54498diff
changeset | 5496 | 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: 
54498diff
changeset | 5497 | |
| 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 Andreas Lochbihler parents: 
54498diff
changeset | 5498 | lemma lexordp_iff: | 
| 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 Andreas Lochbihler parents: 
54498diff
changeset | 5499 | "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: 
54498diff
changeset | 5500 | (is "?lhs = ?rhs") | 
| 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 Andreas Lochbihler parents: 
54498diff
changeset | 5501 | proof | 
| 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 Andreas Lochbihler parents: 
54498diff
changeset | 5502 | assume ?lhs thus ?rhs | 
| 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 Andreas Lochbihler parents: 
54498diff
changeset | 5503 | proof induct | 
| 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 Andreas Lochbihler parents: 
54498diff
changeset | 5504 | case Cons_eq thus ?case by simp (metis append.simps(2)) | 
| 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 Andreas Lochbihler parents: 
54498diff
changeset | 5505 | qed(fastforce intro: disjI2 del: disjCI intro: exI[where x="[]"])+ | 
| 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 Andreas Lochbihler parents: 
54498diff
changeset | 5506 | next | 
| 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 Andreas Lochbihler parents: 
54498diff
changeset | 5507 | assume ?rhs thus ?lhs | 
| 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 Andreas Lochbihler parents: 
54498diff
changeset | 5508 | by(auto intro: lexordp_append_leftI[where us="[]", simplified] lexordp_append_leftI) | 
| 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 Andreas Lochbihler parents: 
54498diff
changeset | 5509 | qed | 
| 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 Andreas Lochbihler parents: 
54498diff
changeset | 5510 | |
| 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 Andreas Lochbihler parents: 
54498diff
changeset | 5511 | lemma lexordp_conv_lexord: | 
| 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 Andreas Lochbihler parents: 
54498diff
changeset | 5512 |   "lexordp xs ys \<longleftrightarrow> (xs, ys) \<in> lexord {(x, y). x < y}"
 | 
| 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 Andreas Lochbihler parents: 
54498diff
changeset | 5513 | by(simp add: lexordp_iff lexord_def) | 
| 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 Andreas Lochbihler parents: 
54498diff
changeset | 5514 | |
| 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 Andreas Lochbihler parents: 
54498diff
changeset | 5515 | lemma lexordp_eq_antisym: | 
| 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 Andreas Lochbihler parents: 
54498diff
changeset | 5516 | assumes "lexordp_eq xs ys" "lexordp_eq ys xs" | 
| 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 Andreas Lochbihler parents: 
54498diff
changeset | 5517 | shows "xs = ys" | 
| 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 Andreas Lochbihler parents: 
54498diff
changeset | 5518 | using assms by induct simp_all | 
| 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 Andreas Lochbihler parents: 
54498diff
changeset | 5519 | |
| 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 Andreas Lochbihler parents: 
54498diff
changeset | 5520 | lemma lexordp_eq_trans: | 
| 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 Andreas Lochbihler parents: 
54498diff
changeset | 5521 | assumes "lexordp_eq xs ys" and "lexordp_eq ys zs" | 
| 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 Andreas Lochbihler parents: 
54498diff
changeset | 5522 | shows "lexordp_eq xs zs" | 
| 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 Andreas Lochbihler parents: 
54498diff
changeset | 5523 | using assms | 
| 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 Andreas Lochbihler parents: 
54498diff
changeset | 5524 | apply(induct arbitrary: zs) | 
| 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 Andreas Lochbihler parents: 
54498diff
changeset | 5525 | apply(case_tac [2-3] zs) | 
| 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 Andreas Lochbihler parents: 
54498diff
changeset | 5526 | apply auto | 
| 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 Andreas Lochbihler parents: 
54498diff
changeset | 5527 | done | 
| 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 Andreas Lochbihler parents: 
54498diff
changeset | 5528 | |
| 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 Andreas Lochbihler parents: 
54498diff
changeset | 5529 | lemma lexordp_trans: | 
| 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 Andreas Lochbihler parents: 
54498diff
changeset | 5530 | assumes "lexordp xs ys" "lexordp ys zs" | 
| 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 Andreas Lochbihler parents: 
54498diff
changeset | 5531 | shows "lexordp xs zs" | 
| 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 Andreas Lochbihler parents: 
54498diff
changeset | 5532 | using assms | 
| 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 Andreas Lochbihler parents: 
54498diff
changeset | 5533 | apply(induct arbitrary: zs) | 
| 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 Andreas Lochbihler parents: 
54498diff
changeset | 5534 | apply(case_tac [2-3] zs) | 
| 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 Andreas Lochbihler parents: 
54498diff
changeset | 5535 | apply auto | 
| 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 Andreas Lochbihler parents: 
54498diff
changeset | 5536 | done | 
| 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 Andreas Lochbihler parents: 
54498diff
changeset | 5537 | |
| 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 Andreas Lochbihler parents: 
54498diff
changeset | 5538 | 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: 
54498diff
changeset | 5539 | proof(induct xs arbitrary: ys) | 
| 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 Andreas Lochbihler parents: 
54498diff
changeset | 5540 | case Nil thus ?case by(cases ys) simp_all | 
| 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 Andreas Lochbihler parents: 
54498diff
changeset | 5541 | next | 
| 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 Andreas Lochbihler parents: 
54498diff
changeset | 5542 | case Cons thus ?case by(cases ys) auto | 
| 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 Andreas Lochbihler parents: 
54498diff
changeset | 5543 | qed | 
| 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 Andreas Lochbihler parents: 
54498diff
changeset | 5544 | |
| 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 Andreas Lochbihler parents: 
54498diff
changeset | 5545 | 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: 
54498diff
changeset | 5546 | (is "?lhs \<longleftrightarrow> ?rhs") | 
| 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 Andreas Lochbihler parents: 
54498diff
changeset | 5547 | proof | 
| 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 Andreas Lochbihler parents: 
54498diff
changeset | 5548 | assume ?lhs | 
| 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 Andreas Lochbihler parents: 
54498diff
changeset | 5549 | moreover hence "\<not> lexordp_eq ys xs" by induct simp_all | 
| 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 Andreas Lochbihler parents: 
54498diff
changeset | 5550 | ultimately show ?rhs by(simp add: lexordp_into_lexordp_eq) | 
| 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 Andreas Lochbihler parents: 
54498diff
changeset | 5551 | next | 
| 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 Andreas Lochbihler parents: 
54498diff
changeset | 5552 | assume ?rhs | 
| 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 Andreas Lochbihler parents: 
54498diff
changeset | 5553 | 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: 
54498diff
changeset | 5554 | thus ?lhs by induct simp_all | 
| 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 Andreas Lochbihler parents: 
54498diff
changeset | 5555 | qed | 
| 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 Andreas Lochbihler parents: 
54498diff
changeset | 5556 | |
| 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 Andreas Lochbihler parents: 
54498diff
changeset | 5557 | 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: 
54498diff
changeset | 5558 | 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: 
54498diff
changeset | 5559 | |
| 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 Andreas Lochbihler parents: 
54498diff
changeset | 5560 | 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: 
54498diff
changeset | 5561 | apply(induct xs arbitrary: ys) | 
| 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 Andreas Lochbihler parents: 
54498diff
changeset | 5562 | apply(case_tac [!] ys) | 
| 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 Andreas Lochbihler parents: 
54498diff
changeset | 5563 | apply auto | 
| 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 Andreas Lochbihler parents: 
54498diff
changeset | 5564 | done | 
| 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 Andreas Lochbihler parents: 
54498diff
changeset | 5565 | |
| 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 Andreas Lochbihler parents: 
54498diff
changeset | 5566 | lemma lexordp_linorder: "class.linorder lexordp_eq lexordp" | 
| 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 Andreas Lochbihler parents: 
54498diff
changeset | 5567 | 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: 
54498diff
changeset | 5568 | |
| 
8c0a27b9c1bd
add predicate version of lexicographic order on lists
 Andreas Lochbihler parents: 
54498diff
changeset | 5569 | end | 
| 15656 | 5570 | |
| 40230 | 5571 | subsubsection {* Lexicographic combination of measure functions *}
 | 
| 21103 
367b4ad7c7cc
Added "measures" combinator for lexicographic combinations of multiple measures.
 krauss parents: 
21079diff
changeset | 5572 | |
| 
367b4ad7c7cc
Added "measures" combinator for lexicographic combinations of multiple measures.
 krauss parents: 
21079diff
changeset | 5573 | text {* These are useful for termination proofs *}
 | 
| 
367b4ad7c7cc
Added "measures" combinator for lexicographic combinations of multiple measures.
 krauss parents: 
21079diff
changeset | 5574 | |
| 50548 | 5575 | 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: 
21079diff
changeset | 5576 | |
| 44013 
5cfc1c36ae97
moved recdef package to HOL/Library/Old_Recdef.thy
 krauss parents: 
43594diff
changeset | 5577 | lemma wf_measures[simp]: "wf (measures fs)" | 
| 24349 | 5578 | unfolding measures_def | 
| 5579 | by blast | |
| 21103 
367b4ad7c7cc
Added "measures" combinator for lexicographic combinations of multiple measures.
 krauss parents: 
21079diff
changeset | 5580 | |
| 
367b4ad7c7cc
Added "measures" combinator for lexicographic combinations of multiple measures.
 krauss parents: 
21079diff
changeset | 5581 | lemma in_measures[simp]: | 
| 
367b4ad7c7cc
Added "measures" combinator for lexicographic combinations of multiple measures.
 krauss parents: 
21079diff
changeset | 5582 | "(x, y) \<in> measures [] = False" | 
| 
367b4ad7c7cc
Added "measures" combinator for lexicographic combinations of multiple measures.
 krauss parents: 
21079diff
changeset | 5583 | "(x, y) \<in> measures (f # fs) | 
| 
367b4ad7c7cc
Added "measures" combinator for lexicographic combinations of multiple measures.
 krauss parents: 
21079diff
changeset | 5584 | = (f x < f y \<or> (f x = f y \<and> (x, y) \<in> measures fs))" | 
| 24349 | 5585 | unfolding measures_def | 
| 5586 | by auto | |
| 21103 
367b4ad7c7cc
Added "measures" combinator for lexicographic combinations of multiple measures.
 krauss parents: 
21079diff
changeset | 5587 | |
| 
367b4ad7c7cc
Added "measures" combinator for lexicographic combinations of multiple measures.
 krauss parents: 
21079diff
changeset | 5588 | lemma measures_less: "f x < f y ==> (x, y) \<in> measures (f#fs)" | 
| 24349 | 5589 | by simp | 
| 21103 
367b4ad7c7cc
Added "measures" combinator for lexicographic combinations of multiple measures.
 krauss parents: 
21079diff
changeset | 5590 | |
| 
367b4ad7c7cc
Added "measures" combinator for lexicographic combinations of multiple measures.
 krauss parents: 
21079diff
changeset | 5591 | lemma measures_lesseq: "f x <= f y ==> (x, y) \<in> measures fs ==> (x, y) \<in> measures (f#fs)" | 
| 24349 | 5592 | by auto | 
| 21103 
367b4ad7c7cc
Added "measures" combinator for lexicographic combinations of multiple measures.
 krauss parents: 
21079diff
changeset | 5593 | |
| 
367b4ad7c7cc
Added "measures" combinator for lexicographic combinations of multiple measures.
 krauss parents: 
21079diff
changeset | 5594 | |
| 40230 | 5595 | subsubsection {* Lifting Relations to Lists: one element *}
 | 
| 5596 | ||
| 5597 | definition listrel1 :: "('a \<times> 'a) set \<Rightarrow> ('a list \<times> 'a list) set" where
 | |
| 5598 | "listrel1 r = {(xs,ys).
 | |
| 5599 | \<exists>us z z' vs. xs = us @ z # vs \<and> (z,z') \<in> r \<and> ys = us @ z' # vs}" | |
| 5600 | ||
| 5601 | lemma listrel1I: | |
| 5602 | "\<lbrakk> (x, y) \<in> r; xs = us @ x # vs; ys = us @ y # vs \<rbrakk> \<Longrightarrow> | |
| 5603 | (xs, ys) \<in> listrel1 r" | |
| 5604 | unfolding listrel1_def by auto | |
| 5605 | ||
| 5606 | lemma listrel1E: | |
| 5607 | "\<lbrakk> (xs, ys) \<in> listrel1 r; | |
| 5608 | !!x y us vs. \<lbrakk> (x, y) \<in> r; xs = us @ x # vs; ys = us @ y # vs \<rbrakk> \<Longrightarrow> P | |
| 5609 | \<rbrakk> \<Longrightarrow> P" | |
| 5610 | unfolding listrel1_def by auto | |
| 5611 | ||
| 5612 | lemma not_Nil_listrel1 [iff]: "([], xs) \<notin> listrel1 r" | |
| 5613 | unfolding listrel1_def by blast | |
| 5614 | ||
| 5615 | lemma not_listrel1_Nil [iff]: "(xs, []) \<notin> listrel1 r" | |
| 5616 | unfolding listrel1_def by blast | |
| 5617 | ||
| 5618 | lemma Cons_listrel1_Cons [iff]: | |
| 5619 | "(x # xs, y # ys) \<in> listrel1 r \<longleftrightarrow> | |
| 5620 | (x,y) \<in> r \<and> xs = ys \<or> x = y \<and> (xs, ys) \<in> listrel1 r" | |
| 5621 | by (simp add: listrel1_def Cons_eq_append_conv) (blast) | |
| 5622 | ||
| 5623 | lemma listrel1I1: "(x,y) \<in> r \<Longrightarrow> (x # xs, y # xs) \<in> listrel1 r" | |
| 5624 | by (metis Cons_listrel1_Cons) | |
| 5625 | ||
| 5626 | lemma listrel1I2: "(xs, ys) \<in> listrel1 r \<Longrightarrow> (x # xs, x # ys) \<in> listrel1 r" | |
| 5627 | by (metis Cons_listrel1_Cons) | |
| 5628 | ||
| 5629 | lemma append_listrel1I: | |
| 5630 | "(xs, ys) \<in> listrel1 r \<and> us = vs \<or> xs = ys \<and> (us, vs) \<in> listrel1 r | |
| 5631 | \<Longrightarrow> (xs @ us, ys @ vs) \<in> listrel1 r" | |
| 5632 | unfolding listrel1_def | |
| 5633 | by auto (blast intro: append_eq_appendI)+ | |
| 5634 | ||
| 5635 | lemma Cons_listrel1E1[elim!]: | |
| 5636 | assumes "(x # xs, ys) \<in> listrel1 r" | |
| 5637 | and "\<And>y. ys = y # xs \<Longrightarrow> (x, y) \<in> r \<Longrightarrow> R" | |
| 5638 | and "\<And>zs. ys = x # zs \<Longrightarrow> (xs, zs) \<in> listrel1 r \<Longrightarrow> R" | |
| 5639 | shows R | |
| 5640 | using assms by (cases ys) blast+ | |
| 5641 | ||
| 5642 | lemma Cons_listrel1E2[elim!]: | |
| 5643 | assumes "(xs, y # ys) \<in> listrel1 r" | |
| 5644 | and "\<And>x. xs = x # ys \<Longrightarrow> (x, y) \<in> r \<Longrightarrow> R" | |
| 5645 | and "\<And>zs. xs = y # zs \<Longrightarrow> (zs, ys) \<in> listrel1 r \<Longrightarrow> R" | |
| 5646 | shows R | |
| 5647 | using assms by (cases xs) blast+ | |
| 5648 | ||
| 5649 | lemma snoc_listrel1_snoc_iff: | |
| 5650 | "(xs @ [x], ys @ [y]) \<in> listrel1 r | |
| 5651 | \<longleftrightarrow> (xs, ys) \<in> listrel1 r \<and> x = y \<or> xs = ys \<and> (x,y) \<in> r" (is "?L \<longleftrightarrow> ?R") | |
| 5652 | proof | |
| 5653 | assume ?L thus ?R | |
| 44890 
22f665a2e91c
new fastforce replacing fastsimp - less confusing name
 nipkow parents: 
44635diff
changeset | 5654 | by (fastforce simp: listrel1_def snoc_eq_iff_butlast butlast_append) | 
| 40230 | 5655 | next | 
| 5656 | assume ?R then show ?L unfolding listrel1_def by force | |
| 5657 | qed | |
| 5658 | ||
| 5659 | lemma listrel1_eq_len: "(xs,ys) \<in> listrel1 r \<Longrightarrow> length xs = length ys" | |
| 5660 | unfolding listrel1_def by auto | |
| 5661 | ||
| 5662 | lemma listrel1_mono: | |
| 5663 | "r \<subseteq> s \<Longrightarrow> listrel1 r \<subseteq> listrel1 s" | |
| 5664 | unfolding listrel1_def by blast | |
| 5665 | ||
| 5666 | ||
| 5667 | lemma listrel1_converse: "listrel1 (r^-1) = (listrel1 r)^-1" | |
| 5668 | unfolding listrel1_def by blast | |
| 5669 | ||
| 5670 | lemma in_listrel1_converse: | |
| 5671 | "(x,y) : listrel1 (r^-1) \<longleftrightarrow> (x,y) : (listrel1 r)^-1" | |
| 5672 | unfolding listrel1_def by blast | |
| 5673 | ||
| 5674 | lemma listrel1_iff_update: | |
| 5675 | "(xs,ys) \<in> (listrel1 r) | |
| 5676 | \<longleftrightarrow> (\<exists>y n. (xs ! n, y) \<in> r \<and> n < length xs \<and> ys = xs[n:=y])" (is "?L \<longleftrightarrow> ?R") | |
| 5677 | proof | |
| 5678 | assume "?L" | |
| 5679 | then obtain x y u v where "xs = u @ x # v" "ys = u @ y # v" "(x,y) \<in> r" | |
| 5680 | unfolding listrel1_def by auto | |
| 5681 | then have "ys = xs[length u := y]" and "length u < length xs" | |
| 5682 | and "(xs ! length u, y) \<in> r" by auto | |
| 5683 | then show "?R" by auto | |
| 5684 | next | |
| 5685 | assume "?R" | |
| 5686 | then obtain x y n where "(xs!n, y) \<in> r" "n < size xs" "ys = xs[n:=y]" "x = xs!n" | |
| 5687 | by auto | |
| 5688 | then obtain u v where "xs = u @ x # v" and "ys = u @ y # v" and "(x, y) \<in> r" | |
| 5689 | by (auto intro: upd_conv_take_nth_drop id_take_nth_drop) | |
| 5690 | then show "?L" by (auto simp: listrel1_def) | |
| 5691 | qed | |
| 5692 | ||
| 5693 | ||
| 44510 | 5694 | text{* Accessible part and wellfoundedness: *}
 | 
| 40230 | 5695 | |
| 5696 | lemma Cons_acc_listrel1I [intro!]: | |
| 54295 | 5697 | "x \<in> Wellfounded.acc r \<Longrightarrow> xs \<in> Wellfounded.acc (listrel1 r) \<Longrightarrow> (x # xs) \<in> Wellfounded.acc (listrel1 r)" | 
| 5698 | apply (induct arbitrary: xs set: Wellfounded.acc) | |
| 40230 | 5699 | apply (erule thin_rl) | 
| 5700 | apply (erule acc_induct) | |
| 5701 | apply (rule accI) | |
| 5702 | apply (blast) | |
| 5703 | done | |
| 5704 | ||
| 54295 | 5705 | lemma lists_accD: "xs \<in> lists (Wellfounded.acc r) \<Longrightarrow> xs \<in> Wellfounded.acc (listrel1 r)" | 
| 40230 | 5706 | apply (induct set: lists) | 
| 5707 | apply (rule accI) | |
| 5708 | apply simp | |
| 5709 | apply (rule accI) | |
| 5710 | apply (fast dest: acc_downward) | |
| 5711 | done | |
| 5712 | ||
| 54295 | 5713 | lemma lists_accI: "xs \<in> Wellfounded.acc (listrel1 r) \<Longrightarrow> xs \<in> lists (Wellfounded.acc r)" | 
| 5714 | apply (induct set: Wellfounded.acc) | |
| 40230 | 5715 | apply clarify | 
| 5716 | apply (rule accI) | |
| 44890 
22f665a2e91c
new fastforce replacing fastsimp - less confusing name
 nipkow parents: 
44635diff
changeset | 5717 | apply (fastforce dest!: in_set_conv_decomp[THEN iffD1] simp: listrel1_def) | 
| 40230 | 5718 | done | 
| 5719 | ||
| 44510 | 5720 | lemma wf_listrel1_iff[simp]: "wf(listrel1 r) = wf r" | 
| 5721 | by(metis wf_acc_iff in_lists_conv_set lists_accI lists_accD Cons_in_lists_iff) | |
| 5722 | ||
| 40230 | 5723 | |
| 5724 | subsubsection {* Lifting Relations to Lists: all elements *}
 | |
| 15302 | 5725 | |
| 23740 | 5726 | inductive_set | 
| 46317 | 5727 |   listrel :: "('a \<times> 'b) set \<Rightarrow> ('a list \<times> 'b list) set"
 | 
| 5728 |   for r :: "('a \<times> 'b) set"
 | |
| 22262 | 5729 | where | 
| 23740 | 5730 | Nil: "([],[]) \<in> listrel r" | 
| 5731 | | Cons: "[| (x,y) \<in> r; (xs,ys) \<in> listrel r |] ==> (x#xs, y#ys) \<in> listrel r" | |
| 5732 | ||
| 5733 | inductive_cases listrel_Nil1 [elim!]: "([],xs) \<in> listrel r" | |
| 5734 | inductive_cases listrel_Nil2 [elim!]: "(xs,[]) \<in> listrel r" | |
| 5735 | inductive_cases listrel_Cons1 [elim!]: "(y#ys,xs) \<in> listrel r" | |
| 5736 | inductive_cases listrel_Cons2 [elim!]: "(xs,y#ys) \<in> listrel r" | |
| 15302 | 5737 | |
| 5738 | ||
| 40230 | 5739 | lemma listrel_eq_len: "(xs, ys) \<in> listrel r \<Longrightarrow> length xs = length ys" | 
| 5740 | by(induct rule: listrel.induct) auto | |
| 5741 | ||
| 46313 | 5742 | lemma listrel_iff_zip [code_unfold]: "(xs,ys) : listrel r \<longleftrightarrow> | 
| 40230 | 5743 | length xs = length ys & (\<forall>(x,y) \<in> set(zip xs ys). (x,y) \<in> r)" (is "?L \<longleftrightarrow> ?R") | 
| 5744 | proof | |
| 5745 | assume ?L thus ?R by induct (auto intro: listrel_eq_len) | |
| 5746 | next | |
| 5747 | assume ?R thus ?L | |
| 5748 | apply (clarify) | |
| 5749 | by (induct rule: list_induct2) (auto intro: listrel.intros) | |
| 5750 | qed | |
| 5751 | ||
| 5752 | lemma listrel_iff_nth: "(xs,ys) : listrel r \<longleftrightarrow> | |
| 5753 | length xs = length ys & (\<forall>n < length xs. (xs!n, ys!n) \<in> r)" (is "?L \<longleftrightarrow> ?R") | |
| 5754 | by (auto simp add: all_set_conv_all_nth listrel_iff_zip) | |
| 5755 | ||
| 5756 | ||
| 15302 | 5757 | lemma listrel_mono: "r \<subseteq> s \<Longrightarrow> listrel r \<subseteq> listrel s" | 
| 5758 | apply clarify | |
| 23740 | 5759 | apply (erule listrel.induct) | 
| 5760 | apply (blast intro: listrel.intros)+ | |
| 15302 | 5761 | done | 
| 5762 | ||
| 5763 | lemma listrel_subset: "r \<subseteq> A \<times> A \<Longrightarrow> listrel r \<subseteq> lists A \<times> lists A" | |
| 5764 | apply clarify | |
| 23740 | 5765 | apply (erule listrel.induct, auto) | 
| 15302 | 5766 | done | 
| 5767 | ||
| 30198 | 5768 | lemma listrel_refl_on: "refl_on A r \<Longrightarrow> refl_on (lists A) (listrel r)" | 
| 5769 | apply (simp add: refl_on_def listrel_subset Ball_def) | |
| 15302 | 5770 | apply (rule allI) | 
| 5771 | apply (induct_tac x) | |
| 23740 | 5772 | apply (auto intro: listrel.intros) | 
| 15302 | 5773 | done | 
| 5774 | ||
| 5775 | lemma listrel_sym: "sym r \<Longrightarrow> sym (listrel r)" | |
| 5776 | apply (auto simp add: sym_def) | |
| 23740 | 5777 | apply (erule listrel.induct) | 
| 5778 | apply (blast intro: listrel.intros)+ | |
| 15302 | 5779 | done | 
| 5780 | ||
| 5781 | lemma listrel_trans: "trans r \<Longrightarrow> trans (listrel r)" | |
| 5782 | apply (simp add: trans_def) | |
| 5783 | apply (intro allI) | |
| 5784 | apply (rule impI) | |
| 23740 | 5785 | apply (erule listrel.induct) | 
| 5786 | apply (blast intro: listrel.intros)+ | |
| 15302 | 5787 | done | 
| 5788 | ||
| 5789 | theorem equiv_listrel: "equiv A r \<Longrightarrow> equiv (lists A) (listrel r)" | |
| 30198 | 5790 | by (simp add: equiv_def listrel_refl_on listrel_sym listrel_trans) | 
| 15302 | 5791 | |
| 40230 | 5792 | lemma listrel_rtrancl_refl[iff]: "(xs,xs) : listrel(r^*)" | 
| 5793 | using listrel_refl_on[of UNIV, OF refl_rtrancl] | |
| 5794 | by(auto simp: refl_on_def) | |
| 5795 | ||
| 5796 | lemma listrel_rtrancl_trans: | |
| 5797 | "\<lbrakk> (xs,ys) : listrel(r^*); (ys,zs) : listrel(r^*) \<rbrakk> | |
| 5798 | \<Longrightarrow> (xs,zs) : listrel(r^*)" | |
| 5799 | by (metis listrel_trans trans_def trans_rtrancl) | |
| 5800 | ||
| 5801 | ||
| 15302 | 5802 | lemma listrel_Nil [simp]: "listrel r `` {[]} = {[]}"
 | 
| 23740 | 5803 | by (blast intro: listrel.intros) | 
| 15302 | 5804 | |
| 5805 | lemma listrel_Cons: | |
| 33318 
ddd97d9dfbfb
moved Nat_Transfer before Divides; distributed Nat_Transfer setup accordingly
 haftmann parents: 
32960diff
changeset | 5806 |      "listrel r `` {x#xs} = set_Cons (r``{x}) (listrel r `` {xs})"
 | 
| 
ddd97d9dfbfb
moved Nat_Transfer before Divides; distributed Nat_Transfer setup accordingly
 haftmann parents: 
32960diff
changeset | 5807 | by (auto simp add: set_Cons_def intro: listrel.intros) | 
| 15302 | 5808 | |
| 40230 | 5809 | text {* Relating @{term listrel1}, @{term listrel} and closures: *}
 | 
| 5810 | ||
| 5811 | lemma listrel1_rtrancl_subset_rtrancl_listrel1: | |
| 5812 | "listrel1 (r^*) \<subseteq> (listrel1 r)^*" | |
| 5813 | proof (rule subrelI) | |
| 5814 | fix xs ys assume 1: "(xs,ys) \<in> listrel1 (r^*)" | |
| 5815 |   { fix x y us vs
 | |
| 5816 | have "(x,y) : r^* \<Longrightarrow> (us @ x # vs, us @ y # vs) : (listrel1 r)^*" | |
| 5817 | proof(induct rule: rtrancl.induct) | |
| 5818 | case rtrancl_refl show ?case by simp | |
| 5819 | next | |
| 5820 | case rtrancl_into_rtrancl thus ?case | |
| 5821 | by (metis listrel1I rtrancl.rtrancl_into_rtrancl) | |
| 5822 | qed } | |
| 5823 | thus "(xs,ys) \<in> (listrel1 r)^*" using 1 by(blast elim: listrel1E) | |
| 5824 | qed | |
| 5825 | ||
| 5826 | lemma rtrancl_listrel1_eq_len: "(x,y) \<in> (listrel1 r)^* \<Longrightarrow> length x = length y" | |
| 5827 | by (induct rule: rtrancl.induct) (auto intro: listrel1_eq_len) | |
| 5828 | ||
| 5829 | lemma rtrancl_listrel1_ConsI1: | |
| 5830 | "(xs,ys) : (listrel1 r)^* \<Longrightarrow> (x#xs,x#ys) : (listrel1 r)^*" | |
| 5831 | apply(induct rule: rtrancl.induct) | |
| 5832 | apply simp | |
| 5833 | by (metis listrel1I2 rtrancl.rtrancl_into_rtrancl) | |
| 5834 | ||
| 5835 | lemma rtrancl_listrel1_ConsI2: | |
| 5836 | "(x,y) \<in> r^* \<Longrightarrow> (xs, ys) \<in> (listrel1 r)^* | |
| 5837 | \<Longrightarrow> (x # xs, y # ys) \<in> (listrel1 r)^*" | |
| 5838 | by (blast intro: rtrancl_trans rtrancl_listrel1_ConsI1 | |
| 5839 | subsetD[OF listrel1_rtrancl_subset_rtrancl_listrel1 listrel1I1]) | |
| 5840 | ||
| 5841 | lemma listrel1_subset_listrel: | |
| 5842 | "r \<subseteq> r' \<Longrightarrow> refl r' \<Longrightarrow> listrel1 r \<subseteq> listrel(r')" | |
| 5843 | by(auto elim!: listrel1E simp add: listrel_iff_zip set_zip refl_on_def) | |
| 5844 | ||
| 5845 | lemma listrel_reflcl_if_listrel1: | |
| 5846 | "(xs,ys) : listrel1 r \<Longrightarrow> (xs,ys) : listrel(r^*)" | |
| 5847 | by(erule listrel1E)(auto simp add: listrel_iff_zip set_zip) | |
| 5848 | ||
| 5849 | lemma listrel_rtrancl_eq_rtrancl_listrel1: "listrel (r^*) = (listrel1 r)^*" | |
| 5850 | proof | |
| 5851 |   { fix x y assume "(x,y) \<in> listrel (r^*)"
 | |
| 5852 | then have "(x,y) \<in> (listrel1 r)^*" | |
| 5853 | by induct (auto intro: rtrancl_listrel1_ConsI2) } | |
| 5854 | then show "listrel (r^*) \<subseteq> (listrel1 r)^*" | |
| 5855 | by (rule subrelI) | |
| 5856 | next | |
| 5857 | show "listrel (r^*) \<supseteq> (listrel1 r)^*" | |
| 5858 | proof(rule subrelI) | |
| 5859 | fix xs ys assume "(xs,ys) \<in> (listrel1 r)^*" | |
| 5860 | then show "(xs,ys) \<in> listrel (r^*)" | |
| 5861 | proof induct | |
| 5862 | case base show ?case by(auto simp add: listrel_iff_zip set_zip) | |
| 5863 | next | |
| 5864 | case (step ys zs) | |
| 5865 | thus ?case by (metis listrel_reflcl_if_listrel1 listrel_rtrancl_trans) | |
| 5866 | qed | |
| 5867 | qed | |
| 5868 | qed | |
| 5869 | ||
| 5870 | lemma rtrancl_listrel1_if_listrel: | |
| 5871 | "(xs,ys) : listrel r \<Longrightarrow> (xs,ys) : (listrel1 r)^*" | |
| 5872 | by(metis listrel_rtrancl_eq_rtrancl_listrel1 subsetD[OF listrel_mono] r_into_rtrancl subsetI) | |
| 5873 | ||
| 5874 | lemma listrel_subset_rtrancl_listrel1: "listrel r \<subseteq> (listrel1 r)^*" | |
| 5875 | by(fast intro:rtrancl_listrel1_if_listrel) | |
| 5876 | ||
| 15302 | 5877 | |
| 26749 
397a1aeede7d
* New attribute "termination_simp": Simp rules for termination proofs
 krauss parents: 
26734diff
changeset | 5878 | subsection {* Size function *}
 | 
| 
397a1aeede7d
* New attribute "termination_simp": Simp rules for termination proofs
 krauss parents: 
26734diff
changeset | 5879 | |
| 26875 
e18574413bc4
Measure functions can now be declared via special rules, allowing for a
 krauss parents: 
26795diff
changeset | 5880 | lemma [measure_function]: "is_measure f \<Longrightarrow> is_measure (list_size f)" | 
| 
e18574413bc4
Measure functions can now be declared via special rules, allowing for a
 krauss parents: 
26795diff
changeset | 5881 | by (rule is_measure_trivial) | 
| 
e18574413bc4
Measure functions can now be declared via special rules, allowing for a
 krauss parents: 
26795diff
changeset | 5882 | |
| 
e18574413bc4
Measure functions can now be declared via special rules, allowing for a
 krauss parents: 
26795diff
changeset | 5883 | lemma [measure_function]: "is_measure f \<Longrightarrow> is_measure (option_size f)" | 
| 
e18574413bc4
Measure functions can now be declared via special rules, allowing for a
 krauss parents: 
26795diff
changeset | 5884 | by (rule is_measure_trivial) | 
| 
e18574413bc4
Measure functions can now be declared via special rules, allowing for a
 krauss parents: 
26795diff
changeset | 5885 | |
| 
e18574413bc4
Measure functions can now be declared via special rules, allowing for a
 krauss parents: 
26795diff
changeset | 5886 | lemma list_size_estimation[termination_simp]: | 
| 
e18574413bc4
Measure functions can now be declared via special rules, allowing for a
 krauss parents: 
26795diff
changeset | 5887 | "x \<in> set xs \<Longrightarrow> y < f x \<Longrightarrow> y < list_size f xs" | 
| 26749 
397a1aeede7d
* New attribute "termination_simp": Simp rules for termination proofs
 krauss parents: 
26734diff
changeset | 5888 | by (induct xs) auto | 
| 
397a1aeede7d
* New attribute "termination_simp": Simp rules for termination proofs
 krauss parents: 
26734diff
changeset | 5889 | |
| 26875 
e18574413bc4
Measure functions can now be declared via special rules, allowing for a
 krauss parents: 
26795diff
changeset | 5890 | lemma list_size_estimation'[termination_simp]: | 
| 
e18574413bc4
Measure functions can now be declared via special rules, allowing for a
 krauss parents: 
26795diff
changeset | 5891 | "x \<in> set xs \<Longrightarrow> y \<le> f x \<Longrightarrow> y \<le> list_size f xs" | 
| 
e18574413bc4
Measure functions can now be declared via special rules, allowing for a
 krauss parents: 
26795diff
changeset | 5892 | by (induct xs) auto | 
| 
e18574413bc4
Measure functions can now be declared via special rules, allowing for a
 krauss parents: 
26795diff
changeset | 5893 | |
| 
e18574413bc4
Measure functions can now be declared via special rules, allowing for a
 krauss parents: 
26795diff
changeset | 5894 | lemma list_size_map[simp]: "list_size f (map g xs) = list_size (f o g) xs" | 
| 
e18574413bc4
Measure functions can now be declared via special rules, allowing for a
 krauss parents: 
26795diff
changeset | 5895 | by (induct xs) auto | 
| 
e18574413bc4
Measure functions can now be declared via special rules, allowing for a
 krauss parents: 
26795diff
changeset | 5896 | |
| 44619 
fd520fa2fb09
adding list_size_append (thanks to Rene Thiemann)
 bulwahn parents: 
44618diff
changeset | 5897 | lemma list_size_append[simp]: "list_size f (xs @ ys) = list_size f xs + list_size f ys" | 
| 
fd520fa2fb09
adding list_size_append (thanks to Rene Thiemann)
 bulwahn parents: 
44618diff
changeset | 5898 | by (induct xs, auto) | 
| 
fd520fa2fb09
adding list_size_append (thanks to Rene Thiemann)
 bulwahn parents: 
44618diff
changeset | 5899 | |
| 26875 
e18574413bc4
Measure functions can now be declared via special rules, allowing for a
 krauss parents: 
26795diff
changeset | 5900 | lemma list_size_pointwise[termination_simp]: | 
| 44618 
f3635643a376
strengthening list_size_pointwise (thanks to Rene Thiemann)
 bulwahn parents: 
44510diff
changeset | 5901 | "(\<And>x. x \<in> set xs \<Longrightarrow> f x \<le> g x) \<Longrightarrow> list_size f xs \<le> list_size g xs" | 
| 26875 
e18574413bc4
Measure functions can now be declared via special rules, allowing for a
 krauss parents: 
26795diff
changeset | 5902 | by (induct xs) force+ | 
| 26749 
397a1aeede7d
* New attribute "termination_simp": Simp rules for termination proofs
 krauss parents: 
26734diff
changeset | 5903 | |
| 31048 
ac146fc38b51
refined HOL string theories and corresponding ML fragments
 haftmann parents: 
31022diff
changeset | 5904 | |
| 46143 | 5905 | subsection {* Monad operation *}
 | 
| 5906 | ||
| 5907 | definition bind :: "'a list \<Rightarrow> ('a \<Rightarrow> 'b list) \<Rightarrow> 'b list" where
 | |
| 50548 | 5908 | "bind xs f = concat (map f xs)" | 
| 46143 | 5909 | |
| 5910 | hide_const (open) bind | |
| 5911 | ||
| 5912 | lemma bind_simps [simp]: | |
| 5913 | "List.bind [] f = []" | |
| 5914 | "List.bind (x # xs) f = f x @ List.bind xs f" | |
| 5915 | by (simp_all add: bind_def) | |
| 5916 | ||
| 5917 | ||
| 33318 
ddd97d9dfbfb
moved Nat_Transfer before Divides; distributed Nat_Transfer setup accordingly
 haftmann parents: 
32960diff
changeset | 5918 | subsection {* Transfer *}
 | 
| 
ddd97d9dfbfb
moved Nat_Transfer before Divides; distributed Nat_Transfer setup accordingly
 haftmann parents: 
32960diff
changeset | 5919 | |
| 50548 | 5920 | definition embed_list :: "nat list \<Rightarrow> int list" where | 
| 5921 | "embed_list l = map int l" | |
| 5922 | ||
| 5923 | definition nat_list :: "int list \<Rightarrow> bool" where | |
| 5924 | "nat_list l = nat_set (set l)" | |
| 5925 | ||
| 5926 | definition return_list :: "int list \<Rightarrow> nat list" where | |
| 5927 | "return_list l = map nat l" | |
| 33318 
ddd97d9dfbfb
moved Nat_Transfer before Divides; distributed Nat_Transfer setup accordingly
 haftmann parents: 
32960diff
changeset | 5928 | |
| 
ddd97d9dfbfb
moved Nat_Transfer before Divides; distributed Nat_Transfer setup accordingly
 haftmann parents: 
32960diff
changeset | 5929 | lemma transfer_nat_int_list_return_embed: "nat_list l \<longrightarrow> | 
| 
ddd97d9dfbfb
moved Nat_Transfer before Divides; distributed Nat_Transfer setup accordingly
 haftmann parents: 
32960diff
changeset | 5930 | embed_list (return_list l) = l" | 
| 
ddd97d9dfbfb
moved Nat_Transfer before Divides; distributed Nat_Transfer setup accordingly
 haftmann parents: 
32960diff
changeset | 5931 | 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: 
32960diff
changeset | 5932 | apply (induct l) | 
| 
ddd97d9dfbfb
moved Nat_Transfer before Divides; distributed Nat_Transfer setup accordingly
 haftmann parents: 
32960diff
changeset | 5933 | apply auto | 
| 
ddd97d9dfbfb
moved Nat_Transfer before Divides; distributed Nat_Transfer setup accordingly
 haftmann parents: 
32960diff
changeset | 5934 | done | 
| 
ddd97d9dfbfb
moved Nat_Transfer before Divides; distributed Nat_Transfer setup accordingly
 haftmann parents: 
32960diff
changeset | 5935 | |
| 
ddd97d9dfbfb
moved Nat_Transfer before Divides; distributed Nat_Transfer setup accordingly
 haftmann parents: 
32960diff
changeset | 5936 | lemma transfer_nat_int_list_functions: | 
| 
ddd97d9dfbfb
moved Nat_Transfer before Divides; distributed Nat_Transfer setup accordingly
 haftmann parents: 
32960diff
changeset | 5937 | "l @ m = return_list (embed_list l @ embed_list m)" | 
| 
ddd97d9dfbfb
moved Nat_Transfer before Divides; distributed Nat_Transfer setup accordingly
 haftmann parents: 
32960diff
changeset | 5938 | "[] = return_list []" | 
| 
ddd97d9dfbfb
moved Nat_Transfer before Divides; distributed Nat_Transfer setup accordingly
 haftmann parents: 
32960diff
changeset | 5939 | unfolding return_list_def embed_list_def | 
| 
ddd97d9dfbfb
moved Nat_Transfer before Divides; distributed Nat_Transfer setup accordingly
 haftmann parents: 
32960diff
changeset | 5940 | apply auto | 
| 
ddd97d9dfbfb
moved Nat_Transfer before Divides; distributed Nat_Transfer setup accordingly
 haftmann parents: 
32960diff
changeset | 5941 | apply (induct l, auto) | 
| 
ddd97d9dfbfb
moved Nat_Transfer before Divides; distributed Nat_Transfer setup accordingly
 haftmann parents: 
32960diff
changeset | 5942 | apply (induct m, auto) | 
| 
ddd97d9dfbfb
moved Nat_Transfer before Divides; distributed Nat_Transfer setup accordingly
 haftmann parents: 
32960diff
changeset | 5943 | done | 
| 
ddd97d9dfbfb
moved Nat_Transfer before Divides; distributed Nat_Transfer setup accordingly
 haftmann parents: 
32960diff
changeset | 5944 | |
| 
ddd97d9dfbfb
moved Nat_Transfer before Divides; distributed Nat_Transfer setup accordingly
 haftmann parents: 
32960diff
changeset | 5945 | (* | 
| 
ddd97d9dfbfb
moved Nat_Transfer before Divides; distributed Nat_Transfer setup accordingly
 haftmann parents: 
32960diff
changeset | 5946 | lemma transfer_nat_int_fold1: "fold f l x = | 
| 
ddd97d9dfbfb
moved Nat_Transfer before Divides; distributed Nat_Transfer setup accordingly
 haftmann parents: 
32960diff
changeset | 5947 | fold (%x. f (nat x)) (embed_list l) x"; | 
| 
ddd97d9dfbfb
moved Nat_Transfer before Divides; distributed Nat_Transfer setup accordingly
 haftmann parents: 
32960diff
changeset | 5948 | *) | 
| 
ddd97d9dfbfb
moved Nat_Transfer before Divides; distributed Nat_Transfer setup accordingly
 haftmann parents: 
32960diff
changeset | 5949 | |
| 
ddd97d9dfbfb
moved Nat_Transfer before Divides; distributed Nat_Transfer setup accordingly
 haftmann parents: 
32960diff
changeset | 5950 | |
| 37605 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 5951 | subsection {* Code generation *}
 | 
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 5952 | |
| 51875 
dafd097dd1f4
tail recursive version of map, for code generation, optionally
 nipkow parents: 
51738diff
changeset | 5953 | text{* Optional tail recursive version of @{const map}. Can avoid
 | 
| 
dafd097dd1f4
tail recursive version of map, for code generation, optionally
 nipkow parents: 
51738diff
changeset | 5954 | stack overflow in some target languages. *} | 
| 
dafd097dd1f4
tail recursive version of map, for code generation, optionally
 nipkow parents: 
51738diff
changeset | 5955 | |
| 
dafd097dd1f4
tail recursive version of map, for code generation, optionally
 nipkow parents: 
51738diff
changeset | 5956 | 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: 
51738diff
changeset | 5957 | "map_tailrec_rev f [] bs = bs" | | 
| 
dafd097dd1f4
tail recursive version of map, for code generation, optionally
 nipkow parents: 
51738diff
changeset | 5958 | "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: 
51738diff
changeset | 5959 | |
| 
dafd097dd1f4
tail recursive version of map, for code generation, optionally
 nipkow parents: 
51738diff
changeset | 5960 | lemma map_tailrec_rev: | 
| 
dafd097dd1f4
tail recursive version of map, for code generation, optionally
 nipkow parents: 
51738diff
changeset | 5961 | "map_tailrec_rev f as bs = rev(map f as) @ bs" | 
| 
dafd097dd1f4
tail recursive version of map, for code generation, optionally
 nipkow parents: 
51738diff
changeset | 5962 | by(induction as arbitrary: bs) simp_all | 
| 
dafd097dd1f4
tail recursive version of map, for code generation, optionally
 nipkow parents: 
51738diff
changeset | 5963 | |
| 
dafd097dd1f4
tail recursive version of map, for code generation, optionally
 nipkow parents: 
51738diff
changeset | 5964 | 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: 
51738diff
changeset | 5965 | "map_tailrec f as = rev (map_tailrec_rev f as [])" | 
| 
dafd097dd1f4
tail recursive version of map, for code generation, optionally
 nipkow parents: 
51738diff
changeset | 5966 | |
| 
dafd097dd1f4
tail recursive version of map, for code generation, optionally
 nipkow parents: 
51738diff
changeset | 5967 | text{* Code equation: *}
 | 
| 
dafd097dd1f4
tail recursive version of map, for code generation, optionally
 nipkow parents: 
51738diff
changeset | 5968 | lemma map_eq_map_tailrec: "map = map_tailrec" | 
| 
dafd097dd1f4
tail recursive version of map, for code generation, optionally
 nipkow parents: 
51738diff
changeset | 5969 | by(simp add: fun_eq_iff map_tailrec_def map_tailrec_rev) | 
| 
dafd097dd1f4
tail recursive version of map, for code generation, optionally
 nipkow parents: 
51738diff
changeset | 5970 | |
| 
dafd097dd1f4
tail recursive version of map, for code generation, optionally
 nipkow parents: 
51738diff
changeset | 5971 | |
| 37605 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 5972 | subsubsection {* Counterparts for set-related operations *}
 | 
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 5973 | |
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 5974 | definition member :: "'a list \<Rightarrow> 'a \<Rightarrow> bool" where | 
| 50548 | 5975 | [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: 
37465diff
changeset | 5976 | |
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 5977 | text {*
 | 
| 46030 
51b2f3412a03
attribute code_abbrev superseedes code_unfold_post; tuned text
 haftmann parents: 
45993diff
changeset | 5978 |   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: 
37465diff
changeset | 5979 |   @{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: 
37465diff
changeset | 5980 | *} | 
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 5981 | |
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 5982 | lemma member_rec [code]: | 
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 5983 | "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: 
37465diff
changeset | 5984 | "member [] y \<longleftrightarrow> False" | 
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 5985 | by (auto simp add: member_def) | 
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 5986 | |
| 46030 
51b2f3412a03
attribute code_abbrev superseedes code_unfold_post; tuned text
 haftmann parents: 
45993diff
changeset | 5987 | 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: 
37465diff
changeset | 5988 | "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: 
37465diff
changeset | 5989 | by (simp add: member_def) | 
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 5990 | |
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 5991 | definition list_all :: "('a \<Rightarrow> bool) \<Rightarrow> 'a list \<Rightarrow> bool" where
 | 
| 50548 | 5992 | list_all_iff [code_abbrev]: "list_all P xs \<longleftrightarrow> Ball (set xs) P" | 
| 37605 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 5993 | |
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 5994 | definition list_ex :: "('a \<Rightarrow> bool) \<Rightarrow> 'a list \<Rightarrow> bool" where
 | 
| 50548 | 5995 | 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: 
45993diff
changeset | 5996 | |
| 
51b2f3412a03
attribute code_abbrev superseedes code_unfold_post; tuned text
 haftmann parents: 
45993diff
changeset | 5997 | definition list_ex1 :: "('a \<Rightarrow> bool) \<Rightarrow> 'a list \<Rightarrow> bool" where
 | 
| 50548 | 5998 | list_ex1_iff [code_abbrev]: "list_ex1 P xs \<longleftrightarrow> (\<exists>! x. x \<in> set xs \<and> P x)" | 
| 40652 | 5999 | |
| 37605 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 6000 | text {*
 | 
| 46030 
51b2f3412a03
attribute code_abbrev superseedes code_unfold_post; tuned text
 haftmann parents: 
45993diff
changeset | 6001 |   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: 
45993diff
changeset | 6002 |   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: 
45993diff
changeset | 6003 |   and @{const list_ex1} in specifications.
 | 
| 37605 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 6004 | *} | 
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 6005 | |
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 6006 | lemma list_all_simps [simp, code]: | 
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 6007 | "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: 
37465diff
changeset | 6008 | "list_all P [] \<longleftrightarrow> True" | 
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 6009 | by (simp_all add: list_all_iff) | 
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 6010 | |
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 6011 | lemma list_ex_simps [simp, code]: | 
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 6012 | "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: 
37465diff
changeset | 6013 | "list_ex P [] \<longleftrightarrow> False" | 
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 6014 | by (simp_all add: list_ex_iff) | 
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 6015 | |
| 40652 | 6016 | lemma list_ex1_simps [simp, code]: | 
| 6017 | "list_ex1 P [] = False" | |
| 6018 | "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: 
45993diff
changeset | 6019 | by (auto simp add: list_ex1_iff list_all_iff) | 
| 
51b2f3412a03
attribute code_abbrev superseedes code_unfold_post; tuned text
 haftmann parents: 
45993diff
changeset | 6020 | |
| 
51b2f3412a03
attribute code_abbrev superseedes code_unfold_post; tuned text
 haftmann parents: 
45993diff
changeset | 6021 | 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: 
37465diff
changeset | 6022 | "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: 
37465diff
changeset | 6023 | by (simp add: list_all_iff) | 
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 6024 | |
| 46030 
51b2f3412a03
attribute code_abbrev superseedes code_unfold_post; tuned text
 haftmann parents: 
45993diff
changeset | 6025 | 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: 
37465diff
changeset | 6026 | "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: 
37465diff
changeset | 6027 | by (simp add: list_ex_iff) | 
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 6028 | |
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 6029 | lemma list_all_append [simp]: | 
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 6030 | "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: 
37465diff
changeset | 6031 | by (auto simp add: list_all_iff) | 
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 6032 | |
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 6033 | lemma list_ex_append [simp]: | 
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 6034 | "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: 
37465diff
changeset | 6035 | by (auto simp add: list_ex_iff) | 
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 6036 | |
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 6037 | lemma list_all_rev [simp]: | 
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 6038 | "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: 
37465diff
changeset | 6039 | by (simp add: list_all_iff) | 
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 6040 | |
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 6041 | lemma list_ex_rev [simp]: | 
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 6042 | "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: 
37465diff
changeset | 6043 | by (simp add: list_ex_iff) | 
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 6044 | |
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 6045 | lemma list_all_length: | 
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 6046 | "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: 
37465diff
changeset | 6047 | 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: 
37465diff
changeset | 6048 | |
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 6049 | lemma list_ex_length: | 
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 6050 | "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: 
37465diff
changeset | 6051 | 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: 
37465diff
changeset | 6052 | |
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 6053 | lemma list_all_cong [fundef_cong]: | 
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 6054 | "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: 
37465diff
changeset | 6055 | by (simp add: list_all_iff) | 
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 6056 | |
| 47131 | 6057 | lemma list_ex_cong [fundef_cong]: | 
| 37605 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 6058 | "xs = ys \<Longrightarrow> (\<And>x. x \<in> set ys \<Longrightarrow> f x = g x) \<Longrightarrow> list_ex f xs = list_ex g ys" | 
| 47131 | 6059 | by (simp add: list_ex_iff) | 
| 37605 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 6060 | |
| 50548 | 6061 | definition can_select :: "('a \<Rightarrow> bool) \<Rightarrow> 'a set \<Rightarrow> bool" where
 | 
| 6062 | [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: 
49808diff
changeset | 6063 | |
| 
744934b818c7
moved quite generic material from theory Enum to more appropriate places
 haftmann parents: 
49808diff
changeset | 6064 | lemma can_select_set_list_ex1 [code]: | 
| 
744934b818c7
moved quite generic material from theory Enum to more appropriate places
 haftmann parents: 
49808diff
changeset | 6065 | "can_select P (set A) = list_ex1 P A" | 
| 
744934b818c7
moved quite generic material from theory Enum to more appropriate places
 haftmann parents: 
49808diff
changeset | 6066 | by (simp add: list_ex1_iff can_select_def) | 
| 
744934b818c7
moved quite generic material from theory Enum to more appropriate places
 haftmann parents: 
49808diff
changeset | 6067 | |
| 
744934b818c7
moved quite generic material from theory Enum to more appropriate places
 haftmann parents: 
49808diff
changeset | 6068 | |
| 46313 | 6069 | text {* Executable checks for relations on sets *}
 | 
| 6070 | ||
| 6071 | definition listrel1p :: "('a \<Rightarrow> 'a \<Rightarrow> bool) \<Rightarrow> 'a list \<Rightarrow> 'a list \<Rightarrow> bool" where
 | |
| 6072 | "listrel1p r xs ys = ((xs, ys) \<in> listrel1 {(x, y). r x y})"
 | |
| 6073 | ||
| 6074 | lemma [code_unfold]: | |
| 6075 | "(xs, ys) \<in> listrel1 r = listrel1p (\<lambda>x y. (x, y) \<in> r) xs ys" | |
| 6076 | unfolding listrel1p_def by auto | |
| 6077 | ||
| 6078 | lemma [code]: | |
| 6079 | "listrel1p r [] xs = False" | |
| 6080 | "listrel1p r xs [] = False" | |
| 6081 | "listrel1p r (x # xs) (y # ys) \<longleftrightarrow> | |
| 6082 | r x y \<and> xs = ys \<or> x = y \<and> listrel1p r xs ys" | |
| 6083 | by (simp add: listrel1p_def)+ | |
| 6084 | ||
| 6085 | definition | |
| 6086 |   lexordp :: "('a \<Rightarrow> 'a \<Rightarrow> bool) \<Rightarrow> 'a list \<Rightarrow> 'a list \<Rightarrow> bool" where
 | |
| 6087 |   "lexordp r xs ys = ((xs, ys) \<in> lexord {(x, y). r x y})"
 | |
| 6088 | ||
| 6089 | lemma [code_unfold]: | |
| 6090 | "(xs, ys) \<in> lexord r = lexordp (\<lambda>x y. (x, y) \<in> r) xs ys" | |
| 6091 | unfolding lexordp_def by auto | |
| 6092 | ||
| 6093 | lemma [code]: | |
| 6094 | "lexordp r xs [] = False" | |
| 6095 | "lexordp r [] (y#ys) = True" | |
| 6096 | "lexordp r (x # xs) (y # ys) = (r x y | (x = y & lexordp r xs ys))" | |
| 6097 | unfolding lexordp_def by auto | |
| 6098 | ||
| 37605 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 6099 | text {* Bounded quantification and summation over nats. *}
 | 
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 6100 | |
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 6101 | lemma atMost_upto [code_unfold]: | 
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 6102 |   "{..n} = set [0..<Suc n]"
 | 
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 6103 | by auto | 
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 6104 | |
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 6105 | lemma atLeast_upt [code_unfold]: | 
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 6106 |   "{..<n} = set [0..<n]"
 | 
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 6107 | by auto | 
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 6108 | |
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 6109 | lemma greaterThanLessThan_upt [code_unfold]: | 
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 6110 |   "{n<..<m} = set [Suc n..<m]"
 | 
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 6111 | by auto | 
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 6112 | |
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 6113 | 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: 
37465diff
changeset | 6114 | |
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 6115 | lemma greaterThanAtMost_upt [code_unfold]: | 
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 6116 |   "{n<..m} = set [Suc n..<Suc m]"
 | 
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 6117 | by auto | 
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 6118 | |
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 6119 | lemma atLeastAtMost_upt [code_unfold]: | 
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 6120 |   "{n..m} = set [n..<Suc m]"
 | 
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 6121 | by auto | 
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 6122 | |
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 6123 | lemma all_nat_less_eq [code_unfold]: | 
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 6124 |   "(\<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: 
37465diff
changeset | 6125 | by auto | 
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 6126 | |
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 6127 | lemma ex_nat_less_eq [code_unfold]: | 
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 6128 |   "(\<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: 
37465diff
changeset | 6129 | by auto | 
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 6130 | |
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 6131 | lemma all_nat_less [code_unfold]: | 
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 6132 |   "(\<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: 
37465diff
changeset | 6133 | by auto | 
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 6134 | |
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 6135 | lemma ex_nat_less [code_unfold]: | 
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 6136 |   "(\<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: 
37465diff
changeset | 6137 | by auto | 
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 6138 | |
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 6139 | 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: 
37465diff
changeset | 6140 | "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: 
37465diff
changeset | 6141 | 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: 
37465diff
changeset | 6142 | |
| 53954 | 6143 | text{* Bounded @{text LEAST} operator: *}
 | 
| 6144 | ||
| 6145 | definition "Bleast S P = (LEAST x. x \<in> S \<and> P x)" | |
| 6146 | ||
| 6147 | definition "abort_Bleast S P = (LEAST x. x \<in> S \<and> P x)" | |
| 6148 | ||
| 54890 
cb892d835803
fundamental treatment of undefined vs. universally partial replaces code_abort
 haftmann parents: 
54885diff
changeset | 6149 | declare [[code abort: abort_Bleast]] | 
| 53954 | 6150 | |
| 6151 | lemma Bleast_code [code]: | |
| 6152 | "Bleast (set xs) P = (case filter P (sort xs) of | |
| 6153 | x#xs \<Rightarrow> x | | |
| 6154 | [] \<Rightarrow> abort_Bleast (set xs) P)" | |
| 6155 | proof (cases "filter P (sort xs)") | |
| 6156 | case Nil thus ?thesis by (simp add: Bleast_def abort_Bleast_def) | |
| 6157 | next | |
| 6158 | case (Cons x ys) | |
| 6159 | have "(LEAST x. x \<in> set xs \<and> P x) = x" | |
| 6160 | proof (rule Least_equality) | |
| 6161 | show "x \<in> set xs \<and> P x" | |
| 6162 | by (metis Cons Cons_eq_filter_iff in_set_conv_decomp set_sort) | |
| 6163 | next | |
| 6164 | fix y assume "y : set xs \<and> P y" | |
| 6165 | hence "y : set (filter P xs)" by auto | |
| 6166 | thus "x \<le> y" | |
| 6167 | by (metis Cons eq_iff filter_sort set_ConsD set_sort sorted_Cons sorted_sort) | |
| 6168 | qed | |
| 6169 | thus ?thesis using Cons by (simp add: Bleast_def) | |
| 6170 | qed | |
| 6171 | ||
| 6172 | declare Bleast_def[symmetric, code_unfold] | |
| 6173 | ||
| 37605 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 6174 | text {* Summation over ints. *}
 | 
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 6175 | |
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 6176 | lemma greaterThanLessThan_upto [code_unfold]: | 
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 6177 |   "{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: 
37465diff
changeset | 6178 | by auto | 
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 6179 | |
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 6180 | lemma atLeastLessThan_upto [code_unfold]: | 
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 6181 |   "{i..<j::int} = set [i..j - 1]"
 | 
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 6182 | by auto | 
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 6183 | |
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 6184 | lemma greaterThanAtMost_upto [code_unfold]: | 
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 6185 |   "{i<..j::int} = set [i+1..j]"
 | 
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 6186 | by auto | 
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 6187 | |
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 6188 | 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: 
37465diff
changeset | 6189 | |
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 6190 | 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: 
37465diff
changeset | 6191 | "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: 
37465diff
changeset | 6192 | 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: 
37465diff
changeset | 6193 | |
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 6194 | |
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 6195 | subsubsection {* Optimizing by rewriting *}
 | 
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 6196 | |
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 6197 | definition null :: "'a list \<Rightarrow> bool" where | 
| 46030 
51b2f3412a03
attribute code_abbrev superseedes code_unfold_post; tuned text
 haftmann parents: 
45993diff
changeset | 6198 | [code_abbrev]: "null xs \<longleftrightarrow> xs = []" | 
| 37605 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 6199 | |
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 6200 | text {*
 | 
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 6201 |   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: 
37465diff
changeset | 6202 | *} | 
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 6203 | |
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 6204 | lemma null_rec [code]: | 
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 6205 | "null (x # xs) \<longleftrightarrow> False" | 
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 6206 | "null [] \<longleftrightarrow> True" | 
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 6207 | by (simp_all add: null_def) | 
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 6208 | |
| 46030 
51b2f3412a03
attribute code_abbrev superseedes code_unfold_post; tuned text
 haftmann parents: 
45993diff
changeset | 6209 | 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: 
37465diff
changeset | 6210 | "xs = [] \<longleftrightarrow> null xs" | 
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 6211 | by (simp add: null_def) | 
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 6212 | |
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 6213 | lemma equal_Nil_null [code_unfold]: | 
| 38857 
97775f3e8722
renamed class/constant eq to equal; tuned some instantiations
 haftmann parents: 
38715diff
changeset | 6214 | "HOL.equal xs [] \<longleftrightarrow> null xs" | 
| 53940 
36cf426cb1c6
Added symmetric code_unfold-lemmas for null and is_none
 lammich <lammich@in.tum.de> parents: 
53721diff
changeset | 6215 | "HOL.equal [] = null" | 
| 
36cf426cb1c6
Added symmetric code_unfold-lemmas for null and is_none
 lammich <lammich@in.tum.de> parents: 
53721diff
changeset | 6216 | 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: 
37465diff
changeset | 6217 | |
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 6218 | 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: 
45993diff
changeset | 6219 | [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: 
37465diff
changeset | 6220 | |
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 6221 | 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: 
37465diff
changeset | 6222 | [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: 
37465diff
changeset | 6223 | |
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 6224 | text {*
 | 
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 6225 |   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: 
37465diff
changeset | 6226 | 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: 
37465diff
changeset | 6227 | *} | 
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 6228 | |
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 6229 | lemma maps_simps [code]: | 
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 6230 | "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: 
37465diff
changeset | 6231 | "maps f [] = []" | 
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 6232 | by (simp_all add: maps_def) | 
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 6233 | |
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 6234 | lemma map_filter_simps [code]: | 
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 6235 | "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: 
37465diff
changeset | 6236 | "map_filter f [] = []" | 
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 6237 | 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: 
37465diff
changeset | 6238 | |
| 46030 
51b2f3412a03
attribute code_abbrev superseedes code_unfold_post; tuned text
 haftmann parents: 
45993diff
changeset | 6239 | 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: 
37465diff
changeset | 6240 | "concat (map f xs) = maps f xs" | 
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 6241 | by (simp add: maps_def) | 
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 6242 | |
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 6243 | lemma map_filter_map_filter [code_unfold]: | 
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 6244 | "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: 
37465diff
changeset | 6245 | by (simp add: map_filter_def) | 
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 6246 | |
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 6247 | 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: 
37465diff
changeset | 6248 | and similiarly for @{text"\<exists>"}. *}
 | 
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 6249 | |
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 6250 | 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: 
37465diff
changeset | 6251 |   "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: 
37465diff
changeset | 6252 | |
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 6253 | lemma [code]: | 
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 6254 | "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: 
37465diff
changeset | 6255 | proof - | 
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 6256 |   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: 
37465diff
changeset | 6257 | proof - | 
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 6258 | fix n | 
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 6259 |     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: 
37465diff
changeset | 6260 | 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: 
37465diff
changeset | 6261 | qed | 
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 6262 | 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: 
37465diff
changeset | 6263 | qed | 
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 6264 | |
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 6265 | 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: 
37465diff
changeset | 6266 | "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: 
37465diff
changeset | 6267 | 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: 
37465diff
changeset | 6268 | |
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 6269 | 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: 
37465diff
changeset | 6270 | "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: 
37465diff
changeset | 6271 | 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: 
37465diff
changeset | 6272 | |
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 6273 | 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: 
37465diff
changeset | 6274 |   "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: 
37465diff
changeset | 6275 | |
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 6276 | lemma [code]: | 
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 6277 | "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: 
37465diff
changeset | 6278 | proof - | 
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 6279 |   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: 
37465diff
changeset | 6280 | proof - | 
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 6281 | fix k | 
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 6282 |     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: 
37465diff
changeset | 6283 | 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: 
37465diff
changeset | 6284 | qed | 
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 6285 | 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: 
37465diff
changeset | 6286 | qed | 
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 6287 | |
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 6288 | 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: 
37465diff
changeset | 6289 | "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: 
37465diff
changeset | 6290 | 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: 
37465diff
changeset | 6291 | |
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 6292 | 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: 
37465diff
changeset | 6293 | "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: 
37465diff
changeset | 6294 | 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: 
37465diff
changeset | 6295 | |
| 49808 
418991ce7567
tail-recursive implementation for length
 Andreas Lochbihler parents: 
49757diff
changeset | 6296 | text {* optimized code (tail-recursive) for @{term length} *}
 | 
| 
418991ce7567
tail-recursive implementation for length
 Andreas Lochbihler parents: 
49757diff
changeset | 6297 | |
| 
418991ce7567
tail-recursive implementation for length
 Andreas Lochbihler parents: 
49757diff
changeset | 6298 | definition gen_length :: "nat \<Rightarrow> 'a list \<Rightarrow> nat" | 
| 
418991ce7567
tail-recursive implementation for length
 Andreas Lochbihler parents: 
49757diff
changeset | 6299 | where "gen_length n xs = n + length xs" | 
| 
418991ce7567
tail-recursive implementation for length
 Andreas Lochbihler parents: 
49757diff
changeset | 6300 | |
| 
418991ce7567
tail-recursive implementation for length
 Andreas Lochbihler parents: 
49757diff
changeset | 6301 | lemma gen_length_code [code]: | 
| 
418991ce7567
tail-recursive implementation for length
 Andreas Lochbihler parents: 
49757diff
changeset | 6302 | "gen_length n [] = n" | 
| 
418991ce7567
tail-recursive implementation for length
 Andreas Lochbihler parents: 
49757diff
changeset | 6303 | "gen_length n (x # xs) = gen_length (Suc n) xs" | 
| 
418991ce7567
tail-recursive implementation for length
 Andreas Lochbihler parents: 
49757diff
changeset | 6304 | by(simp_all add: gen_length_def) | 
| 
418991ce7567
tail-recursive implementation for length
 Andreas Lochbihler parents: 
49757diff
changeset | 6305 | |
| 
418991ce7567
tail-recursive implementation for length
 Andreas Lochbihler parents: 
49757diff
changeset | 6306 | declare list.size(3-4)[code del] | 
| 
418991ce7567
tail-recursive implementation for length
 Andreas Lochbihler parents: 
49757diff
changeset | 6307 | |
| 
418991ce7567
tail-recursive implementation for length
 Andreas Lochbihler parents: 
49757diff
changeset | 6308 | lemma length_code [code]: "length = gen_length 0" | 
| 
418991ce7567
tail-recursive implementation for length
 Andreas Lochbihler parents: 
49757diff
changeset | 6309 | by(simp add: gen_length_def fun_eq_iff) | 
| 
418991ce7567
tail-recursive implementation for length
 Andreas Lochbihler parents: 
49757diff
changeset | 6310 | |
| 
418991ce7567
tail-recursive implementation for length
 Andreas Lochbihler parents: 
49757diff
changeset | 6311 | 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: 
37465diff
changeset | 6312 | |
| 49948 
744934b818c7
moved quite generic material from theory Enum to more appropriate places
 haftmann parents: 
49808diff
changeset | 6313 | |
| 37605 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 6314 | subsubsection {* Pretty lists *}
 | 
| 15064 
4f3102b50197
- Moved code generator setup for lists from Main.thy to List.thy
 berghofe parents: 
15045diff
changeset | 6315 | |
| 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: 
50134diff
changeset | 6316 | 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: 
50134diff
changeset | 6317 | (* 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: 
50134diff
changeset | 6318 | |
| 
ee729dbd1b7f
avoid ML_file in large theory files to improve performance of dependency discovery of main HOL (approx. 1s CPU time) -- relevant for any application using it, e.g. small paper sessions;
 wenzelm parents: 
50134diff
changeset | 6319 | 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: 
50134diff
changeset | 6320 | sig | 
| 55148 
7e1b7cb54114
avoid (now superfluous) indirect passing of constant names
 haftmann parents: 
55147diff
changeset | 6321 | 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: 
50134diff
changeset | 6322 | 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: 
50134diff
changeset | 6323 | -> (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: 
50134diff
changeset | 6324 | -> 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: 
50134diff
changeset | 6325 | 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: 
50134diff
changeset | 6326 | 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: 
50134diff
changeset | 6327 | |
| 
ee729dbd1b7f
avoid ML_file in large theory files to improve performance of dependency discovery of main HOL (approx. 1s CPU time) -- relevant for any application using it, e.g. small paper sessions;
 wenzelm parents: 
50134diff
changeset | 6328 | 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: 
50134diff
changeset | 6329 | 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: 
50134diff
changeset | 6330 | |
| 
ee729dbd1b7f
avoid ML_file in large theory files to improve performance of dependency discovery of main HOL (approx. 1s CPU time) -- relevant for any application using it, e.g. small paper sessions;
 wenzelm parents: 
50134diff
changeset | 6331 | 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: 
50134diff
changeset | 6332 | |
| 55148 
7e1b7cb54114
avoid (now superfluous) indirect passing of constant names
 haftmann parents: 
55147diff
changeset | 6333 | 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: 
50134diff
changeset | 6334 | let | 
| 55148 
7e1b7cb54114
avoid (now superfluous) indirect passing of constant names
 haftmann parents: 
55147diff
changeset | 6335 |     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: 
50134diff
changeset | 6336 | | 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: 
50134diff
changeset | 6337 | 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: 
50134diff
changeset | 6338 | in case t' | 
| 55148 
7e1b7cb54114
avoid (now superfluous) indirect passing of constant names
 haftmann parents: 
55147diff
changeset | 6339 |    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: 
50134diff
changeset | 6340 | | _ => 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: 
50134diff
changeset | 6341 | 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: 
50134diff
changeset | 6342 | |
| 
ee729dbd1b7f
avoid ML_file in large theory files to improve performance of dependency discovery of main HOL (approx. 1s CPU time) -- relevant for any application using it, e.g. small paper sessions;
 wenzelm parents: 
50134diff
changeset | 6343 | 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: 
50134diff
changeset | 6344 | 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: 
50134diff
changeset | 6345 | 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: 
50134diff
changeset | 6346 | 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: 
50134diff
changeset | 6347 | 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: 
50134diff
changeset | 6348 | ); | 
| 
ee729dbd1b7f
avoid ML_file in large theory files to improve performance of dependency discovery of main HOL (approx. 1s CPU time) -- relevant for any application using it, e.g. small paper sessions;
 wenzelm parents: 
50134diff
changeset | 6349 | |
| 
ee729dbd1b7f
avoid ML_file in large theory files to improve performance of dependency discovery of main HOL (approx. 1s CPU time) -- relevant for any application using it, e.g. small paper sessions;
 wenzelm parents: 
50134diff
changeset | 6350 | 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: 
50134diff
changeset | 6351 | let | 
| 55148 
7e1b7cb54114
avoid (now superfluous) indirect passing of constant names
 haftmann parents: 
55147diff
changeset | 6352 | fun pretty literals pr _ vars fxy [(t1, _), (t2, _)] = | 
| 
7e1b7cb54114
avoid (now superfluous) indirect passing of constant names
 haftmann parents: 
55147diff
changeset | 6353 | 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: 
50134diff
changeset | 6354 | 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: 
50134diff
changeset | 6355 | 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: 
50134diff
changeset | 6356 | | 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: 
50134diff
changeset | 6357 | 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: 
52380diff
changeset | 6358 | in | 
| 
6646bb548c6b
migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
 haftmann parents: 
52380diff
changeset | 6359 |     Code_Target.set_printings (Code_Symbol.Constant (@{const_name Cons},
 | 
| 55148 
7e1b7cb54114
avoid (now superfluous) indirect passing of constant names
 haftmann parents: 
55147diff
changeset | 6360 | [(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: 
50134diff
changeset | 6361 | 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: 
50134diff
changeset | 6362 | |
| 
ee729dbd1b7f
avoid ML_file in large theory files to improve performance of dependency discovery of main HOL (approx. 1s CPU time) -- relevant for any application using it, e.g. small paper sessions;
 wenzelm parents: 
50134diff
changeset | 6363 | 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: 
50134diff
changeset | 6364 | *} | 
| 31055 
2cf6efca6c71
proper structures for list and string code generation stuff
 haftmann parents: 
31048diff
changeset | 6365 | |
| 52435 
6646bb548c6b
migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
 haftmann parents: 
52380diff
changeset | 6366 | code_printing | 
| 
6646bb548c6b
migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
 haftmann parents: 
52380diff
changeset | 6367 | type_constructor list \<rightharpoonup> | 
| 
6646bb548c6b
migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
 haftmann parents: 
52380diff
changeset | 6368 | (SML) "_ list" | 
| 
6646bb548c6b
migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
 haftmann parents: 
52380diff
changeset | 6369 | and (OCaml) "_ list" | 
| 
6646bb548c6b
migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
 haftmann parents: 
52380diff
changeset | 6370 | and (Haskell) "![(_)]" | 
| 
6646bb548c6b
migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
 haftmann parents: 
52380diff
changeset | 6371 | and (Scala) "List[(_)]" | 
| 
6646bb548c6b
migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
 haftmann parents: 
52380diff
changeset | 6372 | | constant Nil \<rightharpoonup> | 
| 
6646bb548c6b
migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
 haftmann parents: 
52380diff
changeset | 6373 | (SML) "[]" | 
| 
6646bb548c6b
migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
 haftmann parents: 
52380diff
changeset | 6374 | and (OCaml) "[]" | 
| 
6646bb548c6b
migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
 haftmann parents: 
52380diff
changeset | 6375 | and (Haskell) "[]" | 
| 
6646bb548c6b
migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
 haftmann parents: 
52380diff
changeset | 6376 | and (Scala) "!Nil" | 
| 
6646bb548c6b
migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
 haftmann parents: 
52380diff
changeset | 6377 | | 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: 
52380diff
changeset | 6378 | (Haskell) - | 
| 
6646bb548c6b
migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
 haftmann parents: 
52380diff
changeset | 6379 | | 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: 
52380diff
changeset | 6380 | (Haskell) infix 4 "==" | 
| 
6646bb548c6b
migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
 haftmann parents: 
52380diff
changeset | 6381 | |
| 
6646bb548c6b
migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
 haftmann parents: 
52380diff
changeset | 6382 | setup {* fold (List_Code.add_literal_list) ["SML", "OCaml", "Haskell", "Scala"] *}
 | 
| 31048 
ac146fc38b51
refined HOL string theories and corresponding ML fragments
 haftmann parents: 
31022diff
changeset | 6383 | |
| 
ac146fc38b51
refined HOL string theories and corresponding ML fragments
 haftmann parents: 
31022diff
changeset | 6384 | code_reserved SML | 
| 
ac146fc38b51
refined HOL string theories and corresponding ML fragments
 haftmann parents: 
31022diff
changeset | 6385 | list | 
| 
ac146fc38b51
refined HOL string theories and corresponding ML fragments
 haftmann parents: 
31022diff
changeset | 6386 | |
| 
ac146fc38b51
refined HOL string theories and corresponding ML fragments
 haftmann parents: 
31022diff
changeset | 6387 | code_reserved OCaml | 
| 
ac146fc38b51
refined HOL string theories and corresponding ML fragments
 haftmann parents: 
31022diff
changeset | 6388 | list | 
| 
ac146fc38b51
refined HOL string theories and corresponding ML fragments
 haftmann parents: 
31022diff
changeset | 6389 | |
| 21061 
580dfc999ef6
added normal post setup; cleaned up "execution" constants
 haftmann parents: 
21046diff
changeset | 6390 | |
| 37424 
ed431cc99f17
use various predefined Haskell operations when generating code
 haftmann parents: 
37408diff
changeset | 6391 | subsubsection {* Use convenient predefined operations *}
 | 
| 
ed431cc99f17
use various predefined Haskell operations when generating code
 haftmann parents: 
37408diff
changeset | 6392 | |
| 52435 
6646bb548c6b
migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
 haftmann parents: 
52380diff
changeset | 6393 | code_printing | 
| 
6646bb548c6b
migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
 haftmann parents: 
52380diff
changeset | 6394 | constant "op @" \<rightharpoonup> | 
| 
6646bb548c6b
migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
 haftmann parents: 
52380diff
changeset | 6395 | (SML) infixr 7 "@" | 
| 
6646bb548c6b
migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
 haftmann parents: 
52380diff
changeset | 6396 | and (OCaml) infixr 6 "@" | 
| 
6646bb548c6b
migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
 haftmann parents: 
52380diff
changeset | 6397 | and (Haskell) infixr 5 "++" | 
| 
6646bb548c6b
migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
 haftmann parents: 
52380diff
changeset | 6398 | and (Scala) infixl 7 "++" | 
| 
6646bb548c6b
migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
 haftmann parents: 
52380diff
changeset | 6399 | | constant map \<rightharpoonup> | 
| 
6646bb548c6b
migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
 haftmann parents: 
52380diff
changeset | 6400 | (Haskell) "map" | 
| 
6646bb548c6b
migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
 haftmann parents: 
52380diff
changeset | 6401 | | constant filter \<rightharpoonup> | 
| 
6646bb548c6b
migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
 haftmann parents: 
52380diff
changeset | 6402 | (Haskell) "filter" | 
| 
6646bb548c6b
migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
 haftmann parents: 
52380diff
changeset | 6403 | | constant concat \<rightharpoonup> | 
| 
6646bb548c6b
migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
 haftmann parents: 
52380diff
changeset | 6404 | (Haskell) "concat" | 
| 
6646bb548c6b
migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
 haftmann parents: 
52380diff
changeset | 6405 | | constant List.maps \<rightharpoonup> | 
| 
6646bb548c6b
migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
 haftmann parents: 
52380diff
changeset | 6406 | (Haskell) "concatMap" | 
| 
6646bb548c6b
migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
 haftmann parents: 
52380diff
changeset | 6407 | | constant rev \<rightharpoonup> | 
| 
6646bb548c6b
migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
 haftmann parents: 
52380diff
changeset | 6408 | (Haskell) "reverse" | 
| 
6646bb548c6b
migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
 haftmann parents: 
52380diff
changeset | 6409 | | constant zip \<rightharpoonup> | 
| 
6646bb548c6b
migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
 haftmann parents: 
52380diff
changeset | 6410 | (Haskell) "zip" | 
| 
6646bb548c6b
migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
 haftmann parents: 
52380diff
changeset | 6411 | | constant List.null \<rightharpoonup> | 
| 
6646bb548c6b
migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
 haftmann parents: 
52380diff
changeset | 6412 | (Haskell) "null" | 
| 
6646bb548c6b
migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
 haftmann parents: 
52380diff
changeset | 6413 | | constant takeWhile \<rightharpoonup> | 
| 
6646bb548c6b
migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
 haftmann parents: 
52380diff
changeset | 6414 | (Haskell) "takeWhile" | 
| 
6646bb548c6b
migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
 haftmann parents: 
52380diff
changeset | 6415 | | constant dropWhile \<rightharpoonup> | 
| 
6646bb548c6b
migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
 haftmann parents: 
52380diff
changeset | 6416 | (Haskell) "dropWhile" | 
| 
6646bb548c6b
migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
 haftmann parents: 
52380diff
changeset | 6417 | | constant list_all \<rightharpoonup> | 
| 
6646bb548c6b
migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
 haftmann parents: 
52380diff
changeset | 6418 | (Haskell) "all" | 
| 
6646bb548c6b
migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
 haftmann parents: 
52380diff
changeset | 6419 | | constant list_ex \<rightharpoonup> | 
| 
6646bb548c6b
migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
 haftmann parents: 
52380diff
changeset | 6420 | (Haskell) "any" | 
| 37605 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 6421 | |
| 46147 
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
 haftmann parents: 
46143diff
changeset | 6422 | |
| 
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
 haftmann parents: 
46143diff
changeset | 6423 | subsubsection {* Implementation of sets by lists *}
 | 
| 
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
 haftmann parents: 
46143diff
changeset | 6424 | |
| 
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
 haftmann parents: 
46143diff
changeset | 6425 | lemma is_empty_set [code]: | 
| 
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
 haftmann parents: 
46143diff
changeset | 6426 | "Set.is_empty (set xs) \<longleftrightarrow> List.null xs" | 
| 
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
 haftmann parents: 
46143diff
changeset | 6427 | by (simp add: Set.is_empty_def null_def) | 
| 
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
 haftmann parents: 
46143diff
changeset | 6428 | |
| 
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
 haftmann parents: 
46143diff
changeset | 6429 | lemma empty_set [code]: | 
| 
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
 haftmann parents: 
46143diff
changeset | 6430 |   "{} = set []"
 | 
| 
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
 haftmann parents: 
46143diff
changeset | 6431 | by simp | 
| 
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
 haftmann parents: 
46143diff
changeset | 6432 | |
| 46156 | 6433 | lemma UNIV_coset [code]: | 
| 6434 | "UNIV = List.coset []" | |
| 6435 | by simp | |
| 6436 | ||
| 6437 | lemma compl_set [code]: | |
| 6438 | "- set xs = List.coset xs" | |
| 6439 | by simp | |
| 6440 | ||
| 6441 | lemma compl_coset [code]: | |
| 6442 | "- List.coset xs = set xs" | |
| 6443 | by simp | |
| 6444 | ||
| 46147 
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
 haftmann parents: 
46143diff
changeset | 6445 | lemma [code]: | 
| 
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
 haftmann parents: 
46143diff
changeset | 6446 | "x \<in> set xs \<longleftrightarrow> List.member xs x" | 
| 
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
 haftmann parents: 
46143diff
changeset | 6447 | "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: 
46143diff
changeset | 6448 | by (simp_all add: member_def) | 
| 
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
 haftmann parents: 
46143diff
changeset | 6449 | |
| 
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
 haftmann parents: 
46143diff
changeset | 6450 | lemma insert_code [code]: | 
| 
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
 haftmann parents: 
46143diff
changeset | 6451 | "insert x (set xs) = set (List.insert x xs)" | 
| 
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
 haftmann parents: 
46143diff
changeset | 6452 | "insert x (List.coset xs) = List.coset (removeAll x xs)" | 
| 
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
 haftmann parents: 
46143diff
changeset | 6453 | by simp_all | 
| 
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
 haftmann parents: 
46143diff
changeset | 6454 | |
| 
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
 haftmann parents: 
46143diff
changeset | 6455 | lemma remove_code [code]: | 
| 
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
 haftmann parents: 
46143diff
changeset | 6456 | "Set.remove x (set xs) = set (removeAll x xs)" | 
| 
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
 haftmann parents: 
46143diff
changeset | 6457 | "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: 
46143diff
changeset | 6458 | by (simp_all add: remove_def Compl_insert) | 
| 
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
 haftmann parents: 
46143diff
changeset | 6459 | |
| 49757 
73ab6d4a9236
rename Set.project to Set.filter - more appropriate name
 kuncar parents: 
49739diff
changeset | 6460 | lemma filter_set [code]: | 
| 
73ab6d4a9236
rename Set.project to Set.filter - more appropriate name
 kuncar parents: 
49739diff
changeset | 6461 | "Set.filter P (set xs) = set (filter P xs)" | 
| 46156 | 6462 | by auto | 
| 6463 | ||
| 6464 | lemma image_set [code]: | |
| 6465 | "image f (set xs) = set (map f xs)" | |
| 6466 | by simp | |
| 6467 | ||
| 47398 | 6468 | lemma subset_code [code]: | 
| 6469 | "set xs \<le> B \<longleftrightarrow> (\<forall>x\<in>set xs. x \<in> B)" | |
| 6470 | "A \<le> List.coset ys \<longleftrightarrow> (\<forall>y\<in>set ys. y \<notin> A)" | |
| 6471 | "List.coset [] \<le> set [] \<longleftrightarrow> False" | |
| 6472 | by auto | |
| 6473 | ||
| 6474 | text {* A frequent case – avoid intermediate sets *}
 | |
| 6475 | lemma [code_unfold]: | |
| 6476 | "set xs \<subseteq> set ys \<longleftrightarrow> list_all (\<lambda>x. x \<in> set ys) xs" | |
| 6477 | by (auto simp: list_all_iff) | |
| 6478 | ||
| 46147 
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
 haftmann parents: 
46143diff
changeset | 6479 | lemma Ball_set [code]: | 
| 
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
 haftmann parents: 
46143diff
changeset | 6480 | "Ball (set xs) P \<longleftrightarrow> list_all P xs" | 
| 
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
 haftmann parents: 
46143diff
changeset | 6481 | by (simp add: list_all_iff) | 
| 
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
 haftmann parents: 
46143diff
changeset | 6482 | |
| 
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
 haftmann parents: 
46143diff
changeset | 6483 | lemma Bex_set [code]: | 
| 
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
 haftmann parents: 
46143diff
changeset | 6484 | "Bex (set xs) P \<longleftrightarrow> list_ex P xs" | 
| 
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
 haftmann parents: 
46143diff
changeset | 6485 | by (simp add: list_ex_iff) | 
| 
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
 haftmann parents: 
46143diff
changeset | 6486 | |
| 
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
 haftmann parents: 
46143diff
changeset | 6487 | lemma card_set [code]: | 
| 
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
 haftmann parents: 
46143diff
changeset | 6488 | "card (set xs) = length (remdups xs)" | 
| 
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
 haftmann parents: 
46143diff
changeset | 6489 | proof - | 
| 
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
 haftmann parents: 
46143diff
changeset | 6490 | have "card (set (remdups xs)) = length (remdups xs)" | 
| 
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
 haftmann parents: 
46143diff
changeset | 6491 | by (rule distinct_card) simp | 
| 
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
 haftmann parents: 
46143diff
changeset | 6492 | then show ?thesis by simp | 
| 
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
 haftmann parents: 
46143diff
changeset | 6493 | qed | 
| 
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
 haftmann parents: 
46143diff
changeset | 6494 | |
| 46156 | 6495 | lemma the_elem_set [code]: | 
| 6496 | "the_elem (set [x]) = x" | |
| 6497 | by simp | |
| 6498 | ||
| 6499 | lemma Pow_set [code]: | |
| 6500 |   "Pow (set []) = {{}}"
 | |
| 6501 | "Pow (set (x # xs)) = (let A = Pow (set xs) in A \<union> insert x ` A)" | |
| 6502 | by (simp_all add: Pow_insert Let_def) | |
| 6503 | ||
| 46383 | 6504 | lemma setsum_code [code]: | 
| 6505 | "setsum f (set xs) = listsum (map f (remdups xs))" | |
| 6506 | by (simp add: listsum_distinct_conv_setsum_set) | |
| 46147 
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
 haftmann parents: 
46143diff
changeset | 6507 | |
| 46424 
b447318e5e1a
adding code equation for Relation.image; adding map_project as correspondence to map_filter on lists
 bulwahn parents: 
46418diff
changeset | 6508 | 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: 
46418diff
changeset | 6509 |   "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: 
46418diff
changeset | 6510 | |
| 
b447318e5e1a
adding code equation for Relation.image; adding map_project as correspondence to map_filter on lists
 bulwahn parents: 
46418diff
changeset | 6511 | lemma [code]: | 
| 
b447318e5e1a
adding code equation for Relation.image; adding map_project as correspondence to map_filter on lists
 bulwahn parents: 
46418diff
changeset | 6512 | "map_project f (set xs) = set (List.map_filter f xs)" | 
| 47398 | 6513 | 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: 
46418diff
changeset | 6514 | |
| 
b447318e5e1a
adding code equation for Relation.image; adding map_project as correspondence to map_filter on lists
 bulwahn parents: 
46418diff
changeset | 6515 | hide_const (open) map_project | 
| 
b447318e5e1a
adding code equation for Relation.image; adding map_project as correspondence to map_filter on lists
 bulwahn parents: 
46418diff
changeset | 6516 | |
| 49948 
744934b818c7
moved quite generic material from theory Enum to more appropriate places
 haftmann parents: 
49808diff
changeset | 6517 | |
| 46147 
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
 haftmann parents: 
46143diff
changeset | 6518 | text {* Operations on relations *}
 | 
| 
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
 haftmann parents: 
46143diff
changeset | 6519 | |
| 
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
 haftmann parents: 
46143diff
changeset | 6520 | lemma product_code [code]: | 
| 
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
 haftmann parents: 
46143diff
changeset | 6521 | "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: 
46143diff
changeset | 6522 | by (auto simp add: Product_Type.product_def) | 
| 
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
 haftmann parents: 
46143diff
changeset | 6523 | |
| 
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
 haftmann parents: 
46143diff
changeset | 6524 | lemma Id_on_set [code]: | 
| 
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
 haftmann parents: 
46143diff
changeset | 6525 | "Id_on (set xs) = set [(x, x). x \<leftarrow> xs]" | 
| 
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
 haftmann parents: 
46143diff
changeset | 6526 | by (auto simp add: Id_on_def) | 
| 
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
 haftmann parents: 
46143diff
changeset | 6527 | |
| 46424 
b447318e5e1a
adding code equation for Relation.image; adding map_project as correspondence to map_filter on lists
 bulwahn parents: 
46418diff
changeset | 6528 | lemma [code]: | 
| 
b447318e5e1a
adding code equation for Relation.image; adding map_project as correspondence to map_filter on lists
 bulwahn parents: 
46418diff
changeset | 6529 | "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: 
46418diff
changeset | 6530 | 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: 
46418diff
changeset | 6531 | |
| 46147 
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
 haftmann parents: 
46143diff
changeset | 6532 | lemma trancl_set_ntrancl [code]: | 
| 
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
 haftmann parents: 
46143diff
changeset | 6533 | "trancl (set xs) = ntrancl (card (set xs) - 1) (set xs)" | 
| 
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
 haftmann parents: 
46143diff
changeset | 6534 | by (simp add: finite_trancl_ntranl) | 
| 
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
 haftmann parents: 
46143diff
changeset | 6535 | |
| 47433 
07f4bf913230
renamed "rel_comp" to "relcomp" (to be consistent with, e.g., "relpow")
 griff parents: 
47131diff
changeset | 6536 | lemma set_relcomp [code]: | 
| 46147 
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
 haftmann parents: 
46143diff
changeset | 6537 | "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: 
46143diff
changeset | 6538 | by (auto simp add: Bex_def) | 
| 
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
 haftmann parents: 
46143diff
changeset | 6539 | |
| 
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
 haftmann parents: 
46143diff
changeset | 6540 | lemma wf_set [code]: | 
| 
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
 haftmann parents: 
46143diff
changeset | 6541 | "wf (set xs) = acyclic (set xs)" | 
| 
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
 haftmann parents: 
46143diff
changeset | 6542 | by (simp add: wf_iff_acyclic_if_finite) | 
| 
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
 haftmann parents: 
46143diff
changeset | 6543 | |
| 55129 
26bd1cba3ab5
killed 'More_BNFs' by moving its various bits where they (now) belong
 blanchet parents: 
54890diff
changeset | 6544 | |
| 53012 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6545 | subsection {* Setup for Lifting/Transfer *}
 | 
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6546 | |
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6547 | subsubsection {* Relator and predicator properties *}
 | 
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6548 | |
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6549 | lemma list_all2_eq'[relator_eq]: | 
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6550 | "list_all2 (op =) = (op =)" | 
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6551 | by (rule ext)+ (simp add: list_all2_eq) | 
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6552 | |
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6553 | lemma list_all2_mono'[relator_mono]: | 
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6554 | assumes "A \<le> B" | 
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6555 | shows "(list_all2 A) \<le> (list_all2 B)" | 
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6556 | using assms by (auto intro: list_all2_mono) | 
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6557 | |
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6558 | lemma list_all2_OO[relator_distr]: "list_all2 A OO list_all2 B = list_all2 (A OO B)" | 
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6559 | proof (intro ext iffI) | 
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6560 | fix xs ys | 
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6561 | assume "list_all2 (A OO B) xs ys" | 
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6562 | thus "(list_all2 A OO list_all2 B) xs ys" | 
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6563 | unfolding OO_def | 
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6564 | by (induct, simp, simp add: list_all2_Cons1 list_all2_Cons2, fast) | 
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6565 | next | 
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6566 | fix xs ys | 
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6567 | assume "(list_all2 A OO list_all2 B) xs ys" | 
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6568 | then obtain zs where "list_all2 A xs zs" and "list_all2 B zs ys" .. | 
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6569 | thus "list_all2 (A OO B) xs ys" | 
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6570 | by (induct arbitrary: ys, simp, clarsimp simp add: list_all2_Cons1, fast) | 
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6571 | qed | 
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6572 | |
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6573 | lemma Domainp_list[relator_domain]: | 
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6574 | assumes "Domainp A = P" | 
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6575 | shows "Domainp (list_all2 A) = (list_all P)" | 
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6576 | proof - | 
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6577 |   {
 | 
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6578 | fix x | 
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6579 | have *: "\<And>x. (\<exists>y. A x y) = P x" using assms unfolding Domainp_iff by blast | 
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6580 | have "(\<exists>y. (list_all2 A x y)) = list_all P x" | 
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6581 | by (induction x) (simp_all add: * list_all2_Cons1) | 
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6582 | } | 
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6583 | then show ?thesis | 
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6584 | unfolding Domainp_iff[abs_def] | 
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6585 | by (auto iff: fun_eq_iff) | 
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6586 | qed | 
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6587 | |
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6588 | lemma left_total_list_all2[reflexivity_rule]: | 
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6589 | "left_total R \<Longrightarrow> left_total (list_all2 R)" | 
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6590 | unfolding left_total_def | 
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6591 | apply safe | 
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6592 | apply (rename_tac xs, induct_tac xs, simp, simp add: list_all2_Cons1) | 
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6593 | done | 
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6594 | |
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6595 | lemma left_unique_list_all2 [reflexivity_rule]: | 
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6596 | "left_unique R \<Longrightarrow> left_unique (list_all2 R)" | 
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6597 | unfolding left_unique_def | 
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6598 | apply (subst (2) all_comm, subst (1) all_comm) | 
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6599 | apply (rule allI, rename_tac zs, induct_tac zs) | 
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6600 | apply (auto simp add: list_all2_Cons2) | 
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6601 | done | 
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6602 | |
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6603 | lemma right_total_list_all2 [transfer_rule]: | 
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6604 | "right_total R \<Longrightarrow> right_total (list_all2 R)" | 
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6605 | unfolding right_total_def | 
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6606 | by (rule allI, induct_tac y, simp, simp add: list_all2_Cons2) | 
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6607 | |
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6608 | lemma right_unique_list_all2 [transfer_rule]: | 
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6609 | "right_unique R \<Longrightarrow> right_unique (list_all2 R)" | 
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6610 | unfolding right_unique_def | 
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6611 | apply (rule allI, rename_tac xs, induct_tac xs) | 
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6612 | apply (auto simp add: list_all2_Cons1) | 
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6613 | done | 
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6614 | |
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6615 | lemma bi_total_list_all2 [transfer_rule]: | 
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6616 | "bi_total A \<Longrightarrow> bi_total (list_all2 A)" | 
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6617 | unfolding bi_total_def | 
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6618 | apply safe | 
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6619 | apply (rename_tac xs, induct_tac xs, simp, simp add: list_all2_Cons1) | 
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6620 | apply (rename_tac ys, induct_tac ys, simp, simp add: list_all2_Cons2) | 
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6621 | done | 
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6622 | |
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6623 | lemma bi_unique_list_all2 [transfer_rule]: | 
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6624 | "bi_unique A \<Longrightarrow> bi_unique (list_all2 A)" | 
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6625 | unfolding bi_unique_def | 
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6626 | apply (rule conjI) | 
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6627 | apply (rule allI, rename_tac xs, induct_tac xs) | 
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6628 | apply (simp, force simp add: list_all2_Cons1) | 
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6629 | apply (subst (2) all_comm, subst (1) all_comm) | 
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6630 | apply (rule allI, rename_tac xs, induct_tac xs) | 
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6631 | apply (simp, force simp add: list_all2_Cons2) | 
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6632 | done | 
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6633 | |
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6634 | lemma list_invariant_commute [invariant_commute]: | 
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6635 | "list_all2 (Lifting.invariant P) = Lifting.invariant (list_all P)" | 
| 55524 
f41ef840f09d
folded 'list_all2' with the relator generated by 'datatype_new'
 blanchet parents: 
55473diff
changeset | 6636 | apply (simp add: fun_eq_iff list_all2_iff list_all_iff Lifting.invariant_def Ball_def) | 
| 53012 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6637 | apply (intro allI) | 
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6638 | apply (induct_tac rule: list_induct2') | 
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6639 | apply simp_all | 
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6640 | apply fastforce | 
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6641 | done | 
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6642 | |
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6643 | subsubsection {* Quotient theorem for the Lifting package *}
 | 
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6644 | |
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6645 | lemma Quotient_list[quot_map]: | 
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6646 | assumes "Quotient R Abs Rep T" | 
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6647 | shows "Quotient (list_all2 R) (map Abs) (map Rep) (list_all2 T)" | 
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6648 | proof (unfold Quotient_alt_def, intro conjI allI impI) | 
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6649 | from assms have 1: "\<And>x y. T x y \<Longrightarrow> Abs x = y" | 
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6650 | unfolding Quotient_alt_def by simp | 
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6651 | fix xs ys assume "list_all2 T xs ys" thus "map Abs xs = ys" | 
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6652 | by (induct, simp, simp add: 1) | 
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6653 | next | 
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6654 | from assms have 2: "\<And>x. T (Rep x) x" | 
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6655 | unfolding Quotient_alt_def by simp | 
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6656 | fix xs show "list_all2 T (map Rep xs) xs" | 
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6657 | by (induct xs, simp, simp add: 2) | 
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6658 | next | 
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6659 | from assms have 3: "\<And>x y. R x y \<longleftrightarrow> T x (Abs x) \<and> T y (Abs y) \<and> Abs x = Abs y" | 
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6660 | unfolding Quotient_alt_def by simp | 
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6661 | fix xs ys show "list_all2 R xs ys \<longleftrightarrow> list_all2 T xs (map Abs xs) \<and> | 
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6662 | list_all2 T ys (map Abs ys) \<and> map Abs xs = map Abs ys" | 
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6663 | by (induct xs ys rule: list_induct2', simp_all, metis 3) | 
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6664 | qed | 
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6665 | |
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6666 | subsubsection {* Transfer rules for the Transfer package *}
 | 
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6667 | |
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6668 | context | 
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6669 | begin | 
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6670 | interpretation lifting_syntax . | 
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6671 | |
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6672 | lemma Nil_transfer [transfer_rule]: "(list_all2 A) [] []" | 
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6673 | by simp | 
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6674 | |
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6675 | lemma Cons_transfer [transfer_rule]: | 
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6676 | "(A ===> list_all2 A ===> list_all2 A) Cons Cons" | 
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6677 | unfolding fun_rel_def by simp | 
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6678 | |
| 55404 
5cb95b79a51f
transformed 'option' and 'list' into new-style datatypes (but register them as old-style as well)
 blanchet parents: 
55148diff
changeset | 6679 | lemma case_list_transfer [transfer_rule]: | 
| 53012 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6680 | "(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: 
55148diff
changeset | 6681 | case_list case_list" | 
| 53012 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6682 | unfolding fun_rel_def by (simp split: list.split) | 
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6683 | |
| 55404 
5cb95b79a51f
transformed 'option' and 'list' into new-style datatypes (but register them as old-style as well)
 blanchet parents: 
55148diff
changeset | 6684 | lemma rec_list_transfer [transfer_rule]: | 
| 53012 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6685 | "(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: 
55148diff
changeset | 6686 | rec_list rec_list" | 
| 53012 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6687 | unfolding fun_rel_def by (clarify, erule list_all2_induct, simp_all) | 
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6688 | |
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6689 | lemma tl_transfer [transfer_rule]: | 
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6690 | "(list_all2 A ===> list_all2 A) tl tl" | 
| 55405 
0a155051bd9d
use new selector support to define 'the', 'hd', 'tl'
 blanchet parents: 
55404diff
changeset | 6691 | unfolding tl_def[abs_def] by transfer_prover | 
| 53012 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6692 | |
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6693 | lemma butlast_transfer [transfer_rule]: | 
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6694 | "(list_all2 A ===> list_all2 A) butlast butlast" | 
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6695 | by (rule fun_relI, erule list_all2_induct, auto) | 
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6696 | |
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6697 | lemma set_transfer [transfer_rule]: | 
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6698 | "(list_all2 A ===> set_rel A) set set" | 
| 55584 | 6699 | unfolding set_rec[abs_def] by transfer_prover | 
| 53012 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6700 | |
| 55465 | 6701 | lemma map_rec: "map f xs = rec_list Nil (%x _ y. Cons (f x) y) xs" | 
| 6702 | by (induct xs) auto | |
| 6703 | ||
| 53012 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6704 | lemma map_transfer [transfer_rule]: | 
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6705 | "((A ===> B) ===> list_all2 A ===> list_all2 B) map map" | 
| 55465 | 6706 | unfolding map_rec[abs_def] by transfer_prover | 
| 53012 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6707 | |
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6708 | lemma append_transfer [transfer_rule]: | 
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6709 | "(list_all2 A ===> list_all2 A ===> list_all2 A) append append" | 
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6710 | unfolding List.append_def by transfer_prover | 
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6711 | |
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6712 | lemma rev_transfer [transfer_rule]: | 
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6713 | "(list_all2 A ===> list_all2 A) rev rev" | 
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6714 | unfolding List.rev_def by transfer_prover | 
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6715 | |
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6716 | lemma filter_transfer [transfer_rule]: | 
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6717 | "((A ===> op =) ===> list_all2 A ===> list_all2 A) filter filter" | 
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6718 | unfolding List.filter_def by transfer_prover | 
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6719 | |
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6720 | lemma fold_transfer [transfer_rule]: | 
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6721 | "((A ===> B ===> B) ===> list_all2 A ===> B ===> B) fold fold" | 
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6722 | unfolding List.fold_def by transfer_prover | 
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6723 | |
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6724 | lemma foldr_transfer [transfer_rule]: | 
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6725 | "((A ===> B ===> B) ===> list_all2 A ===> B ===> B) foldr foldr" | 
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6726 | unfolding List.foldr_def by transfer_prover | 
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6727 | |
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6728 | lemma foldl_transfer [transfer_rule]: | 
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6729 | "((B ===> A ===> B) ===> B ===> list_all2 A ===> B) foldl foldl" | 
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6730 | unfolding List.foldl_def by transfer_prover | 
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6731 | |
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6732 | lemma concat_transfer [transfer_rule]: | 
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6733 | "(list_all2 (list_all2 A) ===> list_all2 A) concat concat" | 
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6734 | unfolding List.concat_def by transfer_prover | 
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6735 | |
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6736 | lemma drop_transfer [transfer_rule]: | 
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6737 | "(op = ===> list_all2 A ===> list_all2 A) drop drop" | 
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6738 | unfolding List.drop_def by transfer_prover | 
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6739 | |
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6740 | lemma take_transfer [transfer_rule]: | 
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6741 | "(op = ===> list_all2 A ===> list_all2 A) take take" | 
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6742 | unfolding List.take_def by transfer_prover | 
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6743 | |
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6744 | lemma list_update_transfer [transfer_rule]: | 
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6745 | "(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: 
52435diff
changeset | 6746 | unfolding list_update_def by transfer_prover | 
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6747 | |
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6748 | lemma takeWhile_transfer [transfer_rule]: | 
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6749 | "((A ===> op =) ===> list_all2 A ===> list_all2 A) takeWhile takeWhile" | 
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6750 | unfolding takeWhile_def by transfer_prover | 
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6751 | |
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6752 | lemma dropWhile_transfer [transfer_rule]: | 
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6753 | "((A ===> op =) ===> list_all2 A ===> list_all2 A) dropWhile dropWhile" | 
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6754 | unfolding dropWhile_def by transfer_prover | 
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6755 | |
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6756 | lemma zip_transfer [transfer_rule]: | 
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6757 | "(list_all2 A ===> list_all2 B ===> list_all2 (prod_rel A B)) zip zip" | 
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6758 | unfolding zip_def by transfer_prover | 
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6759 | |
| 53721 
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
 traytel parents: 
53689diff
changeset | 6760 | lemma product_transfer [transfer_rule]: | 
| 
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
 traytel parents: 
53689diff
changeset | 6761 | "(list_all2 A ===> list_all2 B ===> list_all2 (prod_rel A B)) List.product List.product" | 
| 
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
 traytel parents: 
53689diff
changeset | 6762 | unfolding List.product_def by transfer_prover | 
| 
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
 traytel parents: 
53689diff
changeset | 6763 | |
| 
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
 traytel parents: 
53689diff
changeset | 6764 | lemma product_lists_transfer [transfer_rule]: | 
| 
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
 traytel parents: 
53689diff
changeset | 6765 | "(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: 
53689diff
changeset | 6766 | unfolding product_lists_def by transfer_prover | 
| 
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
 traytel parents: 
53689diff
changeset | 6767 | |
| 53012 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6768 | lemma insert_transfer [transfer_rule]: | 
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6769 | assumes [transfer_rule]: "bi_unique A" | 
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6770 | 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: 
52435diff
changeset | 6771 | unfolding List.insert_def [abs_def] by transfer_prover | 
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6772 | |
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6773 | lemma find_transfer [transfer_rule]: | 
| 55525 | 6774 | "((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: 
52435diff
changeset | 6775 | unfolding List.find_def by transfer_prover | 
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6776 | |
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6777 | lemma remove1_transfer [transfer_rule]: | 
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6778 | assumes [transfer_rule]: "bi_unique A" | 
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6779 | shows "(A ===> list_all2 A ===> list_all2 A) remove1 remove1" | 
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6780 | unfolding remove1_def by transfer_prover | 
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6781 | |
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6782 | lemma removeAll_transfer [transfer_rule]: | 
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6783 | assumes [transfer_rule]: "bi_unique A" | 
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6784 | shows "(A ===> list_all2 A ===> list_all2 A) removeAll removeAll" | 
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6785 | unfolding removeAll_def by transfer_prover | 
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6786 | |
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6787 | lemma distinct_transfer [transfer_rule]: | 
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6788 | assumes [transfer_rule]: "bi_unique A" | 
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6789 | shows "(list_all2 A ===> op =) distinct distinct" | 
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6790 | unfolding distinct_def by transfer_prover | 
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6791 | |
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6792 | lemma remdups_transfer [transfer_rule]: | 
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6793 | assumes [transfer_rule]: "bi_unique A" | 
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6794 | shows "(list_all2 A ===> list_all2 A) remdups remdups" | 
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6795 | unfolding remdups_def by transfer_prover | 
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6796 | |
| 53721 
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
 traytel parents: 
53689diff
changeset | 6797 | lemma remdups_adj_transfer [transfer_rule]: | 
| 
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
 traytel parents: 
53689diff
changeset | 6798 | assumes [transfer_rule]: "bi_unique A" | 
| 
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
 traytel parents: 
53689diff
changeset | 6799 | shows "(list_all2 A ===> list_all2 A) remdups_adj remdups_adj" | 
| 
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
 traytel parents: 
53689diff
changeset | 6800 | proof (rule fun_relI, erule list_all2_induct) | 
| 
ccaceea6c768
added two functions to List (one contributed by Manuel Eberl)
 traytel parents: 
53689diff
changeset | 6801 | 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: 
53689diff
changeset | 6802 | |
| 53012 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6803 | lemma replicate_transfer [transfer_rule]: | 
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6804 | "(op = ===> A ===> list_all2 A) replicate replicate" | 
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6805 | unfolding replicate_def by transfer_prover | 
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6806 | |
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6807 | lemma length_transfer [transfer_rule]: | 
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6808 | "(list_all2 A ===> op =) length length" | 
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6809 | unfolding list_size_overloaded_def by transfer_prover | 
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6810 | |
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6811 | lemma rotate1_transfer [transfer_rule]: | 
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6812 | "(list_all2 A ===> list_all2 A) rotate1 rotate1" | 
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6813 | unfolding rotate1_def by transfer_prover | 
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6814 | |
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6815 | lemma rotate_transfer [transfer_rule]: | 
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6816 | "(op = ===> list_all2 A ===> list_all2 A) rotate rotate" | 
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6817 | unfolding rotate_def [abs_def] by transfer_prover | 
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6818 | |
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6819 | lemma list_all2_transfer [transfer_rule]: | 
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6820 | "((A ===> B ===> op =) ===> list_all2 A ===> list_all2 B ===> op =) | 
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6821 | list_all2 list_all2" | 
| 55524 
f41ef840f09d
folded 'list_all2' with the relator generated by 'datatype_new'
 blanchet parents: 
55473diff
changeset | 6822 | apply (subst (4) list_all2_iff [abs_def]) | 
| 
f41ef840f09d
folded 'list_all2' with the relator generated by 'datatype_new'
 blanchet parents: 
55473diff
changeset | 6823 | apply (subst (3) list_all2_iff [abs_def]) | 
| 53012 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6824 | apply transfer_prover | 
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6825 | done | 
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6826 | |
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6827 | lemma sublist_transfer [transfer_rule]: | 
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6828 | "(list_all2 A ===> set_rel (op =) ===> list_all2 A) sublist sublist" | 
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6829 | unfolding sublist_def [abs_def] by transfer_prover | 
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6830 | |
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6831 | lemma partition_transfer [transfer_rule]: | 
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6832 | "((A ===> op =) ===> list_all2 A ===> prod_rel (list_all2 A) (list_all2 A)) | 
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6833 | partition partition" | 
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6834 | unfolding partition_def by transfer_prover | 
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6835 | |
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6836 | lemma lists_transfer [transfer_rule]: | 
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6837 | "(set_rel A ===> set_rel (list_all2 A)) lists lists" | 
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6838 | apply (rule fun_relI, rule set_relI) | 
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6839 | apply (erule lists.induct, simp) | 
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6840 | apply (simp only: set_rel_def list_all2_Cons1, metis lists.Cons) | 
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6841 | apply (erule lists.induct, simp) | 
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6842 | apply (simp only: set_rel_def list_all2_Cons2, metis lists.Cons) | 
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6843 | done | 
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6844 | |
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6845 | lemma set_Cons_transfer [transfer_rule]: | 
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6846 | "(set_rel A ===> set_rel (list_all2 A) ===> set_rel (list_all2 A)) | 
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6847 | set_Cons set_Cons" | 
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6848 | unfolding fun_rel_def set_rel_def set_Cons_def | 
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6849 | apply safe | 
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6850 | apply (simp add: list_all2_Cons1, fast) | 
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6851 | apply (simp add: list_all2_Cons2, fast) | 
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6852 | done | 
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6853 | |
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6854 | lemma listset_transfer [transfer_rule]: | 
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6855 | "(list_all2 (set_rel A) ===> set_rel (list_all2 A)) listset listset" | 
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6856 | unfolding listset_def by transfer_prover | 
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6857 | |
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6858 | lemma null_transfer [transfer_rule]: | 
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6859 | "(list_all2 A ===> op =) List.null List.null" | 
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6860 | unfolding fun_rel_def List.null_def by auto | 
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6861 | |
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6862 | lemma list_all_transfer [transfer_rule]: | 
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6863 | "((A ===> op =) ===> list_all2 A ===> op =) list_all list_all" | 
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6864 | unfolding list_all_iff [abs_def] by transfer_prover | 
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6865 | |
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6866 | lemma list_ex_transfer [transfer_rule]: | 
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6867 | "((A ===> op =) ===> list_all2 A ===> op =) list_ex list_ex" | 
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6868 | unfolding list_ex_iff [abs_def] by transfer_prover | 
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6869 | |
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6870 | lemma splice_transfer [transfer_rule]: | 
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6871 | "(list_all2 A ===> list_all2 A ===> list_all2 A) splice splice" | 
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6872 | apply (rule fun_relI, erule list_all2_induct, simp add: fun_rel_def, simp) | 
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6873 | apply (rule fun_relI) | 
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6874 | apply (erule_tac xs=x in list_all2_induct, simp, simp add: fun_rel_def) | 
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6875 | done | 
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6876 | |
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6877 | lemma listsum_transfer[transfer_rule]: | 
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6878 | assumes [transfer_rule]: "A 0 0" | 
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6879 | assumes [transfer_rule]: "(A ===> A ===> A) op + op +" | 
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6880 | shows "(list_all2 A ===> A) listsum listsum" | 
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6881 | unfolding listsum_def[abs_def] | 
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6882 | by transfer_prover | 
| 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6883 | |
| 23388 | 6884 | end | 
| 47397 
d654c73e4b12
no preference wrt. fold(l/r); prefer fold rather than foldr for iterating over lists in generated code
 haftmann parents: 
47131diff
changeset | 6885 | |
| 53012 
cb82606b8215
move Lifting/Transfer relevant parts of Library/Quotient_* to Main
 kuncar parents: 
52435diff
changeset | 6886 | end |