caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Patrick M Doane <patrick@watson.org>
To: Jacques Garrigue <garrigue@kurims.kyoto-u.ac.jp>
Cc: caml-list@inria.fr
Subject: Re: [Caml-list] Polymorphic variant abbreviations
Date: Wed, 19 Sep 2001 12:58:00 -0400 (EDT)	[thread overview]
Message-ID: <Pine.BSF.3.96.1010919123600.95614B-100000@fledge.watson.org> (raw)
In-Reply-To: <20010919214524Z.garrigue@kurims.kyoto-u.ac.jp>

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 = <fun>
# let f2 (#t as x) = x;;
val f2 : #t -> [> `A] = <fun>
# let f3 ~x:(#t as x) = x;;
val f3 : x:#t -> [> `A] = <fun>
# 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


      reply	other threads:[~2001-09-19 16:59 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2001-09-18 22:48 Patrick M Doane
2001-09-19  0:05 ` Patrick M Doane
2001-09-19  8:37   ` Laurent Vibert
2001-09-19  8:56   ` Remi VANICAT
2001-09-19  8:01 ` Laurent Vibert
2001-09-19 16:35   ` Patrick M Doane
2001-09-19 12:45 ` Jacques Garrigue
2001-09-19 16:58   ` Patrick M Doane [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=Pine.BSF.3.96.1010919123600.95614B-100000@fledge.watson.org \
    --to=patrick@watson.org \
    --cc=caml-list@inria.fr \
    --cc=garrigue@kurims.kyoto-u.ac.jp \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).