src/HOL/Library/Permutation.thy
author nipkow
Mon Nov 05 18:18:39 2007 +0100 (2007-11-05)
changeset 25287 094dab519ff5
parent 25277 95128fcdd7e8
child 25379 12bcf37252b1
permissions -rw-r--r--
added lemmas
     1 (*  Title:      HOL/Library/Permutation.thy
     2     Author:     Lawrence C Paulson and Thomas M Rasmussen and Norbert Voelker
     3 *)
     4 
     5 header {* Permutations *}
     6 
     7 theory Permutation
     8 imports Multiset
     9 begin
    10 
    11 inductive
    12   perm :: "'a list => 'a list => bool"  ("_ <~~> _"  [50, 50] 50)
    13   where
    14     Nil  [intro!]: "[] <~~> []"
    15   | swap [intro!]: "y # x # l <~~> x # y # l"
    16   | Cons [intro!]: "xs <~~> ys ==> z # xs <~~> z # ys"
    17   | trans [intro]: "xs <~~> ys ==> ys <~~> zs ==> xs <~~> zs"
    18 
    19 lemma perm_refl [iff]: "l <~~> l"
    20   by (induct l) auto
    21 
    22 
    23 subsection {* Some examples of rule induction on permutations *}
    24 
    25 lemma xperm_empty_imp_aux: "xs <~~> ys ==> xs = [] --> ys = []"
    26     -- {*the form of the premise lets the induction bind @{term xs}
    27          and @{term ys} *}
    28   apply (erule perm.induct)
    29      apply (simp_all (no_asm_simp))
    30   done
    31 
    32 lemma xperm_empty_imp: "[] <~~> ys ==> ys = []"
    33   using xperm_empty_imp_aux by blast
    34 
    35 
    36 text {*
    37   \medskip This more general theorem is easier to understand!
    38   *}
    39 
    40 lemma perm_length: "xs <~~> ys ==> length xs = length ys"
    41   by (erule perm.induct) simp_all
    42 
    43 lemma perm_empty_imp: "[] <~~> xs ==> xs = []"
    44   by (drule perm_length) auto
    45 
    46 lemma perm_sym: "xs <~~> ys ==> ys <~~> xs"
    47   by (erule perm.induct) auto
    48 
    49 
    50 subsection {* Ways of making new permutations *}
    51 
    52 text {*
    53   We can insert the head anywhere in the list.
    54 *}
    55 
    56 lemma perm_append_Cons: "a # xs @ ys <~~> xs @ a # ys"
    57   by (induct xs) auto
    58 
    59 lemma perm_append_swap: "xs @ ys <~~> ys @ xs"
    60   apply (induct xs)
    61     apply simp_all
    62   apply (blast intro: perm_append_Cons)
    63   done
    64 
    65 lemma perm_append_single: "a # xs <~~> xs @ [a]"
    66   by (rule perm.trans [OF _ perm_append_swap]) simp
    67 
    68 lemma perm_rev: "rev xs <~~> xs"
    69   apply (induct xs)
    70    apply simp_all
    71   apply (blast intro!: perm_append_single intro: perm_sym)
    72   done
    73 
    74 lemma perm_append1: "xs <~~> ys ==> l @ xs <~~> l @ ys"
    75   by (induct l) auto
    76 
    77 lemma perm_append2: "xs <~~> ys ==> xs @ l <~~> ys @ l"
    78   by (blast intro!: perm_append_swap perm_append1)
    79 
    80 
    81 subsection {* Further results *}
    82 
    83 lemma perm_empty [iff]: "([] <~~> xs) = (xs = [])"
    84   by (blast intro: perm_empty_imp)
    85 
    86 lemma perm_empty2 [iff]: "(xs <~~> []) = (xs = [])"
    87   apply auto
    88   apply (erule perm_sym [THEN perm_empty_imp])
    89   done
    90 
    91 lemma perm_sing_imp [rule_format]: "ys <~~> xs ==> xs = [y] --> ys = [y]"
    92   by (erule perm.induct) auto
    93 
    94 lemma perm_sing_eq [iff]: "(ys <~~> [y]) = (ys = [y])"
    95   by (blast intro: perm_sing_imp)
    96 
    97 lemma perm_sing_eq2 [iff]: "([y] <~~> ys) = (ys = [y])"
    98   by (blast dest: perm_sym)
    99 
   100 
   101 subsection {* Removing elements *}
   102 
   103 consts
   104   remove :: "'a => 'a list => 'a list"
   105 primrec
   106   "remove x [] = []"
   107   "remove x (y # ys) = (if x = y then ys else y # remove x ys)"
   108 
   109 lemma perm_remove: "x \<in> set ys ==> ys <~~> x # remove x ys"
   110   by (induct ys) auto
   111 
   112 lemma remove_commute: "remove x (remove y l) = remove y (remove x l)"
   113   by (induct l) auto
   114 
   115 lemma multiset_of_remove[simp]:
   116     "multiset_of (remove a x) = multiset_of x - {#a#}"
   117   apply (induct x)
   118    apply (auto simp: multiset_eq_conv_count_eq)
   119   done
   120 
   121 
   122 text {* \medskip Congruence rule *}
   123 
   124 lemma perm_remove_perm: "xs <~~> ys ==> remove z xs <~~> remove z ys"
   125   by (erule perm.induct) auto
   126 
   127 lemma remove_hd [simp]: "remove z (z # xs) = xs"
   128   by auto
   129 
   130 lemma cons_perm_imp_perm: "z # xs <~~> z # ys ==> xs <~~> ys"
   131   by (drule_tac z = z in perm_remove_perm) auto
   132 
   133 lemma cons_perm_eq [iff]: "(z#xs <~~> z#ys) = (xs <~~> ys)"
   134   by (blast intro: cons_perm_imp_perm)
   135 
   136 lemma append_perm_imp_perm: "!!xs ys. zs @ xs <~~> zs @ ys ==> xs <~~> ys"
   137   apply (induct zs rule: rev_induct)
   138    apply (simp_all (no_asm_use))
   139   apply blast
   140   done
   141 
   142 lemma perm_append1_eq [iff]: "(zs @ xs <~~> zs @ ys) = (xs <~~> ys)"
   143   by (blast intro: append_perm_imp_perm perm_append1)
   144 
   145 lemma perm_append2_eq [iff]: "(xs @ zs <~~> ys @ zs) = (xs <~~> ys)"
   146   apply (safe intro!: perm_append2)
   147   apply (rule append_perm_imp_perm)
   148   apply (rule perm_append_swap [THEN perm.trans])
   149     -- {* the previous step helps this @{text blast} call succeed quickly *}
   150   apply (blast intro: perm_append_swap)
   151   done
   152 
   153 lemma multiset_of_eq_perm: "(multiset_of xs = multiset_of ys) = (xs <~~> ys) "
   154   apply (rule iffI)
   155   apply (erule_tac [2] perm.induct, simp_all add: union_ac)
   156   apply (erule rev_mp, rule_tac x=ys in spec)
   157   apply (induct_tac xs, auto)
   158   apply (erule_tac x = "remove a x" in allE, drule sym, simp)
   159   apply (subgoal_tac "a \<in> set x")
   160   apply (drule_tac z=a in perm.Cons)
   161   apply (erule perm.trans, rule perm_sym, erule perm_remove)
   162   apply (drule_tac f=set_of in arg_cong, simp)
   163   done
   164 
   165 lemma multiset_of_le_perm_append:
   166     "(multiset_of xs \<le># multiset_of ys) = (\<exists>zs. xs @ zs <~~> ys)";
   167   apply (auto simp: multiset_of_eq_perm[THEN sym] mset_le_exists_conv)
   168   apply (insert surj_multiset_of, drule surjD)
   169   apply (blast intro: sym)+
   170   done
   171 
   172 lemma perm_set_eq: "xs <~~> ys ==> set xs = set ys"
   173 by (metis multiset_of_eq_perm multiset_of_eq_setD)
   174 
   175 lemma perm_distinct_iff: "xs <~~> ys ==> distinct xs = distinct ys"
   176 apply(induct rule:perm.induct)
   177    apply simp_all
   178  apply fastsimp
   179 apply (metis perm_set_eq)
   180 done
   181 
   182 lemma eq_set_perm_remdups: "set xs = set ys ==> remdups xs <~~> remdups ys"
   183 apply(induct xs arbitrary: ys rule:length_induct)
   184 apply (case_tac "remdups xs", simp, simp)
   185 apply(subgoal_tac "a : set (remdups ys)")
   186  prefer 2 apply (metis set.simps(2) insert_iff set_remdups)
   187 apply(drule split_list) apply(elim exE conjE)
   188 apply(drule_tac x=list in spec) apply(erule impE) prefer 2
   189  apply(drule_tac x="ysa@zs" in spec) apply(erule impE) prefer 2
   190   apply simp
   191   apply(subgoal_tac "a#list <~~> a#ysa@zs")
   192    apply (metis Cons_eq_appendI perm_append_Cons trans)
   193   apply (metis Cons Cons_eq_appendI distinct.simps(2) distinct_remdups distinct_remdups_id perm_append_swap perm_distinct_iff)
   194  apply(subgoal_tac "set (a#list) = set (ysa@a#zs) & distinct (a#list) & distinct (ysa@a#zs)")
   195   apply(fastsimp simp add: insert_ident)
   196  apply (metis distinct_remdups set_remdups)
   197 apply (metis Nat.le_less_trans Suc_length_conv le_def length_remdups_leq less_Suc_eq)
   198 done
   199 
   200 lemma perm_remdups_iff_eq_set: "remdups x <~~> remdups y = (set x = set y)"
   201 by (metis List.set_remdups perm_set_eq eq_set_perm_remdups)
   202 
   203 end