src/HOL/Library/Permutation.thy
author paulson
Thu Jul 22 10:33:26 2004 +0200 (2004-07-22)
changeset 15072 4861bf6af0b4
parent 15005 546c8e7e28d4
child 15131 c69542757a4d
permissions -rw-r--r--
new material courtesy of Norbert Voelker
wenzelm@11054
     1
(*  Title:      HOL/Library/Permutation.thy
paulson@15005
     2
    Author:     Lawrence C Paulson and Thomas M Rasmussen and Norbert Voelker
wenzelm@11054
     3
*)
wenzelm@11054
     4
wenzelm@14706
     5
header {* Permutations *}
wenzelm@11054
     6
paulson@15005
     7
theory Permutation = Multiset:
wenzelm@11054
     8
wenzelm@11054
     9
consts
wenzelm@11054
    10
  perm :: "('a list * 'a list) set"
wenzelm@11054
    11
wenzelm@11054
    12
syntax
wenzelm@11054
    13
  "_perm" :: "'a list => 'a list => bool"    ("_ <~~> _"  [50, 50] 50)
wenzelm@11054
    14
translations
wenzelm@11054
    15
  "x <~~> y" == "(x, y) \<in> perm"
wenzelm@11054
    16
wenzelm@11054
    17
inductive perm
paulson@11153
    18
  intros
paulson@11153
    19
    Nil  [intro!]: "[] <~~> []"
paulson@11153
    20
    swap [intro!]: "y # x # l <~~> x # y # l"
paulson@11153
    21
    Cons [intro!]: "xs <~~> ys ==> z # xs <~~> z # ys"
paulson@11153
    22
    trans [intro]: "xs <~~> ys ==> ys <~~> zs ==> xs <~~> zs"
wenzelm@11054
    23
wenzelm@11054
    24
lemma perm_refl [iff]: "l <~~> l"
paulson@15005
    25
by (induct l, auto)
wenzelm@11054
    26
wenzelm@11054
    27
wenzelm@11054
    28
subsection {* Some examples of rule induction on permutations *}
wenzelm@11054
    29
wenzelm@11054
    30
lemma xperm_empty_imp_aux: "xs <~~> ys ==> xs = [] --> ys = []"
paulson@15072
    31
    -- {*the form of the premise lets the induction bind @{term xs} 
paulson@15072
    32
         and @{term ys} *}
wenzelm@11054
    33
  apply (erule perm.induct)
wenzelm@11054
    34
     apply (simp_all (no_asm_simp))
wenzelm@11054
    35
  done
wenzelm@11054
    36
wenzelm@11054
    37
lemma xperm_empty_imp: "[] <~~> ys ==> ys = []"
paulson@15005
    38
by (insert xperm_empty_imp_aux, blast)
wenzelm@11054
    39
wenzelm@11054
    40
wenzelm@11054
    41
text {*
wenzelm@11054
    42
  \medskip This more general theorem is easier to understand!
wenzelm@11054
    43
  *}
wenzelm@11054
    44
wenzelm@11054
    45
lemma perm_length: "xs <~~> ys ==> length xs = length ys"
paulson@15005
    46
by (erule perm.induct, simp_all)
wenzelm@11054
    47
wenzelm@11054
    48
lemma perm_empty_imp: "[] <~~> xs ==> xs = []"
paulson@15005
    49
by (drule perm_length, auto)
wenzelm@11054
    50
wenzelm@11054
    51
lemma perm_sym: "xs <~~> ys ==> ys <~~> xs"
paulson@15005
    52
by (erule perm.induct, auto)
wenzelm@11054
    53
wenzelm@11054
    54
lemma perm_mem [rule_format]: "xs <~~> ys ==> x mem xs --> x mem ys"
paulson@15005
    55
by (erule perm.induct, auto)
wenzelm@11054
    56
wenzelm@11054
    57
wenzelm@11054
    58
subsection {* Ways of making new permutations *}
wenzelm@11054
    59
wenzelm@11054
    60
