src/HOL/Library/Permutation.thy
author paulson
Thu Jul 22 10:33:26 2004 +0200 (2004-07-22)
changeset 15072 4861bf6af0b4
parent 15005 546c8e7e28d4
child 15131 c69542757a4d
permissions -rw-r--r--
new material courtesy of Norbert Voelker
     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 = Multiset:
     8 
     9 consts
    10   perm :: "('a list * 'a list) set"
    11 
    12 syntax
    13   "_perm" :: "'a list => 'a list => bool"    ("_ <~~> _"  [50, 50] 50)
    14 translations
    15   "x <~~> y" == "(x, y) \<in> perm"
    16 
    17 inductive perm
    18   intros
    19     Nil  [intro!]: "[] <~~> []"
    20     swap [intro!]: "y # x # l <~~> x # y # l"
    21     Cons [intro!]: "xs <~~> ys ==> z # xs <~~> z # ys"
    22     trans [intro]: "xs <~~> ys ==> ys <~~> zs ==> xs <~~> zs"
    23 
    24 lemma perm_refl [iff]: "l <~~> l"
    25 by (induct l, auto)
    26 
    27 
    28 subsection {* Some examples of rule induction on permutations *}
    29 
    30 lemma xperm_empty_imp_aux: "xs <~~> ys ==> xs = [] --> ys = []"
    31     -- {*the form of the premise lets the induction bind @{term xs} 
    32          and @{term ys} *}
    33   apply (erule perm.induct)
    34      apply (simp_all (no_asm_simp))
    35   done
    36 
    37 lemma xperm_empty_imp: "[] <~~> ys ==> ys = []"
    38 by (insert xperm_empty_imp_aux, blast)
    39 
    40 
    41 text {*
    42   \medskip This more general theorem is easier to understand!
    43   *}
    44 
    45 lemma perm_length: "xs <~~> ys ==> length xs = length ys"
    46 by (erule perm.induct, simp_all)
    47 
    48 lemma perm_empty_imp: "[] <~~> xs ==> xs = []"
    49 by (drule perm_length, auto)
    50 
    51 lemma perm_sym: "xs <~~> ys ==> ys <~~> xs"
    52 by (erule perm.induct, auto)
    53 
    54 lemma perm_mem [rule_format]: "xs <~~> ys ==> x mem xs --> x mem ys"
    55 by (erule perm.induct, auto)
    56 
    57 
    58 subsection {* Ways of making new permutations *}
    59 
    60 text {*
    61   We can insert the head anywhere in the list.
    62 *}
    63 
    64 lemma perm_append_Cons: "a # xs @ ys <~~> xs @ a # ys"
    65 by (induct xs, auto)
    66 
    67 lemma perm_append_swap: "xs @ ys <~~> ys @ xs"
    68   apply (induct xs, simp_all)
    69   apply (blast intro: perm_append_Cons)
    70   done
    71 
    72 lemma perm_append_single: "a # xs <~~> xs @ [a]"
    73   by (rule perm.trans [OF _ perm_append_swap], simp)
    74 
    75 lemma perm_rev: "rev xs <~~> xs"
    76   apply (induct xs, simp_all)
    77   apply (blast intro!: perm_append_single intro: perm_sym)
    78   done
    79 
    80 lemma perm_append1: "xs <~~> ys ==> l @ xs <~~> l @ ys"
    81 by (induct l, auto)
    82 
    83 lemma perm_append2: "xs <~~> ys ==> xs @ l <~~> ys @ l"
    84 by (blast intro!: perm_append_swap perm_append1)
    85 
    86 
    87 subsection {* Further results *}
    88 
    89 lemma perm_empty [iff]: "([] <~~> xs) = (xs = [])"
    90 by (blast intro: perm_empty_imp)
    91 
    92 lemma perm_empty2 [iff]: "(xs <~~> []) = (xs = [])"
    93   apply auto
    94   apply (erule perm_sym [THEN perm_empty_imp])
    95   done
    96 
    97 lemma perm_sing_imp [rule_format]: "ys <~~> xs ==> xs = [y] --> ys = [y]"
    98 by (erule perm.induct, auto)
    99 
   100 lemma perm_sing_eq [iff]: "(ys <~~> [y]) = (ys = [y])"
   101 by (blast intro: perm_sing_imp)
   102 
   103 lemma perm_sing_eq2 [iff]: "([y] <~~> ys) = (ys = [y])"
   104 by (blast dest: perm_sym)
   105 
   106 
   107 subsection {* Removing elements *}
   108 
   109 consts
   110   remove :: "'a => 'a list => 'a list"
   111 primrec
   112   "remove x [] = []"
   113   "remove x (y # ys) = (if x = y then ys else y # remove x ys)"
   114 
   115 lemma perm_remove: "x \<in> set ys ==> ys <~~> x # remove x ys"
   116 by (induct ys, auto)
   117 
   118 lemma remove_commute: "remove x (remove y l) = remove y (remove x l)"
   119 by (induct l, auto)
   120 
   121 lemma multiset_of_remove[simp]: 
   122   "multiset_of (remove a x) = multiset_of x - {#a#}"
   123   by (induct_tac x, auto simp: multiset_eq_conv_count_eq) 
   124 
   125 
   126 text {* \medskip Congruence rule *}
   127 
   128 lemma perm_remove_perm: "xs <~~> ys ==> remove z xs <~~> remove z ys"
   129 by (erule perm.induct, auto)
   130 
   131 lemma remove_hd [simp]: "remove z (z # xs) = xs"
   132   by auto
   133 
   134 lemma cons_perm_imp_perm: "z # xs <~~> z # ys ==> xs <~~> ys"
   135 by (drule_tac z = z in perm_remove_perm, auto)
   136 
   137 lemma cons_perm_eq [iff]: "(z#xs <~~> z#ys) = (xs <~~> ys)"
   138 by (blast intro: cons_perm_imp_perm)
   139 
   140 lemma append_perm_imp_perm: "!!xs ys. zs @ xs <~~> zs @ ys ==> xs <~~> ys"
   141   apply (induct zs rule: rev_induct)
   142    apply (simp_all (no_asm_use))
   143   apply blast
   144   done
   145 
   146 lemma perm_append1_eq [iff]: "(zs @ xs <~~> zs @ ys) = (xs <~~> ys)"
   147 by (blast intro: append_perm_imp_perm perm_append1)
   148 
   149 lemma perm_append2_eq [iff]: "(xs @ zs <~~> ys @ zs) = (xs <~~> ys)"
   150   apply (safe intro!: perm_append2)
   151   apply (rule append_perm_imp_perm)
   152   apply (rule perm_append_swap [THEN perm.trans])
   153     -- {* the previous step helps this @{text blast} call succeed quickly *}
   154   apply (blast intro: perm_append_swap)
   155   done
   156 
   157 lemma multiset_of_eq_perm: "(multiset_of xs = multiset_of ys) = (xs <~~> ys) "
   158   apply (rule iffI) 
   159   apply (erule_tac [2] perm.induct, simp_all add: union_ac) 
   160   apply (erule rev_mp, rule_tac x=ys in spec) 
   161   apply (induct_tac xs, auto) 
   162   apply (erule_tac x = "remove a x" in allE, drule sym, simp) 
   163   apply (subgoal_tac "a \<in> set x") 
   164   apply (drule_tac z=a in perm.Cons) 
   165   apply (erule perm.trans, rule perm_sym, erule perm_remove) 
   166   apply (drule_tac f=set_of in arg_cong, simp)
   167   done
   168 
   169 lemma multiset_of_le_perm_append: 
   170   "(multiset_of xs \<le># multiset_of ys) = (\<exists> zs. xs @ zs <~~> ys)"; 
   171   apply (auto simp: multiset_of_eq_perm[THEN sym] mset_le_exists_conv) 
   172   apply (insert surj_multiset_of, drule surjD)
   173   apply (blast intro: sym)+
   174   done
   175 
   176 end