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

src/HOL/Library/Permutation.thy

author | chaieb |

Mon Jun 11 11:06:04 2007 +0200 (2007-06-11) | |

changeset 23315 | df3a7e9ebadb |

parent 21404 | eb85850d3eb7 |

child 23755 | 1c4672d130b1 |

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

tuned Proof

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 Multiset

9 begin

11 consts

12 perm :: "('a list * 'a list) set"

14 abbreviation

15 perm_rel :: "'a list => 'a list => bool" ("_ <~~> _" [50, 50] 50) where

16 "x <~~> y == (x, y) \<in> perm"

18 inductive perm

19 intros

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

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

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

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

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

26 by (induct l) auto

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

31 lemma xperm_empty_imp_aux: "xs <~~> ys ==> xs = [] --> ys = []"

32 -- {*the form of the premise lets the induction bind @{term xs}

33 and @{term ys} *}

34 apply (erule perm.induct)

35 apply (simp_all (no_asm_simp))

36 done

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

39 using xperm_empty_imp_aux by blast

42 text {*

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

44 *}

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

47 by (erule perm.induct) simp_all

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

50 by (drule perm_length) auto

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

53 by (erule perm.induct) auto

55 lemma perm_mem [rule_format]: "xs <~~> ys ==> x mem xs --> x mem ys"

56 by (erule perm.induct) auto

59 subsection {* Ways of making new permutations *}

61 text {*

62 We can insert the head anywhere in the list.

63 *}

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

66 by (induct xs) auto

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

69 apply (induct xs)

70 apply simp_all

71 apply (blast intro: perm_append_Cons)

72 done

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

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

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

78 apply (induct xs)

79 apply simp_all

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

81 done

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

84 by (induct l) auto

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

87 by (blast intro!: perm_append_swap perm_append1)

90 subsection {* Further results *}

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

93 by (blast intro: perm_empty_imp)

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

96 apply auto

97 apply (erule perm_sym [THEN perm_empty_imp])

98 done

100 lemma perm_sing_imp [rule_format]: "ys <~~> xs ==> xs = [y] --> ys = [y]"

101 by (erule perm.induct) auto

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

104 by (blast intro: perm_sing_imp)

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

107 by (blast dest: perm_sym)

110 subsection {* Removing elements *}

112 consts

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

114 primrec

115 "remove x [] = []"

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

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

119 by (induct ys) auto

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

122 by (induct l) auto

124 lemma multiset_of_remove[simp]:

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

126 apply (induct x)

127 apply (auto simp: multiset_eq_conv_count_eq)

128 done

131 text {* \medskip Congruence rule *}

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

134 by (erule perm.induct) auto

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

137 by auto

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

140 by (drule_tac z = z in perm_remove_perm) auto

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

143 by (blast intro: cons_perm_imp_perm)

145 lemma append_perm_imp_perm: "!!xs ys. zs @ xs <~~> zs @ ys ==> xs <~~> ys"

146 apply (induct zs rule: rev_induct)

147 apply (simp_all (no_asm_use))

148 apply blast

149 done

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

152 by (blast intro: append_perm_imp_perm perm_append1)

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

155 apply (safe intro!: perm_append2)

156 apply (rule append_perm_imp_perm)

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

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

159 apply (blast intro: perm_append_swap)

160 done

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

163 apply (rule iffI)

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

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

166 apply (induct_tac xs, auto)

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

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

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

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

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

172 done

174 lemma multiset_of_le_perm_append:

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

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

177 apply (insert surj_multiset_of, drule surjD)

178 apply (blast intro: sym)+

179 done

181 end