src/HOL/Library/Dlist.thy
author haftmann
Fri Aug 27 19:34:23 2010 +0200 (2010-08-27 ago)
changeset 38857 97775f3e8722
parent 38512 ed4703b416ed
child 39380 5a2662c1e44a
permissions -rw-r--r--
renamed class/constant eq to equal; tuned some instantiations
     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 {* The type of distinct lists *}
    10 
    11 typedef (open) 'a dlist = "{xs::'a list. distinct xs}"
    12   morphisms list_of_dlist Abs_dlist
    13 proof
    14   show "[] \<in> ?dlist" by simp
    15 qed
    16 
    17 lemma dlist_ext:
    18   assumes "list_of_dlist dxs = list_of_dlist dys"
    19   shows "dxs = dys"
    20   using assms by (simp add: list_of_dlist_inject)
    21 
    22 
    23 text {* Formal, totalized constructor for @{typ "'a dlist"}: *}
    24 
    25 definition Dlist :: "'a list \<Rightarrow> 'a dlist" where
    26   "Dlist xs = Abs_dlist (remdups xs)"
    27 
    28 lemma distinct_list_of_dlist [simp]:
    29   "distinct (list_of_dlist dxs)"
    30   using list_of_dlist [of dxs] by simp
    31 
    32 lemma list_of_dlist_Dlist [simp]:
    33   "list_of_dlist (Dlist xs) = remdups xs"
    34   by (simp add: Dlist_def Abs_dlist_inverse)
    35 
    36 lemma Dlist_list_of_dlist [simp, code abstype]:
    37   "Dlist (list_of_dlist dxs) = dxs"
    38   by (simp add: Dlist_def list_of_dlist_inverse distinct_remdups_id)
    39 
    40 
    41 text {* Fundamental operations: *}
    42 
    43 definition empty :: "'a dlist" where
    44   "empty = Dlist []"
    45 
    46 definition insert :: "'a \<Rightarrow> 'a dlist \<Rightarrow> 'a dlist" where
    47   "insert x dxs = Dlist (List.insert x (list_of_dlist dxs))"
    48 
    49 definition remove :: "'a \<Rightarrow> 'a dlist \<Rightarrow> 'a dlist" where
    50   "remove x dxs = Dlist (remove1 x (list_of_dlist dxs))"
    51 
    52 definition map :: "('a \<Rightarrow> 'b) \<Rightarrow> 'a dlist \<Rightarrow> 'b dlist" where
    53   "map f dxs = Dlist (remdups (List.map f (list_of_dlist dxs)))"
    54 
    55 definition filter :: "('a \<Rightarrow> bool) \<Rightarrow> 'a dlist \<Rightarrow> 'a dlist" where
    56   "filter P dxs = Dlist (List.filter P (list_of_dlist dxs))"
    57 
    58 
    59 text {* Derived operations: *}
    60 
    61 definition null :: "'a dlist \<Rightarrow> bool" where
    62   "null dxs = List.null (list_of_dlist dxs)"
    63 
    64 definition member :: "'a dlist \<Rightarrow> 'a \<Rightarrow> bool" where
    65   "member dxs = List.member (list_of_dlist dxs)"
    66 
    67 definition length :: "'a dlist \<Rightarrow> nat" where
    68   "length dxs = List.length (list_of_dlist dxs)"
    69 
    70 definition fold :: "('a \<Rightarrow> 'b \<Rightarrow> 'b) \<Rightarrow> 'a dlist \<Rightarrow> 'b \<Rightarrow> 'b" where
    71   "fold f dxs = More_List.fold f (list_of_dlist dxs)"
    72 
    73 definition foldr :: "('a \<Rightarrow> 'b \<Rightarrow> 'b) \<Rightarrow> 'a dlist \<Rightarrow> 'b \<Rightarrow> 'b" where
    74   "foldr f dxs = List.foldr f (list_of_dlist dxs)"
    75 
    76 
    77 section {* Executable version obeying invariant *}
    78 
    79 lemma list_of_dlist_empty [simp, code abstract]:
    80   "list_of_dlist empty = []"
    81   by (simp add: empty_def)
    82 
    83 lemma list_of_dlist_insert [simp, code abstract]:
    84   "list_of_dlist (insert x dxs) = List.insert x (list_of_dlist dxs)"
    85   by (simp add: insert_def)
    86 
    87 lemma list_of_dlist_remove [simp, code abstract]:
    88   "list_of_dlist (remove x dxs) = remove1 x (list_of_dlist dxs)"
    89   by (simp add: remove_def)
    90 
    91 lemma list_of_dlist_map [simp, code abstract]:
    92   "list_of_dlist (map f dxs) = remdups (List.map f (list_of_dlist dxs))"
    93   by (simp add: map_def)
    94 
    95 lemma list_of_dlist_filter [simp, code abstract]:
    96   "list_of_dlist (filter P dxs) = List.filter P (list_of_dlist dxs)"
    97   by (simp add: filter_def)
    98 
    99 
   100 text {* Explicit executable conversion *}
   101 
   102 definition dlist_of_list [simp]:
   103   "dlist_of_list = Dlist"
   104 
   105 lemma [code abstract]:
   106   "list_of_dlist (dlist_of_list xs) = remdups xs"
   107   by simp
   108 
   109 
   110 text {* Equality *}
   111 
   112 instantiation dlist :: (equal) equal
   113 begin
   114 
   115 definition "HOL.equal dxs dys \<longleftrightarrow> HOL.equal (list_of_dlist dxs) (list_of_dlist dys)"
   116 
   117 instance proof
   118 qed (simp add: equal_dlist_def equal list_of_dlist_inject)
   119 
   120 end
   121 
   122 lemma [code nbe]:
   123   "HOL.equal (dxs :: 'a::equal dlist) dxs \<longleftrightarrow> True"
   124   by (fact equal_refl)
   125 
   126 
   127 section {* Induction principle and case distinction *}
   128 
   129 lemma dlist_induct [case_names empty insert, induct type: dlist]:
   130   assumes empty: "P empty"
   131   assumes insrt: "\<And>x dxs. \<not> member dxs x \<Longrightarrow> P dxs \<Longrightarrow> P (insert x dxs)"
   132   shows "P dxs"
   133 proof (cases dxs)
   134   case (Abs_dlist xs)
   135   then have "distinct xs" and dxs: "dxs = Dlist xs" by (simp_all add: Dlist_def distinct_remdups_id)
   136   from `distinct xs` have "P (Dlist xs)"
   137   proof (induct xs rule: distinct_induct)
   138     case Nil from empty show ?case by (simp add: empty_def)
   139   next
   140     case (insert x xs)
   141     then have "\<not> member (Dlist xs) x" and "P (Dlist xs)"
   142       by (simp_all add: member_def List.member_def)
   143     with insrt have "P (insert x (Dlist xs))" .
   144     with insert show ?case by (simp add: insert_def distinct_remdups_id)
   145   qed
   146   with dxs show "P dxs" by simp
   147 qed
   148 
   149 lemma dlist_case [case_names empty insert, cases type: dlist]:
   150   assumes empty: "dxs = empty \<Longrightarrow> P"
   151   assumes insert: "\<And>x dys. \<not> member dys x \<Longrightarrow> dxs = insert x dys \<Longrightarrow> P"
   152   shows P
   153 proof (cases dxs)
   154   case (Abs_dlist xs)
   155   then have dxs: "dxs = Dlist xs" and distinct: "distinct xs"
   156     by (simp_all add: Dlist_def distinct_remdups_id)
   157   show P proof (cases xs)
   158     case Nil with dxs have "dxs = empty" by (simp add: empty_def) 
   159     with empty show P .
   160   next
   161     case (Cons x xs)
   162     with dxs distinct have "\<not> member (Dlist xs) x"
   163       and "dxs = insert x (Dlist xs)"
   164       by (simp_all add: member_def List.member_def insert_def distinct_remdups_id)
   165     with insert show P .
   166   qed
   167 qed
   168 
   169 
   170 section {* Implementation of sets by distinct lists -- canonical! *}
   171 
   172 definition Set :: "'a dlist \<Rightarrow> 'a fset" where
   173   "Set dxs = Fset.Set (list_of_dlist dxs)"
   174 
   175 definition Coset :: "'a dlist \<Rightarrow> 'a fset" where
   176   "Coset dxs = Fset.Coset (list_of_dlist dxs)"
   177 
   178 code_datatype Set Coset
   179 
   180 declare member_code [code del]
   181 declare is_empty_Set [code del]
   182 declare empty_Set [code del]
   183 declare UNIV_Set [code del]
   184 declare insert_Set [code del]
   185 declare remove_Set [code del]
   186 declare compl_Set [code del]
   187 declare compl_Coset [code del]
   188 declare map_Set [code del]
   189 declare filter_Set [code del]
   190 declare forall_Set [code del]
   191 declare exists_Set [code del]
   192 declare card_Set [code del]
   193 declare inter_project [code del]
   194 declare subtract_remove [code del]
   195 declare union_insert [code del]
   196 declare Infimum_inf [code del]
   197 declare Supremum_sup [code del]
   198 
   199 lemma Set_Dlist [simp]:
   200   "Set (Dlist xs) = Fset (set xs)"
   201   by (rule fset_eqI) (simp add: Set_def)
   202 
   203 lemma Coset_Dlist [simp]:
   204   "Coset (Dlist xs) = Fset (- set xs)"
   205   by (rule fset_eqI) (simp add: Coset_def)
   206 
   207 lemma member_Set [simp]:
   208   "Fset.member (Set dxs) = List.member (list_of_dlist dxs)"
   209   by (simp add: Set_def member_set)
   210 
   211 lemma member_Coset [simp]:
   212   "Fset.member (Coset dxs) = Not \<circ> List.member (list_of_dlist dxs)"
   213   by (simp add: Coset_def member_set not_set_compl)
   214 
   215 lemma Set_dlist_of_list [code]:
   216   "Fset.Set xs = Set (dlist_of_list xs)"
   217   by (rule fset_eqI) simp
   218 
   219 lemma Coset_dlist_of_list [code]:
   220   "Fset.Coset xs = Coset (dlist_of_list xs)"
   221   by (rule fset_eqI) simp
   222 
   223 lemma is_empty_Set [code]:
   224   "Fset.is_empty (Set dxs) \<longleftrightarrow> null dxs"
   225   by (simp add: null_def List.null_def member_set)
   226 
   227 lemma bot_code [code]:
   228   "bot = Set empty"
   229   by (simp add: empty_def)
   230 
   231 lemma top_code [code]:
   232   "top = Coset empty"
   233   by (simp add: empty_def)
   234 
   235 lemma insert_code [code]:
   236   "Fset.insert x (Set dxs) = Set (insert x dxs)"
   237   "Fset.insert x (Coset dxs) = Coset (remove x dxs)"
   238   by (simp_all add: insert_def remove_def member_set not_set_compl)
   239 
   240 lemma remove_code [code]:
   241   "Fset.remove x (Set dxs) = Set (remove x dxs)"
   242   "Fset.remove x (Coset dxs) = Coset (insert x dxs)"
   243   by (auto simp add: insert_def remove_def member_set not_set_compl)
   244 
   245 lemma member_code [code]:
   246   "Fset.member (Set dxs) = member dxs"
   247   "Fset.member (Coset dxs) = Not \<circ> member dxs"
   248   by (simp_all add: member_def)
   249 
   250 lemma compl_code [code]:
   251   "- Set dxs = Coset dxs"
   252   "- Coset dxs = Set dxs"
   253   by (rule fset_eqI, simp add: member_set not_set_compl)+
   254 
   255 lemma map_code [code]:
   256   "Fset.map f (Set dxs) = Set (map f dxs)"
   257   by (rule fset_eqI) (simp add: member_set)
   258   
   259 lemma filter_code [code]:
   260   "Fset.filter f (Set dxs) = Set (filter f dxs)"
   261   by (rule fset_eqI) (simp add: member_set)
   262 
   263 lemma forall_Set [code]:
   264   "Fset.forall P (Set xs) \<longleftrightarrow> list_all P (list_of_dlist xs)"
   265   by (simp add: member_set list_all_iff)
   266 
   267 lemma exists_Set [code]:
   268   "Fset.exists P (Set xs) \<longleftrightarrow> list_ex P (list_of_dlist xs)"
   269   by (simp add: member_set list_ex_iff)
   270 
   271 lemma card_code [code]:
   272   "Fset.card (Set dxs) = length dxs"
   273   by (simp add: length_def member_set distinct_card)
   274 
   275 lemma inter_code [code]:
   276   "inf A (Set xs) = Set (filter (Fset.member A) xs)"
   277   "inf A (Coset xs) = foldr Fset.remove xs A"
   278   by (simp_all only: Set_def Coset_def foldr_def inter_project list_of_dlist_filter)
   279 
   280 lemma subtract_code [code]:
   281   "A - Set xs = foldr Fset.remove xs A"
   282   "A - Coset xs = Set (filter (Fset.member A) xs)"
   283   by (simp_all only: Set_def Coset_def foldr_def subtract_remove list_of_dlist_filter)
   284 
   285 lemma union_code [code]:
   286   "sup (Set xs) A = foldr Fset.insert xs A"
   287   "sup (Coset xs) A = Coset (filter (Not \<circ> Fset.member A) xs)"
   288   by (simp_all only: Set_def Coset_def foldr_def union_insert list_of_dlist_filter)
   289 
   290 context complete_lattice
   291 begin
   292 
   293 lemma Infimum_code [code]:
   294   "Infimum (Set As) = foldr inf As top"
   295   by (simp only: Set_def Infimum_inf foldr_def inf.commute)
   296 
   297 lemma Supremum_code [code]:
   298   "Supremum (Set As) = foldr sup As bot"
   299   by (simp only: Set_def Supremum_sup foldr_def sup.commute)
   300 
   301 end
   302 
   303 
   304 hide_const (open) member fold foldr empty insert remove map filter null member length fold
   305 
   306 end