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