caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: emmanuel.engel@ota.fr.socgen.com
To: Don Syme <dsyme@microsoft.com>
Cc: "'David.Mentre@irisa.fr'" <David.Mentre@irisa.fr>, caml-list@inria.fr
Subject: Re[2]: How to cleanly encode "quasi-constants"?
Date: Fri, 23 Jun 2000 10:04:14 +0200	[thread overview]
Message-ID: <C1256907.002C586C.00@pouilly.ota.marc.societe-generale.fr> (raw)



What prevent you from doing arbitrary computations at initialization time ?
These computations can be used to initialize your quasi-constants.

eg :

let s =
  let arg1 = ref false
  in  Arg.parse  [("arg1", Arg.Set arg1)] (fun _ -> ()) "" ;
      if arg1 then "arg1 is set" else "arg1 is not set"


let main () =
   print_string s ;

let _ = main ()


Theses initalizations can be done in different modules. As long as i can
remember the order
of modules on command line at link time is relevent for initialization order.


Emmanuel Engel

______________________________ Reply Separator _________________________________



Don Syme <dsyme@microsoft.com>
06/21/2000 07:59:22 PM GMT



|-----------------------------------------------------------+------------------|
|                                                           |Return Receipt: No|
| To:  "'David.Mentre@irisa.fr'" <David.Mentre@irisa.fr>    |Importance: Normal|
|      caml-list@inria.fr                                   |                  |
| cc:   (bcc: Emmanuel ENGEL/eqty/fr/socgen)                |                  |
|                                                           |                  |
|-----------------------------------------------------------+------------------|




SUBJECT: RE: How to cleanly encode "quasi-constants"?




You have spotted a problem with ML-style type systems - they are not great
at encoding the kind of "phasing" of initialization that you refer to
(neither most are other type systems, though of course one could imagine a
type system that allowed you to express this sort of thing).  However, the
"constants" you refer to can encode quite complex computations (including
allocation of data structures, e.g. let x = [2;3;5]]), which is a big
advantage compared to, say, C, but it does not cover everything.  So in
practice people tend to make do with constant values until the
initialization sequence gets too complicated, then move to references, but
with the knowledge that these references are set once-for-all.  Hiding the
references behind functions is, as you say, often a good idea.

Basically, don't be afraid of using references for this kind of thing - it's
one of the reasons they are in the language.

Cheers,
Don


-----Original Message-----
From: David.Mentre@irisa.fr [mailto:David.Mentre@irisa.fr]
Sent: 20 June 2000 17:00
To: caml-list@inria.fr
Subject: How to cleanly encode "quasi-constants"?


Hello dear camlists,

I'm still learning caml and functionnal programming, so this is maybe a
newbie question. Please don't be too rude.

In my program, I have variables that can only be modified once
(potentially at initialization time after parsing command line and done
some computation) and then are never modified for a very long time (rest
of program execution). I call them "quasi-constants", they have a
write-once semantic. They are used throught the program so it is very
painfull to propagate them along function calls (i.e. these variables
need a global scope, at least from a programmer point of view).

Right now, I'm using constants (let var = ...) or mutable references
(let var := ...) to encode such things.

However I'm not satisfied. With constants, I obviously can only modify
them at compile time. With mutables, I can modify them but potentially
at any time, meaning potential bugs. So my question is:

 What is the Right Way(tm) to encode such a thing in an ML like
 language and more specifically in OCaml?

I was thinking of using a functor (return a module with functions
referencing these quasi-constants and quasi-constants values given at
module creation). Is it right? It is also possible to use functions,
however is seems impracticable for a real program (this approach creates
a function many pages long):

# let create_manipulator v =
  let f1 () = v
  and f2 x = v+x in (f1, f2);;
  val create_manipulator : int -> (unit -> int) * (int -> int) = <fun>
# let (g1, g2) = create_manipulator 3;;
val g1 : unit -> int = <fun>
val g2 : int -> int = <fun>
# g1 ();;
- : int = 3


Two side questions:

 - is there such a problem tackled in a big program ? (ocaml compiler?)

 - it seems to me that Arg.Set and Arg.Clear types have this semantic
   (an option is only set at parse time).



Best regards,
david
--
 David.Mentre@irisa.fr -- http://www.irisa.fr/prive/dmentre/
 Opinions expressed here are only mine.






                 reply	other threads:[~2000-06-23 14:18 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=C1256907.002C586C.00@pouilly.ota.marc.societe-generale.fr \
    --to=emmanuel.engel@ota.fr.socgen.com \
    --cc=David.Mentre@irisa.fr \
    --cc=caml-list@inria.fr \
    --cc=dsyme@microsoft.com \
    /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).