src/HOL/Library/Permutation.thy
author wenzelm
Wed Jun 17 11:03:05 2015 +0200 (2015-06-17)
changeset 60500 903bb1495239
parent 60397 f8a513fedb31
child 60502 aa58872267ee
permissions -rw-r--r--
isabelle update_cartouches;
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@60500
     5
section \<open>Permutations\<close>
wenzelm@11054
     6
nipkow@15131
     7
theory Permutation
wenzelm@51542
     8
imports Multiset
nipkow@15131
     9
begin
wenzelm@11054
    10
wenzelm@53238
    11
inductive perm :: "'a list \<Rightarrow> 'a list \<Rightarrow> bool"  ("_ <~~> _"  [50, 50] 50)  (* FIXME proper infix, without ambiguity!? *)
wenzelm@53238
    12
where
wenzelm@53238
    13
  Nil [intro!]: "[] <~~> []"
wenzelm@53238
    14
| swap [intro!]: "y # x # l <~~> x # y # l"
wenzelm@53238
    15
| Cons [intro!]: "xs <~~> ys \<Longrightarrow> z # xs <~~> z # ys"
wenzelm@53238
    16
| trans [intro]: "xs <~~> ys \<Longrightarrow> ys <~~> zs \<Longrightarrow> xs <~~> zs"
wenzelm@11054
    17
wenzelm@11054
    18
lemma perm_refl [iff]: "l <~~> l"
wenzelm@17200
    19
  by (induct l) auto
wenzelm@11054
    20
wenzelm@11054
    21
wenzelm@60500
    22
subsection \<open>Some examples of rule induction on permutations\<close>
wenzelm@11054
    23
wenzelm@53238
    24
lemma xperm_empty_imp: "[] <~~> ys \<Longrightarrow> ys = []"
wenzelm@56796
    25
  by (induct xs == "[] :: 'a list" ys pred: perm) simp_all
wenzelm@11054
    26
wenzelm@11054
    27
wenzelm@60500
    28
text \<open>\medskip This more general theorem is easier to understand!\<close>
wenzelm@11054
    29
wenzelm@53238
    30
lemma perm_length: "xs <~~> ys \<Longrightarrow> length xs = length ys"
wenzelm@25379
    31
  by (induct pred: perm) simp_all
wenzelm@11054
    32
wenzelm@53238
    33
lemma perm_empty_imp: "[] <~~> xs \<Longrightarrow> xs = []"
wenzelm@17200
    34
  by (drule perm_length) auto
wenzelm@11054
    35
wenzelm@53238
    36
lemma perm_sym: "xs <~~> ys \<Longrightarrow> ys <~~> xs"
wenzelm@25379
    37
  by (induct pred: perm) auto
wenzelm@11054
    38
wenzelm@11054
    39
wenzelm@60500
    40
subsection \<open>Ways of making new permutations\<close>
wenzelm@11054
    41
wenzelm@60500
    42
text \<open>We can insert the head anywhere in the list.\<close>
wenzelm@11054
    43
wenzelm@11054
    44
lemma perm_append_Cons: "a # xs @ ys <~~> xs @ a # ys"
wenzelm@17200
    45
  by (induct xs) auto
wenzelm@11054
    46
wenzelm@11054
    47
lemma perm_append_swap: "xs @ ys <~~> ys @ xs"
wenzelm@17200
    48
  apply (induct xs)
wenzelm@17200
    49
    apply simp_all
wenzelm@11054
    50
  apply (blast intro: perm_append_Cons)
wenzelm@11054
    51
  done
wenzelm@11054
    52
wenzelm@11054
    53
lemma perm_append_single: "a # xs <~~> xs @ [a]"
wenzelm@17200
    54
  by (rule perm.trans [OF _ perm_append_swap]) simp
wenzelm@11054
    55
wenzelm@11054
    56
lemma perm_rev: "rev xs <~~> xs"
wenzelm@17200
    57
  apply (induct xs)
wenzelm@17200
    58
   apply simp_all
paulson@11153
    59
  apply (blast intro!: perm_append_single intro: perm_sym)
wenzelm@11054
    60
  done
wenzelm@11054
    61
wenzelm@53238
    62
lemma perm_append1: "xs <~~> ys \<Longrightarrow> l @ xs <~~> l @ ys"
wenzelm@17200
    63
  by (induct l) auto
wenzelm@11054
    64
wenzelm@53238
    65
lemma perm_append2: "xs <~~> ys \<Longrightarrow> xs @ l <~~> ys @ l"
wenzelm@17200
    66
  by (blast intro!: perm_append_swap perm_append1)
wenzelm@11054
    67
wenzelm@11054
    68
wenzelm@60500
    69
