src/HOL/Code_Numeral.thy
author haftmann
Sun Oct 16 09:31:04 2016 +0200 (2016-10-16)
changeset 64241 430d74089d4d
parent 64178 12e6c3bbb488
child 64246 15d1ee6e847b
permissions -rw-r--r--
transfer rules for divides relation on integer and natural
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@51143
     8
imports Nat_Transfer 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]:
blanchet@55945
    85
  "rel_fun HOL.eq pcr_integer (of_nat :: nat \<Rightarrow> int) (of_nat :: nat \<Rightarrow> integer)"
haftmann@64178
    86
  by (rule transfer_rule_of_nat) transfer_prover+
haftmann@51143
    87
haftmann@51143
    88
lemma [transfer_rule]:
blanchet@55945
    89
  "rel_fun HOL.eq pcr_integer (\<lambda>k :: int. k :: int) (of_int :: int \<Rightarrow> integer)"
haftmann@51143
    90
proof -
blanchet@55945
    91
  have "rel_fun HOL.eq pcr_integer (of_int :: int \<Rightarrow> int) (of_int :: int \<Rightarrow> integer)"
haftmann@64178
    92
    by (rule transfer_rule_of_int) transfer_prover+
haftmann@51143
    93
  then show ?thesis by (simp add: id_def)
haftmann@24999
    94
qed
haftmann@24999
    95
haftmann@51143
    96
lemma [transfer_rule]:
blanchet@55945
    97
  "rel_fun HOL.eq pcr_integer (numeral :: num \<Rightarrow> int) (numeral :: num \<Rightarrow> integer)"
haftmann@64178
    98
  by (rule transfer_rule_numeral) transfer_prover+
haftmann@26140
    99
haftmann@51143
   100
lemma [transfer_rule]:
blanchet@55945
   101
  "rel_fun HOL.eq (rel_fun HOL.eq pcr_integer) (Num.sub :: _ \<Rightarrow> _ \<Rightarrow> int) (Num.sub :: _ \<Rightarrow> _ \<Rightarrow> integer)"
haftmann@51143
   102
  by (unfold Num.sub_def [abs_def]) transfer_prover
haftmann@51143
   103
haftmann@51143
   104
lemma int_of_integer_of_nat [simp]:
haftmann@51143
   105
  "int_of_integer (of_nat n) = of_nat n"
haftmann@51143
   106
  by transfer rule
haftmann@51143
   107
haftmann@51143
   108
lift_definition integer_of_nat :: "nat \<Rightarrow> integer"
haftmann@51143
   109
  is "of_nat :: nat \<Rightarrow> int"
haftmann@51143
   110
  .
haftmann@51143
   111
haftmann@51143
   112
lemma integer_of_nat_eq_of_nat [code]:
haftmann@51143
   113
  "integer_of_nat = of_nat"
haftmann@51143
   114
  by transfer rule
haftmann@51143
   115
haftmann@51143
   116
lemma int_of_integer_integer_of_nat [simp]:
haftmann@51143
   117
  "int_of_integer (integer_of_nat n) = of_nat n"
haftmann@51143
   118
  by transfer rule
haftmann@51143
   119
haftmann@51143
   120
lift_definition nat_of_integer :: "integer \<Rightarrow> nat"
haftmann@51143
   121
  is Int.nat
haftmann@51143
   122
  .
haftmann@26140
   123
haftmann@51143
   124
lemma nat_of_integer_of_nat [simp]:
haftmann@51143
   125
  "nat_of_integer (of_nat n) = n"
haftmann@51143
   126
  by transfer simp
haftmann@51143
   127
haftmann@51143
   128
lemma int_of_integer_of_int [simp]:
haftmann@51143
   129
  "int_of_integer (of_int k) = k"
haftmann@51143
   130
  by transfer simp
haftmann@51143
   131
haftmann@51143
   132
lemma nat_of_integer_integer_of_nat [simp]:
haftmann@51143
   133
  "nat_of_integer (integer_of_nat n) = n"
haftmann@51143
   134
  by transfer simp
haftmann@51143
   135
haftmann@51143
   136
lemma integer_of_int_eq_of_int [simp, code_abbrev]:
haftmann@51143
   137
  "integer_of_int = of_int"
haftmann@51143
   138
  by transfer (simp add: fun_eq_iff)
haftmann@26140
   139
haftmann@51143
   140
lemma of_int_integer_of [simp]:
haftmann@51143
   141
  "of_int (int_of_integer k) = (k :: integer)"
haftmann@51143
   142
  by transfer rule
haftmann@51143
   143
haftmann@51143
   144
lemma int_of_integer_numeral [simp]:
haftmann@51143
   145
  "int_of_integer (numeral k) = numeral k"
haftmann@51143
   146
  by transfer rule
haftmann@51143
   147
haftmann@51143
   148
lemma int_of_integer_sub [simp]:
haftmann@51143
   149
  "int_of_integer (Num.sub k l) = Num.sub k l"
haftmann@51143
   150
  by transfer rule
haftmann@51143
   151
haftmann@61275
   152
lift_definition integer_of_num :: "num \<Rightarrow> integer"
haftmann@61275
   153
  is "numeral :: num \<Rightarrow> int"
haftmann@61275
   154
  .
haftmann@61275
   155
haftmann@61275
   156
lemma integer_of_num [code]:
haftmann@61275
   157
  "integer_of_num num.One = 1"
haftmann@61275
   158
  "integer_of_num (num.Bit0 n) = (let k = integer_of_num n in k + k)"
haftmann@61275
   159
  "integer_of_num (num.Bit1 n) = (let k = integer_of_num n in k + k + 1)"
haftmann@61275
   160
  by (transfer, simp only: numeral.simps Let_def)+
haftmann@61275
   161
haftmann@61275
   162
lemma numeral_unfold_integer_of_num:
haftmann@61275
   163
  "numeral = integer_of_num"
haftmann@61275
   164
  by (simp add: integer_of_num_def map_fun_def fun_eq_iff)
haftmann@61275
   165
haftmann@61275
   166
lemma integer_of_num_triv:
haftmann@61275
   167
  "integer_of_num Num.One = 1"
haftmann@61275
   168
  "integer_of_num (Num.Bit0 Num.One) = 2"
haftmann@61275
   169
  by (transfer, simp)+
haftmann@61275
   170
haftmann@51143
   171
instantiation integer :: "{ring_div, equal, linordered_idom}"
haftmann@26140
   172
begin
haftmann@26140
   173
haftmann@60352
   174
lift_definition divide_integer :: "integer \<Rightarrow> integer \<Rightarrow> integer"
haftmann@60352
   175
  is "divide :: int \<Rightarrow> int \<Rightarrow> int"
haftmann@51143
   176
  .
haftmann@51143
   177
haftmann@60352
   178
declare divide_integer.rep_eq [simp]
haftmann@51143
   179
haftmann@63950
   180
lift_definition modulo_integer :: "integer \<Rightarrow> integer \<Rightarrow> integer"
haftmann@63950
   181
  is "modulo :: int \<Rightarrow> int \<Rightarrow> int"
haftmann@51143
   182
  .
haftmann@51143
   183
haftmann@63950
   184
declare modulo_integer.rep_eq [simp]
haftmann@51143
   185
haftmann@51143
   186
lift_definition abs_integer :: "integer \<Rightarrow> integer"
haftmann@51143
   187
  is "abs :: int \<Rightarrow> int"
haftmann@51143
   188
  .
haftmann@51143
   189
haftmann@51143
   190
declare abs_integer.rep_eq [simp]
haftmann@26140
   191
haftmann@51143
   192
lift_definition sgn_integer :: "integer \<Rightarrow> integer"
haftmann@51143
   193
  is "sgn :: int \<Rightarrow> int"