text {*
wenzelm@11054
    61
  We can insert the head anywhere in the list.
wenzelm@11054
    62
*}
wenzelm@11054
    63
wenzelm@11054
    64
lemma perm_append_Cons: "a # xs @ ys <~~> xs @ a # ys"
paulson@15005
    65
by (induct xs, auto)
wenzelm@11054
    66
wenzelm@11054
    67
lemma perm_append_swap: "xs @ ys <~~> ys @ xs"
paulson@15005
    68
  apply (induct xs, simp_all)
wenzelm@11054
    69
  apply (blast intro: perm_append_Cons)
wenzelm@11054
    70
  done
wenzelm@11054
    71
wenzelm@11054
    72
lemma perm_append_single: "a # xs <~~> xs @ [a]"
paulson@15072
    73
  by (rule perm.trans [OF _ perm_append_swap], simp)
wenzelm@11054
    74
wenzelm@11054
    75
lemma perm_rev: "rev xs <~~> xs"
paulson@15005
    76
  apply (induct xs, simp_all)
paulson@11153
    77
  apply (blast intro!: perm_append_single intro: perm_sym)
wenzelm@11054
    78
  done
wenzelm@11054
    79
wenzelm@11054
    80
lemma perm_append1: "xs <~~> ys ==> l @ xs <~~> l @ ys"
paulson@15005
    81
by (induct l, auto)
wenzelm@11054
    82
wenzelm@11054
    83
lemma perm_append2: "xs <~~> ys ==> xs @ l <~~> ys @ l"
paulson@15005
    84
by (blast intro!: perm_append_swap perm_append1)
wenzelm@11054
    85
wenzelm@11054
    86
wenzelm@11054
    87
subsection {* Further results *}
wenzelm@11054
    88
wenzelm@11054
    89
lemma perm_empty [iff]: "([] <~~> xs) = (xs = [])"
paulson@15005
    90
by (blast intro: perm_empty_imp)
wenzelm@11054
    91
wenzelm@11054
    92
lemma perm_empty2 [iff]: "(xs <~~> []) = (xs = [])"
wenzelm@11054
    93
  apply auto
wenzelm@11054
    94
  apply (erule perm_sym [THEN perm_empty_imp])
wenzelm@11054
    95
  done
wenzelm@11054
    96
wenzelm@11054
    97
lemma perm_sing_imp [rule_format]: "ys <~~> xs ==> xs = [y] --> ys = [y]"
paulson@15005
    98
by (erule perm.induct, auto)
wenzelm@11054
    99
wenzelm@11054
   100
lemma perm_sing_eq [iff]: "(ys <~~> [y]) = (ys = [y])"
paulson@15005
   101
by (blast intro: perm_sing_imp)
wenzelm@11054
   102
wenzelm@11054
   103
lemma perm_sing_eq2 [iff]: "([y] <~~> ys) = (ys = [y])"
paulson@15005
   104
by (blast dest: perm_sym)
wenzelm@11054
   105
wenzelm@11054
   106
wenzelm@11054
   107
subsection {* Removing elements *}
wenzelm@11054
   108
wenzelm@11054
   109
consts
wenzelm@11054
   110
  remove :: "'a => 'a list => 'a list"
wenzelm@11054
   111
primrec
wenzelm@11054
   112
  "remove x [] = []"
wenzelm@11054
   113
  "remove x (y # ys) = (if x = y then ys else y # remove x ys)"
wenzelm@11054
   114
wenzelm@11054
   115
lemma perm_remove: "x \<in> set ys ==> ys <~~> x # remove x ys"
paulson@15005
   116
by (induct ys, auto)
wenzelm@11054
   117
wenzelm@11054
   118
lemma remove_commute: "remove x (remove y l) = remove y (remove x l)"
paulson@15005
   119
by (induct l, auto)
wenzelm@11054
   120
paulson@15072
   121
lemma multiset_of_remove[simp]: 
paulson@15072
   122
  "multiset_of (remove a x) = multiset_of x - {#a#}"
