caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* Re: [Caml-list] global record
@ 2006-07-19 17:14 Eric Breck
  2006-07-19 19:41 ` On language extensions (was Re: [Caml-list] global record) Daniel Bünzli
  0 siblings, 1 reply; 14+ messages in thread
From: Eric Breck @ 2006-07-19 17:14 UTC (permalink / raw)
  To: caml-list

[-- Attachment #1: Type: text/plain, Size: 3028 bytes --]


> Date: Wed, 19 Jul 2006 15:07:29 +0100
> From: Richard Jones <rich@annexia.org>
> Subject: Re: [Caml-list] global record
> To: Andreas Biegert <andreas.biegert@googlemail.com>
> Cc: caml-list@yquem.inria.fr

>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"

<gauche_self_promotion>

I was also writing code that looked like that, and I got tired of the
duplication, and also the non-locality - each new option requires a
modification in several places.  So the pa_arg syntax extension I just
announced makes this a lot easier.  Except for the reading of values from the
dotfile, all the above code is generated from this declaration (not tested):

open Parseopt

type option myopts = {
username = "" : help = "Adwords account username"; string;
password = "" : help = "Adwords account password";  string;
client: help = "Adwords account client (optional)"; string;
token = "": help = "Adwords account token"; string;
update = false: help = "Perform updates"; action = set; bool;
verbose = false: help = "Be verbose"; action = set; bool;
}
let myopts = {myopts with keyspecs = [Long2]} in (** specify you want field
foo => --foo *)

let options, args = parse_argv myopts in

...

if options.verbose then print "this is verbose mode!\n"

...

You also get a detailed usage and help message and a function to convert the
returned options to a string, plus further customizability.

http://www.cs.cornell.edu/~ebreck/pa_arg

-E r i c

</gauche_self_promotion>

[-- Attachment #2: Type: text/html, Size: 4376 bytes --]

^ permalink raw reply	[flat|nested] 14+ messages in thread

* On language extensions (was Re: [Caml-list] global record)
  2006-07-19 17:14 [Caml-list] global record Eric Breck
@ 2006-07-19 19:41 ` Daniel Bünzli
  2006-07-19 19:57   ` Richard Jones
                     ` (2 more replies)
  0 siblings, 3 replies; 14+ messages in thread
From: Daniel Bünzli @ 2006-07-19 19:41 UTC (permalink / raw)
  To: list caml-list

Hello,

I would like to issue a warning about everybody rolling its own  
syntax extension to suit its taste.

Contrary to lisp's macros, each syntax extension is syntactically a  
new language. Hence not only do we need to learn a new semantics but  
also a new syntax. If I have to read someone else's code I want to  
read caml code and not caml code augmented with the author's  
syntactic obsessions.

In code readability, there is a trade-off between succinctness and  
syntactic regularity. Camlp4 extensions increase the former but  
decrease the latter. For me camlp4's usage should be limited to real  
domain specific languages (e.g. like coq does) and research, it  
should not be used to increase ocaml's succinctness.

This may be seen as a matter of personal taste: it is true that I  
lean towards syntactic regularity, i.e. less syntactic constructs.  
However there is another argument against using extensions: code  
maintenance. You have no guarantee that (1) an extension will not be  
broken by the next version of the language and (2) that the author  
will continue to maintain it.

And by the way, are we guaranteed that two arbitrary camlp4  
extensions will compose or may the order of application matter ?

Cheers,

Daniel

P.S. Even for domain specific languages many things can be done in  
pure ocaml by embedding the dsl in ocaml using meta-programming  
techniques.


^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: On language extensions (was Re: [Caml-list] global record)
  2006-07-19 19:41 ` On language extensions (was Re: [Caml-list] global record) Daniel Bünzli
@ 2006-07-19 19:57   ` Richard Jones
  2006-07-19 20:22     ` Stefano Zacchiroli
  2006-07-19 21:33   ` Nicolas Pouillard
  2006-07-20  2:57   ` Martin Jambon
  2 siblings, 1 reply; 14+ messages in thread
From: Richard Jones @ 2006-07-19 19:57 UTC (permalink / raw)
  To: Daniel Bünzli; +Cc: list caml-list

On Wed, Jul 19, 2006 at 09:41:46PM +0200, Daniel Bünzli wrote:
> And by the way, are we guaranteed that two arbitrary camlp4  
> extensions will compose or may the order of application matter ?

I'm probably wrong, but I think the only way to compose two camlp4
extensions is to combine the source code to both together and compile
a new extension.  Is there some other way?

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


^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: On language extensions (was Re: [Caml-list] global record)
  2006-07-19 19:57   ` Richard Jones
