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