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