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

src/HOL/Library/Permutation.thy

author | nipkow |

Mon Nov 05 08:31:12 2007 +0100 (2007-11-05) | |

changeset 25277 | 95128fcdd7e8 |

parent 23755 | 1c4672d130b1 |

child 25287 | 094dab519ff5 |

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

added lemmas

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 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_aux: "xs <~~> ys ==> xs = [] --> ys = []"

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

27 and @{term ys} *}

28 apply (erule perm.induct)

29 apply (simp_all (no_asm_simp))

30 done

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

33 using xperm_empty_imp_aux by blast

36 text {*

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

38 *}

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

41 by (erule perm.induct) simp_all

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

44 by (drule perm_length) auto

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

47 by (erule perm.induct) auto

50 subsection {* Ways of making new permutations *}

52 text {*

53 We can insert the head anywhere in the list.

54 *}

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

57 by (induct xs) auto

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

60 apply (induct xs)

61 apply simp_all

62 apply (blast intro: perm_append_Cons)

63 done

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

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

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

69 apply (induct xs)

70 apply simp_all

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

72 done

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

75 by (induct l) auto

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

78 by (blast intro!: perm_append_swap perm_append1)

81 subsection {* Further results *}

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

84 by (blast intro: perm_empty_imp)

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

87 apply auto

88 apply (erule perm_sym [THEN perm_empty_imp])

89 done

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

92 by (erule perm.induct) auto

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

95 by (blast intro: perm_sing_imp)

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

98 by (blast dest: perm_sym)

101 subsection {* Removing elements *}

103 consts

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

105 primrec

106 "remove x [] = []"

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

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

110 by (induct ys) auto

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

113 by (induct l) auto

115 lemma multiset_of_remove[simp]:

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

117 apply (induct x)

118 apply (auto simp: multiset_eq_conv_count_eq)

119 done

122 text {* \medskip Congruence rule *}

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

125 by (erule perm.induct) auto

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

128 by auto

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

131 by (drule_tac z = z in perm_remove_perm) auto

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

134 by (blast intro: cons_perm_imp_perm)

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

137 apply (induct zs rule: rev_induct)

138 apply (simp_all (no_asm_use))

139 apply blast

140 done

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

143 by (blast intro: append_perm_imp_perm perm_append1)

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

146 apply (safe intro!: perm_append2)

147 apply (rule append_perm_imp_perm)

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

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

150 apply (blast intro: perm_append_swap)

151 done

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

154 apply (rule iffI)

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

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

157 apply (induct_tac xs, auto)

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

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

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

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

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

163 done

165 lemma multiset_of_le_perm_append:

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

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

168 apply (insert surj_multiset_of, drule surjD)

169 apply (blast intro: sym)+

170 done

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

173 by (metis multiset_of_eq_perm multiset_of_eq_setD)

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

176 apply(induct rule:perm.induct)

177 apply simp_all

178 apply fastsimp

179 apply (metis perm_set_eq)

180 done

182 end