From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Delivered-To: caml-list@yquem.inria.fr Received: from nez-perce.inria.fr (nez-perce.inria.fr [192.93.2.78]) by yquem.inria.fr (Postfix) with ESMTP id 74327BC48 for ; Wed, 6 Apr 2005 21:23:51 +0200 (CEST) Received: from pauillac.inria.fr (pauillac.inria.fr [128.93.11.35]) by nez-perce.inria.fr (8.13.0/8.13.0) with ESMTP id j36JNpV5006156 for ; Wed, 6 Apr 2005 21:23:51 +0200 Received: from nez-perce.inria.fr (nez-perce.inria.fr [192.93.2.78]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id VAA16121 for ; Wed, 6 Apr 2005 21:23:50 +0200 (MET DST) Received: from furbychan.cocan.org (furbychan.cocan.org [80.68.91.176]) by nez-perce.inria.fr (8.13.0/8.13.0) with ESMTP id j36JNnnJ006153 (version=TLSv1/SSLv3 cipher=EDH-RSA-DES-CBC3-SHA bits=168 verify=NO) for ; Wed, 6 Apr 2005 21:23:50 +0200 Received: from rich by furbychan.cocan.org with local (Exim 3.35 #1 (Debian)) id 1DJG7x-0002Nz-00 for ; Wed, 06 Apr 2005 20:23:49 +0100 Date: Wed, 6 Apr 2005 20:23:48 +0100 To: caml-list@inria.fr Subject: Re: [Caml-list] ambitious proposal: polymorphic arithmetics Message-ID: <20050406192347.GB6338@furbychan.cocan.org> References: <20050406.111505.68543084.eijiro_sumii@anet.ne.jp> <87psx7lszi.fsf@qrnik.zagroda> <38697.131.254.50.45.1112810460.squirrel@mail.irisa.fr> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <38697.131.254.50.45.1112810460.squirrel@mail.irisa.fr> User-Agent: Mutt/1.3.28i From: Richard Jones X-Miltered: at nez-perce with ID 42543747.000 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Miltered: at nez-perce with ID 42543745.000 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Spam: no; 0.00; caml-list:01 irisa:01 bool:01 pointer:01 pointer:01 bool:01 runtime:01 printf:01 prerr:01 endline:01 endline:01 cvs:01 sourceforge:01 viewcvs:01 ocaml:01 X-Spam-Checker-Version: SpamAssassin 3.0.2 (2004-11-16) on yquem.inria.fr X-Spam-Status: No, score=0.0 required=5.0 tests=none autolearn=disabled version=3.0.2 X-Spam-Level: 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