src/HOL/Library/Numeral_Type.thy
author wenzelm
Thu Feb 11 23:00:22 2010 +0100 (2010-02-11)
changeset 35115 446c5063e4fd
parent 33361 1f18de40b43f
child 35362 828a42fb7445
permissions -rw-r--r--
modernized translations;
formal markup of @{syntax_const} and @{const_syntax};
minor tuning;
haftmann@29629
     1
(*  Title:      HOL/Library/Numeral_Type.thy
haftmann@29629
     2
    Author:     Brian Huffman
kleing@24332
     3
*)
kleing@24332
     4
haftmann@29629
     5
header {* Numeral Syntax for Types *}
kleing@24332
     6
kleing@24332
     7
theory Numeral_Type
haftmann@30663
     8
imports Main
kleing@24332
     9
begin
kleing@24332
    10
kleing@24332
    11
subsection {* Preliminary lemmas *}
kleing@24332
    12
(* These should be moved elsewhere *)
kleing@24332
    13
kleing@24332
    14
lemma (in type_definition) univ:
kleing@24332
    15
  "UNIV = Abs ` A"
kleing@24332
    16
proof
kleing@24332
    17
  show "Abs ` A \<subseteq> UNIV" by (rule subset_UNIV)
kleing@24332
    18
  show "UNIV \<subseteq> Abs ` A"
kleing@24332
    19
  proof
kleing@24332
    20
    fix x :: 'b
kleing@24332
    21
    have "x = Abs (Rep x)" by (rule Rep_inverse [symmetric])
kleing@24332
    22
    moreover have "Rep x \<in> A" by (rule Rep)
kleing@24332
    23
    ultimately show "x \<in> Abs ` A" by (rule image_eqI)
kleing@24332
    24
  qed
kleing@24332
    25
qed
kleing@24332
    26
kleing@24332
    27
lemma (in type_definition) card: "card (UNIV :: 'b set) = card A"
kleing@24332
    28
  by (simp add: univ card_image inj_on_def Abs_inject)
kleing@24332
    29
kleing@24332
    30
kleing@24332
    31
subsection {* Cardinalities of types *}
kleing@24332
    32
kleing@24332
    33
syntax "_type_card" :: "type => nat" ("(1CARD/(1'(_')))")
kleing@24332
    34
huffman@28920
    35
translations "CARD(t)" => "CONST card (CONST UNIV \<Colon> t set)"
kleing@24332
    36
huffman@24407
    37
