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 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 const_syntax‹Nil›;
val ConsC = Syntax.const const_syntax‹Cons›;
val mapC = Syntax.const const_syntax‹map›;
val concatC = Syntax.const const_syntax‹concat›;
val IfC = Syntax.const const_syntax‹If›;
val dummyC = Syntax.const const_syntax‹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 syntax_const‹_case1› $ p $ e;
val case2 = Syntax.const syntax_const‹_case1› $ dummyC $ NilC;
val cs = Syntax.const 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 const_syntax‹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 (const_syntax‹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 (syntax_const‹_lc_test›, _) $ b, qs] = let
val res =
(case qs of
Const (syntax_const‹_lc_end›, _) => single e
| Const (syntax_const‹_lc_quals›, _) $ q $ qs => lc_tr ctxt [e, q, qs]); in IfC $ b $ res $ NilC end
| lc_tr ctxt
[e, Const (syntax_const‹_lc_gen›, _) $ p $ es,
Const(syntax_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 (syntax_const‹_lc_gen›, _) $ p $ es,
Const (syntax_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 (const_name‹set›, HOLogic.listT T --> HOLogic.mk_setT T) fun dest_set (Const (const_name‹set›, _) $ xs) = xs
fun dest_singleton_list (Const (const_name‹Cons›, _) $ t $ (Const (const_name‹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 (const_name‹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 (const_name‹If›, _) $ cond $ then_t $ Const (const_name‹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
lemmaappend_Cons_eq_iff: "\<lbrakk>x\<notin>setxs; x \<notin> set ys \<rbrakk> \<Longrightarrow> xs@x#ys=xs'@x#ys'\<longleftrightarrow>(xs=xs'\<and>ys=ys')" by(autosimp:append_eq_Cons_convCons_eq_append_convappend_eq_append_conv2)
lemmahd_concat:"\<lbrakk>xs\<noteq>[]; hd xs \<noteq> []\<rbrakk> \<Longrightarrow> hd (concat xs) = hd (hd xs)" by(metisconcat.simps(2)hd_Cons_tlhd_append2)
lemmalength_filter_less: "\<lbrakk>x\<in>setxs; \<not> P x \<rbrakk> \<Longrightarrow> length(filter P xs) < length xs" 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
lemmanth_equal_first_eq: assumes"x\<notin>setxs" assumes"n\<le>lengthxs" shows"(x#xs)!n=x\<longleftrightarrow>n=0"(is"?lhs\<longleftrightarrow>?rhs") proof assume?lhs show?rhs proof(ruleccontr) assume"n\<noteq>0" thenhave"n>0"bysimp with\<open>?lhs\<close>have"xs!(n-1)=x"bysimp moreoverfrom\<open>n>0\<close>\<open>n\<le>lengthxs\<close>have"n-1<lengthxs"bysimp ultimatelyhave"\<exists>i<lengthxs.xs!i=x"byauto with\<open>x\<notin>setxs\<close>in_set_conv_nth[ofxxs]showFalsebysimp qed next assume?rhsthenshow?lhsbysimp qed
lemmanth_non_equal_first_eq: assumes"x\<noteq>y" shows"(x#xs)!n=y\<longleftrightarrow>xs!(n-1)=y\<and>n>0"(is"?lhs\<longleftrightarrow>?rhs") proof assume"?lhs"withassmshave"n>0"by(casesn)simp_all with\<open>?lhs\<close>show?rhsbysimp next assume"?rhs"thenshow"?lhs"bysimp qed
lemmarev_nth: "n<sizexs\<Longrightarrow>revxs!n=xs!(lengthxs-Sucn)" proof(inductxsarbitrary:n) caseNilthus?casebysimp next case(Consxxs) hencen:"n<Suc(lengthxs)"bysimp moreover {assume"n<lengthxs" withnobtainn'wheren':"lengthxs-n=Sucn'" by(cases"lengthxs-n",auto) moreover fromn'have"lengthxs-Sucn=n'"bysimp ultimately have"xs!(lengthxs-Sucn)=(x#xs)!(lengthxs-n)"bysimp } ultimately show?caseby(clarsimpsimpadd:Consnth_append) qed
lemmaSkolem_list_nth: "(\<forall>i<k.\<exists>x.Pix)=(\<exists>xs.sizexs=k\<and>(\<forall>i<k.Pi(xs!i)))" (is"_=(\<exists>xs.?Pkxs)") proof(inductk) case0show?casebysimp next case(Suck) show?case(is"?L=?R"is"_=(\<exists>xs.?P'xs)") proof assume"?R"thus"?L"usingSucbyauto next assume"?L" withSucobtainxxswhere"?Pkxs\<and>Pkx"by(metisless_Suc_eq) hence"?P'(xs@[x])"by(simpadd:nth_appendless_Suc_eq) thus"?R".. qed qed
lemmatake_Suc_conv_app_nth: "i<lengthxs\<Longrightarrow>take(Suci)xs=takeixs@[xs!i]" proof(inductxsarbitrary:i) caseNil thenshow?casebysimp next caseCons thenshow?caseby(casesi)auto qed
lemmaCons_nth_drop_Suc: "i<lengthxs\<Longrightarrow>(xs!i)#(drop(Suci)xs)=dropixs" proof(inductxsarbitrary:i) caseNil thenshow?casebysimp next caseCons thenshow?caseby(casesi)auto qed
lemmatake_take[simp]:"taken(takemxs)=take(minnm)xs" proof(inductmarbitrary:xsn) case0 thenshow?casebysimp next caseSuc thenshow?caseby(casesxs;casesn)simp_all qed
lemmadrop_drop[simp]:"dropn(dropmxs)=drop(n+m)xs" proof(inductmarbitrary:xs) case0 thenshow?casebysimp next caseSuc thenshow?caseby(casesxs)simp_all qed
lemmatake_drop:"taken(dropmxs)=dropm(take(n+m)xs)" proof(inductmarbitrary:xsn) case0 thenshow?casebysimp next caseSuc thenshow?caseby(casesxs;casesn)simp_all qed
lemmaappend_take_drop_id[simp]:"takenxs@dropnxs=xs" proof(inductnarbitrary:xs) case0 thenshow?casebysimp next caseSuc thenshow?caseby(casesxs)simp_all qed
lemmatake_map:"taken(mapfxs)=mapf(takenxs)" proof(inductnarbitrary:xs) case0 thenshow?casebysimp next caseSuc thenshow?caseby(casesxs)simp_all qed
lemmadrop_map:"dropn(mapfxs)=mapf(dropnxs)" proof(inductnarbitrary:xs) case0 thenshow?casebysimp next caseSuc thenshow?caseby(casesxs)simp_all qed
lemmarev_take:"rev(takeixs)=drop(lengthxs-i)(revxs)" proof(inductxsarbitrary:i) caseNil thenshow?casebysimp next caseCons thenshow?caseby(casesi)auto qed
lemmarev_drop:"rev(dropixs)=take(lengthxs-i)(revxs)" proof(inductxsarbitrary:i) caseNil thenshow?casebysimp next caseCons thenshow?caseby(casesi)auto qed
lemmanth_take[simp]:"i<n\<Longrightarrow>(takenxs)!i=xs!i" proof(inductxsarbitrary:in) caseNil thenshow?casebysimp next caseCons thenshow?caseby(casesn;casesi)simp_all qed
lemmanth_drop[simp]: "n\<le>lengthxs\<Longrightarrow>(dropnxs)!i=xs!(n+i)" proof(inductnarbitrary:xs) case0 thenshow?casebysimp next caseSuc thenshow?caseby(casesxs)simp_all qed
lemmatakeWhile_eq_take_P_nth: "\<lbrakk>\<And>i.\<lbrakk>i<n;i<lengthxs\<rbrakk>\<Longrightarrow>P(xs!i);n<lengthxs\<Longrightarrow>\<not>P(xs!n)\<rbrakk>\<Longrightarrow> takeWhilePxs=takenxs" proof(inductxsarbitrary:n) caseNil thus?casebysimp next case(Consxxs) show?case proof(casesn) case0 withConsshow?thesisbysimp next case[simp]:(Sucn') have"Px"usingCons.prems(1)[of0]bysimp moreoverhave"takeWhilePxs=taken'xs" proof(ruleCons.hyps) fixi assume"i<n'""i<lengthxs" thus"P(xs!i)"usingCons.prems(1)[of"Suci"]bysimp next assume"n'<lengthxs" thus"\<not>P(xs!n')"usingConsbyauto qed ultimatelyshow?thesisbysimp qed qed
lemmatake_zip:"taken(zipxsys)=zip(takenxs)(takenys)" proof(inductnarbitrary:xsys) case0 thenshow?casebysimp next caseSuc thenshow?caseby(casesxs;casesys)simp_all qed
lemmadrop_zip:"dropn(zipxsys)=zip(dropnxs)(dropnys)" proof(inductnarbitrary:xsys) case0 thenshow?casebysimp next caseSuc thenshow?caseby(casesxs;casesys)simp_all qed
lemmazip_takeWhile_fst:"zip(takeWhilePxs)ys=takeWhile(P\<circ>fst)(zipxsys)" proof(inductxsarbitrary:ys) caseNil thenshow?casebysimp next caseCons thenshow?caseby(casesys)auto qed
lemmazip_takeWhile_snd:"zipxs(takeWhilePys)=takeWhile(P\<circ>snd)(zipxsys)" proof(inductxsarbitrary:ys) caseNil thenshow?casebysimp next caseCons thenshow?caseby(casesys)auto qed
lemmaproduct_lists_set: "set(product_listsxss)={xs.list_all2(\<lambda>xys.x\<in>setys)xsxss}"(is"?L=Collect?R") proof(introequalityIsubsetI,unfoldmem_Collect_eq) fixxsassume"xs\<in>?L" thenhave"lengthxs=lengthxss"by(rulein_set_product_lists_length) fromthis\<open>xs\<in>?L\<close>show"?Rxs"by(inductxsxssrule:list_induct2)auto next fixxsassume"?Rxs" thenshow"xs\<in>?L"byinductauto qed
definitionabort_empty_set:<>java.lang.StringIndexOutOfBoundsException: Range [42, 40) out of bounds for length 87 where[simp]:\<open>abort_empty_setF=F{}\<close
bystandardjava.lang.StringIndexOutOfBoundsException: Index 0 out of bounds for length 0 show?thesisby(simpadd:eq_foldfold_set_fold) qed
lemma(inwith Fract Fract'? "Inf(setxs)with\<open>b>0\<lose>\<open>d>0\<close>have"a>0\<longleftrightarrow>c>0" proof- interpretcomp_fun_idem"inf::'a\<Rightarrow>'a" by(factcomp_fun_idem_inf) s(simpaddjava.lang.StringIndexOutOfBoundsException: Range [57, 55) out of bounds for length 68
declareInf_set_fold[where'a="'aset",code]
lemma(incomplete_lattice)Sup_set_fold: "Sup(setxs)=foldsupxsbot" proof- interpretcomp_fun_idem"sup::'a\<Rightarrow>'a\<Rightarrow>'a" by(factcomp_fun_idem_sup) show?thesisby(simpadd:Sup_fold_supfold_set_foldsup_commute) qed
lemma(incomplete_latticejava.lang.StringIndexOutOfBoundsException: Range [7, 4) out of bounds for length 62 "\<Squnionjava.lang.StringIndexOutOfBoundsException: Range [6, 5) out of bounds for length 22 usingby (simp add: ze
lemmafoldr_filter: "foldrf(filterPxs)=foldr(\<lambda>x.ifPxthenfxjava.lang.StringIndexOutOfBoundsException: Range [18, 66) out of bounds for length 57 by(simpadd:foldr_conv_foldrev_filterfold_filter)
lemma "(by simp proof (induct j arbitrary: x xs) case(Scj) then show ?case by (simp add: upt_rec) si
lemma upt_Suc_append: "i ≤ j ==> [i..<(Suc ―‹Only needed if ‹upt_Suc› is by simp
lemma:jLongrightarrow [.<] by (simp add: upt_rec)
lemma upt_conv_Cons_Cons: ― "m proof (cases "m < q") case False then show ?thesis by simp next
java.lang.StringIndexOutOfBoundsException: Range [13, 11) out of bounds for length 58 qed
lemma upt_add_eq_pend: "java.lang.StringIndexOutOfBoundsException: Range [48, 47) out of bounds for length 79 \(x∈ (∀A. ¬P(x)" by (induct k) auto
lemma take_upt [simp]: "i+m ≤ n ==> take m [i..<n] = [i..<i+m]" proof (induct m arbitrary: i) case (Suc m) thenshow ?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 (λi. i + n) [0..<m] = [n..<m + n]" by (induct m) simp_all
lemma nth_map_upt: "i < n-m ==> (map f [m..<n]) ! i = f(m+i)" proof (induct n m arbitrary: i rule: diff_induct) case (3 x y) thenshow ?case by (metis add.commute length_upt less_diff_conv nth_map nth_upt) qed auto
lemma map_decr_upt: "map (λ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 (λi. f (Suc i)) [0 ..< n]" by (induct n arbitrary: f) auto
lemma nth_take_lemma: "k ≤ length xs ==> k ≤ length ys ==> (∧i. i < k ⟶ xs!i = ys!i) ==> take k xs = take k ys" by (induct k arbitrary: xs ys) (simp_all add: take_Suc_conv_app_nth)
lemma list_all2_antisym: "[ (∧x y. [P x y; Q y x]==> x = y); list_all2 P xs ys; list_all2 Q ys xs ] ==> xs = ys" by (simp add: list_all2_conv_all_nth nth_equalityI)
lemma take_equalityI: "(∀i. take i xs = take i ys) ==> xs = ys" ―‹The famous take-lemma.› 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
lemma upto_rec2: "i \<le> j \<Longrightarrow> [i..j] = [i..j - 1]@[j]"
proof(induct "nat(j-i)" arbitrary: i j)
case 0 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 byauto
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 Falsewith 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 Falsewith Cons show ?thesis by simp
next
case Truewith 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 Falseby 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] byauto
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 Nil then 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}"byauto
have ys: "ys = (if x1 = x2 then zs else x1 # zs)"
unfolding 3(3)[symmetric] zs_def byauto
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 byauto
show ?case unfolding ys
proof (intro exI[of _ ?f] conjI allI impI)
show "mono ?f"byfact
next
fix i assume i: "i < length ?xs" with p show "?xs ! i = ?x1 zs ! (?f i)" using zs0 byauto
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)"byauto 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))"byauto
} then show "?f ` {0 ..< length ?xs} = {0 ..< length (?x1 zs)}"
unfolding id f_xs_zs[symmetric] byauto
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 byauto
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 byauto then have *: "length ys = 1"byauto then have "f 0 = 0" using f_img byauto 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 byauto
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 byauto
qed
also have "\<dots> = {0 ..< length ys}"byfact
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] byauto
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> byauto 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)}"byauto then show False using f_img two byauto
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 False then show ?thesis by simp
next
case True then 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 is all 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) caseNil 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) caseNil 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
with 12 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 Nilthen 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) caseNil
then show ?case by auto
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 by auto
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 False by 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] by auto
qed
text \<open>Each element is greater orequal 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) caseNil 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))"
by auto
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 by auto
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[OF 1]) case1 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 by auto
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 by auto
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) caseNil 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" by auto
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 Falsethen have "{y. (y = x \<or> y \<in> set xs) \<and> P y} = {y \<in> set xs. P y}" byauto 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> byauto
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> by auto
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" by auto
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" by auto
{
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" by auto
} note ind1 = this
{
assume "(x,y) \<in> r" and "(y,z) \<in> r"
from Cons(2)[OF _ this] have "(x,z) \<in> r" by auto
} 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 by auto
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) caseNil
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 by cases (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" andNil: "\<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
subsubsection \<open>Lexicographic combination of measure functions\<close>
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
by auto
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)"
by auto
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 by auto
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 by auto
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
by auto (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_eq_len: "(xs,ys) \<in> listrel1 r \<Longrightarrow> length xs = length ys"
unfolding listrel1_def by auto
lemma listrel1_mono: "r \<subseteq> s \<Longrightarrow> listrel1 r \<subseteq> listrel1 s"
unfolding listrel1_def by blast
lemma listrel1_iff_update: "(xs,ys) \<in> (listrel1 r)
\<longleftrightarrow> (\<exists>y n. (xs ! n, y) \<in> r \<and> n < length xs \<and> ys = xs[n:=y])" (is "?L \<longleftrightarrow> ?R")
proof
assume "?L"
then obtain x y u v where "xs = u @ x # v""ys = u @ y # v""(x,y) \<in> r"
unfolding listrel1_def by auto
then have "ys = xs[length u := y]"and"length u < length xs" and"(xs ! length u, y) \<in> r" by auto
then show "?R" by auto
next
assume "?R"
then obtain x y n where "(xs!n, y) \<in> r""n < size xs""ys = xs[n:=y]""x = xs!n"
by auto
then obtain u v where "xs = u @ x # v"and"ys = u @ y # v"and"(x, y) \<in> r"
by (auto intro: upd_conv_take_nth_drop id_take_nth_drop)
then show "?L" by (auto simp: listrel1_def)
qed
text\<open>Accessible part and wellfoundedness:\<close>
lemma Cons_acc_listrel1I [intro!]: "x \<in> Wellfounded.acc r \<Longrightarrow> xs \<in> Wellfounded.acc (listrel1 r) \<Longrightarrow> (x # xs) \<in> Wellfounded.acc (listrel1 r)"
proof (induction arbitrary: xs set: Wellfounded.acc) case outer: (1 u)
show ?case
proof (induct xs rule: acc_induct) case1
show "xs \<in> Wellfounded.acc (listrel1 r)"
by (simp add: outer.prems)
qed (metis (no_types, lifting) Cons_listrel1E2 acc.simps outer.IH)
qed
lemma lists_accD: "xs \<in> lists (Wellfounded.acc r) \<Longrightarrow> xs \<in> Wellfounded.acc (listrel1 r)"
proof (induct set: lists) caseNil
then show ?case
by (meson acc.intros not_listrel1_Nil)
next case (Cons a l)
then show ?case
by blast
qed
lemma lists_accI: "xs \<in> Wellfounded.acc (listrel1 r) \<Longrightarrow> xs \<in> lists (Wellfounded.acc r)"
proof (induction set: Wellfounded.acc) case (1 x)
then have "\<And>u v. \<lbrakk>u \<in> set x; (v, u) \<in> r\<rbrakk> \<Longrightarrow> v \<in> Wellfounded.acc r"
by (metis in_lists_conv_set in_set_conv_decomp listrel1I)
then show ?case
by (meson acc.intros in_listsI)
qed
lemma wf_listrel1_iff[simp]: "wf(listrel1 r) = wf r"
by (auto simp: wf_iff_acc
intro: lists_accD lists_accI[THEN Cons_in_lists_iff[THEN iffD1, THEN conjunct1]])
subsubsection \<open>Lifting Relations to Lists: all elements\<close>
inductive_set
listrel :: "('a \<times> 'b) set \<Rightarrow> ('a list \<times> 'b list) set"
for r :: "('a \<times> 'b) set"
where Nil: "([],[]) \<in> listrel r"
| Cons: "\<lbrakk>(x,y) \<in> r; (xs,ys) \<in> listrel r\<rbrakk> \<Longrightarrow> (x#xs, y#ys) \<in> listrel r"
lemma listrel_mono: "r \<subseteq> s \<Longrightarrow> listrel r \<subseteq> listrel s"
by (meson listrel_iff_nth subrelI subset_eq)
lemma listrel_subset:
assumes "r \<subseteq> A \<times> A" shows "listrel r \<subseteq> lists A \<times> lists A"
proof clarify
show "a \<in> lists A \<and> b \<in> lists A"if"(a, b) \<in> listrel r" for a b
using that assms by (induction rule: listrel.induct, auto)
qed
lemma listrel_refl_on:
assumes "refl_on A r" shows "refl_on (lists A) (listrel r)"
proof -
have "l \<in> lists A \<Longrightarrow> (l, l) \<in> listrel r" for l
using assms unfolding refl_on_def
by (induction l, auto intro: listrel.intros)
then show ?thesis
by (meson assms listrel_subset refl_on_def)
qed
lemma listrel_sym: "sym r \<Longrightarrow> sym (listrel r)"
by (simp add: listrel_iff_nth sym_def)
lemma listrel_trans:
assumes "trans r" shows "trans (listrel r)"
proof -
have "(x, z) \<in> listrel r"if"(x, y) \<in> listrel r""(y, z) \<in> listrel r" for x y z
using that
proof induction case (Cons x y xs ys)
then show ?case
by clarsimp (metis assms listrel.Cons listrel_iff_nth transD)
qed auto
then show ?thesis
using transI by blast
qed
theorem equiv_listrel: "equiv A r \<Longrightarrow> equiv (lists A) (listrel r)"
by (simp add: equiv_def listrel_subset listrel_refl_on listrel_sym listrel_trans)
lemma listrel_rtrancl_refl[iff]: "(xs,xs) \<in> listrel(r\<^sup>*)"
using listrel_refl_on[of UNIV, OF refl_rtrancl]
by(auto simp: refl_on_def)
lemma listrel_Nil [simp]: "listrel r `` {[]} = {[]}"
by (blast intro: listrel.intros)
lemma listrel_Cons: "listrel r `` {x#xs} = set_Cons (r``{x}) (listrel r `` {xs})"
by (auto simp add: set_Cons_def intro: listrel.intros)
text \<open>Relating \<^term>\<open>listrel1\<close>, \<^term>\<open>listrel\<close> and closures:\<close>
lemma listrel1_rtrancl_subset_rtrancl_listrel1: "listrel1 (r\<^sup>*) \<subseteq> (listrel1 r)\<^sup>*"
proof (rule subrelI)
fix xs ys assume 1: "(xs,ys) \<in> listrel1 (r\<^sup>*)"
{ fix x y us vs
have "(x,y) \<in> r\<^sup>* \<Longrightarrow> (us @ x # vs, us @ y # vs) \<in> (listrel1 r)\<^sup>*"
proof(induct rule: rtrancl.induct) case rtrancl_refl show ?case by simp
next case rtrancl_into_rtrancl thus ?case
by (metis listrel1I rtrancl.rtrancl_into_rtrancl)
qed }
thus "(xs,ys) \<in> (listrel1 r)\<^sup>*" using 1 by(blast elim: listrel1E)
qed
lemma rtrancl_listrel1_eq_len: "(x,y) \<in> (listrel1 r)\<^sup>* \<Longrightarrow> length x = length y"
by (induct rule: rtrancl.induct) (auto intro: listrel1_eq_len)
lemma rtrancl_listrel1_ConsI1: "(xs,ys) \<in> (listrel1 r)\<^sup>* \<Longrightarrow> (x#xs,x#ys) \<in> (listrel1 r)\<^sup>*"
proof (induction rule: rtrancl.induct) case (rtrancl_into_rtrancl a b c)
then show ?case
by (metis listrel1I2 rtrancl.rtrancl_into_rtrancl)
qed auto
lemma listrel_rtrancl_eq_rtrancl_listrel1: "listrel (r\<^sup>*) = (listrel1 r)\<^sup>*"
proof
{ fix x y assume "(x,y) \<in> listrel (r\<^sup>*)"
then have "(x,y) \<in> (listrel1 r)\<^sup>*"
by induct (auto intro: rtrancl_listrel1_ConsI2) }
then show "listrel (r\<^sup>*) \<subseteq> (listrel1 r)\<^sup>*"
by (rule subrelI)
next
show "listrel (r\<^sup>*) \<supseteq> (listrel1 r)\<^sup>*"
proof(rule subrelI)
fix xs ys assume "(xs,ys) \<in> (listrel1 r)\<^sup>*"
then show "(xs,ys) \<in> listrel (r\<^sup>*)"
proof induct case base show ?case by(auto simp add: listrel_iff_zip set_zip)
next case (step ys zs)
thus ?case by (metis listrel_reflcl_if_listrel1 listrel_rtrancl_trans)
qed
qed
qed
lemma listrel_subset_rtrancl_listrel1: "listrel r \<subseteq> (listrel1 r)\<^sup>*"
by(fast intro:rtrancl_listrel1_if_listrel)
subsection \<open>Size function\<close>
lemma [measure_function]: "is_measure f \<Longrightarrow> is_measure (size_list f)"
by (rule is_measure_trivial)
lemma [measure_function]: "is_measure f \<Longrightarrow> is_measure (size_option f)"
by (rule is_measure_trivial)
lemma size_list_estimation[termination_simp]: "x \<in> set xs \<Longrightarrow> y < f x \<Longrightarrow> y < size_list f xs"
by (induct xs) auto
lemma size_list_estimation'[termination_simp]: "x \<in> set xs \<Longrightarrow> y \<le> f x \<Longrightarrow> y \<le> size_list f xs"
by (induct xs) auto
lemma size_list_map[simp]: "size_list f (map g xs) = size_list (f \<circ> g) xs"
by (induct xs) auto
lemma size_list_append[simp]: "size_list f (xs @ ys) = size_list f xs + size_list f ys"
by (induct xs, auto)
lemma size_list_pointwise[termination_simp]: "(\<And>x. x \<in> set xs \<Longrightarrow> f x \<le> g x) \<Longrightarrow> size_list f xs \<le> size_list g xs"
by (induct xs) force+
subsection \<open>Monad operation\<close>
definition bind :: "'a list \<Rightarrow> ('a \<Rightarrow> 'b list) \<Rightarrow> 'b list" where "bind xs f = concat (map f xs)"
hide_const (open) bind
lemma bind_simps [simp]: "List.bind [] f = []" "List.bind (x # xs) f = f x @ List.bind xs f"
by (simp_all add: bind_def)
lemma list_bind_cong [fundef_cong]:
assumes "xs = ys""(\<And>x. x \<in> set xs \<Longrightarrow> f x = g x)"
shows "List.bind xs f = List.bind ys g"
proof -
from assms(2) have "List.bind xs f = List.bind xs g"
by (induction xs) simp_all
with assms(1) show ?thesis by simp
qed
lemma set_list_bind: "set (List.bind xs f) = (\<Union>x\<in>set xs. set (f x))"
by (induction xs) simp_all
subsection \<open>Code generation\<close>
subsubsection \<open>Counterparts for set-related operations\<close>
context
begin
qualified definition member :: \<open>'a list \<Rightarrow> 'a \<Rightarrow> bool\<close> \<comment> \<open>only for code generation\<close>
where member_iff [code_abbrev, simp]: \<open>member xs x \<longleftrightarrow> x \<in> set xs\<close>
text \<open>
Use \<open>member\<close> only for generating executable code. Otherwise use
\<^prop>\<open>x \<in> set xs\<close> instead --- it is much easier to reason about.
\<close>
qualified lemma member_code [code, no_atp]:
\<open>member [] y \<longleftrightarrow> False\<close>
\<open>member (x # xs) y \<longleftrightarrow> x = y \<or> member xs y\<close>
by auto
qualified lemma Collect_member [code_unfold, no_atp]: \<comment> \<open>make preprocessor setup confluent\<close>
\<open>{x. List.member xs x \<and> P x} = Set.filter P (set xs)\<close>
by simp
qualified lemma Collect_pair_member [code_unfold, no_atp]: \<comment> \<open>make preprocessor setup confluent\<close>
\<open>{(x, y). List.member xs (x, y) \<and> P x y} = Set.filter (\<lambda>(x, y). P x y) (set xs)\<close>
by auto
qualified lemma Collect_triple_member [code_unfold, no_atp]: \<comment> \<open>make preprocessor setup confluent\<close>
\<open>{(x, y, z). List.member xs (x, y, z) \<and> P x y z} = Set.filter (\<lambda>(x, y, z). P x y z) (set xs)\<close>
by auto
end
lemma list_all_iff [code_abbrev]:
\<open>list_all P xs \<longleftrightarrow> Ball (set xs) P\<close>
by (simp add: list.pred_set)
definition list_ex :: \<open>('a \<Rightarrow> bool) \<Rightarrow> 'a list \<Rightarrow> bool\<close>
where list_ex_iff [code_abbrev]: \<open>list_ex P xs \<longleftrightarrow> Bex (set xs) P\<close>
definition list_ex1 :: \<open>('a \<Rightarrow> bool) \<Rightarrow> 'a list \<Rightarrow> bool\<close>
where list_ex1_iff [code_abbrev]: \<open>list_ex1 P xs \<longleftrightarrow> Set.can_select P (set xs)\<close>
text \<open>
Usually you should prefer \<open>\<forall>x\<in>set xs\<close>, \<open>\<exists>x\<in>set xs\<close> and \<open>\<exists>!x. x\<in>set xs \<and> _\<close> over \<^const>\<open>list_all\<close>, \<^const>\<open>list_ex\<close> and \<^const>\<open>list_ex1\<close> in specifications.
\<close>
lemma list_all_Nil_iff [code, no_atp]:
\<open>list_all P [] \<longleftrightarrow> True\<close>
by (simp add: list_all_iff)
lemma list_all_Cons_iff [code, no_atp]:
\<open>list_all P (x # xs) \<longleftrightarrow> P x \<and> list_all P xs\<close>
by (simp add: list_all_iff)
lemma list_ex_Nil_iff [simp, code, no_atp]:
\<open>list_ex P [] \<longleftrightarrow> False\<close>
by (simp add: list_ex_iff)
lemma list_ex_Cons_iff [simp, code, no_atp]:
\<open>list_ex P (x # xs) \<longleftrightarrow> P x \<or> list_ex P xs\<close>
by (simp add: list_ex_iff)
lemma list_ex1_Nil_iff [simp, code, no_atp]:
\<open>list_ex1 P [] \<longleftrightarrow> False\<close> by (auto simp add: list_ex1_iff)
lemma list_ex1_Cons_iff [simp, code, no_atp]:
\<open>list_ex1 P (x # xs) \<longleftrightarrow> (if P x then list_all (\<lambda>y. \<not> P y \<or> x = y) xs else list_ex1 P xs)\<close> by (auto simp add: list_ex1_iff list_all_iff)
lemma list_all_append [simp]:
\<open>list_all P (xs @ ys) \<longleftrightarrow> list_all P xs \<and> list_all P ys\<close> by (auto simp add: list_all_iff)
lemma list_ex_append [simp]:
\<open>list_ex P (xs @ ys) \<longleftrightarrow> list_ex P xs \<or> list_ex P ys\<close> by (auto simp add: list_ex_iff)
lemma list_all_rev [simp]:
\<open>list_all P (rev xs) \<longleftrightarrow> list_all P xs\<close> by (simp add: list_all_iff)
lemma list_ex_rev [simp]:
\<open>list_ex P (rev xs) \<longleftrightarrow> list_ex P xs\<close> by (simp add: list_ex_iff)
lemma list_all_length:
\<open>list_all P xs \<longleftrightarrow> (\<forall>n < length xs. P (xs ! n))\<close> by (auto simp add: list_all_iff set_conv_nth)
lemma list_ex_length:
\<open>list_ex P xs \<longleftrightarrow> (\<exists>n < length xs. P (xs ! n))\<close> by (auto simp add: list_ex_iff set_conv_nth)
lemma list_all_cong [fundef_cong]:
\<open>list_all f xs = list_all g ys\<close> if \<open>xs = ys\<close> \<open>(\<And>x. x \<in> set ys \<Longrightarrow> f x = g x)\<close>
using that by (rule list.pred_cong)
lemma list_ex_cong [fundef_cong]:
\<open>list_ex f xs = list_ex g ys\<close> if \<open>xs = ys\<close> \<open>(\<And>x. x \<in> set ys \<Longrightarrow> f x = g x)\<close>
using that by (simp add: list_ex_iff)
context begin
qualified definition superset :: \<open>'a list \<Rightarrow> 'a list \<Rightarrow> bool\<close> where superset_iff [code_abbrev, simp]: \<open>superset ys xs \<longleftrightarrow> set xs \<subseteq> set ys\<close>
lemma [code, no_atp]:
\<open>superset xs = list_all (\<lambda>x. x \<in> set xs)\<close> by (auto simp: fun_eq_iff list_all_iff)
end
text \<open>Executable checks for relations on sets\<close>
definition listrel1p :: \<open>('a \<Rightarrow> 'a \<Rightarrow> bool) \<Rightarrow> 'a list \<Rightarrow> 'a list \<Rightarrow> bool\<close> \<comment> \<open>only for code generation\<close> where \<open>listrel1p r xs ys \<longleftrightarrow> (xs, ys) \<in> listrel1 {(x, y). r x y}\<close>
lemma [code_unfold]:
\<open>(xs, ys) \<in> listrel1 r \<longleftrightarrow> listrel1p (\<lambda>x y. (x, y) \<in> r) xs ys\<close> by (simp add: listrel1p_def)
lemma [code]:
\<open>listrel1p r [] xs \<longleftrightarrow> False\<close>
\<open>listrel1p r xs [] \<longleftrightarrow> False\<close>
\<open>listrel1p r (x # xs) (y # ys) \<longleftrightarrow>
r x y \<and> xs = ys \<or> x = y \<and> listrel1p r xs ys\<close> by (simp_all add: listrel1p_def)
definition lexordp :: \<open>('a \<Rightarrow> 'a \<Rightarrow> bool) \<Rightarrow> 'a list \<Rightarrow> 'a list \<Rightarrow> bool\<close> \<comment> \<open>only for code generation\<close> where \<open>lexordp r xs ys \<longleftrightarrow> (xs, ys) \<in> lexord {(x, y). r x y}\<close>
lemma [code_unfold]:
\<open>(xs, ys) \<in> lexord r = lexordp (\<lambda>x y. (x, y) \<in> r) xs ys\<close> by (simp add: lexordp_def)
lemma [code]:
\<open>lexordp r xs [] \<longleftrightarrow> False\<close>
\<open>lexordp r [] (y # ys) \<longleftrightarrow> True\<close>
\<open>lexordp r (x # xs) (y # ys) \<longleftrightarrow>
r x y \<or> (x = y \<and> lexordp r xs ys)\<close> by (simp_all add: add: lexordp_def)
text \<open>Executable intervals\<close>
context preorder begin
lemma forall_less_eq_iff [code_unfold]:
\<open>(\<forall>n\<le>b. P n) \<longleftrightarrow> (\<forall>n\<in>{..b}. P n)\<close> byauto
lemma exists_less_eq_iff [code_unfold]:
\<open>(\<exists>n\<le>b. P n) \<longleftrightarrow> (\<exists>n\<in>{..b}. P n)\<close> byauto
lemma forall_less_iff [code_unfold]:
\<open>(\<forall>n<b. P n) \<longleftrightarrow> (\<forall>n\<in>{..<b}. P n)\<close> byauto
lemma exists_less_iff [code_unfold]:
\<open>(\<exists>n<b. P n) \<longleftrightarrow> (\<exists>n\<in>{..<b}. P n)\<close> byauto
lemma forall_greater_eq_iff [code_unfold]:
\<open>(\<forall>n\<ge>a. P n) \<longleftrightarrow> (\<forall>n\<in>{a..}. P n)\<close> byauto
lemma exists_greater_eq_iff [code_unfold]:
\<open>(\<exists>n\<ge>a. P n) \<longleftrightarrow> (\<exists>n\<in>{a..}. P n)\<close> byauto
lemma forall_greater_iff [code_unfold]:
\<open>(\<forall>n>a. P n) \<longleftrightarrow> (\<forall>n\<in>{a<..}. P n)\<close> byauto
lemma exists_greater_iff [code_unfold]:
\<open>(\<exists>n>a. P n) \<longleftrightarrow> (\<exists>n\<in>{a<..}. P n)\<close> byauto
end
class interval = linorder + comm_semiring_1_cancel +
assumes finite_atLeastAtMost: \<open>finite {a..b}\<close>
assumes dec_less_imp_less_eq: \<open>a - 1 < b \<Longrightarrow> a \<le> b\<close>
assumes less_inc_imp_less_eq: \<open>a < b + 1 \<Longrightarrow> a \<le> b\<close>
assumes dec_greater_eq_self_imp_bot: \<open>a \<le> a - 1 \<Longrightarrow> a \<le> c\<close>
assumes inc_less_eq_self_imp_top: \<open>b + 1 \<le> b \<Longrightarrow> d \<le> b\<close> begin
context begin
qualified lemma less_imp_less_eq_dec:
\<open>c < b \<Longrightarrow> a < b \<Longrightarrow> a \<le> b - 1\<close>
using local.dec_less_imp_less_eq local.not_less by blast
qualified lemma less_imp_in_less_eq:
\<open>a < c \<Longrightarrow> a < b \<Longrightarrow> a + 1 \<le> b\<close>
using local.less_inc_imp_less_eq local.not_less by blast
qualified lemma less_eq_dec_imp_less:
\<open>c < b \<Longrightarrow> a \<le> b - 1 \<Longrightarrow> a < b\<close>
using local.dec_greater_eq_self_imp_bot local.dual_order.trans local.not_le by blast
qualified lemma inc_less_eq_imp_less:
\<open>a < c \<Longrightarrow> a + 1 \<le> b \<Longrightarrow> a < b\<close>
using local.inc_less_eq_self_imp_top local.not_le local.order.strict_trans2 by blast
qualified definition interval :: \<open>'a \<Rightarrow> 'a \<Rightarrow> 'a list\<close> \<comment> \<open>only for code generation\<close> where interval_eq: \<open>interval a b = sorted_list_of_set {a..b}\<close>
qualified lemma set_interval_eq [simp]:
\<open>set (interval a b) = {a..b}\<close>
using finite_atLeastAtMost [of a b] by (simp add: interval_eq)
qualified lemma distinct_interval [simp]:
\<open>distinct (interval a b)\<close> by (simp add: interval_eq)
qualified lemma interval_code [code]:
\<open>interval a b = (if a < b then a # interval (a + 1) b elseif a = b then [a] else [])\<close>
proof -
consider (less) \<open>a < b\<close> | (eq) \<open>a = b\<close> | (greater) \<open>a > b\<close>
using less_linear by blast then show ?thesis proof cases
case less then have \<open>{a..b} = insert a {a + 1..b}\<close> by (auto simp add: not_le dest: less_imp_le local.inc_less_eq_imp_less dest!: less_inc_imp_less_eq)
moreover have \<open>{a + 1..b} - {a} = {a + 1..b}\<close>
using less by (auto dest: local.inc_less_eq_imp_less)
moreover have \<open>insort a (sorted_list_of_set {a + 1..b}) = a # sorted_list_of_set {a + 1..b}\<close>
using finite_atLeastAtMost [of \<open>a + 1\<close> b] less by (auto intro!: insort_is_Cons dest: local.inc_less_eq_imp_less less_imp_le)
ultimately show ?thesis
using less finite_atLeastAtMost [of \<open>a + 1\<close> b] by (simp add: interval_eq)
next
case eq then show ?thesis by (simp add: interval_eq)
next
case greater then show ?thesis by (auto simp add: interval_eq)
qed
qed
qualified lemma atLeastAtMost_eq_interval [code]:
\<open>{a..b} = set (interval a b)\<close> by simp
qualified lemma atLeastLessThan_eq_interval [code]:
\<open>{a..<b} = (let d = b - 1inif d < b then set (interval a d) else {})\<close> by (auto simp add: Let_def not_less local.less_imp_less_eq_dec intro: dec_greater_eq_self_imp_bot)
qualified lemma greaterThanAtMost_eq_interval [code]:
\<open>{a<..b} = (let c = a + 1inif a < c then set (interval c b) else {})\<close> by (auto simp add: Let_def not_less dec_less_imp_less_eq intro: inc_less_eq_self_imp_top)
qualified lemma greaterThanLessThan_eq_interval [code]:
\<open>{a<..<b} = (let c = a + 1; d = b - 1inif a < c \<and> d < b then set (interval c d) else {})\<close> by (auto simp add: Let_def not_less dec_less_imp_less_eq
dest: local.less_imp_less_eq_dec local.inc_less_eq_imp_less local.less_eq_dec_imp_less)
qualified definition all_interval :: \<open>('a \<Rightarrow> bool) \<Rightarrow> 'a \<Rightarrow> 'a \<Rightarrow> bool\<close> \<comment> \<open>only for code generation\<close> where all_interval_iff [code_post, simp]: \<open>all_interval P a b \<longleftrightarrow> (\<forall>n\<in>{a..b}. P n)\<close>
qualified lemma all_interval_code [code]:
\<open>all_interval P a b \<longleftrightarrow> ((a < b \<longrightarrow> P a \<and> all_interval P (a + 1) b) \<and> (a = b \<longrightarrow> P a))\<close> by (simp only: all_interval_iff interval_code [of a b] flip: set_interval_eq) auto
qualified lemma forall_atLeastAtMost_iff [code_unfold]:
\<open>(\<forall>n\<in>{a..b}. P n) \<longleftrightarrow> all_interval P a b\<close> by simp
qualified lemma exists_atLeastAtMost_iff [code_unfold]:
\<open>(\<exists>n\<in>{a..b}. P n) \<longleftrightarrow> \<not> all_interval (Not \<circ> P) a b\<close>
using forall_atLeastAtMost_iff [of a b \<open>Not \<circ> P\<close>] by simp
qualified lemma forall_atLeastLessThan_iff [code_unfold]:
\<open>(\<forall>n\<in>{a..<b}. P n) \<longleftrightarrow> (let d = b - 1in d < b \<longrightarrow> all_interval P a d)\<close> by (auto simp add: not_less Let_def intro: local.less_eq_dec_imp_less local.less_imp_less_eq_dec elim!: bspec)
qualified lemma exists_atLeastLessThan_iff [code_unfold]:
\<open>(\<exists>n\<in>{a..<b}. P n) \<longleftrightarrow> (let d = b - 1in d < b \<and> \<not> all_interval (Not \<circ> P) a d)\<close>
using forall_atLeastLessThan_iff [of a b \<open>Not \<circ> P\<close>] by (auto simp add: Let_def)
qualified lemma forall_greaterThanAtMost_iff [code_unfold]:
\<open>(\<forall>n\<in>{a<..b}. P n) \<longleftrightarrow> (let c = a + 1in a < c \<longrightarrow> all_interval P c b)\<close> by (auto simp add: Let_def not_less intro: local.less_imp_in_less_eq local.inc_less_eq_imp_less elim!: bspec)
qualified lemma exists_greaterThanAtMost_iff [code_unfold]:
\<open>(\<exists>n\<in>{a<..b}. P n) \<longleftrightarrow> (let c = a + 1in a < c \<and> \<not> all_interval (Not \<circ> P) c b)\<close>
using forall_greaterThanAtMost_iff [of a b \<open>Not \<circ> P\<close>] by (auto simp add: Let_def)
qualified lemma forall_greaterThanLessThan_iff [code_unfold]:
\<open>(\<forall>n\<in>{a<..<b}. P n) \<longleftrightarrow> (let c = a + 1; d = b - 1in a < c \<longrightarrow> d < b \<longrightarrow> all_interval P c d)\<close> by (auto simp add: Let_def not_less local.less_imp_in_less_eq local.less_imp_less_eq_dec
intro: local.inc_less_eq_imp_less local.less_eq_dec_imp_less elim!: bspec)
qualified lemma exists_greaterThanLessThan_iff [code_unfold]:
\<open>(\<exists>n\<in>{a<..<b}. P n) \<longleftrightarrow> (let c = a + 1; d = b - 1in a < c \<and> d < b \<and> \<not> all_interval (Not \<circ> P) c d)\<close>
using forall_greaterThanLessThan_iff [of a b \<open>Not \<circ> P\<close>] by (auto simp add: Let_def)
qualified definition map_tailrec_rev :: \<open>('a \<Rightarrow> 'b) \<Rightarrow> 'a list \<Rightarrow> 'b list \<Rightarrow> 'b list\<close> \<comment> \<open>only for code generation\<close> where map_tailrec_rev [simp]: \<open>map_tailrec_rev f as bs = rev (map f as) @ bs\<close>
text \<open>
Optional tail recursive version of \<^const>\<open>map\<close>. Can avoid
stack overflow in some target languages. Do not use for proving.
\<close>
qualified lemma map_tailrec_rev_code [code, no_atp]:
\<open>map_tailrec_rev f [] bs = bs\<close>
\<open>map_tailrec_rev f (a # as) bs = map_tailrec_rev f as (f a # bs)\<close> by simp_all
qualified definition map_tailrec :: \<open>('a \<Rightarrow> 'b) \<Rightarrow> 'a list \<Rightarrow> 'b list\<close> \<comment> \<open>only for code generation\<close> where map_tailrec_eq [simp]: \<open>map_tailrec = map\<close>
qualified lemma map_tailrec_code [code, no_atp]:
\<open>map_tailrec f as = rev (map_tailrec_rev f as [])\<close> by simp
text \<open>Potential code equation:\<close>
qualified lemma map_eq_map_tailrec:
\<open>map = map_tailrec\<close> by simp
end
definition map_filter :: \<open>('a \<Rightarrow> 'b option) \<Rightarrow> 'a list \<Rightarrow> 'b list\<close> where [code_post]: "map_filter f xs = map (the \<circ> f) (filter (\<lambda>x. f x \<noteq> None) xs)"
text \<open>
Operation \<^const>\<open>map_filter\<close> avoids
intermediate lists on execution -- do not use for proving.
\<close>
lemma map_filter_simps [simp, code, no_atp]:
\<open>map_filter f [] = []\<close>
\<open>map_filter f (x # xs) = (case f x of None \<Rightarrow> map_filter f xs | Some y \<Rightarrow> y # map_filter f xs)\<close> by (simp_all add: map_filter_def split: option.split)
lemma map_filter_map_filter [code_unfold]:
\<open>map f (filter P xs) = map_filter (\<lambda>x. if P x then Some (f x) else None) xs\<close> by (simp add: map_filter_def)
hide_const (open) map_filter
subsubsection \<open>Operations for optimization and efficiency\<close>
context begin
qualified definition null :: \<open>'a list \<Rightarrow> bool\<close> \<comment> \<open>only for code generation\<close> where null_iff [code_abbrev, simp]: \<open>null xs \<longleftrightarrow> xs = []\<close>
lemma insert_code [code]: "insert x (set xs) = set (List.insert x xs)" "insert x (List.coset xs) = List.coset (removeAll x xs)" by simp_all
lemma remove_code [code]: "Set.remove x (set xs) = set (removeAll x xs)" "Set.remove x (List.coset xs) = List.coset (List.insert x xs)"
by (simp_all add: set_eq_iff ac_simps)
lemma filter_set [code]: "Set.filter P (set xs) = set (filter P xs)"
by simp
lemma image_set [code]: "image f (set xs) = set (map f xs)"
by simp
lemma subset_code [code]: "set xs \<subseteq> B \<longleftrightarrow> (\<forall>x\<in>set xs. x \<in> B)" "A \<subseteq> List.coset ys \<longleftrightarrow> (\<forall>y\<in>set ys. y \<notin> A)" "List.coset [] \<subseteq> set [] \<longleftrightarrow> False"
by auto
lemma Ball_set [code]: "Ball (set xs) P \<longleftrightarrow> list_all P xs"
by (simp add: list_all_iff)
lemma Bex_set [code]: "Bex (set xs) P \<longleftrightarrow> list_ex P xs"
by (simp add: list_ex_iff)
lemma the_elem_set [code]: "the_elem (set [x]) = x"
by simp
lemma Pow_set [code]: "Pow (set []) = {{}}" "Pow (set (x # xs)) = (let A = Pow (set xs) in A \<union> insert x ` A)"
by (simp_all add: Pow_insert Let_def)
lemma these_set_code [code]:
\<open>Option.these (set xs) = set (List.map_filter (\<lambda>x. x) xs)\<close>
by (simp add: Option.these_eq Option.is_none_def set_eq_iff map_filter_def)
lemma image_filter_set_eq [code]:
\<open>Option.image_filter f (set xs) = set (List.map_filter f xs)\<close> apply (simp add: Option.image_filter_eq these_set_code set_eq_iff flip: set_map) apply (auto simp add: map_filter_def image_iff)
done
lemma can_select_set_list_ex1 [code]: "Set.can_select P (set A) = list_ex1 P A"
by (simp add: list_ex1_iff)
lemma product_code [code]: "Product_Type.product (set xs) (set ys) = set [(x, y). x \<leftarrow> xs, y \<leftarrow> ys]"
by (auto simp add: Product_Type.product_def)
lemma Id_on_set [code]: "Id_on (set xs) = set [(x, x). x \<leftarrow> xs]"
by (auto simp add: Id_on_def)
lemma Image_code [code]: "R `` S = Option.image_filter (\<lambda>(x, y). if x \<in> S then Some y else None) R" apply (simp add: Option.image_filter_eq case_prod_unfold Option.these_eq) apply force
done
fun add_literal_list target = let
fun pretty literals pr _ vars fxy [(t1, _), (t2, _)] = case Option.map (cons t1) (implode_list t2)
of SOME ts =>
Code_Printer.literal_list literals (map (pr vars Code_Printer.NOBR) ts)
| NONE =>
print_list (Code_Printer.infix_cons literals) (pr vars) fxy t1 t2;
in
Code_Target.set_printings (Code_Symbol.Constant (\<^const_name>\<open>Cons\<close>,
[(target, SOME (Code_Printer.complex_const_syntax (2, pretty)))]))
end
end;
\<close>
code_printing
type_constructor list \<rightharpoonup>
(SML) "_ list" and (OCaml) "_ list" and (Haskell) "![(_)]" and (Scala) "List[(_)]"
| constant Nil \<rightharpoonup>
(SML) "[]" and (OCaml) "[]" and (Haskell) "[]" and (Scala) "!Nil"
| class_instance list :: equal \<rightharpoonup>
(Haskell) -
| constant "HOL.equal :: 'a list \<Rightarrow> 'a list \<Rightarrow> bool" \<rightharpoonup>
(Haskell) infix 4"=="
subsection \<open>Setup for Lifting/Transfer\<close>
subsubsection \<open>Transfer rules for the Transfer package\<close>
context includes lifting_syntax
begin
lemma tl_transfer [transfer_rule]: "(list_all2 A ===> list_all2 A) tl tl"
unfolding tl_def[abs_def] by transfer_prover
lemma butlast_transfer [transfer_rule]: "(list_all2 A ===> list_all2 A) butlast butlast"
by (rule rel_funI, erule list_all2_induct, auto)
lemma append_transfer [transfer_rule]: "(list_all2 A ===> list_all2 A ===> list_all2 A) append append"
unfolding List.append_def by transfer_prover
lemma rev_transfer [transfer_rule]: "(list_all2 A ===> list_all2 A) rev rev"
unfolding List.rev_def by transfer_prover
lemma filter_transfer [transfer_rule]: "((A ===> (=)) ===> list_all2 A ===> list_all2 A) filter filter"
unfolding List.filter_def by transfer_prover
lemma fold_transfer [transfer_rule]: "((A ===> B ===> B) ===> list_all2 A ===> B ===> B) fold fold"
unfolding List.fold_def by transfer_prover
lemma foldr_transfer [transfer_rule]: "((A ===> B ===> B) ===> list_all2 A ===> B ===> B) foldr foldr"
unfolding List.foldr_def by transfer_prover
lemma foldl_transfer [transfer_rule]: "((B ===> A ===> B) ===> B ===> list_all2 A ===> B) foldl foldl"
unfolding List.foldl_def by transfer_prover
lemma concat_transfer [transfer_rule]: "(list_all2 (list_all2 A) ===> list_all2 A) concat concat"
unfolding List.concat_def by transfer_prover
lemma drop_transfer [transfer_rule]: "((=) ===> list_all2 A ===> list_all2 A) drop drop"
unfolding List.drop_def by transfer_prover
lemma take_transfer [transfer_rule]: "((=) ===> list_all2 A ===> list_all2 A) take take"
unfolding List.take_def by transfer_prover
lemma list_update_transfer [transfer_rule]: "(list_all2 A ===> (=) ===> A ===> list_all2 A) list_update list_update"
unfolding list_update_def by transfer_prover
lemma takeWhile_transfer [transfer_rule]: "((A ===> (=)) ===> list_all2 A ===> list_all2 A) takeWhile takeWhile"
unfolding takeWhile_def by transfer_prover
lemma dropWhile_transfer [transfer_rule]: "((A ===> (=)) ===> list_all2 A ===> list_all2 A) dropWhile dropWhile"
unfolding dropWhile_def by transfer_prover
lemma zip_transfer [transfer_rule]: "(list_all2 A ===> list_all2 B ===> list_all2 (rel_prod A B)) zip zip"
unfolding zip_def by transfer_prover
lemma product_transfer [transfer_rule]: "(list_all2 A ===> list_all2 B ===> list_all2 (rel_prod A B)) List.product List.product"
unfolding List.product_def by transfer_prover
lemma product_lists_transfer [transfer_rule]: "(list_all2 (list_all2 A) ===> list_all2 (list_all2 A)) product_lists product_lists"
unfolding product_lists_def by transfer_prover
lemma insert_transfer [transfer_rule]:
assumes [transfer_rule]: "bi_unique A"
shows "(A ===> list_all2 A ===> list_all2 A) List.insert List.insert"
unfolding List.insert_def [abs_def] by transfer_prover
lemma find_transfer [transfer_rule]: "((A ===> (=)) ===> list_all2 A ===> rel_option A) List.find List.find"
unfolding List.find_def by transfer_prover
lemma those_transfer [transfer_rule]: "(list_all2 (rel_option P) ===> rel_option (list_all2 P)) those those"
unfolding List.those_def by transfer_prover
lemma remove1_transfer [transfer_rule]:
assumes [transfer_rule]: "bi_unique A"
shows "(A ===> list_all2 A ===> list_all2 A) remove1 remove1"
unfolding remove1_def by transfer_prover
lemma removeAll_transfer [transfer_rule]:
assumes [transfer_rule]: "bi_unique A"
shows "(A ===> list_all2 A ===> list_all2 A) removeAll removeAll"
unfolding removeAll_def by transfer_prover
lemma successively_transfer [transfer_rule]: "((A ===> A ===> (=)) ===> list_all2 A ===> (=)) successively successively"
unfolding successively_altdef by transfer_prover
lemma distinct_transfer [transfer_rule]:
assumes [transfer_rule]: "bi_unique A"
shows "(list_all2 A ===> (=)) distinct distinct"
unfolding distinct_def by transfer_prover
lemma distinct_adj_transfer [transfer_rule]:
assumes "bi_unique A"
shows "(list_all2 A ===> (=)) distinct_adj distinct_adj"
unfolding rel_fun_def
proof (intro allI impI)
fix xs ys assume "list_all2 A xs ys"
thus "distinct_adj xs \<longleftrightarrow> distinct_adj ys"
proof (induction rule: list_all2_induct) case (Cons x xs y ys)
show ?case
by (metis Cons assms bi_unique_def distinct_adj_Cons list.rel_sel)
qed auto
qed
lemma remdups_transfer [transfer_rule]:
assumes [transfer_rule]: "bi_unique A"
shows "(list_all2 A ===> list_all2 A) remdups remdups"
unfolding remdups_def by transfer_prover
lemma remdups_adj_transfer [transfer_rule]:
assumes [transfer_rule]: "bi_unique A"
shows "(list_all2 A ===> list_all2 A) remdups_adj remdups_adj"
proof (rule rel_funI, erule list_all2_induct)
qed (auto simp: remdups_adj_Cons assms[unfolded bi_unique_def] split: list.splits)
lemma replicate_transfer [transfer_rule]: "((=) ===> A ===> list_all2 A) replicate replicate"
unfolding replicate_def by transfer_prover
lemma length_transfer [transfer_rule]: "(list_all2 A ===> (=)) length length"
unfolding size_list_overloaded_def size_list_def by transfer_prover
lemma rotate1_transfer [transfer_rule]: "(list_all2 A ===> list_all2 A) rotate1 rotate1"
unfolding rotate1_def by transfer_prover
lemma rotate_transfer [transfer_rule]: "((=) ===> list_all2 A ===> list_all2 A) rotate rotate"
unfolding rotate_def [abs_def] by transfer_prover
lemma nths_transfer [transfer_rule]: "(list_all2 A ===> rel_set (=) ===> list_all2 A) nths nths"
unfolding nths_def [abs_def] by transfer_prover
lemma subseqs_transfer [transfer_rule]: "(list_all2 A ===> list_all2 (list_all2 A)) subseqs subseqs"
unfolding subseqs_def [abs_def] by transfer_prover
lemma partition_transfer [transfer_rule]: "((A ===> (=)) ===> list_all2 A ===> rel_prod (list_all2 A) (list_all2 A))
partition partition"
unfolding partition_def by transfer_prover
lemma lists_transfer [transfer_rule]: "(rel_set A ===> rel_set (list_all2 A)) lists lists"
proof (rule rel_funI, rule rel_setI)
show "\<lbrakk>l \<in> lists X; rel_set A X Y\<rbrakk> \<Longrightarrow> \<exists>y\<in>lists Y. list_all2 A l y" for X Y l
proof (induction l rule: lists.induct) case (Cons a l)
then show ?case
by (simp only: rel_set_def list_all2_Cons1, metis lists.Cons)
qed auto
show "\<lbrakk>l \<in> lists Y; rel_set A X Y\<rbrakk> \<Longrightarrow> \<exists>x\<in>lists X. list_all2 A x l" for X Y l
proof (induction l rule: lists.induct) case (Cons a l)
then show ?case
by (simp only: rel_set_def list_all2_Cons2, metis lists.Cons)
qed auto
qed
lemma set_Cons_transfer [transfer_rule]: "(rel_set A ===> rel_set (list_all2 A) ===> rel_set (list_all2 A))
set_Cons set_Cons"
unfolding rel_fun_def rel_set_def set_Cons_def
by (fastforce simp add: list_all2_Cons1 list_all2_Cons2)
lemma listset_transfer [transfer_rule]: "(list_all2 (rel_set A) ===> rel_set (list_all2 A)) listset listset"
unfolding listset_def by transfer_prover
lemma null_transfer [transfer_rule]: "(list_all2 A ===> (=)) List.null List.null"
unfolding rel_fun_def by auto
lemma list_all_transfer [transfer_rule]: "((A ===> (=)) ===> list_all2 A ===> (=)) list_all list_all"
using list.pred_transfer by blast
lemma list_ex_transfer [transfer_rule]: "((A ===> (=)) ===> list_all2 A ===> (=)) list_ex list_ex"
unfolding list_ex_iff [abs_def] by transfer_prover
lemma splice_transfer [transfer_rule]: "(list_all2 A ===> list_all2 A ===> list_all2 A) splice splice" apply (rule rel_funI, erule list_all2_induct, simp add: rel_fun_def, simp) apply (rule rel_funI) apply (erule_tac xs=x in list_all2_induct, simp, simp add: rel_fun_def)
done
lemma shuffles_transfer [transfer_rule]: "(list_all2 A ===> list_all2 A ===> rel_set (list_all2 A)) shuffles shuffles"
proof (intro rel_funI, goal_cases) case (1 xs xs' ys ys')
thus ?case
proof (induction xs ys arbitrary: xs' ys' rule: shuffles.induct) case (3 x xs y ys xs' ys')
from "3.prems" obtain x' xs'' where xs': "xs' = x' # xs''" by (cases xs') auto
from "3.prems" obtain y' ys'' where ys': "ys' = y' # ys''" by (cases ys') auto
have [transfer_rule]: "A x x'""A y y'""list_all2 A xs xs''""list_all2 A ys ys''"
using "3.prems" by (simp_all add: xs' ys')
have [transfer_rule]: "rel_set (list_all2 A) (shuffles xs (y # ys)) (shuffles xs'' ys')"and
[transfer_rule]: "rel_set (list_all2 A) (shuffles (x # xs) ys) (shuffles xs' ys'')"
using "3.prems" by (auto intro!: "3.IH" simp: xs' ys')
have "rel_set (list_all2 A) ((#) x ` shuffles xs (y # ys) \<union> (#) y ` shuffles (x # xs) ys)
((#) x' ` shuffles xs'' ys' \<union> (#) y' ` shuffles xs' ys'')" by transfer_prover
thus ?case by (simp add: xs' ys')
qed (auto simp: rel_set_def)
qed
lemma rtrancl_parametric [transfer_rule]:
assumes [transfer_rule]: "bi_unique A""bi_total A"
shows "(rel_set (rel_prod A A) ===> rel_set (rel_prod A A)) rtrancl rtrancl"
unfolding rtrancl_def by transfer_prover
lemma monotone_parametric [transfer_rule]:
assumes [transfer_rule]: "bi_total A"
shows "((A ===> A ===> (=)) ===> (B ===> B ===> (=)) ===> (A ===> B) ===> (=)) monotone monotone"
unfolding monotone_def[abs_def] by transfer_prover
lemma fun_ord_parametric [transfer_rule]:
assumes [transfer_rule]: "bi_total C"
shows "((A ===> B ===> (=)) ===> (C ===> A) ===> (C ===> B) ===> (=)) fun_ord fun_ord"
unfolding fun_ord_def[abs_def] by transfer_prover
lemma fun_lub_parametric [transfer_rule]:
assumes [transfer_rule]: "bi_total A""bi_unique A"
shows "((rel_set A ===> B) ===> rel_set (C ===> A) ===> C ===> B) fun_lub fun_lub"
unfolding fun_lub_def[abs_def] by transfer_prover
end
subsection \<open>Misc\<close>
lemma Ball_set_list_all: (* FIXME delete candidate *) "Ball (set xs) P \<longleftrightarrow> list_all P xs"
by (fact Ball_set)
lemma Bex_set_list_ex: (* FIXME delete candidate *) "Bex (set xs) P \<longleftrightarrow> list_ex P xs"
by (fact Bex_set)
end
Messung V0.5 in Prozent
¤ 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.3.404Bemerkung:
¤
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.