src/HOL/Code_Numeral.thy
author haftmann
Sun Mar 10 15:16:45 2019 +0000 (6 weeks ago)
changeset 69906 55534affe445
parent 69593 3dda49e08b9d
child 69946 494934c30f38
permissions -rw-r--r--
migrated from Nums to Zarith as library for OCaml integer arithmetic
haftmann@51143
     1
(*  Title:      HOL/Code_Numeral.thy
haftmann@51143
     2
    Author:     Florian Haftmann, TU Muenchen
haftmann@51143
     3
*)
haftmann@24999
     4
wenzelm@60758
     5
section \<open>Numeric types for code generation onto target language numerals only\<close>
haftmann@24999
     6
haftmann@31205
     7
theory Code_Numeral
haftmann@66836
     8
imports Divides Lifting
haftmann@51143
     9
begin
haftmann@51143
    10
wenzelm@60758
    11
subsection \<open>Type of target language integers\<close>
haftmann@51143
    12
wenzelm@61076
    13
typedef integer = "UNIV :: int set"
haftmann@51143
    14
  morphisms int_of_integer integer_of_int ..
haftmann@51143
    15
haftmann@59487
    16
setup_lifting type_definition_integer
haftmann@51143
    17
haftmann@51143
    18
lemma integer_eq_iff:
haftmann@51143
    19
  "k = l \<longleftrightarrow> int_of_integer k = int_of_integer l"
haftmann@51143
    20
  by transfer rule
haftmann@51143
    21
haftmann@51143
    22
lemma integer_eqI:
haftmann@51143
    23
  "int_of_integer k = int_of_integer l \<Longrightarrow> k = l"
haftmann@51143
    24
  using integer_eq_iff [of k l] by simp
haftmann@51143
    25
haftmann@51143
    26
lemma int_of_integer_integer_of_int [simp]:
haftmann@51143
    27
  "int_of_integer (integer_of_int k) = k"
haftmann@51143
    28
  by transfer rule
haftmann@51143
    29
haftmann@51143
    30
lemma integer_of_int_int_of_integer [simp]:
haftmann@51143
    31
  "integer_of_int (int_of_integer k) = k"
haftmann@51143
    32
  by transfer rule
haftmann@51143
    33
haftmann@51143
    34
instantiation integer :: ring_1
haftmann@24999
    35
begin
haftmann@24999
    36
haftmann@51143
    37
lift_definition zero_integer :: integer
haftmann@51143
    38
  is "0 :: int"
haftmann@51143
    39
  .
haftmann@51143
    40
haftmann@51143
    41
declare zero_integer.rep_eq [simp]
haftmann@24999
    42
haftmann@51143
    43
lift_definition one_integer :: integer
haftmann@51143
    44
  is "1 :: int"
haftmann@51143
    45
  .
haftmann@51143
    46
haftmann@51143
    47
declare one_integer.rep_eq [simp]
haftmann@24999
    48
haftmann@51143
    49
lift_definition plus_integer :: "integer \<Rightarrow> integer \<Rightarrow> integer"
haftmann@51143
    50
  is "plus :: int \<Rightarrow> int \<Rightarrow> int"
haftmann@51143
    51
  .
haftmann@51143
    52
haftmann@51143
    53
declare plus_integer.rep_eq [simp]
haftmann@24999
    54
haftmann@51143
    55
lift_definition uminus_integer :: "integer \<Rightarrow> integer"
haftmann@51143
    56
  is "uminus :: int \<Rightarrow> int"
haftmann@51143
    57
  .
haftmann@51143
    58
haftmann@51143
    59
declare uminus_integer.rep_eq [simp]
haftmann@24999
    60
haftmann@51143
    61
lift_definition minus_integer :: "integer \<Rightarrow> integer \<Rightarrow> integer"
haftmann@51143
    62
  is "minus :: int \<Rightarrow> int \<Rightarrow> int"
haftmann@51143
    63
  .
haftmann@51143
    64
haftmann@51143
    65
declare minus_integer.rep_eq [simp]
haftmann@24999
    66
haftmann@51143
    67
lift_definition times_integer :: "integer \<Rightarrow> integer \<Rightarrow> integer"
haftmann@51143
    68
  is "times :: int \<Rightarrow> int \<Rightarrow> int"
haftmann@51143
    69
  .
haftmann@51143
    70
haftmann@51143
    71
declare times_integer.rep_eq [simp]
haftmann@28708
    72
haftmann@51143
    73
instance proof
haftmann@51143
    74
qed (transfer, simp add: algebra_simps)+
haftmann@51143
    75
haftmann@51143
    76
end
haftmann@51143
    77
haftmann@64241
    78
instance integer :: Rings.dvd ..
haftmann@64241
    79
haftmann@64241
    80
lemma [transfer_rule]:
haftmann@64241
    81
  "rel_fun pcr_integer (rel_fun pcr_integer HOL.iff) Rings.dvd Rings.dvd"
haftmann@64241
    82
  unfolding dvd_def by transfer_prover
haftmann@64241
    83
haftmann@51143
    84
lemma [transfer_rule]:
haftmann@68010
    85
  "rel_fun (=) pcr_integer (of_bool :: bool \<Rightarrow> int) (of_bool :: bool \<Rightarrow> integer)"
haftmann@68010
    86
  by (unfold of_bool_def [abs_def]) transfer_prover
haftmann@68010
    87
haftmann@68010
    88
lemma [transfer_rule]:
haftmann@68010
    89
  "rel_fun (=) pcr_integer (of_nat :: nat \<Rightarrow> int) (of_nat :: nat \<Rightarrow> integer)"
haftmann@64178
    90
  by (rule transfer_rule_of_nat) transfer_prover+
haftmann@51143
    91
haftmann@51143
    92
lemma [transfer_rule]:
haftmann@68010
    93
  "rel_fun (=) pcr_integer (\<lambda>k :: int. k :: int) (of_int :: int \<Rightarrow> integer)"
haftmann@51143
    94
proof -
blanchet@55945
    95
  have "rel_fun HOL.eq pcr_integer (of_int :: int \<Rightarrow> int) (of_int :: int \<Rightarrow> integer)"
haftmann@64178
    96
    by (rule transfer_rule_of_int) transfer_prover+
haftmann@51143
    97
  then show ?thesis by (simp add: id_def)
haftmann@24999
    98
qed
haftmann@24999
    99
haftmann@51143
   100
lemma [transfer_rule]:
blanchet@55945
   101
  "rel_fun HOL.eq pcr_integer (numeral :: num \<Rightarrow> int) (numeral :: num \<Rightarrow> integer)"
haftmann@64178
   102
  by (rule transfer_rule_numeral) transfer_prover+
haftmann@26140
   103
haftmann@51143
   104
lemma [transfer_rule]:
blanchet@55945
   105
  "rel_fun HOL.eq (rel_fun HOL.eq pcr_integer) (Num.sub :: _ \<Rightarrow> _ \<Rightarrow> int) (Num.sub :: _ \<Rightarrow> _ \<Rightarrow> integer)"
haftmann@51143
   106
  by (unfold Num.sub_def [abs_def]) transfer_prover
haftmann@51143
   107
haftmann@68010
   108
lemma [transfer_rule]:
haftmann@68010
   109
  "rel_fun pcr_integer (rel_fun (=) pcr_integer) (power :: _ \<Rightarrow> _ \<Rightarrow> int) (power :: _ \<Rightarrow> _ \<Rightarrow> integer)"
haftmann@68010
   110
  by (unfold power_def [abs_def]) transfer_prover
haftmann@68010
   111
haftmann@51143
   112
lemma int_of_integer_of_nat [simp]:
haftmann@51143
   113
  "int_of_integer (of_nat n) = of_nat n"
haftmann@51143
   114
  by transfer rule
haftmann@51143
   115
haftmann@51143
   116
lift_definition integer_of_nat :: "nat \<Rightarrow> integer"
haftmann@51143
   117
  is "of_nat :: nat \<Rightarrow> int"
haftmann@51143
   118
  .
haftmann@51143
   119
haftmann@51143
   120
lemma integer_of_nat_eq_of_nat [code]:
haftmann@51143
   121
  "integer_of_nat = of_nat"
haftmann@51143
   122
  by transfer rule
haftmann@51143
   123
haftmann@51143
   124
lemma int_of_integer_integer_of_nat [simp]:
haftmann@51143
   125
  "int_of_integer (integer_of_nat n) = of_nat n"
haftmann@51143
   126
  by transfer rule
haftmann@51143
   127
haftmann@51143
   128
lift_definition nat_of_integer :: "integer \<Rightarrow> nat"
haftmann@51143
   129
  is Int.nat
haftmann@51143
   130
  .
haftmann@26140
   131
haftmann@51143
   132
lemma nat_of_integer_of_nat [simp]:
haftmann@51143
   133
  "nat_of_integer (of_nat n) = n"
haftmann@51143
   134
  by transfer simp
haftmann@51143
   135
haftmann@51143
   136
lemma int_of_integer_of_int [simp]:
haftmann@51143
   137
  "int_of_integer (of_int k) = k"
haftmann@51143
   138
  by transfer simp
haftmann@51143
   139
haftmann@51143
   140
lemma nat_of_integer_integer_of_nat [simp]:
haftmann@51143
   141
  "nat_of_integer (integer_of_nat n) = n"
haftmann@51143
   142
  by transfer simp
haftmann@51143
   143
haftmann@51143
   144
lemma integer_of_int_eq_of_int [simp, code_abbrev]:
haftmann@51143
   145
  "integer_of_int = of_int"
