caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* records with polymorphic variants?
@ 2007-03-04  6:03 Eliot Handelman
  2007-03-04 12:04 ` [Caml-list] " Lukasz Stafiniak
                   ` (2 more replies)
  0 siblings, 3 replies; 11+ messages in thread
From: Eliot Handelman @ 2007-03-04  6:03 UTC (permalink / raw)
  To: caml

Hi,

I'm trying to figure out how to define a record with a field whose type 
is an extensible polymorphic variant.

I can  this:

type f = [ `A of int ]

type r = { x : f }  

but then I can't do this:

{ x = `B "test" }

I'm not even sure if what I'm asking for is possible.

Guessing at the syntax I tried

type  r = {
    f :  [> #foo]
  }

which results in:

Warning D: this syntax is deprecated.
      f :  [> #foo]
              ^^^^
Characters 24-28:
      f :  [> #foo]
              ^^^^
The type [< foo ] is not a polymorphic variant type

I hope it is clear what I'm trying to do. Thanks for your help.

-- eliot



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

* Re: [Caml-list] records with polymorphic variants?
  2007-03-04  6:03 records with polymorphic variants? Eliot Handelman
@ 2007-03-04 12:04 ` Lukasz Stafiniak
  2007-03-05  1:24   ` Jacques Garrigue
  2007-03-04 12:08 ` Dmitri Boulytchev
  2007-03-04 14:22 ` Zheng Li
  2 siblings, 1 reply; 11+ messages in thread
From: Lukasz Stafiniak @ 2007-03-04 12:04 UTC (permalink / raw)
  To: eliot; +Cc: caml

This question should go to the "OCaml Beginners"
<ocaml_beginners@yahoogroups.com>.

On 3/4/07, Eliot Handelman <eliot@generation.net> wrote:
> Hi,
>
> I'm trying to figure out how to define a record with a field whose type
> is an extensible polymorphic variant.
>
> I can  this:
>
> type f = [ `A of int ]
>
> type r = { x : f }
>
> but then I can't do this:
>
> { x = `B "test" }

This is crearly a type mismatch: you specified r.x to allow only `A of int.
>
> I'm not even sure if what I'm asking for is possible.
>
I don't think so.

> Guessing at the syntax I tried
>
> type  r = {
>     f :  [> #foo]
>   }
>
> which results in:
>
> Warning D: this syntax is deprecated.
>       f :  [> #foo]
>               ^^^^
> Characters 24-28:
>       f :  [> #foo]
>               ^^^^
> The type [< foo ] is not a polymorphic variant type
>
Try the syntax:

# type r = {x : [> foo]};;
Characters 14-21:
  type r = {x : [> foo]};;
                ^^^^^^^
Unbound type parameter [..]

The open variant type introduces a type parameter, which is not bound
in definition of type r. I don't think these unnamed parameters can be
bound, so that you could write:

 "type 'a r = {x : foo SUM 'a}".

> I hope it is clear what I'm trying to do. Thanks for your help.
>
No, it is not clear. You haven't specified the context.


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

* Re: [Caml-list] records with polymorphic variants?
  2007-03-04  6:03 records with polymorphic variants? Eliot Handelman
  2007-03-04 12:04 ` [Caml-list] " Lukasz Stafiniak
@ 2007-03-04 12:08 ` Dmitri Boulytchev
  2007-03-04 14:22 ` Zheng Li
  2 siblings, 0 replies; 11+ messages in thread
From: Dmitri Boulytchev @ 2007-03-04 12:08 UTC (permalink / raw)
  To: eliot; +Cc: caml

    Making type r polymorphic works fine:

    type 'a r = {x : 'a}

    let u = {x = `A 3}
    let v = {x = `B "test"}

    Best regards,
    Dmitry Boulytchev,
    St.Petersburg State University.

> Hi,
>
> I'm trying to figure out how to define a record with a field whose 
> type is an extensible polymorphic variant.
>
> I can  this:
>
> type f = [ `A of int ]
>
> type r = { x : f } 
> but then I can't do this:
>
> { x = `B "test" }
>
> I'm not even sure if what I'm asking for is possible.
>
> Guessing at the syntax I tried
>
> type  r = {
>    f :  [> #foo]
>  }
>
> which results in:
>
> Warning D: this syntax is deprecated.
>      f :  [> #foo]
>              ^^^^
> Characters 24-28:
>      f :  [> #foo]
>              ^^^^
> The type [< foo ] is not a polymorphic variant type
>
> I hope it is clear what I'm trying to do. Thanks for your help.
>
> -- eliot
>
>
> _______________________________________________
> Caml-list mailing list. Subscription management:
> http://yquem.inria.fr/cgi-bin/mailman/listinfo/caml-list
> Archives: http://caml.inria.fr
> Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
> Bug reports: http://caml.inria.fr/bin/caml-bugs
>
>


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

* Re: records with polymorphic variants?
  2007-03-04  6:03 records with polymorphic variants? Eliot Handelman
  2007-03-04 12:04 ` [Caml-list] " Lukasz Stafiniak
  2007-03-04 12:08 ` Dmitri Boulytchev
@ 2007-03-04 14:22 ` Zheng Li
  2007-03-04 16:24   ` [Caml-list] " Lukasz Stafiniak
  2 siblings, 1 reply; 11+ messages in thread
From: Zheng Li @ 2007-03-04 14:22 UTC (permalink / raw)
  To: caml-list


Hi,

Eliot Handelman <eliot@generation.net> writes:
> type f = [ `A of int ]
>
> type r = { x : f }  
>
> but then I can't do this:
>
> { x = `B "test" }
Here you want type f polymorphic, however as the type of a record field, its
polymorphism has to be reflected (bound) as type parameter in the declaration
of r.

I guess you want the follows

# type 'a r = { x : 'a } constraint 'a = [> f]
type 'a r = { x : 'a; } constraint 'a = [> f ]
# {x = "test"}
Characters 6-12:
  { x = "test"};;
        ^^^^^^
This expression has type string but is here used with type [> f ]
# {x = `B "test"}
- : [> `A of int | `B of string ] r = {x = `B "test"}

HTH, :)

--
Zheng Li
http://www.pps.jussieu.fr/~li/



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

* Re: [Caml-list] Re: records with polymorphic variants?
  2007-03-04 14:22 ` Zheng Li
@ 2007-03-04 16:24   ` Lukasz Stafiniak
  2007-03-04 16:50     ` Eliot Handelman
  2007-03-04 21:02     ` Martin Jambon
  0 siblings, 2 replies; 11+ messages in thread
From: Lukasz Stafiniak @ 2007-03-04 16:24 UTC (permalink / raw)
  To: Zheng Li; +Cc: caml-list

On 3/4/07, Zheng Li <li@pps.jussieu.fr> wrote:
>
> Hi,
>
> Eliot Handelman <eliot@generation.net> writes:
> > type f = [ `A of int ]
> >
> > type r = { x : f }
> >
> > but then I can't do this:
> >
> > { x = `B "test" }
> Here you want type f polymorphic, however as the type of a record field, its
> polymorphism has to be reflected (bound) as type parameter in the declaration
> of r.

Yes, but the type f itself is not polymorphic, it is not [> f].
>
> I guess you want the follows
>
> # type 'a r = { x : 'a } constraint 'a = [> f]
> type 'a r = { x : 'a; } constraint 'a = [> f ]
> # {x = "test"}
> Characters 6-12:
>   { x = "test"};;
>         ^^^^^^
> This expression has type string but is here used with type [> f ]
> # {x = `B "test"}
> - : [> `A of int | `B of string ] r = {x = `B "test"}
>
Nice! I didn't realize this is possible. Thanks.


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

* Re: [Caml-list] Re: records with polymorphic variants?
  2007-03-04 16:24   ` [Caml-list] " Lukasz Stafiniak
@ 2007-03-04 16:50     ` Eliot Handelman
  2007-03-04 21:02     ` Martin Jambon
  1 sibling, 0 replies; 11+ messages in thread
From: Eliot Handelman @ 2007-03-04 16:50 UTC (permalink / raw)
  To: Lukasz Stafiniak; +Cc: Zheng Li, caml-list

Lukasz Stafiniak wrote:
> On 3/4/07, Zheng Li <li@pps.jussieu.fr> wrote:
>
> Yes, but the type f itself is not polymorphic, it is not [> f].
>>
>> I guess you want the follows
>>
>> # type 'a r = { x : 'a } constraint 'a = [> f]
>> type 'a r = { x : 'a; } constraint 'a = [> f ]
>> # {x = "test"}
>> Characters 6-12:
>>   { x = "test"};;
>>         ^^^^^^
>> This expression has type string but is here used with type [> f ]
>> # {x = `B "test"}
>> - : [> `A of int | `B of string ] r = {x = `B "test"}
>>
> Nice! I didn't realize this is possible. Thanks.

Just what I was looking for -- many thanks.

-- eliot


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

* Re: [Caml-list] Re: records with polymorphic variants?
  2007-03-04 16:24   ` [Caml-list] " Lukasz Stafiniak
  2007-03-04 16:50     ` Eliot Handelman
@ 2007-03-04 21:02     ` Martin Jambon
  2007-03-05  5:26       ` Eliot Handelman
  1 sibling, 1 reply; 11+ messages in thread
From: Martin Jambon @ 2007-03-04 21:02 UTC (permalink / raw)
  To: Lukasz Stafiniak; +Cc: Zheng Li, caml-list

On Sun, 4 Mar 2007, Lukasz Stafiniak wrote:

> On 3/4/07, Zheng Li <li@pps.jussieu.fr> wrote:
>> 
>> Hi,
>> 
>> Eliot Handelman <eliot@generation.net> writes:
>> > type f = [ `A of int ]
>> >
>> > type r = { x : f }
>> >
>> > but then I can't do this:
>> >
>> > { x = `B "test" }
>> Here you want type f polymorphic, however as the type of a record field, 
>> its
>> polymorphism has to be reflected (bound) as type parameter in the 
>> declaration
>> of r.
>
> Yes, but the type f itself is not polymorphic, it is not [> f].

You can do this directly if you prefer:

type 'a f = 'a
   constraint 'a = [> `A of int ]

>> I guess you want the follows
>> 
>> # type 'a r = { x : 'a } constraint 'a = [> f]
>> type 'a r = { x : 'a; } constraint 'a = [> f ]
>> # {x = "test"}
>> Characters 6-12:
>>   { x = "test"};;
>>         ^^^^^^
>> This expression has type string but is here used with type [> f ]
>> # {x = `B "test"}
>> - : [> `A of int | `B of string ] r = {x = `B "test"}
>> 
> Nice! I didn't realize this is possible. Thanks.
>
> _______________________________________________
> Caml-list mailing list. Subscription management:
> http://yquem.inria.fr/cgi-bin/mailman/listinfo/caml-list
> Archives: http://caml.inria.fr
> Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
> Bug reports: http://caml.inria.fr/bin/caml-bugs
>

--
Martin Jambon
http://martin.jambon.free.fr


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

* Re: [Caml-list] records with polymorphic variants?
  2007-03-04 12:04 ` [Caml-list] " Lukasz Stafiniak
@ 2007-03-05  1:24   ` Jacques Garrigue
  0 siblings, 0 replies; 11+ messages in thread
From: Jacques Garrigue @ 2007-03-05  1:24 UTC (permalink / raw)
  To: lukstafi; +Cc: eliot, caml-list

From: "Lukasz Stafiniak" <lukstafi@gmail.com>

> Try the syntax:
> 
> # type r = {x : [> foo]};;
> Characters 14-21:
>   type r = {x : [> foo]};;
>                 ^^^^^^^
> Unbound type parameter [..]
> 
> The open variant type introduces a type parameter, which is not bound
> in definition of type r. I don't think these unnamed parameters can be
> bound, so that you could write:

The correct syntax in this case is

  type r = {x : 'a. [> foo] as 'a}

Unfortunately there seems to be a bug in 3.09, so that it doesn't
work. This will work again in 3.10.

(This was a stupid bug, as usual. Other cases, like
  type r = {x : 'a. <m : int; ..> as 'a}
do work in 3.09.)

Note that this seems not to be what the original poster wanted
actually, which was just a constrained type.

Jacques Garrigue


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

* Re: [Caml-list] Re: records with polymorphic variants?
  2007-03-04 21:02     ` Martin Jambon
@ 2007-03-05  5:26       ` Eliot Handelman
  2007-03-05  5:47         ` Martin Jambon
  2007-03-05  8:01         ` Jacques GARRIGUE
  0 siblings, 2 replies; 11+ messages in thread
From: Eliot Handelman @ 2007-03-05  5:26 UTC (permalink / raw)
  To: Martin Jambon; +Cc: caml-list

Martin Jambon wrote:
>
> You can do this directly if you prefer:
>
> type 'a f = 'a
>   constraint 'a = [> `A of int ]

This is exactly what I was trying to do in the first place.
Where exactly is the main documentation for this syntax? Not in
JG's papers, unless I've missed something, as is possible.

Many thanks again,.

-- eliot
 


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

* Re: [Caml-list] Re: records with polymorphic variants?
  2007-03-05  5:26       ` Eliot Handelman
@ 2007-03-05  5:47         ` Martin Jambon
  2007-03-05  8:01         ` Jacques GARRIGUE
  1 sibling, 0 replies; 11+ messages in thread
From: Martin Jambon @ 2007-03-05  5:47 UTC (permalink / raw)
  To: Eliot Handelman; +Cc: caml-list

On Mon, 5 Mar 2007, Eliot Handelman wrote:

> Martin Jambon wrote:
>> 
>> You can do this directly if you prefer:
>> 
>> type 'a f = 'a
>>   constraint 'a = [> `A of int ]
>
> This is exactly what I was trying to do in the first place.
> Where exactly is the main documentation for this syntax? Not in
> JG's papers, unless I've missed something, as is possible.

Personally, I know it from someone who posted the same answer on caml-list 
sometime around 2004.
:-)


Martin

--
Martin Jambon
http://martin.jambon.free.fr


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

* Re: [Caml-list] Re: records with polymorphic variants?
  2007-03-05  5:26       ` Eliot Handelman
  2007-03-05  5:47         ` Martin Jambon
@ 2007-03-05  8:01         ` Jacques GARRIGUE
  1 sibling, 0 replies; 11+ messages in thread
From: Jacques GARRIGUE @ 2007-03-05  8:01 UTC (permalink / raw)
  To: eliot; +Cc: caml-list

From: Eliot Handelman <eliot@generation.net>
> Martin Jambon wrote:
> >
> > You can do this directly if you prefer:
> >
> > type 'a f = 'a
> >   constraint 'a = [> `A of int ]
> 
> This is exactly what I was trying to do in the first place.
> Where exactly is the main documentation for this syntax? Not in
> JG's papers, unless I've missed something, as is possible.

It's not in my papers, because it is a rather subtle feature of the
type system, and is not directly related to type inference. It should
be in Vouillon&Remy's paper about objects.

It is documented in the manual, at the very end of section 6.8.1.
There are some examples involving objects (using the class syntax) in
section 3.10 of the tutorial.

Jacques Garrigue


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

end of thread, other threads:[~2007-03-05  8:02 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2007-03-04  6:03 records with polymorphic variants? Eliot Handelman
2007-03-04 12:04 ` [Caml-list] " Lukasz Stafiniak
2007-03-05  1:24   ` Jacques Garrigue
2007-03-04 12:08 ` Dmitri Boulytchev
2007-03-04 14:22 ` Zheng Li
2007-03-04 16:24   ` [Caml-list] " Lukasz Stafiniak
2007-03-04 16:50     ` Eliot Handelman
2007-03-04 21:02     ` Martin Jambon
2007-03-05  5:26       ` Eliot Handelman
2007-03-05  5:47         ` Martin Jambon
2007-03-05  8:01         ` Jacques GARRIGUE

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