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