haftmann@51143
   146
  by transfer (simp add: fun_eq_iff)
haftmann@26140
   147
haftmann@51143
   148
lemma of_int_integer_of [simp]:
haftmann@51143
   149
  "of_int (int_of_integer k) = (k :: integer)"
haftmann@51143
   150
  by transfer rule
haftmann@51143
   151
haftmann@51143
   152
lemma int_of_integer_numeral [simp]:
haftmann@51143
   153
  "int_of_integer (numeral k) = numeral k"
haftmann@51143
   154
  by transfer rule
haftmann@51143
   155
haftmann@51143
   156
lemma int_of_integer_sub [simp]:
haftmann@51143
   157
  "int_of_integer (Num.sub k l) = Num.sub k l"
haftmann@51143
   158
  by transfer rule
haftmann@51143
   159
haftmann@66190
   160
definition integer_of_num :: "num \<Rightarrow> integer"
haftmann@66190
   161
  where [simp]: "integer_of_num = numeral"
haftmann@61275
   162
haftmann@61275
   163
lemma integer_of_num [code]:
haftmann@66190
   164
  "integer_of_num Num.One = 1"
haftmann@66190
   165
  "integer_of_num (Num.Bit0 n) = (let k = integer_of_num n in k + k)"
haftmann@66190
   166
  "integer_of_num (Num.Bit1 n) = (let k = integer_of_num n in k + k + 1)"
haftmann@66190
   167
  by (simp_all only: integer_of_num_def numeral.simps Let_def)
haftmann@61275
   168
haftmann@61275
   169
lemma integer_of_num_triv:
haftmann@61275
   170
  "integer_of_num Num.One = 1"
haftmann@61275
   171
  "integer_of_num (Num.Bit0 Num.One) = 2"
haftmann@66190
   172
  by simp_all
haftmann@61275
   173
haftmann@64592
   174
instantiation integer :: "{linordered_idom, equal}"
haftmann@26140
   175
begin
haftmann@26140
   176
haftmann@51143
   177
lift_definition abs_integer :: "integer \<Rightarrow> integer"
haftmann@51143
   178
  is "abs :: int \<Rightarrow> int"
haftmann@51143
   179
  .
haftmann@51143
   180
haftmann@51143
   181
declare abs_integer.rep_eq [simp]
haftmann@26140
   182
haftmann@51143
   183
lift_definition sgn_integer :: "integer \<Rightarrow> integer"
haftmann@51143
   184
  is "sgn :: int \<Rightarrow> int"
haftmann@51143
   185
  .
haftmann@51143
   186
haftmann@51143
   187
declare sgn_integer.rep_eq [simp]
haftmann@51143
   188
haftmann@51143
   189
lift_definition less_eq_integer :: "integer \<Rightarrow> integer \<Rightarrow> bool"
haftmann@51143
   190
  is "less_eq :: int \<Rightarrow> int \<Rightarrow> bool"
haftmann@51143
   191
  .
haftmann@51143
   192
haftmann@64592
   193
haftmann@51143
   194
lift_definition less_integer :: "integer \<Rightarrow> integer \<Rightarrow> bool"
haftmann@51143
   195
  is "less :: int \<Rightarrow> int \<Rightarrow> bool"
haftmann@51143
   196
  .
haftmann@51143
   197
haftmann@51143
   198
lift_definition equal_integer :: "integer \<Rightarrow> integer \<Rightarrow> bool"
haftmann@51143
   199
  is "HOL.equal :: int \<Rightarrow> int \<Rightarrow> bool"
haftmann@51143
   200
  .
haftmann@51143
   201
haftmann@64592
   202
instance
haftmann@64592
   203
  by standard (transfer, simp add: algebra_simps equal less_le_not_le [symmetric] mult_strict_right_mono linear)+
haftmann@26140
   204
haftmann@26140
   205
end
haftmann@26140
   206
haftmann@51143
   207
lemma [transfer_rule]:
blanchet@55945
   208
  "rel_fun pcr_integer (rel_fun pcr_integer pcr_integer) (min :: _ \<Rightarrow> _ \<Rightarrow> int) (min :: _ \<Rightarrow> _ \<Rightarrow> integer)"
haftmann@51143
   209
  by (unfold min_def [abs_def]) transfer_prover
haftmann@51143
   210
haftmann@51143
   211
lemma [transfer_rule]:
blanchet@55945
   212
  "rel_fun pcr_integer (rel_fun pcr_integer pcr_integer) (max :: _ \<Rightarrow> _ \<Rightarrow> int) (max :: _ \<Rightarrow> _ \<Rightarrow> integer)"
haftmann@51143
   213
  by (unfold max_def [abs_def]) transfer_prover
haftmann@51143
   214
haftmann@51143
   215
lemma int_of_integer_min [simp]:
haftmann@51143
   216
  "int_of_integer (min k l) = min (int_of_integer k) (int_of_integer l)"
haftmann@51143
   217
  by transfer rule
haftmann@51143
   218
haftmann@51143
   219
lemma int_of_integer_max [simp]:
haftmann@51143
   220
  "int_of_integer (max k l) = max (int_of_integer k) (int_of_integer l)"
haftmann@51143
   221
  by transfer rule
haftmann@26140
   222
haftmann@51143
   223
lemma nat_of_integer_non_positive [simp]:
haftmann@51143
   224
  "k \<le> 0 \<Longrightarrow> nat_of_integer k = 0"
haftmann@51143
   225
  by transfer simp
haftmann@51143
   226
haftmann@51143
   227
lemma of_nat_of_integer [simp]:
haftmann@51143
   228
  "of_nat (nat_of_integer k) = max 0 k"
haftmann@51143
   229
  by transfer auto
haftmann@51143
   230
haftmann@66817
   231
instantiation integer :: unique_euclidean_ring
haftmann@64592
   232
begin
haftmann@64592
   233
haftmann@64592
   234
lift_definition divide_integer :: "integer \<Rightarrow> integer \<Rightarrow> integer"
haftmann@64592
   235
  is "divide :: int \<Rightarrow> int \<Rightarrow> int"
haftmann@64592
   236
  .
haftmann@64592
   237
haftmann@64592
   238
declare divide_integer.rep_eq [simp]
haftmann@64592
   239
haftmann@64592
   240
lift_definition modulo_integer :: "integer \<Rightarrow> integer \<Rightarrow> integer"
haftmann@64592
   241
  is "modulo :: int \<Rightarrow> int \<Rightarrow> int"
haftmann@64592
   242
  .
haftmann@64592
   243
haftmann@64592
   244
declare modulo_integer.rep_eq [simp]
haftmann@64592
   245
haftmann@66806
   246
lift_definition euclidean_size_integer :: "integer \<Rightarrow> nat"
haftmann@66806
   247
  is "euclidean_size :: int \<Rightarrow> nat"
haftmann@66806
   248
  .
haftmann@66806
   249
haftmann@66806
   250
declare euclidean_size_integer.rep_eq [simp]
haftmann@66806
   251
haftmann@66838
   252
lift_definition division_segment_integer :: "integer \<Rightarrow> integer"
haftmann@66838
   253
  is "division_segment :: int \<Rightarrow> int"
haftmann@66806
   254
  .
haftmann@66806
   255
haftmann@66838
   256
declare division_segment_integer.rep_eq [simp]
haftmann@66806
   257
haftmann@64592
   258
instance
haftmann@66806
   259
  by (standard; transfer)
haftmann@66838
   260
    (use mult_le_mono2 [of 1] in \<open>auto simp add: sgn_mult_abs abs_mult sgn_mult abs_mod_less sgn_mod nat_mult_distrib
haftmann@66838
   261
     division_segment_mult division_segment_mod intro: div_eqI\<close>)
haftmann@64592
   262
haftmann@64592
   263
end
haftmann@64592
   264
haftmann@66806
   265
lemma [code]:
haftmann@66806
   266
  "euclidean_size = nat_of_integer \<circ> abs"
haftmann@66806
   267
  by (simp add: fun_eq_iff nat_of_integer.rep_eq)
haftmann@66806
   268
haftmann@66806
   269
lemma [code]:
haftmann@66838
   270
  "division_segment (k :: integer) = (if k \<ge> 0 then 1 else - 1)"
haftmann@66838
   271
  by transfer (simp add: division_segment_int_def)
haftmann@66806
   272
haftmann@66815
   273
instance integer :: ring_parity
haftmann@66839
   274
  by (standard; transfer) (simp_all add: of_nat_div division_segment_int_def)
haftmann@66815
   275
haftmann@68010
   276
lemma [transfer_rule]:
haftmann@68010
   277
  "rel_fun (=) (rel_fun pcr_integer pcr_integer) (push_bit :: _ \<Rightarrow> _ \<Rightarrow> int) (push_bit :: _ \<Rightarrow> _ \<Rightarrow> integer)"
haftmann@68010
   278
  by (unfold push_bit_eq_mult [abs_def]) transfer_prover
haftmann@68010
   279
haftmann@68010
   280
lemma [transfer_rule]:
haftmann@68010
   281
  "rel_fun (=) (rel_fun pcr_integer pcr_integer) (take_bit :: _ \<Rightarrow> _ \<Rightarrow> int) (take_bit :: _ \<Rightarrow> _ \<Rightarrow> integer)"
haftmann@68010
   282
  by (unfold take_bit_eq_mod [abs_def]) transfer_prover
haftmann@68010
   283
haftmann@68010
   284
lemma [transfer_rule]:
haftmann@68010
   285
  "rel_fun (=) (rel_fun pcr_integer pcr_integer) (drop_bit :: _ \<Rightarrow> _ \<Rightarrow> int) (drop_bit :: _ \<Rightarrow> _ \<Rightarrow> integer)"
haftmann@68010
   286
  by (unfold drop_bit_eq_div [abs_def]) transfer_prover
haftmann@68010
   287
haftmann@66806
   288
instantiation integer :: unique_euclidean_semiring_numeral
haftmann@61275
   289
begin
haftmann@61275
   290
haftmann@61275
   291
definition divmod_integer :: "num \<Rightarrow> num \<Rightarrow> integer \<times> integer"
haftmann@61275
   292
where
haftmann@61275
   293
  divmod_integer'_def: "divmod_integer m n = (numeral m div numeral n, numeral m mod numeral n)"
haftmann@61275
   294
haftmann@61275
   295
definition divmod_step_integer :: "num \<Rightarrow> integer \<times> integer \<Rightarrow> integer \<times> integer"
haftmann@61275
   296
where
haftmann@61275
   297
  "divmod_step_integer l qr = (let (q, r) = qr
haftmann@61275
   298
    in if r \<ge> numeral l then (2 * q + 1, r - numeral l)
haftmann@61275
   299
    else (2 * q, r))"
haftmann@61275
   300
haftmann@61275
   301
instance proof
haftmann@61275
   302
  show "divmod m n = (numeral m div numeral n :: integer, numeral m mod numeral n)"
haftmann@61275
   303
    for m n by (fact divmod_integer'_def)
haftmann@61275
   304
  show "divmod_step l qr = (let (q, r) = qr
haftmann@61275
   305
    in if r \<ge> numeral l then (2 * q + 1, r - numeral l)
haftmann@61275
   306
    else (2 * q, r))" for l and qr :: "integer \<times> integer"
haftmann@61275
   307
    by (fact divmod_step_integer_def)
haftmann@61275
   308
qed (transfer,
haftmann@61275
   309
  fact le_add_diff_inverse2
haftmann@66806
   310
  unique_euclidean_semiring_numeral_class.div_less
haftmann@66806
   311
  unique_euclidean_semiring_numeral_class.mod_less
haftmann@66806
   312
  unique_euclidean_semiring_numeral_class.div_positive
haftmann@66806
   313
  unique_euclidean_semiring_numeral_class.mod_less_eq_dividend
haftmann@66806
   314
  unique_euclidean_semiring_numeral_class.pos_mod_bound
haftmann@66806
   315
  unique_euclidean_semiring_numeral_class.pos_mod_sign
haftmann@66806
   316
  unique_euclidean_semiring_numeral_class.mod_mult2_eq
haftmann@66806
   317
  unique_euclidean_semiring_numeral_class.div_mult2_eq
haftmann@66806
   318
  unique_euclidean_semiring_numeral_class.discrete)+
haftmann@61275
   319
haftmann@61275
   320
end
haftmann@61275
   321
haftmann@61275
   322
declare divmod_algorithm_code [where ?'a = integer,
haftmann@66190
   323
  folded integer_of_num_def, unfolded integer_of_num_triv, 
haftmann@61275
   324
  code]
haftmann@53069
   325
Andreas@55427
   326
lemma integer_of_nat_0: "integer_of_nat 0 = 0"
Andreas@55427
   327
by transfer simp
Andreas@55427
   328
Andreas@55427
   329
lemma integer_of_nat_1: "integer_of_nat 1 = 1"
Andreas@55427
   330
by transfer simp
Andreas@55427
   331
Andreas@55427
   332
lemma integer_of_nat_numeral:
Andreas@55427
   333
  "integer_of_nat (numeral n) = numeral n"
Andreas@55427
   334
by transfer simp
haftmann@26140
   335
haftmann@68010
   336
wenzelm@60758
   337
subsection \<open>Code theorems for target language integers\<close>
haftmann@51143
   338
wenzelm@60758
   339
text \<open>Constructors\<close>
haftmann@26140
   340
haftmann@51143
   341
definition Pos :: "num \<Rightarrow> integer"
haftmann@51143
   342
where
haftmann@61274
   343
  [simp, code_post]: "Pos = numeral"
haftmann@51143
   344
haftmann@51143
   345
lemma [transfer_rule]:
blanchet@55945
   346
  "rel_fun HOL.eq pcr_integer numeral Pos"
haftmann@51143
   347
  by simp transfer_prover
haftmann@30245
   348
haftmann@61274
   349
lemma Pos_fold [code_unfold]:
haftmann@61274
   350
  "numeral Num.One = Pos Num.One"
haftmann@61274
   351
  "numeral (Num.Bit0 k) = Pos (Num.Bit0 k)"
haftmann@61274
   352
  "numeral (Num.Bit1 k) = Pos (Num.Bit1 k)"
haftmann@61274
   353
  by simp_all
haftmann@61274
   354
haftmann@51143
   355
definition Neg :: "num \<Rightarrow> integer"
haftmann@51143
   356
where
haftmann@54489
   357
  [simp, code_abbrev]: "Neg n = - Pos n"
haftmann@51143
   358
haftmann@51143
   359
lemma [transfer_rule]:
blanchet@55945
   360
  "rel_fun HOL.eq pcr_integer (\<lambda>n. - numeral n) Neg"
haftmann@54489
   361
  by (simp add: Neg_def [abs_def]) transfer_prover
haftmann@51143
   362
haftmann@51143
   363
code_datatype "0::integer" Pos Neg
haftmann@51143
   364
haftmann@64994
   365
  
haftmann@64994
   366
text \<open>A further pair of constructors for generated computations\<close>
haftmann@64994
   367
haftmann@64994
   368
context
haftmann@64994
   369
begin  
haftmann@64994
   370
haftmann@64994
   371
qualified definition positive :: "num \<Rightarrow> integer"
haftmann@64994
   372
  where [simp]: "positive = numeral"
haftmann@64994
   373
haftmann@64994
   374
qualified definition negative :: "num \<Rightarrow> integer"
haftmann@64994
   375
  where [simp]: "negative = uminus \<circ> numeral"
haftmann@64994
   376
haftmann@64994
   377
lemma [code_computation_unfold]:
haftmann@64994
   378
  "numeral = positive"
haftmann@64994
   379
  "Pos = positive"
haftmann@64994
   380
  "Neg = negative"
haftmann@64994
   381
  by (simp_all add: fun_eq_iff)
haftmann@64994
   382
haftmann@64994
   383
end
haftmann@64994
   384
haftmann@51143
   385
wenzelm@60758
   386
text \<open>Auxiliary operations\<close>
haftmann@51143
   387
haftmann@51143
   388
lift_definition dup :: "integer \<Rightarrow> integer"
haftmann@51143
   389
  is "\<lambda>k::int. k + k"
haftmann@51143
   390
  .
haftmann@26140
   391
haftmann@51143
   392
lemma dup_code [code]:
haftmann@51143
   393
  "dup 0 = 0"
haftmann@51143
   394
  "dup (Pos n) = Pos (Num.Bit0 n)"
haftmann@51143
   395
  "dup (Neg n) = Neg (Num.Bit0 n)"
haftmann@54489
   396
  by (transfer, simp only: numeral_Bit0 minus_add_distrib)+
haftmann@51143
   397
haftmann@51143
   398
lift_definition sub :: "num \<Rightarrow> num \<Rightarrow> integer"
haftmann@51143
   399
  is "\<lambda>m n. numeral m - numeral n :: int"
haftmann@51143
   400
  .
haftmann@26140
   401
haftmann@51143
   402
lemma sub_code [code]:
haftmann@51143
   403
  "sub Num.One Num.One = 0"
haftmann@51143
   404
  "sub (Num.Bit0 m) Num.One = Pos (Num.BitM m)"
haftmann@51143
   405
  "sub (Num.Bit1 m) Num.One = Pos (Num.Bit0 m)"
haftmann@51143
   406
  "sub Num.One (Num.Bit0 n) = Neg (Num.BitM n)"
haftmann@51143
   407
  "sub Num.One (Num.Bit1 n) = Neg (Num.Bit0 n)"
haftmann@51143
   408
  "sub (Num.Bit0 m) (Num.Bit0 n) = dup (sub m n)"
haftmann@51143
   409
  "sub (Num.Bit1 m) (Num.Bit1 n) = dup (sub m n)"
haftmann@51143
   410
  "sub (Num.Bit1 m) (Num.Bit0 n) = dup (sub m n) + 1"
haftmann@51143
   411
  "sub (Num.Bit0 m) (Num.Bit1 n) = dup (sub m n) - 1"
haftmann@51143
   412
  by (transfer, simp add: dbl_def dbl_inc_def dbl_dec_def)+
haftmann@28351
   413
haftmann@24999
   414
wenzelm@60758
   415
text \<open>Implementations\<close>
haftmann@24999
   416
haftmann@51143
   417
lemma one_integer_code [code, code_unfold]:
haftmann@51143
   418
  "1 = Pos Num.One"
haftmann@51143
   419
  by simp
haftmann@24999
   420
haftmann@51143
   421
lemma plus_integer_code [code]:
haftmann@51143
   422
  "k + 0 = (k::integer)"
haftmann@51143
   423
  "0 + l = (l::integer)"
haftmann@51143
   424
  "Pos m + Pos n = Pos (m + n)"
haftmann@51143
   425
  "Pos m + Neg n = sub m n"
haftmann@51143
   426
  "Neg m + Pos n = sub n m"
haftmann@51143
   427
  "Neg m + Neg n = Neg (m + n)"
haftmann@51143
   428
  by (transfer, simp)+
haftmann@24999
   429
