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