caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] Polymorphic variants as type parameters
@ 2002-08-30 15:34 Tim Freeman
  2002-08-30 16:27 ` John Prevost
  0 siblings, 1 reply; 3+ messages in thread
From: Tim Freeman @ 2002-08-30 15:34 UTC (permalink / raw)
  To: caml-list

Here's some code that I'd like to be able to compile:

   type eventtype = [`VECTOR | `SCALAR | `UNIT]
   type +'a eventid
   constraint 'a = [> eventtype ]
   type scalar_eventid = [`SCALAR] eventid

Unfortunately it gives me an error:

   Characters 124-133:
        type scalar_eventid = [`SCALAR] eventid
                              ^^^^^^^^^
   This type [ `SCALAR] should be an instance of type
     [> eventtype] = [> `SCALAR | `UNIT | `VECTOR]
   The first variant type does not allow tag(s) `UNIT, `VECTOR

This feels like a bug to me: I said the type variable was covariant,
and the type I'm substituting for the type variable is more specific
than the old one, so I had expected it to be happy with that.

The present workaround I'm using is to leave out the type constraint
on the type argument to eventid:

   type eventtype = [`VECTOR | `SCALAR | `UNIT]
   type +'a eventid
   type scalar_eventid = [`SCALAR] eventid

This doesn't say what I want because it only makes sense for the type
argument to eventid to be some subset of the set {`VECTOR, `SCALAR, `UNIT}.

Another approach to getting the code to compile is to give
scalar_eventid a type parameter.  This compiles but it doesn't say
what I want to say because scalar_eventid shouldn't have a type
parameter; that would force my code to grow useless extra type
variables in lots of places.

   type eventtype = [`VECTOR | `SCALAR | `UNIT]
   type +'a eventid
   constraint 'a = [> eventtype ]
   type 'a scalar_eventid = 'a eventid
   constraint 'a = [> `SCALAR ]

Is there any way to say what I want to say in OCAML?  Is this as a
bug?

-- 
Tim Freeman       
tim@fungible.com
GPG public key fingerprint ECDF 46F8 3B80 BB9E 575D  7180 76DF FE00 34B1 5C78 
-------------------
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


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [Caml-list] Polymorphic variants as type parameters
  2002-08-30 15:34 [Caml-list] Polymorphic variants as type parameters Tim Freeman
@ 2002-08-30 16:27 ` John Prevost
  2002-08-30 17:08   ` Tim Freeman
  0 siblings, 1 reply; 3+ messages in thread
From: John Prevost @ 2002-08-30 16:27 UTC (permalink / raw)
  To: Tim Freeman; +Cc: caml-list

>>>>> "tf" == Tim Freeman <tim@fungible.com> writes:

    tf> Here's some code that I'd like to be able to compile:

           type eventtype = [`VECTOR | `SCALAR | `UNIT]
           type +'a eventid
           constraint 'a = [> eventtype ]
           type scalar_eventid = [`SCALAR] eventid

    tf> Unfortunately it gives me an error:


The problem is that when you say [> `X | `Y | `Z] you describe a type
that contains at *least* `X, `Y, and `Z, and possibly others.  This
type is typical for an argument type to a function.  Think of this as
"greater or equal to the type [`X|`Y|`Z]".

When you want a type that may contain possibly `X or `Y or `Z but
nothing besides those, you should write [< `X | `Y | `Z].  This is
like "less than or equal to the type [`X|`Y|`Z]".

Using this I get:

# type eventtype = [`VECTOR | `SCALAR | `UNIT]
  type +'a eventid constraint 'a = [< eventtype ]
  type scalar_eventid = [`SCALAR] eventid;;
type eventtype = [ `VECTOR | `SCALAR | `UNIT]
type +'a eventid constraint 'a = [< eventtype]
type scalar_eventid = [ `SCALAR] eventid

John.
-------------------
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


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [Caml-list] Polymorphic variants as type parameters
  2002-08-30 16:27 ` John Prevost
@ 2002-08-30 17:08   ` Tim Freeman
  0 siblings, 0 replies; 3+ messages in thread
From: Tim Freeman @ 2002-08-30 17:08 UTC (permalink / raw)
  To: j.prevost; +Cc: caml-list

>The problem is that when you say [> `X | `Y | `Z] you describe a type
>that contains at *least* `X, `Y, and `Z, and possibly others.

Thanks much.  I had figured that since the `X | `Y | `Z was on the
right side of the ">", it was the one that was supposed to be lesser,
like the "2" in 3 > 2.  Tricky syntax.

-- 
Tim Freeman       
tim@fungible.com
GPG public key fingerprint ECDF 46F8 3B80 BB9E 575D  7180 76DF FE00 34B1 5C78 
-------------------
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


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2002-08-30 17:09 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2002-08-30 15:34 [Caml-list] Polymorphic variants as type parameters Tim Freeman
2002-08-30 16:27 ` John Prevost
2002-08-30 17:08   ` Tim Freeman

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).