Mailing list for all users of the OCaml language and system.
 help / color / mirror / Atom feed
From: Arseniy Alekseyev <aalekseyev@janestreet.com>
To: Ben Millwood <bmillwood@janestreet.com>
Cc: Joris Giovannangeli <joris@giovannangeli.fr>,
	caml users <caml-list@inria.fr>
Subject: Re: [Caml-list] Matching exhausitvity with GADT and modules
Date: Wed, 27 May 2015 16:43:38 +0100	[thread overview]
Message-ID: <CALDciAniDGSQp=zN8+4o_SwWaO7pFPLOkwS8ne6Ywjfu=v5S1A@mail.gmail.com> (raw)
In-Reply-To: <CA+MHO52jpTU25fvqLfLonx5doGWfF7pULof+LN-6VQU7NxDU7w@mail.gmail.com>

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

Interestingly, giving names to constructors is not sufficient. You must
give *distinct* names. In particular, the following produces a warning for
[g], but not for [f]:

module A= struct

  type foo = T
  type bar = T

  type 'a gadt =
      Foo : int -> foo gadt
    | Bar : int -> bar gadt

  let f : foo gadt -> int = function
     | Foo i -> i

end

module B = struct
  open A

  let g : foo gadt -> int = function
     | Foo i -> i
end

Joris, I think you can still kind-of use this idea for abstract data types,
at the cost of adding a wrapper type. In particular, this works:

module A= struct

  type foo
  type bar
  type foo_marker = Foo of foo
  type bar_marker = Bar of bar

  type 'a gadt =
      Foo : int -> foo_marker gadt
    | Bar : int -> bar_marker gadt

  let f : foo_marker gadt -> int = function
     | Foo i -> i

end

module B = struct
  open A

  let g : foo_marker gadt -> int = function
     | Foo i -> i
end



On Wed, May 27, 2015 at 4:14 PM, Ben Millwood <bmillwood@janestreet.com>
wrote:

> This is a common problem that has annoyed me as well. The issue is that B
> sees foo and bar as abstract types, so cannot be sure they are different,
> so cannot be sure that a value of type foo gadt can't be constructed with
> Bar. If you add explicit constructors for both currently-empty types, then
> their inequality will be exposed and your pattern match should work.
>
> On 27 May 2015 at 16:04, Joris Giovannangeli <joris@giovannangeli.fr>
> wrote:
>
>> Hi,
>>
>> The following snippet is compiling without warning :
>>
>> module A = struct
>>
>>   type foo
>>   type bar
>>
>>   type 'a gadt =
>>       Foo : int -> foo gadt
>>     | Bar : int -> bar gadt
>>
>>   let f = function
>>      | Foo i -> i
>> end
>>
>> But if I split the code into two modules :
>>
>> module A = struct
>>
>>   type foo
>>   type bar
>>
>>   type 'a gadt =
>>       Foo : int -> foo gadt
>>     | Bar : int -> bar gadt
>>
>> end
>>
>> module B = struct
>>   include A
>>
>>   let f : foo gadt -> int = function
>>      | Foo i -> i
>> end
>>
>> I get the following warning :
>>
>> Warning 8: this pattern-matching is not exhaustive.
>> Here is an example of a value that is not matched:
>> Bar _
>>
>> How can i work around this issue ? As far as i can tell, it is not
>> possible for Bar to be matched by the function f.
>>
>> Best regards,
>> joris
>>
>>
>> --
>> Caml-list mailing list.  Subscription management and archives:
>> https://sympa.inria.fr/sympa/arc/caml-list
>> Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
>> Bug reports: http://caml.inria.fr/bin/caml-bugs
>
>
>

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

      parent reply	other threads:[~2015-05-27 15:43 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-05-27 15:04 Joris Giovannangeli
2015-05-27 15:14 ` Ben Millwood
2015-05-27 15:22   ` Joris Giovannangeli
2015-05-27 15:36     ` Yannis Juglaret
2015-05-27 20:22       ` Alain Frisch
2015-05-27 15:43   ` Arseniy Alekseyev [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='CALDciAniDGSQp=zN8+4o_SwWaO7pFPLOkwS8ne6Ywjfu=v5S1A@mail.gmail.com' \
    --to=aalekseyev@janestreet.com \
    --cc=bmillwood@janestreet.com \
    --cc=caml-list@inria.fr \
    --cc=joris@giovannangeli.fr \
    /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