src/HOL/Library/Permutation.thy
author wenzelm
Thu May 06 14:14:18 2004 +0200 (2004-05-06)
changeset 14706 71590b7733b7
parent 11153 950ede59c05a
child 15005 546c8e7e28d4
permissions -rw-r--r--
tuned document;
     1 (*  Title:      HOL/Library/Permutation.thy
     2     ID:         $Id$
     3     Author:     Lawrence C Paulson and Thomas M Rasmussen
     4     Copyright   1995  University of Cambridge
     5 
     6 TODO: it would be nice to prove (for "multiset", defined on
     7 HOL/ex/Sorting.thy) xs <~~> ys = (\<forall>x. multiset xs x = multiset ys x)
     8 *)
     9 
    10 header {* Permutations *}
    11 
    12 theory Permutation = Main:
    13 
    14 consts
    15   perm :: "('a list * 'a list) set"
    16 
    17 syntax
    18   "_perm" :: "'a list => 'a list => bool"    ("_ <~~> _"  [50, 50] 50)
    19 translations
    20   "x <~~> y" == "(x, y) \<in> perm"
    21 
    22 inductive perm
    23   intros
    24     Nil  [intro!]: "[] <~~> []"
    25     swap [intro!]: "y # x # l <~~> x # y # l"
    26     Cons [intro!]: "xs <~~> ys ==> z # xs <~~> z # ys"
    27     trans [intro]: "xs <~~> ys ==> ys <~~> zs ==> xs <~~> zs"
    28 
    29 lemma perm_refl [iff]: "l <~~> l"
    30   apply (induct l)
    31    apply auto
    32   done
    33 
    34 
    35 subsection {* Some examples of rule induction on permutations *}
    36 
    37 lemma xperm_empty_imp_aux: "xs <~~> ys ==> xs = [] --> ys = []"
    38     -- {* the form of the premise lets the induction bind @{term xs} and @{term ys} *}
    39   apply (erule perm.induct)
    40      apply (simp_all (no_asm_simp))
    41   done
    42 
    43 lemma xperm_empty_imp: "[] <~~> ys ==> ys = []"
    44   apply (insert xperm_empty_imp_aux)
    45   apply blast
    46   done
    47 
    48 
    49 text {*
    50   \medskip This more general theorem is easier to understand!
    51   *}
    52 
    53 lemma perm_length: "xs <~~> ys ==> length xs = length ys"
    54   apply (erule perm.induct)
    55      apply simp_all
    56   done
    57 
    58 lemma perm_empty_imp: "[] <~~> xs ==> xs = []"
    59   apply (drule perm_length)
    60   apply auto
    61   done
    62 
    63 lemma perm_sym: "xs <~~> ys ==> ys <~~> xs"
    64   apply (erule perm.induct)
    65      apply auto
    66   done
    67 
    68 lemma perm_mem [rule_format]: "xs <~~> ys ==> x mem xs --> x mem ys"
    69   apply (erule perm.induct)
    70      apply auto
    71   done
    72 
    73 
    74 subsection {* Ways of making new permutations *}
    75 
    76 text {*
    77   We can insert the head anywhere in the list.
    78 *}
    79 
    80 lemma perm_append_Cons: "a # xs @ ys <~~> xs @ a # ys"
    81   apply (induct xs)
    82    apply auto
    83   done
    84 
    85 lemma perm_append_swap: "xs @ ys <~~> ys @ xs"
    86   apply (induct xs)
    87     apply simp_all
    88   apply (blast intro: perm_append_Cons)
    89   done
    90 
    91 lemma perm_append_single: "a # xs <~~> xs @ [a]"
    92   apply (rule perm.trans)
    93    prefer 2
    94    apply (rule perm_append_swap)
    95   apply simp
    96   done
    97 
    98 lemma perm_rev: "rev xs <~~> xs"
    99   apply (induct xs)
   100    apply simp_all
   101   apply (blast intro!: perm_append_single intro: perm_sym)
   102   done
   103 
   104 lemma perm_append1: "xs <~~> ys ==> l @ xs <~~> l @ ys"
   105   apply (induct l)
   106    apply auto
   107   done
   108 
   109 lemma perm_append2: "xs <~~> ys ==> xs @ l <~~> ys @ l"
   110   apply (blast intro!: perm_append_swap perm_append1)
   111   done
   112 
   113 
   114 subsection {* Further results *}
   115 
   116 lemma perm_empty [iff]: "([] <~~> xs) = (xs = [])"
   117   apply (blast intro: perm_empty_imp)
   118   done
   119 
   120 lemma perm_empty2 [iff]: "(xs <~~> []) = (xs = [])"
   121   apply auto
   122   apply (erule perm_sym [THEN perm_empty_imp])
   123   done
   124 
   125 lemma perm_sing_imp [rule_format]: "ys <~~> xs ==> xs = [y] --> ys = [y]"
   126   apply (erule perm.induct)
   127      apply auto
   128   done
   129 
   130 lemma perm_sing_eq [iff]: "(ys <~~> [y]) = (ys = [y])"
   131   apply (blast intro: perm_sing_imp)
   132   done
   133 
   134 lemma perm_sing_eq2 [iff]: "([y] <~~> ys) = (ys = [y])"
   135   apply (blast dest: perm_sym)
   136   done
   137 
   138 
   139 subsection {* Removing elements *}
   140 
   141 consts
   142   remove :: "'a => 'a list => 'a list"
   143 primrec
   144   "remove x [] = []"
   145   "remove x (y # ys) = (if x = y then ys else y # remove x ys)"
   146 
   147 lemma perm_remove: "x \<in> set ys ==> ys <~~> x # remove x ys"
   148   apply (induct ys)
   149    apply auto
   150   done
   151 
   152 lemma remove_commute: "remove x (remove y l) = remove y (remove x l)"
   153   apply (induct l)
   154    apply auto
   155   done
   156 
   157 
   158 text {* \medskip Congruence rule *}
   159 
   160 lemma perm_remove_perm: "xs <~~> ys ==> remove z xs <~~> remove z ys"
   161   apply (erule perm.induct)
   162      apply auto
   163   done
   164 
   165 lemma remove_hd [simp]: "remove z (z # xs) = xs"
   166   apply auto
   167   done
   168 
   169 lemma cons_perm_imp_perm: "z # xs <~~> z # ys ==> xs <~~> ys"
   170   apply (drule_tac z = z in perm_remove_perm)
   171   apply auto
   172   done
   173 
   174 lemma cons_perm_eq [iff]: "(z#xs <~~> z#ys) = (xs <~~> ys)"
   175   apply (blast intro: cons_perm_imp_perm)
   176   done
   177 
   178 lemma append_perm_imp_perm: "!!xs ys. zs @ xs <~~> zs @ ys ==> xs <~~> ys"
   179   apply (induct zs rule: rev_induct)
   180    apply (simp_all (no_asm_use))
   181   apply blast
   182   done
   183 
   184 lemma perm_append1_eq [iff]: "(zs @ xs <~~> zs @ ys) = (xs <~~> ys)"
   185   apply (blast intro: append_perm_imp_perm perm_append1)
   186   done
   187 
   188 lemma perm_append2_eq [iff]: "(xs @ zs <~~> ys @ zs) = (xs <~~> ys)"
   189   apply (safe intro!: perm_append2)
   190   apply (rule append_perm_imp_perm)
   191   apply (rule perm_append_swap [THEN perm.trans])
   192     -- {* the previous step helps this @{text blast} call succeed quickly *}
   193   apply (blast intro: perm_append_swap)
   194   done
   195 
   196 end