paulson@15072
   123
  by (induct_tac x, auto simp: multiset_eq_conv_count_eq) 
paulson@15072
   124
wenzelm@11054
   125
wenzelm@11054
   126
text {* \medskip Congruence rule *}
wenzelm@11054
   127
wenzelm@11054
   128
lemma perm_remove_perm: "xs <~~> ys ==> remove z xs <~~> remove z ys"
paulson@15005
   129
by (erule perm.induct, auto)
wenzelm@11054
   130
wenzelm@11054
   131
lemma remove_hd [simp]: "remove z (z # xs) = xs"
paulson@15072
   132
  by auto
wenzelm@11054
   133
wenzelm@11054
   134
lemma cons_perm_imp_perm: "z # xs <~~> z # ys ==> xs <~~> ys"
paulson@15005
   135
by (drule_tac z = z in perm_remove_perm, auto)
wenzelm@11054
   136
wenzelm@11054
   137
lemma cons_perm_eq [iff]: "(z#xs <~~> z#ys) = (xs <~~> ys)"
paulson@15005
   138
by (blast intro: cons_perm_imp_perm)
wenzelm@11054
   139
wenzelm@11054
   140
lemma append_perm_imp_perm: "!!xs ys. zs @ xs <~~> zs @ ys ==> xs <~~> ys"
wenzelm@11054
   141
  apply (induct zs rule: rev_induct)
wenzelm@11054
   142
   apply (simp_all (no_asm_use))
wenzelm@11054
   143
  apply blast
wenzelm@11054
   144
  done
wenzelm@11054
   145
wenzelm@11054
   146
lemma perm_append1_eq [iff]: "(zs @ xs <~~> zs @ ys) = (xs <~~> ys)"
paulson@15005
   147
by (blast intro: append_perm_imp_perm perm_append1)
wenzelm@11054
   148
wenzelm@11054
   149
lemma perm_append2_eq [iff]: "(xs @ zs <~~> ys @ zs) = (xs <~~> ys)"
wenzelm@11054
   150
  apply (safe intro!: perm_append2)
wenzelm@11054
   151
  apply (rule append_perm_imp_perm)
wenzelm@11054
   152
  apply (rule perm_append_swap [THEN perm.trans])
wenzelm@11054
   153
    -- {* the previous step helps this @{text blast} call succeed quickly *}
wenzelm@11054
   154
  apply (blast intro: perm_append_swap)
wenzelm@11054
   155
  done
wenzelm@11054
   156
paulson@15072
   157
lemma multiset_of_eq_perm: "(multiset_of xs = multiset_of ys) = (xs <~~> ys) "
paulson@15005
   158
  apply (rule iffI) 
paulson@15005
   159
  apply (erule_tac [2] perm.induct, simp_all add: union_ac) 
paulson@15072
   160
  apply (erule rev_mp, rule_tac x=ys in spec) 
paulson@15072
   161
  apply (induct_tac xs, auto) 
paulson@15005
   162
  apply (erule_tac x = "remove a x" in allE, drule sym, simp) 
paulson@15005
   163
  apply (subgoal_tac "a \<in> set x") 
paulson@15005
   164
  apply (drule_tac z=a in perm.Cons) 
paulson@15005
   165
  apply (erule perm.trans, rule perm_sym, erule perm_remove) 
paulson@15005
   166
  apply (drule_tac f=set_of in arg_cong, simp)
paulson@15005
   167
  done
paulson@15005
   168
paulson@15072
   169
lemma multiset_of_le_perm_append: 
paulson@15072
   170
  "(multiset_of xs \<le># multiset_of ys) = (\<exists> zs. xs @ zs <~~> ys)"; 
paulson@15072
   171
  apply (auto simp: multiset_of_eq_perm[THEN sym] mset_le_exists_conv) 
paulson@15072
   172
  apply (insert surj_multiset_of, drule surjD)
paulson@15072
   173
  apply (blast intro: sym)+
paulson@15072
   174
  done
paulson@15005
   175
wenzelm@11054
   176
end