summary |
shortlog |
changelog |
graph |
tags |
branches |
files |
changeset |
file |
revisions |
annotate |
diff |
raw

src/HOL/Library/Permutation.thy

author | haftmann |

Wed Apr 22 19:09:21 2009 +0200 (2009-04-22) | |

changeset 30960 | fec1a04b7220 |

parent 30742 | 3e89ac3905b9 |

child 33498 | 318acc1c9399 |

permissions | -rw-r--r-- |

power operation defined generic

1 (* Title: HOL/Library/Permutation.thy

2 Author: Lawrence C Paulson and Thomas M Rasmussen and Norbert Voelker

3 *)

5 header {* Permutations *}

7 theory Permutation

8 imports Main Multiset

9 begin

11 inductive

12 perm :: "'a list => 'a list => bool" ("_ <~~> _" [50, 50] 50)

13 where

14 Nil [intro!]: "[] <~~> []"

15 | swap [intro!]: "y # x # l <~~> x # y # l"

16 | Cons [intro!]: "xs <~~> ys ==> z # xs <~~> z # ys"

17 | trans [intro]: "xs <~~> ys ==> ys <~~> zs ==> xs <~~> zs"

19 lemma perm_refl [iff]: "l <~~> l"

20 by (induct l) auto

23 subsection {* Some examples of rule induction on permutations *}

25 lemma xperm_empty_imp: "[] <~~> ys ==> ys = []"

26 by (induct xs == "[]::'a list" ys pred: perm) simp_all

29 text {*

30 \medskip This more general theorem is easier to understand!

31 *}

33 lemma perm_length: "xs <~~> ys ==> length xs = length ys"

34 by (induct pred: perm) simp_all

36 lemma perm_empty_imp: "[] <~~> xs ==> xs = []"

37 by (drule perm_length) auto

39 lemma perm_sym: "xs <~~> ys ==> ys <~~> xs"

40 by (induct pred: perm) auto

43 subsection {* Ways of making new permutations *}

45 text {*

46 We can insert the head anywhere in the list.

47 *}

49 lemma perm_append_Cons: "a # xs @ ys <~~> xs @ a # ys"

50 by (induct xs) auto

52 lemma perm_append_swap: "xs @ ys <~~> ys @ xs"

53 apply (induct xs)

54 apply simp_all

55 apply (blast intro: perm_append_Cons)

56 done

58 lemma perm_append_single: "a # xs <~~> xs @ [a]"

59 by (rule perm.trans [OF _ perm_append_swap]) simp

61 lemma perm_rev: "rev xs <~~> xs"

62 apply (induct xs)

63 apply simp_all

64 apply (blast intro!: perm_append_single intro: perm_sym)

65 done

67 lemma perm_append1: "xs <~~> ys ==> l @ xs <~~> l @ ys"

68 by (induct l) auto

70 lemma perm_append2: "xs <~~> ys ==> xs @ l <~~> ys @ l"

71 by (blast intro!: perm_append_swap perm_append1)

74 subsection {* Further results *}

76 lemma perm_empty [iff]: "([] <~~> xs) = (xs = [])"

77 by (blast intro: perm_empty_imp)

79 lemma perm_empty2 [iff]: "(xs <~~> []) = (xs = [])"

80 apply auto

81 apply (erule perm_sym [THEN perm_empty_imp])

82 done

84 lemma perm_sing_imp: "ys <~~> xs ==> xs = [y] ==> ys = [y]"

85 by (induct pred: perm) auto

87 lemma perm_sing_eq [iff]: "(ys <~~> [y]) = (ys = [y])"

88 by (blast intro: perm_sing_imp)

90 lemma perm_sing_eq2 [iff]: "([y] <~~> ys) = (ys = [y])"

91 by (blast dest: perm_sym)

94 subsection {* Removing elements *}

96 consts

97 remove :: "'a => 'a list => 'a list"

98 primrec

99 "remove x [] = []"

100 "remove x (y # ys) = (if x = y then ys else y # remove x ys)"

102 lemma perm_remove: "x \<in> set ys ==> ys <~~> x # remove x ys"

103 by (induct ys) auto

105 lemma remove_commute: "remove x (remove y l) = remove y (remove x l)"

106 by (induct l) auto

108 lemma multiset_of_remove [simp]:

109 "multiset_of (remove a x) = multiset_of x - {#a#}"

110 apply (induct x)

111 apply (auto simp: multiset_eq_conv_count_eq)

112 done

115 text {* \medskip Congruence rule *}

