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