src/HOL/Library/Permutation.thy
author bulwahn
Fri Apr 08 16:31:14 2011 +0200 (2011-04-08)
changeset 42316 12635bb655fd
parent 40122 1d8ad2ff3e01
child 44890 22f665a2e91c
permissions -rw-r--r--
deactivating other compilations in quickcheck_exhaustive momentarily that only interesting for my benchmarks and experiments
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
haftmann@30738
     8
imports Main 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
nipkow@36903
    96
lemma perm_remove: "x \<in> set ys ==> ys <~~> x # remove1 x ys"
wenzelm@17200
    97
  by (induct ys) auto
wenzelm@11054
    98
wenzelm@11054
    99
wenzelm@11054
   100
text {* \medskip Congruence rule *}
wenzelm@11054
   101
nipkow@36903
   102
lemma perm_remove_perm: "xs <~~> ys ==> remove1 z xs <~~> remove1 z ys"
wenzelm@25379
   103
  by (induct pred: perm) auto
wenzelm@11054
   104
nipkow@36903
   105
lemma remove_hd [simp]: "remove1 z (z # xs) = xs"
paulson@15072
   106
  by auto
wenzelm@11054
   107
wenzelm@11054
   108
lemma cons_perm_imp_perm: "z # xs <~~> z # ys ==> xs <~~> ys"
wenzelm@17200
   109
  by (drule_tac z = z in perm_remove_perm) auto
wenzelm@11054
   110
wenzelm@11054
   111
lemma cons_perm_eq [iff]: "(z#xs <~~> z#ys) = (xs <~~> ys)"
wenzelm@17200
   112
  by (blast intro: cons_perm_imp_perm)
wenzelm@11054
   113
wenzelm@25379
   114
lemma append_perm_imp_perm: "zs @ xs <~~> zs @ ys ==> xs <~~> ys"
wenzelm@25379
   115
  apply (induct zs arbitrary: xs ys rule: rev_induct)
wenzelm@11054
   116
   apply (simp_all (no_asm_use))
wenzelm@11054
   117
  apply blast
wenzelm@11054
   118
  done
wenzelm@11054
   119
wenzelm@11054
   120
lemma perm_append1_eq [iff]: "(zs @ xs <~~> zs @ ys) = (xs <~~> ys)"
wenzelm@17200
   121
  by (blast intro: append_perm_imp_perm perm_append1)
wenzelm@11054
   122
wenzelm@11054
   123
lemma perm_append2_eq [iff]: "(xs @ zs <~~> ys @ zs) = (xs <~~> ys)"
wenzelm@11054
   124
  apply (safe intro!: perm_append2)
wenzelm@11054
   125
  apply (rule append_perm_imp_perm)
wenzelm@11054
   126
  apply (rule perm_append_swap [THEN perm.trans])
wenzelm@11054
   127
    -- {* the previous step helps this @{text blast} call succeed quickly *}
wenzelm@11054
   128
  apply (blast intro: perm_append_swap)
wenzelm@11054
   129
  done
wenzelm@11054
   130
paulson@15072
   131
lemma multiset_of_eq_perm: "(multiset_of xs = multiset_of ys) = (xs <~~> ys) "
wenzelm@17200
   132
  apply (rule iffI)
wenzelm@17200
   133
  apply (erule_tac [2] perm.induct, simp_all add: union_ac)
wenzelm@17200
   134
  apply (erule rev_mp, rule_tac x=ys in spec)
wenzelm@17200
   135
  apply (induct_tac xs, auto)
nipkow@36903
   136
  apply (erule_tac x = "remove1 a x" in allE, drule sym, simp)
wenzelm@17200
   137
  apply (subgoal_tac "a \<in> set x")
wenzelm@17200
   138
  apply (drule_tac z=a in perm.Cons)
wenzelm@17200
   139
  apply (erule perm.trans, rule perm_sym, erule perm_remove)
paulson@15005
   140
  apply (drule_tac f=set_of in arg_cong, simp)
paulson@15005
   141
  done
paulson@15005
   142
