datatype (set: 'a) list =
Nil (‹[]›)
| Cons (hd: 'a) (tl: "'a list") (infixr‹#›65) for
map: map
rel: list_all2
pred: list_all where "tl [] = []"
bundle list_syntax begin notation Nil (‹[]›) and Cons (infixr‹#›65) end
datatype_compat list
lemma [case_names Nil Cons, cases type: list]:
― ‹for backward compatibility -- names of variables differ› "(y = [] ==> P) ==> (∧a list. y = a # list ==> P) ==> P" by (rule list.exhaust)
lemma [case_names Nil Cons, induct type: list]:
― ‹for backward compatibility -- names of variables differ› "P [] ==> (∧a list. P list ==> P (a # list)) ==> P list" by (rule list.induct)
primrec filter:: "('a ==> bool) ==> 'a list ==> 'a list"where "filter P [] = []" | "filter P (x # xs) = (if P x then x # filter P xs else filter P xs)"
open_bundle filter_syntax ― ‹Special input syntax for filter› begin
primrec fold :: "('a ==> 'b ==> 'b) ==> 'a list ==> 'b ==> 'b"where
fold_Nil: "fold f [] = id" |
fold_Cons: "fold f (x # xs) = fold f xs ∘ f x"
primrec foldr :: "('a ==> 'b ==> 'b) ==> 'a list ==> 'b ==> 'b"where
foldr_Nil: "foldr f [] = id" |
foldr_Cons: "foldr f (x # xs) = f x ∘ foldr f xs"
primrec foldl :: "('b ==> 'a ==> 'b) ==> 'b ==> 'a list ==> 'b"where
foldl_Nil: "foldl f a [] = a" |
foldl_Cons: "foldl f a (x # xs) = foldl f (f a x) xs"
primrec concat:: "'a list list ==> 'a list"where "concat [] = []" | "concat (x # xs) = x @ concat xs"
primrec drop:: "nat ==> 'a list ==> 'a list"where
drop_Nil: "drop n [] = []" |
drop_Cons: "drop n (x # xs) = (case n of 0 ==> x # xs | Suc m ==> drop m xs)"
― ‹Warning: simpset does not contain this definition, but separate
theorems for ‹n = 0› and ‹n = Suc k››
primrec take:: "nat ==> 'a list ==> 'a list"where
take_Nil:"take n [] = []" |
take_Cons: "take n (x # xs) = (case n of 0 ==> [] | Suc m ==> x # take m xs)"
― ‹Warning: simpset does not contain this definition, but separate
theorems for ‹n = 0› and ‹n = Suc k››
primrec (nonexhaustive) nth :: "'a list => nat => 'a" (infixl‹!›100) where
nth_Cons: "(x # xs) ! n = (case n of 0 ==> x | Suc k ==> xs ! k)"
― ‹Warning: simpset does not contain this definition, but separate
theorems for ‹n = 0› and ‹n = Suc k››
primrec list_update :: "'a list ==> nat ==> 'a ==> 'a list"where "list_update [] i v = []" | "list_update (x # xs) i v = (case i of 0 ==> v # xs | Suc j ==> x # list_update xs j v)"
primrec takeWhile :: "('a ==> bool) ==> 'a list ==> 'a list"where "takeWhile P [] = []" | "takeWhile P (x # xs) = (if P x then x # takeWhile P xs else [])"
primrec dropWhile :: "('a ==> bool) ==> 'a list ==> 'a list"where "dropWhile P [] = []" | "dropWhile P (x # xs) = (if P x then dropWhile P xs else x # xs)"
primrec zip :: "'a list ==> 'b list ==> ('a × 'b) list"where "zip xs [] = []" |
zip_Cons: "zip xs (y # ys) = (case xs of [] ==> [] | z # zs ==> (z, y) # zip zs ys)"
― ‹Warning: simpset does not contain this definition, but separate
theorems for ‹xs = []› and ‹xs = z # zs››
abbreviation map2 :: "('a ==> 'b ==> 'c) ==> 'a list ==> 'b list ==> 'c list"where "map2 f xs ys ≡ map (λ(x,y). f x y) (zip xs ys)"
primrec product_lists :: "'a list list ==> 'a list list"where "product_lists [] = [[]]" | "product_lists (xs # xss) = concat (map (λx. map (Cons x) (product_lists xss)) xs)"
primrec upt :: "nat ==> nat ==> nat list" (‹(‹indent=1 notation=‹mixfix list interval››[_..</_'])›) where
upt_0: "[i..<0] = []" |
upt_Suc: "[i..<(Suc j)] = (if i ≤ j then [i..<j] @ [j] else [])"
definition insert :: "'a ==> 'a list ==> 'a list"where "insert x xs = (if x ∈ set xs then xs else x # xs)"
definition union :: "'a list ==> 'a list ==> 'a list"where "union = fold insert"
hide_const (open) insert union
hide_fact (open) insert_def union_def
primrec find :: "('a ==> bool) ==> 'a list ==> 'a option"where "find _ [] = None" | "find P (x#xs) = (if P x then Some x else find P xs)"
text‹In the context of multisets, ‹count_list› is equivalent to term‹count ∘ mset› and it is advisable to use the latter.› primrec count_list :: "'a list ==> 'a ==> nat"where "count_list [] y = 0" | "count_list (x#xs) y = (if x=y then count_list xs y + 1 else count_list xs y)"
definition "extract" :: "('a ==> bool) ==> 'a list ==> ('a list * 'a * 'a list) option" where"extract P xs = (case dropWhile (Not ∘ P) xs of [] ==> None | y#ys ==> Some(takeWhile (Not ∘ P) xs, y, ys))"
hide_const (open) "extract"
primrec those :: "'a option list ==> 'a list option" where "those [] = Some []" | "those (x # xs) = (case x of None ==> None | Some y ==> map_option (Cons y) (those xs))"
primrec remove1 :: "'a ==> 'a list ==> 'a list"where "remove1 x [] = []" | "remove1 x (y # xs) = (if x = y then xs else y # remove1 x xs)"
primrec removeAll :: "'a ==> 'a list ==> 'a list"where "removeAll x [] = []" | "removeAll x (y # xs) = (if x = y then removeAll x xs else y # removeAll x xs)"
definition minus_list_mset :: "'a list ==> 'a list ==> 'a list"where "minus_list_mset xs ys = foldr remove1 ys xs"
definition minus_list_set :: "'a list ==> 'a list ==> 'a list"where "minus_list_set xs ys = foldr removeAll ys xs"
definition inter_list_set :: "'a list ==> 'a list ==> 'a list"where "inter_list_set xs ys = filter (λx. x ∈ set ys) xs"
primrec distinct :: "'a list ==> bool"where "distinct [] ⟷ True" | "distinct (x # xs) ⟷ x ∉ set xs ∧ distinct xs"
fun successively :: "('a ==> 'a ==> bool) ==> 'a list ==> bool"where "successively P [] = True" | "successively P [x] = True" | "successively P (x # y # xs) = (P x y ∧ successively P (y#xs))"
definition distinct_adj where "distinct_adj = successively (≠)"
primrec remdups :: "'a list ==> 'a list"where "remdups [] = []" | "remdups (x # xs) = (if x ∈ set xs then remdups xs else x # remdups xs)"
fun remdups_adj :: "'a list ==> 'a list"where "remdups_adj [] = []" | "remdups_adj [x] = [x]" | "remdups_adj (x # y # xs) = (if x = y then remdups_adj (x # xs) else x # remdups_adj (y # xs))"
primrec replicate :: "nat ==> 'a ==> 'a list"where
replicate_0: "replicate 0 x = []" |
replicate_Suc: "replicate (Suc n) x = x # replicate n x"
text‹
Function ‹size› is overloaded for all datatypes. Users may
refer to the list version as ‹length›.›
abbreviation length :: "'a list ==> nat"where "length ≡ size"
definition enumerate :: "nat ==> 'a list ==> (nat × 'a) list"where
enumerate_eq_zip: "enumerate n xs = zip [n..<n + length xs] xs"
definition rotate :: "nat ==> 'a list ==> 'a list"where "rotate n = rotate1 ^^ n"
definition nths :: "'a list => nat set => 'a list"where "nths xs A = map fst (filter (λp. snd p ∈ A) (zip xs [0..<size xs]))"
primrec subseqs :: "'a list ==> 'a list list"where "subseqs [] = [[]]" | "subseqs (x#xs) = (let xss = subseqs xs in map (Cons x) xss @ xss)"
primrec n_lists :: "nat ==> 'a list ==> 'a list list"where "n_lists 0 xs = [[]]" | "n_lists (Suc n) xs = concat (map (λys. map (λy. y # ys) xs) (n_lists n xs))"
hide_const (open) n_lists
function splice :: "'a list ==> 'a list ==> 'a list"where "splice [] ys = ys" | "splice (x#xs) ys = x # splice ys xs" by pat_completeness auto
termination by(relation "measure(λ(xs,ys). size xs + size ys)") auto
function shuffles where "shuffles [] ys = {ys}"
| "shuffles xs [] = {xs}"
| "shuffles (x # xs) (y # ys) = (#) x ` shuffles xs (y # ys) ∪ (#) y ` shuffles (x # xs) ys" by pat_completeness simp_all terminationby lexicographic_order
text‹Use only if you cannot use const‹Min› instead:› fun min_list :: "'a::ord list ==> 'a"where "min_list (x # xs) = (case xs of [] ==> x | _ ==> min x (min_list xs))"
text‹Returns first minimum:› fun arg_min_list :: "('a ==> ('b::linorder)) ==> 'a list ==> 'a"where "arg_min_list f [x] = x" | "arg_min_list f (x#y#zs) = (let m = arg_min_list f (y#zs) in if f x ≤ f m then x else m)"
text‹
begin{figure}[htbp]
fbox{
begin{tabular}{l}
{lemma "[a,b]@[c,d] = [a,b,c,d]" by simp}\\
{lemma "length [a,b,c] = 3" by simp}\\
{lemma "set [a,b,c] = {a,b,c}" by simp}\\
{lemma "map f [a,b,c] = [f a, f b, f c]" by simp}\\
{lemma "rev [a,b,c] = [c,b,a]" by simp}\\
{lemma "hd [a,b,c,d] = a" by simp}\\
{lemma "tl [a,b,c,d] = [b,c,d]" by simp}\\
{lemma "last [a,b,c,d] = d" by simp}\\
{lemma "butlast [a,b,c,d] = [a,b,c]" by simp}\\
{lemma[source] "filter (λn::nat. n<2) [0,2,1] = [0,1]" by simp}\\
{lemma "concat [[a,b],[c,d,e],[],[f]] = [a,b,c,d,e,f]" by simp}\\
{lemma "fold f [a,b,c] x = f c (f b (f a x))" by simp}\\
{lemma "foldr f [a,b,c] x = f a (f b (f c x))" by simp}\\
{lemma "foldl f x [a,b,c] = f (f (f x a) b) c" by simp}\\
{lemma "successively (≠) [True,False,True,False]" by simp}\\
{lemma "zip [a,b,c] [x,y,z] = [(a,x),(b,y),(c,z)]" by simp}\\
{lemma "zip [a,b] [x,y,z] = [(a,x),(b,y)]" by simp}\\
{lemma "enumerate 3 [a,b,c] = [(3,a),(4,b),(5,c)]" by normalization}\\
{lemma "List.product [a,b] [c,d] = [(a, c), (a, d), (b, c), (b, d)]" by simp}\\
{lemma "product_lists [[a,b], [c], [d,e]] = [[a,c,d], [a,c,e], [b,c,d], [b,c,e]]" by simp}\\
{lemma "splice [a,b,c] [x,y,z] = [a,x,b,y,c,z]" by simp}\\
{lemma "splice [a,b,c,d] [x,y] = [a,x,b,y,c,d]" by simp}\\
{lemma "shuffles [a,b] [c,d] = {[a,b,c,d],[a,c,b,d],[a,c,d,b],[c,a,b,d],[c,a,d,b],[c,d,a,b]}"
by (simp add: insert_commute)}\\
{lemma "take 2 [a,b,c,d] = [a,b]" by simp}\\
{lemma "take 6 [a,b,c,d] = [a,b,c,d]" by simp}\\
{lemma "drop 2 [a,b,c,d] = [c,d]" by simp}\\
{lemma "drop 6 [a,b,c,d] = []" by simp}\\
{lemma "takeWhile (%n::nat. n<3) [1,2,3,0] = [1,2]" by simp}\\
{lemma "dropWhile (%n::nat. n<3) [1,2,3,0] = [3,0]" by simp}\\
{lemma "distinct [2,0,1::nat]" by simp}\\
{lemma "remdups [2,0,2,1::nat,2] = [0,1,2]" by simp}\\
{lemma "remdups_adj [2,2,3,1,1::nat,2,1] = [2,3,1,2,1]" by simp}\\
{lemma "List.insert 2 [0::nat,1,2] = [0,1,2]" by (simp add: List.insert_def)}\\
{lemma "List.insert 3 [0::nat,1,2] = [3,0,1,2]" by (simp add: List.insert_def)}\\
{lemma "List.union [2,3,4] [0::int,1,2] = [4,3,0,1,2]" by (simp add: List.insert_def List.union_def)}\\
{lemma "List.find (%i::int. i>0) [0,0] = None" by simp}\\
{lemma "List.find (%i::int. i>0) [0,1,0,2] = Some 1" by simp}\\
{lemma "count_list [0,1,0,2::int] 0 = 2" by (simp)}\\
{lemma "List.extract (%i::int. i>0) [0,0] = None" by(simp add: extract_def)}\\
{lemma "List.extract (%i::int. i>0) [0,1,0,2] = Some([0], 1, [0,2])" by(simp add: extract_def)}\\
{lemma "remove1 2 [2,0,2,1::nat,2] = [0,2,1,2]" by simp}\\
{lemma "removeAll 2 [2,0,2,1::nat,2] = [0,1]" by simp}\\
{lemma "nth [a,b,c,d] 2 = c" by simp}\\
{lemma "[a,b,c,d][2 := x] = [a,b,x,d]" by simp}\\
{lemma "nths [a,b,c,d,e] {0,2,3} = [a,c,d]" by (simp add:nths_def)}\\
{lemma "subseqs [a,b] = [[a, b], [a], [b], []]" by simp}\\
{lemma "List.n_lists 2 [a,b,c] = [[a, a], [b, a], [c, a], [a, b], [b, b], [c, b], [a, c], [b, c], [c, c]]" by (simp add: eval_nat_numeral)}\\
{lemma "rotate1 [a,b,c,d] = [b,c,d,a]" by simp}\\
{lemma "rotate 3 [a,b,c,d] = [d,a,b,c]" by (simp add:rotate_def eval_nat_numeral)}\\
{lemma "replicate 4 a = [a,a,a,a]" by (simp add:eval_nat_numeral)}\\
{lemma "[2..<5] = [2,3,4]" by (simp add:eval_nat_numeral)}\\
{lemma "min_list [3,1,-2::int] = -2" by (simp)}\\
{lemma "arg_min_list (λi. i*i) [3,-1,1,-2::int] = -1" by (simp)}
end{tabular}}
caption{Characteristic examples}
label{fig:Characteristic}
end{figure}
~\ref{fig:Characteristic} shows characteristic examples
should give an intuitive understanding of the above functions. ›
text‹The following simple sort(ed) functions are intended for proofs,
for efficient implementations.›
text‹A sorted predicate w.r.t. a relation:›
fun sorted_wrt :: "('a ==> 'a ==> bool) ==> 'a list ==> bool"where "sorted_wrt P [] = True" | "sorted_wrt P (x # ys) = ((∀y ∈ set ys. P x y) ∧ sorted_wrt P ys)"
lemma sorted_simps: "sorted [] = True""sorted (x # ys) = ((∀y ∈ set ys. x≤y) ∧ sorted ys)" by auto
lemma strict_sorted_simps: "sorted_wrt (<) [] = True""sorted_wrt (<) (x # ys) = ((∀y∈ set ys. x<y) ∧ sorted_wrt (<) ys)" by auto
primrec insort_key :: "('b ==> 'a) ==> 'b ==> 'b list ==> 'b list"where "insort_key f x [] = [x]" | "insort_key f x (y#ys) = (if f x ≤ f y then (x#y#ys) else y#(insort_key f x ys))"
definition insort_insert_key :: "('b ==> 'a) ==> 'b ==> 'b list ==> 'b list"where "insort_insert_key f x xs = (if f x ∈ f ` set xs then xs else insort_key f x xs)"
definition stable_sort_key :: "(('b ==> 'a) ==> 'b list ==> 'b list) ==> bool"where "stable_sort_key sk = (∀f xs k. filter (λy. f y = k) (sk f xs) = filter (λy. f y = k) xs)"
lemma strict_sorted_iff: "sorted_wrt (<) l ⟷ sorted l ∧ distinct l" by (induction l) (auto iff: antisym_conv1)
text‹Input syntax for Haskell-like list comprehension notation.
example: ‹[(x,y). x ← xs, y ← ys, x ≠ y]›,
list of all pairs of distinct elements from ‹xs› and ‹ys›.
syntax is as in Haskell, except that ‹|› becomes a dot
like in Isabelle's set comprehension): ‹[e. x ← xs, …]› rather than
verb![e| x <- xs, ...]!.
qualifiers after the dot are
begin{description}
item[generators] ‹p ← xs›,
where ‹p› is a pattern and ‹xs› an expression of list type, or
item[guards] ‹b›, where ‹b› is a boolean expression. \item[local bindings] @ {text"let x = e"}.
end{description}
like in Haskell, list comprehension is just a shorthand. To avoid
, the translation into desugared form is not reversed
output. Note that the translation of ‹[e. x ← xs]› is
to term‹map (%x. e) xs›.
is easy to write short list comprehensions which stand for complex
. During proofs, they may become unreadable (and
). In such cases it can be advisable to introduce separate
for the list comprehensions in question.›
syntax (ASCII) "_lc_gen" :: "'a ==> 'a list ==> lc_qual" (‹_ <- _›) end
parse_translation‹
val NilC = Syntax.const 🍋‹Nil›;
val ConsC = Syntax.const 🍋‹Cons›;
val mapC = Syntax.const 🍋‹map›;
val concatC = Syntax.const 🍋‹concat›;
val IfC = Syntax.const 🍋‹If›;
val dummyC = Syntax.const 🍋‹Pure.dummy_pattern›
fun single x = ConsC $ x $ NilC;
fun pat_tr ctxt p e opti = (* %x. case x of p => e | _ => [] *) let (* FIXME proper name context!? *)
val x =
Free (singleton (Name.variant_list (fold Term.add_free_names [p, e] [])) "x", dummyT);
val e = if opti then single e else e;
val case1 = Syntax.const 🍋‹_case1› $ p $ e;
val case2 = Syntax.const 🍋‹_case1› $ dummyC $ NilC;
val cs = Syntax.const 🍋‹_case2› $ case1 $ case2; in Syntax_Trans.abs_tr [x, Case_Translation.case_tr false ctxt [x, cs]] end;
fun pair_pat_tr (x as Free _) e = Syntax_Trans.abs_tr [x, e]
| pair_pat_tr (_ $ p1 $ p2) e = Syntax.const 🍋‹case_prod› $ pair_pat_tr p1 (pair_pat_tr p2 e)
| pair_pat_tr dummy e = Syntax_Trans.abs_tr [Syntax.const "_idtdummy", e]
fun pair_pat ctxt (Const (🍋‹Pair›,_) $ s $ t) =
pair_pat ctxt s andalso pair_pat ctxt t
| pair_pat ctxt (Free (s,_)) = let
val thy = Proof_Context.theory_of ctxt;
val s' = Proof_Context.intern_const ctxt s; in not (Sign.declared_const thy s') end
| pair_pat _ t = (t = dummyC);
fun abs_tr ctxt p e opti = let val p = Term_Position.strip_positions p inif pair_pat ctxt p then (pair_pat_tr p e, true)
else (pat_tr ctxt p e opti, false) end
fun lc_tr ctxt [e, Const (🍋‹_lc_test›, _) $ b, qs] = let
val res =
(case qs of
Const (🍋‹_lc_end›, _) => single e
| Const (🍋‹_lc_quals›, _) $ q $ qs => lc_tr ctxt [e, q, qs]); in IfC $ b $ res $ NilC end
| lc_tr ctxt
[e, Const (🍋‹_lc_gen›, _) $ p $ es,
Const(🍋‹_lc_end›, _)] =
(case abs_tr ctxt p e true of
(f, true) => mapC $ f $ es
| (f, false) => concatC $ (mapC $ f $ es))
| lc_tr ctxt
[e, Const (🍋‹_lc_gen›, _) $ p $ es,
Const (🍋‹_lc_quals›, _) $ q $ qs] = let val e' = lc_tr ctxt [e, q, qs]; in concatC $ (mapC $ (fst (abs_tr ctxt p e' false)) $ es) end;
fun right_hand_set_comprehension_conv conv ctxt =
HOLogic.Trueprop_conv (HOLogic.eq_conv Conv.all_conv
(Collect_conv (all_exists_conv conv o #2) ctxt))
(* term abstraction of list comprehension patterns *)
datatype termlets = If | Case of typ * int
local
val set_Nil_I = @{lemma"set [] = {x. False}"by (simp add: empty_def [symmetric])}
val set_singleton = @{lemma"set [a] = {x. x = a}"by simp}
val inst_Collect_mem_eq = @{lemma"set A = {x. x ∈ set A}"by simp}
val del_refl_eq = @{lemma"(t = t ∧ P) ≡ P"by simp}
fun mk_set T = Const (🍋‹set›, HOLogic.listT T --> HOLogic.mk_setT T) fun dest_set (Const (🍋‹set›, _) $ xs) = xs
fun dest_singleton_list (Const (🍋‹Cons›, _) $ t $ (Const (🍋‹Nil›, _))) = t
| dest_singleton_list t = raise TERM ("dest_singleton_list", [t])
(*We check that one case returns a singleton list and all other cases
return [], and return the index of the one singleton list case.*) fun possible_index_of_singleton_case cases = let fun check (i, case_t) s =
(case strip_abs_body case_t of
(Const (🍋‹Nil›, _)) => s
| _ => (case s of SOME NONE => SOME (SOME i) | _ => NONE)) in
fold_index check cases (SOME NONE) |> the_default NONE end
(*returns condition continuing term option*) fun dest_if (Const (🍋‹If›, _) $ cond $ then_t $ Const (🍋‹Nil›, _)) =
SOME (cond, then_t)
| dest_if _ = NONE
(*returns (case_expr type index chosen_case constr_name) option*) fun dest_case ctxt case_term = let
val (case_const, args) = strip_comb case_term in
(case try dest_Const case_const of
SOME (c, T) =>
(case Ctr_Sugar.ctr_sugar_of_case ctxt c of
SOME {ctrs, ...} =>
(case possible_index_of_singleton_case (fst (split_last args)) of
SOME i => let
val constr_names = map dest_Const_name ctrs
val (Ts, _) = strip_type T
val T' = List.last Ts in SOME (List.last args, T', i, nth args i, nth constr_names i) end
| NONE => NONE)
| NONE => NONE)
| NONE => NONE) end
lemmalist_induct3[consumes2,case_namesNilCons]: "lengthxs=lengthys\<Longrightarrow>lengthys=lengthzs\<Longrightarrow>P[][][]\<Longrightarrow> (\<And>xxsyyszzs.lengthxs=lengthys\<Longrightarrow>lengthys=lengthzs\<Longrightarrow>Pxsyszs\<Longrightarrow>P(x#xs)(y#ys)(z#zs)) \<Longrightarrow>Pxsyszs" proof(inductxsarbitrary:yszs) caseNilthenshow?casebysimp next case(Consxxsyszs)thenshow?caseby(casesys,simp_all) (caseszs,simp_all) qed
lemmalist_induct4[consumes3,case_namesNilCons]: "lengthxs=lengthys\<Longrightarrow>lengthys=lengthzs\<Longrightarrow>lengthzs=lengthws\<Longrightarrow> P[][][][]\<Longrightarrow>(\<And>xxsyyszzswws.lengthxs=lengthys\<Longrightarrow> lengthys=lengthzs\<Longrightarrow>lengthzs=lengthws\<Longrightarrow>Pxsyszsws\<Longrightarrow> P(x#xs)(y#ys)(z#zs)(w#ws))\<Longrightarrow>Pxsyszsws" proof(inductxsarbitrary:yszsws) caseNilthenshow?casebysimp next case(Consxxsyszsws)thenshow?caseby((casesys,simp_all),(caseszs,simp_all))(casesws,simp_all) qed
lemmarev_is_rev_conv[iff]:"(revxs=revys)=(xs=ys)" proof(inductxsarbitrary:ys) caseNil thenshow?casebyforce next caseCons thenshow?caseby(casesys)auto qed
lemmasplit_list:"x\<in>setxs\<Longrightarrow>\<exists>yszs.xs=ys@x#zs" proof(inductxs) caseNilthus?casebysimp next caseConsthus?caseby(autointro:Cons_eq_appendI) qed
lemmasplit_list_first:"x\<in>setxs\<Longrightarrow>\<exists>yszs.xs=ys@x#zs\<and>x\<notin>setys" proof(inductxs) caseNilthus?casebysimp next case(Consaxs) show?case proofcases assume"x=a"thus?caseusingConsbyfastforce next assume"x\<noteq>a"thus?caseusingConsby(fastforceintro!:Cons_eq_appendI) qed qed
lemmasplit_list_last:"x\<in>setxs\<Longrightarrow>\<exists>yszs.xs=ys@x#zs\<and>x\<notin>setzs" proof(inductxsrule:rev_induct) caseNilthus?casebysimp next case(snocaxs) show?case proofcases assume"x=a"thus?caseusingsnocby(autointro!:exI) next assume"x\<noteq>a"thus?caseusingsnocbyfastforce qed qed
lemmasplit_list_prop:"\<exists>x\<in>setxs.Px\<Longrightarrow>\<exists>ysxzs.xs=ys@x#zs\<and>Px" proof(inductxs) caseNilthus?casebysimp next caseConsthus?case by(simpadd:Bex_def)(metisappend_Consappend.simps(1)) qed
lemmasplit_list_first_prop: "\<exists>x\<in>setxs.Px\<Longrightarrow> \<exists>ysxzs.xs=ys@x#zs\<and>Px\<and>(\<forall>y\<in>setys.\<not>Py)" proof(inductxs) caseNilthus?casebysimp next case(Consxxs) show?case proofcases assume"Px" hence"x#xs=[]@x#xs\<and>Px\<and>(\<forall>y\<in>set[].\<not>Py)"bysimp thus?thesisbyfast next assume"\<not>Px" hence"\<exists>x\<in>setxs.Px"usingCons(2)bysimp thus?thesisusing\<open>\<not>Px\<close>Cons(1)by(metisappend_Consset_ConsD) qed qed
lemmasplit_list_last_prop: "\<exists>x\<in>setxs.Px\<Longrightarrow> \<exists>ysxzs.xs=ys@x#zs\<and>Px\<and>(\<forall>z\<in>setzs.\<not>Pz)" proof(inductxsrule:rev_induct) caseNilthus?casebysimp next case(snocxxs) show?case proofcases assume"Px"thus?thesisby(autointro!:exI) next assume"\<not>Px" hence"\<exists>x\<in>setxs.Px"usingsnoc(2)bysimp thus?thesisusing\<open>\<not>Px\<close>snoc(1)byfastforce qed qed
lemmalength_filter_less: "\<lbrakk>x\<in>setxs;\<not>Px\<rbrakk>\<Longrightarrow>length(filterPxs)<lengthxs" proof(inductxs) caseNilthus?casebysimp next case(Consxxs)thus?case usingSuc_le_eqbyfastforce qed
lemmalength_filter_conv_card: "length(filterpxs)=card{i.i<lengthxs\<and>p(xs!i)}" proof(inductxs) caseNilthus?casebysimp next case(Consxxs) let?S="{i.i<lengthxs\<and>p(xs!i)}" havefin:"finite?S"by(fastintro:bounded_nat_set_is_finite) show?case(is"?l=card?S'") proof(cases) assume"px" henceeq:"?S'=insert0(Suc`?S)" by(autosimp:image_defsplit:nat.splitdest:gr0_implies_Suc) have"length(filterp(x#xs))=Suc(card?S)" usingCons\<open>px\<close>bysimp alsohave"\<dots>=Suc(card(Suc`?S))"usingfin by(simpadd:card_image) alsohave"\<dots>=card?S'"usingeqfin by(simpadd:card_insert_if) finallyshow?thesis. next assume"\<not>px" henceeq:"?S'=Suc`?S" by(autosimpadd:image_defsplit:nat.splitelim:lessE) have"length(filterp(x#xs))=card?S" usingCons\<open>\<not>px\<close>bysimp alsohave"\<dots>=card(Suc`?S)"usingfin by(simpadd:card_image) alsohave"\<dots>=card?S'"usingeqfin by(simpadd:card_insert_if) finallyshow?thesis. qed qed
lemmaCons_eq_filterD: "x#xs=filterPys\<Longrightarrow> \<exists>usvs.ys=us@x#vs\<and>(\<forall>u\<in>setus.\<not>Pu)\<and>Px\<and>xs=filterPvs" (is"_\<Longrightarrow>\<exists>usvs.?Pysusvs") proof(inductys) caseNilthus?casebysimp next case(Consyys) show?case(is"\<exists>x.?Qx") proofcases assumePy:"Py" show?thesis proofcases assume"x=y" withPyCons.premshave"?Q[]"bysimp thenshow?thesis.. next assume"x\<noteq>y" withPyCons.premsshow?thesisbysimp qed next assume"\<not>Py" withConsobtainusvswhere"?P(y#ys)(y#us)vs"byfastforce thenhave"?Q(y#us)"bysimp thenshow?thesis.. qed qed
lemmaef\inner{innerjava.lang.StringIndexOutOfBoundsException: Index 21 out of bounds for length 21 proof(inductxsarbitrary:n)% macro works on the assumption that #1 is 'AnnIndent' kjl 24/7/92 case(Consxxs) thenshow?case usingless_Suc_eq_0_disjbyauto qedsimp
lemmaset_conv_nth:"setxs={xs!i|i.i<lengthxs}" proof(inductxs) case(Consx\\F@bls#1@bl@\@@=#1}\bls |i=(xsii(}(is"=R) proof show"?L\<subseteq>?R" byforce "R<>?" usingwiw=@indentjava.lang.StringIndexOutOfBoundsException: Index 25 out of bounds for length 25 qed withConsshow?case @#1#{\Macro\preFromHooklnout
qed simp
showrhs
proof (rule ccontr)\\{
assumen< "
n "by
with \<\ifmmode\\endcomposite\subIII\\\kw{end}}%
moreover from \<open>n > 0\<close> \<\ifnextchar:@\cfl}
ultimately have "\else \b@type\kw{compose}#\kw{ of }#2kw end}\@type\}
<x\notin\<>in_set_conv_nth[ xxs by
next
assume ?rhs then show ?lhs by simp\ifnextchar%
qed
lemma nth_non_equal_first_eq:\#\{#}2
assumes "x \<noteq> y"
shows "def\valuedef1{\@trueFirstDeftrue
proof
assume "?lhs" with assms have "n > 0" by (cases n) simp_all
withopen?\<showrhsby simp
next
assume "?rhs" then show "?lhs" by ifx@@empa@tempb
qed
def\@tempa#}
lemma list_ball_nth: "\<lbrakk>n < length xs;
by (auto simp add: set_conv_nth)
lemma nth_mem [
by (autodef\@\@java.lang.StringIndexOutOfBoundsException: Index 36 out of bounds for length 36
"\<lbrakk\Inyd
by (auto\fin
all_set_conv_all_nth "(\<forall>x \<in> set xs. P x) = ( \in@al}
by (auto simp add: set_conv_nth)
next\@
% \fn@in@let@stmt@ is set true when \fn@in@let@ is true but TeX is not in
hence n: "n < Suc (length xs)" by simp
moreover
{ assume "% \end{fn}
with ' ' length xs -n= Suc n'
by (cases "length xs - n", auto)
moreover
from n' have "length xs - Suc n = n'" by simp
ultimately
have "xs ! (length xs - Suc n) = (x # xs) ! (length xs - n)" by simp
}
ultimately% fred : A -> B
showpat
qed
lemma "% TeX will be in maths mode if the function is being typeset within a
(is "_ = (\<exists>xs. ?P k xs)"% (kjl, 10/10/91).
induct k)
case 0 funmath#2\\@@
next
{\parms
show is" "is" \existsxs. P xs))
proof "" thus"?L"using by
java.lang.StringIndexOutOfBoundsException: Range [34, 35) out of bounds for length 6
?
?[" :nth_append less_Suc_eqjava.lang.StringIndexOutOfBoundsException: Index 59 out of bounds for length 59
lemma rev_update \\\F@\MNbls
k length \>(k=y)= ( xs)[length xs - k - 1 := y]"
arbitrary k) autosimp:list_update_appendsplit:natsplits
lemma update_zip: "arg#1{#1eassdef
by (induct ys arbitrary: i xy xs) (auto, case_tac java.lang.StringIndexOutOfBoundsException: Index 52 out of bounds for length 28
lemma set_update_subset_insert: "set(xs[i:=x]) \< \endgroup\enddefstmt}
by (induct xs arbitrary: i) (auto split: nat% This always had a line-break after the `Lin' part, now the user
lemma set_update_subsetI: "\<lbrakk>set xs \<subseteq> A; x \<in> A\<rbrakk> iffn@@let@afterpat@truedef\sep=}\\AMblsFbls}
by (blast dest!: set_update_subset_insert [THEN subsetD])
lemma set_update_memI: "n < length xs \<Longrightarrow>\def@letbestmt{\\@{kw be }\AM@
by xs :n autosplit.splits)
: "x \<in> set (butlast xs) \<Longrightarrow> x \<in> set xs"
xs split:if_split_asm
lemma in_set_butlast_appendI: "x \<in> set (butlast xs) \<or> x \<in> set (butlast ys) \<Longrightarrow>
by (auto dest: in_set_butlastD simp add: butlast_append)
[]: "n <lengthxs\Longrightarrow last(drop n xs) last xs
by (induct xs arbitrary: n)(auto split:nat.split)
assumes "n < length (\def\wi@tack{}
proof (cases xs)
case (Cons y ys)
assms "xs ! n = (butlast xs @ last xs] ! n"
by (simp add: nth_append)
ultimately show ?thesis using append_butlast_last_id by simp
qed simp 1
lemmalast_map:" \<noteq>[] \<Longrightarrow> last (map f xs) f last xs)java.lang.StringIndexOutOfBoundsException: Index 80 out of bounds for length 80
by (cases xs rule: rev_cases) simp_all
lemma map_butlast: "map f (butlast xs) = butlast (map f xs)"
(
lemma nth_via_drop: "drop n xs = y#ys \<Longrightarrow xs! = y"
by (induct xs arbitrary: n, simp)(auto simp: drop_Cons nth_Cons \\Infer{global\dvance\indentLevelby\ne
lemma take_Suc_conv_app_nth: "i < length xs \<Longrightarrow> take (Suc i) xs = take i xs @ [xs!i]"
proof (induct xs arbitrary: i) caseNil
then show ?case by simp
next case Cons
then show ?case by (cases i) auto
qed
lemma Cons_nth_drop_Suc: "i < length xs \<Longrightarrow> (xs!i) # (drop (Suc i) xs) = drop i xs"
proof (induct xs arbitrary: i) caseNil
then show ?case by simp
next case Cons
then show ?case by (cases i) auto
qed
lemma length_take [simp]: "length (take n xs) = min (length xs) n"
by (induct n arbitrary: xs) (auto, case_tac xs, auto)
lemma length_drop [simp]: "length (drop n xs) = (length xs - n)"
by (induct n arbitrary: xs) (auto, case_tac xs, auto)
lemma take_all [simp]: "length xs \<le> n \<Longrightarrow> take n xs = xs"
by (induct n arbitrary: xs) (auto, case_tac xs, auto)
lemma drop_all [simp]: "length xs \<le> n \<Longrightarrow> drop n xs = []"
by (induct n arbitrary: xs) (auto, case_tac xs, auto)
lemma take_all_iff [simp]: "take n xs = xs \<longleftrightarrow> length xs \<le> n"
by (metis length_take min.order_iff take_all)
(* Looks like a good simp rule but can cause looping;
too much interaction between take and length
lemmas take_all_iff2[simp] = take_all_iff[THEN eq_iff_swap]
*)
lemma take_append [simp]: "take n (xs @ ys) = (take n xs @ take (n - length xs) ys)"
by (induct n arbitrary: xs) (auto, case_tac xs, auto)
lemma drop_append [simp]: "drop n (xs @ ys) = drop n xs @ drop (n - length xs) ys"
by (induct n arbitrary: xs) (auto, case_tac xs, auto)
lemma take_take [simp]: "take n (take m xs) = take (min n m) xs"
proof (induct m arbitrary: xs n) case0
then show ?case by simp
next case Suc
then show ?case by (cases xs; cases n) simp_all
qed
lemma drop_drop [simp]: "drop n (drop m xs) = drop (n + m) xs"
proof (induct m arbitrary: xs) case0
then show ?case by simp
next case Suc
then show ?case by (cases xs) simp_all
qed
lemma take_drop: "take n (drop m xs) = drop m (take (n + m) xs)"
proof (induct m arbitrary: xs n) case0
then show ?case by simp
next case Suc
then show ?case by (cases xs; cases n) simp_all
qed
lemma drop_take: "drop n (take m xs) = take (m-n) (drop n xs)"
by(induct xs arbitrary: m n)(auto simp: take_Cons drop_Cons split: nat.split)
lemma append_take_drop_id [simp]: "take n xs @ drop n xs = xs"
proof (induct n arbitrary: xs) case0
then show ?case by simp
next case Suc
then show ?case by (cases xs) simp_all
qed
lemma take_map: "take n (map f xs) = map f (take n xs)"
proof (induct n arbitrary: xs) case0
then show ?case by simp
next case Suc
then show ?case by (cases xs) simp_all
qed
lemma drop_map: "drop n (map f xs) = map f (drop n xs)"
proof (induct n arbitrary: xs) case0
then show ?case by simp
next case Suc
then show ?case by (cases xs) simp_all
qed
lemma rev_take: "rev (take i xs) = drop (length xs - i) (rev xs)"
proof (induct xs arbitrary: i) caseNil
then show ?case by simp
next case Cons
then show ?case by (cases i) auto
qed
lemma rev_drop: "rev (drop i xs) = take (length xs - i) (rev xs)"
proof (induct xs arbitrary: i) caseNil
then show ?case by simp
next case Cons
then show ?case by (cases i) auto
qed
lemma nth_take [simp]: "i < n \<Longrightarrow> (take n xs)!i = xs!i"
proof (induct xs arbitrary: i n) caseNil
then show ?case by simp
next case Cons
then show ?case by (cases n; cases i) simp_all
qed
lemma nth_drop [simp]: "n \<le> length xs \<Longrightarrow> (drop n xs)!i = xs!(n + i)"
proof (induct n arbitrary: xs) case0
then show ?case by simp
next case Suc
then show ?case by (cases xs) simp_all
qed
lemma butlast_take: "n \<le> length xs \<Longrightarrow> butlast (take n xs) = take (n - 1) xs"
by (simp add: butlast_conv_take)
lemma butlast_drop: "butlast (drop n xs) = drop n (butlast xs)"
by (simp add: butlast_conv_take drop_take ac_simps)
lemma take_butlast: "n < length xs \<Longrightarrow> take n (butlast xs) = take n xs"
by (simp add: butlast_conv_take)
lemma drop_butlast: "drop n (butlast xs) = butlast (drop n xs)"
by (simp add: butlast_conv_take drop_take ac_simps)
lemma butlast_power: "(butlast ^^ n) xs = take (length xs - n) xs"
by (induct n) (auto simp: butlast_take)
lemma set_take_subset_set_take: "m \<le> n \<Longrightarrow> set(take m xs) \<le> set(take n xs)"
proof (induct xs arbitrary: m n) case (Cons x xs m n) then show ?case
by (cases n) (auto simp: take_Cons)
qed simp
lemma set_take_subset: "set(take n xs) \<subseteq> set xs"
by(induct xs arbitrary: n)(auto simp:take_Cons split:nat.split)
lemma set_drop_subset: "set(drop n xs) \<subseteq> set xs"
by(induct xs arbitrary: n)(auto simp:drop_Cons split:nat.split)
lemma set_drop_subset_set_drop: "m \<ge> n \<Longrightarrow> set(drop m xs) \<le> set(drop n xs)"
proof (induct xs arbitrary: m n) case (Cons x xs m n)
then show ?case
by (clarsimp simp: drop_Cons split: nat.split) (metis set_drop_subset subset_iff)
qed simp
lemma in_set_takeD: "x \<in> set(take n xs) \<Longrightarrow> x \<in> set xs"
using set_take_subset by fast
lemma in_set_dropD: "x \<in> set(drop n xs) \<Longrightarrow> x \<in> set xs"
using set_drop_subset by fast
lemma append_eq_conv_conj: "(xs @ ys = zs) = (xs = take (length xs) zs \<and> ys = drop (length xs) zs)"
proof (induct xs arbitrary: zs) case (Cons x xs zs) then show ?case
by (cases zs, auto)
qed auto
lemma map_eq_append_conv: "map f xs = ys @ zs \<longleftrightarrow> (\<exists>us vs. xs = us @ vs \<and> ys = map f us \<and> zs = map f vs)"
proof -
have "map f xs \<noteq> ys @ zs \<and> map f xs \<noteq> ys @ zs \<or> map f xs \<noteq> ys @ zs \<or> map f xs = ys @ zs \<and>
(\<exists>bs bsa. xs = bs @ bsa \<and> ys = map f bs \<and> zs = map f bsa)"
by (metis append_eq_conv_conj append_take_drop_id drop_map take_map)
then show ?thesis
using map_append by blast
qed
lemma take_add: "take (i+j) xs = take i xs @ take j (drop i xs)"
proof (induct xs arbitrary: i) case (Cons x xs i) then show ?case
by (cases i, auto)
qed auto
lemma append_eq_append_conv_if: "(xs\<^sub>1 @ xs\<^sub>2 = ys\<^sub>1 @ ys\<^sub>2) =
(if size xs\<^sub>1 \<le> size ys\<^sub>1
then xs\<^sub>1 = take (size xs\<^sub>1) ys\<^sub>1 \<and> xs\<^sub>2 = drop (size xs\<^sub>1) ys\<^sub>1 @ ys\<^sub>2
else take (size ys\<^sub>1) xs\<^sub>1 = ys\<^sub>1 \<and> drop (size ys\<^sub>1) xs\<^sub>1 @ xs\<^sub>2 = ys\<^sub>2)"
proof (induct xs\<^sub>1 arbitrary: ys\<^sub>1) case (Cons a xs\<^sub>1 ys\<^sub>1) then show ?case
by (cases ys\<^sub>1, auto)
qed auto
lemma take_hd_drop: "n < length xs \<Longrightarrow> take n xs @ [hd (drop n xs)] = take (Suc n) xs"
by (induct xs arbitrary: n) (simp_all add:drop_Cons split:nat.split)
lemma id_take_nth_drop: "i < length xs \<Longrightarrow> xs = take i xs @ xs!i # drop (Suc i) xs"
proof -
assume si: "i < length xs"
hence "xs = take (Suc i) xs @ drop (Suc i) xs" by auto
moreover
from si have "take (Suc i) xs = take i xs @ [xs!i]"
using take_Suc_conv_app_nth by blast
ultimately show ?thesis by auto
qed
lemma take_update_cancel[simp]: "n \<le> m \<Longrightarrow> take n (xs[m := y]) = take n xs"
by(simp add: list_eq_iff_nth_eq)
lemma drop_update_cancel[simp]: "n < m \<Longrightarrow> drop m (xs[n := x]) = drop m xs"
by(simp add: list_eq_iff_nth_eq)
lemma upd_conv_take_nth_drop: "i < length xs \<Longrightarrow> xs[i:=a] = take i xs @ a # drop (Suc i) xs"
proof -
assume i: "i < length xs"
have "xs[i:=a] = (take i xs @ xs!i # drop (Suc i) xs)[i:=a]"
by(rule arg_cong[OF id_take_nth_drop[OF i]])
also have "\<dots> = take i xs @ a # drop (Suc i) xs"
using i by (simp add: list_update_append)
finally show ?thesis .
qed
lemma take_update_swap: "take m (xs[n := x]) = (take m xs)[n := x]"
proof (cases "n \<ge> length xs") case False
then show ?thesis
by (simp add: list_update_beyond upd_conv_take_nth_drop take_Cons drop_take min_def diff_Suc split: nat.split)
qed (auto simp: list_update_beyond)
lemma drop_update_swap:
assumes "m \<le> n" shows "drop m (xs[n := x]) = (drop m xs)[n-m := x]"
proof (cases "n \<ge> length xs") case False
with assms show ?thesis
by (simp add: upd_conv_take_nth_drop drop_take)
qed (auto simp: list_update_beyond)
lemma nth_image: "l \<le> size xs \<Longrightarrow> nth xs ` {0..<l} = set(take l xs)"
by (simp add: set_conv_nth) force
lemma set_list_update: "set (xs [i := k]) = insert k (set (take i xs) \<union> set (drop (Suc i) xs))" if \<open>i < length xs\<close>
using that proof (induct xs arbitrary: i) caseNil
then show ?case
by simp
next case (Cons x xs i)
then show ?case
by (cases i) (simp_all add: insert_commute)
qed
subsubsection \<open>\<^const>\<open>takeWhile\<close> and \<^const>\<open>dropWhile\<close>\<close>
lemma length_takeWhile_le: "length (takeWhile P xs) \<le> length xs"
by (induct xs) auto
lemma takeWhile_dropWhile_id [simp]: "takeWhile P xs @ dropWhile P xs = xs"
by (induct xs) auto
lemma takeWhile_append1 [simp]: "\<lbrakk>x \<in> set xs; \<not>P(x)\<rbrakk> \<Longrightarrow> takeWhile P (xs @ ys) = takeWhile P xs"
by (induct xs) auto
lemma takeWhile_append2 [simp]: "(\<And>x. x \<in> set xs \<Longrightarrow> P x) \<Longrightarrow> takeWhile P (xs @ ys) = xs @ takeWhile P ys"
by (induct xs) auto
lemma takeWhile_append: "takeWhile P (xs @ ys) = (if \<forall>x\<in>set xs. P x then xs @ takeWhile P ys else takeWhile P xs)"
using takeWhile_append1[of _ xs P ys] takeWhile_append2[of xs P ys] by auto
lemma takeWhile_tail: "\<not> P x \<Longrightarrow> takeWhile P (xs @ (x#l)) = takeWhile P xs"
by (induct xs) auto
lemma takeWhile_eq_Nil_iff: "takeWhile P xs = [] \<longleftrightarrow> xs = [] \<or> \<not>P (hd xs)"
by (cases xs) auto
lemma takeWhile_nth: "j < length (takeWhile P xs) \<Longrightarrow> takeWhile P xs ! j = xs ! j"
by (metis nth_append takeWhile_dropWhile_id)
lemma takeWhile_takeWhile: "takeWhile Q (takeWhile P xs) = takeWhile (\<lambda>x. P x \<and> Q x) xs"
by(induct xs) simp_all
lemma dropWhile_nth: "j < length (dropWhile P xs) \<Longrightarrow>
dropWhile P xs ! j = xs ! (j + length (takeWhile P xs))"
by (metis add.commute nth_append_length_plus takeWhile_dropWhile_id)
lemma length_dropWhile_le: "length (dropWhile P xs) \<le> length xs"
by (induct xs) auto
lemma dropWhile_append1 [simp]: "\<lbrakk>x \<in> set xs; \<not>P(x)\<rbrakk> \<Longrightarrow> dropWhile P (xs @ ys) = (dropWhile P xs)@ys"
by (induct xs) auto
lemma dropWhile_append2 [simp]: "(\<And>x. x \<in> set xs \<Longrightarrow> P(x)) \<Longrightarrow> dropWhile P (xs @ ys) = dropWhile P ys"
by (induct xs) auto
lemma dropWhile_id[simp]: "(\<And>x. x \<in> set xs \<Longrightarrow> \<not> P x) \<Longrightarrow> dropWhile P xs = xs"
using takeWhile_dropWhile_id[of P xs] takeWhile_eq_Nil_iff[of P xs]
by fastforce
lemma dropWhile_append3: "\<not> P y \<Longrightarrow>dropWhile P (xs @ y # ys) = dropWhile P xs @ y # ys"
by (induct xs) auto
lemma dropWhile_append: "dropWhile P (xs @ ys) = (if \<forall>x\<in>set xs. P x then dropWhile P ys else dropWhile P xs @ ys)"
using dropWhile_append1[of _ xs P ys] dropWhile_append2[of xs P ys] by auto
lemma dropWhile_last: "x \<in> set xs \<Longrightarrow> \<not> P x \<Longrightarrow> last (dropWhile P xs) = last xs"
by (auto simp add: dropWhile_append3 in_set_conv_decomp)
lemma set_dropWhileD: "x \<in> set (dropWhile P xs) \<Longrightarrow> x \<in> set xs"
by (induct xs) (auto split: if_split_asm)
lemma set_takeWhileD: "x \<in> set (takeWhile P xs) \<Longrightarrow> x \<in> set xs \<and> P x"
by (induct xs) (auto split: if_split_asm)
lemma takeWhile_eq_all_conv[simp]: "(takeWhile P xs = xs) = (\<forall>x \<in> set xs. P x)"
by(induct xs, auto)
lemma dropWhile_eq_Nil_conv[simp]: "(dropWhile P xs = []) = (\<forall>x \<in> set xs. P x)"
by(induct xs, auto)
lemma dropWhile_eq_Cons_conv: "(dropWhile P xs = y#ys) = (xs = takeWhile P xs @ y # ys \<and> \<not> P y)"
by(induct xs, auto)
lemma dropWhile_dropWhile1: "(\<And>x. Q x \<Longrightarrow> P x) \<Longrightarrow> dropWhile Q (dropWhile P xs) = dropWhile P xs"
by(induct xs) simp_all
lemma dropWhile_dropWhile2: "(\<And>x. P x \<Longrightarrow> Q x) \<Longrightarrow> takeWhile P (takeWhile Q xs) = takeWhile P xs"
by(induct xs) simp_all
lemma dropWhile_takeWhile: "(\<And>x. P x \<Longrightarrow> Q x) \<Longrightarrow> dropWhile P (takeWhile Q xs) = takeWhile Q (dropWhile P xs)"
by (induction xs) auto
lemma distinct_takeWhile[simp]: "distinct xs \<Longrightarrow> distinct (takeWhile P xs)"
by (induct xs) (auto dest: set_takeWhileD)
lemma distinct_dropWhile[simp]: "distinct xs \<Longrightarrow> distinct (dropWhile P xs)"
by (induct xs) auto
lemma takeWhile_map: "takeWhile P (map f xs) = map f (takeWhile (P \<circ> f) xs)"
by (induct xs) auto
lemma dropWhile_map: "dropWhile P (map f xs) = map f (dropWhile (P \<circ> f) xs)"
by (induct xs) auto
lemma takeWhile_eq_take: "takeWhile P xs = take (length (takeWhile P xs)) xs"
by (induct xs) auto
lemma dropWhile_eq_drop: "dropWhile P xs = drop (length (takeWhile P xs)) xs"
by (induct xs) auto
lemma hd_dropWhile: "dropWhile P xs \<noteq> [] \<Longrightarrow> \<not> P (hd (dropWhile P xs))"
by (induct xs) auto
lemma takeWhile_eq_filter:
assumes "\<And> x. x \<in> set (dropWhile P xs) \<Longrightarrow> \<not> P x"
shows "takeWhile P xs = filter P xs"
proof -
have A: "filter P xs = filter P (takeWhile P xs @ dropWhile P xs)"
by simp
have B: "filter P (dropWhile P xs) = []"
unfolding filter_empty_conv using assms by blast
have "filter P xs = takeWhile P xs"
unfolding A filter_append B
by (auto simp add: filter_id_conv dest: set_takeWhileD)
thus ?thesis ..
qed
lemma takeWhile_eq_take_P_nth: "\<lbrakk> \<And> i. \<lbrakk> i < n ; i < length xs \<rbrakk> \<Longrightarrow> P (xs ! i) ; n < length xs \<Longrightarrow> \<not> P (xs ! n) \<rbrakk> \<Longrightarrow>
takeWhile P xs = take n xs"
proof (induct xs arbitrary: n) caseNil
thus ?case by simp
next case (Cons x xs)
show ?case
proof (cases n) case0
with Cons show ?thesis by simp
next case [simp]: (Suc n')
have "P x" using Cons.prems(1)[of 0] by simp
moreover have "takeWhile P xs = take n' xs"
proof (rule Cons.hyps)
fix i
assume "i < n'""i < length xs"
thus "P (xs ! i)" using Cons.prems(1)[of "Suc i"] by simp
next
assume "n' < length xs"
thus "\<not> P (xs ! n')" using Cons by auto
qed
ultimately show ?thesis by simp
qed
qed
lemma nth_length_takeWhile: "length (takeWhile P xs) < length xs \<Longrightarrow> \<not> P (xs ! length (takeWhile P xs))"
by (induct xs) auto
lemma length_takeWhile_less_P_nth:
assumes all: "\<And> i. i < j \<Longrightarrow> P (xs ! i)"and"j \<le> length xs"
shows "j \<le> length (takeWhile P xs)"
proof (rule classical)
assume "\<not> ?thesis"
hence "length (takeWhile P xs) < length xs" using assms by simp
thus ?thesis using all \<open>\<not> ?thesis\<close> nth_length_takeWhile[of P xs] by auto
qed
lemma takeWhile_neq_rev: "\<lbrakk>distinct xs; x \<in> set xs\<rbrakk> \<Longrightarrow>
takeWhile (\<lambda>y. y \<noteq> x) (rev xs) = rev (tl (dropWhile (\<lambda>y. y \<noteq> x) xs))"
by(induct xs) (auto simp: takeWhile_tail[where l="[]"])
lemma dropWhile_neq_rev: "\<lbrakk>distinct xs; x \<in> set xs\<rbrakk> \<Longrightarrow>
dropWhile (\<lambda>y. y \<noteq> x) (rev xs) = x # rev (takeWhile (\<lambda>y. y \<noteq> x) xs)"
proof (induct xs) case (Cons a xs)
then show ?case
by(auto, subst dropWhile_append2, auto)
qed simp
lemma takeWhile_not_last: "distinct xs \<Longrightarrow> takeWhile (\<lambda>y. y \<noteq> last xs) xs = butlast xs"
by(induction xs rule: induct_list012) auto
lemma takeWhile_cong [fundef_cong]: "\<lbrakk>l = k; \<And>x. x \<in> set l \<Longrightarrow> P x = Q x\<rbrakk>
\<Longrightarrow> takeWhile P l = takeWhile Q k"
by (induct k arbitrary: l) (simp_all)
lemma dropWhile_cong [fundef_cong]: "\<lbrakk>l = k; \<And>x. x \<in> set l \<Longrightarrow> P x = Q x\<rbrakk>
\<Longrightarrow> dropWhile P l = dropWhile Q k"
by (induct k arbitrary: l, simp_all)
lemma takeWhile_idem [simp]: "takeWhile P (takeWhile P xs) = takeWhile P xs"
by (induct xs) auto
lemma dropWhile_idem [simp]: "dropWhile P (dropWhile P xs) = dropWhile P xs"
by (induct xs) auto
lemma length_zip [simp]: "length (zip xs ys) = min (length xs) (length ys)"
by (induct xs ys rule:list_induct2') auto
lemma zip_obtain_same_length:
assumes "\<And>zs ws n. length zs = length ws \<Longrightarrow> n = min (length xs) (length ys)
\<Longrightarrow> zs = take n xs \<Longrightarrow> ws = take n ys \<Longrightarrow> P (zip zs ws)"
shows "P (zip xs ys)"
proof - let ?n = "min (length xs) (length ys)"
have "P (zip (take ?n xs) (take ?n ys))"
by (rule assms) simp_all
moreover have "zip xs ys = zip (take ?n xs) (take ?n ys)"
proof (induct xs arbitrary: ys) caseNil then show ?case by simp
next case (Cons x xs) then show ?case by (cases ys) simp_all
qed
ultimately show ?thesis by simp
qed
lemma zip_append1: "zip (xs @ ys) zs =
zip xs (take (length xs) zs) @ zip ys (drop (length xs) zs)"
by (induct xs zs rule:list_induct2') auto
lemma zip_append2: "zip xs (ys @ zs) =
zip (take (length ys) xs) ys @ zip (drop (length ys) xs) zs"
by (induct xs ys rule:list_induct2') auto
lemma zip_append [simp]: "\<lbrakk>length xs = length us\<rbrakk> \<Longrightarrow>
zip (xs@ys) (us@vs) = zip xs us @ zip ys vs"
by (simp add: zip_append1)
lemma zip_map_map: "zip (map f xs) (map g ys) = map (\<lambda> (x, y). (f x, g y)) (zip xs ys)"
proof (induct xs arbitrary: ys) case (Cons x xs) note Cons_x_xs = Cons.hyps
show ?case
proof (cases ys) case (Cons y ys')
show ?thesis unfolding Cons using Cons_x_xs by simp
qed simp
qed simp
lemma zip_map1: "zip (map f xs) ys = map (\<lambda>(x, y). (f x, y)) (zip xs ys)"
using zip_map_map[of f xs "\<lambda>x. x" ys] by simp
lemma zip_map2: "zip xs (map f ys) = map (\<lambda>(x, y). (x, f y)) (zip xs ys)"
using zip_map_map[of "\<lambda>x. x" xs f ys] by simp
lemma map_zip_map: "map f (zip (map g xs) ys) = map (%(x,y). f(g x, y)) (zip xs ys)"
by (auto simp: zip_map1)
lemma map_zip_map2: "map f (zip xs (map g ys)) = map (%(x,y). f(x, g y)) (zip xs ys)"
by (auto simp: zip_map2)
text\<open>Courtesy of Andreas Lochbihler:\<close>
lemma zip_same_conv_map: "zip xs xs = map (\<lambda>x. (x, x)) xs"
by(induct xs) auto
lemma nth_zip [simp]: "\<lbrakk>i < length xs; i < length ys\<rbrakk> \<Longrightarrow> (zip xs ys)!i = (xs!i, ys!i)"
proof (induct ys arbitrary: i xs) case (Cons y ys)
then show ?case
by (cases xs) (simp_all add: nth.simps split: nat.split)
qed auto
lemma set_zip: "set (zip xs ys) = {(xs!i, ys!i) | i. i < min (length xs) (length ys)}"
by(simp add: set_conv_nth cong: rev_conj_cong)
lemma zip_same: "((a,b) \<in> set (zip xs xs)) = (a \<in> set xs \<and> a = b)"
by(induct xs) auto
lemma zip_replicate [simp]: "zip (replicate i x) (replicate j y) = replicate (min i j) (x,y)"
proof (induct i arbitrary: j) case (Suc i)
then show ?case
by (cases j, auto)
qed auto
lemma zip_replicate1: "zip (replicate n x) ys = map (Pair x) (take n ys)"
by(induction ys arbitrary: n)(case_tac [2] n, simp_all)
lemma take_zip: "take n (zip xs ys) = zip (take n xs) (take n ys)"
proof (induct n arbitrary: xs ys) case0
then show ?case by simp
next case Suc
then show ?case by (cases xs; cases ys) simp_all
qed
lemma drop_zip: "drop n (zip xs ys) = zip (drop n xs) (drop n ys)"
proof (induct n arbitrary: xs ys) case0
then show ?case by simp
next case Suc
then show ?case by (cases xs; cases ys) simp_all
qed
lemma zip_takeWhile_fst: "zip (takeWhile P xs) ys = takeWhile (P \<circ> fst) (zip xs ys)"
proof (induct xs arbitrary: ys) caseNil
then show ?case by simp
next case Cons
then show ?case by (cases ys) auto
qed
lemma zip_takeWhile_snd: "zip xs (takeWhile P ys) = takeWhile (P \<circ> snd) (zip xs ys)"
proof (induct xs arbitrary: ys) caseNil
then show ?case by simp
next case Cons
then show ?case by (cases ys) auto
qed
lemma set_zip_leftD: "(x,y)\<in> set (zip xs ys) \<Longrightarrow> x \<in> set xs"
by (induct xs ys rule:list_induct2') auto
lemma set_zip_rightD: "(x,y)\<in> set (zip xs ys) \<Longrightarrow> y \<in> set ys"
by (induct xs ys rule:list_induct2') auto
lemma in_set_zipE: "(x,y) \<in> set(zip xs ys) \<Longrightarrow> (\<lbrakk> x \<in> set xs; y \<in> set ys \<rbrakk> \<Longrightarrow> R) \<Longrightarrow> R"
by(blast dest: set_zip_leftD set_zip_rightD)
lemma in_set_zip: "p \<in> set (zip xs ys) \<longleftrightarrow> (\<exists>n. xs ! n = fst p \<and> ys ! n = snd p
\<and> n < length xs \<and> n < length ys)"
by (cases p) (auto simp add: set_zip)
lemma in_set_impl_in_set_zip1:
assumes "length xs = length ys"
assumes "x \<in> set xs"
obtains y where "(x, y) \<in> set (zip xs ys)"
proof -
from assms have "x \<in> set (map fst (zip xs ys))" by simp
from this that show ?thesis by fastforce
qed
lemma in_set_impl_in_set_zip2:
assumes "length xs = length ys"
assumes "y \<in> set ys"
obtains x where "(x, y) \<in> set (zip xs ys)"
proof -
from assms have "y \<in> set (map snd (zip xs ys))" by simp
from this that show ?thesis by fastforce
qed
lemma zip_eq_ConsE:
assumes "zip xs ys = xy # xys"
obtains x xs' y ys' where "xs = x # xs'" and"ys = y # ys'"and"xy = (x, y)" and"xys = zip xs' ys'"
proof -
from assms have "xs \<noteq> []"and"ys \<noteq> []"
using zip_eq_Nil_iff [of xs ys] by simp_all
then obtain x xs' y ys' where xs: "xs = x # xs'" and ys: "ys = y # ys'"
by (cases xs; cases ys) auto
with assms have "xy = (x, y)"and"xys = zip xs' ys'"
by simp_all
with xs ys show ?thesis ..
qed
lemma semilattice_map2: "semilattice (map2 (\<^bold>*))"if"semilattice (\<^bold>*)"
for f (infixl \<open>\<^bold>*\<close> 70)
proof -
from that interpret semilattice f .
show ?thesis
proof
show "map2 (\<^bold>*) (map2 (\<^bold>*) xs ys) zs = map2 (\<^bold>*) xs (map2 (\<^bold>*) ys zs)"
for xs ys zs :: "'a list"
proof (induction "zip xs (zip ys zs)" arbitrary: xs ys zs) caseNil
from Nil [symmetric] show ?case
by auto
next case (Cons xyz xyzs)
from Cons.hyps(2) [symmetric] show ?case
by (rule zip_eq_ConsE) (erule zip_eq_ConsE,
auto intro: Cons.hyps(1) simp add: ac_simps)
qed
show "map2 (\<^bold>*) xs ys = map2 (\<^bold>*) ys xs"
for xs ys :: "'a list"
proof (induction "zip xs ys" arbitrary: xs ys) caseNil
then show ?case
by auto
next case (Cons xy xys)
from Cons.hyps(2) [symmetric] show ?case
by (rule zip_eq_ConsE) (auto intro: Cons.hyps(1) simp add: ac_simps)
qed
show "map2 (\<^bold>*) xs xs = xs"
for xs :: "'a list"
by (induction xs) simp_all
qed
qed
lemma pair_list_eqI:
assumes "map fst xs = map fst ys"and"map snd xs = map snd ys"
shows "xs = ys"
proof -
from assms(1) have "length xs = length ys" by (rule map_eq_imp_length_eq)
from this assms show ?thesis
by (induct xs ys rule: list_induct2) (simp_all add: prod_eqI)
qed
lemma hd_zip:
\<open>hd (zip xs ys) = (hd xs, hd ys)\<close> if \<open>xs \<noteq> []\<close> and \<open>ys \<noteq> []\<close>
using that by (cases xs; cases ys) simp_all
lemma last_zip:
\<open>last (zip xs ys) = (last xs, last ys)\<close> if \<open>xs \<noteq> []\<close> and \<open>ys \<noteq> []\<close> and \<open>length xs = length ys\<close>
using that by (cases xs rule: rev_cases; cases ys rule: rev_cases) simp_all
lemma list_all2_lengthD [intro?]: "list_all2 P xs ys \<Longrightarrow> length xs = length ys"
by (simp add: list_all2_iff)
lemma list_all2_Nil [iff, code]: "list_all2 P [] ys = (ys = [])"
by (simp add: list_all2_iff)
lemma list_all2_Nil2 [iff, code]: "list_all2 P xs [] = (xs = [])"
by (simp add: list_all2_iff)
lemma list_all2_Cons [iff, code]: "list_all2 P (x # xs) (y # ys) = (P x y \<and> list_all2 P xs ys)"
by (auto simp add: list_all2_iff)
lemma list_all2_Cons1: "list_all2 P (x # xs) ys = (\<exists>z zs. ys = z # zs \<and> P x z \<and> list_all2 P xs zs)"
by (cases ys) auto
lemma list_all2_Cons2: "list_all2 P xs (y # ys) = (\<exists>z zs. xs = z # zs \<and> P z y \<and> list_all2 P zs ys)"
by (cases xs) auto
lemma list_all2_induct
[consumes 1, case_names Nil Cons, induct set: list_all2]:
assumes P: "list_all2 P xs ys"
assumes Nil: "R [] []"
assumes Cons: "\<And>x xs y ys.
\<lbrakk>P x y; list_all2 P xs ys; R xs ys\<rbrakk> \<Longrightarrow> R (x # xs) (y # ys)"
shows "R xs ys"
using P
by (induct xs arbitrary: ys) (auto simp add: list_all2_Cons1 Nil Cons)
lemma list_all2_rev [iff]: "list_all2 P (rev xs) (rev ys) = list_all2 P xs ys"
by (simp add: list_all2_iff zip_rev cong: conj_cong)
lemma list_all2_rev1: "list_all2 P (rev xs) ys = list_all2 P xs (rev ys)"
by (subst list_all2_rev [symmetric]) simp
lemma list_all2_append1: "list_all2 P (xs @ ys) zs =
(\<exists>us vs. zs = us @ vs \<and> length us = length xs \<and> length vs = length ys \<and>
list_all2 P xs us \<and> list_all2 P ys vs)" (is "?lhs = ?rhs")
proof
assume ?lhs
then show ?rhs apply (rule_tac x = "take (length xs) zs" in exI) apply (rule_tac x = "drop (length xs) zs" in exI) apply (force split: nat_diff_split simp add: list_all2_iff zip_append1)
done
next
assume ?rhs
then show ?lhs
by (auto simp add: list_all2_iff)
qed
lemma list_all2_append2: "list_all2 P xs (ys @ zs) =
(\<exists>us vs. xs = us @ vs \<and> length us = length ys \<and> length vs = length zs \<and>
list_all2 P us ys \<and> list_all2 P vs zs)" (is "?lhs = ?rhs")
proof
assume ?lhs
then show ?rhs apply (rule_tac x = "take (length ys) xs" in exI) apply (rule_tac x = "drop (length ys) xs" in exI) apply (force split: nat_diff_split simp add: list_all2_iff zip_append2)
done
next
assume ?rhs
then show ?lhs
by (auto simp add: list_all2_iff)
qed
lemma list_all2_append: "length xs = length ys \<Longrightarrow>
list_all2 P (xs@us) (ys@vs) = (list_all2 P xs ys \<and> list_all2 P us vs)"
by (induct rule:list_induct2, simp_all)
lemma list_all2_appendI [intro?, trans]: "\<lbrakk> list_all2 P a b; list_all2 P c d \<rbrakk> \<Longrightarrow> list_all2 P (a@c) (b@d)"
by (simp add: list_all2_append list_all2_lengthD)
lemma list_all2_conv_all_nth: "list_all2 P xs ys =
(length xs = length ys \<and> (\<forall>i < length xs. P (xs!i) (ys!i)))"
by (force simp add: list_all2_iff set_zip)
lemma list_all2_trans:
assumes tr: "!!a b c. P1 a b \<Longrightarrow> P2 b c \<Longrightarrow> P3 a c"
shows "!!bs cs. list_all2 P1 as bs \<Longrightarrow> list_all2 P2 bs cs \<Longrightarrow> list_all2 P3 as cs"
(is "!!bs cs. PROP ?Q as bs cs")
proof (induct as)
fix x xs bs assume I1: "!!bs cs. PROP ?Q xs bs cs"
show "!!cs. PROP ?Q (x # xs) bs cs"
proof (induct bs)
fix y ys cs assume I2: "!!cs. PROP ?Q (x # xs) ys cs"
show "PROP ?Q (x # xs) (y # ys) cs"
by (induct cs) (auto intro: tr I1 I2)
qed simp
qed simp
lemma list_all2_all_nthI [intro?]: "length a = length b \<Longrightarrow> (\<And>n. n < length a \<Longrightarrow> P (a!n) (b!n)) \<Longrightarrow> list_all2 P a b"
by (simp add: list_all2_conv_all_nth)
lemma list_all2I: "\<forall>x \<in> set (zip a b). case_prod P x \<Longrightarrow> length a = length b \<Longrightarrow> list_all2 P a b"
by (simp add: list_all2_iff)
lemma list_all2_nthD: "\<lbrakk> list_all2 P xs ys; p < size xs \<rbrakk> \<Longrightarrow> P (xs!p) (ys!p)"
by (simp add: list_all2_conv_all_nth)
lemma list_all2_nthD2: "\<lbrakk>list_all2 P xs ys; p < size ys\<rbrakk> \<Longrightarrow> P (xs!p) (ys!p)"
by (frule list_all2_lengthD) (auto intro: list_all2_nthD)
lemma list_all2_map1: "list_all2 P (map f as) bs = list_all2 (\<lambda>x y. P (f x) y) as bs"
by (simp add: list_all2_conv_all_nth)
lemma list_all2_map2: "list_all2 P as (map f bs) = list_all2 (\<lambda>x y. P x (f y)) as bs"
by (auto simp add: list_all2_conv_all_nth)
lemma list_all2_refl [intro?]: "(\<And>x. P x x) \<Longrightarrow> list_all2 P xs xs"
by (simp add: list_all2_conv_all_nth)
lemma list_all2_update_cong: "\<lbrakk> list_all2 P xs ys; P x y \<rbrakk> \<Longrightarrow> list_all2 P (xs[i:=x]) (ys[i:=y])"
by (cases "i < length ys") (auto simp add: list_all2_conv_all_nth nth_list_update)
lemma list_all2_takeI [simp,intro?]: "list_all2 P xs ys \<Longrightarrow> list_all2 P (take n xs) (take n ys)"
proof (induct xs arbitrary: n ys) case (Cons x xs)
then show ?case
by (cases n) (auto simp: list_all2_Cons1)
qed auto
lemma list_all2_dropI [simp,intro?]: "list_all2 P xs ys \<Longrightarrow> list_all2 P (drop n xs) (drop n ys)"
proof (induct xs arbitrary: n ys) case (Cons x xs)
then show ?case
by (cases n) (auto simp: list_all2_Cons1)
qed auto
lemma list_all2_mono [intro?]: "list_all2 P xs ys \<Longrightarrow> (\<And>xs ys. P xs ys \<Longrightarrow> Q xs ys) \<Longrightarrow> list_all2 Q xs ys"
by (rule list.rel_mono_strong)
lemma list_all2_eq: "xs = ys \<longleftrightarrow> list_all2 (=) xs ys"
by (induct xs ys rule: list_induct2') auto
lemma product_nth:
assumes "n < length xs * length ys"
shows "List.product xs ys ! n = (xs ! (n div length ys), ys ! (n mod length ys))"
using assms proof (induct xs arbitrary: n) caseNil then show ?case by simp
next case (Cons x xs n)
then have "length ys > 0" by auto
with Cons show ?case
by (auto simp add: nth_append not_less le_mod_geq le_div_geq)
qed
lemma in_set_product_lists_length: "xs \<in> set (product_lists xss) \<Longrightarrow> length xs = length xss"
by (induct xss arbitrary: xs) auto
lemma product_lists_set: "set (product_lists xss) = {xs. list_all2 (\<lambda>x ys. x \<in> set ys) xs xss}" (is "?L = Collect ?R")
proof (intro equalityI subsetI, unfold mem_Collect_eq)
fix xs assume "xs \<in> ?L"
then have "length xs = length xss" by (rule in_set_product_lists_length)
from this \<open>xs \<in> ?L\<close> show "?R xs" by (induct xs xss rule: list_induct2) auto
next
fix xs assume "?R xs"
then show "xs \<in> ?L" by induct auto
qed
subsubsection \<open>\<^const>\<open>fold\<close> with natural argument order\<close>
lemma fold_simps [code]: \<comment> \<open>eta-expanded variant for generated code -- enables tail-recursion optimisation in Scala\<close> "fold f [] s = s" "fold f (x # xs) s = fold f xs (f x s)"
by simp_all
lemma fold_remove1_split: "\<lbrakk> \<And>x y. x \<in> set xs \<Longrightarrow> y \<in> set xs \<Longrightarrow> f x \<circ> f y = f y \<circ> f x;
x \<in> set xs \<rbrakk>
\<Longrightarrow> fold f xs = fold f (remove1 x xs) \<circ> f x"
by (induct xs) (auto simp add: comp_assoc)
lemma fold_cong [fundef_cong]: "a = b \<Longrightarrow> xs = ys \<Longrightarrow> (\<And>x. x \<in> set xs \<Longrightarrow> f x = g x)
\<Longrightarrow> fold f xs a = fold g ys b"
by (induct ys arbitrary: a b xs) simp_all
lemma fold_id: "(\<And>x. x \<in> set xs \<Longrightarrow> f x = id) \<Longrightarrow> fold f xs = id"
by (induct xs) simp_all
lemma fold_commute: "(\<And>x. x \<in> set xs \<Longrightarrow> h \<circ> g x = f x \<circ> h) \<Longrightarrow> h \<circ> fold g xs = fold f xs \<circ> h"
by (induct xs) (simp_all add: fun_eq_iff)
lemma fold_commute_apply:
assumes "\<And>x. x \<in> set xs \<Longrightarrow> h \<circ> g x = f x \<circ> h"
shows "h (fold g xs s) = fold f xs (h s)"
proof -
from assms have "h \<circ> fold g xs = fold f xs \<circ> h" by (rule fold_commute)
then show ?thesis by (simp add: fun_eq_iff)
qed
lemma fold_invariant: "\<lbrakk> \<And>x. x \<in> set xs \<Longrightarrow> Q x; P s; \<And>x s. Q x \<Longrightarrow> P s \<Longrightarrow> P (f x s) \<rbrakk>
\<Longrightarrow> P (fold f xs s)"
by (induct xs arbitrary: s) simp_all
lemma fold_append [simp]: "fold f (xs @ ys) = fold f ys \<circ> fold f xs"
by (induct xs) simp_all
lemma fold_map [code_unfold]: "fold g (map f xs) = fold (g \<circ> f) xs"
by (induct xs) simp_all
lemma fold_filter: "fold f (filter P xs) = fold (\<lambda>x. if P x then f x else id) xs"
by (induct xs) simp_all
lemma fold_rev: "(\<And>x y. x \<in> set xs \<Longrightarrow> y \<in> set xs \<Longrightarrow> f y \<circ> f x = f x \<circ> f y)
\<Longrightarrow> fold f (rev xs) = fold f xs"
by (induct xs) (simp_all add: fold_commute_apply fun_eq_iff)
lemma fold_inject:
assumes "\<And>w x y z. f w x = f y z \<longleftrightarrow> w = y \<and> x = z"and "\<And>x y. f x y \<noteq> a"and "\<And>x y. f x y \<noteq> b"
shows "fold f xs a = fold f ys b \<longleftrightarrow> xs = ys \<and> a = b"
by (induction xs ys rule: List.rev_induct2) (use assms(2,3,1) in auto)
text \<open>\<^const>\<open>Finite_Set.fold\<close> and \<^const>\<open>fold\<close>\<close>
lemma (in comp_fun_commute_on) fold_set_fold_remdups:
assumes "set xs \<subseteq> S"
shows "Finite_Set.fold f y (set xs) = fold f (remdups xs) y"
by (rule sym, use assms in \<open>induct xs arbitrary: y\<close>)
(simp_all add: insert_absorb fold_fun_left_comm)
lemma (in comp_fun_idem_on) fold_set_fold:
assumes "set xs \<subseteq> S"
shows "Finite_Set.fold f y (set xs) = fold f xs y"
by (rule sym, use assms in \<open>induct xs arbitrary: y\<close>) (simp_all add: fold_fun_left_comm)
lemma union_set_fold [code]: "set xs \<union> A = fold Set.insert xs A"
proof -
interpret comp_fun_idem Set.insert
by (fact comp_fun_idem_insert)
show ?thesis by (simp add: union_fold_insert fold_set_fold)
qed
lemma union_coset_filter [code]: "List.coset xs \<union> A = List.coset (List.filter (\<lambda>x. x \<notin> A) xs)"
by auto
lemma minus_set_fold [code]: "A - set xs = fold Set.remove xs A"
proof -
interpret comp_fun_idem Set.remove
by (fact comp_fun_idem_remove)
show ?thesis
by (simp add: minus_fold_remove [of _ A] fold_set_fold)
qed
lemma minus_coset_filter [code]: "A - List.coset xs = set (List.filter (\<lambda>x. x \<in> A) xs)"
by auto
lemma inter_set_filter [code]: "A \<inter> set xs = set (List.filter (\<lambda>x. x \<in> A) xs)"
by auto
lemma inter_coset_fold [code]: "A \<inter> List.coset xs = fold Set.remove xs A"
by (simp add: Diff_eq [symmetric] minus_set_fold)
definition abort_empty_set :: \<open>('a set \<Rightarrow> 'a) \<Rightarrow> 'a\<close>
where [simp]: \<open>abort_empty_set F = F {}\<close>
declare [[code abort: abort_empty_set]]
lemma (in semilattice_set) set_empty_abort [code]:
\<open>F (set []) = abort_empty_set F\<close>
by simp
lemma (in semilattice_set) set_eq_fold [code]:
\<open>F (set (x # xs)) = fold f xs x\<close>
proof -
interpret comp_fun_idem f
by standard (simp_all add: fun_eq_iff left_commute)
show ?thesis by (simp add: eq_fold fold_set_fold)
qed
lemma (in complete_lattice) Inf_set_fold: "Inf (set xs) = fold inf xs top"
proof -
interpret comp_fun_idem "inf :: 'a \<Rightarrow> 'a \<Rightarrow> 'a"
by (fact comp_fun_idem_inf)
show ?thesis by (simp add: Inf_fold_inf fold_set_fold inf_commute)
qed
declare Inf_set_fold [where 'a = "'a set", code]
lemma (in complete_lattice) Sup_set_fold: "Sup (set xs) = fold sup xs bot"
proof -
interpret comp_fun_idem "sup :: 'a \<Rightarrow> 'a \<Rightarrow> 'a"
by (fact comp_fun_idem_sup)
show ?thesis by (simp add: Sup_fold_sup fold_set_fold sup_commute)
qed
declare Sup_set_fold [where 'a = "'a set", code]
lemma (in complete_lattice) INF_set_fold: "\<Sqinter>(f ` set xs) = fold (inf \<circ> f) xs top"
using Inf_set_fold [of "map f xs"] by (simp add: fold_map)
lemma (in complete_lattice) SUP_set_fold: "\<Squnion>(f ` set xs) = fold (sup \<circ> f) xs bot"
using Sup_set_fold [of "map f xs"] by (simp add: fold_map)
subsubsection \<open>Fold variants: \<^const>\<open>foldr\<close> and \<^const>\<open>foldl\<close>\<close>
text \<open>Correspondence\<close>
lemma foldr_conv_fold [code_abbrev]: "foldr f xs = fold f (rev xs)"
by (induct xs) simp_all
lemma foldl_conv_fold: "foldl f s xs = fold (\<lambda>x s. f s x) xs s"
by (induct xs arbitrary: s) simp_all
lemma foldr_conv_foldl: \<comment> \<open>The ``Third Duality Theorem'' in Bird \& Wadler:\<close> "foldr f xs a = foldl (\<lambda>x y. f y x) a (rev xs)"
by (simp add: foldr_conv_fold foldl_conv_fold)
lemma foldl_conv_foldr: "foldl f a xs = foldr (\<lambda>x y. f y x) (rev xs) a"
by (simp add: foldr_conv_fold foldl_conv_fold)
lemma foldr_fold: "(\<And>x y. x \<in> set xs \<Longrightarrow> y \<in> set xs \<Longrightarrow> f y \<circ> f x = f x \<circ> f y)
\<Longrightarrow> foldr f xs = fold f xs"
unfolding foldr_conv_fold by (rule fold_rev)
lemma foldr_cong [fundef_cong]: "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"
by (auto simp add: foldr_conv_fold intro!: fold_cong)
lemma foldl_cong [fundef_cong]: "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"
by (auto simp add: foldl_conv_fold intro!: fold_cong)
lemma foldr_append [simp]: "foldr f (xs @ ys) a = foldr f xs (foldr f ys a)"
by (simp add: foldr_conv_fold)
lemma foldl_append [simp]: "foldl f a (xs @ ys) = foldl f (foldl f a xs) ys"
by (simp add: foldl_conv_fold)
lemma foldr_map [code_unfold]: "foldr g (map f xs) a = foldr (g \<circ> f) xs a"
by (simp add: foldr_conv_fold fold_map rev_map)
lemma foldr_filter: "foldr f (filter P xs) = foldr (\<lambda>x. if P x then f x else id) xs"
by (simp add: foldr_conv_fold rev_filter fold_filter)
lemma foldl_map [code_unfold]: "foldl g a (map f xs) = foldl (\<lambda>a x. g a (f x)) a xs"
by (simp add: foldl_conv_fold fold_map comp_def)
lemma foldl_inject:
assumes "\<And>w x y z. f w x = f y z \<longleftrightarrow> w = y \<and> x = z"and "\<And>x y. f x y \<noteq> a"and "\<And>x y. f x y \<noteq> b"
shows "foldl f a xs = foldl f b ys \<longleftrightarrow> a = b \<and> xs = ys"
by (induction xs ys rule: rev_induct2) (use assms(2,3,1) in auto)
lemma foldr_inject:
assumes "\<And>w x y z. f w x = f y z \<longleftrightarrow> w = y \<and> x = z"and "\<And>x y. f x y \<noteq> a"and "\<And>x y. f x y \<noteq> b"
shows "foldr f xs a = foldr f ys b \<longleftrightarrow> xs = ys \<and> a = b"
by (induction xs ys rule: list_induct2') (use assms(2,3,1) in auto)
lemma upt_eq_Cons_conv: "([i..<j] = x#xs) = (i < j \<and> i = x \<and> [i+1..<j] = xs)"
proof (induct j arbitrary: x xs) case (Suc j)
then show ?case
by (simp add: upt_rec)
qed simp
lemma upt_Suc_append: "i \<le> j \<Longrightarrow> [i..<(Suc j)] = [i..<j]@[j]"
\<comment> \<open>Only needed if \<open>upt_Suc\<close> is deleted from the simpset.\<close>
by simp
lemma upt_conv_Cons: "i < j \<Longrightarrow> [i..<j] = i # [Suc i..<j]"
by (simp add: upt_rec)
lemma upt_conv_Cons_Cons: \<comment> \<open>no precondition\<close> "m # n # ns = [m..<q] \<longleftrightarrow> n # ns = [Suc m..<q]"
proof (cases "m < q") case False then show ?thesis by simp
next case True then show ?thesis by (simp add: upt_conv_Cons)
qed
lemma upt_add_eq_append: "i<=j \<Longrightarrow> [i..<j+k] = [i..<j]@[j..<j+k]"
\<comment> \<open>LOOPS as a simprule, since \<open>j \<le> j\<close>.\<close>
by (induct k) auto
lemma take_upt [simp]: "i+m \<le> n \<Longrightarrow> take m [i..<n] = [i..<i+m]"
proof (induct m arbitrary: i) case (Suc m)
then show ?case
by (subst take_Suc_conv_app_nth) auto
qed simp
lemma drop_upt[simp]: "drop m [i..<j] = [i+m..<j]"
by(induct j) auto
lemma map_Suc_upt: "map Suc [m..<n] = [Suc m..<Suc n]"
by (induct n) auto
lemma map_add_upt: "map (\<lambda>i. i + n) [0..<m] = [n..<m + n]"
by (induct m) simp_all
lemma nth_map_upt: "i < n-m \<Longrightarrow> (map f [m..<n]) ! i = f(m+i)"
proof (induct n m arbitrary: i rule: diff_induct) case (3 x y)
then show ?case
by (metis add.commute length_upt less_diff_conv nth_map nth_upt)
qed auto
lemma map_decr_upt: "map (\<lambda>n. n - Suc 0) [Suc m..<Suc n] = [m..<n]"
by (induct n) simp_all
lemma map_upt_Suc: "map f [0 ..< Suc n] = f 0 # map (\<lambda>i. f (Suc i)) [0 ..< n]"
by (induct n arbitrary: f) auto
lemma nth_take_lemma: "k \<le> length xs \<Longrightarrow> k \<le> length ys \<Longrightarrow>
(\<And>i. i < k \<longrightarrow> xs!i = ys!i) \<Longrightarrow> take k xs = take k ys"
by (induct k arbitrary: xs ys) (simp_all add: take_Suc_conv_app_nth)
lemma list_all2_antisym: "\<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>
\<Longrightarrow> xs = ys"
by (simp add: list_all2_conv_all_nth nth_equalityI)
lemma take_equalityI: "(\<forall>i. take i xs = take i ys) \<Longrightarrow> xs = ys"
\<comment> \<open>The famous take-lemma.\<close>
by (metis length_take min.commute order_refl take_all)
lemma take_Cons': "take n (x # xs) = (if n = 0 then [] else x # take (n - 1) xs)"
by (cases n) simp_all
lemma drop_Cons': "drop n (x # xs) = (if n = 0 then x # xs else drop (n - 1) xs)"
by (cases n) simp_all
lemma nth_Cons': "(x # xs)!n = (if n = 0 then x else xs!(n - 1))"
by (cases n) simp_all
lemma take_Cons_numeral [simp]: "take (numeral v) (x # xs) = x # take (numeral v - 1) xs"
by (simp add: take_Cons')
lemma drop_Cons_numeral [simp]: "drop (numeral v) (x # xs) = drop (numeral v - 1) xs"
by (simp add: drop_Cons')
lemma nth_Cons_numeral [simp]: "(x # xs) ! numeral v = xs ! (numeral v - 1)"
by (simp add: nth_Cons')
lemma map_upt_eqI:
\<open>map f [m..<n] = xs\<close> if \<open>length xs = n - m\<close>
\<open>\<And>i. i < length xs \<Longrightarrow> xs ! i = f (m + i)\<close>
proof (rule nth_equalityI)
from \<open>length xs = n - m\<close> show \<open>length (map f [m..<n]) = length xs\<close>
by simp
next
fix i
assume \<open>i < length (map f [m..<n])\<close>
then have \<open>i < n - m\<close>
by simp
with that have \<open>xs ! i = f (m + i)\<close>
by simp
with \<open>i < n - m\<close> show \<open>map f [m..<n] ! i = xs ! i\<close>
by simp
qed
subsubsection \<open>\<open>upto\<close>: interval-list on \<^typ>\<open>int\<close>\<close>
function upto :: "int \<Rightarrow> int \<Rightarrow> int list" (\<open>(\<open>indent=1 notation=\<open>mixfix list interval\<close>\<close>[_../_])\<close>) where "upto i j = (if i \<le> j then i # [i+1..j] else [])"
by auto
termination
by(relation "measure(%(i::int,j). nat(j - i + 1))") auto
declare upto.simps[simp del]
lemmas upto_rec_numeral [simp] =
upto.simps[of "numeral m""numeral n"]
upto.simps[of "numeral m""- numeral n"]
upto.simps[of "- numeral m""numeral n"]
upto.simps[of "- numeral m""- numeral n"] for m n
lemma upto_rec2: "i \<le> j \<Longrightarrow> [i..j] = [i..j - 1]@[j]"
proof(induct "nat(j-i)" arbitrary: i j) case0 thus ?case by(simp add: upto.simps)
next case (Suc n)
hence "n = nat (j - (i + 1))""i < j" by linarith+
from this(2) Suc.hyps(1)[OF this(1)] Suc(2,3) upto_rec1 show ?case by simp
qed
lemma length_upto[simp]: "length [i..j] = nat(j - i + 1)"
by(induction i j rule: upto.induct) (auto simp: upto.simps)
lemma set_upto[simp]: "set[i..j] = {i..j}"
proof(induct i j rule:upto.induct) case (1 i j)
from this show ?case
unfolding upto.simps[of i j] by auto
qed
lemma nth_upto[simp]: "i + int k \<le> j \<Longrightarrow> [i..j] ! k = i + int k"
proof(induction i j arbitrary: k rule: upto.induct) case (1 i j)
then show ?case
by (auto simp add: upto_rec1 [of i j] nth_Cons')
qed
lemma upto_split1: "i \<le> j \<Longrightarrow> j \<le> k \<Longrightarrow> [i..k] = [i..j-1] @ [j..k]"
proof (induction j rule: int_ge_induct) case base thus ?case by (simp add: upto_rec1)
next case step thus ?case using upto_rec1 upto_rec2 by simp
qed
lemma upto_split2: "i \<le> j \<Longrightarrow> j \<le> k \<Longrightarrow> [i..k] = [i..j] @ [j+1..k]"
using upto_rec1 upto_rec2 upto_split1 by auto
lemma upto_split3: "\<lbrakk> i \<le> j; j \<le> k \<rbrakk> \<Longrightarrow> [i..k] = [i..j-1] @ j # [j+1..k]"
using upto_rec1 upto_split1 by auto
text\<open>Tail recursive version for code generation:\<close>
definition upto_aux :: "int \<Rightarrow> int \<Rightarrow> int list \<Rightarrow> int list" where "upto_aux i j js = [i..j] @ js"
lemma upto_aux_rec [code]: "upto_aux i j js = (if j<i then js else upto_aux i (j - 1) (j#js))"
by (simp add: upto_aux_def upto_rec2)
lemma successively_Cons: "successively P (x # xs) \<longleftrightarrow> xs = [] \<or> P x (hd xs) \<and> successively P xs"
by (cases xs) auto
lemma successively_cong [cong]:
assumes "\<And>x y. x \<in> set xs \<Longrightarrow> y \<in> set xs \<Longrightarrow> P x y \<longleftrightarrow> Q x y""xs = ys"
shows "successively P xs \<longleftrightarrow> successively Q ys"
unfolding assms(2) [symmetric] using assms(1)
by (induction xs) (auto simp: successively_Cons)
lemma successively_append_iff: "successively P (xs @ ys) \<longleftrightarrow>
successively P xs \<and> successively P ys \<and>
(xs = [] \<or> ys = [] \<or> P (last xs) (hd ys))"
by (induction xs) (auto simp: successively_Cons)
lemma successively_if_sorted_wrt: "sorted_wrt P xs \<Longrightarrow> successively P xs"
by (induction xs rule: induct_list012) auto
lemma successively_iff_sorted_wrt_strong:
assumes "\<And>x y z. x \<in> set xs \<Longrightarrow> y \<in> set xs \<Longrightarrow> z \<in> set xs \<Longrightarrow>
P x y \<Longrightarrow> P y z \<Longrightarrow> P x z"
shows "successively P xs \<longleftrightarrow> sorted_wrt P xs"
proof
assume "successively P xs"
from this and assms show "sorted_wrt P xs"
proof (induction xs rule: induct_list012) case (3 x y xs)
from "3.prems" have "P x y"
by auto
have IH: "sorted_wrt P (y # xs)"
using "3.prems"
by(intro "3.IH"(2) list.set_intros(2))(simp, blast intro: list.set_intros(2))
have "P x z"if asm: "z \<in> set xs" for z
proof -
from IH and asm have "P y z"
by auto
with \<open>P x y\<close> show "P x z"
using "3.prems" asm by auto
qed
with IH and \<open>P x y\<close> show ?case by auto
qed auto
qed (use successively_if_sorted_wrt in blast)
lemma successively_conv_sorted_wrt:
assumes "transp P"
shows "successively P xs \<longleftrightarrow> sorted_wrt P xs"
using assms unfolding transp_def
by (intro successively_iff_sorted_wrt_strong) blast
lemma successively_rev [simp]: "successively P (rev xs) \<longleftrightarrow> successively (\<lambda>x y. P y x) xs"
by (induction xs rule: remdups_adj.induct)
(auto simp: successively_append_iff successively_Cons)
lemma successively_map: "successively P (map f xs) \<longleftrightarrow> successively (\<lambda>x y. P (f x) (f y)) xs"
by (induction xs rule: induct_list012) auto
lemma successively_mono:
assumes "successively P xs"
assumes "\<And>x y. x \<in> set xs \<Longrightarrow> y \<in> set xs \<Longrightarrow> P x y \<Longrightarrow> Q x y"
shows "successively Q xs"
using assms by (induction Q xs rule: successively.induct) auto
lemma successively_altdef: "successively = (\<lambda>P. rec_list True (\<lambda>x xs b. case xs of [] \<Rightarrow> True | y # _ \<Rightarrow> P x y \<and> b))"
proof (intro ext)
fix P and xs :: "'a list"
show "successively P xs = rec_list True (\<lambda>x xs b. case xs of [] \<Rightarrow> True | y # _ \<Rightarrow> P x y \<and> b) xs"
by (induction xs) (auto simp: successively_Cons split: list.splits)
qed
subsubsection \<open>\<^const>\<open>distinct\<close> and \<^const>\<open>remdups\<close> and \<^const>\<open>remdups_adj\<close>\<close>
lemma finite_distinct_list: "finite A \<Longrightarrow> \<exists>xs. set xs = A \<and> distinct xs"
by (metis distinct_remdups finite_list set_remdups)
lemma remdups_eq_nil_iff [simp]: "(remdups x = []) = (x = [])"
by (induct x, auto)
lemma length_remdups_leq[iff]: "length(remdups xs) \<le> length xs"
by (induct xs) auto
lemma length_remdups_eq[iff]: "(length (remdups xs) = length xs) = (remdups xs = xs)"
proof (induct xs) case (Cons a xs)
then show ?case
by simp (metis Suc_n_not_le_n impossible_Cons length_remdups_leq)
qed auto
lemma remdups_filter: "remdups(filter P xs) = filter P (remdups xs)"
by (induct xs) auto
lemma distinct_map: "distinct(map f xs) = (distinct xs \<and> inj_on f (set xs))"
by (induct xs) auto
lemma distinct_map_filter: "distinct (map f xs) \<Longrightarrow> distinct (map f (filter P xs))"
by (induct xs) auto
lemma distinct_filter [simp]: "distinct xs \<Longrightarrow> distinct (filter P xs)"
by (induct xs) auto
lemma distinct_upt[simp]: "distinct[i..<j]"
by (induct j) auto
lemma distinct_upto[simp]: "distinct[i..j]"
proof (induction i j rule: upto.induct) case (1 i j)
then show ?case
by (simp add: upto.simps [of i])
qed
lemma distinct_take[simp]: "distinct xs \<Longrightarrow> distinct (take i xs)"
proof (induct xs arbitrary: i) case (Cons a xs)
then show ?case
by (metis Cons.prems append_take_drop_id distinct_append)
qed auto
lemma distinct_drop[simp]: "distinct xs \<Longrightarrow> distinct (drop i xs)"
proof (induct xs arbitrary: i) case (Cons a xs)
then show ?case
by (metis Cons.prems append_take_drop_id distinct_append)
qed auto
lemma distinct_list_update:
assumes d: "distinct xs"and a: "a \<notin> set xs - {xs!i}"
shows "distinct (xs[i:=a])"
proof (cases "i < length xs") case True
with a have anot: "a \<notin> set (take i xs @ xs ! i # drop (Suc i) xs) - {xs!i}"
by simp (metis in_set_dropD in_set_takeD)
show ?thesis
proof (cases "a = xs!i") case True
with d show ?thesis
by auto
next case False
have "set (take i xs) \<inter> set (drop (Suc i) xs) = {}"
by (metis True d disjoint_insert(1) distinct_append id_take_nth_drop list.set(2))
then show ?thesis
using d False anot \<open>i < length xs\<close> by (simp add: upd_conv_take_nth_drop)
qed
next case False with d show ?thesis by (auto simp: list_update_beyond)
qed
lemma distinct_concat_rev[simp]: "distinct (concat (rev xs)) = distinct (concat xs)"
by (induction xs) auto
lemma distinct_concat: "\<lbrakk> distinct xs;
\<And> ys. ys \<in> set xs \<Longrightarrow> distinct ys;
\<And> ys zs. \<lbrakk> ys \<in> set xs ; zs \<in> set xs ; ys \<noteq> zs \<rbrakk> \<Longrightarrow> set ys \<inter> set zs = {}
\<rbrakk> \<Longrightarrow> distinct (concat xs)"
by (induct xs) auto
text \<open>An iff-version of @{thm distinct_concat} is available further down as \<open>distinct_concat_iff\<close>.\<close>
text \<open>It is best to avoid the following indexed version of distinct, but sometimes it is useful.\<close>
lemma distinct_conv_nth: "distinct xs = (\<forall>i < size xs. \<forall>j < size xs. i \<noteq> j \<longrightarrow> xs!i \<noteq> xs!j)"
proof (induct xs) case (Cons x xs)
show ?case apply (auto simp add: Cons nth_Cons less_Suc_eq_le split: nat.split_asm) apply (metis Suc_leI in_set_conv_nth length_pos_if_in_set lessI less_imp_le_nat less_nat_zero_code) apply (metis Suc_le_eq)
done
qed auto
lemma distinct_card: "distinct xs \<Longrightarrow> card (set xs) = size xs"
by (induct xs) auto
lemma card_distinct: "card (set xs) = size xs \<Longrightarrow> distinct xs"
proof (induct xs) case (Cons x xs)
show ?case
proof (cases "x \<in> set xs") case False with Cons show ?thesis by simp
next case True with Cons.prems
have "card (set xs) = Suc (length xs)"
by (simp add: card_insert_if split: if_split_asm)
moreover have "card (set xs) \<le> length xs" by (rule card_length)
ultimately have False by simp
thus ?thesis ..
qed
qed simp
lemma distinct_length_filter: "distinct xs \<Longrightarrow> length (filter P xs) = card ({x. P x} Int set xs)"
by (induct xs) (auto)
lemma not_distinct_decomp: "\<not> distinct ws \<Longrightarrow> \<exists>xs ys zs y. ws = xs@[y]@ys@[y]@zs"
proof (induct n == "length ws" arbitrary:ws) case (Suc n ws)
then show ?case
using length_Suc_conv [of ws n] apply (auto simp: eq_commute) apply (metis append_Nil in_set_conv_decomp_first)
by (metis append_Cons)
qed simp
lemma not_distinct_conv_prefix:
defines "dec as xs y ys \<equiv> y \<in> set xs \<and> distinct xs \<and> as = xs @ y # ys"
shows "\<not>distinct as \<longleftrightarrow> (\<exists>xs y ys. dec as xs y ys)" (is"?L = ?R")
proof
assume "?L"then show "?R"
proof (induct "length as" arbitrary: as rule: less_induct)
case less
obtain xs ys zs y where decomp: "as = (xs @ y # ys) @ y # zs"
using not_distinct_decomp[OF less.prems] by auto
show ?case
proof (cases"distinct (xs @ y # ys)")
case True with decomp have "dec as (xs @ y # ys) y zs"by (simp add: dec_def) then show ?thesis by blast
next
case False with less decomp obtain xs' y' ys' where "dec (xs @ y # ys) xs' y' ys'" by atomize_elim auto with decomp have "dec as xs' y' (ys' @ y # zs)"by (simp add: dec_def) then show ?thesis by blast
qed
qed
qed (auto simp: dec_def)
lemma distinct_product_lists:
assumes "\<forall>xs \<in> set xss. distinct xs"
shows "distinct (product_lists xss)"
using assms proof (induction xss)
case (Cons xs xss) note * = this then show ?case
proof (cases"product_lists xss")
case Nilthen show ?thesis by (induct xs) simp_all
next
case (Cons ps pss) with * show ?thesis by (auto intro!: inj_onI distinct_concat simp add: distinct_map)
qed
qed simp
lemma length_remdups_concat: "length (remdups (concat xss)) = card (\<Union>xs\<in>set xss. set xs)" by (simp add: distinct_card [symmetric])
lemma length_remdups_card_conv: "length(remdups xs) = card(set xs)"
proof -
have xs: "concat[xs] = xs"by simp from length_remdups_concat[of"[xs]"] show ?thesis unfolding xs by simp
qed
lemma distinct_butlast:
assumes "distinct xs"
shows "distinct (butlast xs)"
proof (cases"xs = []")
case False from \<open>xs \<noteq> []\<close> obtain ys y where "xs = ys @ [y]"by (cases xs rule: rev_cases) auto with \<open>distinct xs\<close> show ?thesis by simp
qed (auto)
lemma remdups_map_remdups: "remdups (map f (remdups xs)) = remdups (map f xs)" by (induct xs) simp_all
lemma distinct_zipI1:
assumes "distinct xs"
shows "distinct (zip xs ys)"
proof (rule zip_obtain_same_length)
fix xs' :: "'a list" and ys' :: "'b list" and n
assume "length xs' = length ys'"
assume "xs' = take n xs" with assms have "distinct xs'"by simp with \<open>length xs' = length ys'\<close> show "distinct (zip xs' ys')" by (induct xs' ys' rule: list_induct2) (auto elim: in_set_zipE)
qed
lemma distinct_zipI2:
assumes "distinct ys"
shows "distinct (zip xs ys)"
proof (rule zip_obtain_same_length)
fix xs' :: "'b list" and ys' :: "'a list" and n
assume "length xs' = length ys'"
assume "ys' = take n ys" with assms have "distinct ys'"by simp with \<open>length xs' = length ys'\<close> show "distinct (zip xs' ys')" by (induct xs' ys' rule: list_induct2) (auto elim: in_set_zipE)
qed
lemma set_take_disj_set_drop_if_distinct: "distinct vs \<Longrightarrow> i \<le> j \<Longrightarrow> set (take i vs) \<inter> set (drop j vs) = {}" by (auto simp: in_set_conv_nth distinct_conv_nth)
(* The next two lemmas help Sledgehammer. *)
lemma distinct_singleton: "distinct [x]"by simp
lemma distinct_length_2_or_more: "distinct (a # b # xs) \<longleftrightarrow> (a \<noteq> b \<and> distinct (a # xs) \<and> distinct (b # xs))" by force
lemma remdups_adj_altdef: "(remdups_adj xs = ys) \<longleftrightarrow>
(\<exists>f::nat => nat. mono f \<and> f ` {0 ..< size xs} = {0 ..< size ys}
\<and> (\<forall>i < size xs. xs!i = ys!(f i))
\<and> (\<forall>i. i + 1 < size xs \<longrightarrow> (xs!i = xs!(i+1) \<longleftrightarrow> f i = f(i+1))))" (is "?L \<longleftrightarrow> (\<exists>f. ?p f xs ys)")
proof
assume ?L then show "\<exists>f. ?p f xs ys"
proof (induct xs arbitrary: ys rule: remdups_adj.induct)
case (1 ys)
thus ?case by (intro exI[of _ id]) (auto simp: mono_def)
next
case (2 x ys)
thus ?case by (intro exI[of _ id]) (auto simp: mono_def)
next
case (3 x1 x2 xs ys) let ?xs = "x1 # x2 # xs" let ?cond = "x1 = x2"
define zs where "zs = remdups_adj (x2 # xs)" from3(1-2)[of zs]
obtain f where p: "?p f (x2 # xs) zs" unfolding zs_def by (cases ?cond) auto then have f0: "f 0 = 0" by (intro mono_image_least[where f=f]) blast+ from p have mono: "mono f"and f_xs_zs: "f ` {0..<length (x2 # xs)} = {0..<length zs}"by auto
have ys: "ys = (if x1 = x2 then zs else x1 # zs)"
unfolding 3(3)[symmetric] zs_def by auto
have zs0: "zs ! 0 = x2" unfolding zs_def by (induct xs) auto
have zsne: "zs \<noteq> []" unfolding zs_def by (induct xs) auto let ?Succ = "if ?cond then id else Suc" let ?x1 = "if ?cond then id else Cons x1" let ?f = "\<lambda> i. if i = 0 then 0 else ?Succ (f (i - 1))"
have ys: "ys = ?x1 zs" unfolding ys by (cases ?cond, auto)
have mono: "mono ?f" using \<open>mono f\<close> unfolding mono_def by auto
show ?case unfolding ys
proof (intro exI[of _ ?f] conjI allI impI)
show "mono ?f"by fact
next
fix i assume i: "i < length ?xs" with p show "?xs ! i = ?x1 zs ! (?f i)" using zs0 by auto
next
fix i assume i: "i + 1 < length ?xs" with p show "(?xs ! i = ?xs ! (i + 1)) = (?f i = ?f (i + 1))" by (cases i) (auto simp: f0)
next
have id: "{0 ..< length (?x1 zs)} = insert 0 (?Succ ` {0 ..< length zs})"
using zsne by (cases ?cond, auto)
{ fix i assume "i < Suc (length xs)"
hence "Suc i \<in> {0..<Suc (Suc (length xs))} \<inter> Collect ((<) 0)"by auto from imageI[OF this, of"\<lambda>i. ?Succ (f (i - Suc 0))"]
have "?Succ (f i) \<in> (\<lambda>i. ?Succ (f (i - Suc 0))) ` ({0..<Suc (Suc (length xs))} \<inter> Collect ((<) 0))"by auto
} then show "?f ` {0 ..< length ?xs} = {0 ..< length (?x1 zs)}"
unfolding id f_xs_zs[symmetric] by auto
qed
qed
next
assume "\<exists> f. ?p f xs ys" then show ?L
proof (induct xs arbitrary: ys rule: remdups_adj.induct)
case 1then show ?case by auto
next
case (2 x) then obtain f where f_img: "f ` {0 ..< size [x]} = {0 ..< size ys}" and f_nth: "\<And>i. i < size [x] \<Longrightarrow> [x]!i = ys!(f i)" by blast
have "length ys = card (f ` {0 ..< size [x]})"
using f_img by auto then have *: "length ys = 1"by auto then have "f 0 = 0" using f_img by auto with * show ?case using f_nth by (cases ys) auto
next
case (3 x1 x2 xs) from"3.prems" obtain f where f_mono: "mono f" and f_img: "f ` {0..<length (x1 # x2 # xs)} = {0..<length ys}" and f_nth: "\<And>i. i < length (x1 # x2 # xs) \<Longrightarrow> (x1 # x2 # xs) ! i = ys ! f i" "\<And>i. i + 1 < length (x1 # x2 #xs) \<Longrightarrow>
((x1 # x2 # xs) ! i = (x1 # x2 # xs) ! (i + 1)) = (f i = f (i + 1))" by blast
show ?case
proof cases
assume "x1 = x2"
let ?f' = "f \<circ> Suc"
have "remdups_adj (x1 # xs) = ys"
proof (intro "3.hyps" exI conjI impI allI)
show "mono ?f'"
using f_mono by (simp add: mono_iff_le_Suc)
next
have "?f' ` {0 ..< length (x1 # xs)} = f ` {Suc 0 ..< length (x1 # x2 # xs)}"
using less_Suc_eq_0_disj by auto
also have "\<dots> = f ` {0 ..< length (x1 # x2 # xs)}"
proof -
have "f 0 = f (Suc 0)" using \<open>x1 = x2\<close> f_nth[of0] by simp then show ?thesis
using less_Suc_eq_0_disj by auto
qed
also have "\<dots> = {0 ..< length ys}"by fact
finally show "?f' ` {0 ..< length (x1 # xs)} = {0 ..< length ys}" .
qed (insert f_nth[of"Suc i"for i], auto simp: \<open>x1 = x2\<close>) then show ?thesis using \<open>x1 = x2\<close> by simp
next
assume "x1 \<noteq> x2"
have two: "Suc (Suc 0) \<le> length ys"
proof -
have "2 = card {f 0, f 1}" using \<open>x1 \<noteq> x2\<close> f_nth[of0] by auto
also have "\<dots> \<le> card (f ` {0..< length (x1 # x2 # xs)})" by (rule card_mono) auto
finally show ?thesis using f_img by simp
qed
have "f 0 = 0" using f_mono f_img by (rule mono_image_least) simp
have "f (Suc 0) = Suc 0"
proof (rule ccontr)
assume "f (Suc 0) \<noteq> Suc 0" then have "Suc 0 < f (Suc 0)" using f_nth[of0] \<open>x1 \<noteq> x2\<close> \<open>f 0 = 0\<close> by auto then have "\<And>i. Suc 0 < f (Suc i)"
using f_mono by (meson Suc_le_mono le0 less_le_trans monoD) then have "Suc 0 \<noteq> f i"for i using \<open>f 0 = 0\<close> by (cases i) fastforce+ then have "Suc 0 \<notin> f ` {0 ..< length (x1 # x2 # xs)}"by auto then show False using f_img two by auto
qed
obtain ys' where "ys = x1 # x2 # ys'"
using two f_nth[of0] f_nth[of1] by (auto simp: Suc_le_length_iff \<open>f 0 = 0\<close> \<open>f (Suc 0) = Suc 0\<close>)
have Suc0_le_f_Suc: "Suc 0 \<le> f (Suc i)"for i by (metis Suc_le_mono \<open>f (Suc 0) = Suc 0\<close> f_mono le0 mono_def)
define f' where "f' x = f (Suc x) - 1" for x
have f_Suc: "f (Suc i) = Suc (f' i)"for i
using Suc0_le_f_Suc[of i] by (auto simp: f'_def)
have "remdups_adj (x2 # xs) = (x2 # ys')"
proof (intro "3.hyps" exI conjI impI allI)
show "mono f'"
using Suc0_le_f_Suc f_mono by (auto simp: f'_def mono_iff_le_Suc le_diff_iff)
next
have "f' ` {0 ..< length (x2 # xs)} = (\<lambda>x. f x - 1) ` {0 ..< length (x1 # x2 #xs)}" by (auto simp: f'_def \<open>f 0 = 0\<close> \<open>f (Suc 0) = Suc 0\<close> image_def Bex_def less_Suc_eq_0_disj)
also have "\<dots> = (\<lambda>x. x - 1) ` f ` {0 ..< length (x1 # x2 #xs)}" by (auto simp: image_comp)
also have "\<dots> = (\<lambda>x. x - 1) ` {0 ..< length ys}" by (simp only: f_img)
also have "\<dots> = {0 ..< length (x2 # ys')}"
using \<open>ys = _\<close> by (fastforce intro: rev_image_eqI)
finally show "f' ` {0 ..< length (x2 # xs)} = {0 ..< length (x2 # ys')}" .
qed (insert f_nth[of"Suc i"for i] \<open>x1 \<noteq> x2\<close>, auto simp add: f_Suc \<open>ys = _\<close>) then show ?case using \<open>ys = _\<close> \<open>x1 \<noteq> x2\<close> by simp
qed
qed
qed
lemma hd_remdups_adj[simp]: "hd (remdups_adj xs) = hd xs" by (induction xs rule: remdups_adj.induct) simp_all
lemma remdups_adj_Cons: "remdups_adj (x # xs) =
(case remdups_adj xs of [] \<Rightarrow> [x] | y # xs \<Rightarrow> if x = y then y # xs else x # y # xs)" by (induct xs arbitrary: x) (auto split: list.splits)
lemma remdups_adj_append_two: "remdups_adj (xs @ [x,y]) = remdups_adj (xs @ [x]) @ (if x = y then [] else [y])" by (induct xs rule: remdups_adj.induct, simp_all)
lemma remdups_adj_adjacent: "Suc i < length (remdups_adj xs) \<Longrightarrow> remdups_adj xs ! i \<noteq> remdups_adj xs ! Suc i"
proof (induction xs arbitrary: i rule: remdups_adj.induct)
case (3 x y xs i)
thus ?case by (cases i, cases"x = y") (simp, auto simp: hd_conv_nth[symmetric])
qed simp_all
lemma remdups_adj_map_injective:
assumes "inj f"
shows "remdups_adj (map f xs) = map f (remdups_adj xs)" by (induct xs rule: remdups_adj.induct) (auto simp add: injD[OF assms])
lemma remdups_adj_replicate: "remdups_adj (replicate n x) = (if n = 0 then [] else [x])" by (induction n) (auto simp: remdups_adj_Cons)
lemma remdups_upt [simp]: "remdups [m..<n] = [m..<n]"
proof (cases"m \<le> n")
case Falsethen show ?thesis by simp
next
case Truethen obtain q where "n = m + q" by (auto simp add: le_iff_add)
moreover have "remdups [m..<m + q] = [m..<m + q]" by (induct q) simp_all
ultimately show ?thesis by simp
qed
lemma successively_remdups_adjI: "successively P xs \<Longrightarrow> successively P (remdups_adj xs)" by (induction xs rule: remdups_adj.induct) (auto simp: successively_Cons)
lemma successively_remdups_adj_iff: "(\<And>x. x \<in> set xs \<Longrightarrow> P x x) \<Longrightarrow>
successively P (remdups_adj xs) \<longleftrightarrow> successively P xs" by (induction xs rule: remdups_adj.induct)(auto simp: successively_Cons)
lemma successively_conv_nth: "successively P xs \<longleftrightarrow> (\<forall>i. Suc i < length xs \<longrightarrow> P (xs ! i) (xs ! Suc i))" by (induction P xs rule: successively.induct)
(force simp: nth_Cons split: nat.splits)+
lemma successively_nth: "successively P xs \<Longrightarrow> Suc i < length xs \<Longrightarrow> P (xs ! i) (xs ! Suc i)"
unfolding successively_conv_nth by blast
lemma distinct_adj_conv_nth: "distinct_adj xs \<longleftrightarrow> (\<forall>i. Suc i < length xs \<longrightarrow> xs ! i \<noteq> xs ! Suc i)" by (simp add: distinct_adj_def successively_conv_nth)
lemma distinct_adj_nth: "distinct_adj xs \<Longrightarrow> Suc i < length xs \<Longrightarrow> xs ! i \<noteq> xs ! Suc i"
unfolding distinct_adj_conv_nth by blast
lemma remdups_adj_Cons': "remdups_adj (x # xs) = x # remdups_adj (dropWhile (\<lambda>y. y = x) xs)" by (induction xs) auto
lemma tl_remdups_adj: "ys \<noteq> [] \<Longrightarrow> tl (remdups_adj ys) = remdups_adj (dropWhile (\<lambda>x. x = hd ys) (tl ys))" by (cases ys) (simp_all add: remdups_adj_Cons')
lemma remdups_adj_append_dropWhile: "remdups_adj (xs @ y # ys) = remdups_adj (xs @ [y]) @ remdups_adj (dropWhile (\<lambda>x. x = y) ys)" by (subst remdups_adj_append) (simp add: tl_remdups_adj)
lemma remdups_adj_append':
assumes "xs = [] \<or> ys = [] \<or> last xs \<noteq> hd ys"
shows "remdups_adj (xs @ ys) = remdups_adj xs @ remdups_adj ys"
proof -
have ?thesis if [simp]: "xs \<noteq> []""ys \<noteq> []"and"last xs \<noteq> hd ys"
proof -
obtain x xs' where xs: "xs = xs' @ [x]" by (cases xs rule: rev_cases) auto
have "remdups_adj (xs' @ x # ys) = remdups_adj (xs' @ [x]) @ remdups_adj ys"
using \<open>last xs \<noteq> hd ys\<close> unfolding xs by (metis (full_types) dropWhile_eq_self_iff last_snoc remdups_adj_append_dropWhile)
thus ?thesis by (simp add: xs)
qed
thus ?thesis using assms by (cases"xs = []"; cases"ys = []") auto
qed
lemma remdups_adj_append'': "xs \<noteq> []
\<Longrightarrow> remdups_adj (xs @ ys) = remdups_adj xs @ remdups_adj (dropWhile (\<lambda>y. y = last xs) ys)" by (induction xs rule: remdups_adj.induct) (auto simp: remdups_adj_Cons')
lemma remdups_filter_last: "last [x\<leftarrow>remdups xs. P x] = last [x\<leftarrow>xs. P x]" by (induction xs, auto simp: filter_empty_conv)
lemma remdups_append: "set xs \<subseteq> set ys \<Longrightarrow> remdups (xs @ ys) = remdups ys" by (induction xs, simp_all)
lemma remdups_concat: "remdups (concat (remdups xs)) = remdups (concat xs)"
proof (induction xs)
case Nil then show ?case by simp
next
case (Cons a xs)
show ?case
proof (cases"a \<in> set xs")
case True then have "remdups (concat xs) = remdups (a @ concat xs)" by (metis remdups_append concat.simps(2) insert_absorb set_simps(2) set_append set_concat sup_ge1) then show ?thesis by (simp add: Cons True)
next
case False then show ?thesis by (metis Cons remdups_append2 concat.simps(2) remdups.simps(2))
qed
qed
subsection \<open>@{const distinct_adj}\<close>
lemma distinct_adj_Nil [simp]: "distinct_adj []" and distinct_adj_singleton [simp]: "distinct_adj [x]" and distinct_adj_Cons_Cons [simp]: "distinct_adj (x # y # xs) \<longleftrightarrow> x \<noteq> y \<and> distinct_adj (y # xs)" by (auto simp: distinct_adj_def)
lemma distinct_adj_Cons: "distinct_adj (x # xs) \<longleftrightarrow> xs = [] \<or> x \<noteq> hd xs \<and> distinct_adj xs" by (cases xs) auto
lemma distinct_adj_ConsD: "distinct_adj (x # xs) \<Longrightarrow> distinct_adj xs" by (cases xs) auto
lemma distinct_adj_mapI: "distinct_adj xs \<Longrightarrow> inj_on f (set xs) \<Longrightarrow> distinct_adj (map f xs)"
unfolding distinct_adj_def successively_map by (erule successively_mono) (auto simp: inj_on_def)
lemma distinct_adj_mapD: "distinct_adj (map f xs) \<Longrightarrow> distinct_adj xs"
unfolding distinct_adj_def successively_map by (erule successively_mono) auto
lemma distinct_adj_map_iff: "inj_on f (set xs) \<Longrightarrow> distinct_adj (map f xs) \<longleftrightarrow> distinct_adj xs"
using distinct_adj_mapD distinct_adj_mapI by blast
lemma distinct_adj_conv_length_remdups_adj: "distinct_adj xs \<longleftrightarrow> length (remdups_adj xs) = length xs"
proof (induction xs rule: remdups_adj.induct)
case (3 x y xs)
thus ?case
using remdups_adj_length[of"y # xs"] by auto
qed auto
text\<open>This isall one should need to know about union:\<close>
lemma set_union[simp]: "set (List.union xs ys) = set xs \<union> set ys"
unfolding List.union_def by(induct xs arbitrary: ys) simp_all
lemma find_None_iff: "List.find P xs = None \<longleftrightarrow> \<not> (\<exists>x. x \<in> set xs \<and> P x)"
proof (induction xs)
case Nil thus ?case by simp
next
case (Cons x xs) thus ?case by (fastforce split: if_splits)
qed
lemma find_Some_iff: "List.find P xs = Some x \<longleftrightarrow>
(\<exists>i<length xs. P (xs!i) \<and> x = xs!i \<and> (\<forall>j<i. \<not> P (xs!j)))"
proof (induction xs)
case Nil thus ?case by simp
next
case (Cons x xs) thus ?case
apply(auto simp: nth_Cons' split: if_splits)
using diff_Suc_1 less_Suc_eq_0_disj by fastforce
qed
lemma find_cong[fundef_cong]:
assumes "xs = ys"and"\<And>x. x \<in> set ys \<Longrightarrow> P x = Q x"
shows "List.find P xs = List.find Q ys"
proof (cases"List.find P xs")
case None thus ?thesis by (metis find_None_iff assms)
next
case (Some x)
hence "List.find Q ys = Some x" using assms by (auto simp add: find_Some_iff)
thus ?thesis using Some by auto
qed
lemma find_dropWhile: "List.find P xs = (case dropWhile (Not \<circ> P) xs of [] \<Rightarrow> None
| x # _ \<Rightarrow> Some x)" by (induct xs) simp_all
lemma count_list_rev[simp]: "count_list (rev xs) x = count_list xs x" by (induction xs) auto
lemma sum_count_set: "set xs \<subseteq> X \<Longrightarrow> finite X \<Longrightarrow> sum (count_list xs) X = length xs"
proof (induction xs arbitrary: X)
case (Cons x xs) then show ?case
using sum.remove [of X x "count_list xs"] by (auto simp: sum.If_cases simp flip: diff_eq)
qed simp
lemma count_list_Suc_split_first:
assumes "count_list xs x = Suc n"
shows "\<exists> pref rest. xs = pref @ x # rest \<and> x \<notin> set pref \<and> count_list rest x = n"
proof - let ?pref = "takeWhile (\<lambda>u. u \<noteq> x) xs" let ?rest = "drop (length ?pref) xs"
have "x \<in> set xs" using assms count_notin by fastforce
hence rest: "?rest \<noteq> [] \<and> hd ?rest = x" by (metis (mono_tags, lifting) append_Nil2 dropWhile_eq_drop hd_dropWhile
takeWhile_dropWhile_id takeWhile_eq_all_conv)
have 1: "x \<notin> set ?pref"by (metis (full_types) set_takeWhileD)
have 2: "xs = ?pref @ x # tl ?rest" by (metis rest append_eq_conv_conj hd_Cons_tl takeWhile_eq_take)
have "count_list (tl ?rest) x = n"
using assms rest 12 count_notin count_list_append[of ?pref "x # tl ?rest" x] by simp with12 show ?thesis by blast
qed
lemma count_list_eq_length_filter: "count_list xs y = length(filter ((=) y) xs)" by (induction xs) auto
lemma split_list_cycles: "\<exists>pref xss. xs = pref @ concat xss \<and> x \<notin> set pref \<and> (\<forall>ys \<in> set xss. \<exists>zs. ys = x # zs)"
proof (induction "count_list xs x" arbitrary: xs)
case 0
show ?case using 0[symmetric] concat.simps(1) count_list_0_iff by fastforce
next
case (Suc n) from Suc.hyps(2) obtain pref rest where
*: "xs = pref @ x # rest""x \<notin> set pref""count_list rest x = n" by (metis count_list_Suc_split_first) from Suc.hyps(1)[OF *(3)[symmetric]] obtain pref1 xss where
**: "rest = pref1 @ concat xss""x \<notin> set pref1""\<forall>ys\<in>set xss. \<exists>zs. ys = x # zs" by blast let ?xss = "(x # pref1) # xss"
have "xs = pref @ concat ?xss \<and> x \<notin> set pref \<and> (\<forall>ys \<in> set ?xss. \<exists>zs. ys = x # zs)"
using *(1,2) ** by auto
thus ?case by blast
qed
lemma extract_SomeE:
"List.extract P xs = Some (ys, y, zs) \<Longrightarrow>
xs = ys @ y # zs \<and> P y \<and> \<not> (\<exists> y \<in> set ys. P y)"
by(auto simp: extract_def dropWhile_eq_Cons_conv split: list.splits)
lemma extract_Some_iff:
"List.extract P xs = Some (ys, y, zs) \<longleftrightarrow>
xs = ys @ y # zs \<and> P y \<and> \<not> (\<exists> y \<in> set ys. P y)"
by(auto simp: extract_def dropWhile_eq_Cons_conv dest: set_takeWhileD split: list.splits)
lemma extract_Cons_code [code]:
"List.extract P (x # xs) = (if P x then Some ([], x, xs) else
(case List.extract P xs of
None \<Rightarrow> None |
Some (ys, y, zs) \<Rightarrow> Some (x#ys, y, zs)))"
by(auto simp add: extract_def comp_def split: list.splits)
(metis dropWhile_eq_Nil_conv list.distinct(1))
lemma count_list_remove1[simp]:
"count_list (remove1 a xs) b = count_list xs b - (if a=b then 1 else 0)"
by(induction xs) auto
lemma remove1_append:
"remove1 x (xs @ ys) =
(if x \<in> set xs then remove1 x xs @ ys else xs @ remove1 x ys)"
by (induct xs) auto
lemma remove1_commute: "remove1 x (remove1 y zs) = remove1 y (remove1 x zs)"
by (induct zs) auto
lemma in_set_remove1[simp]:
"a \<noteq> b \<Longrightarrow> a \<in> set(remove1 b xs) = (a \<in> set xs)"
by (induct xs) auto
lemma set_remove1_subset: "set(remove1 x xs) \<subseteq> set xs"
by (induct xs) auto
lemma set_remove1_eq [simp]: "distinct xs \<Longrightarrow> set(remove1 x xs) = set xs - {x}"
by (induct xs) auto
lemma length_remove1:
"length(remove1 x xs) = (if x \<in> set xs then length xs - 1 else length xs)"
by (induct xs) (auto dest!:length_pos_if_in_set)
lemma remove1_filter_not[simp]:
"\<not> P x \<Longrightarrow> remove1 x (filter P xs) = filter P xs"
by(induct xs) auto
lemma filter_remove1:
"filter Q (remove1 x xs) = remove1 x (filter Q xs)"
by (induct xs) auto
lemma notin_set_remove1[simp]: "x \<notin> set xs \<Longrightarrow> x \<notin> set(remove1 y xs)"
by(insert set_remove1_subset) fast
lemma distinct_remove1[simp]: "distinct xs \<Longrightarrow> distinct(remove1 x xs)"
by (induct xs) simp_all
lemma remove1_remdups:
"distinct xs \<Longrightarrow> remove1 x (remdups xs) = remdups (remove1 x xs)"
by (induct xs) simp_all
lemma remove1_idem: "x \<notin> set xs \<Longrightarrow> remove1 x xs = xs"
by (induct xs) simp_all
lemma remove1_split:
"a \<in> set xs \<Longrightarrow> remove1 a xs = ys \<longleftrightarrow> (\<exists>ls rs. xs = ls @ a # rs \<and> a \<notin> set ls \<and> ys = ls @ rs)"
by (metis remove1.simps(2) remove1_append split_list_first)
lemma foldr_fold_remove1[code_unfold]: "foldr remove1 = fold remove1"
using foldr_fold[of _ remove1] remove1_commute by fastforce
lemma removeAll_filter_not_eq:
"removeAll x = filter (\<lambda>y. x \<noteq> y)"
proof
fix xs
show "removeAll x xs = filter (\<lambda>y. x \<noteq> y) xs"
by (induct xs) auto
qed
lemma removeAll_append[simp]:
"removeAll x (xs @ ys) = removeAll x xs @ removeAll x ys"
by (induct xs) auto
lemma removeAll_commute: "removeAll x (removeAll y zs) = removeAll y (removeAll x zs)"
by (induct zs) auto
lemma set_removeAll[simp]: "set(removeAll x xs) = set xs - {x}"
by (induct xs) auto
lemma removeAll_id[simp]: "x \<notin> set xs \<Longrightarrow> removeAll x xs = xs"
by (induct xs) auto
lemma removeAll_filter_not[simp]:
"\<not> P x \<Longrightarrow> removeAll x (filter P xs) = filter P xs"
by(induct xs) auto
lemma distinct_removeAll:
"distinct xs \<Longrightarrow> distinct (removeAll x xs)"
by (simp add: removeAll_filter_not_eq)
lemma distinct_remove1_removeAll:
"distinct xs \<Longrightarrow> remove1 x xs = removeAll x xs"
by (induct xs) simp_all
lemma map_removeAll_inj_on: "inj_on f (insert x (set xs)) \<Longrightarrow>
map f (removeAll x xs) = removeAll (f x) (map f xs)"
by (induct xs) (simp_all add:inj_on_def)
lemma map_removeAll_inj: "inj f \<Longrightarrow>
map f (removeAll x xs) = removeAll (f x) (map f xs)"
by (rule map_removeAll_inj_on, erule inj_on_subset, rule subset_UNIV)
lemma length_removeAll_less_eq [simp]:
"length (removeAll x xs) \<le> length xs"
by (simp add: removeAll_filter_not_eq)
lemma length_removeAll_less [termination_simp]:
"x \<in> set xs \<Longrightarrow> length (removeAll x xs) < length xs"
by (auto dest: length_filter_less simp add: removeAll_filter_not_eq)
lemma distinct_concat_iff: "distinct (concat xs) \<longleftrightarrow>
distinct (removeAll [] xs) \<and>
(\<forall>ys. ys \<in> set xs \<longrightarrow> distinct ys) \<and>
(\<forall>ys zs. ys \<in> set xs \<and> zs \<in> set xs \<and> ys \<noteq> zs \<longrightarrow> set ys \<inter> set zs = {})"
proof (induct xs)
case Nil
then show ?case by auto
next
case (Cons a xs)
have "\<lbrakk>set a \<inter> \<Union> (set ` set xs) = {}; a \<in> set xs\<rbrakk> \<Longrightarrow> a=[]"
by (metis Int_iff UN_I empty_iff equals0I set_empty)
then show ?case
by (auto simp: Cons)
qed
lemma foldr_fold_removeAll[code_unfold]: "foldr removeAll = fold removeAll"
using foldr_fold[of _ removeAll] removeAll_commute by fastforce
text \<open>The difference of two lists viewed as multisets.
Conceptually, the result of \<^const>\<open>minus_list_mset\<close> is only determined up to permutation,
i.e. up to the multiset of elements. Thus this function comes into its own in connection
with multisets where \<open>mset(minus_list_mset xs ys) = mset xs - mset ys\<close> is proved. Lemma
\<open>count_list_minus_list_mset\<close> is the equivalent on the list level.\<close>
lemma minus_list_mset_Nil1 [simp]: "minus_list_mset [] xs = []"
by (induction xs) auto
lemma minus_list_mset_Cons1: "minus_list_mset (x#xs) ys =
(if x \<in> set ys then minus_list_mset xs (remove1 x ys) else x # (minus_list_mset xs ys))"
proof (induction ys)
case Nil
then show ?case by simp
next
case (Cons a ys)
then show ?case
by (metis list.set_intros(1,2) minus_list_mset_Cons2 minus_list_mset_remove1_commute remove1.simps(2)
set_ConsD)
qed
text \<open>The difference of two lists viewed as sets.
Conceptually, the result of \<^const>\<open>minus_list_set\<close> is only determined up to the set of elements:\<close>
lemma set_minus_list_set[simp]: "set(minus_list_set xs ys) = set xs - set ys"
by(induction ys) (auto simp: minus_list_set_def)
text \<open>The intersection of two lists viewed as sets.
Conceptually, the result of \<^const>\<open>inter_list_set\<close> is only determined up to the set of elements:\<close>
lemma set_inter_list_set[simp]: "set(inter_list_set xs ys) = set xs \<inter> set ys"
by(auto simp add: inter_list_set_def)
lemma length_replicate [simp]: "length (replicate n x) = n"
by (induct n) auto
lemma replicate_eqI:
assumes "length xs = n" and "\<And>y. y \<in> set xs \<Longrightarrow> y = x"
shows "xs = replicate n x"
using assms
proof (induct xs arbitrary: n)
case Nil then show ?case by simp
next
case (Cons x xs) then show ?case by (cases n) simp_all
qed
lemma Ex_list_of_length: "\<exists>xs. length xs = n"
by (rule exI[of _ "replicate n undefined"]) simp
lemma map_replicate [simp]: "map f (replicate n x) = replicate n (f x)"
by (induct n) auto
lemma map_replicate_const:
"map (\<lambda> x. k) lst = replicate (length lst) k"
by (induct lst) auto
lemma replicate_app_Cons_same:
"(replicate n x) @ (x # xs) = x # replicate n x @ xs"
by (induct n) auto
lemma rev_replicate [simp]: "rev (replicate n x) = replicate n x"
by (metis length_rev map_replicate map_replicate_const rev_map)
lemma replicate_add: "replicate (n + m) x = replicate n x @ replicate m x"
by (induct n) auto
text\<open>Courtesy of Matthias Daum:\<close>
lemma append_replicate_commute:
"replicate n x @ replicate k x = replicate k x @ replicate n x"
by (metis add.commute replicate_add)
text\<open>Courtesy of Andreas Lochbihler:\<close>
lemma filter_replicate:
"filter P (replicate n x) = (if P x then replicate n x else [])"
by(induct n) auto
lemma hd_replicate [simp]: "n \<noteq> 0 \<Longrightarrow> hd (replicate n x) = x"
by (induct n) auto
lemma tl_replicate [simp]: "tl (replicate n x) = replicate (n - 1) x"
by (induct n) auto
lemma last_replicate [simp]: "n \<noteq> 0 \<Longrightarrow> last (replicate n x) = x"
by (atomize (full), induct n) auto
lemma nth_replicate[simp]: "i < n \<Longrightarrow> (replicate n x)!i = x"
by (induct n arbitrary: i)(auto simp: nth_Cons split: nat.split)
text\<open>Courtesy of Matthias Daum (2 lemmas):\<close>
lemma take_replicate[simp]: "take i (replicate k x) = replicate (min i k) x"
proof (cases "k \<le> i")
case True
then show ?thesis
by (simp add: min_def)
next
case False
then have "replicate k x = replicate i x @ replicate (k - i) x"
by (simp add: replicate_add [symmetric])
then show ?thesis
by (simp add: min_def)
qed
lemma drop_replicate[simp]: "drop i (replicate k x) = replicate (k-i) x"
proof (induct k arbitrary: i)
case (Suc k)
then show ?case
by (simp add: drop_Cons')
qed simp
lemma set_replicate_Suc: "set (replicate (Suc n) x) = {x}"
by (induct n) auto
lemma set_replicate [simp]: "n \<noteq> 0 \<Longrightarrow> set (replicate n x) = {x}"
by (fast dest!: not0_implies_Suc intro!: set_replicate_Suc)
lemma set_replicate_conv_if: "set (replicate n x) = (if n = 0 then {} else {x})"
by auto
lemma in_set_replicate[simp]: "(x \<in> set (replicate n y)) = (x = y \<and> n \<noteq> 0)"
by (simp add: set_replicate_conv_if)
lemma replicate_eq_replicate[simp]:
"(replicate m x = replicate n y) \<longleftrightarrow> (m=n \<and> (m\<noteq>0 \<longrightarrow> x=y))"
proof (induct m arbitrary: n)
case (Suc m n)
then show ?case
by (induct n) auto
qed simp
lemma takeWhile_replicate[simp]:
"takeWhile P (replicate n x) = (if P x then replicate n x else [])"
using takeWhile_eq_Nil_iff by fastforce
lemma dropWhile_replicate[simp]:
"dropWhile P (replicate n x) = (if P x then [] else replicate n x)"
using dropWhile_eq_self_iff by fastforce
lemma replicate_length_filter:
"replicate (length (filter (\<lambda>y. x = y) xs)) x = filter (\<lambda>y. x = y) xs"
by (induct xs) auto
lemma comm_append_are_replicate:
"xs @ ys = ys @ xs \<Longrightarrow> \<exists>m n zs. concat (replicate m zs) = xs \<and> concat (replicate n zs) = ys"
proof (induction "length (xs @ ys) + length xs" arbitrary: xs ys rule: less_induct)
case less
consider (1) "length ys < length xs" | (2) "xs = []" | (3) "length xs \<le> length ys \<and> xs \<noteq> []"
by linarith
then show ?case
proof (cases)
case 1
then show ?thesis
using less.hyps[OF _ less.prems[symmetric]] nat_add_left_cancel_less by auto
next
case 2
then have "concat (replicate 0 ys) = xs \<and> concat (replicate 1 ys) = ys"
by simp
then show ?thesis
by blast
next
case 3
then have "length xs \<le> length ys" and "xs \<noteq> []"
by blast+
from \<open>length xs \<le> length ys\<close> and \<open>xs @ ys = ys @ xs\<close>
obtain ws where "ys = xs @ ws"
by (auto simp: append_eq_append_conv2)
from this and \<open>xs \<noteq> []\<close>
have "length ws < length ys"
by simp
from \<open>xs @ ys = ys @ xs\<close>[unfolded \<open>ys = xs @ ws\<close>]
have "xs @ ws = ws @ xs"
by simp
from less.hyps[OF _ this] \<open>length ws < length ys\<close>
obtain m n' zs where "concat (replicate m zs) = xs" and "concat (replicate n' zs) = ws"
by auto
then have "concat (replicate (m+n') zs) = ys"
using \<open>ys = xs @ ws\<close>
by (simp add: replicate_add)
then show ?thesis
using \<open>concat (replicate m zs) = xs\<close> by blast
qed
qed
lemma comm_append_is_replicate:
fixes xs ys :: "'a list"
assumes "xs \<noteq> []" "ys \<noteq> []"
assumes "xs @ ys = ys @ xs"
shows "\<exists>n zs. n > 1 \<and> concat (replicate n zs) = xs @ ys"
proof -
obtain m n zs where "concat (replicate m zs) = xs"
and "concat (replicate n zs) = ys"
using comm_append_are_replicate[OF assms(3)] by blast
then have "m + n > 1" and "concat (replicate (m+n) zs) = xs @ ys"
using \<open>xs \<noteq> []\<close> and \<open>ys \<noteq> []\<close>
by (auto simp: replicate_add)
then show ?thesis by blast
qed
lemma Cons_replicate_eq:
"x # xs = replicate n y \<longleftrightarrow> x = y \<and> n > 0 \<and> xs = replicate (n - 1) x"
by (induct n) auto
lemma replicate_length_same:
"(\<forall>y\<in>set xs. y = x) \<Longrightarrow> replicate (length xs) x = xs"
by (induct xs) simp_all
lemma foldr_replicate [simp]:
"foldr f (replicate n x) = f x ^^ n"
by (induct n) (simp_all)
lemma fold_replicate [simp]:
"fold f (replicate n x) = f x ^^ n"
by (subst foldr_fold [symmetric]) simp_all
lemma rotate_drop_take:
"rotate n xs = drop (n mod length xs) xs @ take (n mod length xs) xs"
proof (induct n)
case (Suc n)
show ?case
proof (cases "xs = []")
case False
then show ?thesis
proof (cases "n mod length xs = 0")
case True
then show ?thesis
by (auto simp add: mod_Suc False Suc.hyps drop_Suc rotate1_hd_tl take_Suc Suc_length_conv)
next
case False
with \<open>xs \<noteq> []\<close> Suc
show ?thesis
by (simp add: rotate_def mod_Suc rotate1_hd_tl drop_Suc[symmetric] drop_tl[symmetric]
take_hd_drop linorder_not_le)
qed
qed simp
qed simp
lemma rotate_conv_mod: "rotate n xs = rotate (n mod length xs) xs"
by(simp add:rotate_drop_take)
lemma rotate_id[simp]: "n mod length xs = 0 \<Longrightarrow> rotate n xs = xs"
by(simp add:rotate_drop_take)
lemma length_rotate[simp]: "length(rotate n xs) = length xs"
by (induct n arbitrary: xs) (simp_all add:rotate_def)
lemma distinct1_rotate[simp]: "distinct(rotate1 xs) = distinct xs"
by (cases xs) auto
lemma distinct_rotate[simp]: "distinct(rotate n xs) = distinct xs"
by (induct n) (simp_all add:rotate_def)
lemma rotate_map: "rotate n (map f xs) = map f (rotate n xs)"
by(simp add:rotate_drop_take take_map drop_map)
lemma set_rotate1[simp]: "set(rotate1 xs) = set xs"
by (cases xs) auto
lemma set_rotate[simp]: "set(rotate n xs) = set xs"
by (induct n) (simp_all add:rotate_def)
lemma rotate1_replicate[simp]: "rotate1 (replicate n a) = replicate n a"
by (cases n) (simp_all add: replicate_append_same)
lemma rotate1_is_Nil_conv[simp]: "(rotate1 xs = []) = (xs = [])"
by (cases xs) auto
lemma rotate_is_Nil_conv[simp]: "(rotate n xs = []) = (xs = [])"
by (induct n) (simp_all add:rotate_def)
lemma rotate_rev: "rotate n (rev xs) = rev(rotate (length xs - (n mod length xs)) xs)"
proof (cases "length xs = 0 \<or> n mod length xs = 0") case False
then show ?thesis
by(simp add:rotate_drop_take rev_drop rev_take)
qed force
lemma hd_rotate_conv_nth:
assumes "xs \<noteq> []" shows "hd(rotate n xs) = xs!(n mod length xs)"
proof -
have "n mod length xs < length xs"
using assms by simp
then show ?thesis
by (metis drop_eq_Nil hd_append2 hd_drop_conv_nth leD rotate_drop_take)
qed
lemma nth_rotate:
\<open>rotate m xs ! n = xs ! ((m + n) mod length xs)\<close> if \<open>n < length xs\<close>
by (smt (verit) add.commute hd_rotate_conv_nth length_rotate not_less0 list.size(3) mod_less rotate_rotate that)
lemma nth_rotate1:
\<open>rotate1 xs ! n = xs ! (Suc n mod length xs)\<close> if \<open>n < length xs\<close>
using that nth_rotate [of n xs 1] by simp
lemma inj_rotate1: "inj rotate1"
proof
fix xs ys :: "'a list" show "rotate1 xs = rotate1 ys \<Longrightarrow> xs = ys"
by (cases xs; cases ys; simp)
qed
lemma surj_rotate1: "surj rotate1"
proof (safe, simp_all)
fix xs :: "'a list" show "xs \<in> range rotate1"
proof (cases xs rule: rev_exhaust) caseNil
hence "xs = rotate1 []" by auto
thus ?thesis by fast
next case (snoc as a)
hence "xs = rotate1 (a#as)" by force
thus ?thesis by fast
qed
qed
lemma bij_rotate1: "bij (rotate1 :: 'a list \<Rightarrow> 'a list)"
using bijI inj_rotate1 surj_rotate1 by blast
lemma nths_shift_lemma_Suc: "map fst (filter (\<lambda>p. P(Suc(snd p))) (zip xs is)) =
map fst (filter (\<lambda>p. P(snd p)) (zip xs (map Suc is)))"
proof (induct xs arbitrary: "is") case (Cons x xs "is")
show ?case
by (cases "is") (auto simp add: Cons.hyps)
qed simp
lemma nths_shift_lemma: "map fst (filter (\<lambda>p. snd p \<in> A) (zip xs [i..<i + length xs])) =
map fst (filter (\<lambda>p. snd p + i \<in> A) (zip xs [0..<length xs]))"
by (induct xs rule: rev_induct) (simp_all add: add.commute)
lemma nths_append: "nths (l @ l') A = nths l A @ nths l' {j. j + length l \<in> A}"
unfolding nths_def
proof (induct l' rule: rev_induct) case (snoc x xs)
then show ?case
by (simp add: upt_add_eq_append[of 0] nths_shift_lemma add.commute)
qed auto
lemma nths_Cons: "nths (x # l) A = (if 0 \<in> A then [x] else []) @ nths l {j. Suc j \<in> A}"
proof (induct l rule: rev_induct) case (snoc x xs)
then show ?case
by (simp flip: append_Cons add: nths_append)
qed (auto simp: nths_def)
lemma nths_map: "nths (map f xs) I = map f (nths xs I)"
by(induction xs arbitrary: I) (simp_all add: nths_Cons)
lemma nths_upt_eq_take [simp]: "nths l {..<n} = take n l"
by (induct l rule: rev_induct) (simp_all split: nat_diff_split add: nths_append)
lemma nths_nths: "nths (nths xs A) B = nths xs {i \<in> A. \<exists>j \<in> B. card {i' \<in> A. i' < i} = j}"
by (induction xs arbitrary: A B) (auto simp add: nths_Cons card_less_Suc card_less_Suc2)
lemma drop_eq_nths: "drop n xs = nths xs {i. i \<ge> n}"
by (induction xs arbitrary: n) (auto simp add: nths_Cons nths_all drop_Cons' intro: arg_cong2[where f=nths, OF refl])
lemma nths_drop: "nths (drop n xs) I = nths xs ((+) n ` I)"
by(force simp: drop_eq_nths nths_nths simp flip: atLeastLessThan_iff
intro: arg_cong2[where f=nths, OF refl])
lemma filter_in_nths: "distinct xs \<Longrightarrow> filter (%x. x \<in> set (nths xs s)) xs = nths xs s"
proof (induct xs arbitrary: s) caseNil thus ?case by simp
next case (Cons a xs)
then have "\<forall>x. x \<in> set xs \<longrightarrow> x \<noteq> a" by auto
with Cons show ?case by(simp add: nths_Cons cong:filter_cong)
qed
subsubsection \<open>\<^const>\<open>subseqs\<close> and \<^const>\<open>List.n_lists\<close>\<close>
lemma subseqs_powset: "set ` set (subseqs xs) = Pow (set xs)"
proof -
have aux: "\<And>x A. set ` Cons x ` A = insert x ` set ` A"
by (auto simp add: image_def)
have "set (map set (subseqs xs)) = Pow (set xs)"
by (induct xs) (simp_all add: aux Let_def Pow_insert Un_commute comp_def del: map_map)
then show ?thesis by simp
qed
lemma distinct_set_subseqs:
assumes "distinct xs"
shows "distinct (map set (subseqs xs))"
by (simp add: assms card_Pow card_distinct distinct_card length_subseqs subseqs_powset)
lemma n_lists_Nil [simp]: "List.n_lists n [] = (if n = 0 then [[]] else [])"
by (induct n) simp_all
lemma length_n_lists_elem: "ys \<in> set (List.n_lists n xs) \<Longrightarrow> length ys = n"
by (induct n arbitrary: ys) auto
lemma set_n_lists: "set (List.n_lists n xs) = {ys. length ys = n \<and> set ys \<subseteq> set xs}"
proof (rule set_eqI)
fix ys :: "'a list"
show "ys \<in> set (List.n_lists n xs) \<longleftrightarrow> ys \<in> {ys. length ys = n \<and> set ys \<subseteq> set xs}"
proof -
have "ys \<in> set (List.n_lists n xs) \<Longrightarrow> length ys = n"
by (induct n arbitrary: ys) auto
moreover have "\<And>x. ys \<in> set (List.n_lists n xs) \<Longrightarrow> x \<in> set ys \<Longrightarrow> x \<in> set xs"
by (induct n arbitrary: ys) auto
moreover have "set ys \<subseteq> set xs \<Longrightarrow> ys \<in> set (List.n_lists (length ys) xs)"
by (induct ys) auto
ultimately show ?thesis by auto
qed
qed
lemma subseqs_refl: "xs \<in> set (subseqs xs)"
by (induct xs) (simp_all add: Let_def)
lemma subset_subseqs: "X \<subseteq> set xs \<Longrightarrow> X \<in> set ` set (subseqs xs)"
unfolding subseqs_powset by simp
lemma Cons_in_subseqsD: "y # ys \<in> set (subseqs xs) \<Longrightarrow> ys \<in> set (subseqs xs)"
by (induct xs) (auto simp: Let_def)
lemma subseqs_distinctD: "\<lbrakk> ys \<in> set (subseqs xs); distinct xs \<rbrakk> \<Longrightarrow> distinct ys"
proof (induct xs arbitrary: ys) case (Cons x xs ys)
then show ?case
by (auto simp: Let_def) (metis Pow_iff contra_subsetD image_eqI subseqs_powset)
qed simp
lemma splice_replicate[simp]: "splice (replicate m x) (replicate n x) = replicate (m+n) x"
proof (induction "replicate m x""replicate n x" arbitrary: m n rule: splice.induct) case (2 x xs)
then show ?case
by (auto simp add: Cons_replicate_eq dest: gr0_implies_Suc)
qed auto
lemma set_shuffles: "zs \<in> shuffles xs ys \<Longrightarrow> set zs = set xs \<union> set ys"
by (induction xs ys arbitrary: zs rule: shuffles.induct) auto
lemma distinct_disjoint_shuffles:
assumes "distinct xs""distinct ys""set xs \<inter> set ys = {}""zs \<in> shuffles xs ys"
shows "distinct zs"
using assms
proof (induction xs ys arbitrary: zs rule: shuffles.induct) case (3 x xs y ys)
show ?case
proof (cases zs) case (Cons z zs')
with "3.prems"and"3.IH"[of zs'] show ?thesis by (force dest: set_shuffles)
qed simp_all
qed simp_all
lemma Cons_shuffles_subset1: "(#) x ` shuffles xs ys \<subseteq> shuffles (x # xs) ys"
by (cases ys) auto
lemma Cons_shuffles_subset2: "(#) y ` shuffles xs ys \<subseteq> shuffles xs (y # ys)"
by (cases xs) auto
lemma filter_shuffles: "filter P ` shuffles xs ys = shuffles (filter P xs) (filter P ys)"
proof -
have *: "filter P ` (#) x ` A = (if P x then (#) x ` filter P ` A else filter P ` A)" for x A
by (auto simp: image_image)
show ?thesis
by (induction xs ys rule: shuffles.induct)
(simp_all split: if_splits add: image_Un * Un_absorb1 Un_absorb2
Cons_shuffles_subset1 Cons_shuffles_subset2)
qed
lemma filter_shuffles_disjoint1:
assumes "set xs \<inter> set ys = {}""zs \<in> shuffles xs ys"
shows "filter (\<lambda>x. x \<in> set xs) zs = xs" (is "filter ?P _ = _") and"filter (\<lambda>x. x \<notin> set xs) zs = ys" (is "filter ?Q _ = _")
using assms
proof -
from assms have "filter ?P zs \<in> filter ?P ` shuffles xs ys" by blast
also have "filter ?P ` shuffles xs ys = shuffles (filter ?P xs) (filter ?P ys)"
by (rule filter_shuffles)
also have "filter ?P xs = xs" by (rule filter_True) simp_all
also have "filter ?P ys = []" by (rule filter_False) (insert assms(1), auto)
also have "shuffles xs [] = {xs}" by simp
finally show "filter ?P zs = xs" by simp
next
from assms have "filter ?Q zs \<in> filter ?Q ` shuffles xs ys" by blast
also have "filter ?Q ` shuffles xs ys = shuffles (filter ?Q xs) (filter ?Q ys)"
by (rule filter_shuffles)
also have "filter ?Q ys = ys" by (rule filter_True) (insert assms(1), auto)
also have "filter ?Q xs = []" by (rule filter_False) (insert assms(1), auto)
also have "shuffles [] ys = {ys}" by simp
finally show "filter ?Q zs = ys" by simp
qed
lemma filter_shuffles_disjoint2:
assumes "set xs \<inter> set ys = {}""zs \<in> shuffles xs ys"
shows "filter (\<lambda>x. x \<in> set ys) zs = ys""filter (\<lambda>x. x \<notin> set ys) zs = xs"
using filter_shuffles_disjoint1[of ys xs zs] assms
by (simp_all add: shuffles_commutes Int_commute)
lemma partition_in_shuffles: "xs \<in> shuffles (filter P xs) (filter (\<lambda>x. \<not>P x) xs)"
proof (induction xs) case (Cons x xs)
show ?case
proof (cases "P x") case True
hence "x # xs \<in> (#) x ` shuffles (filter P xs) (filter (\<lambda>x. \<not>P x) xs)"
by (intro imageI Cons.IH)
also have "\<dots> \<subseteq> shuffles (filter P (x # xs)) (filter (\<lambda>x. \<not>P x) (x # xs))"
by (simp add: True Cons_shuffles_subset1)
finally show ?thesis .
next case False
hence "x # xs \<in> (#) x ` shuffles (filter P xs) (filter (\<lambda>x. \<not>P x) xs)"
by (intro imageI Cons.IH)
also have "\<dots> \<subseteq> shuffles (filter P (x # xs)) (filter (\<lambda>x. \<not>P x) (x # xs))"
by (simp add: False Cons_shuffles_subset2)
finally show ?thesis .
qed
qed auto
lemma inv_image_partition:
assumes "\<And>x. x \<in> set xs \<Longrightarrow> P x""\<And>y. y \<in> set ys \<Longrightarrow> \<not>P y"
shows "partition P -` {(xs, ys)} = shuffles xs ys"
proof (intro equalityI subsetI)
fix zs assume zs: "zs \<in> shuffles xs ys"
hence [simp]: "set zs = set xs \<union> set ys" by (rule set_shuffles)
from assms have "filter P zs = filter (\<lambda>x. x \<in> set xs) zs" "filter (\<lambda>x. \<not>P x) zs = filter (\<lambda>x. x \<in> set ys) zs"
by (intro filter_cong refl; force)+
moreover from assms have "set xs \<inter> set ys = {}" by auto
ultimately show "zs \<in> partition P -` {(xs, ys)}" using zs
by (simp add: o_def filter_shuffles_disjoint1 filter_shuffles_disjoint2)
next
fix zs assume "zs \<in> partition P -` {(xs, ys)}"
thus "zs \<in> shuffles xs ys" using partition_in_shuffles[of zs] by (auto simp: o_def)
qed
subsubsection \<open>Transpose\<close>
function transpose where "transpose [] = []" | "transpose ([] # xss) = transpose xss" | "transpose ((x#xs) # xss) =
(x # [h. (h#t) \<leftarrow> xss]) # transpose (xs # [t. (h#t) \<leftarrow> xss])"
by pat_completeness auto
lemma transpose_aux_filter_head: "concat (map (case_list [] (\<lambda>h t. [h])) xss) =
map (\<lambda>xs. hd xs) (filter (\<lambda>ys. ys \<noteq> []) xss)"
by (induct xss) (auto split: list.split)
lemma transpose_aux_max: "max (Suc (length xs)) (foldr (\<lambda>xs. max (length xs)) xss 0) =
Suc (max (length xs) (foldr (\<lambda>x. max (length x - Suc 0)) (filter (\<lambda>ys. ys \<noteq> []) xss) 0))"
(is "max _ ?foldB = Suc (max _ ?foldA)")
proof (cases "(filter (\<lambda>ys. ys \<noteq> []) xss) = []") case True
hence "foldr (\<lambda>xs. max (length xs)) xss 0 = 0"
proof (induct xss) case (Cons x xs)
then have "x = []" by (cases x) auto
with Cons show ?case by auto
qed simp
thus ?thesis using True by simp
next case False
have foldA: "?foldA = foldr (\<lambda>x. max (length x)) (filter (\<lambda>ys. ys \<noteq> []) xss) 0 - 1"
by (induct xss) auto
have foldB: "?foldB = foldr (\<lambda>x. max (length x)) (filter (\<lambda>ys. ys \<noteq> []) xss) 0"
by (induct xss) auto
have "0 < ?foldB"
proof -
from False
obtain z zs where zs: "(filter (\<lambda>ys. ys \<noteq> []) xss) = z#zs" by (auto simp: neq_Nil_conv)
hence "z \<in> set (filter (\<lambda>ys. ys \<noteq> []) xss)" by auto
hence "z \<noteq> []" by auto
thus ?thesis
unfolding foldB zs
by (auto simp: max_def intro: less_le_trans)
qed
thus ?thesis
unfolding foldA foldB max_Suc_Suc[symmetric]
by simp
qed
fix i assume "i < length (transpose (map (map f) xs))"
thus "transpose (map (map f) xs) ! i = map (map f) (transpose xs) ! i"
by (simp add: nth_transpose filter_map comp_def)
qed
subsubsection \<open>\<^const>\<open>min\<close> and \<^const>\<open>arg_min\<close>\<close>
lemma min_list_Min: "xs \<noteq> [] \<Longrightarrow> min_list xs = Min (set xs)"
by (induction xs rule: induct_list012)(auto)
lemma f_arg_min_list_f: "xs \<noteq> [] \<Longrightarrow> f (arg_min_list f xs) = Min (f ` (set xs))"
by(induction f xs rule: arg_min_list.induct) (auto simp: min_def intro!: antisym)
lemma arg_min_list_in: "xs \<noteq> [] \<Longrightarrow> arg_min_list f xs \<in> set xs"
by(induction xs rule: induct_list012) (auto simp: Let_def)
subsubsection \<open>(In)finiteness\<close>
lemma finite_list_length: "finite {xs::('a::finite) list. length xs = n}"
proof(induction n) case (Suc n)
have "{xs::'a list. length xs = Suc n} = (\<Union>x. (#) x ` {xs. length xs = n})"
by (auto simp: length_Suc_conv)
then show ?case using Suc by simp
qed simp
lemma finite_maxlen: "finite (M::'a list set) \<Longrightarrow> \<exists>n. \<forall>s\<in>M. size s < n"
proof (induct rule: finite.induct) case emptyI show ?case by simp
next case (insertI M xs)
then obtain n where "\<forall>s\<in>M. length s < n" by blast
hence "\<forall>s\<in>insert xs M. size s < max n (size xs) + 1" by auto
thus ?case ..
qed
lemma lists_length_Suc_eq: "{xs. set xs \<subseteq> A \<and> length xs = Suc n} =
(\<lambda>(xs, n). n#xs) ` ({xs. set xs \<subseteq> A \<and> length xs = n} \<times> A)"
by (auto simp: length_Suc_conv)
lemma
assumes "finite A"
shows finite_lists_length_eq: "finite {xs. set xs \<subseteq> A \<and> length xs = n}" and card_lists_length_eq: "card {xs. set xs \<subseteq> A \<and> length xs = n} = (card A)^n"
using \<open>finite A\<close>
by (induct n)
(auto simp: card_image inj_split_Cons lists_length_Suc_eq cong: conj_cong)
lemma finite_lists_length_le:
assumes "finite A" shows "finite {xs. set xs \<subseteq> A \<and> length xs \<le> n}"
(is "finite ?S")
proof-
have "?S = (\<Union>n\<in>{0..n}. {xs. set xs \<subseteq> A \<and> length xs = n})" by auto
thus ?thesis by (auto intro!: finite_lists_length_eq[OF \<open>finite A\<close>] simp only:)
qed
lemma card_lists_length_le:
assumes "finite A" shows "card {xs. set xs \<subseteq> A \<and> length xs \<le> n} = (\<Sum>i\<le>n. card A^i)"
proof -
have "(\<Sum>i\<le>n. card A^i) = card (\<Union>i\<le>n. {xs. set xs \<subseteq> A \<and> length xs = i})"
using \<open>finite A\<close>
by (subst card_UN_disjoint)
(auto simp add: card_lists_length_eq finite_lists_length_eq)
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}"
by auto
finally show ?thesis by simp
qed
lemma finite_subset_distinct:
assumes "finite A"
shows "finite {xs. set xs \<subseteq> A \<and> distinct xs}" (is "finite ?S")
proof (rule finite_subset)
from assms show "?S \<subseteq> {xs. set xs \<subseteq> A \<and> length xs \<le> card A}"
by clarsimp (metis distinct_card card_mono)
from assms show "finite ..." by (rule finite_lists_length_le)
qed
lemma card_lists_distinct_length_eq:
assumes "finite A""k \<le> card A"
shows "card {xs. length xs = k \<and> distinct xs \<and> set xs \<subseteq> A} = \<Prod>{card A - k + 1 .. card A}"
using assms
proof (induct k) case0
then have "{xs. length xs = 0 \<and> distinct xs \<and> set xs \<subseteq> A} = {[]}" by auto
then show ?case by simp
next case (Suc k) let"?k_list" = "\<lambda>k xs. length xs = k \<and> distinct xs \<and> set xs \<subseteq> A"
have inj_Cons: "\<And>A. inj_on (\<lambda>(xs, n). n # xs) A" by (rule inj_onI) auto
from Suc have "k \<le> card A" by simp
moreover note \<open>finite A\<close>
moreover have "finite {xs. ?k_list k xs}"
by (rule finite_subset) (use finite_lists_length_eq[OF \<open>finite A\<close>, of k] in auto)
moreover have "\<And>i j. i \<noteq> j \<longrightarrow> {i} \<times> (A - set i) \<inter> {j} \<times> (A - set j) = {}"
by auto
moreover have "\<And>i. i \<in> {xs. ?k_list k xs} \<Longrightarrow> card (A - set i) = card A - k"
by (simp add: card_Diff_subset distinct_card)
moreover have "{xs. ?k_list (Suc k) xs} =
(\<lambda>(xs, n). n#xs) ` \<Union>((\<lambda>xs. {xs} \<times> (A - set xs)) ` {xs. ?k_list k xs})"
by (auto simp: length_Suc_conv)
moreover have "Suc (card A - Suc k) = card A - k" using Suc.prems by simp
then have "(card A - k) * \<Prod>{Suc (card A - k)..card A} = \<Prod>{Suc (card A - Suc k)..card A}"
by (subst prod.insert[symmetric]) (simp add: atLeastAtMost_insertL)+
ultimately show ?case
by (simp add: card_image inj_Cons card_UN_disjoint Suc.hyps algebra_simps)
qed
lemma card_lists_distinct_length_eq':
assumes "k < card A"
shows "card {xs. length xs = k \<and> distinct xs \<and> set xs \<subseteq> A} = \<Prod>{card A - k + 1 .. card A}"
proof - from \<open>k < card A\<close> have "finite A"and"k \<le> card A" using card.infinite by force+ from this show ?thesis by (rule card_lists_distinct_length_eq)
qed
lemma infinite_UNIV_listI: "\<not> finite(UNIV::'a list set)" by (metis UNIV_I finite_maxlen length_replicate less_irrefl)
lemma same_length_different:
assumes "xs \<noteq> ys"and"length xs = length ys"
shows "\<exists>pre x xs' y ys'. x\<noteq>y \<and> xs = pre @ [x] @ xs' \<and> ys = pre @ [y] @ ys'"
using assms
proof (induction xs arbitrary: ys)
case Nil then show ?case byauto
next
case (Cons x xs) then obtain z zs where ys: "ys = Cons z zs" by (metis length_Suc_conv)
show ?case
proof (cases"x=z")
case True then have "xs \<noteq> zs""length xs = length zs"
using Cons.prems ys byauto then obtain pre u xs' v ys'where"u\<noteq>v"and xs: "xs = pre @ [u] @ xs'"and zs: "zs = pre @ [v] @ys'"
using Cons.IH by meson then have "x # xs = (z#pre) @ [u] @ xs' \<and> ys = (z#pre) @ [v] @ ys'" by (simp add: True ys) with \<open>u\<noteq>v\<close> show ?thesis by blast
next
case False then have "x # xs = [] @ [x] @ xs \<and> ys = [] @ [z] @ zs" by (simp add: ys) then show ?thesis
using Falseby blast
qed
qed
text \<open>Sometimes the second equation in the definition of \<^const>\<open>sorted_wrt\<close> is too aggressive
because it relates each list element to \emph{all} its successors. Then this equation
should be removed and \<open>sorted_wrt2_simps\<close> should be added instead.\<close>
lemma sorted_wrt1: "sorted_wrt P [x] = True" by(simp)
lemma sorted_wrt2: "transp P \<Longrightarrow> sorted_wrt P (x # y # zs) = (P x y \<and> sorted_wrt P (y # zs))"
proof (induction zs arbitrary: x y)
case (Cons z zs) then show ?case by simp (meson transpD)+
qed auto
lemma sorted_wrt_append: "sorted_wrt P (xs @ ys) \<longleftrightarrow>
sorted_wrt P xs \<and> sorted_wrt P ys \<and> (\<forall>x\<in>set xs. \<forall>y\<in>set ys. P x y)" by (induction xs) auto
lemma sorted_wrt_map: "sorted_wrt R (map f xs) = sorted_wrt (\<lambda>x y. R (f x) (f y)) xs" by (induction xs) simp_all
lemma
assumes "sorted_wrt f xs"
shows sorted_wrt_take[simp]: "sorted_wrt f (take n xs)" and sorted_wrt_drop[simp]: "sorted_wrt f (drop n xs)"
proof - from assms have "sorted_wrt f (take n xs @ drop n xs)"by simp
thus "sorted_wrt f (take n xs)"and"sorted_wrt f (drop n xs)"
unfolding sorted_wrt_append by simp_all
qed
lemma sorted_wrt_dropWhile[simp]: "sorted_wrt R xs \<Longrightarrow> sorted_wrt R (dropWhile P xs)" by (auto dest: sorted_wrt_drop simp: dropWhile_eq_drop)
lemma sorted_wrt_takeWhile[simp]: "sorted_wrt R xs \<Longrightarrow> sorted_wrt R (takeWhile P xs)" by (subst takeWhile_eq_take) (auto dest: sorted_wrt_take)
lemma sorted_wrt_filter: "sorted_wrt f xs \<Longrightarrow> sorted_wrt f (filter P xs)" by (induction xs) auto
lemma sorted_wrt_rev: "sorted_wrt P (rev xs) = sorted_wrt (\<lambda>x y. P y x) xs" by (induction xs) (auto simp add: sorted_wrt_append)
lemma sorted_wrt_mono_rel: "(\<And>x y. \<lbrakk> x \<in> set xs; y \<in> set xs; P x y \<rbrakk> \<Longrightarrow> Q x y) \<Longrightarrow> sorted_wrt P xs \<Longrightarrow> sorted_wrt Q xs" by(induction xs)(auto)
lemma sorted_wrt_upto[simp]: "sorted_wrt (<) [i..j]"
proof(induct i j rule:upto.induct)
case (1 i j) from this show ?case
unfolding upto.simps[of i j] byauto
qed
text \<open>Each element is greater or equal to its index:\<close>
lemma sorted_wrt_less_idx: "sorted_wrt (<) ns \<Longrightarrow> i < length ns \<Longrightarrow> i \<le> ns!i"
proof (induction ns arbitrary: i rule: rev_induct)
case Nil thus ?case by simp
next
case snoc
thus ?case by (simp add: nth_append sorted_wrt_append)
(metis less_antisym not_less nth_mem)
qed
text \<open>Sometimes the second equation in the definition of \<^const>\<open>sorted\<close> is too aggressive
because it relates each list element to \emph{all} its successors. Then this equation
should be removed and \<open>sorted2_simps\<close> should be added instead.
Executable code is one such use case.\<close>
lemma sorted0: "sorted [] = True" by simp
lemma sorted1: "sorted [x] = True" by simp
lemma sorted2: "sorted (x # y # zs) = (x \<le> y \<and> sorted (y # zs))" byauto
lemmas sorted2_simps = sorted1 sorted2
lemma sorted_append: "sorted (xs@ys) = (sorted xs \<and> sorted ys \<and> (\<forall>x \<in> set xs. \<forall>y \<in> set ys. x\<le>y))" by (simp add: sorted_wrt_append)
lemma sorted_map: "sorted (map f xs) = sorted_wrt (\<lambda>x y. f x \<le> f y) xs" by (simp add: sorted_wrt_map)
lemma sorted_rev_iff_nth_mono: "sorted (rev xs) \<longleftrightarrow> (\<forall> i j. i \<le> j \<longrightarrow> j < length xs \<longrightarrow> xs!j \<le> xs!i)" (is "?L = ?R")
proof
assume ?L thus ?R by (blast intro: sorted_rev_nth_mono)
next
assume ?R
have "rev xs ! k \<le> rev xs ! l"if asms: "k \<le> l""l < length(rev xs)" for k l
proof -
have "k < length xs""l < length xs" "length xs - Suc l \<le> length xs - Suc k""length xs - Suc k < length xs"
using asms byauto
thus "rev xs ! k \<le> rev xs ! l" by (simp add: \<open>?R\<close> rev_nth)
qed
thus ?L by (simp add: sorted_iff_nth_mono)
qed
lemma sorted_rev_iff_nth_Suc: "sorted (rev xs) \<longleftrightarrow> (\<forall>i. Suc i < length xs \<longrightarrow> xs!(Suc i) \<le> xs!i)"
proof-
interpret dual: linorder "(\<lambda>x y. y \<le> x)""(\<lambda>x y. y < x)"
using dual_linorder .
show ?thesis
using dual_linorder dual.sorted_iff_nth_Suc dual.sorted_iff_nth_mono
unfolding sorted_rev_iff_nth_mono by simp
qed
lemma sorted_map_remove1: "sorted (map f xs) \<Longrightarrow> sorted (map f (remove1 x xs))" by (induct xs) (auto)
lemma sorted_remove1: "sorted xs \<Longrightarrow> sorted (remove1 a xs)"
using sorted_map_remove1 [of"\<lambda>x. x"] by simp
lemma sorted_distinct_set_unique:
assumes "sorted xs""distinct xs""sorted ys""distinct ys""set xs = set ys"
shows "xs = ys"
proof - from assms have 1: "length xs = length ys"by (auto dest!: distinct_card) from assms show ?thesis
proof(induct rule:list_induct2[OF1])
case 1 show ?case by simp
next
case (2 x xs y ys) then show ?case by (cases \<open>x = y\<close>) (auto simp add: insert_eq_iff)
qed
qed
lemma map_sorted_distinct_set_unique:
assumes "inj_on f (set xs \<union> set ys)"
assumes "sorted (map f xs)""distinct (map f xs)" "sorted (map f ys)""distinct (map f ys)"
assumes "set xs = set ys"
shows "xs = ys"
using assms map_inj_on sorted_distinct_set_unique by fastforce
lemma sorted_dropWhile: "sorted xs \<Longrightarrow> sorted (dropWhile P xs)" by (auto dest: sorted_wrt_drop simp add: dropWhile_eq_drop)
lemma sorted_takeWhile: "sorted xs \<Longrightarrow> sorted (takeWhile P xs)" by (subst takeWhile_eq_take) (auto dest: sorted_wrt_take)
lemma sorted_filter: "sorted (map f xs) \<Longrightarrow> sorted (map f (filter P xs))" by (induct xs) simp_all
lemma foldr_max_sorted:
assumes "sorted (rev xs)"
shows "foldr max xs y = (if xs = [] then y else max (xs ! 0) y)"
using assms
proof (induct xs)
case (Cons x xs) then have "sorted (rev xs)" using sorted_append byauto with Cons show ?case by (cases xs) (auto simp add: sorted_append max_def)
qed simp
lemma filter_equals_takeWhile_sorted_rev:
assumes sorted: "sorted (rev (map f xs))"
shows "filter (\<lambda>x. t < f x) xs = takeWhile (\<lambda> x. t < f x) xs"
(is "filter ?P xs = ?tW")
proof (rule takeWhile_eq_filter[symmetric]) let"?dW" = "dropWhile ?P xs"
fix x assume x: "x \<in> set ?dW" then obtain i where i: "i < length ?dW"and nth_i: "x = ?dW ! i"
unfolding in_set_conv_nth byauto
hence "length ?tW + i < length (?tW @ ?dW)"
unfolding length_append by simp
hence i': "length (map f ?tW) + i < length (map f xs)" by simp
have "(map f ?tW @ map f ?dW) ! (length (map f ?tW) + i) \<le>
(map f ?tW @ map f ?dW) ! (length (map f ?tW) + 0)"
using sorted_rev_nth_mono[OF sorted _ i', of "length ?tW"]
unfolding map_append[symmetric] by simp
hence "f x \<le> f (?dW ! 0)"
unfolding nth_append_length_plus nth_i
using i preorder_class.le_less_trans[OF le0 i] by simp
also have "... \<le> t" by (metis hd_conv_nth hd_dropWhile length_greater_0_conv length_pos_if_in_set local.leI x)
finally show "\<not> t < f x"by simp
qed
lemma sorted_map_same: "sorted (map f (filter (\<lambda>x. f x = g xs) xs))"
proof (induct xs arbitrary: g)
case Nil then show ?case by simp
next
case (Cons x xs) then have "sorted (map f (filter (\<lambda>y. f y = (\<lambda>xs. f x) xs) xs))" .
moreover from Cons have "sorted (map f (filter (\<lambda>y. f y = (g \<circ> Cons x) xs) xs))" .
ultimately show ?case by simp_all
qed
lemma sorted_same: "sorted (filter (\<lambda>x. x = g xs) xs)"
using sorted_map_same [of"\<lambda>x. x"] by simp
text\<open>Currently it is not shown that \<^const>\<open>sort\<close> returns a
permutation of its input because the nicest proof is via multisets,
which are not part of Main. Alternatively one could define a function
that counts the number of occurrences of an element in a list and use
that instead of multisets to state the correctness property.\<close>
context linorder begin
lemma set_insort_key: "set (insort_key f x xs) = insert x (set xs)" by (induct xs) auto
lemma length_insort [simp]: "length (insort_key f x xs) = Suc (length xs)" by (induct xs) simp_all
lemma insort_key_left_comm:
assumes "f x \<noteq> f y"
shows "insort_key f y (insort_key f x xs) = insort_key f x (insort_key f y xs)" by (induct xs) (auto simp add: assms dest: order.antisym)
lemma insort_left_comm: "insort x (insort y xs) = insort y (insort x xs)" by (cases"x = y") (auto intro: insort_key_left_comm)
lemma comp_fun_commute_insort: "comp_fun_commute insort"
proof
qed (simp add: insort_left_comm fun_eq_iff)
lemma sort_key_simps [simp]: "sort_key f [] = []" "sort_key f (x#xs) = insort_key f x (sort_key f xs)" by (simp_all add: sort_key_def)
lemma sort_key_conv_fold:
assumes "inj_on f (set xs)"
shows "sort_key f xs = fold (insort_key f) xs []"
proof -
have "fold (insort_key f) (rev xs) = fold (insort_key f) xs"
proof (rule fold_rev, rule ext)
fix zs
fix x y
assume "x \<in> set xs""y \<in> set xs" with assms have *: "f y = f x \<Longrightarrow> y = x"by (auto dest: inj_onD)
have **: "x = y \<longleftrightarrow> y = x"byauto
show "(insort_key f y \<circ> insort_key f x) zs = (insort_key f x \<circ> insort_key f y) zs" by (induct zs) (auto intro: * simp add: **)
qed then show ?thesis by (simp add: sort_key_def foldr_conv_fold)
qed
lemma length_sort[simp]: "length (sort_key f xs) = length xs" by (induct xs, auto)
lemma set_sort[simp]: "set(sort_key f xs) = set xs" by (induct xs) (simp_all add: set_insort_key)
lemma distinct_insort: "distinct (insort_key f x xs) = (x \<notin> set xs \<and> distinct xs)" by(induct xs)(auto simp: set_insort_key)
lemma distinct_insort_key: "distinct (map f (insort_key f x xs)) = (f x \<notin> f ` set xs \<and> (distinct (map f xs)))" by (induct xs) (auto simp: set_insort_key)
lemma distinct_sort[simp]: "distinct (sort_key f xs) = distinct xs" by (induct xs) (simp_all add: distinct_insort)
lemma sorted_insort_key: "sorted (map f (insort_key f x xs)) = sorted (map f xs)" by (induct xs) (auto simp: set_insort_key)
lemma sorted_insort: "sorted (insort x xs) = sorted xs"
using sorted_insort_key [where f="\<lambda>x. x"] by simp
theorem sorted_sort_key [simp]: "sorted (map f (sort_key f xs))" by (induct xs) (auto simp:sorted_insort_key)
theorem sorted_sort [simp]: "sorted (sort xs)"
using sorted_sort_key [where f="\<lambda>x. x"] by simp
lemma insort_not_Nil [simp]: "insort_key f a xs \<noteq> []" by (induction xs) simp_all
lemma insort_is_Cons: "\<forall>x\<in>set xs. f a \<le> f x \<Longrightarrow> insort_key f a xs = a # xs" by (cases xs) auto
lemma sort_key_id_if_sorted: "sorted (map f xs) \<Longrightarrow> sort_key f xs = xs" by (induction xs) (auto simp add: insort_is_Cons)
text \<open>Subsumed by @{thm sort_key_id_if_sorted} but easier to find:\<close> lemma sorted_sort_id: "sorted xs \<Longrightarrow> sort xs = xs" by (simp add: sort_key_id_if_sorted)
lemma sort_replicate [simp]: "sort (replicate n x) = replicate n x"
using sorted_replicate sorted_sort_id by presburger
lemma insort_key_remove1:
assumes "a \<in> set xs"and"sorted (map f xs)"and"hd (filter (\<lambda>x. f a = f x) xs) = a"
shows "insort_key f a (remove1 a xs) = xs"
using assms proof (induct xs)
case (Cons x xs) then show ?case
proof (cases"x = a")
case False then have "f x \<noteq> f a" using Cons.prems byauto then have "f x < f a" using Cons.prems byauto with \<open>f x \<noteq> f a\<close> show ?thesis using Cons by (auto simp: insort_is_Cons)
qed (auto simp: insort_is_Cons)
qed simp
lemma insort_remove1:
assumes "a \<in> set xs"and"sorted xs"
shows "insort a (remove1 a xs) = xs"
proof (rule insort_key_remove1)
define n where"n = length (filter ((=) a) xs) - 1" from \<open>a \<in> set xs\<close> show "a \<in> set xs" . from \<open>sorted xs\<close> show "sorted (map (\<lambda>x. x) xs)"by simp from \<open>a \<in> set xs\<close> have "a \<in> set (filter ((=) a) xs)"byauto then have "set (filter ((=) a) xs) \<noteq> {}"byauto then have "filter ((=) a) xs \<noteq> []"by (auto simp only: set_empty) then have "length (filter ((=) a) xs) > 0"by simp then have n: "Suc n = length (filter ((=) a) xs)"by (simp add: n_def)
moreover have "replicate (Suc n) a = a # replicate n a" by simp
ultimately show "hd (filter ((=) a) xs) = a"by (simp add: replicate_length_filter)
qed
lemma finite_sorted_distinct_unique:
assumes "finite A" shows "\<exists>!xs. set xs = A \<and> sorted xs \<and> distinct xs"
proof -
obtain xs where"distinct xs""A = set xs"
using finite_distinct_list [OF assms] by metis then show ?thesis by (rule_tac a="sort xs"in ex1I) (auto simp: sorted_distinct_set_unique)
qed
lemma insort_insert_key_triv: "f x \<in> f ` set xs \<Longrightarrow> insort_insert_key f x xs = xs" by (simp add: insort_insert_key_def)
lemma insort_insert_triv: "x \<in> set xs \<Longrightarrow> insort_insert x xs = xs"
using insort_insert_key_triv [of"\<lambda>x. x"] by simp
lemma insort_insert_insort_key: "f x \<notin> f ` set xs \<Longrightarrow> insort_insert_key f x xs = insort_key f x xs" by (simp add: insort_insert_key_def)
lemma insort_insert_insort: "x \<notin> set xs \<Longrightarrow> insort_insert x xs = insort x xs"
using insort_insert_insort_key [of"\<lambda>x. x"] by simp
lemma set_insort_insert: "set (insort_insert x xs) = insert x (set xs)" by (auto simp add: insort_insert_key_def set_insort_key)
lemma distinct_insort_insert:
assumes "distinct xs"
shows "distinct (insort_insert_key f x xs)"
using assms by (induct xs) (auto simp add: insort_insert_key_def set_insort_key)
lemma sorted_insort_insert_key:
assumes "sorted (map f xs)"
shows "sorted (map f (insort_insert_key f x xs))"
using assms by (simp add: insort_insert_key_def sorted_insort_key)
lemma sorted_insort_insert:
assumes "sorted xs"
shows "sorted (insort_insert x xs)"
using assms sorted_insort_insert_key [of "\<lambda>x. x"] by simp
lemma filter_insort_triv:
"\<not> P x \<Longrightarrow> filter P (insort_key f x xs) = filter P xs"
by (induct xs) simp_all
lemma filter_insort:
"sorted (map f xs) \<Longrightarrow> P x \<Longrightarrow> filter P (insort_key f x xs) = insort_key f x (filter P xs)"
by (induct xs) (auto, subst insort_is_Cons, auto)
lemma filter_sort:
"filter P (sort_key f xs) = sort_key f (filter P xs)"
by (induct xs) (simp_all add: filter_insort_triv filter_insort)
lemma remove1_insort_key [simp]:
"remove1 x (insort_key f x xs) = xs"
by (induct xs) simp_all
lemma sorted_find_Min:
"sorted xs \<Longrightarrow> \<exists>x \<in> set xs. P x \<Longrightarrow> List.find P xs = Some (Min {x\<in>set xs. P x})"
proof (induct xs)
case Nil then show ?case by simp
next
case (Cons x xs) show ?case proof (cases "P x")
case True
with Cons show ?thesis by (auto intro: Min_eqI [symmetric])
next
case False then have "{y. (y = x \<or> y \<in> set xs) \<and> P y} = {y \<in> set xs. P y}"
by auto
with Cons False show ?thesis by (simp_all)
qed
qed
lemma sorted_enumerate [simp]: "sorted (map fst (enumerate n xs))"
by (simp add: enumerate_eq_zip)
lemma sorted_insort_is_snoc: "sorted xs \<Longrightarrow> \<forall>x \<in> set xs. a \<ge> x \<Longrightarrow> insort a xs = xs @ [a]"
by (induct xs) (auto dest!: insort_is_Cons)
text \<open>Stability of \<^const>\<open>sort_key\<close>:\<close>
lemma sort_key_stable: "filter (\<lambda>y. f y = k) (sort_key f xs) = filter (\<lambda>y. f y = k) xs"
by (induction xs) (auto simp: filter_insort insort_is_Cons filter_insort_triv)
lemma transpose_max_length:
"foldr (\<lambda>xs. max (length xs)) (transpose xs) 0 = length (filter (\<lambda>x. x \<noteq> []) xs)"
(is "?L = ?R")
proof (cases "transpose xs = []")
case False
have "?L = foldr max (map length (transpose xs)) 0"
by (simp add: foldr_map comp_def)
also have "... = length (transpose xs ! 0)"
using False sorted_transpose by (simp add: foldr_max_sorted)
finally show ?thesis
using False by (simp add: nth_transpose)
next
case True
hence "filter (\<lambda>x. x \<noteq> []) xs = []"
by (auto intro!: filter_False simp: transpose_empty)
thus ?thesis by (simp add: transpose_empty True)
qed
lemma length_transpose_sorted:
fixes xs :: "'a list list"
assumes sorted: "sorted (rev (map length xs))"
shows "length (transpose xs) = (if xs = [] then 0 else length (xs ! 0))"
proof (cases "xs = []")
case False
thus ?thesis
using foldr_max_sorted[OF sorted] False
unfolding length_transpose foldr_map comp_def
by simp
qed simp
lemma nth_nth_transpose_sorted[simp]:
fixes xs :: "'a list list"
assumes sorted: "sorted (rev (map length xs))"
and i: "i < length (transpose xs)"
and j: "j < length (filter (\<lambda>ys. i < length ys) xs)"
shows "transpose xs ! i ! j = xs ! j ! i"
using j filter_equals_takeWhile_sorted_rev[OF sorted, of i]
nth_transpose[OF i] nth_map[OF j]
by (simp add: takeWhile_nth)
lemma transpose_column_length:
fixes xs :: "'a list list"
assumes sorted: "sorted (rev (map length xs))" and "i < length xs"
shows "length (filter (\<lambda>ys. i < length ys) (transpose xs)) = length (xs ! i)"
proof -
have "xs \<noteq> []" using \<open>i < length xs\<close> by auto
note filter_equals_takeWhile_sorted_rev[OF sorted, simp]
{ fix j assume "j \<le> i"
note sorted_rev_nth_mono[OF sorted, of j i, simplified, OF this \<open>i < length xs\<close>]
} note sortedE = this[consumes 1]
have "{j. j < length (transpose xs) \<and> i < length (transpose xs ! j)}
= {..< length (xs ! i)}"
proof safe
fix j
assume "j < length (transpose xs)" and "i < length (transpose xs ! j)"
with this(2) nth_transpose[OF this(1)]
have "i < length (takeWhile (\<lambda>ys. j < length ys) xs)" by simp
from nth_mem[OF this] takeWhile_nth[OF this]
show "j < length (xs ! i)" by (auto dest: set_takeWhileD)
next
fix j assume "j < length (xs ! i)"
thus "j < length (transpose xs)"
using foldr_max_sorted[OF sorted] \<open>xs \<noteq> []\<close> sortedE[OF le0]
by (auto simp: length_transpose comp_def foldr_map)
have "Suc i \<le> length (takeWhile (\<lambda>ys. j < length ys) xs)"
using \<open>i < length xs\<close> \<open>j < length (xs ! i)\<close> less_Suc_eq_le
by (auto intro!: length_takeWhile_less_P_nth dest!: sortedE)
with nth_transpose[OF \<open>j < length (transpose xs)\<close>]
show "i < length (transpose xs ! j)" by simp
qed
thus ?thesis by (simp add: length_filter_conv_card)
qed
lemma transpose_column:
fixes xs :: "'a list list"
assumes sorted: "sorted (rev (map length xs))" and "i < length xs"
shows "map (\<lambda>ys. ys ! i) (filter (\<lambda>ys. i < length ys) (transpose xs))
= xs ! i" (is "?R = _")
proof (rule nth_equalityI)
show length: "length ?R = length (xs ! i)"
using transpose_column_length[OF assms] by simp
fix j assume j: "j < length ?R"
note * = less_le_trans[OF this, unfolded length_map, OF length_filter_le]
from j have j_less: "j < length (xs ! i)" using length by simp
have i_less_tW: "Suc i \<le> length (takeWhile (\<lambda>ys. Suc j \<le> length ys) xs)"
proof (rule length_takeWhile_less_P_nth)
show "Suc i \<le> length xs" using \<open>i < length xs\<close> by simp
fix k assume "k < Suc i"
hence "k \<le> i" by auto
with sorted_rev_nth_mono[OF sorted this] \<open>i < length xs\<close>
have "length (xs ! i) \<le> length (xs ! k)" by simp
thus "Suc j \<le> length (xs ! k)" using j_less by simp
qed
have i_less_filter: "i < length (filter (\<lambda>ys. j < length ys) xs) "
unfolding filter_equals_takeWhile_sorted_rev[OF sorted, of j]
using i_less_tW by (simp_all add: Suc_le_eq)
from j show "?R ! j = xs ! i ! j"
unfolding filter_equals_takeWhile_sorted_rev[OF sorted_transpose, of i]
by (simp add: takeWhile_nth nth_nth_transpose_sorted[OF sorted * i_less_filter])
qed
lemma transpose_transpose:
fixes xs :: "'a list list"
assumes sorted: "sorted (rev (map length xs))"
shows "transpose (transpose xs) = takeWhile (\<lambda>x. x \<noteq> []) xs" (is "?L = ?R")
proof -
have len: "length ?L = length ?R"
unfolding length_transpose transpose_max_length
using filter_equals_takeWhile_sorted_rev[OF sorted, of 0]
by simp
{ fix i assume "i < length ?R"
with less_le_trans[OF _ length_takeWhile_le[of _ xs]]
have "i < length xs" by simp
} note * = this
show ?thesis
by (rule nth_equalityI)
(simp_all add: len nth_transpose transpose_column[OF sorted] * takeWhile_nth)
qed
theorem transpose_rectangle:
assumes "xs = [] \<Longrightarrow> n = 0"
assumes rect: "\<And> i. i < length xs \<Longrightarrow> length (xs ! i) = n"
shows "transpose xs = map (\<lambda> i. map (\<lambda> j. xs ! j ! i) [0..<length xs]) [0..<n]"
(is "?trans = ?map")
proof (rule nth_equalityI)
have "sorted (rev (map length xs))"
by (auto simp: rev_nth rect sorted_iff_nth_mono)
from foldr_max_sorted[OF this] assms
show len: "length ?trans = length ?map"
by (simp_all add: length_transpose foldr_map comp_def)
moreover
{ fix i assume "i < n" hence "filter (\<lambda>ys. i < length ys) xs = xs"
using rect by (auto simp: in_set_conv_nth intro!: filter_True) }
ultimately show "\<And>i. i < length (transpose xs) \<Longrightarrow> ?trans ! i = ?map ! i"
by (auto simp: nth_transpose intro: nth_equalityI)
qed
text\<open>
This function maps (finite) linearly ordered sets to sorted lists.
The linear order is obtained by a key function that maps the elements of the set to a type
that is linearly ordered.
Warning: in most cases it is not a good idea to convert from
sets to lists but one should convert in the other direction (via \<^const>\<open>set\<close>).
Note: this is a generalisation of the older \<open>sorted_list_of_set\<close> that is obtained by setting
the key function to the identity. Consequently, new theorems should be added to the locale
below. They should also be aliased to more convenient names for use with \<open>sorted_list_of_set\<close>
as seen further below.
\<close>
definition (in linorder) sorted_key_list_of_set :: "('b \<Rightarrow> 'a) \<Rightarrow> 'b set \<Rightarrow> 'b list"
where "sorted_key_list_of_set f \<equiv> folding_on.F (insort_key f) []"
locale folding_insort_key = lo?: linorder "less_eq :: 'a \<Rightarrow> 'a \<Rightarrow> bool" less
for less_eq (infix \<open>\<preceq>\<close> 50) and less (infix \<open>\<prec>\<close> 50) +
fixes S
fixes f :: "'b \<Rightarrow> 'a"
assumes inj_on: "inj_on f S"
begin
lemma insort_key_commute:
"x \<in> S \<Longrightarrow> y \<in> S \<Longrightarrow> insort_key f y o insort_key f x = insort_key f x o insort_key f y"
proof(rule ext, goal_cases)
case (1 xs)
with inj_on show ?case by (induction xs) (auto simp: inj_onD)
qed
sublocale fold_insort_key: folding_on S "insort_key f" "[]"
rewrites "folding_on.F (insort_key f) [] = sorted_key_list_of_set f"
proof -
show "folding_on S (insort_key f)"
by standard (simp add: insort_key_commute)
qed (simp add: sorted_key_list_of_set_def)
lemma idem_if_sorted_distinct:
assumes "set xs \<subseteq> S" and "sorted (map f xs)" "distinct xs"
shows "sorted_key_list_of_set f (set xs) = xs"
proof(cases "S = {}")
case True
then show ?thesis using \<open>set xs \<subseteq> S\<close> by auto
next
case False
with assms show ?thesis
proof(induction xs)
case (Cons a xs)
with Cons show ?case by (cases xs) auto
qed simp
qed
lemma sorted_key_list_of_set_empty:
"sorted_key_list_of_set f {} = []"
by (fact fold_insort_key.empty)
lemma sorted_key_list_of_set_insert:
assumes "insert x A \<subseteq> S" and "finite A" "x \<notin> A"
shows "sorted_key_list_of_set f (insert x A)
= insort_key f x (sorted_key_list_of_set f A)"
using assms by (fact fold_insort_key.insert)
lemma sorted_key_list_of_set_insert_remove [simp]:
assumes "insert x A \<subseteq> S" and "finite A"
shows "sorted_key_list_of_set f (insert x A)
= insort_key f x (sorted_key_list_of_set f (A - {x}))"
using assms by (fact fold_insort_key.insert_remove)
lemma sorted_key_list_of_set_eq_Nil_iff [simp]:
assumes "A \<subseteq> S" and "finite A"
shows "sorted_key_list_of_set f A = [] \<longleftrightarrow> A = {}"
using assms by (auto simp: fold_insort_key.remove)
lemma set_sorted_key_list_of_set [simp]:
assumes "A \<subseteq> S" and "finite A"
shows "set (sorted_key_list_of_set f A) = A"
using assms(2,1)
by (induct A rule: finite_induct) (simp_all add: set_insort_key)
lemma sorted_sorted_key_list_of_set [simp]:
assumes "A \<subseteq> S"
shows "sorted (map f (sorted_key_list_of_set f A))"
proof (cases "finite A")
case True thus ?thesis using \<open>A \<subseteq> S\<close>
by (induction A) (simp_all add: sorted_insort_key)
next
case False thus ?thesis by simp
qed
lemma distinct_if_distinct_map: "distinct (map f xs) \<Longrightarrow> distinct xs"
using inj_on by (simp add: distinct_map)
lemma distinct_sorted_key_list_of_set [simp]:
assumes "A \<subseteq> S"
shows "distinct (map f (sorted_key_list_of_set f A))"
proof (cases "finite A")
case True thus ?thesis using \<open>A \<subseteq> S\<close> inj_on
by (induction A) (force simp: distinct_insort_key dest: inj_onD)+
next
case False thus ?thesis by simp
qed
lemma length_sorted_key_list_of_set [simp]:
assumes "A \<subseteq> S"
shows "length (sorted_key_list_of_set f A) = card A"
proof (cases "finite A")
case True
with assms inj_on show ?thesis
using distinct_card[symmetric, OF distinct_sorted_key_list_of_set]
by (auto simp: inj_on_subset intro!: card_image)
qed auto
lemma sorted_key_list_of_set_remove:
assumes "insert x A \<subseteq> S" and "finite A"
shows "sorted_key_list_of_set f (A - {x}) = remove1 x (sorted_key_list_of_set f A)"
proof (cases "x \<in> A")
case False with assms have "x \<notin> set (sorted_key_list_of_set f A)" by simp
with False show ?thesis by (simp add: remove1_idem)
next
case True then obtain B where A: "A = insert x B" by (rule Set.set_insert)
with assms show ?thesis by simp
qed
lemma strict_sorted_key_list_of_set [simp]:
"A \<subseteq> S \<Longrightarrow> sorted_wrt (\<prec>) (map f (sorted_key_list_of_set f A))"
by (cases "finite A") (auto simp: strict_sorted_iff inj_on_subset[OF inj_on])
lemma finite_set_strict_sorted:
assumes "A \<subseteq> S" and "finite A"
obtains l where "sorted_wrt (\<prec>) (map f l)" "set l = A" "length l = card A"
using assms
by (meson length_sorted_key_list_of_set set_sorted_key_list_of_set strict_sorted_key_list_of_set)
lemma (in linorder) strict_sorted_equal:
assumes "sorted_wrt (<) xs"
and "sorted_wrt (<) ys"
and "set ys = set xs"
shows "ys = xs"
using assms
proof (induction xs arbitrary: ys)
case (Cons x xs)
show ?case
proof (cases ys)
case Nil
then show ?thesis
using Cons.prems by auto
next
case (Cons y ys')
then have "xs = ys'"
by (metis Cons.prems list.inject sorted_distinct_set_unique strict_sorted_iff)
moreover have "x = y"
using Cons.prems \<open>xs = ys'\<close> local.Cons by fastforce
ultimately show ?thesis
using local.Cons by blast
qed
qed auto
lemma (in linorder) strict_sorted_equal_Uniq: "\<exists>\<^sub>\<le>\<^sub>1xs. sorted_wrt (<) xs \<and> set xs = A"
by (simp add: Uniq_def strict_sorted_equal)
lemma sorted_key_list_of_set_inject:
assumes "A \<subseteq> S" "B \<subseteq> S"
assumes "sorted_key_list_of_set f A = sorted_key_list_of_set f B" "finite A" "finite B"
shows "A = B"
using assms set_sorted_key_list_of_set by metis
lemma sorted_key_list_of_set_unique:
assumes "A \<subseteq> S" and "finite A"
shows "sorted_wrt (\<prec>) (map f l) \<and> set l = A \<and> length l = card A
\<longleftrightarrow> sorted_key_list_of_set f A = l"
using assms
by (auto simp: strict_sorted_iff card_distinct idem_if_sorted_distinct)
text \<open>
We abuse the \<open>rewrites\<close> functionality of locales to remove trivial assumptions that result
from instantiating the key function to the identity.
\<close>
sublocale sorted_list_of_set: folding_insort_key "(\<le>)" "(<)" UNIV "(\<lambda>x. x)"
rewrites "sorted_key_list_of_set (\<lambda>x. x) = sorted_list_of_set"
and "\<And>xs. map (\<lambda>x. x) xs \<equiv> xs"
and "\<And>X. (X \<subseteq> UNIV) \<equiv> True"
and "\<And>x. x \<in> UNIV \<equiv> True"
and "\<And>P. (True \<Longrightarrow> P) \<equiv> Trueprop P"
and "\<And>P Q. (True \<Longrightarrow> PROP P \<Longrightarrow> PROP Q) \<equiv> (PROP P \<Longrightarrow> True \<Longrightarrow> PROP Q)"
proof -
show "folding_insort_key (\<le>) (<) UNIV (\<lambda>x. x)"
by standard simp
qed (simp_all add: sorted_list_of_set_def)
lemma ex1_sorted_list_for_set_if_finite:
"finite X \<Longrightarrow> \<exists>!xs. sorted_wrt (<) xs \<and> set xs = X"
by (metis sorted_list_of_set.finite_set_strict_sorted strict_sorted_equal)
text \<open>Alias theorems for backwards compatibility and ease of use.\<close>
lemmas sorted_list_of_set = sorted_list_of_set.sorted_key_list_of_set and
sorted_list_of_set_empty = sorted_list_of_set.sorted_key_list_of_set_empty and
sorted_list_of_set_insert = sorted_list_of_set.sorted_key_list_of_set_insert and
sorted_list_of_set_insert_remove = sorted_list_of_set.sorted_key_list_of_set_insert_remove and
sorted_list_of_set_eq_Nil_iff = sorted_list_of_set.sorted_key_list_of_set_eq_Nil_iff and
set_sorted_list_of_set = sorted_list_of_set.set_sorted_key_list_of_set and
sorted_sorted_list_of_set = sorted_list_of_set.sorted_sorted_key_list_of_set and
distinct_sorted_list_of_set = sorted_list_of_set.distinct_sorted_key_list_of_set and
length_sorted_list_of_set = sorted_list_of_set.length_sorted_key_list_of_set and
sorted_list_of_set_remove = sorted_list_of_set.sorted_key_list_of_set_remove and
strict_sorted_list_of_set = sorted_list_of_set.strict_sorted_key_list_of_set and
sorted_list_of_set_inject = sorted_list_of_set.sorted_key_list_of_set_inject and
sorted_list_of_set_unique = sorted_list_of_set.sorted_key_list_of_set_unique and
finite_set_strict_sorted = sorted_list_of_set.finite_set_strict_sorted
lemma sorted_list_of_set_sort_remdups [code]:
"sorted_list_of_set (set xs) = sort (remdups xs)"
proof -
interpret comp_fun_commute insort by (fact comp_fun_commute_insort)
show ?thesis
by (simp add: sorted_list_of_set.fold_insort_key.eq_fold sort_conv_fold fold_set_fold_remdups)
qed
lemma sorted_list_of_set_nonempty:
assumes "finite A" "A \<noteq> {}"
shows "sorted_list_of_set A = Min A # sorted_list_of_set (A - {Min A})"
using assms
by (auto simp: less_le simp flip: sorted_list_of_set.sorted_key_list_of_set_unique intro: Min_in)
lemma sorted_list_of_set_greaterThanLessThan:
assumes "Suc i < j"
shows "sorted_list_of_set {i<..<j} = Suc i # sorted_list_of_set {Suc i<..<j}"
proof -
have "{i<..<j} = insert (Suc i) {Suc i<..<j}"
using assms by auto
then show ?thesis
by (metis assms atLeastSucLessThan_greaterThanLessThan sorted_list_of_set_range upt_conv_Cons)
qed
lemma sorted_list_of_set_greaterThanAtMost:
assumes "Suc i \<le> j"
shows "sorted_list_of_set {i<..j} = Suc i # sorted_list_of_set {Suc i<..j}"
using sorted_list_of_set_greaterThanLessThan [of i "Suc j"]
by (metis assms greaterThanAtMost_def greaterThanLessThan_eq le_imp_less_Suc lessThan_Suc_atMost)
lemma nth_sorted_list_of_set_greaterThanLessThan:
"n < j - Suc i \<Longrightarrow> sorted_list_of_set {i<..<j} ! n = Suc (i+n)"
by (induction n arbitrary: i) (auto simp: sorted_list_of_set_greaterThanLessThan)
lemma nth_sorted_list_of_set_greaterThanAtMost:
"n < j - i \<Longrightarrow> sorted_list_of_set {i<..j} ! n = Suc (i+n)"
using nth_sorted_list_of_set_greaterThanLessThan [of n "Suc j" i]
by (simp add: greaterThanAtMost_def greaterThanLessThan_eq lessThan_Suc_atMost)
lemma sorted_wrt_induct [consumes 1, case_names Nil Cons]:
assumes "sorted_wrt R xs"
assumes "P []"
"\<And>x xs. (\<And>y. y \<in> set xs \<Longrightarrow> R x y) \<Longrightarrow> P xs \<Longrightarrow> P (x # xs)"
shows "P xs"
using assms(1) by (induction xs) (auto intro: assms)
lemma sorted_wrt_trans_induct [consumes 2, case_names Nil single Cons]:
assumes "sorted_wrt R xs" "transp R"
assumes "P []" "\<And>x. P [x]"
"\<And>x y xs. R x y \<Longrightarrow> P (y # xs) \<Longrightarrow> P (x # y # xs)"
shows "P xs"
using assms(1)
by (induction xs rule: induct_list012)
(auto intro: assms simp: sorted_wrt2[OF assms(2)])
lemma sorted_wrt_map_mono:
assumes "sorted_wrt R xs"
assumes "\<And>x y. x \<in> set xs \<Longrightarrow> y \<in> set xs \<Longrightarrow> R x y \<Longrightarrow> R' (f x) (f y)"
shows "sorted_wrt R' (map f xs)"
using assms by (induction rule: sorted_wrt_induct) auto
lemma sorted_map_mono:
assumes "sorted xs" and "mono_on (set xs) f"
shows "sorted (map f xs)"
using assms(1)
by (rule sorted_wrt_map_mono) (use assms in \<open>auto simp: mono_on_def\<close>)
subsubsection \<open>\<open>lists\<close>: the list-forming operator over sets\<close>
inductive_set
lists :: "'a set => 'a list set"
for A :: "'a set"
where
Nil [intro!, simp]: "[] \<in> lists A"
| Cons [intro!, simp]: "\<lbrakk>a \<in> A; l \<in> lists A\<rbrakk> \<Longrightarrow> a#l \<in> lists A"
inductive_simps listsp_simps[code]:
"listsp A []"
"listsp A (x # xs)"
lemma listsp_mono [mono]: "A \<le> B \<Longrightarrow> listsp A \<le> listsp B"
by (rule predicate1I, erule listsp.induct, blast+)
lemmas lists_mono = listsp_mono [to_set]
lemma listsp_infI:
assumes l: "listsp A l" shows "listsp B l \<Longrightarrow> listsp (inf A B) l" using l
by induct blast+
lemmas lists_IntI = listsp_infI [to_set]
lemma listsp_inf_eq [simp]: "listsp (inf A B) = inf (listsp A) (listsp B)"
proof (rule mono_inf [where f=listsp, THEN order_antisym])
show "mono listsp" by (simp add: mono_def listsp_mono)
show "inf (listsp A) (listsp B) \<le> listsp (inf A B)" by (blast intro!: listsp_infI)
qed
lemma in_listsp_conv_set: "(listsp A xs) = (\<forall>x \<in> set xs. A x)"
\<comment> \<open>eliminate \<open>listsp\<close> in favour of \<open>set\<close>\<close>
by (induct xs) auto
lemma in_listspD [dest!]: "listsp A xs \<Longrightarrow> \<forall>x\<in>set xs. A x"
by (rule in_listsp_conv_set [THEN iffD1])
lemmas in_listsD [dest!] = in_listspD [to_set]
lemma in_listspI [intro!]: "\<forall>x\<in>set xs. A x \<Longrightarrow> listsp A xs"
by (rule in_listsp_conv_set [THEN iffD2])
lemmas in_listsI [intro!] = in_listspI [to_set]
lemma mono_lists: "mono lists"
unfolding mono_def by auto
lemma lists_eq_set: "lists A = {xs. set xs \<le> A}"
by auto
lemma lists_empty [simp]: "lists {} = {[]}"
by auto
lemma lists_UNIV [simp]: "lists UNIV = UNIV"
by auto
lemma lists_image: "lists (f`A) = map f ` lists A"
proof -
{ fix xs have "\<forall>x\<in>set xs. x \<in> f ` A \<Longrightarrow> xs \<in> map f ` lists A"
by (induct xs) (auto simp del: list.map simp add: list.map[symmetric] intro!: imageI) }
then show ?thesis by auto
qed
lemma inj_on_map_lists: assumes "inj_on f A"
shows "inj_on (map f) (lists A)"
proof
fix xs ys
assume "xs \<in> lists A" and "ys \<in> lists A" and "map f xs = map f ys"
have "x = y" if "x \<in> set xs" and "y \<in> set ys" and "f x = f y" for x y
using in_listsD[OF \<open>xs \<in> lists A\<close>, rule_format, OF \<open>x \<in> set xs\<close>]
in_listsD[OF \<open>ys \<in> lists A\<close>, rule_format, OF \<open>y \<in> set ys\<close>]
\<open>inj_on f A\<close>[unfolded inj_on_def, rule_format, OF _ _ \<open>f x = f y\<close>] by blast
from list.inj_map_strong[OF this \<open>map f xs = map f ys\<close>]
show "xs = ys".
qed
lemma bij_lists: "bij_betw f X Y \<Longrightarrow> bij_betw (map f) (lists X) (lists Y)"
unfolding bij_betw_def using inj_on_map_lists lists_image by metis
lemma replicate_in_lists: "a \<in> A \<Longrightarrow> replicate k a \<in> lists A"
by (induction k) auto
subsubsection \<open>Inductive definition for membership\<close>
inductive ListMem :: "'a \<Rightarrow> 'a list \<Rightarrow> bool"
where
elem: "ListMem x (x # xs)"
| insert: "ListMem x xs \<Longrightarrow> ListMem x (y # xs)"
lemma ListMem_iff: "(ListMem x xs) = (x \<in> set xs)"
proof
show "ListMem x xs \<Longrightarrow> x \<in> set xs"
by (induct set: ListMem) auto
show "x \<in> set xs \<Longrightarrow> ListMem x xs"
by (induct xs) (auto intro: ListMem.intros)
qed
subsubsection \<open>Lists as Cartesian products\<close>
text\<open>\<open>set_Cons A Xs\<close>: the set of lists with head drawn from
\<^term>\<open>A\<close> and tail drawn from \<^term>\<open>Xs\<close>.\<close>
definition set_Cons :: "'a set \<Rightarrow> 'a list set \<Rightarrow> 'a list set" where
"set_Cons A XS = {z. \<exists>x xs. z = x # xs \<and> x \<in> A \<and> xs \<in> XS}"
lemma set_Cons_sing_Nil [simp]: "set_Cons A {[]} = (%x. [x])`A"
by (auto simp add: set_Cons_def)
text\<open>Yields the set of lists, all of the same length as the argument and
with elements drawn from the corresponding element of the argument.\<close>
primrec listset :: "'a set list \<Rightarrow> 'a list set" where
"listset [] = {[]}" |
"listset (A # As) = set_Cons A (listset As)"
subsubsection \<open>Transitive Closure on Lists\<close>
text \<open>Use \<open>\<^sup>+\<close> on binary relations if possible.
Transitive closure on lists is useful for executable definitions on the list level.
Is not efficient, naive closure computation.\<close>
lemma set_trans_list_step_subset_trancl: "set (trans_list_step ps) \<subseteq> (set ps)^+"
unfolding trans_list_step_def by auto
function trancl_list :: "('a * 'a) list \<Rightarrow> ('a * 'a) list" where
"trancl_list ps =
(let ps' = trans_list_step ps
in if set ps' \<subseteq> set ps then ps else trancl_list (List.union ps' ps))"
by pat_completeness auto
fix ps ps' :: "('a * 'a) list"
assume asms: "ps' = trans_list_step ps" "\<not> set ps' \<subseteq> set ps"
let ?P = "set ps" let ?P' = "set(trans_list_step ps)"
have "(?P' \<union> ?P)\<^sup>+ - (?P' \<union> ?P) = ?P\<^sup>+ - (?P' \<union> ?P)"
using trancl_absorb_subset_trancl[OF set_trans_list_step_subset_trancl] by (metis Un_commute)
also have "?P\<^sup>+ - (?P' \<union> ?P) < ?P\<^sup>+ - ?P"
using asms(1,2) set_trans_list_step_subset_trancl by fastforce
finally have "card((?P' \<union> ?P)\<^sup>+ - (?P' \<union> ?P)) < card (?P\<^sup>+ - ?P)"
by (meson List.finite_set finite_Diff finite_trancl psubset_card_mono)
with asms show "(List.union ps' ps, ps) \<in> measure ?r" by(simp)
qed
declare trancl_list.simps[code, simp del]
lemma set_trancl_list: "set(trancl_list ps) = (set ps)^+"
proof (induction ps rule: trancl_list.induct)
case (1 ps)
let ?P = "set ps" let ?P' = "set(trans_list_step ps)"
show ?case
proof (cases "?P' \<subseteq> ?P")
case True
then have "(a,b) \<in> set ps \<Longrightarrow> (b,c) \<in> set ps \<Longrightarrow> (a,c) \<in> set ps" for a b c
unfolding trans_list_step_def by fastforce
then show ?thesis using True trancl_id[OF transI, of ?P]
using [[simp_depth_limit=3]] by(simp add: Let_def trancl_list.simps[of ps])
next
case False
from 1[OF refl False] False
show ?thesis using trancl_absorb_subset_trancl[OF set_trans_list_step_subset_trancl]
by(auto simp add: Un_commute Let_def trancl_list.simps[of ps])
qed
qed
text\<open>These orderings preserve well-foundedness: shorter lists
precede longer lists. These ordering are not used in dictionaries.\<close>
primrec \<comment> \<open>The lexicographic ordering for lists of the specified length\<close>
lexn :: "('a \<times> 'a) set \<Rightarrow> nat \<Rightarrow> ('a list \<times> 'a list) set" where
"lexn r 0 = {}" |
"lexn r (Suc n) =
(map_prod (%(x, xs). x#xs) (%(x, xs). x#xs) ` (r <*lex*> lexn r n)) Int
{(xs, ys). length xs = Suc n \<and> length ys = Suc n}"
definition lex :: "('a \<times> 'a) set \<Rightarrow> ('a list \<times> 'a list) set" where
"lex r = (\<Union>n. lexn r n)" \<comment> \<open>Holds only between lists of the same length\<close>
definition lenlex :: "('a \<times> 'a) set => ('a list \<times> 'a list) set" where
"lenlex r = inv_image (less_than <*lex*> lex r) (\<lambda>xs. (length xs, xs))"
\<comment> \<open>Compares lists by their length and then lexicographically\<close>
lemma wf_lexn: assumes "wf r" shows "wf (lexn r n)"
proof (induct n)
case (Suc n)
have inj: "inj (\<lambda>(x, xs). x # xs)"
using assms by (auto simp: inj_on_def)
have wf: "wf (map_prod (\<lambda>(x, xs). x # xs) (\<lambda>(x, xs). x # xs) ` (r <*lex*> lexn r n))"
by (simp add: Suc.hyps assms wf_lex_prod wf_map_prod_image [OF _ inj])
then show ?case
by (rule wf_subset) auto
qed auto
lemma lexn_length:
"(xs, ys) \<in> lexn r n \<Longrightarrow> length xs = n \<and> length ys = n"
by (induct n arbitrary: xs ys) auto
lemma wf_lex [intro!]:
assumes "wf r" shows "wf (lex r)"
unfolding lex_def
proof (rule wf_UN)
show "wf (lexn r i)" for i
by (simp add: assms wf_lexn)
show "\<And>i j. lexn r i \<noteq> lexn r j \<Longrightarrow> Domain (lexn r i) \<inter> Range (lexn r j) = {}"
by (metis DomainE Int_emptyI RangeE lexn_length)
qed
lemma lexn_conv:
"lexn r n =
{(xs,ys). length xs = n \<and> length ys = n \<and>
(\<exists>xys x y xs' ys'. xs = xys @ x#xs' \<and> ys = xys @ y # ys' \<and> (x, y) \<in> r)}"
(is "?L n = ?R n" is "_ = {(xs,ys). ?len n xs \<and> ?len n ys \<and> (\<exists>xys. ?P xs ys xys)}")
proof (induction n)
case (Suc n)
(* A compact proof referring to a system-generated name:
then show ?case
apply (auto simp add: image_Collect lex_prod_def)
apply blast
apply (meson Cons_eq_appendI)
apply (case_tac xys; fastforce)
done
*)
have "(xs,ys) \<in> ?L (Suc n)" if r: "(xs,ys) \<in> ?R (Suc n)" for xs ys
proof -
from r obtain xys where r': "?len (Suc n) xs" "?len (Suc n) ys" "?P xs ys xys" by auto
then show ?thesis
using r' Suc
by (cases xys; fastforce simp: image_Collect lex_prod_def)
qed
moreover have "(xs,ys) \<in> ?L (Suc n) \<Longrightarrow> (xs,ys) \<in> ?R (Suc n)" for xs ys
using Suc by (auto simp add: image_Collect lex_prod_def)(blast, meson Cons_eq_appendI)
ultimately show ?case by (meson pred_equals_eq2)
qed auto
text\<open>By Mathias Fleury:\<close>
proposition lexn_transI:
assumes "trans r" shows "trans (lexn r n)"
unfolding trans_def
proof (intro allI impI)
fix as bs cs
assume asbs: "(as, bs) \<in> lexn r n" and bscs: "(bs, cs) \<in> lexn r n"
obtain abs a b as' bs' where
n: "length as = n" and "length bs = n" and
as: "as = abs @ a # as'" and
bs: "bs = abs @ b # bs'" and
abr: "(a, b) \<in> r"
using asbs unfolding lexn_conv by blast
obtain bcs b' c' cs' bs' where
n': "length cs = n" and "length bs = n" and
bs': "bs = bcs @ b' # bs'" and
cs: "cs = bcs @ c' # cs'" and
b'c'r: "(b', c') \<in> r"
using bscs unfolding lexn_conv by blast
consider (le) "length bcs < length abs"
| (eq) "length bcs = length abs"
| (ge) "length bcs > length abs" by linarith
thus "(as, cs) \<in> lexn r n"
proof cases
let ?k = "length bcs"
case le
hence "as ! ?k = bs ! ?k" unfolding as bs by (simp add: nth_append)
hence "(as ! ?k, cs ! ?k) \<in> r" using b'c'r unfolding bs' cs by auto
moreover
have "length bcs < length as" using le unfolding as by simp
from id_take_nth_drop[OF this]
have "as = take ?k as @ as ! ?k # drop (Suc ?k) as" .
moreover
have "length bcs < length cs" unfolding cs by simp
from id_take_nth_drop[OF this]
have "cs = take ?k cs @ cs ! ?k # drop (Suc ?k) cs" .
moreover have "take ?k as = take ?k cs"
using le arg_cong[OF bs, of "take (length bcs)"]
unfolding cs as bs' by auto
ultimately show ?thesis using n n' unfolding lexn_conv by auto
next
let ?k = "length abs"
case ge
hence "bs ! ?k = cs ! ?k" unfolding bs' cs by (simp add: nth_append)
hence "(as ! ?k, cs ! ?k) \<in> r" using abr unfolding as bs by auto
moreover
have "length abs < length as" using ge unfolding as by simp
from id_take_nth_drop[OF this]
have "as = take ?k as @ as ! ?k # drop (Suc ?k) as" .
moreover have "length abs < length cs" using n n' unfolding as by simp
from id_take_nth_drop[OF this]
have "cs = take ?k cs @ cs ! ?k # drop (Suc ?k) cs" .
moreover have "take ?k as = take ?k cs"
using ge arg_cong[OF bs', of "take (length abs)"]
unfolding cs as bs by auto
ultimately show ?thesis using n n' unfolding lexn_conv by auto
next
let ?k = "length abs"
case eq
hence *: "abs = bcs" "b = b'" using bs bs' by auto
hence "(a, c') \<in> r"
using abr b'c'r assms unfolding trans_def by blast
with * show ?thesis using n n' unfolding lexn_conv as bs cs by auto
qed
qed
lemma total_lenlex:
assumes "total r"
shows "total (lenlex r)"
proof -
have "(xs,ys) \<in> lexn r (length xs) \<or> (ys,xs) \<in> lexn r (length xs)"
if "xs \<noteq> ys" and len: "length xs = length ys" for xs ys
proof -
obtain pre x xs' y ys' where "x\<noteq>y" and xs: "xs = pre @ [x] @ xs'" and ys: "ys = pre @ [y] @ys'"
by (meson len \<open>xs \<noteq> ys\<close> same_length_different)
then consider "(x,y) \<in> r" | "(y,x) \<in> r"
by (meson UNIV_I assms total_on_def)
then show ?thesis
by cases (use len in \<open>(force simp add: lexn_conv xs ys)+\<close>)
qed
then show ?thesis
by (fastforce simp: lenlex_def total_on_def lex_def)
qed
lemma lenlex_transI [intro]: "trans r \<Longrightarrow> trans (lenlex r)"
unfolding lenlex_def
by (meson lex_transI trans_inv_image trans_less_than trans_lex_prod)
lemma lex_take_index:
assumes "(xs, ys) \<in> lex r"
obtains i where "i < length xs" and "i < length ys" and "take i xs = take i ys"
and "(xs ! i, ys ! i) \<in> r"
proof -
obtain n us x xs' y ys' where "(xs, ys) \<in> lexn r n" and "length xs = n" and "length ys = n"
and "xs = us @ x # xs'" and "ys = us @ y # ys'" and "(x, y) \<in> r"
using assms by (fastforce simp: lex_def lexn_conv)
then show ?thesis by (intro that [of "length us"]) auto
qed
lemma irrefl_lex: "irrefl r \<Longrightarrow> irrefl (lex r)"
by (meson irrefl_def lex_take_index)
lemma lexl_not_refl [simp]: "irrefl r \<Longrightarrow> (x,x) \<notin> lex r"
by (meson irrefl_def lex_take_index)
text \<open>Classical lexicographic ordering on lists, ie. "a" < "ab" < "b".
This ordering does \emph{not} preserve well-foundedness.
Author: N. Voelker, March 2005.\<close>
definition lexord :: "('a \<times> 'a) set \<Rightarrow> ('a list \<times> 'a list) set" where
"lexord r = {(x,y). \<exists> a v. y = x @ a # v \<or>
(\<exists> u a b v w. (a,b) \<in> r \<and> x = u @ (a # v) \<and> y = u @ (b # w))}"
lemma lexord_Nil_left[simp]: "([],y) \<in> lexord r = (\<exists> a x. y = a # x)"
by (unfold lexord_def, induct_tac y, auto)
lemma lexord_same_pref_iff:
"(xs @ ys, xs @ zs) \<in> lexord r \<longleftrightarrow> (\<exists>x \<in> set xs. (x,x) \<in> r) \<or> (ys, zs) \<in> lexord r"
by(induction xs) auto
lemma lexord_same_pref_if_irrefl[simp]:
"irrefl r \<Longrightarrow> (xs @ ys, xs @ zs) \<in> lexord r \<longleftrightarrow> (ys, zs) \<in> lexord r"
by (simp add: irrefl_def lexord_same_pref_iff)
lemma lexord_append_rightI: "\<exists> b z. y = b # z \<Longrightarrow> (x, x @ y) \<in> lexord r"
by (metis append_Nil2 lexord_Nil_left lexord_same_pref_iff)
lemma lexord_append_left_rightI:
"(a,b) \<in> r \<Longrightarrow> (u @ a # x, u @ b # y) \<in> lexord r"
by (simp add: lexord_same_pref_iff)
lemma lexord_append_leftI: "(u,v) \<in> lexord r \<Longrightarrow> (x @ u, x @ v) \<in> lexord r"
by (simp add: lexord_same_pref_iff)
lemma lexord_append_leftD:
"\<lbrakk>(x @ u, x @ v) \<in> lexord r; (\<forall>a. (a,a) \<notin> r) \<rbrakk> \<Longrightarrow> (u,v) \<in> lexord r"
by (simp add: lexord_same_pref_iff)
lemma lexord_take_index_conv:
"((x,y) \<in> lexord r) =
((length x < length y \<and> take (length x) y = x) \<or>
(\<exists>i. i < min(length x)(length y) \<and> take i x = take i y \<and> (x!i,y!i) \<in> r))"
proof -
have "(\<exists>a v. y = x @ a # v) = (length x < length y \<and> take (length x) y = x)" by (metis Cons_nth_drop_Suc append_eq_conv_conj drop_all list.simps(3) not_le)
moreover
have "(\<exists>u a b. (a, b) \<in> r \<and> (\<exists>v. x = u @ a # v) \<and> (\<exists>w. y = u @ b # w)) =
(\<exists>i<length x. i < length y \<and> take i x = take i y \<and> (x ! i, y ! i) \<in> r)"
(is "?L=?R")
proof
show "?L\<Longrightarrow>?R" by (metis append_eq_conv_conj drop_all leI list.simps(3) nth_append_length)
show "?R\<Longrightarrow>?L" by (metis id_take_nth_drop)
qed
ultimately show ?thesis by (auto simp: lexord_def Let_def)
qed
\<comment> \<open>lexord is extension of partial ordering List.lex\<close> lemma lexord_lex: "(x,y) \<in> lex r = ((x,y) \<in> lexord r \<and> length x = length y)"
proof (induction x arbitrary: y)
case (Cons a x y) then show ?case by (cases y) (force+)
qed auto
lemma lexord_sufI:
assumes "(u,w) \<in> lexord r""length w \<le> length u"
shows "(u@v,w@z) \<in> lexord r"
proof- from leD[OF assms(2)] assms(1)[unfolded lexord_take_index_conv[of u w r] min_absorb2[OF assms(2)]]
obtain i where"take i u = take i w"and"(u!i,w!i) \<in> r"and"i < length w" by blast
hence "((u@v)!i, (w@z)!i) \<in> r"
unfolding nth_append using less_le_trans[OF \<open>i < length w\<close> assms(2)] \<open>(u!i,w!i) \<in> r\<close> by presburger
moreover have "i < min (length (u@v)) (length (w@z))"
using assms(2) \<open>i < length w\<close> by simp
moreover have "take i (u@v) = take i (w@z)"
using assms(2) \<open>i < length w\<close> \<open>take i u = take i w\<close> by simp
ultimately show ?thesis
using lexord_take_index_conv by blast
qed
lemma lexord_sufE:
assumes "(xs@zs,ys@qs) \<in> lexord r""xs \<noteq> ys""length xs = length ys""length zs = length qs"
shows "(xs,ys) \<in> lexord r"
proof-
obtain i where"i < length (xs@zs)"and"i < length (ys@qs)"and"take i (xs@zs) = take i (ys@qs)" and"((xs@zs) ! i, (ys@qs) ! i) \<in> r"
using assms(1) lex_take_index[unfolded lexord_lex,of"xs @ zs""ys @ qs" r]
length_append[of xs zs, unfolded assms(3,4), folded length_append[of ys qs]] by blast
have "length (take i xs) = length (take i ys)" by (simp add: assms(3))
have "i < length xs"
using assms(2,3) le_less_linear take_all[of xs i] take_all[of ys i]
\<open>take i (xs @ zs) = take i (ys @ qs)\<close> append_eq_append_conv take_append by metis
hence "(xs ! i, ys ! i) \<in> r"
using \<open>((xs @ zs) ! i, (ys @ qs) ! i) \<in> r\<close> assms(3) by (simp add: nth_append)
moreover have "take i xs = take i ys"
using assms(3) \<open>take i (xs @ zs) = take i (ys @ qs)\<close> byauto
ultimately show ?thesis
unfolding lexord_take_index_conv using \<open>i < length xs\<close> assms(3) by fastforce
qed
lemma lexord_irreflexive: "\<forall>x. (x,x) \<notin> r \<Longrightarrow> (xs,xs) \<notin> lexord r" by (induct xs) auto
text\<open>By Ren\'e Thiemann:\<close> lemma lexord_partial_trans: "(\<And>x y z. x \<in> set xs \<Longrightarrow> (x,y) \<in> r \<Longrightarrow> (y,z) \<in> r \<Longrightarrow> (x,z) \<in> r)
\<Longrightarrow> (xs,ys) \<in> lexord r \<Longrightarrow> (ys,zs) \<in> lexord r \<Longrightarrow> (xs,zs) \<in> lexord r"
proof (induct xs arbitrary: ys zs)
case Nil from Nil(3) show ?case unfolding lexord_def by (cases zs, auto)
next
case (Cons x xs yys zzs) from Cons(3) obtain y ys where yys: "yys = y # ys" unfolding lexord_def by (cases yys, auto)
note Cons = Cons[unfolded yys] from Cons(3) have one: "(x,y) \<in> r \<or> x = y \<and> (xs,ys) \<in> lexord r"byauto from Cons(4) obtain z zs where zzs: "zzs = z # zs" unfolding lexord_def by (cases zzs, auto)
note Cons = Cons[unfolded zzs] from Cons(4) have two: "(y,z) \<in> r \<or> y = z \<and> (ys,zs) \<in> lexord r"byauto
{
assume "(xs,ys) \<in> lexord r"and"(ys,zs) \<in> lexord r" from Cons(1)[OF _ this] Cons(2)
have "(xs,zs) \<in> lexord r"byauto
} note ind1 = this
{
assume "(x,y) \<in> r"and"(y,z) \<in> r" from Cons(2)[OF _ this] have "(x,z) \<in> r"byauto
} note ind2 = this from one two ind1 ind2
have "(x,z) \<in> r \<or> x = z \<and> (xs,zs) \<in> lexord r"by blast
thus ?case unfolding zzs byauto
qed
lemma lexord_transI: "trans r \<Longrightarrow> trans (lexord r)" by (meson lexord_trans transI)
lemma total_lexord: "total r \<Longrightarrow> total (lexord r)"
unfolding total_on_def
proof clarsimp
fix x y
assume "\<forall>x y. x \<noteq> y \<longrightarrow> (x, y) \<in> r \<or> (y, x) \<in> r" and"(x::'a list) \<noteq> y" and"(y, x) \<notin> lexord r" then
show "(x, y) \<in> lexord r"
proof (induction x arbitrary: y)
case Nil then show ?case by (metis lexord_Nil_left list.exhaust)
next
case (Cons a x y) then show ?case by (cases y) (force+)
qed
qed
corollary lexord_linear: "(\<forall>a b. (a,b) \<in> r \<or> a = b \<or> (b,a) \<in> r) \<Longrightarrow> (x,y) \<in> lexord r \<or> x = y \<or> (y,x) \<in> lexord r"
using total_lexord by (metis UNIV_I total_on_def)
lemma lexord_irrefl: "irrefl R \<Longrightarrow> irrefl (lexord R)" by (simp add: irrefl_def lexord_irreflexive)
lemma lexord_asym:
assumes "asym R"
shows "asym (lexord R)"
proof
fix xs ys
assume "(xs, ys) \<in> lexord R" then show "(ys, xs) \<notin> lexord R"
proof (induct xs arbitrary: ys)
case Nil then show ?case by simp
next
case (Cons x xs) then obtain z zs where ys: "ys = z # zs"by (cases ys) auto with assms Cons show ?case by (auto dest: asymD)
qed
qed
lemma lexord_asymmetric:
assumes "asym R"
assumes hyp: "(a, b) \<in> lexord R"
shows "(b, a) \<notin> lexord R"
proof - from \<open>asym R\<close> have "asym (lexord R)"by (rule lexord_asym) then show ?thesis by (auto simp: hyp dest: asymD)
qed
lemma asym_lex: "asym R \<Longrightarrow> asym (lex R)" by (meson asymI asymD irrefl_lex lexord_asym lexord_lex)
lemma asym_lenlex: "asym R \<Longrightarrow> asym (lenlex R)" by (simp add: lenlex_def asym_inv_image asym_less_than asym_lex)
lemma lenlex_append1:
assumes len: "(us,xs) \<in> lenlex R"and eq: "length vs = length ys"
shows "(us @ vs, xs @ ys) \<in> lenlex R"
using len
proof (induction us)
case Nil then show ?case by (simp add: lenlex_def eq)
next
case (Cons u us) with lex_append_rightI show ?case by (fastforce simp add: lenlex_def eq)
qed
lemma lenlex_append2 [simp]:
assumes "irrefl R"
shows "(us @ xs, us @ ys) \<in> lenlex R \<longleftrightarrow> (xs, ys) \<in> lenlex R"
proof (induction us)
case Nil then show ?case by (simp add: lenlex_def)
next
case (Cons u us) with assms show ?case by (auto simp: lenlex_def irrefl_def)
qed
text \<open>
Predicate version of lexicographic order integrated with Isabelle's order type classes.
Author: Andreas Lochbihler
\<close>
context ord begin
context
notes [[inductive_internals]] begin
inductive lexordp :: "'a list \<Rightarrow> 'a list \<Rightarrow> bool" where
Nil: "lexordp [] (y # ys)"
| Cons: "x < y \<Longrightarrow> lexordp (x # xs) (y # ys)"
| Cons_eq: "\<lbrakk> \<not> x < y; \<not> y < x; lexordp xs ys \<rbrakk> \<Longrightarrow> lexordp (x # xs) (y # ys)"
lemma lexordp_cases [consumes 1, case_names Nil Cons Cons_eq, cases pred: lexordp]:
assumes "lexordp xs ys"
obtains (Nil) y ys' where "xs = []" "ys = y # ys'"
| (Cons) x xs' y ys'where"xs = x # xs'""ys = y # ys'""x < y"
| (Cons_eq) x xs' ys'where"xs = x # xs'""ys = x # ys'""lexordp xs' ys'"
using assms bycases (fastforce simp add: not_less_iff_gr_or_eq)+
lemma lexordp_induct [consumes 1, case_names Nil Cons Cons_eq, induct pred: lexordp]:
assumes major: "lexordp xs ys" and Nil: "\<And>y ys. P [] (y # ys)" and Cons: "\<And>x xs y ys. x < y \<Longrightarrow> P (x # xs) (y # ys)" and Cons_eq: "\<And>x xs ys. \<lbrakk> lexordp xs ys; P xs ys \<rbrakk> \<Longrightarrow> P (x # xs) (x # ys)"
shows "P xs ys"
using major by induct (simp_all add: Nil Cons not_less_iff_gr_or_eq Cons_eq)
lemma lexordp_iff: "lexordp xs ys \<longleftrightarrow> (\<exists>x vs. ys = xs @ x # vs) \<or> (\<exists>us a b vs ws. a < b \<and> xs = us @ a # vs \<and> ys = us @ b # ws)"
(is "?lhs = ?rhs")
proof
assume ?lhs thus ?rhs
proof induct
case Cons_eq thus ?case by simp (metis append.simps(2))
qed(fastforce intro: disjI2 del: disjCI intro: exI[where x="[]"])+
next
assume ?rhs thus ?lhs by(auto intro: lexordp_append_leftI[where us="[]", simplified] lexordp_append_leftI)
qed
text \<open>These are useful for termination proofs\<close>
definition "measures fs = inv_image (lex less_than) (%a. map (%f. f a) fs)"
lemma wf_measures[simp]: "wf (measures fs)"
unfolding measures_def by blast
lemma in_measures[simp]: "(x, y) \<in> measures [] = False" "(x, y) \<in> measures (f # fs)
= (f x < f y \<or> (f x = f y \<and> (x, y) \<in> measures fs))"
unfolding measures_def byauto
lemma measures_less: "f x < f y \<Longrightarrow> (x, y) \<in> measures (f#fs)" by simp
lemma measures_lesseq: "f x \<le> f y \<Longrightarrow> (x, y) \<in> measures fs \<Longrightarrow> (x, y) \<in> measures (f#fs)" byauto
subsubsection \<open>Lifting Relations to Lists: one element\<close>
definition listrel1 :: "('a \<times> 'a) set \<Rightarrow> ('a list \<times> 'a list) set"where "listrel1 r = {(xs,ys).
\<exists>us z z' vs. xs = us @ z # vs \<and> (z,z') \<in> r \<and> ys = us @ z' # vs}"
lemma listrel1I: "\<lbrakk> (x, y) \<in> r; xs = us @ x # vs; ys = us @ y # vs \<rbrakk> \<Longrightarrow>
(xs, ys) \<in> listrel1 r"
unfolding listrel1_def byauto
lemma listrel1E: "\<lbrakk> (xs, ys) \<in> listrel1 r;
!!x y us vs. \<lbrakk> (x, y) \<in> r; xs = us @ x # vs; ys = us @ y # vs \<rbrakk> \<Longrightarrow> P
\<rbrakk> \<Longrightarrow> P"
unfolding listrel1_def byauto
lemma Cons_listrel1_Cons [iff]: "(x # xs, y # ys) \<in> listrel1 r \<longleftrightarrow>
(x,y) \<in> r \<and> xs = ys \<or> x = y \<and> (xs, ys) \<in> listrel1 r" by (simp add: listrel1_def Cons_eq_append_conv) (blast)
lemma listrel1I1: "(x,y) \<in> r \<Longrightarrow> (x # xs, y # xs) \<in> listrel1 r" by fast
lemma listrel1I2: "(xs, ys) \<in> listrel1 r \<Longrightarrow> (x # xs, x # ys) \<in> listrel1 r" by fast
lemma append_listrel1I: "(xs, ys) \<in> listrel1 r \<and> us = vs \<or> xs = ys \<and> (us, vs) \<in> listrel1 r
\<Longrightarrow> (xs @ us, ys @ vs) \<in> listrel1 r"
unfolding listrel1_def byauto (blast intro: append_eq_appendI)+
lemma Cons_listrel1E1[elim!]:
assumes "(x # xs, ys) \<in> listrel1 r" and"\<And>y. ys = y # xs \<Longrightarrow> (x, y) \<in> r \<Longrightarrow> R" and"\<And>zs. ys = x # zs \<Longrightarrow> (xs, zs) \<in> listrel1 r \<Longrightarrow> R"
shows R
using assms by (cases ys) blast+
lemma Cons_listrel1E2[elim!]:
assumes "(xs, y # ys) \<in> listrel1 r" and"\<And>x. xs = x # ys \<Longrightarrow> (x, y) \<in> r \<Longrightarrow> R" and"\<And>zs. xs = y # zs \<Longrightarrow> (zs, ys) \<in> listrel1 r \<Longrightarrow> R"
shows R
using assms by (cases xs) blast+
lemma snoc_listrel1_snoc_iff: "(xs @ [x], ys @ [y]) \<in> listrel1 r
\<longleftrightarrow> (xs, ys) \<in> listrel1 r \<and> x = y \<or> xs = ys \<and> (x,y) \<in> r" (is "?L \<longleftrightarrow> ?R")
proof
assume ?L thus ?R by (fastforce simp: listrel1_def snoc_eq_iff_butlast butlast_append)
next
assume ?R then show ?L unfolding listrel1_def by force
qed
lemma listrel1_iff_update: "(xs,ys) ∈ (listrel1 r) ⟷ (∃y n. (xs ! n, y) ∈ r ∧ n < length xs ∧ ys = xs[n:=y])" (is"?L ⟷ ?R") proof assume"?L" thenobtain x y u v where"xs = u @ x # v""ys = u @ y # v""(x,y) ∈ r" unfolding listrel1_def by auto thenhave"ys = xs[length u := y]"and"length u < length xs" and"(xs ! length u, y) ∈ r"by auto thenshow"?R"by auto next assume"?R" thenobtain x y n where"(xs!n, y) ∈ r""n < size xs""ys = xs[n:=y]""x = xs!n" by auto thenobtain u v where"xs = u @ x # v"and"ys = u @ y # v"and"(x, y) ∈ r" by (auto intro: upd_conv_take_nth_drop id_take_nth_drop) thenshow"?L"by (auto simp: listrel1_def) qed
lemmalists_accD:"xs\<in>lists(Wellfounded.accr)\<Longrightarrow>xs\<in>Wellfounded.acc(listrel1r)" proof(inductset:lists) caseNil thenshow?case by(mesonacc.introsnot_listrel1_Nil) next case(Consal) thenshow?case byblast qed
lemmalists_accI:"xs\<in>Wellfounded.acc(listrel1r)\<Longrightarrow>xs\<in>lists(Wellfounded.accr)" proof(inductionset:Wellfounded.acc) case(1x) thenhave"\<And>uv.\<lbrakk>u\<in>setx;(v,u)\<in>r\<rbrakk>\<Longrightarrow>v\<in>Wellfounded.accr" by(metisin_lists_conv_setin_set_conv_decomplistrel1I) thenshow?case by(mesonacc.introsin_listsI) qed
lemmalistrel_iff_zip[code_unfold]:"(xs,ys)\<in>listrelr\<longleftrightarrow> lengthxs=lengthys\<and>(\<forall>(x,y)\<in>set(zipxsys).(x,y)\<in>r)"(is"?L\<longleftrightarrow>?R") proof assume?Lthus?Rbyinduct(autointro:listrel_eq_len) next assume?Rthus?L apply(clarify) by(inductrule:list_induct2)(autointro:listrel.intros) qed
lemmalistrel_rtrancl_eq_rtrancl_listrel1:"listrel(r\<^sup>*)=(listrel1r)\<^sup>*" proof {fixxyassume"(x,y)\<in>listrel(r\<^sup>*)" thenhave"(x,y)\<in>(listrel1r)\<^sup>*" byinduct(autointro:rtrancl_listrel1_ConsI2)} thenshow"listrel(r\<^sup>*)\<subseteq>(listrel1r)\<^sup>*" by(rulesubrelI) next show"listrel(r\<^sup>*)\<supseteq>(listrel1r)\<^sup>*" proof(rulesubrelI) fixxsysassume"(xs,ys)\<in>(listrel1r)\<^sup>*" thenshow"(xs,ys)\<in>listrel(r\<^sup>*)" proofinduct casebaseshow?caseby(autosimpadd:listrel_iff_zipset_zip) next case(stepyszs) thus?caseby(metislistrel_reflcl_if_listrel1listrel_rtrancl_trans) qed qed qed
funadd_literal_listtarget= let funprettyliteralspr_varsfxy[(t1,_),(t2,_)]= caseOption.map(const1)(implode_listt2) ofSOMEts=> Code_Printer.literal_listliterals(map(prvarsCode_Printer.NOBR)ts) |NONE=> print_list(Code_Printer.infix_consliterals)(prvars)fxyt1t2; in Code_Target.set_printings(Code_Symbol.Constant(\<^const_name>\<open>Cons\<close>, [(target,SOME(Code_Printer.complex_const_syntax(2,pretty)))])) end
lemmalists_transfer[transfer_rule]: "(rel_setA===>rel_set(list_all2A))listslists" proof(rulerel_funI,rulerel_setI) show"\<lbrakk>l\<in>listsX; rel_set A X Y\<rbrakk> \<Longrightarrow> \<exists>y\<in>lists Y. list_all2 A l y" for X Y l proof(inductionlrule:lists.induct) case(Consal) thenshow?case by(simponly:rel_set_deflist_all2_Cons1,metislists.Cons) qedauto show"\<lbrakk>l\<in>listsY; rel_set A X Y\<rbrakk> \<Longrightarrow> \<exists>x\<in>lists X. list_all2 A x l" for X Y l proof(inductionlrule:lists.induct) case(Consal) thenshow?case by(simponly:rel_set_deflist_all2_Cons2,metislists.Cons) qedauto qed
¤ Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.1.354Bemerkung:
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.