haftmann@51143
   430
lemma uminus_integer_code [code]:
haftmann@51143
   431
  "uminus 0 = (0::integer)"
haftmann@51143
   432
  "uminus (Pos m) = Neg m"
haftmann@51143
   433
  "uminus (Neg m) = Pos m"
haftmann@51143
   434
  by simp_all
haftmann@28708
   435
haftmann@51143
   436
lemma minus_integer_code [code]:
haftmann@51143
   437
  "k - 0 = (k::integer)"
haftmann@51143
   438
  "0 - l = uminus (l::integer)"
haftmann@51143
   439
  "Pos m - Pos n = sub m n"
haftmann@51143
   440
  "Pos m - Neg n = Pos (m + n)"
haftmann@51143
   441
  "Neg m - Pos n = Neg (m + n)"
haftmann@51143
   442
  "Neg m - Neg n = sub n m"
haftmann@51143
   443
  by (transfer, simp)+
haftmann@46028
   444
haftmann@51143
   445
lemma abs_integer_code [code]:
haftmann@51143
   446
  "\<bar>k\<bar> = (if (k::integer) < 0 then - k else k)"
haftmann@51143
   447
  by simp
huffman@47108
   448
haftmann@51143
   449
lemma sgn_integer_code [code]:
haftmann@51143
   450
  "sgn k = (if k = 0 then 0 else if (k::integer) < 0 then - 1 else 1)"
huffman@47108
   451
  by simp
haftmann@46028
   452
haftmann@51143
   453
lemma times_integer_code [code]:
haftmann@51143
   454
  "k * 0 = (0::integer)"
haftmann@51143
   455
  "0 * l = (0::integer)"
haftmann@51143
   456
  "Pos m * Pos n = Pos (m * n)"
haftmann@51143
   457
  "Pos m * Neg n = Neg (m * n)"
haftmann@51143
   458
  "Neg m * Pos n = Neg (m * n)"
haftmann@51143
   459
  "Neg m * Neg n = Pos (m * n)"
haftmann@51143
   460
  by simp_all
haftmann@51143
   461
haftmann@51143
   462
definition divmod_integer :: "integer \<Rightarrow> integer \<Rightarrow> integer \<times> integer"
haftmann@51143
   463
where
haftmann@51143
   464
  "divmod_integer k l = (k div l, k mod l)"
haftmann@51143
   465
haftmann@66801
   466
lemma fst_divmod_integer [simp]:
haftmann@51143
   467
  "fst (divmod_integer k l) = k div l"
haftmann@51143
   468
  by (simp add: divmod_integer_def)
haftmann@51143
   469
haftmann@66801
   470
lemma snd_divmod_integer [simp]:
haftmann@51143
   471
  "snd (divmod_integer k l) = k mod l"
haftmann@51143
   472
  by (simp add: divmod_integer_def)
haftmann@51143
   473
haftmann@51143
   474
definition divmod_abs :: "integer \<Rightarrow> integer \<Rightarrow> integer \<times> integer"
haftmann@51143
   475
where
haftmann@51143
   476
  "divmod_abs k l = (\<bar>k\<bar> div \<bar>l\<bar>, \<bar>k\<bar> mod \<bar>l\<bar>)"
haftmann@51143
   477
haftmann@51143
   478
lemma fst_divmod_abs [simp]:
haftmann@51143
   479
  "fst (divmod_abs k l) = \<bar>k\<bar> div \<bar>l\<bar>"
haftmann@51143
   480
  by (simp add: divmod_abs_def)
haftmann@51143
   481
haftmann@51143
   482
lemma snd_divmod_abs [simp]:
haftmann@51143
   483
  "snd (divmod_abs k l) = \<bar>k\<bar> mod \<bar>l\<bar>"
haftmann@51143
   484
  by (simp add: divmod_abs_def)
haftmann@28708
   485
haftmann@53069
   486
lemma divmod_abs_code [code]:
haftmann@53069
   487
  "divmod_abs (Pos k) (Pos l) = divmod k l"
haftmann@53069
   488
  "divmod_abs (Neg k) (Neg l) = divmod k l"
haftmann@53069
   489
  "divmod_abs (Neg k) (Pos l) = divmod k l"
haftmann@53069
   490
  "divmod_abs (Pos k) (Neg l) = divmod k l"
haftmann@51143
   491
  "divmod_abs j 0 = (0, \<bar>j\<bar>)"
haftmann@51143
   492
  "divmod_abs 0 j = (0, 0)"
haftmann@51143
   493
  by (simp_all add: prod_eq_iff)
haftmann@51143
   494
haftmann@51143
   495
lemma divmod_integer_code [code]:
haftmann@51143
   496
  "divmod_integer k l =
haftmann@51143
   497
    (if k = 0 then (0, 0) else if l = 0 then (0, k) else
haftmann@51143
   498
    (apsnd \<circ> times \<circ> sgn) l (if sgn k = sgn l
haftmann@51143
   499
      then divmod_abs k l
haftmann@51143
   500
      else (let (r, s) = divmod_abs k l in
haftmann@51143
   501
        if s = 0 then (- r, 0) else (- r - 1, \<bar>l\<bar> - s))))"
haftmann@51143
   502
proof -
haftmann@51143
   503
  have aux1: "\<And>k l::int. sgn k = sgn l \<longleftrightarrow> k = 0 \<and> l = 0 \<or> 0 < l \<and> 0 < k \<or> l < 0 \<and> k < 0"
haftmann@51143
   504
    by (auto simp add: sgn_if)
haftmann@51143
   505
  have aux2: "\<And>q::int. - int_of_integer k = int_of_integer l * q \<longleftrightarrow> int_of_integer k = int_of_integer l * - q" by auto
haftmann@51143
   506
  show ?thesis
blanchet@55414
   507
    by (simp add: prod_eq_iff integer_eq_iff case_prod_beta aux1)
haftmann@51143
   508
      (auto simp add: zdiv_zminus1_eq_if zmod_zminus1_eq_if div_minus_right mod_minus_right aux2)
haftmann@51143
   509
qed
haftmann@51143
   510
haftmann@51143
   511
lemma div_integer_code [code]:
haftmann@51143
   512
  "k div l = fst (divmod_integer k l)"
haftmann@28708
   513
  by simp
haftmann@28708
   514
haftmann@51143
   515
lemma mod_integer_code [code]:
haftmann@51143
   516
  "k mod l = snd (divmod_integer k l)"
haftmann@25767
   517
  by simp
haftmann@24999
   518
haftmann@68028
   519
definition bit_cut_integer :: "integer \<Rightarrow> integer \<times> bool"
haftmann@68028
   520
  where "bit_cut_integer k = (k div 2, odd k)"
haftmann@68028
   521
haftmann@68028
   522
lemma bit_cut_integer_code [code]:
haftmann@68028
   523
  "bit_cut_integer k = (if k = 0 then (0, False)
haftmann@68028
   524
     else let (r, s) = Code_Numeral.divmod_abs k 2
haftmann@68028
   525
       in (if k > 0 then r else - r - s, s = 1))"
haftmann@68028
   526
proof -
haftmann@68028
   527
  have "bit_cut_integer k = (let (r, s) = divmod_integer k 2 in (r, s = 1))"
haftmann@68028
   528
    by (simp add: divmod_integer_def bit_cut_integer_def odd_iff_mod_2_eq_one)
haftmann@68028
   529
  then show ?thesis
haftmann@68028
   530
    by (simp add: divmod_integer_code) (auto simp add: split_def)
haftmann@68028
   531
qed
haftmann@68028
   532
haftmann@51143
   533
lemma equal_integer_code [code]:
haftmann@51143
   534
  "HOL.equal 0 (0::integer) \<longleftrightarrow> True"
haftmann@51143
   535
  "HOL.equal 0 (Pos l) \<longleftrightarrow> False"
haftmann@51143
   536
  "HOL.equal 0 (Neg l) \<longleftrightarrow> False"
haftmann@51143
   537
  "HOL.equal (Pos k) 0 \<longleftrightarrow> False"
haftmann@51143
   538
  "HOL.equal (Pos k) (Pos l) \<longleftrightarrow> HOL.equal k l"
haftmann@51143
   539
  "HOL.equal (Pos k) (Neg l) \<longleftrightarrow> False"
haftmann@51143
   540
  "HOL.equal (Neg k) 0 \<longleftrightarrow> False"
haftmann@51143
   541
  "HOL.equal (Neg k) (Pos l) \<longleftrightarrow> False"
haftmann@51143
   542
  "HOL.equal (Neg k) (Neg l) \<longleftrightarrow> HOL.equal k l"
haftmann@51143
   543
  by (simp_all add: equal)
haftmann@51143
   544
haftmann@51143
   545
lemma equal_integer_refl [code nbe]:
haftmann@51143
   546
  "HOL.equal (k::integer) k \<longleftrightarrow> True"
haftmann@51143
   547
  by (fact equal_refl)
haftmann@31266
   548
haftmann@51143
   549
lemma less_eq_integer_code [code]:
haftmann@51143
   550
  "0 \<le> (0::integer) \<longleftrightarrow> True"
haftmann@51143
   551
  "0 \<le> Pos l \<longleftrightarrow> True"
haftmann@51143
   552
  "0 \<le> Neg l \<longleftrightarrow> False"
haftmann@51143
   553
  "Pos k \<le> 0 \<longleftrightarrow> False"
haftmann@51143
   554
  "Pos k \<le> Pos l \<longleftrightarrow> k \<le> l"
haftmann@51143
   555
  "Pos k \<le> Neg l \<longleftrightarrow> False"
