src/HOL/Library/Permutation.thy
author haftmann
Mon Jun 05 15:59:41 2017 +0200 (2017-06-05)
changeset 66010 2f7d39285a1a
parent 64587 8355a6e2df79
child 69597 ff784d5a5bfb
permissions -rw-r--r--
executable domain membership checks
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
lp15@61699
    18
proposition 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
lp15@61699
    24
proposition 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
lp15@61699
    30
proposition perm_length: "xs <~~> ys \<Longrightarrow> length xs = length ys"
wenzelm@25379
    31
  by (induct pred: perm) simp_all
wenzelm@11054
    32
lp15@61699
    33
proposition perm_empty_imp: "[] <~~> xs \<Longrightarrow> xs = []"
wenzelm@17200
    34
  by (drule perm_length) auto
wenzelm@11054
    35
lp15@61699
    36
proposition 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
lp15@61699
    44
proposition perm_append_Cons: "a # xs @ ys <~~> xs @ a # ys"
wenzelm@17200
    45
  by (induct xs) auto
wenzelm@11054
    46
lp15@61699
    47
proposition perm_append_swap: "xs @ ys <~~> ys @ xs"
lp15@61699
    48
  by (induct xs) (auto intro: perm_append_Cons)
wenzelm@11054
    49
lp15@61699
    50
proposition perm_append_single: "a # xs <~~> xs @ [a]"
wenzelm@17200
    51
  by (rule perm.trans [OF _ perm_append_swap]) simp
wenzelm@11054
    52
lp15@61699
    53
proposition perm_rev: "rev xs <~~> xs"
lp15@61699
    54
  by (induct xs) (auto intro!: perm_append_single intro: perm_sym)
wenzelm@11054
    55
lp15@61699
    56
proposition perm_append1: "xs <~~> ys \<Longrightarrow> l @ xs <~~> l @ ys"
wenzelm@17200
    57
  by (induct l) auto
wenzelm@11054
    58
lp15@61699
    59
proposition perm_append2: "xs <~~> ys \<Longrightarrow> xs @ l <~~> ys @ l"
wenzelm@17200
    60
  by (blast intro!: perm_append_swap perm_append1)
wenzelm@11054
    61
wenzelm@11054
    62
wenzelm@60500
    63
subsection \<open>Further results\<close>
wenzelm@11054
    64
lp15@61699
    65
proposition perm_empty [iff]: "[] <~~> xs \<longleftrightarrow> xs = []"
wenzelm@17200
    66
  by (blast intro: perm_empty_imp)
wenzelm@11054
    67
lp15@61699
    68
proposition perm_empty2 [iff]: "xs <~~> [] \<longleftrightarrow> xs = []"
wenzelm@11054
    69
  apply auto
wenzelm@11054
    70
  apply (erule perm_sym [THEN perm_empty_imp])
wenzelm@11054
    71
  done
wenzelm@11054
    72
lp15@61699
    73
proposition perm_sing_imp: "ys <~~> xs \<Longrightarrow> xs = [y] \<Longrightarrow> ys = [y]"
wenzelm@25379
    74
  by (induct pred: perm) auto
wenzelm@11054
    75
lp15@61699
    76
proposition perm_sing_eq [iff]: "ys <~~> [y] \<longleftrightarrow> ys = [y]"
wenzelm@17200
    77
  by (blast intro: perm_sing_imp)
wenzelm@11054
    78
lp15@61699
    79
proposition perm_sing_eq2 [iff]: "[y] <~~> ys \<longleftrightarrow> ys = [y]"
wenzelm@17200
    80
  by (blast dest: perm_sym)
wenzelm@11054
    81
wenzelm@11054
    82
wenzelm@60500
    83
subsection \<open>Removing elements\<close>
wenzelm@11054
    84
lp15@61699
    85
proposition perm_remove: "x \<in> set ys \<Longrightarrow> ys <~~> x # remove1 x ys"
wenzelm@17200
    86
  by (induct ys) auto
wenzelm@11054
    87
wenzelm@11054
    88
wenzelm@60500
    89
text \<open>\medskip Congruence rule\<close>
wenzelm@11054
    90
lp15@61699
    91
proposition perm_remove_perm: "xs <~~> ys \<Longrightarrow> remove1 z xs <~~> remove1 z ys"
wenzelm@25379
    92
  by (induct pred: perm) auto
wenzelm@11054
    93
lp15@61699
    94
proposition remove_hd [simp]: "remove1 z (z # xs) = xs"
paulson@15072
    95
  by auto
wenzelm@11054
    96
lp15@61699
    97
proposition cons_perm_imp_perm: "z # xs <~~> z # ys \<Longrightarrow> xs <~~> ys"
wenzelm@63649
    98
  by (drule perm_remove_perm [where z = z]) auto
wenzelm@11054
    99
lp15@61699
   100
proposition cons_perm_eq [iff]: "z#xs <~~> z#ys \<longleftrightarrow> xs <~~> ys"
wenzelm@17200
   101
  by (blast intro: cons_perm_imp_perm)
wenzelm@11054
   102
lp15@61699
   103
proposition append_perm_imp_perm: "zs @ xs <~~> zs @ ys \<Longrightarrow> xs <~~> ys"
wenzelm@53238
   104
  by (induct zs arbitrary: xs ys rule: rev_induct) auto
wenzelm@11054
   105
lp15@61699
   106
proposition perm_append1_eq [iff]: "zs @ xs <~~> zs @ ys \<longleftrightarrow> xs <~~> ys"
wenzelm@17200
   107
  by (blast intro: append_perm_imp_perm perm_append1)
wenzelm@11054
   108
lp15@61699
   109
proposition perm_append2_eq [iff]: "xs @ zs <~~> ys @ zs \<longleftrightarrow> xs <~~> ys"
wenzelm@11054
   110
  apply (safe intro!: perm_append2)
wenzelm@11054
   111
  apply (rule append_perm_imp_perm)
wenzelm@11054
   112
  apply (rule perm_append_swap [THEN perm.trans])
wenzelm@61585
   113
    \<comment> \<open>the previous step helps this \<open>blast\<close> call succeed quickly\<close>
wenzelm@11054
   114
  apply (blast intro: perm_append_swap)
wenzelm@11054
   115
  done
wenzelm@11054
   116
lp15@61699
   117
theorem mset_eq_perm: "mset xs = mset ys \<longleftrightarrow> xs <~~> ys"
wenzelm@17200
   118
  apply (rule iffI)
wenzelm@56796
   119
  apply (erule_tac [2] perm.induct)
wenzelm@56796
   120
  apply (simp_all add: union_ac)
wenzelm@56796
   121
  apply (erule rev_mp)
wenzelm@56796
   122
  apply (rule_tac x=ys in spec)
wenzelm@56796
   123
  apply (induct_tac xs)
wenzelm@56796
   124
  apply auto
wenzelm@56796
   125
  apply (erule_tac x = "remove1 a x" in allE)
wenzelm@56796
   126
  apply (drule sym)
wenzelm@56796
   127
  apply simp
wenzelm@17200
   128
  apply (subgoal_tac "a \<in> set x")
wenzelm@53238
   129
  apply (drule_tac z = a in perm.Cons)
wenzelm@56796
   130
  apply (erule perm.trans)
wenzelm@56796
   131
  apply (rule perm_sym)
wenzelm@56796
   132
  apply (erule perm_remove)
nipkow@60495
   133
  apply (drule_tac f=set_mset in arg_cong)
wenzelm@56796
   134
  apply simp
paulson@15005
   135
  done
paulson@15005
   136
haftmann@64587
   137
proposition mset_le_perm_append: "mset xs \<subseteq># mset ys \<longleftrightarrow> (\<exists>zs. xs @ zs <~~> ys)"
Mathias@63310
   138
  apply (auto simp: mset_eq_perm[THEN sym] mset_subset_eq_exists_conv)
nipkow@60515
   139
  apply (insert surj_mset)
wenzelm@56796
   140
  apply (drule surjD)
paulson@15072
   141
  apply (blast intro: sym)+
paulson@15072
   142
  done
paulson@15005
   143
lp15@61699
   144
proposition perm_set_eq: "xs <~~> ys \<Longrightarrow> set xs = set ys"
nipkow@60515
   145
  by (metis mset_eq_perm mset_eq_setD)
nipkow@25277
   146
lp15@61699
   147
