Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

[Caml-list] global record

2 views
Skip to first unread message

Andreas Biegert

unread,
Jul 19, 2006, 7:16:33 AM7/19/06
to caml...@yquem.inria.fr
Hi,

I am developing a bioinformatics sequence analysis application which
contains about 20 modules. One of those modules, the 'Par' module,
encapsulates a record of about 30 configuration parameters needed
throughout the whole application. The parameter record is mostly
static but some values can be overwritten by command-line options. Is
there a way to make the (possibly modified) parameters record globally
accessable throughout all modules? This would be much more convenient
than having to pass the parameters record to virtually all functions
in my application. THX for helping.

Andreas

_______________________________________________
Caml-list mailing list. Subscription management:
http://yquem.inria.fr/cgi-bin/mailman/listinfo/caml-list
Archives: http://caml.inria.fr
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
Bug reports: http://caml.inria.fr/bin/caml-bugs

Pietro Abate

unread,
Jul 19, 2006, 7:45:33 AM7/19/06
to caml...@yquem.inria.fr
On Wed, Jul 19, 2006 at 01:11:22PM +0200, Andreas Biegert wrote:
> static but some values can be overwritten by command-line options. Is
> there a way to make the (possibly modified) parameters record globally
> accessable throughout all modules? This would be much more convenient
> than having to pass the parameters record to virtually all functions
> in my application. THX for helping.

For my application I've a module where each runtime option is a ref and
then I use the Arg module to set the proper value. Don't know if there
are better way of doing this...

:)
p

--
++ Blog: http://blog.rsise.anu.edu.au/?q=pietro
++
++ "All great truths begin as blasphemies." -George Bernard Shaw
++ Please avoid sending me Word or PowerPoint attachments.
See http://www.fsf.org/philosophy/no-word-attachments.html

David MENTRE

unread,
Jul 19, 2006, 8:38:04 AM7/19/06
to Ocaml
Oops, forgot the caml-list.

d.

2006/7/19, David MENTRE <david....@gmail.com>:
> Hello,
>
> 2006/7/19, Andreas Biegert <andreas...@googlemail.com>:


> > Is
> > there a way to make the (possibly modified) parameters record globally
> > accessable throughout all modules?
>

> Define a global variable in the Par module, use it in the other modules.
>
> ==par.ml==
> type param = { mutable a: int; }
>
> let params = { a = 3; }
> ==
>
> ==foo.ml==
> let bar = Par.params.a
>
> let _ = Par.params.a <- bar + 1
> ==


>
> > This would be much more convenient
> > than having to pass the parameters record to virtually all functions
> > in my application.
>

> <personal taste>I find this purely functional approach much cleaner
> because you know where your global variables are read or modified
> directly by looking at function parameters.</personal taste>
>
> Best wishes,
> d.

Richard Jones

unread,
Jul 19, 2006, 10:10:46 AM7/19/06
to Andreas Biegert, caml...@yquem.inria.fr
On Wed, Jul 19, 2006 at 01:11:22PM +0200, Andreas Biegert wrote:
> I am developing a bioinformatics sequence analysis application which
> contains about 20 modules. One of those modules, the 'Par' module,
> encapsulates a record of about 30 configuration parameters needed
> throughout the whole application. The parameter record is mostly
> static but some values can be overwritten by command-line options. Is
> there a way to make the (possibly modified) parameters record globally
> accessable throughout all modules? This would be much more convenient
> than having to pass the parameters record to virtually all functions
> in my application. THX for helping.

This is a bit ugly, but we use it in our Adwords API toolkit:

-------------------------------------------------- stdargs.mli
val username : string
val password : string
val client : string option
val token : string
val update : bool
val verbose : bool
val args : string list

-------------------------------------------------- stdargs.ml
let username = ref ""
let password = ref ""
let client = ref ""
let token = ref ""
let update = ref false
let verbose = ref false
let args = ref []

(* followed by some code which tries to read default values
* from $HOME/.adwordsapidata -- code omitted
*)

(* parse the command line parameters *)
let argspec = [
"--username", Arg.Set_string username, "Adwords account username.";
"--password", Arg.Set_string password, "Adwords account password.";
"--client", Arg.Set_string client, "Adwords account client (optional).";
"--token", Arg.Set_string token, "Adwords account token.";
"--update", Arg.Set update, "Perform updates.";
"--verbose", Arg.Set verbose, "Be verbose.";
]

let anon_fn str = args := str :: !args
let usage =
Sys.executable_name ^ " [--options]"

let () = Arg.parse argspec anon_fn usage