haftmann@51143
   194
  .
haftmann@51143
   195
haftmann@51143
   196
declare sgn_integer.rep_eq [simp]
haftmann@51143
   197
haftmann@51143
   198
lift_definition less_eq_integer :: "integer \<Rightarrow> integer \<Rightarrow> bool"
haftmann@51143
   199
  is "less_eq :: int \<Rightarrow> int \<Rightarrow> bool"
haftmann@51143
   200
  .
haftmann@51143
   201
haftmann@51143
   202
lift_definition less_integer :: "integer \<Rightarrow> integer \<Rightarrow> bool"
haftmann@51143
   203
  is "less :: int \<Rightarrow> int \<Rightarrow> bool"
haftmann@51143
   204
  .
haftmann@51143
   205
haftmann@51143
   206
lift_definition equal_integer :: "integer \<Rightarrow> integer \<Rightarrow> bool"
haftmann@51143
   207
  is "HOL.equal :: int \<Rightarrow> int \<Rightarrow> bool"
haftmann@51143
   208
  .
haftmann@51143
   209
haftmann@51143
   210
instance proof
haftmann@51143
   211
qed (transfer, simp add: algebra_simps equal less_le_not_le [symmetric] mult_strict_right_mono linear)+
haftmann@26140
   212
haftmann@26140
   213
end
haftmann@26140
   214
haftmann@51143
   215
lemma [transfer_rule]:
blanchet@55945
   216
  "rel_fun pcr_integer (rel_fun pcr_integer pcr_integer) (min :: _ \<Rightarrow> _ \<Rightarrow> int) (min :: _ \<Rightarrow> _ \<Rightarrow> integer)"
haftmann@51143
   217
  by (unfold min_def [abs_def]) transfer_prover
haftmann@51143
   218
haftmann@51143
   219
lemma [transfer_rule]:
blanchet@55945
   220
  "rel_fun pcr_integer (rel_fun pcr_integer pcr_integer) (max :: _ \<Rightarrow> _ \<Rightarrow> int) (max :: _ \<Rightarrow> _ \<Rightarrow> integer)"
haftmann@51143
   221
  by (unfold max_def [abs_def]) transfer_prover
haftmann@51143
   222
haftmann@51143
   223
lemma int_of_integer_min [simp]:
haftmann@51143
   224
  "int_of_integer (min k l) = min (int_of_integer k) (int_of_integer l)"
haftmann@51143
   225
  by transfer rule
haftmann@51143
   226
haftmann@51143
   227
lemma int_of_integer_max [simp]:
haftmann@51143
   228
  "int_of_integer (max k l) = max (int_of_integer k) (int_of_integer l)"
haftmann@51143
   229
  by transfer rule
haftmann@26140
   230
haftmann@51143
   231
lemma nat_of_integer_non_positive [simp]:
haftmann@51143
   232
  "k \<le> 0 \<Longrightarrow> nat_of_integer k = 0"
haftmann@51143
   233
  by transfer simp
haftmann@51143
   234
haftmann@51143
   235
lemma of_nat_of_integer [simp]:
haftmann@51143
   236
  "of_nat (nat_of_integer k) = max 0 k"
haftmann@51143
   237
  by transfer auto
haftmann@51143
   238
haftmann@61275
   239
instantiation integer :: semiring_numeral_div
haftmann@61275
   240
begin
haftmann@61275
   241
haftmann@61275
   242
definition divmod_integer :: "num \<Rightarrow> num \<Rightarrow> integer \<times> integer"
haftmann@61275
   243
where
haftmann@61275
   244
  divmod_integer'_def: "divmod_integer m n = (numeral m div numeral n, numeral m mod numeral n)"
haftmann@61275
   245
haftmann@61275
   246
definition divmod_step_integer :: "num \<Rightarrow> integer \<times> integer \<Rightarrow> integer \<times> integer"
haftmann@61275
   247
where
haftmann@61275
   248
  "divmod_step_integer l qr = (let (q, r) = qr
haftmann@61275
   249
    in if r \<ge> numeral l then (2 * q + 1, r - numeral l)
haftmann@61275
   250
    else (2 * q, r))"
haftmann@61275
   251
haftmann@61275
   252
instance proof
haftmann@61275
   253
  show "divmod m n = (numeral m div numeral n :: integer, numeral m mod numeral n)"
haftmann@61275
   254
    for m n by (fact divmod_integer'_def)
haftmann@61275
   255
  show "divmod_step l qr = (let (q, r) = qr
haftmann@61275
   256
    in if r \<ge> numeral l then (2 * q + 1, r - numeral l)
haftmann@61275
   257
    else (2 * q, r))" for l and qr :: "integer \<times> integer"
haftmann@61275
   258
    by (fact divmod_step_integer_def)
haftmann@61275
   259
qed (transfer,
haftmann@61275
   260
  fact le_add_diff_inverse2
haftmann@61275
   261
  semiring_numeral_div_class.div_less
haftmann@61275
   262
  semiring_numeral_div_class.mod_less
haftmann@61275
   263
  semiring_numeral_div_class.div_positive
haftmann@61275
   264
  semiring_numeral_div_class.mod_less_eq_dividend
haftmann@61275
   265
  semiring_numeral_div_class.pos_mod_bound
haftmann@61275
   266
  semiring_numeral_div_class.pos_mod_sign
haftmann@61275
   267
  semiring_numeral_div_class.mod_mult2_eq
haftmann@61275
   268
  semiring_numeral_div_class.div_mult2_eq
haftmann@61275
   269
  semiring_numeral_div_class.discrete)+
haftmann@61275
   270
haftmann@61275
   271
end
haftmann@61275
   272
haftmann@61275
   273
declare divmod_algorithm_code [where ?'a = integer,
haftmann@61275
   274
  unfolded numeral_unfold_integer_of_num, unfolded integer_of_num_triv, 
haftmann@61275
   275
  code]
haftmann@53069
   276
Andreas@55427
   277
lemma integer_of_nat_0: "integer_of_nat 0 = 0"
Andreas@55427
   278
by transfer simp
Andreas@55427
   279
Andreas@55427
   280
lemma integer_of_nat_1: "integer_of_nat 1 = 1"
Andreas@55427
   281
by transfer simp
Andreas@55427
   282
Andreas@55427
   283
lemma integer_of_nat_numeral:
Andreas@55427
   284
  "integer_of_nat (numeral n) = numeral n"
Andreas@55427
   285
by transfer simp
haftmann@26140
   286
wenzelm@60758
   287
subsection \<open>Code theorems for target language integers\<close>
haftmann@51143
   288
wenzelm@60758
   289
text \<open>Constructors\<close>
haftmann@26140
   290
haftmann@51143
   291
definition Pos :: "num \<Rightarrow> integer"
haftmann@51143
   292
where
haftmann@61274
   293
  [simp, code_post]: "Pos = numeral"
haftmann@51143
   294
haftmann@51143
   295
lemma [transfer_rule]:
blanchet@55945
   296
  "rel_fun HOL.eq pcr_integer numeral Pos"
haftmann@51143
   297
  by simp transfer_prover
haftmann@30245
   298
haftmann@61274
   299
lemma Pos_fold [code_unfold]:
haftmann@61274
   300
  "numeral Num.One = Pos Num.One"
haftmann@61274
   301
  "numeral (Num.Bit0 k) = Pos (Num.Bit0 k)"
haftmann@61274
   302
  "numeral (Num.Bit1 k) = Pos (Num.Bit1 k)"
haftmann@61274
   303
  by simp_all
haftmann@61274
   304
haftmann@51143
   305
definition Neg :: "num \<Rightarrow> integer"
haftmann@51143
   306
