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 SAA21187; Wed, 19 Sep 2001 18:59:43 +0200 (MET DST) X-Authentication-Warning: pauillac.inria.fr: majordomo set sender to owner-caml-list@pauillac.inria.fr using -f 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 SAA21318 for ; Wed, 19 Sep 2001 18:58:21 +0200 (MET DST) Received: from fledge.watson.org (fledge.watson.org [204.156.12.50]) by nez-perce.inria.fr (8.11.1/8.10.0) with ESMTP id f8JGwK101654 for ; Wed, 19 Sep 2001 18:58:21 +0200 (MET DST) Received: from localhost (patrick@localhost) by fledge.watson.org (8.11.6/8.11.5) with SMTP id f8JGw1896632; Wed, 19 Sep 2001 12:58:05 -0400 (EDT) (envelope-from patrick@watson.org) Date: Wed, 19 Sep 2001 12:58:00 -0400 (EDT) From: Patrick M Doane To: Jacques Garrigue cc: caml-list@inria.fr Subject: Re: [Caml-list] Polymorphic variant abbreviations In-Reply-To: <20010919214524Z.garrigue@kurims.kyoto-u.ac.jp> Message-ID: MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk On Wed, 19 Sep 2001, Jacques Garrigue wrote: > > I have some code that would like to use polymorphic type abbreviations > > along with labels. It seems to be not behaving quite like I expected. > > What could I do to make this shorter: > > > > type a = [ `A ] > > > > let f ~a:(#a as a) = ... > > Nothing. But this doesn't strike me as a very frequent idiom. I'll motivate how I came to using this feature. I am working with a protocol that sends messages containing data as typed s-expressions. A natural representation with standard variants would be: type data = | List of data list | Int of int | String of string (* there are about a dozen or so types in reality *) There is a limited set of messages that can be sent and they have specific type constraints on their data. My hope was to express many of these constraints using polymorphic variants and then check the rest at runtime. Without polymorphic variants, I'm forced to do all checks at runtime. So, I start with a function like this: let msg1 arg1 arg2 = { name = "msg1"; data = `List [arg1; arg2] } Now, the protocol defines type constraints for the arguments to all messages. So, I create some type abbreviation for each kind of argument that is used: type arg1 = [ `String of string ] type arg2 = [ `Int ] So now I would like to require that the arguments to msg1 have the correct type: (* this doesn't work - types are too constrained *) let msg1 (arg1 : #arg1) (arg2 : #arg2) = ... (* need to use the 'as' form like this to get better typing *) let msg1 (#arg1 as arg1) (#arg2 as arg2) = ... And finally, I want the message arguments to be labeled by their name. Hence: let msg1 ~arg1:(#arg1 as arg1) ~arg2:(#arg2 as arg2) = ... Maybe I'm trying to push the system too far. I found this methodology worked pretty well until I came to lists. I have not been able to restrict the elements of a list to be a specific type. > > let f ~a:#a = ... > > > > but then it says that 'a' is unbound. > > Sure, nothing binds a here: ~a: is a label, and #a an abbreviation for > `A. Well, '~a' implies the binding '~a:a'. I don't see why it is unreasonable to assume a similar case applies with patterns. > > I also find that the error messages are not as useful as they could be > > with the type abbreviations. E.g: > > > > This expression has type ... but is here used with #a as 'a = 'a > > Could you give me the code leading to this error message? > This is clearly a bug. This seems to happen in all cases I have tried: # type t = [`A];; type t = [ `A] # let f1 (x:#t) = x;; val f1 : (#t as 'a) -> 'a = # let f2 (#t as x) = x;; val f2 : #t -> [> `A] = # let f3 ~x:(#t as x) = x;; val f3 : x:#t -> [> `A] = # f1 `B;; Characters 3-5: This expression has type [> `B] but is here used with type #t as 'a = 'a # f2 `B;; Characters 3-5: This expression has type [> `B] but is here used with type #t as 'a = 'a # f3 `B;; Characters 3-5: This expression has type [> `B] but is here used with type #t as 'a = 'a # Thanks very much for everyone's help! Patrick ------------------- Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/ To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr