From mboxrd@z Thu Jan 1 00:00:00 1970 Received: (from majordomo@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id OAA20800; Mon, 28 May 2001 14:36:03 +0200 (MET DST) X-Authentication-Warning: pauillac.inria.fr: majordomo set sender to owner-caml-list@pauillac.inria.fr using -f Received: from concorde.inria.fr (concorde.inria.fr [192.93.2.39]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id OAA20792 for ; Mon, 28 May 2001 14:36:02 +0200 (MET DST) Received: from pauillac.inria.fr (pauillac.inria.fr [128.93.11.35]) by concorde.inria.fr (8.11.1/8.10.0) with ESMTP id f4SCWcj07391; Mon, 28 May 2001 14:32:39 +0200 (MET DST) Received: (from xleroy@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id OAA20744; Mon, 28 May 2001 14:32:38 +0200 (MET DST) Date: Mon, 28 May 2001 14:32:38 +0200 From: Xavier Leroy To: David Fox Cc: caml-list@inria.fr Subject: Re: [Caml-list] ocaml and named constants Message-ID: <20010528143238.D19801@pauillac.inria.fr> References: Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Mailer: Mutt 1.0i In-Reply-To: ; from dsf@hci.ucsd.edu on Wed, May 23, 2001 at 10:06:19AM -0700 Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk > I've noticed that there is no notion of a named constant in ML. I > sort of assumed that the compiler could determine that uses of a > variable after a declaration like > > let mpg_joint_stereo = 1 > > could be compiled as a constant provided the value was never altered > and no references to the variable were created. Does the compiler > actually do this? Yes, ocamlopt does this for integer constants (and "integer-like" constants such as characters, booleans, constant constructors) as part of its integer constant propagation phase. > I can't see how it could do it for a variable that > wasn't module local. The information "mpg_joint_stereo is equal to 1" is stored in the .cmx file associated with the module, along with other cross-module optimization information. > I ask because it seems odd that camlp4 includes > a feature for creating real defined constants that are textually > substituted before compilation begins. One motivation for this is to be able to put named constants in patterns, e.g. match get_next_byte() with mpg_joint_stereo -> ... | mpg_78rpm -> ... | _ -> ... which cannot be done in plain ML. Another motivation could be to get "constant inlining" in bytecode and for non-integer constants. > Would it be a better bet to use a function like > > let is_joint_stereo head = (mpg_mode head) = 1 > > and hope the compiler inlines the function? Inlining and integer constant propagation are performed simultaneously, so this would be at best equivalent (if both the function and the constant are inlined) or less efficient (if neither is inlined). - Xavier Leroy ------------------- To unsubscribe, mail caml-list-request@inria.fr. Archives: http://caml.inria.fr