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