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