caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] Printf question
@ 2003-09-26 18:02 Richard Jones
  2003-09-26 19:04 ` Alain.Frisch
  2003-09-27  0:11 ` Olivier Andrieu
  0 siblings, 2 replies; 28+ messages in thread
From: Richard Jones @ 2003-09-26 18:02 UTC (permalink / raw)
  To: caml-list

I want to write a function which is a bit like Printf.printf, but
handles the %s placeholder differently. It needs to do SQL-style
escaping, eg:

	my_printf "The string: %s" "string with 'quotes'"

would return the string:

	The string: 'string with ''quotes'''

(hope I've got that right ...)

Anyway, it looks like this should be possible, and possibly even quite
simple, with a custom formatter.

Can anyone give me a clue about this? Is Printf.printf implemented in
pure OCaml, or does it require trickery in the compiler to work? The
code is scarily complex ...

Rich.

-- 
Richard Jones. http://www.annexia.org/ http://freshmeat.net/users/rwmj
Merjis Ltd. http://www.merjis.com/ - all your business data are belong to you.
MAKE+ is a sane replacement for GNU autoconf/automake. One script compiles,
RPMs, pkgs etc. Linux, BSD, Solaris. http://www.annexia.org/freeware/makeplus/

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


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

* Re: [Caml-list] Printf question
  2003-09-26 18:02 [Caml-list] Printf question Richard Jones
@ 2003-09-26 19:04 ` Alain.Frisch
  2003-09-29  7:44   ` Mike Potanin
  2003-09-27  0:11 ` Olivier Andrieu
  1 sibling, 1 reply; 28+ messages in thread
From: Alain.Frisch @ 2003-09-26 19:04 UTC (permalink / raw)
  To: Richard Jones; +Cc: caml-list

On Fri, 26 Sep 2003, Richard Jones wrote:

> I want to write a function which is a bit like Printf.printf, but
> handles the %s placeholder differently. It needs to do SQL-style
> escaping, eg:
>
> 	my_printf "The string: %s" "string with 'quotes'"

If you're ready to write

  <:my_printf<The string: %s> "string with 'quotes'"

instead, you can write your own quotation expander with Camlp4 and
implement the behavior you want.  Mmmh, a re-implementation of the
Printf and Format modules with this technique would be nice.
And also for Scanf (with a pattern quotation expander?).

Yes, the typing of the printf functions are magic, and their
implementation also. You cannot adapt their behavior without hacking the
compiler and using the Obj module.

-- Alain

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


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

* Re: [Caml-list] Printf question
  2003-09-26 18:02 [Caml-list] Printf question Richard Jones
  2003-09-26 19:04 ` Alain.Frisch
@ 2003-09-27  0:11 ` Olivier Andrieu
  2003-09-27  7:23   ` Richard Jones
  1 sibling, 1 reply; 28+ messages in thread
From: Olivier Andrieu @ 2003-09-27  0:11 UTC (permalink / raw)
  To: Richard Jones; +Cc: caml-list

 Richard Jones [Friday 26 September 2003] :
 >
 > I want to write a function which is a bit like Printf.printf, but
 > handles the %s placeholder differently. It needs to do SQL-style
 > escaping, eg:
 > 
 > 	my_printf "The string: %s" "string with 'quotes'"
 > 
 > would return the string:
 > 
 > 	The string: 'string with ''quotes'''
 > 
 > (hope I've got that right ...)
 > 
 > Anyway, it looks like this should be possible, and possibly even quite
 > simple, with a custom formatter.

You could use the %a format. See the Prinf module documentation : 

   * `a': user-defined printer. Takes two arguments and apply the first
     one to `outchan' (the current output channel) and to the second
     argument. The first argument must therefore have type `out_channel
     -> 'b -> unit' and the second `'b'.  The output produced by the
     function is therefore inserted in the output of `fprintf' at the
     current point.

(that's for fprintf, for sprintf the printer would be of type 
 `unit -> 'b -> string')

-- 
   Olivier

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


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

* Re: [Caml-list] Printf question
  2003-09-27  0:11 ` Olivier Andrieu
@ 2003-09-27  7:23   ` Richard Jones
  2003-09-27  8:20     ` Basile Starynkevitch
  0 siblings, 1 reply; 28+ messages in thread
From: Richard Jones @ 2003-09-27  7:23 UTC (permalink / raw)
  Cc: caml-list

On Sat, Sep 27, 2003 at 02:11:14AM +0200, Olivier Andrieu wrote:
> You could use the %a format. See the Prinf module documentation : 
> 
>    * `a': user-defined printer. Takes two arguments and apply the first
>      one to `outchan' (the current output channel) and to the second
>      argument. The first argument must therefore have type `out_channel
>      -> 'b -> unit' and the second `'b'.  The output produced by the
>      function is therefore inserted in the output of `fprintf' at the
>      current point.
> 
> (that's for fprintf, for sprintf the printer would be of type 
>  `unit -> 'b -> string')

I guess the problem with this is that I lose any type safety,
which is the whole point of doing things this way.

Rich.

-- 
Richard Jones. http://www.annexia.org/ http://freshmeat.net/users/rwmj
Merjis Ltd. http://www.merjis.com/ - all your business data are belong to you.
C2LIB is a library of basic Perl/STL-like types for C. Vectors, hashes,
trees, string funcs, pool allocator: http://www.annexia.org/freeware/c2lib/

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


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

* Re: [Caml-list] Printf question
  2003-09-27  7:23   ` Richard Jones
@ 2003-09-27  8:20     ` Basile Starynkevitch
  2003-09-27  9:14       ` Richard Jones
  0 siblings, 1 reply; 28+ messages in thread
From: Basile Starynkevitch @ 2003-09-27  8:20 UTC (permalink / raw)
  To: Richard Jones; +Cc: caml-list

On Sat, Sep 27, 2003 at 08:23:25AM +0100, Richard Jones wrote:
> On Sat, Sep 27, 2003 at 02:11:14AM +0200, Olivier Andrieu wrote:
> > You could use the %a format. [...]
> 
> I guess the problem with this is that I lose any type safety,
> which is the whole point of doing things this way.
> 

I don't think that %a in formats lose type safety (provided that the passed
function has the correct type).

# let pr= function 0->"zero" | 1->"one" | n -> string_of_int n;;
val pr : int -> string = <fun>
# let myprint n = Printf.sprintf "n=%a" (fun () n->pr n) n;;
val myprint : int -> string = <fun>
# myprint 1;;
- : string = "n=one"

-- 
Basile STARYNKEVITCH -- basile dot starynkevitch at inria dot fr
Project cristal.inria.fr - phone +33 1 3963 5197 - mobile 6 8501 2359
http://cristal.inria.fr/~starynke --- all opinions are only mine 

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


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

* Re: [Caml-list] Printf question
  2003-09-27  8:20     ` Basile Starynkevitch
@ 2003-09-27  9:14       ` Richard Jones
  2003-09-27  9:39         ` Maxence Guesdon
  2003-09-29 16:42         ` Pierre Weis
  0 siblings, 2 replies; 28+ messages in thread
From: Richard Jones @ 2003-09-27  9:14 UTC (permalink / raw)
  Cc: caml-list

On Sat, Sep 27, 2003 at 10:20:07AM +0200, Basile Starynkevitch wrote:
> On Sat, Sep 27, 2003 at 08:23:25AM +0100, Richard Jones wrote:
> > On Sat, Sep 27, 2003 at 02:11:14AM +0200, Olivier Andrieu wrote:
> > > You could use the %a format. [...]
> > 
> > I guess the problem with this is that I lose any type safety,
> > which is the whole point of doing things this way.
> > 
> 
> I don't think that %a in formats lose type safety (provided that the passed
> function has the correct type).
> 
> # let pr= function 0->"zero" | 1->"one" | n -> string_of_int n;;
> val pr : int -> string = <fun>
> # let myprint n = Printf.sprintf "n=%a" (fun () n->pr n) n;;
> val myprint : int -> string = <fun>
> # myprint 1;;
> - : string = "n=one"

