caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] type-safe printf
@ 2016-09-13 15:12 Андрей Бергман
  2016-09-13 15:25 ` Mario Pereira
                   ` (3 more replies)
  0 siblings, 4 replies; 8+ messages in thread
From: Андрей Бергман @ 2016-09-13 15:12 UTC (permalink / raw)
  To: caml-list

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.

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

* Re: [Caml-list] type-safe printf
  2016-09-13 15:12 [Caml-list] type-safe printf Андрей Бергман
@ 2016-09-13 15:25 ` Mario Pereira
  2016-09-13 15:36   ` Maxime Dénès
  2016-09-13 15:31 ` Kakadu
                   ` (2 subsequent siblings)
  3 siblings, 1 reply; 8+ messages in thread
From: Mario Pereira @ 2016-09-13 15:25 UTC (permalink / raw)
  To: caml-list

Hi,

You can do it using GADTs. However, I am not sure if this is explained 
somewhere.

Le 13/09/2016 à 17:12, Андрей Бергман a écrit :
> 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.
>


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

* Re: [Caml-list] type-safe printf
  2016-09-13 15:12 [Caml-list] type-safe printf Андрей Бергман
  2016-09-13 15:25 ` Mario Pereira
@ 2016-09-13 15:31 ` Kakadu
  2016-09-13 15:40   ` Daniel Bünzli
  2016-09-14 11:19   ` Oleg
  2016-09-13 15:38 ` David Allsopp
  2016-09-13 17:54 ` Андрей Бергман
  3 siblings, 2 replies; 8+ messages in thread
From: Kakadu @ 2016-09-13 15:31 UTC (permalink / raw)
  To: Андрей
	Бергман
  Cc: Caml List

Functional unparsing by Olivier Danvy can be related article.
http://www.brics.dk/RS/98/12/BRICS-RS-98-12.pdf


Happy hacking,
Kakadu

On Tue, Sep 13, 2016 at 6:12 PM, Андрей Бергман <vkni@yandex.ru> wrote:
> 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

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

* Re: [Caml-list] type-safe printf
  2016-09-13 15:25 ` Mario Pereira
@ 2016-09-13 15:36   ` Maxime Dénès
  0 siblings, 0 replies; 8+ messages in thread
From: Maxime Dénès @ 2016-09-13 15:36 UTC (permalink / raw)
  To: caml-list

Hello,

Some ideas are described here:

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

Maxime.

On 09/13/16 17:25, Mario Pereira wrote:
> Hi,
> 
> You can do it using GADTs. However, I am not sure if this is explained
> somewhere.
> 
> Le 13/09/2016 à 17:12, Андрей Бергман a écrit :
>> 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.
>>
> 
> 

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

* RE: [Caml-list] type-safe printf
  2016-09-13 15:12 [Caml-list] type-safe printf Андрей Бергман
  2016-09-13 15:25 ` Mario Pereira
  2016-09-13 15:31 ` Kakadu
@ 2016-09-13 15:38 ` David Allsopp
  2016-09-13 17:54 ` Андрей Бергман
  3 siblings, 0 replies; 8+ messages in thread
From: David Allsopp @ 2016-09-13 15:38 UTC (permalink / raw)
  To: Андрей
	Бергман,
	caml-list

Андрей Бергман wrote: 
> 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


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

* Re: [Caml-list] type-safe printf
  2016-09-13 15:31 ` Kakadu
@ 2016-09-13 15:40   ` Daniel Bünzli
  2016-09-14 11:19   ` Oleg
  1 sibling, 0 replies; 8+ messages in thread
From: Daniel Bünzli @ 2016-09-13 15:40 UTC (permalink / raw)
  To: Андрей
	Бергман
  Cc: Caml List

On Tuesday 13 September 2016 at 17:31, Kakadu wrote:
> 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



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

* Re: [Caml-list] type-safe printf
  2016-09-13 15:12 [Caml-list] type-safe printf Андрей Бергман
                   ` (2 preceding siblings ...)
  2016-09-13 15:38 ` David Allsopp
@ 2016-09-13 17:54 ` Андрей Бергман
  3 siblings, 0 replies; 8+ messages in thread
From: Андрей Бергман @ 2016-09-13 17:54 UTC (permalink / raw)
  To: caml-list

Thank you all very much! Your references and explanations made it clear to me.

13.09.2016, 18:19, "Андрей Бергман" <vkni@yandex.ru>:
> 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

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

* Re: [Caml-list] type-safe printf
  2016-09-13 15:31 ` Kakadu
  2016-09-13 15:40   ` Daniel Bünzli
@ 2016-09-14 11:19   ` Oleg
  1 sibling, 0 replies; 8+ messages in thread
From: Oleg @ 2016-09-14 11:19 UTC (permalink / raw)
  To: kakadu.hafanana; +Cc: caml-list


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




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

end of thread, other threads:[~2016-09-14 11:14 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-09-13 15:12 [Caml-list] type-safe printf Андрей Бергман
2016-09-13 15:25 ` Mario Pereira
2016-09-13 15:36   ` Maxime Dénès
2016-09-13 15:31 ` Kakadu
2016-09-13 15:40   ` Daniel Bünzli
2016-09-14 11:19   ` Oleg
2016-09-13 15:38 ` David Allsopp
2016-09-13 17:54 ` Андрей Бергман

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