caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] format type
@ 2002-11-03  7:08 Cezary Kaliszyk
  2002-11-04  4:00 ` Ken Wakita
  0 siblings, 1 reply; 5+ messages in thread
From: Cezary Kaliszyk @ 2002-11-03  7:08 UTC (permalink / raw)
  To: caml-list

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

I'm trying to write a function that takes a format and a function and
applies some (got from elsewere) arguments to the function.

All works ok for nonempty format.
Ie: 
 myfun "%c%s"
is of type:
 (char -> string -> unit) -> unit

IS IT POSSIBLE (with the current implementation) of the format type type
checking to pass some format that would require a (unit -> unit)
function?

Ie: I'd like:
 myfun ""
to be of type:
 (unit -> unit) -> unit

If such construnction is not possible at all perheapes some new
identifier "%?" should be added to fromat type type checking. This
argument would take exacly one argument of type unit.

My current implementation:
 
let receive (fmt : (('a, unit, unit) format)) (f : 'a) : unit =
  let fmt_str = string_of_format fmt in
  let rec parse f i =
    match fmt.[i] with
      ...
        I call recursively:
          parse (lazified ((forced f) with applied arg)) (i + 1)
      ...
  in
  Obj.magic (parse (fun () -> f) 0) ()
;;

The only known workaround for me for now is to pass "%t" and
make my function not (unit -> unit) but ('a -> unit). 

Writing code with changed types just for the sake of the language is
very bad. And with "%t" 'a seems to be (unit -> unit).

Cezary Kaliszyk
-- 

[-- Attachment #2: Type: application/pgp-signature, Size: 254 bytes --]

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

* Re: [Caml-list] format type
  2002-11-03  7:08 [Caml-list] format type Cezary Kaliszyk
@ 2002-11-04  4:00 ` Ken Wakita
  2002-11-04  7:54   ` Alessandro Baretta
  0 siblings, 1 reply; 5+ messages in thread
From: Ken Wakita @ 2002-11-04  4:00 UTC (permalink / raw)
  To: Cezary Kaliszyk, caml-list


Did you try "%t"?

Printf.printf "%t": (out_channel -> unit) -> unit = <fun>

You could simply ignore out_channel if you are not interested.

Ken Wakita

> From: Cezary Kaliszyk <ck189400@zodiac.mimuw.edu.pl>
> Date: Sun, 3 Nov 2002 08:08:58 +0100
> To: caml-list@inria.fr
> Subject: [Caml-list] format type
> 
> I'm trying to write a function that takes a format and a function and
> applies some (got from elsewere) arguments to the function.
> 
> All works ok for nonempty format.
> Ie: 
> myfun "%c%s"
> is of type:
> (char -> string -> unit) -> unit
> 
> IS IT POSSIBLE (with the current implementation) of the format type type
> checking to pass some format that would require a (unit -> unit)
> function?
> 
> Ie: I'd like:
> myfun ""
> to be of type:
> (unit -> unit) -> unit
> 
> If such construnction is not possible at all perheapes some new
> identifier "%?" should be added to fromat type type checking. This
> argument would take exacly one argument of type unit.
> 
> My current implementation:
> 
> let receive (fmt : (('a, unit, unit) format)) (f : 'a) : unit =
> let fmt_str = string_of_format fmt in
> let rec parse f i =
>   match fmt.[i] with
>     ...
>       I call recursively:
>         parse (lazified ((forced f) with applied arg)) (i + 1)
>     ...
> in
> Obj.magic (parse (fun () -> f) 0) ()
> ;;
> 
> The only known workaround for me for now is to pass "%t" and
> make my function not (unit -> unit) but ('a -> unit).
> 
> Writing code with changed types just for the sake of the language is
> very bad. And with "%t" 'a seems to be (unit -> unit).
> 
> Cezary Kaliszyk
> -- 
> 

-------------------
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] 5+ messages in thread

* Re: [Caml-list] format type
  2002-11-04  4:00 ` Ken Wakita
@ 2002-11-04  7:54   ` Alessandro Baretta
  2002-11-04 11:03     ` Cezary Kaliszyk
  0 siblings, 1 reply; 5+ messages in thread
From: Alessandro Baretta @ 2002-11-04  7:54 UTC (permalink / raw)
  To: Ken Wakita; +Cc: Cezary Kaliszyk, caml-list



Ken Wakita wrote:
> Did you try "%t"?
> 
> Printf.printf "%t": (out_channel -> unit) -> unit = <fun>

Since Cezary mentions parsing, I suppose he meant Scanf, 
rather than Printf.

