src/HOL/Library/Dlist.thy
 author haftmann Mon Feb 22 15:53:18 2010 +0100 (2010-02-22) changeset 35303 816e48d60b13 child 35688 cfe0accda6e3 permissions -rw-r--r--
```     1 (* Author: Florian Haftmann, TU Muenchen *)
```
```     2
```
```     3 header {* Lists with elements distinct as canonical example for datatype invariants *}
```
```     4
```
```     5 theory Dlist
```
```     6 imports Main Fset
```
```     7 begin
```
```     8
```
```     9 section {* Prelude *}
```
```    10
```
```    11 text {* Without canonical argument order, higher-order things tend to get confusing quite fast: *}
```
```    12
```
```    13 setup {* Sign.map_naming (Name_Space.add_path "List") *}
```
```    14
```
```    15 primrec member :: "'a list \<Rightarrow> 'a \<Rightarrow> bool" where
```
```    16     "member [] y \<longleftrightarrow> False"
```
```    17   | "member (x#xs) y \<longleftrightarrow> x = y \<or> member xs y"
```
```    18
```
```    19 lemma member_set:
```
```    20   "member = set"
```
```    21 proof (rule ext)+
```
```    22   fix xs :: "'a list" and x :: 'a
```
```    23   have "member xs x \<longleftrightarrow> x \<in> set xs" by (induct xs) auto
```
```    24   then show "member xs x = set xs x" by (simp add: mem_def)
```
```    25 qed
```
```    26
```
```    27 lemma not_set_compl:
```
```    28   "Not \<circ> set xs = - set xs"
```
```    29   by (simp add: fun_Compl_def bool_Compl_def comp_def expand_fun_eq)
```
```    30
```
```    31 primrec fold :: "('a \<Rightarrow> 'b \<Rightarrow> 'b) \<Rightarrow> 'a list \<Rightarrow> 'b \<Rightarrow> 'b" where
```
```    32     "fold f [] s = s"
```
```    33   | "fold f (x#xs) s = fold f xs (f x s)"
```
```    34
```
```    35 lemma foldl_fold:
```
```    36   "foldl f s xs = List.fold (\<lambda>x s. f s x) xs s"
```
```    37   by (induct xs arbitrary: s) simp_all
```
```    38
```
```    39 setup {* Sign.map_naming Name_Space.parent_path *}
```
```    40
```
```    41
```
```    42 section {* The type of distinct lists *}
```
```    43
```
```    44 typedef (open) 'a dlist = "{xs::'a list. distinct xs}"
```
```    45   morphisms list_of_dlist Abs_dlist
```
```    46 proof
```
```    47   show "[] \<in> ?dlist" by simp
```
```    48 qed
```
```    49
```
```    50 text {* Formal, totalized constructor for @{typ "'a dlist"}: *}
```
```    51
```
```    52 definition Dlist :: "'a list \<Rightarrow> 'a dlist" where
```
```    53   [code del]: "Dlist xs = Abs_dlist (remdups xs)"
```
```    54
```
```    55 lemma distinct_list_of_dlist [simp]:
```
```    56   "distinct (list_of_dlist dxs)"
```
```    57   using list_of_dlist [of dxs] by simp
```
```    58
```
```    59 lemma list_of_dlist_Dlist [simp]:
```
```    60   "list_of_dlist (Dlist xs) = remdups xs"
```
```    61   by (simp add: Dlist_def Abs_dlist_inverse)
```
```    62
```
```    63 lemma Dlist_list_of_dlist [simp]:
```
```    64   "Dlist (list_of_dlist dxs) = dxs"
```
```    65   by (simp add: Dlist_def list_of_dlist_inverse distinct_remdups_id)
```
```    66
```
```    67
```
```    68 text {* Fundamental operations: *}
```
```    69
```
```    70 definition empty :: "'a dlist" where
```
```    71   "empty = Dlist []"
```
```    72
```
```    73 definition insert :: "'a \<Rightarrow> 'a dlist \<Rightarrow> 'a dlist" where
```
```    74   "insert x dxs = Dlist (List.insert x (list_of_dlist dxs))"
```
```    75
```
```    76 definition remove :: "'a \<Rightarrow> 'a dlist \<Rightarrow> 'a dlist" where
```
```    77   "remove x dxs = Dlist (remove1 x (list_of_dlist dxs))"
```
```    78
```
```    79 definition map :: "('a \<Rightarrow> 'b) \<Rightarrow> 'a dlist \<Rightarrow> 'b dlist" where
```
```    80   "map f dxs = Dlist (remdups (List.map f (list_of_dlist dxs)))"
```
```    81
```
```    82 definition filter :: "('a \<Rightarrow> bool) \<Rightarrow> 'a dlist \<Rightarrow> 'a dlist" where
```
```    83   "filter P dxs = Dlist (List.filter P (list_of_dlist dxs))"
```
```    84
```
```    85
```
```    86 text {* Derived operations: *}
```
```    87
```
```    88 definition null :: "'a dlist \<Rightarrow> bool" where
```
```    89   "null dxs = List.null (list_of_dlist dxs)"
```
```    90
```
```    91 definition member :: "'a dlist \<Rightarrow> 'a \<Rightarrow> bool" where
```
```    92   "member dxs = List.member (list_of_dlist dxs)"
```
```    93
```
```    94 definition length :: "'a dlist \<Rightarrow> nat" where
```
```    95   "length dxs = List.length (list_of_dlist dxs)"
```
```    96
```
```    97 definition fold :: "('a \<Rightarrow> 'b \<Rightarrow> 'b) \<Rightarrow> 'a dlist \<Rightarrow> 'b \<Rightarrow> 'b" where
```
```    98   "fold f dxs = List.fold f (list_of_dlist dxs)"
```
```    99
```
```   100
```
```   101 section {* Executable version obeying invariant *}
```
```   102
```
```   103 code_abstype Dlist list_of_dlist
```
```   104   by simp
```
```   105
```
```   106 lemma list_of_dlist_empty [simp, code abstract]:
```
```   107   "list_of_dlist empty = []"
```
```   108   by (simp add: empty_def)
```
```   109
```
```   110 lemma list_of_dlist_insert [simp, code abstract]:
```
```   111   "list_of_dlist (insert x dxs) = List.insert x (list_of_dlist dxs)"
```
```   112   by (simp add: insert_def)
```
```   113
```
```   114 lemma list_of_dlist_remove [simp, code abstract]:
```
```   115   "list_of_dlist (remove x dxs) = remove1 x (list_of_dlist dxs)"
```
```   116   by (simp add: remove_def)
```
```   117
```
```   118 lemma list_of_dlist_map [simp, code abstract]:
```
```   119   "list_of_dlist (map f dxs) = remdups (List.map f (list_of_dlist dxs))"
```
```   120   by (simp add: map_def)
```
```   121
```
```   122 lemma list_of_dlist_filter [simp, code abstract]:
```
```   123   "list_of_dlist (filter P dxs) = List.filter P (list_of_dlist dxs)"
```
```   124   by (simp add: filter_def)
```
```   125
```
```   126 declare null_def [code] member_def [code] length_def [code] fold_def [code] -- {* explicit is better than implicit *}
```
```   127
```
```   128
```
```   129 section {* Implementation of sets by distinct lists -- canonical! *}
```
```   130
```
```   131 definition Set :: "'a dlist \<Rightarrow> 'a fset" where
```
```   132   "Set dxs = Fset.Set (list_of_dlist dxs)"
```
```   133
```
```   134 definition Coset :: "'a dlist \<Rightarrow> 'a fset" where
```
```   135   "Coset dxs = Fset.Coset (list_of_dlist dxs)"
```
```   136
```
```   137 code_datatype Set Coset
```
```   138
```
```   139 declare member_code [code del]
```
```   140 declare is_empty_Set [code del]
```
```   141 declare empty_Set [code del]
```
```   142 declare UNIV_Set [code del]
```
```   143 declare insert_Set [code del]
```
```   144 declare remove_Set [code del]
```
```   145 declare map_Set [code del]
```
```   146 declare filter_Set [code del]
```
```   147 declare forall_Set [code del]
```
```   148 declare exists_Set [code del]
```
```   149 declare card_Set [code del]
```
```   150 declare subfset_eq_forall [code del]
```
```   151 declare subfset_subfset_eq [code del]
```
```   152 declare eq_fset_subfset_eq [code del]
```
```   153 declare inter_project [code del]
```
```   154 declare subtract_remove [code del]
```
```   155 declare union_insert [code del]
```
```   156 declare Infimum_inf [code del]
```
```   157 declare Supremum_sup [code del]
```
```   158
```
```   159 lemma Set_Dlist [simp]:
```
```   160   "Set (Dlist xs) = Fset (set xs)"
```
```   161   by (simp add: Set_def Fset.Set_def)
```
```   162
```
```   163 lemma Coset_Dlist [simp]:
```
```   164   "Coset (Dlist xs) = Fset (- set xs)"
```
```   165   by (simp add: Coset_def Fset.Coset_def)
```
```   166
```
```   167 lemma member_Set [simp]:
```
```   168   "Fset.member (Set dxs) = List.member (list_of_dlist dxs)"
```
```   169   by (simp add: Set_def member_set)
```
```   170
```
```   171 lemma member_Coset [simp]:
```
```   172   "Fset.member (Coset dxs) = Not \<circ> List.member (list_of_dlist dxs)"
```
```   173   by (simp add: Coset_def member_set not_set_compl)
```
```   174
```
```   175 lemma is_empty_Set [code]:
```
```   176   "Fset.is_empty (Set dxs) \<longleftrightarrow> null dxs"
```
```   177   by (simp add: null_def null_empty member_set)
```
```   178
```
```   179 lemma bot_code [code]:
```
```   180   "bot = Set empty"
```
```   181   by (simp add: empty_def)
```
```   182
```
```   183 lemma top_code [code]:
```
```   184   "top = Coset empty"
```
```   185   by (simp add: empty_def)
```
```   186
```
```   187 lemma insert_code [code]:
```
```   188   "Fset.insert x (Set dxs) = Set (insert x dxs)"
```
```   189   "Fset.insert x (Coset dxs) = Coset (remove x dxs)"
```
```   190   by (simp_all add: insert_def remove_def member_set not_set_compl)
```
```   191
```
```   192 lemma remove_code [code]:
```
```   193   "Fset.remove x (Set dxs) = Set (remove x dxs)"
```
```   194   "Fset.remove x (Coset dxs) = Coset (insert x dxs)"
```
```   195   by (auto simp add: insert_def remove_def member_set not_set_compl)
```
```   196
```
```   197 lemma member_code [code]:
```
```   198   "Fset.member (Set dxs) = member dxs"
```
```   199   "Fset.member (Coset dxs) = Not \<circ> member dxs"
```
```   200   by (simp_all add: member_def)
```
```   201
```
```   202 lemma map_code [code]:
```
```   203   "Fset.map f (Set dxs) = Set (map f dxs)"
```
```   204   by (simp add: member_set)
```
```   205
```
```   206 lemma filter_code [code]:
```
```   207   "Fset.filter f (Set dxs) = Set (filter f dxs)"
```
```   208   by (simp add: member_set)
```
```   209
```
```   210 lemma forall_Set [code]:
```
```   211   "Fset.forall P (Set xs) \<longleftrightarrow> list_all P (list_of_dlist xs)"
```
```   212   by (simp add: member_set list_all_iff)
```
```   213
```
```   214 lemma exists_Set [code]:
```
```   215   "Fset.exists P (Set xs) \<longleftrightarrow> list_ex P (list_of_dlist xs)"
```
```   216   by (simp add: member_set list_ex_iff)
```
```   217
```
```   218 lemma card_code [code]:
```
```   219   "Fset.card (Set dxs) = length dxs"
```
```   220   by (simp add: length_def member_set distinct_card)
```
```   221
```
```   222 lemma foldl_list_of_dlist:
```
```   223   "foldl f s (list_of_dlist dxs) = fold (\<lambda>x s. f s x) dxs s"
```
```   224   by (simp add: foldl_fold fold_def)
```
```   225
```
```   226 lemma inter_code [code]:
```
```   227   "inf A (Set xs) = Set (filter (Fset.member A) xs)"
```
```   228   "inf A (Coset xs) = fold Fset.remove xs A"
```
```   229   by (simp_all only: Set_def Coset_def foldl_list_of_dlist inter_project list_of_dlist_filter)
```
```   230
```
```   231 lemma subtract_code [code]:
```
```   232   "A - Set xs = fold Fset.remove xs A"
```
```   233   "A - Coset xs = Set (filter (Fset.member A) xs)"
```
```   234   by (simp_all only: Set_def Coset_def foldl_list_of_dlist subtract_remove list_of_dlist_filter)
```
```   235
```
```   236 lemma union_code [code]:
```
```   237   "sup (Set xs) A = fold Fset.insert xs A"
```
```   238   "sup (Coset xs) A = Coset (filter (Not \<circ> Fset.member A) xs)"
```
```   239   by (simp_all only: Set_def Coset_def foldl_list_of_dlist union_insert list_of_dlist_filter)
```
```   240
```
```   241 context complete_lattice
```
```   242 begin
```
```   243
```
```   244 lemma Infimum_code [code]:
```
```   245   "Infimum (Set As) = fold inf As top"
```
```   246   by (simp only: Set_def Infimum_inf foldl_list_of_dlist inf.commute)
```
```   247
```
```   248 lemma Supremum_code [code]:
```
```   249   "Supremum (Set As) = fold sup As bot"
```
```   250   by (simp only: Set_def Supremum_sup foldl_list_of_dlist sup.commute)
```
```   251
```
```   252 end
```
```   253
```
```   254 hide (open) const member fold empty insert remove map filter null member length fold
```
```   255
```
```   256 end
```