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