let username = !username
let password = !password
let client = if !client = "" then None else Some !client
let token = !token
let update = !update
let verbose = !verbose
let args = List.rev !args

---

Then the code just stuff like:

open Stdargs

if verbose then printf "this is verbose mode\n"

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

Yoann Padioleau

unread,
Jul 19, 2006, 10:44:49 AM7/19/06
to Richard Jones, caml...@yquem.inria.fr, Andreas Biegert
Richard Jones <ri...@annexia.org> writes:

> On Wed, Jul 19, 2006 at 01:11:22PM +0200, Andreas Biegert wrote:
>> I am developing a bioinformatics sequence analysis application which
>> contains about 20 modules. One of those modules, the 'Par' module,
>> encapsulates a record of about 30 configuration parameters needed
>> throughout the whole application. The parameter record is mostly
>> static but some values can be overwritten by command-line options. Is
>> there a way to make the (possibly modified) parameters record globally
>> accessable throughout all modules? This would be much more convenient
>> than having to pass the parameters record to virtually all functions
>> in my application. THX for helping.
>
> This is a bit ugly, but we use it in our Adwords API toolkit:
>
> -------------------------------------------------- stdargs.mli
> val username : string

You certainly mean
val username: string ref

> val password : string
> val client : string option
> val token : string
> val update : bool
> val verbose : bool
> val args : string list
>
> -------------------------------------------------- stdargs.ml
> let username = ref ""
> let password = ref ""
> let client = ref ""
> let token = ref ""
> let update = ref false
> let verbose = ref false
> let args = ref []
>

[ ... ]


>
> Then the code just stuff like:
>
> open Stdargs
>
> if verbose then printf "this is verbose mode\n"

You certainly mean

if !verbose then printf "this is verbose mode\n"

>
> Rich.

--
pad

Richard Jones

unread,
Jul 19, 2006, 10:53:56 AM7/19/06
to Yoann Padioleau, caml...@yquem.inria.fr, Andreas Biegert
On Wed, Jul 19, 2006 at 04:39:32PM +0200, Yoann Padioleau wrote:
> Richard Jones <ri...@annexia.org> writes:
> > val username : string
>
> You certainly mean
> val username: string ref

No, I mean:

val username : string

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

_______________________________________________

William D. Neumann

unread,
Jul 19, 2006, 11:09:58 AM7/19/06
to Yoann Padioleau, caml...@yquem.inria.fr, Andreas Biegert, Richard Jones
On Wed, 19 Jul 2006, Yoann Padioleau wrote:

> You certainly mean
>
> if !verbose then printf "this is verbose mode\n"

No. After he calls Arg parse, he does:


let username = !username
let password = !password
let client = if !client = "" then None else Some !client
let token = !token
let update = !update
let verbose = !verbose
let args = List.rev !args

and loses the refs.

William D. Neumann

---

"There's just so many extra children, we could just feed the
children to these tigers. We don't need them, we're not doing
anything with them.

Tigers are noble and sleek; children are loud and messy."

-- Neko Case

Life is unfair. Kill yourself or get over it.
-- Black Box Recorder

Jean-Christophe Filliatre

unread,
Jul 19, 2006, 11:11:13 AM7/19/06
to Richard Jones, caml...@yquem.inria.fr, Andreas Biegert

Richard Jones writes:
> On Wed, Jul 19, 2006 at 04:39:32PM +0200, Yoann Padioleau wrote:
> > Richard Jones <ri...@annexia.org> writes:
> > > val username : string
> >
> > You certainly mean
> > val username: string ref
>
> No, I mean:
>
> val username : string

and I confirm :-)

I use the same trick of dereferencing the references used for
command-line parsing as soon as it is done and I find this very
convenient. This can be realized in the very first module on your
ocaml link command, and thus in whole the remaining of your code you
do not need to use the deref operator (!) anymore.

--
Jean-Christophe

Jeff Henrikson

unread,
Jul 20, 2006, 3:20:53 AM7/20/06
to Andreas Biegert, caml...@yquem.inria.fr
The full generalization of this is dynamic scoping, as you may know.
There's a discussion of the implementation in emacs in the emacs
manual. Not thread safe but can be made so with an immutable binary
tree, eg the Map module. You could map either strings or polymorphic
variants to your config value type, probably a regular variant. I'm not
sure how much of a performance savings polymoprhic variants would be
over strings.

For a couple of other approaches see
http://caml.inria.fr/pub/ml-archives/caml-list/2004/12/a0924032de03d517cb8cb8f2adde6c94.en.html


Jeff

0 new messages