subsection \<open>Further results\<close>
wenzelm@11054
    70
wenzelm@56796
    71
lemma perm_empty [iff]: "[] <~~> xs \<longleftrightarrow> xs = []"
wenzelm@17200
    72
  by (blast intro: perm_empty_imp)
wenzelm@11054
    73
wenzelm@56796
    74
lemma perm_empty2 [iff]: "xs <~~> [] \<longleftrightarrow> xs = []"
wenzelm@11054
    75
  apply auto
wenzelm@11054
    76
  apply (erule perm_sym [THEN perm_empty_imp])
wenzelm@11054
    77
  done
wenzelm@11054
    78
wenzelm@53238
    79
lemma perm_sing_imp: "ys <~~> xs \<Longrightarrow> xs = [y] \<Longrightarrow> ys = [y]"
wenzelm@25379
    80
  by (induct pred: perm) auto
wenzelm@11054
    81
wenzelm@56796
    82
lemma perm_sing_eq [iff]: "ys <~~> [y] \<longleftrightarrow> ys = [y]"
wenzelm@17200
    83
  by (blast intro: perm_sing_imp)
wenzelm@11054
    84
wenzelm@56796
    85
lemma perm_sing_eq2 [iff]: "[y] <~~> ys \<longleftrightarrow> ys = [y]"
wenzelm@17200
    86
  by (blast dest: perm_sym)
wenzelm@11054
    87
wenzelm@11054
    88
wenzelm@60500
    89
subsection \<open>Removing elements\<close>
wenzelm@11054
    90
wenzelm@53238
    91
lemma perm_remove: "x \<in> set ys \<Longrightarrow> ys <~~> x # remove1 x ys"
wenzelm@17200
    92
  by (induct ys) auto
wenzelm@11054
    93
wenzelm@11054
    94
wenzelm@60500
    95
text \<open>\medskip Congruence rule\<close>
wenzelm@11054
    96
wenzelm@53238
    97
lemma perm_remove_perm: "xs <~~> ys \<Longrightarrow> remove1 z xs <~~> remove1 z ys"
wenzelm@25379
    98
  by (induct pred: perm) auto
wenzelm@11054
    99
nipkow@36903
   100
lemma remove_hd [simp]: "remove1 z (z # xs) = xs"
paulson@15072
   101
  by auto
wenzelm@11054
   102
wenzelm@53238
   103
lemma cons_perm_imp_perm: "z # xs <~~> z # ys \<Longrightarrow> xs <~~> ys"
wenzelm@17200
   104
  by (drule_tac z = z in perm_remove_perm) auto
wenzelm@11054
   105
wenzelm@56796
   106
lemma cons_perm_eq [iff]: "z#xs <~~> z#ys \<longleftrightarrow> xs <~~> ys"
wenzelm@17200
   107
  by (blast intro: cons_perm_imp_perm)
wenzelm@11054
   108
wenzelm@53238
   109
lemma append_perm_imp_perm: "zs @ xs <~~> zs @ ys \<Longrightarrow> xs <~~> ys"
wenzelm@53238
   110
  by (induct zs arbitrary: xs ys rule: rev_induct) auto
wenzelm@11054
   111
wenzelm@56796
   112
lemma perm_append1_eq [iff]: "zs @ xs <~~> zs @ ys \<longleftrightarrow> xs <~~> ys"
wenzelm@17200
   113
  by (blast intro: append_perm_imp_perm perm_append1)
wenzelm@11054
   114
wenzelm@56796
   115
lemma perm_append2_eq [iff]: "xs @ zs <~~> ys @ zs \<longleftrightarrow> xs <~~> ys"
wenzelm@11054
   116
  apply (safe intro!: perm_append2)
wenzelm@11054
   117
  apply (rule append_perm_imp_perm)
wenzelm@11054
   118
  apply (rule perm_append_swap [THEN perm.trans])
wenzelm@60500
   119
    -- \<open>the previous step helps this @{text blast} call succeed quickly\<close>
wenzelm@11054
   120
  apply (blast intro: perm_append_swap)
wenzelm@11054
   121
  done
wenzelm@11054
   122
wenzelm@56796
   123
lemma multiset_of_eq_perm: "multiset_of xs = multiset_of ys \<longleftrightarrow> xs <~~> ys"
wenzelm@17200
   124
  apply (rule iffI)
wenzelm@56796
   125
  apply (erule_tac [2] perm.induct)
wenzelm@56796
   126
  apply (simp_all add: union_ac)
wenzelm@56796
   127
  apply (erule rev_mp)
wenzelm@56796
   128
  apply (rule_tac x=ys in spec)
wenzelm@56796
   129
  apply (induct_tac xs)
