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

src/HOL/Library/Dlist.thy

author | wenzelm |

Mon Dec 28 01:28:28 2015 +0100 (2015-12-28) | |

changeset 61945 | 1135b8de26c3 |

parent 61115 | 3a4400985780 |

child 62139 | 519362f817c7 |

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

more symbols;

1 (* Author: Florian Haftmann, TU Muenchen *)

3 section \<open>Lists with elements distinct as canonical example for datatype invariants\<close>

5 theory Dlist

6 imports Main

7 begin

9 subsection \<open>The type of distinct lists\<close>

11 typedef 'a dlist = "{xs::'a list. distinct xs}"

12 morphisms list_of_dlist Abs_dlist

13 proof

14 show "[] \<in> {xs. distinct xs}" by simp

15 qed

17 lemma dlist_eq_iff:

18 "dxs = dys \<longleftrightarrow> list_of_dlist dxs = list_of_dlist dys"

19 by (simp add: list_of_dlist_inject)

21 lemma dlist_eqI:

22 "list_of_dlist dxs = list_of_dlist dys \<Longrightarrow> dxs = dys"

23 by (simp add: dlist_eq_iff)

25 text \<open>Formal, totalized constructor for @{typ "'a dlist"}:\<close>

27 definition Dlist :: "'a list \<Rightarrow> 'a dlist" where

28 "Dlist xs = Abs_dlist (remdups xs)"

30 lemma distinct_list_of_dlist [simp, intro]:

31 "distinct (list_of_dlist dxs)"

32 using list_of_dlist [of dxs] by simp

34 lemma list_of_dlist_Dlist [simp]:

35 "list_of_dlist (Dlist xs) = remdups xs"

36 by (simp add: Dlist_def Abs_dlist_inverse)

38 lemma remdups_list_of_dlist [simp]:

39 "remdups (list_of_dlist dxs) = list_of_dlist dxs"

40 by simp

42 lemma Dlist_list_of_dlist [simp, code abstype]:

43 "Dlist (list_of_dlist dxs) = dxs"

44 by (simp add: Dlist_def list_of_dlist_inverse distinct_remdups_id)

47 text \<open>Fundamental operations:\<close>

49 context

50 begin

52 qualified definition empty :: "'a dlist" where

53 "empty = Dlist []"

55 qualified definition insert :: "'a \<Rightarrow> 'a dlist \<Rightarrow> 'a dlist" where

56 "insert x dxs = Dlist (List.insert x (list_of_dlist dxs))"

58 qualified definition remove :: "'a \<Rightarrow> 'a dlist \<Rightarrow> 'a dlist" where

59 "remove x dxs = Dlist (remove1 x (list_of_dlist dxs))"

61 qualified definition map :: "('a \<Rightarrow> 'b) \<Rightarrow> 'a dlist \<Rightarrow> 'b dlist" where

62 "map f dxs = Dlist (remdups (List.map f (list_of_dlist dxs)))"

64 qualified definition filter :: "('a \<Rightarrow> bool) \<Rightarrow> 'a dlist \<Rightarrow> 'a dlist" where

65 "filter P dxs = Dlist (List.filter P (list_of_dlist dxs))"

67 end

70 text \<open>Derived operations:\<close>

72 context

73 begin

75 qualified definition null :: "'a dlist \<Rightarrow> bool" where

76 "null dxs = List.null (list_of_dlist dxs)"

78 qualified definition member :: "'a dlist \<Rightarrow> 'a \<Rightarrow> bool" where

79 "member dxs = List.member (list_of_dlist dxs)"

81 qualified definition length :: "'a dlist \<Rightarrow> nat" where

82 "length dxs = List.length (list_of_dlist dxs)"

84 qualified definition fold :: "('a \<Rightarrow> 'b \<Rightarrow> 'b) \<Rightarrow> 'a dlist \<Rightarrow> 'b \<Rightarrow> 'b" where

85 "fold f dxs = List.fold f (list_of_dlist dxs)"

87 qualified definition foldr :: "('a \<Rightarrow> 'b \<Rightarrow> 'b) \<Rightarrow> 'a dlist \<Rightarrow> 'b \<Rightarrow> 'b" where

88 "foldr f dxs = List.foldr f (list_of_dlist dxs)"

90 end

93 subsection \<open>Executable version obeying invariant\<close>

