caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* question about polymorphic methods
@ 2007-09-24  2:11 Warren Harris
  2007-09-24 12:23 ` [Caml-list] " Julien Moutinho
  0 siblings, 1 reply; 4+ messages in thread
From: Warren Harris @ 2007-09-24  2:11 UTC (permalink / raw)
  To: caml-list

I have a simple output stream class that abstracts over out_channels and 
buffers. I would like to add a printf method that takes a format 
directive, and calls the appropriate Printf function. To do this, I 
needed to introduce a polymorphic method which abstracts over the first 
type parameter to the format type (the 'a parameter of printf):

class type ['b] out_stream =
object
  method print : string -> unit
  method printf : 'a . ('a, 'b, unit) format -> 'a
  method flush : unit
end

class out_stream_of_buffer buf =
object (self : Buffer.t #out_stream)
  method print str = Buffer.add_string buf str
  method printf : 'a . ('a, Buffer.t, unit) format -> 'a =
    fun fmt -> Printf.bprintf buf fmt
  method flush = ()
end

class out_stream_of_channel och =
object (self : out_channel #out_stream)
  method print str = output_string och str
  method printf : 'a . ('a, out_channel, unit) format -> 'a =
    fun fmt -> Printf.fprintf och fmt
  method flush = flush och
end

However, as you can see from this code, I also needed to abstract over 
the second parameter to format in the definition of the out_stream class 
type ('b is the type of the first argument of the Printf function). This 
type parameter propagates through numerous places in my code, in some 
cases requiring other methods to become polymorphic. This is 
unfortunate, since 'b should be completely hidden by the particular 
implementation of out_stream (Buffer.t in the case of 
out_stream_of_buffer, or out_channel in the case of out_stream_of_channel).

Is there some other way to implement this that I'm overlooking? It seems 
like 'b should be "monomorphic" ('_b) and determined uniquely whenever 
the printf method is called. However, if I eliminate the 'b class type 
parameter, I get the following error:

  .......... out_stream =
  object
    method print : string -> unit
    method printf : 'a . ('a, 'b, unit) format -> 'a
    method flush : unit
  end
Some type variables are unbound in this type:
  class type out_stream =
    object
      method flush : unit
      method print : string -> unit
      method printf : ('a, 'b, unit) format -> 'a
    end
The method printf has type 'a. ('a, 'b, unit) format -> 'a where 'b
is unbound

Any suggestions on a better way to do this would be appreciated.

BTW, all this code would be unnecessary if ocaml provided an 
output_channel_of_buffer primitive. :-)

Warren


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

* Re: [Caml-list] question about polymorphic methods
  2007-09-24  2:11 question about polymorphic methods Warren Harris
@ 2007-09-24 12:23 ` Julien Moutinho
  2007-09-24 23:45   ` [Caml-list] question about polymorphic methods (caml: to exclusive) Warren Harris
  0 siblings, 1 reply; 4+ messages in thread
From: Julien Moutinho @ 2007-09-24 12:23 UTC (permalink / raw)
  To: caml-list

On Sun, Sep 23, 2007 at 07:11:21PM -0700, Warren Harris wrote:
> I have a simple output stream class that abstracts over out_channels and 
> buffers. I would like to add a printf method that takes a format directive, 
> and calls the appropriate Printf function.
> [...]
> This type parameter propagates through numerous places in my code,
> in some cases requiring other methods to become polymorphic.

Consider using the class below instead of out_stream directly:

class my'out_stream och buf =
  object
    val buffer = new out_stream_of_buffer buf
    method buffer = buffer
    val outchan = new out_stream_of_channel och
    method outchan = outchan
  end

Or this one, depending on how you get your buf and och:

class my'out_stream'opt
  ?och ?buf () =
  object (self)
    val mutable buffer = None
    method init_buffer buf =
        buffer <- Some (new out_stream_of_buffer buf)
    initializer
        match buf with None -> ()
        | Some buf -> self#init_buffer buf
    method buffer =
        match buffer with
        | Some o -> o
        | None -> failwith "MyStream.my'out_stream'opt#buf: no buffer provided"
    
    val mutable outchan = None
    method init_outchan och =
        outchan <- Some (new out_stream_of_channel och)
    initializer
        match och with None -> ()
        | Some och -> self#init_outchan och
    method outchan =
        match outchan with
        | Some o -> o
        | None -> failwith "MyStream.my'out_stream'opt#och: no channel provided"
  end

HTH,
  Julien.


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

* Re: [Caml-list] question about polymorphic methods (caml: to exclusive)
  2007-09-24 12:23 ` [Caml-list] " Julien Moutinho