wenzelm@56796
   130
  apply auto
wenzelm@56796
   131
  apply (erule_tac x = "remove1 a x" in allE)
wenzelm@56796
   132
  apply (drule sym)
wenzelm@56796
   133
  apply simp
wenzelm@17200
   134
  apply (subgoal_tac "a \<in> set x")
wenzelm@53238
   135
  apply (drule_tac z = a in perm.Cons)
wenzelm@56796
   136
  apply (erule perm.trans)
wenzelm@56796
   137
  apply (rule perm_sym)
wenzelm@56796
   138
  apply (erule perm_remove)
wenzelm@56796
   139
  apply (drule_tac f=set_of in arg_cong)
wenzelm@56796
   140
  apply simp
paulson@15005
   141
  done
paulson@15005
   142
Mathias@60397
   143
lemma multiset_of_le_perm_append: "multiset_of xs \<le># multiset_of ys \<longleftrightarrow> (\<exists>zs. xs @ zs <~~> ys)"
wenzelm@17200
   144
  apply (auto simp: multiset_of_eq_perm[THEN sym] mset_le_exists_conv)
wenzelm@56796
   145
  apply (insert surj_multiset_of)
wenzelm@56796
   146
  apply (drule surjD)
paulson@15072
   147
  apply (blast intro: sym)+
paulson@15072
   148
  done
paulson@15005
   149
wenzelm@53238
   150
lemma perm_set_eq: "xs <~~> ys \<Longrightarrow> set xs = set ys"
wenzelm@25379
   151
  by (metis multiset_of_eq_perm multiset_of_eq_setD)
nipkow@25277
   152
wenzelm@53238
   153
lemma perm_distinct_iff: "xs <~~> ys \<Longrightarrow> distinct xs = distinct ys"
wenzelm@25379
   154
  apply (induct pred: perm)
wenzelm@25379
   155
     apply simp_all
nipkow@44890
   156
   apply fastforce
wenzelm@25379
   157
  apply (metis perm_set_eq)
wenzelm@25379
   158
  done
nipkow@25277
   159
wenzelm@53238
   160
lemma eq_set_perm_remdups: "set xs = set ys \<Longrightarrow> remdups xs <~~> remdups ys"
wenzelm@25379
   161
  apply (induct xs arbitrary: ys rule: length_induct)
wenzelm@53238
   162
  apply (case_tac "remdups xs")
wenzelm@53238
   163
   apply simp_all
wenzelm@53238
   164
  apply (subgoal_tac "a \<in> set (remdups ys)")
blanchet@57816
   165
   prefer 2 apply (metis list.set(2) insert_iff set_remdups)
wenzelm@56796
   166
  apply (drule split_list) apply (elim exE conjE)
wenzelm@56796
   167
  apply (drule_tac x = list in spec) apply (erule impE) prefer 2
wenzelm@56796
   168
   apply (drule_tac x = "ysa @ zs" in spec) apply (erule impE) prefer 2
wenzelm@25379
   169
    apply simp
wenzelm@53238
   170
    apply (subgoal_tac "a # list <~~> a # ysa @ zs")
wenzelm@25379
   171
     apply (metis Cons_eq_appendI perm_append_Cons trans)
haftmann@40122
   172
    apply (metis Cons Cons_eq_appendI distinct.simps(2)
wenzelm@25379
   173
      distinct_remdups distinct_remdups_id perm_append_swap perm_distinct_iff)
