src/HOL/Library/Permutation.thy
changeset 56796 9f84219715a7
parent 56154 f0a927235162
child 57816 d8bbb97689d3
     1.1 --- a/src/HOL/Library/Permutation.thy	Tue Apr 29 21:54:26 2014 +0200
     1.2 +++ b/src/HOL/Library/Permutation.thy	Tue Apr 29 22:50:55 2014 +0200
     1.3 @@ -22,12 +22,10 @@
     1.4  subsection {* Some examples of rule induction on permutations *}
     1.5  
     1.6  lemma xperm_empty_imp: "[] <~~> ys \<Longrightarrow> ys = []"
     1.7 -  by (induct xs == "[]::'a list" ys pred: perm) simp_all
     1.8 +  by (induct xs == "[] :: 'a list" ys pred: perm) simp_all
     1.9  
    1.10  
    1.11 -text {*
    1.12 -  \medskip This more general theorem is easier to understand!
    1.13 -  *}
    1.14 +text {* \medskip This more general theorem is easier to understand! *}
    1.15  
    1.16  lemma perm_length: "xs <~~> ys \<Longrightarrow> length xs = length ys"
    1.17    by (induct pred: perm) simp_all
    1.18 @@ -41,9 +39,7 @@
    1.19  
    1.20  subsection {* Ways of making new permutations *}
    1.21  
    1.22 -text {*
    1.23 -  We can insert the head anywhere in the list.
    1.24 -*}
    1.25 +text {* We can insert the head anywhere in the list. *}
    1.26  
    1.27  lemma perm_append_Cons: "a # xs @ ys <~~> xs @ a # ys"
    1.28    by (induct xs) auto
    1.29 @@ -72,10 +68,10 @@
    1.30  
    1.31  subsection {* Further results *}
    1.32  
    1.33 -lemma perm_empty [iff]: "([] <~~> xs) = (xs = [])"
    1.34 +lemma perm_empty [iff]: "[] <~~> xs \<longleftrightarrow> xs = []"
    1.35    by (blast intro: perm_empty_imp)
    1.36  
    1.37 -lemma perm_empty2 [iff]: "(xs <~~> []) = (xs = [])"
    1.38 +lemma perm_empty2 [iff]: "xs <~~> [] \<longleftrightarrow> xs = []"
    1.39    apply auto
    1.40    apply (erule perm_sym [THEN perm_empty_imp])
    1.41    done
    1.42 @@ -83,10 +79,10 @@
    1.43  lemma perm_sing_imp: "ys <~~> xs \<Longrightarrow> xs = [y] \<Longrightarrow> ys = [y]"
    1.44    by (induct pred: perm) auto
    1.45  
    1.46 -lemma perm_sing_eq [iff]: "(ys <~~> [y]) = (ys = [y])"
    1.47 +lemma perm_sing_eq [iff]: "ys <~~> [y] \<longleftrightarrow> ys = [y]"
    1.48    by (blast intro: perm_sing_imp)
    1.49  
    1.50 -lemma perm_sing_eq2 [iff]: "([y] <~~> ys) = (ys = [y])"
    1.51 +lemma perm_sing_eq2 [iff]: "[y] <~~> ys \<longleftrightarrow> ys = [y]"
    1.52    by (blast dest: perm_sym)
    1.53  
    1.54  
    1.55 @@ -107,16 +103,16 @@
    1.56  lemma cons_perm_imp_perm: "z # xs <~~> z # ys \<Longrightarrow> xs <~~> ys"
    1.57    by (drule_tac z = z in perm_remove_perm) auto
    1.58  
    1.59 -lemma cons_perm_eq [iff]: "(z#xs <~~> z#ys) = (xs <~~> ys)"
    1.60 +lemma cons_perm_eq [iff]: "z#xs <~~> z#ys \<longleftrightarrow> xs <~~> ys"
    1.61    by (blast intro: cons_perm_imp_perm)
    1.62  
    1.63  lemma append_perm_imp_perm: "zs @ xs <~~> zs @ ys \<Longrightarrow> xs <~~> ys"
    1.64    by (induct zs arbitrary: xs ys rule: rev_induct) auto
    1.65  
    1.66 -lemma perm_append1_eq [iff]: "(zs @ xs <~~> zs @ ys) = (xs <~~> ys)"
    1.67 +lemma perm_append1_eq [iff]: "zs @ xs <~~> zs @ ys \<longleftrightarrow> xs <~~> ys"
    1.68    by (blast intro: append_perm_imp_perm perm_append1)
    1.69  
    1.70 -lemma perm_append2_eq [iff]: "(xs @ zs <~~> ys @ zs) = (xs <~~> ys)"
    1.71 +lemma perm_append2_eq [iff]: "xs @ zs <~~> ys @ zs \<longleftrightarrow> xs <~~> ys"
    1.72    apply (safe intro!: perm_append2)
    1.73    apply (rule append_perm_imp_perm)
    1.74    apply (rule perm_append_swap [THEN perm.trans])
    1.75 @@ -124,21 +120,30 @@
    1.76    apply (blast intro: perm_append_swap)
    1.77    done
    1.78  
    1.79 -lemma multiset_of_eq_perm: "(multiset_of xs = multiset_of ys) = (xs <~~> ys) "
    1.80 +lemma multiset_of_eq_perm: "multiset_of xs = multiset_of ys \<longleftrightarrow> xs <~~> ys"
    1.81    apply (rule iffI)
    1.82 -  apply (erule_tac [2] perm.induct, simp_all add: union_ac)
    1.83 -  apply (erule rev_mp, rule_tac x=ys in spec)
    1.84 -  apply (induct_tac xs, auto)
    1.85 -  apply (erule_tac x = "remove1 a x" in allE, drule sym, simp)
    1.86 +  apply (erule_tac [2] perm.induct)
    1.87 +  apply (simp_all add: union_ac)
    1.88 +  apply (erule rev_mp)
    1.89 +  apply (rule_tac x=ys in spec)
    1.90 +  apply (induct_tac xs)
    1.91 +  apply auto
    1.92 +  apply (erule_tac x = "remove1 a x" in allE)
    1.93 +  apply (drule sym)
    1.94 +  apply simp
    1.95    apply (subgoal_tac "a \<in> set x")
    1.96    apply (drule_tac z = a in perm.Cons)
    1.97 -  apply (erule perm.trans, rule perm_sym, erule perm_remove)
    1.98 -  apply (drule_tac f=set_of in arg_cong, simp)
    1.99 +  apply (erule perm.trans)
   1.100 +  apply (rule perm_sym)
   1.101 +  apply (erule perm_remove)
   1.102 +  apply (drule_tac f=set_of in arg_cong)
   1.103 +  apply simp
   1.104    done
   1.105  
   1.106  lemma multiset_of_le_perm_append: "multiset_of xs \<le> multiset_of ys \<longleftrightarrow> (\<exists>zs. xs @ zs <~~> ys)"
   1.107    apply (auto simp: multiset_of_eq_perm[THEN sym] mset_le_exists_conv)
   1.108 -  apply (insert surj_multiset_of, drule surjD)
   1.109 +  apply (insert surj_multiset_of)
   1.110 +  apply (drule surjD)
   1.111    apply (blast intro: sym)+
   1.112    done
   1.113  
   1.114 @@ -158,15 +163,16 @@
   1.115     apply simp_all
   1.116    apply (subgoal_tac "a \<in> set (remdups ys)")
   1.117     prefer 2 apply (metis set_simps(2) insert_iff set_remdups)
   1.118 -  apply (drule split_list) apply(elim exE conjE)
   1.119 -  apply (drule_tac x=list in spec) apply(erule impE) prefer 2
   1.120 -   apply (drule_tac x="ysa@zs" in spec) apply(erule impE) prefer 2
   1.121 +  apply (drule split_list) apply (elim exE conjE)
   1.122 +  apply (drule_tac x = list in spec) apply (erule impE) prefer 2
   1.123 +   apply (drule_tac x = "ysa @ zs" in spec) apply (erule impE) prefer 2
   1.124      apply simp
   1.125      apply (subgoal_tac "a # list <~~> a # ysa @ zs")
   1.126       apply (metis Cons_eq_appendI perm_append_Cons trans)
   1.127      apply (metis Cons Cons_eq_appendI distinct.simps(2)
   1.128        distinct_remdups distinct_remdups_id perm_append_swap perm_distinct_iff)
   1.129 -   apply (subgoal_tac "set (a#list) = set (ysa@a#zs) & distinct (a#list) & distinct (ysa@a#zs)")
   1.130 +   apply (subgoal_tac "set (a # list) =
   1.131 +      set (ysa @ a # zs) \<and> distinct (a # list) \<and> distinct (ysa @ a # zs)")
   1.132      apply (fastforce simp add: insert_ident)
   1.133     apply (metis distinct_remdups set_remdups)
   1.134     apply (subgoal_tac "length (remdups xs) < Suc (length xs)")
   1.135 @@ -176,15 +182,17 @@
   1.136     apply (rule length_remdups_leq)
   1.137    done
   1.138  
   1.139 -lemma perm_remdups_iff_eq_set: "remdups x <~~> remdups y \<longleftrightarrow> (set x = set y)"
   1.140 +lemma perm_remdups_iff_eq_set: "remdups x <~~> remdups y \<longleftrightarrow> set x = set y"
   1.141    by (metis List.set_remdups perm_set_eq eq_set_perm_remdups)
   1.142  
   1.143  lemma permutation_Ex_bij:
   1.144    assumes "xs <~~> ys"
   1.145    shows "\<exists>f. bij_betw f {..<length xs} {..<length ys} \<and> (\<forall>i<length xs. xs ! i = ys ! (f i))"
   1.146 -using assms proof induct
   1.147 +  using assms
   1.148 +proof induct
   1.149    case Nil
   1.150 -  then show ?case unfolding bij_betw_def by simp
   1.151 +  then show ?case
   1.152 +    unfolding bij_betw_def by simp
   1.153  next
   1.154    case (swap y x l)
   1.155    show ?case
   1.156 @@ -192,14 +200,15 @@
   1.157      show "bij_betw (Fun.swap 0 1 id) {..<length (y # x # l)} {..<length (x # y # l)}"
   1.158        by (auto simp: bij_betw_def)
   1.159      fix i
   1.160 -    assume "i < length(y#x#l)"
   1.161 +    assume "i < length (y # x # l)"
   1.162      show "(y # x # l) ! i = (x # y # l) ! (Fun.swap 0 1 id) i"
   1.163        by (cases i) (auto simp: Fun.swap_def gr0_conv_Suc)
   1.164    qed
   1.165  next
   1.166    case (Cons xs ys z)
   1.167 -  then obtain f where bij: "bij_betw f {..<length xs} {..<length ys}" and
   1.168 -    perm: "\<forall>i<length xs. xs ! i = ys ! (f i)" by blast
   1.169 +  then obtain f where bij: "bij_betw f {..<length xs} {..<length ys}"
   1.170 +    and perm: "\<forall>i<length xs. xs ! i = ys ! (f i)"
   1.171 +    by blast
   1.172    let ?f = "\<lambda>i. case i of Suc n \<Rightarrow> Suc (f n) | 0 \<Rightarrow> 0"
   1.173    show ?case
   1.174    proof (intro exI[of _ ?f] allI conjI impI)
   1.175 @@ -214,21 +223,24 @@
   1.176          by (auto intro!: inj_onI imageI dest: inj_onD simp: image_comp comp_def)
   1.177      qed (auto simp: bij_betw_def)
   1.178      fix i
   1.179 -    assume "i < length (z#xs)"
   1.180 +    assume "i < length (z # xs)"
   1.181      then show "(z # xs) ! i = (z # ys) ! (?f i)"
   1.182        using perm by (cases i) auto
   1.183    qed
   1.184  next
   1.185    case (trans xs ys zs)
   1.186 -  then obtain f g where
   1.187 -    bij: "bij_betw f {..<length xs} {..<length ys}" "bij_betw g {..<length ys} {..<length zs}" and
   1.188 -    perm: "\<forall>i<length xs. xs ! i = ys ! (f i)" "\<forall>i<length ys. ys ! i = zs ! (g i)" by blast
   1.189 +  then obtain f g
   1.190 +    where bij: "bij_betw f {..<length xs} {..<length ys}" "bij_betw g {..<length ys} {..<length zs}"
   1.191 +    and perm: "\<forall>i<length xs. xs ! i = ys ! (f i)" "\<forall>i<length ys. ys ! i = zs ! (g i)"
   1.192 +    by blast
   1.193    show ?case
   1.194    proof (intro exI[of _ "g \<circ> f"] conjI allI impI)
   1.195      show "bij_betw (g \<circ> f) {..<length xs} {..<length zs}"
   1.196        using bij by (rule bij_betw_trans)
   1.197 -    fix i assume "i < length xs"
   1.198 -    with bij have "f i < length ys" unfolding bij_betw_def by force
   1.199 +    fix i
   1.200 +    assume "i < length xs"
   1.201 +    with bij have "f i < length ys"
   1.202 +      unfolding bij_betw_def by force
   1.203      with `i < length xs` show "xs ! i = zs ! (g \<circ> f) i"
   1.204        using trans(1,3)[THEN perm_length] perm by auto
   1.205    qed