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