src/HOL/Library/Numeral_Type.thy
 author blanchet Wed Sep 24 15:45:55 2014 +0200 (2014-09-24) changeset 58425 246985c6b20b parent 57514 bdc2c6b40bf2 child 58881 b9556a055632 permissions -rw-r--r--
simpler proof
```     1 (*  Title:      HOL/Library/Numeral_Type.thy
```
```     2     Author:     Brian Huffman
```
```     3 *)
```
```     4
```
```     5 header {* Numeral Syntax for Types *}
```
```     6
```
```     7 theory Numeral_Type
```
```     8 imports Cardinality
```
```     9 begin
```
```    10
```
```    11 subsection {* Numeral Types *}
```
```    12
```
```    13 typedef num0 = "UNIV :: nat set" ..
```
```    14 typedef num1 = "UNIV :: unit set" ..
```
```    15
```
```    16 typedef 'a bit0 = "{0 ..< 2 * int CARD('a::finite)}"
```
```    17 proof
```
```    18   show "0 \<in> {0 ..< 2 * int CARD('a)}"
```
```    19     by simp
```
```    20 qed
```
```    21
```
```    22 typedef 'a bit1 = "{0 ..< 1 + 2 * int CARD('a::finite)}"
```
```    23 proof
```
```    24   show "0 \<in> {0 ..< 1 + 2 * int CARD('a)}"
```
```    25     by simp
```
```    26 qed
```
```    27
```
```    28 lemma card_num0 [simp]: "CARD (num0) = 0"
```
```    29   unfolding type_definition.card [OF type_definition_num0]
```
```    30   by simp
```
```    31
```
```    32 lemma infinite_num0: "\<not> finite (UNIV :: num0 set)"
```
```    33   using card_num0[unfolded card_eq_0_iff]
```
```    34   by simp
```
```    35
```
```    36 lemma card_num1 [simp]: "CARD(num1) = 1"
```
```    37   unfolding type_definition.card [OF type_definition_num1]
```
```    38   by (simp only: card_UNIV_unit)
```
```    39
```
```    40 lemma card_bit0 [simp]: "CARD('a bit0) = 2 * CARD('a::finite)"
```
```    41   unfolding type_definition.card [OF type_definition_bit0]
```
```    42   by simp
```
```    43
```
```    44 lemma card_bit1 [simp]: "CARD('a bit1) = Suc (2 * CARD('a::finite))"
```
```    45   unfolding type_definition.card [OF type_definition_bit1]
```
```    46   by simp
```
```    47
```
```    48 instance num1 :: finite
```
```    49 proof
```
```    50   show "finite (UNIV::num1 set)"
```
```    51     unfolding type_definition.univ [OF type_definition_num1]
```
```    52     using finite by (rule finite_imageI)
```
```    53 qed
```
```    54
```
```    55 instance bit0 :: (finite) card2
```
```    56 proof
```
```    57   show "finite (UNIV::'a bit0 set)"
```
```    58     unfolding type_definition.univ [OF type_definition_bit0]
```
```    59     by simp
```
```    60   show "2 \<le> CARD('a bit0)"
```
```    61     by simp
```
```    62 qed
```
```    63
```
```    64 instance bit1 :: (finite) card2
```
```    65 proof
```
```    66   show "finite (UNIV::'a bit1 set)"
```
```    67     unfolding type_definition.univ [OF type_definition_bit1]
```
```    68     by simp
```
```    69   show "2 \<le> CARD('a bit1)"
```
```    70     by simp
```
```    71 qed
```
```    72
```
```    73 subsection {* Locales for for modular arithmetic subtypes *}
```
```    74
```
```    75 locale mod_type =
```
```    76   fixes n :: int
```
```    77   and Rep :: "'a::{zero,one,plus,times,uminus,minus} \<Rightarrow> int"
```
```    78   and Abs :: "int \<Rightarrow> 'a::{zero,one,plus,times,uminus,minus}"
```
```    79   assumes type: "type_definition Rep Abs {0..<n}"
```
```    80   and size1: "1 < n"
```
```    81   and zero_def: "0 = Abs 0"
```
```    82   and one_def:  "1 = Abs 1"
```
```    83   and add_def:  "x + y = Abs ((Rep x + Rep y) mod n)"
```
```    84   and mult_def: "x * y = Abs ((Rep x * Rep y) mod n)"
```
```    85   and diff_def: "x - y = Abs ((Rep x - Rep y) mod n)"
```
```    86   and minus_def: "- x = Abs ((- Rep x) mod n)"
```
```    87 begin
```
```    88
```
```    89 lemma size0: "0 < n"
```
```    90 using size1 by simp
```
```    91
```
```    92 lemmas definitions =
```
```    93   zero_def one_def add_def mult_def minus_def diff_def
```
```    94
```
```    95 lemma Rep_less_n: "Rep x < n"
```
```    96 by (rule type_definition.Rep [OF type, simplified, THEN conjunct2])
```
```    97
```
```    98 lemma Rep_le_n: "Rep x \<le> n"
```
```    99 by (rule Rep_less_n [THEN order_less_imp_le])
```
```   100
```
```   101 lemma Rep_inject_sym: "x = y \<longleftrightarrow> Rep x = Rep y"
```
```   102 by (rule type_definition.Rep_inject [OF type, symmetric])
```
```   103
```
```   104 lemma Rep_inverse: "Abs (Rep x) = x"
```
```   105 by (rule type_definition.Rep_inverse [OF type])
```
```   106
```
```   107 lemma Abs_inverse: "m \<in> {0..<n} \<Longrightarrow> Rep (Abs m) = m"
```
```   108 by (rule type_definition.Abs_inverse [OF type])
```
```   109
```
```   110 lemma Rep_Abs_mod: "Rep (Abs (m mod n)) = m mod n"
```
```   111 by (simp add: Abs_inverse pos_mod_conj [OF size0])
```
```   112
```
```   113 lemma Rep_Abs_0: "Rep (Abs 0) = 0"
```
```   114 by (simp add: Abs_inverse size0)
```
```   115
```
```   116 lemma Rep_0: "Rep 0 = 0"
```
```   117 by (simp add: zero_def Rep_Abs_0)
```
```   118
```
```   119 lemma Rep_Abs_1: "Rep (Abs 1) = 1"
```
```   120 by (simp add: Abs_inverse size1)
```
```   121
```
```   122 lemma Rep_1: "Rep 1 = 1"
```
```   123 by (simp add: one_def Rep_Abs_1)
```
```   124
```
```   125 lemma Rep_mod: "Rep x mod n = Rep x"
```
```   126 apply (rule_tac x=x in type_definition.Abs_cases [OF type])
```
```   127 apply (simp add: type_definition.Abs_inverse [OF type])
```
```   128 apply (simp add: mod_pos_pos_trivial)
```
```   129 done
```
```   130
```
```   131 lemmas Rep_simps =
```
```   132   Rep_inject_sym Rep_inverse Rep_Abs_mod Rep_mod Rep_Abs_0 Rep_Abs_1
```
```   133
```
```   134 lemma comm_ring_1: "OFCLASS('a, comm_ring_1_class)"
```
```   135 apply (intro_classes, unfold definitions)
```
```   136 apply (simp_all add: Rep_simps zmod_simps field_simps)
```
```   137 done
```
```   138
```
```   139 end
```
```   140
```
```   141 locale mod_ring = mod_type n Rep Abs
```
```   142   for n :: int
```
```   143   and Rep :: "'a::{comm_ring_1} \<Rightarrow> int"
```
```   144   and Abs :: "int \<Rightarrow> 'a::{comm_ring_1}"
```
```   145 begin
```
```   146
```
```   147 lemma of_nat_eq: "of_nat k = Abs (int k mod n)"
```
```   148 apply (induct k)
```
```   149 apply (simp add: zero_def)
```
```   150 apply (simp add: Rep_simps add_def one_def zmod_simps ac_simps)
```
```   151 done
```
```   152
```
```   153 lemma of_int_eq: "of_int z = Abs (z mod n)"
```
```   154 apply (cases z rule: int_diff_cases)
```
```   155 apply (simp add: Rep_simps of_nat_eq diff_def zmod_simps)
```
```   156 done
```
```   157
```
```   158 lemma Rep_numeral:
```
```   159   "Rep (numeral w) = numeral w mod n"
```
```   160 using of_int_eq [of "numeral w"]
```
```   161 by (simp add: Rep_inject_sym Rep_Abs_mod)
```
```   162
```
```   163 lemma iszero_numeral:
```
```   164   "iszero (numeral w::'a) \<longleftrightarrow> numeral w mod n = 0"
```
```   165 by (simp add: Rep_inject_sym Rep_numeral Rep_0 iszero_def)
```
```   166
```
```   167 lemma cases:
```
```   168   assumes 1: "\<And>z. \<lbrakk>(x::'a) = of_int z; 0 \<le> z; z < n\<rbrakk> \<Longrightarrow> P"
```
```   169   shows "P"
```
```   170 apply (cases x rule: type_definition.Abs_cases [OF type])
```
```   171 apply (rule_tac z="y" in 1)
```
```   172 apply (simp_all add: of_int_eq mod_pos_pos_trivial)
```
```   173 done
```
```   174
```
```   175 lemma induct:
```
```   176   "(\<And>z. \<lbrakk>0 \<le> z; z < n\<rbrakk> \<Longrightarrow> P (of_int z)) \<Longrightarrow> P (x::'a)"
```
```   177 by (cases x rule: cases) simp
```
```   178
```
```   179 end
```
```   180
```
```   181
```
```   182 subsection {* Ring class instances *}
```
```   183
```
```   184 text {*
```
```   185   Unfortunately @{text ring_1} instance is not possible for
```
```   186   @{typ num1}, since 0 and 1 are not distinct.
```
```   187 *}
```
```   188
```
```   189 instantiation num1 :: "{comm_ring,comm_monoid_mult,numeral}"
```
```   190 begin
```
```   191
```
```   192 lemma num1_eq_iff: "(x::num1) = (y::num1) \<longleftrightarrow> True"
```
```   193   by (induct x, induct y) simp
```
```   194
```
```   195 instance proof
```
```   196 qed (simp_all add: num1_eq_iff)
```
```   197
```
```   198 end
```
```   199
```
```   200 instantiation
```
```   201   bit0 and bit1 :: (finite) "{zero,one,plus,times,uminus,minus}"
```
```   202 begin
```
```   203
```
```   204 definition Abs_bit0' :: "int \<Rightarrow> 'a bit0" where
```
```   205   "Abs_bit0' x = Abs_bit0 (x mod int CARD('a bit0))"
```
```   206
```
```   207 definition Abs_bit1' :: "int \<Rightarrow> 'a bit1" where
```
```   208   "Abs_bit1' x = Abs_bit1 (x mod int CARD('a bit1))"
```
```   209
```
```   210 definition "0 = Abs_bit0 0"
```
```   211 definition "1 = Abs_bit0 1"
```
```   212 definition "x + y = Abs_bit0' (Rep_bit0 x + Rep_bit0 y)"
```
```   213 definition "x * y = Abs_bit0' (Rep_bit0 x * Rep_bit0 y)"
```
```   214 definition "x - y = Abs_bit0' (Rep_bit0 x - Rep_bit0 y)"
```
```   215 definition "- x = Abs_bit0' (- Rep_bit0 x)"
```
```   216
```
```   217 definition "0 = Abs_bit1 0"
```
```   218 definition "1 = Abs_bit1 1"
```
```   219 definition "x + y = Abs_bit1' (Rep_bit1 x + Rep_bit1 y)"
```
```   220 definition "x * y = Abs_bit1' (Rep_bit1 x * Rep_bit1 y)"
```
```   221 definition "x - y = Abs_bit1' (Rep_bit1 x - Rep_bit1 y)"
```
```   222 definition "- x = Abs_bit1' (- Rep_bit1 x)"
```
```   223
```
```   224 instance ..
```
```   225
```
```   226 end
```
```   227
```
```   228 interpretation bit0:
```
```   229   mod_type "int CARD('a::finite bit0)"
```
```   230            "Rep_bit0 :: 'a::finite bit0 \<Rightarrow> int"
```
```   231            "Abs_bit0 :: int \<Rightarrow> 'a::finite bit0"
```
```   232 apply (rule mod_type.intro)
```
```   233 apply (simp add: int_mult type_definition_bit0)
```
```   234 apply (rule one_less_int_card)
```
```   235 apply (rule zero_bit0_def)
```
```   236 apply (rule one_bit0_def)
```
```   237 apply (rule plus_bit0_def [unfolded Abs_bit0'_def])
```
```   238 apply (rule times_bit0_def [unfolded Abs_bit0'_def])
```
```   239 apply (rule minus_bit0_def [unfolded Abs_bit0'_def])
```
```   240 apply (rule uminus_bit0_def [unfolded Abs_bit0'_def])
```
```   241 done
```
```   242
```
```   243 interpretation bit1:
```
```   244   mod_type "int CARD('a::finite bit1)"
```
```   245            "Rep_bit1 :: 'a::finite bit1 \<Rightarrow> int"
```
```   246            "Abs_bit1 :: int \<Rightarrow> 'a::finite bit1"
```
```   247 apply (rule mod_type.intro)
```
```   248 apply (simp add: int_mult type_definition_bit1)
```
```   249 apply (rule one_less_int_card)
```
```   250 apply (rule zero_bit1_def)
```
```   251 apply (rule one_bit1_def)
```
```   252 apply (rule plus_bit1_def [unfolded Abs_bit1'_def])
```
```   253 apply (rule times_bit1_def [unfolded Abs_bit1'_def])
```
```   254 apply (rule minus_bit1_def [unfolded Abs_bit1'_def])
```
```   255 apply (rule uminus_bit1_def [unfolded Abs_bit1'_def])
```
```   256 done
```
```   257
```
```   258 instance bit0 :: (finite) comm_ring_1
```
```   259   by (rule bit0.comm_ring_1)
```
```   260
```
```   261 instance bit1 :: (finite) comm_ring_1
```
```   262   by (rule bit1.comm_ring_1)
```
```   263
```
```   264 interpretation bit0:
```
```   265   mod_ring "int CARD('a::finite bit0)"
```
```   266            "Rep_bit0 :: 'a::finite bit0 \<Rightarrow> int"
```
```   267            "Abs_bit0 :: int \<Rightarrow> 'a::finite bit0"
```
```   268   ..
```
```   269
```
```   270 interpretation bit1:
```
```   271   mod_ring "int CARD('a::finite bit1)"
```
```   272            "Rep_bit1 :: 'a::finite bit1 \<Rightarrow> int"
```
```   273            "Abs_bit1 :: int \<Rightarrow> 'a::finite bit1"
```
```   274   ..
```
```   275
```
```   276 text {* Set up cases, induction, and arithmetic *}
```
```   277
```
```   278 lemmas bit0_cases [case_names of_int, cases type: bit0] = bit0.cases
```
```   279 lemmas bit1_cases [case_names of_int, cases type: bit1] = bit1.cases
```
```   280
```
```   281 lemmas bit0_induct [case_names of_int, induct type: bit0] = bit0.induct
```
```   282 lemmas bit1_induct [case_names of_int, induct type: bit1] = bit1.induct
```
```   283
```
```   284 lemmas bit0_iszero_numeral [simp] = bit0.iszero_numeral
```
```   285 lemmas bit1_iszero_numeral [simp] = bit1.iszero_numeral
```
```   286
```
```   287 lemmas [simp] = eq_numeral_iff_iszero [where 'a="'a bit0"] for dummy :: "'a::finite"
```
```   288 lemmas [simp] = eq_numeral_iff_iszero [where 'a="'a bit1"] for dummy :: "'a::finite"
```
```   289
```
```   290 subsection {* Order instances *}
```
```   291
```
```   292 instantiation bit0 and bit1 :: (finite) linorder begin
```
```   293 definition "a < b \<longleftrightarrow> Rep_bit0 a < Rep_bit0 b"
```
```   294 definition "a \<le> b \<longleftrightarrow> Rep_bit0 a \<le> Rep_bit0 b"
```
```   295 definition "a < b \<longleftrightarrow> Rep_bit1 a < Rep_bit1 b"
```
```   296 definition "a \<le> b \<longleftrightarrow> Rep_bit1 a \<le> Rep_bit1 b"
```
```   297
```
```   298 instance
```
```   299   by(intro_classes)
```
```   300     (auto simp add: less_eq_bit0_def less_bit0_def less_eq_bit1_def less_bit1_def Rep_bit0_inject Rep_bit1_inject)
```
```   301 end
```
```   302
```
```   303 lemma (in preorder) tranclp_less: "op <\<^sup>+\<^sup>+ = op <"
```
```   304 by(auto simp add: fun_eq_iff intro: less_trans elim: tranclp.induct)
```
```   305
```
```   306 instance bit0 and bit1 :: (finite) wellorder
```
```   307 proof -
```
```   308   have "wf {(x :: 'a bit0, y). x < y}"
```
```   309     by(auto simp add: trancl_def tranclp_less intro!: finite_acyclic_wf acyclicI)
```
```   310   thus "OFCLASS('a bit0, wellorder_class)"
```
```   311     by(rule wf_wellorderI) intro_classes
```
```   312 next
```
```   313   have "wf {(x :: 'a bit1, y). x < y}"
```
```   314     by(auto simp add: trancl_def tranclp_less intro!: finite_acyclic_wf acyclicI)
```
```   315   thus "OFCLASS('a bit1, wellorder_class)"
```
```   316     by(rule wf_wellorderI) intro_classes
```
```   317 qed
```
```   318
```
```   319 subsection {* Code setup and type classes for code generation *}
```
```   320
```
```   321 text {* Code setup for @{typ num0} and @{typ num1} *}
```
```   322
```
```   323 definition Num0 :: num0 where "Num0 = Abs_num0 0"
```
```   324 code_datatype Num0
```
```   325
```
```   326 instantiation num0 :: equal begin
```
```   327 definition equal_num0 :: "num0 \<Rightarrow> num0 \<Rightarrow> bool"
```
```   328   where "equal_num0 = op ="
```
```   329 instance by intro_classes (simp add: equal_num0_def)
```
```   330 end
```
```   331
```
```   332 lemma equal_num0_code [code]:
```
```   333   "equal_class.equal Num0 Num0 = True"
```
```   334 by(rule equal_refl)
```
```   335
```
```   336 code_datatype "1 :: num1"
```
```   337
```
```   338 instantiation num1 :: equal begin
```
```   339 definition equal_num1 :: "num1 \<Rightarrow> num1 \<Rightarrow> bool"
```
```   340   where "equal_num1 = op ="
```
```   341 instance by intro_classes (simp add: equal_num1_def)
```
```   342 end
```
```   343
```
```   344 lemma equal_num1_code [code]:
```
```   345   "equal_class.equal (1 :: num1) 1 = True"
```
```   346 by(rule equal_refl)
```
```   347
```
```   348 instantiation num1 :: enum begin
```
```   349 definition "enum_class.enum = [1 :: num1]"
```
```   350 definition "enum_class.enum_all P = P (1 :: num1)"
```
```   351 definition "enum_class.enum_ex P = P (1 :: num1)"
```
```   352 instance
```
```   353   by intro_classes
```
```   354      (auto simp add: enum_num1_def enum_all_num1_def enum_ex_num1_def num1_eq_iff Ball_def,
```
```   355       (metis (full_types) num1_eq_iff)+)
```
```   356 end
```
```   357
```
```   358 instantiation num0 and num1 :: card_UNIV begin
```
```   359 definition "finite_UNIV = Phantom(num0) False"
```
```   360 definition "card_UNIV = Phantom(num0) 0"
```
```   361 definition "finite_UNIV = Phantom(num1) True"
```
```   362 definition "card_UNIV = Phantom(num1) 1"
```
```   363 instance
```
```   364   by intro_classes
```
```   365      (simp_all add: finite_UNIV_num0_def card_UNIV_num0_def infinite_num0 finite_UNIV_num1_def card_UNIV_num1_def)
```
```   366 end
```
```   367
```
```   368
```
```   369 text {* Code setup for @{typ "'a bit0"} and @{typ "'a bit1"} *}
```
```   370
```
```   371 declare
```
```   372   bit0.Rep_inverse[code abstype]
```
```   373   bit0.Rep_0[code abstract]
```
```   374   bit0.Rep_1[code abstract]
```
```   375
```
```   376 lemma Abs_bit0'_code [code abstract]:
```
```   377   "Rep_bit0 (Abs_bit0' x :: 'a :: finite bit0) = x mod int (CARD('a bit0))"
```
```   378 by(auto simp add: Abs_bit0'_def intro!: Abs_bit0_inverse)
```
```   379   (metis bit0.Rep_Abs_mod bit0.Rep_less_n card_bit0 of_nat_numeral zmult_int)
```
```   380
```
```   381 lemma inj_on_Abs_bit0:
```
```   382   "inj_on (Abs_bit0 :: int \<Rightarrow> 'a bit0) {0..<2 * int CARD('a :: finite)}"
```
```   383 by(auto intro: inj_onI simp add: Abs_bit0_inject)
```
```   384
```
```   385 declare
```
```   386   bit1.Rep_inverse[code abstype]
```
```   387   bit1.Rep_0[code abstract]
```
```   388   bit1.Rep_1[code abstract]
```
```   389
```
```   390 lemma Abs_bit1'_code [code abstract]:
```
```   391   "Rep_bit1 (Abs_bit1' x :: 'a :: finite bit1) = x mod int (CARD('a bit1))"
```
```   392 by(auto simp add: Abs_bit1'_def intro!: Abs_bit1_inverse)
```
```   393   (metis of_nat_0_less_iff of_nat_Suc of_nat_mult of_nat_numeral pos_mod_conj zero_less_Suc)
```
```   394
```
```   395 lemma inj_on_Abs_bit1:
```
```   396   "inj_on (Abs_bit1 :: int \<Rightarrow> 'a bit1) {0..<1 + 2 * int CARD('a :: finite)}"
```
```   397 by(auto intro: inj_onI simp add: Abs_bit1_inject)
```
```   398
```
```   399 instantiation bit0 and bit1 :: (finite) equal begin
```
```   400
```
```   401 definition "equal_class.equal x y \<longleftrightarrow> Rep_bit0 x = Rep_bit0 y"
```
```   402 definition "equal_class.equal x y \<longleftrightarrow> Rep_bit1 x = Rep_bit1 y"
```
```   403
```
```   404 instance
```
```   405   by intro_classes (simp_all add: equal_bit0_def equal_bit1_def Rep_bit0_inject Rep_bit1_inject)
```
```   406
```
```   407 end
```
```   408
```
```   409 instantiation bit0 :: (finite) enum begin
```
```   410 definition "(enum_class.enum :: 'a bit0 list) = map (Abs_bit0' \<circ> int) (upt 0 (CARD('a bit0)))"
```
```   411 definition "enum_class.enum_all P = (\<forall>b :: 'a bit0 \<in> set enum_class.enum. P b)"
```
```   412 definition "enum_class.enum_ex P = (\<exists>b :: 'a bit0 \<in> set enum_class.enum. P b)"
```
```   413
```
```   414 instance
```
```   415 proof(intro_classes)
```
```   416   show "distinct (enum_class.enum :: 'a bit0 list)"
```
```   417     by(auto intro!: inj_onI simp add: Abs_bit0'_def Abs_bit0_inject zmod_int[symmetric] enum_bit0_def distinct_map)
```
```   418
```
```   419   show univ_eq: "(UNIV :: 'a bit0 set) = set enum_class.enum"
```
```   420     unfolding enum_bit0_def type_definition.Abs_image[OF type_definition_bit0, symmetric]
```
```   421     by(simp add: image_comp [symmetric] inj_on_Abs_bit0 card_image image_int_atLeastLessThan)
```
```   422       (auto intro!: image_cong[OF refl] simp add: Abs_bit0'_def mod_pos_pos_trivial)
```
```   423
```
```   424   fix P :: "'a bit0 \<Rightarrow> bool"
```
```   425   show "enum_class.enum_all P = Ball UNIV P"
```
```   426     and "enum_class.enum_ex P = Bex UNIV P"
```
```   427     by(simp_all add: enum_all_bit0_def enum_ex_bit0_def univ_eq)
```
```   428 qed
```
```   429
```
```   430 end
```
```   431
```
```   432 instantiation bit1 :: (finite) enum begin
```
```   433 definition "(enum_class.enum :: 'a bit1 list) = map (Abs_bit1' \<circ> int) (upt 0 (CARD('a bit1)))"
```
```   434 definition "enum_class.enum_all P = (\<forall>b :: 'a bit1 \<in> set enum_class.enum. P b)"
```
```   435 definition "enum_class.enum_ex P = (\<exists>b :: 'a bit1 \<in> set enum_class.enum. P b)"
```
```   436
```
```   437 instance
```
```   438 proof(intro_classes)
```
```   439   show "distinct (enum_class.enum :: 'a bit1 list)"
```
```   440     by(simp only: Abs_bit1'_def zmod_int[symmetric] enum_bit1_def distinct_map Suc_eq_plus1 card_bit1 o_apply inj_on_def)
```
```   441       (clarsimp simp add: Abs_bit1_inject)
```
```   442
```
```   443   show univ_eq: "(UNIV :: 'a bit1 set) = set enum_class.enum"
```
```   444     unfolding enum_bit1_def type_definition.Abs_image[OF type_definition_bit1, symmetric]
```
```   445     by(simp add: image_comp [symmetric] inj_on_Abs_bit1 card_image image_int_atLeastLessThan)
```
```   446       (auto intro!: image_cong[OF refl] simp add: Abs_bit1'_def mod_pos_pos_trivial)
```
```   447
```
```   448   fix P :: "'a bit1 \<Rightarrow> bool"
```
```   449   show "enum_class.enum_all P = Ball UNIV P"
```
```   450     and "enum_class.enum_ex P = Bex UNIV P"
```
```   451     by(simp_all add: enum_all_bit1_def enum_ex_bit1_def univ_eq)
```
```   452 qed
```
```   453
```
```   454 end
```
```   455
```
```   456 instantiation bit0 and bit1 :: (finite) finite_UNIV begin
```
```   457 definition "finite_UNIV = Phantom('a bit0) True"
```
```   458 definition "finite_UNIV = Phantom('a bit1) True"
```
```   459 instance by intro_classes (simp_all add: finite_UNIV_bit0_def finite_UNIV_bit1_def)
```
```   460 end
```
```   461
```
```   462 instantiation bit0 and bit1 :: ("{finite,card_UNIV}") card_UNIV begin
```
```   463 definition "card_UNIV = Phantom('a bit0) (2 * of_phantom (card_UNIV :: 'a card_UNIV))"
```
```   464 definition "card_UNIV = Phantom('a bit1) (1 + 2 * of_phantom (card_UNIV :: 'a card_UNIV))"
```
```   465 instance by intro_classes (simp_all add: card_UNIV_bit0_def card_UNIV_bit1_def card_UNIV)
```
```   466 end
```
```   467
```
```   468 subsection {* Syntax *}
```
```   469
```
```   470 syntax
```
```   471   "_NumeralType" :: "num_token => type"  ("_")
```
```   472   "_NumeralType0" :: type ("0")
```
```   473   "_NumeralType1" :: type ("1")
```
```   474
```
```   475 translations
```
```   476   (type) "1" == (type) "num1"
```
```   477   (type) "0" == (type) "num0"
```
```   478
```
```   479 parse_translation {*
```
```   480   let
```
```   481     fun mk_bintype n =
```
```   482       let
```
```   483         fun mk_bit 0 = Syntax.const @{type_syntax bit0}
```
```   484           | mk_bit 1 = Syntax.const @{type_syntax bit1};
```
```   485         fun bin_of n =
```
```   486           if n = 1 then Syntax.const @{type_syntax num1}
```
```   487           else if n = 0 then Syntax.const @{type_syntax num0}
```
```   488           else if n = ~1 then raise TERM ("negative type numeral", [])
```
```   489           else
```
```   490             let val (q, r) = Integer.div_mod n 2;
```
```   491             in mk_bit r \$ bin_of q end;
```
```   492       in bin_of n end;
```
```   493
```
```   494     fun numeral_tr [Free (str, _)] = mk_bintype (the (Int.fromString str))
```
```   495       | numeral_tr ts = raise TERM ("numeral_tr", ts);
```
```   496
```
```   497   in [(@{syntax_const "_NumeralType"}, K numeral_tr)] end;
```
```   498 *}
```
```   499
```
```   500 print_translation {*
```
```   501   let
```
```   502     fun int_of [] = 0
```
```   503       | int_of (b :: bs) = b + 2 * int_of bs;
```
```   504
```
```   505     fun bin_of (Const (@{type_syntax num0}, _)) = []
```
```   506       | bin_of (Const (@{type_syntax num1}, _)) = [1]
```
```   507       | bin_of (Const (@{type_syntax bit0}, _) \$ bs) = 0 :: bin_of bs
```
```   508       | bin_of (Const (@{type_syntax bit1}, _) \$ bs) = 1 :: bin_of bs
```
```   509       | bin_of t = raise TERM ("bin_of", [t]);
```
```   510
```
```   511     fun bit_tr' b [t] =
```
```   512           let
```
```   513             val rev_digs = b :: bin_of t handle TERM _ => raise Match
```
```   514             val i = int_of rev_digs;
```
```   515             val num = string_of_int (abs i);
```
```   516           in
```
```   517             Syntax.const @{syntax_const "_NumeralType"} \$ Syntax.free num
```
```   518           end
```
```   519       | bit_tr' b _ = raise Match;
```
```   520   in
```
```   521    [(@{type_syntax bit0}, K (bit_tr' 0)),
```
```   522     (@{type_syntax bit1}, K (bit_tr' 1))]
```
```   523   end;
```
```   524 *}
```
```   525
```
```   526 subsection {* Examples *}
```
```   527
```
```   528 lemma "CARD(0) = 0" by simp
```
```   529 lemma "CARD(17) = 17" by simp
```
```   530 lemma "8 * 11 ^ 3 - 6 = (2::5)" by simp
```
```   531
```
```   532 end
```