src/HOL/Code_Numeral.thy
 author haftmann Fri Apr 20 07:36:58 2018 +0000 (14 months ago) changeset 68010 3f223b9a0066 parent 67905 fe0f4eeceeb7 child 68028 1f9f973eed2a permissions -rw-r--r--
algebraic embeddings for bit operations
```     1 (*  Title:      HOL/Code_Numeral.thy
```
```     2     Author:     Florian Haftmann, TU Muenchen
```
```     3 *)
```
```     4
```
```     5 section \<open>Numeric types for code generation onto target language numerals only\<close>
```
```     6
```
```     7 theory Code_Numeral
```
```     8 imports Divides Lifting
```
```     9 begin
```
```    10
```
```    11 subsection \<open>Type of target language integers\<close>
```
```    12
```
```    13 typedef integer = "UNIV :: int set"
```
```    14   morphisms int_of_integer integer_of_int ..
```
```    15
```
```    16 setup_lifting type_definition_integer
```
```    17
```
```    18 lemma integer_eq_iff:
```
```    19   "k = l \<longleftrightarrow> int_of_integer k = int_of_integer l"
```
```    20   by transfer rule
```
```    21
```
```    22 lemma integer_eqI:
```
```    23   "int_of_integer k = int_of_integer l \<Longrightarrow> k = l"
```
```    24   using integer_eq_iff [of k l] by simp
```
```    25
```
```    26 lemma int_of_integer_integer_of_int [simp]:
```
```    27   "int_of_integer (integer_of_int k) = k"
```
```    28   by transfer rule
```
```    29
```
```    30 lemma integer_of_int_int_of_integer [simp]:
```
```    31   "integer_of_int (int_of_integer k) = k"
```
```    32   by transfer rule
```
```    33
```
```    34 instantiation integer :: ring_1
```
```    35 begin
```
```    36
```
```    37 lift_definition zero_integer :: integer
```
```    38   is "0 :: int"
```
```    39   .
```
```    40
```
```    41 declare zero_integer.rep_eq [simp]
```
```    42
```
```    43 lift_definition one_integer :: integer
```
```    44   is "1 :: int"
```
```    45   .
```
```    46
```
```    47 declare one_integer.rep_eq [simp]
```
```    48
```
```    49 lift_definition plus_integer :: "integer \<Rightarrow> integer \<Rightarrow> integer"
```
```    50   is "plus :: int \<Rightarrow> int \<Rightarrow> int"
```
```    51   .
```
```    52
```
```    53 declare plus_integer.rep_eq [simp]
```
```    54
```
```    55 lift_definition uminus_integer :: "integer \<Rightarrow> integer"
```
```    56   is "uminus :: int \<Rightarrow> int"
```
```    57   .
```
```    58
```
```    59 declare uminus_integer.rep_eq [simp]
```
```    60
```
```    61 lift_definition minus_integer :: "integer \<Rightarrow> integer \<Rightarrow> integer"
```
```    62   is "minus :: int \<Rightarrow> int \<Rightarrow> int"
```
```    63   .
```
```    64
```
```    65 declare minus_integer.rep_eq [simp]
```
```    66
```
```    67 lift_definition times_integer :: "integer \<Rightarrow> integer \<Rightarrow> integer"
```
```    68   is "times :: int \<Rightarrow> int \<Rightarrow> int"
```
```    69   .
```
```    70
```
```    71 declare times_integer.rep_eq [simp]
```
```    72
```
```    73 instance proof
```
```    74 qed (transfer, simp add: algebra_simps)+
```
```    75
```
```    76 end
```
```    77
```
```    78 instance integer :: Rings.dvd ..
```
```    79
```
```    80 lemma [transfer_rule]:
```
```    81   "rel_fun pcr_integer (rel_fun pcr_integer HOL.iff) Rings.dvd Rings.dvd"
```
```    82   unfolding dvd_def by transfer_prover
```
```    83
```
```    84 lemma [transfer_rule]:
```
```    85   "rel_fun (=) pcr_integer (of_bool :: bool \<Rightarrow> int) (of_bool :: bool \<Rightarrow> integer)"
```
```    86   by (unfold of_bool_def [abs_def]) transfer_prover
```
```    87
```
```    88 lemma [transfer_rule]:
```
```    89   "rel_fun (=) pcr_integer (of_nat :: nat \<Rightarrow> int) (of_nat :: nat \<Rightarrow> integer)"
```
```    90   by (rule transfer_rule_of_nat) transfer_prover+
```
```    91
```
```    92 lemma [transfer_rule]:
```
```    93   "rel_fun (=) pcr_integer (\<lambda>k :: int. k :: int) (of_int :: int \<Rightarrow> integer)"
```
```    94 proof -
```
```    95   have "rel_fun HOL.eq pcr_integer (of_int :: int \<Rightarrow> int) (of_int :: int \<Rightarrow> integer)"
```
```    96     by (rule transfer_rule_of_int) transfer_prover+
```
```    97   then show ?thesis by (simp add: id_def)
```
```    98 qed
```
```    99
```
```   100 lemma [transfer_rule]:
```
```   101   "rel_fun HOL.eq pcr_integer (numeral :: num \<Rightarrow> int) (numeral :: num \<Rightarrow> integer)"
```
```   102   by (rule transfer_rule_numeral) transfer_prover+
```
```   103
```
```   104 lemma [transfer_rule]:
```
```   105   "rel_fun HOL.eq (rel_fun HOL.eq pcr_integer) (Num.sub :: _ \<Rightarrow> _ \<Rightarrow> int) (Num.sub :: _ \<Rightarrow> _ \<Rightarrow> integer)"
```
```   106   by (unfold Num.sub_def [abs_def]) transfer_prover
```
```   107
```
```   108 lemma [transfer_rule]:
```
```   109   "rel_fun pcr_integer (rel_fun (=) pcr_integer) (power :: _ \<Rightarrow> _ \<Rightarrow> int) (power :: _ \<Rightarrow> _ \<Rightarrow> integer)"
```
```   110   by (unfold power_def [abs_def]) transfer_prover
```
```   111
```
```   112 lemma int_of_integer_of_nat [simp]:
```
```   113   "int_of_integer (of_nat n) = of_nat n"
```
```   114   by transfer rule
```
```   115
```
```   116 lift_definition integer_of_nat :: "nat \<Rightarrow> integer"
```
```   117   is "of_nat :: nat \<Rightarrow> int"
```
```   118   .
```
```   119
```
```   120 lemma integer_of_nat_eq_of_nat [code]:
```
```   121   "integer_of_nat = of_nat"
```
```   122   by transfer rule
```
```   123
```
```   124 lemma int_of_integer_integer_of_nat [simp]:
```
```   125   "int_of_integer (integer_of_nat n) = of_nat n"
```
```   126   by transfer rule
```
```   127
```
```   128 lift_definition nat_of_integer :: "integer \<Rightarrow> nat"
```
```   129   is Int.nat
```
```   130   .
```
```   131
```
```   132 lemma nat_of_integer_of_nat [simp]:
```
```   133   "nat_of_integer (of_nat n) = n"
```
```   134   by transfer simp
```
```   135
```
```   136 lemma int_of_integer_of_int [simp]:
```
```   137   "int_of_integer (of_int k) = k"
```
```   138   by transfer simp
```
```   139
```
```   140 lemma nat_of_integer_integer_of_nat [simp]:
```
```   141   "nat_of_integer (integer_of_nat n) = n"
```
```   142   by transfer simp
```
```   143
```
```   144 lemma integer_of_int_eq_of_int [simp, code_abbrev]:
```
```   145   "integer_of_int = of_int"
```
```   146   by transfer (simp add: fun_eq_iff)
```
```   147
```
```   148 lemma of_int_integer_of [simp]:
```
```   149   "of_int (int_of_integer k) = (k :: integer)"
```
```   150   by transfer rule
```
```   151
```
```   152 lemma int_of_integer_numeral [simp]:
```
```   153   "int_of_integer (numeral k) = numeral k"
```
```   154   by transfer rule
```
```   155
```
```   156 lemma int_of_integer_sub [simp]:
```
```   157   "int_of_integer (Num.sub k l) = Num.sub k l"
```
```   158   by transfer rule
```
```   159
```
```   160 definition integer_of_num :: "num \<Rightarrow> integer"
```
```   161   where [simp]: "integer_of_num = numeral"
```
```   162
```
```   163 lemma integer_of_num [code]:
```
```   164   "integer_of_num Num.One = 1"
```
```   165   "integer_of_num (Num.Bit0 n) = (let k = integer_of_num n in k + k)"
```
```   166   "integer_of_num (Num.Bit1 n) = (let k = integer_of_num n in k + k + 1)"
```
```   167   by (simp_all only: integer_of_num_def numeral.simps Let_def)
```
```   168
```
```   169 lemma integer_of_num_triv:
```
```   170   "integer_of_num Num.One = 1"
```
```   171   "integer_of_num (Num.Bit0 Num.One) = 2"
```
```   172   by simp_all
```
```   173
```
```   174 instantiation integer :: "{linordered_idom, equal}"
```
```   175 begin
```
```   176
```
```   177 lift_definition abs_integer :: "integer \<Rightarrow> integer"
```
```   178   is "abs :: int \<Rightarrow> int"
```
```   179   .
```
```   180
```
```   181 declare abs_integer.rep_eq [simp]
```
```   182
```
```   183 lift_definition sgn_integer :: "integer \<Rightarrow> integer"
```
```   184   is "sgn :: int \<Rightarrow> int"
```
```   185   .
```
```   186
```
```   187 declare sgn_integer.rep_eq [simp]
```
```   188
```
```   189 lift_definition less_eq_integer :: "integer \<Rightarrow> integer \<Rightarrow> bool"
```
```   190   is "less_eq :: int \<Rightarrow> int \<Rightarrow> bool"
```
```   191   .
```
```   192
```
```   193
```
```   194 lift_definition less_integer :: "integer \<Rightarrow> integer \<Rightarrow> bool"
```
```   195   is "less :: int \<Rightarrow> int \<Rightarrow> bool"
```
```   196   .
```
```   197
```
```   198 lift_definition equal_integer :: "integer \<Rightarrow> integer \<Rightarrow> bool"
```
```   199   is "HOL.equal :: int \<Rightarrow> int \<Rightarrow> bool"
```
```   200   .
```
```   201
```
```   202 instance
```
```   203   by standard (transfer, simp add: algebra_simps equal less_le_not_le [symmetric] mult_strict_right_mono linear)+
```
```   204
```
```   205 end
```
```   206
```
```   207 lemma [transfer_rule]:
```
```   208   "rel_fun pcr_integer (rel_fun pcr_integer pcr_integer) (min :: _ \<Rightarrow> _ \<Rightarrow> int) (min :: _ \<Rightarrow> _ \<Rightarrow> integer)"
```
```   209   by (unfold min_def [abs_def]) transfer_prover
```
```   210
```
```   211 lemma [transfer_rule]:
```
```   212   "rel_fun pcr_integer (rel_fun pcr_integer pcr_integer) (max :: _ \<Rightarrow> _ \<Rightarrow> int) (max :: _ \<Rightarrow> _ \<Rightarrow> integer)"
```
```   213   by (unfold max_def [abs_def]) transfer_prover
```
```   214
```
```   215 lemma int_of_integer_min [simp]:
```
```   216   "int_of_integer (min k l) = min (int_of_integer k) (int_of_integer l)"
```
```   217   by transfer rule
```
```   218
```
```   219 lemma int_of_integer_max [simp]:
```
```   220   "int_of_integer (max k l) = max (int_of_integer k) (int_of_integer l)"
```
```   221   by transfer rule
```
```   222
```
```   223 lemma nat_of_integer_non_positive [simp]:
```
```   224   "k \<le> 0 \<Longrightarrow> nat_of_integer k = 0"
```
```   225   by transfer simp
```
```   226
```
```   227 lemma of_nat_of_integer [simp]:
```
```   228   "of_nat (nat_of_integer k) = max 0 k"
```
```   229   by transfer auto
```
```   230
```
```   231 instantiation integer :: unique_euclidean_ring
```
```   232 begin
```
```   233
```
```   234 lift_definition divide_integer :: "integer \<Rightarrow> integer \<Rightarrow> integer"
```
```   235   is "divide :: int \<Rightarrow> int \<Rightarrow> int"
```
```   236   .
```
```   237
```
```   238 declare divide_integer.rep_eq [simp]
```
```   239
```
```   240 lift_definition modulo_integer :: "integer \<Rightarrow> integer \<Rightarrow> integer"
```
```   241   is "modulo :: int \<Rightarrow> int \<Rightarrow> int"
```
```   242   .
```
```   243
```
```   244 declare modulo_integer.rep_eq [simp]
```
```   245
```
```   246 lift_definition euclidean_size_integer :: "integer \<Rightarrow> nat"
```
```   247   is "euclidean_size :: int \<Rightarrow> nat"
```
```   248   .
```
```   249
```
```   250 declare euclidean_size_integer.rep_eq [simp]
```
```   251
```
```   252 lift_definition division_segment_integer :: "integer \<Rightarrow> integer"
```
```   253   is "division_segment :: int \<Rightarrow> int"
```
```   254   .
```
```   255
```
```   256 declare division_segment_integer.rep_eq [simp]
```
```   257
```
```   258 instance
```
```   259   by (standard; transfer)
```
```   260     (use mult_le_mono2 [of 1] in \<open>auto simp add: sgn_mult_abs abs_mult sgn_mult abs_mod_less sgn_mod nat_mult_distrib
```
```   261      division_segment_mult division_segment_mod intro: div_eqI\<close>)
```
```   262
```
```   263 end
```
```   264
```
```   265 lemma [code]:
```
```   266   "euclidean_size = nat_of_integer \<circ> abs"
```
```   267   by (simp add: fun_eq_iff nat_of_integer.rep_eq)
```
```   268
```
```   269 lemma [code]:
```
```   270   "division_segment (k :: integer) = (if k \<ge> 0 then 1 else - 1)"
```
```   271   by transfer (simp add: division_segment_int_def)
```
```   272
```
```   273 instance integer :: ring_parity
```
```   274   by (standard; transfer) (simp_all add: of_nat_div division_segment_int_def)
```
```   275
```
```   276 lemma [transfer_rule]:
```
```   277   "rel_fun (=) (rel_fun pcr_integer pcr_integer) (push_bit :: _ \<Rightarrow> _ \<Rightarrow> int) (push_bit :: _ \<Rightarrow> _ \<Rightarrow> integer)"
```
```   278   by (unfold push_bit_eq_mult [abs_def]) transfer_prover
```
```   279
```
```   280 lemma [transfer_rule]:
```
```   281   "rel_fun (=) (rel_fun pcr_integer pcr_integer) (take_bit :: _ \<Rightarrow> _ \<Rightarrow> int) (take_bit :: _ \<Rightarrow> _ \<Rightarrow> integer)"
```
```   282   by (unfold take_bit_eq_mod [abs_def]) transfer_prover
```
```   283
```
```   284 lemma [transfer_rule]:
```
```   285   "rel_fun (=) (rel_fun pcr_integer pcr_integer) (drop_bit :: _ \<Rightarrow> _ \<Rightarrow> int) (drop_bit :: _ \<Rightarrow> _ \<Rightarrow> integer)"
```
```   286   by (unfold drop_bit_eq_div [abs_def]) transfer_prover
```
```   287
```
```   288 instantiation integer :: unique_euclidean_semiring_numeral
```
```   289 begin
```
```   290
```
```   291 definition divmod_integer :: "num \<Rightarrow> num \<Rightarrow> integer \<times> integer"
```
```   292 where
```
```   293   divmod_integer'_def: "divmod_integer m n = (numeral m div numeral n, numeral m mod numeral n)"
```
```   294
```
```   295 definition divmod_step_integer :: "num \<Rightarrow> integer \<times> integer \<Rightarrow> integer \<times> integer"
```
```   296 where
```
```   297   "divmod_step_integer l qr = (let (q, r) = qr
```
```   298     in if r \<ge> numeral l then (2 * q + 1, r - numeral l)
```
```   299     else (2 * q, r))"
```
```   300
```
```   301 instance proof
```
```   302   show "divmod m n = (numeral m div numeral n :: integer, numeral m mod numeral n)"
```
```   303     for m n by (fact divmod_integer'_def)
```
```   304   show "divmod_step l qr = (let (q, r) = qr
```
```   305     in if r \<ge> numeral l then (2 * q + 1, r - numeral l)
```
```   306     else (2 * q, r))" for l and qr :: "integer \<times> integer"
```
```   307     by (fact divmod_step_integer_def)
```
```   308 qed (transfer,
```
```   309   fact le_add_diff_inverse2
```
```   310   unique_euclidean_semiring_numeral_class.div_less
```
```   311   unique_euclidean_semiring_numeral_class.mod_less
```
```   312   unique_euclidean_semiring_numeral_class.div_positive
```
```   313   unique_euclidean_semiring_numeral_class.mod_less_eq_dividend
```
```   314   unique_euclidean_semiring_numeral_class.pos_mod_bound
```
```   315   unique_euclidean_semiring_numeral_class.pos_mod_sign
```
```   316   unique_euclidean_semiring_numeral_class.mod_mult2_eq
```
```   317   unique_euclidean_semiring_numeral_class.div_mult2_eq
```
```   318   unique_euclidean_semiring_numeral_class.discrete)+
```
```   319
```
```   320 end
```
```   321
```
```   322 declare divmod_algorithm_code [where ?'a = integer,
```
```   323   folded integer_of_num_def, unfolded integer_of_num_triv,
```
```   324   code]
```
```   325
```
```   326 lemma integer_of_nat_0: "integer_of_nat 0 = 0"
```
```   327 by transfer simp
```
```   328
```
```   329 lemma integer_of_nat_1: "integer_of_nat 1 = 1"
```
```   330 by transfer simp
```
```   331
```
```   332 lemma integer_of_nat_numeral:
```
```   333   "integer_of_nat (numeral n) = numeral n"
```
```   334 by transfer simp
```
```   335
```
```   336
```
```   337 subsection \<open>Code theorems for target language integers\<close>
```
```   338
```
```   339 text \<open>Constructors\<close>
```
```   340
```
```   341 definition Pos :: "num \<Rightarrow> integer"
```
```   342 where
```
```   343   [simp, code_post]: "Pos = numeral"
```
```   344
```
```   345 lemma [transfer_rule]:
```
```   346   "rel_fun HOL.eq pcr_integer numeral Pos"
```
```   347   by simp transfer_prover
```
```   348
```
```   349 lemma Pos_fold [code_unfold]:
```
```   350   "numeral Num.One = Pos Num.One"
```
```   351   "numeral (Num.Bit0 k) = Pos (Num.Bit0 k)"
```
```   352   "numeral (Num.Bit1 k) = Pos (Num.Bit1 k)"
```
```   353   by simp_all
```
```   354
```
```   355 definition Neg :: "num \<Rightarrow> integer"
```
```   356 where
```
```   357   [simp, code_abbrev]: "Neg n = - Pos n"
```
```   358
```
```   359 lemma [transfer_rule]:
```
```   360   "rel_fun HOL.eq pcr_integer (\<lambda>n. - numeral n) Neg"
```
```   361   by (simp add: Neg_def [abs_def]) transfer_prover
```
```   362
```
```   363 code_datatype "0::integer" Pos Neg
```
```   364
```
```   365
```
```   366 text \<open>A further pair of constructors for generated computations\<close>
```
```   367
```
```   368 context
```
```   369 begin
```
```   370
```
```   371 qualified definition positive :: "num \<Rightarrow> integer"
```
```   372   where [simp]: "positive = numeral"
```
```   373
```
```   374 qualified definition negative :: "num \<Rightarrow> integer"
```
```   375   where [simp]: "negative = uminus \<circ> numeral"
```
```   376
```
```   377 lemma [code_computation_unfold]:
```
```   378   "numeral = positive"
```
```   379   "Pos = positive"
```
```   380   "Neg = negative"
```
```   381   by (simp_all add: fun_eq_iff)
```
```   382
```
```   383 end
```
```   384
```
```   385
```
```   386 text \<open>Auxiliary operations\<close>
```
```   387
```
```   388 lift_definition dup :: "integer \<Rightarrow> integer"
```
```   389   is "\<lambda>k::int. k + k"
```
```   390   .
```
```   391
```
```   392 lemma dup_code [code]:
```
```   393   "dup 0 = 0"
```
```   394   "dup (Pos n) = Pos (Num.Bit0 n)"
```
```   395   "dup (Neg n) = Neg (Num.Bit0 n)"
```
```   396   by (transfer, simp only: numeral_Bit0 minus_add_distrib)+
```
```   397
```
```   398 lift_definition sub :: "num \<Rightarrow> num \<Rightarrow> integer"
```
```   399   is "\<lambda>m n. numeral m - numeral n :: int"
```
```   400   .
```
```   401
```
```   402 lemma sub_code [code]:
```
```   403   "sub Num.One Num.One = 0"
```
```   404   "sub (Num.Bit0 m) Num.One = Pos (Num.BitM m)"
```
```   405   "sub (Num.Bit1 m) Num.One = Pos (Num.Bit0 m)"
```
```   406   "sub Num.One (Num.Bit0 n) = Neg (Num.BitM n)"
```
```   407   "sub Num.One (Num.Bit1 n) = Neg (Num.Bit0 n)"
```
```   408   "sub (Num.Bit0 m) (Num.Bit0 n) = dup (sub m n)"
```
```   409   "sub (Num.Bit1 m) (Num.Bit1 n) = dup (sub m n)"
```
```   410   "sub (Num.Bit1 m) (Num.Bit0 n) = dup (sub m n) + 1"
```
```   411   "sub (Num.Bit0 m) (Num.Bit1 n) = dup (sub m n) - 1"
```
```   412   by (transfer, simp add: dbl_def dbl_inc_def dbl_dec_def)+
```
```   413
```
```   414
```
```   415 text \<open>Implementations\<close>
```
```   416
```
```   417 lemma one_integer_code [code, code_unfold]:
```
```   418   "1 = Pos Num.One"
```
```   419   by simp
```
```   420
```
```   421 lemma plus_integer_code [code]:
```
```   422   "k + 0 = (k::integer)"
```
```   423   "0 + l = (l::integer)"
```
```   424   "Pos m + Pos n = Pos (m + n)"
```
```   425   "Pos m + Neg n = sub m n"
```
```   426   "Neg m + Pos n = sub n m"
```
```   427   "Neg m + Neg n = Neg (m + n)"
```
```   428   by (transfer, simp)+
```
```   429
```
```   430 lemma uminus_integer_code [code]:
```
```   431   "uminus 0 = (0::integer)"
```
```   432   "uminus (Pos m) = Neg m"
```
```   433   "uminus (Neg m) = Pos m"
```
```   434   by simp_all
```
```   435
```
```   436 lemma minus_integer_code [code]:
```
```   437   "k - 0 = (k::integer)"
```
```   438   "0 - l = uminus (l::integer)"
```
```   439   "Pos m - Pos n = sub m n"
```
```   440   "Pos m - Neg n = Pos (m + n)"
```
```   441   "Neg m - Pos n = Neg (m + n)"
```
```   442   "Neg m - Neg n = sub n m"
```
```   443   by (transfer, simp)+
```
```   444
```
```   445 lemma abs_integer_code [code]:
```
```   446   "\<bar>k\<bar> = (if (k::integer) < 0 then - k else k)"
```
```   447   by simp
```
```   448
```
```   449 lemma sgn_integer_code [code]:
```
```   450   "sgn k = (if k = 0 then 0 else if (k::integer) < 0 then - 1 else 1)"
```
```   451   by simp
```
```   452
```
```   453 lemma times_integer_code [code]:
```
```   454   "k * 0 = (0::integer)"
```
```   455   "0 * l = (0::integer)"
```
```   456   "Pos m * Pos n = Pos (m * n)"
```
```   457   "Pos m * Neg n = Neg (m * n)"
```
```   458   "Neg m * Pos n = Neg (m * n)"
```
```   459   "Neg m * Neg n = Pos (m * n)"
```
```   460   by simp_all
```
```   461
```
```   462 definition divmod_integer :: "integer \<Rightarrow> integer \<Rightarrow> integer \<times> integer"
```
```   463 where
```
```   464   "divmod_integer k l = (k div l, k mod l)"
```
```   465
```
```   466 lemma fst_divmod_integer [simp]:
```
```   467   "fst (divmod_integer k l) = k div l"
```
```   468   by (simp add: divmod_integer_def)
```
```   469
```
```   470 lemma snd_divmod_integer [simp]:
```
```   471   "snd (divmod_integer k l) = k mod l"
```
```   472   by (simp add: divmod_integer_def)
```
```   473
```
```   474 definition divmod_abs :: "integer \<Rightarrow> integer \<Rightarrow> integer \<times> integer"
```
```   475 where
```
```   476   "divmod_abs k l = (\<bar>k\<bar> div \<bar>l\<bar>, \<bar>k\<bar> mod \<bar>l\<bar>)"
```
```   477
```
```   478 lemma fst_divmod_abs [simp]:
```
```   479   "fst (divmod_abs k l) = \<bar>k\<bar> div \<bar>l\<bar>"
```
```   480   by (simp add: divmod_abs_def)
```
```   481
```
```   482 lemma snd_divmod_abs [simp]:
```
```   483   "snd (divmod_abs k l) = \<bar>k\<bar> mod \<bar>l\<bar>"
```
```   484   by (simp add: divmod_abs_def)
```
```   485
```
```   486 lemma divmod_abs_code [code]:
```
```   487   "divmod_abs (Pos k) (Pos l) = divmod k l"
```
```   488   "divmod_abs (Neg k) (Neg l) = divmod k l"
```
```   489   "divmod_abs (Neg k) (Pos l) = divmod k l"
```
```   490   "divmod_abs (Pos k) (Neg l) = divmod k l"
```
```   491   "divmod_abs j 0 = (0, \<bar>j\<bar>)"
```
```   492   "divmod_abs 0 j = (0, 0)"
```
```   493   by (simp_all add: prod_eq_iff)
```
```   494
```
```   495 lemma divmod_integer_code [code]:
```
```   496   "divmod_integer k l =
```
```   497     (if k = 0 then (0, 0) else if l = 0 then (0, k) else
```
```   498     (apsnd \<circ> times \<circ> sgn) l (if sgn k = sgn l
```
```   499       then divmod_abs k l
```
```   500       else (let (r, s) = divmod_abs k l in
```
```   501         if s = 0 then (- r, 0) else (- r - 1, \<bar>l\<bar> - s))))"
```
```   502 proof -
```
```   503   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"
```
```   504     by (auto simp add: sgn_if)
```
```   505   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
```
```   506   show ?thesis
```
```   507     by (simp add: prod_eq_iff integer_eq_iff case_prod_beta aux1)
```
```   508       (auto simp add: zdiv_zminus1_eq_if zmod_zminus1_eq_if div_minus_right mod_minus_right aux2)
```
```   509 qed
```
```   510
```
```   511 lemma div_integer_code [code]:
```
```   512   "k div l = fst (divmod_integer k l)"
```
```   513   by simp
```
```   514
```
```   515 lemma mod_integer_code [code]:
```
```   516   "k mod l = snd (divmod_integer k l)"
```
```   517   by simp
```
```   518
```
```   519 lemma equal_integer_code [code]:
```
```   520   "HOL.equal 0 (0::integer) \<longleftrightarrow> True"
```
```   521   "HOL.equal 0 (Pos l) \<longleftrightarrow> False"
```
```   522   "HOL.equal 0 (Neg l) \<longleftrightarrow> False"
```
```   523   "HOL.equal (Pos k) 0 \<longleftrightarrow> False"
```
```   524   "HOL.equal (Pos k) (Pos l) \<longleftrightarrow> HOL.equal k l"
```
```   525   "HOL.equal (Pos k) (Neg l) \<longleftrightarrow> False"
```
```   526   "HOL.equal (Neg k) 0 \<longleftrightarrow> False"
```
```   527   "HOL.equal (Neg k) (Pos l) \<longleftrightarrow> False"
```
```   528   "HOL.equal (Neg k) (Neg l) \<longleftrightarrow> HOL.equal k l"
```
```   529   by (simp_all add: equal)
```
```   530
```
```   531 lemma equal_integer_refl [code nbe]:
```
```   532   "HOL.equal (k::integer) k \<longleftrightarrow> True"
```
```   533   by (fact equal_refl)
```
```   534
```
```   535 lemma less_eq_integer_code [code]:
```
```   536   "0 \<le> (0::integer) \<longleftrightarrow> True"
```
```   537   "0 \<le> Pos l \<longleftrightarrow> True"
```
```   538   "0 \<le> Neg l \<longleftrightarrow> False"
```
```   539   "Pos k \<le> 0 \<longleftrightarrow> False"
```
```   540   "Pos k \<le> Pos l \<longleftrightarrow> k \<le> l"
```
```   541   "Pos k \<le> Neg l \<longleftrightarrow> False"
```
```   542   "Neg k \<le> 0 \<longleftrightarrow> True"
```
```   543   "Neg k \<le> Pos l \<longleftrightarrow> True"
```
```   544   "Neg k \<le> Neg l \<longleftrightarrow> l \<le> k"
```
```   545   by simp_all
```
```   546
```
```   547 lemma less_integer_code [code]:
```
```   548   "0 < (0::integer) \<longleftrightarrow> False"
```
```   549   "0 < Pos l \<longleftrightarrow> True"
```
```   550   "0 < Neg l \<longleftrightarrow> False"
```
```   551   "Pos k < 0 \<longleftrightarrow> False"
```
```   552   "Pos k < Pos l \<longleftrightarrow> k < l"
```
```   553   "Pos k < Neg l \<longleftrightarrow> False"
```
```   554   "Neg k < 0 \<longleftrightarrow> True"
```
```   555   "Neg k < Pos l \<longleftrightarrow> True"
```
```   556   "Neg k < Neg l \<longleftrightarrow> l < k"
```
```   557   by simp_all
```
```   558
```
```   559 lift_definition num_of_integer :: "integer \<Rightarrow> num"
```
```   560   is "num_of_nat \<circ> nat"
```
```   561   .
```
```   562
```
```   563 lemma num_of_integer_code [code]:
```
```   564   "num_of_integer k = (if k \<le> 1 then Num.One
```
```   565      else let
```
```   566        (l, j) = divmod_integer k 2;
```
```   567        l' = num_of_integer l;
```
```   568        l'' = l' + l'
```
```   569      in if j = 0 then l'' else l'' + Num.One)"
```
```   570 proof -
```
```   571   {
```
```   572     assume "int_of_integer k mod 2 = 1"
```
```   573     then have "nat (int_of_integer k mod 2) = nat 1" by simp
```
```   574     moreover assume *: "1 < int_of_integer k"
```
```   575     ultimately have **: "nat (int_of_integer k) mod 2 = 1" by (simp add: nat_mod_distrib)
```
```   576     have "num_of_nat (nat (int_of_integer k)) =
```
```   577       num_of_nat (2 * (nat (int_of_integer k) div 2) + nat (int_of_integer k) mod 2)"
```
```   578       by simp
```
```   579     then have "num_of_nat (nat (int_of_integer k)) =
```
```   580       num_of_nat (nat (int_of_integer k) div 2 + nat (int_of_integer k) div 2 + nat (int_of_integer k) mod 2)"
```
```   581       by (simp add: mult_2)
```
```   582     with ** have "num_of_nat (nat (int_of_integer k)) =
```
```   583       num_of_nat (nat (int_of_integer k) div 2 + nat (int_of_integer k) div 2 + 1)"
```
```   584       by simp
```
```   585   }
```
```   586   note aux = this
```
```   587   show ?thesis
```
```   588     by (auto simp add: num_of_integer_def nat_of_integer_def Let_def case_prod_beta
```
```   589       not_le integer_eq_iff less_eq_integer_def
```
```   590       nat_mult_distrib nat_div_distrib num_of_nat_One num_of_nat_plus_distrib
```
```   591        mult_2 [where 'a=nat] aux add_One)
```
```   592 qed
```
```   593
```
```   594 lemma nat_of_integer_code [code]:
```
```   595   "nat_of_integer k = (if k \<le> 0 then 0
```
```   596      else let
```
```   597        (l, j) = divmod_integer k 2;
```
```   598        l' = nat_of_integer l;
```
```   599        l'' = l' + l'
```
```   600      in if j = 0 then l'' else l'' + 1)"
```
```   601 proof -
```
```   602   obtain j where k: "k = integer_of_int j"
```
```   603   proof
```
```   604     show "k = integer_of_int (int_of_integer k)" by simp
```
```   605   qed
```
```   606   have *: "nat j mod 2 = nat_of_integer (of_int j mod 2)" if "j \<ge> 0"
```
```   607     using that by transfer (simp add: nat_mod_distrib)
```
```   608   from k show ?thesis
```
```   609     by (auto simp add: split_def Let_def nat_of_integer_def nat_div_distrib mult_2 [symmetric]
```
```   610       minus_mod_eq_mult_div [symmetric] *)
```
```   611 qed
```
```   612
```
```   613 lemma int_of_integer_code [code]:
```
```   614   "int_of_integer k = (if k < 0 then - (int_of_integer (- k))
```
```   615      else if k = 0 then 0
```
```   616      else let
```
```   617        (l, j) = divmod_integer k 2;
```
```   618        l' = 2 * int_of_integer l
```
```   619      in if j = 0 then l' else l' + 1)"
```
```   620   by (auto simp add: split_def Let_def integer_eq_iff minus_mod_eq_mult_div [symmetric])
```
```   621
```
```   622 lemma integer_of_int_code [code]:
```
```   623   "integer_of_int k = (if k < 0 then - (integer_of_int (- k))
```
```   624      else if k = 0 then 0
```
```   625      else let
```
```   626        l = 2 * integer_of_int (k div 2);
```
```   627        j = k mod 2
```
```   628      in if j = 0 then l else l + 1)"
```
```   629   by (auto simp add: split_def Let_def integer_eq_iff minus_mod_eq_mult_div [symmetric])
```
```   630
```
```   631 hide_const (open) Pos Neg sub dup divmod_abs
```
```   632
```
```   633
```
```   634 subsection \<open>Serializer setup for target language integers\<close>
```
```   635
```
```   636 code_reserved Eval int Integer abs
```
```   637
```
```   638 code_printing
```
```   639   type_constructor integer \<rightharpoonup>
```
```   640     (SML) "IntInf.int"
```
```   641     and (OCaml) "Big'_int.big'_int"
```
```   642     and (Haskell) "Integer"
```
```   643     and (Scala) "BigInt"
```
```   644     and (Eval) "int"
```
```   645 | class_instance integer :: equal \<rightharpoonup>
```
```   646     (Haskell) -
```
```   647
```
```   648 code_printing
```
```   649   constant "0::integer" \<rightharpoonup>
```
```   650     (SML) "!(0/ :/ IntInf.int)"
```
```   651     and (OCaml) "Big'_int.zero'_big'_int"
```
```   652     and (Haskell) "!(0/ ::/ Integer)"
```
```   653     and (Scala) "BigInt(0)"
```
```   654
```
```   655 setup \<open>
```
```   656   fold (fn target =>
```
```   657     Numeral.add_code @{const_name Code_Numeral.Pos} I Code_Printer.literal_numeral target
```
```   658     #> Numeral.add_code @{const_name Code_Numeral.Neg} (~) Code_Printer.literal_numeral target)
```
```   659     ["SML", "OCaml", "Haskell", "Scala"]
```
```   660 \<close>
```
```   661
```
```   662 code_printing
```
```   663   constant "plus :: integer \<Rightarrow> _ \<Rightarrow> _" \<rightharpoonup>
```
```   664     (SML) "IntInf.+ ((_), (_))"
```
```   665     and (OCaml) "Big'_int.add'_big'_int"
```
```   666     and (Haskell) infixl 6 "+"
```
```   667     and (Scala) infixl 7 "+"
```
```   668     and (Eval) infixl 8 "+"
```
```   669 | constant "uminus :: integer \<Rightarrow> _" \<rightharpoonup>
```
```   670     (SML) "IntInf.~"
```
```   671     and (OCaml) "Big'_int.minus'_big'_int"
```
```   672     and (Haskell) "negate"
```
```   673     and (Scala) "!(- _)"
```
```   674     and (Eval) "~/ _"
```
```   675 | constant "minus :: integer \<Rightarrow> _" \<rightharpoonup>
```
```   676     (SML) "IntInf.- ((_), (_))"
```
```   677     and (OCaml) "Big'_int.sub'_big'_int"
```
```   678     and (Haskell) infixl 6 "-"
```
```   679     and (Scala) infixl 7 "-"
```
```   680     and (Eval) infixl 8 "-"
```
```   681 | constant Code_Numeral.dup \<rightharpoonup>
```
```   682     (SML) "IntInf.*/ (2,/ (_))"
```
```   683     and (OCaml) "Big'_int.mult'_big'_int/ (Big'_int.big'_int'_of'_int/ 2)"
```
```   684     and (Haskell) "!(2 * _)"
```
```   685     and (Scala) "!(2 * _)"
```
```   686     and (Eval) "!(2 * _)"
```
```   687 | constant Code_Numeral.sub \<rightharpoonup>
```
```   688     (SML) "!(raise/ Fail/ \"sub\")"
```
```   689     and (OCaml) "failwith/ \"sub\""
```
```   690     and (Haskell) "error/ \"sub\""
```
```   691     and (Scala) "!sys.error(\"sub\")"
```
```   692 | constant "times :: integer \<Rightarrow> _ \<Rightarrow> _" \<rightharpoonup>
```
```   693     (SML) "IntInf.* ((_), (_))"
```
```   694     and (OCaml) "Big'_int.mult'_big'_int"
```
```   695     and (Haskell) infixl 7 "*"
```
```   696     and (Scala) infixl 8 "*"
```
```   697     and (Eval) infixl 9 "*"
```
```   698 | constant Code_Numeral.divmod_abs \<rightharpoonup>
```
```   699     (SML) "IntInf.divMod/ (IntInf.abs _,/ IntInf.abs _)"
```
```   700     and (OCaml) "Big'_int.quomod'_big'_int/ (Big'_int.abs'_big'_int _)/ (Big'_int.abs'_big'_int _)"
```
```   701     and (Haskell) "divMod/ (abs _)/ (abs _)"
```
```   702     and (Scala) "!((k: BigInt) => (l: BigInt) =>/ if (l == 0)/ (BigInt(0), k) else/ (k.abs '/% l.abs))"
```
```   703     and (Eval) "Integer.div'_mod/ (abs _)/ (abs _)"
```
```   704 | constant "HOL.equal :: integer \<Rightarrow> _ \<Rightarrow> bool" \<rightharpoonup>
```
```   705     (SML) "!((_ : IntInf.int) = _)"
```
```   706     and (OCaml) "Big'_int.eq'_big'_int"
```
```   707     and (Haskell) infix 4 "=="
```
```   708     and (Scala) infixl 5 "=="
```
```   709     and (Eval) infixl 6 "="
```
```   710 | constant "less_eq :: integer \<Rightarrow> _ \<Rightarrow> bool" \<rightharpoonup>
```
```   711     (SML) "IntInf.<= ((_), (_))"
```
```   712     and (OCaml) "Big'_int.le'_big'_int"
```
```   713     and (Haskell) infix 4 "<="
```
```   714     and (Scala) infixl 4 "<="
```
```   715     and (Eval) infixl 6 "<="
```
```   716 | constant "less :: integer \<Rightarrow> _ \<Rightarrow> bool" \<rightharpoonup>
```
```   717     (SML) "IntInf.< ((_), (_))"
```
```   718     and (OCaml) "Big'_int.lt'_big'_int"
```
```   719     and (Haskell) infix 4 "<"
```
```   720     and (Scala) infixl 4 "<"
```
```   721     and (Eval) infixl 6 "<"
```
```   722 | constant "abs :: integer \<Rightarrow> _" \<rightharpoonup>
```
```   723     (SML) "IntInf.abs"
```
```   724     and (OCaml) "Big'_int.abs'_big'_int"
```
```   725     and (Haskell) "Prelude.abs"
```
```   726     and (Scala) "_.abs"
```
```   727     and (Eval) "abs"
```
```   728
```
```   729 code_identifier
```
```   730   code_module Code_Numeral \<rightharpoonup> (SML) Arith and (OCaml) Arith and (Haskell) Arith
```
```   731
```
```   732
```
```   733 subsection \<open>Type of target language naturals\<close>
```
```   734
```
```   735 typedef natural = "UNIV :: nat set"
```
```   736   morphisms nat_of_natural natural_of_nat ..
```
```   737
```
```   738 setup_lifting type_definition_natural
```
```   739
```
```   740 lemma natural_eq_iff [termination_simp]:
```
```   741   "m = n \<longleftrightarrow> nat_of_natural m = nat_of_natural n"
```
```   742   by transfer rule
```
```   743
```
```   744 lemma natural_eqI:
```
```   745   "nat_of_natural m = nat_of_natural n \<Longrightarrow> m = n"
```
```   746   using natural_eq_iff [of m n] by simp
```
```   747
```
```   748 lemma nat_of_natural_of_nat_inverse [simp]:
```
```   749   "nat_of_natural (natural_of_nat n) = n"
```
```   750   by transfer rule
```
```   751
```
```   752 lemma natural_of_nat_of_natural_inverse [simp]:
```
```   753   "natural_of_nat (nat_of_natural n) = n"
```
```   754   by transfer rule
```
```   755
```
```   756 instantiation natural :: "{comm_monoid_diff, semiring_1}"
```
```   757 begin
```
```   758
```
```   759 lift_definition zero_natural :: natural
```
```   760   is "0 :: nat"
```
```   761   .
```
```   762
```
```   763 declare zero_natural.rep_eq [simp]
```
```   764
```
```   765 lift_definition one_natural :: natural
```
```   766   is "1 :: nat"
```
```   767   .
```
```   768
```
```   769 declare one_natural.rep_eq [simp]
```
```   770
```
```   771 lift_definition plus_natural :: "natural \<Rightarrow> natural \<Rightarrow> natural"
```
```   772   is "plus :: nat \<Rightarrow> nat \<Rightarrow> nat"
```
```   773   .
```
```   774
```
```   775 declare plus_natural.rep_eq [simp]
```
```   776
```
```   777 lift_definition minus_natural :: "natural \<Rightarrow> natural \<Rightarrow> natural"
```
```   778   is "minus :: nat \<Rightarrow> nat \<Rightarrow> nat"
```
```   779   .
```
```   780
```
```   781 declare minus_natural.rep_eq [simp]
```
```   782
```
```   783 lift_definition times_natural :: "natural \<Rightarrow> natural \<Rightarrow> natural"
```
```   784   is "times :: nat \<Rightarrow> nat \<Rightarrow> nat"
```
```   785   .
```
```   786
```
```   787 declare times_natural.rep_eq [simp]
```
```   788
```
```   789 instance proof
```
```   790 qed (transfer, simp add: algebra_simps)+
```
```   791
```
```   792 end
```
```   793
```
```   794 instance natural :: Rings.dvd ..
```
```   795
```
```   796 lemma [transfer_rule]:
```
```   797   "rel_fun pcr_natural (rel_fun pcr_natural HOL.iff) Rings.dvd Rings.dvd"
```
```   798   unfolding dvd_def by transfer_prover
```
```   799
```
```   800 lemma [transfer_rule]:
```
```   801   "rel_fun (=) pcr_natural (of_bool :: bool \<Rightarrow> nat) (of_bool :: bool \<Rightarrow> natural)"
```
```   802   by (unfold of_bool_def [abs_def]) transfer_prover
```
```   803
```
```   804 lemma [transfer_rule]:
```
```   805   "rel_fun HOL.eq pcr_natural (\<lambda>n::nat. n) (of_nat :: nat \<Rightarrow> natural)"
```
```   806 proof -
```
```   807   have "rel_fun HOL.eq pcr_natural (of_nat :: nat \<Rightarrow> nat) (of_nat :: nat \<Rightarrow> natural)"
```
```   808     by (unfold of_nat_def [abs_def]) transfer_prover
```
```   809   then show ?thesis by (simp add: id_def)
```
```   810 qed
```
```   811
```
```   812 lemma [transfer_rule]:
```
```   813   "rel_fun HOL.eq pcr_natural (numeral :: num \<Rightarrow> nat) (numeral :: num \<Rightarrow> natural)"
```
```   814 proof -
```
```   815   have "rel_fun HOL.eq pcr_natural (numeral :: num \<Rightarrow> nat) (\<lambda>n. of_nat (numeral n))"
```
```   816     by transfer_prover
```
```   817   then show ?thesis by simp
```
```   818 qed
```
```   819
```
```   820 lemma [transfer_rule]:
```
```   821   "rel_fun pcr_natural (rel_fun (=) pcr_natural) (power :: _ \<Rightarrow> _ \<Rightarrow> nat) (power :: _ \<Rightarrow> _ \<Rightarrow> natural)"
```
```   822   by (unfold power_def [abs_def]) transfer_prover
```
```   823
```
```   824 lemma nat_of_natural_of_nat [simp]:
```
```   825   "nat_of_natural (of_nat n) = n"
```
```   826   by transfer rule
```
```   827
```
```   828 lemma natural_of_nat_of_nat [simp, code_abbrev]:
```
```   829   "natural_of_nat = of_nat"
```
```   830   by transfer rule
```
```   831
```
```   832 lemma of_nat_of_natural [simp]:
```
```   833   "of_nat (nat_of_natural n) = n"
```
```   834   by transfer rule
```
```   835
```
```   836 lemma nat_of_natural_numeral [simp]:
```
```   837   "nat_of_natural (numeral k) = numeral k"
```
```   838   by transfer rule
```
```   839
```
```   840 instantiation natural :: "{linordered_semiring, equal}"
```
```   841 begin
```
```   842
```
```   843 lift_definition less_eq_natural :: "natural \<Rightarrow> natural \<Rightarrow> bool"
```
```   844   is "less_eq :: nat \<Rightarrow> nat \<Rightarrow> bool"
```
```   845   .
```
```   846
```
```   847 declare less_eq_natural.rep_eq [termination_simp]
```
```   848
```
```   849 lift_definition less_natural :: "natural \<Rightarrow> natural \<Rightarrow> bool"
```
```   850   is "less :: nat \<Rightarrow> nat \<Rightarrow> bool"
```
```   851   .
```
```   852
```
```   853 declare less_natural.rep_eq [termination_simp]
```
```   854
```
```   855 lift_definition equal_natural :: "natural \<Rightarrow> natural \<Rightarrow> bool"
```
```   856   is "HOL.equal :: nat \<Rightarrow> nat \<Rightarrow> bool"
```
```   857   .
```
```   858
```
```   859 instance proof
```
```   860 qed (transfer, simp add: algebra_simps equal less_le_not_le [symmetric] linear)+
```
```   861
```
```   862 end
```
```   863
```
```   864 lemma [transfer_rule]:
```
```   865   "rel_fun pcr_natural (rel_fun pcr_natural pcr_natural) (min :: _ \<Rightarrow> _ \<Rightarrow> nat) (min :: _ \<Rightarrow> _ \<Rightarrow> natural)"
```
```   866   by (unfold min_def [abs_def]) transfer_prover
```
```   867
```
```   868 lemma [transfer_rule]:
```
```   869   "rel_fun pcr_natural (rel_fun pcr_natural pcr_natural) (max :: _ \<Rightarrow> _ \<Rightarrow> nat) (max :: _ \<Rightarrow> _ \<Rightarrow> natural)"
```
```   870   by (unfold max_def [abs_def]) transfer_prover
```
```   871
```
```   872 lemma nat_of_natural_min [simp]:
```
```   873   "nat_of_natural (min k l) = min (nat_of_natural k) (nat_of_natural l)"
```
```   874   by transfer rule
```
```   875
```
```   876 lemma nat_of_natural_max [simp]:
```
```   877   "nat_of_natural (max k l) = max (nat_of_natural k) (nat_of_natural l)"
```
```   878   by transfer rule
```
```   879
```
```   880 instantiation natural :: unique_euclidean_semiring
```
```   881 begin
```
```   882
```
```   883 lift_definition divide_natural :: "natural \<Rightarrow> natural \<Rightarrow> natural"
```
```   884   is "divide :: nat \<Rightarrow> nat \<Rightarrow> nat"
```
```   885   .
```
```   886
```
```   887 declare divide_natural.rep_eq [simp]
```
```   888
```
```   889 lift_definition modulo_natural :: "natural \<Rightarrow> natural \<Rightarrow> natural"
```
```   890   is "modulo :: nat \<Rightarrow> nat \<Rightarrow> nat"
```
```   891   .
```
```   892
```
```   893 declare modulo_natural.rep_eq [simp]
```
```   894
```
```   895 lift_definition euclidean_size_natural :: "natural \<Rightarrow> nat"
```
```   896   is "euclidean_size :: nat \<Rightarrow> nat"
```
```   897   .
```
```   898
```
```   899 declare euclidean_size_natural.rep_eq [simp]
```
```   900
```
```   901 lift_definition division_segment_natural :: "natural \<Rightarrow> natural"
```
```   902   is "division_segment :: nat \<Rightarrow> nat"
```
```   903   .
```
```   904
```
```   905 declare division_segment_natural.rep_eq [simp]
```
```   906
```
```   907 instance
```
```   908   by (standard; transfer)
```
```   909     (auto simp add: algebra_simps unit_factor_nat_def gr0_conv_Suc)
```
```   910
```
```   911 end
```
```   912
```
```   913 lemma [code]:
```
```   914   "euclidean_size = nat_of_natural"
```
```   915   by (simp add: fun_eq_iff)
```
```   916
```
```   917 lemma [code]:
```
```   918   "division_segment (n::natural) = 1"
```
```   919   by (simp add: natural_eq_iff)
```
```   920
```
```   921 instance natural :: linordered_semidom
```
```   922   by (standard; transfer) simp_all
```
```   923
```
```   924 instance natural :: semiring_parity
```
```   925   by (standard; transfer) simp_all
```
```   926
```
```   927 lemma [transfer_rule]:
```
```   928   "rel_fun (=) (rel_fun pcr_natural pcr_natural) (push_bit :: _ \<Rightarrow> _ \<Rightarrow> nat) (push_bit :: _ \<Rightarrow> _ \<Rightarrow> natural)"
```
```   929   by (unfold push_bit_eq_mult [abs_def]) transfer_prover
```
```   930
```
```   931 lemma [transfer_rule]:
```
```   932   "rel_fun (=) (rel_fun pcr_natural pcr_natural) (take_bit :: _ \<Rightarrow> _ \<Rightarrow> nat) (take_bit :: _ \<Rightarrow> _ \<Rightarrow> natural)"
```
```   933   by (unfold take_bit_eq_mod [abs_def]) transfer_prover
```
```   934
```
```   935 lemma [transfer_rule]:
```
```   936   "rel_fun (=) (rel_fun pcr_natural pcr_natural) (drop_bit :: _ \<Rightarrow> _ \<Rightarrow> nat) (drop_bit :: _ \<Rightarrow> _ \<Rightarrow> natural)"
```
```   937   by (unfold drop_bit_eq_div [abs_def]) transfer_prover
```
```   938
```
```   939 lift_definition natural_of_integer :: "integer \<Rightarrow> natural"
```
```   940   is "nat :: int \<Rightarrow> nat"
```
```   941   .
```
```   942
```
```   943 lift_definition integer_of_natural :: "natural \<Rightarrow> integer"
```
```   944   is "of_nat :: nat \<Rightarrow> int"
```
```   945   .
```
```   946
```
```   947 lemma natural_of_integer_of_natural [simp]:
```
```   948   "natural_of_integer (integer_of_natural n) = n"
```
```   949   by transfer simp
```
```   950
```
```   951 lemma integer_of_natural_of_integer [simp]:
```
```   952   "integer_of_natural (natural_of_integer k) = max 0 k"
```
```   953   by transfer auto
```
```   954
```
```   955 lemma int_of_integer_of_natural [simp]:
```
```   956   "int_of_integer (integer_of_natural n) = of_nat (nat_of_natural n)"
```
```   957   by transfer rule
```
```   958
```
```   959 lemma integer_of_natural_of_nat [simp]:
```
```   960   "integer_of_natural (of_nat n) = of_nat n"
```
```   961   by transfer rule
```
```   962
```
```   963 lemma [measure_function]:
```
```   964   "is_measure nat_of_natural"
```
```   965   by (rule is_measure_trivial)
```
```   966
```
```   967
```
```   968 subsection \<open>Inductive representation of target language naturals\<close>
```
```   969
```
```   970 lift_definition Suc :: "natural \<Rightarrow> natural"
```
```   971   is Nat.Suc
```
```   972   .
```
```   973
```
```   974 declare Suc.rep_eq [simp]
```
```   975
```
```   976 old_rep_datatype "0::natural" Suc
```
```   977   by (transfer, fact nat.induct nat.inject nat.distinct)+
```
```   978
```
```   979 lemma natural_cases [case_names nat, cases type: natural]:
```
```   980   fixes m :: natural
```
```   981   assumes "\<And>n. m = of_nat n \<Longrightarrow> P"
```
```   982   shows P
```
```   983   using assms by transfer blast
```
```   984
```
```   985 instantiation natural :: size
```
```   986 begin
```
```   987
```
```   988 definition size_nat where [simp, code]: "size_nat = nat_of_natural"
```
```   989
```
```   990 instance ..
```
```   991
```
```   992 end
```
```   993
```
```   994 lemma natural_decr [termination_simp]:
```
```   995   "n \<noteq> 0 \<Longrightarrow> nat_of_natural n - Nat.Suc 0 < nat_of_natural n"
```
```   996   by transfer simp
```
```   997
```
```   998 lemma natural_zero_minus_one: "(0::natural) - 1 = 0"
```
```   999   by (rule zero_diff)
```
```  1000
```
```  1001 lemma Suc_natural_minus_one: "Suc n - 1 = n"
```
```  1002   by transfer simp
```
```  1003
```
```  1004 hide_const (open) Suc
```
```  1005
```
```  1006
```
```  1007 subsection \<open>Code refinement for target language naturals\<close>
```
```  1008
```
```  1009 lift_definition Nat :: "integer \<Rightarrow> natural"
```
```  1010   is nat
```
```  1011   .
```
```  1012
```
```  1013 lemma [code_post]:
```
```  1014   "Nat 0 = 0"
```
```  1015   "Nat 1 = 1"
```
```  1016   "Nat (numeral k) = numeral k"
```
```  1017   by (transfer, simp)+
```
```  1018
```
```  1019 lemma [code abstype]:
```
```  1020   "Nat (integer_of_natural n) = n"
```
```  1021   by transfer simp
```
```  1022
```
```  1023 lemma [code]:
```
```  1024   "natural_of_nat n = natural_of_integer (integer_of_nat n)"
```
```  1025   by transfer simp
```
```  1026
```
```  1027 lemma [code abstract]:
```
```  1028   "integer_of_natural (natural_of_integer k) = max 0 k"
```
```  1029   by simp
```
```  1030
```
```  1031 lemma [code_abbrev]:
```
```  1032   "natural_of_integer (Code_Numeral.Pos k) = numeral k"
```
```  1033   by transfer simp
```
```  1034
```
```  1035 lemma [code abstract]:
```
```  1036   "integer_of_natural 0 = 0"
```
```  1037   by transfer simp
```
```  1038
```
```  1039 lemma [code abstract]:
```
```  1040   "integer_of_natural 1 = 1"
```
```  1041   by transfer simp
```
```  1042
```
```  1043 lemma [code abstract]:
```
```  1044   "integer_of_natural (Code_Numeral.Suc n) = integer_of_natural n + 1"
```
```  1045   by transfer simp
```
```  1046
```
```  1047 lemma [code]:
```
```  1048   "nat_of_natural = nat_of_integer \<circ> integer_of_natural"
```
```  1049   by transfer (simp add: fun_eq_iff)
```
```  1050
```
```  1051 lemma [code, code_unfold]:
```
```  1052   "case_natural f g n = (if n = 0 then f else g (n - 1))"
```
```  1053   by (cases n rule: natural.exhaust) (simp_all, simp add: Suc_def)
```
```  1054
```
```  1055 declare natural.rec [code del]
```
```  1056
```
```  1057 lemma [code abstract]:
```
```  1058   "integer_of_natural (m + n) = integer_of_natural m + integer_of_natural n"
```
```  1059   by transfer simp
```
```  1060
```
```  1061 lemma [code abstract]:
```
```  1062   "integer_of_natural (m - n) = max 0 (integer_of_natural m - integer_of_natural n)"
```
```  1063   by transfer simp
```
```  1064
```
```  1065 lemma [code abstract]:
```
```  1066   "integer_of_natural (m * n) = integer_of_natural m * integer_of_natural n"
```
```  1067   by transfer simp
```
```  1068
```
```  1069 lemma [code abstract]:
```
```  1070   "integer_of_natural (m div n) = integer_of_natural m div integer_of_natural n"
```
```  1071   by transfer (simp add: zdiv_int)
```
```  1072
```
```  1073 lemma [code abstract]:
```
```  1074   "integer_of_natural (m mod n) = integer_of_natural m mod integer_of_natural n"
```
```  1075   by transfer (simp add: zmod_int)
```
```  1076
```
```  1077 lemma [code]:
```
```  1078   "HOL.equal m n \<longleftrightarrow> HOL.equal (integer_of_natural m) (integer_of_natural n)"
```
```  1079   by transfer (simp add: equal)
```
```  1080
```
```  1081 lemma [code nbe]: "HOL.equal n (n::natural) \<longleftrightarrow> True"
```
```  1082   by (rule equal_class.equal_refl)
```
```  1083
```
```  1084 lemma [code]: "m \<le> n \<longleftrightarrow> integer_of_natural m \<le> integer_of_natural n"
```
```  1085   by transfer simp
```
```  1086
```
```  1087 lemma [code]: "m < n \<longleftrightarrow> integer_of_natural m < integer_of_natural n"
```
```  1088   by transfer simp
```
```  1089
```
```  1090 hide_const (open) Nat
```
```  1091
```
```  1092 lifting_update integer.lifting
```
```  1093 lifting_forget integer.lifting
```
```  1094
```
```  1095 lifting_update natural.lifting
```
```  1096 lifting_forget natural.lifting
```
```  1097
```
```  1098 code_reflect Code_Numeral
```
```  1099   datatypes natural
```
```  1100   functions "Code_Numeral.Suc" "0 :: natural" "1 :: natural"
```
```  1101     "plus :: natural \<Rightarrow> _" "minus :: natural \<Rightarrow> _"
```
```  1102     "times :: natural \<Rightarrow> _" "divide :: natural \<Rightarrow> _"
```
```  1103     "modulo :: natural \<Rightarrow> _"
```
```  1104     integer_of_natural natural_of_integer
```
```  1105
```
```  1106 end
```