src/HOL/Library/Permutation.thy
author berghofe
Wed Jul 11 11:28:13 2007 +0200 (2007-07-11)
changeset 23755 1c4672d130b1
parent 21404 eb85850d3eb7
child 25277 95128fcdd7e8
permissions -rw-r--r--
Adapted to new inductive definition package.
     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 lemma perm_mem [rule_format]: "xs <~~> ys ==> x mem xs --> x mem ys"
    50   by (erule perm.induct) auto
    51 
    52 
    53 subsection {* Ways of making new permutations *}
    54 
    55 text {*
    56   We can insert the head anywhere in the list.
    57 *}
    58 
    59 lemma perm_append_Cons: "a # xs @ ys <~~> xs @ a # ys"
    60   by (induct xs) auto
    61 
    62 lemma perm_append_swap: "xs @ ys <~~> ys @ xs"
    63   apply (induct xs)
    64     apply simp_all
    65   apply (blast intro: perm_append_Cons)
    66   done
    67 
    68 lemma perm_append_single: "a # xs <~~> xs @ [a]"
    69   by (rule perm.trans [OF _ perm_append_swap]) simp
    70 
    71 lemma perm_rev: "rev xs <~~> xs"
    72   apply (induct xs)
    73    apply simp_all
    74   apply (blast intro!: perm_append_single intro: perm_sym)
    75   done
    76 
    77 lemma perm_append1: "xs <~~> ys ==> l @ xs <~~> l @ ys"
    78   by (induct l) auto
    79 
    80 lemma perm_append2: "xs <~~> ys ==> xs @ l <~~> ys @ l"
    81   by (blast intro!: perm_append_swap perm_append1)
    82 
    83 
    84 subsection {* Further results *}
    85 
    86 lemma perm_empty [iff]: "([] <~~> xs) = (xs = [])"
    87   by (blast intro: perm_empty_imp)
    88 
    89 lemma perm_empty2 [iff]: "(xs <~~> []) = (xs = [])"
    90   apply auto
    91   apply (erule perm_sym [THEN perm_empty_imp])
    92   done
    93 
    94 lemma perm_sing_imp [rule_format]: "ys <~~> xs ==> xs = [y] --> ys = [y]"
    95   by (erule perm.induct) auto
    96 
    97 lemma perm_sing_eq [iff]: "(ys <~~> [y]) = (ys = [y])"
    98   by (blast intro: perm_sing_imp)
    99 
   100 lemma perm_sing_eq2 [iff]: "([y] <~~> ys) = (ys = [y])"
   101   by (blast dest: perm_sym)
   102 
   103 
   104 subsection {* Removing elements *}
   105 
   106 consts
   107   remove :: "'a => 'a list => 'a list"
   108 primrec
   109   "remove x [] = []"
   110   "remove x (y # ys) = (if x = y then ys else y # remove x ys)"
   111 
   112 lemma perm_remove: "x \<in> set ys ==> ys <~~> x # remove x ys"
   113   by (induct ys) auto
   114 
   115 lemma remove_commute: "remove x (remove y l) = remove y (remove x l)"
   116   by (induct l) auto
   117 
   118 lemma multiset_of_remove[simp]:
   119     "multiset_of (remove a x) = multiset_of x - {#a#}"
   120   apply (induct x)
   121    apply (auto simp: multiset_eq_conv_count_eq)
   122   done
   123 
   124 
   125 text {* \medskip Congruence rule *}
   126 
   127 lemma perm_remove_perm: "xs <~~> ys ==> remove z xs <~~> remove z ys"
   128   by (erule perm.induct) auto
   129 
   130 lemma remove_hd [simp]: "remove z (z # xs) = xs"
   131   by auto
   132 
   133 lemma cons_perm_imp_perm: "z # xs <~~> z # ys ==> xs <~~> ys"
   134   by (drule_tac z = z in perm_remove_perm) auto
   135 
   136 lemma cons_perm_eq [iff]: "(z#xs <~~> z#ys) = (xs <~~> ys)"
   137   by (blast intro: cons_perm_imp_perm)
   138 
   139 lemma append_perm_imp_perm: "!!xs ys. zs @ xs <~~> zs @ ys ==> xs <~~> ys"
   140   apply (induct zs rule: rev_induct)
   141    apply (simp_all (no_asm_use))
   142   apply blast
   143   done
   144 
   145 lemma perm_append1_eq [iff]: "(zs @ xs <~~> zs @ ys) = (xs <~~> ys)"
   146   by (blast intro: append_perm_imp_perm perm_append1)
   147 
   148 lemma perm_append2_eq [iff]: "(xs @ zs <~~> ys @ zs) = (xs <~~> ys)"
   149   apply (safe intro!: perm_append2)
   150   apply (rule append_perm_imp_perm)
   151   apply (rule perm_append_swap [THEN perm.trans])
   152     -- {* the previous step helps this @{text blast} call succeed quickly *}
   153   apply (blast intro: perm_append_swap)
   154   done
   155 
   156 lemma multiset_of_eq_perm: "(multiset_of xs = multiset_of ys) = (xs <~~> ys) "
   157   apply (rule iffI)
   158   apply (erule_tac [2] perm.induct, simp_all add: union_ac)
   159   apply (erule rev_mp, rule_tac x=ys in spec)
   160   apply (induct_tac xs, auto)
   161   apply (erule_tac x = "remove a x" in allE, drule sym, simp)
   162   apply (subgoal_tac "a \<in> set x")
   163   apply (drule_tac z=a in perm.Cons)
   164   apply (erule perm.trans, rule perm_sym, erule perm_remove)
   165   apply (drule_tac f=set_of in arg_cong, simp)
   166   done
   167 
   168 lemma multiset_of_le_perm_append:
   169     "(multiset_of xs \<le># multiset_of ys) = (\<exists>zs. xs @ zs <~~> ys)";
   170   apply (auto simp: multiset_of_eq_perm[THEN sym] mset_le_exists_conv)
   171   apply (insert surj_multiset_of, drule surjD)
   172   apply (blast intro: sym)+
   173   done
   174 
   175 end