proposition perm_distinct_iff: "xs <~~> ys \<Longrightarrow> distinct xs = distinct ys"
wenzelm@25379
   148
  apply (induct pred: perm)
wenzelm@25379
   149
     apply simp_all
nipkow@44890
   150
   apply fastforce
wenzelm@25379
   151
  apply (metis perm_set_eq)
wenzelm@25379
   152
  done
nipkow@25277
   153
lp15@61699
   154
theorem eq_set_perm_remdups: "set xs = set ys \<Longrightarrow> remdups xs <~~> remdups ys"
wenzelm@25379
   155
  apply (induct xs arbitrary: ys rule: length_induct)
wenzelm@53238
   156
  apply (case_tac "remdups xs")
wenzelm@53238
   157
   apply simp_all
wenzelm@53238
   158
  apply (subgoal_tac "a \<in> set (remdups ys)")
blanchet@57816
   159
   prefer 2 apply (metis list.set(2) insert_iff set_remdups)
wenzelm@56796
   160
  apply (drule split_list) apply (elim exE conjE)
wenzelm@56796
   161
  apply (drule_tac x = list in spec) apply (erule impE) prefer 2
wenzelm@56796
   162
   apply (drule_tac x = "ysa @ zs" in spec) apply (erule impE) prefer 2
wenzelm@25379
   163
    apply simp
wenzelm@53238
   164
    apply (subgoal_tac "a # list <~~> a # ysa @ zs")
wenzelm@25379
   165
     apply (metis Cons_eq_appendI perm_append_Cons trans)
haftmann@40122
   166
    apply (metis Cons Cons_eq_appendI distinct.simps(2)
wenzelm@25379
   167
      distinct_remdups distinct_remdups_id perm_append_swap perm_distinct_iff)
