src/HOL/Library/Numeral_Type.thy
author blanchet
Wed Mar 04 10:45:52 2009 +0100 (2009-03-04)
changeset 30240 5b25fee0362c
parent 29629 5111ce425e7a
child 30242 aea5d7fa7ef5
permissions -rw-r--r--
Merge.
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@27487
     8
imports Plain "~~/src/HOL/Presburger"
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
huffman@28920
    39
  fun card_univ_tr' show_sorts _ [Const (@{const_name UNIV}, Type(_,[T,_]))] =
huffman@24407
    40
    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
blanchet@30240
    45
lemma card_unit [simp]: "CARD(unit) = 1"
haftmann@26153
    46
  unfolding UNIV_unit by simp
kleing@24332
    47
blanchet@30240
    48
lemma card_bool [simp]: "CARD(bool) = 2"
haftmann@26153
    49
  unfolding UNIV_bool by simp
kleing@24332
    50
blanchet@30240
    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
blanchet@30240
    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
blanchet@30240
    57
lemma card_option [simp]: "CARD('a option) = Suc CARD('a::finite)"
haftmann@26153
    58
  unfolding insert_None_conv_UNIV [symmetric]
kleing@24332
    59
  apply (subgoal_tac "(None::'a option) \<notin> range Some")
blanchet@30240
    60
  apply (simp add: card_image)
kleing@24332
    61
  apply fast
kleing@24332
    62
  done
kleing@24332
    63
blanchet@30240
    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
blanchet@30240
    68
lemma card_nat [simp]: "CARD(nat) = 0"
blanchet@30240
    69
  by (simp add: infinite_UNIV_nat card_eq_0_iff)
blanchet@30240
    70
blanchet@30240
    71
blanchet@30240
    72
subsection {* Classes with at least 1 and 2  *}
blanchet@30240
    73
blanchet@30240
    74
text {* Class finite already captures "at least 1" *}
blanchet@30240
    75
blanchet@30240
    76
lemma zero_less_card_finite [simp]: "0 < CARD('a::finite)"
blanchet@30240
    77
  unfolding neq0_conv [symmetric] by simp
blanchet@30240
    78
blanchet@30240
    79
lemma one_le_card_finite [simp]: "Suc 0 \<le> CARD('a::finite)"
blanchet@30240
    80
  by (simp add: less_Suc_eq_le [symmetric])
blanchet@30240
    81
blanchet@30240
    82
text {* Class for cardinality "at least 2" *}
blanchet@30240
    83
blanchet@30240
    84
class card2 = finite + 
blanchet@30240
    85
  assumes two_le_card: "2 \<le> CARD('a)"
blanchet@30240
    86
blanchet@30240
    87
lemma one_less_card: "Suc 0 < CARD('a::card2)"
blanchet@30240
    88
  using two_le_card [where 'a='a] by simp
blanchet@30240
    89
blanchet@30240
    90
lemma one_less_int_card: "1 < int CARD('a::card2)"
blanchet@30240
    91
  using one_less_card [where 'a='a] by simp
blanchet@30240
    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" ..
blanchet@30240
    98
blanchet@30240
    99
typedef (open) 'a bit0 = "{0 ..< 2 * int CARD('a::finite)}"
blanchet@30240
   100
proof
blanchet@30240
   101
  show "0 \<in> {0 ..< 2 * int CARD('a)}"
blanchet@30240
   102
    by simp
blanchet@30240
   103
qed
blanchet@30240
   104
blanchet@30240
   105
typedef (open) 'a bit1 = "{0 ..< 1 + 2 * int CARD('a::finite)}"
blanchet@30240
   106
proof
blanchet@30240
   107
  show "0 \<in> {0 ..< 1 + 2 * int CARD('a)}"
blanchet@30240
   108
    by simp
blanchet@30240
   109
qed
blanchet@30240
   110
blanchet@30240
   111
lemma card_num0 [simp]: "CARD (num0) = 0"
blanchet@30240
   112
  unfolding type_definition.card [OF type_definition_num0]
blanchet@30240
   113
  by simp
blanchet@30240
   114
blanchet@30240
   115
lemma card_num1 [simp]: "CARD(num1) = 1"
blanchet@30240
   116
  unfolding type_definition.card [OF type_definition_num1]
blanchet@30240
   117
  by (simp only: card_unit)
blanchet@30240
   118
blanchet@30240
   119
lemma card_bit0 [simp]: "CARD('a bit0) = 2 * CARD('a::finite)"
blanchet@30240
   120
  unfolding type_definition.card [OF type_definition_bit0]
blanchet@30240
   121
  by simp
blanchet@30240
   122
blanchet@30240
   123
lemma card_bit1 [simp]: "CARD('a bit1) = Suc (2 * CARD('a::finite))"
blanchet@30240
   124
  unfolding type_definition.card [OF type_definition_bit1]
blanchet@30240
   125
  by simp
kleing@24332
   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
blanchet@30240
   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]
blanchet@30240
   138
    by simp
blanchet@30240
   139
  show "2 \<le> CARD('a bit0)"
blanchet@30240
   140
    by simp
kleing@24332
   141
qed
kleing@24332
   142
blanchet@30240
   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]
blanchet@30240
   147
    by simp
blanchet@30240
   148
  show "2 \<le> CARD('a bit1)"
blanchet@30240
   149
    by simp
kleing@24332
   150
qed
kleing@24332
   151
blanchet@30240
   152
blanchet@30240
   153
subsection {* Locale for modular arithmetic subtypes *}
blanchet@30240
   154
blanchet@30240
   155
locale mod_type =
blanchet@30240
   156
  fixes n :: int
blanchet@30240
   157
  and Rep :: "'a::{zero,one,plus,times,uminus,minus,power} \<Rightarrow> int"
blanchet@30240
   158
  and Abs :: "int \<Rightarrow> 'a::{zero,one,plus,times,uminus,minus,power}"
blanchet@30240
   159
  assumes type: "type_definition Rep Abs {0..<n}"
blanchet@30240
   160
  and size1: "1 < n"
blanchet@30240
   161
  and zero_def: "0 = Abs 0"
blanchet@30240
   162
  and one_def:  "1 = Abs 1"
blanchet@30240
   163
  and add_def:  "x + y = Abs ((Rep x + Rep y) mod n)"
blanchet@30240
   164
  and mult_def: "x * y = Abs ((Rep x * Rep y) mod n)"
blanchet@30240
   165
  and diff_def: "x - y = Abs ((Rep x - Rep y) mod n)"
blanchet@30240
   166
  and minus_def: "- x = Abs ((- Rep x) mod n)"
blanchet@30240
   167
  and power_def: "x ^ k = Abs (Rep x ^ k mod n)"
blanchet@30240
   168
begin
blanchet@30240
   169
blanchet@30240
   170
lemma size0: "0 < n"
blanchet@30240
   171
by (cut_tac size1, simp)
blanchet@30240
   172
blanchet@30240
   173
lemmas definitions =
blanchet@30240
   174
  zero_def one_def add_def mult_def minus_def diff_def power_def
blanchet@30240
   175
blanchet@30240
   176
lemma Rep_less_n: "Rep x < n"
blanchet@30240
   177
by (rule type_definition.Rep [OF type, simplified, THEN conjunct2])
blanchet@30240
   178
blanchet@30240
   179
lemma Rep_le_n: "Rep x \<le> n"
blanchet@30240
   180
by (rule Rep_less_n [THEN order_less_imp_le])
blanchet@30240
   181
blanchet@30240
   182
lemma Rep_inject_sym: "x = y \<longleftrightarrow> Rep x = Rep y"
blanchet@30240
   183
by (rule type_definition.Rep_inject [OF type, symmetric])
blanchet@30240
   184
blanchet@30240
   185
lemma Rep_inverse: "Abs (Rep x) = x"
blanchet@30240
   186
by (rule type_definition.Rep_inverse [OF type])
blanchet@30240
   187
blanchet@30240
   188
lemma Abs_inverse: "m \<in> {0..<n} \<Longrightarrow> Rep (Abs m) = m"
blanchet@30240
   189
by (rule type_definition.Abs_inverse [OF type])
blanchet@30240
   190
blanchet@30240
   191
lemma Rep_Abs_mod: "Rep (Abs (m mod n)) = m mod n"
blanchet@30240
   192
by (simp add: Abs_inverse IntDiv.pos_mod_conj [OF size0])
blanchet@30240
   193
blanchet@30240
   194
lemma Rep_Abs_0: "Rep (Abs 0) = 0"
blanchet@30240
   195
by (simp add: Abs_inverse size0)
blanchet@30240
   196
blanchet@30240
   197
lemma Rep_0: "Rep 0 = 0"
blanchet@30240
   198
by (simp add: zero_def Rep_Abs_0)
blanchet@30240
   199
blanchet@30240
   200
lemma Rep_Abs_1: "Rep (Abs 1) = 1"
blanchet@30240
   201
by (simp add: Abs_inverse size1)
blanchet@30240
   202
blanchet@30240
   203
lemma Rep_1: "Rep 1 = 1"
blanchet@30240
   204
by (simp add: one_def Rep_Abs_1)
kleing@24332
   205
blanchet@30240
   206
lemma Rep_mod: "Rep x mod n = Rep x"
blanchet@30240
   207
apply (rule_tac x=x in type_definition.Abs_cases [OF type])
blanchet@30240
   208
apply (simp add: type_definition.Abs_inverse [OF type])
blanchet@30240
   209
apply (simp add: mod_pos_pos_trivial)
blanchet@30240
   210
done
blanchet@30240
   211
blanchet@30240
   212
lemmas Rep_simps =
blanchet@30240
   213
  Rep_inject_sym Rep_inverse Rep_Abs_mod Rep_mod Rep_Abs_0 Rep_Abs_1
blanchet@30240
   214
blanchet@30240
   215
lemma comm_ring_1: "OFCLASS('a, comm_ring_1_class)"
blanchet@30240
   216
apply (intro_classes, unfold definitions)
blanchet@30240
   217
apply (simp_all add: Rep_simps zmod_simps ring_simps)
blanchet@30240
   218
done
blanchet@30240
   219
blanchet@30240
   220
lemma recpower: "OFCLASS('a, recpower_class)"
blanchet@30240
   221
apply (intro_classes, unfold definitions)
blanchet@30240
   222
apply (simp_all add: Rep_simps zmod_simps add_ac mult_assoc
blanchet@30240
   223
                     mod_pos_pos_trivial size1)
blanchet@30240
   224
done
blanchet@30240
   225
blanchet@30240
   226
end
blanchet@30240
   227
blanchet@30240
   228
locale mod_ring = mod_type +
blanchet@30240
   229
  constrains n :: int
blanchet@30240
   230
  and Rep :: "'a::{number_ring,power} \<Rightarrow> int"
blanchet@30240
   231
  and Abs :: "int \<Rightarrow> 'a::{number_ring,power}"
blanchet@30240
   232
begin
kleing@24332
   233
blanchet@30240
   234
lemma of_nat_eq: "of_nat k = Abs (int k mod n)"
blanchet@30240
   235
apply (induct k)
blanchet@30240
   236
apply (simp add: zero_def)
blanchet@30240
   237
apply (simp add: Rep_simps add_def one_def zmod_simps add_ac)
blanchet@30240
   238
done
blanchet@30240
   239
blanchet@30240
   240
lemma of_int_eq: "of_int z = Abs (z mod n)"
blanchet@30240
   241
apply (cases z rule: int_diff_cases)
blanchet@30240
   242
apply (simp add: Rep_simps of_nat_eq diff_def zmod_simps)
blanchet@30240
   243
done
blanchet@30240
   244
blanchet@30240
   245
lemma Rep_number_of:
blanchet@30240
   246
  "Rep (number_of w) = number_of w mod n"
blanchet@30240
   247
by (simp add: number_of_eq of_int_eq Rep_Abs_mod)
blanchet@30240
   248
blanchet@30240
   249
lemma iszero_number_of:
blanchet@30240
   250
  "iszero (number_of w::'a) \<longleftrightarrow> number_of w mod n = 0"
blanchet@30240
   251
by (simp add: Rep_simps number_of_eq of_int_eq iszero_def zero_def)
blanchet@30240
   252
blanchet@30240
   253
lemma cases:
blanchet@30240
   254
  assumes 1: "\<And>z. \<lbrakk>(x::'a) = of_int z; 0 \<le> z; z < n\<rbrakk> \<Longrightarrow> P"
blanchet@30240
   255
  shows "P"
blanchet@30240
   256
apply (cases x rule: type_definition.Abs_cases [OF type])
blanchet@30240
   257
apply (rule_tac z="y" in 1)
blanchet@30240
   258
apply (simp_all add: of_int_eq mod_pos_pos_trivial)
blanchet@30240
   259
done
blanchet@30240
   260
blanchet@30240
   261
lemma induct:
blanchet@30240
   262
  "(\<And>z. \<lbrakk>0 \<le> z; z < n\<rbrakk> \<Longrightarrow> P (of_int z)) \<Longrightarrow> P (x::'a)"