95 lemma list_of_dlist_empty [simp, code abstract]:

96 "list_of_dlist Dlist.empty = []"

97 by (simp add: Dlist.empty_def)

99 lemma list_of_dlist_insert [simp, code abstract]:

100 "list_of_dlist (Dlist.insert x dxs) = List.insert x (list_of_dlist dxs)"

101 by (simp add: Dlist.insert_def)

103 lemma list_of_dlist_remove [simp, code abstract]:

104 "list_of_dlist (Dlist.remove x dxs) = remove1 x (list_of_dlist dxs)"

105 by (simp add: Dlist.remove_def)

107 lemma list_of_dlist_map [simp, code abstract]:

108 "list_of_dlist (Dlist.map f dxs) = remdups (List.map f (list_of_dlist dxs))"

109 by (simp add: Dlist.map_def)

111 lemma list_of_dlist_filter [simp, code abstract]:

112 "list_of_dlist (Dlist.filter P dxs) = List.filter P (list_of_dlist dxs)"

113 by (simp add: Dlist.filter_def)

116 text \<open>Explicit executable conversion\<close>

118 definition dlist_of_list [simp]:

119 "dlist_of_list = Dlist"

121 lemma [code abstract]:

122 "list_of_dlist (dlist_of_list xs) = remdups xs"

123 by simp

126 text \<open>Equality\<close>

128 instantiation dlist :: (equal) equal

129 begin

131 definition "HOL.equal dxs dys \<longleftrightarrow> HOL.equal (list_of_dlist dxs) (list_of_dlist dys)"

133 instance

134 by standard (simp add: equal_dlist_def equal list_of_dlist_inject)

136 end

138 declare equal_dlist_def [code]

140 lemma [code nbe]: "HOL.equal (dxs :: 'a::equal dlist) dxs \<longleftrightarrow> True"

141 by (fact equal_refl)

144 subsection \<open>Induction principle and case distinction\<close>

146 lemma dlist_induct [case_names empty insert, induct type: dlist]:

147 assumes empty: "P Dlist.empty"

148 assumes insrt: "\<And>x dxs. \<not> Dlist.member dxs x \<Longrightarrow> P dxs \<Longrightarrow> P (Dlist.insert x dxs)"

149 shows "P dxs"

150 proof (cases dxs)

151 case (Abs_dlist xs)

152 then have "distinct xs" and dxs: "dxs = Dlist xs"

153 by (simp_all add: Dlist_def distinct_remdups_id)

154 from \<open>distinct xs\<close> have "P (Dlist xs)"

155 proof (induct xs)

156 case Nil from empty show ?case by (simp add: Dlist.empty_def)

157 next

158 case (Cons x xs)

159 then have "\<not> Dlist.member (Dlist xs) x" and "P (Dlist xs)"

160 by (simp_all add: Dlist.member_def List.member_def)

161 with insrt have "P (Dlist.insert x (Dlist xs))" .

162 with Cons show ?case by (simp add: Dlist.insert_def distinct_remdups_id)

163 qed

164 with dxs show "P dxs" by simp

165 qed

167 lemma dlist_case [cases type: dlist]:

168 obtains (empty) "dxs = Dlist.empty"

169 | (insert) x dys where "\<not> Dlist.member dys x" and "dxs = Dlist.insert x dys"

170 proof (cases dxs)

171 case (Abs_dlist xs)

172 then have dxs: "dxs = Dlist xs" and distinct: "distinct xs"

173 by (simp_all add: Dlist_def distinct_remdups_id)

174 show thesis

175 proof (cases xs)

176 case Nil with dxs

177 have "dxs = Dlist.empty" by (simp add: Dlist.empty_def)

178 with empty show ?thesis .

179 next

180 case (Cons x xs)

181 with dxs distinct have "\<not> Dlist.member (Dlist xs) x"

182 and "dxs = Dlist.insert x (Dlist xs)"

183 by (simp_all add: Dlist.member_def List.member_def Dlist.insert_def distinct_remdups_id)

184 with insert show ?thesis .

185 qed

186 qed

189 subsection \<open>Functorial structure\<close>

191 functor map: map

192 by (simp_all add: remdups_map_remdups fun_eq_iff dlist_eq_iff)

195 subsection \<open>Quickcheck generators\<close>

197 quickcheck_generator dlist predicate: distinct constructors: Dlist.empty, Dlist.insert

199 end