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 EAA00788; Wed, 3 Sep 2003 04:08:28 +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 EAA15835 for ; Wed, 3 Sep 2003 04:08:25 +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.11.1/8.11.1) with ESMTP id h8328Nf20702 for ; Wed, 3 Sep 2003 04:08:23 +0200 (MET DST) Received: from localhost (suiren.kurims.kyoto-u.ac.jp [130.54.16.25]) by kurims.kurims.kyoto-u.ac.jp (8.9.3p2/3.7W) with ESMTP id LAA06994; Wed, 3 Sep 2003 11:08:13 +0900 (JST) To: c_bauer@informatik.uni-kl.de Cc: caml-list@inria.fr Subject: Re: [Caml-list] Type inference + optional parameters In-Reply-To: References: 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: <20030903110813O.garrigue@kurims.kyoto-u.ac.jp> Date: Wed, 03 Sep 2003 11:08:13 +0900 From: Jacques Garrigue X-Dispatcher: imput version 20000228(IM140) X-Loop: caml-list@inria.fr X-Spam: no; 0.00; caml-list:01 inference:01 jacques:01 bauer:01 bauer:01 ocamls:01 inference:01 type-safe:01 3.07:01 val:01 val:01 expressive:01 struct:01 jacques:01 compiler:01 Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk From: Christoph Bauer > ocamls type inference uses information of optionl arguments. This > results in a strange behaviour. Brian Rogoff already answered this one. Do you seriously expect a type-safe compiler to simply ignore the type of the default value :-) > Here is an example: > > Objective Caml version 3.07+beta 2 > > # let do_with_opt_conv ?(conv = fun s -> s) a = conv a;; > do_with_opt_conv ~conv:string_of_int "1";;- : int = 1 > # val do_with_opt_conv : ?conv:('a -> 'a) -> 'a -> 'a = > # do_with_opt_conv ~conv:string_of_int "1";; > > Is there a solution? It is not possible to > add the "right" type information: > > # let do_with_opt_conv ?(conv : 'a -> 'b = fun s -> s) (a:'a) : 'b = conv a;; > val do_with_opt_conv : ?conv:('a -> 'a) -> 'a -> 'a = The answer is that the function you want cannot be defined with optional arguments. Actually, we need a slightly more expressive type to allow this. module Default : sig type ('a,'b) t val default : ('a,'a) t val custom : 'a -> ('a,'b) t val get : (unit -> 'b) -> ('a,'b) t -> 'a end = struct type ('a,'b) t = Custom of 'a | Default let default = Default let custom x = Custom x let get def = function Default -> Obj.magic (def ()) | Custom x -> x end Note that you need to use Obj.magic to define the above module, but the type of [default] ensures that this is sound. Then you can define your function: # let do_with_opt_conv ~conv a = let conv = Default.get (fun () x -> x) conv in conv a ;; val do_with_opt_conv : conv:('a -> 'b, 'c -> 'c) Default.t -> 'a -> 'b # do_with_opt_conv ~conv:Default.default 1;; - : int = 1 # do_with_opt_conv ~conv:(Default.custom string_of_int) 1;; - : string = "1" 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