src/HOL/Library/Efficient_Nat.thy
author wenzelm
Fri, 16 Apr 2010 21:28:09 +0200
changeset 36176 3fe7e97ccca8
parent 35689 c3bef0c972d7
child 36603 d5d6111761a6
permissions -rw-r--r--
replaced generic 'hide' command by more conventional 'hide_class', 'hide_type', 'hide_const', 'hide_fact' -- frees some popular keywords;
haftmann@23854
     1
(*  Title:      HOL/Library/Efficient_Nat.thy
haftmann@25931
     2
    Author:     Stefan Berghofer, Florian Haftmann, TU Muenchen
haftmann@23854
     3
*)
haftmann@23854
     4
haftmann@25931
     5
header {* Implementation of natural numbers by target-language integers *}
haftmann@23854
     6
haftmann@23854
     7
theory Efficient_Nat
haftmann@31203
     8
imports Code_Integer Main
haftmann@23854
     9
begin
haftmann@23854
    10
haftmann@23854
    11
text {*
haftmann@25931
    12
  When generating code for functions on natural numbers, the
haftmann@25931
    13
  canonical representation using @{term "0::nat"} and
haftmann@25931
    14
  @{term "Suc"} is unsuitable for computations involving large
haftmann@25931
    15
  numbers.  The efficiency of the generated code can be improved
haftmann@25931
    16
  drastically by implementing natural numbers by target-language
haftmann@25931
    17
  integers.  To do this, just include this theory.
haftmann@23854
    18
*}
haftmann@23854
    19
haftmann@25931
    20
subsection {* Basic arithmetic *}
haftmann@23854
    21
haftmann@23854
    22
text {*
haftmann@25931
    23
  Most standard arithmetic functions on natural numbers are implemented
haftmann@25931
    24
  using their counterparts on the integers:
haftmann@23854
    25
*}
haftmann@23854
    26
haftmann@25931
    27
code_datatype number_nat_inst.number_of_nat
haftmann@23854
    28
haftmann@32061
    29
lemma zero_nat_code [code, code_unfold_post]:
haftmann@25931
    30
  "0 = (Numeral0 :: nat)"
haftmann@25931
    31
  by simp
haftmann@23854
    32
haftmann@32061
    33
lemma one_nat_code [code, code_unfold_post]:
haftmann@25931
    34
  "1 = (Numeral1 :: nat)"
haftmann@25931
    35
  by simp
haftmann@23854
    36
haftmann@25931
    37
lemma Suc_code [code]:
haftmann@25931
    38
  "Suc n = n + 1"
haftmann@25931
    39
  by simp
haftmann@23854
    40
haftmann@25931
    41
lemma plus_nat_code [code]:
haftmann@25931
    42
  "n + m = nat (of_nat n + of_nat m)"
haftmann@25931
    43
  by simp
haftmann@23854
    44
haftmann@25931
    45
lemma minus_nat_code [code]:
haftmann@25931
    46
  "n - m = nat (of_nat n - of_nat m)"
haftmann@25931
    47
  by simp
haftmann@23854
    48
haftmann@25931
    49
lemma times_nat_code [code]:
haftmann@25931
    50
  "n * m = nat (of_nat n * of_nat m)"
haftmann@25931
    51
  unfolding of_nat_mult [symmetric] by simp
haftmann@23854
    52
haftmann@26009
    53
text {* Specialized @{term "op div \<Colon> nat \<Rightarrow> nat \<Rightarrow> nat"} 
haftmann@26009
    54
  and @{term "op mod \<Colon> nat \<Rightarrow> nat \<Rightarrow> nat"} operations. *}
haftmann@23854
    55
haftmann@28694
    56
definition divmod_aux ::  "nat \<Rightarrow> nat \<Rightarrow> nat \<times> nat" where
haftmann@33338
    57
  [code del]: "divmod_aux = divmod_nat"
haftmann@26009
    58
haftmann@28522
    59
lemma [code]:
haftmann@33338
    60
  "divmod_nat n m = (if m = 0 then (0, n) else divmod_aux n m)"
