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