wenzelm@56796
   174
   apply (subgoal_tac "set (a # list) =
wenzelm@56796
   175
      set (ysa @ a # zs) \<and> distinct (a # list) \<and> distinct (ysa @ a # zs)")
nipkow@44890
   176
    apply (fastforce simp add: insert_ident)
wenzelm@25379
   177
   apply (metis distinct_remdups set_remdups)
haftmann@30742
   178
   apply (subgoal_tac "length (remdups xs) < Suc (length xs)")
haftmann@30742
   179
   apply simp
haftmann@30742
   180
   apply (subgoal_tac "length (remdups xs) \<le> length xs")
haftmann@30742
   181
   apply simp
haftmann@30742
   182
   apply (rule length_remdups_leq)
wenzelm@25379
   183
  done
nipkow@25287
   184
wenzelm@56796
   185
lemma perm_remdups_iff_eq_set: "remdups x <~~> remdups y \<longleftrightarrow> set x = set y"
wenzelm@25379
   186
  by (metis List.set_remdups perm_set_eq eq_set_perm_remdups)
nipkow@25287
   187
hoelzl@39075
   188
lemma permutation_Ex_bij:
hoelzl@39075
   189
  assumes "xs <~~> ys"
hoelzl@39075
   190
  shows "\<exists>f. bij_betw f {..<length xs} {..<length ys} \<and> (\<forall>i<length xs. xs ! i = ys ! (f i))"
wenzelm@56796
   191
  using assms
wenzelm@56796
   192
proof induct
wenzelm@53238
   193
  case Nil
wenzelm@56796
   194
  then show ?case
wenzelm@56796
   195
    unfolding bij_betw_def by simp
hoelzl@39075
   196
next
hoelzl@39075
   197
  case (swap y x l)
hoelzl@39075
   198
  show ?case
hoelzl@39075
   199
  proof (intro exI[of _ "Fun.swap 0 1 id"] conjI allI impI)
hoelzl@39075
   200
    show "bij_betw (Fun.swap 0 1 id) {..<length (y # x # l)} {..<length (x # y # l)}"
bulwahn@50037
   201
      by (auto simp: bij_betw_def)
wenzelm@53238
   202
    fix i
wenzelm@56796
   203
    assume "i < length (y # x # l)"
hoelzl@39075
   204
    show "(y # x # l) ! i = (x # y # l) ! (Fun.swap 0 1 id) i"
hoelzl@39075
   205
      by (cases i) (auto simp: Fun.swap_def gr0_conv_Suc)
hoelzl@39075
   206
  qed
hoelzl@39075
   207
next
hoelzl@39075
   208
  case (Cons xs ys z)
wenzelm@56796
   209
  then obtain f where bij: "bij_betw f {..<length xs} {..<length ys}"
wenzelm@56796
   210
    and perm: "\<forall>i<length xs. xs ! i = ys ! (f i)"
wenzelm@56796
   211
    by blast
wenzelm@53238
   212
  let ?f = "\<lambda>i. case i of Suc n \<Rightarrow> Suc (f n) | 0 \<Rightarrow> 0"
hoelzl@39075
   213
  show ?case
hoelzl@39075
   214
  proof (intro exI[of _ ?f] allI conjI impI)
hoelzl@39075
   215
    have *: "{..<length (z#xs)} = {0} \<union> Suc ` {..<length xs}"
hoelzl@39075
   216
            "{..<length (z#ys)} = {0} \<union> Suc ` {..<length ys}"
hoelzl@39078
   217
      by (simp_all add: lessThan_Suc_eq_insert_0)
wenzelm@53238
   218
    show "bij_betw ?f {..<length (z#xs)} {..<length (z#ys)}"
wenzelm@53238
   219
      unfolding *
hoelzl@39075
   220
    proof (rule bij_betw_combine)
hoelzl@39075
   221
      show "bij_betw ?f (Suc ` {..<length xs}) (Suc ` {..<length ys})"
hoelzl@39075
   222
        using bij unfolding bij_betw_def
haftmann@56154
   223
        by (auto intro!: inj_onI imageI dest: inj_onD simp: image_comp comp_def)
hoelzl@39075
   224
    qed (auto simp: bij_betw_def)
wenzelm@53238
   225
    fix i
wenzelm@56796
   226
    assume "i < length (z # xs)"
hoelzl@39075
   227
    then show "(z # xs) ! i = (z # ys) ! (?f i)"
hoelzl@39075
   228
      using perm by (cases i) auto
hoelzl@39075
   229
  qed
hoelzl@39075
   230
next
hoelzl@39075
   231
  case (trans xs ys zs)
wenzelm@56796
   232
  then obtain f g
wenzelm@56796
   233
    where bij: "bij_betw f {..<length xs} {..<length ys}" "bij_betw g {..<length ys} {..<length zs}"
wenzelm@56796
   234
    and perm: "\<forall>i<length xs. xs ! i = ys ! (f i)" "\<forall>i<length ys. ys ! i = zs ! (g i)"
wenzelm@56796
   235
    by blast
hoelzl@39075
   236
  show ?case
wenzelm@53238
   237
  proof (intro exI[of _ "g \<circ> f"] conjI allI impI)
hoelzl@39075
   238
    show "bij_betw (g \<circ> f) {..<length xs} {..<length zs}"
hoelzl@39075
   239
      using bij by (rule bij_betw_trans)
wenzelm@56796
   240
    fix i
wenzelm@56796
   241
    assume "i < length xs"
wenzelm@56796
   242
    with bij have "f i < length ys"
wenzelm@56796
   243
      unfolding bij_betw_def by force
wenzelm@60500
   244
    with \<open>i < length xs\<close> show "xs ! i = zs ! (g \<circ> f) i"
wenzelm@53238
   245
      using trans(1,3)[THEN perm_length] perm by auto
hoelzl@39075
   246
  qed
hoelzl@39075
   247
qed
hoelzl@39075
   248
wenzelm@11054
   249
end