caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Dmitry Bely <dbely@mail.ru>
To: Peter Jolly <peter@jollys.org>
Cc: caml-list <caml-list@inria.fr>
Subject: Re: [Caml-list] camlp4 pa_macro
Date: Thu, 08 Sep 2005 12:47:04 +0400	[thread overview]
Message-ID: <irxc0wvr.fsf@mail.ru> (raw)
In-Reply-To: <431ED6FB.9010009@jollys.org> (Peter Jolly's message of "Wed, 07 Sep 2005 13:03:07 +0100")

Peter Jolly <peter@jollys.org> writes:

>> Is it possible to achieve with pa_macro something like this:
>> 
>> IFDEF NDEBUG THEN
>> DEFINE LOG(expr) = ()
>> ELSE
>> DEFINE LOG(expr) = Printf.printf expr
>> ENDIF
>> ...
>> LOG("x=%d,y=%d" x y);
>> 
>> Unfortunately the code above does not work: debug version is OK, but then
>> NDEBUG is turned on I have
>> 
>> "This expression is not a function, it cannot be applied" on LOG()
>> expression.
>
> Getting camlp4 to pretty-print the code after macro expansion is a
> useful technique for debugging this sort of problem:
>
> 	$ camlp4 pa_o.cmo pa_op.cmo pr_o.cmo pa_macro.cmo test.ml
> 	...
> 	Printf.printf ("x=%d,y=%d" x y)
>
> It should be clear why that isn't working.

I see. Thank you very much for the info.

>
>> If I use
>> 
>> LOG "x=%d,y=%d" x y;
>> 
>> then the release version surprisingly works, but the debug one gives
>> 
>> "Parse error: currified constructor"
>
> Yes, because this does not pass any arguments to the LOG macro - it
> expands it with an empty <expr>.  So this works in the latter case,
> because LOG just expands to "Printf.printf", but in the former case you
> end up with
>
> 	() "x=%d,y=%d" x y
>
> which is a syntax error as reported.
>
>> How to overcome this?
>
> 	IFDEF NDEBUG THEN
> 	DEFINE LOG = Printf.kprintf ignore
> 	ELSE
> 	DEFINE LOG = Printf.printf
> 	ENDIF

It's quite useless - 

let debug = ref true

let log fmt =
 if !debug then
   Printf.kprintf print_string fmt
 else
   Printf.kprintf ignore fmt

will in fact give the same result. It does not solve the initial problem -
completely remove the debugging code from the release binary.

> Or just replace all instances of LOG with "if debug then Printf.printf",
> on the grounds that the compiler is probably clever enough to prune
> conditions that always evaluate to false, and you probably won't notice
> any significant difference in speed even if it isn't.

But the debugging code (format strings etc.) will be there. OK, the best I
can get now is

IFDEF NDEBUG THEN
DEFINE LOG(expr) = ()
ELSE
let dprintf = Printf.printf
DEFINE LOG(expr) = expr
ENDIF
...
LOG(dprintf "x=%d,y=%d" x y);

Not very elegant, but works.

- Dmitry Bely


  parent reply	other threads:[~2005-09-08  8:43 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2005-09-06  8:07 Dmitry Bely
2005-09-06 19:35 ` [Caml-list] camlp4 pa_macro (doc request) Martin Jambon
2005-09-06 21:55   ` Remi Vanicat
2005-09-07  8:29     ` [Caml-list] camlp4 pa_macro Dmitry Bely
2005-09-07  8:40       ` Remi Vanicat
2005-09-07 12:03 ` Peter Jolly
2005-09-07 12:28   ` Basile STARYNKEVITCH
2005-09-07 19:46     ` Jonathan Roewen
2005-09-08  8:47   ` Dmitry Bely [this message]
2005-09-08 12:48     ` David MENTRE

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=irxc0wvr.fsf@mail.ru \
    --to=dbely@mail.ru \
    --cc=caml-list@inria.fr \
    --cc=peter@jollys.org \
    /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).