haftmann@33338
    61
  unfolding divmod_aux_def divmod_nat_div_mod by simp
haftmann@26009
    62
haftmann@26100
    63
lemma divmod_aux_code [code]:
haftmann@26100
    64
  "divmod_aux n m = (nat (of_nat n div of_nat m), nat (of_nat n mod of_nat m))"
haftmann@33338
    65
  unfolding divmod_aux_def divmod_nat_div_mod zdiv_int [symmetric] zmod_int [symmetric] by simp
haftmann@23854
    66
haftmann@25931
    67
lemma eq_nat_code [code]:
haftmann@28351
    68
  "eq_class.eq n m \<longleftrightarrow> eq_class.eq (of_nat n \<Colon> int) (of_nat m)"
haftmann@28351
    69
  by (simp add: eq)
haftmann@28351
    70
haftmann@28351
    71
lemma eq_nat_refl [code nbe]:
haftmann@28351
    72
  "eq_class.eq (n::nat) n \<longleftrightarrow> True"
haftmann@28351
    73
  by (rule HOL.eq_refl)
haftmann@25931
    74
haftmann@25931
    75
lemma less_eq_nat_code [code]:
haftmann@25931
    76
  "n \<le> m \<longleftrightarrow> (of_nat n \<Colon> int) \<le> of_nat m"
haftmann@25931
    77
  by simp
haftmann@25931
    78
haftmann@25931
    79
lemma less_nat_code [code]:
haftmann@25931
    80
  "n < m \<longleftrightarrow> (of_nat n \<Colon> int) < of_nat m"
haftmann@25931
    81
  by simp
haftmann@25931
    82
haftmann@25931
    83
subsection {* Case analysis *}
haftmann@24423
    84
haftmann@23854
    85
text {*
haftmann@23854
    86
  Case analysis on natural numbers is rephrased using a conditional
haftmann@23854
    87
  expression:
haftmann@23854
    88
*}
haftmann@23854
    89
haftmann@31998
    90
lemma [code, code_unfold]:
haftmann@25615
    91
  "nat_case = (\<lambda>f g n. if n = 0 then f else g (n - 1))"
haftmann@25931
    92
  by (auto simp add: expand_fun_eq dest!: gr0_implies_Suc)
haftmann@25615
    93
haftmann@23854
    94
haftmann@23854
    95
subsection {* Preprocessors *}
haftmann@23854
    96
haftmann@23854
    97
text {*
haftmann@23854
    98
  In contrast to @{term "Suc n"}, the term @{term "n + (1::nat)"} is no longer
haftmann@23854
    99
  a constructor term. Therefore, all occurrences of this term in a position
haftmann@23854
   100
  where a pattern is expected (i.e.\ on the left-hand side of a recursion
haftmann@23854
   101
  equation or in the arguments of an inductive relation in an introduction
haftmann@23854
   102
  rule) must be eliminated.
haftmann@23854
   103
  This can be accomplished by applying the following transformation rules:
haftmann@23854
   104
*}
haftmann@23854
   105
haftmann@29874
   106
lemma Suc_if_eq: "(\<And>n. f (Suc n) \<equiv> h n) \<Longrightarrow> f 0 \<equiv> g \<Longrightarrow>
haftmann@29874
   107
  f n \<equiv> if n = 0 then g else h (n - 1)"
haftmann@31954
   108
  by (rule eq_reflection) (cases n, simp_all)
haftmann@29874
   109
haftmann@25931
   110
lemma Suc_clause: "(\<And>n. P n (Suc n)) \<Longrightarrow> n \<noteq> 0 \<Longrightarrow> P (n - 1) n"
haftmann@29869
   111
  by (cases n) simp_all
haftmann@23854
   112
haftmann@23854
   113
text {*
haftmann@23854
   114
  The rules above are built into a preprocessor that is plugged into
haftmann@23854
   115
  the code generator. Since the preprocessor for introduction rules
haftmann@23854
   116
  does not know anything about modes, some of the modes that worked
haftmann@23854
   117
  for the canonical representation of natural numbers may no longer work.
haftmann@23854
   118
*}
haftmann@23854
   119
