src/HOL/Library/Dlist.thy
 author wenzelm Mon Dec 28 17:43:30 2015 +0100 (2015-12-28) changeset 61952 546958347e05 parent 61115 3a4400985780 child 62139 519362f817c7 permissions -rw-r--r--
prefer symbols for "Union", "Inter";
```     1 (* Author: Florian Haftmann, TU Muenchen *)
```
```     2
```
```     3 section \<open>Lists with elements distinct as canonical example for datatype invariants\<close>
```
```     4
```
```     5 theory Dlist
```
```     6 imports Main
```
```     7 begin
```
```     8
```
```     9 subsection \<open>The type of distinct lists\<close>
```
```    10
```
```    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
```
```    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 \<open>Formal, totalized constructor for @{typ "'a dlist"}:\<close>
```
```    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 remdups_list_of_dlist [simp]:
```
```    39   "remdups (list_of_dlist dxs) = list_of_dlist dxs"
```
```    40   by simp
```
```    41
```
```    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)
```
```    45
```
```    46
```
```    47 text \<open>Fundamental operations:\<close>
```
```    48
```
```    49 context
```
```    50 begin
```
```    51
```
```    52 qualified definition empty :: "'a dlist" where
```
```    53   "empty = Dlist []"
```
```    54
```
```    55 qualified definition insert :: "'a \<Rightarrow> 'a dlist \<Rightarrow> 'a dlist" where
```
```    56   "insert x dxs = Dlist (List.insert x (list_of_dlist dxs))"
```
```    57
```
```    58 qualified definition remove :: "'a \<Rightarrow> 'a dlist \<Rightarrow> 'a dlist" where
```
```    59   "remove x dxs = Dlist (remove1 x (list_of_dlist dxs))"
```
```    60
```
```    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)))"
```
```    63
```
```    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))"
```
```    66
```
```    67 end
```
```    68
```
```    69
```
```    70 text \<open>Derived operations:\<close>
```
```    71
```
```    72 context
```
```    73 begin
```
```    74
```
```    75 qualified definition null :: "'a dlist \<Rightarrow> bool" where
```
```    76   "null dxs = List.null (list_of_dlist dxs)"
```
```    77
```
```    78 qualified definition member :: "'a dlist \<Rightarrow> 'a \<Rightarrow> bool" where
```
```    79   "member dxs = List.member (list_of_dlist dxs)"
```
```    80
```
```    81 qualified definition length :: "'a dlist \<Rightarrow> nat" where
```
```    82   "length dxs = List.length (list_of_dlist dxs)"
```
```    83
```
```    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)"
```
```    86
```
```    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)"
```
```    89
```
```    90 end
```
```    91
```
```    92
```
```    93 subsection \<open>Executable version obeying invariant\<close>
```
```    94
```
```    95 lemma list_of_dlist_empty [simp, code abstract]:
```
```    96   "list_of_dlist Dlist.empty = []"
```
```    97   by (simp add: Dlist.empty_def)
```
```    98
```
```    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)
```
```   102
```
```   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)
```
```   106
```
```   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)
```
```   110
```
```   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)
```
```   114
```
```   115
```
```   116 text \<open>Explicit executable conversion\<close>
```
```   117
```
```   118 definition dlist_of_list [simp]:
```
```   119   "dlist_of_list = Dlist"
```
```   120
```
```   121 lemma [code abstract]:
```
```   122   "list_of_dlist (dlist_of_list xs) = remdups xs"
```
```   123   by simp
```
```   124
```
```   125
```
```   126 text \<open>Equality\<close>
```
```   127
```
```   128 instantiation dlist :: (equal) equal
```
```   129 begin
```
```   130
```
```   131 definition "HOL.equal dxs dys \<longleftrightarrow> HOL.equal (list_of_dlist dxs) (list_of_dlist dys)"
```
```   132
```
```   133 instance
```
```   134   by standard (simp add: equal_dlist_def equal list_of_dlist_inject)
```
```   135
```
```   136 end
```
```   137
```
```   138 declare equal_dlist_def [code]
```
```   139
```
```   140 lemma [code nbe]: "HOL.equal (dxs :: 'a::equal dlist) dxs \<longleftrightarrow> True"
```
```   141   by (fact equal_refl)
```
```   142
```
```   143
```
```   144 subsection \<open>Induction principle and case distinction\<close>
```
```   145
```
```   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
```
```   166
```
```   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
```
```   187
```
```   188
```
```   189 subsection \<open>Functorial structure\<close>
```
```   190
```
```   191 functor map: map
```
```   192   by (simp_all add: remdups_map_remdups fun_eq_iff dlist_eq_iff)
```
```   193
```
```   194
```
```   195 subsection \<open>Quickcheck generators\<close>
```
```   196
```
```   197 quickcheck_generator dlist predicate: distinct constructors: Dlist.empty, Dlist.insert
```
```   198
```
```   199 end
```