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