wenzelm@17200
   143
lemma multiset_of_le_perm_append:
haftmann@35272
   144
    "multiset_of xs \<le> multiset_of ys \<longleftrightarrow> (\<exists>zs. xs @ zs <~~> ys)"
wenzelm@17200
   145
  apply (auto simp: multiset_of_eq_perm[THEN sym] mset_le_exists_conv)
paulson@15072
   146
  apply (insert surj_multiset_of, drule surjD)
paulson@15072
   147
  apply (blast intro: sym)+
paulson@15072
   148
  done
paulson@15005
   149
nipkow@25277
   150
lemma perm_set_eq: "xs <~~> ys ==> set xs = set ys"
wenzelm@25379
   151
  by (metis multiset_of_eq_perm multiset_of_eq_setD)
nipkow@25277
   152
nipkow@25277
   153
lemma perm_distinct_iff: "xs <~~> ys ==> distinct xs = distinct ys"
wenzelm@25379
   154
  apply (induct pred: perm)
wenzelm@25379
   155
     apply simp_all
wenzelm@25379
   156
   apply fastsimp
wenzelm@25379
   157
  apply (metis perm_set_eq)
wenzelm@25379
   158
  done
nipkow@25277
   159
nipkow@25287
   160
lemma eq_set_perm_remdups: "set xs = set ys ==> remdups xs <~~> remdups ys"
wenzelm@25379
   161
  apply (induct xs arbitrary: ys rule: length_induct)
wenzelm@25379
   162
  apply (case_tac "remdups xs", simp, simp)
wenzelm@25379
   163
  apply (subgoal_tac "a : set (remdups ys)")
wenzelm@25379
   164
   prefer 2 apply (metis set.simps(2) insert_iff set_remdups)
wenzelm@25379
   165
  apply (drule split_list) apply(elim exE conjE)
wenzelm@25379
   166
  apply (drule_tac x=list in spec) apply(erule impE) prefer 2
wenzelm@25379
   167
   apply (drule_tac x="ysa@zs" in spec) apply(erule impE) prefer 2
wenzelm@25379
   168
    apply simp
wenzelm@25379
   169
    apply (subgoal_tac "a#list <~~> a#ysa@zs")
wenzelm@25379
   170
     apply (metis Cons_eq_appendI perm_append_Cons trans)
haftmann@40122
   171
    apply (metis Cons Cons_eq_appendI distinct.simps(2)
wenzelm@25379
   172
      distinct_remdups distinct_remdups_id perm_append_swap perm_distinct_iff)
wenzelm@25379
   173
   apply (subgoal_tac "set (a#list) = set (ysa@a#zs) & distinct (a#list) & distinct (ysa@a#zs)")
wenzelm@25379
   174
    apply (fastsimp simp add: insert_ident)
wenzelm@25379
   175
   apply (metis distinct_remdups set_remdups)
haftmann@30742
   176
   apply (subgoal_tac "length (remdups xs) < Suc (length xs)")
haftmann@30742
   177
   apply simp
haftmann@30742
   178
   apply (subgoal_tac "length (remdups xs) \<le> length xs")
haftmann@30742
   179
   apply simp
haftmann@30742
   180
   apply (rule length_remdups_leq)
wenzelm@25379
   181
  done
nipkow@25287
   182
nipkow@25287
   183
lemma perm_remdups_iff_eq_set: "remdups x <~~> remdups y = (set x = set y)"
wenzelm@25379
   184
  by (metis List.set_remdups perm_set_eq eq_set_perm_remdups)
nipkow@25287
   185
hoelzl@39075
   186
lemma permutation_Ex_bij:
hoelzl@39075
   187
  assumes "xs <~~> ys"
hoelzl@39075
   188
  shows "\<exists>f. bij_betw f {..<length xs} {..<length ys} \<and> (\<forall>i<length xs. xs ! i = ys ! (f i))"
hoelzl@39075
   189
using assms proof induct
hoelzl@39075
   190
  case Nil then show ?case unfolding bij_betw_def by simp
hoelzl@39075
   191
