src/HOL/Library/Permutation.thy
 author haftmann Mon Feb 22 09:15:10 2010 +0100 (2010-02-22) changeset 35272 c283ae736bea parent 33498 318acc1c9399 child 36903 489c1fbbb028 permissions -rw-r--r--
switched notations for pointwise and multiset order
```     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 Main 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 primrec remove :: "'a => 'a list => 'a list" where
```
```    97     "remove x [] = []"
```
```    98   | "remove x (y # ys) = (if x = y then ys else y # remove x ys)"
```
```    99
```
```   100 lemma perm_remove: "x \<in> set ys ==> ys <~~> x # remove x ys"
```
```   101   by (induct ys) auto
```
```   102
```
```   103 lemma remove_commute: "remove x (remove y l) = remove y (remove x l)"
```
```   104   by (induct l) auto
```
```   105
```
```   106 lemma multiset_of_remove [simp]:
```
```   107     "multiset_of (remove a x) = multiset_of x - {#a#}"
```
```   108   apply (induct x)
```
```   109    apply (auto simp: multiset_eq_conv_count_eq)
```
```   110   done
```
```   111
```
```   112
```
```   113 text {* \medskip Congruence rule *}
```
```   114
```
```   115 lemma perm_remove_perm: "xs <~~> ys ==> remove z xs <~~> remove z ys"
```
```   116   by (induct pred: perm) auto
```
```   117
```
```   118 lemma remove_hd [simp]: "remove z (z # xs) = xs"
```
```   119   by auto
```
```   120
```
```   121 lemma cons_perm_imp_perm: "z # xs <~~> z # ys ==> xs <~~> ys"
```
```   122   by (drule_tac z = z in perm_remove_perm) auto
```
```   123
```
```   124 lemma cons_perm_eq [iff]: "(z#xs <~~> z#ys) = (xs <~~> ys)"
```
```   125   by (blast intro: cons_perm_imp_perm)
```
```   126
```
```   127 lemma append_perm_imp_perm: "zs @ xs <~~> zs @ ys ==> xs <~~> ys"
```
```   128   apply (induct zs arbitrary: xs ys rule: rev_induct)
```
```   129    apply (simp_all (no_asm_use))
```
```   130   apply blast
```
```   131   done
```
```   132
```
```   133 lemma perm_append1_eq [iff]: "(zs @ xs <~~> zs @ ys) = (xs <~~> ys)"
```
```   134   by (blast intro: append_perm_imp_perm perm_append1)
```
```   135
```
```   136 lemma perm_append2_eq [iff]: "(xs @ zs <~~> ys @ zs) = (xs <~~> ys)"
```
```   137   apply (safe intro!: perm_append2)
```
```   138   apply (rule append_perm_imp_perm)
```
```   139   apply (rule perm_append_swap [THEN perm.trans])
```
```   140     -- {* the previous step helps this @{text blast} call succeed quickly *}
```
```   141   apply (blast intro: perm_append_swap)
```
```   142   done
```
```   143
```
```   144 lemma multiset_of_eq_perm: "(multiset_of xs = multiset_of ys) = (xs <~~> ys) "
```
```   145   apply (rule iffI)
```
```   146   apply (erule_tac [2] perm.induct, simp_all add: union_ac)
```
```   147   apply (erule rev_mp, rule_tac x=ys in spec)
```
```   148   apply (induct_tac xs, auto)
```
```   149   apply (erule_tac x = "remove a x" in allE, drule sym, simp)
```
```   150   apply (subgoal_tac "a \<in> set x")
```
```   151   apply (drule_tac z=a in perm.Cons)
```
```   152   apply (erule perm.trans, rule perm_sym, erule perm_remove)
```
```   153   apply (drule_tac f=set_of in arg_cong, simp)
```
```   154   done
```
```   155
```
```   156 lemma multiset_of_le_perm_append:
```
```   157     "multiset_of xs \<le> multiset_of ys \<longleftrightarrow> (\<exists>zs. xs @ zs <~~> ys)"
```
```   158   apply (auto simp: multiset_of_eq_perm[THEN sym] mset_le_exists_conv)
```
```   159   apply (insert surj_multiset_of, drule surjD)
```
```   160   apply (blast intro: sym)+
```
```   161   done
```
```   162
```
```   163 lemma perm_set_eq: "xs <~~> ys ==> set xs = set ys"
```
```   164   by (metis multiset_of_eq_perm multiset_of_eq_setD)
```
```   165
```
```   166 lemma perm_distinct_iff: "xs <~~> ys ==> distinct xs = distinct ys"
```
```   167   apply (induct pred: perm)
```
```   168      apply simp_all
```
```   169    apply fastsimp
```
```   170   apply (metis perm_set_eq)
```
```   171   done
```
```   172
```
```   173 lemma eq_set_perm_remdups: "set xs = set ys ==> remdups xs <~~> remdups ys"
```
```   174   apply (induct xs arbitrary: ys rule: length_induct)
```
```   175   apply (case_tac "remdups xs", simp, simp)
```
```   176   apply (subgoal_tac "a : set (remdups ys)")
```
```   177    prefer 2 apply (metis set.simps(2) insert_iff set_remdups)
```
```   178   apply (drule split_list) apply(elim exE conjE)
```
```   179   apply (drule_tac x=list in spec) apply(erule impE) prefer 2
```
```   180    apply (drule_tac x="ysa@zs" in spec) apply(erule impE) prefer 2
```
```   181     apply simp
```
```   182     apply (subgoal_tac "a#list <~~> a#ysa@zs")
```
```   183      apply (metis Cons_eq_appendI perm_append_Cons trans)
```
```   184     apply (metis Cons Cons_eq_appendI distinct.simps(2)
```
```   185       distinct_remdups distinct_remdups_id perm_append_swap perm_distinct_iff)
```
```   186    apply (subgoal_tac "set (a#list) = set (ysa@a#zs) & distinct (a#list) & distinct (ysa@a#zs)")
```
```   187     apply (fastsimp simp add: insert_ident)
```
```   188    apply (metis distinct_remdups set_remdups)
```
```   189    apply (subgoal_tac "length (remdups xs) < Suc (length xs)")
```
```   190    apply simp
```
```   191    apply (subgoal_tac "length (remdups xs) \<le> length xs")
```
```   192    apply simp
```
```   193    apply (rule length_remdups_leq)
```
```   194   done
```
```   195
```
```   196 lemma perm_remdups_iff_eq_set: "remdups x <~~> remdups y = (set x = set y)"
```
```   197   by (metis List.set_remdups perm_set_eq eq_set_perm_remdups)
```
```   198
```
```   199 end
```