caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Pierre Weis <pierre.weis@inria.fr>
To: basile.starynkevitch@inria.fr (Basile Starynkevitch [local])
Cc: caml-list@inria.fr
Subject: Re: [Caml-list] kprintf with user formatters
Date: Fri, 16 Jul 2004 08:47:36 +0200 (MET DST)	[thread overview]
Message-ID: <200407160647.IAA02290@pauillac.inria.fr> (raw)
In-Reply-To: <20040715142214.GB28587@bourg.inria.fr> from "Basile Starynkevitch [local]" at "Jul 15, 104 04:22:14 pm"

> On Thu, Jul 15, 2004 at 03:45:30PM +0200, Markus Mottl wrote:
> > On Thu, 15 Jul 2004, Basile Starynkevitch [local] wrote:
> > > I don't understand what this zprintf function should be,
> > 
> > It should parse the format string, and ignore format arguments following
> > it.
> 
> let zprintf fmt = Printf.kprintf (fun _ -> ()) fmt
> should be ok.
> 
> But I think that the format string should not even be parsed. I've got
> no solution (except the one below) to this!

Well, you just need to think functionnally!

If I understand properly:

- you want to skip the runtime time penalty of formatting the
arguments to string before discarding the result,
- you even want not to parse the format string,
- ideally you also want NOT TO EVALUATE the reminding arguments of
your printf call ?

Hmm, this sounds extremely lazy to me; so this suggests thunk
programming; hey, we have that in the language, so let's go!

let log level thunk =
 if may_log level then thunk ();;

...

 log 2 (fun () ->
   eprintf "Argument 1 is hard to compute %d\n" (ackermann x x))

That's the way we use to log in the OcamlP3l compiler: we've got no
runtime penalty if there is no necessity to log. Moreover, this
solution is general enough to accomodate threads, side effects, or
whatever.

To me the (fun () -> ) additional verbosity is not so bad: it clearly
emphasizes that nothing at all is evaluated when logging is unnecessary.

To go beyond that, we would need some help from the language that
would offer some provision for debugging from a special debug keyword,
semantically reminiscent to lazy and assert (as a kind of combined
semantics of both constructs). A compiler flag would then
automatically remove the debugging code (as is done for assert with
the -noassert flag) and the compiler will automatically insert the
(fun () -> ) as it already does in the case of lazy ...

[...]
> Actually, I more and more hate printf, both in C and in Ocaml. I
> really believe it is a nightmare.

Could you elaborate a bit ? We worked hard to provide a clean fully
typed interface to printf and, well ... we like using it :)

We may have missed the point that makes your printf experience ``a
nightmare'', and having information on that could be helpful to
improve the implementation. 

> A possible suggestion might be to 
>   add a StopPrintf exception to Printf
> and
>   add a tryprintf function to Printf, which takes a prologue function
> as an argument. If the prologue raises an exception, no formatting
> occur;  otherwise, tryprintf works like kprintf

I don't think the problem is in printf. The problem is in the way you
call printf (no printf variant could prevent evaluating the format
string and evaluating the arguments of its call: this is mandatory due
to the semantics of the language).

Best regards,

Pierre Weis

INRIA, Projet Cristal, Pierre.Weis@inria.fr, http://pauillac.inria.fr/~weis/


-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


  parent reply	other threads:[~2004-07-16  6:47 UTC|newest]

Thread overview: 56+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2004-06-30 16:32 Damien
2004-07-14 21:10 ` Pierre Weis
2004-07-15  0:17   ` Markus Mottl
2004-07-15  7:30     ` David MENTRE
2004-07-15  7:59       ` Jean-Christophe Filliatre
2004-07-15 23:35         ` henri dubois-ferriere
2004-07-15  7:39     ` Damien
2004-07-15 12:19       ` Markus Mottl
2004-07-15 12:42         ` Basile Starynkevitch [local]
2004-07-15 13:45           ` Markus Mottl
2004-07-15 14:22             ` Basile Starynkevitch [local]
2004-07-15 14:57               ` Markus Mottl
2004-07-16  6:47               ` Pierre Weis [this message]
2004-07-16  7:13                 ` Jean-Christophe Filliatre
2004-07-16  7:23                   ` henri dubois-ferriere
2004-07-16  7:44                     ` Jean-Christophe Filliatre
2004-07-16 17:56                   ` Markus Mottl
2004-07-19  9:17                   ` Pierre Weis
2004-07-19  9:32                     ` Jean-Christophe Filliatre
2004-07-16  7:21                 ` henri dubois-ferriere
2004-07-16 17:44                 ` Markus Mottl
2004-07-19 10:10                   ` Pierre Weis
2004-07-19 10:43                     ` Jon Harrop
2004-07-21 15:52                       ` Pierre Weis
2004-07-21 17:43                         ` lazyness in ocaml (was : [Caml-list] kprintf with user formatters) Daniel Bünzli
2004-07-22 16:28                           ` Pierre Weis
2004-07-22 17:03                             ` William Lovas
2004-07-22 23:00                             ` skaller
2004-07-23  3:32                               ` William Lovas
2004-07-28  7:26                               ` Pierre Weis
2004-07-28  8:06                                 ` skaller
2004-07-28  8:29                                   ` Daniel Bünzli
2004-07-28  9:13                                   ` Pierre Weis
2004-07-28  9:36                                     ` skaller
2004-07-28  9:38                                     ` skaller
2004-07-28 10:17                                 ` Jason Smith
2004-07-28 12:31                                   ` skaller
2004-07-21 20:41                         ` [Caml-list] kprintf with user formatters Jon Harrop
2004-07-22 15:39                           ` Pierre Weis
2004-07-22 22:16                             ` [Caml-list] lazy evaluation: [Was: kprintf with user formatters] skaller
2004-07-22 22:42                             ` [Caml-list] kprintf with user formatters skaller
2004-07-22  8:05                         ` [Caml-list] wait instruction lehalle@miriad
2004-07-22  8:40                           ` Olivier Andrieu
2004-07-22 10:35                             ` lehalle@miriad
2004-07-22 10:33                           ` Vitaly Lugovsky
2004-07-16  6:17             ` [Caml-list] kprintf with user formatters Pierre Weis
2004-07-16 17:14               ` Markus Mottl
2004-07-19 10:00                 ` Pierre Weis
2004-07-16  6:02       ` Pierre Weis
2004-07-16  8:42         ` Damien
2004-07-19  9:00           ` Pierre Weis
2004-07-16 16:52         ` Markus Mottl
2004-07-19  9:28           ` Pierre Weis
2004-07-15 22:20     ` Pierre Weis
2004-07-15 23:01       ` Markus Mottl
2004-07-16 16:17     ` james woodyatt

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=200407160647.IAA02290@pauillac.inria.fr \
    --to=pierre.weis@inria.fr \
    --cc=basile.starynkevitch@inria.fr \
    --cc=caml-list@inria.fr \
    /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).