@ 2007-09-24 23:45   ` Warren Harris
  2007-09-25  1:25     ` Till Varoquaux
  0 siblings, 1 reply; 4+ messages in thread
From: Warren Harris @ 2007-09-24 23:45 UTC (permalink / raw)
  To: Julien Moutinho - julien.moutinho@gmail.com; +Cc: caml-list

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

Julien, Thanks for your suggestion. I realize that I can paper over the 
problem in various ways, but I was hoping for a more type-theoretic 
approach to solving the problem of needing an implementation-specific 
but otherwise abstract type parameter in printf's signature. If I could 
declare 'b as a function of the particular implementation in question, 
that might solve it, but I'm not sure how to express this.

Warren


Julien Moutinho - julien.moutinho@gmail.com wrote:
> On Sun, Sep 23, 2007 at 07:11:21PM -0700, Warren Harris wrote:
>   
>> I have a simple output stream class that abstracts over out_channels and 
>> buffers. I would like to add a printf method that takes a format directive, 
>> and calls the appropriate Printf function.
>> [...]
>> This type parameter propagates through numerous places in my code,
>> in some cases requiring other methods to become polymorphic.
>>     
>
> Consider using the class below instead of out_stream directly:
>
> class my'out_stream och buf =
>   object
>     val buffer = new out_stream_of_buffer buf
>     method buffer = buffer
>     val outchan = new out_stream_of_channel och
>     method outchan = outchan
>   end
>
> Or this one, depending on how you get your buf and och:
>
> class my'out_stream'opt
>   ?och ?buf () =
>   object (self)
>     val mutable buffer = None
>     method init_buffer buf =
>         buffer <- Some (new out_stream_of_buffer buf)
>     initializer
>         match buf with None -> ()
>         | Some buf -> self#init_buffer buf
>     method buffer =
>         match buffer with
>         | Some o -> o
>         | None -> failwith "MyStream.my'out_stream'opt#buf: no buffer provided"
>     
>     val mutable outchan = None
>     method init_outchan och =
>         outchan <- Some (new out_stream_of_channel och)
>     initializer
>         match och with None -> ()
>         | Some och -> self#init_outchan och
>     method outchan =
>         match outchan with
>         | Some o -> o
>         | None -> failwith "MyStream.my'out_stream'opt#och: no channel provided"
>   end
>
> HTH,
>   Julien.
>
> _______________________________________________
> Caml-list mailing list. Subscription management:
> http://yquem.inria.fr/cgi-bin/mailman/listinfo/caml-list
> Archives: http://caml.inria.fr
> 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: 3170 bytes --]

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

* Re: [Caml-list] question about polymorphic methods (caml: to exclusive)
  2007-09-24 23:45   ` [Caml-list] question about polymorphic methods (caml: to exclusive) Warren Harris
@ 2007-09-25  1:25     ` Till Varoquaux
  0 siblings, 0 replies; 4+ messages in thread
From: Till Varoquaux @ 2007-09-25  1:25 UTC (permalink / raw)
  To: Warren Harris; +Cc: caml-list

It appears to me as though the second argument to the format type is
used only internally in ocaml's stdlib (by mkprintf for instance) and
that it should not be exposed the way it is. I do not believe you have
a simple way to get rid of it  without resorting to some magic
(yikes):