> You could simply ignore out_channel if you are not interested.
> 
> Ken Wakita
> 
> 
>>From: Cezary Kaliszyk <ck189400@zodiac.mimuw.edu.pl>
>>Date: Sun, 3 Nov 2002 08:08:58 +0100
>>To: caml-list@inria.fr
>>Subject: [Caml-list] format type
>>
>>I'm trying to write a function that takes a format and a function and
>>applies some (got from elsewere) arguments to the function.
>>
>><snip>
>>
>>The only known workaround for me for now is to pass "%t" and
>>make my function not (unit -> unit) but ('a -> unit).

As far as I can see from the docs, "%t" is only meaningful 
with the Printf module. I see no mention of it in Scanf. 
What exactly are you trying to do anyway?

Assuming you are talking about the Scanf module, I'd say you 
can't. Of course, if your function takes a unit input, it 
can very well be a perfectly polymorphic function (à la 
ignore) with type ('a -> unit). In which case you can force 
a call to such function while scanning the input buffer by 
passing it a range conversion with an empty range: 
"%[^\000-\255]". Your function would be called with an empty 
string as an actual parameter.

Or, better yet, you can use the "%N" conversion, which 
passes the number of characters consumed. Just discard this 
value.

>>Writing code with changed types just for the sake of the language is
>>very bad. And with "%t" 'a seems to be (unit -> unit).

It is also very bad to complain without doing any work 
yourself. You are probably the only one on this list wishing 
to call a function with no input while parsing a buffer. 
Nothing wrong with this, apart from stylistic 
considerations, but it implies two things: 1) I don't 
suppose Pierre ever even thought of such a conversion for 
Scanf, and 2) even if he did, I doubt he'd care to implement 
it. If you really think it is necessary, you might consider 
hacking it into the Scanf module and submitting the patch to 
the maintainers.

Alex

-------------------
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] 5+ messages in thread

* Re: [Caml-list] format type
  2002-11-04  7:54   ` Alessandro Baretta
@ 2002-11-04 11:03     ` Cezary Kaliszyk
  2002-11-04 11:56       ` Alessandro Baretta
  0 siblings, 1 reply; 5+ messages in thread
From: Cezary Kaliszyk @ 2002-11-04 11:03 UTC (permalink / raw)
  To: Alessandro Baretta, caml-list

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

On Mon, Nov 04, 2002 at 08:54:13AM +0100, Alessandro Baretta wrote:
> >>From: Cezary Kaliszyk <ck189400@zodiac.mimuw.edu.pl>
> >>Date: Sun, 3 Nov 2002 08:08:58 +0100
> >>To: caml-list@inria.fr
> >>Subject: [Caml-list] format type
> >>
> >>I'm trying to write a function that takes a format and a function and
> >>applies some (got from elsewere) arguments to the function.
> >>
> >><snip>
> >>
> >>The only known workaround for me for now is to pass "%t" and
> >>make my function not (unit -> unit) but ('a -> unit).
> 
> As far as I can see from the docs, "%t" is only meaningful 
> with the Printf module. I see no mention of it in Scanf. 
> What exactly are you trying to do anyway?
I am trying to create a protocol for passing messages via network.
Each message may be acompanied by a certain amount of parameters
(of type char, int, float or string) (the parameters are dependant on the
message).

output/input_value aren't apropriate. (For network speed reasons, and no
type extensibility)

I am writing a function which adds a message to the protocol, which takes the
type of arguments accompanying the message and the receiving function.
It should return function used to send this message type. Eg:

let two_char_sender = add_to_protocol "%c%c" two_char_receiver;;

Where two_char_receiver : (char -> char -> unit)
And   tho_char_sender : (char -> char -> unit).

And I'd like the types to be controlled.

And I've written it. But it doesn't work if I want to create a message
that does not take any parameters. And I suppose the builtin type format
doesn't have the functionality necessary to write it. 
> Assuming you are talking about the Scanf module, I'd say you 
> can't. Of course, if your function takes a unit input, it 
> can very well be a perfectly polymorphic function (? la 
> ignore) with type ('a -> unit). In which case you can force 
> a call to such function while scanning the input buffer by 
> passing it a range conversion with an empty range: 
> "%[^\000-\255]". Your function would be called with an empty 
> string as an actual parameter.
I'm not talkin about Printf nor Scanf, but about my module that uses the
format type.
> Or, better yet, you can use the "%N" conversion, which 
> passes the number of characters consumed. Just discard this 
> value.
As above.
> >>Writing code with changed types just for the sake of the language is
> >>very bad. And with "%t" 'a seems to be (unit -> unit).
> 
> It is also very bad to complain without doing any work 
> yourself. You are probably the only one on this list wishing 
> to call a function with no input while parsing a buffer. 
I'm not parsing a buffer...
> Nothing wrong with this, apart from stylistic 
> considerations, but it implies two things: 1) I don't 
> suppose Pierre ever even thought of such a conversion for 
> Scanf, and 2) even if he did, I doubt he'd care to implement 
> it. If you really think it is necessary, you might consider 
> hacking it into the Scanf module and submitting the patch to 
> the maintainers.
It's not scanf relative...
> Alex
The format argument is parsed by me, not by scanf or printf (as I wrote
in the example code) and I don't know where in ocaml code is type
checking of the arguments accompanying the format type.

Cezary Kaliszyk
-- 

[-- Attachment #2: Type: application/pgp-signature, Size: 254 bytes --]

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

* Re: [Caml-list] format type
  2002-11-04 11:03     ` Cezary Kaliszyk
