* [Caml-list] format strings
@ 2015-02-09 11:58 Jiten Pathy
2015-02-09 12:25 ` John F Carr
2015-02-09 17:32 ` David Allsopp
0 siblings, 2 replies; 10+ messages in thread
From: Jiten Pathy @ 2015-02-09 11:58 UTC (permalink / raw)
To: caml-list
When i define a wrapper around format functions like
let f s = (fun x -> Printf.sprintf s x);;
val f : ('a -> 'b, unit, bytes) format -> 'a -> 'b = <fun>
I am confused about the inferred type. where does this 'b comes from?
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: [Caml-list] format strings
2015-02-09 11:58 [Caml-list] format strings Jiten Pathy
@ 2015-02-09 12:25 ` John F Carr
2015-02-09 16:28 ` Jiten Pathy
2015-02-09 17:32 ` David Allsopp
1 sibling, 1 reply; 10+ messages in thread
From: John F Carr @ 2015-02-09 12:25 UTC (permalink / raw)
To: Jiten Pathy; +Cc: caml-list
Your wrapper returns a function. Type ‘a, the argument to that function, is determined by the first %-conversion in the format string. The function returns type ‘b. If your format argument has one %-conversion, type ‘b will be a string. If it has more than one, type ‘b will be a function accepting the remaining arguments.
# let g = f “%d %d”;;
val g : int -> int -> string = <fun>
# let h = g 0;;
val h : int -> string = <fun>
# h 1;;
- : string = "0 1"
In the past partial application of printf family functions hasn’t worked well, so test before relying on behavior of the returned function.
> On Feb 9, 2015, at 06:58 , Jiten Pathy <jpathy@fssrv.net> wrote:
>
> When i define a wrapper around format functions like
>
> let f s = (fun x -> Printf.sprintf s x);;
>
> val f : ('a -> 'b, unit, bytes) format -> 'a -> 'b = <fun>
>
> I am confused about the inferred type. where does this 'b comes from?
>
> --
> 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] 10+ messages in thread
* Re: [Caml-list] format strings
2015-02-09 12:25 ` John F Carr
@ 2015-02-09 16:28 ` Jiten Pathy
0 siblings, 0 replies; 10+ messages in thread
From: Jiten Pathy @ 2015-02-09 16:28 UTC (permalink / raw)
To: John F Carr; +Cc: caml-list
[-- Attachment #1: Type: text/plain, Size: 1391 bytes --]
Right. what confuses me is, how the format type has 'a -> 'b instead of 'a.
On Feb 9, 2015 4:25 AM, "John F Carr" <jfc@mit.edu> wrote:
> Your wrapper returns a function. Type ‘a, the argument to that function,
> is determined by the first %-conversion in the format string. The function
> returns type ‘b. If your format argument has one %-conversion, type ‘b
> will be a string. If it has more than one, type ‘b will be a function
> accepting the remaining arguments.
>
> # let g = f “%d %d”;;
> val g : int -> int -> string = <fun>
> # let h = g 0;;
> val h : int -> string = <fun>
> # h 1;;
> - : string = "0 1"
>
> In the past partial application of printf family functions hasn’t worked
> well, so test before relying on behavior of the returned function.
>
> > On Feb 9, 2015, at 06:58 , Jiten Pathy <jpathy@fssrv.net> wrote:
> >
> > When i define a wrapper around format functions like
> >
> > let f s = (fun x -> Printf.sprintf s x);;
> >
> > val f : ('a -> 'b, unit, bytes) format -> 'a -> 'b = <fun>
> >
> > I am confused about the inferred type. where does this 'b comes from?
> >
> > --
> > 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: 2060 bytes --]
^ permalink raw reply [flat|nested] 10+ messages in thread
* RE: [Caml-list] format strings
2015-02-09 11:58 [Caml-list] format strings Jiten Pathy
2015-02-09 12:25 ` John F Carr
@ 2015-02-09 17:32 ` David Allsopp
2015-02-10 1:05 ` Jiten Pathy
1 sibling, 1 reply; 10+ messages in thread
From: David Allsopp @ 2015-02-09 17:32 UTC (permalink / raw)
To: Jiten Pathy, caml-list
Jiten Pathy wrote:
> When i define a wrapper around format functions like
>
> let f s = (fun x -> Printf.sprintf s x);;
>
> val f : ('a -> 'b, unit, bytes) format -> 'a -> 'b = <fun>
>
> I am confused about the inferred type. where does this 'b comes from?
Your wrapper carries the requirement that the format [s] must have at least one %-parameter. Writing without the fun might make this more obvious:
let f s x = Printf.sprintf s x
The 'a -> 'b is the encoding of this requirement. f "foo" will type. If instead you wrote:
let f s = Printf.sprintf s
you'd get the type you're expecting: ('a, unit, bytes) format -> 'a
and you can have format strings with no %-parameters (so f "foo" now types). If you meant to have it so that the format string must have *exactly* one %-parameter then you need an annotation:
let f s x : string = Printf.sprintf s x
and now f "%d" will type, but f "%s %d" will not.
HTH,
David
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: [Caml-list] format strings
2015-02-09 17:32 ` David Allsopp
@ 2015-02-10 1:05 ` Jiten Pathy
2015-02-10 2:47 ` Jiten Pathy
0 siblings, 1 reply; 10+ messages in thread
From: Jiten Pathy @ 2015-02-10 1:05 UTC (permalink / raw)
To: David Allsopp; +Cc: caml-list
Is there a way I can define a function which has the same signature of
printf family but is a no-op?
f : ('a, 'b, unit) format -> 'a
assert(f "%s %d" "hello" 1 = ());;
On Mon, Feb 9, 2015 at 9:32 AM, David Allsopp <dra-news@metastack.com> wrote:
> Jiten Pathy wrote:
>> When i define a wrapper around format functions like
>>
>> let f s = (fun x -> Printf.sprintf s x);;
>>
>> val f : ('a -> 'b, unit, bytes) format -> 'a -> 'b = <fun>
>>
>> I am confused about the inferred type. where does this 'b comes from?
>
> Your wrapper carries the requirement that the format [s] must have at least one %-parameter. Writing without the fun might make this more obvious:
>
> let f s x = Printf.sprintf s x
>
> The 'a -> 'b is the encoding of this requirement. f "foo" will type. If instead you wrote:
>
> let f s = Printf.sprintf s
>
> you'd get the type you're expecting: ('a, unit, bytes) format -> 'a
>
> and you can have format strings with no %-parameters (so f "foo" now types). If you meant to have it so that the format string must have *exactly* one %-parameter then you need an annotation:
>
> let f s x : string = Printf.sprintf s x
>
> and now f "%d" will type, but f "%s %d" will not.
>
> HTH,
>
>
> David
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: [Caml-list] format strings
2015-02-10 1:05 ` Jiten Pathy
@ 2015-02-10 2:47 ` Jiten Pathy
2015-02-10 7:44 ` David Allsopp
0 siblings, 1 reply; 10+ messages in thread
From: Jiten Pathy @ 2015-02-10 2:47 UTC (permalink / raw)
To: David Allsopp; +Cc: caml-list
nvm. make_printf is what I am after. Some complicated stuff is going
on in CamlInternalFormat.
On Mon, Feb 9, 2015 at 5:05 PM, Jiten Pathy <jpathy@fssrv.net> wrote:
> Is there a way I can define a function which has the same signature of
> printf family but is a no-op?
>
> f : ('a, 'b, unit) format -> 'a
>
> assert(f "%s %d" "hello" 1 = ());;
>
> On Mon, Feb 9, 2015 at 9:32 AM, David Allsopp <dra-news@metastack.com> wrote:
>> Jiten Pathy wrote:
>>> When i define a wrapper around format functions like
>>>
>>> let f s = (fun x -> Printf.sprintf s x);;
>>>
>>> val f : ('a -> 'b, unit, bytes) format -> 'a -> 'b = <fun>
>>>
>>> I am confused about the inferred type. where does this 'b comes from?
>>
>> Your wrapper carries the requirement that the format [s] must have at least one %-parameter. Writing without the fun might make this more obvious:
>>
>> let f s x = Printf.sprintf s x
>>
>> The 'a -> 'b is the encoding of this requirement. f "foo" will type. If instead you wrote:
>>
>> let f s = Printf.sprintf s
>>
>> you'd get the type you're expecting: ('a, unit, bytes) format -> 'a
>>
>> and you can have format strings with no %-parameters (so f "foo" now types). If you meant to have it so that the format string must have *exactly* one %-parameter then you need an annotation:
>>
>> let f s x : string = Printf.sprintf s x
>>
>> and now f "%d" will type, but f "%s %d" will not.
>>
>> HTH,
>>
>>
>> David
^ permalink raw reply [flat|nested] 10+ messages in thread
* RE: [Caml-list] format strings
2015-02-10 2:47 ` Jiten Pathy
@ 2015-02-10 7:44 ` David Allsopp
2015-02-10 7:55 ` Jiten Pathy
0 siblings, 1 reply; 10+ messages in thread
From: David Allsopp @ 2015-02-10 7:44 UTC (permalink / raw)
To: Jiten Pathy; +Cc: caml-list
Jiten Pathy wrote:
> nvm. make_printf is what I am after.
No it isn't - using internal functions is, to 1 significant figure, never what you're after. Apart from anything, you're instantly forcing your code to be 4.02+
> Some complicated stuff is going on in CamlInternalFormat.
Indeed - 4.02 has a very clever reimplementation of the printf engine.
> On Mon, Feb 9, 2015 at 5:05 PM wrote:
> > Is there a way I can define a function which has the same signature of
> > printf family but is a no-op?
> >
> > f : ('a, 'b, unit) format -> 'a
Printf.ifprintf (you can do similar with Printf.ksprintf and Printf.ikfprintf).
I don't know what lead you to read CamlInternalFormat.mli, but the reference manual (http://caml.inria.fr/pub/docs/manual-ocaml-4.02/libref/Printf.html) is a much better place to start for information on these modules than their source code.
David
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: [Caml-list] format strings
2015-02-10 7:44 ` David Allsopp
@ 2015-02-10 7:55 ` Jiten Pathy
2015-02-10 8:25 ` David Allsopp
0 siblings, 1 reply; 10+ messages in thread
From: Jiten Pathy @ 2015-02-10 7:55 UTC (permalink / raw)
To: David Allsopp; +Cc: caml-list
Ok, If i am not supposed to use make_printf, then what is the
recommended way of writing the following?
This adds a new line to given format string. In particular how do i
manipulate the format strings passed to me?
open CamlinternalFormat;;
let f (Format (fmt, _)) =
make_printf (fun o acc -> output_acc o (Acc_char_literal (acc,
'\n')); ignore o) stdout End_of_acc fmt
;;
val f : ('a, out_channel, unit, unit, unit, unit) format6 -> 'a = <fun>
utop # f "%s %d" "# " 3;;
# 3
- : unit = ()
I now have a slightly better understanding of CamlinternalFormat.
On Mon, Feb 9, 2015 at 11:44 PM, David Allsopp <dra-news@metastack.com> wrote:
> Jiten Pathy wrote:
>> nvm. make_printf is what I am after.
>
> No it isn't - using internal functions is, to 1 significant figure, never what you're after. Apart from anything, you're instantly forcing your code to be 4.02+
>
>> Some complicated stuff is going on in CamlInternalFormat.
>
> Indeed - 4.02 has a very clever reimplementation of the printf engine.
>
>> On Mon, Feb 9, 2015 at 5:05 PM wrote:
>> > Is there a way I can define a function which has the same signature of
>> > printf family but is a no-op?
>> >
>> > f : ('a, 'b, unit) format -> 'a
>
> Printf.ifprintf (you can do similar with Printf.ksprintf and Printf.ikfprintf).
>
> I don't know what lead you to read CamlInternalFormat.mli, but the reference manual (http://caml.inria.fr/pub/docs/manual-ocaml-4.02/libref/Printf.html) is a much better place to start for information on these modules than their source code.
>
>
> David
^ permalink raw reply [flat|nested] 10+ messages in thread
* RE: [Caml-list] format strings
2015-02-10 7:55 ` Jiten Pathy
@ 2015-02-10 8:25 ` David Allsopp
2015-02-10 8:34 ` Jiten Pathy
0 siblings, 1 reply; 10+ messages in thread
From: David Allsopp @ 2015-02-10 8:25 UTC (permalink / raw)
To: Jiten Pathy; +Cc: caml-list
Jiten Pathy wrote:
> Ok, If i am not supposed to use make_printf, then what is the recommended
> way of writing the following?
>
> This adds a new line to given format string. In particular how do i
> manipulate the format strings passed to me?
There wasn't a vast amount of type-safe manipulation possible with format strings in the old implementation, though given the newer representation, perhaps something may come in the future.
> open CamlinternalFormat;;
> let f (Format (fmt, _)) =
> make_printf (fun o acc -> output_acc o (Acc_char_literal (acc, '\n'));
> ignore o) stdout End_of_acc fmt ;; val f : ('a, out_channel, unit, unit,
> unit, unit) format6 -> 'a = <fun>
>
> utop # f "%s %d" "# " 3;;
> # 3
> - : unit = ()
However, for this "simple" manipulation there are two ways which don't touch the internals:
let f fmt = Printf.ksprintf (Printf.printf "%s\n") fmt
[that's my preference - to me it's clearer that you're wrapping the formatted output. I'd often do this to add %! as well - i.e. "%s\n%!"]
let f fmt = Printf.printf (fmt ^^ "\n")
[See http://caml.inria.fr/pub/docs/manual-ocaml-4.02/libref/Pervasives.html#6_Operationsonformatstrings]
David
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: [Caml-list] format strings
2015-02-10 8:25 ` David Allsopp
@ 2015-02-10 8:34 ` Jiten Pathy
0 siblings, 0 replies; 10+ messages in thread
From: Jiten Pathy @ 2015-02-10 8:34 UTC (permalink / raw)
To: David Allsopp; +Cc: caml-list
Thanks, that helps. I can't think of any complicated manipulation i
would need, for now.
On Tue, Feb 10, 2015 at 12:25 AM, David Allsopp <dra-news@metastack.com> wrote:
> Jiten Pathy wrote:
>> Ok, If i am not supposed to use make_printf, then what is the recommended
>> way of writing the following?
>>
>> This adds a new line to given format string. In particular how do i
>> manipulate the format strings passed to me?
>
> There wasn't a vast amount of type-safe manipulation possible with format strings in the old implementation, though given the newer representation, perhaps something may come in the future.
>
>> open CamlinternalFormat;;
>> let f (Format (fmt, _)) =
>> make_printf (fun o acc -> output_acc o (Acc_char_literal (acc, '\n'));
>> ignore o) stdout End_of_acc fmt ;; val f : ('a, out_channel, unit, unit,
>> unit, unit) format6 -> 'a = <fun>
>>
>> utop # f "%s %d" "# " 3;;
>> # 3
>> - : unit = ()
>
> However, for this "simple" manipulation there are two ways which don't touch the internals:
>
> let f fmt = Printf.ksprintf (Printf.printf "%s\n") fmt
>
> [that's my preference - to me it's clearer that you're wrapping the formatted output. I'd often do this to add %! as well - i.e. "%s\n%!"]
>
> let f fmt = Printf.printf (fmt ^^ "\n")
>
> [See http://caml.inria.fr/pub/docs/manual-ocaml-4.02/libref/Pervasives.html#6_Operationsonformatstrings]
>
>
> David
^ permalink raw reply [flat|nested] 10+ messages in thread
end of thread, other threads:[~2015-02-10 8:34 UTC | newest]
Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-02-09 11:58 [Caml-list] format strings Jiten Pathy
2015-02-09 12:25 ` John F Carr
2015-02-09 16:28 ` Jiten Pathy
2015-02-09 17:32 ` David Allsopp
2015-02-10 1:05 ` Jiten Pathy
2015-02-10 2:47 ` Jiten Pathy
2015-02-10 7:44 ` David Allsopp
2015-02-10 7:55 ` Jiten Pathy
2015-02-10 8:25 ` David Allsopp
2015-02-10 8:34 ` Jiten Pathy
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox