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