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