src/HOL/Library/Permutation.thy
author wenzelm
Tue Mar 18 20:33:31 2008 +0100 (2008-03-18)
changeset 26316 9e9e67e33557
parent 26072 f65a7fa2da6c
child 27368 9f90ac19e32b
permissions -rw-r--r--
removed redundant less_trans, less_linear, le_imp_less_or_eq, le_less_trans, less_le_trans (cf. Orderings.thy);
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: "[] <~~> ys ==> ys = []"
wenzelm@25379
    26
  by (induct xs == "[]::'a list" ys pred: perm) simp_all
wenzelm@11054
    27
wenzelm@11054
    28
wenzelm@11054
    29
text {*
wenzelm@11054
    30
  \medskip This more general theorem is easier to understand!
wenzelm@11054
    31
  *}
wenzelm@11054
    32
wenzelm@11054
    33
lemma perm_length: "xs <~~> ys ==> length xs = length ys"
wenzelm@25379
    34
  by (induct pred: perm) simp_all
wenzelm@11054
    35
wenzelm@11054
    36
lemma perm_empty_imp: "[] <~~> xs ==> xs = []"
wenzelm@17200
    37
  by (drule perm_length) auto
wenzelm@11054
    38
wenzelm@11054
    39
lemma perm_sym: "xs <~~> ys ==> ys <~~> xs"
wenzelm@25379
    40
  by (induct pred: perm) auto
wenzelm@11054
    41
wenzelm@11054
    42
wenzelm@11054
    43
subsection {* Ways of making new permutations *}
wenzelm@11054
    44
wenzelm@11054
    45
text {*
wenzelm@11054
    46
  We can insert the head anywhere in the list.
wenzelm@11054
    47
*}
wenzelm@11054
    48
wenzelm@11054
    49
lemma perm_append_Cons: "a # xs @ ys <~~> xs @ a # ys"
wenzelm@17200
    50
  by (induct xs) auto
wenzelm@11054
    51
wenzelm@11054
    52
lemma perm_append_swap: "xs @ ys <~~> ys @ xs"
wenzelm@17200
    53
  apply (induct xs)
wenzelm@17200
    54
    apply simp_all
wenzelm@11054
    55
  apply (blast intro: perm_append_Cons)
wenzelm@11054
    56
  done
wenzelm@11054
    57
wenzelm@11054
    58
lemma perm_append_single: "a # xs <~~> xs @ [a]"
wenzelm@17200
    59
  by (rule perm.trans [OF _ perm_append_swap]) simp
wenzelm@11054
    60
wenzelm@11054
    61
lemma perm_rev: "rev xs <~~> xs"
wenzelm@17200
    62
  apply (induct xs)
wenzelm@17200
    63
   apply simp_all
paulson@11153
    64
  apply (blast intro!: perm_append_single intro: perm_sym)
wenzelm@11054
    65
  done
wenzelm@11054
    66
wenzelm@11054
    67
lemma perm_append1: "xs <~~> ys ==> l @ xs <~~> l @ ys"
wenzelm@17200
    68
  by (induct l) auto
wenzelm@11054
    69
wenzelm@11054
    70
lemma perm_append2: "xs <~~> ys ==> xs @ l <~~> ys @ l"
wenzelm@17200
    71
  by (blast intro!: perm_append_swap perm_append1)
wenzelm@11054
    72
wenzelm@11054
    73
wenzelm@11054
    74
subsection {* Further results *}
wenzelm@11054
    75
wenzelm@11054
    76
lemma perm_empty [iff]: "([] <~~> xs) = (xs = [])"
wenzelm@17200
    77
  by (blast intro: perm_empty_imp)
wenzelm@11054
    78
wenzelm@11054
    79
lemma perm_empty2 [iff]: "(xs <~~> []) = (xs = [])"
wenzelm@11054
    80
  apply auto
wenzelm@11054
    81
  apply (erule perm_sym [THEN perm_empty_imp])
wenzelm@11054
    82
  done
wenzelm@11054
    83
wenzelm@25379
    84
lemma perm_sing_imp: "ys <~~> xs ==> xs = [y] ==> ys = [y]"
wenzelm@25379
    85
  by (induct pred: perm) auto
wenzelm@11054
    86
wenzelm@11054
    87
lemma perm_sing_eq [iff]: "(ys <~~> [y]) = (ys = [y])"
wenzelm@17200
    88
  by (blast intro: perm_sing_imp)
wenzelm@11054
    89
wenzelm@11054
    90
lemma perm_sing_eq2 [iff]: "([y] <~~> ys) = (ys = [y])"
wenzelm@17200
    91
  by (blast dest: perm_sym)
wenzelm@11054
    92
wenzelm@11054
    93
wenzelm@11054
    94
subsection {* Removing elements *}
wenzelm@11054
    95
wenzelm@11054
    96
consts
wenzelm@11054
    97
  remove :: "'a => 'a list => 'a list"
wenzelm@11054
    98
primrec
wenzelm@11054
    99
  "remove x [] = []"
wenzelm@11054
   100
  "remove x (y # ys) = (if x = y then ys else y # remove x ys)"
wenzelm@11054
   101
wenzelm@11054
   102
lemma perm_remove: "x \<in> set ys ==> ys <~~> x # remove x ys"
wenzelm@17200
   103
  by (induct ys) auto
wenzelm@11054
   104
wenzelm@11054
   105
lemma remove_commute: "remove x (remove y l) = remove y (remove x l)"
wenzelm@17200
   106
  by (induct l) auto
wenzelm@11054
   107
wenzelm@25379
   108
lemma multiset_of_remove [simp]:
wenzelm@17200
   109
    "multiset_of (remove a x) = multiset_of x - {#a#}"
wenzelm@17200
   110
  apply (induct x)
wenzelm@17200
   111
   apply (auto simp: multiset_eq_conv_count_eq)
wenzelm@17200
   112
  done
paulson@15072
   113
wenzelm@11054
   114
wenzelm@11054
   115
text {* \medskip Congruence rule *}
wenzelm@11054
   116
wenzelm@11054
   117
lemma perm_remove_perm: "xs <~~> ys ==> remove z xs <~~> remove z ys"
wenzelm@25379
   118
  by (induct pred: perm) auto
wenzelm@11054
   119
wenzelm@11054
   120
lemma remove_hd [simp]: "remove z (z # xs) = xs"
paulson@15072
   121
  by auto
wenzelm@11054
   122
wenzelm@11054
   123
lemma cons_perm_imp_perm: "z # xs <~~> z # ys ==> xs <~~> ys"
wenzelm@17200
   124
  by (drule_tac z = z in perm_remove_perm) auto
wenzelm@11054
   125
wenzelm@11054
   126
lemma cons_perm_eq [iff]: "(z#xs <~~> z#ys) = (xs <~~> ys)"
wenzelm@17200
   127
  by (blast intro: cons_perm_imp_perm)
wenzelm@11054
   128
wenzelm@25379
   129
lemma append_perm_imp_perm: "zs @ xs <~~> zs @ ys ==> xs <~~> ys"
wenzelm@25379
   130
  apply (induct zs arbitrary: xs ys rule: rev_induct)
wenzelm@11054
   131
   apply (simp_all (no_asm_use))
wenzelm@11054
   132
  apply blast
wenzelm@11054
   133
  done
wenzelm@11054
   134
wenzelm@11054
   135
lemma perm_append1_eq [iff]: "(zs @ xs <~~> zs @ ys) = (xs <~~> ys)"
wenzelm@17200
   136
  by (blast intro: append_perm_imp_perm perm_append1)
wenzelm@11054
   137
wenzelm@11054
   138
lemma perm_append2_eq [iff]: "(xs @ zs <~~> ys @ zs) = (xs <~~> ys)"
wenzelm@11054
   139
  apply (safe intro!: perm_append2)
wenzelm@11054
   140
  apply (rule append_perm_imp_perm)
wenzelm@11054
   141
  apply (rule perm_append_swap [THEN perm.trans])
wenzelm@11054
   142
    -- {* the previous step helps this @{text blast} call succeed quickly *}
wenzelm@11054
   143
  apply (blast intro: perm_append_swap)
wenzelm@11054
   144
  done
wenzelm@11054
   145
paulson@15072
   146
lemma multiset_of_eq_perm: "(multiset_of xs = multiset_of ys) = (xs <~~> ys) "
wenzelm@17200
   147
  apply (rule iffI)
wenzelm@17200
   148
  apply (erule_tac [2] perm.induct, simp_all add: union_ac)
wenzelm@17200
   149
  apply (erule rev_mp, rule_tac x=ys in spec)
wenzelm@17200
   150
  apply (induct_tac xs, auto)
wenzelm@17200
   151
  apply (erule_tac x = "remove a x" in allE, drule sym, simp)
wenzelm@17200
   152
  apply (subgoal_tac "a \<in> set x")
wenzelm@17200
   153
  apply (drule_tac z=a in perm.Cons)
wenzelm@17200
   154
  apply (erule perm.trans, rule perm_sym, erule perm_remove)
paulson@15005
   155
  apply (drule_tac f=set_of in arg_cong, simp)
paulson@15005
   156
  done
paulson@15005
   157
wenzelm@17200
   158
lemma multiset_of_le_perm_append:
wenzelm@17200
   159
    "(multiset_of xs \<le># multiset_of ys) = (\<exists>zs. xs @ zs <~~> ys)";
wenzelm@17200
   160
  apply (auto simp: multiset_of_eq_perm[THEN sym] mset_le_exists_conv)
paulson@15072
   161
  apply (insert surj_multiset_of, drule surjD)
paulson@15072
   162
  apply (blast intro: sym)+
paulson@15072
   163
  done
paulson@15005
   164
nipkow@25277
   165
lemma perm_set_eq: "xs <~~> ys ==> set xs = set ys"
wenzelm@25379
   166
  by (metis multiset_of_eq_perm multiset_of_eq_setD)
nipkow@25277
   167
nipkow@25277
   168
lemma perm_distinct_iff: "xs <~~> ys ==> distinct xs = distinct ys"
wenzelm@25379
   169
  apply (induct pred: perm)
wenzelm@25379
   170
     apply simp_all
wenzelm@25379
   171
   apply fastsimp
wenzelm@25379
   172
  apply (metis perm_set_eq)
wenzelm@25379
   173
  done
nipkow@25277
   174
nipkow@25287
   175
lemma eq_set_perm_remdups: "set xs = set ys ==> remdups xs <~~> remdups ys"
wenzelm@25379
   176
  apply (induct xs arbitrary: ys rule: length_induct)
wenzelm@25379
   177
  apply (case_tac "remdups xs", simp, simp)
wenzelm@25379
   178
  apply (subgoal_tac "a : set (remdups ys)")
wenzelm@25379
   179
   prefer 2 apply (metis set.simps(2) insert_iff set_remdups)
wenzelm@25379
   180
  apply (drule split_list) apply(elim exE conjE)
wenzelm@25379
   181
  apply (drule_tac x=list in spec) apply(erule impE) prefer 2
wenzelm@25379
   182
   apply (drule_tac x="ysa@zs" in spec) apply(erule impE) prefer 2
wenzelm@25379
   183
    apply simp
wenzelm@25379
   184
    apply (subgoal_tac "a#list <~~> a#ysa@zs")
wenzelm@25379
   185
     apply (metis Cons_eq_appendI perm_append_Cons trans)
wenzelm@25379
   186
    apply (metis Cons Cons_eq_appendI distinct.simps(2)
wenzelm@25379
   187
      distinct_remdups distinct_remdups_id perm_append_swap perm_distinct_iff)
wenzelm@25379
   188
   apply (subgoal_tac "set (a#list) = set (ysa@a#zs) & distinct (a#list) & distinct (ysa@a#zs)")
wenzelm@25379
   189
    apply (fastsimp simp add: insert_ident)
wenzelm@25379
   190
   apply (metis distinct_remdups set_remdups)
wenzelm@26316
   191
  apply (metis le_less_trans Suc_length_conv length_remdups_leq less_Suc_eq nat_less_le)
wenzelm@25379
   192
  done
nipkow@25287
   193
nipkow@25287
   194
lemma perm_remdups_iff_eq_set: "remdups x <~~> remdups y = (set x = set y)"
wenzelm@25379
   195
  by (metis List.set_remdups perm_set_eq eq_set_perm_remdups)
nipkow@25287
   196
wenzelm@11054
   197
end