@ 2006-07-19 20:22     ` Stefano Zacchiroli
  0 siblings, 0 replies; 14+ messages in thread
From: Stefano Zacchiroli @ 2006-07-19 20:22 UTC (permalink / raw)
  To: list caml-list

On Wed, Jul 19, 2006 at 08:57:44PM +0100, Richard Jones wrote:
> I'm probably wrong, but I think the only way to compose two camlp4
> extensions is to combine the source code to both together and compile
> a new extension.  Is there some other way?

Camlp4 extensions are regular .cmo/.cma files which, upon loading,
dynamically extends a grammar. You can "compose" multiple extensions by
passing all of them to your preferred camlp4 pre-processor.

Merely loading multiple extensions of course does not grant you obtain a
grammar able to parse anything at all ...

Cheers.

-- 
Stefano Zacchiroli -*- Computer Science PhD student @ Uny Bologna, Italy
zack@{cs.unibo.it,debian.org,bononia.it} -%- http://www.bononia.it/zack/
If there's any real truth it's that the entire multidimensional infinity
of the Universe is almost certainly being run by a bunch of maniacs. -!-


^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: On language extensions (was Re: [Caml-list] global record)
  2006-07-19 19:41 ` On language extensions (was Re: [Caml-list] global record) Daniel Bünzli
  2006-07-19 19:57   ` Richard Jones
@ 2006-07-19 21:33   ` Nicolas Pouillard
  2006-07-20  2:57   ` Martin Jambon
  2 siblings, 0 replies; 14+ messages in thread
From: Nicolas Pouillard @ 2006-07-19 21:33 UTC (permalink / raw)
  To: Daniel Bünzli; +Cc: list caml-list

On 7/19/06, Daniel Bünzli <daniel.buenzli@epfl.ch> wrote:
> Hello,
>
> I would like to issue a warning about everybody rolling its own
> syntax extension to suit its taste.
>
> Contrary to lisp's macros, each syntax extension is syntactically a
> new language. Hence not only do we need to learn a new semantics but
> also a new syntax. If I have to read someone else's code I want to
> read caml code and not caml code augmented with the author's
> syntactic obsessions.
>
> In code readability, there is a trade-off between succinctness and
> syntactic regularity. Camlp4 extensions increase the former but
> decrease the latter. For me camlp4's usage should be limited to real
> domain specific languages (e.g. like coq does) and research, it
> should not be used to increase ocaml's succinctness.
>

[...]

Camlp4 is "just" a pre-processor, you always can unsugar your code and
take back a plain ocaml code. This can be done to get rid of a no more
maintained extension, or to understand some code that use such an
extension.

$ camlp4o pa_foo.cmo pa_bar.cmo pr_o.cmo input.ml -o output.ml

You can now read output.ml as a plain ocaml file.

Thus, IMHO, people can add syntax extensions since we have an easy way
to get rid of them.

-- 
Nicolas Pouillard

^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: On language extensions (was Re: [Caml-list] global record)
  2006-07-19 19:41 ` On language extensions (was Re: [Caml-list] global record) Daniel Bünzli
  2006-07-19 19:57   ` Richard Jones
  2006-07-19 21:33   ` Nicolas Pouillard
@ 2006-07-20  2:57   ` Martin Jambon
  2 siblings, 0 replies; 14+ messages in thread
From: Martin Jambon @ 2006-07-20  2:57 UTC (permalink / raw)
  To: Daniel Bünzli; +Cc: list caml-list

[-- Attachment #1: Type: TEXT/PLAIN, Size: 299 bytes --]

On Wed, 19 Jul 2006, Daniel Bünzli wrote:

> Hello,
>
> I would like to issue a warning about everybody rolling its own syntax 
> extension to suit its taste.

Well, it's good to suit your own taste when you write scripts for
yourself :-)


Martin

--
Martin Jambon, PhD
http://martin.jambon.free.fr

^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: [Caml-list] global record
  2006-07-19 11:11 global record Andreas Biegert
                   ` (2 preceding siblings ...)
  2006-07-19 14:07 ` Richard Jones
@ 2006-07-20  7:26 ` Jeff Henrikson
  3 siblings, 0 replies; 14+ messages in thread
From: Jeff Henrikson @ 2006-07-20  7:26 UTC (permalink / raw)
  To: Andreas Biegert; +Cc: caml-list

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



Andreas Biegert wrote:

> 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



^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: [Caml-list] global record
  2006-07-19 14:50     ` Richard Jones
@ 2006-07-19 15:09       ` Jean-Christophe Filliatre
  0 siblings, 0 replies; 14+ messages in thread
From: Jean-Christophe Filliatre @ 2006-07-19 15:09 UTC (permalink / raw)
  To: Richard Jones; +Cc: Yoann Padioleau, caml-list, Andreas Biegert