@ 2002-11-04 11:56       ` Alessandro Baretta
  0 siblings, 0 replies; 5+ messages in thread
From: Alessandro Baretta @ 2002-11-04 11:56 UTC (permalink / raw)
  To: Cezary Kaliszyk, Ocaml



Cezary Kaliszyk wrote:
> On Mon, Nov 04, 2002 at 08:54:13AM +0100, Alessandro Baretta wrote:
> 

>>>>The only known workaround for me for now is to pass "%t" and
>>>>make my function not (unit -> unit) but ('a -> unit).
>>>
>>As far as I can see from the docs, "%t" is only meaningful 
>>with the Printf module. I see no mention of it in Scanf. 
>>What exactly are you trying to do anyway?
> 
> I am trying to create a protocol for passing messages via network.
> Each message may be acompanied by a certain amount of parameters
> (of type char, int, float or string) (the parameters are dependant on the
> message).
> 
> output/input_value aren't apropriate. (For network speed reasons, and no
> type extensibility)

I'd avoid "reinventing the wheel" and stick with well known 
protocols. Did you try anything XML based? It's versatile 
and well supported. And it will spare you a lot of low level 
parsing routines. But of course, it is suboptimal, bitrate-wise.

> I am writing a function which adds a message to the protocol, which takes the
> type of arguments accompanying the message and the receiving function.
> It should return function used to send this message type. Eg:
> 
> let two_char_sender = add_to_protocol "%c%c" two_char_receiver;;
> 
> Where two_char_receiver : (char -> char -> unit)
> And   tho_char_sender : (char -> char -> unit).
> 
> And I'd like the types to be controlled.
> 
> And I've written it. But it doesn't work if I want to create a message
> that does not take any parameters. And I suppose the builtin type format
> doesn't have the functionality necessary to write it. 

I have a feeling you are asking more of the format type than 
it was meant to handle. The format type is meant to be used 
with the Scanf and Printf modules. You can parse a message 
according to a format with Scanf, and by the same means you 
can format a message with Printf. If you are using format 
values in different contexts, you are probably abusing them.

>>Assuming you are talking about the Scanf module, I'd say you 
>>can't. Of course, if your function takes a unit input, it 
>>can very well be a perfectly polymorphic function (? la 
>>ignore) with type ('a -> unit). In which case you can force 
>>a call to such function while scanning the input buffer by 
>>passing it a range conversion with an empty range: 
>>"%[^\000-\255]". Your function would be called with an empty 
>>string as an actual parameter.
> 
> I'm not talkin about Printf nor Scanf, but about my module that uses the
> format type.
<snip>
> The format argument is parsed by me, not by scanf or printf (as I wrote
> in the example code) and I don't know where in ocaml code is type
> checking of the arguments accompanying the format type.
> 
> Cezary Kaliszyk

I lost track of what you are trying to do. Sorry for this. 
But I have a feeling you are making things much more complex 
than necessary.

Did you consider using yacc to parse your data stream 
according to your protocol specification? Format strings are 
sad heritage from C, and good for quick hacks where you do 
not want to take the time to write up a lexer and a parser, 
but you must realize they have some limitations.

I'm sorry for not being any more helpful.

Alex

-------------------
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] 5+ messages in thread

end of thread, other threads:[~2002-11-05  8:55 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2002-11-03  7:08 [Caml-list] format type Cezary Kaliszyk
2002-11-04  4:00 ` Ken Wakita
2002-11-04  7:54   ` Alessandro Baretta
2002-11-04 11:03     ` Cezary Kaliszyk
2002-11-04 11:56       ` Alessandro Baretta

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