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 QAA29347; Fri, 2 Apr 2004 16:09:51 +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 QAA30986 for ; Fri, 2 Apr 2004 16:09:50 +0200 (MET DST) Received: from beaune.inria.fr (beaune.inria.fr [128.93.8.3]) by concorde.inria.fr (8.12.10/8.12.10) with ESMTP id i32E9nYM011937 for ; Fri, 2 Apr 2004 16:09:50 +0200 Received: by beaune.inria.fr (8.8.8/1.1.22.3/14Sep99-0328PM) id QAA0000030130; Fri, 2 Apr 2004 16:09:48 +0200 (MET DST) Date: Fri, 2 Apr 2004 16:09:48 +0200 From: Luc Maranget To: Paul Guyot Cc: caml-list@inria.fr Subject: Re: [Caml-list] failwith, raise and type inference Message-ID: <20040402160514.A29362@beaune.inria.fr> References: Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline User-Agent: Mutt/1.2.5.1i In-Reply-To: ; from pguyot@kallisys.net on Fri, Apr 02, 2004 at 03:41:26PM +0200 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 failwith:01 inference:01 3.07:01 failwith:01 avalue:01 avalue:01 val:01 clue:01 logics:01 --luc:01 dict:01 dict:01 ocaml:01 caml:01 Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk X-Status: X-Keywords: X-UID: 31 > Hello, > > My students came up with a behavior of ocaml I couldn't explain. > I have exactly the same result with 3.07pl2 and a fresh copy checked > out from CVS. > > >let rec f1 key dict = > > match dict with > > [] -> failwith "The key " ^ key ^ " could not be found" > > | (aKey, aValue)::tail -> > > if (aKey = key) > > then aValue > > else f1 key tail;; > > is of type: > > val f1 : string -> (string * string) list -> string = > > Any idea? I think I have a clue : failwith "The key " ^ key ^ " could not be found" is parsed as (failwith "The key ") ^ key ^ " could not be found" Hence the overall type of this expression is string. The other result of the pattern matching matching has the same type as aValue Finally, you get that the type of aValue (second component of your pairs is string). I think you can check my interpretation by issuing >f1 "bonga" [] >;; Exception: Failure "The key ". Of course, the intended coding is failwith ("The key " ^ key ^ " could not be found") Admitedly caml syntax is not very beginner friendly. But the observed parsing has some internal logics. On usualy understand that f x + y is in fact (f x) + y But here, with failwith being a ``special'' function and ^ a ``special'' operator, well... Cheers, --Luc Maranget ------------------- 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