This is right. I didn't make my original point clear.

I'd like to write something like:

let sth = dbh#prepare "select salary from emp where id = %d and name = %s" in
let res = sth#execute 1 "Jones" in
(* ... *)

The type-safety issue is that the arguments to the #execute method be
checked at compile-time.

Using %a I need to write something like:

let sth = dbh#prepare "select salary from emp where id = %a and name = %a" in
let res = sth#execute int_conversion 1 str_conversion "Jones" in

which is fine but the compiler doesn't check that the id passed is
really an int. I might as well have written:

let sth = dbh#prepare "select salary from emp where id = ? and name = ?" in
let res = sth#execute [ `Int 1; `String "Jones" ] in

and just defer the checking to runtime (in fact, defer it to the
database in this case).

There was a previous posting to this list which discussed this
approach, and I'm exploring it as a possible way to wrap database
connections for mod_caml - see also:
http://caml.inria.fr/archives/200306/msg00218.html and also the
response to that message from Christophe Troestler.

To be completely safe at compile time, the OCaml code and the database
tables would need to be generated from some common source. That
approach is probably too heavyweight.

Rich.

-- 
Richard Jones. http://www.annexia.org/ http://freshmeat.net/users/rwmj
Merjis Ltd. http://www.merjis.com/ - all your business data are belong to you.
 All new technology is irrelevant until it is taken up by the public.

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


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

* Re: [Caml-list] Printf question
  2003-09-27  9:14       ` Richard Jones
@ 2003-09-27  9:39         ` Maxence Guesdon
  2003-09-29 16:42         ` Pierre Weis
  1 sibling, 0 replies; 28+ messages in thread
From: Maxence Guesdon @ 2003-09-27  9:39 UTC (permalink / raw)
  To: caml-list

Hi,

> To be completely safe at compile time, the OCaml code and the database
> tables would need to be generated from some common source. That
> approach is probably too heavyweight.

In DBForge, included in Cameleon, you define your tables, with some
information about the ocaml type for each column and the functions to
tranlate to and from these ocaml type. Then dbforge generates a module
with a module per table, with the following functions in each module:
create, insert, update, delete, select, drop. 
The functions have types using the types of the columns you indicated in
the schema. Thus you get for free the main functions to access each table
separately.

A future extension is the possibility to define sql queries. Dbforge would
then check the queries against the schema, and embed them in functions
(still with the types you specified for columns). And all this at compile
time.

DBForge can generate code for OCaml-Mysql, OCaml-Postgresql and OCamlODBC.

Cameleon: http://savannah.nongnu.org/projects/cameleon

Regards,

- Maxence Guesdon

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


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

* Re: [Caml-list] Printf question
  2003-09-26 19:04 ` Alain.Frisch
