caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* conjunctive type in polymorphic variants
@ 2008-10-09  4:15 Conglun Yao
  2008-10-09  7:22 ` [Caml-list] " Nicolas Pouillard
  2008-10-09  8:43 ` Olivier Andrieu
  0 siblings, 2 replies; 4+ messages in thread
From: Conglun Yao @ 2008-10-09  4:15 UTC (permalink / raw)
  To: caml-list

Hi all,

I have just met a strange problem (it might have already been
answered, but I can't find the it) while using camlp4 to generate a
polymorphic type like:

type t = [ `A of int * int | `B of string ]

error msg "The present constructor A has a conjunctive type" is thrown
by the compiler.

I followed the ocaml source code, found in ocaml-3.10.2/typing/typetexp.ml
Line 290, it does the following check in add_field function:
      if List.length stl > 1 || c && stl <> [] then
                  raise(Error(styp.ptyp_loc, Present_has_conjunction l));

Sorry, I can't fully understand the source code, but it seems we can
only define a polymorphic variant with only one additional type
declaration, like
               `A of int   or `A of (int * int)
 rather than `A of int * int

It looks wired, as we can directly define
type t = [ `A of int * int | `B of string ]  in toploop or a *.ml file.

Or I have some mis-understanding in this part.

Thanks for any help.

Best regards,
Conglun


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

* Re: [Caml-list] conjunctive type in polymorphic variants
  2008-10-09  4:15 conjunctive type in polymorphic variants Conglun Yao
@ 2008-10-09  7:22 ` Nicolas Pouillard
  2008-10-09  8:43 ` Olivier Andrieu
  1 sibling, 0 replies; 4+ messages in thread
From: Nicolas Pouillard @ 2008-10-09  7:22 UTC (permalink / raw)
  To: Conglun Yao; +Cc: caml-list

[-- Attachment #1: Type: text/plain, Size: 1349 bytes --]

Excerpts from Conglun Yao's message of Thu Oct 09 06:15:16 +0200 2008:
> Hi all,
> 
> I have just met a strange problem (it might have already been
> answered, but I can't find the it) while using camlp4 to generate a
> polymorphic type like:
> 
> type t = [ `A of int * int | `B of string ]
> 
> error msg "The present constructor A has a conjunctive type" is thrown
> by the compiler.
> 
> I followed the ocaml source code, found in ocaml-3.10.2/typing/typetexp.ml
> Line 290, it does the following check in add_field function:
>       if List.length stl > 1 || c && stl <> [] then
>                   raise(Error(styp.ptyp_loc, Present_has_conjunction l));
> 
> Sorry, I can't fully understand the source code, but it seems we can
> only define a polymorphic variant with only one additional type
> declaration, like
>                `A of int   or `A of (int * int)
>  rather than `A of int * int

Right.

> It looks wired, as we can directly define
> type t = [ `A of int * int | `B of string ]  in toploop or a *.ml file.

I think there is some syntactic thing to make it equal to `A of (int * int) in
this case since `A of int * int does not make sense.

> Or I have some mis-understanding in this part.

In your camlp4 code you have to add tuple nodes to wrap star nodes:
  <:ctyp< $tup:t$ >>

Best regards,

-- 
Nicolas Pouillard aka Ertai

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 240 bytes --]

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

* Re: [Caml-list] conjunctive type in polymorphic variants
  2008-10-09  4:15 conjunctive type in polymorphic variants Conglun Yao
  2008-10-09  7:22 ` [Caml-list] " Nicolas Pouillard
@ 2008-10-09  8:43 ` Olivier Andrieu
  2008-10-09 10:09   ` Conglun Yao
  1 sibling, 1 reply; 4+ messages in thread
From: Olivier Andrieu @ 2008-10-09  8:43 UTC (permalink / raw)
  To: Conglun Yao; +Cc: caml-list

Hi,

On Thu, Oct 9, 2008 at 06:15, Conglun Yao <yaoconglun@gmail.com> wrote:
> Sorry, I can't fully understand the source code, but it seems we can
> only define a polymorphic variant with only one additional type
> declaration, like
>               `A of int   or `A of (int * int)
>  rather than `A of int * int

That's correct.

> It looks wired, as we can directly define
> type t = [ `A of int * int | `B of string ]  in toploop or a *.ml file.

yes, that's because ocaml handle the "of int * int" a bit differently
in regular and
polymorphic variant declarations:
  - in the regular variant the * is a separator between constructor
arguments (thus two arguments)
  - polymorphic variants only have one argument, so int * int is
treated as a whole type expression and * is the "tupling" operator

So yes, this looks wired in the ocaml parser.

-- 
  Olivier


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

* Re: [Caml-list] conjunctive type in polymorphic variants
  2008-10-09  8:43 ` Olivier Andrieu
@ 2008-10-09 10:09   ` Conglun Yao
  0 siblings, 0 replies; 4+ messages in thread
From: Conglun Yao @ 2008-10-09 10:09 UTC (permalink / raw)
  To: caml-list

Nicolas and Olivier,

Thanks for your quick reply, it makes sense!

Conglun

On Thu, Oct 9, 2008 at 9:43 AM, Olivier Andrieu <oandrieu@nerim.net> wrote:
> Hi,
>
> On Thu, Oct 9, 2008 at 06:15, Conglun Yao <yaoconglun@gmail.com> wrote:
>> Sorry, I can't fully understand the source code, but it seems we can
>> only define a polymorphic variant with only one additional type
>> declaration, like
>>               `A of int   or `A of (int * int)
>>  rather than `A of int * int
>
> That's correct.
>
>> It looks wired, as we can directly define
>> type t = [ `A of int * int | `B of string ]  in toploop or a *.ml file.
>
> yes, that's because ocaml handle the "of int * int" a bit differently
> in regular and
> polymorphic variant declarations:
>  - in the regular variant the * is a separator between constructor
> arguments (thus two arguments)
>  - polymorphic variants only have one argument, so int * int is
> treated as a whole type expression and * is the "tupling" operator
>
> So yes, this looks wired in the ocaml parser.
>
> --
>  Olivier
>


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

end of thread, other threads:[~2008-10-09 10:09 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2008-10-09  4:15 conjunctive type in polymorphic variants Conglun Yao
2008-10-09  7:22 ` [Caml-list] " Nicolas Pouillard
2008-10-09  8:43 ` Olivier Andrieu
2008-10-09 10:09   ` Conglun Yao

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