# HG changeset patch # User wenzelm # Date 1020794069 -7200 # Node ID f2b00262bdfc2928e3f1e7c9ce8b09a75f1d732f # Parent 5eb9be7b72a51ce94fd72c85d4c9d7519022df42 converted; diff -r 5eb9be7b72a5 -r f2b00262bdfc src/HOL/List.ML --- a/src/HOL/List.ML Tue May 07 19:54:04 2002 +0200 +++ b/src/HOL/List.ML Tue May 07 19:54:29 2002 +0200 @@ -1,1555 +1,269 @@ -(* Title: HOL/List - ID: $Id$ - Author: Tobias Nipkow - Copyright 1994 TU Muenchen -List lemmas -*) - -Goal "!x. xs ~= x#xs"; -by (induct_tac "xs" 1); -by Auto_tac; -qed_spec_mp "not_Cons_self"; -bind_thm("not_Cons_self2",not_Cons_self RS not_sym); -Addsimps [not_Cons_self,not_Cons_self2]; - -Goal "(xs ~= []) = (? y ys. xs = y#ys)"; -by (induct_tac "xs" 1); -by Auto_tac; -qed "neq_Nil_conv"; - -(* Induction over the length of a list: *) -val [prem] = Goal - "(!!xs. (!ys. length ys < length xs --> P ys) ==> P xs) ==> P(xs)"; -by (rtac measure_induct 1 THEN etac prem 1); -qed "length_induct"; - - -(** "lists": the list-forming operator over sets **) - -Goalw lists.defs "A<=B ==> lists A <= lists B"; -by (rtac lfp_mono 1); -by (REPEAT (ares_tac basic_monos 1)); -qed "lists_mono"; - -bind_thm ("listsE", lists.mk_cases "x#l : lists A"); -AddSEs [listsE]; -AddSIs lists.intrs; - -Goal "l: lists A ==> l: lists B --> l: lists (A Int B)"; -by (etac lists.induct 1); -by (ALLGOALS Blast_tac); -qed_spec_mp "lists_IntI"; - -Goal "lists (A Int B) = lists A Int lists B"; -by (rtac (mono_Int RS equalityI) 1); -by (simp_tac (simpset() addsimps [mono_def, lists_mono]) 1); -by (blast_tac (claset() addSIs [lists_IntI]) 1); -qed "lists_Int_eq"; -Addsimps [lists_Int_eq]; - -Goal "(xs@ys : lists A) = (xs : lists A & ys : lists A)"; -by (induct_tac "xs" 1); -by (Auto_tac); -qed "append_in_lists_conv"; -AddIffs [append_in_lists_conv]; - -(** length **) -(* needs to come before "@" because of thm append_eq_append_conv *) - -section "length"; - -Goal "length(xs@ys) = length(xs)+length(ys)"; -by (induct_tac "xs" 1); -by Auto_tac; -qed"length_append"; -Addsimps [length_append]; +(** legacy ML bindings **) -Goal "length (map f xs) = length xs"; -by (induct_tac "xs" 1); -by Auto_tac; -qed "length_map"; -Addsimps [length_map]; - -Goal "length(rev xs) = length(xs)"; -by (induct_tac "xs" 1); -by Auto_tac; -qed "length_rev"; -Addsimps [length_rev]; - -Goal "length(tl xs) = (length xs) - 1"; -by (case_tac "xs" 1); -by Auto_tac; -qed "length_tl"; -Addsimps [length_tl]; - -Goal "(length xs = 0) = (xs = [])"; -by (induct_tac "xs" 1); -by Auto_tac; -qed "length_0_conv"; -AddIffs [length_0_conv]; - -Goal "(0 < length xs) = (xs ~= [])"; -by (induct_tac "xs" 1); -by Auto_tac; -qed "length_greater_0_conv"; -AddIffs [length_greater_0_conv]; +structure List = +struct -Goal "(length xs = Suc n) = (? y ys. xs = y#ys & length ys = n)"; -by (induct_tac "xs" 1); -by Auto_tac; -qed "length_Suc_conv"; - -(** @ - append **) - -section "@ - append"; - -Goal "(xs@ys)@zs = xs@(ys@zs)"; -by (induct_tac "xs" 1); -by Auto_tac; -qed "append_assoc"; -Addsimps [append_assoc]; - -Goal "xs @ [] = xs"; -by (induct_tac "xs" 1); -by Auto_tac; -qed "append_Nil2"; -Addsimps [append_Nil2]; - -Goal "(xs@ys = []) = (xs=[] & ys=[])"; -by (induct_tac "xs" 1); -by Auto_tac; -qed "append_is_Nil_conv"; -AddIffs [append_is_Nil_conv]; - -Goal "([] = xs@ys) = (xs=[] & ys=[])"; -by (induct_tac "xs" 1); -by Auto_tac; -qed "Nil_is_append_conv"; -AddIffs [Nil_is_append_conv]; - -Goal "(xs @ ys = xs) = (ys=[])"; -by (induct_tac "xs" 1); -by Auto_tac; -qed "append_self_conv"; +val thy = the_context (); -Goal "(xs = xs @ ys) = (ys=[])"; -by (induct_tac "xs" 1); -by Auto_tac; -qed "self_append_conv"; -AddIffs [append_self_conv,self_append_conv]; - -Goal "!ys. length xs = length ys | length us = length vs \ -\ --> (xs@us = ys@vs) = (xs=ys & us=vs)"; -by (induct_tac "xs" 1); - by (rtac allI 1); - by (case_tac "ys" 1); - by (Asm_simp_tac 1); - by (Force_tac 1); -by (rtac allI 1); -by (case_tac "ys" 1); -by (Force_tac 1); -by (Asm_simp_tac 1); -qed_spec_mp "append_eq_append_conv"; -Addsimps [append_eq_append_conv]; - -Goal "(xs @ ys = xs @ zs) = (ys=zs)"; -by (Simp_tac 1); -qed "same_append_eq"; - -Goal "(xs @ [x] = ys @ [y]) = (xs = ys & x = y)"; -by (Simp_tac 1); -qed "append1_eq_conv"; - -Goal "(ys @ xs = zs @ xs) = (ys=zs)"; -by (Simp_tac 1); -qed "append_same_eq"; - -AddIffs [same_append_eq, append1_eq_conv, append_same_eq]; - -Goal "(xs @ ys = ys) = (xs=[])"; -by (cut_inst_tac [("zs","[]")] append_same_eq 1); -by Auto_tac; -qed "append_self_conv2"; - -Goal "(ys = xs @ ys) = (xs=[])"; -by (simp_tac (simpset() addsimps - [simplify (simpset()) (read_instantiate[("ys","[]")]append_same_eq)]) 1); -by (Blast_tac 1); -qed "self_append_conv2"; -AddIffs [append_self_conv2,self_append_conv2]; - -Goal "xs ~= [] --> hd xs # tl xs = xs"; -by (induct_tac "xs" 1); -by Auto_tac; -qed_spec_mp "hd_Cons_tl"; -Addsimps [hd_Cons_tl]; - -Goal "hd(xs@ys) = (if xs=[] then hd ys else hd xs)"; -by (induct_tac "xs" 1); -by Auto_tac; -qed "hd_append"; - -Goal "xs ~= [] ==> hd(xs @ ys) = hd xs"; -by (asm_simp_tac (simpset() addsimps [hd_append] - addsplits [list.split]) 1); -qed "hd_append2"; -Addsimps [hd_append2]; - -Goal "tl(xs@ys) = (case xs of [] => tl(ys) | z#zs => zs@ys)"; -by (simp_tac (simpset() addsplits [list.split]) 1); -qed "tl_append"; - -Goal "xs ~= [] ==> tl(xs @ ys) = (tl xs) @ ys"; -by (asm_simp_tac (simpset() addsimps [tl_append] - addsplits [list.split]) 1); -qed "tl_append2"; -Addsimps [tl_append2]; - -(* trivial rules for solving @-equations automatically *) - -Goal "xs = ys ==> xs = [] @ ys"; -by (Asm_simp_tac 1); -qed "eq_Nil_appendI"; - -Goal "[| x#xs1 = ys; xs = xs1 @ zs |] ==> x#xs = ys@zs"; -by (dtac sym 1); -by (Asm_simp_tac 1); -qed "Cons_eq_appendI"; - -Goal "[| xs@xs1 = zs; ys = xs1 @ us |] ==> xs@ys = zs@us"; -by (dtac sym 1); -by (Asm_simp_tac 1); -qed "append_eq_appendI"; - - -(*** -Simplification procedure for all list equalities. -Currently only tries to rearranges @ to see if -- both lists end in a singleton list, -- or both lists end in the same list. -***) -local - -val list_eq_pattern = - Thm.read_cterm (Theory.sign_of (the_context ())) ("(xs::'a list) = ys",HOLogic.boolT) - -fun last (cons as Const("List.list.Cons",_) $ _ $ xs) = - (case xs of Const("List.list.Nil",_) => cons | _ => last xs) - | last (Const("List.op @",_) $ _ $ ys) = last ys - | last t = t - -fun list1 (Const("List.list.Cons",_) $ _ $ Const("List.list.Nil",_)) = true - | list1 _ = false - -fun butlast ((cons as Const("List.list.Cons",_) $ x) $ xs) = - (case xs of Const("List.list.Nil",_) => xs | _ => cons $ butlast xs) - | butlast ((app as Const("List.op @",_) $ xs) $ ys) = app $ butlast ys - | butlast xs = Const("List.list.Nil",fastype_of xs) - -val rearr_tac = - simp_tac (HOL_basic_ss addsimps [append_assoc,append_Nil,append_Cons]) - -fun list_eq sg _ (F as (eq as Const(_,eqT)) $ lhs $ rhs) = - let - val lastl = last lhs and lastr = last rhs - fun rearr conv = - let val lhs1 = butlast lhs and rhs1 = butlast rhs - val Type(_,listT::_) = eqT - val appT = [listT,listT] ---> listT - val app = Const("List.op @",appT) - val F2 = eq $ (app$lhs1$lastl) $ (app$rhs1$lastr) - val ct = cterm_of sg (HOLogic.mk_Trueprop(HOLogic.mk_eq(F,F2))) - val thm = prove_goalw_cterm [] ct (K [rearr_tac 1]) - handle ERROR => - error("The error(s) above occurred while trying to prove " ^ - string_of_cterm ct) - in Some((conv RS (thm RS trans)) RS eq_reflection) end - - in if list1 lastl andalso list1 lastr - then rearr append1_eq_conv - else - if lastl aconv lastr - then rearr append_same_eq - else None - end -in -val list_eq_simproc = mk_simproc "list_eq" [list_eq_pattern] list_eq +structure list = +struct + val distinct = thms "list.distinct"; + val inject = thms "list.inject"; + val exhaust = thm "list.exhaust"; + val cases = thms "list.cases"; + val split = thm "list.split"; + val split_asm = thm "list.split_asm"; + val induct = thm "list.induct"; + val recs = thms "list.recs"; + val simps = thms "list.simps"; + val size = thms "list.size"; end; -Addsimprocs [list_eq_simproc]; - - -(** map **) - -section "map"; - -Goal "(!x. x : set xs --> f x = g x) --> map f xs = map g xs"; -by (induct_tac "xs" 1); -by Auto_tac; -bind_thm("map_ext", impI RS (allI RS (result() RS mp))); - -Goal "map (%x. x) = (%xs. xs)"; -by (rtac ext 1); -by (induct_tac "xs" 1); -by Auto_tac; -qed "map_ident"; -Addsimps[map_ident]; - -Goal "map f (xs@ys) = map f xs @ map f ys"; -by (induct_tac "xs" 1); -by Auto_tac; -qed "map_append"; -Addsimps[map_append]; - -Goalw [o_def] "map (f o g) xs = map f (map g xs)"; -by (induct_tac "xs" 1); -by Auto_tac; -qed "map_compose"; -(*Addsimps[map_compose];*) - -Goal "rev(map f xs) = map f (rev xs)"; -by (induct_tac "xs" 1); -by Auto_tac; -qed "rev_map"; - -(* a congruence rule for map: *) -Goal "xs=ys ==> (!x. x : set ys --> f x = g x) --> map f xs = map g ys"; -by (hyp_subst_tac 1); -by (induct_tac "ys" 1); -by Auto_tac; -bind_thm("map_cong", impI RSN (2,allI RSN (2, result() RS mp))); - -Goal "(map f xs = []) = (xs = [])"; -by (case_tac "xs" 1); -by Auto_tac; -qed "map_is_Nil_conv"; -AddIffs [map_is_Nil_conv]; - -Goal "([] = map f xs) = (xs = [])"; -by (case_tac "xs" 1); -by Auto_tac; -qed "Nil_is_map_conv"; -AddIffs [Nil_is_map_conv]; - -Goal "(map f xs = y#ys) = (? x xs'. xs = x#xs' & f x = y & map f xs' = ys)"; -by (case_tac "xs" 1); -by (ALLGOALS Asm_simp_tac); -qed "map_eq_Cons"; - -Goal "!xs. map f xs = map f ys --> (!x y. f x = f y --> x=y) --> xs=ys"; -by (induct_tac "ys" 1); - by (Asm_simp_tac 1); -by (fast_tac (claset() addss (simpset() addsimps [map_eq_Cons])) 1); -qed_spec_mp "map_injective"; - -Goal "inj f ==> inj (map f)"; -by (blast_tac (claset() addDs [map_injective,injD] addIs [injI]) 1); -qed "inj_mapI"; - -Goalw [inj_on_def] "inj (map f) ==> inj f"; -by (Clarify_tac 1); -by (eres_inst_tac [("x","[x]")] ballE 1); - by (eres_inst_tac [("x","[y]")] ballE 1); - by (Asm_full_simp_tac 1); - by (Blast_tac 1); -by (Blast_tac 1); -qed "inj_mapD"; - -Goal "inj (map f) = inj f"; -by (blast_tac (claset() addDs [inj_mapD] addIs [inj_mapI]) 1); -qed "inj_map"; - -(** rev **) - -section "rev"; - -Goal "rev(xs@ys) = rev(ys) @ rev(xs)"; -by (induct_tac "xs" 1); -by Auto_tac; -qed "rev_append"; -Addsimps[rev_append]; - -Goal "rev(rev l) = l"; -by (induct_tac "l" 1); -by Auto_tac; -qed "rev_rev_ident"; -Addsimps[rev_rev_ident]; - -Goal "(rev xs = []) = (xs = [])"; -by (induct_tac "xs" 1); -by Auto_tac; -qed "rev_is_Nil_conv"; -AddIffs [rev_is_Nil_conv]; - -Goal "([] = rev xs) = (xs = [])"; -by (induct_tac "xs" 1); -by Auto_tac; -qed "Nil_is_rev_conv"; -AddIffs [Nil_is_rev_conv]; - -Goal "!ys. (rev xs = rev ys) = (xs = ys)"; -by (induct_tac "xs" 1); - by (Force_tac 1); -by (rtac allI 1); -by (case_tac "ys" 1); - by (Asm_simp_tac 1); -by (Force_tac 1); -qed_spec_mp "rev_is_rev_conv"; -AddIffs [rev_is_rev_conv]; - -val prems = Goal "[| P []; !!x xs. P xs ==> P(xs@[x]) |] ==> P xs"; -by (stac (rev_rev_ident RS sym) 1); -by (res_inst_tac [("list", "rev xs")] list.induct 1); -by (ALLGOALS Simp_tac); -by (resolve_tac prems 1); -by (eresolve_tac prems 1); -qed "rev_induct"; - -val rev_induct_tac = induct_thm_tac rev_induct; - -Goal "(xs = [] --> P) --> (!ys y. xs = ys@[y] --> P) --> P"; -by (rev_induct_tac "xs" 1); -by Auto_tac; -qed "rev_exhaust_aux"; - -bind_thm ("rev_exhaust", ObjectLogic.rulify rev_exhaust_aux); - - -(** set **) - -section "set"; - -Goal "finite (set xs)"; -by (induct_tac "xs" 1); -by Auto_tac; -qed "finite_set"; -AddIffs [finite_set]; - -Goal "set (xs@ys) = (set xs Un set ys)"; -by (induct_tac "xs" 1); -by Auto_tac; -qed "set_append"; -Addsimps[set_append]; - -Goal "set l <= set (x#l)"; -by Auto_tac; -qed "set_subset_Cons"; - -Goal "(set xs = {}) = (xs = [])"; -by (induct_tac "xs" 1); -by Auto_tac; -qed "set_empty"; -Addsimps [set_empty]; - -Goal "set(rev xs) = set(xs)"; -by (induct_tac "xs" 1); -by Auto_tac; -qed "set_rev"; -Addsimps [set_rev]; - -Goal "set(map f xs) = f`(set xs)"; -by (induct_tac "xs" 1); -by Auto_tac; -qed "set_map"; -Addsimps [set_map]; - -Goal "set(filter P xs) = {x. x : set xs & P x}"; -by (induct_tac "xs" 1); -by Auto_tac; -qed "set_filter"; -Addsimps [set_filter]; - -Goal "set[i..j(] = {k. i <= k & k < j}"; -by (induct_tac "j" 1); -by (ALLGOALS Asm_simp_tac); -by (etac ssubst 1); -by Auto_tac; -by (arith_tac 1); -qed "set_upt"; -Addsimps [set_upt]; +structure lists = +struct + val intrs = thms "lists.intros"; + val elims = thms "lists.cases"; + val elim = thm "lists.cases"; + val induct = thm "lists.induct"; + val mk_cases = InductivePackage.the_mk_cases (the_context ()) "List.lists"; + val [Nil, Cons] = intrs; +end; -Goal "(x : set xs) = (? ys zs. xs = ys@x#zs)"; -by (induct_tac "xs" 1); - by (Simp_tac 1); -by (Asm_simp_tac 1); -by (rtac iffI 1); -by (blast_tac (claset() addIs [eq_Nil_appendI,Cons_eq_appendI]) 1); -by (REPEAT(etac exE 1)); -by (case_tac "ys" 1); -by Auto_tac; -qed "in_set_conv_decomp"; - - -(* eliminate `lists' in favour of `set' *) - -Goal "(xs : lists A) = (!x : set xs. x : A)"; -by (induct_tac "xs" 1); -by Auto_tac; -qed "in_lists_conv_set"; - -bind_thm("in_listsD",in_lists_conv_set RS iffD1); -AddSDs [in_listsD]; -bind_thm("in_listsI",in_lists_conv_set RS iffD2); -AddSIs [in_listsI]; - -(** mem **) - -section "mem"; - -Goal "(x mem xs) = (x: set xs)"; -by (induct_tac "xs" 1); -by Auto_tac; -qed "set_mem_eq"; - - -(** list_all **) - -section "list_all"; - -Goal "list_all P xs = (!x:set xs. P x)"; -by (induct_tac "xs" 1); -by Auto_tac; -qed "list_all_conv"; - -Goal "list_all P (xs@ys) = (list_all P xs & list_all P ys)"; -by (induct_tac "xs" 1); -by Auto_tac; -qed "list_all_append"; -Addsimps [list_all_append]; - - -(** filter **) - -section "filter"; - -Goal "filter P (xs@ys) = filter P xs @ filter P ys"; -by (induct_tac "xs" 1); -by Auto_tac; -qed "filter_append"; -Addsimps [filter_append]; - -Goal "filter P (filter Q xs) = filter (%x. Q x & P x) xs"; -by (induct_tac "xs" 1); -by Auto_tac; -qed "filter_filter"; -Addsimps [filter_filter]; - -Goal "(!x : set xs. P x) --> filter P xs = xs"; -by (induct_tac "xs" 1); -by Auto_tac; -qed_spec_mp "filter_True"; -Addsimps [filter_True]; - -Goal "(!x : set xs. ~P x) --> filter P xs = []"; -by (induct_tac "xs" 1); -by Auto_tac; -qed_spec_mp "filter_False"; -Addsimps [filter_False]; - -Goal "length (filter P xs) <= length xs"; -by (induct_tac "xs" 1); -by Auto_tac; -by (asm_simp_tac (simpset() addsimps [le_SucI]) 1); -qed "length_filter"; -Addsimps[length_filter]; - -Goal "set (filter P xs) <= set xs"; -by Auto_tac; -qed "filter_is_subset"; -Addsimps [filter_is_subset]; - - -section "concat"; - -Goal "concat(xs@ys) = concat(xs)@concat(ys)"; -by (induct_tac "xs" 1); -by Auto_tac; -qed"concat_append"; -Addsimps [concat_append]; +end; -Goal "(concat xss = []) = (!xs:set xss. xs=[])"; -by (induct_tac "xss" 1); -by Auto_tac; -qed "concat_eq_Nil_conv"; -AddIffs [concat_eq_Nil_conv]; - -Goal "([] = concat xss) = (!xs:set xss. xs=[])"; -by (induct_tac "xss" 1); -by Auto_tac; -qed "Nil_eq_concat_conv"; -AddIffs [Nil_eq_concat_conv]; - -Goal "set(concat xs) = Union(set ` set xs)"; -by (induct_tac "xs" 1); -by Auto_tac; -qed"set_concat"; -Addsimps [set_concat]; - -Goal "map f (concat xs) = concat (map (map f) xs)"; -by (induct_tac "xs" 1); -by Auto_tac; -qed "map_concat"; - -Goal "filter p (concat xs) = concat (map (filter p) xs)"; -by (induct_tac "xs" 1); -by Auto_tac; -qed"filter_concat"; - -Goal "rev(concat xs) = concat (map rev (rev xs))"; -by (induct_tac "xs" 1); -by Auto_tac; -qed "rev_concat"; - -(** nth **) - -section "nth"; - -Goal "(x#xs)!0 = x"; -by Auto_tac; -qed "nth_Cons_0"; -Addsimps [nth_Cons_0]; - -Goal "(x#xs)!(Suc n) = xs!n"; -by Auto_tac; -qed "nth_Cons_Suc"; -Addsimps [nth_Cons_Suc]; - -Delsimps (thms "nth.simps"); - -Goal "!n. (xs@ys)!n = (if n < length xs then xs!n else ys!(n - length xs))"; -by (induct_tac "xs" 1); - by (Asm_simp_tac 1); - by (rtac allI 1); - by (case_tac "n" 1); - by Auto_tac; -qed_spec_mp "nth_append"; - -Goal "!n. n < length xs --> (map f xs)!n = f(xs!n)"; -by (induct_tac "xs" 1); - by (Asm_full_simp_tac 1); -by (rtac allI 1); -by (induct_tac "n" 1); -by Auto_tac; -qed_spec_mp "nth_map"; -Addsimps [nth_map]; - -Goal "set xs = {xs!i |i. i < length xs}"; -by (induct_tac "xs" 1); - by (Simp_tac 1); -by (Asm_simp_tac 1); -by Safe_tac; - by (res_inst_tac [("x","0")] exI 1); - by (Simp_tac 1); - by (res_inst_tac [("x","Suc i")] exI 1); - by (Asm_simp_tac 1); -by (case_tac "i" 1); - by (Asm_full_simp_tac 1); -by (rename_tac "j" 1); - by (res_inst_tac [("x","j")] exI 1); -by (Asm_simp_tac 1); -qed "set_conv_nth"; - -Goal "n < length xs ==> Ball (set xs) P --> P(xs!n)"; -by (simp_tac (simpset() addsimps [set_conv_nth]) 1); -by (Blast_tac 1); -qed_spec_mp "list_ball_nth"; - -Goal "n < length xs ==> xs!n : set xs"; -by (simp_tac (simpset() addsimps [set_conv_nth]) 1); -by (Blast_tac 1); -qed_spec_mp "nth_mem"; -Addsimps [nth_mem]; - -Goal "(!i. i < length xs --> P(xs!i)) --> (!x : set xs. P x)"; -by (simp_tac (simpset() addsimps [set_conv_nth]) 1); -by (Blast_tac 1); -qed_spec_mp "all_nth_imp_all_set"; - -Goal "(!x : set xs. P x) = (!i. i P (xs ! i))"; -by (simp_tac (simpset() addsimps [set_conv_nth]) 1); -by (Blast_tac 1); -qed_spec_mp "all_set_conv_all_nth"; +open List; -(** list update **) - -section "list update"; - -Goal "!i. length(xs[i:=x]) = length xs"; -by (induct_tac "xs" 1); -by (Simp_tac 1); -by (asm_full_simp_tac (simpset() addsplits [nat.split]) 1); -qed_spec_mp "length_list_update"; -Addsimps [length_list_update]; - -Goal "!i j. i < length xs --> (xs[i:=x])!j = (if i=j then x else xs!j)"; -by (induct_tac "xs" 1); - by (Simp_tac 1); -by (auto_tac (claset(), simpset() addsimps [nth_Cons] addsplits [nat.split])); -qed_spec_mp "nth_list_update"; - -Goal "i < length xs ==> (xs[i:=x])!i = x"; -by (asm_simp_tac (simpset() addsimps [nth_list_update]) 1); -qed "nth_list_update_eq"; -Addsimps [nth_list_update_eq]; - -Goal "!i j. i ~= j --> xs[i:=x]!j = xs!j"; -by (induct_tac "xs" 1); - by (Simp_tac 1); -by (auto_tac (claset(), simpset() addsimps [nth_Cons] addsplits [nat.split])); -qed_spec_mp "nth_list_update_neq"; -Addsimps [nth_list_update_neq]; - -Goal "!i. i < size xs --> xs[i:=x, i:=y] = xs[i:=y]"; -by (induct_tac "xs" 1); - by (Simp_tac 1); -by (asm_simp_tac (simpset() addsplits [nat.split]) 1); -qed_spec_mp "list_update_overwrite"; -Addsimps [list_update_overwrite]; - -Goal "!i < length xs. (xs[i := x] = xs) = (xs!i = x)"; -by (induct_tac "xs" 1); - by (Simp_tac 1); -by (simp_tac (simpset() addsplits [nat.split]) 1); -by (Blast_tac 1); -qed_spec_mp "list_update_same_conv"; - -Goal "!i xy xs. length xs = length ys --> \ -\ (zip xs ys)[i:=xy] = zip (xs[i:=fst xy]) (ys[i:=snd xy])"; -by (induct_tac "ys" 1); - by Auto_tac; -by (case_tac "xs" 1); - by (auto_tac (claset(), simpset() addsplits [nat.split])); -qed_spec_mp "update_zip"; - -Goal "!i. set(xs[i:=x]) <= insert x (set xs)"; -by (induct_tac "xs" 1); - by (asm_full_simp_tac (simpset() addsimps []) 1); -by (asm_full_simp_tac (simpset() addsplits [nat.split]) 1); -by (Fast_tac 1); -qed_spec_mp "set_update_subset_insert"; - -Goal "[| set xs <= A; x:A |] ==> set(xs[i := x]) <= A"; -by (fast_tac (claset() addSDs [set_update_subset_insert RS subsetD]) 1); -qed "set_update_subsetI"; - -(** last & butlast **) - -section "last / butlast"; - -Goal "last(xs@[x]) = x"; -by (induct_tac "xs" 1); -by Auto_tac; -qed "last_snoc"; -Addsimps [last_snoc]; - -Goal "butlast(xs@[x]) = xs"; -by (induct_tac "xs" 1); -by Auto_tac; -qed "butlast_snoc"; -Addsimps [butlast_snoc]; - -Goal "length(butlast xs) = length xs - 1"; -by (rev_induct_tac "xs" 1); -by Auto_tac; -qed "length_butlast"; -Addsimps [length_butlast]; - -Goal "!ys. butlast (xs@ys) = (if ys=[] then butlast xs else xs@butlast ys)"; -by (induct_tac "xs" 1); -by Auto_tac; -qed_spec_mp "butlast_append"; - -Goal "xs ~= [] --> butlast xs @ [last xs] = xs"; -by (induct_tac "xs" 1); -by (ALLGOALS Asm_simp_tac); -qed_spec_mp "append_butlast_last_id"; -Addsimps [append_butlast_last_id]; - -Goal "x:set(butlast xs) --> x:set xs"; -by (induct_tac "xs" 1); -by Auto_tac; -qed_spec_mp "in_set_butlastD"; - -Goal "x:set(butlast xs) | x:set(butlast ys) ==> x:set(butlast(xs@ys))"; -by (auto_tac (claset() addDs [in_set_butlastD], - simpset() addsimps [butlast_append])); -qed "in_set_butlast_appendI"; - -(** take & drop **) -section "take & drop"; - -Goal "take 0 xs = []"; -by (induct_tac "xs" 1); -by Auto_tac; -qed "take_0"; - -Goal "drop 0 xs = xs"; -by (induct_tac "xs" 1); -by Auto_tac; -qed "drop_0"; - -Goal "take (Suc n) (x#xs) = x # take n xs"; -by (Simp_tac 1); -qed "take_Suc_Cons"; - -Goal "drop (Suc n) (x#xs) = drop n xs"; -by (Simp_tac 1); -qed "drop_Suc_Cons"; - -Delsimps [take_Cons,drop_Cons]; -Addsimps [take_0,take_Suc_Cons,drop_0,drop_Suc_Cons]; - -Goal "!xs. length(take n xs) = min (length xs) n"; -by (induct_tac "n" 1); - by Auto_tac; -by (case_tac "xs" 1); - by Auto_tac; -qed_spec_mp "length_take"; -Addsimps [length_take]; - -Goal "!xs. length(drop n xs) = (length xs - n)"; -by (induct_tac "n" 1); - by Auto_tac; -by (case_tac "xs" 1); - by Auto_tac; -qed_spec_mp "length_drop"; -Addsimps [length_drop]; - -Goal "!xs. length xs <= n --> take n xs = xs"; -by (induct_tac "n" 1); - by Auto_tac; -by (case_tac "xs" 1); - by Auto_tac; -qed_spec_mp "take_all"; -Addsimps [take_all]; - -Goal "!xs. length xs <= n --> drop n xs = []"; -by (induct_tac "n" 1); - by Auto_tac; -by (case_tac "xs" 1); - by Auto_tac; -qed_spec_mp "drop_all"; -Addsimps [drop_all]; - -Goal "!xs. take n (xs @ ys) = (take n xs @ take (n - length xs) ys)"; -by (induct_tac "n" 1); - by Auto_tac; -by (case_tac "xs" 1); - by Auto_tac; -qed_spec_mp "take_append"; -Addsimps [take_append]; - -Goal "!xs. drop n (xs@ys) = drop n xs @ drop (n - length xs) ys"; -by (induct_tac "n" 1); - by Auto_tac; -by (case_tac "xs" 1); - by Auto_tac; -qed_spec_mp "drop_append"; -Addsimps [drop_append]; - -Goal "!xs n. take n (take m xs) = take (min n m) xs"; -by (induct_tac "m" 1); - by Auto_tac; -by (case_tac "xs" 1); - by Auto_tac; -by (case_tac "na" 1); - by Auto_tac; -qed_spec_mp "take_take"; -Addsimps [take_take]; - -Goal "!xs. drop n (drop m xs) = drop (n + m) xs"; -by (induct_tac "m" 1); - by Auto_tac; -by (case_tac "xs" 1); - by Auto_tac; -qed_spec_mp "drop_drop"; -Addsimps [drop_drop]; - -Goal "!xs n. take n (drop m xs) = drop m (take (n + m) xs)"; -by (induct_tac "m" 1); - by Auto_tac; -by (case_tac "xs" 1); - by Auto_tac; -qed_spec_mp "take_drop"; - -Goal "!xs. take n xs @ drop n xs = xs"; -by (induct_tac "n" 1); - by Auto_tac; -by (case_tac "xs" 1); - by Auto_tac; -qed_spec_mp "append_take_drop_id"; -Addsimps [append_take_drop_id]; - -Goal "!xs. take n (map f xs) = map f (take n xs)"; -by (induct_tac "n" 1); - by Auto_tac; -by (case_tac "xs" 1); - by Auto_tac; -qed_spec_mp "take_map"; - -Goal "!xs. drop n (map f xs) = map f (drop n xs)"; -by (induct_tac "n" 1); - by Auto_tac; -by (case_tac "xs" 1); - by Auto_tac; -qed_spec_mp "drop_map"; - -Goal "!i. rev (take i xs) = drop (length xs - i) (rev xs)"; -by(induct_tac "xs" 1); - by Auto_tac; -by (case_tac "i" 1); - by Auto_tac; -qed_spec_mp "rev_take"; - -Goal "!i. rev (drop i xs) = take (length xs - i) (rev xs)"; -by(induct_tac "xs" 1); - by Auto_tac; -by (case_tac "i" 1); - by Auto_tac; -qed_spec_mp "rev_drop"; - -Goal "!n i. i < n --> (take n xs)!i = xs!i"; -by (induct_tac "xs" 1); - by Auto_tac; -by (case_tac "n" 1); - by (Blast_tac 1); -by (case_tac "i" 1); - by Auto_tac; -qed_spec_mp "nth_take"; -Addsimps [nth_take]; - -Goal "!xs i. n + i <= length xs --> (drop n xs)!i = xs!(n+i)"; -by (induct_tac "n" 1); - by Auto_tac; -by (case_tac "xs" 1); - by Auto_tac; -qed_spec_mp "nth_drop"; -Addsimps [nth_drop]; - - -Goal - "!zs. (xs@ys = zs) = (xs = take (length xs) zs & ys = drop (length xs) zs)"; -by (induct_tac "xs" 1); - by (Simp_tac 1); -by (Asm_full_simp_tac 1); -by (Clarify_tac 1); -by (case_tac "zs" 1); -by (Auto_tac); -qed_spec_mp "append_eq_conv_conj"; - -(** takeWhile & dropWhile **) - -section "takeWhile & dropWhile"; - -Goal "takeWhile P xs @ dropWhile P xs = xs"; -by (induct_tac "xs" 1); -by Auto_tac; -qed "takeWhile_dropWhile_id"; -Addsimps [takeWhile_dropWhile_id]; - -Goal "x:set xs & ~P(x) --> takeWhile P (xs @ ys) = takeWhile P xs"; -by (induct_tac "xs" 1); -by Auto_tac; -bind_thm("takeWhile_append1", conjI RS (result() RS mp)); -Addsimps [takeWhile_append1]; - -Goal "(!x:set xs. P(x)) --> takeWhile P (xs @ ys) = xs @ takeWhile P ys"; -by (induct_tac "xs" 1); -by Auto_tac; -bind_thm("takeWhile_append2", ballI RS (result() RS mp)); -Addsimps [takeWhile_append2]; - -Goal "~P(x) ==> takeWhile P (xs @ (x#l)) = takeWhile P xs"; -by (induct_tac "xs" 1); -by Auto_tac; -qed "takeWhile_tail"; - -Goal "x:set xs & ~P(x) --> dropWhile P (xs @ ys) = (dropWhile P xs)@ys"; -by (induct_tac "xs" 1); -by Auto_tac; -bind_thm("dropWhile_append1", conjI RS (result() RS mp)); -Addsimps [dropWhile_append1]; - -Goal "(!x:set xs. P(x)) --> dropWhile P (xs @ ys) = dropWhile P ys"; -by (induct_tac "xs" 1); -by Auto_tac; -bind_thm("dropWhile_append2", ballI RS (result() RS mp)); -Addsimps [dropWhile_append2]; - -Goal "x:set(takeWhile P xs) --> x:set xs & P x"; -by (induct_tac "xs" 1); -by Auto_tac; -qed_spec_mp"set_take_whileD"; - -(** zip **) -section "zip"; - -Goal "zip [] ys = []"; -by (induct_tac "ys" 1); -by Auto_tac; -qed "zip_Nil"; -Addsimps [zip_Nil]; - -Goal "zip (x#xs) (y#ys) = (x,y)#zip xs ys"; -by (Simp_tac 1); -qed "zip_Cons_Cons"; -Addsimps [zip_Cons_Cons]; - -Delsimps(tl (thms"zip.simps")); - -Goal "!xs. length (zip xs ys) = min (length xs) (length ys)"; -by (induct_tac "ys" 1); - by (Simp_tac 1); -by (Clarify_tac 1); -by (case_tac "xs" 1); - by (Auto_tac); -qed_spec_mp "length_zip"; -Addsimps [length_zip]; - -Goal - "!xs. zip (xs@ys) zs = \ -\ zip xs (take (length xs) zs) @ zip ys (drop (length xs) zs)"; -by (induct_tac "zs" 1); - by (Simp_tac 1); -by (Clarify_tac 1); -by (case_tac "xs" 1); - by (Asm_simp_tac 1); -by (Asm_simp_tac 1); -qed_spec_mp "zip_append1"; - -Goal - "!ys. zip xs (ys@zs) = \ -\ zip (take (length ys) xs) ys @ zip (drop (length ys) xs) zs"; -by (induct_tac "xs" 1); - by (Simp_tac 1); -by (Clarify_tac 1); -by (case_tac "ys" 1); - by (Asm_simp_tac 1); -by (Asm_simp_tac 1); -qed_spec_mp "zip_append2"; - -Goal - "[| length xs = length us; length ys = length vs |] ==> \ -\ zip (xs@ys) (us@vs) = zip xs us @ zip ys vs"; -by (asm_simp_tac (simpset() addsimps [zip_append1]) 1); -qed_spec_mp "zip_append"; -Addsimps [zip_append]; - -Goal "!xs. length xs = length ys --> zip (rev xs) (rev ys) = rev (zip xs ys)"; -by (induct_tac "ys" 1); - by (Asm_full_simp_tac 1); -by (Asm_full_simp_tac 1); -by (Clarify_tac 1); -by (case_tac "xs" 1); - by (Auto_tac); -qed_spec_mp "zip_rev"; - - -Goal -"!i xs. i < length xs --> i < length ys --> (zip xs ys)!i = (xs!i, ys!i)"; -by (induct_tac "ys" 1); - by (Simp_tac 1); -by (Clarify_tac 1); -by (case_tac "xs" 1); - by (Auto_tac); -by (asm_full_simp_tac (simpset() addsimps (thms"nth.simps") addsplits [nat.split]) 1); -qed_spec_mp "nth_zip"; -Addsimps [nth_zip]; - -Goal "set(zip xs ys) = {(xs!i,ys!i) |i. i < min (length xs) (length ys)}"; -by (simp_tac (simpset() addsimps [set_conv_nth]addcongs [rev_conj_cong]) 1); -qed_spec_mp "set_zip"; - -Goal - "length xs = length ys ==> zip (xs[i:=x]) (ys[i:=y]) = (zip xs ys)[i:=(x,y)]"; -by (rtac sym 1); -by (asm_simp_tac (simpset() addsimps [update_zip]) 1); -qed_spec_mp "zip_update"; - -Goal "!j. zip (replicate i x) (replicate j y) = replicate (min i j) (x,y)"; -by (induct_tac "i" 1); - by (Auto_tac); -by (case_tac "j" 1); - by (Auto_tac); -qed "zip_replicate"; -Addsimps [zip_replicate]; - -(** list_all2 **) -section "list_all2"; - -Goalw [list_all2_def] "list_all2 P xs ys ==> length xs = length ys"; -by (Asm_simp_tac 1); -qed "list_all2_lengthD"; - -Goalw [list_all2_def] "list_all2 P [] ys = (ys=[])"; -by (Simp_tac 1); -qed "list_all2_Nil"; -AddIffs [list_all2_Nil]; - -Goalw [list_all2_def] "list_all2 P xs [] = (xs=[])"; -by (Simp_tac 1); -qed "list_all2_Nil2"; -AddIffs [list_all2_Nil2]; - -Goalw [list_all2_def] - "list_all2 P (x#xs) (y#ys) = (P x y & list_all2 P xs ys)"; -by (Auto_tac); -qed "list_all2_Cons"; -AddIffs[list_all2_Cons]; - -Goalw [list_all2_def] - "list_all2 P (x#xs) ys = (? z zs. ys = z#zs & P x z & list_all2 P xs zs)"; -by (case_tac "ys" 1); -by (Auto_tac); -qed "list_all2_Cons1"; - -Goalw [list_all2_def] - "list_all2 P xs (y#ys) = (? z zs. xs = z#zs & P z y & list_all2 P zs ys)"; -by (case_tac "xs" 1); -by (Auto_tac); -qed "list_all2_Cons2"; - -Goalw [list_all2_def] - "list_all2 P (rev xs) (rev ys) = list_all2 P xs ys"; -by (asm_full_simp_tac (simpset() addsimps [zip_rev] addcongs [conj_cong]) 1); -qed "list_all2_rev"; -AddIffs[list_all2_rev]; - -Goalw [list_all2_def] - "list_all2 P (xs@ys) zs = \ -\ (EX us vs. zs = us@vs & length us = length xs & length vs = length ys & \ -\ list_all2 P xs us & list_all2 P ys vs)"; -by (simp_tac (simpset() addsimps [zip_append1]) 1); -by (rtac iffI 1); - by (res_inst_tac [("x","take (length xs) zs")] exI 1); - by (res_inst_tac [("x","drop (length xs) zs")] exI 1); - by (force_tac (claset(), - simpset() addsplits [nat_diff_split] addsimps [min_def]) 1); -by (Clarify_tac 1); -by (asm_full_simp_tac (simpset() addsimps [ball_Un]) 1); -qed "list_all2_append1"; - -Goalw [list_all2_def] - "list_all2 P xs (ys@zs) = \ -\ (EX us vs. xs = us@vs & length us = length ys & length vs = length zs & \ -\ list_all2 P us ys & list_all2 P vs zs)"; -by (simp_tac (simpset() addsimps [zip_append2]) 1); -by (rtac iffI 1); - by (res_inst_tac [("x","take (length ys) xs")] exI 1); - by (res_inst_tac [("x","drop (length ys) xs")] exI 1); - by (force_tac (claset(), - simpset() addsplits [nat_diff_split] addsimps [min_def]) 1); -by (Clarify_tac 1); -by (asm_full_simp_tac (simpset() addsimps [ball_Un]) 1); -qed "list_all2_append2"; - -Goalw [list_all2_def] - "list_all2 P xs ys = \ -\ (length xs = length ys & (!i P2 b c --> P3 a c ==> \ -\ ALL bs cs. list_all2 P1 as bs --> list_all2 P2 bs cs --> list_all2 P3 as cs"; -by (induct_tac "as" 1); -by (Simp_tac 1); -by (rtac allI 1); -by (induct_tac "bs" 1); -by (Simp_tac 1); -by (rtac allI 1); -by (induct_tac "cs" 1); -by Auto_tac; -qed_spec_mp "list_all2_trans"; - - -section "foldl"; - -Goal "!a. foldl f a (xs @ ys) = foldl f (foldl f a xs) ys"; -by (induct_tac "xs" 1); -by Auto_tac; -qed_spec_mp "foldl_append"; -Addsimps [foldl_append]; - -(* Note: `n <= foldl op+ n ns' looks simpler, but is more difficult to use - because it requires an additional transitivity step -*) -Goal "!n::nat. m <= n --> m <= foldl op+ n ns"; -by (induct_tac "ns" 1); -by Auto_tac; -qed_spec_mp "start_le_sum"; - -Goal "!!n::nat. n : set ns ==> n <= foldl op+ 0 ns"; -by (force_tac (claset() addIs [start_le_sum], - simpset() addsimps [in_set_conv_decomp]) 1); -qed "elem_le_sum"; - -Goal "!m::nat. (foldl op+ m ns = 0) = (m=0 & (!n : set ns. n=0))"; -by (induct_tac "ns" 1); -by Auto_tac; -qed_spec_mp "sum_eq_0_conv"; -AddIffs [sum_eq_0_conv]; - -(** upto **) - -(* Does not terminate! *) -Goal "[i..j(] = (if i [i..j(] = []"; -by (stac upt_rec 1); -by (Asm_simp_tac 1); -qed "upt_conv_Nil"; -Addsimps [upt_conv_Nil]; - -(*Only needed if upt_Suc is deleted from the simpset*) -Goal "i<=j ==> [i..(Suc j)(] = [i..j(]@[j]"; -by (Asm_simp_tac 1); -qed "upt_Suc_append"; - -Goal "i [i..j(] = i#[Suc i..j(]"; -by (rtac trans 1); -by (stac upt_rec 1); -by (rtac refl 2); -by (Asm_simp_tac 1); -qed "upt_conv_Cons"; - -(*LOOPS as a simprule, since j<=j*) -Goal "i<=j ==> [i..j+k(] = [i..j(]@[j..j+k(]"; -by (induct_tac "k" 1); -by Auto_tac; -qed "upt_add_eq_append"; - -Goal "length [i..j(] = j-i"; -by (induct_tac "j" 1); - by (Simp_tac 1); -by (asm_simp_tac (simpset() addsimps [Suc_diff_le]) 1); -qed "length_upt"; -Addsimps [length_upt]; - -Goal "i+k < j --> [i..j(] ! k = i+k"; -by (induct_tac "j" 1); - by (asm_simp_tac (simpset() addsimps [less_Suc_eq, nth_append] - addsplits [nat_diff_split]) 2); -by (Simp_tac 1); -qed_spec_mp "nth_upt"; -Addsimps [nth_upt]; - -Goal "!i. i+m <= n --> take m [i..n(] = [i..i+m(]"; -by (induct_tac "m" 1); - by (Simp_tac 1); -by (Clarify_tac 1); -by (stac upt_rec 1); -by (rtac sym 1); -by (stac upt_rec 1); -by (asm_simp_tac (simpset() delsimps (thms"upt.simps")) 1); -qed_spec_mp "take_upt"; -Addsimps [take_upt]; - -Goal "map Suc [m..n(] = [Suc m..n]"; -by (induct_tac "n" 1); -by Auto_tac; -qed "map_Suc_upt"; - -Goal "ALL i. i < n-m --> (map f [m..n(]) ! i = f(m+i)"; -by (induct_thm_tac diff_induct "n m" 1); -by (stac (map_Suc_upt RS sym) 3); -by (auto_tac (claset(), simpset() addsimps [less_diff_conv, nth_upt])); -qed_spec_mp "nth_map_upt"; - -Goal "ALL xs ys. k <= length xs --> k <= length ys --> \ -\ (ALL i. i < k --> xs!i = ys!i) \ -\ --> take k xs = take k ys"; -by (induct_tac "k" 1); -by (ALLGOALS (asm_simp_tac (simpset() addsimps [less_Suc_eq_0_disj, - all_conj_distrib]))); -by (Clarify_tac 1); -(*Both lists must be non-empty*) -by (case_tac "xs" 1); -by (case_tac "ys" 2); -by (ALLGOALS Clarify_tac); -(*prenexing's needed, not miniscoping*) -by (ALLGOALS (full_simp_tac (simpset() addsimps (all_simps RL [sym]) - delsimps (all_simps)))); -by (Blast_tac 1); -qed_spec_mp "nth_take_lemma"; - -Goal "[| length xs = length ys; \ -\ ALL i. i < length xs --> xs!i = ys!i |] \ -\ ==> xs = ys"; -by (forward_tac [[le_refl, eq_imp_le] MRS nth_take_lemma] 1); -by (ALLGOALS (asm_full_simp_tac (simpset() addsimps [take_all]))); -qed_spec_mp "nth_equalityI"; - -(*The famous take-lemma*) -Goal "(ALL i. take i xs = take i ys) ==> xs = ys"; -by (dres_inst_tac [("x", "max (length xs) (length ys)")] spec 1); -by (full_simp_tac (simpset() addsimps [le_max_iff_disj, take_all]) 1); -qed_spec_mp "take_equalityI"; - - -(** distinct & remdups **) -section "distinct & remdups"; - -Goal "distinct(xs@ys) = (distinct xs & distinct ys & set xs Int set ys = {})"; -by(induct_tac "xs" 1); -by Auto_tac; -qed "distinct_append"; -Addsimps [distinct_append]; - -Goal "set(remdups xs) = set xs"; -by (induct_tac "xs" 1); - by (Simp_tac 1); -by (asm_full_simp_tac (simpset() addsimps [insert_absorb]) 1); -qed "set_remdups"; -Addsimps [set_remdups]; - -Goal "distinct(remdups xs)"; -by (induct_tac "xs" 1); -by Auto_tac; -qed "distinct_remdups"; - -Goal "distinct xs --> distinct (filter P xs)"; -by (induct_tac "xs" 1); -by Auto_tac; -qed_spec_mp "distinct_filter"; - -(** replicate **) -section "replicate"; - -Goal "length(replicate n x) = n"; -by (induct_tac "n" 1); -by Auto_tac; -qed "length_replicate"; -Addsimps [length_replicate]; - -Goal "map f (replicate n x) = replicate n (f x)"; -by (induct_tac "n" 1); -by Auto_tac; -qed "map_replicate"; -Addsimps [map_replicate]; - -Goal "(replicate n x) @ (x#xs) = x # replicate n x @ xs"; -by (induct_tac "n" 1); -by Auto_tac; -qed "replicate_app_Cons_same"; - -Goal "rev(replicate n x) = replicate n x"; -by (induct_tac "n" 1); - by (Simp_tac 1); -by (asm_simp_tac (simpset() addsimps [replicate_app_Cons_same]) 1); -qed "rev_replicate"; -Addsimps [rev_replicate]; - -Goal "replicate (n+m) x = replicate n x @ replicate m x"; -by (induct_tac "n" 1); -by Auto_tac; -qed "replicate_add"; - -Goal"n ~= 0 --> hd(replicate n x) = x"; -by (induct_tac "n" 1); -by Auto_tac; -qed_spec_mp "hd_replicate"; -Addsimps [hd_replicate]; - -Goal "n ~= 0 --> tl(replicate n x) = replicate (n - 1) x"; -by (induct_tac "n" 1); -by Auto_tac; -qed_spec_mp "tl_replicate"; -Addsimps [tl_replicate]; - -Goal "n ~= 0 --> last(replicate n x) = x"; -by (induct_tac "n" 1); -by Auto_tac; -qed_spec_mp "last_replicate"; -Addsimps [last_replicate]; - -Goal "!i. i (replicate n x)!i = x"; -by (induct_tac "n" 1); - by (Simp_tac 1); -by (asm_simp_tac (simpset() addsimps [nth_Cons] addsplits [nat.split]) 1); -qed_spec_mp "nth_replicate"; -Addsimps [nth_replicate]; - -Goal "set(replicate (Suc n) x) = {x}"; -by (induct_tac "n" 1); -by Auto_tac; -val lemma = result(); - -Goal "n ~= 0 ==> set(replicate n x) = {x}"; -by (fast_tac (claset() addSDs [not0_implies_Suc] addSIs [lemma]) 1); -qed "set_replicate"; -Addsimps [set_replicate]; - -Goal "set(replicate n x) = (if n=0 then {} else {x})"; -by (Auto_tac); -qed "set_replicate_conv_if"; - -Goal "x : set(replicate n y) --> x=y"; -by (asm_simp_tac (simpset() addsimps [set_replicate_conv_if]) 1); -qed_spec_mp "in_set_replicateD"; - - -(*** Lexcicographic orderings on lists ***) -section"Lexcicographic orderings on lists"; - -Goal "wf r ==> wf(lexn r n)"; -by (induct_tac "n" 1); -by (Simp_tac 1); -by (Simp_tac 1); -by (rtac wf_subset 1); -by (rtac Int_lower1 2); -by (rtac wf_prod_fun_image 1); -by (rtac injI 2); -by Auto_tac; -qed "wf_lexn"; - -Goal "!xs ys. (xs,ys) : lexn r n --> length xs = n & length ys = n"; -by (induct_tac "n" 1); -by Auto_tac; -qed_spec_mp "lexn_length"; - -Goalw [lex_def] "wf r ==> wf(lex r)"; -by (rtac wf_UN 1); -by (blast_tac (claset() addIs [wf_lexn]) 1); -by (Clarify_tac 1); -by (rename_tac "m n" 1); -by (subgoal_tac "m ~= n" 1); - by (Blast_tac 2); -by (blast_tac (claset() addDs [lexn_length,not_sym]) 1); -qed "wf_lex"; -AddSIs [wf_lex]; - - -Goal - "lexn r n = \ -\ {(xs,ys). length xs = n & length ys = n & \ -\ (? xys x y xs' ys'. xs= xys @ x#xs' & ys= xys @ y#ys' & (x,y):r)}"; -by (induct_tac "n" 1); - by (Simp_tac 1); - by (Blast_tac 1); -by (asm_full_simp_tac (simpset() addsimps [image_Collect, lex_prod_def]) 1); -by Auto_tac; - by (Blast_tac 1); - by (rename_tac "a xys x xs' y ys'" 1); - by (res_inst_tac [("x","a#xys")] exI 1); - by (Simp_tac 1); -by (case_tac "xys" 1); - by (ALLGOALS Asm_full_simp_tac); -by (Blast_tac 1); -qed "lexn_conv"; - -Goalw [lex_def] - "lex r = \ -\ {(xs,ys). length xs = length ys & \ -\ (? xys x y xs' ys'. xs= xys @ x#xs' & ys= xys @ y#ys' & (x,y):r)}"; -by (force_tac (claset(), simpset() addsimps [lexn_conv]) 1); -qed "lex_conv"; - -Goalw [lexico_def] "wf r ==> wf(lexico r)"; -by (Blast_tac 1); -qed "wf_lexico"; -AddSIs [wf_lexico]; - -Goalw [lexico_def,diag_def,lex_prod_def,measure_def,inv_image_def] -"lexico r = {(xs,ys). length xs < length ys | \ -\ length xs = length ys & (xs,ys) : lex r}"; -by (Simp_tac 1); -qed "lexico_conv"; - -Goal "([],ys) ~: lex r"; -by (simp_tac (simpset() addsimps [lex_conv]) 1); -qed "Nil_notin_lex"; - -Goal "(xs,[]) ~: lex r"; -by (simp_tac (simpset() addsimps [lex_conv]) 1); -qed "Nil2_notin_lex"; - -AddIffs [Nil_notin_lex,Nil2_notin_lex]; - -Goal "((x#xs,y#ys) : lex r) = \ -\ ((x,y) : r & length xs = length ys | x=y & (xs,ys) : lex r)"; -by (simp_tac (simpset() addsimps [lex_conv]) 1); -by (rtac iffI 1); - by (blast_tac (claset() addIs [Cons_eq_appendI]) 2); -by (Clarify_tac 1); -by (case_tac "xys" 1); -by (Asm_full_simp_tac 1); -by (Asm_full_simp_tac 1); -by (Blast_tac 1); -qed "Cons_in_lex"; -AddIffs [Cons_in_lex]; - - -(*** sublist (a generalization of nth to sets) ***) - -Goalw [sublist_def] "sublist l {} = []"; -by Auto_tac; -qed "sublist_empty"; - -Goalw [sublist_def] "sublist [] A = []"; -by Auto_tac; -qed "sublist_nil"; - -Goal "map fst [p:zip xs [i..i + length xs(] . snd p : A] = \ -\ map fst [p:zip xs [0..length xs(] . snd p + i : A]"; -by (rev_induct_tac "xs" 1); - by (asm_simp_tac (simpset() addsimps [add_commute]) 2); -by (Simp_tac 1); -qed "sublist_shift_lemma"; - -Goalw [sublist_def] - "sublist (l@l') A = sublist l A @ sublist l' {j. j + length l : A}"; -by (rev_induct_tac "l'" 1); -by (Simp_tac 1); -by (asm_simp_tac (simpset() addsimps [inst "i" "0" upt_add_eq_append, - zip_append, sublist_shift_lemma]) 1); -by (asm_simp_tac (simpset() addsimps [add_commute]) 1); -qed "sublist_append"; - -Addsimps [sublist_empty, sublist_nil]; - -Goal "sublist (x#l) A = (if 0:A then [x] else []) @ sublist l {j. Suc j : A}"; -by (rev_induct_tac "l" 1); - by (asm_simp_tac (simpset() delsimps [append_Cons] - addsimps [append_Cons RS sym, sublist_append]) 2); -by (simp_tac (simpset() addsimps [sublist_def]) 1); -qed "sublist_Cons"; - -Goal "sublist [x] A = (if 0 : A then [x] else [])"; -by (simp_tac (simpset() addsimps [sublist_Cons]) 1); -qed "sublist_singleton"; -Addsimps [sublist_singleton]; - -Goal "sublist l {..n(} = take n l"; -by (rev_induct_tac "l" 1); - by (asm_simp_tac (simpset() addsplits [nat_diff_split] - addsimps [sublist_append]) 2); -by (Simp_tac 1); -qed "sublist_upt_eq_take"; -Addsimps [sublist_upt_eq_take]; - - -Goal "take n (x#xs) = (if n=0 then [] else x # take (n - 1) xs)"; -by (case_tac "n" 1); -by (ALLGOALS - (asm_simp_tac (simpset() addsimps [numeral_0_eq_0, numeral_1_eq_1]))); -qed "take_Cons'"; - -Goal "drop n (x#xs) = (if n=0 then x#xs else drop (n - 1) xs)"; -by (case_tac "n" 1); -by (ALLGOALS - (asm_simp_tac (simpset() addsimps [numeral_0_eq_0, numeral_1_eq_1]))); -qed "drop_Cons'"; - -Goal "(x#xs)!n = (if n=0 then x else xs!(n - 1))"; -by (case_tac "n" 1); -by (ALLGOALS - (asm_simp_tac (simpset() addsimps [numeral_0_eq_0, numeral_1_eq_1]))); -qed "nth_Cons'"; - -Addsimps (map (inst "n" "number_of ?v") [take_Cons', drop_Cons', nth_Cons']); - +val Cons_eq_appendI = thm "Cons_eq_appendI"; +val Cons_in_lex = thm "Cons_in_lex"; +val Nil2_notin_lex = thm "Nil2_notin_lex"; +val Nil_eq_concat_conv = thm "Nil_eq_concat_conv"; +val Nil_is_append_conv = thm "Nil_is_append_conv"; +val Nil_is_map_conv = thm "Nil_is_map_conv"; +val Nil_is_rev_conv = thm "Nil_is_rev_conv"; +val Nil_notin_lex = thm "Nil_notin_lex"; +val all_nth_imp_all_set = thm "all_nth_imp_all_set"; +val all_set_conv_all_nth = thm "all_set_conv_all_nth"; +val append1_eq_conv = thm "append1_eq_conv"; +val append_Cons = thm "append_Cons"; +val append_Nil = thm "append_Nil"; +val append_Nil2 = thm "append_Nil2"; +val append_assoc = thm "append_assoc"; +val append_butlast_last_id = thm "append_butlast_last_id"; +val append_eq_appendI = thm "append_eq_appendI"; +val append_eq_append_conv = thm "append_eq_append_conv"; +val append_eq_conv_conj = thm "append_eq_conv_conj"; +val append_in_lists_conv = thm "append_in_lists_conv"; +val append_is_Nil_conv = thm "append_is_Nil_conv"; +val append_same_eq = thm "append_same_eq"; +val append_self_conv = thm "append_self_conv"; +val append_self_conv2 = thm "append_self_conv2"; +val append_take_drop_id = thm "append_take_drop_id"; +val butlast_append = thm "butlast_append"; +val butlast_snoc = thm "butlast_snoc"; +val concat_append = thm "concat_append"; +val concat_eq_Nil_conv = thm "concat_eq_Nil_conv"; +val distinct_append = thm "distinct_append"; +val distinct_filter = thm "distinct_filter"; +val distinct_remdups = thm "distinct_remdups"; +val dropWhile_append1 = thm "dropWhile_append1"; +val dropWhile_append2 = thm "dropWhile_append2"; +val drop_0 = thm "drop_0"; +val drop_Cons = thm "drop_Cons"; +val drop_Cons' = thm "drop_Cons'"; +val drop_Nil = thm "drop_Nil"; +val drop_Suc_Cons = thm "drop_Suc_Cons"; +val drop_all = thm "drop_all"; +val drop_append = thm "drop_append"; +val drop_drop = thm "drop_drop"; +val drop_map = thm "drop_map"; +val elem_le_sum = thm "elem_le_sum"; +val eq_Nil_appendI = thm "eq_Nil_appendI"; +val filter_False = thm "filter_False"; +val filter_True = thm "filter_True"; +val filter_append = thm "filter_append"; +val filter_concat = thm "filter_concat"; +val filter_filter = thm "filter_filter"; +val filter_is_subset = thm "filter_is_subset"; +val finite_set = thm "finite_set"; +val foldl_Cons = thm "foldl_Cons"; +val foldl_Nil = thm "foldl_Nil"; +val foldl_append = thm "foldl_append"; +val hd_Cons_tl = thm "hd_Cons_tl"; +val hd_append = thm "hd_append"; +val hd_append2 = thm "hd_append2"; +val hd_replicate = thm "hd_replicate"; +val in_listsD = thm "in_listsD"; +val in_listsI = thm "in_listsI"; +val in_lists_conv_set = thm "in_lists_conv_set"; +val in_set_butlastD = thm "in_set_butlastD"; +val in_set_butlast_appendI = thm "in_set_butlast_appendI"; +val in_set_conv_decomp = thm "in_set_conv_decomp"; +val in_set_replicateD = thm "in_set_replicateD"; +val inj_map = thm "inj_map"; +val inj_mapD = thm "inj_mapD"; +val inj_mapI = thm "inj_mapI"; +val last_replicate = thm "last_replicate"; +val last_snoc = thm "last_snoc"; +val length_0_conv = thm "length_0_conv"; +val length_Suc_conv = thm "length_Suc_conv"; +val length_append = thm "length_append"; +val length_butlast = thm "length_butlast"; +val length_drop = thm "length_drop"; +val length_filter = thm "length_filter"; +val length_greater_0_conv = thm "length_greater_0_conv"; +val length_induct = thm "length_induct"; +val length_list_update = thm "length_list_update"; +val length_map = thm "length_map"; +val length_replicate = thm "length_replicate"; +val length_rev = thm "length_rev"; +val length_take = thm "length_take"; +val length_tl = thm "length_tl"; +val length_upt = thm "length_upt"; +val length_zip = thm "length_zip"; +val lex_conv = thm "lex_conv"; +val lex_def = thm "lex_def"; +val lexico_conv = thm "lexico_conv"; +val lexico_def = thm "lexico_def"; +val lexn_conv = thm "lexn_conv"; +val lexn_length = thm "lexn_length"; +val list_all2_Cons = thm "list_all2_Cons"; +val list_all2_Cons1 = thm "list_all2_Cons1"; +val list_all2_Cons2 = thm "list_all2_Cons2"; +val list_all2_Nil = thm "list_all2_Nil"; +val list_all2_Nil2 = thm "list_all2_Nil2"; +val list_all2_append1 = thm "list_all2_append1"; +val list_all2_append2 = thm "list_all2_append2"; +val list_all2_conv_all_nth = thm "list_all2_conv_all_nth"; +val list_all2_def = thm "list_all2_def"; +val list_all2_lengthD = thm "list_all2_lengthD"; +val list_all2_rev = thm "list_all2_rev"; +val list_all2_trans = thm "list_all2_trans"; +val list_all_Cons = thm "list_all_Cons"; +val list_all_Nil = thm "list_all_Nil"; +val list_all_append = thm "list_all_append"; +val list_all_conv = thm "list_all_conv"; +val list_ball_nth = thm "list_ball_nth"; +val list_update_overwrite = thm "list_update_overwrite"; +val list_update_same_conv = thm "list_update_same_conv"; +val listsE = thm "listsE"; +val lists_IntI = thm "lists_IntI"; +val lists_Int_eq = thm "lists_Int_eq"; +val lists_mono = thm "lists_mono"; +val map_Suc_upt = thm "map_Suc_upt"; +val map_append = thm "map_append"; +val map_compose = thm "map_compose"; +val map_concat = thm "map_concat"; +val map_cong = thm "map_cong"; +val map_eq_Cons = thm "map_eq_Cons"; +val map_ext = thm "map_ext"; +val map_ident = thm "map_ident"; +val map_injective = thm "map_injective"; +val map_is_Nil_conv = thm "map_is_Nil_conv"; +val map_replicate = thm "map_replicate"; +val neq_Nil_conv = thm "neq_Nil_conv"; +val not_Cons_self = thm "not_Cons_self"; +val not_Cons_self2 = thm "not_Cons_self2"; +val nth_Cons = thm "nth_Cons"; +val nth_Cons' = thm "nth_Cons'"; +val nth_Cons_0 = thm "nth_Cons_0"; +val nth_Cons_Suc = thm "nth_Cons_Suc"; +val nth_append = thm "nth_append"; +val nth_drop = thm "nth_drop"; +val nth_equalityI = thm "nth_equalityI"; +val nth_list_update = thm "nth_list_update"; +val nth_list_update_eq = thm "nth_list_update_eq"; +val nth_list_update_neq = thm "nth_list_update_neq"; +val nth_map = thm "nth_map"; +val nth_map_upt = thm "nth_map_upt"; +val nth_mem = thm "nth_mem"; +val nth_replicate = thm "nth_replicate"; +val nth_take = thm "nth_take"; +val nth_take_lemma = thm "nth_take_lemma"; +val nth_upt = thm "nth_upt"; +val nth_zip = thm "nth_zip"; +val replicate_0 = thm "replicate_0"; +val replicate_Suc = thm "replicate_Suc"; +val replicate_add = thm "replicate_add"; +val replicate_app_Cons_same = thm "replicate_app_Cons_same"; +val rev_append = thm "rev_append"; +val rev_concat = thm "rev_concat"; +val rev_drop = thm "rev_drop"; +val rev_exhaust = thm "rev_exhaust"; +val rev_exhaust_aux = thm "rev_exhaust_aux"; +val rev_induct = thm "rev_induct"; +val rev_is_Nil_conv = thm "rev_is_Nil_conv"; +val rev_is_rev_conv = thm "rev_is_rev_conv"; +val rev_map = thm "rev_map"; +val rev_replicate = thm "rev_replicate"; +val rev_rev_ident = thm "rev_rev_ident"; +val rev_take = thm "rev_take"; +val same_append_eq = thm "same_append_eq"; +val self_append_conv = thm "self_append_conv"; +val self_append_conv2 = thm "self_append_conv2"; +val set_append = thm "set_append"; +val set_concat = thm "set_concat"; +val set_conv_nth = thm "set_conv_nth"; +val set_empty = thm "set_empty"; +val set_filter = thm "set_filter"; +val set_map = thm "set_map"; +val set_mem_eq = thm "set_mem_eq"; +val set_remdups = thm "set_remdups"; +val set_replicate = thm "set_replicate"; +val set_replicate_conv_if = thm "set_replicate_conv_if"; +val set_rev = thm "set_rev"; +val set_subset_Cons = thm "set_subset_Cons"; +val set_take_whileD = thm "set_take_whileD"; +val set_update_subsetI = thm "set_update_subsetI"; +val set_update_subset_insert = thm "set_update_subset_insert"; +val set_upt = thm "set_upt"; +val set_zip = thm "set_zip"; +val start_le_sum = thm "start_le_sum"; +val sublist_Cons = thm "sublist_Cons"; +val sublist_append = thm "sublist_append"; +val sublist_def = thm "sublist_def"; +val sublist_empty = thm "sublist_empty"; +val sublist_nil = thm "sublist_nil"; +val sublist_shift_lemma = thm "sublist_shift_lemma"; +val sublist_singleton = thm "sublist_singleton"; +val sublist_upt_eq_take = thm "sublist_upt_eq_take"; +val sum_eq_0_conv = thm "sum_eq_0_conv"; +val takeWhile_append1 = thm "takeWhile_append1"; +val takeWhile_append2 = thm "takeWhile_append2"; +val takeWhile_dropWhile_id = thm "takeWhile_dropWhile_id"; +val takeWhile_tail = thm "takeWhile_tail"; +val take_0 = thm "take_0"; +val take_Cons = thm "take_Cons"; +val take_Cons' = thm "take_Cons'"; +val take_Nil = thm "take_Nil"; +val take_Suc_Cons = thm "take_Suc_Cons"; +val take_all = thm "take_all"; +val take_append = thm "take_append"; +val take_drop = thm "take_drop"; +val take_equalityI = thm "take_equalityI"; +val take_map = thm "take_map"; +val take_take = thm "take_take"; +val take_upt = thm "take_upt"; +val tl_append = thm "tl_append"; +val tl_append2 = thm "tl_append2"; +val tl_replicate = thm "tl_replicate"; +val update_zip = thm "update_zip"; +val upt_0 = thm "upt_0"; +val upt_Suc = thm "upt_Suc"; +val upt_Suc_append = thm "upt_Suc_append"; +val upt_add_eq_append = thm "upt_add_eq_append"; +val upt_conv_Cons = thm "upt_conv_Cons"; +val upt_conv_Nil = thm "upt_conv_Nil"; +val upt_rec = thm "upt_rec"; +val wf_lex = thm "wf_lex"; +val wf_lexico = thm "wf_lexico"; +val wf_lexn = thm "wf_lexn"; +val zip_Cons_Cons = thm "zip_Cons_Cons"; +val zip_Nil = thm "zip_Nil"; +val zip_append = thm "zip_append"; +val zip_append1 = thm "zip_append1"; +val zip_append2 = thm "zip_append2"; +val zip_replicate = thm "zip_replicate"; +val zip_rev = thm "zip_rev"; +val zip_update = thm "zip_update"; diff -r 5eb9be7b72a5 -r f2b00262bdfc src/HOL/List.thy --- a/src/HOL/List.thy Tue May 07 19:54:04 2002 +0200 +++ b/src/HOL/List.thy Tue May 07 19:54:29 2002 +0200 @@ -2,56 +2,59 @@ ID: $Id$ Author: Tobias Nipkow Copyright 1994 TU Muenchen - -The datatype of finite lists. *) -List = PreList + +header {* The datatype of finite lists *} +theory List1 = PreList: -datatype 'a list = Nil ("[]") | Cons 'a ('a list) (infixr "#" 65) +datatype 'a list = Nil ("[]") | Cons 'a "'a list" (infixr "#" 65) consts - "@" :: ['a list, 'a list] => 'a list (infixr 65) - filter :: ['a => bool, 'a list] => 'a list - concat :: 'a list list => 'a list - foldl :: [['b,'a] => 'b, 'b, 'a list] => 'b - foldr :: [['a,'b] => 'b, 'a list, 'b] => 'b - hd, last :: 'a list => 'a - set :: 'a list => 'a set - list_all :: ('a => bool) => ('a list => bool) - list_all2 :: ('a => 'b => bool) => 'a list => 'b list => bool - map :: ('a=>'b) => ('a list => 'b list) - mem :: ['a, 'a list] => bool (infixl 55) - nth :: ['a list, nat] => 'a (infixl "!" 100) - list_update :: 'a list => nat => 'a => 'a list - take, drop :: [nat, 'a list] => 'a list - takeWhile, - dropWhile :: ('a => bool) => 'a list => 'a list - tl, butlast :: 'a list => 'a list - rev :: 'a list => 'a list - zip :: "'a list => 'b list => ('a * 'b) list" - upt :: nat => nat => nat list ("(1[_../_'(])") - remdups :: "'a list => 'a list" - null, "distinct" :: "'a list => bool" - replicate :: nat => 'a => 'a list + "@" :: "'a list \ 'a list \ 'a list" (infixr 65) + filter :: "('a \ bool) \ 'a list \ 'a list" + concat :: "'a list list \ 'a list" + foldl :: "('b \ 'a \ 'b) \ 'b \ 'a list \ 'b" + foldr :: "('a \ 'b \ 'b) \ 'a list \ 'b \ 'b" + hd :: "'a list \ 'a" + tl :: "'a list \ 'a list" + last :: "'a list \ 'a" + butlast :: "'a list \ 'a list" + set :: "'a list \ 'a set" + list_all :: "('a \ bool) \ ('a list \ bool)" + list_all2 :: "('a \ 'b \ bool) \ 'a list \ 'b list \ bool" + map :: "('a\'b) \ ('a list \ 'b list)" + mem :: "'a \ 'a list \ bool" (infixl 55) + nth :: "'a list \ nat \ 'a" (infixl "!" 100) + list_update :: "'a list \ nat \ 'a \ 'a list" + take :: "nat \ 'a list \ 'a list" + drop :: "nat \ 'a list \ 'a list" + takeWhile :: "('a \ bool) \ 'a list \ 'a list" + dropWhile :: "('a \ bool) \ 'a list \ 'a list" + rev :: "'a list \ 'a list" + zip :: "'a list \ 'b list \ ('a * 'b) list" + upt :: "nat \ nat \ nat list" ("(1[_../_'(])") + remdups :: "'a list \ 'a list" + null :: "'a list \ bool" + "distinct" :: "'a list \ bool" + replicate :: "nat \ 'a \ 'a list" nonterminals lupdbinds lupdbind syntax (* list Enumeration *) - "@list" :: args => 'a list ("[(_)]") + "@list" :: "args \ 'a list" ("[(_)]") (* Special syntax for filter *) - "@filter" :: [pttrn, 'a list, bool] => 'a list ("(1[_:_./ _])") + "@filter" :: "[pttrn, 'a list, bool] \ 'a list" ("(1[_:_./ _])") (* list update *) - "_lupdbind" :: ['a, 'a] => lupdbind ("(2_ :=/ _)") - "" :: lupdbind => lupdbinds ("_") - "_lupdbinds" :: [lupdbind, lupdbinds] => lupdbinds ("_,/ _") - "_LUpdate" :: ['a, lupdbinds] => 'a ("_/[(_)]" [900,0] 900) + "_lupdbind" :: "['a, 'a] \ lupdbind" ("(2_ :=/ _)") + "" :: "lupdbind \ lupdbinds" ("_") + "_lupdbinds" :: "[lupdbind, lupdbinds] \ lupdbinds" ("_,/ _") + "_LUpdate" :: "['a, lupdbinds] \ 'a" ("_/[(_)]" [900,0] 900) - upto :: nat => nat => nat list ("(1[_../_])") + upto :: "nat \ nat \ nat list" ("(1[_../_])") translations "[x, xs]" == "x#[xs]" @@ -65,22 +68,32 @@ syntax (xsymbols) - "@filter" :: [pttrn, 'a list, bool] => 'a list ("(1[_\\_ ./ _])") + "@filter" :: "[pttrn, 'a list, bool] \ 'a list" ("(1[_\_ ./ _])") consts - lists :: 'a set => 'a list set + lists :: "'a set \ 'a list set" - inductive "lists A" - intrs - Nil "[]: lists A" - Cons "[| a: A; l: lists A |] ==> a#l : lists A" +inductive "lists A" +intros +Nil: "[]: lists A" +Cons: "\ a: A; l: lists A \ \ a#l : lists A" (*Function "size" is overloaded for all datatypes. Users may refer to the list version as "length".*) -syntax length :: 'a list => nat -translations "length" => "size:: _ list => nat" +syntax length :: "'a list \ nat" +translations "length" => "size:: _ list \ nat" + +(* translating size::list -> length *) +typed_print_translation +{* +let +fun size_tr' _ (Type ("fun", (Type ("list", _) :: _))) [t] = + Syntax.const "length" $ t + | size_tr' _ _ _ = raise Match; +in [("size", size_tr')] end +*} primrec "hd(x#xs) = x" @@ -102,14 +115,14 @@ "set [] = {}" "set (x#xs) = insert x (set xs)" primrec - list_all_Nil "list_all P [] = True" - list_all_Cons "list_all P (x#xs) = (P(x) & list_all P xs)" + list_all_Nil: "list_all P [] = True" + list_all_Cons: "list_all P (x#xs) = (P(x) & list_all P xs)" primrec "map f [] = []" "map f (x#xs) = f(x)#map f xs" primrec - append_Nil "[] @ys = ys" - append_Cons "(x#xs)@ys = x#(xs@ys)" + append_Nil: "[] @ys = ys" + append_Cons: "(x#xs)@ys = x#(xs@ys)" primrec "rev([]) = []" "rev(x#xs) = rev(xs) @ [x]" @@ -117,8 +130,8 @@ "filter P [] = []" "filter P (x#xs) = (if P x then x#filter P xs else filter P xs)" primrec - foldl_Nil "foldl f a [] = a" - foldl_Cons "foldl f a (x#xs) = foldl f (f a x) xs" + foldl_Nil: "foldl f a [] = a" + foldl_Cons: "foldl f a (x#xs) = foldl f (f a x) xs" primrec "foldr f [] a = a" "foldr f (x#xs) a = f x (foldr f xs a)" @@ -126,23 +139,23 @@ "concat([]) = []" "concat(x#xs) = x @ concat(xs)" primrec - drop_Nil "drop n [] = []" - drop_Cons "drop n (x#xs) = (case n of 0 => x#xs | Suc(m) => drop m xs)" + 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 / n=Suc k*) primrec - take_Nil "take n [] = []" - take_Cons "take n (x#xs) = (case n of 0 => [] | Suc(m) => x # take m xs)" + 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 / n=Suc k*) primrec - nth_Cons "(x#xs)!n = (case n of 0 => x | (Suc k) => xs!k)" + 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 / n=Suc k*) primrec " [][i:=v] = []" - "(x#xs)[i:=v] = (case i of 0 => v # xs - | Suc j => x # xs[j:=v])" + "(x#xs)[i:=v] = (case i of 0 \ v # xs + | Suc j \ x # xs[j:=v])" primrec "takeWhile P [] = []" "takeWhile P (x#xs) = (if P x then x#takeWhile P xs else [])" @@ -151,12 +164,13 @@ "dropWhile P (x#xs) = (if P x then dropWhile P xs else x#xs)" primrec "zip xs [] = []" - "zip xs (y#ys) = (case xs of [] => [] | z#zs => (z,y)#zip zs ys)" +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=[] / xs=z#zs *) primrec - upt_0 "[i..0(] = []" - upt_Suc "[i..(Suc j)(] = (if i <= j then [i..j(] @ [j] else [])" + upt_0: "[i..0(] = []" + upt_Suc: "[i..(Suc j)(] = (if i <= j then [i..j(] @ [j] else [])" primrec "distinct [] = True" "distinct (x#xs) = (x ~: set xs & distinct xs)" @@ -164,46 +178,1170 @@ "remdups [] = []" "remdups (x#xs) = (if x : set xs then remdups xs else x # remdups xs)" primrec - replicate_0 "replicate 0 x = []" - replicate_Suc "replicate (Suc n) x = x # replicate n x" + replicate_0: "replicate 0 x = []" + replicate_Suc: "replicate (Suc n) x = x # replicate n x" defs - list_all2_def + list_all2_def: "list_all2 P xs ys == length xs = length ys & (!(x,y):set(zip xs ys). P x y)" (** Lexicographic orderings on lists **) consts - lexn :: "('a * 'a)set => nat => ('a list * 'a list)set" + lexn :: "('a * 'a)set \ nat \ ('a list * 'a list)set" primrec "lexn r 0 = {}" "lexn r (Suc n) = (prod_fun (%(x,xs). x#xs) (%(x,xs). x#xs) ` (r <*lex*> lexn r n)) Int {(xs,ys). length xs = Suc n & length ys = Suc n}" constdefs - lex :: "('a * 'a)set => ('a list * 'a list)set" + lex :: "('a * 'a)set \ ('a list * 'a list)set" "lex r == UN n. lexn r n" - lexico :: "('a * 'a)set => ('a list * 'a list)set" + lexico :: "('a * 'a)set \ ('a list * 'a list)set" "lexico r == inv_image (less_than <*lex*> lex r) (%xs. (length xs, xs))" - sublist :: "['a list, nat set] => 'a list" + sublist :: "['a list, nat set] \ 'a list" "sublist xs A == map fst (filter (%p. snd p : A) (zip xs [0..size xs(]))" -end + +lemma not_Cons_self[simp]: "\x. xs ~= x#xs" +by(induct_tac "xs", auto) + +lemmas not_Cons_self2[simp] = not_Cons_self[THEN not_sym] + +lemma neq_Nil_conv: "(xs ~= []) = (? y ys. xs = y#ys)"; +by(induct_tac "xs", auto) + +(* Induction over the length of a list: *) +(* "(!!xs. (!ys. length ys < length xs --> P ys) ==> P xs) ==> P(xs)" *) +lemmas length_induct = measure_induct[of length] + + +(** "lists": the list-forming operator over sets **) + +lemma lists_mono: "A<=B ==> lists A <= lists B" +apply(unfold lists.defs) +apply(blast intro!:lfp_mono) +done + +inductive_cases listsE[elim!]: "x#l : lists A" +declare lists.intros[intro!] + +lemma lists_IntI[rule_format]: + "l: lists A ==> l: lists B --> l: lists (A Int B)"; +apply(erule lists.induct) +apply blast+ +done + +lemma lists_Int_eq[simp]: "lists (A Int B) = lists A Int lists B" +apply(rule mono_Int[THEN equalityI]) +apply(simp add:mono_def lists_mono) +apply(blast intro!: lists_IntI) +done + +lemma append_in_lists_conv[iff]: + "(xs@ys : lists A) = (xs : lists A & ys : lists A)" +by(induct_tac "xs", auto) + +(** length **) +(* needs to come before "@" because of thm append_eq_append_conv *) + +section "length" + +lemma length_append[simp]: "length(xs@ys) = length(xs)+length(ys)" +by(induct_tac "xs", auto) + +lemma length_map[simp]: "length (map f xs) = length xs" +by(induct_tac "xs", auto) + +lemma length_rev[simp]: "length(rev xs) = length(xs)" +by(induct_tac "xs", auto) + +lemma length_tl[simp]: "length(tl xs) = (length xs) - 1" +by(case_tac "xs", auto) + +lemma length_0_conv[iff]: "(length xs = 0) = (xs = [])" +by(induct_tac "xs", auto) + +lemma length_greater_0_conv[iff]: "(0 < length xs) = (xs ~= [])" +by(induct_tac xs, auto) + +lemma length_Suc_conv: + "(length xs = Suc n) = (? y ys. xs = y#ys & length ys = n)" +by(induct_tac "xs", auto) + +(** @ - append **) + +section "@ - append" + +lemma append_assoc[simp]: "(xs@ys)@zs = xs@(ys@zs)" +by(induct_tac "xs", auto) -ML +lemma append_Nil2[simp]: "xs @ [] = xs" +by(induct_tac "xs", auto) + +lemma append_is_Nil_conv[iff]: "(xs@ys = []) = (xs=[] & ys=[])" +by(induct_tac "xs", auto) + +lemma Nil_is_append_conv[iff]: "([] = xs@ys) = (xs=[] & ys=[])" +by(induct_tac "xs", auto) + +lemma append_self_conv[iff]: "(xs @ ys = xs) = (ys=[])" +by(induct_tac "xs", auto) + +lemma self_append_conv[iff]: "(xs = xs @ ys) = (ys=[])" +by(induct_tac "xs", auto) + +lemma append_eq_append_conv[rule_format,simp]: + "!ys. length xs = length ys | length us = length vs + --> (xs@us = ys@vs) = (xs=ys & us=vs)" +apply(induct_tac "xs") + apply(rule allI) + apply(case_tac "ys") + apply simp + apply force +apply(rule allI) +apply(case_tac "ys") + apply force +apply simp +done + +lemma same_append_eq[iff]: "(xs @ ys = xs @ zs) = (ys=zs)" +by simp + +lemma append1_eq_conv[iff]: "(xs @ [x] = ys @ [y]) = (xs = ys & x = y)" +by simp + +lemma append_same_eq[iff]: "(ys @ xs = zs @ xs) = (ys=zs)" +by simp +lemma append_self_conv2[iff]: "(xs @ ys = ys) = (xs=[])" +by(insert append_same_eq[of _ _ "[]"], auto) + +lemma self_append_conv2[iff]: "(ys = xs @ ys) = (xs=[])" +by(auto simp add: append_same_eq[of "[]", simplified]) + +lemma hd_Cons_tl[rule_format,simp]: "xs ~= [] --> hd xs # tl xs = xs" +by(induct_tac "xs", auto) + +lemma hd_append: "hd(xs@ys) = (if xs=[] then hd ys else hd xs)" +by(induct_tac "xs", auto) + +lemma hd_append2[simp]: "xs ~= [] ==> hd(xs @ ys) = hd xs" +by(simp add: hd_append split: list.split) + +lemma tl_append: "tl(xs@ys) = (case xs of [] => tl(ys) | z#zs => zs@ys)" +by(simp split: list.split) + +lemma tl_append2[simp]: "xs ~= [] ==> tl(xs @ ys) = (tl xs) @ ys" +by(simp add: tl_append split: list.split) + +(* trivial rules for solving @-equations automatically *) + +lemma eq_Nil_appendI: "xs = ys ==> xs = [] @ ys" +by simp + +lemma Cons_eq_appendI: "[| x#xs1 = ys; xs = xs1 @ zs |] ==> x#xs = ys@zs" +by(drule sym, simp) + +lemma append_eq_appendI: "[| xs@xs1 = zs; ys = xs1 @ us |] ==> xs@ys = zs@us" +by(drule sym, simp) + + +(*** +Simplification procedure for all list equalities. +Currently only tries to rearrange @ to see if +- both lists end in a singleton list, +- or both lists end in the same list. +***) +ML_setup{* local -(* translating size::list -> length *) +val list_eq_pattern = + Thm.read_cterm (Theory.sign_of (the_context ())) ("(xs::'a list) = ys",HOLogic.boolT) + +fun last (cons as Const("List.list.Cons",_) $ _ $ xs) = + (case xs of Const("List.list.Nil",_) => cons | _ => last xs) + | last (Const("List.op @",_) $ _ $ ys) = last ys + | last t = t + +fun list1 (Const("List.list.Cons",_) $ _ $ Const("List.list.Nil",_)) = true + | list1 _ = false + +fun butlast ((cons as Const("List.list.Cons",_) $ x) $ xs) = + (case xs of Const("List.list.Nil",_) => xs | _ => cons $ butlast xs) + | butlast ((app as Const("List.op @",_) $ xs) $ ys) = app $ butlast ys + | butlast xs = Const("List.list.Nil",fastype_of xs) + +val rearr_tac = + simp_tac (HOL_basic_ss addsimps [append_assoc,append_Nil,append_Cons]) + +fun list_eq sg _ (F as (eq as Const(_,eqT)) $ lhs $ rhs) = + let + val lastl = last lhs and lastr = last rhs + fun rearr conv = + let val lhs1 = butlast lhs and rhs1 = butlast rhs + val Type(_,listT::_) = eqT + val appT = [listT,listT] ---> listT + val app = Const("List.op @",appT) + val F2 = eq $ (app$lhs1$lastl) $ (app$rhs1$lastr) + val ct = cterm_of sg (HOLogic.mk_Trueprop(HOLogic.mk_eq(F,F2))) + val thm = prove_goalw_cterm [] ct (K [rearr_tac 1]) + handle ERROR => + error("The error(s) above occurred while trying to prove " ^ + string_of_cterm ct) + in Some((conv RS (thm RS trans)) RS eq_reflection) end + + in if list1 lastl andalso list1 lastr + then rearr append1_eq_conv + else + if lastl aconv lastr + then rearr append_same_eq + else None + end +in +val list_eq_simproc = mk_simproc "list_eq" [list_eq_pattern] list_eq +end; + +Addsimprocs [list_eq_simproc]; +*} + + +(** map **) + +section "map" + +lemma map_ext: "(\x. x : set xs \ f x = g x) \ map f xs = map g xs" +by (induct xs, simp_all) + +lemma map_ident[simp]: "map (%x. x) = (%xs. xs)" +by(rule ext, induct_tac "xs", auto) + +lemma map_append[simp]: "map f (xs@ys) = map f xs @ map f ys" +by(induct_tac "xs", auto) + +lemma map_compose(*[simp]*): "map (f o g) xs = map f (map g xs)" +by(unfold o_def, induct_tac "xs", auto) + +lemma rev_map: "rev(map f xs) = map f (rev xs)" +by(induct_tac xs, auto) + +(* a congruence rule for map: *) +lemma map_cong: + "xs=ys ==> (!!x. x : set ys \ f x = g x) \ map f xs = map g ys" +by (clarify, induct ys, auto) + +lemma map_is_Nil_conv[iff]: "(map f xs = []) = (xs = [])" +by(case_tac xs, auto) + +lemma Nil_is_map_conv[iff]: "([] = map f xs) = (xs = [])" +by(case_tac xs, auto) + +lemma map_eq_Cons: + "(map f xs = y#ys) = (? x xs'. xs = x#xs' & f x = y & map f xs' = ys)" +by(case_tac xs, auto) + +lemma map_injective: + "\xs. map f xs = map f ys \ (!x y. f x = f y --> x=y) \ xs=ys" +by(induct "ys", simp, fastsimp simp add:map_eq_Cons) + +lemma inj_mapI: "inj f ==> inj (map f)" +by(blast dest:map_injective injD intro:injI) + +lemma inj_mapD: "inj (map f) ==> inj f" +apply(unfold inj_on_def) +apply clarify +apply(erule_tac x = "[x]" in ballE) + apply(erule_tac x = "[y]" in ballE) + apply simp + apply blast +apply blast +done + +lemma inj_map: "inj (map f) = inj f" +by(blast dest:inj_mapD intro:inj_mapI) + +(** rev **) + +section "rev" + +lemma rev_append[simp]: "rev(xs@ys) = rev(ys) @ rev(xs)" +by(induct_tac xs, auto) + +lemma rev_rev_ident[simp]: "rev(rev xs) = xs" +by(induct_tac xs, auto) + +lemma rev_is_Nil_conv[iff]: "(rev xs = []) = (xs = [])" +by(induct_tac xs, auto) + +lemma Nil_is_rev_conv[iff]: "([] = rev xs) = (xs = [])" +by(induct_tac xs, auto) + +lemma rev_is_rev_conv[iff]: "!!ys. (rev xs = rev ys) = (xs = ys)" +apply(induct "xs" ) + apply force +apply(case_tac ys) + apply simp +apply force +done + +lemma rev_induct: "[| P []; !!x xs. P xs ==> P(xs@[x]) |] ==> P xs" +apply(subst rev_rev_ident[symmetric]) +apply(rule_tac list = "rev xs" in list.induct, simp_all) +done + +(* Instead of (rev_induct_tac xs) use (induct_tac xs rule: rev_induct) *) + +lemma rev_exhaust: "(xs = [] \ P) \ (!!ys y. xs = ys@[y] \ P) \ P" +by(induct xs rule: rev_induct, auto) + + +(** set **) + +section "set" + +lemma finite_set[iff]: "finite (set xs)" +by(induct_tac xs, auto) + +lemma set_append[simp]: "set (xs@ys) = (set xs Un set ys)" +by(induct_tac xs, auto) + +lemma set_subset_Cons: "set xs \ set (x#xs)" +by auto + +lemma set_empty[iff]: "(set xs = {}) = (xs = [])" +by(induct_tac xs, auto) + +lemma set_rev[simp]: "set(rev xs) = set(xs)" +by(induct_tac xs, auto) + +lemma set_map[simp]: "set(map f xs) = f`(set xs)" +by(induct_tac xs, auto) + +lemma set_filter[simp]: "set(filter P xs) = {x. x : set xs & P x}" +by(induct_tac xs, auto) + +lemma set_upt[simp]: "set[i..j(] = {k. i <= k & k < j}" +apply(induct_tac j) + apply simp_all +apply(erule ssubst) +apply auto +apply arith +done + +lemma in_set_conv_decomp: "(x : set xs) = (? ys zs. xs = ys@x#zs)" +apply(induct_tac "xs") + apply simp +apply simp +apply(rule iffI) + apply(blast intro: eq_Nil_appendI Cons_eq_appendI) +apply(erule exE)+ +apply(case_tac "ys") +apply auto +done + + +(* eliminate `lists' in favour of `set' *) + +lemma in_lists_conv_set: "(xs : lists A) = (!x : set xs. x : A)" +by(induct_tac xs, auto) + +lemmas in_listsD[dest!] = in_lists_conv_set[THEN iffD1] +lemmas in_listsI[intro!] = in_lists_conv_set[THEN iffD2] + + +(** mem **) + +section "mem" + +lemma set_mem_eq: "(x mem xs) = (x : set xs)" +by(induct_tac xs, auto) + + +(** list_all **) + +section "list_all" + +lemma list_all_conv: "list_all P xs = (!x:set xs. P x)" +by(induct_tac xs, auto) + +lemma list_all_append[simp]: + "list_all P (xs@ys) = (list_all P xs & list_all P ys)" +by(induct_tac xs, auto) + + +(** filter **) + +section "filter" + +lemma filter_append[simp]: "filter P (xs@ys) = filter P xs @ filter P ys" +by(induct_tac xs, auto) + +lemma filter_filter[simp]: "filter P (filter Q xs) = filter (%x. Q x & P x) xs" +by(induct_tac xs, auto) + +lemma filter_True[simp]: "!x : set xs. P x \ filter P xs = xs" +by(induct xs, auto) + +lemma filter_False[simp]: "!x : set xs. ~P x \ filter P xs = []" +by(induct xs, auto) + +lemma length_filter[simp]: "length (filter P xs) <= length xs" +by(induct xs, auto simp add: le_SucI) + +lemma filter_is_subset[simp]: "set (filter P xs) <= set xs" +by auto + + +section "concat" + +lemma concat_append[simp]: "concat(xs@ys) = concat(xs)@concat(ys)" +by(induct xs, auto) + +lemma concat_eq_Nil_conv[iff]: "(concat xss = []) = (!xs:set xss. xs=[])" +by(induct xss, auto) + +lemma Nil_eq_concat_conv[iff]: "([] = concat xss) = (!xs:set xss. xs=[])" +by(induct xss, auto) + +lemma set_concat[simp]: "set(concat xs) = Union(set ` set xs)" +by(induct xs, auto) + +lemma map_concat: "map f (concat xs) = concat (map (map f) xs)" +by(induct xs, auto) + +lemma filter_concat: "filter p (concat xs) = concat (map (filter p) xs)" +by(induct xs, auto) + +lemma rev_concat: "rev(concat xs) = concat (map rev (rev xs))" +by(induct xs, auto) + +(** nth **) + +section "nth" + +lemma nth_Cons_0[simp]: "(x#xs)!0 = x" +by auto + +lemma nth_Cons_Suc[simp]: "(x#xs)!(Suc n) = xs!n" +by auto + +declare nth.simps[simp del] + +lemma nth_append: + "!!n. (xs@ys)!n = (if n < length xs then xs!n else ys!(n - length xs))" +apply(induct "xs") + apply simp +apply(case_tac "n" ) + apply auto +done + +lemma nth_map[simp]: "!!n. n < length xs \ (map f xs)!n = f(xs!n)" +apply(induct "xs" ) + apply simp +apply(case_tac "n") + apply auto +done + +lemma set_conv_nth: "set xs = {xs!i |i. i < length xs}" +apply(induct_tac "xs") + apply simp +apply simp +apply safe + apply(rule_tac x = 0 in exI) + apply simp + apply(rule_tac x = "Suc i" in exI) + apply simp +apply(case_tac "i") + apply simp +apply(rename_tac "j") +apply(rule_tac x = "j" in exI) +apply simp +done + +lemma list_ball_nth: "\ n < length xs; !x : set xs. P x \ \ P(xs!n)" +by(simp add:set_conv_nth, blast) + +lemma nth_mem[simp]: "n < length xs ==> xs!n : set xs" +by(simp add:set_conv_nth, blast) + +lemma all_nth_imp_all_set: + "\ !i < length xs. P(xs!i); x : set xs \ \ P x" +by(simp add:set_conv_nth, blast) + +lemma all_set_conv_all_nth: + "(!x : set xs. P x) = (!i. i P (xs ! i))" +by(simp add:set_conv_nth, blast) + + +(** list update **) + +section "list update" + +lemma length_list_update[simp]: "!!i. length(xs[i:=x]) = length xs" +by(induct xs, simp, simp split:nat.split) + +lemma nth_list_update: + "!!i j. i < length xs \ (xs[i:=x])!j = (if i=j then x else xs!j)" +by(induct xs, simp, auto simp add:nth_Cons split:nat.split) + +lemma nth_list_update_eq[simp]: "i < length xs ==> (xs[i:=x])!i = x" +by(simp add:nth_list_update) + +lemma nth_list_update_neq[simp]: "!!i j. i ~= j \ xs[i:=x]!j = xs!j" +by(induct xs, simp, auto simp add:nth_Cons split:nat.split) + +lemma list_update_overwrite[simp]: + "!!i. i < size xs ==> xs[i:=x, i:=y] = xs[i:=y]" +by(induct xs, simp, simp split:nat.split) + +lemma list_update_same_conv: + "!!i. i < length xs \ (xs[i := x] = xs) = (xs!i = x)" +by(induct xs, simp, simp split:nat.split, blast) + +lemma update_zip: +"!!i xy xs. length xs = length ys \ + (zip xs ys)[i:=xy] = zip (xs[i:=fst xy]) (ys[i:=snd xy])" +by(induct ys, auto, case_tac xs, auto split:nat.split) + +lemma set_update_subset_insert: "!!i. set(xs[i:=x]) <= insert x (set xs)" +by(induct xs, simp, simp split:nat.split, fast) + +lemma set_update_subsetI: "[| set xs <= A; x:A |] ==> set(xs[i := x]) <= A" +by(fast dest!:set_update_subset_insert[THEN subsetD]) + + +(** last & butlast **) + +section "last / butlast" + +lemma last_snoc[simp]: "last(xs@[x]) = x" +by(induct xs, auto) + +lemma butlast_snoc[simp]:"butlast(xs@[x]) = xs" +by(induct xs, auto) + +lemma length_butlast[simp]: "length(butlast xs) = length xs - 1" +by(induct xs rule:rev_induct, auto) + +lemma butlast_append: + "!!ys. butlast (xs@ys) = (if ys=[] then butlast xs else xs@butlast ys)" +by(induct xs, auto) + +lemma append_butlast_last_id[simp]: + "xs ~= [] --> butlast xs @ [last xs] = xs" +by(induct xs, auto) + +lemma in_set_butlastD: "x:set(butlast xs) ==> x:set xs" +by(induct xs, auto split:split_if_asm) + +lemma in_set_butlast_appendI: + "x:set(butlast xs) | x:set(butlast ys) ==> x:set(butlast(xs@ys))" +by(auto dest:in_set_butlastD simp add:butlast_append) + +(** take & drop **) +section "take & drop" + +lemma take_0[simp]: "take 0 xs = []" +by(induct xs, auto) + +lemma drop_0[simp]: "drop 0 xs = xs" +by(induct xs, auto) + +lemma take_Suc_Cons[simp]: "take (Suc n) (x#xs) = x # take n xs" +by simp + +lemma drop_Suc_Cons[simp]: "drop (Suc n) (x#xs) = drop n xs" +by simp + +declare take_Cons[simp del] drop_Cons[simp del] + +lemma length_take[simp]: "!!xs. length(take n xs) = min (length xs) n" +by(induct n, auto, case_tac xs, auto) + +lemma length_drop[simp]: "!!xs. length(drop n xs) = (length xs - n)" +by(induct n, auto, case_tac xs, auto) + +lemma take_all[simp]: "!!xs. length xs <= n ==> take n xs = xs" +by(induct n, auto, case_tac xs, auto) + +lemma drop_all[simp]: "!!xs. length xs <= n ==> drop n xs = []" +by(induct n, auto, case_tac xs, auto) + +lemma take_append[simp]: + "!!xs. take n (xs @ ys) = (take n xs @ take (n - length xs) ys)" +by(induct n, auto, case_tac xs, auto) + +lemma drop_append[simp]: + "!!xs. drop n (xs@ys) = drop n xs @ drop (n - length xs) ys" +by(induct n, auto, case_tac xs, auto) + +lemma take_take[simp]: "!!xs n. take n (take m xs) = take (min n m) xs" +apply(induct m) + apply auto +apply(case_tac xs) + apply auto +apply(case_tac na) + apply auto +done + +lemma drop_drop[simp]: "!!xs. drop n (drop m xs) = drop (n + m) xs" +apply(induct m) + apply auto +apply(case_tac xs) + apply auto +done + +lemma take_drop: "!!xs n. take n (drop m xs) = drop m (take (n + m) xs)" +apply(induct m) + apply auto +apply(case_tac xs) + apply auto +done + +lemma append_take_drop_id[simp]: "!!xs. take n xs @ drop n xs = xs" +apply(induct n) + apply auto +apply(case_tac xs) + apply auto +done + +lemma take_map: "!!xs. take n (map f xs) = map f (take n xs)" +apply(induct n) + apply auto +apply(case_tac xs) + apply auto +done + +lemma drop_map: "!!xs. drop n (map f xs) = map f (drop n xs)" +apply(induct n) + apply auto +apply(case_tac xs) + apply auto +done + +lemma rev_take: "!!i. rev (take i xs) = drop (length xs - i) (rev xs)" +apply(induct xs) + apply auto +apply(case_tac i) + apply auto +done + +lemma rev_drop: "!!i. rev (drop i xs) = take (length xs - i) (rev xs)" +apply(induct xs) + apply auto +apply(case_tac i) + apply auto +done + +lemma nth_take[simp]: "!!n i. i < n ==> (take n xs)!i = xs!i" +apply(induct xs) + apply auto +apply(case_tac n) + apply(blast ) +apply(case_tac i) + apply auto +done + +lemma nth_drop[simp]: "!!xs i. n + i <= length xs ==> (drop n xs)!i = xs!(n+i)" +apply(induct n) + apply auto +apply(case_tac xs) + apply auto +done -fun size_tr' _ (Type ("fun", (Type ("list", _) :: _))) [t] = - Syntax.const "length" $ t - | size_tr' _ _ _ = raise Match; +lemma append_eq_conv_conj: + "!!zs. (xs@ys = zs) = (xs = take (length xs) zs & ys = drop (length xs) zs)" +apply(induct xs) + apply simp +apply clarsimp +apply(case_tac zs) +apply auto +done + +(** takeWhile & dropWhile **) + +section "takeWhile & dropWhile" + +lemma takeWhile_dropWhile_id[simp]: "takeWhile P xs @ dropWhile P xs = xs" +by(induct xs, auto) + +lemma takeWhile_append1[simp]: + "\ x:set xs; ~P(x) \ \ takeWhile P (xs @ ys) = takeWhile P xs" +by(induct xs, auto) + +lemma takeWhile_append2[simp]: + "(!!x. x : set xs \ P(x)) ==> takeWhile P (xs @ ys) = xs @ takeWhile P ys" +by(induct xs, auto) + +lemma takeWhile_tail: "~P(x) ==> takeWhile P (xs @ (x#l)) = takeWhile P xs" +by(induct xs, auto) + +lemma dropWhile_append1[simp]: + "\ x : set xs; ~P(x) \ \ dropWhile P (xs @ ys) = (dropWhile P xs)@ys" +by(induct xs, auto) + +lemma dropWhile_append2[simp]: + "(!!x. x:set xs \ P(x)) ==> dropWhile P (xs @ ys) = dropWhile P ys" +by(induct xs, auto) + +lemma set_take_whileD: "x:set(takeWhile P xs) ==> x:set xs & P x" +by(induct xs, auto split:split_if_asm) + + +(** zip **) +section "zip" + +lemma zip_Nil[simp]: "zip [] ys = []" +by(induct ys, auto) + +lemma zip_Cons_Cons[simp]: "zip (x#xs) (y#ys) = (x,y)#zip xs ys" +by simp + +declare zip_Cons[simp del] + +lemma length_zip[simp]: + "!!xs. length (zip xs ys) = min (length xs) (length ys)" +apply(induct ys) + apply simp +apply(case_tac xs) + apply auto +done + +lemma zip_append1: + "!!xs. zip (xs@ys) zs = + zip xs (take (length xs) zs) @ zip ys (drop (length xs) zs)" +apply(induct zs) + apply simp +apply(case_tac xs) + apply simp_all +done + +lemma zip_append2: + "!!ys. zip xs (ys@zs) = + zip (take (length ys) xs) ys @ zip (drop (length ys) xs) zs" +apply(induct xs) + apply simp +apply(case_tac ys) + apply simp_all +done + +lemma zip_append[simp]: + "[| length xs = length us; length ys = length vs |] ==> \ +\ zip (xs@ys) (us@vs) = zip xs us @ zip ys vs" +by(simp add: zip_append1) + +lemma zip_rev: + "!!xs. length xs = length ys ==> zip (rev xs) (rev ys) = rev (zip xs ys)" +apply(induct ys) + apply simp +apply(case_tac xs) + apply simp_all +done + +lemma nth_zip[simp]: +"!!i xs. \ i < length xs; i < length ys \ \ (zip xs ys)!i = (xs!i, ys!i)" +apply(induct ys) + apply simp +apply(case_tac xs) + apply (simp_all add: nth.simps split:nat.split) +done + +lemma set_zip: + "set(zip xs ys) = {(xs!i,ys!i) |i. i < min (length xs) (length ys)}" +by(simp add: set_conv_nth cong: rev_conj_cong) + +lemma zip_update: + "length xs = length ys ==> zip (xs[i:=x]) (ys[i:=y]) = (zip xs ys)[i:=(x,y)]" +by(rule sym, simp add: update_zip) + +lemma zip_replicate[simp]: + "!!j. zip (replicate i x) (replicate j y) = replicate (min i j) (x,y)" +apply(induct i) + apply auto +apply(case_tac j) + apply auto +done + +(** list_all2 **) +section "list_all2" + +lemma list_all2_lengthD: "list_all2 P xs ys ==> length xs = length ys" +by(simp add:list_all2_def) + +lemma list_all2_Nil[iff]: "list_all2 P [] ys = (ys=[])" +by(simp add:list_all2_def) + +lemma list_all2_Nil2[iff]: "list_all2 P xs [] = (xs=[])" +by(simp add:list_all2_def) + +lemma list_all2_Cons[iff]: + "list_all2 P (x#xs) (y#ys) = (P x y & list_all2 P xs ys)" +by(auto simp add:list_all2_def) + +lemma list_all2_Cons1: + "list_all2 P (x#xs) ys = (? z zs. ys = z#zs & P x z & list_all2 P xs zs)" +by(case_tac ys, auto) + +lemma list_all2_Cons2: + "list_all2 P xs (y#ys) = (? z zs. xs = z#zs & P z y & list_all2 P zs ys)" +by(case_tac xs, auto) + +lemma list_all2_rev[iff]: + "list_all2 P (rev xs) (rev ys) = list_all2 P xs ys" +by(simp add:list_all2_def zip_rev cong:conj_cong) + +lemma list_all2_append1: + "list_all2 P (xs@ys) zs = + (EX us vs. zs = us@vs & length us = length xs & length vs = length ys & + list_all2 P xs us & list_all2 P ys vs)" +apply(simp add:list_all2_def zip_append1) +apply(rule iffI) + apply(rule_tac x = "take (length xs) zs" in exI) + apply(rule_tac x = "drop (length xs) zs" in exI) + apply(force split: nat_diff_split simp add:min_def) +apply clarify +apply(simp add: ball_Un) +done + +lemma list_all2_append2: + "list_all2 P xs (ys@zs) = + (EX us vs. xs = us@vs & length us = length ys & length vs = length zs & + list_all2 P us ys & list_all2 P vs zs)" +apply(simp add:list_all2_def zip_append2) +apply(rule iffI) + apply(rule_tac x = "take (length ys) xs" in exI) + apply(rule_tac x = "drop (length ys) xs" in exI) + apply(force split: nat_diff_split simp add:min_def) +apply clarify +apply(simp add: ball_Un) +done + +lemma list_all2_conv_all_nth: + "list_all2 P xs ys = + (length xs = length ys & (!i P2 b c --> P3 a c ==> + ALL bs cs. list_all2 P1 as bs --> list_all2 P2 bs cs --> list_all2 P3 as cs" +apply(induct_tac as) + apply simp +apply(rule allI) +apply(induct_tac bs) + apply simp +apply(rule allI) +apply(induct_tac cs) + apply auto +done + + +section "foldl" + +lemma foldl_append[simp]: + "!!a. foldl f a (xs @ ys) = foldl f (foldl f a xs) ys" +by(induct xs, auto) + +(* Note: `n <= foldl op+ n ns' looks simpler, but is more difficult to use + because it requires an additional transitivity step +*) +lemma start_le_sum: "!!n::nat. m <= n ==> m <= foldl op+ n ns" +by(induct ns, auto) + +lemma elem_le_sum: "!!n::nat. n : set ns ==> n <= foldl op+ 0 ns" +by(force intro: start_le_sum simp add:in_set_conv_decomp) + +lemma sum_eq_0_conv[iff]: + "!!m::nat. (foldl op+ m ns = 0) = (m=0 & (!n : set ns. n=0))" +by(induct ns, auto) + +(** upto **) + +(* Does not terminate! *) +lemma upt_rec: "[i..j(] = (if i [i..j(] = []" +by(subst upt_rec, simp) + +(*Only needed if upt_Suc is deleted from the simpset*) +lemma upt_Suc_append: "i<=j ==> [i..(Suc j)(] = [i..j(]@[j]" +by simp + +lemma upt_conv_Cons: "i [i..j(] = i#[Suc i..j(]" +apply(rule trans) +apply(subst upt_rec) + prefer 2 apply(rule refl) +apply simp +done + +(*LOOPS as a simprule, since j<=j*) +lemma upt_add_eq_append: "i<=j ==> [i..j+k(] = [i..j(]@[j..j+k(]" +by(induct_tac "k", auto) + +lemma length_upt[simp]: "length [i..j(] = j-i" +by(induct_tac j, simp, simp add: Suc_diff_le) + +lemma nth_upt[simp]: "i+k < j ==> [i..j(] ! k = i+k" +apply(induct j) +apply(auto simp add: less_Suc_eq nth_append split:nat_diff_split) +done + +lemma take_upt[simp]: "!!i. i+m <= n ==> take m [i..n(] = [i..i+m(]" +apply(induct m) + apply simp +apply(subst upt_rec) +apply(rule sym) +apply(subst upt_rec) +apply(simp del: upt.simps) +done -in +lemma map_Suc_upt: "map Suc [m..n(] = [Suc m..n]" +by(induct n, auto) + +lemma nth_map_upt: "!!i. i < n-m ==> (map f [m..n(]) ! i = f(m+i)" +thm diff_induct +apply(induct n m rule: diff_induct) +prefer 3 apply(subst map_Suc_upt[symmetric]) +apply(auto simp add: less_diff_conv nth_upt) +done + +lemma nth_take_lemma[rule_format]: + "ALL xs ys. k <= length xs --> k <= length ys + --> (ALL i. i < k --> xs!i = ys!i) + --> take k xs = take k ys" +apply(induct_tac k) +apply(simp_all add: less_Suc_eq_0_disj all_conj_distrib) +apply clarify +(*Both lists must be non-empty*) +apply(case_tac xs) + apply simp +apply(case_tac ys) + apply clarify + apply(simp (no_asm_use)) +apply clarify +(*prenexing's needed, not miniscoping*) +apply(simp (no_asm_use) add: all_simps[symmetric] del: all_simps) +apply blast +(*prenexing's needed, not miniscoping*) +done + +lemma nth_equalityI: + "[| length xs = length ys; ALL i < length xs. xs!i = ys!i |] ==> xs = ys" +apply(frule nth_take_lemma[OF le_refl eq_imp_le]) +apply(simp_all add: take_all) +done + +(*The famous take-lemma*) +lemma take_equalityI: "(ALL i. take i xs = take i ys) ==> xs = ys" +apply(drule_tac x = "max (length xs) (length ys)" in spec) +apply(simp add: le_max_iff_disj take_all) +done + + +(** distinct & remdups **) +section "distinct & remdups" + +lemma distinct_append[simp]: + "distinct(xs@ys) = (distinct xs & distinct ys & set xs Int set ys = {})" +by(induct xs, auto) + +lemma set_remdups[simp]: "set(remdups xs) = set xs" +by(induct xs, simp, simp add:insert_absorb) + +lemma distinct_remdups[iff]: "distinct(remdups xs)" +by(induct xs, auto) + +lemma distinct_filter[simp]: "distinct xs ==> distinct (filter P xs)" +by(induct xs, auto) + +(** replicate **) +section "replicate" + +lemma length_replicate[simp]: "length(replicate n x) = n" +by(induct n, auto) + +lemma map_replicate[simp]: "map f (replicate n x) = replicate n (f x)" +by(induct n, 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" +apply(induct n) + apply simp +apply(simp add: replicate_app_Cons_same) +done + +lemma replicate_add: "replicate (n+m) x = replicate n x @ replicate m x" +by(induct n, auto) + +lemma hd_replicate[simp]: "n ~= 0 ==> hd(replicate n x) = x" +by(induct n, auto) + +lemma tl_replicate[simp]: "n ~= 0 ==> tl(replicate n x) = replicate (n - 1) x" +by(induct n, auto) + +lemma last_replicate[rule_format,simp]: + "n ~= 0 --> last(replicate n x) = x" +by(induct_tac n, auto) + +lemma nth_replicate[simp]: "!!i. i (replicate n x)!i = x" +apply(induct n) + apply simp +apply(simp add: nth_Cons split:nat.split) +done + +lemma set_replicate_Suc: "set(replicate (Suc n) x) = {x}" +by(induct n, auto) + +lemma set_replicate[simp]: "n ~= 0 ==> 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_replicateD: "x : set(replicate n y) ==> x=y" +by(simp add: set_replicate_conv_if split:split_if_asm) + + +(*** Lexcicographic orderings on lists ***) +section"Lexcicographic orderings on lists" -val typed_print_translation = [("size", size_tr')]; +lemma wf_lexn: "wf r ==> wf(lexn r n)" +apply(induct_tac n) + apply simp +apply simp +apply(rule wf_subset) + prefer 2 apply(rule Int_lower1) +apply(rule wf_prod_fun_image) + prefer 2 apply(rule injI) +apply auto +done + +lemma lexn_length: + "!!xs ys. (xs,ys) : lexn r n ==> length xs = n & length ys = n" +by(induct n, auto) + +lemma wf_lex[intro!]: "wf r ==> wf(lex r)" +apply(unfold lex_def) +apply(rule wf_UN) +apply(blast intro: wf_lexn) +apply clarify +apply(rename_tac m n) +apply(subgoal_tac "m ~= n") + prefer 2 apply blast +apply(blast dest: lexn_length not_sym) +done + + +lemma lexn_conv: + "lexn r n = + {(xs,ys). length xs = n & length ys = n & + (? xys x y xs' ys'. xs= xys @ x#xs' & ys= xys @ y#ys' & (x,y):r)}" +apply(induct_tac n) + apply simp + apply blast +apply(simp add: image_Collect lex_prod_def) +apply auto + apply blast + apply(rename_tac a xys x xs' y ys') + apply(rule_tac x = "a#xys" in exI) + apply simp +apply(case_tac xys) + apply simp_all +apply blast +done + +lemma lex_conv: + "lex r = + {(xs,ys). length xs = length ys & + (? xys x y xs' ys'. xs= xys @ x#xs' & ys= xys @ y#ys' & (x,y):r)}" +by(force simp add: lex_def lexn_conv) + +lemma wf_lexico[intro!]: "wf r ==> wf(lexico r)" +by(unfold lexico_def, blast) + +lemma lexico_conv: +"lexico r = {(xs,ys). length xs < length ys | + length xs = length ys & (xs,ys) : lex r}" +by(simp add: lexico_def diag_def lex_prod_def measure_def inv_image_def) + +lemma Nil_notin_lex[iff]: "([],ys) ~: lex r" +by(simp add:lex_conv) + +lemma Nil2_notin_lex[iff]: "(xs,[]) ~: lex r" +by(simp add:lex_conv) + +lemma Cons_in_lex[iff]: + "((x#xs,y#ys) : lex r) = + ((x,y) : r & length xs = length ys | x=y & (xs,ys) : lex r)" +apply(simp add:lex_conv) +apply(rule iffI) + prefer 2 apply(blast intro: Cons_eq_appendI) +apply clarify +apply(case_tac xys) + apply simp +apply simp +apply blast +done + + +(*** sublist (a generalization of nth to sets) ***) + +lemma sublist_empty[simp]: "sublist xs {} = []" +by(auto simp add:sublist_def) + +lemma sublist_nil[simp]: "sublist [] A = []" +by(auto simp add:sublist_def) + +lemma sublist_shift_lemma: + "map fst [p:zip xs [i..i + length xs(] . snd p : A] = + map fst [p:zip xs [0..length xs(] . snd p + i : A]" +apply(induct_tac xs rule: rev_induct) + apply simp +apply(simp add:add_commute) +done + +lemma sublist_append: + "sublist (l@l') A = sublist l A @ sublist l' {j. j + length l : A}" +apply(unfold sublist_def) +apply(induct_tac l' rule: rev_induct) + apply simp +apply(simp add: upt_add_eq_append[of 0] zip_append sublist_shift_lemma) +apply(simp add:add_commute) +done + +lemma sublist_Cons: + "sublist (x#l) A = (if 0:A then [x] else []) @ sublist l {j. Suc j : A}" +apply(induct_tac l rule: rev_induct) + apply(simp add:sublist_def) +apply(simp del: append_Cons add: append_Cons[symmetric] sublist_append) +done + +lemma sublist_singleton[simp]: "sublist [x] A = (if 0 : A then [x] else [])" +by(simp add:sublist_Cons) + +lemma sublist_upt_eq_take[simp]: "sublist l {..n(} = take n l" +apply(induct_tac l rule: rev_induct) + apply simp +apply(simp split:nat_diff_split add:sublist_append) +done + + +lemma take_Cons': "take n (x#xs) = (if n=0 then [] else x # take (n - 1) xs)" +by(case_tac n, simp_all) + +lemma drop_Cons': "drop n (x#xs) = (if n=0 then x#xs else drop (n - 1) xs)" +by(case_tac n, simp_all) + +lemma nth_Cons': "(x#xs)!n = (if n=0 then x else xs!(n - 1))" +by(case_tac n, simp_all) + +lemmas [simp] = take_Cons'[of "number_of v",standard] + drop_Cons'[of "number_of v",standard] + nth_Cons'[of "number_of v",standard] end;