src/HOL/Library/Numeral_Type.thy
author wenzelm
Tue May 15 13:57:39 2018 +0200 (16 months ago)
changeset 68189 6163c90694ef
parent 67411 3f4b0c84630f
child 69216 1a52baa70aed
permissions -rw-r--r--
tuned headers;
     1 (*  Title:      HOL/Library/Numeral_Type.thy
     2     Author:     Brian Huffman
     3 *)
     4 
     5 section \<open>Numeral Syntax for Types\<close>
     6 
     7 theory Numeral_Type
     8 imports Cardinality
     9 begin
    10 
    11 subsection \<open>Numeral Types\<close>
    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 \<open>Locales for for modular arithmetic subtypes\<close>
    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 done
   129 
   130 lemmas Rep_simps =
   131   Rep_inject_sym Rep_inverse Rep_Abs_mod Rep_mod Rep_Abs_0 Rep_Abs_1
   132 
   133 lemma comm_ring_1: "OFCLASS('a, comm_ring_1_class)"
   134 apply (intro_classes, unfold definitions)
   135 apply (simp_all add: Rep_simps mod_simps field_simps)
   136 done
   137 
   138 end
   139 
   140 locale mod_ring = mod_type n Rep Abs
   141   for n :: int
   142   and Rep :: "'a::{comm_ring_1} \<Rightarrow> int"
   143   and Abs :: "int \<Rightarrow> 'a::{comm_ring_1}"
   144 begin
   145 
   146 lemma of_nat_eq: "of_nat k = Abs (int k mod n)"
   147 apply (induct k)
   148 apply (simp add: zero_def)
   149 apply (simp add: Rep_simps add_def one_def mod_simps ac_simps)
   150 done
   151 
   152 lemma of_int_eq: "of_int z = Abs (z mod n)"
   153 apply (cases z rule: int_diff_cases)
   154 apply (simp add: Rep_simps of_nat_eq diff_def mod_simps)
   155 done
   156 
   157 lemma Rep_numeral:
   158   "Rep (numeral w) = numeral w mod n"
   159 using of_int_eq [of "numeral w"]
   160 by (simp add: Rep_inject_sym Rep_Abs_mod)
   161 
   162 lemma iszero_numeral:
   163   "iszero (numeral w::'a) \<longleftrightarrow> numeral w mod n = 0"
   164 by (simp add: Rep_inject_sym Rep_numeral Rep_0 iszero_def)
   165 
   166 lemma cases:
   167   assumes 1: "\<And>z. \<lbrakk>(x::'a) = of_int z; 0 \<le> z; z < n\<rbrakk> \<Longrightarrow> P"
   168   shows "P"
   169 apply (cases x rule: type_definition.Abs_cases [OF type])
   170 apply (rule_tac z="y" in 1)
   171 apply (simp_all add: of_int_eq)
   172 done
   173 
   174 lemma induct:
   175   "(\<And>z. \<lbrakk>0 \<le> z; z < n\<rbrakk> \<Longrightarrow> P (of_int z)) \<Longrightarrow> P (x::'a)"
   176 by (cases x rule: cases) simp
   177 
   178 end
   179 
   180 
   181 subsection \<open>Ring class instances\<close>
   182 
   183 text \<open>
   184   Unfortunately \<open>ring_1\<close> instance is not possible for
   185   @{typ num1}, since 0 and 1 are not distinct.
   186 \<close>
   187 
   188 instantiation num1 :: "{comm_ring,comm_monoid_mult,numeral}"
   189 begin
   190 
   191 lemma num1_eq_iff: "(x::num1) = (y::num1) \<longleftrightarrow> True"
   192   by (induct x, induct y) simp
   193 
   194 instance
   195   by standard (simp_all add: num1_eq_iff)
   196 
   197 end
   198 
   199 instantiation
   200   bit0 and bit1 :: (finite) "{zero,one,plus,times,uminus,minus}"
   201 begin
   202 
   203 definition Abs_bit0' :: "int \<Rightarrow> 'a bit0" where
   204   "Abs_bit0' x = Abs_bit0 (x mod int CARD('a bit0))"
   205 
   206 definition Abs_bit1' :: "int \<Rightarrow> 'a bit1" where
   207   "Abs_bit1' x = Abs_bit1 (x mod int CARD('a bit1))"
   208 
   209 definition "0 = Abs_bit0 0"
   210 definition "1 = Abs_bit0 1"
   211 definition "x + y = Abs_bit0' (Rep_bit0 x + Rep_bit0 y)"
   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 = Abs_bit0' (- Rep_bit0 x)"
   215 
   216 definition "0 = Abs_bit1 0"
   217 definition "1 = Abs_bit1 1"
   218 definition "x + y = Abs_bit1' (Rep_bit1 x + Rep_bit1 y)"
   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 = Abs_bit1' (- Rep_bit1 x)"
   222 
   223 instance ..
   224 
   225 end
   226 
   227 interpretation bit0:
   228   mod_type "int CARD('a::finite bit0)"
   229            "Rep_bit0 :: 'a::finite bit0 \<Rightarrow> int"
   230            "Abs_bit0 :: int \<Rightarrow> 'a::finite bit0"
   231 apply (rule mod_type.intro)
   232 apply (simp add: type_definition_bit0)
   233 apply (rule one_less_int_card)
   234 apply (rule zero_bit0_def)
   235 apply (rule one_bit0_def)
   236 apply (rule plus_bit0_def [unfolded Abs_bit0'_def])
   237 apply (rule times_bit0_def [unfolded Abs_bit0'_def])
   238 apply (rule minus_bit0_def [unfolded Abs_bit0'_def])
   239 apply (rule uminus_bit0_def [unfolded Abs_bit0'_def])
   240 done
   241 
   242 interpretation bit1:
   243   mod_type "int CARD('a::finite bit1)"
   244            "Rep_bit1 :: 'a::finite bit1 \<Rightarrow> int"
   245            "Abs_bit1 :: int \<Rightarrow> 'a::finite bit1"
   246 apply (rule mod_type.intro)
   247 apply (simp add: type_definition_bit1)
   248 apply (rule one_less_int_card)
   249 apply (rule zero_bit1_def)
   250 apply (rule one_bit1_def)
   251 apply (rule plus_bit1_def [unfolded Abs_bit1'_def])
   252 apply (rule times_bit1_def [unfolded Abs_bit1'_def])
   253 apply (rule minus_bit1_def [unfolded Abs_bit1'_def])
   254 apply (rule uminus_bit1_def [unfolded Abs_bit1'_def])
   255 done
   256 
   257 instance bit0 :: (finite) comm_ring_1
   258   by (rule bit0.comm_ring_1)
   259 
   260 instance bit1 :: (finite) comm_ring_1
   261   by (rule bit1.comm_ring_1)
   262 
   263 interpretation bit0:
   264   mod_ring "int CARD('a::finite bit0)"
   265            "Rep_bit0 :: 'a::finite bit0 \<Rightarrow> int"
   266            "Abs_bit0 :: int \<Rightarrow> 'a::finite bit0"
   267   ..
   268 
   269 interpretation bit1:
   270   mod_ring "int CARD('a::finite bit1)"
   271            "Rep_bit1 :: 'a::finite bit1 \<Rightarrow> int"
   272            "Abs_bit1 :: int \<Rightarrow> 'a::finite bit1"
   273   ..
   274 
   275 text \<open>Set up cases, induction, and arithmetic\<close>
   276 
   277 lemmas bit0_cases [case_names of_int, cases type: bit0] = bit0.cases
   278 lemmas bit1_cases [case_names of_int, cases type: bit1] = bit1.cases
   279 
   280 lemmas bit0_induct [case_names of_int, induct type: bit0] = bit0.induct
   281 lemmas bit1_induct [case_names of_int, induct type: bit1] = bit1.induct
   282 
   283 lemmas bit0_iszero_numeral [simp] = bit0.iszero_numeral
   284 lemmas bit1_iszero_numeral [simp] = bit1.iszero_numeral
   285 
   286 lemmas [simp] = eq_numeral_iff_iszero [where 'a="'a bit0"] for dummy :: "'a::finite"
   287 lemmas [simp] = eq_numeral_iff_iszero [where 'a="'a bit1"] for dummy :: "'a::finite"
   288 
   289 subsection \<open>Order instances\<close>
   290 
   291 instantiation bit0 and bit1 :: (finite) linorder begin
   292 definition "a < b \<longleftrightarrow> Rep_bit0 a < Rep_bit0 b"
   293 definition "a \<le> b \<longleftrightarrow> Rep_bit0 a \<le> Rep_bit0 b"
   294 definition "a < b \<longleftrightarrow> Rep_bit1 a < Rep_bit1 b"
   295 definition "a \<le> b \<longleftrightarrow> Rep_bit1 a \<le> Rep_bit1 b"
   296 
   297 instance
   298   by(intro_classes)
   299     (auto simp add: less_eq_bit0_def less_bit0_def less_eq_bit1_def less_bit1_def Rep_bit0_inject Rep_bit1_inject)
   300 end
   301 
   302 lemma (in preorder) tranclp_less: "(<) \<^sup>+\<^sup>+ = (<)"
   303 by(auto simp add: fun_eq_iff intro: less_trans elim: tranclp.induct)
   304 
   305 instance bit0 and bit1 :: (finite) wellorder
   306 proof -
   307   have "wf {(x :: 'a bit0, y). x < y}"
   308     by(auto simp add: trancl_def tranclp_less intro!: finite_acyclic_wf acyclicI)
   309   thus "OFCLASS('a bit0, wellorder_class)"
   310     by(rule wf_wellorderI) intro_classes
   311 next
   312   have "wf {(x :: 'a bit1, y). x < y}"
   313     by(auto simp add: trancl_def tranclp_less intro!: finite_acyclic_wf acyclicI)
   314   thus "OFCLASS('a bit1, wellorder_class)"
   315     by(rule wf_wellorderI) intro_classes
   316 qed
   317 
   318 subsection \<open>Code setup and type classes for code generation\<close>
   319 
   320 text \<open>Code setup for @{typ num0} and @{typ num1}\<close>
   321 
   322 definition Num0 :: num0 where "Num0 = Abs_num0 0"
   323 code_datatype Num0
   324 
   325 instantiation num0 :: equal begin
   326 definition equal_num0 :: "num0 \<Rightarrow> num0 \<Rightarrow> bool"
   327   where "equal_num0 = (=)"
   328 instance by intro_classes (simp add: equal_num0_def)
   329 end
   330 
   331 lemma equal_num0_code [code]:
   332   "equal_class.equal Num0 Num0 = True"
   333 by(rule equal_refl)
   334 
   335 code_datatype "1 :: num1"
   336 
   337 instantiation num1 :: equal begin
   338 definition equal_num1 :: "num1 \<Rightarrow> num1 \<Rightarrow> bool"
   339   where "equal_num1 = (=)"
   340 instance by intro_classes (simp add: equal_num1_def)
   341 end
   342 
   343 lemma equal_num1_code [code]:
   344   "equal_class.equal (1 :: num1) 1 = True"
   345 by(rule equal_refl)
   346 
   347 instantiation num1 :: enum begin
   348 definition "enum_class.enum = [1 :: num1]"
   349 definition "enum_class.enum_all P = P (1 :: num1)"
   350 definition "enum_class.enum_ex P = P (1 :: num1)"
   351 instance
   352   by intro_classes
   353      (auto simp add: enum_num1_def enum_all_num1_def enum_ex_num1_def num1_eq_iff Ball_def,
   354       (metis (full_types) num1_eq_iff)+)
   355 end
   356 
   357 instantiation num0 and num1 :: card_UNIV begin
   358 definition "finite_UNIV = Phantom(num0) False"
   359 definition "card_UNIV = Phantom(num0) 0"
   360 definition "finite_UNIV = Phantom(num1) True"
   361 definition "card_UNIV = Phantom(num1) 1"
   362 instance
   363   by intro_classes
   364      (simp_all add: finite_UNIV_num0_def card_UNIV_num0_def infinite_num0 finite_UNIV_num1_def card_UNIV_num1_def)
   365 end
   366 
   367 
   368 text \<open>Code setup for @{typ "'a bit0"} and @{typ "'a bit1"}\<close>
   369 
   370 declare
   371   bit0.Rep_inverse[code abstype]
   372   bit0.Rep_0[code abstract]
   373   bit0.Rep_1[code abstract]
   374 
   375 lemma Abs_bit0'_code [code abstract]:
   376   "Rep_bit0 (Abs_bit0' x :: 'a :: finite bit0) = x mod int (CARD('a bit0))"
   377 by(auto simp add: Abs_bit0'_def intro!: Abs_bit0_inverse)
   378 
   379 lemma inj_on_Abs_bit0:
   380   "inj_on (Abs_bit0 :: int \<Rightarrow> 'a bit0) {0..<2 * int CARD('a :: finite)}"
   381 by(auto intro: inj_onI simp add: Abs_bit0_inject)
   382 
   383 declare
   384   bit1.Rep_inverse[code abstype]
   385   bit1.Rep_0[code abstract]
   386   bit1.Rep_1[code abstract]
   387 
   388 lemma Abs_bit1'_code [code abstract]:
   389   "Rep_bit1 (Abs_bit1' x :: 'a :: finite bit1) = x mod int (CARD('a bit1))"
   390   by(auto simp add: Abs_bit1'_def intro!: Abs_bit1_inverse)
   391 
   392 lemma inj_on_Abs_bit1:
   393   "inj_on (Abs_bit1 :: int \<Rightarrow> 'a bit1) {0..<1 + 2 * int CARD('a :: finite)}"
   394 by(auto intro: inj_onI simp add: Abs_bit1_inject)
   395 
   396 instantiation bit0 and bit1 :: (finite) equal begin
   397 
   398 definition "equal_class.equal x y \<longleftrightarrow> Rep_bit0 x = Rep_bit0 y"
   399 definition "equal_class.equal x y \<longleftrightarrow> Rep_bit1 x = Rep_bit1 y"
   400 
   401 instance
   402   by intro_classes (simp_all add: equal_bit0_def equal_bit1_def Rep_bit0_inject Rep_bit1_inject)
   403 
   404 end
   405 
   406 instantiation bit0 :: (finite) enum begin
   407 definition "(enum_class.enum :: 'a bit0 list) = map (Abs_bit0' \<circ> int) (upt 0 (CARD('a bit0)))"
   408 definition "enum_class.enum_all P = (\<forall>b :: 'a bit0 \<in> set enum_class.enum. P b)"
   409 definition "enum_class.enum_ex P = (\<exists>b :: 'a bit0 \<in> set enum_class.enum. P b)"
   410 
   411 instance
   412 proof(intro_classes)
   413   show "distinct (enum_class.enum :: 'a bit0 list)"
   414     by (simp add: enum_bit0_def distinct_map inj_on_def Abs_bit0'_def Abs_bit0_inject)
   415 
   416   show univ_eq: "(UNIV :: 'a bit0 set) = set enum_class.enum"
   417     unfolding enum_bit0_def type_definition.Abs_image[OF type_definition_bit0, symmetric]
   418     by (simp add: image_comp [symmetric] inj_on_Abs_bit0 card_image image_int_atLeastLessThan)
   419       (auto intro!: image_cong[OF refl] simp add: Abs_bit0'_def)
   420 
   421   fix P :: "'a bit0 \<Rightarrow> bool"
   422   show "enum_class.enum_all P = Ball UNIV P"
   423     and "enum_class.enum_ex P = Bex UNIV P"
   424     by(simp_all add: enum_all_bit0_def enum_ex_bit0_def univ_eq)
   425 qed
   426 
   427 end
   428 
   429 instantiation bit1 :: (finite) enum begin
   430 definition "(enum_class.enum :: 'a bit1 list) = map (Abs_bit1' \<circ> int) (upt 0 (CARD('a bit1)))"
   431 definition "enum_class.enum_all P = (\<forall>b :: 'a bit1 \<in> set enum_class.enum. P b)"
   432 definition "enum_class.enum_ex P = (\<exists>b :: 'a bit1 \<in> set enum_class.enum. P b)"
   433 
   434 instance
   435 proof(intro_classes)
   436   show "distinct (enum_class.enum :: 'a bit1 list)"
   437     by(simp only: Abs_bit1'_def zmod_int[symmetric] enum_bit1_def distinct_map Suc_eq_plus1 card_bit1 o_apply inj_on_def)
   438       (clarsimp simp add: Abs_bit1_inject)
   439 
   440   show univ_eq: "(UNIV :: 'a bit1 set) = set enum_class.enum"
   441     unfolding enum_bit1_def type_definition.Abs_image[OF type_definition_bit1, symmetric]
   442     by(simp add: image_comp [symmetric] inj_on_Abs_bit1 card_image image_int_atLeastLessThan)
   443       (auto intro!: image_cong[OF refl] simp add: Abs_bit1'_def)
   444 
   445   fix P :: "'a bit1 \<Rightarrow> bool"
   446   show "enum_class.enum_all P = Ball UNIV P"
   447     and "enum_class.enum_ex P = Bex UNIV P"
   448     by(simp_all add: enum_all_bit1_def enum_ex_bit1_def univ_eq)
   449 qed
   450 
   451 end
   452 
   453 instantiation bit0 and bit1 :: (finite) finite_UNIV begin
   454 definition "finite_UNIV = Phantom('a bit0) True"
   455 definition "finite_UNIV = Phantom('a bit1) True"
   456 instance by intro_classes (simp_all add: finite_UNIV_bit0_def finite_UNIV_bit1_def)
   457 end
   458 
   459 instantiation bit0 and bit1 :: ("{finite,card_UNIV}") card_UNIV begin
   460 definition "card_UNIV = Phantom('a bit0) (2 * of_phantom (card_UNIV :: 'a card_UNIV))"
   461 definition "card_UNIV = Phantom('a bit1) (1 + 2 * of_phantom (card_UNIV :: 'a card_UNIV))"
   462 instance by intro_classes (simp_all add: card_UNIV_bit0_def card_UNIV_bit1_def card_UNIV)
   463 end
   464 
   465 subsection \<open>Syntax\<close>
   466 
   467 syntax
   468   "_NumeralType" :: "num_token => type"  ("_")
   469   "_NumeralType0" :: type ("0")
   470   "_NumeralType1" :: type ("1")
   471 
   472 translations
   473   (type) "1" == (type) "num1"
   474   (type) "0" == (type) "num0"
   475 
   476 parse_translation \<open>
   477   let
   478     fun mk_bintype n =
   479       let
   480         fun mk_bit 0 = Syntax.const @{type_syntax bit0}
   481           | mk_bit 1 = Syntax.const @{type_syntax bit1};
   482         fun bin_of n =
   483           if n = 1 then Syntax.const @{type_syntax num1}
   484           else if n = 0 then Syntax.const @{type_syntax num0}
   485           else if n = ~1 then raise TERM ("negative type numeral", [])
   486           else
   487             let val (q, r) = Integer.div_mod n 2;
   488             in mk_bit r $ bin_of q end;
   489       in bin_of n end;
   490 
   491     fun numeral_tr [Free (str, _)] = mk_bintype (the (Int.fromString str))
   492       | numeral_tr ts = raise TERM ("numeral_tr", ts);
   493 
   494   in [(@{syntax_const "_NumeralType"}, K numeral_tr)] end;
   495 \<close>
   496 
   497 print_translation \<open>
   498   let
   499     fun int_of [] = 0
   500       | int_of (b :: bs) = b + 2 * int_of bs;
   501 
   502     fun bin_of (Const (@{type_syntax num0}, _)) = []
   503       | bin_of (Const (@{type_syntax num1}, _)) = [1]
   504       | bin_of (Const (@{type_syntax bit0}, _) $ bs) = 0 :: bin_of bs
   505       | bin_of (Const (@{type_syntax bit1}, _) $ bs) = 1 :: bin_of bs
   506       | bin_of t = raise TERM ("bin_of", [t]);
   507 
   508     fun bit_tr' b [t] =
   509           let
   510             val rev_digs = b :: bin_of t handle TERM _ => raise Match
   511             val i = int_of rev_digs;
   512             val num = string_of_int (abs i);
   513           in
   514             Syntax.const @{syntax_const "_NumeralType"} $ Syntax.free num
   515           end
   516       | bit_tr' b _ = raise Match;
   517   in
   518    [(@{type_syntax bit0}, K (bit_tr' 0)),
   519     (@{type_syntax bit1}, K (bit_tr' 1))]
   520   end;
   521 \<close>
   522 
   523 subsection \<open>Examples\<close>
   524 
   525 lemma "CARD(0) = 0" by simp
   526 lemma "CARD(17) = 17" by simp
   527 lemma "8 * 11 ^ 3 - 6 = (2::5)" by simp
   528 
   529 end