117 lemma perm_remove_perm: "xs <~~> ys ==> remove z xs <~~> remove z ys"

118 by (induct pred: perm) auto

120 lemma remove_hd [simp]: "remove z (z # xs) = xs"

121 by auto

123 lemma cons_perm_imp_perm: "z # xs <~~> z # ys ==> xs <~~> ys"

124 by (drule_tac z = z in perm_remove_perm) auto

126 lemma cons_perm_eq [iff]: "(z#xs <~~> z#ys) = (xs <~~> ys)"

127 by (blast intro: cons_perm_imp_perm)

129 lemma append_perm_imp_perm: "zs @ xs <~~> zs @ ys ==> xs <~~> ys"

130 apply (induct zs arbitrary: xs ys rule: rev_induct)

131 apply (simp_all (no_asm_use))

132 apply blast

133 done

135 lemma perm_append1_eq [iff]: "(zs @ xs <~~> zs @ ys) = (xs <~~> ys)"

136 by (blast intro: append_perm_imp_perm perm_append1)

138 lemma perm_append2_eq [iff]: "(xs @ zs <~~> ys @ zs) = (xs <~~> ys)"

139 apply (safe intro!: perm_append2)

140 apply (rule append_perm_imp_perm)

141 apply (rule perm_append_swap [THEN perm.trans])

142 -- {* the previous step helps this @{text blast} call succeed quickly *}

143 apply (blast intro: perm_append_swap)

144 done

146 lemma multiset_of_eq_perm: "(multiset_of xs = multiset_of ys) = (xs <~~> ys) "

147 apply (rule iffI)

148 apply (erule_tac [2] perm.induct, simp_all add: union_ac)

149 apply (erule rev_mp, rule_tac x=ys in spec)

150 apply (induct_tac xs, auto)

151 apply (erule_tac x = "remove a x" in allE, drule sym, simp)

152 apply (subgoal_tac "a \<in> set x")

153 apply (drule_tac z=a in perm.Cons)

154 apply (erule perm.trans, rule perm_sym, erule perm_remove)

155 apply (drule_tac f=set_of in arg_cong, simp)

156 done

158 lemma multiset_of_le_perm_append:

159 "(multiset_of xs \<le># multiset_of ys) = (\<exists>zs. xs @ zs <~~> ys)";

160 apply (auto simp: multiset_of_eq_perm[THEN sym] mset_le_exists_conv)

161 apply (insert surj_multiset_of, drule surjD)

162 apply (blast intro: sym)+

163 done

165 lemma perm_set_eq: "xs <~~> ys ==> set xs = set ys"

166 by (metis multiset_of_eq_perm multiset_of_eq_setD)

168 lemma perm_distinct_iff: "xs <~~> ys ==> distinct xs = distinct ys"

169 apply (induct pred: perm)

170 apply simp_all

171 apply fastsimp

172 apply (metis perm_set_eq)

173 done

175 lemma eq_set_perm_remdups: "set xs = set ys ==> remdups xs <~~> remdups ys"

176 apply (induct xs arbitrary: ys rule: length_induct)

177 apply (case_tac "remdups xs", simp, simp)

178 apply (subgoal_tac "a : set (remdups ys)")

179 prefer 2 apply (metis set.simps(2) insert_iff set_remdups)

180 apply (drule split_list) apply(elim exE conjE)

181 apply (drule_tac x=list in spec) apply(erule impE) prefer 2

182 apply (drule_tac x="ysa@zs" in spec) apply(erule impE) prefer 2

183 apply simp

184 apply (subgoal_tac "a#list <~~> a#ysa@zs")

185 apply (metis Cons_eq_appendI perm_append_Cons trans)

186 apply (metis Cons Cons_eq_appendI distinct.simps(2)

187 distinct_remdups distinct_remdups_id perm_append_swap perm_distinct_iff)

188 apply (subgoal_tac "set (a#list) = set (ysa@a#zs) & distinct (a#list) & distinct (ysa@a#zs)")

189 apply (fastsimp simp add: insert_ident)

190 apply (metis distinct_remdups set_remdups)

191 apply (subgoal_tac "length (remdups xs) < Suc (length xs)")

192 apply simp

193 apply (subgoal_tac "length (remdups xs) \<le> length xs")

194 apply simp

195 apply (rule length_remdups_leq)

196 done

198 lemma perm_remdups_iff_eq_set: "remdups x <~~> remdups y = (set x = set y)"

199 by (metis List.set_remdups perm_set_eq eq_set_perm_remdups)

201 end