2095 lemma set_rev_mp: "x:A ==> A \<subseteq> B ==> x:B" |
2095 lemma set_rev_mp: "x:A ==> A \<subseteq> B ==> x:B" |
2096 by (rule subsetD) |
2096 by (rule subsetD) |
2097 |
2097 |
2098 lemma set_mp: "A \<subseteq> B ==> x:A ==> x:B" |
2098 lemma set_mp: "A \<subseteq> B ==> x:A ==> x:B" |
2099 by (rule subsetD) |
2099 by (rule subsetD) |
|
2100 |
|
2101 |
|
2102 subsection {* Code generation for finite sets *} |
|
2103 |
|
2104 code_datatype "{}" insert |
|
2105 |
|
2106 |
|
2107 subsubsection {* Primitive predicates *} |
|
2108 |
|
2109 definition |
|
2110 is_empty :: "'a set \<Rightarrow> bool" |
|
2111 where |
|
2112 [code func del]: "is_empty A \<longleftrightarrow> A = {}" |
|
2113 lemmas [code inline] = is_empty_def [symmetric] |
|
2114 |
|
2115 lemma is_empty_insert [code func]: |
|
2116 "is_empty (insert a A) \<longleftrightarrow> False" |
|
2117 by (simp add: is_empty_def) |
|
2118 |
|
2119 lemma is_empty_empty [code func]: |
|
2120 "is_empty {} \<longleftrightarrow> True" |
|
2121 by (simp add: is_empty_def) |
|
2122 |
|
2123 lemma Ball_insert [code func]: |
|
2124 "Ball (insert a A) P \<longleftrightarrow> P a \<and> Ball A P" |
|
2125 by simp |
|
2126 |
|
2127 lemma Ball_empty [code func]: |
|
2128 "Ball {} P \<longleftrightarrow> True" |
|
2129 by simp |
|
2130 |
|
2131 lemma Bex_insert [code func]: |
|
2132 "Bex (insert a A) P \<longleftrightarrow> P a \<or> Bex A P" |
|
2133 by simp |
|
2134 |
|
2135 lemma Bex_empty [code func]: |
|
2136 "Bex {} P \<longleftrightarrow> False" |
|
2137 by simp |
|
2138 |
|
2139 |
|
2140 subsubsection {* Primitive operations *} |
|
2141 |
|
2142 lemma minus_insert [code func]: |
|
2143 "insert a A - B = (let C = A - B in if a \<in> B then C else insert a C)" |
|
2144 by (auto simp add: Let_def) |
|
2145 |
|
2146 lemma minus_empty1 [code func]: |
|
2147 "{} - A = {}" |
|
2148 by simp |
|
2149 |
|
2150 lemma minus_empty2 [code func]: |
|
2151 "A - {} = A" |
|
2152 by simp |
|
2153 |
|
2154 lemma inter_insert [code func]: |
|
2155 "insert a A \<inter> B = (let C = A \<inter> B in if a \<in> B then insert a C else C)" |
|
2156 by (auto simp add: Let_def) |
|
2157 |
|
2158 lemma inter_empty1 [code func]: |
|
2159 "{} \<inter> A = {}" |
|
2160 by simp |
|
2161 |
|
2162 lemma inter_empty2 [code func]: |
|
2163 "A \<inter> {} = {}" |
|
2164 by simp |
|
2165 |
|
2166 lemma union_insert [code func]: |
|
2167 "insert a A \<union> B = (let C = A \<union> B in if a \<in> B then C else insert a C)" |
|
2168 by (auto simp add: Let_def) |
|
2169 |
|
2170 lemma union_empty1 [code func]: |
|
2171 "{} \<union> A = A" |
|
2172 by simp |
|
2173 |
|
2174 lemma union_empty2 [code func]: |
|
2175 "A \<union> {} = A" |
|
2176 by simp |
|
2177 |
|
2178 lemma INTER_insert [code func]: |
|
2179 "INTER (insert a A) f = f a \<inter> INTER A f" |
|
2180 by auto |
|
2181 |
|
2182 lemma INTER_singleton [code func]: |
|
2183 "INTER {a} f = f a" |
|
2184 by auto |
|
2185 |
|
2186 lemma UNION_insert [code func]: |
|
2187 "UNION (insert a A) f = f a \<union> UNION A f" |
|
2188 by auto |
|
2189 |
|
2190 lemma UNION_empty [code func]: |
|
2191 "UNION {} f = {}" |
|
2192 by auto |
|
2193 |
|
2194 |
|
2195 subsubsection {* Derived predicates *} |
|
2196 |
|
2197 lemma in_code [code func]: |
|
2198 "a \<in> A \<longleftrightarrow> (\<exists>x\<in>A. a = x)" |
|
2199 by simp |
|
2200 |
|
2201 instance set :: (eq) eq .. |
|
2202 |
|
2203 lemma eq_set_code [code func]: |
|
2204 fixes A B :: "'a\<Colon>eq set" |
|
2205 shows "A = B \<longleftrightarrow> A \<subseteq> B \<and> B \<subseteq> A" |
|
2206 by auto |
|
2207 |
|
2208 lemma subset_eq_code [code func]: |
|
2209 fixes A B :: "'a\<Colon>eq set" |
|
2210 shows "A \<subseteq> B \<longleftrightarrow> (\<forall>x\<in>A. x \<in> B)" |
|
2211 by auto |
|
2212 |
|
2213 lemma subset_code [code func]: |
|
2214 fixes A B :: "'a\<Colon>eq set" |
|
2215 shows "A \<subset> B \<longleftrightarrow> A \<subseteq> B \<and> \<not> B \<subseteq> A" |
|
2216 by auto |
|
2217 |
|
2218 |
|
2219 subsubsection {* Derived operations *} |
|
2220 |
|
2221 lemma image_code [code func]: |
|
2222 "image f A = UNION A (\<lambda>x. {f x})" by auto |
|
2223 |
|
2224 definition |
|
2225 project :: "('a \<Rightarrow> bool) \<Rightarrow> 'a set \<Rightarrow> 'a set" where |
|
2226 [code func del, code post]: "project P A = {a\<in>A. P a}" |
|
2227 |
|
2228 lemmas [symmetric, code inline] = project_def |
|
2229 |
|
2230 lemma project_code [code func]: |
|
2231 "project P A = UNION A (\<lambda>a. if P a then {a} else {})" |
|
2232 by (auto simp add: project_def split: if_splits) |
|
2233 |
|
2234 lemma Inter_code [code func]: |
|
2235 "Inter A = INTER A (\<lambda>x. x)" |
|
2236 by auto |
|
2237 |
|
2238 lemma Union_code [code func]: |
|
2239 "Union A = UNION A (\<lambda>x. x)" |
|
2240 by auto |
2100 |
2241 |
2101 |
2242 |
2102 subsection {* Basic ML bindings *} |
2243 subsection {* Basic ML bindings *} |
2103 |
2244 |
2104 ML {* |
2245 ML {* |