From: Richard Jones <rich@annexia.org>
To: caml-list@inria.fr
Subject: Re: [Caml-list] ambitious proposal: polymorphic arithmetics
Date: Wed, 6 Apr 2005 20:23:48 +0100 [thread overview]
Message-ID: <20050406192347.GB6338@furbychan.cocan.org> (raw)
In-Reply-To: <38697.131.254.50.45.1112810460.squirrel@mail.irisa.fr>
On Wed, Apr 06, 2005 at 08:01:00PM +0200, padiolea@irisa.fr wrote:
> In the module Obj of the caml library there is a function
> external is_int : t -> bool = "%obj_is_int"
> I guess that the value are represented internally as "cells" and that
> cells have a bit indicating wether it is an int or a pointer (and also
> a bit for the gc)
Internally as "values". The LSB indicates whether a value is an int
or a pointer to something boxed. (LSB = 1 => int). For boxed data
the header tells you what it is and how big it is.
> By using more bits we could know wether or not it is a float, and
> so have also a function
> is_float: t -> bool
> and so we could then code a "generic" + function
Floats are normally stored boxed, except when they happen to be in
registers, or they are stored in an array.
Anyway, it's already possible to write some generic functions,
provided you confine yourself to the type information available at
runtime. For example, a slow generic (+) is:
open Printf
type t = Int of int | Float of float
let (+) a b =
let get_type v =
let r = Obj.repr v in
if Obj.is_int r then
Int (Obj.magic r : int)
else if Obj.size r = 2 && Obj.tag r = Obj.double_tag then
Float (Obj.magic r : float)
else (
prerr_endline ("size = " ^ string_of_int (Obj.size r) ^ ", tag = " ^
string_of_int (Obj.tag r));
invalid_arg "(+): arguments must have type int or float"
)
in
match get_type a, get_type b with
| Int a, Int b -> Int (a + b)
| Float a, Float b -> Float (a +. b)
| Int a, Float b -> Float (float a +. b)
| Float a, Int b -> Float (a +. float b)
let string_of_t = function
| Int a -> string_of_int a
| Float a -> string_of_float a
let () =
print_endline (string_of_t (1 + 2));
print_endline (string_of_t (1. + 2));
print_endline (string_of_t (1 + 2.));
print_endline (string_of_t (1. + 2.))
See also the function 'dump' in:
http://cvs.sourceforge.net/viewcvs.py/ocaml-lib/extlib-dev/std.ml?rev=1.15&view=markup
> My big wish for ocaml would be to have some better tracing facilities,
> a generic print function, and the possibilty to print backtraces,
Being able to print full backtraces (with function names and
parameters) would be just great.
Rich.
--
Richard Jones, CTO Merjis Ltd.
Merjis - web marketing and technology - http://merjis.com
Team Notepad - intranets and extranets for business - http://team-notepad.com
next prev parent reply other threads:[~2005-04-06 19:23 UTC|newest]
Thread overview: 25+ messages / expand[flat|nested] mbox.gz Atom feed top
2005-04-06 15:15 Eijiro Sumii
2005-04-06 15:51 ` [Caml-list] " Sébastien Hinderer
2005-04-06 15:56 ` Richard Jones
2005-04-06 16:43 ` Dmitry Lomov
2005-04-06 18:59 ` Richard Jones
2005-04-06 19:19 ` Jacques Carette
2005-04-07 0:01 ` Ethan Aubin
2005-04-06 16:39 ` [Caml-list] " William Lovas
2005-04-06 16:59 ` Andreas Rossberg
2005-04-06 18:50 ` Eijiro Sumii
2005-04-06 19:33 ` Eijiro Sumii
2005-04-07 0:13 ` William Lovas
2005-04-07 1:58 ` Jacques Garrigue
2005-04-06 17:00 ` Christophe TROESTLER
2005-04-06 19:20 ` Eijiro Sumii
2005-04-07 14:00 ` Christophe TROESTLER
2005-04-06 17:23 ` Marcin 'Qrczak' Kowalczyk
2005-04-06 18:01 ` padiolea
2005-04-06 19:14 ` Eijiro Sumii
2005-04-06 20:31 ` Eijiro Sumii
2005-04-06 21:53 ` Marcin 'Qrczak' Kowalczyk
2005-04-06 22:38 ` Eijiro Sumii
2005-04-06 19:23 ` Richard Jones [this message]
2005-04-09 2:58 ` Jon Harrop
2005-04-09 3:16 ` Eijiro Sumii
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20050406192347.GB6338@furbychan.cocan.org \
--to=rich@annexia.org \
--cc=caml-list@inria.fr \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).