blanchet@30240
   263
by (cases x rule: cases) simp
blanchet@30240
   264
blanchet@30240
   265
end
blanchet@30240
   266
blanchet@30240
   267
blanchet@30240
   268
subsection {* Number ring instances *}
kleing@24332
   269
blanchet@30240
   270
text {*
blanchet@30240
   271
  Unfortunately a number ring instance is not possible for
blanchet@30240
   272
  @{typ num1}, since 0 and 1 are not distinct.
blanchet@30240
   273
*}
blanchet@30240
   274
blanchet@30240
   275
instantiation num1 :: "{comm_ring,comm_monoid_mult,number,recpower}"
blanchet@30240
   276
begin
blanchet@30240
   277
blanchet@30240
   278
lemma num1_eq_iff: "(x::num1) = (y::num1) \<longleftrightarrow> True"
blanchet@30240
   279
  by (induct x, induct y) simp
blanchet@30240
   280
blanchet@30240
   281
instance proof
blanchet@30240
   282
qed (simp_all add: num1_eq_iff)
blanchet@30240
   283
blanchet@30240
   284
end
blanchet@30240
   285
blanchet@30240
   286
instantiation
blanchet@30240
   287
  bit0 and bit1 :: (finite) "{zero,one,plus,times,uminus,minus,power}"
blanchet@30240
   288
begin
blanchet@30240
   289
blanchet@30240
   290
definition Abs_bit0' :: "int \<Rightarrow> 'a bit0" where
blanchet@30240
   291
  "Abs_bit0' x = Abs_bit0 (x mod int CARD('a bit0))"
blanchet@30240
   292
blanchet@30240
   293
definition Abs_bit1' :: "int \<Rightarrow> 'a bit1" where
blanchet@30240
   294
  "Abs_bit1' x = Abs_bit1 (x mod int CARD('a bit1))"
blanchet@30240
   295
blanchet@30240
   296
definition "0 = Abs_bit0 0"
blanchet@30240
   297
definition "1 = Abs_bit0 1"
blanchet@30240
   298
definition "x + y = Abs_bit0' (Rep_bit0 x + Rep_bit0 y)"
blanchet@30240
   299
definition "x * y = Abs_bit0' (Rep_bit0 x * Rep_bit0 y)"
blanchet@30240
   300
definition "x - y = Abs_bit0' (Rep_bit0 x - Rep_bit0 y)"
blanchet@30240
   301
definition "- x = Abs_bit0' (- Rep_bit0 x)"
blanchet@30240
   302
definition "x ^ k = Abs_bit0' (Rep_bit0 x ^ k)"
blanchet@30240
   303
blanchet@30240
   304
definition "0 = Abs_bit1 0"
blanchet@30240
   305
definition "1 = Abs_bit1 1"
blanchet@30240
   306
definition "x + y = Abs_bit1' (Rep_bit1 x + Rep_bit1 y)"
blanchet@30240
   307
definition "x * y = Abs_bit1' (Rep_bit1 x * Rep_bit1 y)"
blanchet@30240
   308
definition "x - y = Abs_bit1' (Rep_bit1 x - Rep_bit1 y)"
blanchet@30240
   309
definition "- x = Abs_bit1' (- Rep_bit1 x)"
blanchet@30240
   310
definition "x ^ k = Abs_bit1' (Rep_bit1 x ^ k)"
blanchet@30240
   311
blanchet@30240
   312
instance ..
blanchet@30240
   313
blanchet@30240
   314
end
kleing@24332
   315
blanchet@30240
   316
interpretation bit0!:
blanchet@30240
   317
  mod_type "int CARD('a::finite bit0)"
blanchet@30240
   318
           "Rep_bit0 :: 'a::finite bit0 \<Rightarrow> int"
blanchet@30240
   319
           "Abs_bit0 :: int \<Rightarrow> 'a::finite bit0"
blanchet@30240
   320
apply (rule mod_type.intro)
blanchet@30240
   321
apply (simp add: int_mult type_definition_bit0)
blanchet@30240
   322
apply (rule one_less_int_card)
blanchet@30240
   323
apply (rule zero_bit0_def)
blanchet@30240
   324
apply (rule one_bit0_def)
blanchet@30240
   325
