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