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