typed_print_translation {*
huffman@24407
    38
let
wenzelm@35115
    39
  fun card_univ_tr' show_sorts _ [Const (@{const_syntax UNIV}, Type(_, [T, _]))] =
wenzelm@35115
    40
    Syntax.const @{syntax_const "_type_card"} $ Syntax.term_of_typ show_sorts T;
huffman@28920
    41
in [(@{const_syntax card}, card_univ_tr')]
huffman@24407
    42
end
huffman@24407
    43
*}
huffman@24407
    44
huffman@30001
    45
lemma card_unit [simp]: "CARD(unit) = 1"
haftmann@26153
    46
  unfolding UNIV_unit by simp
kleing@24332
    47
huffman@30001
    48
lemma card_bool [simp]: "CARD(bool) = 2"
haftmann@26153
    49
  unfolding UNIV_bool by simp
kleing@24332
    50
huffman@30001
    51
lemma card_prod [simp]: "CARD('a \<times> 'b) = CARD('a::finite) * CARD('b::finite)"
haftmann@26153
    52
  unfolding UNIV_Times_UNIV [symmetric] by (simp only: card_cartesian_product)
kleing@24332
    53
huffman@30001
    54
lemma card_sum [simp]: "CARD('a + 'b) = CARD('a::finite) + CARD('b::finite)"
haftmann@26153
    55
  unfolding UNIV_Plus_UNIV [symmetric] by (simp only: finite card_Plus)
kleing@24332
    56
huffman@30001
    57
lemma card_option [simp]: "CARD('a option) = Suc CARD('a::finite)"
nipkow@31080
    58
  unfolding UNIV_option_conv
kleing@24332
    59
  apply (subgoal_tac "(None::'a option) \<notin> range Some")
huffman@29997
    60
  apply (simp add: card_image)
kleing@24332
    61
  apply fast
kleing@24332
    62
  done
kleing@24332
    63
huffman@30001
    64
lemma card_set [simp]: "CARD('a set) = 2 ^ CARD('a::finite)"
haftmann@26153
    65
  unfolding Pow_UNIV [symmetric]
kleing@24332
    66
  by (simp only: card_Pow finite numeral_2_eq_2)
kleing@24332
    67
huffman@30001
    68
lemma card_nat [simp]: "CARD(nat) = 0"
huffman@30001
    69
  by (simp add: infinite_UNIV_nat card_eq_0_iff)
huffman@30001
    70
huffman@30001
    71
huffman@30001
    72
subsection {* Classes with at least 1 and 2  *}
huffman@30001
    73
huffman@30001
    74
text {* Class finite already captures "at least 1" *}
huffman@30001
    75
huffman@30001
    76
lemma zero_less_card_finite [simp]: "0 < CARD('a::finite)"
huffman@29997
    77
  unfolding neq0_conv [symmetric] by simp
huffman@29997
    78
huffman@30001
    79
lemma one_le_card_finite [simp]: "Suc 0 \<le> CARD('a::finite)"
huffman@30001
    80
  by (simp add: less_Suc_eq_le [symmetric])
huffman@30001
    81
huffman@30001
    82
text {* Class for cardinality "at least 2" *}
huffman@30001
    83
huffman@30001
    84
class card2 = finite + 
huffman@30001
    85
  assumes two_le_card: "2 \<le> CARD('a)"
huffman@30001
    86
huffman@30001
    87
lemma one_less_card: "Suc 0 < CARD('a::card2)"
huffman@30001
    88
  using two_le_card [where 'a='a] by simp
huffman@30001
    89
huffman@30001
    90
lemma one_less_int_card: "1 < int CARD('a::card2)"
huffman@30001
    91
  using one_less_card [where 'a='a] by simp
huffman@30001
    92
wenzelm@25378
    93
kleing@24332
    94
subsection {* Numeral Types *}
kleing@24332
    95
huffman@24406
    96
typedef (open) num0 = "UNIV :: nat set" ..
kleing@24332
    97
typedef (open) num1 = "UNIV :: unit set" ..
huffman@29997
    98
huffman@29997
    99
typedef (open) 'a bit0 = "{0 ..< 2 * int CARD('a::finite)}"
huffman@29997
   100
proof
huffman@29997
   101
  show "0 \<in> {0 ..< 2 * int CARD('a)}"
huffman@29997
   102
    by simp
huffman@29997
   103
qed
huffman@29997
   104
huffman@29997
   105
typedef (open) 'a bit1 = "{0 ..< 1 + 2 * int CARD('a::finite)}"
huffman@29997
   106
proof
huffman@29997
   107
  show "0 \<in> {0 ..< 1 + 2 * int CARD('a)}"
huffman@29997
   108
    by simp
huffman@29997
   109
qed
kleing@24332
   110
huffman@30001
   111
lemma card_num0 [simp]: "CARD (num0) = 0"
huffman@30001
   112
  unfolding type_definition.card [OF type_definition_num0]
huffman@30001
   113
  by simp
huffman@30001
   114
huffman@30001
   115
lemma card_num1 [simp]: "CARD(num1) = 1"
huffman@30001
   116
  unfolding type_definition.card [OF type_definition_num1]
huffman@30001
   117
  by (simp only: card_unit)
huffman@30001
   118
huffman@30001
   119
lemma card_bit0 [simp]: "CARD('a bit0) = 2 * CARD('a::finite)"
huffman@30001
   120
  unfolding type_definition.card [OF type_definition_bit0]
huffman@30001
   121
  by simp
huffman@30001
   122
huffman@30001
   123
lemma card_bit1 [simp]: "CARD('a bit1) = Suc (2 * CARD('a::finite))"
huffman@30001
   124
  unfolding type_definition.card [OF type_definition_bit1]
huffman@30001
   125
  by simp
huffman@30001
   126
kleing@24332
   127