haftmann@23854
   120
(*<*)
haftmann@27609
   121
setup {*
haftmann@27609
   122
let
haftmann@23854
   123
haftmann@31954
   124
fun remove_suc thy thms =
haftmann@23854
   125
  let
haftmann@23854
   126
    val vname = Name.variant (map fst
haftmann@29874
   127
      (fold (Term.add_var_names o Thm.full_prop_of) thms [])) "n";
haftmann@23854
   128
    val cv = cterm_of thy (Var ((vname, 0), HOLogic.natT));
haftmann@23854
   129
    fun lhs_of th = snd (Thm.dest_comb
haftmann@31954
   130
      (fst (Thm.dest_comb (cprop_of th))));
haftmann@31954
   131
    fun rhs_of th = snd (Thm.dest_comb (cprop_of th));
haftmann@23854
   132
    fun find_vars ct = (case term_of ct of
haftmann@29869
   133
        (Const (@{const_name Suc}, _) $ Var _) => [(cv, snd (Thm.dest_comb ct))]
haftmann@23854
   134
      | _ $ _ =>
haftmann@23854
   135
        let val (ct1, ct2) = Thm.dest_comb ct
haftmann@23854
   136
        in 
haftmann@23854
   137
          map (apfst (fn ct => Thm.capply ct ct2)) (find_vars ct1) @
haftmann@23854
   138
          map (apfst (Thm.capply ct1)) (find_vars ct2)
haftmann@23854
   139
        end
haftmann@23854
   140
      | _ => []);
haftmann@23854
   141
    val eqs = maps
haftmann@23854
   142
      (fn th => map (pair th) (find_vars (lhs_of th))) thms;
haftmann@23854
   143
    fun mk_thms (th, (ct, cv')) =
haftmann@23854
   144
      let
haftmann@23854
   145
        val th' =
haftmann@23854
   146
          Thm.implies_elim
haftmann@23854
   147
           (Conv.fconv_rule (Thm.beta_conversion true)
haftmann@23854
   148
             (Drule.instantiate'
haftmann@23854
   149
               [SOME (ctyp_of_term ct)] [SOME (Thm.cabs cv ct),
haftmann@23854
   150
                 SOME (Thm.cabs cv' (rhs_of th)), NONE, SOME cv']
haftmann@31954
   151
               @{thm Suc_if_eq})) (Thm.forall_intr cv' th)
haftmann@23854
   152
      in
haftmann@23854
   153
        case map_filter (fn th'' =>
haftmann@23854
   154
            SOME (th'', singleton
haftmann@23854
   155
              (Variable.trade (K (fn [th'''] => [th''' RS th'])) (Variable.thm_context th'')) th'')
haftmann@23854
   156
          handle THM _ => NONE) thms of
haftmann@23854
   157
            [] => NONE
haftmann@23854
   158
          | thps =>
haftmann@23854
   159
              let val (ths1, ths2) = split_list thps
haftmann@23854
   160
              in SOME (subtract Thm.eq_thm (th :: ths1) thms @ ths2) end
haftmann@23854
   161
      end
haftmann@29874
   162
  in get_first mk_thms eqs end;
haftmann@29874
   163
haftmann@34893
   164
fun eqn_suc_base_preproc thy thms =
haftmann@29874
   165
  let
haftmann@31954
   166
    val dest = fst o Logic.dest_equals o prop_of;
haftmann@29874
   167
    val contains_suc = exists_Const (fn (c, _) => c = @{const_name Suc});
haftmann@29874
   168
  in
haftmann@29874
   169
    if forall (can dest) thms andalso exists (contains_suc o dest) thms
haftmann@32348
   170
      then thms |> perhaps_loop (remove_suc thy) |> (Option.map o map) Drule.zero_var_indexes
haftmann@29874
   171
       else NONE
haftmann@23854
   172
  end;
haftmann@23854
   173
haftmann@34893
   174
val eqn_suc_preproc = Code_Preproc.simple_functrans eqn_suc_base_preproc;
haftmann@23854
   175
haftmann@23854
   176
fun remove_suc_clause thy thms =
haftmann@23854
   177
  let
haftmann@23854
   178
    val vname = Name.variant (map fst
wenzelm@29258
   179
      (fold (Term.add_var_names o Thm.full_prop_of) thms [])) "x";
haftmann@24222
   180
    fun find_var (t as Const (@{const_name Suc}, _) $ (v as Var _)) = SOME (t, v)
haftmann@23854
   181
      | find_var (t $ u) = (case find_var t of NONE => find_var u | x => x)
haftmann@23854
   182
      | find_var _ = NONE;
haftmann@23854
   183
    fun find_thm th =
wenzelm@35625
   184
      let val th' = Conv.fconv_rule Object_Logic.atomize th
haftmann@23854
   185
      in Option.map (pair (th, th')) (find_var (prop_of th')) end
haftmann@23854
   186
  in
haftmann@23854
   187
    case get_first find_thm thms of
haftmann@23854
   188
      NONE => thms
haftmann@23854
   189
    | SOME ((th, th'), (Sucv, v)) =>
haftmann@23854
   190
        let
haftmann@23854
   191
          val cert = cterm_of (Thm.theory_of_thm th);
wenzelm@35625
   192
          val th'' = Object_Logic.rulify (Thm.implies_elim
haftmann@23854
   193
            (Conv.fconv_rule (Thm.beta_conversion true)
haftmann@23854
   194
              (Drule.instantiate' []
haftmann@23854
   195
                [SOME (cert (lambda v (Abs ("x", HOLogic.natT,
haftmann@23854
   196
                   abstract_over (Sucv,
haftmann@23854
   197
                     HOLogic.dest_Trueprop (prop_of th')))))),
haftmann@24222
   198
                 SOME (cert v)] @{thm Suc_clause}))
haftmann@23854
   199
            (Thm.forall_intr (cert v) th'))
haftmann@23854
   200
        in
haftmann@23854
   201
          remove_suc_clause thy (map (fn th''' =>
haftmann@23854
   202
            if (op = o pairself prop_of) (th''', th) then th'' else th''') thms)
haftmann@23854
   203
        end
haftmann@23854
   204
  end;
haftmann@23854
   205
haftmann@23854
   206
fun clause_suc_preproc thy ths =
haftmann@23854
   207
  let
haftmann@23854
   208
    val dest = fst o HOLogic.dest_mem o HOLogic.dest_Trueprop
haftmann@23854
   209
  in
haftmann@23854
   210
    if forall (can (dest o concl_of)) ths andalso
wenzelm@29287
   211
      exists (fn th => exists (exists_Const (fn (c, _) => c = @{const_name Suc}))
wenzelm@29287
   212
        (map_filter (try dest) (concl_of th :: prems_of th))) ths
haftmann@23854
   213
    then remove_suc_clause thy ths else ths
haftmann@23854
   214
  end;
haftmann@27609
   215
in
haftmann@27609
   216
haftmann@34893
   217
  Code_Preproc.add_functrans ("eqn_Suc", eqn_suc_preproc)
haftmann@23854
   218
  #> Codegen.add_preprocessor clause_suc_preproc
haftmann@27609
   219
haftmann@27609
   220
end;
haftmann@23854
   221
*}
haftmann@23854
   222
(*>*)
haftmann@23854
   223
haftmann@27609
   224
haftmann@25931
   225
subsection {* Target language setup *}
haftmann@25931
   226
haftmann@25931
   227
text {*
haftmann@25967
   228
  For ML, we map @{typ nat} to target language integers, where we
haftmann@34899
   229
  ensure that values are always non-negative.
haftmann@25931
   230
*}
haftmann@25931
   231
haftmann@25931
   232
code_type nat
haftmann@27496
   233
  (SML "IntInf.int")
haftmann@25931
   234
  (OCaml "Big'_int.big'_int")
haftmann@25931
   235
haftmann@25931
   236
types_code
haftmann@25931
   237
  nat ("int")
haftmann@25931
   238
attach (term_of) {*
haftmann@25931
   239
val term_of_nat = HOLogic.mk_number HOLogic.natT;
haftmann@25931
   240
*}
haftmann@25931
   241
attach (test) {*
haftmann@25931
   242
fun gen_nat i =
haftmann@25931
   243
  let val n = random_range 0 i
haftmann@25931
   244
  in (n, fn () => term_of_nat n) end;
haftmann@25931
   245
*}
haftmann@25931
   246
haftmann@25931
   247
text {*
haftmann@34899
   248
  For Haskell ans Scala we define our own @{typ nat} type.  The reason
haftmann@34899
   249
  is that we have to distinguish type class instances for @{typ nat}
haftmann@34899
   250
  and @{typ int}.
haftmann@25967
   251
*}
haftmann@25967
   252
haftmann@25967
   253
code_include Haskell "Nat" {*
haftmann@25967
   254
newtype Nat = Nat Integer deriving (Show, Eq);
haftmann@25967
   255
haftmann@25967
   256
instance Num Nat where {
haftmann@25967
   257
  fromInteger k = Nat (if k >= 0 then k else 0);
haftmann@25967
   258
  Nat n + Nat m = Nat (n + m);
haftmann@25967
   259
  Nat n - Nat m = fromInteger (n - m);
haftmann@25967
   260
  Nat n * Nat m = Nat (n * m);
haftmann@25967
   261
  abs n = n;
haftmann@25967
   262
  signum _ = 1;
haftmann@25967
   263
  negate n = error "negate Nat";
haftmann@25967
   264
};
haftmann@25967
   265
haftmann@25967
   266
instance Ord Nat where {
haftmann@25967
   267
  Nat n <= Nat m = n <= m;
haftmann@25967
   268
  Nat n < Nat m = n < m;
haftmann@25967
   269
};
haftmann@25967
   270
haftmann@25967
   271
instance Real Nat where {
haftmann@25967
   272
  toRational (Nat n) = toRational n;
haftmann@25967
   273
};
haftmann@25967
   274
haftmann@25967
   275
instance Enum Nat where {
haftmann@25967
   276
  toEnum k = fromInteger (toEnum k);
haftmann@25967
   277
  fromEnum (Nat n) = fromEnum n;
haftmann@25967
   278
};
haftmann@25967
   279
haftmann@25967
   280
instance Integral Nat where {
haftmann@25967
   281
  toInteger (Nat n) = n;
haftmann@25967
   282
  divMod n m = quotRem n m;
haftmann@25967
   283
  quotRem (Nat n) (Nat m) = (Nat k, Nat l) where (k, l) = quotRem n m;
haftmann@25967
   284
};
haftmann@25967
   285
*}
haftmann@25967
   286
haftmann@25967
   287
code_reserved Haskell Nat
haftmann@25967
   288
haftmann@34899
   289
code_include Scala "Nat" {*
haftmann@34931
   290
import scala.Math
haftmann@34931
   291
haftmann@34899
   292
object Nat {
haftmann@34899
   293
haftmann@34899
   294
  def apply(numeral: BigInt): Nat = new Nat(numeral max 0)
haftmann@34899
   295
  def apply(numeral: Int): Nat = Nat(BigInt(numeral))
haftmann@34899
   296
  def apply(numeral: String): Nat = Nat(BigInt(numeral))
haftmann@34899
   297
haftmann@34899
   298
}
haftmann@34899
   299
haftmann@34899
   300
class Nat private(private val value: BigInt) {
haftmann@34899
   301
haftmann@34899
   302
  override def hashCode(): Int = this.value.hashCode()
haftmann@34899
   303
haftmann@34899
   304
  override def equals(that: Any): Boolean = that match {
haftmann@34899
   305
    case that: Nat => this equals that
haftmann@34899
   306
    case _ => false
haftmann@34899
   307
  }
haftmann@34899
   308
haftmann@34899
   309
  override def toString(): String = this.value.toString
haftmann@34899
   310
haftmann@34899
   311
  def equals(that: Nat): Boolean = this.value == that.value
haftmann@34899
   312
haftmann@34899
   313
  def as_BigInt: BigInt = this.value
haftmann@34931
   314
  def as_Int: Int = if (this.value >= Math.MAX_INT && this.value <= Math.MAX_INT)
haftmann@34931
   315
      this.value.intValue
haftmann@34931
   316
    else error("Int value too big:" + this.value.toString)
haftmann@34899
   317
haftmann@34899
   318
  def +(that: Nat): Nat = new Nat(this.value + that.value)
haftmann@34899
   319
  def -(that: Nat): Nat = Nat(this.value + that.value)
haftmann@34899
   320
  def *(that: Nat): Nat = new Nat(this.value * that.value)
haftmann@34899
   321
haftmann@34899
   322
  def /%(that: Nat): (Nat, Nat) = if (that.value == 0) (new Nat(0), this)
haftmann@34899
   323
    else {
haftmann@34899
   324
      val (k, l) = this.value /% that.value
haftmann@34899
   325
      (new Nat(k), new Nat(l))
haftmann@34899
   326
    }
haftmann@34899
   327
haftmann@34899
   328
  def <=(that: Nat): Boolean = this.value <= that.value
haftmann@34899
   329
haftmann@34899
   330
  def <(that: Nat): Boolean = this.value < that.value
haftmann@34899
   331
haftmann@34899
   332
}
haftmann@34899
   333
*}
haftmann@34899
   334
haftmann@34899
   335
code_reserved Scala Nat
haftmann@34899
   336
haftmann@25967
   337
code_type nat
haftmann@29730
   338
  (Haskell "Nat.Nat")
haftmann@34899
   339
  (Scala "Nat.Nat")
haftmann@25967
   340
haftmann@25967
   341
code_instance nat :: eq
haftmann@25967
   342
  (Haskell -)
haftmann@25967
   343
haftmann@25967
   344
text {*
haftmann@25931
   345
  Natural numerals.
haftmann@25931
   346
*}
haftmann@25931
   347
haftmann@32061
   348
lemma [code_unfold_post]:
haftmann@25931
   349
  "nat (number_of i) = number_nat_inst.number_of_nat i"
haftmann@25931
   350
  -- {* this interacts as desired with @{thm nat_number_of_def} *}
haftmann@25931
   351
  by (simp add: number_nat_inst.number_of_nat)
haftmann@25931
   352
haftmann@25931
   353
setup {*
haftmann@25931
   354
  fold (Numeral.add_code @{const_name number_nat_inst.number_of_nat}
haftmann@34931
   355
    false Code_Printer.literal_positive_numeral) ["SML", "OCaml", "Haskell"]
haftmann@34899
   356
  #> Numeral.add_code @{const_name number_nat_inst.number_of_nat}
haftmann@34931
   357
    false Code_Printer.literal_positive_numeral "Scala"
haftmann@25931
   358
*}
haftmann@25931
   359
haftmann@25931
   360
text {*
haftmann@25931
   361
  Since natural numbers are implemented
haftmann@25967
   362
  using integers in ML, the coercion function @{const "of_nat"} of type
haftmann@25931
   363
  @{typ "nat \<Rightarrow> int"} is simply implemented by the identity function.
haftmann@25931
   364
  For the @{const "nat"} function for converting an integer to a natural
haftmann@25931
   365
  number, we give a specific implementation using an ML function that
haftmann@25931
   366
  returns its input value, provided that it is non-negative, and otherwise
haftmann@25931
   367
  returns @{text "0"}.
haftmann@25931
   368
*}
haftmann@25931
   369
haftmann@32065
   370
definition int :: "nat \<Rightarrow> int" where
haftmann@28562
   371
  [code del]: "int = of_nat"
haftmann@25931
   372
haftmann@28562
   373
lemma int_code' [code]:
haftmann@25931
   374
  "int (number_of l) = (if neg (number_of l \<Colon> int) then 0 else number_of l)"
haftmann@25931
   375
  unfolding int_nat_number_of [folded int_def] ..
haftmann@25931
   376
haftmann@28562
   377
lemma nat_code' [code]:
haftmann@25931
   378
  "nat (number_of l) = (if neg (number_of l \<Colon> int) then 0 else number_of l)"
huffman@28969
   379
  unfolding nat_number_of_def number_of_is_id neg_def by simp
haftmann@25931
   380
haftmann@32061
   381
lemma of_nat_int [code_unfold_post]:
haftmann@25931
   382
  "of_nat = int" by (simp add: int_def)
haftmann@25931
   383
haftmann@32065
   384
lemma of_nat_aux_int [code_unfold]:
haftmann@32065
   385
  "of_nat_aux (\<lambda>i. i + 1) k 0 = int k"
haftmann@32065
   386
  by (simp add: int_def Nat.of_nat_code)
haftmann@32065
   387
haftmann@25931
   388
code_const int
haftmann@25931
   389
  (SML "_")
haftmann@25931
   390
  (OCaml "_")
haftmann@25931
   391
haftmann@25931
   392
consts_code
haftmann@25931
   393
  int ("(_)")
haftmann@25931
   394
  nat ("\<module>nat")
haftmann@25931
   395
attach {*
haftmann@25931
   396
fun nat i = if i < 0 then 0 else i;
haftmann@25931
   397
*}
haftmann@25931
   398
haftmann@25967
   399
code_const nat
haftmann@25967
   400
  (SML "IntInf.max/ (/0,/ _)")
haftmann@25967
   401
  (OCaml "Big'_int.max'_big'_int/ Big'_int.zero'_big'_int")
haftmann@25967
   402
haftmann@35689
   403
text {* For Haskell and Scala, things are slightly different again. *}
haftmann@25967
   404
haftmann@25967
   405
code_const int and nat
haftmann@25967
   406
  (Haskell "toInteger" and "fromInteger")
haftmann@34899
   407
  (Scala "!_.as'_BigInt" and "!Nat.Nat((_))")
haftmann@25931
   408
haftmann@25931
   409
text {* Conversion from and to indices. *}
haftmann@25931
   410
haftmann@31205
   411
code_const Code_Numeral.of_nat
haftmann@25967
   412
  (SML "IntInf.toInt")
haftmann@31377
   413
  (OCaml "_")
haftmann@27673
   414
  (Haskell "fromEnum")
haftmann@34902
   415
  (Scala "!_.as'_Int")
haftmann@25967
   416
haftmann@31205
   417
code_const Code_Numeral.nat_of
haftmann@25931
   418
  (SML "IntInf.fromInt")
haftmann@31377
   419
  (OCaml "_")
haftmann@27673
   420
  (Haskell "toEnum")
haftmann@34899
   421
  (Scala "!Nat.Nat((_))")
haftmann@25931
   422
haftmann@25931
   423
text {* Using target language arithmetic operations whenever appropriate *}
haftmann@25931
   424
haftmann@25931
   425
code_const "op + \<Colon> nat \<Rightarrow> nat \<Rightarrow> nat"
haftmann@25931
   426
  (SML "IntInf.+ ((_), (_))")
haftmann@25931
   427
  (OCaml "Big'_int.add'_big'_int")
haftmann@25931
   428
  (Haskell infixl 6 "+")
haftmann@34899
   429
  (Scala infixl 7 "+")
haftmann@34899
   430
haftmann@34899
   431
code_const "op - \<Colon> nat \<Rightarrow> nat \<Rightarrow> nat"
haftmann@34899
   432
  (Haskell infixl 6 "-")
haftmann@34899
   433
  (Scala infixl 7 "-")
haftmann@25931
   434
haftmann@25931
   435
code_const "op * \<Colon> nat \<Rightarrow> nat \<Rightarrow> nat"
haftmann@25931
   436
  (SML "IntInf.* ((_), (_))")
haftmann@25931
   437
  (OCaml "Big'_int.mult'_big'_int")
haftmann@25931
   438
  (Haskell infixl 7 "*")
haftmann@34899
   439
  (Scala infixl 8 "*")
haftmann@25931
   440
haftmann@26100
   441
code_const divmod_aux
haftmann@26009
   442
  (SML "IntInf.divMod/ ((_),/ (_))")
haftmann@26009
   443
  (OCaml "Big'_int.quomod'_big'_int")
haftmann@26009
   444
  (Haskell "divMod")
haftmann@34899
   445
  (Scala infixl 8 "/%")
haftmann@34899
   446
haftmann@34899
   447
code_const divmod_nat
haftmann@34899
   448
  (Haskell "divMod")
haftmann@34899
   449
  (Scala infixl 8 "/%")
haftmann@25931
   450
haftmann@28346
   451
code_const "eq_class.eq \<Colon> nat \<Rightarrow> nat \<Rightarrow> bool"
haftmann@25931
   452
  (SML "!((_ : IntInf.int) = _)")
haftmann@25931
   453
  (OCaml "Big'_int.eq'_big'_int")
haftmann@25931
   454
  (Haskell infixl 4 "==")
haftmann@34899
   455
  (Scala infixl 5 "==")
haftmann@25931
   456
haftmann@25931
   457
code_const "op \<le> \<Colon> nat \<Rightarrow> nat \<Rightarrow> bool"
haftmann@25931
   458
  (SML "IntInf.<= ((_), (_))")
haftmann@25931
   459
  (OCaml "Big'_int.le'_big'_int")
haftmann@25931
   460
  (Haskell infix 4 "<=")
haftmann@34899
   461
  (Scala infixl 4 "<=")
haftmann@25931
   462
haftmann@25931
   463
code_const "op < \<Colon> nat \<Rightarrow> nat \<Rightarrow> bool"
haftmann@25931
   464
  (SML "IntInf.< ((_), (_))")
haftmann@25931
   465
  (OCaml "Big'_int.lt'_big'_int")
haftmann@25931
   466
  (Haskell infix 4 "<")
haftmann@34899
   467
  (Scala infixl 4 "<")
haftmann@25931
   468
haftmann@25931
   469
consts_code
haftmann@28522
   470
  "0::nat"                     ("0")
haftmann@28522
   471
  "1::nat"                     ("1")
haftmann@25931
   472
  Suc                          ("(_ +/ 1)")
haftmann@25931
   473
  "op + \<Colon>  nat \<Rightarrow> nat \<Rightarrow> nat"   ("(_ +/ _)")
haftmann@25931
   474
  "op * \<Colon>  nat \<Rightarrow> nat \<Rightarrow> nat"   ("(_ */ _)")
haftmann@25931
   475
  "op \<le> \<Colon>  nat \<Rightarrow> nat \<Rightarrow> bool"  ("(_ <=/ _)")
haftmann@25931
   476
  "op < \<Colon>  nat \<Rightarrow> nat \<Rightarrow> bool"  ("(_ </ _)")
haftmann@25931
   477
haftmann@25931
   478
haftmann@28228
   479
text {* Evaluation *}
haftmann@28228
   480
haftmann@28562
   481
lemma [code, code del]:
haftmann@32657
   482
  "(Code_Evaluation.term_of \<Colon> nat \<Rightarrow> term) = Code_Evaluation.term_of" ..
haftmann@28228
   483
haftmann@32657
   484
code_const "Code_Evaluation.term_of \<Colon> nat \<Rightarrow> term"
haftmann@28228
   485
  (SML "HOLogic.mk'_number/ HOLogic.natT")
haftmann@28228
   486
haftmann@28228
   487
haftmann@25931
   488
text {* Module names *}
haftmann@23854
   489
haftmann@23854
   490
code_modulename SML
haftmann@33364
   491
  Efficient_Nat Arith
haftmann@23854
   492
haftmann@23854
   493
code_modulename OCaml
haftmann@33364
   494
  Efficient_Nat Arith
haftmann@23854
   495
haftmann@23854
   496
code_modulename Haskell
haftmann@33364
   497
  Efficient_Nat Arith
haftmann@23854
   498
wenzelm@36176
   499
hide_const int
haftmann@23854
   500
haftmann@23854
   501
end