where
haftmann@54489
   307
  [simp, code_abbrev]: "Neg n = - Pos n"
haftmann@51143
   308
haftmann@51143
   309
lemma [transfer_rule]:
blanchet@55945
   310
  "rel_fun HOL.eq pcr_integer (\<lambda>n. - numeral n) Neg"
haftmann@54489
   311
  by (simp add: Neg_def [abs_def]) transfer_prover
haftmann@51143
   312
haftmann@51143
   313
code_datatype "0::integer" Pos Neg
haftmann@51143
   314
haftmann@51143
   315
wenzelm@60758
   316
text \<open>Auxiliary operations\<close>
haftmann@51143
   317
haftmann@51143
   318
lift_definition dup :: "integer \<Rightarrow> integer"
haftmann@51143
   319
  is "\<lambda>k::int. k + k"
haftmann@51143
   320
  .
haftmann@26140
   321
haftmann@51143
   322
lemma dup_code [code]:
haftmann@51143
   323
  "dup 0 = 0"
haftmann@51143
   324
  "dup (Pos n) = Pos (Num.Bit0 n)"
haftmann@51143
   325
  "dup (Neg n) = Neg (Num.Bit0 n)"
haftmann@54489
   326
  by (transfer, simp only: numeral_Bit0 minus_add_distrib)+
haftmann@51143
   327
haftmann@51143
   328
lift_definition sub :: "num \<Rightarrow> num \<Rightarrow> integer"
haftmann@51143
   329
  is "\<lambda>m n. numeral m - numeral n :: int"
haftmann@51143
   330
  .
haftmann@26140
   331
haftmann@51143
   332
lemma sub_code [code]:
haftmann@51143
   333
  "sub Num.One Num.One = 0"
haftmann@51143
   334
  "sub (Num.Bit0 m) Num.One = Pos (Num.BitM m)"
haftmann@51143
   335
  "sub (Num.Bit1 m) Num.One = Pos (Num.Bit0 m)"
haftmann@51143
   336
  "sub Num.One (Num.Bit0 n) = Neg (Num.BitM n)"
haftmann@51143
   337
  "sub Num.One (Num.Bit1 n) = Neg (Num.Bit0 n)"
haftmann@51143
   338
  "sub (Num.Bit0 m) (Num.Bit0 n) = dup (sub m n)"
haftmann@51143
   339
  "sub (Num.Bit1 m) (Num.Bit1 n) = dup (sub m n)"
haftmann@51143
   340
  "sub (Num.Bit1 m) (Num.Bit0 n) = dup (sub m n) + 1"
haftmann@51143
   341
  "sub (Num.Bit0 m) (Num.Bit1 n) = dup (sub m n) - 1"
haftmann@51143
   342
  by (transfer, simp add: dbl_def dbl_inc_def dbl_dec_def)+
haftmann@28351
   343
haftmann@24999
   344
wenzelm@60758
   345
text \<open>Implementations\<close>
haftmann@24999
   346
haftmann@51143
   347
lemma one_integer_code [code, code_unfold]:
haftmann@51143
   348
  "1 = Pos Num.One"
haftmann@51143
   349
  by simp
haftmann@24999
   350
haftmann@51143
   351
lemma plus_integer_code [code]:
haftmann@51143
   352
  "k + 0 = (k::integer)"
haftmann@51143
   353
  "0 + l = (l::integer)"
haftmann@51143
   354
  "Pos m + Pos n = Pos (m + n)"
haftmann@51143
   355
  "Pos m + Neg n = sub m n"
haftmann@51143
   356
  "Neg m + Pos n = sub n m"
haftmann@51143
   357
  "Neg m + Neg n = Neg (m + n)"
haftmann@51143
   358
  by (transfer, simp)+
haftmann@24999
   359
haftmann@51143
   360
lemma uminus_integer_code [code]:
haftmann@51143
   361
  "uminus 0 = (0::integer)"
haftmann@51143
   362
  "uminus (Pos m) = Neg m"
haftmann@51143
   363
  "uminus (Neg m) = Pos m"
haftmann@51143
   364
  by simp_all
haftmann@28708
   365
haftmann@51143
   366
lemma minus_integer_code [code]:
haftmann@51143
   367
  "k - 0 = (k::integer)"
haftmann@51143
   368
  "0 - l = uminus (l::integer)"
haftmann@51143
   369
  "Pos m - Pos n = sub m n"
haftmann@51143
   370
  "Pos m - Neg n = Pos (m + n)"
haftmann@51143
   371
  "Neg m - Pos n = Neg (m + n)"
haftmann@51143
   372
  "Neg m - Neg n = sub n m"
haftmann@51143
   373
  by (transfer, simp)+
haftmann@46028
   374
haftmann@51143
   375
lemma abs_integer_code [code]:
haftmann@51143
   376
  "\<bar>k\<bar> = (if (k::integer) < 0 then - k else k)"
haftmann@51143
   377
  by simp
huffman@47108
   378
haftmann@51143
   379
lemma sgn_integer_code [code]:
haftmann@51143
   380
  "sgn k = (if k = 0 then 0 else if (k::integer) < 0 then - 1 else 1)"
huffman@47108
   381
  by simp
haftmann@46028
   382
haftmann@51143
   383
lemma times_integer_code [code]:
haftmann@51143
   384
  "k * 0 = (0::integer)"
haftmann@51143
   385
  "0 * l = (0::integer)"
haftmann@51143
   386
  "Pos m * Pos n = Pos (m * n)"
haftmann@51143
   387
  "Pos m * Neg n = Neg (m * n)"
haftmann@51143
   388
  "Neg m * Pos n = Neg (m * n)"
haftmann@51143
   389
  "Neg m * Neg n = Pos (m * n)"
haftmann@51143
   390
  by simp_all
haftmann@51143
   391
haftmann@51143
   392
definition divmod_integer :: "integer \<Rightarrow> integer \<Rightarrow> integer \<times> integer"
haftmann@51143
   393
where
haftmann@51143
   394
  "divmod_integer k l = (k div l, k mod l)"
haftmann@51143
   395
haftmann@51143
   396
lemma fst_divmod [simp]:
haftmann@51143
   397
  "fst (divmod_integer k l) = k div l"
haftmann@51143
   398
  by (simp add: divmod_integer_def)
haftmann@51143
   399
haftmann@51143
   400
lemma snd_divmod [simp]:
haftmann@51143
   401
  "snd (divmod_integer k l) = k mod l"
haftmann@51143
   402
  by (simp add: divmod_integer_def)
haftmann@51143
   403
haftmann@51143
   404
definition divmod_abs :: "integer \<Rightarrow> integer \<Rightarrow> integer \<times> integer"
haftmann@51143
   405
where
haftmann@51143
   406
  "divmod_abs k l = (\<bar>k\<bar> div \<bar>l\<bar>, \<bar>k\<bar> mod \<bar>l\<bar>)"
haftmann@51143
   407
haftmann@51143
   408
lemma fst_divmod_abs [simp]:
haftmann@51143
   409
  "fst (divmod_abs k l) = \<bar>k\<bar> div \<bar>l\<bar>"
haftmann@51143
   410
  by (simp add: divmod_abs_def)
haftmann@51143
   411
haftmann@51143
   412
lemma snd_divmod_abs [simp]:
haftmann@51143
   413
  "snd (divmod_abs k l) = \<bar>k\<bar> mod \<bar>l\<bar>"
haftmann@51143
   414
  by (simp add: divmod_abs_def)
haftmann@28708
   415
haftmann@53069
   416
lemma divmod_abs_code [code]:
haftmann@53069
   417
  "divmod_abs (Pos k) (Pos l) = divmod k l"
haftmann@53069
   418
  "divmod_abs (Neg k) (Neg l) = divmod k l"
haftmann@53069
   419
  "divmod_abs (Neg k) (Pos l) = divmod k l"
haftmann@53069
   420
  "divmod_abs (Pos k) (Neg l) = divmod k l"
haftmann@51143
   421
  "divmod_abs j 0 = (0, \<bar>j\<bar>)"
haftmann@51143
   422
  "divmod_abs 0 j = (0, 0)"
haftmann@51143
   423
  by (simp_all add: prod_eq_iff)
haftmann@51143
   424
haftmann@51143
   425
lemma divmod_integer_code [code]:
haftmann@51143
   426
  "divmod_integer k l =
haftmann@51143
   427
    (if k = 0 then (0, 0) else if l = 0 then (0, k) else
haftmann@51143
   428
    (apsnd \<circ> times \<circ> sgn) l (if sgn k = sgn l
haftmann@51143
   429
      then divmod_abs k l
haftmann@51143
   430
      else (let (r, s) = divmod_abs k l in
haftmann@51143
   431
        if s = 0 then (- r, 0) else (- r - 1, \<bar>l\<bar> - s))))"
haftmann@51143
   432
proof -
haftmann@51143
   433
  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
   434
    by (auto simp add: sgn_if)
haftmann@51143
   435
  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
   436
  show ?thesis
blanchet@55414
   437
    by (simp add: prod_eq_iff integer_eq_iff case_prod_beta aux1)
haftmann@51143
   438
      (auto simp add: zdiv_zminus1_eq_if zmod_zminus1_eq_if div_minus_right mod_minus_right aux2)
haftmann@51143
   439
qed
haftmann@51143
   440
haftmann@51143
   441
lemma div_integer_code [code]:
haftmann@51143
   442
  "k div l = fst (divmod_integer k l)"
haftmann@28708
   443
  by simp
haftmann@28708
   444
haftmann@51143
   445
lemma mod_integer_code [code]:
haftmann@51143
   446
  "k mod l = snd (divmod_integer k l)"
haftmann@25767
   447
  by simp
haftmann@24999
   448
haftmann@51143
   449
lemma equal_integer_code [code]:
haftmann@51143
   450
  "HOL.equal 0 (0::integer) \<longleftrightarrow> True"
haftmann@51143
   451
  "HOL.equal 0 (Pos l) \<longleftrightarrow> False"
haftmann@51143
   452
  "HOL.equal 0 (Neg l) \<longleftrightarrow> False"
haftmann@51143
   453
  "HOL.equal (Pos k) 0 \<longleftrightarrow> False"
haftmann@51143
   454
  "HOL.equal (Pos k) (Pos l) \<longleftrightarrow> HOL.equal k l"
haftmann@51143
   455
  "HOL.equal (Pos k) (Neg l) \<longleftrightarrow> False"
haftmann@51143
   456
  "HOL.equal (Neg k) 0 \<longleftrightarrow> False"
haftmann@51143
   457
  "HOL.equal (Neg k) (Pos l) \<longleftrightarrow> False"
haftmann@51143
   458
  "HOL.equal (Neg k) (Neg l) \<longleftrightarrow> HOL.equal k l"
haftmann@51143
   459
  by (simp_all add: equal)
haftmann@51143
   460
haftmann@51143
   461
lemma equal_integer_refl [code nbe]:
haftmann@51143
   462
  "HOL.equal (k::integer) k \<longleftrightarrow> True"
haftmann@51143
   463
  by (fact equal_refl)
haftmann@31266
   464
haftmann@51143
   465
lemma less_eq_integer_code [code]:
haftmann@51143
   466
  "0 \<le> (0::integer) \<longleftrightarrow> True"
haftmann@51143
   467
  "0 \<le> Pos l \<longleftrightarrow> True"
haftmann@51143
   468
  "0 \<le> Neg l \<longleftrightarrow> False"
haftmann@51143
   469
  "Pos k \<le> 0 \<longleftrightarrow> False"
haftmann@51143
   470
  "Pos k \<le> Pos l \<longleftrightarrow> k \<le> l"
haftmann@51143
   471
  "Pos k \<le> Neg l \<longleftrightarrow> False"
haftmann@51143
   472
  "Neg k \<le> 0 \<longleftrightarrow> True"
haftmann@51143
   473
  "Neg k \<le> Pos l \<longleftrightarrow> True"
haftmann@51143
   474
  "Neg k \<le> Neg l \<longleftrightarrow> l \<le> k"
haftmann@51143
   475
  by simp_all
haftmann@51143
   476
haftmann@51143
   477
lemma less_integer_code [code]:
haftmann@51143
   478
  "0 < (0::integer) \<longleftrightarrow> False"
haftmann@51143
   479
  "0 < Pos l \<longleftrightarrow> True"
haftmann@51143
   480
  "0 < Neg l \<longleftrightarrow> False"
haftmann@51143
   481
  "Pos k < 0 \<longleftrightarrow> False"
haftmann@51143
   482
  "Pos k < Pos l \<longleftrightarrow> k < l"
haftmann@51143
   483
  "Pos k < Neg l \<longleftrightarrow> False"
haftmann@51143
   484
  "Neg k < 0 \<longleftrightarrow> True"
haftmann@51143
   485
  "Neg k < Pos l \<longleftrightarrow> True"
haftmann@51143
   486
  "Neg k < Neg l \<longleftrightarrow> l < k"
haftmann@51143
   487
  by simp_all
haftmann@26140
   488
haftmann@51143
   489
lift_definition num_of_integer :: "integer \<Rightarrow> num"
haftmann@51143
   490
  is "num_of_nat \<circ> nat"
haftmann@51143
   491
  .
haftmann@51143
   492
haftmann@51143
   493
lemma num_of_integer_code [code]:
haftmann@51143
   494
  "num_of_integer k = (if k \<le> 1 then Num.One
haftmann@51143
   495
     else let
haftmann@51143
   496
       (l, j) = divmod_integer k 2;
haftmann@51143
   497
       l' = num_of_integer l;
haftmann@51143
   498
       l'' = l' + l'
haftmann@51143
   499
     in if j = 0 then l'' else l'' + Num.One)"
haftmann@51143
   500
proof -
haftmann@51143
   501
  {
haftmann@51143
   502
    assume "int_of_integer k mod 2 = 1"
haftmann@51143
   503
    then have "nat (int_of_integer k mod 2) = nat 1" by simp
haftmann@51143
   504
    moreover assume *: "1 < int_of_integer k"
haftmann@51143
   505
    ultimately have **: "nat (int_of_integer k) mod 2 = 1" by (simp add: nat_mod_distrib)
haftmann@51143
   506
    have "num_of_nat (nat (int_of_integer k)) =
haftmann@51143
   507
      num_of_nat (2 * (nat (int_of_integer k) div 2) + nat (int_of_integer k) mod 2)"
haftmann@51143
   508
      by simp
haftmann@51143
   509
    then have "num_of_nat (nat (int_of_integer k)) =
haftmann@51143
   510
      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
   511
      by (simp add: mult_2)
haftmann@51143
   512
    with ** have "num_of_nat (nat (int_of_integer k)) =
haftmann@51143
   513
      num_of_nat (nat (int_of_integer k) div 2 + nat (int_of_integer k) div 2 + 1)"
haftmann@51143
   514
      by simp
haftmann@51143
   515
  }
haftmann@51143
   516
  note aux = this
haftmann@51143
   517
  show ?thesis
