src/HOL/Library/Numeral_Type.thy
author haftmann
Mon Jun 05 15:59:41 2017 +0200 (2017-06-05)
changeset 66010 2f7d39285a1a
parent 64593 50c715579715
child 66886 960509bfd47e
permissions -rw-r--r--
executable domain membership checks
     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 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 mod_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 mod_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 mod_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 \<open>Ring class instances\<close>
   183 
   184 text \<open>
   185   Unfortunately \<open>ring_1\<close> instance is not possible for
   186   @{typ num1}, since 0 and 1 are not distinct.
   187 \<close>
   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
   196   by standard (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: of_nat_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: of_nat_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 \<open>Set up cases, induction, and arithmetic\<close>
   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 \<open>Order instances\<close>
   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 \<open>Code setup and type classes for code generation\<close>
   320 
   321 text \<open>Code setup for @{typ num0} and @{typ num1}\<close>
   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 \<open>Code setup for @{typ "'a bit0"} and @{typ "'a bit1"}\<close>
   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 
   380 lemma inj_on_Abs_bit0:
   381   "inj_on (Abs_bit0 :: int \<Rightarrow> 'a bit0) {0..<2 * int CARD('a :: finite)}"
   382 by(auto intro: inj_onI simp add: Abs_bit0_inject)
   383 
   384 declare
   385   bit1.Rep_inverse[code abstype]
   386   bit1.Rep_0[code abstract]
   387   bit1.Rep_1[code abstract]
   388 
   389 lemma Abs_bit1'_code [code abstract]:
   390   "Rep_bit1 (Abs_bit1' x :: 'a :: finite bit1) = x mod int (CARD('a bit1))"
   391   by(auto simp add: Abs_bit1'_def intro!: Abs_bit1_inverse)
   392 
   393 lemma inj_on_Abs_bit1:
   394   "inj_on (Abs_bit1 :: int \<Rightarrow> 'a bit1) {0..<1 + 2 * int CARD('a :: finite)}"
   395 by(auto intro: inj_onI simp add: Abs_bit1_inject)
   396 
   397 instantiation bit0 and bit1 :: (finite) equal begin
   398 
   399 definition "equal_class.equal x y \<longleftrightarrow> Rep_bit0 x = Rep_bit0 y"
   400 definition "equal_class.equal x y \<longleftrightarrow> Rep_bit1 x = Rep_bit1 y"
   401 
   402 instance
   403   by intro_classes (simp_all add: equal_bit0_def equal_bit1_def Rep_bit0_inject Rep_bit1_inject)
   404 
   405 end
   406 
   407 instantiation bit0 :: (finite) enum begin
   408 definition "(enum_class.enum :: 'a bit0 list) = map (Abs_bit0' \<circ> int) (upt 0 (CARD('a bit0)))"
   409 definition "enum_class.enum_all P = (\<forall>b :: 'a bit0 \<in> set enum_class.enum. P b)"
   410 definition "enum_class.enum_ex P = (\<exists>b :: 'a bit0 \<in> set enum_class.enum. P b)"
   411 
   412 instance
   413 proof(intro_classes)
   414   show "distinct (enum_class.enum :: 'a bit0 list)"
   415     by (simp add: enum_bit0_def distinct_map inj_on_def Abs_bit0'_def Abs_bit0_inject mod_pos_pos_trivial)
   416 
   417   show univ_eq: "(UNIV :: 'a bit0 set) = set enum_class.enum"
   418     unfolding enum_bit0_def type_definition.Abs_image[OF type_definition_bit0, symmetric]
   419     by(simp add: image_comp [symmetric] inj_on_Abs_bit0 card_image image_int_atLeastLessThan)
   420       (auto intro!: image_cong[OF refl] simp add: Abs_bit0'_def mod_pos_pos_trivial)
   421 
   422   fix P :: "'a bit0 \<Rightarrow> bool"
   423   show "enum_class.enum_all P = Ball UNIV P"
   424     and "enum_class.enum_ex P = Bex UNIV P"
   425     by(simp_all add: enum_all_bit0_def enum_ex_bit0_def univ_eq)
   426 qed
   427 
   428 end
   429 
   430 instantiation bit1 :: (finite) enum begin
   431 definition "(enum_class.enum :: 'a bit1 list) = map (Abs_bit1' \<circ> int) (upt 0 (CARD('a bit1)))"
   432 definition "enum_class.enum_all P = (\<forall>b :: 'a bit1 \<in> set enum_class.enum. P b)"
   433 definition "enum_class.enum_ex P = (\<exists>b :: 'a bit1 \<in> set enum_class.enum. P b)"
   434 
   435 instance
   436 proof(intro_classes)
   437   show "distinct (enum_class.enum :: 'a bit1 list)"
   438     by(simp only: Abs_bit1'_def zmod_int[symmetric] enum_bit1_def distinct_map Suc_eq_plus1 card_bit1 o_apply inj_on_def)
   439       (clarsimp simp add: Abs_bit1_inject)
   440 
   441   show univ_eq: "(UNIV :: 'a bit1 set) = set enum_class.enum"
   442     unfolding enum_bit1_def type_definition.Abs_image[OF type_definition_bit1, symmetric]
   443     by(simp add: image_comp [symmetric] inj_on_Abs_bit1 card_image image_int_atLeastLessThan)
   444       (auto intro!: image_cong[OF refl] simp add: Abs_bit1'_def mod_pos_pos_trivial)
   445 
   446   fix P :: "'a bit1 \<Rightarrow> bool"
   447   show "enum_class.enum_all P = Ball UNIV P"
   448     and "enum_class.enum_ex P = Bex UNIV P"
   449     by(simp_all add: enum_all_bit1_def enum_ex_bit1_def univ_eq)
   450 qed
   451 
   452 end
   453 
   454 instantiation bit0 and bit1 :: (finite) finite_UNIV begin
   455 definition "finite_UNIV = Phantom('a bit0) True"
   456 definition "finite_UNIV = Phantom('a bit1) True"
   457 instance by intro_classes (simp_all add: finite_UNIV_bit0_def finite_UNIV_bit1_def)
   458 end
   459 
   460 instantiation bit0 and bit1 :: ("{finite,card_UNIV}") card_UNIV begin
   461 definition "card_UNIV = Phantom('a bit0) (2 * of_phantom (card_UNIV :: 'a card_UNIV))"
   462 definition "card_UNIV = Phantom('a bit1) (1 + 2 * of_phantom (card_UNIV :: 'a card_UNIV))"
   463 instance by intro_classes (simp_all add: card_UNIV_bit0_def card_UNIV_bit1_def card_UNIV)
   464 end
   465 
   466 subsection \<open>Syntax\<close>
   467 
   468 syntax
   469   "_NumeralType" :: "num_token => type"  ("_")
   470   "_NumeralType0" :: type ("0")
   471   "_NumeralType1" :: type ("1")
   472 
   473 translations
   474   (type) "1" == (type) "num1"
   475   (type) "0" == (type) "num0"
   476 
   477 parse_translation \<open>
   478   let
   479     fun mk_bintype n =
   480       let
   481         fun mk_bit 0 = Syntax.const @{type_syntax bit0}
   482           | mk_bit 1 = Syntax.const @{type_syntax bit1};
   483         fun bin_of n =
   484           if n = 1 then Syntax.const @{type_syntax num1}
   485           else if n = 0 then Syntax.const @{type_syntax num0}
   486           else if n = ~1 then raise TERM ("negative type numeral", [])
   487           else
   488             let val (q, r) = Integer.div_mod n 2;
   489             in mk_bit r $ bin_of q end;
   490       in bin_of n end;
   491 
   492     fun numeral_tr [Free (str, _)] = mk_bintype (the (Int.fromString str))
   493       | numeral_tr ts = raise TERM ("numeral_tr", ts);
   494 
   495   in [(@{syntax_const "_NumeralType"}, K numeral_tr)] end;
   496 \<close>
   497 
   498 print_translation \<open>
   499   let
   500     fun int_of [] = 0
   501       | int_of (b :: bs) = b + 2 * int_of bs;
   502 
   503     fun bin_of (Const (@{type_syntax num0}, _)) = []
   504       | bin_of (Const (@{type_syntax num1}, _)) = [1]
   505       | bin_of (Const (@{type_syntax bit0}, _) $ bs) = 0 :: bin_of bs
   506       | bin_of (Const (@{type_syntax bit1}, _) $ bs) = 1 :: bin_of bs
   507       | bin_of t = raise TERM ("bin_of", [t]);
   508 
   509     fun bit_tr' b [t] =
   510           let
   511             val rev_digs = b :: bin_of t handle TERM _ => raise Match
   512             val i = int_of rev_digs;
   513             val num = string_of_int (abs i);
   514           in
   515             Syntax.const @{syntax_const "_NumeralType"} $ Syntax.free num
   516           end
   517       | bit_tr' b _ = raise Match;
   518   in
   519    [(@{type_syntax bit0}, K (bit_tr' 0)),
   520     (@{type_syntax bit1}, K (bit_tr' 1))]
   521   end;
   522 \<close>
   523 
   524 subsection \<open>Examples\<close>
   525 
   526 lemma "CARD(0) = 0" by simp
   527 lemma "CARD(17) = 17" by simp
   528 lemma "8 * 11 ^ 3 - 6 = (2::5)" by simp
   529 
   530 end