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