class type out_stream =
object
 method print : string -> unit
  method printf : 'a . ('a,unit,unit) format -> 'a
 method flush : unit
end

class out_stream_of_buffer buf =
object (self)
  method print str = Buffer.add_string buf str
  method printf : 'a . ('a,unit,unit) format -> 'a =
  fun fmt -> Printf.bprintf buf (Obj.magic fmt)
 method flush = ()
end

..

There is some black (Obj.)magic going here (Printf is very magical
anyways) so you can expect troubles (unless the second parameter is
really useless).

Cheers,
Till

On 9/25/07, Warren Harris <warren@liveops.com> wrote:
>
>  Julien, Thanks for your suggestion. I realize that I can paper over the
> problem in various ways, but I was hoping for a more type-theoretic approach
> to solving the problem of needing an implementation-specific but otherwise
> abstract type parameter in printf's signature. If I could declare 'b as a
> function of the particular implementation in question, that might solve it,
> but I'm not sure how to express this.
>
>  Warren
>
>
>  Julien Moutinho - julien.moutinho@gmail.com wrote:
>  On Sun, Sep 23, 2007 at 07:11:21PM -0700, Warren Harris wrote:
>
>
>  I have a simple output stream class that abstracts over out_channels and
> buffers. I would like to add a printf method that takes a format directive,
> and calls the appropriate Printf function.
> [...]
> This type parameter propagates through numerous places in my code,
> in some cases requiring other methods to become polymorphic.
>
>  Consider using the class below instead of out_stream directly:
>
> class my'out_stream och buf =
>  object
>  val buffer = new out_stream_of_buffer buf
>  method buffer = buffer
>  val outchan = new out_stream_of_channel och
>  method outchan = outchan
>  end
>
> Or this one, depending on how you get your buf and och:
>
> class my'out_stream'opt
>  ?och ?buf () =
>  object (self)
>  val mutable buffer = None
>  method init_buffer buf =
>  buffer <- Some (new out_stream_of_buffer buf)
>  initializer
>  match buf with None -> ()
>  | Some buf -> self#init_buffer buf
>  method buffer =
>  match buffer with
>  | Some o -> o
>  | None -> failwith "MyStream.my'out_stream'opt#buf: no buffer provided"
>
>  val mutable outchan = None
>  method init_outchan och =
>  outchan <- Some (new out_stream_of_channel och)
>  initializer
>  match och with None -> ()
>  | Some och -> self#init_outchan och
>  method outchan =
>  match outchan with
>  | Some o -> o
>  | None -> failwith "MyStream.my'out_stream'opt#och: no channel provided"
>  end
>
> HTH,
>  Julien.
>
> _______________________________________________
> Caml-list mailing list. Subscription management:
> http://yquem.inria.fr/cgi-bin/mailman/listinfo/caml-list
> Archives: http://caml.inria.fr
> Beginner's list:
> http://groups.yahoo.com/group/ocaml_beginners
> Bug reports: http://caml.inria.fr/bin/caml-bugs
>
>
>
> _______________________________________________
> Caml-list mailing list. Subscription management:
> http://yquem.inria.fr/cgi-bin/mailman/listinfo/caml-list
> Archives: http://caml.inria.fr
> Beginner's list:
> http://groups.yahoo.com/group/ocaml_beginners
> Bug reports: http://caml.inria.fr/bin/caml-bugs
>
>


-- 
http://till-varoquaux.blogspot.com/


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

end of thread, other threads:[~2007-09-25  1:25 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2007-09-24  2:11 question about polymorphic methods Warren Harris
2007-09-24 12:23 ` [Caml-list] " Julien Moutinho
2007-09-24 23:45   ` [Caml-list] question about polymorphic methods (caml: to exclusive) Warren Harris
2007-09-25  1:25     ` Till Varoquaux

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