instance num1 :: finite
kleing@24332
   128
proof
kleing@24332
   129
  show "finite (UNIV::num1 set)"
kleing@24332
   130
    unfolding type_definition.univ [OF type_definition_num1]
kleing@24332
   131
    using finite by (rule finite_imageI)
kleing@24332
   132
qed
kleing@24332
   133
huffman@30001
   134
instance bit0 :: (finite) card2
kleing@24332
   135
proof
kleing@24332
   136
  show "finite (UNIV::'a bit0 set)"
kleing@24332
   137
    unfolding type_definition.univ [OF type_definition_bit0]
huffman@29997
   138
    by simp
huffman@30001
   139
  show "2 \<le> CARD('a bit0)"
huffman@30001
   140
    by simp
kleing@24332
   141
qed
kleing@24332
   142
huffman@30001
   143
instance bit1 :: (finite) card2
kleing@24332
   144
proof
kleing@24332
   145
  show "finite (UNIV::'a bit1 set)"
kleing@24332
   146
    unfolding type_definition.univ [OF type_definition_bit1]
huffman@29997
   147
    by simp
huffman@30001
   148
  show "2 \<le> CARD('a bit1)"
huffman@30001
   149
    by simp
kleing@24332
   150
qed
kleing@24332
   151
wenzelm@25378
   152
huffman@29997
   153
subsection {* Locale for modular arithmetic subtypes *}
huffman@29997
   154
huffman@29997
   155
locale mod_type =
huffman@29997
   156
  fixes n :: int
haftmann@30960
   157
  and Rep :: "'a::{zero,one,plus,times,uminus,minus} \<Rightarrow> int"
haftmann@30960
   158
  and Abs :: "int \<Rightarrow> 'a::{zero,one,plus,times,uminus,minus}"
huffman@29997
   159
  assumes type: "type_definition Rep Abs {0..<n}"
huffman@29997
   160
  and size1: "1 < n"
huffman@29997
   161
  and zero_def: "0 = Abs 0"
huffman@29997
   162
  and one_def:  "1 = Abs 1"
huffman@29997
   163
  and add_def:  "x + y = Abs ((Rep x + Rep y) mod n)"
huffman@29997
   164
  and mult_def: "x * y = Abs ((Rep x * Rep y) mod n)"
huffman@29997
   165
  and diff_def: "x - y = Abs ((Rep x - Rep y) mod n)"
huffman@29997
   166
  and minus_def: "- x = Abs ((- Rep x) mod n)"
huffman@29997
   167
begin
huffman@29997
   168
huffman@29997
   169
lemma size0: "0 < n"
huffman@29997
   170
by (cut_tac size1, simp)
huffman@29997
   171
huffman@29997
   172
lemmas definitions =
haftmann@30960
   173
  zero_def one_def add_def mult_def minus_def diff_def
huffman@29997
   174
huffman@29997
   175
lemma Rep_less_n: "Rep x < n"
huffman@29997
   176
by (rule type_definition.Rep [OF type, simplified, THEN conjunct2])
huffman@29997
   177
huffman@29997
   178
lemma Rep_le_n: "Rep x \<le> n"
huffman@29997
   179
by (rule Rep_less_n [THEN order_less_imp_le])
huffman@29997
   180
huffman@29997
   181
lemma Rep_inject_sym: "x = y \<longleftrightarrow> Rep x = Rep y"
huffman@29997
   182
by (rule type_definition.Rep_inject [OF type, symmetric])
huffman@29997
   183
huffman@29997
   184
lemma Rep_inverse: "Abs (Rep x) = x"
huffman@29997
   185
by (rule type_definition.Rep_inverse [OF type])
huffman@29997
   186
huffman@29997
   187
lemma Abs_inverse: "m \<in> {0..<n} \<Longrightarrow> Rep (Abs m) = m"
huffman@29997
   188
by (rule type_definition.Abs_inverse [OF type])
huffman@29997
   189
huffman@29997
   190
lemma Rep_Abs_mod: "Rep (Abs (m mod n)) = m mod n"
haftmann@33361
   191
by (simp add: Abs_inverse pos_mod_conj [OF size0])
huffman@29997
   192