wenzelm@56796
   168
   apply (subgoal_tac "set (a # list) =
wenzelm@56796
   169
      set (ysa @ a # zs) \<and> distinct (a # list) \<and> distinct (ysa @ a # zs)")
nipkow@44890
   170
    apply (fastforce simp add: insert_ident)
wenzelm@25379
   171
   apply (metis distinct_remdups set_remdups)
haftmann@30742
   172
   apply (subgoal_tac "length (remdups xs) < Suc (length xs)")
haftmann@30742
   173
   apply simp
haftmann@30742
   174
   apply (subgoal_tac "length (remdups xs) \<le> length xs")
haftmann@30742
   175
   apply simp
haftmann@30742
   176
   apply (rule length_remdups_leq)
wenzelm@25379
   177
  done
nipkow@25287
   178
lp15@61699
   179
proposition perm_remdups_iff_eq_set: "remdups x <~~> remdups y \<longleftrightarrow> set x = set y"
wenzelm@25379
   180
  by (metis List.set_remdups perm_set_eq eq_set_perm_remdups)
nipkow@25287
   181
lp15@61699
   182
theorem permutation_Ex_bij:
hoelzl@39075
   183
  assumes "xs <~~> ys"
hoelzl@39075
   184
  shows "\<exists>f. bij_betw f {..<length xs} {..<length ys} \<and> (\<forall>i<length xs. xs ! i = ys ! (f i))"
wenzelm@56796
   185
  using assms
wenzelm@56796
   186
proof induct
wenzelm@53238
   187
  case Nil
wenzelm@56796
   188
  then show ?case
wenzelm@56796
   189
    unfolding bij_betw_def by simp
hoelzl@39075
   190
next
hoelzl@39075
   191
  case (swap y x l)
hoelzl@39075
   192
  show ?case
hoelzl@39075
   193
  proof (intro exI[of _ "Fun.swap 0 1 id"] conjI allI impI)
hoelzl@39075
   194
    show "bij_betw (Fun.swap 0 1 id) {..<length (y # x # l)} {..<length (x # y # l)}"
bulwahn@50037
   195
      by (auto simp: bij_betw_def)
wenzelm@53238
   196
    fix i
wenzelm@56796
   197
    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)
wenzelm@56796
   203
  then obtain f where bij: "bij_betw f {..<length xs} {..<length ys}"
wenzelm@56796
   204
    and perm: "\<forall>i<length xs. xs ! i = ys ! (f i)"
wenzelm@56796
   205
    by blast
wenzelm@53238
   206
  let ?f = "\<lambda>i. case i of Suc n \<Rightarrow> Suc (f n) | 0 \<Rightarrow> 0"
hoelzl@39075
   207
  show ?case
hoelzl@39075
   208
  proof (intro exI[of _ ?f] allI conjI impI)
hoelzl@39075
   209
    have *: "{..<length (z#xs)} = {0} \<union> Suc ` {..<length xs}"
hoelzl@39075
   210
            "{..<length (z#ys)} = {0} \<union> Suc ` {..<length ys}"
hoelzl@39078
   211
      by (simp_all add: lessThan_Suc_eq_insert_0)
wenzelm@53238
   212
    show "bij_betw ?f {..<length (z#xs)} {..<length (z#ys)}"
wenzelm@53238
   213
      unfolding *
hoelzl@39075
   214
    proof (rule bij_betw_combine)
hoelzl@39075
   215
      show "bij_betw ?f (Suc ` {..<length xs}) (Suc ` {..<length ys})"
hoelzl@39075
   216
        using bij unfolding bij_betw_def
haftmann@56154
   217
        by (auto intro!: inj_onI imageI dest: inj_onD simp: image_comp comp_def)
hoelzl@39075
   218
    qed (auto simp: bij_betw_def)
wenzelm@53238
   219
    fix i
wenzelm@56796
   220
    assume "i < length (z # xs)"
hoelzl@39075
   221
    then show "(z # xs) ! i = (z # ys) ! (?f i)"
hoelzl@39075
   222
      using perm by (cases i) auto
hoelzl@39075
   223
  qed
hoelzl@39075
   224
next
hoelzl@39075
   225
  case (trans xs ys zs)
wenzelm@56796
   226
  then obtain f g
wenzelm@56796
   227
    where bij: "bij_betw f {..<length xs} {..<length ys}" "bij_betw g {..<length ys} {..<length zs}"
wenzelm@56796
   228
    and perm: "\<forall>i<length xs. xs ! i = ys ! (f i)" "\<forall>i<length ys. ys ! i = zs ! (g i)"
wenzelm@56796
   229
    by blast
hoelzl@39075
   230
  show ?case
wenzelm@53238
   231
  proof (intro exI[of _ "g \<circ> f"] conjI allI impI)
hoelzl@39075
   232
    show "bij_betw (g \<circ> f) {..<length xs} {..<length zs}"
hoelzl@39075
   233
      using bij by (rule bij_betw_trans)
wenzelm@56796
   234
    fix i
wenzelm@56796
   235
    assume "i < length xs"
wenzelm@56796
   236
    with bij have "f i < length ys"
wenzelm@56796
   237
      unfolding bij_betw_def by force
wenzelm@60500
   238
    with \<open>i < length xs\<close> show "xs ! i = zs ! (g \<circ> f) i"
wenzelm@53238
   239
      using trans(1,3)[THEN perm_length] perm by auto
hoelzl@39075
   240
  qed
hoelzl@39075
   241
qed
hoelzl@39075
   242
lp15@61699
   243
proposition perm_finite: "finite {B. B <~~> A}"
lp15@61699
   244
proof (rule finite_subset[where B="{xs. set xs \<subseteq> set A \<and> length xs \<le> length A}"])
lp15@61699
   245
 show "finite {xs. set xs \<subseteq> set A \<and> length xs \<le> length A}"
lp15@61699
   246
   apply (cases A, simp)
lp15@61699
   247
   apply (rule card_ge_0_finite)
lp15@61699
   248
   apply (auto simp: card_lists_length_le)
lp15@61699
   249
   done
lp15@61699
   250
next
lp15@61699
   251
 show "{B. B <~~> A} \<subseteq> {xs. set xs \<subseteq> set A \<and> length xs \<le> length A}"
lp15@61699
   252
   by (clarsimp simp add: perm_length perm_set_eq)
lp15@61699
   253
qed
lp15@61699
   254
lp15@61699
   255
proposition perm_swap:
lp15@61699
   256
    assumes "i < length xs" "j < length xs"
lp15@61699
   257
    shows "xs[i := xs ! j, j := xs ! i] <~~> xs"
lp15@61699
   258
  using assms by (simp add: mset_eq_perm[symmetric] mset_swap)
lp15@61699
   259
wenzelm@11054
   260
end