haftmann@51143
   556
  "Neg k \<le> 0 \<longleftrightarrow> True"
haftmann@51143
   557
  "Neg k \<le> Pos l \<longleftrightarrow> True"
haftmann@51143
   558
  "Neg k \<le> Neg l \<longleftrightarrow> l \<le> k"
haftmann@51143
   559
  by simp_all
haftmann@51143
   560
haftmann@51143
   561
lemma less_integer_code [code]:
haftmann@51143
   562
  "0 < (0::integer) \<longleftrightarrow> False"
haftmann@51143
   563
  "0 < Pos l \<longleftrightarrow> True"
haftmann@51143
   564
  "0 < Neg l \<longleftrightarrow> False"
haftmann@51143
   565
  "Pos k < 0 \<longleftrightarrow> False"
haftmann@51143
   566
  "Pos k < Pos l \<longleftrightarrow> k < l"
haftmann@51143
   567
  "Pos k < Neg l \<longleftrightarrow> False"
haftmann@51143
   568
  "Neg k < 0 \<longleftrightarrow> True"
haftmann@51143
   569
  "Neg k < Pos l \<longleftrightarrow> True"
haftmann@51143
   570
  "Neg k < Neg l \<longleftrightarrow> l < k"
haftmann@51143
   571
  by simp_all
haftmann@26140
   572
haftmann@51143
   573
lift_definition num_of_integer :: "integer \<Rightarrow> num"
haftmann@51143
   574
  is "num_of_nat \<circ> nat"
haftmann@51143
   575
  .
haftmann@51143
   576
haftmann@51143
   577
lemma num_of_integer_code [code]:
haftmann@51143
   578
  "num_of_integer k = (if k \<le> 1 then Num.One
haftmann@51143
   579
     else let
haftmann@51143
   580
       (l, j) = divmod_integer k 2;
haftmann@51143
   581
       l' = num_of_integer l;
haftmann@51143
   582
       l'' = l' + l'
haftmann@51143
   583
     in if j = 0 then l'' else l'' + Num.One)"
haftmann@51143
   584
proof -
haftmann@51143
   585
  {
haftmann@51143
   586
    assume "int_of_integer k mod 2 = 1"
haftmann@51143
   587
    then have "nat (int_of_integer k mod 2) = nat 1" by simp
haftmann@51143
   588
    moreover assume *: "1 < int_of_integer k"
haftmann@51143
   589
    ultimately have **: "nat (int_of_integer k) mod 2 = 1" by (simp add: nat_mod_distrib)
haftmann@51143
   590
    have "num_of_nat (nat (int_of_integer k)) =
haftmann@51143
   591
      num_of_nat (2 * (nat (int_of_integer k) div 2) + nat (int_of_integer k) mod 2)"
haftmann@51143
   592
      by simp
haftmann@51143
   593
    then have "num_of_nat (nat (int_of_integer k)) =
haftmann@51143
   594
      num_of_nat (nat (int_of_integer k) div 2 + nat (int_of_integer k) div 2 + nat (int_of_integer k) mod 2)"
haftmann@51143
   595
      by (simp add: mult_2)
haftmann@51143
   596
    with ** have "num_of_nat (nat (int_of_integer k)) =
haftmann@51143
   597
      num_of_nat (nat (int_of_integer k) div 2 + nat (int_of_integer k) div 2 + 1)"
haftmann@51143
   598
      by simp
haftmann@51143
   599
  }
haftmann@51143
   600
  note aux = this
haftmann@51143
   601
  show ?thesis
blanchet@55414
   602
    by (auto simp add: num_of_integer_def nat_of_integer_def Let_def case_prod_beta
haftmann@51143
   603
      not_le integer_eq_iff less_eq_integer_def
haftmann@51143
   604
      nat_mult_distrib nat_div_distrib num_of_nat_One num_of_nat_plus_distrib
haftmann@51143
   605
       mult_2 [where 'a=nat] aux add_One)
haftmann@25918
   606
qed
haftmann@25918
   607
haftmann@51143
   608
lemma nat_of_integer_code [code]:
haftmann@51143
   609
  "nat_of_integer k = (if k \<le> 0 then 0
haftmann@51143
   610
     else let
haftmann@51143
   611
       (l, j) = divmod_integer k 2;
haftmann@51143
   612
       l' = nat_of_integer l;
haftmann@51143
   613
       l'' = l' + l'
haftmann@51143
   614
     in if j = 0 then l'' else l'' + 1)"
haftmann@33340
   615
proof -
haftmann@66886
   616
  obtain j where k: "k = integer_of_int j"
haftmann@51143
   617
  proof
haftmann@51143
   618
    show "k = integer_of_int (int_of_integer k)" by simp
haftmann@51143
   619
  qed
haftmann@66886
   620
  have *: "nat j mod 2 = nat_of_integer (of_int j mod 2)" if "j \<ge> 0"
haftmann@66886
   621
    using that by transfer (simp add: nat_mod_distrib)
haftmann@66886
   622
  from k show ?thesis
haftmann@66886
   623
    by (auto simp add: split_def Let_def nat_of_integer_def nat_div_distrib mult_2 [symmetric]
haftmann@66886
   624
      minus_mod_eq_mult_div [symmetric] *)
haftmann@33340
   625
qed
haftmann@28708
   626
haftmann@51143
   627
lemma int_of_integer_code [code]:
haftmann@51143
   628
  "int_of_integer k = (if k < 0 then - (int_of_integer (- k))
haftmann@51143
   629
     else if k = 0 then 0
haftmann@51143
   630
     else let
haftmann@51143
   631
       (l, j) = divmod_integer k 2;
haftmann@51143
   632
       l' = 2 * int_of_integer l
haftmann@51143
   633
     in if j = 0 then l' else l' + 1)"
haftmann@64246
   634
  by (auto simp add: split_def Let_def integer_eq_iff minus_mod_eq_mult_div [symmetric])
haftmann@28708
   635
haftmann@51143
   636
lemma integer_of_int_code [code]:
haftmann@51143
   637
  "integer_of_int k = (if k < 0 then - (integer_of_int (- k))
haftmann@51143
   638
     else if k = 0 then 0
haftmann@51143
   639
     else let
haftmann@60868
   640
       l = 2 * integer_of_int (k div 2);
haftmann@60868
   641
       j = k mod 2
haftmann@60868
   642
     in if j = 0 then l else l + 1)"
haftmann@64246
   643
  by (auto simp add: split_def Let_def integer_eq_iff minus_mod_eq_mult_div [symmetric])
haftmann@51143
   644
haftmann@51143
   645
hide_const (open) Pos Neg sub dup divmod_abs
huffman@46547
   646
haftmann@28708
   647
wenzelm@60758
   648
subsection \<open>Serializer setup for target language integers\<close>
haftmann@24999
   649
haftmann@51143
   650
code_reserved Eval int Integer abs
haftmann@25767
   651
haftmann@52435
   652
code_printing
haftmann@52435
   653
  type_constructor integer \<rightharpoonup>
haftmann@52435
   654
    (SML) "IntInf.int"
haftmann@69906
   655
    and (OCaml) "Z.t"
haftmann@52435
   656
    and (Haskell) "Integer"
haftmann@52435
   657
    and (Scala) "BigInt"
haftmann@52435
   658
    and (Eval) "int"
haftmann@52435
   659
| class_instance integer :: equal \<rightharpoonup>
haftmann@52435
   660
    (Haskell) -
haftmann@24999
   661
haftmann@52435
   662
code_printing
haftmann@52435
   663
  constant "0::integer" \<rightharpoonup>
haftmann@58400
   664
    (SML) "!(0/ :/ IntInf.int)"
haftmann@69906
   665
    and (OCaml) "Z.zero"
haftmann@58400
   666
    and (Haskell) "!(0/ ::/ Integer)"
haftmann@52435
   667
    and (Scala) "BigInt(0)"
huffman@47108
   668
wenzelm@60758
   669
