src/HOL/Library/Permutation.thy
 author paulson Thu Jun 24 17:54:53 2004 +0200 (2004-06-24) changeset 15005 546c8e7e28d4 parent 14706 71590b7733b7 child 15072 4861bf6af0b4 permissions -rw-r--r--
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} and @{term ys} *}
```
```    32   apply (erule perm.induct)
```
```    33      apply (simp_all (no_asm_simp))
```
```    34   done
```
```    35
```
```    36 lemma xperm_empty_imp: "[] <~~> ys ==> ys = []"
```
```    37 by (insert xperm_empty_imp_aux, blast)
```
```    38
```
```    39
```
```    40 text {*
```
```    41   \medskip This more general theorem is easier to understand!
```
```    42   *}
```
```    43
```
```    44 lemma perm_length: "xs <~~> ys ==> length xs = length ys"
```
```    45 by (erule perm.induct, simp_all)
```
```    46
```
```    47 lemma perm_empty_imp: "[] <~~> xs ==> xs = []"
```
```    48 by (drule perm_length, auto)
```
```    49
```
```    50 lemma perm_sym: "xs <~~> ys ==> ys <~~> xs"
```
```    51 by (erule perm.induct, auto)
```
```    52
```
```    53 lemma perm_mem [rule_format]: "xs <~~> ys ==> x mem xs --> x mem ys"
```
```    54 by (erule perm.induct, auto)
```
```    55
```
```    56
```
```    57 subsection {* Ways of making new permutations *}
```
```    58
```
```    59 text {*
```
```    60   We can insert the head anywhere in the list.
```
```    61 *}
```
```    62
```
```    63 lemma perm_append_Cons: "a # xs @ ys <~~> xs @ a # ys"
```
```    64 by (induct xs, auto)
```
```    65
```
```    66 lemma perm_append_swap: "xs @ ys <~~> ys @ xs"
```
```    67   apply (induct xs, simp_all)
```
```    68   apply (blast intro: perm_append_Cons)
```
```    69   done
```
```    70
```
```    71 lemma perm_append_single: "a # xs <~~> xs @ [a]"
```
```    72   apply (rule perm.trans)
```
```    73    prefer 2
```
```    74    apply (rule perm_append_swap, simp)
```
```    75   done
```
```    76
```
```    77 lemma perm_rev: "rev xs <~~> xs"
```
```    78   apply (induct xs, simp_all)
```
```    79   apply (blast intro!: perm_append_single intro: perm_sym)
```
```    80   done
```
```    81
```
```    82 lemma perm_append1: "xs <~~> ys ==> l @ xs <~~> l @ ys"
```
```    83 by (induct l, auto)
```
```    84
```
```    85 lemma perm_append2: "xs <~~> ys ==> xs @ l <~~> ys @ l"
```
```    86 by (blast intro!: perm_append_swap perm_append1)
```
```    87
```
```    88
```
```    89 subsection {* Further results *}
```
```    90
```
```    91 lemma perm_empty [iff]: "([] <~~> xs) = (xs = [])"
```
```    92 by (blast intro: perm_empty_imp)
```
```    93
```
```    94 lemma perm_empty2 [iff]: "(xs <~~> []) = (xs = [])"
```
```    95   apply auto
```
```    96   apply (erule perm_sym [THEN perm_empty_imp])
```
```    97   done
```
```    98
```
```    99 lemma perm_sing_imp [rule_format]: "ys <~~> xs ==> xs = [y] --> ys = [y]"
```
```   100 by (erule perm.induct, auto)
```
```   101
```
```   102 lemma perm_sing_eq [iff]: "(ys <~~> [y]) = (ys = [y])"
```
```   103 by (blast intro: perm_sing_imp)
```
```   104
```
```   105 lemma perm_sing_eq2 [iff]: "([y] <~~> ys) = (ys = [y])"
```
```   106 by (blast dest: perm_sym)
```
```   107
```
```   108
```
```   109 subsection {* Removing elements *}
```
```   110
```
```   111 consts
```
```   112   remove :: "'a => 'a list => 'a list"
```
```   113 primrec
```
```   114   "remove x [] = []"
```
```   115   "remove x (y # ys) = (if x = y then ys else y # remove x ys)"
```
```   116
```
```   117 lemma perm_remove: "x \<in> set ys ==> ys <~~> x # remove x ys"
```
```   118 by (induct ys, auto)
```
```   119
```
```   120 lemma remove_commute: "remove x (remove y l) = remove y (remove x l)"
```
```   121 by (induct l, auto)
```
```   122
```
```   123
```
```   124 text {* \medskip Congruence rule *}
```
```   125
```
```   126 lemma perm_remove_perm: "xs <~~> ys ==> remove z xs <~~> remove z ys"
```
```   127 by (erule perm.induct, auto)
```
```   128
```
```   129 lemma remove_hd [simp]: "remove z (z # xs) = xs"
```
```   130   apply auto
```
```   131   done
```
```   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 (****************** Norbert Voelker 17 June 2004 **************)
```
```   157
```
```   158 consts
```
```   159   multiset_of :: "'a list \<Rightarrow> 'a multiset"
```
```   160 primrec
```
```   161   "multiset_of [] = {#}"
```
```   162   "multiset_of (a # x) = multiset_of x + {# a #}"
```
```   163
```
```   164 lemma multiset_of_zero_iff[simp]: "(multiset_of x = {#}) = (x = [])"
```
```   165   by (induct_tac x, auto)
```
```   166
```
```   167 lemma multiset_of_zero_iff_right[simp]: "({#} = multiset_of x) = (x = [])"
```
```   168   by (induct_tac x, auto)
```
```   169
```
```   170 lemma set_of_multiset_of[simp]: "set_of(multiset_of x) = set x"
```
```   171  by (induct_tac x, auto)
```
```   172
```
```   173 lemma multiset_of_remove[simp]:
```
```   174   "multiset_of (remove a x) = multiset_of x - {#a#}"
```
```   175   by (induct_tac x, auto simp: multiset_eq_conv_count_eq)
```
```   176
```
```   177 lemma multiset_of_eq_perm:  "(multiset_of xs = multiset_of ys) = (xs <~~> ys) "
```
```   178   apply (rule iffI)
```
```   179   apply (erule_tac  perm.induct, simp_all add: union_ac)
```
```   180   apply (erule rev_mp, rule_tac x=ys in spec, induct_tac xs, auto)
```
```   181   apply (erule_tac x = "remove a x" in allE, drule sym, simp)
```
```   182   apply (subgoal_tac "a \<in> set x")
```
```   183   apply (drule_tac z=a in perm.Cons)
```
```   184   apply (erule perm.trans, rule perm_sym, erule perm_remove)
```
```   185   apply (drule_tac f=set_of in arg_cong, simp)
```
```   186   done
```
```   187
```
```   188 lemma set_count_multiset_of: "set x = {a. 0 < count (multiset_of x) a}"
```
```   189   by (induct_tac x, auto)
```
```   190
```
```   191 lemma distinct_count_multiset_of:
```
```   192    "distinct x \<Longrightarrow> count (multiset_of x) a = (if a \<in> set x then 1 else 0)"
```
```   193   by (erule rev_mp, induct_tac x, auto)
```
```   194
```
```   195 lemma distinct_set_eq_iff_multiset_of_eq:
```
```   196   "\<lbrakk>distinct x; distinct y\<rbrakk> \<Longrightarrow> (set x = set y) = (multiset_of x = multiset_of y)"
```
```   197   by (auto simp: multiset_eq_conv_count_eq distinct_count_multiset_of)
```
```   198
```
```   199 end
```