huffman@29997
   193
lemma Rep_Abs_0: "Rep (Abs 0) = 0"
huffman@29997
   194
by (simp add: Abs_inverse size0)
huffman@29997
   195
huffman@29997
   196
lemma Rep_0: "Rep 0 = 0"
huffman@29997
   197
by (simp add: zero_def Rep_Abs_0)
huffman@29997
   198
huffman@29997
   199
lemma Rep_Abs_1: "Rep (Abs 1) = 1"
huffman@29997
   200
by (simp add: Abs_inverse size1)
huffman@29997
   201
huffman@29997
   202
lemma Rep_1: "Rep 1 = 1"
huffman@29997
   203
by (simp add: one_def Rep_Abs_1)
huffman@29997
   204
huffman@29997
   205
lemma Rep_mod: "Rep x mod n = Rep x"
huffman@29997
   206
apply (rule_tac x=x in type_definition.Abs_cases [OF type])
huffman@29997
   207
apply (simp add: type_definition.Abs_inverse [OF type])
huffman@29997
   208
apply (simp add: mod_pos_pos_trivial)
huffman@29997
   209
done
huffman@29997
   210
huffman@29997
   211
lemmas Rep_simps =
huffman@29997
   212
  Rep_inject_sym Rep_inverse Rep_Abs_mod Rep_mod Rep_Abs_0 Rep_Abs_1
huffman@29997
   213
huffman@29997
   214
lemma comm_ring_1: "OFCLASS('a, comm_ring_1_class)"
huffman@29997
   215
apply (intro_classes, unfold definitions)
huffman@29997
   216
apply (simp_all add: Rep_simps zmod_simps ring_simps)
huffman@29997
   217
done
huffman@29997
   218
huffman@29997
   219
end
huffman@29997
   220
huffman@29997
   221
locale mod_ring = mod_type +
huffman@29997
   222
  constrains n :: int
haftmann@30960
   223
  and Rep :: "'a::{number_ring} \<Rightarrow> int"
haftmann@30960
   224
  and Abs :: "int \<Rightarrow> 'a::{number_ring}"
huffman@29997
   225
begin
huffman@29997
   226
huffman@29997
   227
lemma of_nat_eq: "of_nat k = Abs (int k mod n)"
huffman@29997
   228
apply (induct k)
huffman@29997
   229
apply (simp add: zero_def)
huffman@29997
   230
apply (simp add: Rep_simps add_def one_def zmod_simps add_ac)
huffman@29997
   231
done
huffman@29997
   232
huffman@29997
   233
lemma of_int_eq: "of_int z = Abs (z mod n)"
huffman@29997
   234
apply (cases z rule: int_diff_cases)
huffman@29997
   235
apply (simp add: Rep_simps of_nat_eq diff_def zmod_simps)
huffman@29997
   236
done
huffman@29997
   237
huffman@29997
   238
lemma Rep_number_of:
huffman@29997
   239
  "Rep (number_of w) = number_of w mod n"
huffman@29997
   240
by (simp add: number_of_eq of_int_eq Rep_Abs_mod)
huffman@29997
   241
huffman@29997
   242
lemma iszero_number_of:
huffman@29997
   243
  "iszero (number_of w::'a) \<longleftrightarrow> number_of w mod n = 0"
huffman@29997
   244
by (simp add: Rep_simps number_of_eq of_int_eq iszero_def zero_def)
huffman@29997
   245
huffman@29997
   246
lemma cases:
huffman@29997
   247
  assumes 1: "\<And>z. \<lbrakk>(x::'a) = of_int z; 0 \<le> z; z < n\<rbrakk> \<Longrightarrow> P"
huffman@29997
   248
  shows "P"
huffman@29997
   249
apply (cases x rule: type_definition.Abs_cases [OF type])
huffman@29997
   250
apply (rule_tac z="y" in 1)
huffman@29997
   251
apply (simp_all add: of_int_eq mod_pos_pos_trivial)
huffman@29997
   252
done
huffman@29997
   253
huffman@29997
   254
lemma induct:
huffman@29997
   255
  "(\<And>z. \<lbrakk>0 \<le> z; z < n\<rbrakk> \<Longrightarrow> P (of_int z)) \<Longrightarrow> P (x::'a)"
