caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] Explicit Arity with Polymorphic Variants
@ 2015-01-23  6:53 Jordan W
  2015-01-23  8:03 ` Jacques Garrigue
  0 siblings, 1 reply; 11+ messages in thread
From: Jordan W @ 2015-01-23  6:53 UTC (permalink / raw)
  To: caml-list

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

The OCaml compiler allows distinguishing between variants that accept a
single tuple and variant types that accept several parameters. What looks
like a variant type accepting a tuple, is actually the later:

type x = TwoSeparateArguments of int * int
let tuple = (10,10)
let thisWontWork = TwoSeparateArguments tuple;;
>> Error: The constructor TwoSeparateArguments expects 2 argument(s),
                                                           but is applied
here to 1 argument(s)

(* Notice the extra parens around the two ints *)
type x = OneArgumentThatIsATuple of (int * int)
let thisActuallyWorks = OneArgumentThatIsATuple tuple

The extra parens distinguish at type definition time which of the two is
intended.

But OCaml does some automatic massaging of the data that you supply to
constructor values.
let _ = OneArgumentThatIsATuple (4, 5)
let _ = TwoSeparateArguments (4, 5)

No extra parens are required in this case. But OCaml does give you the
ability to annotate patterns and expressions with an "explicit_arity"
attribute which allows syntactic distinction between supplying two separate
parameters vs. one that happens to be a tuple. This is important for other
parser extensions that wish to treat the two distinctly. What OCaml allows
(explicit_arity attribute) works well enough.

The only problem is that there doesn't seem to be a way to utilize the same
explicit_arity attributes with polymorphic variants. Such attributes are
not acknowledged by the type system. Is this intended?

Taking a quick look at typecore.ml, explicit_arity appears to be
acknowledged on standard constructors but not polymorphic variants.
https://github.com/ocaml/ocaml/blob/6e85c2d956c8fd5b45acd70a27586e44bb3a3119/typing/typecore.ml

It seems these should be brought to consistency. I will file a mantis issue
unless anyone believes this is intended.

Thank you in advance.

Jordan

[-- Attachment #2: Type: text/html, Size: 2528 bytes --]

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

end of thread, other threads:[~2015-01-26  4:06 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-01-23  6:53 [Caml-list] Explicit Arity with Polymorphic Variants Jordan W
2015-01-23  8:03 ` Jacques Garrigue
2015-01-23  9:04   ` Jordan W
2015-01-23  9:56     ` David Allsopp
2015-01-24  8:52     ` Gabriel Scherer
2015-01-25  8:02       ` Jordan W
2015-01-25 10:11         ` David Allsopp
2015-01-25 19:57           ` Jordo
2015-01-26  4:05             ` Jacques Garrigue
2015-01-24  3:47   ` Jordan W
2015-01-24  8:24     ` David Allsopp

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