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