src/HOL/Library/Dlist.thy
author haftmann
Thu May 20 18:00:48 2010 +0200 (2010-05-20)
changeset 37029 d754fb55a20f
parent 37022 f9681d9d1d56
child 37106 d56e0b30ce5a
permissions -rw-r--r--
proper code generator for complement
     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 More_List 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 xs = list_of_dlist ys"
    19   shows "xs = ys"
    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   [code del]: "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 section {* Implementation of sets by distinct lists -- canonical! *}
   111 
   112 definition Set :: "'a dlist \<Rightarrow> 'a fset" where
   113   "Set dxs = Fset.Set (list_of_dlist dxs)"
   114 
   115 definition Coset :: "'a dlist \<Rightarrow> 'a fset" where
   116   "Coset dxs = Fset.Coset (list_of_dlist dxs)"
   117 
   118 code_datatype Set Coset
   119 
   120 declare member_code [code del]
   121 declare is_empty_Set [code del]
   122 declare empty_Set [code del]
   123 declare UNIV_Set [code del]
   124 declare insert_Set [code del]
   125 declare remove_Set [code del]
   126 declare compl_Set [code del]
   127 declare compl_Coset [code del]
   128 declare map_Set [code del]
   129 declare filter_Set [code del]
   130 declare forall_Set [code del]
   131 declare exists_Set [code del]
   132 declare card_Set [code del]
   133 declare inter_project [code del]
   134 declare subtract_remove [code del]
   135 declare union_insert [code del]
   136 declare Infimum_inf [code del]
   137 declare Supremum_sup [code del]
   138 
   139 lemma Set_Dlist [simp]:
   140   "Set (Dlist xs) = Fset (set xs)"
   141   by (simp add: Set_def Fset.Set_def)
   142 
   143 lemma Coset_Dlist [simp]:
   144   "Coset (Dlist xs) = Fset (- set xs)"
   145   by (simp add: Coset_def Fset.Coset_def)
   146 
   147 lemma member_Set [simp]:
   148   "Fset.member (Set dxs) = List.member (list_of_dlist dxs)"
   149   by (simp add: Set_def member_set)
   150 
   151 lemma member_Coset [simp]:
   152   "Fset.member (Coset dxs) = Not \<circ> List.member (list_of_dlist dxs)"
   153   by (simp add: Coset_def member_set not_set_compl)
   154 
   155 lemma Set_dlist_of_list [code]:
   156   "Fset.Set xs = Set (dlist_of_list xs)"
   157   by simp
   158 
   159 lemma Coset_dlist_of_list [code]:
   160   "Fset.Coset xs = Coset (dlist_of_list xs)"
   161   by simp
   162 
   163 lemma is_empty_Set [code]:
   164   "Fset.is_empty (Set dxs) \<longleftrightarrow> null dxs"
   165   by (simp add: null_def null_empty member_set)
   166 
   167 lemma bot_code [code]:
   168   "bot = Set empty"
   169   by (simp add: empty_def)
   170 
   171 lemma top_code [code]:
   172   "top = Coset empty"
   173   by (simp add: empty_def)
   174 
   175 lemma insert_code [code]:
   176   "Fset.insert x (Set dxs) = Set (insert x dxs)"
   177   "Fset.insert x (Coset dxs) = Coset (remove x dxs)"
   178   by (simp_all add: insert_def remove_def member_set not_set_compl)
   179 
   180 lemma remove_code [code]:
   181   "Fset.remove x (Set dxs) = Set (remove x dxs)"
   182   "Fset.remove x (Coset dxs) = Coset (insert x dxs)"
   183   by (auto simp add: insert_def remove_def member_set not_set_compl)
   184 
   185 lemma member_code [code]:
   186   "Fset.member (Set dxs) = member dxs"
   187   "Fset.member (Coset dxs) = Not \<circ> member dxs"
   188   by (simp_all add: member_def)
   189 
   190 lemma compl_code [code]:
   191   "- Set dxs = Coset dxs"
   192   "- Coset dxs = Set dxs"
   193   by (simp_all add: not_set_compl member_set)
   194 
   195 lemma map_code [code]:
   196   "Fset.map f (Set dxs) = Set (map f dxs)"
   197   by (simp add: member_set)
   198   
   199 lemma filter_code [code]:
   200   "Fset.filter f (Set dxs) = Set (filter f dxs)"
   201   by (simp add: member_set)
   202 
   203 lemma forall_Set [code]:
   204   "Fset.forall P (Set xs) \<longleftrightarrow> list_all P (list_of_dlist xs)"
   205   by (simp add: member_set list_all_iff)
   206 
   207 lemma exists_Set [code]:
   208   "Fset.exists P (Set xs) \<longleftrightarrow> list_ex P (list_of_dlist xs)"
   209   by (simp add: member_set list_ex_iff)
   210 
   211 lemma card_code [code]:
   212   "Fset.card (Set dxs) = length dxs"
   213   by (simp add: length_def member_set distinct_card)
   214 
   215 lemma inter_code [code]:
   216   "inf A (Set xs) = Set (filter (Fset.member A) xs)"
   217   "inf A (Coset xs) = foldr Fset.remove xs A"
   218   by (simp_all only: Set_def Coset_def foldr_def inter_project list_of_dlist_filter)
   219 
   220 lemma subtract_code [code]:
   221   "A - Set xs = foldr Fset.remove xs A"
   222   "A - Coset xs = Set (filter (Fset.member A) xs)"
   223   by (simp_all only: Set_def Coset_def foldr_def subtract_remove list_of_dlist_filter)
   224 
   225 lemma union_code [code]:
   226   "sup (Set xs) A = foldr Fset.insert xs A"
   227   "sup (Coset xs) A = Coset (filter (Not \<circ> Fset.member A) xs)"
   228   by (simp_all only: Set_def Coset_def foldr_def union_insert list_of_dlist_filter)
   229 
   230 context complete_lattice
   231 begin
   232 
   233 lemma Infimum_code [code]:
   234   "Infimum (Set As) = foldr inf As top"
   235   by (simp only: Set_def Infimum_inf foldr_def inf.commute)
   236 
   237 lemma Supremum_code [code]:
   238   "Supremum (Set As) = foldr sup As bot"
   239   by (simp only: Set_def Supremum_sup foldr_def sup.commute)
   240 
   241 end
   242 
   243 hide_const (open) member fold foldr empty insert remove map filter null member length fold
   244 
   245 end