From mboxrd@z Thu Jan 1 00:00:00 1970 Received: (from majordomo@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id AAA19830; Sun, 11 Apr 2004 00:40:14 +0200 (MET DST) X-Authentication-Warning: pauillac.inria.fr: majordomo set sender to owner-caml-list@pauillac.inria.fr using -f Received: from concorde.inria.fr (concorde.inria.fr [192.93.2.39]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id AAA19828 for ; Sun, 11 Apr 2004 00:40:13 +0200 (MET DST) Received: from moutng.kundenserver.de (moutng.kundenserver.de [212.227.126.185]) by concorde.inria.fr (8.12.10/8.12.10) with ESMTP id i3AMeCYM010835 for ; Sun, 11 Apr 2004 00:40:12 +0200 Received: from [212.227.126.160] (helo=mrelayng.kundenserver.de) by moutng.kundenserver.de with esmtp (Exim 3.35 #1) id 1BCR91-0004RE-00; Sun, 11 Apr 2004 00:40:11 +0200 Received: from [80.129.97.23] (helo=gate.gerd-stolpmann.de) by mrelayng.kundenserver.de with asmtp (Exim 3.35 #1) id 1BCR90-0003lQ-00; Sun, 11 Apr 2004 00:40:11 +0200 Received: from ice.gerd-stolpmann.de (ice.gerd-stolpmann.de [192.168.0.13]) by gate.gerd-stolpmann.de (Postfix) with ESMTP id 24F0D575A; Sun, 11 Apr 2004 00:40:10 +0200 (CEST) Received: by ice.gerd-stolpmann.de (Postfix, from userid 1000) id 10553B032; Sun, 11 Apr 2004 00:40:08 +0200 (CEST) Subject: Re: [Caml-list] Fwd: Polymorphic optional label argument, with default From: Gerd Stolpmann To: Richard Jones Cc: caml-list@inria.fr In-Reply-To: <20040410213616.GA28863@redhat.com> References: <20040410213616.GA28863@redhat.com> Content-Type: text/plain Content-Transfer-Encoding: 7bit Message-Id: <1081636807.24595.88.camel@ice.gerd-stolpmann.de> Mime-Version: 1.0 X-Mailer: Ximian Evolution 1.4.5 Date: Sun, 11 Apr 2004 00:40:08 +0200 X-Provags-ID: kundenserver.de abuse@kundenserver.de auth:a6865a839c0178d9aa0ce41878507ea2 X-Miltered: at concorde by Joe's j-chkmail ("http://j-chkmail.ensmp.fr")! X-Loop: caml-list@inria.fr X-Spam: no; 0.00; caml-list:01 gerd:01 stolpmann:01 2004:99 beginners:01 api:01 beginners:01 unsound:01 extensional:01 gerd:01 stolpmann:01 viktoriastr:01 64293:01 darmstadt:01 ocaml:01 Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk X-Status: X-Keywords: X-UID: 233 On Sam, 2004-04-10 at 23:36, Richard Jones wrote: > Hi: > > I posted the message below originally on ocaml_beginners. There were > many helpful responses, and the general consensus was that this was > not possible, but no one could give a definitive answer. I just > wanted to confirm on the 'grown-ups' list (:-) if this is really true > before I finalise the API I am designing. > > Thanks, > > Rich. > > To: ocaml_beginners@yahoogroups.com > From: Richard Jones > Subject: Polymorphic optional label argument, with default > > I have a function defined: > > let plot ?(labels = string_of_int) graph = > (* ... *) > > The idea of the optional 'labels' argument is to specify a polymorphic > function 'a -> string which is used to print labels stored in the > 'graph' argument. > > The problem is that because string_of_int is obviously int -> string, > my function is typed as: > > plot : ?labels : (int -> string) -> (* ... int ... *) -> unit > > but I want it to be typed as: > > plot : ?labels : ('a -> string) -> (* ... 'a ... *) -> unit > > [The 'a types are the same type. If I leave out the initializer, then > it works.] > > How to? This does not work. If "plot" were not specialised to int labels, you could call plot string_graph (i.e. without ~labels), and where string_graph would be another specialisation to string labels. This is unsound, because you would apply string_of_int to strings. One way out is the two function solution already discussed in ocaml-beginners (i.e. having "plot" for the polymorphic case, and "plot_int" for the int case). Another idea is to store the default for "labels" in the graph. (Or to implement the graphs with classes, which is a similar approach, but more effective when you have several such defaults.) I suppose there is also a good solution with extensional polymorphism, but this isn't yet in mainstream O'Caml. Gerd -- ------------------------------------------------------------ Gerd Stolpmann * Viktoriastr. 45 * 64293 Darmstadt * Germany gerd@gerd-stolpmann.de http://www.gerd-stolpmann.de ------------------------------------------------------------ ------------------- 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