next
hoelzl@39075
   192
  case (swap y x l)
hoelzl@39075
   193
  show ?case
hoelzl@39075
   194
  proof (intro exI[of _ "Fun.swap 0 1 id"] conjI allI impI)
hoelzl@39075
   195
    show "bij_betw (Fun.swap 0 1 id) {..<length (y # x # l)} {..<length (x # y # l)}"
hoelzl@39078
   196
      by (auto simp: bij_betw_def bij_betw_swap_iff)
hoelzl@39075
   197
    fix i assume "i < length(y#x#l)"
hoelzl@39075
   198
    show "(y # x # l) ! i = (x # y # l) ! (Fun.swap 0 1 id) i"
hoelzl@39075
   199
      by (cases i) (auto simp: Fun.swap_def gr0_conv_Suc)
hoelzl@39075
   200
  qed
hoelzl@39075
   201
next
hoelzl@39075
   202
  case (Cons xs ys z)
hoelzl@39075
   203
  then obtain f where bij: "bij_betw f {..<length xs} {..<length ys}" and
hoelzl@39075
   204
    perm: "\<forall>i<length xs. xs ! i = ys ! (f i)" by blast
hoelzl@39075
   205
  let "?f i" = "case i of Suc n \<Rightarrow> Suc (f n) | 0 \<Rightarrow> 0"
hoelzl@39075
   206
  show ?case
hoelzl@39075
   207
  proof (intro exI[of _ ?f] allI conjI impI)
hoelzl@39075
   208
    have *: "{..<length (z#xs)} = {0} \<union> Suc ` {..<length xs}"
hoelzl@39075
   209
            "{..<length (z#ys)} = {0} \<union> Suc ` {..<length ys}"
hoelzl@39078
   210
      by (simp_all add: lessThan_Suc_eq_insert_0)
hoelzl@39075
   211
    show "bij_betw ?f {..<length (z#xs)} {..<length (z#ys)}" unfolding *
hoelzl@39075
   212
    proof (rule bij_betw_combine)
hoelzl@39075
   213
      show "bij_betw ?f (Suc ` {..<length xs}) (Suc ` {..<length ys})"
hoelzl@39075
   214
        using bij unfolding bij_betw_def
hoelzl@39075
   215
        by (auto intro!: inj_onI imageI dest: inj_onD simp: image_compose[symmetric] comp_def)
hoelzl@39075
   216
    qed (auto simp: bij_betw_def)
hoelzl@39075
   217
    fix i assume "i < length (z#xs)"
hoelzl@39075
   218
    then show "(z # xs) ! i = (z # ys) ! (?f i)"
hoelzl@39075
   219
      using perm by (cases i) auto
hoelzl@39075
   220
  qed
hoelzl@39075
   221
next
hoelzl@39075
   222
  case (trans xs ys zs)
hoelzl@39075
   223
  then obtain f g where
hoelzl@39075
   224
    bij: "bij_betw f {..<length xs} {..<length ys}" "bij_betw g {..<length ys} {..<length zs}" and
hoelzl@39075
   225
    perm: "\<forall>i<length xs. xs ! i = ys ! (f i)" "\<forall>i<length ys. ys ! i = zs ! (g i)" by blast
hoelzl@39075
   226
  show ?case
hoelzl@39075
   227
  proof (intro exI[of _ "g\<circ>f"] conjI allI impI)
hoelzl@39075
   228
    show "bij_betw (g \<circ> f) {..<length xs} {..<length zs}"
hoelzl@39075
   229
      using bij by (rule bij_betw_trans)
hoelzl@39075
   230
    fix i assume "i < length xs"
hoelzl@39075
   231
    with bij have "f i < length ys" unfolding bij_betw_def by force
hoelzl@39075
   232
    with `i < length xs` show "xs ! i = zs ! (g \<circ> f) i"
hoelzl@39075
   233
      using trans(1,3)[THEN perm_length] perm by force
hoelzl@39075
   234
  qed
hoelzl@39075
   235
qed
hoelzl@39075
   236
wenzelm@11054
   237
end