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; 11+ 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] 11+ messages in thread
* Re: On language extensions (was Re: [Caml-list] global record)
@ 2006-07-20  1:12 Eric Breck
  2006-07-20  5:16 ` skaller
  2006-07-20 12:42 ` Bünzli Daniel
  0 siblings, 2 replies; 11+ messages in thread
From: Eric Breck @ 2006-07-20  1:12 UTC (permalink / raw)
  To: caml-list


> From: Daniel_Bünzli <daniel.buenzli@e...>
> Subject: On language extensions (was Re: [Caml-list] global record)
>
> 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.

Succinctness is not precisely what I was after so much as locality -  
in this case, having each option declared in a single location - as  
opposed to the type definition in one place, the default value in  
another, the Arg.spec in another, code to print the chosen value of  
each option in another, and, optionally, the de-ref-ication in  
another.  Such locality is a basic principle of software engineering,  
and in this case I don't really know how to achieve it with only a  
library (and not a syntax extension).

I'm not advocating extending the language willy-nilly, but I feel  
that any new library already requires some amount of learning to use  
its particular API, and if a small, local extension to the language  
vastly shrinks (and localizes) code required to interface to the  
library, it seems reasonable to me (IMHO).

To put it another way, I do not agree with the adage that "syntactic  
sugar causes cancer of the semicolon." :)

As far as readability, I tried to choose syntax that would make clear  
to a casual reader what was going on, but clearly I'm blinded by  
having stared at this for awhile now.  As Nicolas Pouillard points  
out, you can always unsugar the code.

Anyway, I've been using variants of this library for half a year or  
so, and I've found it very convenient, so I thought I would offer it  
to others.

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

I'm not sure why either 1 or 2 is more true for a syntax extension  
than for an arbitrary library, assuming the syntax extension doesn't  
try to make large-scale changes to the grammar.

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

I'm not sure what you mean here.  I understand meta-programming to  
mean (roughly) generating code programatically.  A solution along  
those lines to my problem (locality of command-line arguments) would  
be something like this: specify the options in some file, write some  
piece of code to process this and produce an ocaml file, which is  
then compiled and linked with the main program.  I actually  
originally did something along these lines, but then decided that  
since this approach also requires defining and processing a new  
syntax (of the option file), I would use camlp4 and integrate that  
syntax into my main program.  That is, camlp4 *is* meta-programming,  
it's just that it (a) integrates the DSL into the main program, and  
(b) hides the intermediate file.

But perhaps I'm misunderstanding your point - if you could clarify  
what you mean, I'd like to know what alternate approaches are.

Finally, I have the sense (perhaps I'm completely wrong) that the  
post I'm responding to was motivated in part by a thought that  
command-line option parsing is somehow not worthy of a domain- 
specific language / syntax extension.  I suppose it is something of a  
"syntactic obsession" with me to find a clear and concise way of  
specifying such options, since I write mostly small, command-line  
programs to which I want to be able to quickly add variant  
functionality.  Clearly, the benefit to any particular program is  
small - when you write enough of them, though, the lifetime savings  
in tearing-my-hair-out is, I think, a win.

best,

-E r i c

(PS: sorry if this doesn't get threaded appropriately, I read the  
digest of the list).


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

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

Thread overview: 11+ 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
2006-07-20  1:12 Eric Breck
2006-07-20  5:16 ` skaller
2006-07-20  6:29   ` Stefano Zacchiroli
2006-07-20  8:57     ` Jean-Marie Gaillourdet
2006-07-20 12:42 ` Bünzli Daniel

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