@ 2003-09-29  7:44   ` Mike Potanin
  0 siblings, 0 replies; 28+ messages in thread
From: Mike Potanin @ 2003-09-29  7:44 UTC (permalink / raw)
  To: caml-list

On Fri, 26 Sep 2003 Alain.Frisch@ens.fr wrote:

> On Fri, 26 Sep 2003, Richard Jones wrote:
>
> If you're ready to write
>
>   <:my_printf<The string: %s> "string with 'quotes'"

Simple example http://wtk.ru/pm/fp/caml/qprintf.tgz
It is more effective standart "dynamic" printf. :-)

>
> instead, you can write your own quotation expander with Camlp4 and
> implement the behavior you want.  Mmmh, a re-implementation of the
> Printf and Format modules with this technique would be nice.
> And also for Scanf (with a pattern quotation expander?).
>
> Yes, the typing of the printf functions are magic, and their
> implementation also. You cannot adapt their behavior without hacking the
> compiler and using the Obj module.
>
> -- Alain
>
> -------------------
> 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
>

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


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

* Re: [Caml-list] Printf question
  2003-09-27  9:14       ` Richard Jones
  2003-09-27  9:39         ` Maxence Guesdon
@ 2003-09-29 16:42         ` Pierre Weis
  2003-09-29 18:13           ` Richard Jones
  1 sibling, 1 reply; 28+ messages in thread
From: Pierre Weis @ 2003-09-29 16:42 UTC (permalink / raw)
  To: Richard Jones; +Cc: caml-list

[...]
> This is right. I didn't make my original point clear.
> 
> I'd like to write something like:
> 
> let sth = dbh#prepare "select salary from emp where id = %d and name = %s" in
> let res = sth#execute 1 "Jones" in
> (* ... *)
> 
> The type-safety issue is that the arguments to the #execute method be
> checked at compile-time.
> 
> Using %a I need to write something like:
> 
> let sth = dbh#prepare "select salary from emp where id = %a and name = %a" in
> let res = sth#execute int_conversion 1 str_conversion "Jones" in
> 
> which is fine but the compiler doesn't check that the id passed is
> really an int. I might as well have written:
> 
> let sth = dbh#prepare "select salary from emp where id = ? and name = ?" in
> let res = sth#execute [ `Int 1; `String "Jones" ] in
> 
> and just defer the checking to runtime (in fact, defer it to the
> database in this case).

[...]

I'm pretty sure that format string contsnats are typechecked
statically with the usual typechecker resolution mechanism (a bit more
complex, but no more magic than the typechecking of the -> type
constructor).

So why not using format values directly ?

For instance:

# let prepare fmt = Printf.printf fmt;;
val prepare : ('a, out_channel, unit) format -> 'a = <fun>

# let sth x =
    prepare "select salary from emp where id = %d and name = %s" x;;
val sth : int -> string -> unit = <fun>

# sth 1 "Jones"
select salary from emp where id = 1 and name = Jones- : unit = ()

This is fully statically typechecked as required.

Or may be I'm missing something ?

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


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

* Re: [Caml-list] Printf question
  2003-09-29 16:42         ` Pierre Weis
@ 2003-09-29 18:13           ` Richard Jones
  2003-09-29 19:57             ` Pierre Weis
  0 siblings, 1 reply; 28+ messages in thread
From: Richard Jones @ 2003-09-29 18:13 UTC (permalink / raw)
  To: Pierre Weis; +Cc: caml-list

