src/HOL/Library/Code_Integer.thy
 changeset 47108 2a1953f0d20d parent 39272 0b61951d2682 child 47125 a3a64240cd98
```     1.1 --- a/src/HOL/Library/Code_Integer.thy	Sat Mar 24 16:27:04 2012 +0100
1.2 +++ b/src/HOL/Library/Code_Integer.thy	Sun Mar 25 20:15:39 2012 +0200
1.3 @@ -9,6 +9,43 @@
1.4  begin
1.5
1.6  text {*
1.7 +  Representation-ignorant code equations for conversions.
1.8 +*}
1.9 +
1.10 +lemma nat_code [code]:
1.11 +  "nat k = (if k \<le> 0 then 0 else
1.12 +     let
1.13 +       (l, j) = divmod_int k 2;
1.14 +       l' = 2 * nat l
1.15 +     in if j = 0 then l' else Suc l')"
1.16 +proof -
1.17 +  have "2 = nat 2" by simp
1.18 +  show ?thesis
1.19 +    apply (auto simp add: Let_def divmod_int_mod_div not_le
1.20 +     nat_div_distrib nat_mult_distrib mult_div_cancel mod_2_not_eq_zero_eq_one_int)
1.21 +    apply (unfold `2 = nat 2`)
1.22 +    apply (subst nat_mod_distrib [symmetric])
1.23 +    apply simp_all
1.24 +  done
1.25 +qed
1.26 +
1.27 +lemma (in ring_1) of_int_code:
1.28 +  "of_int k = (if k = 0 then 0
1.29 +     else if k < 0 then - of_int (- k)
1.30 +     else let
1.31 +       (l, j) = divmod_int k 2;
1.32 +       l' = 2 * of_int l
1.33 +     in if j = 0 then l' else l' + 1)"
1.34 +proof -
1.35 +  from mod_div_equality have *: "of_int k = of_int (k div 2 * 2 + k mod 2)" by simp
1.36 +  show ?thesis
1.37 +    by (simp add: Let_def divmod_int_mod_div mod_2_not_eq_zero_eq_one_int
1.39 +qed
1.40 +
1.41 +declare of_int_code [code]
1.42 +
1.43 +text {*
1.44    HOL numeral expressions are mapped to integer literals
1.45    in target languages, using predefined target language
1.46    operations for abstract integer operations.
1.47 @@ -24,42 +61,21 @@
1.48  code_instance int :: equal
1.50
1.51 +code_const "0::int"
1.52 +  (SML "0")
1.53 +  (OCaml "Big'_int.zero'_big'_int")
1.55 +  (Scala "BigInt(0)")
1.56 +
1.57  setup {*
1.58 -  fold (Numeral.add_code @{const_name number_int_inst.number_of_int}
1.59 -    true Code_Printer.literal_numeral) ["SML", "OCaml", "Haskell", "Scala"]
1.60 +  fold (Numeral.add_code @{const_name Int.Pos}
1.61 +    false Code_Printer.literal_numeral) ["SML", "OCaml", "Haskell", "Scala"]
1.62  *}
1.63
1.64 -code_const "Int.Pls" and "Int.Min" and "Int.Bit0" and "Int.Bit1"
1.65 -  (SML "raise/ Fail/ \"Pls\""
1.66 -     and "raise/ Fail/ \"Min\""
1.67 -     and "!((_);/ raise/ Fail/ \"Bit0\")"
1.68 -     and "!((_);/ raise/ Fail/ \"Bit1\")")
1.69 -  (OCaml "failwith/ \"Pls\""
1.70 -     and "failwith/ \"Min\""
1.71 -     and "!((_);/ failwith/ \"Bit0\")"
1.72 -     and "!((_);/ failwith/ \"Bit1\")")
1.74 -     and "error/ \"Min\""
1.75 -     and "error/ \"Bit0\""
1.76 -     and "error/ \"Bit1\"")
1.77 -  (Scala "!error(\"Pls\")"
1.78 -     and "!error(\"Min\")"
1.79 -     and "!error(\"Bit0\")"
1.80 -     and "!error(\"Bit1\")")
1.81 -
1.82 -code_const Int.pred
1.83 -  (SML "IntInf.- ((_), 1)")
1.84 -  (OCaml "Big'_int.pred'_big'_int")
1.85 -  (Haskell "!(_/ -/ 1)")
1.86 -  (Scala "!(_ -/ 1)")
1.87 -  (Eval "!(_/ -/ 1)")
1.88 -
1.89 -code_const Int.succ
1.90 -  (SML "IntInf.+ ((_), 1)")
1.91 -  (OCaml "Big'_int.succ'_big'_int")
1.92 -  (Haskell "!(_/ +/ 1)")
1.93 -  (Scala "!(_ +/ 1)")
1.94 -  (Eval "!(_/ +/ 1)")
1.95 +setup {*
1.96 +  fold (Numeral.add_code @{const_name Int.Neg}
1.97 +    true Code_Printer.literal_numeral) ["SML", "OCaml", "Haskell", "Scala"]
1.98 +*}
1.99
1.100  code_const "op + \<Colon> int \<Rightarrow> int \<Rightarrow> int"
1.101    (SML "IntInf.+ ((_), (_))")
1.102 @@ -82,6 +98,19 @@
1.103    (Scala infixl 7 "-")
1.104    (Eval infixl 8 "-")
1.105
1.106 +code_const Int.dup
1.107 +  (SML "IntInf.*/ (2,/ (_))")
1.108 +  (OCaml "Big'_int.mult'_big'_int/ 2")
1.109 +  (Haskell "!(2 * _)")
1.110 +  (Scala "!(2 * _)")
1.111 +  (Eval "!(2 * _)")
1.112 +
1.113 +code_const Int.sub
1.114 +  (SML "!(raise/ Fail/ \"sub\")")
1.115 +  (OCaml "failwith/ \"sub\"")
1.117 +  (Scala "!error(\"sub\")")
1.118 +
1.119  code_const "op * \<Colon> int \<Rightarrow> int \<Rightarrow> int"
1.120    (SML "IntInf.* ((_), (_))")
1.121    (OCaml "Big'_int.mult'_big'_int")
1.122 @@ -124,9 +153,7 @@
1.123    (Scala "!_.as'_BigInt")
1.124    (Eval "_")
1.125
1.126 -text {* Evaluation *}
1.127 -
1.128  code_const "Code_Evaluation.term_of \<Colon> int \<Rightarrow> term"
1.129    (Eval "HOLogic.mk'_number/ HOLogic.intT")
1.130
1.131 -end
1.132 \ No newline at end of file
1.133 +end
```