caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Gabriel Scherer <gabriel.scherer@gmail.com>
To: Gabriel Mayr <gabriel.mayr@mail.de>
Cc: caml users <caml-list@inria.fr>
Subject: Re: [Caml-list] unnamed records in match on multiple constructors
Date: Thu, 15 Dec 2016 10:08:34 -0500	[thread overview]
Message-ID: <CAPFanBHjqzo8E_gB3S7pNnkcVEia-aECw9pNX4uW4Qa-a8AyoA@mail.gmail.com> (raw)
In-Reply-To: <20161215141240.01BE740092@smtp03.mail.de>

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

The issue is "what is the type of an inline record"? Internally each inline
record in a constructor declaration has its own type, so in particular two
different records have distinct types. One way to think of it is that

  type t = A of {i:int} | B of {i:int};;

is morally equivalent to

  type t_internal_A = {i : int};;
  type t_internal_B = {i : int};;
  type t = A of t_internal_A | B of t_internal_B;;

And then (function (A e | B e) -> e.i) does not type-check either -- with
the same error message. This comes from the fact that, in OCaml (unlike in
SML), records are nominal, have an identity: declaring another record type
with the same fields creates a new, incompatible type.

So the answer is: this is a natural, expected consequence of how inline
records and how records work in OCaml.

On Thu, Dec 15, 2016 at 9:12 AM, Gabriel Mayr <gabriel.mayr@mail.de> wrote:

>
> Hi,
>
> I'm using utop version 1.19.3 (using OCaml version 4.03.0).
>
> Using an *named* record in a type definition and afterwards match on
> multiple constructors *works:*
> # type a = {i:int};;
> #type t = A of a | B of a;;
> # let x = A {i=10};;
> # match x with
> | A e | B e -> print_int e.i;;
>
>
> Using an *unnamed* record in a type definition and afterwards match on
> multiple constructors *does not work:*
> # type t = A of {i:int} | B of {i:int};;
> # let x = A {i=10};;
> # match x with
> | A e | B e -> print_int e.i;;
>
> "Error: The variable e on the left-hand side of this or-pattern has type
> t.A but on the right-hand side it has type t.B"
>
>
>
> I don't know if this is behaviour of OCaml is desired or it's a fault.
>
> Hope I could help you with this little hint.
>
> Best regards,
>
> Gabriel
>
> ------------------------------
> FreeMail powered by mail.de - *mehr Sicherheit, Seriosität und Komfort*

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

      reply	other threads:[~2016-12-15 15:09 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-12-15 14:12 Gabriel Mayr
2016-12-15 15:08 ` Gabriel Scherer [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=CAPFanBHjqzo8E_gB3S7pNnkcVEia-aECw9pNX4uW4Qa-a8AyoA@mail.gmail.com \
    --to=gabriel.scherer@gmail.com \
    --cc=caml-list@inria.fr \
    --cc=gabriel.mayr@mail.de \
    /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).