setup \<open>
haftmann@58399
   670
  fold (fn target =>
wenzelm@69593
   671
    Numeral.add_code \<^const_name>\<open>Code_Numeral.Pos\<close> I Code_Printer.literal_numeral target
wenzelm@69593
   672
    #> Numeral.add_code \<^const_name>\<open>Code_Numeral.Neg\<close> (~) Code_Printer.literal_numeral target)
haftmann@58399
   673
    ["SML", "OCaml", "Haskell", "Scala"]
wenzelm@60758
   674
\<close>
haftmann@51143
   675
haftmann@52435
   676
code_printing
haftmann@52435
   677
  constant "plus :: integer \<Rightarrow> _ \<Rightarrow> _" \<rightharpoonup>
haftmann@52435
   678
    (SML) "IntInf.+ ((_), (_))"
haftmann@69906
   679
    and (OCaml) "Z.add"
haftmann@52435
   680
    and (Haskell) infixl 6 "+"
haftmann@52435
   681
    and (Scala) infixl 7 "+"
haftmann@52435
   682
    and (Eval) infixl 8 "+"
haftmann@52435
   683
| constant "uminus :: integer \<Rightarrow> _" \<rightharpoonup>
haftmann@52435
   684
    (SML) "IntInf.~"
haftmann@69906
   685
    and (OCaml) "Z.neg"
haftmann@52435
   686
    and (Haskell) "negate"
haftmann@52435
   687
    and (Scala) "!(- _)"
haftmann@52435
   688
    and (Eval) "~/ _"
haftmann@52435
   689
| constant "minus :: integer \<Rightarrow> _" \<rightharpoonup>
haftmann@52435
   690
    (SML) "IntInf.- ((_), (_))"
haftmann@69906
   691
    and (OCaml) "Z.sub"
haftmann@52435
   692
    and (Haskell) infixl 6 "-"
haftmann@52435
   693
    and (Scala) infixl 7 "-"
haftmann@52435
   694
    and (Eval) infixl 8 "-"
haftmann@52435
   695
| constant Code_Numeral.dup \<rightharpoonup>
haftmann@52435
   696
    (SML) "IntInf.*/ (2,/ (_))"
haftmann@69906
   697
    and (OCaml) "Z.shift'_left/ _/ 1"
haftmann@52435
   698
    and (Haskell) "!(2 * _)"
haftmann@52435
   699
    and (Scala) "!(2 * _)"
haftmann@52435
   700
    and (Eval) "!(2 * _)"
haftmann@52435
   701
| constant Code_Numeral.sub \<rightharpoonup>
haftmann@52435
   702
    (SML) "!(raise/ Fail/ \"sub\")"
haftmann@52435
   703
    and (OCaml) "failwith/ \"sub\""
haftmann@52435
   704
    and (Haskell) "error/ \"sub\""
haftmann@52435
   705
    and (Scala) "!sys.error(\"sub\")"
haftmann@52435
   706
| constant "times :: integer \<Rightarrow> _ \<Rightarrow> _" \<rightharpoonup>
haftmann@52435
   707
    (SML) "IntInf.* ((_), (_))"
haftmann@69906
   708
    and (OCaml) "Z.mul"
haftmann@52435
   709
    and (Haskell) infixl 7 "*"
haftmann@52435
   710
    and (Scala) infixl 8 "*"
haftmann@52435
   711
    and (Eval) infixl 9 "*"
haftmann@52435
   712
| constant Code_Numeral.divmod_abs \<rightharpoonup>
haftmann@52435
   713
    (SML) "IntInf.divMod/ (IntInf.abs _,/ IntInf.abs _)"
haftmann@69906
   714
    and (OCaml) "!(fun k l ->/ if Z.equal Z.zero l then/ (Z.zero, l) else/ Z.div'_rem/ (Z.abs k)/ (Z.abs l))"
haftmann@52435
   715
    and (Haskell) "divMod/ (abs _)/ (abs _)"
haftmann@52435
   716
    and (Scala) "!((k: BigInt) => (l: BigInt) =>/ if (l == 0)/ (BigInt(0), k) else/ (k.abs '/% l.abs))"
haftmann@52435
   717
    and (Eval) "Integer.div'_mod/ (abs _)/ (abs _)"
haftmann@52435
   718
| constant "HOL.equal :: integer \<Rightarrow> _ \<Rightarrow> bool" \<rightharpoonup>
haftmann@52435
   719
    (SML) "!((_ : IntInf.int) = _)"
haftmann@69906
   720
    and (OCaml) "Z.equal"
haftmann@52435
   721
    and (Haskell) infix 4 "=="
haftmann@52435
   722
    and (Scala) infixl 5 "=="
haftmann@52435
   723
    and (Eval) infixl 6 "="
haftmann@52435
   724
| constant "less_eq :: integer \<Rightarrow> _ \<Rightarrow> bool" \<rightharpoonup>
haftmann@52435
   725
    (SML) "IntInf.<= ((_), (_))"
haftmann@69906
   726
    and (OCaml) "Z.leq"
haftmann@52435
   727
    and (Haskell) infix 4 "<="
haftmann@52435
   728
    and (Scala) infixl 4 "<="
haftmann@52435
   729
    and (Eval) infixl 6 "<="
haftmann@52435
   730
| constant "less :: integer \<Rightarrow> _ \<Rightarrow> bool" \<rightharpoonup>
haftmann@52435
   731
    (SML) "IntInf.< ((_), (_))"
haftmann@69906
   732
    and (OCaml) "Z.lt"
haftmann@52435
   733
    and (Haskell) infix 4 "<"
haftmann@52435
   734
    and (Scala) infixl 4 "<"
haftmann@52435
   735
    and (Eval) infixl 6 "<"
Andreas@61857
   736
| constant "abs :: integer \<Rightarrow> _" \<rightharpoonup>
Andreas@61857
   737
    (SML) "IntInf.abs"
haftmann@69906
   738
    and (OCaml) "Z.abs"
Andreas@61857
   739
    and (Haskell) "Prelude.abs"
Andreas@61857
   740
    and (Scala) "_.abs"
Andreas@61857
   741
    and (Eval) "abs"
haftmann@51143
   742
haftmann@52435
   743
code_identifier
haftmann@52435
   744
  code_module Code_Numeral \<rightharpoonup> (SML) Arith and (OCaml) Arith and (Haskell) Arith
huffman@46547
   745
haftmann@51143
   746
wenzelm@60758
   747
subsection \<open>Type of target language naturals\<close>
haftmann@51143
   748
wenzelm@61076
   749
typedef natural = "UNIV :: nat set"
haftmann@51143
   750
  morphisms nat_of_natural natural_of_nat ..
haftmann@51143
   751
haftmann@59487
   752
setup_lifting type_definition_natural
haftmann@51143
   753
haftmann@51143
   754
lemma natural_eq_iff [termination_simp]:
haftmann@51143
   755
  "m = n \<longleftrightarrow> nat_of_natural m = nat_of_natural n"
haftmann@51143
   756
  by transfer rule
haftmann@51143
   757
haftmann@51143
   758
lemma natural_eqI:
haftmann@51143
   759
  "nat_of_natural m = nat_of_natural n \<Longrightarrow> m = n"
haftmann@51143
   760
  using natural_eq_iff [of m n] by simp
haftmann@51143
   761
haftmann@51143
   762
lemma nat_of_natural_of_nat_inverse [simp]:
haftmann@51143
   763
  "nat_of_natural (natural_of_nat n) = n"
haftmann@51143
   764
  by transfer rule
haftmann@51143
   765
haftmann@51143
   766
lemma natural_of_nat_of_natural_inverse [simp]:
haftmann@51143
   767
  "natural_of_nat (nat_of_natural n) = n"
haftmann@51143
   768
  by transfer rule
haftmann@51143
   769
haftmann@51143
   770
instantiation natural :: "{comm_monoid_diff, semiring_1}"
haftmann@51143
   771
begin
haftmann@51143
   772
haftmann@51143
   773
lift_definition zero_natural :: natural
haftmann@51143
   774
  is "0 :: nat"
haftmann@51143
   775
  .
haftmann@51143
   776
haftmann@51143
   777
declare zero_natural.rep_eq [simp]
haftmann@51143
   778
haftmann@51143
   779
lift_definition one_natural :: natural
haftmann@51143
   780
  is "1 :: nat"
haftmann@51143
   781
  .
haftmann@51143
   782
haftmann@51143
   783
declare one_natural.rep_eq [simp]
haftmann@51143
   784
haftmann@51143
   785
lift_definition plus_natural :: "natural \<Rightarrow> natural \<Rightarrow> natural"
haftmann@51143
   786
  is "plus :: nat \<Rightarrow> nat \<Rightarrow> nat"
haftmann@51143
   787
  .
haftmann@51143
   788
haftmann@51143
   789
declare plus_natural.rep_eq [simp]
haftmann@51143
   790
haftmann@51143
   791
lift_definition minus_natural :: "natural \<Rightarrow> natural \<Rightarrow> natural"
haftmann@51143
   792
  is "minus :: nat \<Rightarrow> nat \<Rightarrow> nat"
haftmann@51143
   793
  .
haftmann@51143
   794
haftmann@51143
   795
declare minus_natural.rep_eq [simp]
haftmann@51143
   796
haftmann@51143
   797
lift_definition times_natural :: "natural \<Rightarrow> natural \<Rightarrow> natural"
haftmann@51143
   798
  is "times :: nat \<Rightarrow> nat \<Rightarrow> nat"
haftmann@51143
   799
  .
haftmann@51143
   800
haftmann@51143
   801
declare times_natural.rep_eq [simp]
haftmann@51143
   802
haftmann@51143
   803
instance proof
haftmann@51143
   804
qed (transfer, simp add: algebra_simps)+
haftmann@51143
   805
haftmann@51143
   806
end
haftmann@51143
   807
haftmann@64241
   808
instance natural :: Rings.dvd ..
haftmann@64241
   809
haftmann@64241
   810
lemma [transfer_rule]:
haftmann@64241
   811
  "rel_fun pcr_natural (rel_fun pcr_natural HOL.iff) Rings.dvd Rings.dvd"
haftmann@64241
   812
  unfolding dvd_def by transfer_prover
haftmann@64241
   813
haftmann@51143
   814
lemma [transfer_rule]:
haftmann@68010
   815
  "rel_fun (=) pcr_natural (of_bool :: bool \<Rightarrow> nat) (of_bool :: bool \<Rightarrow> natural)"
haftmann@68010
   816
  by (unfold of_bool_def [abs_def]) transfer_prover
haftmann@68010
   817
haftmann@68010
   818
lemma [transfer_rule]:
blanchet@55945
   819
  "rel_fun HOL.eq pcr_natural (\<lambda>n::nat. n) (of_nat :: nat \<Rightarrow> natural)"
haftmann@51143
   820
proof -
blanchet@55945
   821
  have "rel_fun HOL.eq pcr_natural (of_nat :: nat \<Rightarrow> nat) (of_nat :: nat \<Rightarrow> natural)"
haftmann@51143
   822
    by (unfold of_nat_def [abs_def]) transfer_prover
haftmann@51143
   823
  then show ?thesis by (simp add: id_def)
haftmann@51143
   824
qed
haftmann@51143
   825
haftmann@51143
   826
lemma [transfer_rule]:
blanchet@55945
   827
  "rel_fun HOL.eq pcr_natural (numeral :: num \<Rightarrow> nat) (numeral :: num \<Rightarrow> natural)"
haftmann@51143
   828
proof -
blanchet@55945
   829
  have "rel_fun HOL.eq pcr_natural (numeral :: num \<Rightarrow> nat) (\<lambda>n. of_nat (numeral n))"
haftmann@51143
   830
    by transfer_prover
haftmann@51143
   831
  then show ?thesis by simp
haftmann@51143
   832
qed
haftmann@51143
   833
haftmann@68010
   834
lemma [transfer_rule]:
haftmann@68010
   835
  "rel_fun pcr_natural (rel_fun (=) pcr_natural) (power :: _ \<Rightarrow> _ \<Rightarrow> nat) (power :: _ \<Rightarrow> _ \<Rightarrow> natural)"
haftmann@68010
   836
  by (unfold power_def [abs_def]) transfer_prover
haftmann@68010
   837
haftmann@51143
   838
lemma nat_of_natural_of_nat [simp]:
haftmann@51143
   839
  "nat_of_natural (of_nat n) = n"
haftmann@51143
   840
  by transfer rule
haftmann@51143
   841
haftmann@51143
   842
lemma natural_of_nat_of_nat [simp, code_abbrev]:
haftmann@51143
   843
  "natural_of_nat = of_nat"
haftmann@51143
   844
  by transfer rule
haftmann@51143
   845
haftmann@51143
   846
lemma of_nat_of_natural [simp]:
haftmann@51143
   847
  "of_nat (nat_of_natural n) = n"
haftmann@51143
   848
  by transfer rule
haftmann@51143
   849
haftmann@51143
   850
lemma nat_of_natural_numeral [simp]:
haftmann@51143
   851
  "nat_of_natural (numeral k) = numeral k"
haftmann@51143
   852
  by transfer rule
haftmann@51143
   853
haftmann@64592
   854
instantiation natural :: "{linordered_semiring, equal}"
haftmann@51143
   855
begin
haftmann@51143
   856
haftmann@51143
   857
lift_definition less_eq_natural :: "natural \<Rightarrow> natural \<Rightarrow> bool"
haftmann@51143
   858
  is "less_eq :: nat \<Rightarrow> nat \<Rightarrow> bool"
haftmann@51143
   859
  .
haftmann@51143
   860
haftmann@51143
   861
declare less_eq_natural.rep_eq [termination_simp]
haftmann@51143
   862
haftmann@51143
   863
lift_definition less_natural :: "natural \<Rightarrow> natural \<Rightarrow> bool"
haftmann@51143
   864
  is "less :: nat \<Rightarrow> nat \<Rightarrow> bool"
haftmann@51143
   865
  .
haftmann@51143
   866
haftmann@51143
   867
declare less_natural.rep_eq [termination_simp]
haftmann@51143
   868
haftmann@51143
   869
lift_definition equal_natural :: "natural \<Rightarrow> natural \<Rightarrow> bool"
haftmann@51143
   870
  is "HOL.equal :: nat \<Rightarrow> nat \<Rightarrow> bool"
haftmann@51143
   871
  .
haftmann@51143
   872
haftmann@51143
   873
instance proof
haftmann@51143
   874
qed (transfer, simp add: algebra_simps equal less_le_not_le [symmetric] linear)+
haftmann@51143
   875
haftmann@24999
   876
end
haftmann@46664
   877
haftmann@51143
   878
lemma [transfer_rule]:
blanchet@55945
   879
  "rel_fun pcr_natural (rel_fun pcr_natural pcr_natural) (min :: _ \<Rightarrow> _ \<Rightarrow> nat) (min :: _ \<Rightarrow> _ \<Rightarrow> natural)"
haftmann@51143
   880
  by (unfold min_def [abs_def]) transfer_prover
haftmann@51143
   881
haftmann@51143
   882
lemma [transfer_rule]:
blanchet@55945
   883
  "rel_fun pcr_natural (rel_fun pcr_natural pcr_natural) (max :: _ \<Rightarrow> _ \<Rightarrow> nat) (max :: _ \<Rightarrow> _ \<Rightarrow> natural)"
haftmann@51143
   884
  by (unfold max_def [abs_def]) transfer_prover
haftmann@51143
   885
haftmann@51143
   886
lemma nat_of_natural_min [simp]:
haftmann@51143
   887
  "nat_of_natural (min k l) = min (nat_of_natural k) (nat_of_natural l)"
haftmann@51143
   888
  by transfer rule
haftmann@51143
   889
haftmann@51143
   890
lemma nat_of_natural_max [simp]:
haftmann@51143
   891
  "nat_of_natural (max k l) = max (nat_of_natural k) (nat_of_natural l)"
haftmann@51143
   892
  by transfer rule
haftmann@51143
   893
haftmann@66806
   894
instantiation natural :: unique_euclidean_semiring
haftmann@64592
   895
begin
haftmann@64592
   896
haftmann@64592
   897
lift_definition divide_natural :: "natural \<Rightarrow> natural \<Rightarrow> natural"
haftmann@64592
   898
  is "divide :: nat \<Rightarrow> nat \<Rightarrow> nat"
haftmann@64592
   899
  .
haftmann@64592
   900
haftmann@64592
   901
declare divide_natural.rep_eq [simp]
haftmann@64592
   902
haftmann@64592
   903
lift_definition modulo_natural :: "natural \<Rightarrow> natural \<Rightarrow> natural"
haftmann@64592
   904
  is "modulo :: nat \<Rightarrow> nat \<Rightarrow> nat"
haftmann@64592
   905
  .
haftmann@64592
   906
haftmann@64592
   907
declare modulo_natural.rep_eq [simp]
haftmann@64592
   908
haftmann@66806
   909
lift_definition euclidean_size_natural :: "natural \<Rightarrow> nat"
haftmann@66806
   910
  is "euclidean_size :: nat \<Rightarrow> nat"
haftmann@66806
   911
  .
haftmann@66806
   912
haftmann@66806
   913
declare euclidean_size_natural.rep_eq [simp]
haftmann@66806
   914
haftmann@66838
   915
lift_definition division_segment_natural :: "natural \<Rightarrow> natural"
haftmann@66838
   916
  is "division_segment :: nat \<Rightarrow> nat"
haftmann@66806
   917
  .
haftmann@66806
   918
haftmann@66838
   919
declare division_segment_natural.rep_eq [simp]
haftmann@66806
   920
haftmann@64592
   921
instance
haftmann@66806
   922
  by (standard; transfer)
haftmann@66806
   923
    (auto simp add: algebra_simps unit_factor_nat_def gr0_conv_Suc)
haftmann@64592
   924
haftmann@64592
   925
end
haftmann@64592
   926
haftmann@66806
   927
lemma [code]:
haftmann@66806
   928
  "euclidean_size = nat_of_natural"
haftmann@66806
   929
  by (simp add: fun_eq_iff)
haftmann@66806
   930
haftmann@66806
   931
lemma [code]:
haftmann@66838
   932
  "division_segment (n::natural) = 1"
haftmann@66838
   933
  by (simp add: natural_eq_iff)
haftmann@66806
   934
haftmann@67905
   935
instance natural :: linordered_semidom
haftmann@67905
   936
  by (standard; transfer) simp_all
haftmann@67905
   937
haftmann@66815
   938
instance natural :: semiring_parity
haftmann@66839
   939
  by (standard; transfer) simp_all
haftmann@66815
   940
haftmann@68010
   941
lemma [transfer_rule]:
haftmann@68010
   942
  "rel_fun (=) (rel_fun pcr_natural pcr_natural) (push_bit :: _ \<Rightarrow> _ \<Rightarrow> nat) (push_bit :: _ \<Rightarrow> _ \<Rightarrow> natural)"
haftmann@68010
   943
  by (unfold push_bit_eq_mult [abs_def]) transfer_prover
haftmann@68010
   944
haftmann@68010
   945
lemma [transfer_rule]:
haftmann@68010
   946
  "rel_fun (=) (rel_fun pcr_natural pcr_natural) (take_bit :: _ \<Rightarrow> _ \<Rightarrow> nat) (take_bit :: _ \<Rightarrow> _ \<Rightarrow> natural)"
haftmann@68010
   947
  by (unfold take_bit_eq_mod [abs_def]) transfer_prover
haftmann@68010
   948
haftmann@68010
   949
lemma [transfer_rule]:
haftmann@68010
   950
  "rel_fun (=) (rel_fun pcr_natural pcr_natural) (drop_bit :: _ \<Rightarrow> _ \<Rightarrow> nat) (drop_bit :: _ \<Rightarrow> _ \<Rightarrow> natural)"
haftmann@68010
   951
  by (unfold drop_bit_eq_div [abs_def]) transfer_prover
haftmann@68010
   952
haftmann@51143
   953
lift_definition natural_of_integer :: "integer \<Rightarrow> natural"
haftmann@51143
   954
  is "nat :: int \<Rightarrow> nat"
haftmann@51143
   955
  .
haftmann@51143
   956
haftmann@51143
   957
lift_definition integer_of_natural :: "natural \<Rightarrow> integer"
haftmann@51143
   958
  is "of_nat :: nat \<Rightarrow> int"
haftmann@51143
   959
  .
haftmann@51143
   960
haftmann@51143
   961
lemma natural_of_integer_of_natural [simp]:
haftmann@51143
   962
  "natural_of_integer (integer_of_natural n) = n"
haftmann@51143
   963
  by transfer simp
haftmann@51143
   964
haftmann@51143
   965
lemma integer_of_natural_of_integer [simp]:
haftmann@51143
   966
  "integer_of_natural (natural_of_integer k) = max 0 k"
haftmann@51143
   967
  by transfer auto
haftmann@51143
   968
haftmann@51143
   969
lemma int_of_integer_of_natural [simp]:
haftmann@51143
   970
  "int_of_integer (integer_of_natural n) = of_nat (nat_of_natural n)"
haftmann@51143
   971
  by transfer rule
haftmann@51143
   972
haftmann@51143
   973
lemma integer_of_natural_of_nat [simp]:
haftmann@51143
   974
  "integer_of_natural (of_nat n) = of_nat n"
haftmann@51143
   975
  by transfer rule
haftmann@51143
   976
haftmann@51143
   977
lemma [measure_function]:
haftmann@51143
   978
  "is_measure nat_of_natural"
haftmann@51143
   979
  by (rule is_measure_trivial)
haftmann@51143
   980
haftmann@51143
   981
wenzelm@60758
   982
subsection \<open>Inductive representation of target language naturals\<close>
haftmann@51143
   983
haftmann@51143
   984
lift_definition Suc :: "natural \<Rightarrow> natural"
haftmann@51143
   985
  is Nat.Suc
haftmann@51143
   986
  .
haftmann@51143
   987
haftmann@51143
   988
declare Suc.rep_eq [simp]
haftmann@51143
   989
blanchet@58306
   990
old_rep_datatype "0::natural" Suc
haftmann@51143
   991
  by (transfer, fact nat.induct nat.inject nat.distinct)+
haftmann@51143
   992
blanchet@55416
   993
lemma natural_cases [case_names nat, cases type: natural]:
haftmann@51143
   994
  fixes m :: natural
haftmann@51143
   995
  assumes "\<And>n. m = of_nat n \<Longrightarrow> P"
haftmann@51143
   996
  shows P
haftmann@51143
   997
  using assms by transfer blast
haftmann@51143
   998
blanchet@67332
   999
instantiation natural :: size
blanchet@67332
  1000
begin
blanchet@58379
  1001
blanchet@67332
  1002
definition size_nat where [simp, code]: "size_nat = nat_of_natural"
blanchet@67332
  1003
blanchet@67332
  1004
instance ..
blanchet@67332
  1005
blanchet@67332
  1006
end
blanchet@58379
  1007
haftmann@51143
  1008
lemma natural_decr [termination_simp]:
haftmann@51143
  1009
  "n \<noteq> 0 \<Longrightarrow> nat_of_natural n - Nat.Suc 0 < nat_of_natural n"
haftmann@51143
  1010
  by transfer simp
haftmann@51143
  1011
blanchet@58379
  1012
lemma natural_zero_minus_one: "(0::natural) - 1 = 0"
blanchet@58379
  1013
  by (rule zero_diff)
haftmann@51143
  1014
blanchet@58379
  1015
lemma Suc_natural_minus_one: "Suc n - 1 = n"
haftmann@51143
  1016
  by transfer simp
haftmann@51143
  1017
haftmann@51143
  1018
hide_const (open) Suc
haftmann@51143
  1019
haftmann@51143
  1020
wenzelm@60758
  1021
subsection \<open>Code refinement for target language naturals\<close>
haftmann@51143
  1022
haftmann@51143
  1023
lift_definition Nat :: "integer \<Rightarrow> natural"
haftmann@51143
  1024
  is nat
haftmann@51143
  1025
  .
haftmann@51143
  1026
haftmann@51143
  1027
lemma [code_post]:
haftmann@51143
  1028
  "Nat 0 = 0"
haftmann@51143
  1029
  "Nat 1 = 1"
haftmann@51143
  1030
  "Nat (numeral k) = numeral k"
haftmann@51143
  1031
  by (transfer, simp)+
haftmann@51143
  1032
haftmann@51143
  1033
lemma [code abstype]:
haftmann@51143
  1034
  "Nat (integer_of_natural n) = n"
haftmann@51143
  1035
  by transfer simp
haftmann@51143
  1036
haftmann@63174
  1037
lemma [code]:
haftmann@63174
  1038
  "natural_of_nat n = natural_of_integer (integer_of_nat n)"
haftmann@63174
  1039
  by transfer simp
haftmann@51143
  1040
haftmann@51143
  1041
lemma [code abstract]:
haftmann@51143
  1042
  "integer_of_natural (natural_of_integer k) = max 0 k"
haftmann@51143
  1043
  by simp
haftmann@51143
  1044
haftmann@51143
  1045
lemma [code_abbrev]:
haftmann@51143
  1046
  "natural_of_integer (Code_Numeral.Pos k) = numeral k"
haftmann@51143
  1047
  by transfer simp
haftmann@51143
  1048
haftmann@51143
  1049
lemma [code abstract]:
haftmann@51143
  1050
  "integer_of_natural 0 = 0"
haftmann@51143
  1051
  by transfer simp
haftmann@51143
  1052
haftmann@51143
  1053
lemma [code abstract]:
haftmann@51143
  1054
  "integer_of_natural 1 = 1"
haftmann@51143
  1055
  by transfer simp
haftmann@51143
  1056
haftmann@51143
  1057
lemma [code abstract]:
haftmann@51143
  1058
  "integer_of_natural (Code_Numeral.Suc n) = integer_of_natural n + 1"
haftmann@51143
  1059
  by transfer simp
haftmann@51143
  1060
haftmann@51143
  1061
lemma [code]:
haftmann@51143
  1062
  "nat_of_natural = nat_of_integer \<circ> integer_of_natural"
haftmann@51143
  1063
  by transfer (simp add: fun_eq_iff)
haftmann@51143
  1064
haftmann@51143
  1065
lemma [code, code_unfold]:
blanchet@55416
  1066
  "case_natural f g n = (if n = 0 then f else g (n - 1))"
haftmann@51143
  1067
  by (cases n rule: natural.exhaust) (simp_all, simp add: Suc_def)
haftmann@51143
  1068
blanchet@55642
  1069
declare natural.rec [code del]
haftmann@51143
  1070
haftmann@51143
  1071
lemma [code abstract]:
haftmann@51143
  1072
  "integer_of_natural (m + n) = integer_of_natural m + integer_of_natural n"
haftmann@51143
  1073
  by transfer simp
haftmann@51143
  1074
haftmann@51143
  1075
lemma [code abstract]:
haftmann@51143
  1076
  "integer_of_natural (m - n) = max 0 (integer_of_natural m - integer_of_natural n)"
haftmann@51143
  1077
  by transfer simp
haftmann@51143
  1078
haftmann@51143
  1079
lemma [code abstract]:
haftmann@51143
  1080
  "integer_of_natural (m * n) = integer_of_natural m * integer_of_natural n"
haftmann@64592
  1081
  by transfer simp
haftmann@64592
  1082
haftmann@51143
  1083
lemma [code abstract]:
haftmann@51143
  1084
  "integer_of_natural (m div n) = integer_of_natural m div integer_of_natural n"
haftmann@51143
  1085
  by transfer (simp add: zdiv_int)
haftmann@51143
  1086
haftmann@51143
  1087
lemma [code abstract]:
haftmann@51143
  1088
  "integer_of_natural (m mod n) = integer_of_natural m mod integer_of_natural n"
haftmann@51143
  1089
  by transfer (simp add: zmod_int)
haftmann@51143
  1090
haftmann@51143
  1091
lemma [code]:
haftmann@51143
  1092
  "HOL.equal m n \<longleftrightarrow> HOL.equal (integer_of_natural m) (integer_of_natural n)"
haftmann@51143
  1093
  by transfer (simp add: equal)
haftmann@51143
  1094
blanchet@58379
  1095
lemma [code nbe]: "HOL.equal n (n::natural) \<longleftrightarrow> True"
blanchet@58379
  1096
  by (rule equal_class.equal_refl)
haftmann@51143
  1097
blanchet@58379
  1098
lemma [code]: "m \<le> n \<longleftrightarrow> integer_of_natural m \<le> integer_of_natural n"
haftmann@51143
  1099
  by transfer simp
haftmann@51143
  1100
blanchet@58379
  1101
lemma [code]: "m < n \<longleftrightarrow> integer_of_natural m < integer_of_natural n"
haftmann@51143
  1102
  by transfer simp
haftmann@51143
  1103
haftmann@51143
  1104
hide_const (open) Nat
haftmann@51143
  1105
kuncar@55736
  1106
lifting_update integer.lifting
kuncar@55736
  1107
lifting_forget integer.lifting
kuncar@55736
  1108
kuncar@55736
  1109
lifting_update natural.lifting
kuncar@55736
  1110
lifting_forget natural.lifting
haftmann@51143
  1111
haftmann@51143
  1112
code_reflect Code_Numeral
haftmann@63174
  1113
  datatypes natural
haftmann@63174
  1114
  functions "Code_Numeral.Suc" "0 :: natural" "1 :: natural"
haftmann@63174
  1115
    "plus :: natural \<Rightarrow> _" "minus :: natural \<Rightarrow> _"
haftmann@63174
  1116
    "times :: natural \<Rightarrow> _" "divide :: natural \<Rightarrow> _"
haftmann@63950
  1117
    "modulo :: natural \<Rightarrow> _"
haftmann@63174
  1118
    integer_of_natural natural_of_integer
haftmann@51143
  1119
haftmann@51143
  1120
end