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