caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] format4 query
@ 2004-05-19  6:15 David J. Trombley
  2004-05-19  7:22 ` Olivier Andrieu
  0 siblings, 1 reply; 4+ messages in thread
From: David J. Trombley @ 2004-05-19  6:15 UTC (permalink / raw)
  To: caml-list; +Cc: hmaon


Suppose I have

type listener = string -> unit

which is intended to receive a string type as a value.

Could someone demonstrate a function that takes a listener
and a format as arguments, and uses sprintf to obtain a
string, and then calls the listener with the string
argument? No matter how I seem to do this, the first
parameter of the format4 type seems to get inferred to
string, causing the parser to not consume all of the
intended format tokens, causing a 'too many arguments'
error.  For example:

# type listener = string -> unit;;
type listener = string -> unit
# let r s = printf "%s\n" s;;
val r : string -> unit = <fun>
# let q (a : listener) b = let str  = sprintf b in a str;;
val q : listener -> (string, unit, string) format -> unit =
<fun>
(* already messed up as evidenced by the first 'string' *)
# q r "%s ya" "hey";;
This function is applied to too many arguments, maybe you
forgot a `;'

I've discovered the kprintf function, which looked promising
since I could get simple examples to work, but utterly fails
in the same stupid way when I put it in a context that is
not a toplevel evaluation. (ie. I try to use this in some
functional way, and the type inference screws up).

Isn't there some way to simply define a function that takes
a format4 arg in some opaque way so that the type of the
result of the kprintf doesn't mess up the type of the
original function by some complicated inference mechanism?

My actual code currently looks something like this:

   Hashtbl.iter ( function cbx ->
      function
        x when x.minLevel <= level ->
           (try (Hashtbl.find x.catchExnTable error) error
            with Not_found -> let ccb = x.callback stream
level error in
                                        Printf.kprintf ccb
fmt)
      | x -> ()
     ) stream.listeners;

and the types are

val stream : log_stream
val level : log_level
val error : log_error
val fmt : <some sort of format4 that is broken>

type log_callback = (log_stream -> log_level -> log_error ->
string -> unit)
 and log_stream   = { streamName : string;
                      listeners : (log_callback, log_entry)
Hashtbl.t }
 and log_entry    = { callback : log_callback;
                      minLevel : log_level;
                      expectList : (int * log_error) list;
                      catchExnTable : (log_error,
log_error -> unit) Hashtbl.t }

Thanks,
    David J. Trombley

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

* Re: [Caml-list] format4 query
  2004-05-19  6:15 [Caml-list] format4 query David J. Trombley
@ 2004-05-19  7:22 ` Olivier Andrieu
  2004-05-19 16:13   ` David J. Trombley
  0 siblings, 1 reply; 4+ messages in thread
From: Olivier Andrieu @ 2004-05-19  7:22 UTC (permalink / raw)
  To: swampy; +Cc: caml-list, hmaon

 David J. Trombley [Wed, 19 May 2004]:
 > 
 > Suppose I have
 > 
 > type listener = string -> unit
 > 
 > which is intended to receive a string type as a value.
 > 
 > Could someone demonstrate a function that takes a listener
 > and a format as arguments, and uses sprintf to obtain a
 > string, and then calls the listener with the string
 > argument? 

That is precisely what kprintf does.

# type listener = string -> unit;;
type listener = string -> unit
# let f (l : listener) fmt = Printf.kprintf l fmt ;;
val f : listener -> ('a, unit, string, unit) format4 -> 'a = <fun>
# f print_endline "%s %d" "abc" 23 ;;
abc 23
- : unit = ()

-- 
   Olivier

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

* Re: [Caml-list] format4 query
  2004-05-19  7:22 ` Olivier Andrieu
@ 2004-05-19 16:13   ` David J. Trombley
  2004-05-19 17:20     ` Damien Doligez
  0 siblings, 1 reply; 4+ messages in thread
From: David J. Trombley @ 2004-05-19 16:13 UTC (permalink / raw)
  To: Olivier Andrieu; +Cc: caml-list, hmaon


Right, as I said I can get simple examples to work.   But I
can't get syntax such as

 # let g (l : listener) (k : int) fmt =
    match k with
      0 -> 0
    | x -> Printf.kprintf l fmt; k;;

val g : listener -> int -> ('a, unit, string, unit)
format4 -> int = <fun>

to function, as you can see the format type has been
inferred to a type that is not useful at all.

I'd like to use kprintf as a side effect to a function which
evaluates to some type, but I would not like that type or
the unit type of a ; evaluation to affect the format type.

-dj

----- Original Message ----- 
From: "Olivier Andrieu" <andrieu@ijm.jussieu.fr>
To: <swampy@spacebird.net>
Cc: <caml-list@inria.fr>; <hmaon@bumba.net>
Sent: Wednesday, May 19, 2004 3:22 AM
Subject: Re: [Caml-list] format4 query


> David J. Trombley [Wed, 19 May 2004]:
>  >
>  > Suppose I have
>  >
>  > type listener = string -> unit
>  >
>  > which is intended to receive a string type as a value.
>  >
>  > Could someone demonstrate a function that takes a
listener
>  > and a format as arguments, and uses sprintf to obtain a
>  > string, and then calls the listener with the string
>  > argument?
>
> That is precisely what kprintf does.
>
> # type listener = string -> unit;;
> type listener = string -> unit
> # let f (l : listener) fmt = Printf.kprintf l fmt ;;
> val f : listener -> ('a, unit, string, unit) format4 -> 'a
= <fun>
> # f print_endline "%s %d" "abc" 23 ;;
> abc 23
> - : unit = ()
>
> -- 
>    Olivier
>

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

* Re: [Caml-list] format4 query
  2004-05-19 16:13   ` David J. Trombley
@ 2004-05-19 17:20     ` Damien Doligez
  0 siblings, 0 replies; 4+ messages in thread
From: Damien Doligez @ 2004-05-19 17:20 UTC (permalink / raw)
  To: David J. Trombley; +Cc: <caml-list@inria.fr>

On May 19, 2004, at 18:13, David J. Trombley wrote:

> Right, as I said I can get simple examples to work.   But I
> can't get syntax such as
>
>  # let g (l : listener) (k : int) fmt =
>     match k with
>       0 -> 0
>     | x -> Printf.kprintf l fmt; k;;
>
> val g : listener -> int -> ('a, unit, string, unit)
> format4 -> int = <fun>
>
> to function, as you can see the format type has been
> inferred to a type that is not useful at all.
>
> I'd like to use kprintf as a side effect to a function which
> evaluates to some type, but I would not like that type or
> the unit type of a ; evaluation to affect the format type.

Here is the trick: the call to kprintf must always be the last
thing that your function does.  You get control back when kprintf
calls your continuation function:

   let g (l : listener) (k : int) fmt =
     let cont s =
       match k with
       | 0 -> 0
       | _ -> l s; k
     in
     Printf.kprintf cont fmt
   ;;

Note that kprintf will be called in all cases, but that's
unavoidable because it has to consume the arguments that will
be passed to g after the fmt argument.

-- Damien

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

end of thread, other threads:[~2004-05-19 17:20 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-05-19  6:15 [Caml-list] format4 query David J. Trombley
2004-05-19  7:22 ` Olivier Andrieu
2004-05-19 16:13   ` David J. Trombley
2004-05-19 17:20     ` Damien Doligez

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