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