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