src/HOL/Library/Permutation.thy
 author wenzelm Tue Mar 18 20:33:31 2008 +0100 (2008-03-18) changeset 26316 9e9e67e33557 parent 26072 f65a7fa2da6c child 27368 9f90ac19e32b permissions -rw-r--r--
removed redundant less_trans, less_linear, le_imp_less_or_eq, le_less_trans, less_le_trans (cf. Orderings.thy);
```     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: "[] <~~> ys ==> ys = []"
```
```    26   by (induct xs == "[]::'a list" ys pred: perm) simp_all
```
```    27
```
```    28
```
```    29 text {*
```
```    30   \medskip This more general theorem is easier to understand!
```
```    31   *}
```
```    32
```
```    33 lemma perm_length: "xs <~~> ys ==> length xs = length ys"
```
```    34   by (induct pred: perm) simp_all
```
```    35
```
```    36 lemma perm_empty_imp: "[] <~~> xs ==> xs = []"
```
```    37   by (drule perm_length) auto
```
```    38
```
```    39 lemma perm_sym: "xs <~~> ys ==> ys <~~> xs"
```
```    40   by (induct pred: perm) auto
```
```    41
```
```    42
```
```    43 subsection {* Ways of making new permutations *}
```
```    44
```
```    45 text {*
```
```    46   We can insert the head anywhere in the list.
```
```    47 *}
```
```    48
```
```    49 lemma perm_append_Cons: "a # xs @ ys <~~> xs @ a # ys"
```
```    50   by (induct xs) auto
```
```    51
```
```    52 lemma perm_append_swap: "xs @ ys <~~> ys @ xs"
```
```    53   apply (induct xs)
```
```    54     apply simp_all
```
```    55   apply (blast intro: perm_append_Cons)
```
```    56   done
```
```    57
```
```    58 lemma perm_append_single: "a # xs <~~> xs @ [a]"
```
```    59   by (rule perm.trans [OF _ perm_append_swap]) simp
```
```    60
```
```    61 lemma perm_rev: "rev xs <~~> xs"
```
```    62   apply (induct xs)
```
```    63    apply simp_all
```
```    64   apply (blast intro!: perm_append_single intro: perm_sym)
```
```    65   done
```
```    66
```
```    67 lemma perm_append1: "xs <~~> ys ==> l @ xs <~~> l @ ys"
```
```    68   by (induct l) auto
```
```    69
```
```    70 lemma perm_append2: "xs <~~> ys ==> xs @ l <~~> ys @ l"
```
```    71   by (blast intro!: perm_append_swap perm_append1)
```
```    72
```
```    73
```
```    74 subsection {* Further results *}
```
```    75
```
```    76 lemma perm_empty [iff]: "([] <~~> xs) = (xs = [])"
```
```    77   by (blast intro: perm_empty_imp)
```
```    78
```
```    79 lemma perm_empty2 [iff]: "(xs <~~> []) = (xs = [])"
```
```    80   apply auto
```
```    81   apply (erule perm_sym [THEN perm_empty_imp])
```
```    82   done
```
```    83
```
```    84 lemma perm_sing_imp: "ys <~~> xs ==> xs = [y] ==> ys = [y]"
```
```    85   by (induct pred: perm) auto
```
```    86
```
```    87 lemma perm_sing_eq [iff]: "(ys <~~> [y]) = (ys = [y])"
```
```    88   by (blast intro: perm_sing_imp)
```
```    89
```
```    90 lemma perm_sing_eq2 [iff]: "([y] <~~> ys) = (ys = [y])"
```
```    91   by (blast dest: perm_sym)
```
```    92
```
```    93
```
```    94 subsection {* Removing elements *}
```
```    95
```
```    96 consts
```
```    97   remove :: "'a => 'a list => 'a list"
```
```    98 primrec
```
```    99   "remove x [] = []"
```
```   100   "remove x (y # ys) = (if x = y then ys else y # remove x ys)"
```
```   101
```
```   102 lemma perm_remove: "x \<in> set ys ==> ys <~~> x # remove x ys"
```
```   103   by (induct ys) auto
```
```   104
```
```   105 lemma remove_commute: "remove x (remove y l) = remove y (remove x l)"
```
```   106   by (induct l) auto
```
```   107
```
```   108 lemma multiset_of_remove [simp]:
```
```   109     "multiset_of (remove a x) = multiset_of x - {#a#}"
```
```   110   apply (induct x)
```
```   111    apply (auto simp: multiset_eq_conv_count_eq)
```
```   112   done
```
```   113
```
```   114
```
```   115 text {* \medskip Congruence rule *}
```
```   116
```
```   117 lemma perm_remove_perm: "xs <~~> ys ==> remove z xs <~~> remove z ys"
```
```   118   by (induct pred: perm) auto
```
```   119
```
```   120 lemma remove_hd [simp]: "remove z (z # xs) = xs"
```
```   121   by auto
```
```   122
```
```   123 lemma cons_perm_imp_perm: "z # xs <~~> z # ys ==> xs <~~> ys"
```
```   124   by (drule_tac z = z in perm_remove_perm) auto
```
```   125
```
```   126 lemma cons_perm_eq [iff]: "(z#xs <~~> z#ys) = (xs <~~> ys)"
```
```   127   by (blast intro: cons_perm_imp_perm)
```
```   128
```
```   129 lemma append_perm_imp_perm: "zs @ xs <~~> zs @ ys ==> xs <~~> ys"
```
```   130   apply (induct zs arbitrary: xs ys rule: rev_induct)
```
```   131    apply (simp_all (no_asm_use))
```
```   132   apply blast
```
```   133   done
```
```   134
```
```   135 lemma perm_append1_eq [iff]: "(zs @ xs <~~> zs @ ys) = (xs <~~> ys)"
```
```   136   by (blast intro: append_perm_imp_perm perm_append1)
```
```   137
```
```   138 lemma perm_append2_eq [iff]: "(xs @ zs <~~> ys @ zs) = (xs <~~> ys)"
```
```   139   apply (safe intro!: perm_append2)
```
```   140   apply (rule append_perm_imp_perm)
```
```   141   apply (rule perm_append_swap [THEN perm.trans])
```
```   142     -- {* the previous step helps this @{text blast} call succeed quickly *}
```
```   143   apply (blast intro: perm_append_swap)
```
```   144   done
```
```   145
```
```   146 lemma multiset_of_eq_perm: "(multiset_of xs = multiset_of ys) = (xs <~~> ys) "
```
```   147   apply (rule iffI)
```
```   148   apply (erule_tac [2] perm.induct, simp_all add: union_ac)
```
```   149   apply (erule rev_mp, rule_tac x=ys in spec)
```
```   150   apply (induct_tac xs, auto)
```
```   151   apply (erule_tac x = "remove a x" in allE, drule sym, simp)
```
```   152   apply (subgoal_tac "a \<in> set x")
```
```   153   apply (drule_tac z=a in perm.Cons)
```
```   154   apply (erule perm.trans, rule perm_sym, erule perm_remove)
```
```   155   apply (drule_tac f=set_of in arg_cong, simp)
```
```   156   done
```
```   157
```
```   158 lemma multiset_of_le_perm_append:
```
```   159     "(multiset_of xs \<le># multiset_of ys) = (\<exists>zs. xs @ zs <~~> ys)";
```
```   160   apply (auto simp: multiset_of_eq_perm[THEN sym] mset_le_exists_conv)
```
```   161   apply (insert surj_multiset_of, drule surjD)
```
```   162   apply (blast intro: sym)+
```
```   163   done
```
```   164
```
```   165 lemma perm_set_eq: "xs <~~> ys ==> set xs = set ys"
```
```   166   by (metis multiset_of_eq_perm multiset_of_eq_setD)
```
```   167
```
```   168 lemma perm_distinct_iff: "xs <~~> ys ==> distinct xs = distinct ys"
```
```   169   apply (induct pred: perm)
```
```   170      apply simp_all
```
```   171    apply fastsimp
```
```   172   apply (metis perm_set_eq)
```
```   173   done
```
```   174
```
```   175 lemma eq_set_perm_remdups: "set xs = set ys ==> remdups xs <~~> remdups ys"
```
```   176   apply (induct xs arbitrary: ys rule: length_induct)
```
```   177   apply (case_tac "remdups xs", simp, simp)
```
```   178   apply (subgoal_tac "a : set (remdups ys)")
```
```   179    prefer 2 apply (metis set.simps(2) insert_iff set_remdups)
```
```   180   apply (drule split_list) apply(elim exE conjE)
```
```   181   apply (drule_tac x=list in spec) apply(erule impE) prefer 2
```
```   182    apply (drule_tac x="ysa@zs" in spec) apply(erule impE) prefer 2
```
```   183     apply simp
```
```   184     apply (subgoal_tac "a#list <~~> a#ysa@zs")
```
```   185      apply (metis Cons_eq_appendI perm_append_Cons trans)
```
```   186     apply (metis Cons Cons_eq_appendI distinct.simps(2)
```
```   187       distinct_remdups distinct_remdups_id perm_append_swap perm_distinct_iff)
```
```   188    apply (subgoal_tac "set (a#list) = set (ysa@a#zs) & distinct (a#list) & distinct (ysa@a#zs)")
```
```   189     apply (fastsimp simp add: insert_ident)
```
```   190    apply (metis distinct_remdups set_remdups)
```
```   191   apply (metis le_less_trans Suc_length_conv length_remdups_leq less_Suc_eq nat_less_le)
```
```   192   done
```
```   193
```
```   194 lemma perm_remdups_iff_eq_set: "remdups x <~~> remdups y = (set x = set y)"
```
```   195   by (metis List.set_remdups perm_set_eq eq_set_perm_remdups)
```
```   196
```
```   197 end
```