On Mon, Sep 29, 2003 at 06:42:09PM +0200, Pierre Weis wrote:
> I'm pretty sure that format string contsnats are typechecked
> statically with the usual typechecker resolution mechanism (a bit more
> complex, but no more magic than the typechecking of the -> type
> constructor).
> 
> So why not using format values directly ?
> 
> For instance:
> 
> # let prepare fmt = Printf.printf fmt;;
> val prepare : ('a, out_channel, unit) format -> 'a = <fun>
> 
> # let sth x =
>     prepare "select salary from emp where id = %d and name = %s" x;;
> val sth : int -> string -> unit = <fun>
> 
> # sth 1 "Jones"
> select salary from emp where id = 1 and name = Jones- : unit = ()
> 
> This is fully statically typechecked as required.
> 
> Or may be I'm missing something ?

I guess the problem is that I want my own interpretation for
%s. It has to do SQL-quoting, otherwise you could write:

# sth 1 "'Jones'; drop database 'company'";; 
select salary from emp where id = 1 and name = 'Jones'; drop database 'company'- : unit = ()

(Or worse ...?)

Rich.

-- 
Richard Jones. http://www.annexia.org/ http://freshmeat.net/users/rwmj
Merjis Ltd. http://www.merjis.com/ - all your business data are belong to you.
NET::FTPSERVER is a full-featured, secure, configurable, database-backed
FTP server written in Perl: http://www.annexia.org/freeware/netftpserver/

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


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

* Re: [Caml-list] Printf question
  2003-09-29 18:13           ` Richard Jones
@ 2003-09-29 19:57             ` Pierre Weis
  2003-09-29 21:50               ` Richard Jones
  0 siblings, 1 reply; 28+ messages in thread
From: Pierre Weis @ 2003-09-29 19:57 UTC (permalink / raw)
  To: Richard Jones; +Cc: pierre.weis, caml-list

> On Mon, Sep 29, 2003 at 06:42:09PM +0200, Pierre Weis wrote:
> > I'm pretty sure that format string contsnats are typechecked
> > statically with the usual typechecker resolution mechanism (a bit more
> > complex, but no more magic than the typechecking of the -> type
> > constructor).
> > 
> > So why not using format values directly ?
> > 
> > For instance:
> > 
> > # let prepare fmt = Printf.printf fmt;;
> > val prepare : ('a, out_channel, unit) format -> 'a = <fun>
> > 
> > # let sth x =
> >     prepare "select salary from emp where id = %d and name = %s" x;;
> > val sth : int -> string -> unit = <fun>
> > 
> > # sth 1 "Jones"
> > select salary from emp where id = 1 and name = Jones- : unit = ()
> > 
> > This is fully statically typechecked as required.
> > 
> > Or may be I'm missing something ?
> 
> I guess the problem is that I want my own interpretation for
> %s. It has to do SQL-quoting, otherwise you could write:

Hence, I think you are seeking for the %a conversion. For instance,
defining a quotation function for SQL as in

# let sql_quoting oc = Printf.fprintf oc "'%s'";;   
val sql_quoting : out_channel -> string -> unit = <fun>

You can define sth with a %a conversion to apply quoting on the fly:

# let sth x =
    prepare "select salary from emp where id = %d and name = %a" x;;
val sth : int -> (out_channel -> 'a -> unit) -> 'a -> unit = <fun>

# sth 1 sql_quoting "Jones";;
select salary from emp where id = 1 and name = 'Jones'- : unit = ()

Still fully type-checked! Sounds better, no ?

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


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

* Re: [Caml-list] Printf question
  2003-09-29 19:57             ` Pierre Weis
@ 2003-09-29 21:50               ` Richard Jones
  2003-09-29 22:36                 ` Pierre Weis
  0 siblings, 1 reply; 28+ messages in thread
From: Richard Jones @ 2003-09-29 21:50 UTC (permalink / raw)
  Cc: caml-list

On Mon, Sep 29, 2003 at 09:57:19PM +0200, Pierre Weis wrote:
> Hence, I think you are seeking for the %a conversion. For instance,
> defining a quotation function for SQL as in
> 
> # let sql_quoting oc = Printf.fprintf oc "'%s'";;   
> val sql_quoting : out_channel -> string -> unit = <fun>
> 
> You can define sth with a %a conversion to apply quoting on the fly:
> 
> # let sth x =
>     prepare "select salary from emp where id = %d and name = %a" x;;
> val sth : int -> (out_channel -> 'a -> unit) -> 'a -> unit = <fun>
> 
> # sth 1 sql_quoting "Jones";;
> select salary from emp where id = 1 and name = 'Jones'- : unit = ()
> 
> Still fully type-checked! Sounds better, no ?

Hmmm ... but as I explained earlier, that's really not type safe
at all.

Rich.

-- 
Richard Jones. http://www.annexia.org/ http://freshmeat.net/users/rwmj
Merjis Ltd. http://www.merjis.com/ - all your business data are belong to you.
C2LIB is a library of basic Perl/STL-like types for C. Vectors, hashes,
trees, string funcs, pool allocator: http://www.annexia.org/freeware/c2lib/

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


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

* Re: [Caml-list] Printf question
  2003-09-29 21:50               ` Richard Jones
@ 2003-09-29 22:36                 ` Pierre Weis
  2003-09-30  8:03                   ` Richard Jones
  2003-09-30 13:19                   ` skaller
  0 siblings, 2 replies; 28+ messages in thread
From: Pierre Weis @ 2003-09-29 22:36 UTC (permalink / raw)
  To: Richard Jones; +Cc: caml-list

> On Mon, Sep 29, 2003 at 09:57:19PM +0200, Pierre Weis wrote:
> > Hence, I think you are seeking for the %a conversion. For instance,
> > defining a quotation function for SQL as in
> > 
> > # let sql_quoting oc = Printf.fprintf oc "'%s'";;   
> > val sql_quoting : out_channel -> string -> unit = <fun>
> > 
> > You can define sth with a %a conversion to apply quoting on the fly:
> > 
> > # let sth x =
> >     prepare "select salary from emp where id = %d and name = %a" x;;
> > val sth : int -> (out_channel -> 'a -> unit) -> 'a -> unit = <fun>
> > 
> > # sth 1 sql_quoting "Jones";;
> > select salary from emp where id = 1 and name = 'Jones'- : unit = ()
> > 
> > Still fully type-checked! Sounds better, no ?
> 
> Hmmm ... but as I explained earlier, that's really not type safe
> at all.
> 
> Rich.

What do you mean by ``not type safe at all'' ?

The common acceptation is ``it could lead to type errors at runtime''.
If you ever exhibit such a problem with format strings, please show us
a complete example, since you indeed discovered an unpleasant bug in
the compiler :(

Remember that our claim for the printf/scanf and format strings
implementation is the same as for the rest of the language: programs
are safe and can never go wrong (i.e. they never produce bus errors or
jeopardize type safety).

(To be precise, this claim evidently does not apply to programs that
could use faulty interfaces with foreign languages, functions from the
Obj module, uncautious input_value/output_value calls, or are compiled
with the -unsafe option.)

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


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

* Re: [Caml-list] Printf question
  2003-09-29 22:36                 ` Pierre Weis
@ 2003-09-30  8:03                   ` Richard Jones
  2003-09-30  8:45                     ` Pierre Weis
  2003-09-30 13:19                   ` skaller
  1 sibling, 1 reply; 28+ messages in thread
From: Richard Jones @ 2003-09-30  8:03 UTC (permalink / raw)
  To: caml-list

On Tue, Sep 30, 2003 at 12:36:59AM +0200, Pierre Weis wrote:
> What do you mean by ``not type safe at all'' ?

Well it's not a bug in the compiler.

Here's an example which isn't type safe:

let sth = dbh#prepare "select name from employees where id = %a" in
sth#execute string_conversion "foo";

To avoid going over the same ground again, here's my original posting
in this thread:

http://caml.inria.fr/archives/200309/msg00294.html

Rich.

-- 
Richard Jones. http://www.annexia.org/ http://freshmeat.net/users/rwmj
Merjis Ltd. http://www.merjis.com/ - all your business data are belong to you.
MONOLITH is an advanced framework for writing web applications in C, easier
than using Perl & Java, much faster and smaller, reusable widget-based arch,
database-backed, discussion, chat, calendaring:
http://www.annexia.org/freeware/monolith/

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


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

* Re: [Caml-list] Printf question
  2003-09-30  8:03                   ` Richard Jones
@ 2003-09-30  8:45                     ` Pierre Weis
  2003-09-30  9:17                       ` Michal Moskal
  0 siblings, 1 reply; 28+ messages in thread
From: Pierre Weis @ 2003-09-30  8:45 UTC (permalink / raw)
  To: Richard Jones; +Cc: caml-list

> On Tue, Sep 30, 2003 at 12:36:59AM +0200, Pierre Weis wrote:
> > What do you mean by ``not type safe at all'' ?
> 
> Well it's not a bug in the compiler.
> 
> Here's an example which isn't type safe:
> 
> let sth = dbh#prepare "select name from employees where id = %a" in
> sth#execute string_conversion "foo";
> 
> To avoid going over the same ground again, here's my original posting
> in this thread:
> 
> http://caml.inria.fr/archives/200309/msg00294.html
> 
> Rich.

I'm sorry to confess I do not understand the example. BTW my fresh Caml
compiler has the same problem :(

        Objective Caml version 3.07

# let sth = dbh#prepare "select name from employees where id = %a" in
  sth#execute string_conversion "foo";
  ;;
Unbound value dbh

Could you give us a self contained example that would run into the
current version and exhibit the ``isn't type safe'' property ?

(To me a type safety problem is of prime importance, and I would fill
better when I understand yours :)

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


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

* Re: [Caml-list] Printf question
  2003-09-30  8:45                     ` Pierre Weis
@ 2003-09-30  9:17                       ` Michal Moskal
  2003-09-30 14:14                         ` Christophe TROESTLER
  0 siblings, 1 reply; 28+ messages in thread
From: Michal Moskal @ 2003-09-30  9:17 UTC (permalink / raw)
  To: Pierre Weis; +Cc: Richard Jones, caml-list

On Tue, Sep 30, 2003 at 10:45:13AM +0200, Pierre Weis wrote:
> > On Tue, Sep 30, 2003 at 12:36:59AM +0200, Pierre Weis wrote:
> > > What do you mean by ``not type safe at all'' ?
> > 
> > Well it's not a bug in the compiler.
> > 
> > Here's an example which isn't type safe:
> > 
> > let sth = dbh#prepare "select name from employees where id = %a" in
> > sth#execute string_conversion "foo";
> > 
> > To avoid going over the same ground again, here's my original posting
> > in this thread:
> > 
> > http://caml.inria.fr/archives/200309/msg00294.html
> > 
> > Rich.
> 
> I'm sorry to confess I do not understand the example. BTW my fresh Caml
> compiler has the same problem :(
> 
>         Objective Caml version 3.07
> 
> # let sth = dbh#prepare "select name from employees where id = %a" in
>   sth#execute string_conversion "foo";
>   ;;
> Unbound value dbh
> 
> Could you give us a self contained example that would run into the
> current version and exhibit the ``isn't type safe'' property ?

The problem isn't that it will crash the executable or something.
Richard means SQL-type-safety. The example (well, SQL tables behind it),
expects integer argument to "id = %a", but show that you can pass
string (with string_conversion function) as argument to "id = %a".
Which would result in perfectly valid string (so you consider it
type-safe), but this wouldn't be valid SQL (which Richard considers
non-type-safe, and you probably consider logic error).

There is simple solution:

let sth = (fun i s -> dbh#prepare 
           "select name from employees where id = %a and name = %a"
	   int_conversion i string_conversion s)

Which would ensure type-safety Richard is talking about, but he would
like to be able to write:

let sth = dbh#prepare
           "select name from employees where id = %i and name = %s"

and get the same result. And this wouldn't be hard when he could retain
magic typing of printf, but own printf implementation (i.e. to hook into
processing of %s and %i).

-- 
: Michal Moskal :: http://www.kernel.pl/~malekith : GCS {C,UL}++++$ a? !tv
: When in doubt, use brute force. -- Ken Thompson : {E-,w}-- {b++,e}>+++ h

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


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

* Re: [Caml-list] Printf question
  2003-09-29 22:36                 ` Pierre Weis
  2003-09-30  8:03                   ` Richard Jones
@ 2003-09-30 13:19                   ` skaller
  2003-09-30 20:52                     ` Pierre Weis
  1 sibling, 1 reply; 28+ messages in thread
From: skaller @ 2003-09-30 13:19 UTC (permalink / raw)
  To: Pierre Weis; +Cc: Richard Jones, caml-list

On Tue, 2003-09-30 at 08:36, Pierre Weis wrote:
> > On Mon, Sep 29, 2003 at 09:57:19PM +0200, Pierre Weis wrote:
> > > Hence, I think you are seeking for the %a conversion. For instance,
> > > defining a quotation function for SQL as in
> > > 
> > > # let sql_quoting oc = Printf.fprintf oc "'%s'";;   
> > > val sql_quoting : out_channel -> string -> unit = <fun>
> > > 
> > > You can define sth with a %a conversion to apply quoting on the fly:
> > > 
> > > # let sth x =
> > >     prepare "select salary from emp where id = %d and name = %a" x;;
> > > val sth : int -> (out_channel -> 'a -> unit) -> 'a -> unit = <fun>
> > > 
> > > # sth 1 sql_quoting "Jones";;
> > > select salary from emp where id = 1 and name = 'Jones'- : unit = ()
> > > 
> > > Still fully type-checked! Sounds better, no ?
> > 
> > Hmmm ... but as I explained earlier, that's really not type safe
> > at all.
> > 
> > Rich.
> 
> What do you mean by ``not type safe at all'' ?
> 
> The common acceptation is ``it could lead to type errors at runtime''.

What I think he means is that it isn't type safe with respect to an
SQL typing: the Ocaml typing is not 'sound' for SQL in that bogus
SQL syntax can be produced by type correct Ocaml: 
the SQL call can result in an SQL Syntax Error at run time.


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


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

* Re: [Caml-list] Printf question
  2003-09-30  9:17                       ` Michal Moskal
@ 2003-09-30 14:14                         ` Christophe TROESTLER
  0 siblings, 0 replies; 28+ messages in thread
From: Christophe TROESTLER @ 2003-09-30 14:14 UTC (permalink / raw)
  To: O'Caml Mailing List

On Tue, 30 Sep 2003, Michal Moskal <malekith@pld-linux.org> wrote:
> 
> The example (well, SQL tables behind it), expects integer argument
> to "id = %a", but show that you can pass string (with
> string_conversion function) as argument to "id = %a".

Well, there is absolutely no way to make sure that the SQL types are
correctly matched to Caml types.  Moreover, the two type systems do
not even possess the same basic types (what about DECIMAL, DATE,...?).
So, the better one can do is to have the types as close as possible to
their use in the SQL query to avoid mistakes (they should be catched
at run time by the DB in any case).

> There is simple solution:
> 
> let sth = (fun i s -> dbh#prepare 
>            "select name from employees where id = %a and name = %a"
> 	   int_conversion i string_conversion s)

What I proposed some time ago was

let t = new DB.row_conversion
let q = dbh#prepapre "select name from employees where id = ? and name = ?"
let getsth = dbh#exec (t#int ++ t#string)

which gives the function [getsth : int -> string -> ('param, 'row) result].

This looks a bit complex but is the price to pay I think to be able to
have specific (or even tailor made) conversion functions for each
database (and to bind them directly to the DB structure, without
string intermediates).  Maybe, on top of functions of the kind of the
above, one could add some sugar -- I am particularly thinking of
something like the nice regexp sugar provided by Yukata Oiwa
(http://web.yl.is.s.u-tokyo.ac.jp/~oiwa/caml/).

[One could also try to associate to a source another one,
automatically generated, containing statements to check the queries
against the DB; thus asserting before running the (main) program that
none of its queries will fail because of a type mismatch.  A Camlp4
guru should be able to do that I guess.]

Cheers,
ChriS

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


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

* Re: [Caml-list] Printf question
  2003-09-30 13:19                   ` skaller
@ 2003-09-30 20:52                     ` Pierre Weis
  2003-10-01 14:39                       ` Christophe TROESTLER
  0 siblings, 1 reply; 28+ messages in thread
From: Pierre Weis @ 2003-09-30 20:52 UTC (permalink / raw)
  To: skaller; +Cc: pierre.weis, rich, caml-list

> > What do you mean by ``not type safe at all'' ?
> > 
> > The common acceptation is ``it could lead to type errors at runtime''.
> 
> What I think he means is that it isn't type safe with respect to an
> SQL typing: the Ocaml typing is not 'sound' for SQL in that bogus
> SQL syntax can be produced by type correct Ocaml: 
> the SQL call can result in an SQL Syntax Error at run time.

 Thank you very much for the clarification. So the problem is the
generation of programs (SQL requests) that should be well-typed (for
SQL).

 The normal Caml approach to this kind of problems is to generate a
tree like structure with constructors (the AST of the request) and
then to pretty-print this tree to textual form.

 This way the pretty-print phase is simple and may easily generate
syntactically correct requests. Hence, we end with the problem of
generating sound trees for the requests.

 The first benefit of this two stages approach is that the
constructors are typed, thus their applications is typechecked and
statically eliminate the generation of trivially wrong programs (wrong
requests). If this checks (provided for free by the Caml compiler) are
not sufficient to obtain the expected safety, you probably need to
write an extra static analysis pass on the generated tree to detect
the remainding errors.

Mmm, this is interesting, but this is no more a printf problem, and
indeed far from a simple programming question ...

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


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

* Re: [Caml-list] Printf question
  2003-09-30 20:52                     ` Pierre Weis
@ 2003-10-01 14:39                       ` Christophe TROESTLER
  2003-10-01 14:57                         ` Richard Jones
  2003-10-01 16:21                         ` [Caml-list] Printf question Florian Hars
  0 siblings, 2 replies; 28+ messages in thread
From: Christophe TROESTLER @ 2003-10-01 14:39 UTC (permalink / raw)
  To: pierre.weis; +Cc: skaller, rich, caml-list

On Tue, 30 Sep 2003, Pierre Weis <pierre.weis@inria.fr> wrote:
> 
> [...] two stages approach [...] eliminate the generation of
> trivially wrong programs (wrong requests).
> 
> Mmm, this is interesting, [...]

Indeed.  So if I got what you say right, one should

1) Develop AST for SQL requests which would guarantee their internal
   coherence (and also write a tool for each DB engine that, for a
   given database, outputs a module (or so) to enable the checking of
   the query against the DB types (1)).

2) Develop Camlp4 macros that translate SQL requests into AST (a la
   "embedded SQL" rather than with strings -- AFAIK embedded SQL is
   normalized).  This would also solve the "binding to variables"
   problem.  One could conform to standard embedded SQL and/or provide
   forms that are more "functional", e.g.

     let FirstName, LastName =
        EXEC SQL SELECT OWNERFIRSTNAME, OWNERLASTNAME
        FROM ANTIQUEOWNERS
        WHERE BUYERID = :BuyerID in ...

   instead of

     EXEC SQL SELECT OWNERFIRSTNAME, OWNERLASTNAME
        INTO :FirstName, :LastName
        FROM ANTIQUEOWNERS
        WHERE BUYERID = :BuyerID in ...

   or

     let v1, v2, v3 = EXEC SQL FETCH ItemCursor in ...

   instead of

     EXEC SQL FETCH ItemCursor INTO :v1, :v2, :v3 in ...

3) Write, for each DB engine, an "evaluator" of the AST.

Once this is done (we need interested people here!!), if the source
compile the requests will be valid SQL queries and statically
typechecked against the DB (given (1)).

Is it possible to evaluate how much effort is needed to get to this
point?  Do people know where to find some (documented) AST for SQL
expressions?  What is the opinion of the existing DB interfaces
writers?

Cheers,
ChriS

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


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

* Re: [Caml-list] Printf question
  2003-10-01 14:39                       ` Christophe TROESTLER
@ 2003-10-01 14:57                         ` Richard Jones
  2003-10-01 15:52                           ` [Caml-list] DBI (was: Printf question) Christophe TROESTLER
  2003-10-01 16:21                         ` [Caml-list] Printf question Florian Hars
  1 sibling, 1 reply; 28+ messages in thread
From: Richard Jones @ 2003-10-01 14:57 UTC (permalink / raw)
  Cc: caml-list

On Wed, Oct 01, 2003 at 04:39:55PM +0200, Christophe TROESTLER wrote:
> 1) Develop AST for SQL requests which would guarantee their internal
>    coherence (and also write a tool for each DB engine that, for a
>    given database, outputs a module (or so) to enable the checking of
>    the query against the DB types (1)).

That's a huge amount of work, and you have to redo it for each DB
engine. (Rather you than me is what I'm saying :-)

If you're interested in my take on "DBI" have a look at the mod_caml
CVS, eg:

http://savannah.nongnu.org/cgi-bin/viewcvs/modcaml/modcaml/dbi.mli?rev=1.2&content-type=text/vnd.viewcvs-markup
http://savannah.nongnu.org/cgi-bin/viewcvs/modcaml/modcaml/dbi_postgres.ml?rev=1.2&content-type=text/vnd.viewcvs-markup

There's a PostgreSQL implementation of the API which mostly works.

I'm afraid you may be disappointed that it doesn't do anything fancy
with printf or camlp4 :-)

Rich.

-- 
Richard Jones. http://www.annexia.org/ http://freshmeat.net/users/rwmj
Merjis Ltd. http://www.merjis.com/ - all your business data are belong to you.
C2LIB is a library of basic Perl/STL-like types for C. Vectors, hashes,
trees, string funcs, pool allocator: http://www.annexia.org/freeware/c2lib/

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


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

* [Caml-list] DBI (was: Printf question)
  2003-10-01 14:57                         ` Richard Jones
@ 2003-10-01 15:52                           ` Christophe TROESTLER
  0 siblings, 0 replies; 28+ messages in thread
From: Christophe TROESTLER @ 2003-10-01 15:52 UTC (permalink / raw)
  To: rich; +Cc: caml-list

On Wed, 1 Oct 2003, Richard Jones <rich@annexia.org> wrote:
> 
> On Wed, Oct 01, 2003 at 04:39:55PM +0200, Christophe TROESTLER wrote:
> > 1) Develop AST for SQL requests [...]
> 
> [...] you have to redo it for each DB engine.

Why?  Can you elaborate?  The point of developing AST (and a
embedded-SQL-like language) is precisely to do it once for all.  What
is needed is an "interpreter" of AST for each DB engine (this should
be the easy part).

> If you're interested in my take on "DBI" have a look at the mod_caml

Will have a look (I am interested and will do my best to contribute
but my spare time is unfortunately little).

Cheers,
ChriS

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


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

* Re: [Caml-list] Printf question
  2003-10-01 14:39                       ` Christophe TROESTLER
  2003-10-01 14:57                         ` Richard Jones
@ 2003-10-01 16:21                         ` Florian Hars
  1 sibling, 0 replies; 28+ messages in thread
From: Florian Hars @ 2003-10-01 16:21 UTC (permalink / raw)
  To: Christophe TROESTLER; +Cc: caml-list

Christophe TROESTLER wrote:
> Is it possible to evaluate how much effort is needed to get to this
> point?  Do people know where to find some (documented) AST for SQL
> expressions? 

There something for perl:
http://theoryx5.uwinnipeg.ca/CPAN/data/Rosetta/SQL/SyntaxModel.html

and haskell:
http://citeseer.nj.nec.com/leijen99domain.html

Yours, Florian.

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


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

* Re: [Caml-list] Printf question
  2003-05-18  6:06 ` Basile STARYNKEVITCH
@ 2003-05-19  9:39   ` Damien
  0 siblings, 0 replies; 28+ messages in thread
From: Damien @ 2003-05-19  9:39 UTC (permalink / raw)
  To: caml-list

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

> You might have a look into the tracing facilities I developed for
> Poesia. These are tracing macros for camlp4.
looks really fine ! but also really complicated...

I personally use the less powerful attached code :

advantages are :
	* you don't need to recompile the code each time you change debug
	options
	* it can _easily_ be adapted to the newbie needs 
	* it doesn't need camlp4 (is this an actual advantage !?)

disadvantages are :
	* you can't automagically display the location in the source code 
	* you need to remove debug statements by hand when compiling efficient
	binaries
	* Basile will certainly find more :-)

note : for the first disadvantage, why not to modify the assert function
in order it also prints the execution stack. 
I don't know if this is possible in both byte and native code, but I
guess it should be when debugging informations are saved (ocamlc -g).
from my mind, this would be really useful.

hope this help,
damien

--
<http://www.ens-lyon.fr/~dpous>

[-- Attachment #2: Makefile --]
[-- Type: application/octet-stream, Size: 162 bytes --]

toto: msg.cmo toto.ml
	ocamlc msg.cmo toto.ml -o toto

msg.cmo: msg.cmi msg.ml
	ocamlc -c msg.ml

msg.cmi: msg.mli
	ocamlc -c msg.mli

clean: 	
	rm -f toto *.cm?

[-- Attachment #3: msg.ml --]
[-- Type: application/octet-stream, Size: 785 bytes --]

(* warning classes  *)
type warning_t = [ 
| `Foo
| `Bar 
| `Arf
]

let map s = 
  match String.uppercase s with
    | "FOO" -> `Foo
    | "BAR" -> `Bar
    | "ARF" -> `Arf
    | _     -> failwith ("Unknown warning : "^s)

(* warnings handled as errors *)
let err_l = ref []
let as_error w = err_l := (map w) :: !err_l

(* ignored warnings *)
let warn_l = ref []
let add_warning w = warn_l := (map w) :: !warn_l


let error () = Printf.kprintf (fun s -> prerr_endline s; exit 1)
let warning w = Obj.magic (
  if List.mem w !err_l then 
    error ()
  else if List.mem w !warn_l then
    Printf.kprintf (fun s -> prerr_endline s; "")
  else
    Printf.kprintf (fun _ -> ""))
(* Obj.magic is used to cast a string to unit and avoid "this expression should have type unit" warnings... *)

[-- Attachment #4: msg.mli --]
[-- Type: application/octet-stream, Size: 334 bytes --]

(* warning classes  *)
type warning_t = [ 
| `Foo
| `Bar 
| `Arf
]

(* warnings to activate or to handle as errors *)
val as_error    : string -> unit
val add_warning : string -> unit

(* warnings / errors *)
val warning : warning_t -> ('a, out_channel, unit) format -> 'a 
val error   :      unit -> ('a, unit, string) format -> 'a


[-- Attachment #5: toto.ml --]
[-- Type: application/octet-stream, Size: 390 bytes --]

let _ = 
  Arg.parse 
    [ "-W", Arg.String Msg.add_warning, " <w>\tenable warnings of kind w"; 
      "-E", Arg.String Msg.as_error,    " <w>\thandle warnings of kind w as errors" ]
    (fun _ -> raise (Arg.Bad "too many arguments"))
    "toto [options]";
  
  Msg.warning `Foo "foo=%d" 51;
  Msg.warning `Bar "bar%s" "jo";
  Msg.warning `Arf "glou: %f" 3.3;
  Msg.error() "boom !"
    


[-- Attachment #6: EXAMPLE --]
[-- Type: application/octet-stream, Size: 124 bytes --]

>./toto
boom !

>./toto -W FOO
foo=51
boom !

>./toto -W FOO -W BAR
foo=51
barjo
boom !

>./toto -W FOO -E BAR
foo=51
barjo

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

* [Caml-list] Printf question
  2003-05-18  1:34 Brian Hurt
  2003-05-18  3:23 ` Manos Renieris
  2003-05-18  3:32 ` William Lovas
@ 2003-05-18  6:06 ` Basile STARYNKEVITCH
  2003-05-19  9:39   ` Damien
  2 siblings, 1 reply; 28+ messages in thread
From: Basile STARYNKEVITCH @ 2003-05-18  6:06 UTC (permalink / raw)
  To: Brian Hurt; +Cc: Ocaml Mailing List

>>>>> "Brian" == Brian Hurt <brian.hurt@qlogic.com> writes:

    Brian> I want to define a wrapper function for printf, basically
    Brian> for debug.  [...]

You might have a look into the tracing facilities I developed for
Poesia. These are tracing macros for camlp4.

See for example
http://www2.poesia-filter.org:8000/cgi-bin/cvsweb.cgi/PoesiaSoft/PoesiaMonIcap/README.trace

and the *trace* files under
http://www2.poesia-filter.org:8000/cgi-bin/cvsweb.cgi/PoesiaSoft/PoesiaMonIcap

-- 

Basile STARYNKEVITCH         http://starynkevitch.net/Basile/ 
email: basile<at>starynkevitch<dot>net 
aliases: basile<at>tunes<dot>org = bstarynk<at>nerim<dot>net
8, rue de la Faïencerie, 92340 Bourg La Reine, France

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


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

* Re: [Caml-list] Printf question
  2003-05-18  1:34 Brian Hurt
  2003-05-18  3:23 ` Manos Renieris
@ 2003-05-18  3:32 ` William Lovas
  2003-05-18  6:06 ` Basile STARYNKEVITCH
  2 siblings, 0 replies; 28+ messages in thread
From: William Lovas @ 2003-05-18  3:32 UTC (permalink / raw)
  To: Ocaml Mailing List

On Sat, May 17, 2003 at 08:34:27PM -0500, Brian Hurt wrote:
> let debug s = Printf.printf ("FOO: " ^ s ^ "\n")
> 
> and:
> 
> let debug s = let t = "FOO: " ^ s ^ "\n" in Printf.printf s
> 
> and neither works.

This is due to the funny type magic that makes printf work in the first
place.

>     debug "some message"; (* outputs "FOO: some message\n" *)
>     debug "answer: %d" 42 ; (* outputs "FOO: answer: 42\n" *)
> 
> basically to prepend "FOO: " and append "\n".  Is this possible in any 
> sane manner?  I tried:

I think the right solution (or at least, *a* right solution) in this case
is to use kprintf, which takes a continuation to pass the resulting string
to.  For an sprintf-like solution:

    let debug s = Printf.kprintf (fun t -> "FOO: " ^ t ^ "\n") s

Or, for something more side-effectual:

    let debug s = Printf.kprintf
                    (fun t -> print_string ("FOO: " ^ t ^ "\n"); "") s

(But you might have to use `ignore' to ignore the useless empty string
result value.)

cheers,
William

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


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

* Re: [Caml-list] Printf question
  2003-05-18  1:34 Brian Hurt
@ 2003-05-18  3:23 ` Manos Renieris
  2003-05-18  3:32 ` William Lovas
  2003-05-18  6:06 ` Basile STARYNKEVITCH
  2 siblings, 0 replies; 28+ messages in thread
From: Manos Renieris @ 2003-05-18  3:23 UTC (permalink / raw)
  To: Brian Hurt; +Cc: Ocaml Mailing List

On Sat, May 17, 2003 at 08:34:27PM -0500, Brian Hurt wrote:
> 
> I want to define a wrapper function for printf, basically for debug.  I'd 
> I tried:
> let debug s = Printf.printf ("FOO: " ^ s ^ "\n")
> and:
> let debug s = let t = "FOO: " ^ s ^ "\n" in Printf.printf s
> and neither works.

You need to concatenate formats, not strings. In the CVS version,
(^^) is defined, and does exactly that.

-- Manos

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


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

* [Caml-list] Printf question
@ 2003-05-18  1:34 Brian Hurt
  2003-05-18  3:23 ` Manos Renieris
                   ` (2 more replies)
  0 siblings, 3 replies; 28+ messages in thread
From: Brian Hurt @ 2003-05-18  1:34 UTC (permalink / raw)
  To: Ocaml Mailing List


I want to define a wrapper function for printf, basically for debug.  I'd 
like it to work so I can do things like:

    debug "some message"; (* outputs "FOO: some message\n" *)
    debug "answer: %d" 42 ; (* outputs "FOO: answer: 42\n" *)

basically to prepend "FOO: " and append "\n".  Is this possible in any 
sane manner?  I tried:

let debug s = Printf.printf ("FOO: " ^ s ^ "\n")

and:

let debug s = let t = "FOO: " ^ s ^ "\n" in Printf.printf s

and neither works.

Brian


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


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

end of thread, other threads:[~2003-10-01 16:21 UTC | newest]

Thread overview: 28+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2003-09-26 18:02 [Caml-list] Printf question Richard Jones
2003-09-26 19:04 ` Alain.Frisch
2003-09-29  7:44   ` Mike Potanin
2003-09-27  0:11 ` Olivier Andrieu
2003-09-27  7:23   ` Richard Jones
2003-09-27  8:20     ` Basile Starynkevitch
2003-09-27  9:14       ` Richard Jones
2003-09-27  9:39         ` Maxence Guesdon
2003-09-29 16:42         ` Pierre Weis
2003-09-29 18:13           ` Richard Jones
2003-09-29 19:57             ` Pierre Weis
2003-09-29 21:50               ` Richard Jones
2003-09-29 22:36                 ` Pierre Weis
2003-09-30  8:03                   ` Richard Jones
2003-09-30  8:45                     ` Pierre Weis
2003-09-30  9:17                       ` Michal Moskal
2003-09-30 14:14                         ` Christophe TROESTLER
2003-09-30 13:19                   ` skaller
2003-09-30 20:52                     ` Pierre Weis
2003-10-01 14:39                       ` Christophe TROESTLER
2003-10-01 14:57                         ` Richard Jones
2003-10-01 15:52                           ` [Caml-list] DBI (was: Printf question) Christophe TROESTLER
2003-10-01 16:21                         ` [Caml-list] Printf question Florian Hars
  -- strict thread matches above, loose matches on Subject: below --
2003-05-18  1:34 Brian Hurt
2003-05-18  3:23 ` Manos Renieris
2003-05-18  3:32 ` William Lovas
2003-05-18  6:06 ` Basile STARYNKEVITCH
2003-05-19  9:39   ` Damien

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