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 EAA26027; Mon, 12 Apr 2004 04:26:00 +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 EAA25890 for ; Mon, 12 Apr 2004 04:25:58 +0200 (MET DST) Received: from kurims.kurims.kyoto-u.ac.jp (kurims.kurims.kyoto-u.ac.jp [130.54.16.1]) by concorde.inria.fr (8.12.10/8.12.10) with ESMTP id i3C2PuYM010433 for ; Mon, 12 Apr 2004 04:25:57 +0200 Received: from localhost (suiren.kurims.kyoto-u.ac.jp [130.54.16.25]) by kurims.kurims.kyoto-u.ac.jp (8.9.3p2-20030924/3.7W) with ESMTP id LAA25327; Mon, 12 Apr 2004 11:25:51 +0900 (JST) To: nadji@noos.fr Cc: caml-list@inria.fr Subject: Re: [Caml-list] Fwd: Polymorphic optional label argument, with default In-Reply-To: <200404111723.14238.nadji@noos.fr> References: <20040411083357.GA19305@redhat.com> <20040411221619G.garrigue@kurims.kyoto-u.ac.jp> <200404111723.14238.nadji@noos.fr> X-Mailer: Mew version 1.94.2 on Emacs 21.2 / Mule 5.0 (SAKAKI) Mime-Version: 1.0 Content-Type: Text/Plain; charset=us-ascii Content-Transfer-Encoding: 7bit Message-Id: <20040412112550J.garrigue@kurims.kyoto-u.ac.jp> Date: Mon, 12 Apr 2004 11:25:50 +0900 From: Jacques Garrigue X-Dispatcher: imput version 20000228(IM140) 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 jacques:01 nadji:01 nadji:01 2004:99 jacques:01 omitted:01 datatypes:01 analogous:01 'a-:01 implemented:01 incr:01 multimatch:01 val:01 incr:01 Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk X-Status: X-Keywords: X-UID: 257 From: nadji > Le Sunday 11 April 2004 15:16, Jacques Garrigue a ecrit : > > Basically, what you are asking for is a new type of constraint, which > > is only to be applied when the optional argument is omitted (and as > > result the default is selected). > > This is not possible in the current type system, and while there are > > some uses for that, it would be hard to justify making types more > > complex in the general case, just to handle this specific problem. > I would like to point out that a machinery to handle this problem > is guarded recursive datatypes proposed by Xi. It is a framework > that solves a bunch of other problems and is compatible with ml type > system (there is currently at Inria a student working on an Ocaml integration > of this type system). > Combining it with your labeled parameters leads to a well typed code > analogous to your phantom-type based solution. Some even view > Xi's GRDT as a way to avoid your Obj.magic when matching against > a phantom type. > For those interested, the pseudo code would be something like : > > guarded 'a opt = Omit with 'a = int | Arg of 'a->unit > let plot : ?(labels:'a opt) -> 'a graph -> unit = > fun ?(labels=Omit) g -> > let printing_labels : 'a -> unit = > match labels with > Omit -> print_int > | Arg x -> x > in > ... printing_labels node_label ... I was not aware that there was already work to include guarded types in ocaml. They are indeed a nice solution to this problem, particularly as they allow to cleanly separate issues. Another (less clean) way to do that is with dependant polymorphic variants, as implemented in a branch of the ocaml CVS: # let incr ~f x = let x = x+1 in multimatch f with `None -> x | `Some f -> f x ;; val incr : f:[< `None & 'a = int | `Some of int -> 'b & 'a = 'b ] -> int -> 'a = # incr ~f:`None 3;; - : int = 4 # incr ~f:(`Some string_of_int) 3;; - : string = "4" (Note that for this to really work, optional arguments would have to use polymorphic variants, which is why it is not as clean as guarded types.) Jacques Garrigue ------------------- 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