blanchet@55414
   518
    by (auto simp add: num_of_integer_def nat_of_integer_def Let_def case_prod_beta
haftmann@51143
   519
      not_le integer_eq_iff less_eq_integer_def
haftmann@51143
   520
      nat_mult_distrib nat_div_distrib num_of_nat_One num_of_nat_plus_distrib
haftmann@51143
   521
       mult_2 [where 'a=nat] aux add_One)
haftmann@25918
   522
qed
haftmann@25918
   523
haftmann@51143
   524
lemma nat_of_integer_code [code]:
haftmann@51143
   525
  "nat_of_integer k = (if k \<le> 0 then 0
haftmann@51143
   526
     else let
haftmann@51143
   527
       (l, j) = divmod_integer k 2;
haftmann@51143
   528
       l' = nat_of_integer l;
haftmann@51143
   529
       l'' = l' + l'
haftmann@51143
   530
     in if j = 0 then l'' else l'' + 1)"
haftmann@33340
   531
proof -
haftmann@51143
   532
  obtain j where "k = integer_of_int j"
haftmann@51143
   533
  proof
haftmann@51143
   534
    show "k = integer_of_int (int_of_integer k)" by simp
haftmann@51143
   535
  qed
haftmann@51143
   536
  moreover have "2 * (j div 2) = j - j mod 2"
haftmann@57512
   537
    by (simp add: zmult_div_cancel mult.commute)
haftmann@51143
   538
  ultimately show ?thesis
haftmann@63950
   539
    by (auto simp add: split_def Let_def modulo_integer_def nat_of_integer_def not_le
haftmann@51143
   540
      nat_add_distrib [symmetric] Suc_nat_eq_nat_zadd1)
haftmann@51143
   541
      (auto simp add: mult_2 [symmetric])
haftmann@33340
   542
qed
haftmann@28708
   543
haftmann@51143
   544
lemma int_of_integer_code [code]:
haftmann@51143
   545
  "int_of_integer k = (if k < 0 then - (int_of_integer (- k))
haftmann@51143
   546
     else if k = 0 then 0
haftmann@51143
   547
     else let
haftmann@51143
   548
       (l, j) = divmod_integer k 2;
haftmann@51143
   549
       l' = 2 * int_of_integer l
haftmann@51143
   550
     in if j = 0 then l' else l' + 1)"
haftmann@51143
   551
  by (auto simp add: split_def Let_def integer_eq_iff zmult_div_cancel)
haftmann@28708
   552
haftmann@51143
   553
lemma integer_of_int_code [code]:
haftmann@51143
   554
  "integer_of_int k = (if k < 0 then - (integer_of_int (- k))
haftmann@51143
   555
     else if k = 0 then 0
haftmann@51143
   556
     else let
haftmann@60868
   557
       l = 2 * integer_of_int (k div 2);
haftmann@60868
   558
       j = k mod 2
haftmann@60868
   559
     in if j = 0 then l else l + 1)"
haftmann@51143
   560
  by (auto simp add: split_def Let_def integer_eq_iff zmult_div_cancel)
haftmann@51143
   561
haftmann@51143
   562
hide_const (open) Pos Neg sub dup divmod_abs
huffman@46547
   563
haftmann@28708
   564
wenzelm@60758
   565
subsection \<open>Serializer setup for target language integers\<close>
haftmann@24999
   566
haftmann@51143
   567
code_reserved Eval int Integer abs
haftmann@25767
   568
haftmann@52435
   569
code_printing
haftmann@52435
   570
  type_constructor integer \<rightharpoonup>
haftmann@52435
   571
    (SML) "IntInf.int"
haftmann@52435
   572
    and (OCaml) "Big'_int.big'_int"
haftmann@52435
   573
    and (Haskell) "Integer"
haftmann@52435
   574
    and (Scala) "BigInt"
haftmann@52435
   575
    and (Eval) "int"
haftmann@52435
   576
| class_instance integer :: equal \<rightharpoonup>
haftmann@52435
   577
    (Haskell) -
haftmann@24999
   578
haftmann@52435
   579
code_printing
haftmann@52435
   580
  constant "0::integer" \<rightharpoonup>
haftmann@58400
   581
    (SML) "!(0/ :/ IntInf.int)"
haftmann@52435
   582
    and (OCaml) "Big'_int.zero'_big'_int"
haftmann@58400
   583
    and (Haskell) "!(0/ ::/ Integer)"
haftmann@52435
   584
    and (Scala) "BigInt(0)"
huffman@47108
   585
wenzelm@60758
   586
