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