src/HOL/Code_Numeral.thy
author kuncar
Fri Mar 08 13:21:06 2013 +0100 (2013-03-08)
changeset 51375 d9e62d9c98de
parent 51143 0a2371e7ced3
child 52435 6646bb548c6b
permissions -rw-r--r--
patch Isabelle ditribution to conform to changes regarding the parametricity
     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 
   227 subsection {* Code theorems for target language integers *}
   228 
   229 text {* Constructors *}
   230 
   231 definition Pos :: "num \<Rightarrow> integer"
   232 where
   233   [simp, code_abbrev]: "Pos = numeral"
   234 
   235 lemma [transfer_rule]:
   236   "fun_rel HOL.eq pcr_integer numeral Pos"
   237   by simp transfer_prover
   238 
   239 definition Neg :: "num \<Rightarrow> integer"
   240 where
   241   [simp, code_abbrev]: "Neg = neg_numeral"
   242 
   243 lemma [transfer_rule]:
   244   "fun_rel HOL.eq pcr_integer neg_numeral Neg"
   245   by simp transfer_prover
   246 
   247 code_datatype "0::integer" Pos Neg
   248 
   249 
   250 text {* Auxiliary operations *}
   251 
   252 lift_definition dup :: "integer \<Rightarrow> integer"
   253   is "\<lambda>k::int. k + k"
   254   .
   255 
   256 lemma dup_code [code]:
   257   "dup 0 = 0"
   258   "dup (Pos n) = Pos (Num.Bit0 n)"
   259   "dup (Neg n) = Neg (Num.Bit0 n)"
   260   by (transfer, simp only: neg_numeral_def numeral_Bit0 minus_add_distrib)+
   261 
   262 lift_definition sub :: "num \<Rightarrow> num \<Rightarrow> integer"
   263   is "\<lambda>m n. numeral m - numeral n :: int"
   264   .
   265 
   266 lemma sub_code [code]:
   267   "sub Num.One Num.One = 0"
   268   "sub (Num.Bit0 m) Num.One = Pos (Num.BitM m)"
   269   "sub (Num.Bit1 m) Num.One = Pos (Num.Bit0 m)"
   270   "sub Num.One (Num.Bit0 n) = Neg (Num.BitM n)"
   271   "sub Num.One (Num.Bit1 n) = Neg (Num.Bit0 n)"
   272   "sub (Num.Bit0 m) (Num.Bit0 n) = dup (sub m n)"
   273   "sub (Num.Bit1 m) (Num.Bit1 n) = dup (sub m n)"
   274   "sub (Num.Bit1 m) (Num.Bit0 n) = dup (sub m n) + 1"
   275   "sub (Num.Bit0 m) (Num.Bit1 n) = dup (sub m n) - 1"
   276   by (transfer, simp add: dbl_def dbl_inc_def dbl_dec_def)+
   277 
   278 
   279 text {* Implementations *}
   280 
   281 lemma one_integer_code [code, code_unfold]:
   282   "1 = Pos Num.One"
   283   by simp
   284 
   285 lemma plus_integer_code [code]:
   286   "k + 0 = (k::integer)"
   287   "0 + l = (l::integer)"
   288   "Pos m + Pos n = Pos (m + n)"
   289   "Pos m + Neg n = sub m n"
   290   "Neg m + Pos n = sub n m"
   291   "Neg m + Neg n = Neg (m + n)"
   292   by (transfer, simp)+
   293 
   294 lemma uminus_integer_code [code]:
   295   "uminus 0 = (0::integer)"
   296   "uminus (Pos m) = Neg m"
   297   "uminus (Neg m) = Pos m"
   298   by simp_all
   299 
   300 lemma minus_integer_code [code]:
   301   "k - 0 = (k::integer)"
   302   "0 - l = uminus (l::integer)"
   303   "Pos m - Pos n = sub m n"
   304   "Pos m - Neg n = Pos (m + n)"
   305   "Neg m - Pos n = Neg (m + n)"
   306   "Neg m - Neg n = sub n m"
   307   by (transfer, simp)+
   308 
   309 lemma abs_integer_code [code]:
   310   "\<bar>k\<bar> = (if (k::integer) < 0 then - k else k)"
   311   by simp
   312 
   313 lemma sgn_integer_code [code]:
   314   "sgn k = (if k = 0 then 0 else if (k::integer) < 0 then - 1 else 1)"
   315   by simp
   316 
   317 lemma times_integer_code [code]:
   318   "k * 0 = (0::integer)"
   319   "0 * l = (0::integer)"
   320   "Pos m * Pos n = Pos (m * n)"
   321   "Pos m * Neg n = Neg (m * n)"
   322   "Neg m * Pos n = Neg (m * n)"
   323   "Neg m * Neg n = Pos (m * n)"
   324   by simp_all
   325 
   326 definition divmod_integer :: "integer \<Rightarrow> integer \<Rightarrow> integer \<times> integer"
   327 where
   328   "divmod_integer k l = (k div l, k mod l)"
   329 
   330 lemma fst_divmod [simp]:
   331   "fst (divmod_integer k l) = k div l"
   332   by (simp add: divmod_integer_def)
   333 
   334 lemma snd_divmod [simp]:
   335   "snd (divmod_integer k l) = k mod l"
   336   by (simp add: divmod_integer_def)
   337 
   338 definition divmod_abs :: "integer \<Rightarrow> integer \<Rightarrow> integer \<times> integer"
   339 where
   340   "divmod_abs k l = (\<bar>k\<bar> div \<bar>l\<bar>, \<bar>k\<bar> mod \<bar>l\<bar>)"
   341 
   342 lemma fst_divmod_abs [simp]:
   343   "fst (divmod_abs k l) = \<bar>k\<bar> div \<bar>l\<bar>"
   344   by (simp add: divmod_abs_def)
   345 
   346 lemma snd_divmod_abs [simp]:
   347   "snd (divmod_abs k l) = \<bar>k\<bar> mod \<bar>l\<bar>"
   348   by (simp add: divmod_abs_def)
   349 
   350 lemma divmod_abs_terminate_code [code]:
   351   "divmod_abs (Neg k) (Neg l) = divmod_abs (Pos k) (Pos l)"
   352   "divmod_abs (Neg k) (Pos l) = divmod_abs (Pos k) (Pos l)"
   353   "divmod_abs (Pos k) (Neg l) = divmod_abs (Pos k) (Pos l)"
   354   "divmod_abs j 0 = (0, \<bar>j\<bar>)"
   355   "divmod_abs 0 j = (0, 0)"
   356   by (simp_all add: prod_eq_iff)
   357 
   358 lemma divmod_abs_rec_code [code]:
   359   "divmod_abs (Pos k) (Pos l) =
   360     (let j = sub k l in
   361        if j < 0 then (0, Pos k)
   362        else let (q, r) = divmod_abs j (Pos l) in (q + 1, r))"
   363   apply (simp add: prod_eq_iff Let_def prod_case_beta)
   364   apply transfer
   365   apply (simp add: sub_non_negative sub_negative div_pos_pos_trivial mod_pos_pos_trivial div_pos_geq mod_pos_geq)
   366   done
   367 
   368 lemma divmod_integer_code [code]:
   369   "divmod_integer k l =
   370     (if k = 0 then (0, 0) else if l = 0 then (0, k) else
   371     (apsnd \<circ> times \<circ> sgn) l (if sgn k = sgn l
   372       then divmod_abs k l
   373       else (let (r, s) = divmod_abs k l in
   374         if s = 0 then (- r, 0) else (- r - 1, \<bar>l\<bar> - s))))"
   375 proof -
   376   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"
   377     by (auto simp add: sgn_if)
   378   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
   379   show ?thesis
   380     by (simp add: prod_eq_iff integer_eq_iff prod_case_beta aux1)
   381       (auto simp add: zdiv_zminus1_eq_if zmod_zminus1_eq_if div_minus_right mod_minus_right aux2)
   382 qed
   383 
   384 lemma div_integer_code [code]:
   385   "k div l = fst (divmod_integer k l)"
   386   by simp
   387 
   388 lemma mod_integer_code [code]:
   389   "k mod l = snd (divmod_integer k l)"
   390   by simp
   391 
   392 lemma equal_integer_code [code]:
   393   "HOL.equal 0 (0::integer) \<longleftrightarrow> True"
   394   "HOL.equal 0 (Pos l) \<longleftrightarrow> False"
   395   "HOL.equal 0 (Neg l) \<longleftrightarrow> False"
   396   "HOL.equal (Pos k) 0 \<longleftrightarrow> False"
   397   "HOL.equal (Pos k) (Pos l) \<longleftrightarrow> HOL.equal k l"
   398   "HOL.equal (Pos k) (Neg l) \<longleftrightarrow> False"
   399   "HOL.equal (Neg k) 0 \<longleftrightarrow> False"
   400   "HOL.equal (Neg k) (Pos l) \<longleftrightarrow> False"
   401   "HOL.equal (Neg k) (Neg l) \<longleftrightarrow> HOL.equal k l"
   402   by (simp_all add: equal)
   403 
   404 lemma equal_integer_refl [code nbe]:
   405   "HOL.equal (k::integer) k \<longleftrightarrow> True"
   406   by (fact equal_refl)
   407 
   408 lemma less_eq_integer_code [code]:
   409   "0 \<le> (0::integer) \<longleftrightarrow> True"
   410   "0 \<le> Pos l \<longleftrightarrow> True"
   411   "0 \<le> Neg l \<longleftrightarrow> False"
   412   "Pos k \<le> 0 \<longleftrightarrow> False"
   413   "Pos k \<le> Pos l \<longleftrightarrow> k \<le> l"
   414   "Pos k \<le> Neg l \<longleftrightarrow> False"
   415   "Neg k \<le> 0 \<longleftrightarrow> True"
   416   "Neg k \<le> Pos l \<longleftrightarrow> True"
   417   "Neg k \<le> Neg l \<longleftrightarrow> l \<le> k"
   418   by simp_all
   419 
   420 lemma less_integer_code [code]:
   421   "0 < (0::integer) \<longleftrightarrow> False"
   422   "0 < Pos l \<longleftrightarrow> True"
   423   "0 < Neg l \<longleftrightarrow> False"
   424   "Pos k < 0 \<longleftrightarrow> False"
   425   "Pos k < Pos l \<longleftrightarrow> k < l"
   426   "Pos k < Neg l \<longleftrightarrow> False"
   427   "Neg k < 0 \<longleftrightarrow> True"
   428   "Neg k < Pos l \<longleftrightarrow> True"
   429   "Neg k < Neg l \<longleftrightarrow> l < k"
   430   by simp_all
   431 
   432 lift_definition integer_of_num :: "num \<Rightarrow> integer"
   433   is "numeral :: num \<Rightarrow> int"
   434   .
   435 
   436 lemma integer_of_num [code]:
   437   "integer_of_num num.One = 1"
   438   "integer_of_num (num.Bit0 n) = (let k = integer_of_num n in k + k)"
   439   "integer_of_num (num.Bit1 n) = (let k = integer_of_num n in k + k + 1)"
   440   by (transfer, simp only: numeral.simps Let_def)+
   441 
   442 lift_definition num_of_integer :: "integer \<Rightarrow> num"
   443   is "num_of_nat \<circ> nat"
   444   .
   445 
   446 lemma num_of_integer_code [code]:
   447   "num_of_integer k = (if k \<le> 1 then Num.One
   448      else let
   449        (l, j) = divmod_integer k 2;
   450        l' = num_of_integer l;
   451        l'' = l' + l'
   452      in if j = 0 then l'' else l'' + Num.One)"
   453 proof -
   454   {
   455     assume "int_of_integer k mod 2 = 1"
   456     then have "nat (int_of_integer k mod 2) = nat 1" by simp
   457     moreover assume *: "1 < int_of_integer k"
   458     ultimately have **: "nat (int_of_integer k) mod 2 = 1" by (simp add: nat_mod_distrib)
   459     have "num_of_nat (nat (int_of_integer k)) =
   460       num_of_nat (2 * (nat (int_of_integer k) div 2) + nat (int_of_integer k) mod 2)"
   461       by simp
   462     then have "num_of_nat (nat (int_of_integer k)) =
   463       num_of_nat (nat (int_of_integer k) div 2 + nat (int_of_integer k) div 2 + nat (int_of_integer k) mod 2)"
   464       by (simp add: mult_2)
   465     with ** have "num_of_nat (nat (int_of_integer k)) =
   466       num_of_nat (nat (int_of_integer k) div 2 + nat (int_of_integer k) div 2 + 1)"
   467       by simp
   468   }
   469   note aux = this
   470   show ?thesis
   471     by (auto simp add: num_of_integer_def nat_of_integer_def Let_def prod_case_beta
   472       not_le integer_eq_iff less_eq_integer_def
   473       nat_mult_distrib nat_div_distrib num_of_nat_One num_of_nat_plus_distrib
   474        mult_2 [where 'a=nat] aux add_One)
   475 qed
   476 
   477 lemma nat_of_integer_code [code]:
   478   "nat_of_integer k = (if k \<le> 0 then 0
   479      else let
   480        (l, j) = divmod_integer k 2;
   481        l' = nat_of_integer l;
   482        l'' = l' + l'
   483      in if j = 0 then l'' else l'' + 1)"
   484 proof -
   485   obtain j where "k = integer_of_int j"
   486   proof
   487     show "k = integer_of_int (int_of_integer k)" by simp
   488   qed
   489   moreover have "2 * (j div 2) = j - j mod 2"
   490     by (simp add: zmult_div_cancel mult_commute)
   491   ultimately show ?thesis
   492     by (auto simp add: split_def Let_def mod_integer_def nat_of_integer_def not_le
   493       nat_add_distrib [symmetric] Suc_nat_eq_nat_zadd1)
   494       (auto simp add: mult_2 [symmetric])
   495 qed
   496 
   497 lemma int_of_integer_code [code]:
   498   "int_of_integer k = (if k < 0 then - (int_of_integer (- k))
   499      else if k = 0 then 0
   500      else let
   501        (l, j) = divmod_integer k 2;
   502        l' = 2 * int_of_integer l
   503      in if j = 0 then l' else l' + 1)"
   504   by (auto simp add: split_def Let_def integer_eq_iff zmult_div_cancel)
   505 
   506 lemma integer_of_int_code [code]:
   507   "integer_of_int k = (if k < 0 then - (integer_of_int (- k))
   508      else if k = 0 then 0
   509      else let
   510        (l, j) = divmod_int k 2;
   511        l' = 2 * integer_of_int l
   512      in if j = 0 then l' else l' + 1)"
   513   by (auto simp add: split_def Let_def integer_eq_iff zmult_div_cancel)
   514 
   515 hide_const (open) Pos Neg sub dup divmod_abs
   516 
   517 
   518 subsection {* Serializer setup for target language integers *}
   519 
   520 code_reserved Eval int Integer abs
   521 
   522 code_type integer
   523   (SML "IntInf.int")
   524   (OCaml "Big'_int.big'_int")
   525   (Haskell "Integer")
   526   (Scala "BigInt")
   527   (Eval "int")
   528 
   529 code_instance integer :: equal
   530   (Haskell -)
   531 
   532 code_const "0::integer"
   533   (SML "0")
   534   (OCaml "Big'_int.zero'_big'_int")
   535   (Haskell "0")
   536   (Scala "BigInt(0)")
   537 
   538 setup {*
   539   fold (Numeral.add_code @{const_name Code_Numeral.Pos}
   540     false Code_Printer.literal_numeral) ["SML", "OCaml", "Haskell", "Scala"]
   541 *}
   542 
   543 setup {*
   544   fold (Numeral.add_code @{const_name Code_Numeral.Neg}
   545     true Code_Printer.literal_numeral) ["SML", "OCaml", "Haskell", "Scala"]
   546 *}
   547 
   548 code_const "plus :: integer \<Rightarrow> _ \<Rightarrow> _"
   549   (SML "IntInf.+ ((_), (_))")
   550   (OCaml "Big'_int.add'_big'_int")
   551   (Haskell infixl 6 "+")
   552   (Scala infixl 7 "+")
   553   (Eval infixl 8 "+")
   554 
   555 code_const "uminus :: integer \<Rightarrow> _"
   556   (SML "IntInf.~")
   557   (OCaml "Big'_int.minus'_big'_int")
   558   (Haskell "negate")
   559   (Scala "!(- _)")
   560   (Eval "~/ _")
   561 
   562 code_const "minus :: integer \<Rightarrow> _"
   563   (SML "IntInf.- ((_), (_))")
   564   (OCaml "Big'_int.sub'_big'_int")
   565   (Haskell infixl 6 "-")
   566   (Scala infixl 7 "-")
   567   (Eval infixl 8 "-")
   568 
   569 code_const Code_Numeral.dup
   570   (SML "IntInf.*/ (2,/ (_))")
   571   (OCaml "Big'_int.mult'_big'_int/ (Big'_int.big'_int'_of'_int/ 2)")
   572   (Haskell "!(2 * _)")
   573   (Scala "!(2 * _)")
   574   (Eval "!(2 * _)")
   575 
   576 code_const Code_Numeral.sub
   577   (SML "!(raise/ Fail/ \"sub\")")
   578   (OCaml "failwith/ \"sub\"")
   579   (Haskell "error/ \"sub\"")
   580   (Scala "!sys.error(\"sub\")")
   581 
   582 code_const "times :: integer \<Rightarrow> _ \<Rightarrow> _"
   583   (SML "IntInf.* ((_), (_))")
   584   (OCaml "Big'_int.mult'_big'_int")
   585   (Haskell infixl 7 "*")
   586   (Scala infixl 8 "*")
   587   (Eval infixl 9 "*")
   588 
   589 code_const Code_Numeral.divmod_abs
   590   (SML "IntInf.divMod/ (IntInf.abs _,/ IntInf.abs _)")
   591   (OCaml "Big'_int.quomod'_big'_int/ (Big'_int.abs'_big'_int _)/ (Big'_int.abs'_big'_int _)")
   592   (Haskell "divMod/ (abs _)/ (abs _)")
   593   (Scala "!((k: BigInt) => (l: BigInt) =>/ if (l == 0)/ (BigInt(0), k) else/ (k.abs '/% l.abs))")
   594   (Eval "Integer.div'_mod/ (abs _)/ (abs _)")
   595 
   596 code_const "HOL.equal :: integer \<Rightarrow> _ \<Rightarrow> bool"
   597   (SML "!((_ : IntInf.int) = _)")
   598   (OCaml "Big'_int.eq'_big'_int")
   599   (Haskell infix 4 "==")
   600   (Scala infixl 5 "==")
   601   (Eval infixl 6 "=")
   602 
   603 code_const "less_eq :: integer \<Rightarrow> _ \<Rightarrow> bool"
   604   (SML "IntInf.<= ((_), (_))")
   605   (OCaml "Big'_int.le'_big'_int")
   606   (Haskell infix 4 "<=")
   607   (Scala infixl 4 "<=")
   608   (Eval infixl 6 "<=")
   609 
   610 code_const "less :: integer \<Rightarrow> _ \<Rightarrow> bool"
   611   (SML "IntInf.< ((_), (_))")
   612   (OCaml "Big'_int.lt'_big'_int")
   613   (Haskell infix 4 "<")
   614   (Scala infixl 4 "<")
   615   (Eval infixl 6 "<")
   616 
   617 code_modulename SML
   618   Code_Numeral Arith
   619 
   620 code_modulename OCaml
   621   Code_Numeral Arith
   622 
   623 code_modulename Haskell
   624   Code_Numeral Arith
   625 
   626 
   627 subsection {* Type of target language naturals *}
   628 
   629 typedef natural = "UNIV \<Colon> nat set"
   630   morphisms nat_of_natural natural_of_nat ..
   631 
   632 setup_lifting (no_code) type_definition_natural
   633 
   634 lemma natural_eq_iff [termination_simp]:
   635   "m = n \<longleftrightarrow> nat_of_natural m = nat_of_natural n"
   636   by transfer rule
   637 
   638 lemma natural_eqI:
   639   "nat_of_natural m = nat_of_natural n \<Longrightarrow> m = n"
   640   using natural_eq_iff [of m n] by simp
   641 
   642 lemma nat_of_natural_of_nat_inverse [simp]:
   643   "nat_of_natural (natural_of_nat n) = n"
   644   by transfer rule
   645 
   646 lemma natural_of_nat_of_natural_inverse [simp]:
   647   "natural_of_nat (nat_of_natural n) = n"
   648   by transfer rule
   649 
   650 instantiation natural :: "{comm_monoid_diff, semiring_1}"
   651 begin
   652 
   653 lift_definition zero_natural :: natural
   654   is "0 :: nat"
   655   .
   656 
   657 declare zero_natural.rep_eq [simp]
   658 
   659 lift_definition one_natural :: natural
   660   is "1 :: nat"
   661   .
   662 
   663 declare one_natural.rep_eq [simp]
   664 
   665 lift_definition plus_natural :: "natural \<Rightarrow> natural \<Rightarrow> natural"
   666   is "plus :: nat \<Rightarrow> nat \<Rightarrow> nat"
   667   .
   668 
   669 declare plus_natural.rep_eq [simp]
   670 
   671 lift_definition minus_natural :: "natural \<Rightarrow> natural \<Rightarrow> natural"
   672   is "minus :: nat \<Rightarrow> nat \<Rightarrow> nat"
   673   .
   674 
   675 declare minus_natural.rep_eq [simp]
   676 
   677 lift_definition times_natural :: "natural \<Rightarrow> natural \<Rightarrow> natural"
   678   is "times :: nat \<Rightarrow> nat \<Rightarrow> nat"
   679   .
   680 
   681 declare times_natural.rep_eq [simp]
   682 
   683 instance proof
   684 qed (transfer, simp add: algebra_simps)+
   685 
   686 end
   687 
   688 lemma [transfer_rule]:
   689   "fun_rel HOL.eq pcr_natural (\<lambda>n::nat. n) (of_nat :: nat \<Rightarrow> natural)"
   690 proof -
   691   have "fun_rel HOL.eq pcr_natural (of_nat :: nat \<Rightarrow> nat) (of_nat :: nat \<Rightarrow> natural)"
   692     by (unfold of_nat_def [abs_def]) transfer_prover
   693   then show ?thesis by (simp add: id_def)
   694 qed
   695 
   696 lemma [transfer_rule]:
   697   "fun_rel HOL.eq pcr_natural (numeral :: num \<Rightarrow> nat) (numeral :: num \<Rightarrow> natural)"
   698 proof -
   699   have "fun_rel HOL.eq pcr_natural (numeral :: num \<Rightarrow> nat) (\<lambda>n. of_nat (numeral n))"
   700     by transfer_prover
   701   then show ?thesis by simp
   702 qed
   703 
   704 lemma nat_of_natural_of_nat [simp]:
   705   "nat_of_natural (of_nat n) = n"
   706   by transfer rule
   707 
   708 lemma natural_of_nat_of_nat [simp, code_abbrev]:
   709   "natural_of_nat = of_nat"
   710   by transfer rule
   711 
   712 lemma of_nat_of_natural [simp]:
   713   "of_nat (nat_of_natural n) = n"
   714   by transfer rule
   715 
   716 lemma nat_of_natural_numeral [simp]:
   717   "nat_of_natural (numeral k) = numeral k"
   718   by transfer rule
   719 
   720 instantiation natural :: "{semiring_div, equal, linordered_semiring}"
   721 begin
   722 
   723 lift_definition div_natural :: "natural \<Rightarrow> natural \<Rightarrow> natural"
   724   is "Divides.div :: nat \<Rightarrow> nat \<Rightarrow> nat"
   725   .
   726 
   727 declare div_natural.rep_eq [simp]
   728 
   729 lift_definition mod_natural :: "natural \<Rightarrow> natural \<Rightarrow> natural"
   730   is "Divides.mod :: nat \<Rightarrow> nat \<Rightarrow> nat"
   731   .
   732 
   733 declare mod_natural.rep_eq [simp]
   734 
   735 lift_definition less_eq_natural :: "natural \<Rightarrow> natural \<Rightarrow> bool"
   736   is "less_eq :: nat \<Rightarrow> nat \<Rightarrow> bool"
   737   .
   738 
   739 declare less_eq_natural.rep_eq [termination_simp]
   740 
   741 lift_definition less_natural :: "natural \<Rightarrow> natural \<Rightarrow> bool"
   742   is "less :: nat \<Rightarrow> nat \<Rightarrow> bool"
   743   .
   744 
   745 declare less_natural.rep_eq [termination_simp]
   746 
   747 lift_definition equal_natural :: "natural \<Rightarrow> natural \<Rightarrow> bool"
   748   is "HOL.equal :: nat \<Rightarrow> nat \<Rightarrow> bool"
   749   .
   750 
   751 instance proof
   752 qed (transfer, simp add: algebra_simps equal less_le_not_le [symmetric] linear)+
   753 
   754 end
   755 
   756 lemma [transfer_rule]:
   757   "fun_rel pcr_natural (fun_rel pcr_natural pcr_natural) (min :: _ \<Rightarrow> _ \<Rightarrow> nat) (min :: _ \<Rightarrow> _ \<Rightarrow> natural)"
   758   by (unfold min_def [abs_def]) transfer_prover
   759 
   760 lemma [transfer_rule]:
   761   "fun_rel pcr_natural (fun_rel pcr_natural pcr_natural) (max :: _ \<Rightarrow> _ \<Rightarrow> nat) (max :: _ \<Rightarrow> _ \<Rightarrow> natural)"
   762   by (unfold max_def [abs_def]) transfer_prover
   763 
   764 lemma nat_of_natural_min [simp]:
   765   "nat_of_natural (min k l) = min (nat_of_natural k) (nat_of_natural l)"
   766   by transfer rule
   767 
   768 lemma nat_of_natural_max [simp]:
   769   "nat_of_natural (max k l) = max (nat_of_natural k) (nat_of_natural l)"
   770   by transfer rule
   771 
   772 lift_definition natural_of_integer :: "integer \<Rightarrow> natural"
   773   is "nat :: int \<Rightarrow> nat"
   774   .
   775 
   776 lift_definition integer_of_natural :: "natural \<Rightarrow> integer"
   777   is "of_nat :: nat \<Rightarrow> int"
   778   .
   779 
   780 lemma natural_of_integer_of_natural [simp]:
   781   "natural_of_integer (integer_of_natural n) = n"
   782   by transfer simp
   783 
   784 lemma integer_of_natural_of_integer [simp]:
   785   "integer_of_natural (natural_of_integer k) = max 0 k"
   786   by transfer auto
   787 
   788 lemma int_of_integer_of_natural [simp]:
   789   "int_of_integer (integer_of_natural n) = of_nat (nat_of_natural n)"
   790   by transfer rule
   791 
   792 lemma integer_of_natural_of_nat [simp]:
   793   "integer_of_natural (of_nat n) = of_nat n"
   794   by transfer rule
   795 
   796 lemma [measure_function]:
   797   "is_measure nat_of_natural"
   798   by (rule is_measure_trivial)
   799 
   800 
   801 subsection {* Inductive represenation of target language naturals *}
   802 
   803 lift_definition Suc :: "natural \<Rightarrow> natural"
   804   is Nat.Suc
   805   .
   806 
   807 declare Suc.rep_eq [simp]
   808 
   809 rep_datatype "0::natural" Suc
   810   by (transfer, fact nat.induct nat.inject nat.distinct)+
   811 
   812 lemma natural_case [case_names nat, cases type: natural]:
   813   fixes m :: natural
   814   assumes "\<And>n. m = of_nat n \<Longrightarrow> P"
   815   shows P
   816   using assms by transfer blast
   817 
   818 lemma [simp, code]:
   819   "natural_size = nat_of_natural"
   820 proof (rule ext)
   821   fix n
   822   show "natural_size n = nat_of_natural n"
   823     by (induct n) simp_all
   824 qed
   825 
   826 lemma [simp, code]:
   827   "size = nat_of_natural"
   828 proof (rule ext)
   829   fix n
   830   show "size n = nat_of_natural n"
   831     by (induct n) simp_all
   832 qed
   833 
   834 lemma natural_decr [termination_simp]:
   835   "n \<noteq> 0 \<Longrightarrow> nat_of_natural n - Nat.Suc 0 < nat_of_natural n"
   836   by transfer simp
   837 
   838 lemma natural_zero_minus_one:
   839   "(0::natural) - 1 = 0"
   840   by simp
   841 
   842 lemma Suc_natural_minus_one:
   843   "Suc n - 1 = n"
   844   by transfer simp
   845 
   846 hide_const (open) Suc
   847 
   848 
   849 subsection {* Code refinement for target language naturals *}
   850 
   851 lift_definition Nat :: "integer \<Rightarrow> natural"
   852   is nat
   853   .
   854 
   855 lemma [code_post]:
   856   "Nat 0 = 0"
   857   "Nat 1 = 1"
   858   "Nat (numeral k) = numeral k"
   859   by (transfer, simp)+
   860 
   861 lemma [code abstype]:
   862   "Nat (integer_of_natural n) = n"
   863   by transfer simp
   864 
   865 lemma [code abstract]:
   866   "integer_of_natural (natural_of_nat n) = of_nat n"
   867   by simp
   868 
   869 lemma [code abstract]:
   870   "integer_of_natural (natural_of_integer k) = max 0 k"
   871   by simp
   872 
   873 lemma [code_abbrev]:
   874   "natural_of_integer (Code_Numeral.Pos k) = numeral k"
   875   by transfer simp
   876 
   877 lemma [code abstract]:
   878   "integer_of_natural 0 = 0"
   879   by transfer simp
   880 
   881 lemma [code abstract]:
   882   "integer_of_natural 1 = 1"
   883   by transfer simp
   884 
   885 lemma [code abstract]:
   886   "integer_of_natural (Code_Numeral.Suc n) = integer_of_natural n + 1"
   887   by transfer simp
   888 
   889 lemma [code]:
   890   "nat_of_natural = nat_of_integer \<circ> integer_of_natural"
   891   by transfer (simp add: fun_eq_iff)
   892 
   893 lemma [code, code_unfold]:
   894   "natural_case f g n = (if n = 0 then f else g (n - 1))"
   895   by (cases n rule: natural.exhaust) (simp_all, simp add: Suc_def)
   896 
   897 declare natural.recs [code del]
   898 
   899 lemma [code abstract]:
   900   "integer_of_natural (m + n) = integer_of_natural m + integer_of_natural n"
   901   by transfer simp
   902 
   903 lemma [code abstract]:
   904   "integer_of_natural (m - n) = max 0 (integer_of_natural m - integer_of_natural n)"
   905   by transfer simp
   906 
   907 lemma [code abstract]:
   908   "integer_of_natural (m * n) = integer_of_natural m * integer_of_natural n"
   909   by transfer (simp add: of_nat_mult)
   910 
   911 lemma [code abstract]:
   912   "integer_of_natural (m div n) = integer_of_natural m div integer_of_natural n"
   913   by transfer (simp add: zdiv_int)
   914 
   915 lemma [code abstract]:
   916   "integer_of_natural (m mod n) = integer_of_natural m mod integer_of_natural n"
   917   by transfer (simp add: zmod_int)
   918 
   919 lemma [code]:
   920   "HOL.equal m n \<longleftrightarrow> HOL.equal (integer_of_natural m) (integer_of_natural n)"
   921   by transfer (simp add: equal)
   922 
   923 lemma [code nbe]:
   924   "HOL.equal n (n::natural) \<longleftrightarrow> True"
   925   by (simp add: equal)
   926 
   927 lemma [code]:
   928   "m \<le> n \<longleftrightarrow> integer_of_natural m \<le> integer_of_natural n"
   929   by transfer simp
   930 
   931 lemma [code]:
   932   "m < n \<longleftrightarrow> integer_of_natural m < integer_of_natural n"
   933   by transfer simp
   934 
   935 hide_const (open) Nat
   936 
   937 
   938 code_reflect Code_Numeral
   939   datatypes natural = _
   940   functions integer_of_natural natural_of_integer
   941 
   942 end
   943