* [Caml-list] Extending Format strings
@ 2014-07-01 9:59 Gabriel Kerneis
2014-07-01 14:15 ` Tianyi Cui
2014-07-01 15:27 ` Jeremie Dimino
0 siblings, 2 replies; 7+ messages in thread
From: Gabriel Kerneis @ 2014-07-01 9:59 UTC (permalink / raw)
To: caml-list
Dear all,
in CIL <http://cil.sf.net>, the module Pretty provides a Format-like
interface with a few extensions compared to usual format strings.
Implementation-wise, it uses string_of_format and re-implements most of
the logic of the pre-4.02 Format module, with Obj.magic all over the
place.
I wondered if anyone has done something similar in their own project
(extending Format in one way or another), and if the new GADT-based
approach could provide a cleaner solution?
One thing I quite like with the current code, despite its uglyness, is
its concision. I am right in assuming that switching to GADTs would
require a lot more boilerplate?
Many thanks for any example you could provide.
With best regards,
--
Gabriel
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [Caml-list] Extending Format strings
2014-07-01 9:59 [Caml-list] Extending Format strings Gabriel Kerneis
@ 2014-07-01 14:15 ` Tianyi Cui
2014-07-01 14:39 ` Gabriel Kerneis
2014-07-01 15:27 ` Jeremie Dimino
1 sibling, 1 reply; 7+ messages in thread
From: Tianyi Cui @ 2014-07-01 14:15 UTC (permalink / raw)
To: Gabriel Kerneis; +Cc: OCaml Mailing List
[-- Attachment #1: Type: text/plain, Size: 1259 bytes --]
I found https://github.com/janestreet/custom_printf quite straightforward
to understand and easy to use, if most of your types already have sexp
converters and to_string functions.
On Tue, Jul 1, 2014 at 5:59 AM, Gabriel Kerneis <gabriel@kerneis.info>
wrote:
> Dear all,
>
> in CIL <http://cil.sf.net>, the module Pretty provides a Format-like
> interface with a few extensions compared to usual format strings.
> Implementation-wise, it uses string_of_format and re-implements most of
> the logic of the pre-4.02 Format module, with Obj.magic all over the
> place.
>
> I wondered if anyone has done something similar in their own project
> (extending Format in one way or another), and if the new GADT-based
> approach could provide a cleaner solution?
>
> One thing I quite like with the current code, despite its uglyness, is
> its concision. I am right in assuming that switching to GADTs would
> require a lot more boilerplate?
>
> Many thanks for any example you could provide.
>
> With best regards,
> --
> Gabriel
>
> --
> 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
>
[-- Attachment #2: Type: text/html, Size: 2043 bytes --]
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [Caml-list] Extending Format strings
2014-07-01 14:15 ` Tianyi Cui
@ 2014-07-01 14:39 ` Gabriel Kerneis
0 siblings, 0 replies; 7+ messages in thread
From: Gabriel Kerneis @ 2014-07-01 14:39 UTC (permalink / raw)
To: Tianyi Cui; +Cc: OCaml Mailing List
Le 2014-07-01 15:15, Tianyi Cui a écrit :
> I found https://github.com/janestreet/custom_printf [5] quite
> straightforward to understand and easy to use, if most of your types
> already have sexp converters and to_string functions.
Thanks. This is a nice hack (provided you are happy to use a
pre-processor). However, if I understand correctly, it is limited to
making %s and %a more convenient to use for existing Printf functions.
I should probably have given more details in my initial request. CIL's
Pretty.dprintf [1] constructs and returns a "document" (similar to
PPrint.document, if you are more familiar with this library), to be
later formated and rendered to a given width. I am not sure
custom_printf provides that level of flexibility.
[1]
http://kerneis.github.io/cil/doc/html/cil/api/Pretty.html#VALdprintf
Best,
--
Gabriel
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [Caml-list] Extending Format strings
2014-07-01 9:59 [Caml-list] Extending Format strings Gabriel Kerneis
2014-07-01 14:15 ` Tianyi Cui
@ 2014-07-01 15:27 ` Jeremie Dimino
2014-07-01 16:06 ` Alain Frisch
1 sibling, 1 reply; 7+ messages in thread
From: Jeremie Dimino @ 2014-07-01 15:27 UTC (permalink / raw)
To: Gabriel Kerneis; +Cc: caml-list
[-- Attachment #1: Type: text/plain, Size: 1144 bytes --]
On Tue, Jul 1, 2014 at 10:59 AM, Gabriel Kerneis <gabriel@kerneis.info>
wrote:
> Dear all,
>
> in CIL <http://cil.sf.net>, the module Pretty provides a Format-like
> interface with a few extensions compared to usual format strings.
> Implementation-wise, it uses string_of_format and re-implements most of
> the logic of the pre-4.02 Format module, with Obj.magic all over the
> place.
>
> I wondered if anyone has done something similar in their own project
> (extending Format in one way or another), and if the new GADT-based
> approach could provide a cleaner solution?
>
> One thing I quite like with the current code, despite its uglyness, is
> its concision. I am right in assuming that switching to GADTs would
> require a lot more boilerplate?
>
I had a quick look at [Pretty.dprintf] and I think you could use
[CamlinternalFormat.make_printf] to implement it with 4.02. @-sequences are
already recognized by the format parser in the compiler, but it should be
compatible with [Pretty]'s syntax, you just have to interpret the
constructors differently. It should actually make the code of
[Pretty.dprintf] much simpler.
--
Jeremie
[-- Attachment #2: Type: text/html, Size: 1642 bytes --]
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [Caml-list] Extending Format strings
2014-07-01 15:27 ` Jeremie Dimino
@ 2014-07-01 16:06 ` Alain Frisch
2014-07-02 9:27 ` Gabriel Kerneis
0 siblings, 1 reply; 7+ messages in thread
From: Alain Frisch @ 2014-07-01 16:06 UTC (permalink / raw)
To: Jeremie Dimino, Gabriel Kerneis; +Cc: caml-list
On 07/01/2014 05:27 PM, Jeremie Dimino wrote:
> On Tue, Jul 1, 2014 at 10:59 AM, Gabriel Kerneis <gabriel@kerneis.info
> <mailto:gabriel@kerneis.info>> wrote:
>
> Dear all,
>
> in CIL <http://cil.sf.net>, the module Pretty provides a Format-like
> interface with a few extensions compared to usual format strings.
> Implementation-wise, it uses string_of_format and re-implements most of
> the logic of the pre-4.02 Format module, with Obj.magic all over the
> place.
>
> I wondered if anyone has done something similar in their own project
> (extending Format in one way or another), and if the new GADT-based
> approach could provide a cleaner solution?
>
> One thing I quite like with the current code, despite its uglyness, is
> its concision. I am right in assuming that switching to GADTs would
> require a lot more boilerplate?
>
>
> I had a quick look at [Pretty.dprintf] and I think you could use
> [CamlinternalFormat.make_printf] to implement it with 4.02. @-sequences
> are already recognized by the format parser in the compiler, but it
> should be compatible with [Pretty]'s syntax, you just have to interpret
> the constructors differently. It should actually make the code of
> [Pretty.dprintf] much simpler.
We had a very similar issue with some internal LexiFi library, also
using @-markers, and returning some structured document. Jeremie's
suggestion works very well.
The code below won't compile, but it can give you an idea on how to proceed:
let printf_k fmt_s (acc : (unit, t) CamlinternalFormat.acc) =
let open CamlinternalFormat in
let l = ref empty in
let add x = l := conc !l x in
let stack = ref [] in
let push x = stack := (x,!l) :: !stack; l := empty in
let err () =
Mlfi_isdatypes.ffailwith "Mlfi_pp.printf: invalid format string %S"
fmt_s
in
let pop () = match !stack with
| (x,old) :: st -> stack := st; let nl = !l in l := old; (x,nl)
| _ -> err ()
in
let rec k = function
| Acc_string(p, s) -> k p; add (str s)
| Acc_char(p, c) -> k p; add (str (String.make 1 c))
| Acc_delay(p, f) -> k p; add f
| Acc_flush p -> k p
| Acc_invalid_arg (_, msg) -> invalid_arg msg
| Acc_formatting_lit (p, lit) ->
k p;
begin match string_of_formatting_lit lit with
| "@[" -> push (`BoxLeft 2)
| "@]" ->
begin match pop () with
| (`BoxLeft k,u) -> add (indent k u)
| _ -> err ()
end
| "@<" -> push `LineLeft
| "@>" ->
begin match pop () with
| `LineLeft, u -> add (line u)
| _ -> err ()
end
| s ->
add (str s)
end
| Acc_formatting_gen (p, _) -> k p
| End_of_acc -> ()
in
k acc;
if !stack <> [] then err ();
!l
let printf (Format (fmt, fmt_s) : ('a, unit, t, f) format4) : 'a =
let open CamlinternalFormat in
make_printf (fun () acc -> printf_k fmt_s acc) () End_of_acc fmt
-- Alain
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [Caml-list] Extending Format strings
2014-07-01 16:06 ` Alain Frisch
@ 2014-07-02 9:27 ` Gabriel Kerneis
2014-07-02 13:34 ` Gabriel Scherer
0 siblings, 1 reply; 7+ messages in thread
From: Gabriel Kerneis @ 2014-07-02 9:27 UTC (permalink / raw)
To: Alain Frisch; +Cc: Jeremie Dimino, caml-list
Alain, Jérémie,
On Tue, Jul 01, 2014 at 06:06:44PM +0200, Alain Frisch wrote:
> We had a very similar issue with some internal LexiFi library, also
> using @-markers, and returning some structured document. Jeremie's
> suggestion works very well.
Many thanks for your insights. I'll probably not update CIL code in the
short term, for backwards-compatibility reasons (I'm a little nervous
when it comes to using internal compiler libs), but it's nice to see it
can be done more cleanly than the current implementation.
Best,
--
Gabriel
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [Caml-list] Extending Format strings
2014-07-02 9:27 ` Gabriel Kerneis
@ 2014-07-02 13:34 ` Gabriel Scherer
0 siblings, 0 replies; 7+ messages in thread
From: Gabriel Scherer @ 2014-07-02 13:34 UTC (permalink / raw)
To: Gabriel Kerneis; +Cc: Alain Frisch, Jeremie Dimino, caml-list
> (I'm a little nervous when it comes to using internal compiler libs)
As you should be: there is no guarantee of forward-compatibility of
the format GADTs -- in fact there is a strong confidence of
*non*-compatibility with 4.03.
On Wed, Jul 2, 2014 at 11:27 AM, Gabriel Kerneis <gabriel@kerneis.info> wrote:
> Alain, Jérémie,
>
> On Tue, Jul 01, 2014 at 06:06:44PM +0200, Alain Frisch wrote:
>> We had a very similar issue with some internal LexiFi library, also
>> using @-markers, and returning some structured document. Jeremie's
>> suggestion works very well.
>
> Many thanks for your insights. I'll probably not update CIL code in the
> short term, for backwards-compatibility reasons (I'm a little nervous
> when it comes to using internal compiler libs), but it's nice to see it
> can be done more cleanly than the current implementation.
>
> Best,
> --
> Gabriel
>
> --
> 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] 7+ messages in thread
end of thread, other threads:[~2014-07-02 13:35 UTC | newest]
Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-07-01 9:59 [Caml-list] Extending Format strings Gabriel Kerneis
2014-07-01 14:15 ` Tianyi Cui
2014-07-01 14:39 ` Gabriel Kerneis
2014-07-01 15:27 ` Jeremie Dimino
2014-07-01 16:06 ` Alain Frisch
2014-07-02 9:27 ` Gabriel Kerneis
2014-07-02 13:34 ` Gabriel Scherer
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox