From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: weis Received: (from weis@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id JAA28213 for caml-redistribution@pauillac.inria.fr; Tue, 25 Jan 2000 09:39:59 +0100 (MET) Resent-Message-Id: <200001250839.JAA28213@pauillac.inria.fr> Received: from nez-perce.inria.fr (nez-perce.inria.fr [192.93.2.78]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id SAA00802 for ; Mon, 24 Jan 2000 18:06:14 +0100 (MET) Received: from uni-sb.de (uni-sb.de [134.96.252.33]) by nez-perce.inria.fr (8.8.7/8.8.7) with ESMTP id SAA20864 for ; Mon, 24 Jan 2000 18:06:12 +0100 (MET) Received: from cs.uni-sb.de (cs.uni-sb.de [134.96.252.31]) by uni-sb.de (8.9.3/2000011400) with ESMTP id SAA23691 for ; Mon, 24 Jan 2000 18:06:11 +0100 (CET) Received: from grizzly.ps.uni-sb.de (grizzly.ps.uni-sb.de [134.96.186.68]) by cs.uni-sb.de (8.9.3/1999031900) with ESMTP id SAA10911 for ; Mon, 24 Jan 2000 18:06:11 +0100 (CET) Received: from ps.uni-sb.de (IDENT:rossberg@grieg.ps.uni-sb.de [134.96.186.139]) by grizzly.ps.uni-sb.de (8.9.1a/8.9.1) with ESMTP id SAA25096; Mon, 24 Jan 2000 18:06:10 +0100 Sender: rossberg@ps.uni-sb.de Message-ID: <388C8682.6E3A8E4@ps.uni-sb.de> Date: Mon, 24 Jan 2000 18:06:10 +0100 From: Andreas Rossberg Organization: =?iso-8859-1?Q?Universit=E4t?= des Saarlandes X-Mailer: Mozilla 4.7 [en] (X11; I; Linux 2.2.12-20 i686) X-Accept-Language: German, de, en MIME-Version: 1.0 To: caml-list@inria.fr Subject: Re: Compiler translation of array indexing References: <200001192250.XAA25766@pauillac.inria.fr> Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit Resent-From: weis@pauillac.inria.fr Resent-Date: Tue, 25 Jan 2000 09:39:59 +0100 Resent-To: caml-redistribution@pauillac.inria.fr Pierre Weis wrote: > > To summerize, I really would like to write > v.(1) instead of Array.get v 1 > s.(1) instead of String.get s 1 > > I would consider as an additional benefit of the new type system, if I > could use: > t.("ok") instead of Hashtbl.find t "ok", > l.("ok") instead of List.assoc "ok" l. Recent work by Mark Jones tries to make this work in Haskell/Hugs by extending multi parameter type classes with dependency annotations: http://www.cse.ogi.edu/~mpj/fds.html Using dependency annotations, one can declare a class like (in some mixed Haskell/Caml pseudo syntax): class Index 'c 'i 'e | 'c -> 'i 'e where .() : 'c -> 'i -> 'e (note that all parameters have kind *) and instances instance Index ('a array) int 'a instance Index string int char instance Index (('a,'b) Hashtbl.t) 'a 'b instance Index (('a*'b) list) 'a 'b These should provide the desired type-driven behaviour for indexing. The dependency annotation ('c -> 'i 'e) in the class ensures that index and element type are determined by the collection type. For example, the compiler could infer the following: [|1;2;3|].(0) : int "hello".(3) : char [(2,39);(4,0)].(2) : int fun i -> "hello".(i) : int -> char Note however, that overloading can quickly become ambiguous, even with dependencies. While let f x = x.(0) would be possible and have qualified type like (Index 'c int 'e) => 'c -> 'e, the only slightly more involved let g x = x.(0).(0) would not be allowed without additional type annotations since (Index 'c1 int 'c2, Index 'c2 int 'e) => 'c1 -> 'e is an ambiguous type. Best regards, - Andreas