apply (rule plus_bit0_def [unfolded Abs_bit0'_def])
blanchet@30240
   326
apply (rule times_bit0_def [unfolded Abs_bit0'_def])
blanchet@30240
   327
apply (rule minus_bit0_def [unfolded Abs_bit0'_def])
blanchet@30240
   328
apply (rule uminus_bit0_def [unfolded Abs_bit0'_def])
blanchet@30240
   329
apply (rule power_bit0_def [unfolded Abs_bit0'_def])
blanchet@30240
   330
done
blanchet@30240
   331
blanchet@30240
   332
interpretation bit1!:
blanchet@30240
   333
  mod_type "int CARD('a::finite bit1)"
blanchet@30240
   334
           "Rep_bit1 :: 'a::finite bit1 \<Rightarrow> int"
blanchet@30240
   335
           "Abs_bit1 :: int \<Rightarrow> 'a::finite bit1"
blanchet@30240
   336
apply (rule mod_type.intro)
blanchet@30240
   337
apply (simp add: int_mult type_definition_bit1)
blanchet@30240
   338
apply (rule one_less_int_card)
blanchet@30240
   339
apply (rule zero_bit1_def)
blanchet@30240
   340
apply (rule one_bit1_def)
blanchet@30240
   341
apply (rule plus_bit1_def [unfolded Abs_bit1'_def])
blanchet@30240
   342
apply (rule times_bit1_def [unfolded Abs_bit1'_def])
blanchet@30240
   343
apply (rule minus_bit1_def [unfolded Abs_bit1'_def])
blanchet@30240
   344
apply (rule uminus_bit1_def [unfolded Abs_bit1'_def])
blanchet@30240
   345
apply (rule power_bit1_def [unfolded Abs_bit1'_def])
blanchet@30240
   346
done
blanchet@30240
   347
blanchet@30240
   348
instance bit0 :: (finite) "{comm_ring_1,recpower}"
blanchet@30240
   349
  by (rule bit0.comm_ring_1 bit0.recpower)+
blanchet@30240
   350
blanchet@30240
   351
instance bit1 :: (finite) "{comm_ring_1,recpower}"
blanchet@30240
   352
  by (rule bit1.comm_ring_1 bit1.recpower)+
blanchet@30240
   353
blanchet@30240
   354
instantiation bit0 and bit1 :: (finite) number_ring
blanchet@30240
   355
begin
blanchet@30240
   356
blanchet@30240
   357
definition "(number_of w :: _ bit0) = of_int w"
blanchet@30240
   358
blanchet@30240
   359
definition "(number_of w :: _ bit1) = of_int w"
blanchet@30240
   360
blanchet@30240
   361
instance proof
blanchet@30240
   362
qed (rule number_of_bit0_def number_of_bit1_def)+
blanchet@30240
   363
blanchet@30240
   364
end
blanchet@30240
   365
blanchet@30240
   366
interpretation bit0!:
blanchet@30240
   367
  mod_ring "int CARD('a::finite bit0)"
blanchet@30240
   368
           "Rep_bit0 :: 'a::finite bit0 \<Rightarrow> int"
blanchet@30240
   369
           "Abs_bit0 :: int \<Rightarrow> 'a::finite bit0"
blanchet@30240
   370
  ..
blanchet@30240
   371
blanchet@30240
   372
interpretation bit1!:
blanchet@30240
   373
  mod_ring "int CARD('a::finite bit1)"
blanchet@30240
   374
           "Rep_bit1 :: 'a::finite bit1 \<Rightarrow> int"
blanchet@30240
   375
           "Abs_bit1 :: int \<Rightarrow> 'a::finite bit1"
blanchet@30240
   376
  ..
blanchet@30240
   377
blanchet@30240
   378
text {* Set up cases, induction, and arithmetic *}
blanchet@30240
   379
blanchet@30240
   380
lemmas bit0_cases [case_names of_int, cases type: bit0] = bit0.cases
blanchet@30240
   381
lemmas bit1_cases [case_names of_int, cases type: bit1] = bit1.cases
blanchet@30240
   382
blanchet@30240
   383
lemmas bit0_induct [case_names of_int, induct type: bit0] = bit0.induct
blanchet@30240
   384
lemmas bit1_induct [case_names of_int, induct type: bit1] = bit1.induct
blanchet@30240
   385
blanchet@30240
   386
lemmas bit0_iszero_number_of [simp] = bit0.iszero_number_of
blanchet@30240
   387
lemmas bit1_iszero_number_of [simp] = bit1.iszero_number_of
blanchet@30240
   388
blanchet@30240
   389
declare power_Suc [where ?'a="'a::finite bit0", standard, simp]
blanchet@30240
   390
declare power_Suc [where ?'a="'a::finite bit1", standard, simp]
kleing@24332
   391
wenzelm@25378
   392
kleing@24332
   393
subsection {* Syntax *}
kleing@24332
   394
kleing@24332
   395
syntax
kleing@24332
   396
  "_NumeralType" :: "num_const => type"  ("_")
kleing@24332
   397
  "_NumeralType0" :: type ("0")
kleing@24332
   398
  "_NumeralType1" :: type ("1")
kleing@24332
   399
kleing@24332
   400
translations
kleing@24332
   401
  "_NumeralType1" == (type) "num1"
huffman@24406
   402
  "_NumeralType0" == (type) "num0"
kleing@24332
   403
kleing@24332
   404
parse_translation {*
kleing@24332
   405
let
kleing@24332
   406
kleing@24332
   407
val num1_const = Syntax.const "Numeral_Type.num1";
huffman@24406
   408
val num0_const = Syntax.const "Numeral_Type.num0";
kleing@24332
   409
val B0_const = Syntax.const "Numeral_Type.bit0";
kleing@24332
   410
val B1_const = Syntax.const "Numeral_Type.bit1";
kleing@24332
   411
kleing@24332
   412
fun mk_bintype n =
kleing@24332
   413
  let
kleing@24332
   414
    fun mk_bit n = if n = 0 then B0_const else B1_const;
kleing@24332
   415
    fun bin_of n =
kleing@24332
   416
      if n = 1 then num1_const
huffman@24406
   417
      else if n = 0 then num0_const
kleing@24332
   418
      else if n = ~1 then raise TERM ("negative type numeral", [])
kleing@24332
   419
      else
wenzelm@24630
   420
        let val (q, r) = Integer.div_mod n 2;
kleing@24332
   421
        in mk_bit r $ bin_of q end;
kleing@24332
   422
  in bin_of n end;
kleing@24332
   423
kleing@24332
   424
fun numeral_tr (*"_NumeralType"*) [Const (str, _)] =
wenzelm@24630
   425
      mk_bintype (valOf (Int.fromString str))
kleing@24332
   426
  | numeral_tr (*"_NumeralType"*) ts = raise TERM ("numeral_tr", ts);
kleing@24332
   427
kleing@24332
   428
in [("_NumeralType", numeral_tr)] end;
kleing@24332
   429
*}
kleing@24332
   430
kleing@24332
   431
print_translation {*
kleing@24332
   432
let
kleing@24332
   433
fun int_of [] = 0
wenzelm@24630
   434
  | int_of (b :: bs) = b + 2 * int_of bs;
kleing@24332
   435
huffman@24406
   436
fun bin_of (Const ("num0", _)) = []
kleing@24332
   437
  | bin_of (Const ("num1", _)) = [1]
kleing@24332
   438
  | bin_of (Const ("bit0", _) $ bs) = 0 :: bin_of bs
kleing@24332
   439
  | bin_of (Const ("bit1", _) $ bs) = 1 :: bin_of bs
kleing@24332
   440
  | bin_of t = raise TERM("bin_of", [t]);
kleing@24332
   441
kleing@24332
   442
fun bit_tr' b [t] =
kleing@24332
   443
  let
kleing@24332
   444
    val rev_digs = b :: bin_of t handle TERM _ => raise Match
kleing@24332
   445
    val i = int_of rev_digs;
wenzelm@24630
   446
    val num = string_of_int (abs i);
kleing@24332
   447
  in
kleing@24332
   448
    Syntax.const "_NumeralType" $ Syntax.free num
kleing@24332
   449
  end
kleing@24332
   450
  | bit_tr' b _ = raise Match;
kleing@24332
   451
kleing@24332
   452
in [("bit0", bit_tr' 0), ("bit1", bit_tr' 1)] end;
kleing@24332
   453
*}
kleing@24332
   454
kleing@24332
   455
subsection {* Examples *}
kleing@24332
   456
kleing@24332
   457
lemma "CARD(0) = 0" by simp
kleing@24332
   458
lemma "CARD(17) = 17" by simp
blanchet@30240
   459
lemma "8 * 11 ^ 3 - 6 = (2::5)" by simp
huffman@28920
   460
kleing@24332
   461
end