Commit efba4b89 authored by Pietro Abate's avatar Pietro Abate

[r2004-07-05 13:48:09 by afrisch] bigint

Original author: afrisch
Date: 2004-07-05 13:48:10+00:00
parent 6695b505
......@@ -49,6 +49,7 @@ and typ_descr = function
| Builtin ("list", [t])
| Builtin ("array", [t]) -> Types.descr (Sequence.star_node (typ t))
| Builtin ("Pervasives.ref", [t]) -> Builtin_defs.ref_type (typ t)
| Builtin ("Big_int.big_int", []) -> Builtin_defs.int
| Builtin ("Cduce_lib.Value.t", []) -> Types.any
| Builtin ("Cduce_lib.Encodings.Utf8.t", []) -> Builtin_defs.string
| Builtin ("unit", []) -> Sequence.nil_type
......@@ -271,7 +272,8 @@ and to_cd_descr e = function
let set = <:expr< fun $lid:y$ -> $e$.val := $tr_y$ >> in
<:expr< Value.mk_ext_ref $tt$ $get$ $set$ >>
)
| Builtin ("Big_int.big_int", []) ->
<:expr< Value.ocaml2cduce_bigint $e$ >>
| Builtin ("Cduce_lib.Value.t", []) -> e
| Builtin ("Cduce_lib.Encodings.Utf8.t", []) ->
<:expr< Value.ocaml2cduce_string_utf8 $e$ >>
......@@ -395,6 +397,8 @@ and to_ml_descr e = function
let e = <:expr< Value.get_field $e$ $label_ascii "get"$ >> in
let e = <:expr< Eval.eval_apply $e$ Value.nil >> in
<:expr< Pervasives.ref $to_ml e t$ >>
| Builtin ("Big_int.big_int", []) ->
<:expr< Value.cduce2ocaml_bigint $e$ >>
| Builtin ("Cduce_lib.Value.t", []) -> e
| Builtin ("Cduce_lib.Encodings.Utf8.t", []) ->
<:expr< Value.cduce2ocaml_string_utf8 $e$ >>
......
......@@ -83,6 +83,7 @@ let builtins =
[
"list"; "Pervasives.ref";
"unit"; "array";
"Big_int.big_int";
"Cduce_lib.Value.t"; "Cduce_lib.Encodings.Utf8.t"
]
......
......@@ -612,6 +612,12 @@ let cduce2ocaml_char = function
| Char c -> Chars.V.to_char c
| _ -> assert false
let ocaml2cduce_bigint i =
Integer (Intervals.V.from_bigint i)
let cduce2ocaml_bigint = function
| Integer i -> Intervals.V.get_bigint i
| _ -> assert false
let print_utf8 v =
print_string (U.get_str v);
......
......@@ -112,6 +112,8 @@ val ocaml2cduce_string_utf8 : U.t -> t
val cduce2ocaml_string_utf8 : t -> U.t
val ocaml2cduce_char : char -> t
val cduce2ocaml_char : t -> char
val ocaml2cduce_bigint : Big_int.big_int -> t
val cduce2ocaml_bigint : t -> Big_int.big_int
val print_utf8: U.t -> unit
......@@ -38,6 +38,12 @@ eval:
./$@
.PHONY: cdnum
cdnum:
$(CAML) -c $@.mli
$(CDUCE) --compile $@.cd -I `ocamlfind query num`
ocamlfind $(CAML) -o $@ -pp "$(CDO2ML) -static" -impl $@.cdo -package cduce,num -linkpkg
./$@
clean:
rm -f *.cmo *.cmx *.o *.cdo *.cmi a.ml *~ a
......@@ -16,6 +16,7 @@ let equal = eq_big_int
let check i = ()
let from_int i = big_int_of_int i
let from_bigint i = i
(* TODO: better serialization of bigints !!! *)
let serialize t i = Serialize.Put.string t (string_of_big_int i)
......@@ -24,6 +25,7 @@ let deserialize t = big_int_of_string (Serialize.Get.string t)
let mk = big_int_of_string
let to_string = string_of_big_int
let get_int = int_of_big_int
let get_bigint i = i
let is_int = is_int_big_int
let add = add_big_int
let mult = mult_big_int
......
......@@ -3,10 +3,12 @@ module V : sig
val print : Format.formatter -> t -> unit
val mk: string -> t
val from_int: int -> t
val from_bigint: Big_int.big_int -> t
val to_string: t -> string
val is_int: t -> bool
val get_int: t -> int
val get_bigint: t -> Big_int.big_int
val is_zero: t -> bool
val add: t -> t -> t
......
......@@ -103,7 +103,9 @@ type <code>Any</code>. The corresponding translation functions are the
identity. This can be used to avoid multiple copies when translating
a complex value back and forth between CDuce and OCaml.
The type <code>Cduce_lib.Encodings.Utf8.t</code> is translated to the CDuce
type <code>String</code>.
type <code>String</code>.
The type <code>Big_int.big_int</code> is translated to the CDuce
type <code>Int</code>.
</li>
<li>
......@@ -157,6 +159,7 @@ The canonical translation is summarized in the following box:
<tr><td><tt>Cduce_lib.Value.t</tt></td><td><tt>Any</tt></td></tr>
<tr><td><tt>Cduce_lib.Encodings.Utf8.t</tt></td><td><tt>String</tt></td></tr>
<tr><td><tt>Big_int.big_int</tt></td><td><tt>Int</tt></td></tr>
</table>
<p>
......@@ -467,6 +470,27 @@ ocamlfind ocamlc -o eval -pp "cdo2ml -static" -impl eval.cdo \
</section>
<section title="Use CDuce to compute the factorial on big integers">
<sample>
(* File cdnum.mli: *)
val fact: Big_int.big_int -> Big_int.big_int
(* File cdnum.cd: *)
let aux ((Int,Int) -> Int)
| (x, 0 | 1) -> x
| (x, n) -> aux (x * n, n - 1)
let fact (x : Int) : Int = aux (Big_int.unit_big_int, x)
(* Could write 1 instead of Big_int.unit_big_int. Just for fun. *)
</sample>
</section>
</box>
</page>
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment