caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: John Carr <jfc@mit.edu>
To: caml-list@inria.fr
Subject: Re: [Caml-list] Conditionally boxed 32 bit integers?
Date: Mon, 05 Sep 2011 11:16:33 -0400	[thread overview]
Message-ID: <201109051516.p85FGX80005823@outgoing.mit.edu> (raw)
In-Reply-To: <20110905072553.GA32471@ccellier.rd.securactive.lan>


rixed@happyleptic.org wrote:

> I suppose you don't want to use any kind of preprocessor, do you ?

I'd rather not preprocess (or compile alternate files).  Both the
netnumber and core implementations use a preprocessor.

Preprocessor-based solutions have two problems.  One is the extra
compilation step.  Another is bitrot in the unused case.

I think it was Plan 9 where the documentation said don't use the
C preprocessor, our compiler does the right thing with if (false).
In ocaml what I'd like to say is

	module Int : sig
	  type t
	  val add : t -> t -> t
	  ...
	end;
	module Intfast : Int = struct type t = int ... end
	module Int = if sixtyfour then Int end else Int32

with the understanding that the expression sixtyfour is evaluated
at compile time.

Here is some similar code using 3.12 syntax:

	module type M = sig type t val add : t -> t -> t val of_int : int -> t end;;
	module Int : M = struct type t = int let add = (+) let of_int x = x end;;
	module M = (val (if Sys.word_size = 64 then (module Int : M) else (module Int32 : M)) : M);;

	let f x = M.add (M.of_int x) (M.of_int 1)

If the test (Sys.word_size = 64) is replaced by (true) this does what
I want.  The compiler looks through the packing and unpacking and
sees the module Int.  The body of function f compiles to a machine add
instruction.

Ocamlopt does not evaluate (Sys.word_size = 64) at compile time
because Sys.word_size is initialized at runtime.  See sys.ml line 26
and run objinfo on sys.cmx.

INRIA developers, is it easy to add an intrinsic so we can write

	extern word_size : int = "%caml_word_size"

in sys.ml?  If sys.cmx has a constant definition, ocamlopt should do
constant folding on conditional expressions testing Sys.word_size.

If I want to do conditional compilation, can ocamlbuild be taught
"compile file a.ml as module X on 32 bit systems and module b.ml as
module X on 64 bit systems"?

    --John Carr (jfc@mit.edu)

  reply	other threads:[~2011-09-05 15:16 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-09-04 15:22 John Carr
2011-09-04 17:28 ` Hezekiah M. Carty
2011-09-04 20:58 ` Richard W.M. Jones
2011-09-05  7:25 ` rixed
2011-09-05 15:16   ` John Carr [this message]
2011-09-05 22:11     ` John Carr
2011-09-06  7:50       ` Fabrice Le Fessant
2011-09-05  9:50 ` Gerd Stolpmann

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=201109051516.p85FGX80005823@outgoing.mit.edu \
    --to=jfc@mit.edu \
    --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).