Discussion:
[Caml-list] type-safe printf
(too old to reply)
Андрей Бергман
2016-09-13 15:19:50 UTC
Permalink
Hello,

is there a good article or just short explanation how compile-type checking of Printf arguments works?

Does it require special compiler support like in C++ compilers/analyzers, or one can somehow do the trick of parsing string literal using Ocaml language itself? (actually latter does not look possible to me)

Thanks,
Andrey.
--
Caml-list mailing list. Subscription management and archives:
https://sympa.inria.fr/sympa/arc/caml-list
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
Bug reports: http://caml.inria.fr/bin/caml-bugs
Mario Pereira
2016-09-13 15:28:38 UTC
Permalink
Hi,

You can do it using GADTs. However, I am not sure if this is explained
somewhere.
Post by Андрей Бергман
Hello,
is there a good article or just short explanation how compile-type checking of Printf arguments works?
Does it require special compiler support like in C++ compilers/analyzers, or one can somehow do the trick of parsing string literal using Ocaml language itself? (actually latter does not look possible to me)
Thanks,
Andrey.
--
Caml-list mailing list. Subscription management and archives:
https://sympa.inria.fr/sympa/arc/caml-list
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
Bug reports: http://caml.inria.fr/bin/caml-bugs
Maxime Dénès
2016-09-13 15:37:01 UTC
Permalink
Hello,

Some ideas are described here:

https://ocaml.org/meetings/ocaml/2013/proposals/formats-as-gadts.pdf

Maxime.
Post by Mario Pereira
Hi,
You can do it using GADTs. However, I am not sure if this is explained
somewhere.
Post by Андрей Бергман
Hello,
is there a good article or just short explanation how compile-type
checking of Printf arguments works?
Does it require special compiler support like in C++
compilers/analyzers, or one can somehow do the trick of parsing string
literal using Ocaml language itself? (actually latter does not look
possible to me)
Thanks,
Andrey.
--
Caml-list mailing list. Subscription management and archives:
https://sympa.inria.fr/sympa/arc/caml-list
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
Bug reports: http://caml.inria.fr/bin/caml-bugs
Kakadu
2016-09-13 15:32:03 UTC
Permalink
Functional unparsing by Olivier Danvy can be related article.
http://www.brics.dk/RS/98/12/BRICS-RS-98-12.pdf


Happy hacking,
Kakadu
Post by Андрей Бергман
Hello,
is there a good article or just short explanation how compile-type checking of Printf arguments works?
Does it require special compiler support like in C++ compilers/analyzers, or one can somehow do the trick of parsing string literal using Ocaml language itself? (actually latter does not look possible to me)
Thanks,
Andrey.
--
https://sympa.inria.fr/sympa/arc/caml-list
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
Bug reports: http://caml.inria.fr/bin/caml-bugs
--
Caml-list mailing list. Subscription management and archives:
https://sympa.inria.fr/sympa/arc/caml-list
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
Bug reports: http://caml.inria.fr/bin/caml-bugs
Daniel Bünzli Андрей Бергман
2016-09-13 15:40:45 UTC
Permalink
Post by Kakadu
Functional unparsing by Olivier Danvy can be related article.
http://www.brics.dk/RS/98/12/BRICS-RS-98-12.pdf
That's a good reference. You may also want to have a look at Gabriel Radanne's recent blog post here:

http://drup.github.io/2016/08/02/difflists/

which will show you how to do the same with GADTs, which is a bit more convenient.

Best,

Daniel
--
Caml-list mailing list. Subscription management and archives:
https://sympa.inria.fr/sympa/arc/caml-list
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
Bug reports: http://caml.inria.fr/bin/caml-bugs
Oleg
2016-09-14 11:15:02 UTC
Permalink
Post by Kakadu
Functional unparsing by Olivier Danvy can be related article.
http://www.brics.dk/RS/98/12/BRICS-RS-98-12.pdf
Well, in that vein one can point out

Functional un|unparsing
http://okmij.org/ftp/typed-formatting/index.html#derivation

which deals with both type-safe printf and scanf (and, moreover, using
exactly the same format for both printing and parsing) and derives even
more type-safe parsing/unparsing methods.
--
Caml-list mailing list. Subscription management and archives:
https://sympa.inria.fr/sympa/arc/caml-list
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
Bug reports: http://caml.inria.fr/bin/caml-bugs
David Allsopp
2016-09-13 15:39:07 UTC
Permalink
Post by Андрей Бергман
is there a good article or just short explanation how compile-type
checking of Printf arguments works?
Does it require special compiler support like in C++ compilers/analyzers,
or one can somehow do the trick of parsing string literal using Ocaml
language itself? (actually latter does not look possible to me)
It does require a certain amount of special support, yes - see the Pexp_constant(Pconst_string _) case in typing/typecore.ml, in particular the comment "Terrible hack for format strings"! In essence, a string is "magically" interpreted as a format string if it is used in a context where one would be expected (e.g. the usual Printf.printf). It's one of the reasons Pervasives.format_of_string exists (because they're type- rather than syntax-driven).

From that point onwards, it's a lot of GADTs, but doesn't require any special language support!

If you were adding Printf to OCaml for the first time now, you'd probably use quoted string literals instead (see http://caml.inria.fr/pub/docs/manual-ocaml/extn.html#sec249) - and the entire thing would then be in OCaml with no special case hacks.


David
--
Caml-list mailing list. Subscription management and archives:
https://sympa.inria.fr/sympa/arc/caml-list
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
Bug reports: http://caml.inria.fr/bin/caml-bugs
Андрей Бергман
2016-09-13 17:55:26 UTC
Permalink
Thank you all very much! Your references and explanations made it clear to me.
Post by Андрей Бергман
Hello,
is there a good article or just short explanation how compile-type checking of Printf arguments works?
Does it require special compiler support like in C++ compilers/analyzers, or one can somehow do the trick of parsing string literal using Ocaml language itself? (actually latter does not look possible to me)
Thanks,
    Andrey.
--
https://sympa.inria.fr/sympa/arc/caml-list
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
Bug reports: http://caml.inria.fr/bin/caml-bugs
--
Caml-list mailing list. Subscription management and archives:
https://sympa.inria.fr/sympa/arc/caml-list
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
Bug reports: http://caml.inria.fr/bin/caml-bugs
Loading...