| author | kuncar | 
| Mon, 26 Mar 2012 15:32:54 +0200 | |
| changeset 47116 | 529d2a949bd4 | 
| parent 46898 | 1570b30ee040 | 
| child 47108 | 2a1953f0d20d | 
| 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 | 
| 44013 
5cfc1c36ae97
moved recdef package to HOL/Library/Old_Recdef.thy
 krauss parents: 
43594diff
changeset | 8 | imports Plain Presburger Code_Numeral Quotient ATP | 
| 41463 
edbf0a86fb1c
adding simproc to rewrite list comprehensions to set comprehensions; adopting proofs
 bulwahn parents: 
41372diff
changeset | 9 | uses | 
| 
edbf0a86fb1c
adding simproc to rewrite list comprehensions to set comprehensions; adopting proofs
 bulwahn parents: 
41372diff
changeset | 10 |   ("Tools/list_code.ML")
 | 
| 
edbf0a86fb1c
adding simproc to rewrite list comprehensions to set comprehensions; adopting proofs
 bulwahn parents: 
41372diff
changeset | 11 |   ("Tools/list_to_set_comprehension.ML")
 | 
| 15131 | 12 | begin | 
| 923 | 13 | |
| 13142 | 14 | datatype 'a list = | 
| 13366 | 15 |     Nil    ("[]")
 | 
| 16 | | Cons 'a "'a list" (infixr "#" 65) | |
| 923 | 17 | |
| 34941 | 18 | syntax | 
| 19 |   -- {* list Enumeration *}
 | |
| 35115 | 20 |   "_list" :: "args => 'a list"    ("[(_)]")
 | 
| 34941 | 21 | |
| 22 | translations | |
| 23 | "[x, xs]" == "x#[xs]" | |
| 24 | "[x]" == "x#[]" | |
| 25 | ||
| 35115 | 26 | |
| 27 | subsection {* Basic list processing functions *}
 | |
| 15302 | 28 | |
| 34941 | 29 | primrec | 
| 30 | hd :: "'a list \<Rightarrow> 'a" where | |
| 31 | "hd (x # xs) = x" | |
| 32 | ||
| 33 | primrec | |
| 34 | tl :: "'a list \<Rightarrow> 'a list" where | |
| 35 | "tl [] = []" | |
| 36 | | "tl (x # xs) = xs" | |
| 37 | ||
| 38 | primrec | |
| 39 | last :: "'a list \<Rightarrow> 'a" where | |
| 40 | "last (x # xs) = (if xs = [] then x else last xs)" | |
| 41 | ||
| 42 | primrec | |
| 43 | butlast :: "'a list \<Rightarrow> 'a list" where | |
| 44 | "butlast []= []" | |
| 45 | | "butlast (x # xs) = (if xs = [] then [] else x # butlast xs)" | |
| 46 | ||
| 47 | primrec | |
| 48 | set :: "'a list \<Rightarrow> 'a set" where | |
| 49 |     "set [] = {}"
 | |
| 50 | | "set (x # xs) = insert x (set xs)" | |
| 51 | ||
| 46133 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 haftmann parents: 
46125diff
changeset | 52 | definition | 
| 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 haftmann parents: 
46125diff
changeset | 53 | coset :: "'a list \<Rightarrow> 'a set" where | 
| 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 haftmann parents: 
46125diff
changeset | 54 | [simp]: "coset xs = - 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 | 55 | |
| 34941 | 56 | primrec | 
| 57 |   map :: "('a \<Rightarrow> 'b) \<Rightarrow> 'a list \<Rightarrow> 'b list" where
 | |
| 58 | "map f [] = []" | |
| 59 | | "map f (x # xs) = f x # map f xs" | |
| 60 | ||
| 61 | primrec | |
| 62 | append :: "'a list \<Rightarrow> 'a list \<Rightarrow> 'a list" (infixr "@" 65) where | |
| 63 | append_Nil:"[] @ ys = ys" | |
| 64 | | append_Cons: "(x#xs) @ ys = x # xs @ ys" | |
| 65 | ||
| 66 | primrec | |
| 67 | rev :: "'a list \<Rightarrow> 'a list" where | |
| 68 | "rev [] = []" | |
| 69 | | "rev (x # xs) = rev xs @ [x]" | |
| 70 | ||
| 71 | primrec | |
| 72 |   filter:: "('a \<Rightarrow> bool) \<Rightarrow> 'a list \<Rightarrow> 'a list" where
 | |
| 73 | "filter P [] = []" | |
| 74 | | "filter P (x # xs) = (if P x then x # filter P xs else filter P xs)" | |
| 75 | ||
| 76 | syntax | |
| 77 |   -- {* Special syntax for filter *}
 | |
| 35115 | 78 |   "_filter" :: "[pttrn, 'a list, bool] => 'a list"    ("(1[_<-_./ _])")
 | 
| 34941 | 79 | |
| 80 | translations | |
| 81 | "[x<-xs . P]"== "CONST filter (%x. P) xs" | |
| 82 | ||
| 83 | syntax (xsymbols) | |
| 35115 | 84 |   "_filter" :: "[pttrn, 'a list, bool] => 'a list"("(1[_\<leftarrow>_ ./ _])")
 | 
| 34941 | 85 | syntax (HTML output) | 
| 35115 | 86 |   "_filter" :: "[pttrn, 'a list, bool] => 'a list"("(1[_\<leftarrow>_ ./ _])")
 | 
| 34941 | 87 | |
| 46133 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 haftmann parents: 
46125diff
changeset | 88 | primrec -- {* canonical argument order *}
 | 
| 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 haftmann parents: 
46125diff
changeset | 89 |   fold :: "('a \<Rightarrow> 'b \<Rightarrow> 'b) \<Rightarrow> 'a list \<Rightarrow> 'b \<Rightarrow> 'b" where
 | 
| 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 haftmann parents: 
46125diff
changeset | 90 | "fold f [] = id" | 
| 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 haftmann parents: 
46125diff
changeset | 91 | | "fold f (x # xs) = fold f xs \<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 | 92 | |
| 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 haftmann parents: 
46125diff
changeset | 93 | definition | 
| 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 haftmann parents: 
46125diff
changeset | 94 |   foldr :: "('a \<Rightarrow> 'b \<Rightarrow> 'b) \<Rightarrow> 'a list \<Rightarrow> 'b \<Rightarrow> 'b" where
 | 
| 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 haftmann parents: 
46125diff
changeset | 95 | [code_abbrev]: "foldr f xs = fold f (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 | 96 | |
| 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 haftmann parents: 
46125diff
changeset | 97 | definition | 
| 34941 | 98 |   foldl :: "('b \<Rightarrow> 'a \<Rightarrow> 'b) \<Rightarrow> 'b \<Rightarrow> 'a list \<Rightarrow> 'b" where
 | 
| 46133 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 haftmann parents: 
46125diff
changeset | 99 | "foldl f s xs = fold (\<lambda>x s. f s x) xs s" | 
| 34941 | 100 | |
| 101 | primrec | |
| 102 | concat:: "'a list list \<Rightarrow> 'a list" where | |
| 103 | "concat [] = []" | |
| 104 | | "concat (x # xs) = x @ concat xs" | |
| 105 | ||
| 39774 | 106 | definition (in monoid_add) | 
| 34941 | 107 | listsum :: "'a list \<Rightarrow> 'a" where | 
| 39774 | 108 | "listsum xs = foldr plus xs 0" | 
| 34941 | 109 | |
| 110 | primrec | |
| 111 | drop:: "nat \<Rightarrow> 'a list \<Rightarrow> 'a list" where | |
| 112 | drop_Nil: "drop n [] = []" | |
| 113 | | drop_Cons: "drop n (x # xs) = (case n of 0 \<Rightarrow> x # xs | Suc m \<Rightarrow> drop m xs)" | |
| 114 |   -- {*Warning: simpset does not contain this definition, but separate
 | |
| 115 |        theorems for @{text "n = 0"} and @{text "n = Suc k"} *}
 | |
| 116 | ||
| 117 | primrec | |
| 118 | take:: "nat \<Rightarrow> 'a list \<Rightarrow> 'a list" where | |
| 119 | take_Nil:"take n [] = []" | |
| 120 | | take_Cons: "take n (x # xs) = (case n of 0 \<Rightarrow> [] | Suc m \<Rightarrow> x # take m xs)" | |
| 121 |   -- {*Warning: simpset does not contain this definition, but separate
 | |
| 122 |        theorems for @{text "n = 0"} and @{text "n = Suc k"} *}
 | |
| 123 | ||
| 124 | primrec | |
| 125 | nth :: "'a list => nat => 'a" (infixl "!" 100) where | |
| 126 | nth_Cons: "(x # xs) ! n = (case n of 0 \<Rightarrow> x | Suc k \<Rightarrow> xs ! k)" | |
| 127 |   -- {*Warning: simpset does not contain this definition, but separate
 | |
| 128 |        theorems for @{text "n = 0"} and @{text "n = Suc k"} *}
 | |
| 129 | ||
| 130 | primrec | |
| 131 | list_update :: "'a list \<Rightarrow> nat \<Rightarrow> 'a \<Rightarrow> 'a list" where | |
| 132 | "list_update [] i v = []" | |
| 133 | | "list_update (x # xs) i v = (case i of 0 \<Rightarrow> v # xs | Suc j \<Rightarrow> x # list_update xs j v)" | |
| 923 | 134 | |
| 41229 
d797baa3d57c
replaced command 'nonterminals' by slightly modernized version 'nonterminal';
 wenzelm parents: 
41075diff
changeset | 135 | 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 | 136 | |
| 923 | 137 | syntax | 
| 13366 | 138 |   "_lupdbind":: "['a, 'a] => lupdbind"    ("(2_ :=/ _)")
 | 
| 139 |   "" :: "lupdbind => lupdbinds"    ("_")
 | |
| 140 |   "_lupdbinds" :: "[lupdbind, lupdbinds] => lupdbinds"    ("_,/ _")
 | |
| 141 |   "_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 | 142 | |
| 923 | 143 | translations | 
| 35115 | 144 | "_LUpdate xs (_lupdbinds b bs)" == "_LUpdate (_LUpdate xs b) bs" | 
| 34941 | 145 | "xs[i:=x]" == "CONST list_update xs i x" | 
| 146 | ||
| 147 | primrec | |
| 148 |   takeWhile :: "('a \<Rightarrow> bool) \<Rightarrow> 'a list \<Rightarrow> 'a list" where
 | |
| 149 | "takeWhile P [] = []" | |
| 150 | | "takeWhile P (x # xs) = (if P x then x # takeWhile P xs else [])" | |
| 151 | ||
| 152 | primrec | |
| 153 |   dropWhile :: "('a \<Rightarrow> bool) \<Rightarrow> 'a list \<Rightarrow> 'a list" where
 | |
| 154 | "dropWhile P [] = []" | |
| 155 | | "dropWhile P (x # xs) = (if P x then dropWhile P xs else x # xs)" | |
| 156 | ||
| 157 | primrec | |
| 158 |   zip :: "'a list \<Rightarrow> 'b list \<Rightarrow> ('a \<times> 'b) list" where
 | |
| 159 | "zip xs [] = []" | |
| 160 | | zip_Cons: "zip xs (y # ys) = (case xs of [] => [] | z # zs => (z, y) # zip zs ys)" | |
| 161 |   -- {*Warning: simpset does not contain this definition, but separate
 | |
| 162 |        theorems for @{text "xs = []"} and @{text "xs = z # zs"} *}
 | |
| 163 | ||
| 164 | primrec | |
| 165 |   upt :: "nat \<Rightarrow> nat \<Rightarrow> nat list" ("(1[_..</_'])") where
 | |
| 166 | upt_0: "[i..<0] = []" | |
| 167 | | upt_Suc: "[i..<(Suc j)] = (if i <= j then [i..<j] @ [j] else [])" | |
| 168 | ||
| 34978 
874150ddd50a
canonical insert operation; generalized lemma foldl_apply_inv to foldl_apply
 haftmann parents: 
34942diff
changeset | 169 | definition | 
| 
874150ddd50a
canonical insert operation; generalized lemma foldl_apply_inv to foldl_apply
 haftmann parents: 
34942diff
changeset | 170 | insert :: "'a \<Rightarrow> 'a list \<Rightarrow> 'a list" where | 
| 
874150ddd50a
canonical insert operation; generalized lemma foldl_apply_inv to foldl_apply
 haftmann parents: 
34942diff
changeset | 171 | "insert x xs = (if x \<in> set xs then xs else x # xs)" | 
| 
874150ddd50a
canonical insert operation; generalized lemma foldl_apply_inv to foldl_apply
 haftmann parents: 
34942diff
changeset | 172 | |
| 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 | 173 | 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 | 174 | hide_fact (open) insert_def | 
| 34978 
874150ddd50a
canonical insert operation; generalized lemma foldl_apply_inv to foldl_apply
 haftmann parents: 
34942diff
changeset | 175 | |
| 34941 | 176 | primrec | 
| 177 | remove1 :: "'a \<Rightarrow> 'a list \<Rightarrow> 'a list" where | |
| 178 | "remove1 x [] = []" | |
| 179 | | "remove1 x (y # xs) = (if x = y then xs else y # remove1 x xs)" | |
| 180 | ||
| 181 | primrec | |
| 182 | removeAll :: "'a \<Rightarrow> 'a list \<Rightarrow> 'a list" where | |
| 183 | "removeAll x [] = []" | |
| 184 | | "removeAll x (y # xs) = (if x = y then removeAll x xs else y # removeAll x xs)" | |
| 185 | ||
| 40122 
1d8ad2ff3e01
dropped (almost) redundant distinct.induct rule; distinct_simps again named distinct.simps
 haftmann parents: 
40077diff
changeset | 186 | primrec | 
| 39915 
ecf97cf3d248
turned distinct and sorted into inductive predicates: yields nice induction principles for free; more elegant proofs
 haftmann parents: 
39774diff
changeset | 187 | distinct :: "'a list \<Rightarrow> bool" where | 
| 40122 
1d8ad2ff3e01
dropped (almost) redundant distinct.induct rule; distinct_simps again named distinct.simps
 haftmann parents: 
40077diff
changeset | 188 | "distinct [] \<longleftrightarrow> True" | 
| 
1d8ad2ff3e01
dropped (almost) redundant distinct.induct rule; distinct_simps again named distinct.simps
 haftmann parents: 
40077diff
changeset | 189 | | "distinct (x # xs) \<longleftrightarrow> x \<notin> set xs \<and> distinct xs" | 
| 39915 
ecf97cf3d248
turned distinct and sorted into inductive predicates: yields nice induction principles for free; more elegant proofs
 haftmann parents: 
39774diff
changeset | 190 | |
| 
ecf97cf3d248
turned distinct and sorted into inductive predicates: yields nice induction principles for free; more elegant proofs
 haftmann parents: 
39774diff
changeset | 191 | primrec | 
| 
ecf97cf3d248
turned distinct and sorted into inductive predicates: yields nice induction principles for free; more elegant proofs
 haftmann parents: 
39774diff
changeset | 192 | remdups :: "'a list \<Rightarrow> 'a list" where | 
| 
ecf97cf3d248
turned distinct and sorted into inductive predicates: yields nice induction principles for free; more elegant proofs
 haftmann parents: 
39774diff
changeset | 193 | "remdups [] = []" | 
| 
ecf97cf3d248
turned distinct and sorted into inductive predicates: yields nice induction principles for free; more elegant proofs
 haftmann parents: 
39774diff
changeset | 194 | | "remdups (x # xs) = (if x \<in> set xs then remdups xs else x # remdups xs)" | 
| 
ecf97cf3d248
turned distinct and sorted into inductive predicates: yields nice induction principles for free; more elegant proofs
 haftmann parents: 
39774diff
changeset | 195 | |
| 34941 | 196 | primrec | 
| 197 | replicate :: "nat \<Rightarrow> 'a \<Rightarrow> 'a list" where | |
| 198 | replicate_0: "replicate 0 x = []" | |
| 199 | | replicate_Suc: "replicate (Suc n) x = x # replicate n x" | |
| 3342 
ec3b55fcb165
New operator "lists" for formalizing sets of lists
 paulson parents: 
3320diff
changeset | 200 | |
| 13142 | 201 | text {*
 | 
| 14589 | 202 |   Function @{text size} is overloaded for all datatypes. Users may
 | 
| 13366 | 203 |   refer to the list version as @{text length}. *}
 | 
| 13142 | 204 | |
| 19363 | 205 | abbreviation | 
| 34941 | 206 | length :: "'a list \<Rightarrow> nat" where | 
| 207 | "length \<equiv> size" | |
| 15307 | 208 | |
| 46440 
d4994e2e7364
use 'primrec' to define "rotate1", for uniformity (and to help first-order tools that rely on "Spec_Rules")
 blanchet parents: 
46439diff
changeset | 209 | primrec rotate1 :: "'a list \<Rightarrow> 'a list" where | 
| 
d4994e2e7364
use 'primrec' to define "rotate1", for uniformity (and to help first-order tools that rely on "Spec_Rules")
 blanchet parents: 
46439diff
changeset | 210 | "rotate1 [] = []" | | 
| 
d4994e2e7364
use 'primrec' to define "rotate1", for uniformity (and to help first-order tools that rely on "Spec_Rules")
 blanchet parents: 
46439diff
changeset | 211 | "rotate1 (x # xs) = xs @ [x]" | 
| 21404 
eb85850d3eb7
more robust syntax for definition/abbreviation/notation;
 wenzelm parents: 
21211diff
changeset | 212 | |
| 
eb85850d3eb7
more robust syntax for definition/abbreviation/notation;
 wenzelm parents: 
21211diff
changeset | 213 | definition | 
| 
eb85850d3eb7
more robust syntax for definition/abbreviation/notation;
 wenzelm parents: 
21211diff
changeset | 214 | rotate :: "nat \<Rightarrow> 'a list \<Rightarrow> 'a list" where | 
| 30971 | 215 | "rotate n = rotate1 ^^ n" | 
| 21404 
eb85850d3eb7
more robust syntax for definition/abbreviation/notation;
 wenzelm parents: 
21211diff
changeset | 216 | |
| 
eb85850d3eb7
more robust syntax for definition/abbreviation/notation;
 wenzelm parents: 
21211diff
changeset | 217 | definition | 
| 
eb85850d3eb7
more robust syntax for definition/abbreviation/notation;
 wenzelm parents: 
21211diff
changeset | 218 |   list_all2 :: "('a => 'b => bool) => 'a list => 'b list => bool" where
 | 
| 37767 | 219 | "list_all2 P xs ys = | 
| 21061 
580dfc999ef6
added normal post setup; cleaned up "execution" constants
 haftmann parents: 
21046diff
changeset | 220 | (length xs = length ys \<and> (\<forall>(x, y) \<in> set (zip xs ys). P x y))" | 
| 21404 
eb85850d3eb7
more robust syntax for definition/abbreviation/notation;
 wenzelm parents: 
21211diff
changeset | 221 | |
| 
eb85850d3eb7
more robust syntax for definition/abbreviation/notation;
 wenzelm parents: 
21211diff
changeset | 222 | definition | 
| 
eb85850d3eb7
more robust syntax for definition/abbreviation/notation;
 wenzelm parents: 
21211diff
changeset | 223 | sublist :: "'a list => nat set => 'a list" where | 
| 
eb85850d3eb7
more robust syntax for definition/abbreviation/notation;
 wenzelm parents: 
21211diff
changeset | 224 | "sublist xs A = map fst (filter (\<lambda>p. snd p \<in> A) (zip xs [0..<size xs]))" | 
| 17086 | 225 | |
| 40593 
1e57b18d27b1
code eqn for slice was missing; redefined splice with fun
 nipkow parents: 
40365diff
changeset | 226 | 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 | 227 | "splice [] ys = ys" | | 
| 
1e57b18d27b1
code eqn for slice was missing; redefined splice with fun
 nipkow parents: 
40365diff
changeset | 228 | "splice xs [] = xs" | | 
| 
1e57b18d27b1
code eqn for slice was missing; redefined splice with fun
 nipkow parents: 
40365diff
changeset | 229 | "splice (x#xs) (y#ys) = x # y # splice xs ys" | 
| 21061 
580dfc999ef6
added normal post setup; cleaned up "execution" constants
 haftmann parents: 
21046diff
changeset | 230 | |
| 26771 | 231 | text{*
 | 
| 232 | \begin{figure}[htbp]
 | |
| 233 | \fbox{
 | |
| 234 | \begin{tabular}{l}
 | |
| 27381 | 235 | @{lemma "[a,b]@[c,d] = [a,b,c,d]" by simp}\\
 | 
| 236 | @{lemma "length [a,b,c] = 3" by simp}\\
 | |
| 237 | @{lemma "set [a,b,c] = {a,b,c}" by simp}\\
 | |
| 238 | @{lemma "map f [a,b,c] = [f a, f b, f c]" by simp}\\
 | |
| 239 | @{lemma "rev [a,b,c] = [c,b,a]" by simp}\\
 | |
| 240 | @{lemma "hd [a,b,c,d] = a" by simp}\\
 | |
| 241 | @{lemma "tl [a,b,c,d] = [b,c,d]" by simp}\\
 | |
| 242 | @{lemma "last [a,b,c,d] = d" by simp}\\
 | |
| 243 | @{lemma "butlast [a,b,c,d] = [a,b,c]" by simp}\\
 | |
| 244 | @{lemma[source] "filter (\<lambda>n::nat. n<2) [0,2,1] = [0,1]" by simp}\\
 | |
| 245 | @{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 | 246 | @{lemma "fold f [a,b,c] x = f c (f b (f a 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 | 247 | @{lemma "foldr f [a,b,c] x = f a (f b (f c x))" by (simp add: foldr_def)}\\
 | 
| 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 haftmann parents: 
46125diff
changeset | 248 | @{lemma "foldl f x [a,b,c] = f (f (f x a) b) c" by (simp add: foldl_def)}\\
 | 
| 27381 | 249 | @{lemma "zip [a,b,c] [x,y,z] = [(a,x),(b,y),(c,z)]" by simp}\\
 | 
| 250 | @{lemma "zip [a,b] [x,y,z] = [(a,x),(b,y)]" by simp}\\
 | |
| 251 | @{lemma "splice [a,b,c] [x,y,z] = [a,x,b,y,c,z]" by simp}\\
 | |
| 252 | @{lemma "splice [a,b,c,d] [x,y] = [a,x,b,y,c,d]" by simp}\\
 | |
| 253 | @{lemma "take 2 [a,b,c,d] = [a,b]" by simp}\\
 | |
| 254 | @{lemma "take 6 [a,b,c,d] = [a,b,c,d]" by simp}\\
 | |
| 255 | @{lemma "drop 2 [a,b,c,d] = [c,d]" by simp}\\
 | |
| 256 | @{lemma "drop 6 [a,b,c,d] = []" by simp}\\
 | |
| 257 | @{lemma "takeWhile (%n::nat. n<3) [1,2,3,0] = [1,2]" by simp}\\
 | |
| 258 | @{lemma "dropWhile (%n::nat. n<3) [1,2,3,0] = [3,0]" by simp}\\
 | |
| 259 | @{lemma "distinct [2,0,1::nat]" by simp}\\
 | |
| 260 | @{lemma "remdups [2,0,2,1::nat,2] = [0,1,2]" by simp}\\
 | |
| 34978 
874150ddd50a
canonical insert operation; generalized lemma foldl_apply_inv to foldl_apply
 haftmann parents: 
34942diff
changeset | 261 | @{lemma "List.insert 2 [0::nat,1,2] = [0,1,2]" by (simp add: List.insert_def)}\\
 | 
| 35295 | 262 | @{lemma "List.insert 3 [0::nat,1,2] = [3,0,1,2]" by (simp add: List.insert_def)}\\
 | 
| 27381 | 263 | @{lemma "remove1 2 [2,0,2,1::nat,2] = [0,2,1,2]" by simp}\\
 | 
| 27693 | 264 | @{lemma "removeAll 2 [2,0,2,1::nat,2] = [0,1]" by simp}\\
 | 
| 27381 | 265 | @{lemma "nth [a,b,c,d] 2 = c" by simp}\\
 | 
| 266 | @{lemma "[a,b,c,d][2 := x] = [a,b,x,d]" by simp}\\
 | |
| 267 | @{lemma "sublist [a,b,c,d,e] {0,2,3} = [a,c,d]" by (simp add:sublist_def)}\\
 | |
| 46440 
d4994e2e7364
use 'primrec' to define "rotate1", for uniformity (and to help first-order tools that rely on "Spec_Rules")
 blanchet parents: 
46439diff
changeset | 268 | @{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 | 269 | @{lemma "rotate 3 [a,b,c,d] = [d,a,b,c]" by (simp add:rotate_def eval_nat_numeral)}\\
 | 
| 40077 | 270 | @{lemma "replicate 4 a = [a,a,a,a]" by (simp add:eval_nat_numeral)}\\
 | 
| 271 | @{lemma "[2..<5] = [2,3,4]" by (simp add:eval_nat_numeral)}\\
 | |
| 46133 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 haftmann parents: 
46125diff
changeset | 272 | @{lemma "listsum [1,2,3::nat] = 6" by (simp add: listsum_def foldr_def)}
 | 
| 26771 | 273 | \end{tabular}}
 | 
| 274 | \caption{Characteristic examples}
 | |
| 275 | \label{fig:Characteristic}
 | |
| 276 | \end{figure}
 | |
| 29927 | 277 | Figure~\ref{fig:Characteristic} shows characteristic examples
 | 
| 26771 | 278 | that should give an intuitive understanding of the above functions. | 
| 279 | *} | |
| 280 | ||
| 24616 | 281 | text{* The following simple sort functions are intended for proofs,
 | 
| 282 | not for efficient implementations. *} | |
| 283 | ||
| 25221 
5ded95dda5df
append/member: more light-weight way to declare authentic syntax;
 wenzelm parents: 
25215diff
changeset | 284 | context linorder | 
| 
5ded95dda5df
append/member: more light-weight way to declare authentic syntax;
 wenzelm parents: 
25215diff
changeset | 285 | begin | 
| 
5ded95dda5df
append/member: more light-weight way to declare authentic syntax;
 wenzelm parents: 
25215diff
changeset | 286 | |
| 39915 
ecf97cf3d248
turned distinct and sorted into inductive predicates: yields nice induction principles for free; more elegant proofs
 haftmann parents: 
39774diff
changeset | 287 | 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 | 288 | Nil [iff]: "sorted []" | 
| 
ecf97cf3d248
turned distinct and sorted into inductive predicates: yields nice induction principles for free; more elegant proofs
 haftmann parents: 
39774diff
changeset | 289 | | 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 | 290 | |
| 
ecf97cf3d248
turned distinct and sorted into inductive predicates: yields nice induction principles for free; more elegant proofs
 haftmann parents: 
39774diff
changeset | 291 | 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 | 292 | "sorted [x]" | 
| 
ecf97cf3d248
turned distinct and sorted into inductive predicates: yields nice induction principles for free; more elegant proofs
 haftmann parents: 
39774diff
changeset | 293 | 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 | 294 | |
| 
ecf97cf3d248
turned distinct and sorted into inductive predicates: yields nice induction principles for free; more elegant proofs
 haftmann parents: 
39774diff
changeset | 295 | lemma sorted_many: | 
| 
ecf97cf3d248
turned distinct and sorted into inductive predicates: yields nice induction principles for free; more elegant proofs
 haftmann parents: 
39774diff
changeset | 296 | "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 | 297 | 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 | 298 | |
| 
ecf97cf3d248
turned distinct and sorted into inductive predicates: yields nice induction principles for free; more elegant proofs
 haftmann parents: 
39774diff
changeset | 299 | 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 | 300 | "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 | 301 | 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 | 302 | |
| 
ecf97cf3d248
turned distinct and sorted into inductive predicates: yields nice induction principles for free; more elegant proofs
 haftmann parents: 
39774diff
changeset | 303 | lemma [code]: | 
| 
ecf97cf3d248
turned distinct and sorted into inductive predicates: yields nice induction principles for free; more elegant proofs
 haftmann parents: 
39774diff
changeset | 304 | "sorted [] \<longleftrightarrow> True" | 
| 
ecf97cf3d248
turned distinct and sorted into inductive predicates: yields nice induction principles for free; more elegant proofs
 haftmann parents: 
39774diff
changeset | 305 | "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 | 306 | by simp_all | 
| 24697 | 307 | |
| 33639 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 308 | primrec insort_key :: "('b \<Rightarrow> 'a) \<Rightarrow> 'b \<Rightarrow> 'b list \<Rightarrow> 'b list" where
 | 
| 46133 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 haftmann parents: 
46125diff
changeset | 309 | "insort_key f x [] = [x]" | | 
| 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 haftmann parents: 
46125diff
changeset | 310 | "insort_key f x (y#ys) = (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 | 311 | |
| 35195 | 312 | definition sort_key :: "('b \<Rightarrow> 'a) \<Rightarrow> 'b list \<Rightarrow> 'b list" where
 | 
| 46133 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 haftmann parents: 
46125diff
changeset | 313 | "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 | 314 | |
| 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 | 315 | definition insort_insert_key :: "('b \<Rightarrow> 'a) \<Rightarrow> 'b \<Rightarrow> 'b list \<Rightarrow> 'b list" where
 | 
| 
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 | 316 | "insort_insert_key f x xs = (if f x \<in> f ` set xs then xs else 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 | 317 | |
| 33639 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 318 | 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 | 319 | 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 | 320 | abbreviation "insort_insert \<equiv> insort_insert_key (\<lambda>x. x)" | 
| 35608 | 321 | |
| 25221 
5ded95dda5df
append/member: more light-weight way to declare authentic syntax;
 wenzelm parents: 
25215diff
changeset | 322 | end | 
| 
5ded95dda5df
append/member: more light-weight way to declare authentic syntax;
 wenzelm parents: 
25215diff
changeset | 323 | |
| 24616 | 324 | |
| 23388 | 325 | subsubsection {* List comprehension *}
 | 
| 23192 | 326 | |
| 24349 | 327 | text{* Input syntax for Haskell-like list comprehension notation.
 | 
| 328 | Typical example: @{text"[(x,y). x \<leftarrow> xs, y \<leftarrow> ys, x \<noteq> y]"},
 | |
| 329 | the list of all pairs of distinct elements from @{text xs} and @{text ys}.
 | |
| 330 | The syntax is as in Haskell, except that @{text"|"} becomes a dot
 | |
| 331 | (like in Isabelle's set comprehension): @{text"[e. x \<leftarrow> xs, \<dots>]"} rather than
 | |
| 332 | \verb![e| x <- xs, ...]!. | |
| 333 | ||
| 334 | The qualifiers after the dot are | |
| 335 | \begin{description}
 | |
| 336 | \item[generators] @{text"p \<leftarrow> xs"},
 | |
| 24476 
f7ad9fbbeeaa
turned list comprehension translations into ML to optimize base case
 nipkow parents: 
24471diff
changeset | 337 |  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 | 338 | \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 | 339 | %\item[local bindings] @ {text"let x = e"}.
 | 
| 24349 | 340 | \end{description}
 | 
| 23240 | 341 | |
| 24476 
f7ad9fbbeeaa
turned list comprehension translations into ML to optimize base case
 nipkow parents: 
24471diff
changeset | 342 | 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 | 343 | misunderstandings, the translation into desugared form is not reversed | 
| 
f7ad9fbbeeaa
turned list comprehension translations into ML to optimize base case
 nipkow parents: 
24471diff
changeset | 344 | 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 | 345 | optmized to @{term"map (%x. e) xs"}.
 | 
| 23240 | 346 | |
| 24349 | 347 | It is easy to write short list comprehensions which stand for complex | 
| 348 | expressions. During proofs, they may become unreadable (and | |
| 349 | mangled). In such cases it can be advisable to introduce separate | |
| 350 | definitions for the list comprehensions in question. *} | |
| 351 | ||
| 46138 
85f8d8a8c711
improved list comprehension syntax: more careful treatment of position constraints, which enables PIDE markup;
 wenzelm parents: 
46133diff
changeset | 352 | nonterminal lc_qual and lc_quals | 
| 23192 | 353 | |
| 354 | syntax | |
| 46138 
85f8d8a8c711
improved list comprehension syntax: more careful treatment of position constraints, which enables PIDE markup;
 wenzelm parents: 
46133diff
changeset | 355 |   "_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 | 356 |   "_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 | 357 |   "_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 | 358 |   (*"_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 | 359 |   "_lc_end" :: "lc_quals" ("]")
 | 
| 
85f8d8a8c711
improved list comprehension syntax: more careful treatment of position constraints, which enables PIDE markup;
 wenzelm parents: 
46133diff
changeset | 360 |   "_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 | 361 | "_lc_abs" :: "'a => 'b list => 'b list" | 
| 23192 | 362 | |
| 24476 
f7ad9fbbeeaa
turned list comprehension translations into ML to optimize base case
 nipkow parents: 
24471diff
changeset | 363 | (* 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 | 364 | translation of [e. p<-xs] | 
| 23192 | 365 | translations | 
| 46138 
85f8d8a8c711
improved list comprehension syntax: more careful treatment of position constraints, which enables PIDE markup;
 wenzelm parents: 
46133diff
changeset | 366 | "[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 | 367 | "_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 | 368 | => "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 | 369 | "[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 | 370 | "_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 | 371 | => "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 | 372 | "_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 | 373 | => "_Let b (_listcompr e Q Qs)" | 
| 24476 
f7ad9fbbeeaa
turned list comprehension translations into ML to optimize base case
 nipkow parents: 
24471diff
changeset | 374 | *) | 
| 23240 | 375 | |
| 23279 
e39dd93161d9
tuned list comprehension, changed filter syntax from : to <-
 nipkow parents: 
23246diff
changeset | 376 | syntax (xsymbols) | 
| 46138 
85f8d8a8c711
improved list comprehension syntax: more careful treatment of position constraints, which enables PIDE markup;
 wenzelm parents: 
46133diff
changeset | 377 |   "_lc_gen" :: "'a \<Rightarrow> 'a list \<Rightarrow> lc_qual"  ("_ \<leftarrow> _")
 | 
| 23279 
e39dd93161d9
tuned list comprehension, changed filter syntax from : to <-
 nipkow parents: 
23246diff
changeset | 378 | syntax (HTML output) | 
| 46138 
85f8d8a8c711
improved list comprehension syntax: more careful treatment of position constraints, which enables PIDE markup;
 wenzelm parents: 
46133diff
changeset | 379 |   "_lc_gen" :: "'a \<Rightarrow> 'a list \<Rightarrow> lc_qual"  ("_ \<leftarrow> _")
 | 
| 24349 | 380 | |
| 381 | parse_translation (advanced) {*
 | |
| 46138 
85f8d8a8c711
improved list comprehension syntax: more careful treatment of position constraints, which enables PIDE markup;
 wenzelm parents: 
46133diff
changeset | 382 | let | 
| 
85f8d8a8c711
improved list comprehension syntax: more careful treatment of position constraints, which enables PIDE markup;
 wenzelm parents: 
46133diff
changeset | 383 |     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 | 384 |     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 | 385 |     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 | 386 |     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 | 387 |     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 | 388 | |
| 
85f8d8a8c711
improved list comprehension syntax: more careful treatment of position constraints, which enables PIDE markup;
 wenzelm parents: 
46133diff
changeset | 389 | 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 | 390 | |
| 
85f8d8a8c711
improved list comprehension syntax: more careful treatment of position constraints, which enables PIDE markup;
 wenzelm parents: 
46133diff
changeset | 391 | 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 | 392 | let | 
| 
85f8d8a8c711
improved list comprehension syntax: more careful treatment of position constraints, which enables PIDE markup;
 wenzelm parents: 
46133diff
changeset | 393 | (* FIXME proper name context!? *) | 
| 
85f8d8a8c711
improved list comprehension syntax: more careful treatment of position constraints, which enables PIDE markup;
 wenzelm parents: 
46133diff
changeset | 394 | val x = | 
| 
85f8d8a8c711
improved list comprehension syntax: more careful treatment of position constraints, which enables PIDE markup;
 wenzelm parents: 
46133diff
changeset | 395 | 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 | 396 | 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 | 397 |         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 | 398 | val case2 = | 
| 
85f8d8a8c711
improved list comprehension syntax: more careful treatment of position constraints, which enables PIDE markup;
 wenzelm parents: 
46133diff
changeset | 399 |           Syntax.const @{syntax_const "_case1"} $
 | 
| 
85f8d8a8c711
improved list comprehension syntax: more careful treatment of position constraints, which enables PIDE markup;
 wenzelm parents: 
46133diff
changeset | 400 |             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 | 401 |         val cs = Syntax.const @{syntax_const "_case2"} $ case1 $ case2;
 | 
| 
85f8d8a8c711
improved list comprehension syntax: more careful treatment of position constraints, which enables PIDE markup;
 wenzelm parents: 
46133diff
changeset | 402 | in Syntax_Trans.abs_tr [x, Datatype_Case.case_tr false ctxt [x, cs]] end; | 
| 
85f8d8a8c711
improved list comprehension syntax: more careful treatment of position constraints, which enables PIDE markup;
 wenzelm parents: 
46133diff
changeset | 403 | |
| 
85f8d8a8c711
improved list comprehension syntax: more careful treatment of position constraints, which enables PIDE markup;
 wenzelm parents: 
46133diff
changeset | 404 | 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 | 405 | (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 | 406 | Free (s, T) => | 
| 
85f8d8a8c711
improved list comprehension syntax: more careful treatment of position constraints, which enables PIDE markup;
 wenzelm parents: 
46133diff
changeset | 407 | let | 
| 
85f8d8a8c711
improved list comprehension syntax: more careful treatment of position constraints, which enables PIDE markup;
 wenzelm parents: 
46133diff
changeset | 408 | 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 | 409 | 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 | 410 | in | 
| 
85f8d8a8c711
improved list comprehension syntax: more careful treatment of position constraints, which enables PIDE markup;
 wenzelm parents: 
46133diff
changeset | 411 | 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 | 412 | 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 | 413 | 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 | 414 | end | 
| 
85f8d8a8c711
improved list comprehension syntax: more careful treatment of position constraints, which enables PIDE markup;
 wenzelm parents: 
46133diff
changeset | 415 | | _ => (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 | 416 | |
| 
85f8d8a8c711
improved list comprehension syntax: more careful treatment of position constraints, which enables PIDE markup;
 wenzelm parents: 
46133diff
changeset | 417 |     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 | 418 | let | 
| 
85f8d8a8c711
improved list comprehension syntax: more careful treatment of position constraints, which enables PIDE markup;
 wenzelm parents: 
46133diff
changeset | 419 | val res = | 
| 
85f8d8a8c711
improved list comprehension syntax: more careful treatment of position constraints, which enables PIDE markup;
 wenzelm parents: 
46133diff
changeset | 420 | (case qs of | 
| 
85f8d8a8c711
improved list comprehension syntax: more careful treatment of position constraints, which enables PIDE markup;
 wenzelm parents: 
46133diff
changeset | 421 |                 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 | 422 |               | 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 | 423 | 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 | 424 | | lc_tr ctxt | 
| 
85f8d8a8c711
improved list comprehension syntax: more careful treatment of position constraints, which enables PIDE markup;
 wenzelm parents: 
46133diff
changeset | 425 |             [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 | 426 |               Const(@{syntax_const "_lc_end"}, _)] =
 | 
| 
85f8d8a8c711
improved list comprehension syntax: more careful treatment of position constraints, which enables PIDE markup;
 wenzelm parents: 
46133diff
changeset | 427 | (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 | 428 | (f, true) => mapC $ f $ es | 
| 
85f8d8a8c711
improved list comprehension syntax: more careful treatment of position constraints, which enables PIDE markup;
 wenzelm parents: 
46133diff
changeset | 429 | | (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 | 430 | | lc_tr ctxt | 
| 
85f8d8a8c711
improved list comprehension syntax: more careful treatment of position constraints, which enables PIDE markup;
 wenzelm parents: 
46133diff
changeset | 431 |             [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 | 432 |               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 | 433 | 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 | 434 | 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 | 435 | |
| 
85f8d8a8c711
improved list comprehension syntax: more careful treatment of position constraints, which enables PIDE markup;
 wenzelm parents: 
46133diff
changeset | 436 |   in [(@{syntax_const "_listcompr"}, lc_tr)] end
 | 
| 24349 | 437 | *} | 
| 23279 
e39dd93161d9
tuned list comprehension, changed filter syntax from : to <-
 nipkow parents: 
23246diff
changeset | 438 | |
| 42167 | 439 | ML {*
 | 
| 440 | let | |
| 441 |     val read = Syntax.read_term @{context};
 | |
| 442 |     fun check s1 s2 = read s1 aconv read s2 orelse error ("Check failed: " ^ quote s1);
 | |
| 443 | in | |
| 444 | check "[(x,y,z). b]" "if b then [(x, y, z)] else []"; | |
| 445 | check "[(x,y,z). x\<leftarrow>xs]" "map (\<lambda>x. (x, y, z)) xs"; | |
| 446 | check "[e x y. x\<leftarrow>xs, y\<leftarrow>ys]" "concat (map (\<lambda>x. map (\<lambda>y. e x y) ys) xs)"; | |
| 447 | check "[(x,y,z). x<a, x>b]" "if x < a then if b < x then [(x, y, z)] else [] else []"; | |
| 448 | check "[(x,y,z). x\<leftarrow>xs, x>b]" "concat (map (\<lambda>x. if b < x then [(x, y, z)] else []) xs)"; | |
| 449 | check "[(x,y,z). x<a, x\<leftarrow>xs]" "if x < a then map (\<lambda>x. (x, y, z)) xs else []"; | |
| 450 | check "[(x,y). Cons True x \<leftarrow> xs]" | |
| 451 | "concat (map (\<lambda>xa. case xa of [] \<Rightarrow> [] | True # x \<Rightarrow> [(x, y)] | False # x \<Rightarrow> []) xs)"; | |
| 452 | check "[(x,y,z). Cons x [] \<leftarrow> xs]" | |
| 453 | "concat (map (\<lambda>xa. case xa of [] \<Rightarrow> [] | [x] \<Rightarrow> [(x, y, z)] | x # aa # lista \<Rightarrow> []) xs)"; | |
| 454 | check "[(x,y,z). x<a, x>b, x=d]" | |
| 455 | "if x < a then if b < x then if x = d then [(x, y, z)] else [] else [] else []"; | |
| 456 | check "[(x,y,z). x<a, x>b, y\<leftarrow>ys]" | |
| 457 | "if x < a then if b < x then map (\<lambda>y. (x, y, z)) ys else [] else []"; | |
| 458 | check "[(x,y,z). x<a, x\<leftarrow>xs,y>b]" | |
| 459 | "if x < a then concat (map (\<lambda>x. if b < y then [(x, y, z)] else []) xs) else []"; | |
| 460 | check "[(x,y,z). x<a, x\<leftarrow>xs, y\<leftarrow>ys]" | |
| 461 | "if x < a then concat (map (\<lambda>x. map (\<lambda>y. (x, y, z)) ys) xs) else []"; | |
| 462 | check "[(x,y,z). x\<leftarrow>xs, x>b, y<a]" | |
| 463 | "concat (map (\<lambda>x. if b < x then if y < a then [(x, y, z)] else [] else []) xs)"; | |
| 464 | check "[(x,y,z). x\<leftarrow>xs, x>b, y\<leftarrow>ys]" | |
| 465 | "concat (map (\<lambda>x. if b < x then map (\<lambda>y. (x, y, z)) ys else []) xs)"; | |
| 466 | check "[(x,y,z). x\<leftarrow>xs, y\<leftarrow>ys,y>x]" | |
| 467 | "concat (map (\<lambda>x. concat (map (\<lambda>y. if x < y then [(x, y, z)] else []) ys)) xs)"; | |
| 468 | check "[(x,y,z). x\<leftarrow>xs, y\<leftarrow>ys,z\<leftarrow>zs]" | |
| 469 | "concat (map (\<lambda>x. concat (map (\<lambda>y. map (\<lambda>z. (x, y, z)) zs) ys)) xs)" | |
| 470 | end; | |
| 471 | *} | |
| 472 | ||
| 35115 | 473 | (* | 
| 24349 | 474 | term "[(x,y). x\<leftarrow>xs, let xx = x+x, y\<leftarrow>ys, y \<noteq> xx]" | 
| 23192 | 475 | *) | 
| 476 | ||
| 42167 | 477 | |
| 41463 
edbf0a86fb1c
adding simproc to rewrite list comprehensions to set comprehensions; adopting proofs
 bulwahn parents: 
41372diff
changeset | 478 | use "Tools/list_to_set_comprehension.ML" | 
| 
edbf0a86fb1c
adding simproc to rewrite list comprehensions to set comprehensions; adopting proofs
 bulwahn parents: 
41372diff
changeset | 479 | |
| 
edbf0a86fb1c
adding simproc to rewrite list comprehensions to set comprehensions; adopting proofs
 bulwahn parents: 
41372diff
changeset | 480 | 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 | 481 | |
| 46133 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 haftmann parents: 
46125diff
changeset | 482 | 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 | 483 | |
| 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 haftmann parents: 
46125diff
changeset | 484 | hide_const (open) coset | 
| 35115 | 485 | |
| 21061 
580dfc999ef6
added normal post setup; cleaned up "execution" constants
 haftmann parents: 
21046diff
changeset | 486 | subsubsection {* @{const Nil} and @{const Cons} *}
 | 
| 
580dfc999ef6
added normal post setup; cleaned up "execution" constants
 haftmann parents: 
21046diff
changeset | 487 | |
| 
580dfc999ef6
added normal post setup; cleaned up "execution" constants
 haftmann parents: 
21046diff
changeset | 488 | lemma not_Cons_self [simp]: | 
| 
580dfc999ef6
added normal post setup; cleaned up "execution" constants
 haftmann parents: 
21046diff
changeset | 489 | "xs \<noteq> x # xs" | 
| 13145 | 490 | by (induct xs) auto | 
| 13114 | 491 | |
| 41697 | 492 | lemma not_Cons_self2 [simp]: | 
| 493 | "x # xs \<noteq> xs" | |
| 494 | by (rule not_Cons_self [symmetric]) | |
| 13114 | 495 | |
| 13142 | 496 | lemma neq_Nil_conv: "(xs \<noteq> []) = (\<exists>y ys. xs = y # ys)" | 
| 13145 | 497 | by (induct xs) auto | 
| 13114 | 498 | |
| 13142 | 499 | lemma length_induct: | 
| 21061 
580dfc999ef6
added normal post setup; cleaned up "execution" constants
 haftmann parents: 
21046diff
changeset | 500 | "(\<And>xs. \<forall>ys. length ys < length xs \<longrightarrow> P ys \<Longrightarrow> P xs) \<Longrightarrow> P xs" | 
| 17589 | 501 | by (rule measure_induct [of length]) iprover | 
| 13114 | 502 | |
| 37289 | 503 | lemma list_nonempty_induct [consumes 1, case_names single cons]: | 
| 504 | assumes "xs \<noteq> []" | |
| 505 | assumes single: "\<And>x. P [x]" | |
| 506 | assumes cons: "\<And>x xs. xs \<noteq> [] \<Longrightarrow> P xs \<Longrightarrow> P (x # xs)" | |
| 507 | shows "P xs" | |
| 508 | using `xs \<noteq> []` proof (induct xs) | |
| 509 | case Nil then show ?case by simp | |
| 510 | next | |
| 511 | case (Cons x xs) show ?case proof (cases xs) | |
| 512 | case Nil with single show ?thesis by simp | |
| 513 | next | |
| 514 | case Cons then have "xs \<noteq> []" by simp | |
| 515 | moreover with Cons.hyps have "P xs" . | |
| 516 | ultimately show ?thesis by (rule cons) | |
| 517 | qed | |
| 518 | qed | |
| 519 | ||
| 45714 | 520 | lemma inj_split_Cons: "inj_on (\<lambda>(xs, n). n#xs) X" | 
| 521 | by (auto intro!: inj_onI) | |
| 13114 | 522 | |
| 21061 
580dfc999ef6
added normal post setup; cleaned up "execution" constants
 haftmann parents: 
21046diff
changeset | 523 | subsubsection {* @{const length} *}
 | 
| 13114 | 524 | |
| 13142 | 525 | text {*
 | 
| 21061 
580dfc999ef6
added normal post setup; cleaned up "execution" constants
 haftmann parents: 
21046diff
changeset | 526 |   Needs to come before @{text "@"} because of theorem @{text
 | 
| 
580dfc999ef6
added normal post setup; cleaned up "execution" constants
 haftmann parents: 
21046diff
changeset | 527 | append_eq_append_conv}. | 
| 13142 | 528 | *} | 
| 13114 | 529 | |
| 13142 | 530 | lemma length_append [simp]: "length (xs @ ys) = length xs + length ys" | 
| 13145 | 531 | by (induct xs) auto | 
| 13114 | 532 | |
| 13142 | 533 | lemma length_map [simp]: "length (map f xs) = length xs" | 
| 13145 | 534 | by (induct xs) auto | 
| 13114 | 535 | |
| 13142 | 536 | lemma length_rev [simp]: "length (rev xs) = length xs" | 
| 13145 | 537 | by (induct xs) auto | 
| 13114 | 538 | |
| 13142 | 539 | lemma length_tl [simp]: "length (tl xs) = length xs - 1" | 
| 13145 | 540 | by (cases xs) auto | 
| 13114 | 541 | |
| 13142 | 542 | lemma length_0_conv [iff]: "(length xs = 0) = (xs = [])" | 
| 13145 | 543 | by (induct xs) auto | 
| 13114 | 544 | |
| 13142 | 545 | lemma length_greater_0_conv [iff]: "(0 < length xs) = (xs \<noteq> [])" | 
| 13145 | 546 | by (induct xs) auto | 
| 13114 | 547 | |
| 23479 | 548 | lemma length_pos_if_in_set: "x : set xs \<Longrightarrow> length xs > 0" | 
| 549 | by auto | |
| 550 | ||
| 13114 | 551 | lemma length_Suc_conv: | 
| 13145 | 552 | "(length xs = Suc n) = (\<exists>y ys. xs = y # ys \<and> length ys = n)" | 
| 553 | by (induct xs) auto | |
| 13142 | 554 | |
| 14025 | 555 | lemma Suc_length_conv: | 
| 556 | "(Suc n = length xs) = (\<exists>y ys. xs = y # ys \<and> length ys = n)" | |
| 14208 | 557 | apply (induct xs, simp, simp) | 
| 14025 | 558 | apply blast | 
| 559 | done | |
| 560 | ||
| 25221 
5ded95dda5df
append/member: more light-weight way to declare authentic syntax;
 wenzelm parents: 
25215diff
changeset | 561 | 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 | 562 | by (induct xs) auto | 
| 
5ded95dda5df
append/member: more light-weight way to declare authentic syntax;
 wenzelm parents: 
25215diff
changeset | 563 | |
| 26442 
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
 haftmann parents: 
26300diff
changeset | 564 | lemma list_induct2 [consumes 1, case_names Nil Cons]: | 
| 
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
 haftmann parents: 
26300diff
changeset | 565 | "length xs = length ys \<Longrightarrow> P [] [] \<Longrightarrow> | 
| 
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
 haftmann parents: 
26300diff
changeset | 566 | (\<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 | 567 | \<Longrightarrow> P xs ys" | 
| 
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
 haftmann parents: 
26300diff
changeset | 568 | proof (induct xs arbitrary: ys) | 
| 
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
 haftmann parents: 
26300diff
changeset | 569 | case Nil then show ?case by simp | 
| 
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
 haftmann parents: 
26300diff
changeset | 570 | next | 
| 
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
 haftmann parents: 
26300diff
changeset | 571 | 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 | 572 | qed | 
| 
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
 haftmann parents: 
26300diff
changeset | 573 | |
| 
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
 haftmann parents: 
26300diff
changeset | 574 | lemma list_induct3 [consumes 2, case_names Nil Cons]: | 
| 
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
 haftmann parents: 
26300diff
changeset | 575 | "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 | 576 | (\<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 | 577 | \<Longrightarrow> P xs ys zs" | 
| 
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
 haftmann parents: 
26300diff
changeset | 578 | proof (induct xs arbitrary: ys zs) | 
| 
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
 haftmann parents: 
26300diff
changeset | 579 | case Nil then show ?case by simp | 
| 
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
 haftmann parents: 
26300diff
changeset | 580 | next | 
| 
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
 haftmann parents: 
26300diff
changeset | 581 | 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 | 582 | (cases zs, simp_all) | 
| 
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
 haftmann parents: 
26300diff
changeset | 583 | qed | 
| 13114 | 584 | |
| 36154 
11c6106d7787
Respectfullness and preservation of list_rel
 Cezary Kaliszyk <kaliszyk@in.tum.de> parents: 
35828diff
changeset | 585 | 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 | 586 | "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 | 587 | 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 | 588 | 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 | 589 | 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 | 590 | proof (induct xs arbitrary: ys zs ws) | 
| 
11c6106d7787
Respectfullness and preservation of list_rel
 Cezary Kaliszyk <kaliszyk@in.tum.de> parents: 
35828diff
changeset | 591 | case Nil then show ?case by simp | 
| 
11c6106d7787
Respectfullness and preservation of list_rel
 Cezary Kaliszyk <kaliszyk@in.tum.de> parents: 
35828diff
changeset | 592 | next | 
| 
11c6106d7787
Respectfullness and preservation of list_rel
 Cezary Kaliszyk <kaliszyk@in.tum.de> parents: 
35828diff
changeset | 593 | 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 | 594 | qed | 
| 
11c6106d7787
Respectfullness and preservation of list_rel
 Cezary Kaliszyk <kaliszyk@in.tum.de> parents: 
35828diff
changeset | 595 | |
| 22493 
db930e490fe5
added another rule for simultaneous induction, and lemmas for zip
 krauss parents: 
22422diff
changeset | 596 | lemma list_induct2': | 
| 
db930e490fe5
added another rule for simultaneous induction, and lemmas for zip
 krauss parents: 
22422diff
changeset | 597 | "\<lbrakk> P [] []; | 
| 
db930e490fe5
added another rule for simultaneous induction, and lemmas for zip
 krauss parents: 
22422diff
changeset | 598 | \<And>x xs. P (x#xs) []; | 
| 
db930e490fe5
added another rule for simultaneous induction, and lemmas for zip
 krauss parents: 
22422diff
changeset | 599 | \<And>y ys. P [] (y#ys); | 
| 
db930e490fe5
added another rule for simultaneous induction, and lemmas for zip
 krauss parents: 
22422diff
changeset | 600 | \<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 | 601 | \<Longrightarrow> P xs ys" | 
| 
db930e490fe5
added another rule for simultaneous induction, and lemmas for zip
 krauss parents: 
22422diff
changeset | 602 | by (induct xs arbitrary: ys) (case_tac x, auto)+ | 
| 
db930e490fe5
added another rule for simultaneous induction, and lemmas for zip
 krauss parents: 
22422diff
changeset | 603 | |
| 22143 
cf58486ca11b
Added simproc list_neq (prompted by an application)
 nipkow parents: 
21911diff
changeset | 604 | lemma neq_if_length_neq: "length xs \<noteq> length ys \<Longrightarrow> (xs = ys) == False" | 
| 24349 | 605 | by (rule Eq_FalseI) auto | 
| 24037 | 606 | |
| 607 | simproc_setup list_neq ("(xs::'a list) = ys") = {*
 | |
| 22143 
cf58486ca11b
Added simproc list_neq (prompted by an application)
 nipkow parents: 
21911diff
changeset | 608 | (* | 
| 
cf58486ca11b
Added simproc list_neq (prompted by an application)
 nipkow parents: 
21911diff
changeset | 609 | 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 | 610 | 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 | 611 | 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 | 612 | *) | 
| 24037 | 613 | |
| 614 | let | |
| 22143 
cf58486ca11b
Added simproc list_neq (prompted by an application)
 nipkow parents: 
21911diff
changeset | 615 | |
| 29856 | 616 | fun len (Const(@{const_name Nil},_)) acc = acc
 | 
| 617 |   | len (Const(@{const_name Cons},_) $ _ $ xs) (ts,n) = len xs (ts,n+1)
 | |
| 618 |   | len (Const(@{const_name append},_) $ xs $ ys) acc = len xs (len ys acc)
 | |
| 619 |   | len (Const(@{const_name rev},_) $ xs) acc = len xs acc
 | |
| 620 |   | len (Const(@{const_name map},_) $ _ $ xs) acc = len xs acc
 | |
| 22143 
cf58486ca11b
Added simproc list_neq (prompted by an application)
 nipkow parents: 
21911diff
changeset | 621 | | len t (ts,n) = (t::ts,n); | 
| 
cf58486ca11b
Added simproc list_neq (prompted by an application)
 nipkow parents: 
21911diff
changeset | 622 | |
| 24037 | 623 | fun list_neq _ ss ct = | 
| 22143 
cf58486ca11b
Added simproc list_neq (prompted by an application)
 nipkow parents: 
21911diff
changeset | 624 | let | 
| 24037 | 625 | val (Const(_,eqT) $ lhs $ rhs) = Thm.term_of ct; | 
| 22143 
cf58486ca11b
Added simproc list_neq (prompted by an application)
 nipkow parents: 
21911diff
changeset | 626 | 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 | 627 | fun prove_neq() = | 
| 
cf58486ca11b
Added simproc list_neq (prompted by an application)
 nipkow parents: 
21911diff
changeset | 628 | let | 
| 
cf58486ca11b
Added simproc list_neq (prompted by an application)
 nipkow parents: 
21911diff
changeset | 629 | val Type(_,listT::_) = eqT; | 
| 22994 | 630 | val size = HOLogic.size_const listT; | 
| 22143 
cf58486ca11b
Added simproc list_neq (prompted by an application)
 nipkow parents: 
21911diff
changeset | 631 | val eq_len = HOLogic.mk_eq (size $ lhs, size $ rhs); | 
| 
cf58486ca11b
Added simproc list_neq (prompted by an application)
 nipkow parents: 
21911diff
changeset | 632 | val neq_len = HOLogic.mk_Trueprop (HOLogic.Not $ eq_len); | 
| 
cf58486ca11b
Added simproc list_neq (prompted by an application)
 nipkow parents: 
21911diff
changeset | 633 | val thm = Goal.prove (Simplifier.the_context ss) [] [] neq_len | 
| 22633 | 634 |           (K (simp_tac (Simplifier.inherit_context ss @{simpset}) 1));
 | 
| 635 |       in SOME (thm RS @{thm neq_if_length_neq}) end
 | |
| 22143 
cf58486ca11b
Added simproc list_neq (prompted by an application)
 nipkow parents: 
21911diff
changeset | 636 | in | 
| 23214 | 637 | if m < n andalso submultiset (op aconv) (ls,rs) orelse | 
| 638 | n < m andalso submultiset (op aconv) (rs,ls) | |
| 22143 
cf58486ca11b
Added simproc list_neq (prompted by an application)
 nipkow parents: 
21911diff
changeset | 639 | then prove_neq() else NONE | 
| 
cf58486ca11b
Added simproc list_neq (prompted by an application)
 nipkow parents: 
21911diff
changeset | 640 | end; | 
| 24037 | 641 | in list_neq end; | 
| 22143 
cf58486ca11b
Added simproc list_neq (prompted by an application)
 nipkow parents: 
21911diff
changeset | 642 | *} | 
| 
cf58486ca11b
Added simproc list_neq (prompted by an application)
 nipkow parents: 
21911diff
changeset | 643 | |
| 
cf58486ca11b
Added simproc list_neq (prompted by an application)
 nipkow parents: 
21911diff
changeset | 644 | |
| 15392 | 645 | subsubsection {* @{text "@"} -- append *}
 | 
| 13114 | 646 | |
| 13142 | 647 | lemma append_assoc [simp]: "(xs @ ys) @ zs = xs @ (ys @ zs)" | 
| 13145 | 648 | by (induct xs) auto | 
| 13114 | 649 | |
| 13142 | 650 | lemma append_Nil2 [simp]: "xs @ [] = xs" | 
| 13145 | 651 | by (induct xs) auto | 
| 3507 | 652 | |
| 13142 | 653 | lemma append_is_Nil_conv [iff]: "(xs @ ys = []) = (xs = [] \<and> ys = [])" | 
| 13145 | 654 | by (induct xs) auto | 
| 13114 | 655 | |
| 13142 | 656 | lemma Nil_is_append_conv [iff]: "([] = xs @ ys) = (xs = [] \<and> ys = [])" | 
| 13145 | 657 | by (induct xs) auto | 
| 13114 | 658 | |
| 13142 | 659 | lemma append_self_conv [iff]: "(xs @ ys = xs) = (ys = [])" | 
| 13145 | 660 | by (induct xs) auto | 
| 13114 | 661 | |
| 13142 | 662 | lemma self_append_conv [iff]: "(xs = xs @ ys) = (ys = [])" | 
| 13145 | 663 | by (induct xs) auto | 
| 13114 | 664 | |
| 35828 
46cfc4b8112e
now use "Named_Thms" for "noatp", and renamed "noatp" to "no_atp"
 blanchet parents: 
35827diff
changeset | 665 | lemma append_eq_append_conv [simp, no_atp]: | 
| 24526 | 666 | "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 | 667 | ==> (xs@us = ys@vs) = (xs=ys \<and> us=vs)" | 
| 24526 | 668 | apply (induct xs arbitrary: ys) | 
| 14208 | 669 | apply (case_tac ys, simp, force) | 
| 670 | apply (case_tac ys, force, simp) | |
| 13145 | 671 | done | 
| 13142 | 672 | |
| 24526 | 673 | lemma append_eq_append_conv2: "(xs @ ys = zs @ ts) = | 
| 674 | (EX us. xs = zs @ us & us @ ys = ts | xs @ us = zs & ys = us@ ts)" | |
| 675 | apply (induct xs arbitrary: ys zs ts) | |
| 44890 
22f665a2e91c
new fastforce replacing fastsimp - less confusing name
 nipkow parents: 
44635diff
changeset | 676 | apply fastforce | 
| 14495 | 677 | apply(case_tac zs) | 
| 678 | apply simp | |
| 44890 
22f665a2e91c
new fastforce replacing fastsimp - less confusing name
 nipkow parents: 
44635diff
changeset | 679 | apply fastforce | 
| 14495 | 680 | done | 
| 681 | ||
| 34910 
b23bd3ee4813
same_append_eq / append_same_eq are now used for simplifying induction rules.
 berghofe parents: 
34064diff
changeset | 682 | lemma same_append_eq [iff, induct_simp]: "(xs @ ys = xs @ zs) = (ys = zs)" | 
| 13145 | 683 | by simp | 
| 13142 | 684 | |
| 685 | lemma append1_eq_conv [iff]: "(xs @ [x] = ys @ [y]) = (xs = ys \<and> x = y)" | |
| 13145 | 686 | by simp | 
| 13114 | 687 | |
| 34910 
b23bd3ee4813
same_append_eq / append_same_eq are now used for simplifying induction rules.
 berghofe parents: 
34064diff
changeset | 688 | lemma append_same_eq [iff, induct_simp]: "(ys @ xs = zs @ xs) = (ys = zs)" | 
| 13145 | 689 | by simp | 
| 13114 | 690 | |
| 13142 | 691 | lemma append_self_conv2 [iff]: "(xs @ ys = ys) = (xs = [])" | 
| 13145 | 692 | using append_same_eq [of _ _ "[]"] by auto | 
| 3507 | 693 | |
| 13142 | 694 | lemma self_append_conv2 [iff]: "(ys = xs @ ys) = (xs = [])" | 
| 13145 | 695 | using append_same_eq [of "[]"] by auto | 
| 13114 | 696 | |
| 35828 
46cfc4b8112e
now use "Named_Thms" for "noatp", and renamed "noatp" to "no_atp"
 blanchet parents: 
35827diff
changeset | 697 | lemma hd_Cons_tl [simp,no_atp]: "xs \<noteq> [] ==> hd xs # tl xs = xs" | 
| 13145 | 698 | by (induct xs) auto | 
| 13114 | 699 | |
| 13142 | 700 | lemma hd_append: "hd (xs @ ys) = (if xs = [] then hd ys else hd xs)" | 
| 13145 | 701 | by (induct xs) auto | 
| 13114 | 702 | |
| 13142 | 703 | lemma hd_append2 [simp]: "xs \<noteq> [] ==> hd (xs @ ys) = hd xs" | 
| 13145 | 704 | by (simp add: hd_append split: list.split) | 
| 13114 | 705 | |
| 13142 | 706 | lemma tl_append: "tl (xs @ ys) = (case xs of [] => tl ys | z#zs => zs @ ys)" | 
| 13145 | 707 | by (simp split: list.split) | 
| 13114 | 708 | |
| 13142 | 709 | lemma tl_append2 [simp]: "xs \<noteq> [] ==> tl (xs @ ys) = tl xs @ ys" | 
| 13145 | 710 | by (simp add: tl_append split: list.split) | 
| 13114 | 711 | |
| 712 | ||
| 14300 | 713 | lemma Cons_eq_append_conv: "x#xs = ys@zs = | 
| 714 | (ys = [] & x#xs = zs | (EX ys'. x#ys' = ys & xs = ys'@zs))" | |
| 715 | by(cases ys) auto | |
| 716 | ||
| 15281 | 717 | lemma append_eq_Cons_conv: "(ys@zs = x#xs) = | 
| 718 | (ys = [] & zs = x#xs | (EX ys'. ys = x#ys' & ys'@zs = xs))" | |
| 719 | by(cases ys) auto | |
| 720 | ||
| 14300 | 721 | |
| 13142 | 722 | text {* Trivial rules for solving @{text "@"}-equations automatically. *}
 | 
| 13114 | 723 | |
| 724 | lemma eq_Nil_appendI: "xs = ys ==> xs = [] @ ys" | |
| 13145 | 725 | by simp | 
| 13114 | 726 | |
| 13142 | 727 | lemma Cons_eq_appendI: | 
| 13145 | 728 | "[| x # xs1 = ys; xs = xs1 @ zs |] ==> x # xs = ys @ zs" | 
| 729 | by (drule sym) simp | |
| 13114 | 730 | |
| 13142 | 731 | lemma append_eq_appendI: | 
| 13145 | 732 | "[| xs @ xs1 = zs; ys = xs1 @ us |] ==> xs @ ys = zs @ us" | 
| 733 | by (drule sym) simp | |
| 13114 | 734 | |
| 735 | ||
| 13142 | 736 | text {*
 | 
| 13145 | 737 | Simplification procedure for all list equalities. | 
| 738 | Currently only tries to rearrange @{text "@"} to see if
 | |
| 739 | - both lists end in a singleton list, | |
| 740 | - or both lists end in the same list. | |
| 13142 | 741 | *} | 
| 742 | ||
| 43594 | 743 | simproc_setup list_eq ("(xs::'a list) = ys")  = {*
 | 
| 13462 | 744 | let | 
| 43594 | 745 |     fun last (cons as Const (@{const_name Cons}, _) $ _ $ xs) =
 | 
| 746 |           (case xs of Const (@{const_name Nil}, _) => cons | _ => last xs)
 | |
| 747 |       | last (Const(@{const_name append},_) $ _ $ ys) = last ys
 | |
| 748 | | last t = t; | |
| 749 | ||
| 750 |     fun list1 (Const(@{const_name Cons},_) $ _ $ Const(@{const_name Nil},_)) = true
 | |
| 751 | | list1 _ = false; | |
| 752 | ||
| 753 |     fun butlast ((cons as Const(@{const_name Cons},_) $ x) $ xs) =
 | |
| 754 |           (case xs of Const (@{const_name Nil}, _) => xs | _ => cons $ butlast xs)
 | |
| 755 |       | butlast ((app as Const (@{const_name append}, _) $ xs) $ ys) = app $ butlast ys
 | |
| 756 |       | butlast xs = Const(@{const_name Nil}, fastype_of xs);
 | |
| 757 | ||
| 758 | val rearr_ss = | |
| 759 |       HOL_basic_ss addsimps [@{thm append_assoc}, @{thm append_Nil}, @{thm append_Cons}];
 | |
| 760 | ||
| 761 | fun list_eq ss (F as (eq as Const(_,eqT)) $ lhs $ rhs) = | |
| 13462 | 762 | let | 
| 43594 | 763 | val lastl = last lhs and lastr = last rhs; | 
| 764 | fun rearr conv = | |
| 765 | let | |
| 766 | val lhs1 = butlast lhs and rhs1 = butlast rhs; | |
| 767 | val Type(_,listT::_) = eqT | |
| 768 | val appT = [listT,listT] ---> listT | |
| 769 |             val app = Const(@{const_name append},appT)
 | |
| 770 | val F2 = eq $ (app$lhs1$lastl) $ (app$rhs1$lastr) | |
| 771 | val eq = HOLogic.mk_Trueprop (HOLogic.mk_eq (F,F2)); | |
| 772 | val thm = Goal.prove (Simplifier.the_context ss) [] [] eq | |
| 773 | (K (simp_tac (Simplifier.inherit_context ss rearr_ss) 1)); | |
| 774 | in SOME ((conv RS (thm RS trans)) RS eq_reflection) end; | |
| 775 | in | |
| 776 |         if list1 lastl andalso list1 lastr then rearr @{thm append1_eq_conv}
 | |
| 777 |         else if lastl aconv lastr then rearr @{thm append_same_eq}
 | |
| 778 | else NONE | |
| 779 | end; | |
| 780 | in fn _ => fn ss => fn ct => list_eq ss (term_of ct) end; | |
| 13114 | 781 | *} | 
| 782 | ||
| 783 | ||
| 15392 | 784 | subsubsection {* @{text map} *}
 | 
| 13114 | 785 | |
| 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 | 786 | 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 | 787 | "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 | 788 | 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 | 789 | |
| 
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 | 790 | 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 | 791 | "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 | 792 | 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 | 793 | |
| 13142 | 794 | lemma map_ext: "(!!x. x : set xs --> f x = g x) ==> map f xs = map g xs" | 
| 13145 | 795 | by (induct xs) simp_all | 
| 13114 | 796 | |
| 13142 | 797 | lemma map_ident [simp]: "map (\<lambda>x. x) = (\<lambda>xs. xs)" | 
| 13145 | 798 | by (rule ext, induct_tac xs) auto | 
| 13114 | 799 | |
| 13142 | 800 | lemma map_append [simp]: "map f (xs @ ys) = map f xs @ map f ys" | 
| 13145 | 801 | by (induct xs) auto | 
| 13114 | 802 | |
| 33639 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 803 | 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 | 804 | 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 | 805 | |
| 35208 | 806 | lemma map_comp_map[simp]: "((map f) o (map g)) = map(f o g)" | 
| 807 | apply(rule ext) | |
| 808 | apply(simp) | |
| 809 | done | |
| 810 | ||
| 13142 | 811 | lemma rev_map: "rev (map f xs) = map f (rev xs)" | 
| 13145 | 812 | by (induct xs) auto | 
| 13114 | 813 | |
| 13737 | 814 | lemma map_eq_conv[simp]: "(map f xs = map g xs) = (!x : set xs. f x = g x)" | 
| 815 | by (induct xs) auto | |
| 816 | ||
| 44013 
5cfc1c36ae97
moved recdef package to HOL/Library/Old_Recdef.thy
 krauss parents: 
43594diff
changeset | 817 | lemma map_cong [fundef_cong]: | 
| 40122 
1d8ad2ff3e01
dropped (almost) redundant distinct.induct rule; distinct_simps again named distinct.simps
 haftmann parents: 
40077diff
changeset | 818 | "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 | 819 | by simp | 
| 13114 | 820 | |
| 13142 | 821 | lemma map_is_Nil_conv [iff]: "(map f xs = []) = (xs = [])" | 
| 13145 | 822 | by (cases xs) auto | 
| 13114 | 823 | |
| 13142 | 824 | lemma Nil_is_map_conv [iff]: "([] = map f xs) = (xs = [])" | 
| 13145 | 825 | by (cases xs) auto | 
| 13114 | 826 | |
| 18447 | 827 | lemma map_eq_Cons_conv: | 
| 14025 | 828 | "(map f xs = y#ys) = (\<exists>z zs. xs = z#zs \<and> f z = y \<and> map f zs = ys)" | 
| 13145 | 829 | by (cases xs) auto | 
| 13114 | 830 | |
| 18447 | 831 | lemma Cons_eq_map_conv: | 
| 14025 | 832 | "(x#xs = map f ys) = (\<exists>z zs. ys = z#zs \<and> x = f z \<and> xs = map f zs)" | 
| 833 | by (cases ys) auto | |
| 834 | ||
| 18447 | 835 | lemmas map_eq_Cons_D = map_eq_Cons_conv [THEN iffD1] | 
| 836 | lemmas Cons_eq_map_D = Cons_eq_map_conv [THEN iffD1] | |
| 837 | declare map_eq_Cons_D [dest!] Cons_eq_map_D [dest!] | |
| 838 | ||
| 14111 | 839 | lemma ex_map_conv: | 
| 840 | "(EX xs. ys = map f xs) = (ALL y : set ys. EX x. y = f x)" | |
| 18447 | 841 | by(induct ys, auto simp add: Cons_eq_map_conv) | 
| 14111 | 842 | |
| 15110 
78b5636eabc7
Added a number of new thms and the new function remove1
 nipkow parents: 
15072diff
changeset | 843 | lemma map_eq_imp_length_eq: | 
| 35510 | 844 | assumes "map f xs = map g ys" | 
| 26734 | 845 | shows "length xs = length ys" | 
| 846 | using assms proof (induct ys arbitrary: xs) | |
| 847 | case Nil then show ?case by simp | |
| 848 | next | |
| 849 | case (Cons y ys) then obtain z zs where xs: "xs = z # zs" by auto | |
| 35510 | 850 | from Cons xs have "map f zs = map g ys" by simp | 
| 26734 | 851 | moreover with Cons have "length zs = length ys" by blast | 
| 852 | with xs show ?case by simp | |
| 853 | qed | |
| 854 | ||
| 15110 
78b5636eabc7
Added a number of new thms and the new function remove1
 nipkow parents: 
15072diff
changeset | 855 | lemma map_inj_on: | 
| 
78b5636eabc7
Added a number of new thms and the new function remove1
 nipkow parents: 
15072diff
changeset | 856 | "[| 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 | 857 | ==> xs = ys" | 
| 
78b5636eabc7
Added a number of new thms and the new function remove1
 nipkow parents: 
15072diff
changeset | 858 | apply(frule map_eq_imp_length_eq) | 
| 
78b5636eabc7
Added a number of new thms and the new function remove1
 nipkow parents: 
15072diff
changeset | 859 | apply(rotate_tac -1) | 
| 
78b5636eabc7
Added a number of new thms and the new function remove1
 nipkow parents: 
15072diff
changeset | 860 | apply(induct rule:list_induct2) | 
| 
78b5636eabc7
Added a number of new thms and the new function remove1
 nipkow parents: 
15072diff
changeset | 861 | apply simp | 
| 
78b5636eabc7
Added a number of new thms and the new function remove1
 nipkow parents: 
15072diff
changeset | 862 | apply(simp) | 
| 
78b5636eabc7
Added a number of new thms and the new function remove1
 nipkow parents: 
15072diff
changeset | 863 | apply (blast intro:sym) | 
| 
78b5636eabc7
Added a number of new thms and the new function remove1
 nipkow parents: 
15072diff
changeset | 864 | done | 
| 
78b5636eabc7
Added a number of new thms and the new function remove1
 nipkow parents: 
15072diff
changeset | 865 | |
| 
78b5636eabc7
Added a number of new thms and the new function remove1
 nipkow parents: 
15072diff
changeset | 866 | lemma inj_on_map_eq_map: | 
| 
78b5636eabc7
Added a number of new thms and the new function remove1
 nipkow parents: 
15072diff
changeset | 867 | "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 | 868 | by(blast dest:map_inj_on) | 
| 
78b5636eabc7
Added a number of new thms and the new function remove1
 nipkow parents: 
15072diff
changeset | 869 | |
| 13114 | 870 | lemma map_injective: | 
| 24526 | 871 | "map f xs = map f ys ==> inj f ==> xs = ys" | 
| 872 | by (induct ys arbitrary: xs) (auto dest!:injD) | |
| 13114 | 873 | |
| 14339 | 874 | lemma inj_map_eq_map[simp]: "inj f \<Longrightarrow> (map f xs = map f ys) = (xs = ys)" | 
| 875 | by(blast dest:map_injective) | |
| 876 | ||
| 13114 | 877 | lemma inj_mapI: "inj f ==> inj (map f)" | 
| 17589 | 878 | by (iprover dest: map_injective injD intro: inj_onI) | 
| 13114 | 879 | |
| 880 | lemma inj_mapD: "inj (map f) ==> inj f" | |
| 14208 | 881 | apply (unfold inj_on_def, clarify) | 
| 13145 | 882 | apply (erule_tac x = "[x]" in ballE) | 
| 14208 | 883 | apply (erule_tac x = "[y]" in ballE, simp, blast) | 
| 13145 | 884 | apply blast | 
| 885 | done | |
| 13114 | 886 | |
| 14339 | 887 | lemma inj_map[iff]: "inj (map f) = inj f" | 
| 13145 | 888 | by (blast dest: inj_mapD intro: inj_mapI) | 
| 13114 | 889 | |
| 15303 | 890 | lemma inj_on_mapI: "inj_on f (\<Union>(set ` A)) \<Longrightarrow> inj_on (map f) A" | 
| 891 | apply(rule inj_onI) | |
| 892 | apply(erule map_inj_on) | |
| 893 | apply(blast intro:inj_onI dest:inj_onD) | |
| 894 | done | |
| 895 | ||
| 14343 | 896 | lemma map_idI: "(\<And>x. x \<in> set xs \<Longrightarrow> f x = x) \<Longrightarrow> map f xs = xs" | 
| 897 | by (induct xs, auto) | |
| 13114 | 898 | |
| 14402 
4201e1916482
moved lemmas from MicroJava/Comp/AuxLemmas.thy to List.thy
 nipkow parents: 
14395diff
changeset | 899 | 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 | 900 | by (induct xs) auto | 
| 
4201e1916482
moved lemmas from MicroJava/Comp/AuxLemmas.thy to List.thy
 nipkow parents: 
14395diff
changeset | 901 | |
| 15110 
78b5636eabc7
Added a number of new thms and the new function remove1
 nipkow parents: 
15072diff
changeset | 902 | lemma map_fst_zip[simp]: | 
| 
78b5636eabc7
Added a number of new thms and the new function remove1
 nipkow parents: 
15072diff
changeset | 903 | "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 | 904 | by (induct rule:list_induct2, simp_all) | 
| 
78b5636eabc7
Added a number of new thms and the new function remove1
 nipkow parents: 
15072diff
changeset | 905 | |
| 
78b5636eabc7
Added a number of new thms and the new function remove1
 nipkow parents: 
15072diff
changeset | 906 | lemma map_snd_zip[simp]: | 
| 
78b5636eabc7
Added a number of new thms and the new function remove1
 nipkow parents: 
15072diff
changeset | 907 | "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 | 908 | by (induct rule:list_induct2, simp_all) | 
| 
78b5636eabc7
Added a number of new thms and the new function remove1
 nipkow parents: 
15072diff
changeset | 909 | |
| 41505 
6d19301074cf
"enriched_type" replaces less specific "type_lifting"
 haftmann parents: 
41463diff
changeset | 910 | enriched_type map: map | 
| 41372 | 911 | by (simp_all add: fun_eq_iff id_def) | 
| 40608 
6c28ab8b8166
mapper for list type; map_pair replaces prod_fun
 haftmann parents: 
40593diff
changeset | 912 | |
| 15110 
78b5636eabc7
Added a number of new thms and the new function remove1
 nipkow parents: 
15072diff
changeset | 913 | |
| 15392 | 914 | subsubsection {* @{text rev} *}
 | 
| 13114 | 915 | |
| 13142 | 916 | lemma rev_append [simp]: "rev (xs @ ys) = rev ys @ rev xs" | 
| 13145 | 917 | by (induct xs) auto | 
| 13114 | 918 | |
| 13142 | 919 | lemma rev_rev_ident [simp]: "rev (rev xs) = xs" | 
| 13145 | 920 | by (induct xs) auto | 
| 13114 | 921 | |
| 15870 | 922 | lemma rev_swap: "(rev xs = ys) = (xs = rev ys)" | 
| 923 | by auto | |
| 924 | ||
| 13142 | 925 | lemma rev_is_Nil_conv [iff]: "(rev xs = []) = (xs = [])" | 
| 13145 | 926 | by (induct xs) auto | 
| 13114 | 927 | |
| 13142 | 928 | lemma Nil_is_rev_conv [iff]: "([] = rev xs) = (xs = [])" | 
| 13145 | 929 | by (induct xs) auto | 
| 13114 | 930 | |
| 15870 | 931 | lemma rev_singleton_conv [simp]: "(rev xs = [x]) = (xs = [x])" | 
| 932 | by (cases xs) auto | |
| 933 | ||
| 934 | lemma singleton_rev_conv [simp]: "([x] = rev xs) = (xs = [x])" | |
| 935 | by (cases xs) auto | |
| 936 | ||
| 46439 
2388be11cb50
removed fact that confuses SPASS -- better rely on "rev_rev_ident", which is stronger and more ATP friendly
 blanchet parents: 
46424diff
changeset | 937 | lemma rev_is_rev_conv [iff, no_atp]: "(rev xs = rev ys) = (xs = ys)" | 
| 21061 
580dfc999ef6
added normal post setup; cleaned up "execution" constants
 haftmann parents: 
21046diff
changeset | 938 | apply (induct xs arbitrary: ys, force) | 
| 14208 | 939 | apply (case_tac ys, simp, force) | 
| 13145 | 940 | done | 
| 13114 | 941 | |
| 15439 | 942 | lemma inj_on_rev[iff]: "inj_on rev A" | 
| 943 | by(simp add:inj_on_def) | |
| 944 | ||
| 13366 | 945 | lemma rev_induct [case_names Nil snoc]: | 
| 946 | "[| 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 | 947 | apply(simplesubst rev_rev_ident[symmetric]) | 
| 13145 | 948 | apply(rule_tac list = "rev xs" in list.induct, simp_all) | 
| 949 | done | |
| 13114 | 950 | |
| 13366 | 951 | lemma rev_exhaust [case_names Nil snoc]: | 
| 952 | "(xs = [] ==> P) ==>(!!ys y. xs = ys @ [y] ==> P) ==> P" | |
| 13145 | 953 | by (induct xs rule: rev_induct) auto | 
| 13114 | 954 | |
| 13366 | 955 | lemmas rev_cases = rev_exhaust | 
| 956 | ||
| 18423 | 957 | lemma rev_eq_Cons_iff[iff]: "(rev xs = y#ys) = (xs = rev ys @ [y])" | 
| 958 | by(rule rev_cases[of xs]) auto | |
| 959 | ||
| 13114 | 960 | |
| 15392 | 961 | subsubsection {* @{text set} *}
 | 
| 13114 | 962 | |
| 46698 
f1dfcf8be88d
converting "set [...]" to "{...}" in evaluation results
 nipkow parents: 
46669diff
changeset | 963 | declare set.simps [code_post] --"pretty output" | 
| 
f1dfcf8be88d
converting "set [...]" to "{...}" in evaluation results
 nipkow parents: 
46669diff
changeset | 964 | |
| 13142 | 965 | lemma finite_set [iff]: "finite (set xs)" | 
| 13145 | 966 | by (induct xs) auto | 
| 13114 | 967 | |
| 13142 | 968 | lemma set_append [simp]: "set (xs @ ys) = (set xs \<union> set ys)" | 
| 13145 | 969 | by (induct xs) auto | 
| 13114 | 970 | |
| 17830 | 971 | lemma hd_in_set[simp]: "xs \<noteq> [] \<Longrightarrow> hd xs : set xs" | 
| 972 | by(cases xs) auto | |
| 14099 | 973 | |
| 13142 | 974 | lemma set_subset_Cons: "set xs \<subseteq> set (x # xs)" | 
| 13145 | 975 | by auto | 
| 13114 | 976 | |
| 14099 | 977 | lemma set_ConsD: "y \<in> set (x # xs) \<Longrightarrow> y=x \<or> y \<in> set xs" | 
| 978 | by auto | |
| 979 | ||
| 13142 | 980 | lemma set_empty [iff]: "(set xs = {}) = (xs = [])"
 | 
| 13145 | 981 | by (induct xs) auto | 
| 13114 | 982 | |
| 15245 | 983 | lemma set_empty2[iff]: "({} = set xs) = (xs = [])"
 | 
| 984 | by(induct xs) auto | |
| 985 | ||
| 13142 | 986 | lemma set_rev [simp]: "set (rev xs) = set xs" | 
| 13145 | 987 | by (induct xs) auto | 
| 13114 | 988 | |
| 13142 | 989 | lemma set_map [simp]: "set (map f xs) = f`(set xs)" | 
| 13145 | 990 | by (induct xs) auto | 
| 13114 | 991 | |
| 13142 | 992 | lemma set_filter [simp]: "set (filter P xs) = {x. x : set xs \<and> P x}"
 | 
| 13145 | 993 | by (induct xs) auto | 
| 13114 | 994 | |
| 32417 | 995 | 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 | 996 | by (induct j) auto | 
| 13114 | 997 | |
| 13142 | 998 | |
| 25221 
5ded95dda5df
append/member: more light-weight way to declare authentic syntax;
 wenzelm parents: 
25215diff
changeset | 999 | lemma split_list: "x : set xs \<Longrightarrow> \<exists>ys zs. xs = ys @ x # zs" | 
| 18049 | 1000 | proof (induct xs) | 
| 26073 | 1001 | case Nil thus ?case by simp | 
| 1002 | next | |
| 1003 | case Cons thus ?case by (auto intro: Cons_eq_appendI) | |
| 1004 | qed | |
| 1005 | ||
| 26734 | 1006 | lemma in_set_conv_decomp: "x \<in> set xs \<longleftrightarrow> (\<exists>ys zs. xs = ys @ x # zs)" | 
| 1007 | by (auto elim: split_list) | |
| 26073 | 1008 | |
| 1009 | lemma split_list_first: "x : set xs \<Longrightarrow> \<exists>ys zs. xs = ys @ x # zs \<and> x \<notin> set ys" | |
| 1010 | proof (induct xs) | |
| 1011 | case Nil thus ?case by simp | |
| 18049 | 1012 | next | 
| 1013 | case (Cons a xs) | |
| 1014 | show ?case | |
| 1015 | proof cases | |
| 44890 
22f665a2e91c
new fastforce replacing fastsimp - less confusing name
 nipkow parents: 
44635diff
changeset | 1016 | assume "x = a" thus ?case using Cons by fastforce | 
| 18049 | 1017 | next | 
| 44890 
22f665a2e91c
new fastforce replacing fastsimp - less confusing name
 nipkow parents: 
44635diff
changeset | 1018 | assume "x \<noteq> a" thus ?case using Cons by(fastforce intro!: Cons_eq_appendI) | 
| 26073 | 1019 | qed | 
| 1020 | qed | |
| 1021 | ||
| 1022 | lemma in_set_conv_decomp_first: | |
| 1023 | "(x : set xs) = (\<exists>ys zs. xs = ys @ x # zs \<and> x \<notin> set ys)" | |
| 26734 | 1024 | by (auto dest!: split_list_first) | 
| 26073 | 1025 | |
| 40122 
1d8ad2ff3e01
dropped (almost) redundant distinct.induct rule; distinct_simps again named distinct.simps
 haftmann parents: 
40077diff
changeset | 1026 | 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 | 1027 | proof (induct xs rule: rev_induct) | 
| 26073 | 1028 | case Nil thus ?case by simp | 
| 1029 | next | |
| 1030 | case (snoc a xs) | |
| 1031 | show ?case | |
| 1032 | proof cases | |
| 40122 
1d8ad2ff3e01
dropped (almost) redundant distinct.induct rule; distinct_simps again named distinct.simps
 haftmann parents: 
40077diff
changeset | 1033 | assume "x = a" thus ?case using snoc by (metis List.set.simps(1) emptyE) | 
| 26073 | 1034 | next | 
| 44890 
22f665a2e91c
new fastforce replacing fastsimp - less confusing name
 nipkow parents: 
44635diff
changeset | 1035 | assume "x \<noteq> a" thus ?case using snoc by fastforce | 
| 18049 | 1036 | qed | 
| 1037 | qed | |
| 1038 | ||
| 26073 | 1039 | lemma in_set_conv_decomp_last: | 
| 1040 | "(x : set xs) = (\<exists>ys zs. xs = ys @ x # zs \<and> x \<notin> set zs)" | |
| 26734 | 1041 | by (auto dest!: split_list_last) | 
| 26073 | 1042 | |
| 1043 | lemma split_list_prop: "\<exists>x \<in> set xs. P x \<Longrightarrow> \<exists>ys x zs. xs = ys @ x # zs & P x" | |
| 1044 | proof (induct xs) | |
| 1045 | case Nil thus ?case by simp | |
| 1046 | next | |
| 1047 | case Cons thus ?case | |
| 1048 | by(simp add:Bex_def)(metis append_Cons append.simps(1)) | |
| 1049 | qed | |
| 1050 | ||
| 1051 | lemma split_list_propE: | |
| 26734 | 1052 | assumes "\<exists>x \<in> set xs. P x" | 
| 1053 | obtains ys x zs where "xs = ys @ x # zs" and "P x" | |
| 1054 | using split_list_prop [OF assms] by blast | |
| 26073 | 1055 | |
| 1056 | lemma split_list_first_prop: | |
| 1057 | "\<exists>x \<in> set xs. P x \<Longrightarrow> | |
| 1058 | \<exists>ys x zs. xs = ys@x#zs \<and> P x \<and> (\<forall>y \<in> set ys. \<not> P y)" | |
| 26734 | 1059 | proof (induct xs) | 
| 26073 | 1060 | case Nil thus ?case by simp | 
| 1061 | next | |
| 1062 | case (Cons x xs) | |
| 1063 | show ?case | |
| 1064 | proof cases | |
| 1065 | assume "P x" | |
| 40122 
1d8ad2ff3e01
dropped (almost) redundant distinct.induct rule; distinct_simps again named distinct.simps
 haftmann parents: 
40077diff
changeset | 1066 | thus ?thesis by simp (metis Un_upper1 contra_subsetD in_set_conv_decomp_first self_append_conv2 set_append) | 
| 26073 | 1067 | next | 
| 1068 | assume "\<not> P x" | |
| 1069 | hence "\<exists>x\<in>set xs. P x" using Cons(2) by simp | |
| 1070 | thus ?thesis using `\<not> P x` Cons(1) by (metis append_Cons set_ConsD) | |
| 1071 | qed | |
| 1072 | qed | |
| 1073 | ||
| 1074 | lemma split_list_first_propE: | |
| 26734 | 1075 | assumes "\<exists>x \<in> set xs. P x" | 
| 1076 | obtains ys x zs where "xs = ys @ x # zs" and "P x" and "\<forall>y \<in> set ys. \<not> P y" | |
| 1077 | using split_list_first_prop [OF assms] by blast | |
| 26073 | 1078 | |
| 1079 | lemma split_list_first_prop_iff: | |
| 1080 | "(\<exists>x \<in> set xs. P x) \<longleftrightarrow> | |
| 1081 | (\<exists>ys x zs. xs = ys@x#zs \<and> P x \<and> (\<forall>y \<in> set ys. \<not> P y))" | |
| 26734 | 1082 | by (rule, erule split_list_first_prop) auto | 
| 26073 | 1083 | |
| 1084 | lemma split_list_last_prop: | |
| 1085 | "\<exists>x \<in> set xs. P x \<Longrightarrow> | |
| 1086 | \<exists>ys x zs. xs = ys@x#zs \<and> P x \<and> (\<forall>z \<in> set zs. \<not> P z)" | |
| 1087 | proof(induct xs rule:rev_induct) | |
| 1088 | case Nil thus ?case by simp | |
| 1089 | next | |
| 1090 | case (snoc x xs) | |
| 1091 | show ?case | |
| 1092 | proof cases | |
| 1093 | assume "P x" thus ?thesis by (metis emptyE set_empty) | |
| 1094 | next | |
| 1095 | assume "\<not> P x" | |
| 1096 | 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 | 1097 | thus ?thesis using `\<not> P x` snoc(1) by fastforce | 
| 26073 | 1098 | qed | 
| 1099 | qed | |
| 1100 | ||
| 1101 | lemma split_list_last_propE: | |
| 26734 | 1102 | assumes "\<exists>x \<in> set xs. P x" | 
| 1103 | obtains ys x zs where "xs = ys @ x # zs" and "P x" and "\<forall>z \<in> set zs. \<not> P z" | |
| 1104 | using split_list_last_prop [OF assms] by blast | |
| 26073 | 1105 | |
| 1106 | lemma split_list_last_prop_iff: | |
| 1107 | "(\<exists>x \<in> set xs. P x) \<longleftrightarrow> | |
| 1108 | (\<exists>ys x zs. xs = ys@x#zs \<and> P x \<and> (\<forall>z \<in> set zs. \<not> P z))" | |
| 26734 | 1109 | by (metis split_list_last_prop [where P=P] in_set_conv_decomp) | 
| 26073 | 1110 | |
| 1111 | lemma finite_list: "finite A ==> EX xs. set xs = A" | |
| 26734 | 1112 | by (erule finite_induct) | 
| 1113 | (auto simp add: set.simps(2) [symmetric] simp del: set.simps(2)) | |
| 13508 | 1114 | |
| 14388 | 1115 | lemma card_length: "card (set xs) \<le> length xs" | 
| 1116 | by (induct xs) (auto simp add: card_insert_if) | |
| 13114 | 1117 | |
| 26442 
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
 haftmann parents: 
26300diff
changeset | 1118 | lemma set_minus_filter_out: | 
| 
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
 haftmann parents: 
26300diff
changeset | 1119 |   "set xs - {y} = set (filter (\<lambda>x. \<not> (x = y)) xs)"
 | 
| 
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
 haftmann parents: 
26300diff
changeset | 1120 | by (induct xs) auto | 
| 15168 | 1121 | |
| 35115 | 1122 | |
| 15392 | 1123 | subsubsection {* @{text filter} *}
 | 
| 13114 | 1124 | |
| 13142 | 1125 | lemma filter_append [simp]: "filter P (xs @ ys) = filter P xs @ filter P ys" | 
| 13145 | 1126 | by (induct xs) auto | 
| 13114 | 1127 | |
| 15305 | 1128 | lemma rev_filter: "rev (filter P xs) = filter P (rev xs)" | 
| 1129 | by (induct xs) simp_all | |
| 1130 | ||
| 13142 | 1131 | lemma filter_filter [simp]: "filter P (filter Q xs) = filter (\<lambda>x. Q x \<and> P x) xs" | 
| 13145 | 1132 | by (induct xs) auto | 
| 13114 | 1133 | |
| 16998 | 1134 | lemma length_filter_le [simp]: "length (filter P xs) \<le> length xs" | 
| 1135 | by (induct xs) (auto simp add: le_SucI) | |
| 1136 | ||
| 18423 | 1137 | lemma sum_length_filter_compl: | 
| 1138 | "length(filter P xs) + length(filter (%x. ~P x) xs) = length xs" | |
| 1139 | by(induct xs) simp_all | |
| 1140 | ||
| 13142 | 1141 | lemma filter_True [simp]: "\<forall>x \<in> set xs. P x ==> filter P xs = xs" | 
| 13145 | 1142 | by (induct xs) auto | 
| 13114 | 1143 | |
| 13142 | 1144 | lemma filter_False [simp]: "\<forall>x \<in> set xs. \<not> P x ==> filter P xs = []" | 
| 13145 | 1145 | by (induct xs) auto | 
| 13114 | 1146 | |
| 16998 | 1147 | lemma filter_empty_conv: "(filter P xs = []) = (\<forall>x\<in>set xs. \<not> P x)" | 
| 24349 | 1148 | by (induct xs) simp_all | 
| 16998 | 1149 | |
| 1150 | lemma filter_id_conv: "(filter P xs = xs) = (\<forall>x\<in>set xs. P x)" | |
| 1151 | apply (induct xs) | |
| 1152 | apply auto | |
| 1153 | apply(cut_tac P=P and xs=xs in length_filter_le) | |
| 1154 | apply simp | |
| 1155 | done | |
| 13114 | 1156 | |
| 16965 | 1157 | lemma filter_map: | 
| 1158 | "filter P (map f xs) = map f (filter (P o f) xs)" | |
| 1159 | by (induct xs) simp_all | |
| 1160 | ||
| 1161 | lemma length_filter_map[simp]: | |
| 1162 | "length (filter P (map f xs)) = length(filter (P o f) xs)" | |
| 1163 | by (simp add:filter_map) | |
| 1164 | ||
| 13142 | 1165 | lemma filter_is_subset [simp]: "set (filter P xs) \<le> set xs" | 
| 13145 | 1166 | by auto | 
| 13114 | 1167 | |
| 15246 | 1168 | lemma length_filter_less: | 
| 1169 | "\<lbrakk> x : set xs; ~ P x \<rbrakk> \<Longrightarrow> length(filter P xs) < length xs" | |
| 1170 | proof (induct xs) | |
| 1171 | case Nil thus ?case by simp | |
| 1172 | next | |
| 1173 | case (Cons x xs) thus ?case | |
| 1174 | apply (auto split:split_if_asm) | |
| 1175 | using length_filter_le[of P xs] apply arith | |
| 1176 | done | |
| 1177 | qed | |
| 13114 | 1178 | |
| 15281 | 1179 | lemma length_filter_conv_card: | 
| 1180 |  "length(filter p xs) = card{i. i < length xs & p(xs!i)}"
 | |
| 1181 | proof (induct xs) | |
| 1182 | case Nil thus ?case by simp | |
| 1183 | next | |
| 1184 | case (Cons x xs) | |
| 1185 |   let ?S = "{i. i < length xs & p(xs!i)}"
 | |
| 1186 | have fin: "finite ?S" by(fast intro: bounded_nat_set_is_finite) | |
| 1187 | show ?case (is "?l = card ?S'") | |
| 1188 | proof (cases) | |
| 1189 | assume "p x" | |
| 1190 | hence eq: "?S' = insert 0 (Suc ` ?S)" | |
| 25162 | 1191 | by(auto simp: image_def split:nat.split dest:gr0_implies_Suc) | 
| 15281 | 1192 | have "length (filter p (x # xs)) = Suc(card ?S)" | 
| 23388 | 1193 | using Cons `p x` by simp | 
| 15281 | 1194 | also have "\<dots> = Suc(card(Suc ` ?S))" using fin | 
| 44921 | 1195 | by (simp add: card_image) | 
| 15281 | 1196 | also have "\<dots> = card ?S'" using eq fin | 
| 1197 | by (simp add:card_insert_if) (simp add:image_def) | |
| 1198 | finally show ?thesis . | |
| 1199 | next | |
| 1200 | assume "\<not> p x" | |
| 1201 | hence eq: "?S' = Suc ` ?S" | |
| 25162 | 1202 | by(auto simp add: image_def split:nat.split elim:lessE) | 
| 15281 | 1203 | have "length (filter p (x # xs)) = card ?S" | 
| 23388 | 1204 | using Cons `\<not> p x` by simp | 
| 15281 | 1205 | also have "\<dots> = card(Suc ` ?S)" using fin | 
| 44921 | 1206 | by (simp add: card_image) | 
| 15281 | 1207 | also have "\<dots> = card ?S'" using eq fin | 
| 1208 | by (simp add:card_insert_if) | |
| 1209 | finally show ?thesis . | |
| 1210 | qed | |
| 1211 | qed | |
| 1212 | ||
| 17629 | 1213 | lemma Cons_eq_filterD: | 
| 1214 | "x#xs = filter P ys \<Longrightarrow> | |
| 1215 | \<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 | 1216 | (is "_ \<Longrightarrow> \<exists>us vs. ?P ys us vs") | 
| 17629 | 1217 | proof(induct ys) | 
| 1218 | case Nil thus ?case by simp | |
| 1219 | next | |
| 1220 | case (Cons y ys) | |
| 1221 | show ?case (is "\<exists>x. ?Q x") | |
| 1222 | proof cases | |
| 1223 | assume Py: "P y" | |
| 1224 | show ?thesis | |
| 1225 | proof cases | |
| 25221 
5ded95dda5df
append/member: more light-weight way to declare authentic syntax;
 wenzelm parents: 
25215diff
changeset | 1226 | assume "x = y" | 
| 
5ded95dda5df
append/member: more light-weight way to declare authentic syntax;
 wenzelm parents: 
25215diff
changeset | 1227 | with Py Cons.prems have "?Q []" by simp | 
| 
5ded95dda5df
append/member: more light-weight way to declare authentic syntax;
 wenzelm parents: 
25215diff
changeset | 1228 | then show ?thesis .. | 
| 17629 | 1229 | next | 
| 25221 
5ded95dda5df
append/member: more light-weight way to declare authentic syntax;
 wenzelm parents: 
25215diff
changeset | 1230 | assume "x \<noteq> y" | 
| 
5ded95dda5df
append/member: more light-weight way to declare authentic syntax;
 wenzelm parents: 
25215diff
changeset | 1231 | with Py Cons.prems show ?thesis by simp | 
| 17629 | 1232 | qed | 
| 1233 | next | |
| 25221 
5ded95dda5df
append/member: more light-weight way to declare authentic syntax;
 wenzelm parents: 
25215diff
changeset | 1234 | assume "\<not> P y" | 
| 44890 
22f665a2e91c
new fastforce replacing fastsimp - less confusing name
 nipkow parents: 
44635diff
changeset | 1235 | 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 | 1236 | then have "?Q (y#us)" by simp | 
| 
5ded95dda5df
append/member: more light-weight way to declare authentic syntax;
 wenzelm parents: 
25215diff
changeset | 1237 | then show ?thesis .. | 
| 17629 | 1238 | qed | 
| 1239 | qed | |
| 1240 | ||
| 1241 | lemma filter_eq_ConsD: | |
| 1242 | "filter P ys = x#xs \<Longrightarrow> | |
| 1243 | \<exists>us vs. ys = us @ x # vs \<and> (\<forall>u\<in>set us. \<not> P u) \<and> P x \<and> xs = filter P vs" | |
| 1244 | by(rule Cons_eq_filterD) simp | |
| 1245 | ||
| 1246 | lemma filter_eq_Cons_iff: | |
| 1247 | "(filter P ys = x#xs) = | |
| 1248 | (\<exists>us vs. ys = us @ x # vs \<and> (\<forall>u\<in>set us. \<not> P u) \<and> P x \<and> xs = filter P vs)" | |
| 1249 | by(auto dest:filter_eq_ConsD) | |
| 1250 | ||
| 1251 | lemma Cons_eq_filter_iff: | |
| 1252 | "(x#xs = filter P ys) = | |
| 1253 | (\<exists>us vs. ys = us @ x # vs \<and> (\<forall>u\<in>set us. \<not> P u) \<and> P x \<and> xs = filter P vs)" | |
| 1254 | by(auto dest:Cons_eq_filterD) | |
| 1255 | ||
| 44013 
5cfc1c36ae97
moved recdef package to HOL/Library/Old_Recdef.thy
 krauss parents: 
43594diff
changeset | 1256 | lemma filter_cong[fundef_cong]: | 
| 17501 | 1257 | "xs = ys \<Longrightarrow> (\<And>x. x \<in> set ys \<Longrightarrow> P x = Q x) \<Longrightarrow> filter P xs = filter Q ys" | 
| 1258 | apply simp | |
| 1259 | apply(erule thin_rl) | |
| 1260 | by (induct ys) simp_all | |
| 1261 | ||
| 15281 | 1262 | |
| 26442 
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
 haftmann parents: 
26300diff
changeset | 1263 | subsubsection {* List partitioning *}
 | 
| 
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
 haftmann parents: 
26300diff
changeset | 1264 | |
| 
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
 haftmann parents: 
26300diff
changeset | 1265 | primrec partition :: "('a \<Rightarrow> bool) \<Rightarrow>'a list \<Rightarrow> 'a list \<times> 'a list" where
 | 
| 
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
 haftmann parents: 
26300diff
changeset | 1266 | "partition P [] = ([], [])" | 
| 
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
 haftmann parents: 
26300diff
changeset | 1267 | | "partition P (x # xs) = | 
| 
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
 haftmann parents: 
26300diff
changeset | 1268 | (let (yes, no) = partition P xs | 
| 
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
 haftmann parents: 
26300diff
changeset | 1269 | in if P x then (x # yes, no) else (yes, x # no))" | 
| 
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
 haftmann parents: 
26300diff
changeset | 1270 | |
| 
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
 haftmann parents: 
26300diff
changeset | 1271 | lemma partition_filter1: | 
| 
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
 haftmann parents: 
26300diff
changeset | 1272 | "fst (partition P xs) = filter P xs" | 
| 
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
 haftmann parents: 
26300diff
changeset | 1273 | by (induct xs) (auto simp add: Let_def split_def) | 
| 
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
 haftmann parents: 
26300diff
changeset | 1274 | |
| 
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
 haftmann parents: 
26300diff
changeset | 1275 | lemma partition_filter2: | 
| 
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
 haftmann parents: 
26300diff
changeset | 1276 | "snd (partition P xs) = filter (Not o P) xs" | 
| 
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
 haftmann parents: 
26300diff
changeset | 1277 | by (induct xs) (auto simp add: Let_def split_def) | 
| 
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
 haftmann parents: 
26300diff
changeset | 1278 | |
| 
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
 haftmann parents: 
26300diff
changeset | 1279 | lemma partition_P: | 
| 
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
 haftmann parents: 
26300diff
changeset | 1280 | assumes "partition P xs = (yes, no)" | 
| 
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
 haftmann parents: 
26300diff
changeset | 1281 | 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 | 1282 | proof - | 
| 
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
 haftmann parents: 
26300diff
changeset | 1283 | 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 | 1284 | by simp_all | 
| 
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
 haftmann parents: 
26300diff
changeset | 1285 | then show ?thesis by (simp_all add: partition_filter1 partition_filter2) | 
| 
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
 haftmann parents: 
26300diff
changeset | 1286 | qed | 
| 
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
 haftmann parents: 
26300diff
changeset | 1287 | |
| 
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
 haftmann parents: 
26300diff
changeset | 1288 | lemma partition_set: | 
| 
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
 haftmann parents: 
26300diff
changeset | 1289 | assumes "partition P xs = (yes, no)" | 
| 
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
 haftmann parents: 
26300diff
changeset | 1290 | shows "set yes \<union> set no = set xs" | 
| 
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
 haftmann parents: 
26300diff
changeset | 1291 | proof - | 
| 
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
 haftmann parents: 
26300diff
changeset | 1292 | 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 | 1293 | by simp_all | 
| 
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
 haftmann parents: 
26300diff
changeset | 1294 | then show ?thesis by (auto simp add: partition_filter1 partition_filter2) | 
| 
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
 haftmann parents: 
26300diff
changeset | 1295 | qed | 
| 
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
 haftmann parents: 
26300diff
changeset | 1296 | |
| 33639 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 1297 | 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 | 1298 | "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 | 1299 | 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 | 1300 | 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 | 1301 | |
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 1302 | declare partition.simps[simp del] | 
| 26442 
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
 haftmann parents: 
26300diff
changeset | 1303 | |
| 35115 | 1304 | |
| 15392 | 1305 | subsubsection {* @{text concat} *}
 | 
| 13114 | 1306 | |
| 13142 | 1307 | lemma concat_append [simp]: "concat (xs @ ys) = concat xs @ concat ys" | 
| 13145 | 1308 | by (induct xs) auto | 
| 13114 | 1309 | |
| 18447 | 1310 | lemma concat_eq_Nil_conv [simp]: "(concat xss = []) = (\<forall>xs \<in> set xss. xs = [])" | 
| 13145 | 1311 | by (induct xss) auto | 
| 13114 | 1312 | |
| 18447 | 1313 | lemma Nil_eq_concat_conv [simp]: "([] = concat xss) = (\<forall>xs \<in> set xss. xs = [])" | 
| 13145 | 1314 | by (induct xss) auto | 
| 13114 | 1315 | |
| 24308 | 1316 | lemma set_concat [simp]: "set (concat xs) = (UN x:set xs. set x)" | 
| 13145 | 1317 | by (induct xs) auto | 
| 13114 | 1318 | |
| 24476 
f7ad9fbbeeaa
turned list comprehension translations into ML to optimize base case
 nipkow parents: 
24471diff
changeset | 1319 | lemma concat_map_singleton[simp]: "concat(map (%x. [f x]) xs) = map f xs" | 
| 24349 | 1320 | by (induct xs) auto | 
| 1321 | ||
| 13142 | 1322 | lemma map_concat: "map f (concat xs) = concat (map (map f) xs)" | 
| 13145 | 1323 | by (induct xs) auto | 
| 13114 | 1324 | |
| 13142 | 1325 | lemma filter_concat: "filter p (concat xs) = concat (map (filter p) xs)" | 
| 13145 | 1326 | by (induct xs) auto | 
| 13114 | 1327 | |
| 13142 | 1328 | lemma rev_concat: "rev (concat xs) = concat (map rev (rev xs))" | 
| 13145 | 1329 | by (induct xs) auto | 
| 13114 | 1330 | |
| 40365 
a1456f2e1c9d
added two lemmas about injectivity of concat to the list theory
 bulwahn parents: 
40304diff
changeset | 1331 | 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 | 1332 | proof (induct xs arbitrary: ys) | 
| 
a1456f2e1c9d
added two lemmas about injectivity of concat to the list theory
 bulwahn parents: 
40304diff
changeset | 1333 | case (Cons x xs ys) | 
| 
a1456f2e1c9d
added two lemmas about injectivity of concat to the list theory
 bulwahn parents: 
40304diff
changeset | 1334 | thus ?case by (cases ys) auto | 
| 
a1456f2e1c9d
added two lemmas about injectivity of concat to the list theory
 bulwahn parents: 
40304diff
changeset | 1335 | qed (auto) | 
| 
a1456f2e1c9d
added two lemmas about injectivity of concat to the list theory
 bulwahn parents: 
40304diff
changeset | 1336 | |
| 
a1456f2e1c9d
added two lemmas about injectivity of concat to the list theory
 bulwahn parents: 
40304diff
changeset | 1337 | 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 | 1338 | by (simp add: concat_eq_concat_iff) | 
| 
a1456f2e1c9d
added two lemmas about injectivity of concat to the list theory
 bulwahn parents: 
40304diff
changeset | 1339 | |
| 13114 | 1340 | |
| 15392 | 1341 | subsubsection {* @{text nth} *}
 | 
| 13114 | 1342 | |
| 29827 | 1343 | lemma nth_Cons_0 [simp, code]: "(x # xs)!0 = x" | 
| 13145 | 1344 | by auto | 
| 13114 | 1345 | |
| 29827 | 1346 | lemma nth_Cons_Suc [simp, code]: "(x # xs)!(Suc n) = xs!n" | 
| 13145 | 1347 | by auto | 
| 13114 | 1348 | |
| 13142 | 1349 | declare nth.simps [simp del] | 
| 13114 | 1350 | |
| 41842 | 1351 | lemma nth_Cons_pos[simp]: "0 < n \<Longrightarrow> (x#xs) ! n = xs ! (n - 1)" | 
| 1352 | by(auto simp: Nat.gr0_conv_Suc) | |
| 1353 | ||
| 13114 | 1354 | lemma nth_append: | 
| 24526 | 1355 | "(xs @ ys)!n = (if n < length xs then xs!n else ys!(n - length xs))" | 
| 1356 | apply (induct xs arbitrary: n, simp) | |
| 14208 | 1357 | apply (case_tac n, auto) | 
| 13145 | 1358 | done | 
| 13114 | 1359 | |
| 14402 
4201e1916482
moved lemmas from MicroJava/Comp/AuxLemmas.thy to List.thy
 nipkow parents: 
14395diff
changeset | 1360 | 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 | 1361 | by (induct xs) auto | 
| 14402 
4201e1916482
moved lemmas from MicroJava/Comp/AuxLemmas.thy to List.thy
 nipkow parents: 
14395diff
changeset | 1362 | |
| 
4201e1916482
moved lemmas from MicroJava/Comp/AuxLemmas.thy to List.thy
 nipkow parents: 
14395diff
changeset | 1363 | 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 | 1364 | by (induct xs) auto | 
| 14402 
4201e1916482
moved lemmas from MicroJava/Comp/AuxLemmas.thy to List.thy
 nipkow parents: 
14395diff
changeset | 1365 | |
| 24526 | 1366 | lemma nth_map [simp]: "n < length xs ==> (map f xs)!n = f(xs!n)" | 
| 1367 | apply (induct xs arbitrary: n, simp) | |
| 14208 | 1368 | apply (case_tac n, auto) | 
| 13145 | 1369 | done | 
| 13114 | 1370 | |
| 45841 | 1371 | lemma nth_tl: | 
| 1372 | assumes "n < length (tl x)" shows "tl x ! n = x ! Suc n" | |
| 1373 | using assms by (induct x) auto | |
| 1374 | ||
| 18423 | 1375 | lemma hd_conv_nth: "xs \<noteq> [] \<Longrightarrow> hd xs = xs!0" | 
| 1376 | by(cases xs) simp_all | |
| 1377 | ||
| 18049 | 1378 | |
| 1379 | lemma list_eq_iff_nth_eq: | |
| 24526 | 1380 | "(xs = ys) = (length xs = length ys \<and> (ALL i<length xs. xs!i = ys!i))" | 
| 1381 | apply(induct xs arbitrary: ys) | |
| 24632 | 1382 | apply force | 
| 18049 | 1383 | apply(case_tac ys) | 
| 1384 | apply simp | |
| 1385 | apply(simp add:nth_Cons split:nat.split)apply blast | |
| 1386 | done | |
| 1387 | ||
| 13142 | 1388 | lemma set_conv_nth: "set xs = {xs!i | i. i < length xs}"
 | 
| 15251 | 1389 | apply (induct xs, simp, simp) | 
| 13145 | 1390 | apply safe | 
| 24632 | 1391 | apply (metis nat_case_0 nth.simps zero_less_Suc) | 
| 1392 | apply (metis less_Suc_eq_0_disj nth_Cons_Suc) | |
| 14208 | 1393 | apply (case_tac i, simp) | 
| 24632 | 1394 | apply (metis diff_Suc_Suc nat_case_Suc nth.simps zero_less_diff) | 
| 13145 | 1395 | done | 
| 13114 | 1396 | |
| 17501 | 1397 | lemma in_set_conv_nth: "(x \<in> set xs) = (\<exists>i < length xs. xs!i = x)" | 
| 1398 | by(auto simp:set_conv_nth) | |
| 1399 | ||
| 13145 | 1400 | lemma list_ball_nth: "[| n < length xs; !x : set xs. P x|] ==> P(xs!n)" | 
| 1401 | by (auto simp add: set_conv_nth) | |
| 13114 | 1402 | |
| 13142 | 1403 | lemma nth_mem [simp]: "n < length xs ==> xs!n : set xs" | 
| 13145 | 1404 | by (auto simp add: set_conv_nth) | 
| 13114 | 1405 | |
| 1406 | lemma all_nth_imp_all_set: | |
| 13145 | 1407 | "[| !i < length xs. P(xs!i); x : set xs|] ==> P x" | 
| 1408 | by (auto simp add: set_conv_nth) | |
| 13114 | 1409 | |
| 1410 | lemma all_set_conv_all_nth: | |
| 13145 | 1411 | "(\<forall>x \<in> set xs. P x) = (\<forall>i. i < length xs --> P (xs ! i))" | 
| 1412 | by (auto simp add: set_conv_nth) | |
| 13114 | 1413 | |
| 25296 | 1414 | lemma rev_nth: | 
| 1415 | "n < size xs \<Longrightarrow> rev xs ! n = xs ! (length xs - Suc n)" | |
| 1416 | proof (induct xs arbitrary: n) | |
| 1417 | case Nil thus ?case by simp | |
| 1418 | next | |
| 1419 | case (Cons x xs) | |
| 1420 | hence n: "n < Suc (length xs)" by simp | |
| 1421 | moreover | |
| 1422 |   { assume "n < length xs"
 | |
| 1423 | with n obtain n' where "length xs - n = Suc n'" | |
| 1424 | by (cases "length xs - n", auto) | |
| 1425 | moreover | |
| 1426 | then have "length xs - Suc n = n'" by simp | |
| 1427 | ultimately | |
| 1428 | have "xs ! (length xs - Suc n) = (x # xs) ! (length xs - n)" by simp | |
| 1429 | } | |
| 1430 | ultimately | |
| 1431 | show ?case by (clarsimp simp add: Cons nth_append) | |
| 1432 | qed | |
| 13114 | 1433 | |
| 31159 | 1434 | lemma Skolem_list_nth: | 
| 1435 | "(ALL i<k. EX x. P i x) = (EX xs. size xs = k & (ALL i<k. P i (xs!i)))" | |
| 1436 | (is "_ = (EX xs. ?P k xs)") | |
| 1437 | proof(induct k) | |
| 1438 | case 0 show ?case by simp | |
| 1439 | next | |
| 1440 | case (Suc k) | |
| 1441 | show ?case (is "?L = ?R" is "_ = (EX xs. ?P' xs)") | |
| 1442 | proof | |
| 1443 | assume "?R" thus "?L" using Suc by auto | |
| 1444 | next | |
| 1445 | assume "?L" | |
| 1446 | with Suc obtain x xs where "?P k xs & P k x" by (metis less_Suc_eq) | |
| 1447 | hence "?P'(xs@[x])" by(simp add:nth_append less_Suc_eq) | |
| 1448 | thus "?R" .. | |
| 1449 | qed | |
| 1450 | qed | |
| 1451 | ||
| 1452 | ||
| 15392 | 1453 | subsubsection {* @{text list_update} *}
 | 
| 13114 | 1454 | |
| 24526 | 1455 | lemma length_list_update [simp]: "length(xs[i:=x]) = length xs" | 
| 1456 | by (induct xs arbitrary: i) (auto split: nat.split) | |
| 13114 | 1457 | |
| 1458 | lemma nth_list_update: | |
| 24526 | 1459 | "i < length xs==> (xs[i:=x])!j = (if i = j then x else xs!j)" | 
| 1460 | by (induct xs arbitrary: i j) (auto simp add: nth_Cons split: nat.split) | |
| 13114 | 1461 | |
| 13142 | 1462 | lemma nth_list_update_eq [simp]: "i < length xs ==> (xs[i:=x])!i = x" | 
| 13145 | 1463 | by (simp add: nth_list_update) | 
| 13114 | 1464 | |
| 24526 | 1465 | lemma nth_list_update_neq [simp]: "i \<noteq> j ==> xs[i:=x]!j = xs!j" | 
| 1466 | by (induct xs arbitrary: i j) (auto simp add: nth_Cons split: nat.split) | |
| 13114 | 1467 | |
| 24526 | 1468 | lemma list_update_id[simp]: "xs[i := xs!i] = xs" | 
| 1469 | by (induct xs arbitrary: i) (simp_all split:nat.splits) | |
| 1470 | ||
| 1471 | lemma list_update_beyond[simp]: "length xs \<le> i \<Longrightarrow> xs[i:=x] = xs" | |
| 1472 | apply (induct xs arbitrary: i) | |
| 17501 | 1473 | apply simp | 
| 1474 | apply (case_tac i) | |
| 1475 | apply simp_all | |
| 1476 | done | |
| 1477 | ||
| 31077 | 1478 | lemma list_update_nonempty[simp]: "xs[k:=x] = [] \<longleftrightarrow> xs=[]" | 
| 1479 | by(metis length_0_conv length_list_update) | |
| 1480 | ||
| 13114 | 1481 | lemma list_update_same_conv: | 
| 24526 | 1482 | "i < length xs ==> (xs[i := x] = xs) = (xs!i = x)" | 
| 1483 | by (induct xs arbitrary: i) (auto split: nat.split) | |
| 13114 | 1484 | |
| 14187 | 1485 | lemma list_update_append1: | 
| 24526 | 1486 | "i < size xs \<Longrightarrow> (xs @ ys)[i:=x] = xs[i:=x] @ ys" | 
| 1487 | apply (induct xs arbitrary: i, simp) | |
| 14187 | 1488 | apply(simp split:nat.split) | 
| 1489 | done | |
| 1490 | ||
| 15868 | 1491 | lemma list_update_append: | 
| 24526 | 1492 | "(xs @ ys) [n:= x] = | 
| 15868 | 1493 | (if n < length xs then xs[n:= x] @ ys else xs @ (ys [n-length xs:= x]))" | 
| 24526 | 1494 | by (induct xs arbitrary: n) (auto split:nat.splits) | 
| 15868 | 1495 | |
| 14402 
4201e1916482
moved lemmas from MicroJava/Comp/AuxLemmas.thy to List.thy
 nipkow parents: 
14395diff
changeset | 1496 | lemma list_update_length [simp]: | 
| 
4201e1916482
moved lemmas from MicroJava/Comp/AuxLemmas.thy to List.thy
 nipkow parents: 
14395diff
changeset | 1497 | "(xs @ x # ys)[length xs := y] = (xs @ y # ys)" | 
| 
4201e1916482
moved lemmas from MicroJava/Comp/AuxLemmas.thy to List.thy
 nipkow parents: 
14395diff
changeset | 1498 | by (induct xs, auto) | 
| 
4201e1916482
moved lemmas from MicroJava/Comp/AuxLemmas.thy to List.thy
 nipkow parents: 
14395diff
changeset | 1499 | |
| 31264 | 1500 | lemma map_update: "map f (xs[k:= y]) = (map f xs)[k := f y]" | 
| 1501 | by(induct xs arbitrary: k)(auto split:nat.splits) | |
| 1502 | ||
| 1503 | lemma rev_update: | |
| 1504 | "k < length xs \<Longrightarrow> rev (xs[k:= y]) = (rev xs)[length xs - k - 1 := y]" | |
| 1505 | by (induct xs arbitrary: k) (auto simp: list_update_append split:nat.splits) | |
| 1506 | ||
| 13114 | 1507 | lemma update_zip: | 
| 31080 | 1508 | "(zip xs ys)[i:=xy] = zip (xs[i:=fst xy]) (ys[i:=snd xy])" | 
| 24526 | 1509 | by (induct ys arbitrary: i xy xs) (auto, case_tac xs, auto split: nat.split) | 
| 1510 | ||
| 1511 | lemma set_update_subset_insert: "set(xs[i:=x]) <= insert x (set xs)" | |
| 1512 | by (induct xs arbitrary: i) (auto split: nat.split) | |
| 13114 | 1513 | |
| 1514 | lemma set_update_subsetI: "[| set xs <= A; x:A |] ==> set(xs[i := x]) <= A" | |
| 13145 | 1515 | by (blast dest!: set_update_subset_insert [THEN subsetD]) | 
| 13114 | 1516 | |
| 24526 | 1517 | lemma set_update_memI: "n < length xs \<Longrightarrow> x \<in> set (xs[n := x])" | 
| 1518 | by (induct xs arbitrary: n) (auto split:nat.splits) | |
| 15868 | 1519 | |
| 31077 | 1520 | lemma list_update_overwrite[simp]: | 
| 24796 | 1521 | "xs [i := x, i := y] = xs [i := y]" | 
| 31077 | 1522 | apply (induct xs arbitrary: i) apply simp | 
| 1523 | apply (case_tac i, simp_all) | |
| 24796 | 1524 | done | 
| 1525 | ||
| 1526 | lemma list_update_swap: | |
| 1527 | "i \<noteq> i' \<Longrightarrow> xs [i := x, i' := x'] = xs [i' := x', i := x]" | |
| 1528 | apply (induct xs arbitrary: i i') | |
| 1529 | apply simp | |
| 1530 | apply (case_tac i, case_tac i') | |
| 1531 | apply auto | |
| 1532 | apply (case_tac i') | |
| 1533 | apply auto | |
| 1534 | done | |
| 1535 | ||
| 29827 | 1536 | lemma list_update_code [code]: | 
| 1537 | "[][i := y] = []" | |
| 1538 | "(x # xs)[0 := y] = y # xs" | |
| 1539 | "(x # xs)[Suc i := y] = x # xs[i := y]" | |
| 1540 | by simp_all | |
| 1541 | ||
| 13114 | 1542 | |
| 15392 | 1543 | subsubsection {* @{text last} and @{text butlast} *}
 | 
| 13114 | 1544 | |
| 13142 | 1545 | lemma last_snoc [simp]: "last (xs @ [x]) = x" | 
| 13145 | 1546 | by (induct xs) auto | 
| 13114 | 1547 | |
| 13142 | 1548 | lemma butlast_snoc [simp]: "butlast (xs @ [x]) = xs" | 
| 13145 | 1549 | by (induct xs) auto | 
| 13114 | 1550 | |
| 14302 | 1551 | lemma last_ConsL: "xs = [] \<Longrightarrow> last(x#xs) = x" | 
| 44921 | 1552 | by simp | 
| 14302 | 1553 | |
| 1554 | lemma last_ConsR: "xs \<noteq> [] \<Longrightarrow> last(x#xs) = last xs" | |
| 44921 | 1555 | by simp | 
| 14302 | 1556 | |
| 1557 | lemma last_append: "last(xs @ ys) = (if ys = [] then last xs else last ys)" | |
| 1558 | by (induct xs) (auto) | |
| 1559 | ||
| 1560 | lemma last_appendL[simp]: "ys = [] \<Longrightarrow> last(xs @ ys) = last xs" | |
| 1561 | by(simp add:last_append) | |
| 1562 | ||
| 1563 | lemma last_appendR[simp]: "ys \<noteq> [] \<Longrightarrow> last(xs @ ys) = last ys" | |
| 1564 | by(simp add:last_append) | |
| 1565 | ||
| 45841 | 1566 | lemma last_tl: "xs = [] \<or> tl xs \<noteq> [] \<Longrightarrow>last (tl xs) = last xs" | 
| 1567 | by (induct xs) simp_all | |
| 1568 | ||
| 1569 | lemma butlast_tl: "butlast (tl xs) = tl (butlast xs)" | |
| 1570 | by (induct xs) simp_all | |
| 1571 | ||
| 17762 | 1572 | lemma hd_rev: "xs \<noteq> [] \<Longrightarrow> hd(rev xs) = last xs" | 
| 1573 | by(rule rev_exhaust[of xs]) simp_all | |
| 1574 | ||
| 1575 | lemma last_rev: "xs \<noteq> [] \<Longrightarrow> last(rev xs) = hd xs" | |
| 1576 | by(cases xs) simp_all | |
| 1577 | ||
| 17765 | 1578 | lemma last_in_set[simp]: "as \<noteq> [] \<Longrightarrow> last as \<in> set as" | 
| 1579 | by (induct as) auto | |
| 17762 | 1580 | |
| 13142 | 1581 | lemma length_butlast [simp]: "length (butlast xs) = length xs - 1" | 
| 13145 | 1582 | by (induct xs rule: rev_induct) auto | 
| 13114 | 1583 | |
| 1584 | lemma butlast_append: | |
| 24526 | 1585 | "butlast (xs @ ys) = (if ys = [] then butlast xs else xs @ butlast ys)" | 
| 1586 | by (induct xs arbitrary: ys) auto | |
| 13114 | 1587 | |
| 13142 | 1588 | lemma append_butlast_last_id [simp]: | 
| 13145 | 1589 | "xs \<noteq> [] ==> butlast xs @ [last xs] = xs" | 
| 1590 | by (induct xs) auto | |
| 13114 | 1591 | |
| 13142 | 1592 | lemma in_set_butlastD: "x : set (butlast xs) ==> x : set xs" | 
| 13145 | 1593 | by (induct xs) (auto split: split_if_asm) | 
| 13114 | 1594 | |
| 1595 | lemma in_set_butlast_appendI: | |
| 13145 | 1596 | "x : set (butlast xs) | x : set (butlast ys) ==> x : set (butlast (xs @ ys))" | 
| 1597 | by (auto dest: in_set_butlastD simp add: butlast_append) | |
| 13114 | 1598 | |
| 24526 | 1599 | lemma last_drop[simp]: "n < length xs \<Longrightarrow> last (drop n xs) = last xs" | 
| 1600 | apply (induct xs arbitrary: n) | |
| 17501 | 1601 | apply simp | 
| 1602 | apply (auto split:nat.split) | |
| 1603 | done | |
| 1604 | ||
| 45841 | 1605 | lemma nth_butlast: | 
| 1606 | assumes "n < length (butlast xs)" shows "butlast xs ! n = xs ! n" | |
| 1607 | proof (cases xs) | |
| 1608 | case (Cons y ys) | |
| 1609 | moreover from assms have "butlast xs ! n = (butlast xs @ [last xs]) ! n" | |
| 1610 | by (simp add: nth_append) | |
| 1611 | ultimately show ?thesis using append_butlast_last_id by simp | |
| 1612 | qed simp | |
| 1613 | ||
| 30128 
365ee7319b86
revert some Suc 0 lemmas back to their original forms; added some simp rules for (1::nat)
 huffman parents: 
30079diff
changeset | 1614 | lemma last_conv_nth: "xs\<noteq>[] \<Longrightarrow> last xs = xs!(length xs - 1)" | 
| 17589 | 1615 | by(induct xs)(auto simp:neq_Nil_conv) | 
| 1616 | ||
| 30128 
365ee7319b86
revert some Suc 0 lemmas back to their original forms; added some simp rules for (1::nat)
 huffman parents: 
30079diff
changeset | 1617 | 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 | 1618 | by (induct xs, simp, case_tac xs, simp_all) | 
| 
46f3b89b2445
move lemmas from Word/BinBoolList.thy to List.thy
 huffman parents: 
26480diff
changeset | 1619 | |
| 31077 | 1620 | lemma last_list_update: | 
| 1621 | "xs \<noteq> [] \<Longrightarrow> last(xs[k:=x]) = (if k = size xs - 1 then x else last xs)" | |
| 1622 | by (auto simp: last_conv_nth) | |
| 1623 | ||
| 1624 | lemma butlast_list_update: | |
| 1625 | "butlast(xs[k:=x]) = | |
| 1626 | (if k = size xs - 1 then butlast xs else (butlast xs)[k:=x])" | |
| 1627 | apply(cases xs rule:rev_cases) | |
| 1628 | apply simp | |
| 1629 | apply(simp add:list_update_append split:nat.splits) | |
| 1630 | done | |
| 1631 | ||
| 36851 | 1632 | lemma last_map: | 
| 1633 | "xs \<noteq> [] \<Longrightarrow> last (map f xs) = f (last xs)" | |
| 1634 | by (cases xs rule: rev_cases) simp_all | |
| 1635 | ||
| 1636 | lemma map_butlast: | |
| 1637 | "map f (butlast xs) = butlast (map f xs)" | |
| 1638 | by (induct xs) simp_all | |
| 1639 | ||
| 40230 | 1640 | lemma snoc_eq_iff_butlast: | 
| 1641 | "xs @ [x] = ys \<longleftrightarrow> (ys \<noteq> [] & butlast ys = xs & last ys = x)" | |
| 1642 | by (metis append_butlast_last_id append_is_Nil_conv butlast_snoc last_snoc not_Cons_self) | |
| 1643 | ||
| 24796 | 1644 | |
| 15392 | 1645 | subsubsection {* @{text take} and @{text drop} *}
 | 
| 13114 | 1646 | |
| 13142 | 1647 | lemma take_0 [simp]: "take 0 xs = []" | 
| 13145 | 1648 | by (induct xs) auto | 
| 13114 | 1649 | |
| 13142 | 1650 | lemma drop_0 [simp]: "drop 0 xs = xs" | 
| 13145 | 1651 | by (induct xs) auto | 
| 13114 | 1652 | |
| 13142 | 1653 | lemma take_Suc_Cons [simp]: "take (Suc n) (x # xs) = x # take n xs" | 
| 13145 | 1654 | by simp | 
| 13114 | 1655 | |
| 13142 | 1656 | lemma drop_Suc_Cons [simp]: "drop (Suc n) (x # xs) = drop n xs" | 
| 13145 | 1657 | by simp | 
| 13114 | 1658 | |
| 13142 | 1659 | declare take_Cons [simp del] and drop_Cons [simp del] | 
| 13114 | 1660 | |
| 30128 
365ee7319b86
revert some Suc 0 lemmas back to their original forms; added some simp rules for (1::nat)
 huffman parents: 
30079diff
changeset | 1661 | 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 | 1662 | 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 | 1663 | |
| 
365ee7319b86
revert some Suc 0 lemmas back to their original forms; added some simp rules for (1::nat)
 huffman parents: 
30079diff
changeset | 1664 | 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 | 1665 | 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 | 1666 | |
| 15110 
78b5636eabc7
Added a number of new thms and the new function remove1
 nipkow parents: 
15072diff
changeset | 1667 | 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 | 1668 | by(clarsimp simp add:neq_Nil_conv) | 
| 
78b5636eabc7
Added a number of new thms and the new function remove1
 nipkow parents: 
15072diff
changeset | 1669 | |
| 14187 | 1670 | lemma drop_Suc: "drop (Suc n) xs = drop n (tl xs)" | 
| 1671 | by(cases xs, simp_all) | |
| 1672 | ||
| 26584 
46f3b89b2445
move lemmas from Word/BinBoolList.thy to List.thy
 huffman parents: 
26480diff
changeset | 1673 | 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 | 1674 | by (induct xs arbitrary: n) simp_all | 
| 
46f3b89b2445
move lemmas from Word/BinBoolList.thy to List.thy
 huffman parents: 
26480diff
changeset | 1675 | |
| 24526 | 1676 | lemma drop_tl: "drop n (tl xs) = tl(drop n xs)" | 
| 1677 | by(induct xs arbitrary: n, simp_all add:drop_Cons drop_Suc split:nat.split) | |
| 1678 | ||
| 26584 
46f3b89b2445
move lemmas from Word/BinBoolList.thy to List.thy
 huffman parents: 
26480diff
changeset | 1679 | 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 | 1680 | by (cases n, simp, cases xs, auto) | 
| 
46f3b89b2445
move lemmas from Word/BinBoolList.thy to List.thy
 huffman parents: 
26480diff
changeset | 1681 | |
| 
46f3b89b2445
move lemmas from Word/BinBoolList.thy to List.thy
 huffman parents: 
26480diff
changeset | 1682 | 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 | 1683 | by (simp only: drop_tl) | 
| 
46f3b89b2445
move lemmas from Word/BinBoolList.thy to List.thy
 huffman parents: 
26480diff
changeset | 1684 | |
| 24526 | 1685 | lemma nth_via_drop: "drop n xs = y#ys \<Longrightarrow> xs!n = y" | 
| 1686 | apply (induct xs arbitrary: n, simp) | |
| 14187 | 1687 | apply(simp add:drop_Cons nth_Cons split:nat.splits) | 
| 1688 | done | |
| 1689 | ||
| 13913 | 1690 | lemma take_Suc_conv_app_nth: | 
| 24526 | 1691 | "i < length xs \<Longrightarrow> take (Suc i) xs = take i xs @ [xs!i]" | 
| 1692 | apply (induct xs arbitrary: i, simp) | |
| 14208 | 1693 | apply (case_tac i, auto) | 
| 13913 | 1694 | done | 
| 1695 | ||
| 14591 | 1696 | lemma drop_Suc_conv_tl: | 
| 24526 | 1697 | "i < length xs \<Longrightarrow> (xs!i) # (drop (Suc i) xs) = drop i xs" | 
| 1698 | apply (induct xs arbitrary: i, simp) | |
| 14591 | 1699 | apply (case_tac i, auto) | 
| 1700 | done | |
| 1701 | ||
| 24526 | 1702 | lemma length_take [simp]: "length (take n xs) = min (length xs) n" | 
| 1703 | by (induct n arbitrary: xs) (auto, case_tac xs, auto) | |
| 1704 | ||
| 1705 | lemma length_drop [simp]: "length (drop n xs) = (length xs - n)" | |
| 1706 | by (induct n arbitrary: xs) (auto, case_tac xs, auto) | |
| 1707 | ||
| 1708 | lemma take_all [simp]: "length xs <= n ==> take n xs = xs" | |
| 1709 | by (induct n arbitrary: xs) (auto, case_tac xs, auto) | |
| 1710 | ||
| 1711 | lemma drop_all [simp]: "length xs <= n ==> drop n xs = []" | |
| 1712 | by (induct n arbitrary: xs) (auto, case_tac xs, auto) | |
| 13114 | 1713 | |
| 13142 | 1714 | lemma take_append [simp]: | 
| 24526 | 1715 | "take n (xs @ ys) = (take n xs @ take (n - length xs) ys)" | 
| 1716 | by (induct n arbitrary: xs) (auto, case_tac xs, auto) | |
| 13114 | 1717 | |
| 13142 | 1718 | lemma drop_append [simp]: | 
| 24526 | 1719 | "drop n (xs @ ys) = drop n xs @ drop (n - length xs) ys" | 
| 1720 | by (induct n arbitrary: xs) (auto, case_tac xs, auto) | |
| 1721 | ||
| 1722 | lemma take_take [simp]: "take n (take m xs) = take (min n m) xs" | |
| 1723 | apply (induct m arbitrary: xs n, auto) | |
| 14208 | 1724 | apply (case_tac xs, auto) | 
| 15236 
f289e8ba2bb3
Proofs needed to be updated because induction now preserves name of
 nipkow parents: 
15176diff
changeset | 1725 | apply (case_tac n, auto) | 
| 13145 | 1726 | done | 
| 13114 | 1727 | |
| 24526 | 1728 | lemma drop_drop [simp]: "drop n (drop m xs) = drop (n + m) xs" | 
| 1729 | apply (induct m arbitrary: xs, auto) | |
| 14208 | 1730 | apply (case_tac xs, auto) | 
| 13145 | 1731 | done | 
| 13114 | 1732 | |
| 24526 | 1733 | lemma take_drop: "take n (drop m xs) = drop m (take (n + m) xs)" | 
| 1734 | apply (induct m arbitrary: xs n, auto) | |
| 14208 | 1735 | apply (case_tac xs, auto) | 
| 13145 | 1736 | done | 
| 13114 | 1737 | |
| 24526 | 1738 | lemma drop_take: "drop n (take m xs) = take (m-n) (drop n xs)" | 
| 1739 | apply(induct xs arbitrary: m n) | |
| 14802 | 1740 | apply simp | 
| 1741 | apply(simp add: take_Cons drop_Cons split:nat.split) | |
| 1742 | done | |
| 1743 | ||
| 24526 | 1744 | lemma append_take_drop_id [simp]: "take n xs @ drop n xs = xs" | 
| 1745 | apply (induct n arbitrary: xs, auto) | |
| 14208 | 1746 | apply (case_tac xs, auto) | 
| 13145 | 1747 | done | 
| 13114 | 1748 | |
| 24526 | 1749 | lemma take_eq_Nil[simp]: "(take n xs = []) = (n = 0 \<or> xs = [])" | 
| 1750 | apply(induct xs arbitrary: n) | |
| 15110 
78b5636eabc7
Added a number of new thms and the new function remove1
 nipkow parents: 
15072diff
changeset | 1751 | apply simp | 
| 
78b5636eabc7
Added a number of new thms and the new function remove1
 nipkow parents: 
15072diff
changeset | 1752 | apply(simp add:take_Cons split:nat.split) | 
| 
78b5636eabc7
Added a number of new thms and the new function remove1
 nipkow parents: 
15072diff
changeset | 1753 | done | 
| 
78b5636eabc7
Added a number of new thms and the new function remove1
 nipkow parents: 
15072diff
changeset | 1754 | |
| 24526 | 1755 | lemma drop_eq_Nil[simp]: "(drop n xs = []) = (length xs <= n)" | 
| 1756 | apply(induct xs arbitrary: n) | |
| 15110 
78b5636eabc7
Added a number of new thms and the new function remove1
 nipkow parents: 
15072diff
changeset | 1757 | apply simp | 
| 
78b5636eabc7
Added a number of new thms and the new function remove1
 nipkow parents: 
15072diff
changeset | 1758 | apply(simp add:drop_Cons split:nat.split) | 
| 
78b5636eabc7
Added a number of new thms and the new function remove1
 nipkow parents: 
15072diff
changeset | 1759 | done | 
| 
78b5636eabc7
Added a number of new thms and the new function remove1
 nipkow parents: 
15072diff
changeset | 1760 | |
| 24526 | 1761 | lemma take_map: "take n (map f xs) = map f (take n xs)" | 
| 1762 | apply (induct n arbitrary: xs, auto) | |
| 14208 | 1763 | apply (case_tac xs, auto) | 
| 13145 | 1764 | done | 
| 13114 | 1765 | |
| 24526 | 1766 | lemma drop_map: "drop n (map f xs) = map f (drop n xs)" | 
| 1767 | apply (induct n arbitrary: xs, auto) | |
| 14208 | 1768 | apply (case_tac xs, auto) | 
| 13145 | 1769 | done | 
| 13114 | 1770 | |
| 24526 | 1771 | lemma rev_take: "rev (take i xs) = drop (length xs - i) (rev xs)" | 
| 1772 | apply (induct xs arbitrary: i, auto) | |
| 14208 | 1773 | apply (case_tac i, auto) | 
| 13145 | 1774 | done | 
| 13114 | 1775 | |
| 24526 | 1776 | lemma rev_drop: "rev (drop i xs) = take (length xs - i) (rev xs)" | 
| 1777 | apply (induct xs arbitrary: i, auto) | |
| 14208 | 1778 | apply (case_tac i, auto) | 
| 13145 | 1779 | done | 
| 13114 | 1780 | |
| 24526 | 1781 | lemma nth_take [simp]: "i < n ==> (take n xs)!i = xs!i" | 
| 1782 | apply (induct xs arbitrary: i n, auto) | |
| 14208 | 1783 | apply (case_tac n, blast) | 
| 1784 | apply (case_tac i, auto) | |
| 13145 | 1785 | done | 
| 13114 | 1786 | |
| 13142 | 1787 | lemma nth_drop [simp]: | 
| 24526 | 1788 | "n + i <= length xs ==> (drop n xs)!i = xs!(n + i)" | 
| 1789 | apply (induct n arbitrary: xs i, auto) | |
| 14208 | 1790 | apply (case_tac xs, auto) | 
| 13145 | 1791 | done | 
| 3507 | 1792 | |
| 26584 
46f3b89b2445
move lemmas from Word/BinBoolList.thy to List.thy
 huffman parents: 
26480diff
changeset | 1793 | 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 | 1794 | "n <= length xs ==> butlast (take n xs) = take (n - 1) xs" | 
| 26584 
46f3b89b2445
move lemmas from Word/BinBoolList.thy to List.thy
 huffman parents: 
26480diff
changeset | 1795 | by (simp add: butlast_conv_take min_max.inf_absorb1 min_max.inf_absorb2) | 
| 
46f3b89b2445
move lemmas from Word/BinBoolList.thy to List.thy
 huffman parents: 
26480diff
changeset | 1796 | |
| 
46f3b89b2445
move lemmas from Word/BinBoolList.thy to List.thy
 huffman parents: 
26480diff
changeset | 1797 | 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 | 1798 | 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 | 1799 | |
| 
46f3b89b2445
move lemmas from Word/BinBoolList.thy to List.thy
 huffman parents: 
26480diff
changeset | 1800 | lemma take_butlast: "n < length xs ==> take n (butlast xs) = take n xs" | 
| 
46f3b89b2445
move lemmas from Word/BinBoolList.thy to List.thy
 huffman parents: 
26480diff
changeset | 1801 | by (simp add: butlast_conv_take min_max.inf_absorb1) | 
| 
46f3b89b2445
move lemmas from Word/BinBoolList.thy to List.thy
 huffman parents: 
26480diff
changeset | 1802 | |
| 
46f3b89b2445
move lemmas from Word/BinBoolList.thy to List.thy
 huffman parents: 
26480diff
changeset | 1803 | 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 | 1804 | 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 | 1805 | |
| 46500 
0196966d6d2d
removing unnecessary premises in theorems of List theory
 bulwahn parents: 
46448diff
changeset | 1806 | lemma hd_drop_conv_nth: "n < length xs \<Longrightarrow> hd(drop n xs) = xs!n" | 
| 18423 | 1807 | by(simp add: hd_conv_nth) | 
| 1808 | ||
| 35248 | 1809 | lemma set_take_subset_set_take: | 
| 1810 | "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 | 1811 | apply (induct xs arbitrary: m n) | 
| 
edbf0a86fb1c
adding simproc to rewrite list comprehensions to set comprehensions; adopting proofs
 bulwahn parents: 
41372diff
changeset | 1812 | apply simp | 
| 
edbf0a86fb1c
adding simproc to rewrite list comprehensions to set comprehensions; adopting proofs
 bulwahn parents: 
41372diff
changeset | 1813 | apply (case_tac n) | 
| 
edbf0a86fb1c
adding simproc to rewrite list comprehensions to set comprehensions; adopting proofs
 bulwahn parents: 
41372diff
changeset | 1814 | apply (auto simp: take_Cons) | 
| 
edbf0a86fb1c
adding simproc to rewrite list comprehensions to set comprehensions; adopting proofs
 bulwahn parents: 
41372diff
changeset | 1815 | done | 
| 35248 | 1816 | |
| 24526 | 1817 | lemma set_take_subset: "set(take n xs) \<subseteq> set xs" | 
| 1818 | by(induct xs arbitrary: n)(auto simp:take_Cons split:nat.split) | |
| 1819 | ||
| 1820 | lemma set_drop_subset: "set(drop n xs) \<subseteq> set xs" | |
| 1821 | by(induct xs arbitrary: n)(auto simp:drop_Cons split:nat.split) | |
| 14025 | 1822 | |
| 35248 | 1823 | lemma set_drop_subset_set_drop: | 
| 1824 | "m >= n \<Longrightarrow> set(drop m xs) <= set(drop n xs)" | |
| 1825 | apply(induct xs arbitrary: m n) | |
| 1826 | apply(auto simp:drop_Cons split:nat.split) | |
| 1827 | apply (metis set_drop_subset subset_iff) | |
| 1828 | done | |
| 1829 | ||
| 14187 | 1830 | lemma in_set_takeD: "x : set(take n xs) \<Longrightarrow> x : set xs" | 
| 1831 | using set_take_subset by fast | |
| 1832 | ||
| 1833 | lemma in_set_dropD: "x : set(drop n xs) \<Longrightarrow> x : set xs" | |
| 1834 | using set_drop_subset by fast | |
| 1835 | ||
| 13114 | 1836 | lemma append_eq_conv_conj: | 
| 24526 | 1837 | "(xs @ ys = zs) = (xs = take (length xs) zs \<and> ys = drop (length xs) zs)" | 
| 1838 | apply (induct xs arbitrary: zs, simp, clarsimp) | |
| 14208 | 1839 | apply (case_tac zs, auto) | 
| 13145 | 1840 | done | 
| 13142 | 1841 | |
| 24526 | 1842 | lemma take_add: | 
| 42713 | 1843 | "take (i+j) xs = take i xs @ take j (drop i xs)" | 
| 24526 | 1844 | apply (induct xs arbitrary: i, auto) | 
| 1845 | apply (case_tac i, simp_all) | |
| 14050 | 1846 | done | 
| 1847 | ||
| 14300 | 1848 | lemma append_eq_append_conv_if: | 
| 24526 | 1849 | "(xs\<^isub>1 @ xs\<^isub>2 = ys\<^isub>1 @ ys\<^isub>2) = | 
| 14300 | 1850 | (if size xs\<^isub>1 \<le> size ys\<^isub>1 | 
| 1851 | then xs\<^isub>1 = take (size xs\<^isub>1) ys\<^isub>1 \<and> xs\<^isub>2 = drop (size xs\<^isub>1) ys\<^isub>1 @ ys\<^isub>2 | |
| 1852 | else take (size ys\<^isub>1) xs\<^isub>1 = ys\<^isub>1 \<and> drop (size ys\<^isub>1) xs\<^isub>1 @ xs\<^isub>2 = ys\<^isub>2)" | |
| 24526 | 1853 | apply(induct xs\<^isub>1 arbitrary: ys\<^isub>1) | 
| 14300 | 1854 | apply simp | 
| 1855 | apply(case_tac ys\<^isub>1) | |
| 1856 | apply simp_all | |
| 1857 | done | |
| 1858 | ||
| 15110 
78b5636eabc7
Added a number of new thms and the new function remove1
 nipkow parents: 
15072diff
changeset | 1859 | 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 | 1860 | "n < length xs \<Longrightarrow> take n xs @ [hd (drop n xs)] = take (Suc n) xs" | 
| 24526 | 1861 | apply(induct xs arbitrary: n) | 
| 15110 
78b5636eabc7
Added a number of new thms and the new function remove1
 nipkow parents: 
15072diff
changeset | 1862 | apply simp | 
| 
78b5636eabc7
Added a number of new thms and the new function remove1
 nipkow parents: 
15072diff
changeset | 1863 | apply(simp add:drop_Cons split:nat.split) | 
| 
78b5636eabc7
Added a number of new thms and the new function remove1
 nipkow parents: 
15072diff
changeset | 1864 | done | 
| 
78b5636eabc7
Added a number of new thms and the new function remove1
 nipkow parents: 
15072diff
changeset | 1865 | |
| 17501 | 1866 | lemma id_take_nth_drop: | 
| 1867 | "i < length xs \<Longrightarrow> xs = take i xs @ xs!i # drop (Suc i) xs" | |
| 1868 | proof - | |
| 1869 | assume si: "i < length xs" | |
| 1870 | hence "xs = take (Suc i) xs @ drop (Suc i) xs" by auto | |
| 1871 | moreover | |
| 1872 | from si have "take (Suc i) xs = take i xs @ [xs!i]" | |
| 1873 | apply (rule_tac take_Suc_conv_app_nth) by arith | |
| 1874 | ultimately show ?thesis by auto | |
| 1875 | qed | |
| 1876 | ||
| 1877 | lemma upd_conv_take_nth_drop: | |
| 1878 | "i < length xs \<Longrightarrow> xs[i:=a] = take i xs @ a # drop (Suc i) xs" | |
| 1879 | proof - | |
| 1880 | assume i: "i < length xs" | |
| 1881 | have "xs[i:=a] = (take i xs @ xs!i # drop (Suc i) xs)[i:=a]" | |
| 1882 | by(rule arg_cong[OF id_take_nth_drop[OF i]]) | |
| 1883 | also have "\<dots> = take i xs @ a # drop (Suc i) xs" | |
| 1884 | using i by (simp add: list_update_append) | |
| 1885 | finally show ?thesis . | |
| 1886 | qed | |
| 1887 | ||
| 24796 | 1888 | lemma nth_drop': | 
| 1889 | "i < length xs \<Longrightarrow> xs ! i # drop (Suc i) xs = drop i xs" | |
| 1890 | apply (induct i arbitrary: xs) | |
| 1891 | apply (simp add: neq_Nil_conv) | |
| 1892 | apply (erule exE)+ | |
| 1893 | apply simp | |
| 1894 | apply (case_tac xs) | |
| 1895 | apply simp_all | |
| 1896 | done | |
| 1897 | ||
| 13114 | 1898 | |
| 15392 | 1899 | subsubsection {* @{text takeWhile} and @{text dropWhile} *}
 | 
| 13114 | 1900 | |
| 33639 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 1901 | 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 | 1902 | 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 | 1903 | |
| 13142 | 1904 | lemma takeWhile_dropWhile_id [simp]: "takeWhile P xs @ dropWhile P xs = xs" | 
| 13145 | 1905 | by (induct xs) auto | 
| 13114 | 1906 | |
| 13142 | 1907 | lemma takeWhile_append1 [simp]: | 
| 13145 | 1908 | "[| x:set xs; ~P(x)|] ==> takeWhile P (xs @ ys) = takeWhile P xs" | 
| 1909 | by (induct xs) auto | |
| 13114 | 1910 | |
| 13142 | 1911 | lemma takeWhile_append2 [simp]: | 
| 13145 | 1912 | "(!!x. x : set xs ==> P x) ==> takeWhile P (xs @ ys) = xs @ takeWhile P ys" | 
| 1913 | by (induct xs) auto | |
| 13114 | 1914 | |
| 13142 | 1915 | lemma takeWhile_tail: "\<not> P x ==> takeWhile P (xs @ (x#l)) = takeWhile P xs" | 
| 13145 | 1916 | by (induct xs) auto | 
| 13114 | 1917 | |
| 33639 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 1918 | 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 | 1919 | 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 | 1920 | |
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 1921 | 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 | 1922 | 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 | 1923 | |
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 1924 | 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 | 1925 | 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 | 1926 | |
| 13142 | 1927 | lemma dropWhile_append1 [simp]: | 
| 13145 | 1928 | "[| x : set xs; ~P(x)|] ==> dropWhile P (xs @ ys) = (dropWhile P xs)@ys" | 
| 1929 | by (induct xs) auto | |
| 13114 | 1930 | |
| 13142 | 1931 | lemma dropWhile_append2 [simp]: | 
| 13145 | 1932 | "(!!x. x:set xs ==> P(x)) ==> dropWhile P (xs @ ys) = dropWhile P ys" | 
| 1933 | by (induct xs) auto | |
| 13114 | 1934 | |
| 45841 | 1935 | lemma dropWhile_append3: | 
| 1936 | "\<not> P y \<Longrightarrow>dropWhile P (xs @ y # ys) = dropWhile P xs @ y # ys" | |
| 1937 | by (induct xs) auto | |
| 1938 | ||
| 1939 | lemma dropWhile_last: | |
| 1940 | "x \<in> set xs \<Longrightarrow> \<not> P x \<Longrightarrow> last (dropWhile P xs) = last xs" | |
| 1941 | by (auto simp add: dropWhile_append3 in_set_conv_decomp) | |
| 1942 | ||
| 1943 | lemma set_dropWhileD: "x \<in> set (dropWhile P xs) \<Longrightarrow> x \<in> set xs" | |
| 1944 | by (induct xs) (auto split: split_if_asm) | |
| 1945 | ||
| 23971 
e6d505d5b03d
renamed lemma "set_take_whileD" to "set_takeWhileD"
 krauss parents: 
23740diff
changeset | 1946 | lemma set_takeWhileD: "x : set (takeWhile P xs) ==> x : set xs \<and> P x" | 
| 13145 | 1947 | by (induct xs) (auto split: split_if_asm) | 
| 13114 | 1948 | |
| 13913 | 1949 | lemma takeWhile_eq_all_conv[simp]: | 
| 1950 | "(takeWhile P xs = xs) = (\<forall>x \<in> set xs. P x)" | |
| 1951 | by(induct xs, auto) | |
| 1952 | ||
| 1953 | lemma dropWhile_eq_Nil_conv[simp]: | |
| 1954 | "(dropWhile P xs = []) = (\<forall>x \<in> set xs. P x)" | |
| 1955 | by(induct xs, auto) | |
| 1956 | ||
| 1957 | lemma dropWhile_eq_Cons_conv: | |
| 1958 | "(dropWhile P xs = y#ys) = (xs = takeWhile P xs @ y # ys & \<not> P y)" | |
| 1959 | by(induct xs, auto) | |
| 1960 | ||
| 31077 | 1961 | lemma distinct_takeWhile[simp]: "distinct xs ==> distinct (takeWhile P xs)" | 
| 1962 | by (induct xs) (auto dest: set_takeWhileD) | |
| 1963 | ||
| 1964 | lemma distinct_dropWhile[simp]: "distinct xs ==> distinct (dropWhile P xs)" | |
| 1965 | by (induct xs) auto | |
| 1966 | ||
| 33639 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 1967 | 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 | 1968 | 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 | 1969 | |
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 1970 | 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 | 1971 | 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 | 1972 | |
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 1973 | 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 | 1974 | 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 | 1975 | |
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 1976 | 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 | 1977 | 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 | 1978 | |
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 1979 | 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 | 1980 | "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 | 1981 | 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 | 1982 | |
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 1983 | 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 | 1984 | 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 | 1985 | 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 | 1986 | proof - | 
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 1987 | 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 | 1988 | 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 | 1989 | 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 | 1990 | 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 | 1991 | 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 | 1992 | 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 | 1993 | 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 | 1994 | 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 | 1995 | qed | 
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 1996 | |
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 1997 | 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 | 1998 | "\<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 | 1999 | 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 | 2000 | 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 | 2001 | 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 | 2002 | 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 | 2003 | 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 | 2004 | 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 | 2005 | 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 | 2006 | 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 | 2007 | 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 | 2008 | 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 | 2009 | 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 | 2010 | qed | 
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 2011 | 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 | 2012 | 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 | 2013 | 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 | 2014 | |
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 2015 | 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 | 2016 | "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 | 2017 | 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 | 2018 | |
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 2019 | 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 | 2020 | 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 | 2021 | 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 | 2022 | 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 | 2023 | 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 | 2024 | 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 | 2025 | 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 | 2026 | qed | 
| 31077 | 2027 | |
| 17501 | 2028 | text{* The following two lemmmas could be generalized to an arbitrary
 | 
| 2029 | property. *} | |
| 2030 | ||
| 2031 | lemma takeWhile_neq_rev: "\<lbrakk>distinct xs; x \<in> set xs\<rbrakk> \<Longrightarrow> | |
| 2032 | takeWhile (\<lambda>y. y \<noteq> x) (rev xs) = rev (tl (dropWhile (\<lambda>y. y \<noteq> x) xs))" | |
| 2033 | by(induct xs) (auto simp: takeWhile_tail[where l="[]"]) | |
| 2034 | ||
| 2035 | lemma dropWhile_neq_rev: "\<lbrakk>distinct xs; x \<in> set xs\<rbrakk> \<Longrightarrow> | |
| 2036 | dropWhile (\<lambda>y. y \<noteq> x) (rev xs) = x # rev (takeWhile (\<lambda>y. y \<noteq> x) xs)" | |
| 2037 | apply(induct xs) | |
| 2038 | apply simp | |
| 2039 | apply auto | |
| 2040 | apply(subst dropWhile_append2) | |
| 2041 | apply auto | |
| 2042 | done | |
| 2043 | ||
| 18423 | 2044 | lemma takeWhile_not_last: | 
| 46500 
0196966d6d2d
removing unnecessary premises in theorems of List theory
 bulwahn parents: 
46448diff
changeset | 2045 | "distinct xs \<Longrightarrow> takeWhile (\<lambda>y. y \<noteq> last xs) xs = butlast xs" | 
| 18423 | 2046 | apply(induct xs) | 
| 2047 | apply simp | |
| 2048 | apply(case_tac xs) | |
| 2049 | apply(auto) | |
| 2050 | done | |
| 2051 | ||
| 44013 
5cfc1c36ae97
moved recdef package to HOL/Library/Old_Recdef.thy
 krauss parents: 
43594diff
changeset | 2052 | lemma takeWhile_cong [fundef_cong]: | 
| 18336 
1a2e30b37ed3
Added recdef congruence rules for bounded quantifiers and commonly used
 krauss parents: 
18049diff
changeset | 2053 | "[| 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 | 2054 | ==> takeWhile P l = takeWhile Q k" | 
| 24349 | 2055 | by (induct k arbitrary: l) (simp_all) | 
| 18336 
1a2e30b37ed3
Added recdef congruence rules for bounded quantifiers and commonly used
 krauss parents: 
18049diff
changeset | 2056 | |
| 44013 
5cfc1c36ae97
moved recdef package to HOL/Library/Old_Recdef.thy
 krauss parents: 
43594diff
changeset | 2057 | lemma dropWhile_cong [fundef_cong]: | 
| 18336 
1a2e30b37ed3
Added recdef congruence rules for bounded quantifiers and commonly used
 krauss parents: 
18049diff
changeset | 2058 | "[| 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 | 2059 | ==> dropWhile P l = dropWhile Q k" | 
| 24349 | 2060 | by (induct k arbitrary: l, simp_all) | 
| 18336 
1a2e30b37ed3
Added recdef congruence rules for bounded quantifiers and commonly used
 krauss parents: 
18049diff
changeset | 2061 | |
| 13114 | 2062 | |
| 15392 | 2063 | subsubsection {* @{text zip} *}
 | 
| 13114 | 2064 | |
| 13142 | 2065 | lemma zip_Nil [simp]: "zip [] ys = []" | 
| 13145 | 2066 | by (induct ys) auto | 
| 13114 | 2067 | |
| 13142 | 2068 | lemma zip_Cons_Cons [simp]: "zip (x # xs) (y # ys) = (x, y) # zip xs ys" | 
| 13145 | 2069 | by simp | 
| 13114 | 2070 | |
| 13142 | 2071 | declare zip_Cons [simp del] | 
| 13114 | 2072 | |
| 36198 | 2073 | lemma [code]: | 
| 2074 | "zip [] ys = []" | |
| 2075 | "zip xs [] = []" | |
| 2076 | "zip (x # xs) (y # ys) = (x, y) # zip xs ys" | |
| 2077 | by (fact zip_Nil zip.simps(1) zip_Cons_Cons)+ | |
| 2078 | ||
| 15281 | 2079 | lemma zip_Cons1: | 
| 2080 | "zip (x#xs) ys = (case ys of [] \<Rightarrow> [] | y#ys \<Rightarrow> (x,y)#zip xs ys)" | |
| 2081 | by(auto split:list.split) | |
| 2082 | ||
| 13142 | 2083 | lemma length_zip [simp]: | 
| 22493 
db930e490fe5
added another rule for simultaneous induction, and lemmas for zip
 krauss parents: 
22422diff
changeset | 2084 | "length (zip xs ys) = min (length xs) (length ys)" | 
| 
db930e490fe5
added another rule for simultaneous induction, and lemmas for zip
 krauss parents: 
22422diff
changeset | 2085 | by (induct xs ys rule:list_induct2') auto | 
| 13114 | 2086 | |
| 34978 
874150ddd50a
canonical insert operation; generalized lemma foldl_apply_inv to foldl_apply
 haftmann parents: 
34942diff
changeset | 2087 | lemma zip_obtain_same_length: | 
| 
874150ddd50a
canonical insert operation; generalized lemma foldl_apply_inv to foldl_apply
 haftmann parents: 
34942diff
changeset | 2088 | 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 | 2089 | \<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 | 2090 | shows "P (zip xs ys)" | 
| 
874150ddd50a
canonical insert operation; generalized lemma foldl_apply_inv to foldl_apply
 haftmann parents: 
34942diff
changeset | 2091 | proof - | 
| 
874150ddd50a
canonical insert operation; generalized lemma foldl_apply_inv to foldl_apply
 haftmann parents: 
34942diff
changeset | 2092 | let ?n = "min (length xs) (length ys)" | 
| 
874150ddd50a
canonical insert operation; generalized lemma foldl_apply_inv to foldl_apply
 haftmann parents: 
34942diff
changeset | 2093 | 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 | 2094 | by (rule assms) simp_all | 
| 
874150ddd50a
canonical insert operation; generalized lemma foldl_apply_inv to foldl_apply
 haftmann parents: 
34942diff
changeset | 2095 | 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 | 2096 | proof (induct xs arbitrary: ys) | 
| 
874150ddd50a
canonical insert operation; generalized lemma foldl_apply_inv to foldl_apply
 haftmann parents: 
34942diff
changeset | 2097 | case Nil then show ?case by simp | 
| 
874150ddd50a
canonical insert operation; generalized lemma foldl_apply_inv to foldl_apply
 haftmann parents: 
34942diff
changeset | 2098 | next | 
| 
874150ddd50a
canonical insert operation; generalized lemma foldl_apply_inv to foldl_apply
 haftmann parents: 
34942diff
changeset | 2099 | 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 | 2100 | qed | 
| 
874150ddd50a
canonical insert operation; generalized lemma foldl_apply_inv to foldl_apply
 haftmann parents: 
34942diff
changeset | 2101 | ultimately show ?thesis by simp | 
| 
874150ddd50a
canonical insert operation; generalized lemma foldl_apply_inv to foldl_apply
 haftmann parents: 
34942diff
changeset | 2102 | qed | 
| 
874150ddd50a
canonical insert operation; generalized lemma foldl_apply_inv to foldl_apply
 haftmann parents: 
34942diff
changeset | 2103 | |
| 13114 | 2104 | lemma zip_append1: | 
| 22493 
db930e490fe5
added another rule for simultaneous induction, and lemmas for zip
 krauss parents: 
22422diff
changeset | 2105 | "zip (xs @ ys) zs = | 
| 13145 | 2106 | 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 | 2107 | by (induct xs zs rule:list_induct2') auto | 
| 13114 | 2108 | |
| 2109 | lemma zip_append2: | |
| 22493 
db930e490fe5
added another rule for simultaneous induction, and lemmas for zip
 krauss parents: 
22422diff
changeset | 2110 | "zip xs (ys @ zs) = | 
| 13145 | 2111 | 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 | 2112 | by (induct xs ys rule:list_induct2') auto | 
| 13114 | 2113 | |
| 13142 | 2114 | lemma zip_append [simp]: | 
| 46500 
0196966d6d2d
removing unnecessary premises in theorems of List theory
 bulwahn parents: 
46448diff
changeset | 2115 | "[| length xs = length us |] ==> | 
| 13145 | 2116 | zip (xs@ys) (us@vs) = zip xs us @ zip ys vs" | 
| 2117 | by (simp add: zip_append1) | |
| 13114 | 2118 | |
| 2119 | lemma zip_rev: | |
| 14247 | 2120 | "length xs = length ys ==> zip (rev xs) (rev ys) = rev (zip xs ys)" | 
| 2121 | by (induct rule:list_induct2, simp_all) | |
| 13114 | 2122 | |
| 33639 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 2123 | 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 | 2124 | "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 | 2125 | 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 | 2126 | 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 | 2127 | 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 | 2128 | 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 | 2129 | 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 | 2130 | 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 | 2131 | 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 | 2132 | 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 | 2133 | |
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 2134 | 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 | 2135 | "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 | 2136 | 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 | 2137 | |
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 2138 | 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 | 2139 | "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 | 2140 | 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 | 2141 | |
| 23096 | 2142 | 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 | 2143 | "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 | 2144 | unfolding zip_map1 by auto | 
| 23096 | 2145 | |
| 2146 | 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 | 2147 | "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 | 2148 | unfolding zip_map2 by auto | 
| 23096 | 2149 | |
| 31080 | 2150 | text{* Courtesy of Andreas Lochbihler: *}
 | 
| 2151 | lemma zip_same_conv_map: "zip xs xs = map (\<lambda>x. (x, x)) xs" | |
| 2152 | by(induct xs) auto | |
| 2153 | ||
| 13142 | 2154 | lemma nth_zip [simp]: | 
| 24526 | 2155 | "[| i < length xs; i < length ys|] ==> (zip xs ys)!i = (xs!i, ys!i)" | 
| 2156 | apply (induct ys arbitrary: i xs, simp) | |
| 13145 | 2157 | apply (case_tac xs) | 
| 2158 | apply (simp_all add: nth.simps split: nat.split) | |
| 2159 | done | |
| 13114 | 2160 | |
| 2161 | lemma set_zip: | |
| 13145 | 2162 | "set (zip xs ys) = {(xs!i, ys!i) | i. i < min (length xs) (length ys)}"
 | 
| 31080 | 2163 | by(simp add: set_conv_nth cong: rev_conj_cong) | 
| 13114 | 2164 | |
| 33639 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 2165 | 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 | 2166 | 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 | 2167 | |
| 13114 | 2168 | lemma zip_update: | 
| 31080 | 2169 | "zip (xs[i:=x]) (ys[i:=y]) = (zip xs ys)[i:=(x,y)]" | 
| 2170 | by(rule sym, simp add: update_zip) | |
| 13114 | 2171 | |
| 13142 | 2172 | lemma zip_replicate [simp]: | 
| 24526 | 2173 | "zip (replicate i x) (replicate j y) = replicate (min i j) (x,y)" | 
| 2174 | apply (induct i arbitrary: j, auto) | |
| 14208 | 2175 | apply (case_tac j, auto) | 
| 13145 | 2176 | done | 
| 13114 | 2177 | |
| 19487 | 2178 | lemma take_zip: | 
| 24526 | 2179 | "take n (zip xs ys) = zip (take n xs) (take n ys)" | 
| 2180 | apply (induct n arbitrary: xs ys) | |
| 19487 | 2181 | apply simp | 
| 2182 | apply (case_tac xs, simp) | |
| 2183 | apply (case_tac ys, simp_all) | |
| 2184 | done | |
| 2185 | ||
| 2186 | lemma drop_zip: | |
| 24526 | 2187 | "drop n (zip xs ys) = zip (drop n xs) (drop n ys)" | 
| 2188 | apply (induct n arbitrary: xs ys) | |
| 19487 | 2189 | apply simp | 
| 2190 | apply (case_tac xs, simp) | |
| 2191 | apply (case_tac ys, simp_all) | |
| 2192 | done | |
| 2193 | ||
| 33639 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 2194 | 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 | 2195 | 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 | 2196 | 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 | 2197 | 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 | 2198 | |
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 2199 | 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 | 2200 | 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 | 2201 | 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 | 2202 | 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 | 2203 | |
| 22493 
db930e490fe5
added another rule for simultaneous induction, and lemmas for zip
 krauss parents: 
22422diff
changeset | 2204 | lemma set_zip_leftD: | 
| 
db930e490fe5
added another rule for simultaneous induction, and lemmas for zip
 krauss parents: 
22422diff
changeset | 2205 | "(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 | 2206 | by (induct xs ys rule:list_induct2') auto | 
| 
db930e490fe5
added another rule for simultaneous induction, and lemmas for zip
 krauss parents: 
22422diff
changeset | 2207 | |
| 
db930e490fe5
added another rule for simultaneous induction, and lemmas for zip
 krauss parents: 
22422diff
changeset | 2208 | lemma set_zip_rightD: | 
| 
db930e490fe5
added another rule for simultaneous induction, and lemmas for zip
 krauss parents: 
22422diff
changeset | 2209 | "(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 | 2210 | by (induct xs ys rule:list_induct2') auto | 
| 13142 | 2211 | |
| 23983 | 2212 | lemma in_set_zipE: | 
| 2213 | "(x,y) : set(zip xs ys) \<Longrightarrow> (\<lbrakk> x : set xs; y : set ys \<rbrakk> \<Longrightarrow> R) \<Longrightarrow> R" | |
| 2214 | by(blast dest: set_zip_leftD set_zip_rightD) | |
| 2215 | ||
| 29829 | 2216 | lemma zip_map_fst_snd: | 
| 2217 | "zip (map fst zs) (map snd zs) = zs" | |
| 2218 | by (induct zs) simp_all | |
| 2219 | ||
| 2220 | lemma zip_eq_conv: | |
| 2221 | "length xs = length ys \<Longrightarrow> zip xs ys = zs \<longleftrightarrow> map fst zs = xs \<and> map snd zs = ys" | |
| 2222 | by (auto simp add: zip_map_fst_snd) | |
| 2223 | ||
| 35115 | 2224 | |
| 15392 | 2225 | subsubsection {* @{text list_all2} *}
 | 
| 13114 | 2226 | |
| 14316 
91b897b9a2dc
added some [intro?] and [trans] for list_all2 lemmas
 kleing parents: 
14302diff
changeset | 2227 | lemma list_all2_lengthD [intro?]: | 
| 
91b897b9a2dc
added some [intro?] and [trans] for list_all2 lemmas
 kleing parents: 
14302diff
changeset | 2228 | "list_all2 P xs ys ==> length xs = length ys" | 
| 24349 | 2229 | by (simp add: list_all2_def) | 
| 19607 
07eeb832f28d
introduced characters for code generator; some improved code lemmas for some list functions
 haftmann parents: 
19585diff
changeset | 2230 | |
| 19787 | 2231 | lemma list_all2_Nil [iff, code]: "list_all2 P [] ys = (ys = [])" | 
| 24349 | 2232 | by (simp add: list_all2_def) | 
| 19607 
07eeb832f28d
introduced characters for code generator; some improved code lemmas for some list functions
 haftmann parents: 
19585diff
changeset | 2233 | |
| 19787 | 2234 | lemma list_all2_Nil2 [iff, code]: "list_all2 P xs [] = (xs = [])" | 
| 24349 | 2235 | by (simp add: list_all2_def) | 
| 19607 
07eeb832f28d
introduced characters for code generator; some improved code lemmas for some list functions
 haftmann parents: 
19585diff
changeset | 2236 | |
| 
07eeb832f28d
introduced characters for code generator; some improved code lemmas for some list functions
 haftmann parents: 
19585diff
changeset | 2237 | lemma list_all2_Cons [iff, code]: | 
| 
07eeb832f28d
introduced characters for code generator; some improved code lemmas for some list functions
 haftmann parents: 
19585diff
changeset | 2238 | "list_all2 P (x # xs) (y # ys) = (P x y \<and> list_all2 P xs ys)" | 
| 24349 | 2239 | by (auto simp add: list_all2_def) | 
| 13114 | 2240 | |
| 2241 | lemma list_all2_Cons1: | |
| 13145 | 2242 | "list_all2 P (x # xs) ys = (\<exists>z zs. ys = z # zs \<and> P x z \<and> list_all2 P xs zs)" | 
| 2243 | by (cases ys) auto | |
| 13114 | 2244 | |
| 2245 | lemma list_all2_Cons2: | |
| 13145 | 2246 | "list_all2 P xs (y # ys) = (\<exists>z zs. xs = z # zs \<and> P z y \<and> list_all2 P zs ys)" | 
| 2247 | by (cases xs) auto | |
| 13114 | 2248 | |
| 45794 | 2249 | lemma list_all2_induct | 
| 2250 | [consumes 1, case_names Nil Cons, induct set: list_all2]: | |
| 2251 | assumes P: "list_all2 P xs ys" | |
| 2252 | assumes Nil: "R [] []" | |
| 2253 | assumes Cons: "\<And>x xs y ys. \<lbrakk>P x y; R xs ys\<rbrakk> \<Longrightarrow> R (x # xs) (y # ys)" | |
| 2254 | shows "R xs ys" | |
| 2255 | using P | |
| 2256 | by (induct xs arbitrary: ys) (auto simp add: list_all2_Cons1 Nil Cons) | |
| 2257 | ||
| 13142 | 2258 | lemma list_all2_rev [iff]: | 
| 13145 | 2259 | "list_all2 P (rev xs) (rev ys) = list_all2 P xs ys" | 
| 2260 | by (simp add: list_all2_def zip_rev cong: conj_cong) | |
| 13114 | 2261 | |
| 13863 | 2262 | lemma list_all2_rev1: | 
| 2263 | "list_all2 P (rev xs) ys = list_all2 P xs (rev ys)" | |
| 2264 | by (subst list_all2_rev [symmetric]) simp | |
| 2265 | ||
| 13114 | 2266 | lemma list_all2_append1: | 
| 13145 | 2267 | "list_all2 P (xs @ ys) zs = | 
| 2268 | (EX us vs. zs = us @ vs \<and> length us = length xs \<and> length vs = length ys \<and> | |
| 2269 | list_all2 P xs us \<and> list_all2 P ys vs)" | |
| 2270 | apply (simp add: list_all2_def zip_append1) | |
| 2271 | apply (rule iffI) | |
| 2272 | apply (rule_tac x = "take (length xs) zs" in exI) | |
| 2273 | apply (rule_tac x = "drop (length xs) zs" in exI) | |
| 14208 | 2274 | apply (force split: nat_diff_split simp add: min_def, clarify) | 
| 13145 | 2275 | apply (simp add: ball_Un) | 
| 2276 | done | |
| 13114 | 2277 | |
| 2278 | lemma list_all2_append2: | |
| 13145 | 2279 | "list_all2 P xs (ys @ zs) = | 
| 2280 | (EX us vs. xs = us @ vs \<and> length us = length ys \<and> length vs = length zs \<and> | |
| 2281 | list_all2 P us ys \<and> list_all2 P vs zs)" | |
| 2282 | apply (simp add: list_all2_def zip_append2) | |
| 2283 | apply (rule iffI) | |
| 2284 | apply (rule_tac x = "take (length ys) xs" in exI) | |
| 2285 | apply (rule_tac x = "drop (length ys) xs" in exI) | |
| 14208 | 2286 | apply (force split: nat_diff_split simp add: min_def, clarify) | 
| 13145 | 2287 | apply (simp add: ball_Un) | 
| 2288 | done | |
| 13114 | 2289 | |
| 13863 | 2290 | lemma list_all2_append: | 
| 14247 | 2291 | "length xs = length ys \<Longrightarrow> | 
| 2292 | list_all2 P (xs@us) (ys@vs) = (list_all2 P xs ys \<and> list_all2 P us vs)" | |
| 2293 | by (induct rule:list_induct2, simp_all) | |
| 13863 | 2294 | |
| 2295 | lemma list_all2_appendI [intro?, trans]: | |
| 2296 | "\<lbrakk> list_all2 P a b; list_all2 P c d \<rbrakk> \<Longrightarrow> list_all2 P (a@c) (b@d)" | |
| 24349 | 2297 | by (simp add: list_all2_append list_all2_lengthD) | 
| 13863 | 2298 | |
| 13114 | 2299 | lemma list_all2_conv_all_nth: | 
| 13145 | 2300 | "list_all2 P xs ys = | 
| 2301 | (length xs = length ys \<and> (\<forall>i < length xs. P (xs!i) (ys!i)))" | |
| 2302 | by (force simp add: list_all2_def set_zip) | |
| 13114 | 2303 | |
| 13883 
0451e0fb3f22
Re-structured some proofs in order to get rid of rule_format attribute.
 berghofe parents: 
13863diff
changeset | 2304 | lemma list_all2_trans: | 
| 
0451e0fb3f22
Re-structured some proofs in order to get rid of rule_format attribute.
 berghofe parents: 
13863diff
changeset | 2305 | 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 | 2306 | 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 | 2307 | (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 | 2308 | proof (induct as) | 
| 
0451e0fb3f22
Re-structured some proofs in order to get rid of rule_format attribute.
 berghofe parents: 
13863diff
changeset | 2309 | 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 | 2310 | 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 | 2311 | proof (induct bs) | 
| 
0451e0fb3f22
Re-structured some proofs in order to get rid of rule_format attribute.
 berghofe parents: 
13863diff
changeset | 2312 | 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 | 2313 | 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 | 2314 | 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 | 2315 | qed simp | 
| 
0451e0fb3f22
Re-structured some proofs in order to get rid of rule_format attribute.
 berghofe parents: 
13863diff
changeset | 2316 | qed simp | 
| 
0451e0fb3f22
Re-structured some proofs in order to get rid of rule_format attribute.
 berghofe parents: 
13863diff
changeset | 2317 | |
| 13863 | 2318 | lemma list_all2_all_nthI [intro?]: | 
| 2319 | "length a = length b \<Longrightarrow> (\<And>n. n < length a \<Longrightarrow> P (a!n) (b!n)) \<Longrightarrow> list_all2 P a b" | |
| 24349 | 2320 | by (simp add: list_all2_conv_all_nth) | 
| 13863 | 2321 | |
| 14395 | 2322 | lemma list_all2I: | 
| 2323 | "\<forall>x \<in> set (zip a b). split P x \<Longrightarrow> length a = length b \<Longrightarrow> list_all2 P a b" | |
| 24349 | 2324 | by (simp add: list_all2_def) | 
| 14395 | 2325 | |
| 14328 | 2326 | lemma list_all2_nthD: | 
| 13863 | 2327 | "\<lbrakk> list_all2 P xs ys; p < size xs \<rbrakk> \<Longrightarrow> P (xs!p) (ys!p)" | 
| 24349 | 2328 | by (simp add: list_all2_conv_all_nth) | 
| 13863 | 2329 | |
| 14302 | 2330 | lemma list_all2_nthD2: | 
| 2331 | "\<lbrakk>list_all2 P xs ys; p < size ys\<rbrakk> \<Longrightarrow> P (xs!p) (ys!p)" | |
| 24349 | 2332 | by (frule list_all2_lengthD) (auto intro: list_all2_nthD) | 
| 14302 | 2333 | |
| 13863 | 2334 | lemma list_all2_map1: | 
| 2335 | "list_all2 P (map f as) bs = list_all2 (\<lambda>x y. P (f x) y) as bs" | |
| 24349 | 2336 | by (simp add: list_all2_conv_all_nth) | 
| 13863 | 2337 | |
| 2338 | lemma list_all2_map2: | |
| 2339 | "list_all2 P as (map f bs) = list_all2 (\<lambda>x y. P x (f y)) as bs" | |
| 24349 | 2340 | by (auto simp add: list_all2_conv_all_nth) | 
| 13863 | 2341 | |
| 14316 
91b897b9a2dc
added some [intro?] and [trans] for list_all2 lemmas
 kleing parents: 
14302diff
changeset | 2342 | lemma list_all2_refl [intro?]: | 
| 13863 | 2343 | "(\<And>x. P x x) \<Longrightarrow> list_all2 P xs xs" | 
| 24349 | 2344 | by (simp add: list_all2_conv_all_nth) | 
| 13863 | 2345 | |
| 2346 | lemma list_all2_update_cong: | |
| 46669 
c1d2ab32174a
one general list_all2_update_cong instead of two special ones
 bulwahn parents: 
46664diff
changeset | 2347 | "\<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 | 2348 | by (cases "i < length ys") (auto simp add: list_all2_conv_all_nth nth_list_update) | 
| 13863 | 2349 | |
| 14302 | 2350 | lemma list_all2_takeI [simp,intro?]: | 
| 24526 | 2351 | "list_all2 P xs ys \<Longrightarrow> list_all2 P (take n xs) (take n ys)" | 
| 2352 | apply (induct xs arbitrary: n ys) | |
| 2353 | apply simp | |
| 2354 | apply (clarsimp simp add: list_all2_Cons1) | |
| 2355 | apply (case_tac n) | |
| 2356 | apply auto | |
| 2357 | done | |
| 14302 | 2358 | |
| 2359 | lemma list_all2_dropI [simp,intro?]: | |
| 24526 | 2360 | "list_all2 P as bs \<Longrightarrow> list_all2 P (drop n as) (drop n bs)" | 
| 2361 | apply (induct as arbitrary: n bs, simp) | |
| 2362 | apply (clarsimp simp add: list_all2_Cons1) | |
| 2363 | apply (case_tac n, simp, simp) | |
| 2364 | done | |
| 13863 | 2365 | |
| 14327 | 2366 | lemma list_all2_mono [intro?]: | 
| 24526 | 2367 | "list_all2 P xs ys \<Longrightarrow> (\<And>xs ys. P xs ys \<Longrightarrow> Q xs ys) \<Longrightarrow> list_all2 Q xs ys" | 
| 2368 | apply (induct xs arbitrary: ys, simp) | |
| 2369 | apply (case_tac ys, auto) | |
| 2370 | done | |
| 13863 | 2371 | |
| 22551 | 2372 | lemma list_all2_eq: | 
| 2373 | "xs = ys \<longleftrightarrow> list_all2 (op =) xs ys" | |
| 24349 | 2374 | by (induct xs ys rule: list_induct2') auto | 
| 22551 | 2375 | |
| 40230 | 2376 | lemma list_eq_iff_zip_eq: | 
| 2377 | "xs = ys \<longleftrightarrow> length xs = length ys \<and> (\<forall>(x,y) \<in> set (zip xs ys). x = y)" | |
| 2378 | by(auto simp add: set_zip list_all2_eq list_all2_conv_all_nth cong: conj_cong) | |
| 2379 | ||
| 13142 | 2380 | |
| 46133 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 haftmann parents: 
46125diff
changeset | 2381 | subsubsection {* @{const fold} with canonical argument order *}
 | 
| 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 haftmann parents: 
46125diff
changeset | 2382 | |
| 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 haftmann parents: 
46125diff
changeset | 2383 | 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 | 2384 | 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 | 2385 | 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 | 2386 | shows "fold f xs = fold f (remove1 x xs) \<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 | 2387 | using assms by (induct xs) (auto simp add: o_assoc [symmetric]) | 
| 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 haftmann parents: 
46125diff
changeset | 2388 | |
| 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 haftmann parents: 
46125diff
changeset | 2389 | 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 | 2390 | "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 | 2391 | \<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 | 2392 | 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 | 2393 | |
| 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 haftmann parents: 
46125diff
changeset | 2394 | 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 | 2395 | 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 | 2396 | 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 | 2397 | 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 | 2398 | |
| 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 haftmann parents: 
46125diff
changeset | 2399 | 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 | 2400 | 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 | 2401 | 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 | 2402 | 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 | 2403 | |
| 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 haftmann parents: 
46125diff
changeset | 2404 | 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 | 2405 | 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 | 2406 | 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 | 2407 | proof - | 
| 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 haftmann parents: 
46125diff
changeset | 2408 | 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 | 2409 | 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 | 2410 | qed | 
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 2411 | |
| 46133 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 haftmann parents: 
46125diff
changeset | 2412 | 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 | 2413 | 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 | 2414 | 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 | 2415 | shows "P (fold f xs s)" | 
| 34978 
874150ddd50a
canonical insert operation; generalized lemma foldl_apply_inv to foldl_apply
 haftmann parents: 
34942diff
changeset | 2416 | 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 | 2417 | |
| 46133 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 haftmann parents: 
46125diff
changeset | 2418 | 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 | 2419 | "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 | 2420 | 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 | 2421 | |
| 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 haftmann parents: 
46125diff
changeset | 2422 | 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 | 2423 | "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 | 2424 | 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 | 2425 | |
| 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 haftmann parents: 
46125diff
changeset | 2426 | 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 | 2427 | 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 | 2428 | 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 | 2429 | 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 | 2430 | |
| 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 haftmann parents: 
46125diff
changeset | 2431 | 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 | 2432 | "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 | 2433 | 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 | 2434 | |
| 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 haftmann parents: 
46125diff
changeset | 2435 | 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 | 2436 | "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 | 2437 | 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 | 2438 | |
| 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 haftmann parents: 
46125diff
changeset | 2439 | 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 | 2440 | "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 | 2441 | 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 | 2442 | |
| 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 haftmann parents: 
46125diff
changeset | 2443 | 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 | 2444 | |
| 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 haftmann parents: 
46125diff
changeset | 2445 | 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 | 2446 | "Finite_Set.fold f y (set xs) = fold f (remdups xs) y" | 
| 35195 | 2447 | by (rule sym, induct xs arbitrary: y) (simp_all add: fold_fun_comm insert_absorb) | 
| 2448 | ||
| 46133 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 haftmann parents: 
46125diff
changeset | 2449 | 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 | 2450 | "Finite_Set.fold f y (set xs) = fold f xs y" | 
| 31455 | 2451 | by (rule sym, induct xs arbitrary: y) (simp_all add: fold_fun_comm) | 
| 2452 | ||
| 46133 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 haftmann parents: 
46125diff
changeset | 2453 | lemma (in ab_semigroup_idem_mult) fold1_set_fold: | 
| 32681 
adeac3cbb659
lemma relating fold1 and foldl; code_unfold rules for Inf_fin, Sup_fin, Min, Max, Inf, Sup
 haftmann parents: 
32422diff
changeset | 2454 | assumes "xs \<noteq> []" | 
| 46133 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 haftmann parents: 
46125diff
changeset | 2455 | shows "Finite_Set.fold1 times (set xs) = fold times (tl xs) (hd xs)" | 
| 32681 
adeac3cbb659
lemma relating fold1 and foldl; code_unfold rules for Inf_fin, Sup_fin, Min, Max, Inf, Sup
 haftmann parents: 
32422diff
changeset | 2456 | proof - | 
| 42871 
1c0b99f950d9
names of fold_set locales resemble name of characteristic property more closely
 haftmann parents: 
42809diff
changeset | 2457 | interpret comp_fun_idem times by (fact comp_fun_idem) | 
| 32681 
adeac3cbb659
lemma relating fold1 and foldl; code_unfold rules for Inf_fin, Sup_fin, Min, Max, Inf, Sup
 haftmann parents: 
32422diff
changeset | 2458 | from assms obtain y ys where xs: "xs = y # ys" | 
| 
adeac3cbb659
lemma relating fold1 and foldl; code_unfold rules for Inf_fin, Sup_fin, Min, Max, Inf, Sup
 haftmann parents: 
32422diff
changeset | 2459 | by (cases xs) auto | 
| 
adeac3cbb659
lemma relating fold1 and foldl; code_unfold rules for Inf_fin, Sup_fin, Min, Max, Inf, Sup
 haftmann parents: 
32422diff
changeset | 2460 | show ?thesis | 
| 
adeac3cbb659
lemma relating fold1 and foldl; code_unfold rules for Inf_fin, Sup_fin, Min, Max, Inf, Sup
 haftmann parents: 
32422diff
changeset | 2461 |   proof (cases "set ys = {}")
 | 
| 
adeac3cbb659
lemma relating fold1 and foldl; code_unfold rules for Inf_fin, Sup_fin, Min, Max, Inf, Sup
 haftmann parents: 
32422diff
changeset | 2462 | case True with xs show ?thesis by simp | 
| 
adeac3cbb659
lemma relating fold1 and foldl; code_unfold rules for Inf_fin, Sup_fin, Min, Max, Inf, Sup
 haftmann parents: 
32422diff
changeset | 2463 | next | 
| 
adeac3cbb659
lemma relating fold1 and foldl; code_unfold rules for Inf_fin, Sup_fin, Min, Max, Inf, Sup
 haftmann parents: 
32422diff
changeset | 2464 | case False | 
| 46034 | 2465 | then have "fold1 times (insert y (set ys)) = Finite_Set.fold times y (set ys)" | 
| 32681 
adeac3cbb659
lemma relating fold1 and foldl; code_unfold rules for Inf_fin, Sup_fin, Min, Max, Inf, Sup
 haftmann parents: 
32422diff
changeset | 2466 | by (simp only: finite_set fold1_eq_fold_idem) | 
| 46133 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 haftmann parents: 
46125diff
changeset | 2467 | with xs show ?thesis by (simp add: fold_set_fold mult_commute) | 
| 32681 
adeac3cbb659
lemma relating fold1 and foldl; code_unfold rules for Inf_fin, Sup_fin, Min, Max, Inf, Sup
 haftmann parents: 
32422diff
changeset | 2468 | qed | 
| 
adeac3cbb659
lemma relating fold1 and foldl; code_unfold rules for Inf_fin, Sup_fin, Min, Max, Inf, Sup
 haftmann parents: 
32422diff
changeset | 2469 | qed | 
| 
adeac3cbb659
lemma relating fold1 and foldl; code_unfold rules for Inf_fin, Sup_fin, Min, Max, Inf, Sup
 haftmann parents: 
32422diff
changeset | 2470 | |
| 46147 
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
 haftmann parents: 
46143diff
changeset | 2471 | lemma union_set_fold: | 
| 
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
 haftmann parents: 
46143diff
changeset | 2472 | "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 | 2473 | proof - | 
| 
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
 haftmann parents: 
46143diff
changeset | 2474 | interpret comp_fun_idem Set.insert | 
| 
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
 haftmann parents: 
46143diff
changeset | 2475 | by (fact comp_fun_idem_insert) | 
| 
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
 haftmann parents: 
46143diff
changeset | 2476 | 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 | 2477 | qed | 
| 
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
 haftmann parents: 
46143diff
changeset | 2478 | |
| 
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
 haftmann parents: 
46143diff
changeset | 2479 | lemma minus_set_fold: | 
| 
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
 haftmann parents: 
46143diff
changeset | 2480 | "A - set xs = fold Set.remove xs A" | 
| 
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
 haftmann parents: 
46143diff
changeset | 2481 | proof - | 
| 
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
 haftmann parents: 
46143diff
changeset | 2482 | interpret comp_fun_idem Set.remove | 
| 
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
 haftmann parents: 
46143diff
changeset | 2483 | by (fact comp_fun_idem_remove) | 
| 
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
 haftmann parents: 
46143diff
changeset | 2484 | show ?thesis | 
| 
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
 haftmann parents: 
46143diff
changeset | 2485 | 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 | 2486 | qed | 
| 
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
 haftmann parents: 
46143diff
changeset | 2487 | |
| 46133 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 haftmann parents: 
46125diff
changeset | 2488 | lemma (in lattice) Inf_fin_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 | 2489 | "Inf_fin (set (x # xs)) = fold inf xs x" | 
| 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 haftmann parents: 
46125diff
changeset | 2490 | proof - | 
| 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 haftmann parents: 
46125diff
changeset | 2491 | interpret ab_semigroup_idem_mult "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 | 2492 | by (fact ab_semigroup_idem_mult_inf) | 
| 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 haftmann parents: 
46125diff
changeset | 2493 | show ?thesis | 
| 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 haftmann parents: 
46125diff
changeset | 2494 | by (simp add: Inf_fin_def fold1_set_fold del: set.simps) | 
| 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 haftmann parents: 
46125diff
changeset | 2495 | qed | 
| 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 haftmann parents: 
46125diff
changeset | 2496 | |
| 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 haftmann parents: 
46125diff
changeset | 2497 | lemma (in lattice) Sup_fin_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 | 2498 | "Sup_fin (set (x # xs)) = fold sup xs x" | 
| 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 haftmann parents: 
46125diff
changeset | 2499 | proof - | 
| 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 haftmann parents: 
46125diff
changeset | 2500 | interpret ab_semigroup_idem_mult "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 | 2501 | by (fact ab_semigroup_idem_mult_sup) | 
| 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 haftmann parents: 
46125diff
changeset | 2502 | show ?thesis | 
| 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 haftmann parents: 
46125diff
changeset | 2503 | by (simp add: Sup_fin_def fold1_set_fold del: set.simps) | 
| 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 haftmann parents: 
46125diff
changeset | 2504 | qed | 
| 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 haftmann parents: 
46125diff
changeset | 2505 | |
| 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 haftmann parents: 
46125diff
changeset | 2506 | lemma (in linorder) Min_fin_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 | 2507 | "Min (set (x # xs)) = fold min xs x" | 
| 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 haftmann parents: 
46125diff
changeset | 2508 | proof - | 
| 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 haftmann parents: 
46125diff
changeset | 2509 | interpret ab_semigroup_idem_mult "min :: '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 | 2510 | by (fact ab_semigroup_idem_mult_min) | 
| 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 haftmann parents: 
46125diff
changeset | 2511 | show ?thesis | 
| 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 haftmann parents: 
46125diff
changeset | 2512 | by (simp add: Min_def fold1_set_fold del: set.simps) | 
| 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 haftmann parents: 
46125diff
changeset | 2513 | qed | 
| 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 haftmann parents: 
46125diff
changeset | 2514 | |
| 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 haftmann parents: 
46125diff
changeset | 2515 | lemma (in linorder) Max_fin_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 | 2516 | "Max (set (x # xs)) = fold max xs x" | 
| 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 haftmann parents: 
46125diff
changeset | 2517 | proof - | 
| 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 haftmann parents: 
46125diff
changeset | 2518 | interpret ab_semigroup_idem_mult "max :: '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 | 2519 | by (fact ab_semigroup_idem_mult_max) | 
| 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 haftmann parents: 
46125diff
changeset | 2520 | show ?thesis | 
| 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 haftmann parents: 
46125diff
changeset | 2521 | by (simp add: Max_def fold1_set_fold del: set.simps) | 
| 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 haftmann parents: 
46125diff
changeset | 2522 | qed | 
| 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 haftmann parents: 
46125diff
changeset | 2523 | |
| 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 haftmann parents: 
46125diff
changeset | 2524 | 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 | 2525 | "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 | 2526 | proof - | 
| 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 haftmann parents: 
46125diff
changeset | 2527 | 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 | 2528 | 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 | 2529 | 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 | 2530 | qed | 
| 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 haftmann parents: 
46125diff
changeset | 2531 | |
| 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 haftmann parents: 
46125diff
changeset | 2532 | 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 | 2533 | "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 | 2534 | proof - | 
| 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 haftmann parents: 
46125diff
changeset | 2535 | 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 | 2536 | 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 | 2537 | 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 | 2538 | qed | 
| 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 haftmann parents: 
46125diff
changeset | 2539 | |
| 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 haftmann parents: 
46125diff
changeset | 2540 | 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 | 2541 | "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 | 2542 | 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 | 2543 | |
| 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 haftmann parents: 
46125diff
changeset | 2544 | 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 | 2545 | "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 | 2546 | 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 | 2547 | |
| 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 haftmann parents: 
46125diff
changeset | 2548 | |
| 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 haftmann parents: 
46125diff
changeset | 2549 | 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 | 2550 | |
| 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 haftmann parents: 
46125diff
changeset | 2551 | 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 | 2552 | |
| 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 haftmann parents: 
46125diff
changeset | 2553 | lemma foldr_foldl: -- {* The ``Third Duality Theorem'' in Bird \& Wadler: *}
 | 
| 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 haftmann parents: 
46125diff
changeset | 2554 | "foldr f xs a = foldl (\<lambda>x y. f y x) a (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 | 2555 | by (simp add: foldr_def foldl_def) | 
| 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 haftmann parents: 
46125diff
changeset | 2556 | |
| 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 haftmann parents: 
46125diff
changeset | 2557 | lemma foldl_foldr: | 
| 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 haftmann parents: 
46125diff
changeset | 2558 | "foldl f a xs = foldr (\<lambda>x y. f y x) (rev xs) a" | 
| 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 haftmann parents: 
46125diff
changeset | 2559 | by (simp add: foldr_def foldl_def) | 
| 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 haftmann parents: 
46125diff
changeset | 2560 | |
| 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 haftmann parents: 
46125diff
changeset | 2561 | 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 | 2562 | 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 | 2563 | shows "foldr f 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 | 2564 | using assms unfolding foldr_def by (rule 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 | 2565 | |
| 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 haftmann parents: 
46125diff
changeset | 2566 | lemma | 
| 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 haftmann parents: 
46125diff
changeset | 2567 | foldr_Nil [code, simp]: "foldr f [] = id" | 
| 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 haftmann parents: 
46125diff
changeset | 2568 | and foldr_Cons [code, simp]: "foldr f (x # xs) = f x \<circ> foldr 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 | 2569 | by (simp_all add: foldr_def) | 
| 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 haftmann parents: 
46125diff
changeset | 2570 | |
| 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 haftmann parents: 
46125diff
changeset | 2571 | lemma | 
| 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 haftmann parents: 
46125diff
changeset | 2572 | foldl_Nil [simp]: "foldl f a [] = a" | 
| 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 haftmann parents: 
46125diff
changeset | 2573 | and foldl_Cons [simp]: "foldl f a (x # xs) = foldl f (f a x) xs" | 
| 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 haftmann parents: 
46125diff
changeset | 2574 | by (simp_all add: foldl_def) | 
| 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 haftmann parents: 
46125diff
changeset | 2575 | |
| 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 haftmann parents: 
46125diff
changeset | 2576 | 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 | 2577 | "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" | 
| 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 haftmann parents: 
46125diff
changeset | 2578 | by (auto simp add: foldr_def intro!: fold_cong) | 
| 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 haftmann parents: 
46125diff
changeset | 2579 | |
| 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 haftmann parents: 
46125diff
changeset | 2580 | 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 | 2581 | "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" | 
| 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 haftmann parents: 
46125diff
changeset | 2582 | by (auto simp add: foldl_def intro!: fold_cong) | 
| 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 haftmann parents: 
46125diff
changeset | 2583 | |
| 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 haftmann parents: 
46125diff
changeset | 2584 | 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 | 2585 | "foldr f (xs @ ys) a = foldr f xs (foldr f ys a)" | 
| 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 haftmann parents: 
46125diff
changeset | 2586 | by (simp add: foldr_def) | 
| 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 haftmann parents: 
46125diff
changeset | 2587 | |
| 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 haftmann parents: 
46125diff
changeset | 2588 | 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 | 2589 | "foldl f a (xs @ ys) = foldl f (foldl f a xs) ys" | 
| 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 haftmann parents: 
46125diff
changeset | 2590 | by (simp add: foldl_def) | 
| 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 haftmann parents: 
46125diff
changeset | 2591 | |
| 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 haftmann parents: 
46125diff
changeset | 2592 | 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 | 2593 | "foldr g (map f xs) a = foldr (g o f) xs a" | 
| 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 haftmann parents: 
46125diff
changeset | 2594 | by (simp add: foldr_def fold_map rev_map) | 
| 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 haftmann parents: 
46125diff
changeset | 2595 | |
| 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 haftmann parents: 
46125diff
changeset | 2596 | 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 | 2597 | "foldl g a (map f xs) = foldl (\<lambda>a x. g a (f x)) a xs" | 
| 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 haftmann parents: 
46125diff
changeset | 2598 | by (simp add: foldl_def fold_map comp_def) | 
| 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 haftmann parents: 
46125diff
changeset | 2599 | |
| 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 haftmann parents: 
46125diff
changeset | 2600 | text {* Executing operations in terms of @{const foldr} -- tail-recursive! *}
 | 
| 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 haftmann parents: 
46125diff
changeset | 2601 | |
| 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 haftmann parents: 
46125diff
changeset | 2602 | 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 | 2603 | "concat xss = foldr append xss []" | 
| 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 haftmann parents: 
46125diff
changeset | 2604 | by (simp add: fold_append_concat_rev foldr_def) | 
| 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 haftmann parents: 
46125diff
changeset | 2605 | |
| 46156 | 2606 | lemma minus_set_foldr [code]: | 
| 2607 | "A - set xs = foldr Set.remove xs A" | |
| 2608 | proof - | |
| 2609 | have "\<And>x y :: 'a. Set.remove y \<circ> Set.remove x = Set.remove x \<circ> Set.remove y" | |
| 2610 | by (auto simp add: remove_def) | |
| 2611 | then show ?thesis by (simp add: minus_set_fold foldr_fold) | |
| 2612 | qed | |
| 2613 | ||
| 2614 | lemma subtract_coset_filter [code]: | |
| 2615 | "A - List.coset xs = set (List.filter (\<lambda>x. x \<in> A) xs)" | |
| 2616 | by auto | |
| 2617 | ||
| 2618 | lemma union_set_foldr [code]: | |
| 46147 
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
 haftmann parents: 
46143diff
changeset | 2619 | "set xs \<union> A = foldr Set.insert xs A" | 
| 
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
 haftmann parents: 
46143diff
changeset | 2620 | proof - | 
| 
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
 haftmann parents: 
46143diff
changeset | 2621 | have "\<And>x y :: 'a. insert y \<circ> insert x = insert x \<circ> insert y" | 
| 
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
 haftmann parents: 
46143diff
changeset | 2622 | by auto | 
| 
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
 haftmann parents: 
46143diff
changeset | 2623 | then show ?thesis by (simp add: union_set_fold foldr_fold) | 
| 
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
 haftmann parents: 
46143diff
changeset | 2624 | qed | 
| 
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
 haftmann parents: 
46143diff
changeset | 2625 | |
| 46156 | 2626 | lemma union_coset_foldr [code]: | 
| 2627 | "List.coset xs \<union> A = List.coset (List.filter (\<lambda>x. x \<notin> A) xs)" | |
| 2628 | by auto | |
| 2629 | ||
| 2630 | lemma inter_set_filer [code]: | |
| 2631 | "A \<inter> set xs = set (List.filter (\<lambda>x. x \<in> A) xs)" | |
| 2632 | by auto | |
| 2633 | ||
| 2634 | lemma inter_coset_foldr [code]: | |
| 2635 | "A \<inter> List.coset xs = foldr Set.remove xs A" | |
| 2636 | by (simp add: Diff_eq [symmetric] minus_set_foldr) | |
| 46147 
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
 haftmann parents: 
46143diff
changeset | 2637 | |
| 46133 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 haftmann parents: 
46125diff
changeset | 2638 | lemma (in lattice) Inf_fin_set_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 | 2639 | "Inf_fin (set (x # xs)) = foldr inf xs x" | 
| 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 haftmann parents: 
46125diff
changeset | 2640 | by (simp add: Inf_fin_set_fold ac_simps foldr_fold fun_eq_iff del: set.simps) | 
| 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 haftmann parents: 
46125diff
changeset | 2641 | |
| 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 haftmann parents: 
46125diff
changeset | 2642 | lemma (in lattice) Sup_fin_set_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 | 2643 | "Sup_fin (set (x # xs)) = foldr sup xs x" | 
| 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 haftmann parents: 
46125diff
changeset | 2644 | by (simp add: Sup_fin_set_fold ac_simps foldr_fold fun_eq_iff del: set.simps) | 
| 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 haftmann parents: 
46125diff
changeset | 2645 | |
| 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 haftmann parents: 
46125diff
changeset | 2646 | lemma (in linorder) Min_fin_set_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 | 2647 | "Min (set (x # xs)) = foldr min xs x" | 
| 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 haftmann parents: 
46125diff
changeset | 2648 | by (simp add: Min_fin_set_fold ac_simps foldr_fold fun_eq_iff del: set.simps) | 
| 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 haftmann parents: 
46125diff
changeset | 2649 | |
| 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 haftmann parents: 
46125diff
changeset | 2650 | lemma (in linorder) Max_fin_set_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 | 2651 | "Max (set (x # xs)) = foldr max xs x" | 
| 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 haftmann parents: 
46125diff
changeset | 2652 | by (simp add: Max_fin_set_fold ac_simps foldr_fold fun_eq_iff del: set.simps) | 
| 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 haftmann parents: 
46125diff
changeset | 2653 | |
| 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 haftmann parents: 
46125diff
changeset | 2654 | lemma (in complete_lattice) Inf_set_foldr: | 
| 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 haftmann parents: 
46125diff
changeset | 2655 | "Inf (set xs) = foldr 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 | 2656 | by (simp add: Inf_set_fold ac_simps foldr_fold 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 | 2657 | |
| 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 haftmann parents: 
46125diff
changeset | 2658 | lemma (in complete_lattice) Sup_set_foldr: | 
| 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 haftmann parents: 
46125diff
changeset | 2659 | "Sup (set xs) = foldr 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 | 2660 | by (simp add: Sup_set_fold ac_simps foldr_fold 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 | 2661 | |
| 46156 | 2662 | declare Inf_set_foldr [where 'a = "'a set", code] Sup_set_foldr [where 'a = "'a set", code] | 
| 2663 | ||
| 46133 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 haftmann parents: 
46125diff
changeset | 2664 | lemma (in complete_lattice) INF_set_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 | 2665 | "INFI (set xs) f = foldr (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 | 2666 | by (simp only: INF_def Inf_set_foldr foldr_map set_map [symmetric]) | 
| 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 haftmann parents: 
46125diff
changeset | 2667 | |
| 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 haftmann parents: 
46125diff
changeset | 2668 | lemma (in complete_lattice) SUP_set_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 | 2669 | "SUPR (set xs) f = foldr (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 | 2670 | by (simp only: SUP_def Sup_set_foldr foldr_map set_map [symmetric]) | 
| 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 haftmann parents: 
46125diff
changeset | 2671 | |
| 35115 | 2672 | |
| 24645 | 2673 | subsubsection {* @{text upt} *}
 | 
| 13114 | 2674 | |
| 17090 | 2675 | lemma upt_rec[code]: "[i..<j] = (if i<j then i#[Suc i..<j] else [])" | 
| 2676 | -- {* simp does not terminate! *}
 | |
| 13145 | 2677 | by (induct j) auto | 
| 13142 | 2678 | |
| 45607 | 2679 | lemmas upt_rec_number_of[simp] = upt_rec[of "number_of m" "number_of n"] for m n | 
| 32005 | 2680 | |
| 15425 | 2681 | lemma upt_conv_Nil [simp]: "j <= i ==> [i..<j] = []" | 
| 13145 | 2682 | by (subst upt_rec) simp | 
| 13114 | 2683 | |
| 15425 | 2684 | lemma upt_eq_Nil_conv[simp]: "([i..<j] = []) = (j = 0 \<or> j <= i)" | 
| 15281 | 2685 | by(induct j)simp_all | 
| 2686 | ||
| 2687 | lemma upt_eq_Cons_conv: | |
| 24526 | 2688 | "([i..<j] = x#xs) = (i < j & i = x & [i+1..<j] = xs)" | 
| 2689 | apply(induct j arbitrary: x xs) | |
| 15281 | 2690 | apply simp | 
| 2691 | apply(clarsimp simp add: append_eq_Cons_conv) | |
| 2692 | apply arith | |
| 2693 | done | |
| 2694 | ||
| 15425 | 2695 | lemma upt_Suc_append: "i <= j ==> [i..<(Suc j)] = [i..<j]@[j]" | 
| 13145 | 2696 | -- {* Only needed if @{text upt_Suc} is deleted from the simpset. *}
 | 
| 2697 | by simp | |
| 13114 | 2698 | |
| 15425 | 2699 | lemma upt_conv_Cons: "i < j ==> [i..<j] = i # [Suc i..<j]" | 
| 26734 | 2700 | by (simp add: upt_rec) | 
| 13114 | 2701 | |
| 15425 | 2702 | lemma upt_add_eq_append: "i<=j ==> [i..<j+k] = [i..<j]@[j..<j+k]" | 
| 13145 | 2703 | -- {* LOOPS as a simprule, since @{text "j <= j"}. *}
 | 
| 2704 | by (induct k) auto | |
| 13114 | 2705 | |
| 15425 | 2706 | lemma length_upt [simp]: "length [i..<j] = j - i" | 
| 13145 | 2707 | by (induct j) (auto simp add: Suc_diff_le) | 
| 13114 | 2708 | |
| 15425 | 2709 | lemma nth_upt [simp]: "i + k < j ==> [i..<j] ! k = i + k" | 
| 13145 | 2710 | apply (induct j) | 
| 2711 | apply (auto simp add: less_Suc_eq nth_append split: nat_diff_split) | |
| 2712 | done | |
| 13114 | 2713 | |
| 17906 | 2714 | |
| 2715 | lemma hd_upt[simp]: "i < j \<Longrightarrow> hd[i..<j] = i" | |
| 2716 | by(simp add:upt_conv_Cons) | |
| 2717 | ||
| 2718 | lemma last_upt[simp]: "i < j \<Longrightarrow> last[i..<j] = j - 1" | |
| 2719 | apply(cases j) | |
| 2720 | apply simp | |
| 2721 | by(simp add:upt_Suc_append) | |
| 2722 | ||
| 24526 | 2723 | lemma take_upt [simp]: "i+m <= n ==> take m [i..<n] = [i..<i+m]" | 
| 2724 | apply (induct m arbitrary: i, simp) | |
| 13145 | 2725 | apply (subst upt_rec) | 
| 2726 | apply (rule sym) | |
| 2727 | apply (subst upt_rec) | |
| 2728 | apply (simp del: upt.simps) | |
| 2729 | done | |
| 3507 | 2730 | |
| 17501 | 2731 | lemma drop_upt[simp]: "drop m [i..<j] = [i+m..<j]" | 
| 2732 | apply(induct j) | |
| 2733 | apply auto | |
| 2734 | done | |
| 2735 | ||
| 24645 | 2736 | lemma map_Suc_upt: "map Suc [m..<n] = [Suc m..<Suc n]" | 
| 13145 | 2737 | by (induct n) auto | 
| 13114 | 2738 | |
| 24526 | 2739 | lemma nth_map_upt: "i < n-m ==> (map f [m..<n]) ! i = f(m+i)" | 
| 2740 | apply (induct n m arbitrary: i rule: diff_induct) | |
| 13145 | 2741 | prefer 3 apply (subst map_Suc_upt[symmetric]) | 
| 44921 | 2742 | apply (auto simp add: less_diff_conv) | 
| 13145 | 2743 | done | 
| 13114 | 2744 | |
| 13883 
0451e0fb3f22
Re-structured some proofs in order to get rid of rule_format attribute.
 berghofe parents: 
13863diff
changeset | 2745 | lemma nth_take_lemma: | 
| 24526 | 2746 | "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 | 2747 | (!!i. i < k --> xs!i = ys!i) ==> take k xs = take k ys" | 
| 24526 | 2748 | apply (atomize, induct k arbitrary: xs ys) | 
| 14208 | 2749 | apply (simp_all add: less_Suc_eq_0_disj all_conj_distrib, clarify) | 
| 13145 | 2750 | txt {* Both lists must be non-empty *}
 | 
| 14208 | 2751 | apply (case_tac xs, simp) | 
| 2752 | apply (case_tac ys, clarify) | |
| 13145 | 2753 | apply (simp (no_asm_use)) | 
| 2754 | apply clarify | |
| 2755 | txt {* prenexing's needed, not miniscoping *}
 | |
| 2756 | apply (simp (no_asm_use) add: all_simps [symmetric] del: all_simps) | |
| 2757 | apply blast | |
| 2758 | done | |
| 13114 | 2759 | |
| 2760 | lemma nth_equalityI: | |
| 2761 | "[| length xs = length ys; ALL i < length xs. xs!i = ys!i |] ==> xs = ys" | |
| 44921 | 2762 | by (frule nth_take_lemma [OF le_refl eq_imp_le]) simp_all | 
| 13142 | 2763 | |
| 24796 | 2764 | lemma map_nth: | 
| 2765 | "map (\<lambda>i. xs ! i) [0..<length xs] = xs" | |
| 2766 | by (rule nth_equalityI, auto) | |
| 2767 | ||
| 13863 | 2768 | (* needs nth_equalityI *) | 
| 2769 | lemma list_all2_antisym: | |
| 2770 | "\<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> | |
| 2771 | \<Longrightarrow> xs = ys" | |
| 2772 | apply (simp add: list_all2_conv_all_nth) | |
| 14208 | 2773 | apply (rule nth_equalityI, blast, simp) | 
| 13863 | 2774 | done | 
| 2775 | ||
| 13142 | 2776 | lemma take_equalityI: "(\<forall>i. take i xs = take i ys) ==> xs = ys" | 
| 13145 | 2777 | -- {* The famous take-lemma. *}
 | 
| 2778 | apply (drule_tac x = "max (length xs) (length ys)" in spec) | |
| 44921 | 2779 | apply (simp add: le_max_iff_disj) | 
| 13145 | 2780 | done | 
| 13142 | 2781 | |
| 2782 | ||
| 15302 | 2783 | lemma take_Cons': | 
| 2784 | "take n (x # xs) = (if n = 0 then [] else x # take (n - 1) xs)" | |
| 2785 | by (cases n) simp_all | |
| 2786 | ||
| 2787 | lemma drop_Cons': | |
| 2788 | "drop n (x # xs) = (if n = 0 then x # xs else drop (n - 1) xs)" | |
| 2789 | by (cases n) simp_all | |
| 2790 | ||
| 2791 | lemma nth_Cons': "(x # xs)!n = (if n = 0 then x else xs!(n - 1))" | |
| 2792 | by (cases n) simp_all | |
| 2793 | ||
| 45607 | 2794 | lemmas take_Cons_number_of = take_Cons'[of "number_of v"] for v | 
| 2795 | lemmas drop_Cons_number_of = drop_Cons'[of "number_of v"] for v | |
| 2796 | lemmas nth_Cons_number_of = nth_Cons'[of _ _ "number_of v"] for v | |
| 18622 | 2797 | |
| 2798 | declare take_Cons_number_of [simp] | |
| 2799 | drop_Cons_number_of [simp] | |
| 2800 | nth_Cons_number_of [simp] | |
| 15302 | 2801 | |
| 2802 | ||
| 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 | 2803 | 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 | 2804 | |
| 
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 | 2805 | (* FIXME make upto tail recursive? *) | 
| 
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 | 2806 | |
| 
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 | 2807 | function upto :: "int \<Rightarrow> int \<Rightarrow> int list" ("(1[_../_])") where
 | 
| 
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 | 2808 | "upto i j = (if i \<le> j then i # [i+1..j] else [])" | 
| 
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 | 2809 | 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 | 2810 | 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 | 2811 | 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 | 2812 | |
| 
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 | 2813 | declare upto.simps[code, simp del] | 
| 
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 | 2814 | |
| 45607 | 2815 | lemmas upto_rec_number_of[simp] = upto.simps[of "number_of m" "number_of 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 | 2816 | |
| 
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 | 2817 | 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 | 2818 | 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 | 2819 | |
| 
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 | 2820 | 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 | 2821 | proof(induct i j rule:upto.induct) | 
| 
edbf0a86fb1c
adding simproc to rewrite list comprehensions to set comprehensions; adopting proofs
 bulwahn parents: 
41372diff
changeset | 2822 | case (1 i j) | 
| 
edbf0a86fb1c
adding simproc to rewrite list comprehensions to set comprehensions; adopting proofs
 bulwahn parents: 
41372diff
changeset | 2823 | from this show ?case | 
| 
edbf0a86fb1c
adding simproc to rewrite list comprehensions to set comprehensions; adopting proofs
 bulwahn parents: 
41372diff
changeset | 2824 | 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 | 2825 | 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 | 2826 | |
| 
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 | 2827 | |
| 15392 | 2828 | subsubsection {* @{text "distinct"} and @{text remdups} *}
 | 
| 13142 | 2829 | |
| 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 | 2830 | 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 | 2831 | "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 | 2832 | 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 | 2833 | |
| 13142 | 2834 | lemma distinct_append [simp]: | 
| 13145 | 2835 | "distinct (xs @ ys) = (distinct xs \<and> distinct ys \<and> set xs \<inter> set ys = {})"
 | 
| 2836 | by (induct xs) auto | |
| 13142 | 2837 | |
| 15305 | 2838 | lemma distinct_rev[simp]: "distinct(rev xs) = distinct xs" | 
| 2839 | by(induct xs) auto | |
| 2840 | ||
| 13142 | 2841 | lemma set_remdups [simp]: "set (remdups xs) = set xs" | 
| 13145 | 2842 | by (induct xs) (auto simp add: insert_absorb) | 
| 13142 | 2843 | |
| 2844 | lemma distinct_remdups [iff]: "distinct (remdups xs)" | |
| 13145 | 2845 | by (induct xs) auto | 
| 13142 | 2846 | |
| 25287 | 2847 | lemma distinct_remdups_id: "distinct xs ==> remdups xs = xs" | 
| 2848 | by (induct xs, auto) | |
| 2849 | ||
| 26734 | 2850 | lemma remdups_id_iff_distinct [simp]: "remdups xs = xs \<longleftrightarrow> distinct xs" | 
| 2851 | by (metis distinct_remdups distinct_remdups_id) | |
| 25287 | 2852 | |
| 24566 | 2853 | lemma finite_distinct_list: "finite A \<Longrightarrow> EX xs. set xs = A & distinct xs" | 
| 24632 | 2854 | by (metis distinct_remdups finite_list set_remdups) | 
| 24566 | 2855 | |
| 15072 | 2856 | 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 | 2857 | by (induct x, auto) | 
| 15072 | 2858 | |
| 2859 | lemma remdups_eq_nil_right_iff [simp]: "([] = remdups x) = (x = [])" | |
| 24349 | 2860 | by (induct x, auto) | 
| 15072 | 2861 | |
| 15245 | 2862 | lemma length_remdups_leq[iff]: "length(remdups xs) <= length xs" | 
| 2863 | by (induct xs) auto | |
| 2864 | ||
| 2865 | lemma length_remdups_eq[iff]: | |
| 2866 | "(length (remdups xs) = length xs) = (remdups xs = xs)" | |
| 2867 | apply(induct xs) | |
| 2868 | apply auto | |
| 2869 | apply(subgoal_tac "length (remdups xs) <= length xs") | |
| 2870 | apply arith | |
| 2871 | apply(rule length_remdups_leq) | |
| 2872 | done | |
| 2873 | ||
| 33945 | 2874 | lemma remdups_filter: "remdups(filter P xs) = filter P (remdups xs)" | 
| 2875 | apply(induct xs) | |
| 2876 | apply auto | |
| 2877 | done | |
| 18490 | 2878 | |
| 2879 | lemma distinct_map: | |
| 2880 | "distinct(map f xs) = (distinct xs & inj_on f (set xs))" | |
| 2881 | by (induct xs) auto | |
| 2882 | ||
| 2883 | ||
| 13142 | 2884 | lemma distinct_filter [simp]: "distinct xs ==> distinct (filter P xs)" | 
| 13145 | 2885 | by (induct xs) auto | 
| 13114 | 2886 | |
| 17501 | 2887 | lemma distinct_upt[simp]: "distinct[i..<j]" | 
| 2888 | by (induct j) auto | |
| 2889 | ||
| 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 | 2890 | 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 | 2891 | 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 | 2892 | 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 | 2893 | 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 | 2894 | 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 | 2895 | |
| 24526 | 2896 | lemma distinct_take[simp]: "distinct xs \<Longrightarrow> distinct (take i xs)" | 
| 2897 | apply(induct xs arbitrary: i) | |
| 17501 | 2898 | apply simp | 
| 2899 | apply (case_tac i) | |
| 2900 | apply simp_all | |
| 2901 | apply(blast dest:in_set_takeD) | |
| 2902 | done | |
| 2903 | ||
| 24526 | 2904 | lemma distinct_drop[simp]: "distinct xs \<Longrightarrow> distinct (drop i xs)" | 
| 2905 | apply(induct xs arbitrary: i) | |
| 17501 | 2906 | apply simp | 
| 2907 | apply (case_tac i) | |
| 2908 | apply simp_all | |
| 2909 | done | |
| 2910 | ||
| 2911 | lemma distinct_list_update: | |
| 2912 | assumes d: "distinct xs" and a: "a \<notin> set xs - {xs!i}"
 | |
| 2913 | shows "distinct (xs[i:=a])" | |
| 2914 | proof (cases "i < length xs") | |
| 2915 | case True | |
| 2916 |   with a have "a \<notin> set (take i xs @ xs ! i # drop (Suc i) xs) - {xs!i}"
 | |
| 2917 | apply (drule_tac id_take_nth_drop) by simp | |
| 2918 | with d True show ?thesis | |
| 2919 | apply (simp add: upd_conv_take_nth_drop) | |
| 2920 | apply (drule subst [OF id_take_nth_drop]) apply assumption | |
| 2921 | apply simp apply (cases "a = xs!i") apply simp by blast | |
| 2922 | next | |
| 2923 | case False with d show ?thesis by auto | |
| 2924 | qed | |
| 2925 | ||
| 31363 
7493b571b37d
Added theorems about distinct & concat, map & replicate and concat & replicate
 hoelzl parents: 
31264diff
changeset | 2926 | lemma distinct_concat: | 
| 
7493b571b37d
Added theorems about distinct & concat, map & replicate and concat & replicate
 hoelzl parents: 
31264diff
changeset | 2927 | assumes "distinct xs" | 
| 
7493b571b37d
Added theorems about distinct & concat, map & replicate and concat & replicate
 hoelzl parents: 
31264diff
changeset | 2928 | 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 | 2929 |   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 | 2930 | shows "distinct (concat xs)" | 
| 
7493b571b37d
Added theorems about distinct & concat, map & replicate and concat & replicate
 hoelzl parents: 
31264diff
changeset | 2931 | using assms by (induct xs) auto | 
| 17501 | 2932 | |
| 2933 | text {* It is best to avoid this indexed version of distinct, but
 | |
| 2934 | sometimes it is useful. *} | |
| 2935 | ||
| 13142 | 2936 | lemma distinct_conv_nth: | 
| 17501 | 2937 | "distinct xs = (\<forall>i < size xs. \<forall>j < size xs. i \<noteq> j --> xs!i \<noteq> xs!j)" | 
| 15251 | 2938 | apply (induct xs, simp, simp) | 
| 14208 | 2939 | apply (rule iffI, clarsimp) | 
| 13145 | 2940 | apply (case_tac i) | 
| 14208 | 2941 | apply (case_tac j, simp) | 
| 13145 | 2942 | apply (simp add: set_conv_nth) | 
| 2943 | 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 | 2944 | apply (clarsimp simp add: set_conv_nth, simp) | 
| 13145 | 2945 | apply (rule conjI) | 
| 24648 | 2946 | (*TOO SLOW | 
| 24632 | 2947 | apply (metis Zero_neq_Suc gr0_conv_Suc in_set_conv_nth lessI less_trans_Suc nth_Cons' nth_Cons_Suc) | 
| 24648 | 2948 | *) | 
| 2949 | apply (clarsimp simp add: set_conv_nth) | |
| 2950 | apply (erule_tac x = 0 in allE, simp) | |
| 2951 | apply (erule_tac x = "Suc i" in allE, simp, clarsimp) | |
| 25130 | 2952 | (*TOO SLOW | 
| 24632 | 2953 | apply (metis Suc_Suc_eq lessI less_trans_Suc nth_Cons_Suc) | 
| 25130 | 2954 | *) | 
| 2955 | apply (erule_tac x = "Suc i" in allE, simp) | |
| 2956 | apply (erule_tac x = "Suc j" in allE, simp) | |
| 13145 | 2957 | done | 
| 13114 | 2958 | |
| 18490 | 2959 | lemma nth_eq_iff_index_eq: | 
| 2960 | "\<lbrakk> distinct xs; i < length xs; j < length xs \<rbrakk> \<Longrightarrow> (xs!i = xs!j) = (i = j)" | |
| 2961 | by(auto simp: distinct_conv_nth) | |
| 2962 | ||
| 15110 
78b5636eabc7
Added a number of new thms and the new function remove1
 nipkow parents: 
15072diff
changeset | 2963 | lemma distinct_card: "distinct xs ==> card (set xs) = size xs" | 
| 24349 | 2964 | by (induct xs) auto | 
| 14388 | 2965 | |
| 15110 
78b5636eabc7
Added a number of new thms and the new function remove1
 nipkow parents: 
15072diff
changeset | 2966 | lemma card_distinct: "card (set xs) = size xs ==> distinct xs" | 
| 14388 | 2967 | proof (induct xs) | 
| 2968 | case Nil thus ?case by simp | |
| 2969 | next | |
| 2970 | case (Cons x xs) | |
| 2971 | show ?case | |
| 2972 | proof (cases "x \<in> set xs") | |
| 2973 | case False with Cons show ?thesis by simp | |
| 2974 | next | |
| 2975 | 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 | 2976 | have "card (set xs) = Suc (length xs)" | 
| 14388 | 2977 | by (simp add: card_insert_if split: split_if_asm) | 
| 2978 | moreover have "card (set xs) \<le> length xs" by (rule card_length) | |
| 2979 | ultimately have False by simp | |
| 2980 | thus ?thesis .. | |
| 2981 | qed | |
| 2982 | qed | |
| 2983 | ||
| 45115 
93c1ac6727a3
adding lemma to List library for executable equation of the (refl) transitive closure
 bulwahn parents: 
44928diff
changeset | 2984 | 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 | 2985 | by (induct xs) (auto) | 
| 
93c1ac6727a3
adding lemma to List library for executable equation of the (refl) transitive closure
 bulwahn parents: 
44928diff
changeset | 2986 | |
| 25287 | 2987 | lemma not_distinct_decomp: "~ distinct ws ==> EX xs ys zs y. ws = xs@[y]@ys@[y]@zs" | 
| 2988 | apply (induct n == "length ws" arbitrary:ws) apply simp | |
| 2989 | apply(case_tac ws) apply simp | |
| 2990 | apply (simp split:split_if_asm) | |
| 2991 | apply (metis Cons_eq_appendI eq_Nil_appendI split_list) | |
| 2992 | done | |
| 18490 | 2993 | |
| 45841 | 2994 | lemma not_distinct_conv_prefix: | 
| 2995 | defines "dec as xs y ys \<equiv> y \<in> set xs \<and> distinct xs \<and> as = xs @ y # ys" | |
| 2996 | shows "\<not>distinct as \<longleftrightarrow> (\<exists>xs y ys. dec as xs y ys)" (is "?L = ?R") | |
| 2997 | proof | |
| 2998 | assume "?L" then show "?R" | |
| 2999 | proof (induct "length as" arbitrary: as rule: less_induct) | |
| 3000 | case less | |
| 3001 | obtain xs ys zs y where decomp: "as = (xs @ y # ys) @ y # zs" | |
| 3002 | using not_distinct_decomp[OF less.prems] by auto | |
| 3003 | show ?case | |
| 3004 | proof (cases "distinct (xs @ y # ys)") | |
| 3005 | case True | |
| 3006 | with decomp have "dec as (xs @ y # ys) y zs" by (simp add: dec_def) | |
| 3007 | then show ?thesis by blast | |
| 3008 | next | |
| 3009 | case False | |
| 3010 | with less decomp obtain xs' y' ys' where "dec (xs @ y # ys) xs' y' ys'" | |
| 3011 | by atomize_elim auto | |
| 3012 | with decomp have "dec as xs' y' (ys' @ y # zs)" by (simp add: dec_def) | |
| 3013 | then show ?thesis by blast | |
| 3014 | qed | |
| 3015 | qed | |
| 3016 | qed (auto simp: dec_def) | |
| 3017 | ||
| 18490 | 3018 | lemma length_remdups_concat: | 
| 44921 | 3019 | "length (remdups (concat xss)) = card (\<Union>xs\<in>set xss. set xs)" | 
| 3020 | by (simp add: distinct_card [symmetric]) | |
| 17906 | 3021 | |
| 33639 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 3022 | 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 | 3023 | proof - | 
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 3024 | 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 | 3025 | 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 | 3026 | qed | 
| 17906 | 3027 | |
| 36275 | 3028 | lemma remdups_remdups: | 
| 3029 | "remdups (remdups xs) = remdups xs" | |
| 3030 | by (induct xs) simp_all | |
| 3031 | ||
| 36851 | 3032 | lemma distinct_butlast: | 
| 46500 
0196966d6d2d
removing unnecessary premises in theorems of List theory
 bulwahn parents: 
46448diff
changeset | 3033 | assumes "distinct xs" | 
| 36851 | 3034 | shows "distinct (butlast xs)" | 
| 46500 
0196966d6d2d
removing unnecessary premises in theorems of List theory
 bulwahn parents: 
46448diff
changeset | 3035 | proof (cases "xs = []") | 
| 
0196966d6d2d
removing unnecessary premises in theorems of List theory
 bulwahn parents: 
46448diff
changeset | 3036 | case False | 
| 
0196966d6d2d
removing unnecessary premises in theorems of List theory
 bulwahn parents: 
46448diff
changeset | 3037 | 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 | 3038 | with `distinct xs` show ?thesis by simp | 
| 
0196966d6d2d
removing unnecessary premises in theorems of List theory
 bulwahn parents: 
46448diff
changeset | 3039 | qed (auto) | 
| 36851 | 3040 | |
| 39728 | 3041 | lemma remdups_map_remdups: | 
| 3042 | "remdups (map f (remdups xs)) = remdups (map f xs)" | |
| 3043 | by (induct xs) simp_all | |
| 3044 | ||
| 39915 
ecf97cf3d248
turned distinct and sorted into inductive predicates: yields nice induction principles for free; more elegant proofs
 haftmann parents: 
39774diff
changeset | 3045 | lemma distinct_zipI1: | 
| 
ecf97cf3d248
turned distinct and sorted into inductive predicates: yields nice induction principles for free; more elegant proofs
 haftmann parents: 
39774diff
changeset | 3046 | assumes "distinct xs" | 
| 
ecf97cf3d248
turned distinct and sorted into inductive predicates: yields nice induction principles for free; more elegant proofs
 haftmann parents: 
39774diff
changeset | 3047 | 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 | 3048 | 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 | 3049 | 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 | 3050 | 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 | 3051 | 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 | 3052 | 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 | 3053 | 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 | 3054 | 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 | 3055 | qed | 
| 
ecf97cf3d248
turned distinct and sorted into inductive predicates: yields nice induction principles for free; more elegant proofs
 haftmann parents: 
39774diff
changeset | 3056 | |
| 
ecf97cf3d248
turned distinct and sorted into inductive predicates: yields nice induction principles for free; more elegant proofs
 haftmann parents: 
39774diff
changeset | 3057 | lemma distinct_zipI2: | 
| 
ecf97cf3d248
turned distinct and sorted into inductive predicates: yields nice induction principles for free; more elegant proofs
 haftmann parents: 
39774diff
changeset | 3058 | assumes "distinct ys" | 
| 
ecf97cf3d248
turned distinct and sorted into inductive predicates: yields nice induction principles for free; more elegant proofs
 haftmann parents: 
39774diff
changeset | 3059 | 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 | 3060 | 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 | 3061 | 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 | 3062 | 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 | 3063 | 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 | 3064 | 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 | 3065 | 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 | 3066 | 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 | 3067 | qed | 
| 
ecf97cf3d248
turned distinct and sorted into inductive predicates: yields nice induction principles for free; more elegant proofs
 haftmann parents: 
39774diff
changeset | 3068 | |
| 44635 
3d046864ebe6
added two lemmas about "distinct" to help Sledgehammer
 blanchet parents: 
44619diff
changeset | 3069 | (* The next two lemmas help Sledgehammer. *) | 
| 
3d046864ebe6
added two lemmas about "distinct" to help Sledgehammer
 blanchet parents: 
44619diff
changeset | 3070 | |
| 
3d046864ebe6
added two lemmas about "distinct" to help Sledgehammer
 blanchet parents: 
44619diff
changeset | 3071 | lemma distinct_singleton: "distinct [x]" by simp | 
| 
3d046864ebe6
added two lemmas about "distinct" to help Sledgehammer
 blanchet parents: 
44619diff
changeset | 3072 | |
| 
3d046864ebe6
added two lemmas about "distinct" to help Sledgehammer
 blanchet parents: 
44619diff
changeset | 3073 | lemma distinct_length_2_or_more: | 
| 
3d046864ebe6
added two lemmas about "distinct" to help Sledgehammer
 blanchet parents: 
44619diff
changeset | 3074 | "distinct (a # b # xs) \<longleftrightarrow> (a \<noteq> b \<and> distinct (a # xs) \<and> distinct (b # xs))" | 
| 
3d046864ebe6
added two lemmas about "distinct" to help Sledgehammer
 blanchet parents: 
44619diff
changeset | 3075 | by (metis distinct.simps(2) hd.simps hd_in_set list.simps(2) set_ConsD set_rev_mp set_subset_Cons) | 
| 
3d046864ebe6
added two lemmas about "distinct" to help Sledgehammer
 blanchet parents: 
44619diff
changeset | 3076 | |
| 37605 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 3077 | 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 | 3078 | |
| 39774 | 3079 | lemma (in monoid_add) listsum_simps [simp]: | 
| 3080 | "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 | 3081 | "listsum (x # xs) = x + listsum xs" | 
| 39774 | 3082 | by (simp_all add: listsum_def) | 
| 3083 | ||
| 3084 | lemma (in monoid_add) listsum_append [simp]: | |
| 3085 | "listsum (xs @ ys) = listsum xs + listsum ys" | |
| 3086 | by (induct xs) (simp_all add: add.assoc) | |
| 3087 | ||
| 3088 | lemma (in comm_monoid_add) listsum_rev [simp]: | |
| 3089 | "listsum (rev xs) = listsum xs" | |
| 46133 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 haftmann parents: 
46125diff
changeset | 3090 | by (simp add: listsum_def foldr_def fold_rev fun_eq_iff add_ac) | 
| 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 haftmann parents: 
46125diff
changeset | 3091 | |
| 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 haftmann parents: 
46125diff
changeset | 3092 | 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 | 3093 | "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 | 3094 | proof | 
| 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 haftmann parents: 
46125diff
changeset | 3095 | 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 | 3096 | 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 | 3097 | also have "\<dots> = fold plus (x # xs) 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 | 3098 | also have "\<dots> = foldr plus (rev xs @ [x]) 0" by (simp add: foldr_def) | 
| 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 haftmann parents: 
46125diff
changeset | 3099 | 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 | 3100 | 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 | 3101 | 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 | 3102 | qed | 
| 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 haftmann parents: 
46125diff
changeset | 3103 | |
| 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 haftmann parents: 
46125diff
changeset | 3104 | lemma (in semigroup_add) foldl_assoc: | 
| 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 haftmann parents: 
46125diff
changeset | 3105 | "foldl plus (x + y) zs = x + foldl plus 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 | 3106 | by (simp add: foldl_def fold_commute_apply [symmetric] fun_eq_iff add_assoc) | 
| 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 haftmann parents: 
46125diff
changeset | 3107 | |
| 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 haftmann parents: 
46125diff
changeset | 3108 | lemma (in ab_semigroup_add) foldr_conv_foldl: | 
| 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 haftmann parents: 
46125diff
changeset | 3109 | "foldr plus xs a = foldl plus a xs" | 
| 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 haftmann parents: 
46125diff
changeset | 3110 | by (simp add: foldl_def foldr_fold fun_eq_iff add_ac) | 
| 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 haftmann parents: 
46125diff
changeset | 3111 | |
| 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 haftmann parents: 
46125diff
changeset | 3112 | text {*
 | 
| 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 haftmann parents: 
46125diff
changeset | 3113 |   Note: @{text "n \<le> foldl (op +) n ns"} looks simpler, but is more
 | 
| 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 haftmann parents: 
46125diff
changeset | 3114 | difficult to use because it requires an additional transitivity step. | 
| 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 haftmann parents: 
46125diff
changeset | 3115 | *} | 
| 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 haftmann parents: 
46125diff
changeset | 3116 | |
| 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 haftmann parents: 
46125diff
changeset | 3117 | lemma start_le_sum: | 
| 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 haftmann parents: 
46125diff
changeset | 3118 | fixes m n :: nat | 
| 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 haftmann parents: 
46125diff
changeset | 3119 | shows "m \<le> n \<Longrightarrow> m \<le> foldl plus n ns" | 
| 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 haftmann parents: 
46125diff
changeset | 3120 | by (simp add: foldl_def add_commute 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 | 3121 | |
| 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 haftmann parents: 
46125diff
changeset | 3122 | lemma elem_le_sum: | 
| 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 haftmann parents: 
46125diff
changeset | 3123 | fixes m n :: nat | 
| 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 haftmann parents: 
46125diff
changeset | 3124 | shows "n \<in> set ns \<Longrightarrow> n \<le> foldl plus 0 ns" | 
| 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 haftmann parents: 
46125diff
changeset | 3125 | by (force intro: start_le_sum simp add: in_set_conv_decomp) | 
| 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 haftmann parents: 
46125diff
changeset | 3126 | |
| 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 haftmann parents: 
46125diff
changeset | 3127 | lemma sum_eq_0_conv [iff]: | 
| 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 haftmann parents: 
46125diff
changeset | 3128 | fixes m :: nat | 
| 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 haftmann parents: 
46125diff
changeset | 3129 | shows "foldl plus m ns = 0 \<longleftrightarrow> m = 0 \<and> (\<forall>n \<in> set ns. n = 0)" | 
| 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 haftmann parents: 
46125diff
changeset | 3130 | by (induct ns arbitrary: m) auto | 
| 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 haftmann parents: 
46125diff
changeset | 3131 | |
| 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 haftmann parents: 
46125diff
changeset | 3132 | 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 | 3133 | |
| 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 haftmann parents: 
46125diff
changeset | 3134 | syntax | 
| 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 haftmann parents: 
46125diff
changeset | 3135 |   "_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 | 3136 | 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 | 3137 |   "_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 | 3138 | 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 | 3139 |   "_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 | 3140 | |
| 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 haftmann parents: 
46125diff
changeset | 3141 | 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 | 3142 | "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 | 3143 | "\<Sum>x\<leftarrow>xs. b" == "CONST listsum (CONST map (%x. b) xs)" | 
| 39774 | 3144 | |
| 3145 | lemma (in comm_monoid_add) listsum_map_remove1: | |
| 3146 | "x \<in> set xs \<Longrightarrow> listsum (map f xs) = f x + listsum (map f (remove1 x xs))" | |
| 3147 | by (induct xs) (auto simp add: ac_simps) | |
| 3148 | ||
| 3149 | 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 | 3150 | "list_size f xs = listsum (map f xs) + size xs" | 
| 39774 | 3151 | by (induct xs) auto | 
| 3152 | ||
| 3153 | lemma (in monoid_add) length_concat: | |
| 3154 | "length (concat xss) = listsum (map length xss)" | |
| 3155 | by (induct xss) simp_all | |
| 3156 | ||
| 3157 | lemma (in monoid_add) listsum_map_filter: | |
| 3158 | 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 | 3159 | shows "listsum (map f (filter P xs)) = listsum (map f xs)" | 
| 39774 | 3160 | using assms by (induct xs) auto | 
| 3161 | ||
| 3162 | lemma (in monoid_add) distinct_listsum_conv_Setsum: | |
| 3163 | "distinct xs \<Longrightarrow> listsum xs = Setsum (set xs)" | |
| 3164 | by (induct xs) simp_all | |
| 3165 | ||
| 3166 | lemma listsum_eq_0_nat_iff_nat [simp]: | |
| 3167 | "listsum ns = (0::nat) \<longleftrightarrow> (\<forall>n \<in> set ns. n = 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 | 3168 | by (simp add: listsum_def foldr_conv_foldl) | 
| 39774 | 3169 | |
| 3170 | lemma elem_le_listsum_nat: | |
| 3171 | "k < size ns \<Longrightarrow> ns ! k \<le> listsum (ns::nat list)" | |
| 37605 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 3172 | apply(induct ns arbitrary: k) | 
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 3173 | apply simp | 
| 44890 
22f665a2e91c
new fastforce replacing fastsimp - less confusing name
 nipkow parents: 
44635diff
changeset | 3174 | apply(fastforce simp add:nth_Cons split: nat.split) | 
| 37605 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 3175 | done | 
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 3176 | |
| 39774 | 3177 | lemma listsum_update_nat: | 
| 3178 | "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 | 3179 | apply(induct ns arbitrary:k) | 
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 3180 | apply (auto split:nat.split) | 
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 3181 | 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 | 3182 | apply arith | 
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 3183 | done | 
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 3184 | |
| 39774 | 3185 | lemma (in monoid_add) listsum_triv: | 
| 3186 | "(\<Sum>x\<leftarrow>xs. r) = of_nat (length xs) * r" | |
| 37605 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 3187 | by (induct xs) (simp_all add: left_distrib) | 
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 3188 | |
| 39774 | 3189 | lemma (in monoid_add) listsum_0 [simp]: | 
| 3190 | "(\<Sum>x\<leftarrow>xs. 0) = 0" | |
| 37605 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 3191 | by (induct xs) (simp_all add: left_distrib) | 
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 3192 | |
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 3193 | text{* For non-Abelian groups @{text xs} needs to be reversed on one side: *}
 | 
| 39774 | 3194 | lemma (in ab_group_add) uminus_listsum_map: | 
| 3195 | "- listsum (map f xs) = listsum (map (uminus \<circ> f) xs)" | |
| 3196 | by (induct xs) simp_all | |
| 3197 | ||
| 3198 | lemma (in comm_monoid_add) listsum_addf: | |
| 3199 | "(\<Sum>x\<leftarrow>xs. f x + g x) = listsum (map f xs) + listsum (map g xs)" | |
| 3200 | by (induct xs) (simp_all add: algebra_simps) | |
| 3201 | ||
| 3202 | lemma (in ab_group_add) listsum_subtractf: | |
| 3203 | "(\<Sum>x\<leftarrow>xs. f x - g x) = listsum (map f xs) - listsum (map g xs)" | |
| 3204 | by (induct xs) (simp_all add: algebra_simps) | |
| 3205 | ||
| 3206 | lemma (in semiring_0) listsum_const_mult: | |
| 3207 | "(\<Sum>x\<leftarrow>xs. c * f x) = c * (\<Sum>x\<leftarrow>xs. f x)" | |
| 3208 | by (induct xs) (simp_all add: algebra_simps) | |
| 3209 | ||
| 3210 | lemma (in semiring_0) listsum_mult_const: | |
| 3211 | "(\<Sum>x\<leftarrow>xs. f x * c) = (\<Sum>x\<leftarrow>xs. f x) * c" | |
| 3212 | by (induct xs) (simp_all add: algebra_simps) | |
| 3213 | ||
| 3214 | lemma (in ordered_ab_group_add_abs) listsum_abs: | |
| 3215 | "\<bar>listsum xs\<bar> \<le> listsum (map abs xs)" | |
| 3216 | 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 | 3217 | |
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 3218 | lemma listsum_mono: | 
| 39774 | 3219 |   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 | 3220 | 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 | 3221 | by (induct xs) (simp, simp add: add_mono) | 
| 3222 | ||
| 3223 | 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 | 3224 | "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 | 3225 | by (induct xs) simp_all | 
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 3226 | |
| 39774 | 3227 | 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 | 3228 | "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 | 3229 | 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 | 3230 | |
| 39774 | 3231 | 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 | 3232 | "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 | 3233 | 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 | 3234 | |
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 3235 | text {* General equivalence between @{const listsum} and @{const setsum} *}
 | 
| 39774 | 3236 | 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 | 3237 | "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 | 3238 | 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 | 3239 | |
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 3240 | |
| 34978 
874150ddd50a
canonical insert operation; generalized lemma foldl_apply_inv to foldl_apply
 haftmann parents: 
34942diff
changeset | 3241 | subsubsection {* @{const insert} *}
 | 
| 
874150ddd50a
canonical insert operation; generalized lemma foldl_apply_inv to foldl_apply
 haftmann parents: 
34942diff
changeset | 3242 | |
| 
874150ddd50a
canonical insert operation; generalized lemma foldl_apply_inv to foldl_apply
 haftmann parents: 
34942diff
changeset | 3243 | lemma in_set_insert [simp]: | 
| 
874150ddd50a
canonical insert operation; generalized lemma foldl_apply_inv to foldl_apply
 haftmann parents: 
34942diff
changeset | 3244 | "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 | 3245 | by (simp add: List.insert_def) | 
| 
874150ddd50a
canonical insert operation; generalized lemma foldl_apply_inv to foldl_apply
 haftmann parents: 
34942diff
changeset | 3246 | |
| 
874150ddd50a
canonical insert operation; generalized lemma foldl_apply_inv to foldl_apply
 haftmann parents: 
34942diff
changeset | 3247 | lemma not_in_set_insert [simp]: | 
| 
874150ddd50a
canonical insert operation; generalized lemma foldl_apply_inv to foldl_apply
 haftmann parents: 
34942diff
changeset | 3248 | "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 | 3249 | by (simp add: List.insert_def) | 
| 
874150ddd50a
canonical insert operation; generalized lemma foldl_apply_inv to foldl_apply
 haftmann parents: 
34942diff
changeset | 3250 | |
| 
874150ddd50a
canonical insert operation; generalized lemma foldl_apply_inv to foldl_apply
 haftmann parents: 
34942diff
changeset | 3251 | lemma insert_Nil [simp]: | 
| 
874150ddd50a
canonical insert operation; generalized lemma foldl_apply_inv to foldl_apply
 haftmann parents: 
34942diff
changeset | 3252 | "List.insert x [] = [x]" | 
| 
874150ddd50a
canonical insert operation; generalized lemma foldl_apply_inv to foldl_apply
 haftmann parents: 
34942diff
changeset | 3253 | by simp | 
| 
874150ddd50a
canonical insert operation; generalized lemma foldl_apply_inv to foldl_apply
 haftmann parents: 
34942diff
changeset | 3254 | |
| 35295 | 3255 | lemma set_insert [simp]: | 
| 34978 
874150ddd50a
canonical insert operation; generalized lemma foldl_apply_inv to foldl_apply
 haftmann parents: 
34942diff
changeset | 3256 | "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 | 3257 | by (auto simp add: List.insert_def) | 
| 
874150ddd50a
canonical insert operation; generalized lemma foldl_apply_inv to foldl_apply
 haftmann parents: 
34942diff
changeset | 3258 | |
| 35295 | 3259 | lemma distinct_insert [simp]: | 
| 3260 | "distinct xs \<Longrightarrow> distinct (List.insert x xs)" | |
| 3261 | by (simp add: List.insert_def) | |
| 3262 | ||
| 36275 | 3263 | lemma insert_remdups: | 
| 3264 | "List.insert x (remdups xs) = remdups (List.insert x xs)" | |
| 3265 | by (simp add: List.insert_def) | |
| 3266 | ||
| 34978 
874150ddd50a
canonical insert operation; generalized lemma foldl_apply_inv to foldl_apply
 haftmann parents: 
34942diff
changeset | 3267 | |
| 15392 | 3268 | subsubsection {* @{text remove1} *}
 | 
| 15110 
78b5636eabc7
Added a number of new thms and the new function remove1
 nipkow parents: 
15072diff
changeset | 3269 | |
| 18049 | 3270 | lemma remove1_append: | 
| 3271 | "remove1 x (xs @ ys) = | |
| 3272 | (if x \<in> set xs then remove1 x xs @ ys else xs @ remove1 x ys)" | |
| 3273 | by (induct xs) auto | |
| 3274 | ||
| 36903 | 3275 | lemma remove1_commute: "remove1 x (remove1 y zs) = remove1 y (remove1 x zs)" | 
| 3276 | by (induct zs) auto | |
| 3277 | ||
| 23479 | 3278 | lemma in_set_remove1[simp]: | 
| 3279 | "a \<noteq> b \<Longrightarrow> a : set(remove1 b xs) = (a : set xs)" | |
| 3280 | apply (induct xs) | |
| 3281 | apply auto | |
| 3282 | done | |
| 3283 | ||
| 15110 
78b5636eabc7
Added a number of new thms and the new function remove1
 nipkow parents: 
15072diff
changeset | 3284 | 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 | 3285 | apply(induct xs) | 
| 
78b5636eabc7
Added a number of new thms and the new function remove1
 nipkow parents: 
15072diff
changeset | 3286 | apply simp | 
| 
78b5636eabc7
Added a number of new thms and the new function remove1
 nipkow parents: 
15072diff
changeset | 3287 | apply simp | 
| 
78b5636eabc7
Added a number of new thms and the new function remove1
 nipkow parents: 
15072diff
changeset | 3288 | apply blast | 
| 
78b5636eabc7
Added a number of new thms and the new function remove1
 nipkow parents: 
15072diff
changeset | 3289 | done | 
| 
78b5636eabc7
Added a number of new thms and the new function remove1
 nipkow parents: 
15072diff
changeset | 3290 | |
| 17724 | 3291 | 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 | 3292 | apply(induct xs) | 
| 
78b5636eabc7
Added a number of new thms and the new function remove1
 nipkow parents: 
15072diff
changeset | 3293 | apply simp | 
| 
78b5636eabc7
Added a number of new thms and the new function remove1
 nipkow parents: 
15072diff
changeset | 3294 | apply simp | 
| 
78b5636eabc7
Added a number of new thms and the new function remove1
 nipkow parents: 
15072diff
changeset | 3295 | apply blast | 
| 
78b5636eabc7
Added a number of new thms and the new function remove1
 nipkow parents: 
15072diff
changeset | 3296 | done | 
| 
78b5636eabc7
Added a number of new thms and the new function remove1
 nipkow parents: 
15072diff
changeset | 3297 | |
| 23479 | 3298 | 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 | 3299 | "length(remove1 x xs) = (if x : set xs then length xs - 1 else length xs)" | 
| 23479 | 3300 | apply (induct xs) | 
| 3301 | apply (auto dest!:length_pos_if_in_set) | |
| 3302 | done | |
| 3303 | ||
| 18049 | 3304 | lemma remove1_filter_not[simp]: | 
| 3305 | "\<not> P x \<Longrightarrow> remove1 x (filter P xs) = filter P xs" | |
| 3306 | by(induct xs) auto | |
| 3307 | ||
| 39073 | 3308 | lemma filter_remove1: | 
| 3309 | "filter Q (remove1 x xs) = remove1 x (filter Q xs)" | |
| 3310 | by (induct xs) auto | |
| 3311 | ||
| 15110 
78b5636eabc7
Added a number of new thms and the new function remove1
 nipkow parents: 
15072diff
changeset | 3312 | 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 | 3313 | apply(insert set_remove1_subset) | 
| 
78b5636eabc7
Added a number of new thms and the new function remove1
 nipkow parents: 
15072diff
changeset | 3314 | apply fast | 
| 
78b5636eabc7
Added a number of new thms and the new function remove1
 nipkow parents: 
15072diff
changeset | 3315 | done | 
| 
78b5636eabc7
Added a number of new thms and the new function remove1
 nipkow parents: 
15072diff
changeset | 3316 | |
| 
78b5636eabc7
Added a number of new thms and the new function remove1
 nipkow parents: 
15072diff
changeset | 3317 | 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 | 3318 | by (induct xs) simp_all | 
| 
78b5636eabc7
Added a number of new thms and the new function remove1
 nipkow parents: 
15072diff
changeset | 3319 | |
| 36275 | 3320 | lemma remove1_remdups: | 
| 3321 | "distinct xs \<Longrightarrow> remove1 x (remdups xs) = remdups (remove1 x xs)" | |
| 3322 | by (induct xs) simp_all | |
| 3323 | ||
| 37107 | 3324 | lemma remove1_idem: | 
| 3325 | assumes "x \<notin> set xs" | |
| 3326 | shows "remove1 x xs = xs" | |
| 3327 | using assms by (induct xs) simp_all | |
| 3328 | ||
| 13114 | 3329 | |
| 27693 | 3330 | subsubsection {* @{text removeAll} *}
 | 
| 3331 | ||
| 34978 
874150ddd50a
canonical insert operation; generalized lemma foldl_apply_inv to foldl_apply
 haftmann parents: 
34942diff
changeset | 3332 | lemma removeAll_filter_not_eq: | 
| 
874150ddd50a
canonical insert operation; generalized lemma foldl_apply_inv to foldl_apply
 haftmann parents: 
34942diff
changeset | 3333 | "removeAll x = filter (\<lambda>y. x \<noteq> y)" | 
| 
874150ddd50a
canonical insert operation; generalized lemma foldl_apply_inv to foldl_apply
 haftmann parents: 
34942diff
changeset | 3334 | proof | 
| 
874150ddd50a
canonical insert operation; generalized lemma foldl_apply_inv to foldl_apply
 haftmann parents: 
34942diff
changeset | 3335 | fix xs | 
| 
874150ddd50a
canonical insert operation; generalized lemma foldl_apply_inv to foldl_apply
 haftmann parents: 
34942diff
changeset | 3336 | 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 | 3337 | by (induct xs) auto | 
| 
874150ddd50a
canonical insert operation; generalized lemma foldl_apply_inv to foldl_apply
 haftmann parents: 
34942diff
changeset | 3338 | qed | 
| 
874150ddd50a
canonical insert operation; generalized lemma foldl_apply_inv to foldl_apply
 haftmann parents: 
34942diff
changeset | 3339 | |
| 27693 | 3340 | lemma removeAll_append[simp]: | 
| 3341 | "removeAll x (xs @ ys) = removeAll x xs @ removeAll x ys" | |
| 3342 | by (induct xs) auto | |
| 3343 | ||
| 3344 | lemma set_removeAll[simp]: "set(removeAll x xs) = set xs - {x}"
 | |
| 3345 | by (induct xs) auto | |
| 3346 | ||
| 3347 | lemma removeAll_id[simp]: "x \<notin> set xs \<Longrightarrow> removeAll x xs = xs" | |
| 3348 | by (induct xs) auto | |
| 3349 | ||
| 46448 
f1201fac7398
more specification of the quotient package in IsarRef
 Cezary Kaliszyk <cezarykaliszyk@gmail.com> parents: 
46440diff
changeset | 3350 | (* Needs count:: 'a \<Rightarrow> 'a list \<Rightarrow> nat | 
| 27693 | 3351 | lemma length_removeAll: | 
| 3352 | "length(removeAll x xs) = length xs - count x xs" | |
| 3353 | *) | |
| 3354 | ||
| 3355 | lemma removeAll_filter_not[simp]: | |
| 3356 | "\<not> P x \<Longrightarrow> removeAll x (filter P xs) = filter P xs" | |
| 3357 | by(induct xs) auto | |
| 3358 | ||
| 34978 
874150ddd50a
canonical insert operation; generalized lemma foldl_apply_inv to foldl_apply
 haftmann parents: 
34942diff
changeset | 3359 | lemma distinct_removeAll: | 
| 
874150ddd50a
canonical insert operation; generalized lemma foldl_apply_inv to foldl_apply
 haftmann parents: 
34942diff
changeset | 3360 | "distinct xs \<Longrightarrow> distinct (removeAll x xs)" | 
| 
874150ddd50a
canonical insert operation; generalized lemma foldl_apply_inv to foldl_apply
 haftmann parents: 
34942diff
changeset | 3361 | by (simp add: removeAll_filter_not_eq) | 
| 27693 | 3362 | |
| 3363 | lemma distinct_remove1_removeAll: | |
| 3364 | "distinct xs ==> remove1 x xs = removeAll x xs" | |
| 3365 | by (induct xs) simp_all | |
| 3366 | ||
| 3367 | lemma map_removeAll_inj_on: "inj_on f (insert x (set xs)) \<Longrightarrow> | |
| 3368 | map f (removeAll x xs) = removeAll (f x) (map f xs)" | |
| 3369 | by (induct xs) (simp_all add:inj_on_def) | |
| 3370 | ||
| 3371 | lemma map_removeAll_inj: "inj f \<Longrightarrow> | |
| 3372 | map f (removeAll x xs) = removeAll (f x) (map f xs)" | |
| 3373 | by(metis map_removeAll_inj_on subset_inj_on subset_UNIV) | |
| 3374 | ||
| 3375 | ||
| 15392 | 3376 | subsubsection {* @{text replicate} *}
 | 
| 13114 | 3377 | |
| 13142 | 3378 | lemma length_replicate [simp]: "length (replicate n x) = n" | 
| 13145 | 3379 | by (induct n) auto | 
| 13124 | 3380 | |
| 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 | 3381 | 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 | 3382 | 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 | 3383 | |
| 13142 | 3384 | lemma map_replicate [simp]: "map f (replicate n x) = replicate n (f x)" | 
| 13145 | 3385 | by (induct n) auto | 
| 13114 | 3386 | |
| 31363 
7493b571b37d
Added theorems about distinct & concat, map & replicate and concat & replicate
 hoelzl parents: 
31264diff
changeset | 3387 | lemma map_replicate_const: | 
| 
7493b571b37d
Added theorems about distinct & concat, map & replicate and concat & replicate
 hoelzl parents: 
31264diff
changeset | 3388 | "map (\<lambda> x. k) lst = replicate (length lst) k" | 
| 
7493b571b37d
Added theorems about distinct & concat, map & replicate and concat & replicate
 hoelzl parents: 
31264diff
changeset | 3389 | by (induct lst) auto | 
| 
7493b571b37d
Added theorems about distinct & concat, map & replicate and concat & replicate
 hoelzl parents: 
31264diff
changeset | 3390 | |
| 13114 | 3391 | lemma replicate_app_Cons_same: | 
| 13145 | 3392 | "(replicate n x) @ (x # xs) = x # replicate n x @ xs" | 
| 3393 | by (induct n) auto | |
| 13114 | 3394 | |
| 13142 | 3395 | lemma rev_replicate [simp]: "rev (replicate n x) = replicate n x" | 
| 14208 | 3396 | apply (induct n, simp) | 
| 13145 | 3397 | apply (simp add: replicate_app_Cons_same) | 
| 3398 | done | |
| 13114 | 3399 | |
| 13142 | 3400 | lemma replicate_add: "replicate (n + m) x = replicate n x @ replicate m x" | 
| 13145 | 3401 | by (induct n) auto | 
| 13114 | 3402 | |
| 16397 | 3403 | text{* Courtesy of Matthias Daum: *}
 | 
| 3404 | lemma append_replicate_commute: | |
| 3405 | "replicate n x @ replicate k x = replicate k x @ replicate n x" | |
| 3406 | apply (simp add: replicate_add [THEN sym]) | |
| 3407 | apply (simp add: add_commute) | |
| 3408 | done | |
| 3409 | ||
| 31080 | 3410 | text{* Courtesy of Andreas Lochbihler: *}
 | 
| 3411 | lemma filter_replicate: | |
| 3412 | "filter P (replicate n x) = (if P x then replicate n x else [])" | |
| 3413 | by(induct n) auto | |
| 3414 | ||
| 13142 | 3415 | lemma hd_replicate [simp]: "n \<noteq> 0 ==> hd (replicate n x) = x" | 
| 13145 | 3416 | by (induct n) auto | 
| 13114 | 3417 | |
| 46500 
0196966d6d2d
removing unnecessary premises in theorems of List theory
 bulwahn parents: 
46448diff
changeset | 3418 | lemma tl_replicate [simp]: "tl (replicate n x) = replicate (n - 1) x" | 
| 13145 | 3419 | by (induct n) auto | 
| 13114 | 3420 | |
| 13142 | 3421 | lemma last_replicate [simp]: "n \<noteq> 0 ==> last (replicate n x) = x" | 
| 13145 | 3422 | by (atomize (full), induct n) auto | 
| 13114 | 3423 | |
| 24526 | 3424 | lemma nth_replicate[simp]: "i < n ==> (replicate n x)!i = x" | 
| 3425 | apply (induct n arbitrary: i, simp) | |
| 13145 | 3426 | apply (simp add: nth_Cons split: nat.split) | 
| 3427 | done | |
| 13114 | 3428 | |
| 16397 | 3429 | text{* Courtesy of Matthias Daum (2 lemmas): *}
 | 
| 3430 | lemma take_replicate[simp]: "take i (replicate k x) = replicate (min i k) x" | |
| 3431 | apply (case_tac "k \<le> i") | |
| 3432 | apply (simp add: min_def) | |
| 3433 | apply (drule not_leE) | |
| 3434 | apply (simp add: min_def) | |
| 3435 | apply (subgoal_tac "replicate k x = replicate i x @ replicate (k - i) x") | |
| 3436 | apply simp | |
| 3437 | apply (simp add: replicate_add [symmetric]) | |
| 3438 | done | |
| 3439 | ||
| 24526 | 3440 | lemma drop_replicate[simp]: "drop i (replicate k x) = replicate (k-i) x" | 
| 3441 | apply (induct k arbitrary: i) | |
| 16397 | 3442 | apply simp | 
| 3443 | apply clarsimp | |
| 3444 | apply (case_tac i) | |
| 3445 | apply simp | |
| 3446 | apply clarsimp | |
| 3447 | done | |
| 3448 | ||
| 3449 | ||
| 13142 | 3450 | lemma set_replicate_Suc: "set (replicate (Suc n) x) = {x}"
 | 
| 13145 | 3451 | by (induct n) auto | 
| 13114 | 3452 | |
| 13142 | 3453 | lemma set_replicate [simp]: "n \<noteq> 0 ==> set (replicate n x) = {x}"
 | 
| 13145 | 3454 | by (fast dest!: not0_implies_Suc intro!: set_replicate_Suc) | 
| 13114 | 3455 | |
| 13142 | 3456 | lemma set_replicate_conv_if: "set (replicate n x) = (if n = 0 then {} else {x})"
 | 
| 13145 | 3457 | by auto | 
| 13114 | 3458 | |
| 37456 | 3459 | lemma in_set_replicate[simp]: "(x : set (replicate n y)) = (x = y & n \<noteq> 0)" | 
| 3460 | by (simp add: set_replicate_conv_if) | |
| 3461 | ||
| 37454 | 3462 | lemma Ball_set_replicate[simp]: | 
| 3463 | "(ALL x : set(replicate n a). P x) = (P a | n=0)" | |
| 3464 | by(simp add: set_replicate_conv_if) | |
| 3465 | ||
| 3466 | lemma Bex_set_replicate[simp]: | |
| 3467 | "(EX x : set(replicate n a). P x) = (P a & n\<noteq>0)" | |
| 3468 | by(simp add: set_replicate_conv_if) | |
| 13114 | 3469 | |
| 24796 | 3470 | lemma replicate_append_same: | 
| 3471 | "replicate i x @ [x] = x # replicate i x" | |
| 3472 | by (induct i) simp_all | |
| 3473 | ||
| 3474 | lemma map_replicate_trivial: | |
| 3475 | "map (\<lambda>i. x) [0..<i] = replicate i x" | |
| 3476 | by (induct i) (simp_all add: replicate_append_same) | |
| 3477 | ||
| 31363 
7493b571b37d
Added theorems about distinct & concat, map & replicate and concat & replicate
 hoelzl parents: 
31264diff
changeset | 3478 | lemma concat_replicate_trivial[simp]: | 
| 
7493b571b37d
Added theorems about distinct & concat, map & replicate and concat & replicate
 hoelzl parents: 
31264diff
changeset | 3479 | "concat (replicate i []) = []" | 
| 
7493b571b37d
Added theorems about distinct & concat, map & replicate and concat & replicate
 hoelzl parents: 
31264diff
changeset | 3480 | by (induct i) (auto simp add: map_replicate_const) | 
| 13114 | 3481 | |
| 28642 | 3482 | lemma replicate_empty[simp]: "(replicate n x = []) \<longleftrightarrow> n=0" | 
| 3483 | by (induct n) auto | |
| 3484 | ||
| 3485 | lemma empty_replicate[simp]: "([] = replicate n x) \<longleftrightarrow> n=0" | |
| 3486 | by (induct n) auto | |
| 3487 | ||
| 3488 | lemma replicate_eq_replicate[simp]: | |
| 3489 | "(replicate m x = replicate n y) \<longleftrightarrow> (m=n & (m\<noteq>0 \<longrightarrow> x=y))" | |
| 3490 | apply(induct m arbitrary: n) | |
| 3491 | apply simp | |
| 3492 | apply(induct_tac n) | |
| 3493 | apply auto | |
| 3494 | done | |
| 3495 | ||
| 39534 
c798d4f1b682
generalized lemma insort_remove1 to insort_key_remove1
 haftmann parents: 
39302diff
changeset | 3496 | lemma replicate_length_filter: | 
| 
c798d4f1b682
generalized lemma insort_remove1 to insort_key_remove1
 haftmann parents: 
39302diff
changeset | 3497 | "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 | 3498 | by (induct xs) auto | 
| 
c798d4f1b682
generalized lemma insort_remove1 to insort_key_remove1
 haftmann parents: 
39302diff
changeset | 3499 | |
| 42714 
fcba668b0839
add a lemma about commutative append to List.thy
 noschinl parents: 
42713diff
changeset | 3500 | lemma comm_append_are_replicate: | 
| 
fcba668b0839
add a lemma about commutative append to List.thy
 noschinl parents: 
42713diff
changeset | 3501 | fixes xs ys :: "'a list" | 
| 
fcba668b0839
add a lemma about commutative append to List.thy
 noschinl parents: 
42713diff
changeset | 3502 | assumes "xs \<noteq> []" "ys \<noteq> []" | 
| 
fcba668b0839
add a lemma about commutative append to List.thy
 noschinl parents: 
42713diff
changeset | 3503 | assumes "xs @ ys = ys @ xs" | 
| 
fcba668b0839
add a lemma about commutative append to List.thy
 noschinl parents: 
42713diff
changeset | 3504 | 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 | 3505 | using assms | 
| 
fcba668b0839
add a lemma about commutative append to List.thy
 noschinl parents: 
42713diff
changeset | 3506 | 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 | 3507 | case less | 
| 
fcba668b0839
add a lemma about commutative append to List.thy
 noschinl parents: 
42713diff
changeset | 3508 | |
| 
fcba668b0839
add a lemma about commutative append to List.thy
 noschinl parents: 
42713diff
changeset | 3509 | 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 | 3510 | 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 | 3511 | then have | 
| 
fcba668b0839
add a lemma about commutative append to List.thy
 noschinl parents: 
42713diff
changeset | 3512 | prems': "length xs' \<le> length ys'" | 
| 
fcba668b0839
add a lemma about commutative append to List.thy
 noschinl parents: 
42713diff
changeset | 3513 | "xs' @ ys' = ys' @ xs'" | 
| 
fcba668b0839
add a lemma about commutative append to List.thy
 noschinl parents: 
42713diff
changeset | 3514 | and "xs' \<noteq> []" | 
| 
fcba668b0839
add a lemma about commutative append to List.thy
 noschinl parents: 
42713diff
changeset | 3515 | and len: "length (xs @ ys) = length (xs' @ ys')" | 
| 
fcba668b0839
add a lemma about commutative append to List.thy
 noschinl parents: 
42713diff
changeset | 3516 | using less by (auto intro: less.hyps) | 
| 
fcba668b0839
add a lemma about commutative append to List.thy
 noschinl parents: 
42713diff
changeset | 3517 | |
| 
fcba668b0839
add a lemma about commutative append to List.thy
 noschinl parents: 
42713diff
changeset | 3518 | from prems' | 
| 
fcba668b0839
add a lemma about commutative append to List.thy
 noschinl parents: 
42713diff
changeset | 3519 | obtain ws where "ys' = xs' @ ws" | 
| 
fcba668b0839
add a lemma about commutative append to List.thy
 noschinl parents: 
42713diff
changeset | 3520 | by (auto simp: append_eq_append_conv2) | 
| 
fcba668b0839
add a lemma about commutative append to List.thy
 noschinl parents: 
42713diff
changeset | 3521 | |
| 
fcba668b0839
add a lemma about commutative append to List.thy
 noschinl parents: 
42713diff
changeset | 3522 | 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 | 3523 | proof (cases "ws = []") | 
| 
fcba668b0839
add a lemma about commutative append to List.thy
 noschinl parents: 
42713diff
changeset | 3524 | case True | 
| 
fcba668b0839
add a lemma about commutative append to List.thy
 noschinl parents: 
42713diff
changeset | 3525 | then have "concat (replicate 1 xs') = xs'" | 
| 
fcba668b0839
add a lemma about commutative append to List.thy
 noschinl parents: 
42713diff
changeset | 3526 | and "concat (replicate 1 xs') = ys'" | 
| 
fcba668b0839
add a lemma about commutative append to List.thy
 noschinl parents: 
42713diff
changeset | 3527 | using `ys' = xs' @ ws` by auto | 
| 
fcba668b0839
add a lemma about commutative append to List.thy
 noschinl parents: 
42713diff
changeset | 3528 | then show ?thesis by blast | 
| 
fcba668b0839
add a lemma about commutative append to List.thy
 noschinl parents: 
42713diff
changeset | 3529 | next | 
| 
fcba668b0839
add a lemma about commutative append to List.thy
 noschinl parents: 
42713diff
changeset | 3530 | case False | 
| 
fcba668b0839
add a lemma about commutative append to List.thy
 noschinl parents: 
42713diff
changeset | 3531 | from `ys' = xs' @ ws` and `xs' @ ys' = ys' @ xs'` | 
| 
fcba668b0839
add a lemma about commutative append to List.thy
 noschinl parents: 
42713diff
changeset | 3532 | have "xs' @ ws = ws @ xs'" by simp | 
| 
fcba668b0839
add a lemma about commutative append to List.thy
 noschinl parents: 
42713diff
changeset | 3533 | 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 | 3534 | 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 | 3535 | by (intro less.hyps) auto | 
| 
fcba668b0839
add a lemma about commutative append to List.thy
 noschinl parents: 
42713diff
changeset | 3536 | then obtain m n zs where "concat (replicate m zs) = xs'" | 
| 
fcba668b0839
add a lemma about commutative append to List.thy
 noschinl parents: 
42713diff
changeset | 3537 | and "concat (replicate n zs) = ws" by blast | 
| 
fcba668b0839
add a lemma about commutative append to List.thy
 noschinl parents: 
42713diff
changeset | 3538 | moreover | 
| 
fcba668b0839
add a lemma about commutative append to List.thy
 noschinl parents: 
42713diff
changeset | 3539 | then have "concat (replicate (m + n) zs) = ys'" | 
| 
fcba668b0839
add a lemma about commutative append to List.thy
 noschinl parents: 
42713diff
changeset | 3540 | using `ys' = xs' @ ws` | 
| 
fcba668b0839
add a lemma about commutative append to List.thy
 noschinl parents: 
42713diff
changeset | 3541 | by (simp add: replicate_add) | 
| 
fcba668b0839
add a lemma about commutative append to List.thy
 noschinl parents: 
42713diff
changeset | 3542 | ultimately | 
| 
fcba668b0839
add a lemma about commutative append to List.thy
 noschinl parents: 
42713diff
changeset | 3543 | show ?thesis by blast | 
| 
fcba668b0839
add a lemma about commutative append to List.thy
 noschinl parents: 
42713diff
changeset | 3544 | qed | 
| 
fcba668b0839
add a lemma about commutative append to List.thy
 noschinl parents: 
42713diff
changeset | 3545 | then show ?case | 
| 
fcba668b0839
add a lemma about commutative append to List.thy
 noschinl parents: 
42713diff
changeset | 3546 | using xs'_def ys'_def by metis | 
| 
fcba668b0839
add a lemma about commutative append to List.thy
 noschinl parents: 
42713diff
changeset | 3547 | qed | 
| 
fcba668b0839
add a lemma about commutative append to List.thy
 noschinl parents: 
42713diff
changeset | 3548 | |
| 
fcba668b0839
add a lemma about commutative append to List.thy
 noschinl parents: 
42713diff
changeset | 3549 | lemma comm_append_is_replicate: | 
| 
fcba668b0839
add a lemma about commutative append to List.thy
 noschinl parents: 
42713diff
changeset | 3550 | fixes xs ys :: "'a list" | 
| 
fcba668b0839
add a lemma about commutative append to List.thy
 noschinl parents: 
42713diff
changeset | 3551 | assumes "xs \<noteq> []" "ys \<noteq> []" | 
| 
fcba668b0839
add a lemma about commutative append to List.thy
 noschinl parents: 
42713diff
changeset | 3552 | assumes "xs @ ys = ys @ xs" | 
| 
fcba668b0839
add a lemma about commutative append to List.thy
 noschinl parents: 
42713diff
changeset | 3553 | 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 | 3554 | |
| 
fcba668b0839
add a lemma about commutative append to List.thy
 noschinl parents: 
42713diff
changeset | 3555 | proof - | 
| 
fcba668b0839
add a lemma about commutative append to List.thy
 noschinl parents: 
42713diff
changeset | 3556 | obtain m n zs where "concat (replicate m zs) = xs" | 
| 
fcba668b0839
add a lemma about commutative append to List.thy
 noschinl parents: 
42713diff
changeset | 3557 | and "concat (replicate n zs) = ys" | 
| 
fcba668b0839
add a lemma about commutative append to List.thy
 noschinl parents: 
42713diff
changeset | 3558 | using assms by (metis comm_append_are_replicate) | 
| 
fcba668b0839
add a lemma about commutative append to List.thy
 noschinl parents: 
42713diff
changeset | 3559 | 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 | 3560 | using `xs \<noteq> []` and `ys \<noteq> []` | 
| 
fcba668b0839
add a lemma about commutative append to List.thy
 noschinl parents: 
42713diff
changeset | 3561 | by (auto simp: replicate_add) | 
| 
fcba668b0839
add a lemma about commutative append to List.thy
 noschinl parents: 
42713diff
changeset | 3562 | then show ?thesis by blast | 
| 
fcba668b0839
add a lemma about commutative append to List.thy
 noschinl parents: 
42713diff
changeset | 3563 | qed | 
| 
fcba668b0839
add a lemma about commutative append to List.thy
 noschinl parents: 
42713diff
changeset | 3564 | |
| 28642 | 3565 | |
| 15392 | 3566 | subsubsection{*@{text rotate1} and @{text rotate}*}
 | 
| 15302 | 3567 | |
| 3568 | lemma rotate0[simp]: "rotate 0 = id" | |
| 3569 | by(simp add:rotate_def) | |
| 3570 | ||
| 3571 | lemma rotate_Suc[simp]: "rotate (Suc n) xs = rotate1(rotate n xs)" | |
| 3572 | by(simp add:rotate_def) | |
| 3573 | ||
| 3574 | lemma rotate_add: | |
| 3575 | "rotate (m+n) = rotate m o rotate n" | |
| 3576 | by(simp add:rotate_def funpow_add) | |
| 3577 | ||
| 3578 | lemma rotate_rotate: "rotate m (rotate n xs) = rotate (m+n) xs" | |
| 3579 | by(simp add:rotate_add) | |
| 3580 | ||
| 18049 | 3581 | lemma rotate1_rotate_swap: "rotate1 (rotate n xs) = rotate n (rotate1 xs)" | 
| 3582 | by(simp add:rotate_def funpow_swap1) | |
| 3583 | ||
| 15302 | 3584 | lemma rotate1_length01[simp]: "length xs <= 1 \<Longrightarrow> rotate1 xs = xs" | 
| 3585 | by(cases xs) simp_all | |
| 3586 | ||
| 3587 | lemma rotate_length01[simp]: "length xs <= 1 \<Longrightarrow> rotate n xs = xs" | |
| 3588 | apply(induct n) | |
| 3589 | apply simp | |
| 3590 | apply (simp add:rotate_def) | |
| 13145 | 3591 | done | 
| 13114 | 3592 | |
| 15302 | 3593 | 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 | 3594 | by (cases xs) simp_all | 
| 15302 | 3595 | |
| 3596 | lemma rotate_drop_take: | |
| 3597 | "rotate n xs = drop (n mod length xs) xs @ take (n mod length xs) xs" | |
| 3598 | apply(induct n) | |
| 3599 | apply simp | |
| 3600 | apply(simp add:rotate_def) | |
| 3601 | apply(cases "xs = []") | |
| 3602 | apply (simp) | |
| 3603 | apply(case_tac "n mod length xs = 0") | |
| 3604 | apply(simp add:mod_Suc) | |
| 3605 | apply(simp add: rotate1_hd_tl drop_Suc take_Suc) | |
| 3606 | apply(simp add:mod_Suc rotate1_hd_tl drop_Suc[symmetric] drop_tl[symmetric] | |
| 3607 | take_hd_drop linorder_not_le) | |
| 13145 | 3608 | done | 
| 13114 | 3609 | |
| 15302 | 3610 | lemma rotate_conv_mod: "rotate n xs = rotate (n mod length xs) xs" | 
| 3611 | by(simp add:rotate_drop_take) | |
| 3612 | ||
| 3613 | lemma rotate_id[simp]: "n mod length xs = 0 \<Longrightarrow> rotate n xs = xs" | |
| 3614 | by(simp add:rotate_drop_take) | |
| 3615 | ||
| 3616 | 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 | 3617 | by (cases xs) simp_all | 
| 15302 | 3618 | |
| 24526 | 3619 | lemma length_rotate[simp]: "length(rotate n xs) = length xs" | 
| 3620 | by (induct n arbitrary: xs) (simp_all add:rotate_def) | |
| 15302 | 3621 | |
| 3622 | 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 | 3623 | by (cases xs) auto | 
| 15302 | 3624 | |
| 3625 | lemma distinct_rotate[simp]: "distinct(rotate n xs) = distinct xs" | |
| 3626 | by (induct n) (simp_all add:rotate_def) | |
| 3627 | ||
| 3628 | lemma rotate_map: "rotate n (map f xs) = map f (rotate n xs)" | |
| 3629 | by(simp add:rotate_drop_take take_map drop_map) | |
| 3630 | ||
| 3631 | 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 | 3632 | by (cases xs) auto | 
| 15302 | 3633 | |
| 3634 | lemma set_rotate[simp]: "set(rotate n xs) = set xs" | |
| 3635 | by (induct n) (simp_all add:rotate_def) | |
| 3636 | ||
| 3637 | 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 | 3638 | by (cases xs) auto | 
| 15302 | 3639 | |
| 3640 | lemma rotate_is_Nil_conv[simp]: "(rotate n xs = []) = (xs = [])" | |
| 3641 | by (induct n) (simp_all add:rotate_def) | |
| 13114 | 3642 | |
| 15439 | 3643 | lemma rotate_rev: | 
| 3644 | "rotate n (rev xs) = rev(rotate (length xs - (n mod length xs)) xs)" | |
| 3645 | apply(simp add:rotate_drop_take rev_drop rev_take) | |
| 3646 | apply(cases "length xs = 0") | |
| 3647 | apply simp | |
| 3648 | apply(cases "n mod length xs = 0") | |
| 3649 | apply simp | |
| 3650 | apply(simp add:rotate_drop_take rev_drop rev_take) | |
| 3651 | done | |
| 3652 | ||
| 18423 | 3653 | lemma hd_rotate_conv_nth: "xs \<noteq> [] \<Longrightarrow> hd(rotate n xs) = xs!(n mod length xs)" | 
| 3654 | apply(simp add:rotate_drop_take hd_append hd_drop_conv_nth hd_conv_nth) | |
| 3655 | apply(subgoal_tac "length xs \<noteq> 0") | |
| 3656 | prefer 2 apply simp | |
| 3657 | using mod_less_divisor[of "length xs" n] by arith | |
| 3658 | ||
| 13114 | 3659 | |
| 15392 | 3660 | subsubsection {* @{text sublist} --- a generalization of @{text nth} to sets *}
 | 
| 13114 | 3661 | |
| 13142 | 3662 | lemma sublist_empty [simp]: "sublist xs {} = []"
 | 
| 13145 | 3663 | by (auto simp add: sublist_def) | 
| 13114 | 3664 | |
| 13142 | 3665 | lemma sublist_nil [simp]: "sublist [] A = []" | 
| 13145 | 3666 | by (auto simp add: sublist_def) | 
| 13114 | 3667 | |
| 15281 | 3668 | lemma length_sublist: | 
| 3669 |   "length(sublist xs I) = card{i. i < length xs \<and> i : I}"
 | |
| 3670 | by(simp add: sublist_def length_filter_conv_card cong:conj_cong) | |
| 3671 | ||
| 3672 | lemma sublist_shift_lemma_Suc: | |
| 24526 | 3673 | "map fst (filter (%p. P(Suc(snd p))) (zip xs is)) = | 
| 3674 | map fst (filter (%p. P(snd p)) (zip xs (map Suc is)))" | |
| 3675 | apply(induct xs arbitrary: "is") | |
| 15281 | 3676 | apply simp | 
| 3677 | apply (case_tac "is") | |
| 3678 | apply simp | |
| 3679 | apply simp | |
| 3680 | done | |
| 3681 | ||
| 13114 | 3682 | lemma sublist_shift_lemma: | 
| 23279 
e39dd93161d9
tuned list comprehension, changed filter syntax from : to <-
 nipkow parents: 
23246diff
changeset | 3683 | "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 | 3684 | map fst [p<-zip xs [0..<length xs] . snd p + i : A]" | 
| 13145 | 3685 | by (induct xs rule: rev_induct) (simp_all add: add_commute) | 
| 13114 | 3686 | |
| 3687 | lemma sublist_append: | |
| 15168 | 3688 |      "sublist (l @ l') A = sublist l A @ sublist l' {j. j + length l : A}"
 | 
| 13145 | 3689 | apply (unfold sublist_def) | 
| 14208 | 3690 | apply (induct l' rule: rev_induct, simp) | 
| 44921 | 3691 | apply (simp add: upt_add_eq_append[of 0] sublist_shift_lemma) | 
| 13145 | 3692 | apply (simp add: add_commute) | 
| 3693 | done | |
| 13114 | 3694 | |
| 3695 | lemma sublist_Cons: | |
| 13145 | 3696 | "sublist (x # l) A = (if 0:A then [x] else []) @ sublist l {j. Suc j : A}"
 | 
| 3697 | apply (induct l rule: rev_induct) | |
| 3698 | apply (simp add: sublist_def) | |
| 3699 | apply (simp del: append_Cons add: append_Cons[symmetric] sublist_append) | |
| 3700 | done | |
| 13114 | 3701 | |
| 24526 | 3702 | lemma set_sublist: "set(sublist xs I) = {xs!i|i. i<size xs \<and> i \<in> I}"
 | 
| 3703 | apply(induct xs arbitrary: I) | |
| 25162 | 3704 | apply(auto simp: sublist_Cons nth_Cons split:nat.split dest!: gr0_implies_Suc) | 
| 15281 | 3705 | done | 
| 3706 | ||
| 3707 | lemma set_sublist_subset: "set(sublist xs I) \<subseteq> set xs" | |
| 3708 | by(auto simp add:set_sublist) | |
| 3709 | ||
| 3710 | lemma notin_set_sublistI[simp]: "x \<notin> set xs \<Longrightarrow> x \<notin> set(sublist xs I)" | |
| 3711 | by(auto simp add:set_sublist) | |
| 3712 | ||
| 3713 | lemma in_set_sublistD: "x \<in> set(sublist xs I) \<Longrightarrow> x \<in> set xs" | |
| 3714 | by(auto simp add:set_sublist) | |
| 3715 | ||
| 13142 | 3716 | lemma sublist_singleton [simp]: "sublist [x] A = (if 0 : A then [x] else [])" | 
| 13145 | 3717 | by (simp add: sublist_Cons) | 
| 13114 | 3718 | |
| 15281 | 3719 | |
| 24526 | 3720 | lemma distinct_sublistI[simp]: "distinct xs \<Longrightarrow> distinct(sublist xs I)" | 
| 3721 | apply(induct xs arbitrary: I) | |
| 15281 | 3722 | apply simp | 
| 3723 | apply(auto simp add:sublist_Cons) | |
| 3724 | done | |
| 3725 | ||
| 3726 | ||
| 15045 | 3727 | lemma sublist_upt_eq_take [simp]: "sublist l {..<n} = take n l"
 | 
| 14208 | 3728 | apply (induct l rule: rev_induct, simp) | 
| 13145 | 3729 | apply (simp split: nat_diff_split add: sublist_append) | 
| 3730 | done | |
| 13114 | 3731 | |
| 24526 | 3732 | lemma filter_in_sublist: | 
| 3733 | "distinct xs \<Longrightarrow> filter (%x. x \<in> set(sublist xs s)) xs = sublist xs s" | |
| 3734 | proof (induct xs arbitrary: s) | |
| 17501 | 3735 | case Nil thus ?case by simp | 
| 3736 | next | |
| 3737 | case (Cons a xs) | |
| 3738 | moreover hence "!x. x: set xs \<longrightarrow> x \<noteq> a" by auto | |
| 3739 | ultimately show ?case by(simp add: sublist_Cons cong:filter_cong) | |
| 3740 | qed | |
| 3741 | ||
| 13114 | 3742 | |
| 19390 | 3743 | subsubsection {* @{const splice} *}
 | 
| 3744 | ||
| 40593 
1e57b18d27b1
code eqn for slice was missing; redefined splice with fun
 nipkow parents: 
40365diff
changeset | 3745 | lemma splice_Nil2 [simp, code]: "splice xs [] = xs" | 
| 19390 | 3746 | by (cases xs) simp_all | 
| 3747 | ||
| 40593 
1e57b18d27b1
code eqn for slice was missing; redefined splice with fun
 nipkow parents: 
40365diff
changeset | 3748 | declare splice.simps(1,3)[code] | 
| 
1e57b18d27b1
code eqn for slice was missing; redefined splice with fun
 nipkow parents: 
40365diff
changeset | 3749 | declare splice.simps(2)[simp del] | 
| 19390 | 3750 | |
| 24526 | 3751 | 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 | 3752 | by (induct xs ys rule: splice.induct) auto | 
| 22793 | 3753 | |
| 35115 | 3754 | |
| 3755 | subsubsection {* Transpose *}
 | |
| 34933 | 3756 | |
| 3757 | function transpose where | |
| 3758 | "transpose [] = []" | | |
| 3759 | "transpose ([] # xss) = transpose xss" | | |
| 3760 | "transpose ((x#xs) # xss) = | |
| 3761 | (x # [h. (h#t) \<leftarrow> xss]) # transpose (xs # [t. (h#t) \<leftarrow> xss])" | |
| 3762 | by pat_completeness auto | |
| 3763 | ||
| 3764 | lemma transpose_aux_filter_head: | |
| 3765 | "concat (map (list_case [] (\<lambda>h t. [h])) xss) = | |
| 3766 | map (\<lambda>xs. hd xs) [ys\<leftarrow>xss . ys \<noteq> []]" | |
| 3767 | by (induct xss) (auto split: list.split) | |
| 3768 | ||
| 3769 | lemma transpose_aux_filter_tail: | |
| 3770 | "concat (map (list_case [] (\<lambda>h t. [t])) xss) = | |
| 3771 | map (\<lambda>xs. tl xs) [ys\<leftarrow>xss . ys \<noteq> []]" | |
| 3772 | by (induct xss) (auto split: list.split) | |
| 3773 | ||
| 3774 | lemma transpose_aux_max: | |
| 3775 | "max (Suc (length xs)) (foldr (\<lambda>xs. max (length xs)) xss 0) = | |
| 3776 | Suc (max (length xs) (foldr (\<lambda>x. max (length x - Suc 0)) [ys\<leftarrow>xss . ys\<noteq>[]] 0))" | |
| 3777 | (is "max _ ?foldB = Suc (max _ ?foldA)") | |
| 3778 | proof (cases "[ys\<leftarrow>xss . ys\<noteq>[]] = []") | |
| 3779 | case True | |
| 3780 | hence "foldr (\<lambda>xs. max (length xs)) xss 0 = 0" | |
| 3781 | proof (induct xss) | |
| 3782 | case (Cons x xs) | |
| 3783 | moreover hence "x = []" by (cases x) auto | |
| 3784 | ultimately show ?case by auto | |
| 3785 | qed simp | |
| 3786 | thus ?thesis using True by simp | |
| 3787 | next | |
| 3788 | case False | |
| 3789 | ||
| 3790 | have foldA: "?foldA = foldr (\<lambda>x. max (length x)) [ys\<leftarrow>xss . ys \<noteq> []] 0 - 1" | |
| 3791 | by (induct xss) auto | |
| 3792 | have foldB: "?foldB = foldr (\<lambda>x. max (length x)) [ys\<leftarrow>xss . ys \<noteq> []] 0" | |
| 3793 | by (induct xss) auto | |
| 3794 | ||
| 3795 | have "0 < ?foldB" | |
| 3796 | proof - | |
| 3797 | from False | |
| 3798 | obtain z zs where zs: "[ys\<leftarrow>xss . ys \<noteq> []] = z#zs" by (auto simp: neq_Nil_conv) | |
| 3799 | hence "z \<in> set ([ys\<leftarrow>xss . ys \<noteq> []])" by auto | |
| 3800 | hence "z \<noteq> []" by auto | |
| 3801 | thus ?thesis | |
| 3802 | unfolding foldB zs | |
| 3803 | by (auto simp: max_def intro: less_le_trans) | |
| 3804 | qed | |
| 3805 | thus ?thesis | |
| 3806 | unfolding foldA foldB max_Suc_Suc[symmetric] | |
| 3807 | by simp | |
| 3808 | qed | |
| 3809 | ||
| 3810 | termination transpose | |
| 3811 | by (relation "measure (\<lambda>xs. foldr (\<lambda>xs. max (length xs)) xs 0 + length xs)") | |
| 3812 | (auto simp: transpose_aux_filter_tail foldr_map comp_def transpose_aux_max less_Suc_eq_le) | |
| 3813 | ||
| 3814 | lemma transpose_empty: "(transpose xs = []) \<longleftrightarrow> (\<forall>x \<in> set xs. x = [])" | |
| 3815 | by (induct rule: transpose.induct) simp_all | |
| 3816 | ||
| 3817 | lemma length_transpose: | |
| 3818 | fixes xs :: "'a list list" | |
| 3819 | shows "length (transpose xs) = foldr (\<lambda>xs. max (length xs)) xs 0" | |
| 3820 | by (induct rule: transpose.induct) | |
| 3821 | (auto simp: transpose_aux_filter_tail foldr_map comp_def transpose_aux_max | |
| 3822 | max_Suc_Suc[symmetric] simp del: max_Suc_Suc) | |
| 3823 | ||
| 3824 | lemma nth_transpose: | |
| 3825 | fixes xs :: "'a list list" | |
| 3826 | assumes "i < length (transpose xs)" | |
| 3827 | shows "transpose xs ! i = map (\<lambda>xs. xs ! i) [ys \<leftarrow> xs. i < length ys]" | |
| 3828 | using assms proof (induct arbitrary: i rule: transpose.induct) | |
| 3829 | case (3 x xs xss) | |
| 3830 | def XS == "(x # xs) # xss" | |
| 3831 | hence [simp]: "XS \<noteq> []" by auto | |
| 3832 | thus ?case | |
| 3833 | proof (cases i) | |
| 3834 | case 0 | |
| 3835 | thus ?thesis by (simp add: transpose_aux_filter_head hd_conv_nth) | |
| 3836 | next | |
| 3837 | case (Suc j) | |
| 3838 | have *: "\<And>xss. xs # map tl xss = map tl ((x#xs)#xss)" by simp | |
| 3839 | have **: "\<And>xss. (x#xs) # filter (\<lambda>ys. ys \<noteq> []) xss = filter (\<lambda>ys. ys \<noteq> []) ((x#xs)#xss)" by simp | |
| 3840 |     { fix x have "Suc j < length x \<longleftrightarrow> x \<noteq> [] \<and> j < length x - Suc 0"
 | |
| 3841 | by (cases x) simp_all | |
| 3842 | } note *** = this | |
| 3843 | ||
| 3844 | have j_less: "j < length (transpose (xs # concat (map (list_case [] (\<lambda>h t. [t])) xss)))" | |
| 3845 | using "3.prems" by (simp add: transpose_aux_filter_tail length_transpose Suc) | |
| 3846 | ||
| 3847 | show ?thesis | |
| 3848 | unfolding transpose.simps `i = Suc j` nth_Cons_Suc "3.hyps"[OF j_less] | |
| 3849 | apply (auto simp: transpose_aux_filter_tail filter_map comp_def length_transpose * ** *** XS_def[symmetric]) | |
| 3850 | apply (rule_tac y=x in list.exhaust) | |
| 3851 | by auto | |
| 3852 | qed | |
| 3853 | qed simp_all | |
| 3854 | ||
| 3855 | lemma transpose_map_map: | |
| 3856 | "transpose (map (map f) xs) = map (map f) (transpose xs)" | |
| 3857 | proof (rule nth_equalityI, safe) | |
| 3858 | have [simp]: "length (transpose (map (map f) xs)) = length (transpose xs)" | |
| 3859 | by (simp add: length_transpose foldr_map comp_def) | |
| 3860 | show "length (transpose (map (map f) xs)) = length (map (map f) (transpose xs))" by simp | |
| 3861 | ||
| 3862 | fix i assume "i < length (transpose (map (map f) xs))" | |
| 3863 | thus "transpose (map (map f) xs) ! i = map (map f) (transpose xs) ! i" | |
| 3864 | by (simp add: nth_transpose filter_map comp_def) | |
| 3865 | qed | |
| 24616 | 3866 | |
| 35115 | 3867 | |
| 31557 | 3868 | subsubsection {* (In)finiteness *}
 | 
| 28642 | 3869 | |
| 3870 | lemma finite_maxlen: | |
| 3871 | "finite (M::'a list set) ==> EX n. ALL s:M. size s < n" | |
| 3872 | proof (induct rule: finite.induct) | |
| 3873 | case emptyI show ?case by simp | |
| 3874 | next | |
| 3875 | case (insertI M xs) | |
| 3876 | then obtain n where "\<forall>s\<in>M. length s < n" by blast | |
| 3877 | hence "ALL s:insert xs M. size s < max n (size xs) + 1" by auto | |
| 3878 | thus ?case .. | |
| 3879 | qed | |
| 3880 | ||
| 45714 | 3881 | lemma lists_length_Suc_eq: | 
| 3882 |   "{xs. set xs \<subseteq> A \<and> length xs = Suc n} =
 | |
| 3883 |     (\<lambda>(xs, n). n#xs) ` ({xs. set xs \<subseteq> A \<and> length xs = n} \<times> A)"
 | |
| 3884 | by (auto simp: length_Suc_conv) | |
| 3885 | ||
| 3886 | lemma | |
| 3887 | assumes "finite A" | |
| 3888 |   shows finite_lists_length_eq: "finite {xs. set xs \<subseteq> A \<and> length xs = n}"
 | |
| 3889 |   and card_lists_length_eq: "card {xs. set xs \<subseteq> A \<and> length xs = n} = (card A)^n"
 | |
| 3890 | using `finite A` | |
| 3891 | by (induct n) | |
| 3892 | (auto simp: card_image inj_split_Cons lists_length_Suc_eq cong: conj_cong) | |
| 31557 | 3893 | |
| 3894 | lemma finite_lists_length_le: | |
| 3895 |   assumes "finite A" shows "finite {xs. set xs \<subseteq> A \<and> length xs \<le> n}"
 | |
| 3896 | (is "finite ?S") | |
| 3897 | proof- | |
| 3898 |   have "?S = (\<Union>n\<in>{0..n}. {xs. set xs \<subseteq> A \<and> length xs = n})" by auto
 | |
| 3899 | thus ?thesis by (auto intro: finite_lists_length_eq[OF `finite A`]) | |
| 3900 | qed | |
| 3901 | ||
| 45714 | 3902 | lemma card_lists_length_le: | 
| 3903 |   assumes "finite A" shows "card {xs. set xs \<subseteq> A \<and> length xs \<le> n} = (\<Sum>i\<le>n. card A^i)"
 | |
| 3904 | proof - | |
| 3905 |   have "(\<Sum>i\<le>n. card A^i) = card (\<Union>i\<le>n. {xs. set xs \<subseteq> A \<and> length xs = i})"
 | |
| 3906 | using `finite A` | |
| 3907 | by (subst card_UN_disjoint) | |
| 3908 | (auto simp add: card_lists_length_eq finite_lists_length_eq) | |
| 3909 |   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}"
 | |
| 3910 | by auto | |
| 3911 | finally show ?thesis by simp | |
| 3912 | qed | |
| 3913 | ||
| 45932 | 3914 | lemma card_lists_distinct_length_eq: | 
| 3915 | assumes "k < card A" | |
| 3916 |   shows "card {xs. length xs = k \<and> distinct xs \<and> set xs \<subseteq> A} = \<Prod>{card A - k + 1 .. card A}"
 | |
| 3917 | using assms | |
| 3918 | proof (induct k) | |
| 3919 | case 0 | |
| 3920 |   then have "{xs. length xs = 0 \<and> distinct xs \<and> set xs \<subseteq> A} = {[]}" by auto
 | |
| 3921 | then show ?case by simp | |
| 3922 | next | |
| 3923 | case (Suc k) | |
| 3924 | let "?k_list" = "\<lambda>k xs. length xs = k \<and> distinct xs \<and> set xs \<subseteq> A" | |
| 3925 | have inj_Cons: "\<And>A. inj_on (\<lambda>(xs, n). n # xs) A" by (rule inj_onI) auto | |
| 3926 | ||
| 3927 | from Suc have "k < card A" by simp | |
| 3928 | moreover have "finite A" using assms by (simp add: card_ge_0_finite) | |
| 3929 |   moreover have "finite {xs. ?k_list k xs}"
 | |
| 3930 | using finite_lists_length_eq[OF `finite A`, of k] | |
| 3931 | by - (rule finite_subset, auto) | |
| 3932 |   moreover have "\<And>i j. i \<noteq> j \<longrightarrow> {i} \<times> (A - set i) \<inter> {j} \<times> (A - set j) = {}"
 | |
| 3933 | by auto | |
| 3934 | moreover have "\<And>i. i \<in>Collect (?k_list k) \<Longrightarrow> card (A - set i) = card A - k" | |
| 3935 | by (simp add: card_Diff_subset distinct_card) | |
| 3936 |   moreover have "{xs. ?k_list (Suc k) xs} =
 | |
| 3937 |       (\<lambda>(xs, n). n#xs) ` \<Union>(\<lambda>xs. {xs} \<times> (A - set xs)) ` {xs. ?k_list k xs}"
 | |
| 3938 | by (auto simp: length_Suc_conv) | |
| 3939 | moreover | |
| 3940 | have "Suc (card A - Suc k) = card A - k" using Suc.prems by simp | |
| 3941 |   then have "(card A - k) * \<Prod>{Suc (card A - k)..card A} = \<Prod>{Suc (card A - Suc k)..card A}"
 | |
| 3942 | by (subst setprod_insert[symmetric]) (simp add: atLeastAtMost_insertL)+ | |
| 3943 | ultimately show ?case | |
| 3944 | by (simp add: card_image inj_Cons card_UN_disjoint Suc.hyps algebra_simps) | |
| 3945 | qed | |
| 3946 | ||
| 28642 | 3947 | lemma infinite_UNIV_listI: "~ finite(UNIV::'a list set)" | 
| 3948 | apply(rule notI) | |
| 3949 | apply(drule finite_maxlen) | |
| 3950 | apply (metis UNIV_I length_replicate less_not_refl) | |
| 3951 | done | |
| 3952 | ||
| 3953 | ||
| 35115 | 3954 | subsection {* Sorting *}
 | 
| 24616 | 3955 | |
| 24617 | 3956 | text{* Currently it is not shown that @{const sort} returns a
 | 
| 3957 | permutation of its input because the nicest proof is via multisets, | |
| 3958 | which are not yet available. Alternatively one could define a function | |
| 3959 | that counts the number of occurrences of an element in a list and use | |
| 3960 | that instead of multisets to state the correctness property. *} | |
| 3961 | ||
| 24616 | 3962 | context linorder | 
| 3963 | begin | |
| 3964 | ||
| 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 | 3965 | 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 | 3966 | "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 | 3967 | 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 | 3968 | |
| 
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 | 3969 | 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 | 3970 | 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 | 3971 | 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 | 3972 | 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 | 3973 | |
| 35195 | 3974 | lemma insort_left_comm: | 
| 3975 | "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 | 3976 | by (cases "x = y") (auto intro: insort_key_left_comm) | 
| 35195 | 3977 | |
| 42871 
1c0b99f950d9
names of fold_set locales resemble name of characteristic property more closely
 haftmann parents: 
42809diff
changeset | 3978 | lemma comp_fun_commute_insort: | 
| 
1c0b99f950d9
names of fold_set locales resemble name of characteristic property more closely
 haftmann parents: 
42809diff
changeset | 3979 | "comp_fun_commute insort" | 
| 35195 | 3980 | proof | 
| 42809 
5b45125b15ba
use pointfree characterisation for fold_set locale
 haftmann parents: 
42714diff
changeset | 3981 | qed (simp add: insort_left_comm fun_eq_iff) | 
| 35195 | 3982 | |
| 3983 | lemma sort_key_simps [simp]: | |
| 3984 | "sort_key f [] = []" | |
| 3985 | "sort_key f (x#xs) = insort_key f x (sort_key f xs)" | |
| 3986 | by (simp_all add: sort_key_def) | |
| 3987 | ||
| 46133 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 haftmann parents: 
46125diff
changeset | 3988 | 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 | 3989 | 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 | 3990 | 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 | 3991 | proof - | 
| 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 haftmann parents: 
46125diff
changeset | 3992 | 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 | 3993 | 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 | 3994 | 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 | 3995 | 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 | 3996 | 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 | 3997 | 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 | 3998 | 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 | 3999 | 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 | 4000 | 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 | 4001 | qed | 
| 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 haftmann parents: 
46125diff
changeset | 4002 | then show ?thesis by (simp add: sort_key_def foldr_def) | 
| 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 haftmann parents: 
46125diff
changeset | 4003 | qed | 
| 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 haftmann parents: 
46125diff
changeset | 4004 | |
| 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 haftmann parents: 
46125diff
changeset | 4005 | 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 | 4006 | "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 | 4007 | by (rule sort_key_conv_fold) simp | 
| 35195 | 4008 | |
| 33639 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 4009 | 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 | 4010 | 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 | 4011 | |
| 25062 | 4012 | lemma sorted_Cons: "sorted (x#xs) = (sorted xs & (ALL y:set xs. x <= y))" | 
| 24616 | 4013 | apply(induct xs arbitrary: x) apply simp | 
| 4014 | by simp (blast intro: order_trans) | |
| 4015 | ||
| 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 | 4016 | 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 | 4017 | "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 | 4018 | 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 | 4019 | |
| 24616 | 4020 | lemma sorted_append: | 
| 25062 | 4021 | "sorted (xs@ys) = (sorted xs & sorted ys & (\<forall>x \<in> set xs. \<forall>y \<in> set ys. x\<le>y))" | 
| 24616 | 4022 | by (induct xs) (auto simp add:sorted_Cons) | 
| 4023 | ||
| 31201 | 4024 | 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 | 4025 | "sorted xs \<Longrightarrow> i \<le> j \<Longrightarrow> j < length xs \<Longrightarrow> xs!i \<le> xs!j" | 
| 31201 | 4026 | by (induct xs arbitrary: i j) (auto simp:nth_Cons' sorted_Cons) | 
| 4027 | ||
| 33639 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 4028 | 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 | 4029 | "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 | 4030 | 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 | 4031 | 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 | 4032 | 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 | 4033 | |
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 4034 | 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 | 4035 | "(\<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 | 4036 | 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 | 4037 | 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 | 4038 | 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 | 4039 | 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 | 4040 | 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 | 4041 | 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 | 4042 | 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 | 4043 | qed | 
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 4044 | moreover | 
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 4045 |   {
 | 
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 4046 | 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 | 4047 | 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 | 4048 | 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 | 4049 | 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 | 4050 | 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 | 4051 | 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 | 4052 | } | 
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 4053 | ultimately | 
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 4054 | 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 | 4055 | 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 | 4056 | 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 | 4057 | |
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 4058 | 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 | 4059 | "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 | 4060 | 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 | 4061 | |
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 4062 | lemma set_insort: "set(insort_key f x xs) = insert x (set xs)" | 
| 24616 | 4063 | by (induct xs) auto | 
| 4064 | ||
| 33639 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 4065 | lemma set_sort[simp]: "set(sort_key f xs) = set xs" | 
| 24616 | 4066 | by (induct xs) (simp_all add:set_insort) | 
| 4067 | ||
| 33639 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 4068 | lemma distinct_insort: "distinct (insort_key f x xs) = (x \<notin> set xs \<and> distinct xs)" | 
| 24616 | 4069 | by(induct xs)(auto simp:set_insort) | 
| 4070 | ||
| 33639 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 4071 | lemma distinct_sort[simp]: "distinct (sort_key f xs) = distinct xs" | 
| 44921 | 4072 | by (induct xs) (simp_all add: distinct_insort) | 
| 24616 | 4073 | |
| 33639 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 4074 | 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 | 4075 | 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 | 4076 | |
| 24616 | 4077 | 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 | 4078 | 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 | 4079 | |
| 
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 | 4080 | 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 | 4081 | 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 | 4082 | |
| 
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 | 4083 | 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 | 4084 | 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 | 4085 | |
| 36851 | 4086 | lemma sorted_butlast: | 
| 4087 | assumes "xs \<noteq> []" and "sorted xs" | |
| 4088 | shows "sorted (butlast xs)" | |
| 4089 | proof - | |
| 4090 | from `xs \<noteq> []` obtain ys y where "xs = ys @ [y]" by (cases xs rule: rev_cases) auto | |
| 4091 | with `sorted xs` show ?thesis by (simp add: sorted_append) | |
| 4092 | qed | |
| 4093 | ||
| 4094 | lemma insort_not_Nil [simp]: | |
| 4095 | "insort_key f a xs \<noteq> []" | |
| 4096 | by (induct xs) simp_all | |
| 4097 | ||
| 33639 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 4098 | 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 | 4099 | by (cases xs) auto | 
| 
314c0bcb7df7
Added useful general lemmas from the work with the HeapMonad
 bulwahn parents: 
26073diff
changeset | 4100 | |
| 44916 
840d8c3d9113
added lemma motivated by a more specific lemma in the AFP-KBPs theories
 bulwahn parents: 
44890diff
changeset | 4101 | 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 | 4102 | 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 | 4103 | |
| 39534 
c798d4f1b682
generalized lemma insort_remove1 to insort_key_remove1
 haftmann parents: 
39302diff
changeset | 4104 | lemma sorted_map_remove1: | 
| 
c798d4f1b682
generalized lemma insort_remove1 to insort_key_remove1
 haftmann parents: 
39302diff
changeset | 4105 | "sorted (map f xs) \<Longrightarrow> sorted (map f (remove1 x xs))" | 
| 
c798d4f1b682
generalized lemma insort_remove1 to insort_key_remove1
 haftmann parents: 
39302diff
changeset | 4106 | by (induct xs) (auto simp add: sorted_Cons) | 
| 
c798d4f1b682
generalized lemma insort_remove1 to insort_key_remove1
 haftmann parents: 
39302diff
changeset | 4107 | |
| 26143 
314c0bcb7df7
Added useful general lemmas from the work with the HeapMonad
 bulwahn parents: 
26073diff
changeset | 4108 | lemma sorted_remove1: "sorted xs \<Longrightarrow> sorted (remove1 a xs)" | 
| 39534 
c798d4f1b682
generalized lemma insort_remove1 to insort_key_remove1
 haftmann parents: 
39302diff
changeset | 4109 | using sorted_map_remove1 [of "\<lambda>x. x"] by simp | 
| 
c798d4f1b682
generalized lemma insort_remove1 to insort_key_remove1
 haftmann parents: 
39302diff
changeset | 4110 | |
| 
c798d4f1b682
generalized lemma insort_remove1 to insort_key_remove1
 haftmann parents: 
39302diff
changeset | 4111 | lemma insort_key_remove1: | 
| 
c798d4f1b682
generalized lemma insort_remove1 to insort_key_remove1
 haftmann parents: 
39302diff
changeset | 4112 | 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 | 4113 | shows "insort_key f a (remove1 a xs) = xs" | 
| 
c798d4f1b682
generalized lemma insort_remove1 to insort_key_remove1
 haftmann parents: 
39302diff
changeset | 4114 | 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 | 4115 | case (Cons x xs) | 
| 39534 
c798d4f1b682
generalized lemma insort_remove1 to insort_key_remove1
 haftmann parents: 
39302diff
changeset | 4116 | 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 | 4117 | 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 | 4118 | case False | 
| 39534 
c798d4f1b682
generalized lemma insort_remove1 to insort_key_remove1
 haftmann parents: 
39302diff
changeset | 4119 | 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 | 4120 | 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 | 4121 | 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 | 4122 | 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 | 4123 | qed simp | 
| 26143 
314c0bcb7df7
Added useful general lemmas from the work with the HeapMonad
 bulwahn parents: 
26073diff
changeset | 4124 | |
| 39534 
c798d4f1b682
generalized lemma insort_remove1 to insort_key_remove1
 haftmann parents: 
39302diff
changeset | 4125 | lemma insort_remove1: | 
| 
c798d4f1b682
generalized lemma insort_remove1 to insort_key_remove1
 haftmann parents: 
39302diff
changeset | 4126 | assumes "a \<in> set xs" and "sorted xs" | 
| 
c798d4f1b682
generalized lemma insort_remove1 to insort_key_remove1
 haftmann parents: 
39302diff
changeset | 4127 | shows "insort a (remove1 a xs) = xs" | 
| 
c798d4f1b682
generalized lemma insort_remove1 to insort_key_remove1
 haftmann parents: 
39302diff
changeset | 4128 | proof (rule insort_key_remove1) | 
| 
c798d4f1b682
generalized lemma insort_remove1 to insort_key_remove1
 haftmann parents: 
39302diff
changeset | 4129 | from `a \<in> set xs` show "a \<in> set xs" . | 
| 
c798d4f1b682
generalized lemma insort_remove1 to insort_key_remove1
 haftmann parents: 
39302diff
changeset | 4130 | 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 | 4131 | 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 | 4132 |   then have "set (filter (op = a) xs) \<noteq> {}" by auto
 | 
| 
c798d4f1b682
generalized lemma insort_remove1 to insort_key_remove1
 haftmann parents: 
39302diff
changeset | 4133 | 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 | 4134 | then have "length (filter (op = a) xs) > 0" by simp | 
| 
c798d4f1b682
generalized lemma insort_remove1 to insort_key_remove1
 haftmann parents: 
39302diff
changeset | 4135 | 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 | 4136 | by (cases "length (filter (op = a) xs)") simp_all | 
| 
c798d4f1b682
generalized lemma insort_remove1 to insort_key_remove1
 haftmann parents: 
39302diff
changeset | 4137 | moreover have "replicate (Suc n) a = a # replicate n a" | 
| 
c798d4f1b682
generalized lemma insort_remove1 to insort_key_remove1
 haftmann parents: 
39302diff
changeset | 4138 | by simp | 
| 
c798d4f1b682
generalized lemma insort_remove1 to insort_key_remove1
 haftmann parents: 
39302diff
changeset | 4139 | 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 | 4140 | qed | 
| 26143 
314c0bcb7df7
Added useful general lemmas from the work with the HeapMonad
 bulwahn parents: 
26073diff
changeset | 4141 | |
| 
314c0bcb7df7
Added useful general lemmas from the work with the HeapMonad
 bulwahn parents: 
26073diff
changeset | 4142 | lemma sorted_remdups[simp]: | 
| 
314c0bcb7df7
Added useful general lemmas from the work with the HeapMonad
 bulwahn parents: 
26073diff
changeset | 4143 | "sorted l \<Longrightarrow> sorted (remdups l)" | 
| 
314c0bcb7df7
Added useful general lemmas from the work with the HeapMonad
 bulwahn parents: 
26073diff
changeset | 4144 | by (induct l) (auto simp: sorted_Cons) | 
| 
314c0bcb7df7
Added useful general lemmas from the work with the HeapMonad
 bulwahn parents: 
26073diff
changeset | 4145 | |
| 24645 | 4146 | lemma sorted_distinct_set_unique: | 
| 4147 | assumes "sorted xs" "distinct xs" "sorted ys" "distinct ys" "set xs = set ys" | |
| 4148 | shows "xs = ys" | |
| 4149 | proof - | |
| 26734 | 4150 | from assms have 1: "length xs = length ys" by (auto dest!: distinct_card) | 
| 24645 | 4151 | from assms show ?thesis | 
| 4152 | proof(induct rule:list_induct2[OF 1]) | |
| 4153 | case 1 show ?case by simp | |
| 4154 | next | |
| 4155 | case 2 thus ?case by (simp add:sorted_Cons) | |
| 4156 | (metis Diff_insert_absorb antisym insertE insert_iff) | |
| 4157 | qed | |
| 4158 | qed | |
| 4159 | ||
| 35603 | 4160 | lemma map_sorted_distinct_set_unique: | 
| 4161 | assumes "inj_on f (set xs \<union> set ys)" | |
| 4162 | assumes "sorted (map f xs)" "distinct (map f xs)" | |
| 4163 | "sorted (map f ys)" "distinct (map f ys)" | |
| 4164 | assumes "set xs = set ys" | |
| 4165 | shows "xs = ys" | |
| 4166 | proof - | |
| 4167 | from assms have "map f xs = map f ys" | |
| 4168 | by (simp add: sorted_distinct_set_unique) | |
| 4169 | moreover with `inj_on f (set xs \<union> set ys)` show "xs = ys" | |
| 4170 | by (blast intro: map_inj_on) | |
| 4171 | qed | |
| 4172 | ||
| 24645 | 4173 | lemma finite_sorted_distinct_unique: | 
| 4174 | shows "finite A \<Longrightarrow> EX! xs. set xs = A & sorted xs & distinct xs" | |
| 4175 | apply(drule finite_distinct_list) | |
| 4176 | apply clarify | |
| 4177 | apply(rule_tac a="sort xs" in ex1I) | |
| 4178 | apply (auto simp: sorted_distinct_set_unique) | |
| 4179 | done | |
| 4180 | ||
| 39915 
ecf97cf3d248
turned distinct and sorted into inductive predicates: yields nice induction principles for free; more elegant proofs
 haftmann parents: 
39774diff
changeset | 4181 | lemma | 
| 
ecf97cf3d248
turned distinct and sorted into inductive predicates: yields nice induction principles for free; more elegant proofs
 haftmann parents: 
39774diff
changeset | 4182 | assumes "sorted xs" | 
| 
ecf97cf3d248
turned distinct and sorted into inductive predicates: yields nice induction principles for free; more elegant proofs
 haftmann parents: 
39774diff
changeset | 4183 | 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 | 4184 | 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 | 4185 | proof - | 
| 
ecf97cf3d248
turned distinct and sorted into inductive predicates: yields nice induction principles for free; more elegant proofs
 haftmann parents: 
39774diff
changeset | 4186 | 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 | 4187 | 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 | 4188 | unfolding sorted_append by simp_all | 
| 29626 | 4189 | qed | 
| 4190 | ||
| 33639 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 4191 | 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 | 4192 | 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 | 4193 | |
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 4194 | 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 | 4195 | by (subst takeWhile_eq_take) (auto dest: sorted_take) | 
| 29626 | 4196 | |
| 34933 | 4197 | lemma sorted_filter: | 
| 4198 | "sorted (map f xs) \<Longrightarrow> sorted (map f (filter P xs))" | |
| 4199 | by (induct xs) (simp_all add: sorted_Cons) | |
| 4200 | ||
| 4201 | lemma foldr_max_sorted: | |
| 4202 | assumes "sorted (rev xs)" | |
| 4203 | shows "foldr max xs y = (if xs = [] then y else max (xs ! 0) y)" | |
| 4204 | using assms proof (induct xs) | |
| 4205 | case (Cons x xs) | |
| 4206 | moreover hence "sorted (rev xs)" using sorted_append by auto | |
| 4207 | ultimately show ?case | |
| 4208 | by (cases xs, auto simp add: sorted_append max_def) | |
| 4209 | qed simp | |
| 4210 | ||
| 4211 | lemma filter_equals_takeWhile_sorted_rev: | |
| 4212 | assumes sorted: "sorted (rev (map f xs))" | |
| 4213 | shows "[x \<leftarrow> xs. t < f x] = takeWhile (\<lambda> x. t < f x) xs" | |
| 4214 | (is "filter ?P xs = ?tW") | |
| 4215 | proof (rule takeWhile_eq_filter[symmetric]) | |
| 4216 | let "?dW" = "dropWhile ?P xs" | |
| 4217 | fix x assume "x \<in> set ?dW" | |
| 4218 | then obtain i where i: "i < length ?dW" and nth_i: "x = ?dW ! i" | |
| 4219 | unfolding in_set_conv_nth by auto | |
| 4220 | hence "length ?tW + i < length (?tW @ ?dW)" | |
| 4221 | unfolding length_append by simp | |
| 4222 | hence i': "length (map f ?tW) + i < length (map f xs)" by simp | |
| 4223 | have "(map f ?tW @ map f ?dW) ! (length (map f ?tW) + i) \<le> | |
| 4224 | (map f ?tW @ map f ?dW) ! (length (map f ?tW) + 0)" | |
| 4225 | using sorted_rev_nth_mono[OF sorted _ i', of "length ?tW"] | |
| 4226 | unfolding map_append[symmetric] by simp | |
| 4227 | hence "f x \<le> f (?dW ! 0)" | |
| 4228 | unfolding nth_append_length_plus nth_i | |
| 4229 | using i preorder_class.le_less_trans[OF le0 i] by simp | |
| 4230 | also have "... \<le> t" | |
| 4231 | using hd_dropWhile[of "?P" xs] le0[THEN preorder_class.le_less_trans, OF i] | |
| 4232 | using hd_conv_nth[of "?dW"] by simp | |
| 4233 | finally show "\<not> t < f x" by simp | |
| 4234 | qed | |
| 4235 | ||
| 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 | 4236 | 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 | 4237 | "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 | 4238 | 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 | 4239 | |
| 
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 | 4240 | 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 | 4241 | "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 | 4242 | 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 | 4243 | |
| 
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 | 4244 | 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 | 4245 | "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 | 4246 | 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 | 4247 | |
| 
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 | 4248 | 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 | 4249 | "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 | 4250 | 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 | 4251 | |
| 35608 | 4252 | lemma set_insort_insert: | 
| 4253 | "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 | 4254 | by (auto simp add: insort_insert_key_def set_insort) | 
| 35608 | 4255 | |
| 4256 | lemma distinct_insort_insert: | |
| 4257 | 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 | 4258 | 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 | 4259 | 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 | 4260 | |
| 
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 | 4261 | 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 | 4262 | 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 | 4263 | 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 | 4264 | using assms by (simp add: insort_insert_key_def sorted_insort_key) | 
| 35608 | 4265 | |
| 4266 | lemma sorted_insort_insert: | |
| 4267 | assumes "sorted xs" | |
| 4268 | 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 | 4269 | 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 | 4270 | |
| 
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 | 4271 | lemma filter_insort_triv: | 
| 37107 | 4272 | "\<not> P x \<Longrightarrow> filter P (insort_key f x xs) = filter P xs" | 
| 4273 | by (induct xs) simp_all | |
| 4274 | ||
| 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 | 4275 | lemma filter_insort: | 
| 37107 | 4276 | "sorted (map f xs) \<Longrightarrow> P x \<Longrightarrow> filter P (insort_key f x xs) = insort_key f x (filter P xs)" | 
| 4277 | using assms by (induct xs) | |
| 4278 | (auto simp add: sorted_Cons, subst insort_is_Cons, auto) | |
| 4279 | ||
| 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 | 4280 | lemma filter_sort: | 
| 37107 | 4281 | "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 | 4282 | by (induct xs) (simp_all add: filter_insort_triv filter_insort) | 
| 37107 | 4283 | |
| 40304 | 4284 | lemma sorted_map_same: | 
| 4285 | "sorted (map f [x\<leftarrow>xs. f x = g xs])" | |
| 4286 | proof (induct xs arbitrary: g) | |
| 37107 | 4287 | case Nil then show ?case by simp | 
| 4288 | next | |
| 4289 | case (Cons x xs) | |
| 40304 | 4290 | then have "sorted (map f [y\<leftarrow>xs . f y = (\<lambda>xs. f x) xs])" . | 
| 4291 | moreover from Cons have "sorted (map f [y\<leftarrow>xs . f y = (g \<circ> Cons x) xs])" . | |
| 37107 | 4292 | ultimately show ?case by (simp_all add: sorted_Cons) | 
| 4293 | qed | |
| 4294 | ||
| 40304 | 4295 | lemma sorted_same: | 
| 4296 | "sorted [x\<leftarrow>xs. x = g xs]" | |
| 4297 | using sorted_map_same [of "\<lambda>x. x"] by simp | |
| 4298 | ||
| 37107 | 4299 | lemma remove1_insort [simp]: | 
| 4300 | "remove1 x (insort x xs) = xs" | |
| 4301 | by (induct xs) simp_all | |
| 4302 | ||
| 24616 | 4303 | end | 
| 4304 | ||
| 25277 | 4305 | lemma sorted_upt[simp]: "sorted[i..<j]" | 
| 4306 | by (induct j) (simp_all add:sorted_append) | |
| 4307 | ||
| 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 | 4308 | 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 | 4309 | 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 | 4310 | 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 | 4311 | 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 | 4312 | 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 | 4313 | |
| 35115 | 4314 | |
| 4315 | subsubsection {* @{const transpose} on sorted lists *}
 | |
| 34933 | 4316 | |
| 4317 | lemma sorted_transpose[simp]: | |
| 4318 | shows "sorted (rev (map length (transpose xs)))" | |
| 4319 | by (auto simp: sorted_equals_nth_mono rev_nth nth_transpose | |
| 4320 | length_filter_conv_card intro: card_mono) | |
| 4321 | ||
| 4322 | lemma transpose_max_length: | |
| 4323 | "foldr (\<lambda>xs. max (length xs)) (transpose xs) 0 = length [x \<leftarrow> xs. x \<noteq> []]" | |
| 4324 | (is "?L = ?R") | |
| 4325 | proof (cases "transpose xs = []") | |
| 4326 | case False | |
| 4327 | have "?L = foldr max (map length (transpose xs)) 0" | |
| 4328 | by (simp add: foldr_map comp_def) | |
| 4329 | also have "... = length (transpose xs ! 0)" | |
| 4330 | using False sorted_transpose by (simp add: foldr_max_sorted) | |
| 4331 | finally show ?thesis | |
| 4332 | using False by (simp add: nth_transpose) | |
| 4333 | next | |
| 4334 | case True | |
| 4335 | hence "[x \<leftarrow> xs. x \<noteq> []] = []" | |
| 4336 | by (auto intro!: filter_False simp: transpose_empty) | |
| 4337 | thus ?thesis by (simp add: transpose_empty True) | |
| 4338 | qed | |
| 4339 | ||
| 4340 | lemma length_transpose_sorted: | |
| 4341 | fixes xs :: "'a list list" | |
| 4342 | assumes sorted: "sorted (rev (map length xs))" | |
| 4343 | shows "length (transpose xs) = (if xs = [] then 0 else length (xs ! 0))" | |
| 4344 | proof (cases "xs = []") | |
| 4345 | case False | |
| 4346 | thus ?thesis | |
| 4347 | using foldr_max_sorted[OF sorted] False | |
| 4348 | unfolding length_transpose foldr_map comp_def | |
| 4349 | by simp | |
| 4350 | qed simp | |
| 4351 | ||
| 4352 | lemma nth_nth_transpose_sorted[simp]: | |
| 4353 | fixes xs :: "'a list list" | |
| 4354 | assumes sorted: "sorted (rev (map length xs))" | |
| 4355 | and i: "i < length (transpose xs)" | |
| 4356 | and j: "j < length [ys \<leftarrow> xs. i < length ys]" | |
| 4357 | shows "transpose xs ! i ! j = xs ! j ! i" | |
| 4358 | using j filter_equals_takeWhile_sorted_rev[OF sorted, of i] | |
| 4359 | nth_transpose[OF i] nth_map[OF j] | |
| 4360 | by (simp add: takeWhile_nth) | |
| 4361 | ||
| 4362 | lemma transpose_column_length: | |
| 4363 | fixes xs :: "'a list list" | |
| 4364 | assumes sorted: "sorted (rev (map length xs))" and "i < length xs" | |
| 4365 | shows "length (filter (\<lambda>ys. i < length ys) (transpose xs)) = length (xs ! i)" | |
| 4366 | proof - | |
| 4367 | have "xs \<noteq> []" using `i < length xs` by auto | |
| 4368 | note filter_equals_takeWhile_sorted_rev[OF sorted, simp] | |
| 4369 |   { fix j assume "j \<le> i"
 | |
| 4370 | note sorted_rev_nth_mono[OF sorted, of j i, simplified, OF this `i < length xs`] | |
| 4371 | } note sortedE = this[consumes 1] | |
| 4372 | ||
| 4373 |   have "{j. j < length (transpose xs) \<and> i < length (transpose xs ! j)}
 | |
| 4374 |     = {..< length (xs ! i)}"
 | |
| 4375 | proof safe | |
| 4376 | fix j | |
| 4377 | assume "j < length (transpose xs)" and "i < length (transpose xs ! j)" | |
| 4378 | with this(2) nth_transpose[OF this(1)] | |
| 4379 | have "i < length (takeWhile (\<lambda>ys. j < length ys) xs)" by simp | |
| 4380 | from nth_mem[OF this] takeWhile_nth[OF this] | |
| 4381 | show "j < length (xs ! i)" by (auto dest: set_takeWhileD) | |
| 4382 | next | |
| 4383 | fix j assume "j < length (xs ! i)" | |
| 4384 | thus "j < length (transpose xs)" | |
| 4385 | using foldr_max_sorted[OF sorted] `xs \<noteq> []` sortedE[OF le0] | |
| 4386 | by (auto simp: length_transpose comp_def foldr_map) | |
| 4387 | ||
| 4388 | have "Suc i \<le> length (takeWhile (\<lambda>ys. j < length ys) xs)" | |
| 4389 | using `i < length xs` `j < length (xs ! i)` less_Suc_eq_le | |
| 4390 | by (auto intro!: length_takeWhile_less_P_nth dest!: sortedE) | |
| 4391 | with nth_transpose[OF `j < length (transpose xs)`] | |
| 4392 | show "i < length (transpose xs ! j)" by simp | |
| 4393 | qed | |
| 4394 | thus ?thesis by (simp add: length_filter_conv_card) | |
| 4395 | qed | |
| 4396 | ||
| 4397 | lemma transpose_column: | |
| 4398 | fixes xs :: "'a list list" | |
| 4399 | assumes sorted: "sorted (rev (map length xs))" and "i < length xs" | |
| 4400 | shows "map (\<lambda>ys. ys ! i) (filter (\<lambda>ys. i < length ys) (transpose xs)) | |
| 4401 | = xs ! i" (is "?R = _") | |
| 4402 | proof (rule nth_equalityI, safe) | |
| 4403 | show length: "length ?R = length (xs ! i)" | |
| 4404 | using transpose_column_length[OF assms] by simp | |
| 4405 | ||
| 4406 | fix j assume j: "j < length ?R" | |
| 4407 | note * = less_le_trans[OF this, unfolded length_map, OF length_filter_le] | |
| 4408 | from j have j_less: "j < length (xs ! i)" using length by simp | |
| 4409 | have i_less_tW: "Suc i \<le> length (takeWhile (\<lambda>ys. Suc j \<le> length ys) xs)" | |
| 4410 | proof (rule length_takeWhile_less_P_nth) | |
| 4411 | show "Suc i \<le> length xs" using `i < length xs` by simp | |
| 4412 | fix k assume "k < Suc i" | |
| 4413 | hence "k \<le> i" by auto | |
| 4414 | with sorted_rev_nth_mono[OF sorted this] `i < length xs` | |
| 4415 | have "length (xs ! i) \<le> length (xs ! k)" by simp | |
| 4416 | thus "Suc j \<le> length (xs ! k)" using j_less by simp | |
| 4417 | qed | |
| 4418 | have i_less_filter: "i < length [ys\<leftarrow>xs . j < length ys]" | |
| 4419 | unfolding filter_equals_takeWhile_sorted_rev[OF sorted, of j] | |
| 4420 | using i_less_tW by (simp_all add: Suc_le_eq) | |
| 4421 | from j show "?R ! j = xs ! i ! j" | |
| 4422 | unfolding filter_equals_takeWhile_sorted_rev[OF sorted_transpose, of i] | |
| 4423 | by (simp add: takeWhile_nth nth_nth_transpose_sorted[OF sorted * i_less_filter]) | |
| 4424 | qed | |
| 4425 | ||
| 4426 | lemma transpose_transpose: | |
| 4427 | fixes xs :: "'a list list" | |
| 4428 | assumes sorted: "sorted (rev (map length xs))" | |
| 4429 | shows "transpose (transpose xs) = takeWhile (\<lambda>x. x \<noteq> []) xs" (is "?L = ?R") | |
| 4430 | proof - | |
| 4431 | have len: "length ?L = length ?R" | |
| 4432 | unfolding length_transpose transpose_max_length | |
| 4433 | using filter_equals_takeWhile_sorted_rev[OF sorted, of 0] | |
| 4434 | by simp | |
| 4435 | ||
| 4436 |   { fix i assume "i < length ?R"
 | |
| 4437 | with less_le_trans[OF _ length_takeWhile_le[of _ xs]] | |
| 4438 | have "i < length xs" by simp | |
| 4439 | } note * = this | |
| 4440 | show ?thesis | |
| 4441 | by (rule nth_equalityI) | |
| 4442 | (simp_all add: len nth_transpose transpose_column[OF sorted] * takeWhile_nth) | |
| 4443 | qed | |
| 24616 | 4444 | |
| 34934 
440605046777
Added transpose_rectangle, when the input list is rectangular.
 hoelzl parents: 
34933diff
changeset | 4445 | theorem transpose_rectangle: | 
| 
440605046777
Added transpose_rectangle, when the input list is rectangular.
 hoelzl parents: 
34933diff
changeset | 4446 | assumes "xs = [] \<Longrightarrow> n = 0" | 
| 
440605046777
Added transpose_rectangle, when the input list is rectangular.
 hoelzl parents: 
34933diff
changeset | 4447 | 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 | 4448 | 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 | 4449 | (is "?trans = ?map") | 
| 
440605046777
Added transpose_rectangle, when the input list is rectangular.
 hoelzl parents: 
34933diff
changeset | 4450 | proof (rule nth_equalityI) | 
| 
440605046777
Added transpose_rectangle, when the input list is rectangular.
 hoelzl parents: 
34933diff
changeset | 4451 | have "sorted (rev (map length xs))" | 
| 
440605046777
Added transpose_rectangle, when the input list is rectangular.
 hoelzl parents: 
34933diff
changeset | 4452 | by (auto simp: rev_nth rect intro!: sorted_nth_monoI) | 
| 
440605046777
Added transpose_rectangle, when the input list is rectangular.
 hoelzl parents: 
34933diff
changeset | 4453 | from foldr_max_sorted[OF this] assms | 
| 
440605046777
Added transpose_rectangle, when the input list is rectangular.
 hoelzl parents: 
34933diff
changeset | 4454 | show len: "length ?trans = length ?map" | 
| 
440605046777
Added transpose_rectangle, when the input list is rectangular.
 hoelzl parents: 
34933diff
changeset | 4455 | by (simp_all add: length_transpose foldr_map comp_def) | 
| 
440605046777
Added transpose_rectangle, when the input list is rectangular.
 hoelzl parents: 
34933diff
changeset | 4456 | moreover | 
| 
440605046777
Added transpose_rectangle, when the input list is rectangular.
 hoelzl parents: 
34933diff
changeset | 4457 |   { 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 | 4458 | 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 | 4459 | ultimately show "\<forall>i < length ?trans. ?trans ! i = ?map ! i" | 
| 
440605046777
Added transpose_rectangle, when the input list is rectangular.
 hoelzl parents: 
34933diff
changeset | 4460 | by (auto simp: nth_transpose intro: nth_equalityI) | 
| 
440605046777
Added transpose_rectangle, when the input list is rectangular.
 hoelzl parents: 
34933diff
changeset | 4461 | qed | 
| 24616 | 4462 | |
| 35115 | 4463 | |
| 25069 | 4464 | subsubsection {* @{text sorted_list_of_set} *}
 | 
| 4465 | ||
| 4466 | text{* This function maps (finite) linearly ordered sets to sorted
 | |
| 4467 | lists. Warning: in most cases it is not a good idea to convert from | |
| 4468 | sets to lists but one should convert in the other direction (via | |
| 4469 | @{const set}). *}
 | |
| 4470 | ||
| 4471 | context linorder | |
| 4472 | begin | |
| 4473 | ||
| 35195 | 4474 | definition sorted_list_of_set :: "'a set \<Rightarrow> 'a list" where | 
| 4475 | "sorted_list_of_set = Finite_Set.fold insort []" | |
| 4476 | ||
| 4477 | lemma sorted_list_of_set_empty [simp]: | |
| 4478 |   "sorted_list_of_set {} = []"
 | |
| 4479 | by (simp add: sorted_list_of_set_def) | |
| 4480 | ||
| 4481 | lemma sorted_list_of_set_insert [simp]: | |
| 4482 | assumes "finite A" | |
| 4483 |   shows "sorted_list_of_set (insert x A) = insort x (sorted_list_of_set (A - {x}))"
 | |
| 4484 | proof - | |
| 42871 
1c0b99f950d9
names of fold_set locales resemble name of characteristic property more closely
 haftmann parents: 
42809diff
changeset | 4485 | interpret comp_fun_commute insort by (fact comp_fun_commute_insort) | 
| 46898 
1570b30ee040
tuned proofs -- eliminated pointless chaining of facts after 'interpret';
 wenzelm parents: 
46884diff
changeset | 4486 | from assms show ?thesis | 
| 
1570b30ee040
tuned proofs -- eliminated pointless chaining of facts after 'interpret';
 wenzelm parents: 
46884diff
changeset | 4487 | by (simp add: sorted_list_of_set_def fold_insert_remove) | 
| 35195 | 4488 | qed | 
| 4489 | ||
| 4490 | lemma sorted_list_of_set [simp]: | |
| 4491 | "finite A \<Longrightarrow> set (sorted_list_of_set A) = A \<and> sorted (sorted_list_of_set A) | |
| 4492 | \<and> distinct (sorted_list_of_set A)" | |
| 4493 | by (induct A rule: finite_induct) (simp_all add: set_insort sorted_insort distinct_insort) | |
| 4494 | ||
| 4495 | lemma sorted_list_of_set_sort_remdups: | |
| 4496 | "sorted_list_of_set (set xs) = sort (remdups xs)" | |
| 4497 | proof - | |
| 42871 
1c0b99f950d9
names of fold_set locales resemble name of characteristic property more closely
 haftmann parents: 
42809diff
changeset | 4498 | interpret comp_fun_commute insort by (fact comp_fun_commute_insort) | 
| 46133 
d9fe85d3d2cd
incorporated canonical fold combinator on lists into body of List theory; refactored passages on List.fold(l/r)
 haftmann parents: 
46125diff
changeset | 4499 | show ?thesis by (simp add: sorted_list_of_set_def sort_conv_fold fold_set_fold_remdups) | 
| 35195 | 4500 | qed | 
| 25069 | 4501 | |
| 37107 | 4502 | lemma sorted_list_of_set_remove: | 
| 4503 | assumes "finite A" | |
| 4504 |   shows "sorted_list_of_set (A - {x}) = remove1 x (sorted_list_of_set A)"
 | |
| 4505 | proof (cases "x \<in> A") | |
| 4506 | case False with assms have "x \<notin> set (sorted_list_of_set A)" by simp | |
| 4507 | with False show ?thesis by (simp add: remove1_idem) | |
| 4508 | next | |
| 4509 | case True then obtain B where A: "A = insert x B" by (rule Set.set_insert) | |
| 4510 | with assms show ?thesis by simp | |
| 4511 | qed | |
| 4512 | ||
| 25069 | 4513 | end | 
| 4514 | ||
| 37107 | 4515 | lemma sorted_list_of_set_range [simp]: | 
| 4516 |   "sorted_list_of_set {m..<n} = [m..<n]"
 | |
| 4517 | by (rule sorted_distinct_set_unique) simp_all | |
| 4518 | ||
| 4519 | ||
| 15392 | 4520 | subsubsection {* @{text lists}: the list-forming operator over sets *}
 | 
| 15302 | 4521 | |
| 23740 | 4522 | inductive_set | 
| 22262 | 4523 | lists :: "'a set => 'a list set" | 
| 23740 | 4524 | for A :: "'a set" | 
| 4525 | where | |
| 39613 | 4526 | Nil [intro!, simp]: "[]: lists A" | 
| 4527 | | Cons [intro!, simp, no_atp]: "[| a: A; l: lists A|] ==> a#l : lists A" | |
| 35828 
46cfc4b8112e
now use "Named_Thms" for "noatp", and renamed "noatp" to "no_atp"
 blanchet parents: 
35827diff
changeset | 4528 | |
| 
46cfc4b8112e
now use "Named_Thms" for "noatp", and renamed "noatp" to "no_atp"
 blanchet parents: 
35827diff
changeset | 4529 | inductive_cases listsE [elim!,no_atp]: "x#l : lists A" | 
| 
46cfc4b8112e
now use "Named_Thms" for "noatp", and renamed "noatp" to "no_atp"
 blanchet parents: 
35827diff
changeset | 4530 | inductive_cases listspE [elim!,no_atp]: "listsp A (x # l)" | 
| 23740 | 4531 | |
| 46313 | 4532 | inductive_simps listsp_simps[code]: | 
| 4533 | "listsp A []" | |
| 4534 | "listsp A (x # xs)" | |
| 4535 | ||
| 23740 | 4536 | lemma listsp_mono [mono]: "A \<le> B ==> listsp A \<le> listsp B" | 
| 46884 | 4537 | 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 | 4538 | |
| 46176 
1898e61e89c4
pred_subset/equals_eq are now standard pred_set_conv rules
 berghofe parents: 
46156diff
changeset | 4539 | lemmas lists_mono = listsp_mono [to_set] | 
| 22262 | 4540 | |
| 22422 
ee19cdb07528
stepping towards uniform lattice theory development in HOL
 haftmann parents: 
22262diff
changeset | 4541 | lemma listsp_infI: | 
| 
ee19cdb07528
stepping towards uniform lattice theory development in HOL
 haftmann parents: 
22262diff
changeset | 4542 | assumes l: "listsp A l" shows "listsp B l ==> listsp (inf A B) l" using l | 
| 24349 | 4543 | by induct blast+ | 
| 15302 | 4544 | |
| 22422 
ee19cdb07528
stepping towards uniform lattice theory development in HOL
 haftmann parents: 
22262diff
changeset | 4545 | lemmas lists_IntI = listsp_infI [to_set] | 
| 
ee19cdb07528
stepping towards uniform lattice theory development in HOL
 haftmann parents: 
22262diff
changeset | 4546 | |
| 
ee19cdb07528
stepping towards uniform lattice theory development in HOL
 haftmann parents: 
22262diff
changeset | 4547 | 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 | 4548 | proof (rule mono_inf [where f=listsp, THEN order_antisym]) | 
| 22262 | 4549 | show "mono listsp" by (simp add: mono_def listsp_mono) | 
| 26795 
a27607030a1c
- Explicitely applied predicate1I in a few proofs, because it is no longer
 berghofe parents: 
26771diff
changeset | 4550 | show "inf (listsp A) (listsp B) \<le> listsp (inf A B)" by (blast intro!: listsp_infI predicate1I) | 
| 14388 | 4551 | qed | 
| 4552 | ||
| 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 | 4553 | 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 | 4554 | |
| 46176 
1898e61e89c4
pred_subset/equals_eq are now standard pred_set_conv rules
 berghofe parents: 
46156diff
changeset | 4555 | lemmas lists_Int_eq [simp] = listsp_inf_eq [to_set] | 
| 22262 | 4556 | |
| 39613 | 4557 | lemma Cons_in_lists_iff[simp]: "x#xs : lists A \<longleftrightarrow> x:A \<and> xs : lists A" | 
| 4558 | by auto | |
| 4559 | ||
| 22262 | 4560 | lemma append_in_listsp_conv [iff]: | 
| 4561 | "(listsp A (xs @ ys)) = (listsp A xs \<and> listsp A ys)" | |
| 15302 | 4562 | by (induct xs) auto | 
| 4563 | ||
| 22262 | 4564 | lemmas append_in_lists_conv [iff] = append_in_listsp_conv [to_set] | 
| 4565 | ||
| 4566 | lemma in_listsp_conv_set: "(listsp A xs) = (\<forall>x \<in> set xs. A x)" | |
| 4567 | -- {* eliminate @{text listsp} in favour of @{text set} *}
 | |
| 15302 | 4568 | by (induct xs) auto | 
| 4569 | ||
| 46313 | 4570 | lemmas in_lists_conv_set [code_unfold] = in_listsp_conv_set [to_set] | 
| 22262 | 4571 | |
| 35828 
46cfc4b8112e
now use "Named_Thms" for "noatp", and renamed "noatp" to "no_atp"
 blanchet parents: 
35827diff
changeset | 4572 | lemma in_listspD [dest!,no_atp]: "listsp A xs ==> \<forall>x\<in>set xs. A x" | 
| 22262 | 4573 | by (rule in_listsp_conv_set [THEN iffD1]) | 
| 4574 | ||
| 35828 
46cfc4b8112e
now use "Named_Thms" for "noatp", and renamed "noatp" to "no_atp"
 blanchet parents: 
35827diff
changeset | 4575 | lemmas in_listsD [dest!,no_atp] = in_listspD [to_set] | 
| 
46cfc4b8112e
now use "Named_Thms" for "noatp", and renamed "noatp" to "no_atp"
 blanchet parents: 
35827diff
changeset | 4576 | |
| 
46cfc4b8112e
now use "Named_Thms" for "noatp", and renamed "noatp" to "no_atp"
 blanchet parents: 
35827diff
changeset | 4577 | lemma in_listspI [intro!,no_atp]: "\<forall>x\<in>set xs. A x ==> listsp A xs" | 
| 22262 | 4578 | by (rule in_listsp_conv_set [THEN iffD2]) | 
| 4579 | ||
| 35828 
46cfc4b8112e
now use "Named_Thms" for "noatp", and renamed "noatp" to "no_atp"
 blanchet parents: 
35827diff
changeset | 4580 | lemmas in_listsI [intro!,no_atp] = in_listspI [to_set] | 
| 15302 | 4581 | |
| 39597 | 4582 | lemma lists_eq_set: "lists A = {xs. set xs <= A}"
 | 
| 4583 | by auto | |
| 4584 | ||
| 39613 | 4585 | lemma lists_empty [simp]: "lists {} = {[]}"
 | 
| 4586 | by auto | |
| 4587 | ||
| 15302 | 4588 | lemma lists_UNIV [simp]: "lists UNIV = UNIV" | 
| 4589 | by auto | |
| 4590 | ||
| 17086 | 4591 | |
| 35115 | 4592 | subsubsection {* Inductive definition for membership *}
 | 
| 17086 | 4593 | |
| 23740 | 4594 | inductive ListMem :: "'a \<Rightarrow> 'a list \<Rightarrow> bool" | 
| 22262 | 4595 | where | 
| 4596 | elem: "ListMem x (x # xs)" | |
| 4597 | | insert: "ListMem x xs \<Longrightarrow> ListMem x (y # xs)" | |
| 4598 | ||
| 4599 | lemma ListMem_iff: "(ListMem x xs) = (x \<in> set xs)" | |
| 17086 | 4600 | apply (rule iffI) | 
| 4601 | apply (induct set: ListMem) | |
| 4602 | apply auto | |
| 4603 | apply (induct xs) | |
| 4604 | apply (auto intro: ListMem.intros) | |
| 4605 | done | |
| 4606 | ||
| 4607 | ||
| 35115 | 4608 | subsubsection {* Lists as Cartesian products *}
 | 
| 15302 | 4609 | |
| 4610 | text{*@{text"set_Cons A Xs"}: the set of lists with head drawn from
 | |
| 4611 | @{term A} and tail drawn from @{term Xs}.*}
 | |
| 4612 | ||
| 34941 | 4613 | definition | 
| 4614 | set_Cons :: "'a set \<Rightarrow> 'a list set \<Rightarrow> 'a list set" where | |
| 37767 | 4615 |   "set_Cons A XS = {z. \<exists>x xs. z = x # xs \<and> x \<in> A \<and> xs \<in> XS}"
 | 
| 15302 | 4616 | |
| 17724 | 4617 | lemma set_Cons_sing_Nil [simp]: "set_Cons A {[]} = (%x. [x])`A"
 | 
| 15302 | 4618 | by (auto simp add: set_Cons_def) | 
| 4619 | ||
| 4620 | text{*Yields the set of lists, all of the same length as the argument and
 | |
| 4621 | with elements drawn from the corresponding element of the argument.*} | |
| 4622 | ||
| 4623 | primrec | |
| 34941 | 4624 | listset :: "'a set list \<Rightarrow> 'a list set" where | 
| 4625 |      "listset [] = {[]}"
 | |
| 4626 | | "listset (A # As) = set_Cons A (listset As)" | |
| 15302 | 4627 | |
| 4628 | ||
| 35115 | 4629 | subsection {* Relations on Lists *}
 | 
| 15656 | 4630 | |
| 4631 | subsubsection {* Length Lexicographic Ordering *}
 | |
| 4632 | ||
| 4633 | text{*These orderings preserve well-foundedness: shorter lists 
 | |
| 4634 | precede longer lists. These ordering are not used in dictionaries.*} | |
| 34941 | 4635 | |
| 4636 | primrec -- {*The lexicographic ordering for lists of the specified length*}
 | |
| 4637 |   lexn :: "('a \<times> 'a) set \<Rightarrow> nat \<Rightarrow> ('a list \<times> 'a list) set" where
 | |
| 37767 | 4638 |     "lexn r 0 = {}"
 | 
| 40608 
6c28ab8b8166
mapper for list type; map_pair replaces prod_fun
 haftmann parents: 
40593diff
changeset | 4639 | | "lexn r (Suc n) = (map_pair (%(x, xs). x#xs) (%(x, xs). x#xs) ` (r <*lex*> lexn r n)) Int | 
| 34941 | 4640 |       {(xs, ys). length xs = Suc n \<and> length ys = Suc n}"
 | 
| 4641 | ||
| 4642 | definition | |
| 4643 |   lex :: "('a \<times> 'a) set \<Rightarrow> ('a list \<times> 'a list) set" where
 | |
| 37767 | 4644 |   "lex r = (\<Union>n. lexn r n)" -- {*Holds only between lists of the same length*}
 | 
| 34941 | 4645 | |
| 4646 | definition | |
| 4647 |   lenlex :: "('a \<times> 'a) set => ('a list \<times> 'a list) set" where
 | |
| 37767 | 4648 | "lenlex r = inv_image (less_than <*lex*> lex r) (\<lambda>xs. (length xs, xs))" | 
| 34941 | 4649 |         -- {*Compares lists by their length and then lexicographically*}
 | 
| 15302 | 4650 | |
| 4651 | lemma wf_lexn: "wf r ==> wf (lexn r n)" | |
| 4652 | apply (induct n, simp, simp) | |
| 4653 | apply(rule wf_subset) | |
| 4654 | prefer 2 apply (rule Int_lower1) | |
| 40608 
6c28ab8b8166
mapper for list type; map_pair replaces prod_fun
 haftmann parents: 
40593diff
changeset | 4655 | apply(rule wf_map_pair_image) | 
| 15302 | 4656 | prefer 2 apply (rule inj_onI, auto) | 
| 4657 | done | |
| 4658 | ||
| 4659 | lemma lexn_length: | |
| 24526 | 4660 | "(xs, ys) : lexn r n ==> length xs = n \<and> length ys = n" | 
| 4661 | by (induct n arbitrary: xs ys) auto | |
| 15302 | 4662 | |
| 4663 | lemma wf_lex [intro!]: "wf r ==> wf (lex r)" | |
| 4664 | apply (unfold lex_def) | |
| 4665 | apply (rule wf_UN) | |
| 4666 | apply (blast intro: wf_lexn, clarify) | |
| 4667 | apply (rename_tac m n) | |
| 4668 | apply (subgoal_tac "m \<noteq> n") | |
| 4669 | prefer 2 apply blast | |
| 4670 | apply (blast dest: lexn_length not_sym) | |
| 4671 | done | |
| 4672 | ||
| 4673 | lemma lexn_conv: | |
| 15656 | 4674 | "lexn r n = | 
| 4675 |     {(xs,ys). length xs = n \<and> length ys = n \<and>
 | |
| 4676 | (\<exists>xys x y xs' ys'. xs= xys @ x#xs' \<and> ys= xys @ y # ys' \<and> (x, y):r)}" | |
| 18423 | 4677 | apply (induct n, simp) | 
| 15302 | 4678 | apply (simp add: image_Collect lex_prod_def, safe, blast) | 
| 4679 | apply (rule_tac x = "ab # xys" in exI, simp) | |
| 4680 | apply (case_tac xys, simp_all, blast) | |
| 4681 | done | |
| 4682 | ||
| 4683 | lemma lex_conv: | |
| 15656 | 4684 | "lex r = | 
| 4685 |     {(xs,ys). length xs = length ys \<and>
 | |
| 4686 | (\<exists>xys x y xs' ys'. xs = xys @ x # xs' \<and> ys = xys @ y # ys' \<and> (x, y):r)}" | |
| 15302 | 4687 | by (force simp add: lex_def lexn_conv) | 
| 4688 | ||
| 15693 | 4689 | lemma wf_lenlex [intro!]: "wf r ==> wf (lenlex r)" | 
| 4690 | by (unfold lenlex_def) blast | |
| 4691 | ||
| 4692 | lemma lenlex_conv: | |
| 4693 |     "lenlex r = {(xs,ys). length xs < length ys |
 | |
| 15656 | 4694 | length xs = length ys \<and> (xs, ys) : lex r}" | 
| 30198 | 4695 | by (simp add: lenlex_def Id_on_def lex_prod_def inv_image_def) | 
| 15302 | 4696 | |
| 4697 | lemma Nil_notin_lex [iff]: "([], ys) \<notin> lex r" | |
| 4698 | by (simp add: lex_conv) | |
| 4699 | ||
| 4700 | lemma Nil2_notin_lex [iff]: "(xs, []) \<notin> lex r" | |
| 4701 | by (simp add:lex_conv) | |
| 4702 | ||
| 18447 | 4703 | lemma Cons_in_lex [simp]: | 
| 15656 | 4704 | "((x # xs, y # ys) : lex r) = | 
| 4705 | ((x, y) : r \<and> length xs = length ys | x = y \<and> (xs, ys) : lex r)" | |
| 15302 | 4706 | apply (simp add: lex_conv) | 
| 4707 | apply (rule iffI) | |
| 4708 | prefer 2 apply (blast intro: Cons_eq_appendI, clarify) | |
| 4709 | apply (case_tac xys, simp, simp) | |
| 4710 | apply blast | |
| 4711 | done | |
| 4712 | ||
| 4713 | ||
| 15656 | 4714 | subsubsection {* Lexicographic Ordering *}
 | 
| 4715 | ||
| 4716 | text {* Classical lexicographic ordering on lists, ie. "a" < "ab" < "b".
 | |
| 4717 |     This ordering does \emph{not} preserve well-foundedness.
 | |
| 17090 | 4718 | Author: N. Voelker, March 2005. *} | 
| 15656 | 4719 | |
| 34941 | 4720 | definition | 
| 4721 |   lexord :: "('a \<times> 'a) set \<Rightarrow> ('a list \<times> 'a list) set" where
 | |
| 37767 | 4722 |   "lexord r = {(x,y ). \<exists> a v. y = x @ a # v \<or>
 | 
| 15656 | 4723 | (\<exists> u a b v w. (a,b) \<in> r \<and> x = u @ (a # v) \<and> y = u @ (b # w))}" | 
| 4724 | ||
| 4725 | lemma lexord_Nil_left[simp]: "([],y) \<in> lexord r = (\<exists> a x. y = a # x)" | |
| 24349 | 4726 | by (unfold lexord_def, induct_tac y, auto) | 
| 15656 | 4727 | |
| 4728 | lemma lexord_Nil_right[simp]: "(x,[]) \<notin> lexord r" | |
| 24349 | 4729 | by (unfold lexord_def, induct_tac x, auto) | 
| 15656 | 4730 | |
| 4731 | lemma lexord_cons_cons[simp]: | |
| 4732 | "((a # x, b # y) \<in> lexord r) = ((a,b)\<in> r | (a = b & (x,y)\<in> lexord r))" | |
| 4733 | apply (unfold lexord_def, safe, simp_all) | |
| 4734 | apply (case_tac u, simp, simp) | |
| 4735 | apply (case_tac u, simp, clarsimp, blast, blast, clarsimp) | |
| 4736 | apply (erule_tac x="b # u" in allE) | |
| 4737 | by force | |
| 4738 | ||
| 4739 | lemmas lexord_simps = lexord_Nil_left lexord_Nil_right lexord_cons_cons | |
| 4740 | ||
| 4741 | lemma lexord_append_rightI: "\<exists> b z. y = b # z \<Longrightarrow> (x, x @ y) \<in> lexord r" | |
| 24349 | 4742 | by (induct_tac x, auto) | 
| 15656 | 4743 | |
| 4744 | lemma lexord_append_left_rightI: | |
| 4745 | "(a,b) \<in> r \<Longrightarrow> (u @ a # x, u @ b # y) \<in> lexord r" | |
| 24349 | 4746 | by (induct_tac u, auto) | 
| 15656 | 4747 | |
| 4748 | lemma lexord_append_leftI: " (u,v) \<in> lexord r \<Longrightarrow> (x @ u, x @ v) \<in> lexord r" | |
| 24349 | 4749 | by (induct x, auto) | 
| 15656 | 4750 | |
| 4751 | lemma lexord_append_leftD: | |
| 4752 | "\<lbrakk> (x @ u, x @ v) \<in> lexord r; (! a. (a,a) \<notin> r) \<rbrakk> \<Longrightarrow> (u,v) \<in> lexord r" | |
| 24349 | 4753 | by (erule rev_mp, induct_tac x, auto) | 
| 15656 | 4754 | |
| 4755 | lemma lexord_take_index_conv: | |
| 4756 | "((x,y) : lexord r) = | |
| 4757 | ((length x < length y \<and> take (length x) y = x) \<or> | |
| 4758 | (\<exists>i. i < min(length x)(length y) & take i x = take i y & (x!i,y!i) \<in> r))" | |
| 4759 | apply (unfold lexord_def Let_def, clarsimp) | |
| 4760 | apply (rule_tac f = "(% a b. a \<or> b)" in arg_cong2) | |
| 4761 | apply auto | |
| 4762 | apply (rule_tac x="hd (drop (length x) y)" in exI) | |
| 4763 | apply (rule_tac x="tl (drop (length x) y)" in exI) | |
| 4764 | apply (erule subst, simp add: min_def) | |
| 4765 | apply (rule_tac x ="length u" in exI, simp) | |
| 4766 | apply (rule_tac x ="take i x" in exI) | |
| 4767 | apply (rule_tac x ="x ! i" in exI) | |
| 4768 | apply (rule_tac x ="y ! i" in exI, safe) | |
| 4769 | apply (rule_tac x="drop (Suc i) x" in exI) | |
| 4770 | apply (drule sym, simp add: drop_Suc_conv_tl) | |
| 4771 | apply (rule_tac x="drop (Suc i) y" in exI) | |
| 4772 | by (simp add: drop_Suc_conv_tl) | |
| 4773 | ||
| 4774 | -- {* lexord is extension of partial ordering List.lex *} 
 | |
| 41986 | 4775 | lemma lexord_lex: "(x,y) \<in> lex r = ((x,y) \<in> lexord r \<and> length x = length y)" | 
| 15656 | 4776 | apply (rule_tac x = y in spec) | 
| 4777 | apply (induct_tac x, clarsimp) | |
| 4778 | by (clarify, case_tac x, simp, force) | |
| 4779 | ||
| 41986 | 4780 | lemma lexord_irreflexive: "ALL x. (x,x) \<notin> r \<Longrightarrow> (xs,xs) \<notin> lexord r" | 
| 4781 | by (induct xs) auto | |
| 4782 | ||
| 4783 | text{* By Ren\'e Thiemann: *}
 | |
| 4784 | lemma lexord_partial_trans: | |
| 4785 | "(\<And>x y z. x \<in> set xs \<Longrightarrow> (x,y) \<in> r \<Longrightarrow> (y,z) \<in> r \<Longrightarrow> (x,z) \<in> r) | |
| 4786 | \<Longrightarrow> (xs,ys) \<in> lexord r \<Longrightarrow> (ys,zs) \<in> lexord r \<Longrightarrow> (xs,zs) \<in> lexord r" | |
| 4787 | proof (induct xs arbitrary: ys zs) | |
| 4788 | case Nil | |
| 4789 | from Nil(3) show ?case unfolding lexord_def by (cases zs, auto) | |
| 4790 | next | |
| 4791 | case (Cons x xs yys zzs) | |
| 4792 | from Cons(3) obtain y ys where yys: "yys = y # ys" unfolding lexord_def | |
| 4793 | by (cases yys, auto) | |
| 4794 | note Cons = Cons[unfolded yys] | |
| 4795 | from Cons(3) have one: "(x,y) \<in> r \<or> x = y \<and> (xs,ys) \<in> lexord r" by auto | |
| 4796 | from Cons(4) obtain z zs where zzs: "zzs = z # zs" unfolding lexord_def | |
| 4797 | by (cases zzs, auto) | |
| 4798 | note Cons = Cons[unfolded zzs] | |
| 4799 | from Cons(4) have two: "(y,z) \<in> r \<or> y = z \<and> (ys,zs) \<in> lexord r" by auto | |
| 4800 |   {
 | |
| 4801 | assume "(xs,ys) \<in> lexord r" and "(ys,zs) \<in> lexord r" | |
| 4802 | from Cons(1)[OF _ this] Cons(2) | |
| 4803 | have "(xs,zs) \<in> lexord r" by auto | |
| 4804 | } note ind1 = this | |
| 4805 |   {
 | |
| 4806 | assume "(x,y) \<in> r" and "(y,z) \<in> r" | |
| 4807 | from Cons(2)[OF _ this] have "(x,z) \<in> r" by auto | |
| 4808 | } note ind2 = this | |
| 4809 | from one two ind1 ind2 | |
| 4810 | have "(x,z) \<in> r \<or> x = z \<and> (xs,zs) \<in> lexord r" by blast | |
| 4811 | thus ?case unfolding zzs by auto | |
| 4812 | qed | |
| 15656 | 4813 | |
| 4814 | lemma lexord_trans: | |
| 4815 | "\<lbrakk> (x, y) \<in> lexord r; (y, z) \<in> lexord r; trans r \<rbrakk> \<Longrightarrow> (x, z) \<in> lexord r" | |
| 41986 | 4816 | by(auto simp: trans_def intro:lexord_partial_trans) | 
| 15656 | 4817 | |
| 4818 | lemma lexord_transI: "trans r \<Longrightarrow> trans (lexord r)" | |
| 24349 | 4819 | by (rule transI, drule lexord_trans, blast) | 
| 15656 | 4820 | |
| 4821 | 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" | |
| 4822 | apply (rule_tac x = y in spec) | |
| 4823 | apply (induct_tac x, rule allI) | |
| 4824 | apply (case_tac x, simp, simp) | |
| 4825 | apply (rule allI, case_tac x, simp, simp) | |
| 4826 | by blast | |
| 4827 | ||
| 4828 | ||
| 40230 | 4829 | subsubsection {* Lexicographic combination of measure functions *}
 | 
| 21103 
367b4ad7c7cc
Added "measures" combinator for lexicographic combinations of multiple measures.
 krauss parents: 
21079diff
changeset | 4830 | |
| 
367b4ad7c7cc
Added "measures" combinator for lexicographic combinations of multiple measures.
 krauss parents: 
21079diff
changeset | 4831 | text {* These are useful for termination proofs *}
 | 
| 
367b4ad7c7cc
Added "measures" combinator for lexicographic combinations of multiple measures.
 krauss parents: 
21079diff
changeset | 4832 | |
| 
367b4ad7c7cc
Added "measures" combinator for lexicographic combinations of multiple measures.
 krauss parents: 
21079diff
changeset | 4833 | definition | 
| 
367b4ad7c7cc
Added "measures" combinator for lexicographic combinations of multiple measures.
 krauss parents: 
21079diff
changeset | 4834 | "measures fs = inv_image (lex less_than) (%a. map (%f. f a) fs)" | 
| 
367b4ad7c7cc
Added "measures" combinator for lexicographic combinations of multiple measures.
 krauss parents: 
21079diff
changeset | 4835 | |
| 44013 
5cfc1c36ae97
moved recdef package to HOL/Library/Old_Recdef.thy
 krauss parents: 
43594diff
changeset | 4836 | lemma wf_measures[simp]: "wf (measures fs)" | 
| 24349 | 4837 | unfolding measures_def | 
| 4838 | by blast | |
| 21103 
367b4ad7c7cc
Added "measures" combinator for lexicographic combinations of multiple measures.
 krauss parents: 
21079diff
changeset | 4839 | |
| 
367b4ad7c7cc
Added "measures" combinator for lexicographic combinations of multiple measures.
 krauss parents: 
21079diff
changeset | 4840 | lemma in_measures[simp]: | 
| 
367b4ad7c7cc
Added "measures" combinator for lexicographic combinations of multiple measures.
 krauss parents: 
21079diff
changeset | 4841 | "(x, y) \<in> measures [] = False" | 
| 
367b4ad7c7cc
Added "measures" combinator for lexicographic combinations of multiple measures.
 krauss parents: 
21079diff
changeset | 4842 | "(x, y) \<in> measures (f # fs) | 
| 
367b4ad7c7cc
Added "measures" combinator for lexicographic combinations of multiple measures.
 krauss parents: 
21079diff
changeset | 4843 | = (f x < f y \<or> (f x = f y \<and> (x, y) \<in> measures fs))" | 
| 24349 | 4844 | unfolding measures_def | 
| 4845 | by auto | |
| 21103 
367b4ad7c7cc
Added "measures" combinator for lexicographic combinations of multiple measures.
 krauss parents: 
21079diff
changeset | 4846 | |
| 
367b4ad7c7cc
Added "measures" combinator for lexicographic combinations of multiple measures.
 krauss parents: 
21079diff
changeset | 4847 | lemma measures_less: "f x < f y ==> (x, y) \<in> measures (f#fs)" | 
| 24349 | 4848 | by simp | 
| 21103 
367b4ad7c7cc
Added "measures" combinator for lexicographic combinations of multiple measures.
 krauss parents: 
21079diff
changeset | 4849 | |
| 
367b4ad7c7cc
Added "measures" combinator for lexicographic combinations of multiple measures.
 krauss parents: 
21079diff
changeset | 4850 | lemma measures_lesseq: "f x <= f y ==> (x, y) \<in> measures fs ==> (x, y) \<in> measures (f#fs)" | 
| 24349 | 4851 | by auto | 
| 21103 
367b4ad7c7cc
Added "measures" combinator for lexicographic combinations of multiple measures.
 krauss parents: 
21079diff
changeset | 4852 | |
| 
367b4ad7c7cc
Added "measures" combinator for lexicographic combinations of multiple measures.
 krauss parents: 
21079diff
changeset | 4853 | |
| 40230 | 4854 | subsubsection {* Lifting Relations to Lists: one element *}
 | 
| 4855 | ||
| 4856 | definition listrel1 :: "('a \<times> 'a) set \<Rightarrow> ('a list \<times> 'a list) set" where
 | |
| 4857 | "listrel1 r = {(xs,ys).
 | |
| 4858 | \<exists>us z z' vs. xs = us @ z # vs \<and> (z,z') \<in> r \<and> ys = us @ z' # vs}" | |
| 4859 | ||
| 4860 | lemma listrel1I: | |
| 4861 | "\<lbrakk> (x, y) \<in> r; xs = us @ x # vs; ys = us @ y # vs \<rbrakk> \<Longrightarrow> | |
| 4862 | (xs, ys) \<in> listrel1 r" | |
| 4863 | unfolding listrel1_def by auto | |
| 4864 | ||
| 4865 | lemma listrel1E: | |
| 4866 | "\<lbrakk> (xs, ys) \<in> listrel1 r; | |
| 4867 | !!x y us vs. \<lbrakk> (x, y) \<in> r; xs = us @ x # vs; ys = us @ y # vs \<rbrakk> \<Longrightarrow> P | |
| 4868 | \<rbrakk> \<Longrightarrow> P" | |
| 4869 | unfolding listrel1_def by auto | |
| 4870 | ||
| 4871 | lemma not_Nil_listrel1 [iff]: "([], xs) \<notin> listrel1 r" | |
| 4872 | unfolding listrel1_def by blast | |
| 4873 | ||
| 4874 | lemma not_listrel1_Nil [iff]: "(xs, []) \<notin> listrel1 r" | |
| 4875 | unfolding listrel1_def by blast | |
| 4876 | ||
| 4877 | lemma Cons_listrel1_Cons [iff]: | |
| 4878 | "(x # xs, y # ys) \<in> listrel1 r \<longleftrightarrow> | |
| 4879 | (x,y) \<in> r \<and> xs = ys \<or> x = y \<and> (xs, ys) \<in> listrel1 r" | |
| 4880 | by (simp add: listrel1_def Cons_eq_append_conv) (blast) | |
| 4881 | ||
| 4882 | lemma listrel1I1: "(x,y) \<in> r \<Longrightarrow> (x # xs, y # xs) \<in> listrel1 r" | |
| 4883 | by (metis Cons_listrel1_Cons) | |
| 4884 | ||
| 4885 | lemma listrel1I2: "(xs, ys) \<in> listrel1 r \<Longrightarrow> (x # xs, x # ys) \<in> listrel1 r" | |
| 4886 | by (metis Cons_listrel1_Cons) | |
| 4887 | ||
| 4888 | lemma append_listrel1I: | |
| 4889 | "(xs, ys) \<in> listrel1 r \<and> us = vs \<or> xs = ys \<and> (us, vs) \<in> listrel1 r | |
| 4890 | \<Longrightarrow> (xs @ us, ys @ vs) \<in> listrel1 r" | |
| 4891 | unfolding listrel1_def | |
| 4892 | by auto (blast intro: append_eq_appendI)+ | |
| 4893 | ||
| 4894 | lemma Cons_listrel1E1[elim!]: | |
| 4895 | assumes "(x # xs, ys) \<in> listrel1 r" | |
| 4896 | and "\<And>y. ys = y # xs \<Longrightarrow> (x, y) \<in> r \<Longrightarrow> R" | |
| 4897 | and "\<And>zs. ys = x # zs \<Longrightarrow> (xs, zs) \<in> listrel1 r \<Longrightarrow> R" | |
| 4898 | shows R | |
| 4899 | using assms by (cases ys) blast+ | |
| 4900 | ||
| 4901 | lemma Cons_listrel1E2[elim!]: | |
| 4902 | assumes "(xs, y # ys) \<in> listrel1 r" | |
| 4903 | and "\<And>x. xs = x # ys \<Longrightarrow> (x, y) \<in> r \<Longrightarrow> R" | |
| 4904 | and "\<And>zs. xs = y # zs \<Longrightarrow> (zs, ys) \<in> listrel1 r \<Longrightarrow> R" | |
| 4905 | shows R | |
| 4906 | using assms by (cases xs) blast+ | |
| 4907 | ||
| 4908 | lemma snoc_listrel1_snoc_iff: | |
| 4909 | "(xs @ [x], ys @ [y]) \<in> listrel1 r | |
| 4910 | \<longleftrightarrow> (xs, ys) \<in> listrel1 r \<and> x = y \<or> xs = ys \<and> (x,y) \<in> r" (is "?L \<longleftrightarrow> ?R") | |
| 4911 | proof | |
| 4912 | assume ?L thus ?R | |
| 44890 
22f665a2e91c
new fastforce replacing fastsimp - less confusing name
 nipkow parents: 
44635diff
changeset | 4913 | by (fastforce simp: listrel1_def snoc_eq_iff_butlast butlast_append) | 
| 40230 | 4914 | next | 
| 4915 | assume ?R then show ?L unfolding listrel1_def by force | |
| 4916 | qed | |
| 4917 | ||
| 4918 | lemma listrel1_eq_len: "(xs,ys) \<in> listrel1 r \<Longrightarrow> length xs = length ys" | |
| 4919 | unfolding listrel1_def by auto | |
| 4920 | ||
| 4921 | lemma listrel1_mono: | |
| 4922 | "r \<subseteq> s \<Longrightarrow> listrel1 r \<subseteq> listrel1 s" | |
| 4923 | unfolding listrel1_def by blast | |
| 4924 | ||
| 4925 | ||
| 4926 | lemma listrel1_converse: "listrel1 (r^-1) = (listrel1 r)^-1" | |
| 4927 | unfolding listrel1_def by blast | |
| 4928 | ||
| 4929 | lemma in_listrel1_converse: | |
| 4930 | "(x,y) : listrel1 (r^-1) \<longleftrightarrow> (x,y) : (listrel1 r)^-1" | |
| 4931 | unfolding listrel1_def by blast | |
| 4932 | ||
| 4933 | lemma listrel1_iff_update: | |
| 4934 | "(xs,ys) \<in> (listrel1 r) | |
| 4935 | \<longleftrightarrow> (\<exists>y n. (xs ! n, y) \<in> r \<and> n < length xs \<and> ys = xs[n:=y])" (is "?L \<longleftrightarrow> ?R") | |
| 4936 | proof | |
| 4937 | assume "?L" | |
| 4938 | then obtain x y u v where "xs = u @ x # v" "ys = u @ y # v" "(x,y) \<in> r" | |
| 4939 | unfolding listrel1_def by auto | |
| 4940 | then have "ys = xs[length u := y]" and "length u < length xs" | |
| 4941 | and "(xs ! length u, y) \<in> r" by auto | |
| 4942 | then show "?R" by auto | |
| 4943 | next | |
| 4944 | assume "?R" | |
| 4945 | then obtain x y n where "(xs!n, y) \<in> r" "n < size xs" "ys = xs[n:=y]" "x = xs!n" | |
| 4946 | by auto | |
| 4947 | then obtain u v where "xs = u @ x # v" and "ys = u @ y # v" and "(x, y) \<in> r" | |
| 4948 | by (auto intro: upd_conv_take_nth_drop id_take_nth_drop) | |
| 4949 | then show "?L" by (auto simp: listrel1_def) | |
| 4950 | qed | |
| 4951 | ||
| 4952 | ||
| 44510 | 4953 | text{* Accessible part and wellfoundedness: *}
 | 
| 40230 | 4954 | |
| 4955 | lemma Cons_acc_listrel1I [intro!]: | |
| 4956 | "x \<in> acc r \<Longrightarrow> xs \<in> acc (listrel1 r) \<Longrightarrow> (x # xs) \<in> acc (listrel1 r)" | |
| 4957 | apply (induct arbitrary: xs set: acc) | |
| 4958 | apply (erule thin_rl) | |
| 4959 | apply (erule acc_induct) | |
| 4960 | apply (rule accI) | |
| 4961 | apply (blast) | |
| 4962 | done | |
| 4963 | ||
| 4964 | lemma lists_accD: "xs \<in> lists (acc r) \<Longrightarrow> xs \<in> acc (listrel1 r)" | |
| 4965 | apply (induct set: lists) | |
| 4966 | apply (rule accI) | |
| 4967 | apply simp | |
| 4968 | apply (rule accI) | |
| 4969 | apply (fast dest: acc_downward) | |
| 4970 | done | |
| 4971 | ||
| 4972 | lemma lists_accI: "xs \<in> acc (listrel1 r) \<Longrightarrow> xs \<in> lists (acc r)" | |
| 4973 | apply (induct set: acc) | |
| 4974 | apply clarify | |
| 4975 | apply (rule accI) | |
| 44890 
22f665a2e91c
new fastforce replacing fastsimp - less confusing name
 nipkow parents: 
44635diff
changeset | 4976 | apply (fastforce dest!: in_set_conv_decomp[THEN iffD1] simp: listrel1_def) | 
| 40230 | 4977 | done | 
| 4978 | ||
| 44510 | 4979 | lemma wf_listrel1_iff[simp]: "wf(listrel1 r) = wf r" | 
| 4980 | by(metis wf_acc_iff in_lists_conv_set lists_accI lists_accD Cons_in_lists_iff) | |
| 4981 | ||
| 40230 | 4982 | |
| 4983 | subsubsection {* Lifting Relations to Lists: all elements *}
 | |
| 15302 | 4984 | |
| 23740 | 4985 | inductive_set | 
| 46317 | 4986 |   listrel :: "('a \<times> 'b) set \<Rightarrow> ('a list \<times> 'b list) set"
 | 
| 4987 |   for r :: "('a \<times> 'b) set"
 | |
| 22262 | 4988 | where | 
| 23740 | 4989 | Nil: "([],[]) \<in> listrel r" | 
| 4990 | | Cons: "[| (x,y) \<in> r; (xs,ys) \<in> listrel r |] ==> (x#xs, y#ys) \<in> listrel r" | |
| 4991 | ||
| 4992 | inductive_cases listrel_Nil1 [elim!]: "([],xs) \<in> listrel r" | |
| 4993 | inductive_cases listrel_Nil2 [elim!]: "(xs,[]) \<in> listrel r" | |
| 4994 | inductive_cases listrel_Cons1 [elim!]: "(y#ys,xs) \<in> listrel r" | |
| 4995 | inductive_cases listrel_Cons2 [elim!]: "(xs,y#ys) \<in> listrel r" | |
| 15302 | 4996 | |
| 4997 | ||
| 40230 | 4998 | lemma listrel_eq_len: "(xs, ys) \<in> listrel r \<Longrightarrow> length xs = length ys" | 
| 4999 | by(induct rule: listrel.induct) auto | |
| 5000 | ||
| 46313 | 5001 | lemma listrel_iff_zip [code_unfold]: "(xs,ys) : listrel r \<longleftrightarrow> | 
| 40230 | 5002 | length xs = length ys & (\<forall>(x,y) \<in> set(zip xs ys). (x,y) \<in> r)" (is "?L \<longleftrightarrow> ?R") | 
| 5003 | proof | |
| 5004 | assume ?L thus ?R by induct (auto intro: listrel_eq_len) | |
| 5005 | next | |
| 5006 | assume ?R thus ?L | |
| 5007 | apply (clarify) | |
| 5008 | by (induct rule: list_induct2) (auto intro: listrel.intros) | |
| 5009 | qed | |
| 5010 | ||
| 5011 | lemma listrel_iff_nth: "(xs,ys) : listrel r \<longleftrightarrow> | |
| 5012 | length xs = length ys & (\<forall>n < length xs. (xs!n, ys!n) \<in> r)" (is "?L \<longleftrightarrow> ?R") | |
| 5013 | by (auto simp add: all_set_conv_all_nth listrel_iff_zip) | |
| 5014 | ||
| 5015 | ||
| 15302 | 5016 | lemma listrel_mono: "r \<subseteq> s \<Longrightarrow> listrel r \<subseteq> listrel s" | 
| 5017 | apply clarify | |
| 23740 | 5018 | apply (erule listrel.induct) | 
| 5019 | apply (blast intro: listrel.intros)+ | |
| 15302 | 5020 | done | 
| 5021 | ||
| 5022 | lemma listrel_subset: "r \<subseteq> A \<times> A \<Longrightarrow> listrel r \<subseteq> lists A \<times> lists A" | |
| 5023 | apply clarify | |
| 23740 | 5024 | apply (erule listrel.induct, auto) | 
| 15302 | 5025 | done | 
| 5026 | ||
| 30198 | 5027 | lemma listrel_refl_on: "refl_on A r \<Longrightarrow> refl_on (lists A) (listrel r)" | 
| 5028 | apply (simp add: refl_on_def listrel_subset Ball_def) | |
| 15302 | 5029 | apply (rule allI) | 
| 5030 | apply (induct_tac x) | |
| 23740 | 5031 | apply (auto intro: listrel.intros) | 
| 15302 | 5032 | done | 
| 5033 | ||
| 5034 | lemma listrel_sym: "sym r \<Longrightarrow> sym (listrel r)" | |
| 5035 | apply (auto simp add: sym_def) | |
| 23740 | 5036 | apply (erule listrel.induct) | 
| 5037 | apply (blast intro: listrel.intros)+ | |
| 15302 | 5038 | done | 
| 5039 | ||
| 5040 | lemma listrel_trans: "trans r \<Longrightarrow> trans (listrel r)" | |
| 5041 | apply (simp add: trans_def) | |
| 5042 | apply (intro allI) | |
| 5043 | apply (rule impI) | |
| 23740 | 5044 | apply (erule listrel.induct) | 
| 5045 | apply (blast intro: listrel.intros)+ | |
| 15302 | 5046 | done | 
| 5047 | ||
| 5048 | theorem equiv_listrel: "equiv A r \<Longrightarrow> equiv (lists A) (listrel r)" | |
| 30198 | 5049 | by (simp add: equiv_def listrel_refl_on listrel_sym listrel_trans) | 
| 15302 | 5050 | |
| 40230 | 5051 | lemma listrel_rtrancl_refl[iff]: "(xs,xs) : listrel(r^*)" | 
| 5052 | using listrel_refl_on[of UNIV, OF refl_rtrancl] | |
| 5053 | by(auto simp: refl_on_def) | |
| 5054 | ||
| 5055 | lemma listrel_rtrancl_trans: | |
| 5056 | "\<lbrakk> (xs,ys) : listrel(r^*); (ys,zs) : listrel(r^*) \<rbrakk> | |
| 5057 | \<Longrightarrow> (xs,zs) : listrel(r^*)" | |
| 5058 | by (metis listrel_trans trans_def trans_rtrancl) | |
| 5059 | ||
| 5060 | ||
| 15302 | 5061 | lemma listrel_Nil [simp]: "listrel r `` {[]} = {[]}"
 | 
| 23740 | 5062 | by (blast intro: listrel.intros) | 
| 15302 | 5063 | |
| 5064 | lemma listrel_Cons: | |
| 33318 
ddd97d9dfbfb
moved Nat_Transfer before Divides; distributed Nat_Transfer setup accordingly
 haftmann parents: 
32960diff
changeset | 5065 |      "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 | 5066 | by (auto simp add: set_Cons_def intro: listrel.intros) | 
| 15302 | 5067 | |
| 40230 | 5068 | text {* Relating @{term listrel1}, @{term listrel} and closures: *}
 | 
| 5069 | ||
| 5070 | lemma listrel1_rtrancl_subset_rtrancl_listrel1: | |
| 5071 | "listrel1 (r^*) \<subseteq> (listrel1 r)^*" | |
| 5072 | proof (rule subrelI) | |
| 5073 | fix xs ys assume 1: "(xs,ys) \<in> listrel1 (r^*)" | |
| 5074 |   { fix x y us vs
 | |
| 5075 | have "(x,y) : r^* \<Longrightarrow> (us @ x # vs, us @ y # vs) : (listrel1 r)^*" | |
| 5076 | proof(induct rule: rtrancl.induct) | |
| 5077 | case rtrancl_refl show ?case by simp | |
| 5078 | next | |
| 5079 | case rtrancl_into_rtrancl thus ?case | |
| 5080 | by (metis listrel1I rtrancl.rtrancl_into_rtrancl) | |
| 5081 | qed } | |
| 5082 | thus "(xs,ys) \<in> (listrel1 r)^*" using 1 by(blast elim: listrel1E) | |
| 5083 | qed | |
| 5084 | ||
| 5085 | lemma rtrancl_listrel1_eq_len: "(x,y) \<in> (listrel1 r)^* \<Longrightarrow> length x = length y" | |
| 5086 | by (induct rule: rtrancl.induct) (auto intro: listrel1_eq_len) | |
| 5087 | ||
| 5088 | lemma rtrancl_listrel1_ConsI1: | |
| 5089 | "(xs,ys) : (listrel1 r)^* \<Longrightarrow> (x#xs,x#ys) : (listrel1 r)^*" | |
| 5090 | apply(induct rule: rtrancl.induct) | |
| 5091 | apply simp | |
| 5092 | by (metis listrel1I2 rtrancl.rtrancl_into_rtrancl) | |
| 5093 | ||
| 5094 | lemma rtrancl_listrel1_ConsI2: | |
| 5095 | "(x,y) \<in> r^* \<Longrightarrow> (xs, ys) \<in> (listrel1 r)^* | |
| 5096 | \<Longrightarrow> (x # xs, y # ys) \<in> (listrel1 r)^*" | |
| 5097 | by (blast intro: rtrancl_trans rtrancl_listrel1_ConsI1 | |
| 5098 | subsetD[OF listrel1_rtrancl_subset_rtrancl_listrel1 listrel1I1]) | |
| 5099 | ||
| 5100 | lemma listrel1_subset_listrel: | |
| 5101 | "r \<subseteq> r' \<Longrightarrow> refl r' \<Longrightarrow> listrel1 r \<subseteq> listrel(r')" | |
| 5102 | by(auto elim!: listrel1E simp add: listrel_iff_zip set_zip refl_on_def) | |
| 5103 | ||
| 5104 | lemma listrel_reflcl_if_listrel1: | |
| 5105 | "(xs,ys) : listrel1 r \<Longrightarrow> (xs,ys) : listrel(r^*)" | |
| 5106 | by(erule listrel1E)(auto simp add: listrel_iff_zip set_zip) | |
| 5107 | ||
| 5108 | lemma listrel_rtrancl_eq_rtrancl_listrel1: "listrel (r^*) = (listrel1 r)^*" | |
| 5109 | proof | |
| 5110 |   { fix x y assume "(x,y) \<in> listrel (r^*)"
 | |
| 5111 | then have "(x,y) \<in> (listrel1 r)^*" | |
| 5112 | by induct (auto intro: rtrancl_listrel1_ConsI2) } | |
| 5113 | then show "listrel (r^*) \<subseteq> (listrel1 r)^*" | |
| 5114 | by (rule subrelI) | |
| 5115 | next | |
| 5116 | show "listrel (r^*) \<supseteq> (listrel1 r)^*" | |
| 5117 | proof(rule subrelI) | |
| 5118 | fix xs ys assume "(xs,ys) \<in> (listrel1 r)^*" | |
| 5119 | then show "(xs,ys) \<in> listrel (r^*)" | |
| 5120 | proof induct | |
| 5121 | case base show ?case by(auto simp add: listrel_iff_zip set_zip) | |
| 5122 | next | |
| 5123 | case (step ys zs) | |
| 5124 | thus ?case by (metis listrel_reflcl_if_listrel1 listrel_rtrancl_trans) | |
| 5125 | qed | |
| 5126 | qed | |
| 5127 | qed | |
| 5128 | ||
| 5129 | lemma rtrancl_listrel1_if_listrel: | |
| 5130 | "(xs,ys) : listrel r \<Longrightarrow> (xs,ys) : (listrel1 r)^*" | |
| 5131 | by(metis listrel_rtrancl_eq_rtrancl_listrel1 subsetD[OF listrel_mono] r_into_rtrancl subsetI) | |
| 5132 | ||
| 5133 | lemma listrel_subset_rtrancl_listrel1: "listrel r \<subseteq> (listrel1 r)^*" | |
| 5134 | by(fast intro:rtrancl_listrel1_if_listrel) | |
| 5135 | ||
| 15302 | 5136 | |
| 26749 
397a1aeede7d
* New attribute "termination_simp": Simp rules for termination proofs
 krauss parents: 
26734diff
changeset | 5137 | subsection {* Size function *}
 | 
| 
397a1aeede7d
* New attribute "termination_simp": Simp rules for termination proofs
 krauss parents: 
26734diff
changeset | 5138 | |
| 26875 
e18574413bc4
Measure functions can now be declared via special rules, allowing for a
 krauss parents: 
26795diff
changeset | 5139 | 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 | 5140 | by (rule is_measure_trivial) | 
| 
e18574413bc4
Measure functions can now be declared via special rules, allowing for a
 krauss parents: 
26795diff
changeset | 5141 | |
| 
e18574413bc4
Measure functions can now be declared via special rules, allowing for a
 krauss parents: 
26795diff
changeset | 5142 | 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 | 5143 | by (rule is_measure_trivial) | 
| 
e18574413bc4
Measure functions can now be declared via special rules, allowing for a
 krauss parents: 
26795diff
changeset | 5144 | |
| 
e18574413bc4
Measure functions can now be declared via special rules, allowing for a
 krauss parents: 
26795diff
changeset | 5145 | lemma list_size_estimation[termination_simp]: | 
| 
e18574413bc4
Measure functions can now be declared via special rules, allowing for a
 krauss parents: 
26795diff
changeset | 5146 | "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 | 5147 | by (induct xs) auto | 
| 
397a1aeede7d
* New attribute "termination_simp": Simp rules for termination proofs
 krauss parents: 
26734diff
changeset | 5148 | |
| 26875 
e18574413bc4
Measure functions can now be declared via special rules, allowing for a
 krauss parents: 
26795diff
changeset | 5149 | lemma list_size_estimation'[termination_simp]: | 
| 
e18574413bc4
Measure functions can now be declared via special rules, allowing for a
 krauss parents: 
26795diff
changeset | 5150 | "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 | 5151 | by (induct xs) auto | 
| 
e18574413bc4
Measure functions can now be declared via special rules, allowing for a
 krauss parents: 
26795diff
changeset | 5152 | |
| 
e18574413bc4
Measure functions can now be declared via special rules, allowing for a
 krauss parents: 
26795diff
changeset | 5153 | 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 | 5154 | by (induct xs) auto | 
| 
e18574413bc4
Measure functions can now be declared via special rules, allowing for a
 krauss parents: 
26795diff
changeset | 5155 | |
| 44619 
fd520fa2fb09
adding list_size_append (thanks to Rene Thiemann)
 bulwahn parents: 
44618diff
changeset | 5156 | 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 | 5157 | by (induct xs, auto) | 
| 
fd520fa2fb09
adding list_size_append (thanks to Rene Thiemann)
 bulwahn parents: 
44618diff
changeset | 5158 | |
| 26875 
e18574413bc4
Measure functions can now be declared via special rules, allowing for a
 krauss parents: 
26795diff
changeset | 5159 | lemma list_size_pointwise[termination_simp]: | 
| 44618 
f3635643a376
strengthening list_size_pointwise (thanks to Rene Thiemann)
 bulwahn parents: 
44510diff
changeset | 5160 | "(\<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 | 5161 | by (induct xs) force+ | 
| 26749 
397a1aeede7d
* New attribute "termination_simp": Simp rules for termination proofs
 krauss parents: 
26734diff
changeset | 5162 | |
| 31048 
ac146fc38b51
refined HOL string theories and corresponding ML fragments
 haftmann parents: 
31022diff
changeset | 5163 | |
| 46143 | 5164 | subsection {* Monad operation *}
 | 
| 5165 | ||
| 5166 | definition bind :: "'a list \<Rightarrow> ('a \<Rightarrow> 'b list) \<Rightarrow> 'b list" where
 | |
| 5167 | "bind xs f = concat (map f xs)" | |
| 5168 | ||
| 5169 | hide_const (open) bind | |
| 5170 | ||
| 5171 | lemma bind_simps [simp]: | |
| 5172 | "List.bind [] f = []" | |
| 5173 | "List.bind (x # xs) f = f x @ List.bind xs f" | |
| 5174 | by (simp_all add: bind_def) | |
| 5175 | ||
| 5176 | ||
| 33318 
ddd97d9dfbfb
moved Nat_Transfer before Divides; distributed Nat_Transfer setup accordingly
 haftmann parents: 
32960diff
changeset | 5177 | subsection {* Transfer *}
 | 
| 
ddd97d9dfbfb
moved Nat_Transfer before Divides; distributed Nat_Transfer setup accordingly
 haftmann parents: 
32960diff
changeset | 5178 | |
| 
ddd97d9dfbfb
moved Nat_Transfer before Divides; distributed Nat_Transfer setup accordingly
 haftmann parents: 
32960diff
changeset | 5179 | definition | 
| 
ddd97d9dfbfb
moved Nat_Transfer before Divides; distributed Nat_Transfer setup accordingly
 haftmann parents: 
32960diff
changeset | 5180 | embed_list :: "nat list \<Rightarrow> int list" | 
| 
ddd97d9dfbfb
moved Nat_Transfer before Divides; distributed Nat_Transfer setup accordingly
 haftmann parents: 
32960diff
changeset | 5181 | where | 
| 
ddd97d9dfbfb
moved Nat_Transfer before Divides; distributed Nat_Transfer setup accordingly
 haftmann parents: 
32960diff
changeset | 5182 | "embed_list l = map int l" | 
| 
ddd97d9dfbfb
moved Nat_Transfer before Divides; distributed Nat_Transfer setup accordingly
 haftmann parents: 
32960diff
changeset | 5183 | |
| 
ddd97d9dfbfb
moved Nat_Transfer before Divides; distributed Nat_Transfer setup accordingly
 haftmann parents: 
32960diff
changeset | 5184 | definition | 
| 
ddd97d9dfbfb
moved Nat_Transfer before Divides; distributed Nat_Transfer setup accordingly
 haftmann parents: 
32960diff
changeset | 5185 | nat_list :: "int list \<Rightarrow> bool" | 
| 
ddd97d9dfbfb
moved Nat_Transfer before Divides; distributed Nat_Transfer setup accordingly
 haftmann parents: 
32960diff
changeset | 5186 | where | 
| 
ddd97d9dfbfb
moved Nat_Transfer before Divides; distributed Nat_Transfer setup accordingly
 haftmann parents: 
32960diff
changeset | 5187 | "nat_list l = nat_set (set l)" | 
| 
ddd97d9dfbfb
moved Nat_Transfer before Divides; distributed Nat_Transfer setup accordingly
 haftmann parents: 
32960diff
changeset | 5188 | |
| 
ddd97d9dfbfb
moved Nat_Transfer before Divides; distributed Nat_Transfer setup accordingly
 haftmann parents: 
32960diff
changeset | 5189 | definition | 
| 
ddd97d9dfbfb
moved Nat_Transfer before Divides; distributed Nat_Transfer setup accordingly
 haftmann parents: 
32960diff
changeset | 5190 | return_list :: "int list \<Rightarrow> nat list" | 
| 
ddd97d9dfbfb
moved Nat_Transfer before Divides; distributed Nat_Transfer setup accordingly
 haftmann parents: 
32960diff
changeset | 5191 | where | 
| 
ddd97d9dfbfb
moved Nat_Transfer before Divides; distributed Nat_Transfer setup accordingly
 haftmann parents: 
32960diff
changeset | 5192 | "return_list l = map nat l" | 
| 
ddd97d9dfbfb
moved Nat_Transfer before Divides; distributed Nat_Transfer setup accordingly
 haftmann parents: 
32960diff
changeset | 5193 | |
| 
ddd97d9dfbfb
moved Nat_Transfer before Divides; distributed Nat_Transfer setup accordingly
 haftmann parents: 
32960diff
changeset | 5194 | 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 | 5195 | embed_list (return_list l) = l" | 
| 
ddd97d9dfbfb
moved Nat_Transfer before Divides; distributed Nat_Transfer setup accordingly
 haftmann parents: 
32960diff
changeset | 5196 | 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 | 5197 | apply (induct l) | 
| 
ddd97d9dfbfb
moved Nat_Transfer before Divides; distributed Nat_Transfer setup accordingly
 haftmann parents: 
32960diff
changeset | 5198 | apply auto | 
| 
ddd97d9dfbfb
moved Nat_Transfer before Divides; distributed Nat_Transfer setup accordingly
 haftmann parents: 
32960diff
changeset | 5199 | done | 
| 
ddd97d9dfbfb
moved Nat_Transfer before Divides; distributed Nat_Transfer setup accordingly
 haftmann parents: 
32960diff
changeset | 5200 | |
| 
ddd97d9dfbfb
moved Nat_Transfer before Divides; distributed Nat_Transfer setup accordingly
 haftmann parents: 
32960diff
changeset | 5201 | lemma transfer_nat_int_list_functions: | 
| 
ddd97d9dfbfb
moved Nat_Transfer before Divides; distributed Nat_Transfer setup accordingly
 haftmann parents: 
32960diff
changeset | 5202 | "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 | 5203 | "[] = return_list []" | 
| 
ddd97d9dfbfb
moved Nat_Transfer before Divides; distributed Nat_Transfer setup accordingly
 haftmann parents: 
32960diff
changeset | 5204 | unfolding return_list_def embed_list_def | 
| 
ddd97d9dfbfb
moved Nat_Transfer before Divides; distributed Nat_Transfer setup accordingly
 haftmann parents: 
32960diff
changeset | 5205 | apply auto | 
| 
ddd97d9dfbfb
moved Nat_Transfer before Divides; distributed Nat_Transfer setup accordingly
 haftmann parents: 
32960diff
changeset | 5206 | apply (induct l, auto) | 
| 
ddd97d9dfbfb
moved Nat_Transfer before Divides; distributed Nat_Transfer setup accordingly
 haftmann parents: 
32960diff
changeset | 5207 | apply (induct m, auto) | 
| 
ddd97d9dfbfb
moved Nat_Transfer before Divides; distributed Nat_Transfer setup accordingly
 haftmann parents: 
32960diff
changeset | 5208 | done | 
| 
ddd97d9dfbfb
moved Nat_Transfer before Divides; distributed Nat_Transfer setup accordingly
 haftmann parents: 
32960diff
changeset | 5209 | |
| 
ddd97d9dfbfb
moved Nat_Transfer before Divides; distributed Nat_Transfer setup accordingly
 haftmann parents: 
32960diff
changeset | 5210 | (* | 
| 
ddd97d9dfbfb
moved Nat_Transfer before Divides; distributed Nat_Transfer setup accordingly
 haftmann parents: 
32960diff
changeset | 5211 | lemma transfer_nat_int_fold1: "fold f l x = | 
| 
ddd97d9dfbfb
moved Nat_Transfer before Divides; distributed Nat_Transfer setup accordingly
 haftmann parents: 
32960diff
changeset | 5212 | fold (%x. f (nat x)) (embed_list l) x"; | 
| 
ddd97d9dfbfb
moved Nat_Transfer before Divides; distributed Nat_Transfer setup accordingly
 haftmann parents: 
32960diff
changeset | 5213 | *) | 
| 
ddd97d9dfbfb
moved Nat_Transfer before Divides; distributed Nat_Transfer setup accordingly
 haftmann parents: 
32960diff
changeset | 5214 | |
| 
ddd97d9dfbfb
moved Nat_Transfer before Divides; distributed Nat_Transfer setup accordingly
 haftmann parents: 
32960diff
changeset | 5215 | |
| 37605 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 5216 | subsection {* Code generation *}
 | 
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 5217 | |
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 5218 | 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 | 5219 | |
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 5220 | definition member :: "'a list \<Rightarrow> 'a \<Rightarrow> bool" where | 
| 46149 
54ca5b2775a8
restore convenient code_abbrev declarations (particulary important if List.set is not the formal constructor for sets)
 haftmann parents: 
46147diff
changeset | 5221 | [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 | 5222 | |
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 5223 | text {*
 | 
| 46030 
51b2f3412a03
attribute code_abbrev superseedes code_unfold_post; tuned text
 haftmann parents: 
45993diff
changeset | 5224 |   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 | 5225 |   @{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 | 5226 | *} | 
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 5227 | |
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 5228 | lemma member_rec [code]: | 
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 5229 | "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 | 5230 | "member [] y \<longleftrightarrow> False" | 
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 5231 | 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 | 5232 | |
| 46030 
51b2f3412a03
attribute code_abbrev superseedes code_unfold_post; tuned text
 haftmann parents: 
45993diff
changeset | 5233 | 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 | 5234 | "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 | 5235 | by (simp add: member_def) | 
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 5236 | |
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 5237 | definition list_all :: "('a \<Rightarrow> bool) \<Rightarrow> 'a list \<Rightarrow> bool" where
 | 
| 46151 | 5238 | 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 | 5239 | |
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 5240 | definition list_ex :: "('a \<Rightarrow> bool) \<Rightarrow> 'a list \<Rightarrow> bool" where
 | 
| 46151 | 5241 | 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 | 5242 | |
| 
51b2f3412a03
attribute code_abbrev superseedes code_unfold_post; tuned text
 haftmann parents: 
45993diff
changeset | 5243 | definition list_ex1 :: "('a \<Rightarrow> bool) \<Rightarrow> 'a list \<Rightarrow> bool" where
 | 
| 
51b2f3412a03
attribute code_abbrev superseedes code_unfold_post; tuned text
 haftmann parents: 
45993diff
changeset | 5244 | list_ex1_iff [code_abbrev]: "list_ex1 P xs \<longleftrightarrow> (\<exists>! x. x \<in> set xs \<and> P x)" | 
| 40652 | 5245 | |
| 37605 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 5246 | text {*
 | 
| 46030 
51b2f3412a03
attribute code_abbrev superseedes code_unfold_post; tuned text
 haftmann parents: 
45993diff
changeset | 5247 |   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 | 5248 |   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 | 5249 |   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 | 5250 | *} | 
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 5251 | |
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 5252 | 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 | 5253 | "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 | 5254 | "list_all P [] \<longleftrightarrow> True" | 
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 5255 | 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 | 5256 | |
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 5257 | 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 | 5258 | "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 | 5259 | "list_ex P [] \<longleftrightarrow> False" | 
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 5260 | 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 | 5261 | |
| 40652 | 5262 | lemma list_ex1_simps [simp, code]: | 
| 5263 | "list_ex1 P [] = False" | |
| 5264 | "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 | 5265 | by (auto simp add: list_ex1_iff list_all_iff) | 
| 
51b2f3412a03
attribute code_abbrev superseedes code_unfold_post; tuned text
 haftmann parents: 
45993diff
changeset | 5266 | |
| 
51b2f3412a03
attribute code_abbrev superseedes code_unfold_post; tuned text
 haftmann parents: 
45993diff
changeset | 5267 | 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 | 5268 | "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 | 5269 | 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 | 5270 | |
| 46030 
51b2f3412a03
attribute code_abbrev superseedes code_unfold_post; tuned text
 haftmann parents: 
45993diff
changeset | 5271 | 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 | 5272 | "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 | 5273 | 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 | 5274 | |
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 5275 | lemma list_all_append [simp]: | 
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 5276 | "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 | 5277 | 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 | 5278 | |
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 5279 | lemma list_ex_append [simp]: | 
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 5280 | "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 | 5281 | 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 | 5282 | |
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 5283 | lemma list_all_rev [simp]: | 
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 5284 | "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 | 5285 | 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 | 5286 | |
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 5287 | lemma list_ex_rev [simp]: | 
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 5288 | "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 | 5289 | 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 | 5290 | |
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 5291 | lemma list_all_length: | 
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 5292 | "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 | 5293 | 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 | 5294 | |
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 5295 | lemma list_ex_length: | 
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 5296 | "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 | 5297 | 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 | 5298 | |
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 5299 | 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 | 5300 | "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 | 5301 | 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 | 5302 | |
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 5303 | lemma list_any_cong [fundef_cong]: | 
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 5304 | "xs = ys \<Longrightarrow> (\<And>x. x \<in> set ys \<Longrightarrow> f x = g x) \<Longrightarrow> list_ex f xs = list_ex g ys" | 
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 5305 | 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 | 5306 | |
| 46313 | 5307 | text {* Executable checks for relations on sets *}
 | 
| 5308 | ||
| 5309 | definition listrel1p :: "('a \<Rightarrow> 'a \<Rightarrow> bool) \<Rightarrow> 'a list \<Rightarrow> 'a list \<Rightarrow> bool" where
 | |
| 5310 | "listrel1p r xs ys = ((xs, ys) \<in> listrel1 {(x, y). r x y})"
 | |
| 5311 | ||
| 5312 | lemma [code_unfold]: | |
| 5313 | "(xs, ys) \<in> listrel1 r = listrel1p (\<lambda>x y. (x, y) \<in> r) xs ys" | |
| 5314 | unfolding listrel1p_def by auto | |
| 5315 | ||
| 5316 | lemma [code]: | |
| 5317 | "listrel1p r [] xs = False" | |
| 5318 | "listrel1p r xs [] = False" | |
| 5319 | "listrel1p r (x # xs) (y # ys) \<longleftrightarrow> | |
| 5320 | r x y \<and> xs = ys \<or> x = y \<and> listrel1p r xs ys" | |
| 5321 | by (simp add: listrel1p_def)+ | |
| 5322 | ||
| 5323 | definition | |
| 5324 |   lexordp :: "('a \<Rightarrow> 'a \<Rightarrow> bool) \<Rightarrow> 'a list \<Rightarrow> 'a list \<Rightarrow> bool" where
 | |
| 5325 |   "lexordp r xs ys = ((xs, ys) \<in> lexord {(x, y). r x y})"
 | |
| 5326 | ||
| 5327 | lemma [code_unfold]: | |
| 5328 | "(xs, ys) \<in> lexord r = lexordp (\<lambda>x y. (x, y) \<in> r) xs ys" | |
| 5329 | unfolding lexordp_def by auto | |
| 5330 | ||
| 5331 | lemma [code]: | |
| 5332 | "lexordp r xs [] = False" | |
| 5333 | "lexordp r [] (y#ys) = True" | |
| 5334 | "lexordp r (x # xs) (y # ys) = (r x y | (x = y & lexordp r xs ys))" | |
| 5335 | unfolding lexordp_def by auto | |
| 5336 | ||
| 37605 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 5337 | 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 | 5338 | |
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 5339 | lemma atMost_upto [code_unfold]: | 
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 5340 |   "{..n} = set [0..<Suc n]"
 | 
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 5341 | by auto | 
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 5342 | |
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 5343 | lemma atLeast_upt [code_unfold]: | 
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 5344 |   "{..<n} = set [0..<n]"
 | 
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 5345 | by auto | 
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 5346 | |
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 5347 | lemma greaterThanLessThan_upt [code_unfold]: | 
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 5348 |   "{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 | 5349 | by auto | 
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 5350 | |
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 5351 | 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 | 5352 | |
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 5353 | lemma greaterThanAtMost_upt [code_unfold]: | 
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 5354 |   "{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 | 5355 | by auto | 
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 5356 | |
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 5357 | lemma atLeastAtMost_upt [code_unfold]: | 
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 5358 |   "{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 | 5359 | by auto | 
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 5360 | |
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 5361 | 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 | 5362 |   "(\<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 | 5363 | by auto | 
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 5364 | |
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 5365 | 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 | 5366 |   "(\<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 | 5367 | by auto | 
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 5368 | |
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 5369 | 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 | 5370 |   "(\<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 | 5371 | by auto | 
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 5372 | |
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 5373 | 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 | 5374 |   "(\<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 | 5375 | by auto | 
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 5376 | |
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 5377 | 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 | 5378 | "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 | 5379 | 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 | 5380 | |
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 5381 | text {* Summation over ints. *}
 | 
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 5382 | |
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 5383 | lemma greaterThanLessThan_upto [code_unfold]: | 
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 5384 |   "{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 | 5385 | by auto | 
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 5386 | |
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 5387 | lemma atLeastLessThan_upto [code_unfold]: | 
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 5388 |   "{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 | 5389 | by auto | 
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 5390 | |
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 5391 | lemma greaterThanAtMost_upto [code_unfold]: | 
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 5392 |   "{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 | 5393 | by auto | 
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 5394 | |
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 5395 | 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 | 5396 | |
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 5397 | 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 | 5398 | "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 | 5399 | 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 | 5400 | |
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 5401 | |
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 5402 | subsubsection {* Optimizing by rewriting *}
 | 
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 5403 | |
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 5404 | definition null :: "'a list \<Rightarrow> bool" where | 
| 46030 
51b2f3412a03
attribute code_abbrev superseedes code_unfold_post; tuned text
 haftmann parents: 
45993diff
changeset | 5405 | [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 | 5406 | |
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 5407 | text {*
 | 
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 5408 |   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 | 5409 | *} | 
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 5410 | |
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 5411 | lemma null_rec [code]: | 
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 5412 | "null (x # xs) \<longleftrightarrow> False" | 
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 5413 | "null [] \<longleftrightarrow> True" | 
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 5414 | 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 | 5415 | |
| 46030 
51b2f3412a03
attribute code_abbrev superseedes code_unfold_post; tuned text
 haftmann parents: 
45993diff
changeset | 5416 | 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 | 5417 | "xs = [] \<longleftrightarrow> null xs" | 
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 5418 | by (simp add: null_def) | 
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 5419 | |
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 5420 | lemma equal_Nil_null [code_unfold]: | 
| 38857 
97775f3e8722
renamed class/constant eq to equal; tuned some instantiations
 haftmann parents: 
38715diff
changeset | 5421 | "HOL.equal xs [] \<longleftrightarrow> null xs" | 
| 
97775f3e8722
renamed class/constant eq to equal; tuned some instantiations
 haftmann parents: 
38715diff
changeset | 5422 | by (simp add: equal eq_Nil_null) | 
| 37605 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 5423 | |
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 5424 | 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 | 5425 | [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 | 5426 | |
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 5427 | 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 | 5428 | [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 | 5429 | |
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 5430 | text {*
 | 
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 5431 |   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 | 5432 | 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 | 5433 | *} | 
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 5434 | |
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 5435 | lemma maps_simps [code]: | 
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 5436 | "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 | 5437 | "maps f [] = []" | 
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 5438 | 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 | 5439 | |
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 5440 | lemma map_filter_simps [code]: | 
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 5441 | "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 | 5442 | "map_filter f [] = []" | 
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 5443 | 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 | 5444 | |
| 46030 
51b2f3412a03
attribute code_abbrev superseedes code_unfold_post; tuned text
 haftmann parents: 
45993diff
changeset | 5445 | 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 | 5446 | "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 | 5447 | by (simp add: maps_def) | 
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 5448 | |
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 5449 | 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 | 5450 | "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 | 5451 | 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 | 5452 | |
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 5453 | 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 | 5454 | and similiarly for @{text"\<exists>"}. *}
 | 
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 5455 | |
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 5456 | 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 | 5457 |   "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 | 5458 | |
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 5459 | lemma [code]: | 
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 5460 | "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 | 5461 | proof - | 
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 5462 |   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 | 5463 | proof - | 
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 5464 | fix n | 
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 5465 |     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 | 5466 | 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 | 5467 | qed | 
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 5468 | 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 | 5469 | qed | 
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 5470 | |
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 5471 | 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 | 5472 | "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 | 5473 | 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 | 5474 | |
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 5475 | 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 | 5476 | "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 | 5477 | 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 | 5478 | |
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 5479 | 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 | 5480 |   "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 | 5481 | |
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 5482 | lemma [code]: | 
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 5483 | "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 | 5484 | proof - | 
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 5485 |   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 | 5486 | proof - | 
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 5487 | fix k | 
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 5488 |     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 | 5489 | 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 | 5490 | qed | 
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 5491 | 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 | 5492 | qed | 
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 5493 | |
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 5494 | 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 | 5495 | "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 | 5496 | 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 | 5497 | |
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 5498 | 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 | 5499 | "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 | 5500 | 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 | 5501 | |
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 5502 | hide_const (open) member null maps map_filter all_interval_nat all_interval_int | 
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 5503 | |
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 5504 | |
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 5505 | subsubsection {* Pretty lists *}
 | 
| 15064 
4f3102b50197
- Moved code generator setup for lists from Main.thy to List.thy
 berghofe parents: 
15045diff
changeset | 5506 | |
| 31055 
2cf6efca6c71
proper structures for list and string code generation stuff
 haftmann parents: 
31048diff
changeset | 5507 | use "Tools/list_code.ML" | 
| 
2cf6efca6c71
proper structures for list and string code generation stuff
 haftmann parents: 
31048diff
changeset | 5508 | |
| 31048 
ac146fc38b51
refined HOL string theories and corresponding ML fragments
 haftmann parents: 
31022diff
changeset | 5509 | code_type list | 
| 
ac146fc38b51
refined HOL string theories and corresponding ML fragments
 haftmann parents: 
31022diff
changeset | 5510 | (SML "_ list") | 
| 
ac146fc38b51
refined HOL string theories and corresponding ML fragments
 haftmann parents: 
31022diff
changeset | 5511 | (OCaml "_ list") | 
| 34886 | 5512 | (Haskell "![(_)]") | 
| 5513 | (Scala "List[(_)]") | |
| 31048 
ac146fc38b51
refined HOL string theories and corresponding ML fragments
 haftmann parents: 
31022diff
changeset | 5514 | |
| 
ac146fc38b51
refined HOL string theories and corresponding ML fragments
 haftmann parents: 
31022diff
changeset | 5515 | code_const Nil | 
| 
ac146fc38b51
refined HOL string theories and corresponding ML fragments
 haftmann parents: 
31022diff
changeset | 5516 | (SML "[]") | 
| 
ac146fc38b51
refined HOL string theories and corresponding ML fragments
 haftmann parents: 
31022diff
changeset | 5517 | (OCaml "[]") | 
| 
ac146fc38b51
refined HOL string theories and corresponding ML fragments
 haftmann parents: 
31022diff
changeset | 5518 | (Haskell "[]") | 
| 37880 
3b9ca8d2c5fb
Scala: subtle difference in printing strings vs. complex mixfix syntax
 haftmann parents: 
37767diff
changeset | 5519 | (Scala "!Nil") | 
| 31048 
ac146fc38b51
refined HOL string theories and corresponding ML fragments
 haftmann parents: 
31022diff
changeset | 5520 | |
| 38857 
97775f3e8722
renamed class/constant eq to equal; tuned some instantiations
 haftmann parents: 
38715diff
changeset | 5521 | code_instance list :: equal | 
| 31048 
ac146fc38b51
refined HOL string theories and corresponding ML fragments
 haftmann parents: 
31022diff
changeset | 5522 | (Haskell -) | 
| 
ac146fc38b51
refined HOL string theories and corresponding ML fragments
 haftmann parents: 
31022diff
changeset | 5523 | |
| 38857 
97775f3e8722
renamed class/constant eq to equal; tuned some instantiations
 haftmann parents: 
38715diff
changeset | 5524 | code_const "HOL.equal \<Colon> 'a list \<Rightarrow> 'a list \<Rightarrow> bool" | 
| 39272 | 5525 | (Haskell infix 4 "==") | 
| 31048 
ac146fc38b51
refined HOL string theories and corresponding ML fragments
 haftmann parents: 
31022diff
changeset | 5526 | |
| 
ac146fc38b51
refined HOL string theories and corresponding ML fragments
 haftmann parents: 
31022diff
changeset | 5527 | code_reserved SML | 
| 
ac146fc38b51
refined HOL string theories and corresponding ML fragments
 haftmann parents: 
31022diff
changeset | 5528 | list | 
| 
ac146fc38b51
refined HOL string theories and corresponding ML fragments
 haftmann parents: 
31022diff
changeset | 5529 | |
| 
ac146fc38b51
refined HOL string theories and corresponding ML fragments
 haftmann parents: 
31022diff
changeset | 5530 | code_reserved OCaml | 
| 
ac146fc38b51
refined HOL string theories and corresponding ML fragments
 haftmann parents: 
31022diff
changeset | 5531 | list | 
| 
ac146fc38b51
refined HOL string theories and corresponding ML fragments
 haftmann parents: 
31022diff
changeset | 5532 | |
| 45181 | 5533 | setup {* fold (List_Code.add_literal_list) ["SML", "OCaml", "Haskell", "Scala"] *}
 | 
| 15064 
4f3102b50197
- Moved code generator setup for lists from Main.thy to List.thy
 berghofe parents: 
15045diff
changeset | 5534 | |
| 21061 
580dfc999ef6
added normal post setup; cleaned up "execution" constants
 haftmann parents: 
21046diff
changeset | 5535 | |
| 37424 
ed431cc99f17
use various predefined Haskell operations when generating code
 haftmann parents: 
37408diff
changeset | 5536 | subsubsection {* Use convenient predefined operations *}
 | 
| 
ed431cc99f17
use various predefined Haskell operations when generating code
 haftmann parents: 
37408diff
changeset | 5537 | |
| 
ed431cc99f17
use various predefined Haskell operations when generating code
 haftmann parents: 
37408diff
changeset | 5538 | code_const "op @" | 
| 
ed431cc99f17
use various predefined Haskell operations when generating code
 haftmann parents: 
37408diff
changeset | 5539 | (SML infixr 7 "@") | 
| 
ed431cc99f17
use various predefined Haskell operations when generating code
 haftmann parents: 
37408diff
changeset | 5540 | (OCaml infixr 6 "@") | 
| 
ed431cc99f17
use various predefined Haskell operations when generating code
 haftmann parents: 
37408diff
changeset | 5541 | (Haskell infixr 5 "++") | 
| 
ed431cc99f17
use various predefined Haskell operations when generating code
 haftmann parents: 
37408diff
changeset | 5542 | (Scala infixl 7 "++") | 
| 
ed431cc99f17
use various predefined Haskell operations when generating code
 haftmann parents: 
37408diff
changeset | 5543 | |
| 
ed431cc99f17
use various predefined Haskell operations when generating code
 haftmann parents: 
37408diff
changeset | 5544 | code_const map | 
| 
ed431cc99f17
use various predefined Haskell operations when generating code
 haftmann parents: 
37408diff
changeset | 5545 | (Haskell "map") | 
| 
ed431cc99f17
use various predefined Haskell operations when generating code
 haftmann parents: 
37408diff
changeset | 5546 | |
| 
ed431cc99f17
use various predefined Haskell operations when generating code
 haftmann parents: 
37408diff
changeset | 5547 | code_const filter | 
| 
ed431cc99f17
use various predefined Haskell operations when generating code
 haftmann parents: 
37408diff
changeset | 5548 | (Haskell "filter") | 
| 
ed431cc99f17
use various predefined Haskell operations when generating code
 haftmann parents: 
37408diff
changeset | 5549 | |
| 
ed431cc99f17
use various predefined Haskell operations when generating code
 haftmann parents: 
37408diff
changeset | 5550 | code_const concat | 
| 
ed431cc99f17
use various predefined Haskell operations when generating code
 haftmann parents: 
37408diff
changeset | 5551 | (Haskell "concat") | 
| 
ed431cc99f17
use various predefined Haskell operations when generating code
 haftmann parents: 
37408diff
changeset | 5552 | |
| 37605 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 5553 | code_const List.maps | 
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 5554 | (Haskell "concatMap") | 
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 5555 | |
| 37424 
ed431cc99f17
use various predefined Haskell operations when generating code
 haftmann parents: 
37408diff
changeset | 5556 | code_const rev | 
| 37451 | 5557 | (Haskell "reverse") | 
| 37424 
ed431cc99f17
use various predefined Haskell operations when generating code
 haftmann parents: 
37408diff
changeset | 5558 | |
| 
ed431cc99f17
use various predefined Haskell operations when generating code
 haftmann parents: 
37408diff
changeset | 5559 | code_const zip | 
| 
ed431cc99f17
use various predefined Haskell operations when generating code
 haftmann parents: 
37408diff
changeset | 5560 | (Haskell "zip") | 
| 
ed431cc99f17
use various predefined Haskell operations when generating code
 haftmann parents: 
37408diff
changeset | 5561 | |
| 37605 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 5562 | code_const List.null | 
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 5563 | (Haskell "null") | 
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 5564 | |
| 37424 
ed431cc99f17
use various predefined Haskell operations when generating code
 haftmann parents: 
37408diff
changeset | 5565 | code_const takeWhile | 
| 
ed431cc99f17
use various predefined Haskell operations when generating code
 haftmann parents: 
37408diff
changeset | 5566 | (Haskell "takeWhile") | 
| 
ed431cc99f17
use various predefined Haskell operations when generating code
 haftmann parents: 
37408diff
changeset | 5567 | |
| 
ed431cc99f17
use various predefined Haskell operations when generating code
 haftmann parents: 
37408diff
changeset | 5568 | code_const dropWhile | 
| 
ed431cc99f17
use various predefined Haskell operations when generating code
 haftmann parents: 
37408diff
changeset | 5569 | (Haskell "dropWhile") | 
| 
ed431cc99f17
use various predefined Haskell operations when generating code
 haftmann parents: 
37408diff
changeset | 5570 | |
| 37605 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 5571 | code_const list_all | 
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 5572 | (Haskell "all") | 
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 5573 | |
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 5574 | code_const list_ex | 
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 5575 | (Haskell "any") | 
| 
625bc011768a
put section on distinctness before listsum; refined code generation operations; dropped ancient infix mem
 haftmann parents: 
37465diff
changeset | 5576 | |
| 46147 
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
 haftmann parents: 
46143diff
changeset | 5577 | |
| 
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
 haftmann parents: 
46143diff
changeset | 5578 | subsubsection {* Implementation of sets by lists *}
 | 
| 
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
 haftmann parents: 
46143diff
changeset | 5579 | |
| 
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
 haftmann parents: 
46143diff
changeset | 5580 | text {* Basic operations *}
 | 
| 
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
 haftmann parents: 
46143diff
changeset | 5581 | |
| 
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
 haftmann parents: 
46143diff
changeset | 5582 | lemma is_empty_set [code]: | 
| 
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
 haftmann parents: 
46143diff
changeset | 5583 | "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 | 5584 | 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 | 5585 | |
| 
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
 haftmann parents: 
46143diff
changeset | 5586 | lemma empty_set [code]: | 
| 
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
 haftmann parents: 
46143diff
changeset | 5587 |   "{} = set []"
 | 
| 
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
 haftmann parents: 
46143diff
changeset | 5588 | by simp | 
| 
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
 haftmann parents: 
46143diff
changeset | 5589 | |
| 46156 | 5590 | lemma UNIV_coset [code]: | 
| 5591 | "UNIV = List.coset []" | |
| 5592 | by simp | |
| 5593 | ||
| 5594 | lemma compl_set [code]: | |
| 5595 | "- set xs = List.coset xs" | |
| 5596 | by simp | |
| 5597 | ||
| 5598 | lemma compl_coset [code]: | |
| 5599 | "- List.coset xs = set xs" | |
| 5600 | by simp | |
| 5601 | ||
| 46147 
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
 haftmann parents: 
46143diff
changeset | 5602 | lemma [code]: | 
| 
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
 haftmann parents: 
46143diff
changeset | 5603 | "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 | 5604 | "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 | 5605 | by (simp_all add: member_def) | 
| 
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
 haftmann parents: 
46143diff
changeset | 5606 | |
| 
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
 haftmann parents: 
46143diff
changeset | 5607 | lemma insert_code [code]: | 
| 
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
 haftmann parents: 
46143diff
changeset | 5608 | "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 | 5609 | "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 | 5610 | by simp_all | 
| 
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
 haftmann parents: 
46143diff
changeset | 5611 | |
| 
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
 haftmann parents: 
46143diff
changeset | 5612 | lemma remove_code [code]: | 
| 
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
 haftmann parents: 
46143diff
changeset | 5613 | "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 | 5614 | "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 | 5615 | by (simp_all add: remove_def Compl_insert) | 
| 
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
 haftmann parents: 
46143diff
changeset | 5616 | |
| 46156 | 5617 | lemma project_set [code]: | 
| 5618 | "Set.project P (set xs) = set (filter P xs)" | |
| 5619 | by auto | |
| 5620 | ||
| 5621 | lemma image_set [code]: | |
| 5622 | "image f (set xs) = set (map f xs)" | |
| 5623 | by simp | |
| 5624 | ||
| 46147 
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
 haftmann parents: 
46143diff
changeset | 5625 | lemma Ball_set [code]: | 
| 
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
 haftmann parents: 
46143diff
changeset | 5626 | "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 | 5627 | by (simp add: list_all_iff) | 
| 
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
 haftmann parents: 
46143diff
changeset | 5628 | |
| 
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
 haftmann parents: 
46143diff
changeset | 5629 | lemma Bex_set [code]: | 
| 
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
 haftmann parents: 
46143diff
changeset | 5630 | "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 | 5631 | by (simp add: list_ex_iff) | 
| 
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
 haftmann parents: 
46143diff
changeset | 5632 | |
| 
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
 haftmann parents: 
46143diff
changeset | 5633 | lemma card_set [code]: | 
| 
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
 haftmann parents: 
46143diff
changeset | 5634 | "card (set xs) = length (remdups xs)" | 
| 
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
 haftmann parents: 
46143diff
changeset | 5635 | proof - | 
| 
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
 haftmann parents: 
46143diff
changeset | 5636 | have "card (set (remdups xs)) = length (remdups xs)" | 
| 
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
 haftmann parents: 
46143diff
changeset | 5637 | by (rule distinct_card) simp | 
| 
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
 haftmann parents: 
46143diff
changeset | 5638 | then show ?thesis by simp | 
| 
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
 haftmann parents: 
46143diff
changeset | 5639 | qed | 
| 
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
 haftmann parents: 
46143diff
changeset | 5640 | |
| 46156 | 5641 | lemma the_elem_set [code]: | 
| 5642 | "the_elem (set [x]) = x" | |
| 5643 | by simp | |
| 5644 | ||
| 5645 | lemma Pow_set [code]: | |
| 5646 |   "Pow (set []) = {{}}"
 | |
| 5647 | "Pow (set (x # xs)) = (let A = Pow (set xs) in A \<union> insert x ` A)" | |
| 5648 | by (simp_all add: Pow_insert Let_def) | |
| 5649 | ||
| 46383 | 5650 | text {* Further operations on sets *}
 | 
| 5651 | ||
| 46396 
da32cf32c0c7
adding a minimally refined equality on sets for code generation
 bulwahn parents: 
46383diff
changeset | 5652 | (* Minimal refinement of equality on sets *) | 
| 46418 
22bb415d7754
another try to improve code generation of set equality (cf. da32cf32c0c7)
 bulwahn parents: 
46396diff
changeset | 5653 | declare subset_eq[code del] | 
| 
22bb415d7754
another try to improve code generation of set equality (cf. da32cf32c0c7)
 bulwahn parents: 
46396diff
changeset | 5654 | lemma subset_code [code]: | 
| 
22bb415d7754
another try to improve code generation of set equality (cf. da32cf32c0c7)
 bulwahn parents: 
46396diff
changeset | 5655 | "set xs <= B \<longleftrightarrow> (ALL x : set xs. x : B)" | 
| 
22bb415d7754
another try to improve code generation of set equality (cf. da32cf32c0c7)
 bulwahn parents: 
46396diff
changeset | 5656 | "List.coset xs <= List.coset ys \<longleftrightarrow> set ys <= set xs" | 
| 
22bb415d7754
another try to improve code generation of set equality (cf. da32cf32c0c7)
 bulwahn parents: 
46396diff
changeset | 5657 | "List.coset [] <= set [] \<longleftrightarrow> False" | 
| 
22bb415d7754
another try to improve code generation of set equality (cf. da32cf32c0c7)
 bulwahn parents: 
46396diff
changeset | 5658 | by auto | 
| 46396 
da32cf32c0c7
adding a minimally refined equality on sets for code generation
 bulwahn parents: 
46383diff
changeset | 5659 | |
| 46383 | 5660 | lemma setsum_code [code]: | 
| 5661 | "setsum f (set xs) = listsum (map f (remdups xs))" | |
| 5662 | 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 | 5663 | |
| 46424 
b447318e5e1a
adding code equation for Relation.image; adding map_project as correspondence to map_filter on lists
 bulwahn parents: 
46418diff
changeset | 5664 | 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 | 5665 |   "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 | 5666 | |
| 
b447318e5e1a
adding code equation for Relation.image; adding map_project as correspondence to map_filter on lists
 bulwahn parents: 
46418diff
changeset | 5667 | lemma [code]: | 
| 
b447318e5e1a
adding code equation for Relation.image; adding map_project as correspondence to map_filter on lists
 bulwahn parents: 
46418diff
changeset | 5668 | "map_project f (set xs) = set (List.map_filter f xs)" | 
| 
b447318e5e1a
adding code equation for Relation.image; adding map_project as correspondence to map_filter on lists
 bulwahn parents: 
46418diff
changeset | 5669 | unfolding map_project_def map_filter_def | 
| 
b447318e5e1a
adding code equation for Relation.image; adding map_project as correspondence to map_filter on lists
 bulwahn parents: 
46418diff
changeset | 5670 | by auto (metis (lifting, mono_tags) CollectI image_eqI o_apply the.simps) | 
| 
b447318e5e1a
adding code equation for Relation.image; adding map_project as correspondence to map_filter on lists
 bulwahn parents: 
46418diff
changeset | 5671 | |
| 
b447318e5e1a
adding code equation for Relation.image; adding map_project as correspondence to map_filter on lists
 bulwahn parents: 
46418diff
changeset | 5672 | 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 | 5673 | |
| 46147 
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
 haftmann parents: 
46143diff
changeset | 5674 | text {* Operations on relations *}
 | 
| 
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
 haftmann parents: 
46143diff
changeset | 5675 | |
| 
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
 haftmann parents: 
46143diff
changeset | 5676 | lemma product_code [code]: | 
| 
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
 haftmann parents: 
46143diff
changeset | 5677 | "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 | 5678 | by (auto simp add: Product_Type.product_def) | 
| 
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
 haftmann parents: 
46143diff
changeset | 5679 | |
| 
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
 haftmann parents: 
46143diff
changeset | 5680 | lemma Id_on_set [code]: | 
| 
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
 haftmann parents: 
46143diff
changeset | 5681 | "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 | 5682 | by (auto simp add: Id_on_def) | 
| 
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
 haftmann parents: 
46143diff
changeset | 5683 | |
| 46424 
b447318e5e1a
adding code equation for Relation.image; adding map_project as correspondence to map_filter on lists
 bulwahn parents: 
46418diff
changeset | 5684 | lemma [code]: | 
| 
b447318e5e1a
adding code equation for Relation.image; adding map_project as correspondence to map_filter on lists
 bulwahn parents: 
46418diff
changeset | 5685 | "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 | 5686 | 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 | 5687 | |
| 46147 
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
 haftmann parents: 
46143diff
changeset | 5688 | lemma trancl_set_ntrancl [code]: | 
| 
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
 haftmann parents: 
46143diff
changeset | 5689 | "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 | 5690 | by (simp add: finite_trancl_ntranl) | 
| 
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
 haftmann parents: 
46143diff
changeset | 5691 | |
| 
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
 haftmann parents: 
46143diff
changeset | 5692 | lemma set_rel_comp [code]: | 
| 
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
 haftmann parents: 
46143diff
changeset | 5693 | "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 | 5694 | by (auto simp add: Bex_def) | 
| 
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
 haftmann parents: 
46143diff
changeset | 5695 | |
| 
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
 haftmann parents: 
46143diff
changeset | 5696 | lemma wf_set [code]: | 
| 
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
 haftmann parents: 
46143diff
changeset | 5697 | "wf (set xs) = acyclic (set xs)" | 
| 
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
 haftmann parents: 
46143diff
changeset | 5698 | by (simp add: wf_iff_acyclic_if_finite) | 
| 
2c4d8de91c4c
moved lemmas about List.set and set operations to List theory
 haftmann parents: 
46143diff
changeset | 5699 | |
| 23388 | 5700 | end |