Richard Jones writes:
 > On Wed, Jul 19, 2006 at 04:39:32PM +0200, Yoann Padioleau wrote:
 > > Richard Jones <rich@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


^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: [Caml-list] global record
  2006-07-19 14:39   ` Yoann Padioleau
  2006-07-19 14:50     ` Richard Jones
@ 2006-07-19 15:08     ` William D. Neumann
  1 sibling, 0 replies; 14+ messages in thread
From: William D. Neumann @ 2006-07-19 15:08 UTC (permalink / raw)
  To: Yoann Padioleau; +Cc: Richard Jones, caml-list, Andreas Biegert

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


^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: [Caml-list] global record
  2006-07-19 14:39   ` Yoann Padioleau
@ 2006-07-19 14:50     ` Richard Jones
  2006-07-19 15:09       ` Jean-Christophe Filliatre
  2006-07-19 15:08     ` William D. Neumann
  1 sibling, 1 reply; 14+ messages in thread
From: Richard Jones @ 2006-07-19 14:50 UTC (permalink / raw)
  To: Yoann Padioleau; +Cc: Andreas Biegert, caml-list

On Wed, Jul 19, 2006 at 04:39:32PM +0200, Yoann Padioleau wrote:
> Richard Jones <rich@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


^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: [Caml-list] global record
  2006-07-19 14:07 ` Richard Jones
@ 2006-07-19 14:39   ` Yoann Padioleau
  2006-07-19 14:50     ` Richard Jones
  2006-07-19 15:08     ` William D. Neumann
  0 siblings, 2 replies; 14+ messages in thread
From: Yoann Padioleau @ 2006-07-19 14:39 UTC (permalink / raw)
  To: Richard Jones; +Cc: Andreas Biegert, caml-list

Richard Jones <rich@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


^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: [Caml-list] global record
  2006-07-19 11:11 global record Andreas Biegert
  2006-07-19 11:42 ` [Caml-list] " Pietro Abate
       [not found] ` <3d13dcfc0607190528y624e5c9eg2c45e1cb17ec3771@mail.gmail.com>
@ 2006-07-19 14:07 ` Richard Jones
  2006-07-19 14:39   ` Yoann Padioleau
  2006-07-20  7:26 ` Jeff Henrikson
  3 siblings, 1 reply; 14+ messages in thread
From: Richard Jones @ 2006-07-19 14:07 UTC (permalink / raw)
  To: Andreas Biegert; +Cc: caml-list

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


^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: [Caml-list] global record
       [not found] ` <3d13dcfc0607190528y624e5c9eg2c45e1cb17ec3771@mail.gmail.com>
@ 2006-07-19 12:29   ` David MENTRE
  0 siblings, 0 replies; 14+ messages in thread
From: David MENTRE @ 2006-07-19 12:29 UTC (permalink / raw)
  To: Ocaml

Oops, forgot the caml-list.

d.

2006/7/19, David MENTRE <david.mentre@gmail.com>:
> Hello,
>
> 2006/7/19, Andreas Biegert <andreas.biegert@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.
>


^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: [Caml-list] global record
  2006-07-19 11:11 global record Andreas Biegert
@ 2006-07-19 11:42 ` Pietro Abate
       [not found] ` <3d13dcfc0607190528y624e5c9eg2c45e1cb17ec3771@mail.gmail.com>
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 14+ messages in thread
From: Pietro Abate @ 2006-07-19 11:42 UTC (permalink / raw)
  To: caml-list

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


^ permalink raw reply	[flat|nested] 14+ messages in thread

end of thread, other threads:[~2006-07-20  7:18 UTC | newest]

Thread overview: 14+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2006-07-19 17:14 [Caml-list] global record Eric Breck
2006-07-19 19:41 ` On language extensions (was Re: [Caml-list] global record) Daniel Bünzli
2006-07-19 19:57   ` Richard Jones
2006-07-19 20:22     ` Stefano Zacchiroli
2006-07-19 21:33   ` Nicolas Pouillard
2006-07-20  2:57   ` Martin Jambon
  -- strict thread matches above, loose matches on Subject: below --
2006-07-19 11:11 global record Andreas Biegert
2006-07-19 11:42 ` [Caml-list] " Pietro Abate
     [not found] ` <3d13dcfc0607190528y624e5c9eg2c45e1cb17ec3771@mail.gmail.com>
2006-07-19 12:29   ` David MENTRE
2006-07-19 14:07 ` Richard Jones
2006-07-19 14:39   ` Yoann Padioleau
2006-07-19 14:50     ` Richard Jones
2006-07-19 15:09       ` Jean-Christophe Filliatre
2006-07-19 15:08     ` William D. Neumann
2006-07-20  7:26 ` Jeff Henrikson

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).