setup \<open>
haftmann@58399
   587
  fold (fn target =>
haftmann@58399
   588
    Numeral.add_code @{const_name Code_Numeral.Pos} I Code_Printer.literal_numeral target
haftmann@58399
   589
    #> Numeral.add_code @{const_name Code_Numeral.Neg} (op ~) Code_Printer.literal_numeral target)
haftmann@58399
   590
    ["SML", "OCaml", "Haskell", "Scala"]
wenzelm@60758
   591
\<close>
haftmann@51143
   592
haftmann@52435
   593
code_printing
haftmann@52435
   594
  constant "plus :: integer \<Rightarrow> _ \<Rightarrow> _" \<rightharpoonup>
haftmann@52435
   595
    (SML) "IntInf.+ ((_), (_))"
haftmann@52435
   596
    and (OCaml) "Big'_int.add'_big'_int"
haftmann@52435
   597
    and (Haskell) infixl 6 "+"
haftmann@52435
   598
    and (Scala) infixl 7 "+"
haftmann@52435
   599
    and (Eval) infixl 8 "+"
haftmann@52435
   600
| constant "uminus :: integer \<Rightarrow> _" \<rightharpoonup>
haftmann@52435
   601
    (SML) "IntInf.~"
haftmann@52435
   602
    and (OCaml) "Big'_int.minus'_big'_int"
haftmann@52435
   603
    and (Haskell) "negate"
haftmann@52435
   604
    and (Scala) "!(- _)"
haftmann@52435
   605
    and (Eval) "~/ _"
haftmann@52435
   606
| constant "minus :: integer \<Rightarrow> _" \<rightharpoonup>
haftmann@52435
   607
    (SML) "IntInf.- ((_), (_))"
haftmann@52435
   608
    and (OCaml) "Big'_int.sub'_big'_int"
haftmann@52435
   609
    and (Haskell) infixl 6 "-"
haftmann@52435
   610
    and (Scala) infixl 7 "-"
haftmann@52435
   611
    and (Eval) infixl 8 "-"
haftmann@52435
   612
| constant Code_Numeral.dup \<rightharpoonup>
haftmann@52435
   613
    (SML) "IntInf.*/ (2,/ (_))"
haftmann@52435
   614
    and (OCaml) "Big'_int.mult'_big'_int/ (Big'_int.big'_int'_of'_int/ 2)"
haftmann@52435
   615
    and (Haskell) "!(2 * _)"
haftmann@52435
   616
    and (Scala) "!(2 * _)"
haftmann@52435
   617
    and (Eval) "!(2 * _)"
haftmann@52435
   618
| constant Code_Numeral.sub \<rightharpoonup>
haftmann@52435
   619
    (SML) "!(raise/ Fail/ \"sub\")"
haftmann@52435
   620
    and (OCaml) "failwith/ \"sub\""
haftmann@52435
   621
    and (Haskell) "error/ \"sub\""
haftmann@52435
   622
    and (Scala) "!sys.error(\"sub\")"
haftmann@52435
   623
| constant "times :: integer \<Rightarrow> _ \<Rightarrow> _" \<rightharpoonup>
haftmann@52435
   624
    (SML) "IntInf.* ((_), (_))"
haftmann@52435
   625
    and (OCaml) "Big'_int.mult'_big'_int"
haftmann@52435
   626
    and (Haskell) infixl 7 "*"
haftmann@52435
   627
    and (Scala) infixl 8 "*"
haftmann@52435
   628
    and (Eval) infixl 9 "*"
haftmann@52435
   629
| constant Code_Numeral.divmod_abs \<rightharpoonup>
haftmann@52435
   630
    (SML) "IntInf.divMod/ (IntInf.abs _,/ IntInf.abs _)"
haftmann@52435
   631
    and (OCaml) "Big'_int.quomod'_big'_int/ (Big'_int.abs'_big'_int _)/ (Big'_int.abs'_big'_int _)"
haftmann@52435
   632
    and (Haskell) "divMod/ (abs _)/ (abs _)"
haftmann@52435
   633
    and (Scala) "!((k: BigInt) => (l: BigInt) =>/ if (l == 0)/ (BigInt(0), k) else/ (k.abs '/% l.abs))"
haftmann@52435
   634
    and (Eval) "Integer.div'_mod/ (abs _)/ (abs _)"
haftmann@52435
   635
| constant "HOL.equal :: integer \<Rightarrow> _ \<Rightarrow> bool" \<rightharpoonup>
haftmann@52435
   636
    (SML) "!((_ : IntInf.int) = _)"
haftmann@52435
   637
    and (OCaml) "Big'_int.eq'_big'_int"
haftmann@52435
   638
    and (Haskell) infix 4 "=="
haftmann@52435
   639
    and (Scala) infixl 5 "=="
haftmann@52435
   640
    and (Eval) infixl 6 "="
haftmann@52435
   641
| constant "less_eq :: integer \<Rightarrow> _ \<Rightarrow> bool" \<rightharpoonup>
haftmann@52435
   642
    (SML) "IntInf.<= ((_), (_))"
haftmann@52435
   643
    and (OCaml) "Big'_int.le'_big'_int"
haftmann@52435
   644
    and (Haskell) infix 4 "<="
haftmann@52435
   645
    and (Scala) infixl 4 "<="
haftmann@52435
   646
    and (Eval) infixl 6 "<="
haftmann@52435
   647
| constant "less :: integer \<Rightarrow> _ \<Rightarrow> bool" \<rightharpoonup>
haftmann@52435
   648
    (SML) "IntInf.< ((_), (_))"
haftmann@52435
   649
    and (OCaml) "Big'_int.lt'_big'_int"
haftmann@52435
   650
    and (Haskell) infix 4 "<"
haftmann@52435
   651
    and (Scala) infixl 4 "<"
haftmann@52435
   652
    and (Eval) infixl 6 "<"
Andreas@61857
   653
| constant "abs :: integer \<Rightarrow> _" \<rightharpoonup>
Andreas@61857
   654
    (SML) "IntInf.abs"
Andreas@61857
   655
    and (OCaml) "Big'_int.abs'_big'_int"
Andreas@61857
   656
    and (Haskell) "Prelude.abs"
Andreas@61857
   657
    and (Scala) "_.abs"
Andreas@61857
   658
    and (Eval) "abs"
haftmann@51143
   659
haftmann@52435
   660
code_identifier
haftmann@52435
   661
  code_module Code_Numeral \<rightharpoonup> (SML) Arith and (OCaml) Arith and (Haskell) Arith
huffman@46547
   662
haftmann@51143
   663
wenzelm@60758
   664
subsection \<open>Type of target language naturals\<close>
haftmann@51143
   665
wenzelm@61076
   666
typedef natural = "UNIV :: nat set"
haftmann@51143
   667
  morphisms nat_of_natural natural_of_nat ..
haftmann@51143
   668
haftmann@59487
   669
setup_lifting type_definition_natural
haftmann@51143
   670
haftmann@51143
   671
lemma natural_eq_iff [termination_simp]:
haftmann@51143
   672
  "m = n \<longleftrightarrow> nat_of_natural m = nat_of_natural n"
haftmann@51143
   673
  by transfer rule
haftmann@51143
   674
haftmann@51143
   675
lemma natural_eqI:
haftmann@51143
   676
  "nat_of_natural m = nat_of_natural n \<Longrightarrow> m = n"
haftmann@51143
   677
  using natural_eq_iff [of m n] by simp
haftmann@51143
   678
haftmann@51143
   679
lemma nat_of_natural_of_nat_inverse [simp]:
haftmann@51143
   680
  "nat_of_natural (natural_of_nat n) = n"
haftmann@51143
   681
  by transfer rule
haftmann@51143
   682
haftmann@51143
   683
lemma natural_of_nat_of_natural_inverse [simp]:
haftmann@51143
   684
  "natural_of_nat (nat_of_natural n) = n"
haftmann@51143
   685
  by transfer rule
haftmann@51143
   686
haftmann@51143
   687
instantiation natural :: "{comm_monoid_diff, semiring_1}"
haftmann@51143
   688
begin
haftmann@51143
   689
haftmann@51143
   690
lift_definition zero_natural :: natural
haftmann@51143
   691
  is "0 :: nat"
haftmann@51143
   692
  .
haftmann@51143
   693
haftmann@51143
   694
declare zero_natural.rep_eq [simp]
haftmann@51143
   695
haftmann@51143
   696
lift_definition one_natural :: natural
haftmann@51143
   697
  is "1 :: nat"
haftmann@51143
   698
  .
haftmann@51143
   699
haftmann@51143
   700
declare one_natural.rep_eq [simp]
haftmann@51143
   701
haftmann@51143
   702
lift_definition plus_natural :: "natural \<Rightarrow> natural \<Rightarrow> natural"
haftmann@51143
   703
  is "plus :: nat \<Rightarrow> nat \<Rightarrow> nat"
haftmann@51143
   704
  .
haftmann@51143
   705
haftmann@51143
   706
declare plus_natural.rep_eq [simp]
haftmann@51143
   707
haftmann@51143
   708
lift_definition minus_natural :: "natural \<Rightarrow> natural \<Rightarrow> natural"
haftmann@51143
   709
  is "minus :: nat \<Rightarrow> nat \<Rightarrow> nat"
haftmann@51143
   710
  .
haftmann@51143
   711
haftmann@51143
   712
declare minus_natural.rep_eq [simp]
haftmann@51143
   713
haftmann@51143
   714
lift_definition times_natural :: "natural \<Rightarrow> natural \<Rightarrow> natural"
haftmann@51143
   715
  is "times :: nat \<Rightarrow> nat \<Rightarrow> nat"
haftmann@51143
   716
  .
haftmann@51143
   717
haftmann@51143
   718
declare times_natural.rep_eq [simp]
haftmann@51143
   719
haftmann@51143
   720
instance proof
haftmann@51143
   721
qed (transfer, simp add: algebra_simps)+
haftmann@51143
   722
haftmann@51143
   723
end
haftmann@51143
   724
haftmann@64241
   725
instance natural :: Rings.dvd ..
haftmann@64241
   726
haftmann@64241
   727
lemma [transfer_rule]:
haftmann@64241
   728
  "rel_fun pcr_natural (rel_fun pcr_natural HOL.iff) Rings.dvd Rings.dvd"
haftmann@64241
   729
  unfolding dvd_def by transfer_prover
haftmann@64241
   730
haftmann@51143
   731
lemma [transfer_rule]:
blanchet@55945
   732
  "rel_fun HOL.eq pcr_natural (\<lambda>n::nat. n) (of_nat :: nat \<Rightarrow> natural)"
haftmann@51143
   733
proof -
blanchet@55945
   734
  have "rel_fun HOL.eq pcr_natural (of_nat :: nat \<Rightarrow> nat) (of_nat :: nat \<Rightarrow> natural)"
haftmann@51143
   735
    by (unfold of_nat_def [abs_def]) transfer_prover
haftmann@51143
   736
  then show ?thesis by (simp add: id_def)
haftmann@51143
   737
qed
haftmann@51143
   738
haftmann@51143
   739
lemma [transfer_rule]:
blanchet@55945
   740
  "rel_fun HOL.eq pcr_natural (numeral :: num \<Rightarrow> nat) (numeral :: num \<Rightarrow> natural)"
haftmann@51143
   741
proof -
blanchet@55945
   742
  have "rel_fun HOL.eq pcr_natural (numeral :: num \<Rightarrow> nat) (\<lambda>n. of_nat (numeral n))"
haftmann@51143
   743
    by transfer_prover
haftmann@51143
   744
  then show ?thesis by simp
haftmann@51143
   745
qed
haftmann@51143
   746
haftmann@51143
   747
lemma nat_of_natural_of_nat [simp]:
haftmann@51143
   748
  "nat_of_natural (of_nat n) = n"
haftmann@51143
   749
  by transfer rule
haftmann@51143
   750
haftmann@51143
   751
lemma natural_of_nat_of_nat [simp, code_abbrev]:
haftmann@51143
   752
  "natural_of_nat = of_nat"
haftmann@51143
   753
  by transfer rule
haftmann@51143
   754
haftmann@51143
   755
lemma of_nat_of_natural [simp]:
haftmann@51143
   756
  "of_nat (nat_of_natural n) = n"
haftmann@51143
   757
  by transfer rule
haftmann@51143
   758
haftmann@51143
   759
lemma nat_of_natural_numeral [simp]:
haftmann@51143
   760
  "nat_of_natural (numeral k) = numeral k"
haftmann@51143
   761
  by transfer rule
haftmann@51143
   762
haftmann@51143
   763
instantiation natural :: "{semiring_div, equal, linordered_semiring}"
haftmann@51143
   764
begin
haftmann@51143
   765
haftmann@60352
   766
lift_definition divide_natural :: "natural \<Rightarrow> natural \<Rightarrow> natural"
haftmann@60352
   767
  is "divide :: nat \<Rightarrow> nat \<Rightarrow> nat"
haftmann@51143
   768
  .
haftmann@51143
   769
haftmann@60352
   770
declare divide_natural.rep_eq [simp]
haftmann@51143
   771
haftmann@63950
   772
lift_definition modulo_natural :: "natural \<Rightarrow> natural \<Rightarrow> natural"
haftmann@63950
   773
  is "modulo :: nat \<Rightarrow> nat \<Rightarrow> nat"
haftmann@51143
   774
  .
haftmann@51143
   775
haftmann@63950
   776
declare modulo_natural.rep_eq [simp]
haftmann@51143
   777
haftmann@51143
   778
lift_definition less_eq_natural :: "natural \<Rightarrow> natural \<Rightarrow> bool"
haftmann@51143
   779
  is "less_eq :: nat \<Rightarrow> nat \<Rightarrow> bool"
haftmann@51143
   780
  .
haftmann@51143
   781
haftmann@51143
   782
declare less_eq_natural.rep_eq [termination_simp]
haftmann@51143
   783
haftmann@51143
   784
lift_definition less_natural :: "natural \<Rightarrow> natural \<Rightarrow> bool"
haftmann@51143
   785
  is "less :: nat \<Rightarrow> nat \<Rightarrow> bool"
haftmann@51143
   786
  .
haftmann@51143
   787
haftmann@51143
   788
declare less_natural.rep_eq [termination_simp]
haftmann@51143
   789
haftmann@51143
   790
lift_definition equal_natural :: "natural \<Rightarrow> natural \<Rightarrow> bool"
haftmann@51143
   791
  is "HOL.equal :: nat \<Rightarrow> nat \<Rightarrow> bool"
haftmann@51143
   792
  .
haftmann@51143
   793
haftmann@51143
   794
instance proof
haftmann@51143
   795
qed (transfer, simp add: algebra_simps equal less_le_not_le [symmetric] linear)+
haftmann@51143
   796
haftmann@24999
   797
end
haftmann@46664
   798
haftmann@51143
   799
lemma [transfer_rule]:
blanchet@55945
   800
  "rel_fun pcr_natural (rel_fun pcr_natural pcr_natural) (min :: _ \<Rightarrow> _ \<Rightarrow> nat) (min :: _ \<Rightarrow> _ \<Rightarrow> natural)"
haftmann@51143
   801
  by (unfold min_def [abs_def]) transfer_prover
haftmann@51143
   802
haftmann@51143
   803
lemma [transfer_rule]:
blanchet@55945
   804
  "rel_fun pcr_natural (rel_fun pcr_natural pcr_natural) (max :: _ \<Rightarrow> _ \<Rightarrow> nat) (max :: _ \<Rightarrow> _ \<Rightarrow> natural)"
haftmann@51143
   805
  by (unfold max_def [abs_def]) transfer_prover
haftmann@51143
   806
haftmann@51143
   807
lemma nat_of_natural_min [simp]:
haftmann@51143
   808
  "nat_of_natural (min k l) = min (nat_of_natural k) (nat_of_natural l)"
haftmann@51143
   809
  by transfer rule
haftmann@51143
   810
haftmann@51143
   811
lemma nat_of_natural_max [simp]:
haftmann@51143
   812
  "nat_of_natural (max k l) = max (nat_of_natural k) (nat_of_natural l)"
haftmann@51143
   813
  by transfer rule
haftmann@51143
   814
haftmann@51143
   815
lift_definition natural_of_integer :: "integer \<Rightarrow> natural"
haftmann@51143
   816
  is "nat :: int \<Rightarrow> nat"
haftmann@51143
   817
  .
haftmann@51143
   818
haftmann@51143
   819
lift_definition integer_of_natural :: "natural \<Rightarrow> integer"
haftmann@51143
   820
  is "of_nat :: nat \<Rightarrow> int"
haftmann@51143
   821
  .
haftmann@51143
   822
haftmann@51143
   823
lemma natural_of_integer_of_natural [simp]:
haftmann@51143
   824
  "natural_of_integer (integer_of_natural n) = n"
haftmann@51143
   825
  by transfer simp
haftmann@51143
   826
haftmann@51143
   827
lemma integer_of_natural_of_integer [simp]:
haftmann@51143
   828
  "integer_of_natural (natural_of_integer k) = max 0 k"
haftmann@51143
   829
  by transfer auto
haftmann@51143
   830
haftmann@51143
   831
lemma int_of_integer_of_natural [simp]:
haftmann@51143
   832
  "int_of_integer (integer_of_natural n) = of_nat (nat_of_natural n)"
haftmann@51143
   833
  by transfer rule
haftmann@51143
   834
haftmann@51143
   835
lemma integer_of_natural_of_nat [simp]:
haftmann@51143
   836
  "integer_of_natural (of_nat n) = of_nat n"
haftmann@51143
   837
  by transfer rule
haftmann@51143
   838
haftmann@51143
   839
lemma [measure_function]:
haftmann@51143
   840
  "is_measure nat_of_natural"
haftmann@51143
   841
  by (rule is_measure_trivial)
haftmann@51143
   842
haftmann@51143
   843
wenzelm@60758
   844
subsection \<open>Inductive representation of target language naturals\<close>
haftmann@51143
   845
haftmann@51143
   846
lift_definition Suc :: "natural \<Rightarrow> natural"
haftmann@51143
   847
  is Nat.Suc
haftmann@51143
   848
  .
haftmann@51143
   849
haftmann@51143
   850
declare Suc.rep_eq [simp]
haftmann@51143
   851
blanchet@58306
   852
old_rep_datatype "0::natural" Suc
haftmann@51143
   853
  by (transfer, fact nat.induct nat.inject nat.distinct)+
haftmann@51143
   854
blanchet@55416
   855
lemma natural_cases [case_names nat, cases type: natural]:
haftmann@51143
   856
  fixes m :: natural
haftmann@51143
   857
  assumes "\<And>n. m = of_nat n \<Longrightarrow> P"
haftmann@51143
   858
  shows P
haftmann@51143
   859
  using assms by transfer blast
haftmann@51143
   860
blanchet@58390
   861
lemma [simp, code]: "size_natural = nat_of_natural"
blanchet@58390
   862
proof (rule ext)
blanchet@58390
   863
  fix n
blanchet@58390
   864
  show "size_natural n = nat_of_natural n"
blanchet@58390
   865
    by (induct n) simp_all
blanchet@58390
   866
qed
blanchet@58379
   867
blanchet@58390
   868
lemma [simp, code]: "size = nat_of_natural"
blanchet@58390
   869
proof (rule ext)
blanchet@58390
   870
  fix n
blanchet@58390
   871
  show "size n = nat_of_natural n"
blanchet@58390
   872
    by (induct n) simp_all
blanchet@58390
   873
qed
blanchet@58379
   874
haftmann@51143
   875
lemma natural_decr [termination_simp]:
haftmann@51143
   876
  "n \<noteq> 0 \<Longrightarrow> nat_of_natural n - Nat.Suc 0 < nat_of_natural n"
haftmann@51143
   877
  by transfer simp
haftmann@51143
   878
blanchet@58379
   879
lemma natural_zero_minus_one: "(0::natural) - 1 = 0"
blanchet@58379
   880
  by (rule zero_diff)
haftmann@51143
   881
blanchet@58379
   882
lemma Suc_natural_minus_one: "Suc n - 1 = n"
haftmann@51143
   883
  by transfer simp
haftmann@51143
   884
haftmann@51143
   885
hide_const (open) Suc
haftmann@51143
   886
haftmann@51143
   887
wenzelm@60758
   888
subsection \<open>Code refinement for target language naturals\<close>
haftmann@51143
   889
haftmann@51143
   890
lift_definition Nat :: "integer \<Rightarrow> natural"
haftmann@51143
   891
  is nat
haftmann@51143
   892
  .
haftmann@51143
   893
haftmann@51143
   894
lemma [code_post]:
haftmann@51143
   895
  "Nat 0 = 0"
haftmann@51143
   896
  "Nat 1 = 1"
haftmann@51143
   897
  "Nat (numeral k) = numeral k"
haftmann@51143
   898
  by (transfer, simp)+
haftmann@51143
   899
haftmann@51143
   900
lemma [code abstype]:
haftmann@51143
   901
  "Nat (integer_of_natural n) = n"
haftmann@51143
   902
  by transfer simp
haftmann@51143
   903
haftmann@63174
   904
lemma [code]:
haftmann@63174
   905
  "natural_of_nat n = natural_of_integer (integer_of_nat n)"
haftmann@63174
   906
  by transfer simp
haftmann@51143
   907
haftmann@51143
   908
lemma [code abstract]:
haftmann@51143
   909
  "integer_of_natural (natural_of_integer k) = max 0 k"
haftmann@51143
   910
  by simp
haftmann@51143
   911
haftmann@51143
   912
lemma [code_abbrev]:
haftmann@51143
   913
  "natural_of_integer (Code_Numeral.Pos k) = numeral k"
haftmann@51143
   914
  by transfer simp
haftmann@51143
   915
haftmann@51143
   916
lemma [code abstract]:
haftmann@51143
   917
  "integer_of_natural 0 = 0"
haftmann@51143
   918
  by transfer simp
haftmann@51143
   919
haftmann@51143
   920
lemma [code abstract]:
haftmann@51143
   921
  "integer_of_natural 1 = 1"
haftmann@51143
   922
  by transfer simp
haftmann@51143
   923
haftmann@51143
   924
lemma [code abstract]:
haftmann@51143
   925
  "integer_of_natural (Code_Numeral.Suc n) = integer_of_natural n + 1"
haftmann@51143
   926
  by transfer simp
haftmann@51143
   927
haftmann@51143
   928
lemma [code]:
haftmann@51143
   929
  "nat_of_natural = nat_of_integer \<circ> integer_of_natural"
haftmann@51143
   930
  by transfer (simp add: fun_eq_iff)
haftmann@51143
   931
haftmann@51143
   932
lemma [code, code_unfold]:
blanchet@55416
   933
  "case_natural f g n = (if n = 0 then f else g (n - 1))"
haftmann@51143
   934
  by (cases n rule: natural.exhaust) (simp_all, simp add: Suc_def)
haftmann@51143
   935
blanchet@55642
   936
declare natural.rec [code del]
haftmann@51143
   937
haftmann@51143
   938
lemma [code abstract]:
haftmann@51143
   939
  "integer_of_natural (m + n) = integer_of_natural m + integer_of_natural n"
haftmann@51143
   940
  by transfer simp
haftmann@51143
   941
haftmann@51143
   942
lemma [code abstract]:
haftmann@51143
   943
  "integer_of_natural (m - n) = max 0 (integer_of_natural m - integer_of_natural n)"
haftmann@51143
   944
  by transfer simp
haftmann@51143
   945
haftmann@51143
   946
lemma [code abstract]:
haftmann@51143
   947
  "integer_of_natural (m * n) = integer_of_natural m * integer_of_natural n"
haftmann@51143
   948
  by transfer (simp add: of_nat_mult)
haftmann@51143
   949
haftmann@51143
   950
lemma [code abstract]:
haftmann@51143
   951
  "integer_of_natural (m div n) = integer_of_natural m div integer_of_natural n"
haftmann@51143
   952
  by transfer (simp add: zdiv_int)
haftmann@51143
   953
haftmann@51143
   954
lemma [code abstract]:
haftmann@51143
   955
  "integer_of_natural (m mod n) = integer_of_natural m mod integer_of_natural n"
haftmann@51143
   956
  by transfer (simp add: zmod_int)
haftmann@51143
   957
haftmann@51143
   958
lemma [code]:
haftmann@51143
   959
  "HOL.equal m n \<longleftrightarrow> HOL.equal (integer_of_natural m) (integer_of_natural n)"
haftmann@51143
   960
  by transfer (simp add: equal)
haftmann@51143
   961
blanchet@58379
   962
lemma [code nbe]: "HOL.equal n (n::natural) \<longleftrightarrow> True"
blanchet@58379
   963
  by (rule equal_class.equal_refl)
haftmann@51143
   964
blanchet@58379
   965
lemma [code]: "m \<le> n \<longleftrightarrow> integer_of_natural m \<le> integer_of_natural n"
haftmann@51143
   966
  by transfer simp
haftmann@51143
   967
blanchet@58379
   968
lemma [code]: "m < n \<longleftrightarrow> integer_of_natural m < integer_of_natural n"
haftmann@51143
   969
  by transfer simp
haftmann@51143
   970
haftmann@51143
   971
hide_const (open) Nat
haftmann@51143
   972
kuncar@55736
   973
lifting_update integer.lifting
kuncar@55736
   974
lifting_forget integer.lifting
kuncar@55736
   975
kuncar@55736
   976
lifting_update natural.lifting
kuncar@55736
   977
lifting_forget natural.lifting
haftmann@51143
   978
haftmann@51143
   979
code_reflect Code_Numeral
haftmann@63174
   980
  datatypes natural
haftmann@63174
   981
  functions "Code_Numeral.Suc" "0 :: natural" "1 :: natural"
haftmann@63174
   982
    "plus :: natural \<Rightarrow> _" "minus :: natural \<Rightarrow> _"
haftmann@63174
   983
    "times :: natural \<Rightarrow> _" "divide :: natural \<Rightarrow> _"
haftmann@63950
   984
    "modulo :: natural \<Rightarrow> _"
haftmann@63174
   985
    integer_of_natural natural_of_integer
haftmann@51143
   986
haftmann@51143
   987
end