huffman@29997
   256
by (cases x rule: cases) simp
huffman@29997
   257
huffman@29997
   258
end
huffman@29997
   259
huffman@29997
   260
huffman@29997
   261
subsection {* Number ring instances *}
huffman@29997
   262
huffman@30032
   263
text {*
huffman@30032
   264
  Unfortunately a number ring instance is not possible for
huffman@30032
   265
  @{typ num1}, since 0 and 1 are not distinct.
huffman@30032
   266
*}
huffman@30032
   267
haftmann@30960
   268
instantiation num1 :: "{comm_ring,comm_monoid_mult,number}"
huffman@30032
   269
begin
huffman@30032
   270
huffman@30032
   271
lemma num1_eq_iff: "(x::num1) = (y::num1) \<longleftrightarrow> True"
huffman@30032
   272
  by (induct x, induct y) simp
huffman@30032
   273
huffman@30032
   274
instance proof
huffman@30032
   275
qed (simp_all add: num1_eq_iff)
huffman@30032
   276
huffman@30032
   277
end
huffman@30032
   278
huffman@29997
   279
instantiation
haftmann@30960
   280
  bit0 and bit1 :: (finite) "{zero,one,plus,times,uminus,minus}"
huffman@29997
   281
begin
huffman@29997
   282
huffman@29997
   283
definition Abs_bit0' :: "int \<Rightarrow> 'a bit0" where
huffman@29998
   284
  "Abs_bit0' x = Abs_bit0 (x mod int CARD('a bit0))"
huffman@29997
   285
huffman@29997
   286
definition Abs_bit1' :: "int \<Rightarrow> 'a bit1" where
huffman@29998
   287
  "Abs_bit1' x = Abs_bit1 (x mod int CARD('a bit1))"
huffman@29997
   288
huffman@29997
   289
definition "0 = Abs_bit0 0"
huffman@29997
   290
definition "1 = Abs_bit0 1"
huffman@29997
   291
definition "x + y = Abs_bit0' (Rep_bit0 x + Rep_bit0 y)"
huffman@29997
   292
definition "x * y = Abs_bit0' (Rep_bit0 x * Rep_bit0 y)"
huffman@29997
   293
definition "x - y = Abs_bit0' (Rep_bit0 x - Rep_bit0 y)"
huffman@29997
   294
definition "- x = Abs_bit0' (- Rep_bit0 x)"
huffman@29997
   295
huffman@29997
   296
definition "0 = Abs_bit1 0"
huffman@29997
   297
definition "1 = Abs_bit1 1"
huffman@29997
   298
definition "x + y = Abs_bit1' (Rep_bit1 x + Rep_bit1 y)"
huffman@29997
   299
definition "x * y = Abs_bit1' (Rep_bit1 x * Rep_bit1 y)"
huffman@29997
   300
definition "x - y = Abs_bit1' (Rep_bit1 x - Rep_bit1 y)"
huffman@29997
   301
definition "- x = Abs_bit1' (- Rep_bit1 x)"
huffman@29997
   302
huffman@29997
   303
instance ..
huffman@29997
   304
huffman@29997
   305
end
huffman@29997
   306
wenzelm@30729
   307
interpretation bit0:
huffman@29998
   308
  mod_type "int CARD('a::finite bit0)"
huffman@29997
   309
           "Rep_bit0 :: 'a::finite bit0 \<Rightarrow> int"
huffman@29997
   310
           "Abs_bit0 :: int \<Rightarrow> 'a::finite bit0"
huffman@29997
   311
apply (rule mod_type.intro)
huffman@29998
   312
apply (simp add: int_mult type_definition_bit0)
huffman@30001
   313
apply (rule one_less_int_card)
huffman@29997
   314
apply (rule zero_bit0_def)
huffman@29997
   315
apply (rule one_bit0_def)
huffman@29997
   316
apply (rule plus_bit0_def [unfolded Abs_bit0'_def])
huffman@29997
   317
apply (rule times_bit0_def [unfolded Abs_bit0'_def])
huffman@29997
   318
apply (rule minus_bit0_def [unfolded Abs_bit0'_def])
huffman@29997
   319
apply (rule uminus_bit0_def [unfolded Abs_bit0'_def])
huffman@29997
   320
done
huffman@29997
   321
wenzelm@30729
   322
interpretation bit1:
huffman@29998
   323
  mod_type "int CARD('a::finite bit1)"
huffman@29997
   324
           "Rep_bit1 :: 'a::finite bit1 \<Rightarrow> int"
huffman@29997
   325
           "Abs_bit1 :: int \<Rightarrow> 'a::finite bit1"
huffman@29997
   326
apply (rule mod_type.intro)
huffman@29998
   327
apply (simp add: int_mult type_definition_bit1)
huffman@30001
   328
apply (rule one_less_int_card)
huffman@29997
   329
apply (rule zero_bit1_def)
huffman@29997
   330
apply (rule one_bit1_def)
huffman@29997
   331
apply (rule plus_bit1_def [unfolded Abs_bit1'_def])
huffman@29997
   332
apply (rule times_bit1_def [unfolded Abs_bit1'_def])
huffman@29997
   333
apply (rule minus_bit1_def [unfolded Abs_bit1'_def])
huffman@29997
   334
apply (rule uminus_bit1_def [unfolded Abs_bit1'_def])
huffman@29997
   335
done
huffman@29997
   336
haftmann@31021
   337
instance bit0 :: (finite) comm_ring_1
haftmann@31021
   338
  by (rule bit0.comm_ring_1)+
huffman@29997
   339
haftmann@31021
   340
instance bit1 :: (finite) comm_ring_1
haftmann@31021
   341
  by (rule bit1.comm_ring_1)+
huffman@29997
   342
huffman@29997
   343
instantiation bit0 and bit1 :: (finite) number_ring
huffman@29997
   344
begin
huffman@29997
   345
huffman@29997
   346
definition "(number_of w :: _ bit0) = of_int w"
huffman@29997
   347
huffman@29997
   348
definition "(number_of w :: _ bit1) = of_int w"
huffman@29997
   349
huffman@29997
   350
instance proof
huffman@29997
   351
qed (rule number_of_bit0_def number_of_bit1_def)+
huffman@29997
   352
huffman@29997
   353
end
huffman@29997
   354
wenzelm@30729
   355
interpretation bit0:
huffman@29998
   356
  mod_ring "int CARD('a::finite bit0)"
huffman@29997
   357
           "Rep_bit0 :: 'a::finite bit0 \<Rightarrow> int"
huffman@29997
   358
           "Abs_bit0 :: int \<Rightarrow> 'a::finite bit0"
huffman@29997
   359
  ..
huffman@29997
   360
wenzelm@30729
   361
interpretation bit1:
huffman@29998
   362
  mod_ring "int CARD('a::finite bit1)"
huffman@29997
   363
           "Rep_bit1 :: 'a::finite bit1 \<Rightarrow> int"
huffman@29997
   364
           "Abs_bit1 :: int \<Rightarrow> 'a::finite bit1"
huffman@29997
   365
  ..
huffman@29997
   366
huffman@29997
   367
text {* Set up cases, induction, and arithmetic *}
huffman@29997
   368
huffman@29999
   369
lemmas bit0_cases [case_names of_int, cases type: bit0] = bit0.cases
huffman@29999
   370
lemmas bit1_cases [case_names of_int, cases type: bit1] = bit1.cases
huffman@29997
   371
huffman@29999
   372
lemmas bit0_induct [case_names of_int, induct type: bit0] = bit0.induct
huffman@29999
   373
lemmas bit1_induct [case_names of_int, induct type: bit1] = bit1.induct
huffman@29997
   374
huffman@29997
   375
lemmas bit0_iszero_number_of [simp] = bit0.iszero_number_of
huffman@29997
   376
lemmas bit1_iszero_number_of [simp] = bit1.iszero_number_of
huffman@29997
   377
huffman@29997
   378
kleing@24332
   379
subsection {* Syntax *}
kleing@24332
   380
kleing@24332
   381
syntax
kleing@24332
   382
  "_NumeralType" :: "num_const => type"  ("_")
kleing@24332
   383
  "_NumeralType0" :: type ("0")
kleing@24332
   384
  "_NumeralType1" :: type ("1")
kleing@24332
   385
kleing@24332
   386
translations
kleing@24332
   387
  "_NumeralType1" == (type) "num1"
huffman@24406
   388
  "_NumeralType0" == (type) "num0"
kleing@24332
   389
kleing@24332
   390
parse_translation {*
kleing@24332
   391
let
wenzelm@35115
   392
(* FIXME @{type_syntax} *)
kleing@24332
   393
val num1_const = Syntax.const "Numeral_Type.num1";
huffman@24406
   394
val num0_const = Syntax.const "Numeral_Type.num0";
kleing@24332
   395
val B0_const = Syntax.const "Numeral_Type.bit0";
kleing@24332
   396
val B1_const = Syntax.const "Numeral_Type.bit1";
kleing@24332
   397
kleing@24332
   398
fun mk_bintype n =
kleing@24332
   399
  let
kleing@24332
   400
    fun mk_bit n = if n = 0 then B0_const else B1_const;
kleing@24332
   401
    fun bin_of n =
kleing@24332
   402
      if n = 1 then num1_const
huffman@24406
   403
      else if n = 0 then num0_const
kleing@24332
   404
      else if n = ~1 then raise TERM ("negative type numeral", [])
kleing@24332
   405
      else
wenzelm@24630
   406
        let val (q, r) = Integer.div_mod n 2;
kleing@24332
   407
        in mk_bit r $ bin_of q end;
kleing@24332
   408
  in bin_of n end;
kleing@24332
   409
kleing@24332
   410
fun numeral_tr (*"_NumeralType"*) [Const (str, _)] =
wenzelm@33035
   411
      mk_bintype (the (Int.fromString str))
kleing@24332
   412
  | numeral_tr (*"_NumeralType"*) ts = raise TERM ("numeral_tr", ts);
kleing@24332
   413
wenzelm@35115
   414
in [(@{syntax_const "_NumeralType"}, numeral_tr)] end;
kleing@24332
   415
*}
kleing@24332
   416
kleing@24332
   417
print_translation {*
kleing@24332
   418
let
kleing@24332
   419
fun int_of [] = 0
wenzelm@24630
   420
  | int_of (b :: bs) = b + 2 * int_of bs;
kleing@24332
   421
wenzelm@35115
   422
(* FIXME @{type_syntax} *)
huffman@24406
   423
fun bin_of (Const ("num0", _)) = []
kleing@24332
   424
  | bin_of (Const ("num1", _)) = [1]
kleing@24332
   425
  | bin_of (Const ("bit0", _) $ bs) = 0 :: bin_of bs
kleing@24332
   426
  | bin_of (Const ("bit1", _) $ bs) = 1 :: bin_of bs
kleing@24332
   427
  | bin_of t = raise TERM("bin_of", [t]);
kleing@24332
   428
kleing@24332
   429
fun bit_tr' b [t] =
kleing@24332
   430
  let
kleing@24332
   431
    val rev_digs = b :: bin_of t handle TERM _ => raise Match
kleing@24332
   432
    val i = int_of rev_digs;
wenzelm@24630
   433
    val num = string_of_int (abs i);
kleing@24332
   434
  in
kleing@24332
   435
    Syntax.const "_NumeralType" $ Syntax.free num
kleing@24332
   436
  end
kleing@24332
   437
  | bit_tr' b _ = raise Match;
kleing@24332
   438
wenzelm@35115
   439
(* FIXME @{type_syntax} *)
kleing@24332
   440
in [("bit0", bit_tr' 0), ("bit1", bit_tr' 1)] end;
kleing@24332
   441
*}
kleing@24332
   442
kleing@24332
   443
subsection {* Examples *}
kleing@24332
   444
kleing@24332
   445
lemma "CARD(0) = 0" by simp
kleing@24332
   446
lemma "CARD(17) = 17" by simp
huffman@29997
   447
lemma "8 * 11 ^ 3 - 6 = (2::5)" by simp
huffman@28920
   448
kleing@24332
   449
end