caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: "Grégoire Henry" <gregoire.henry@ocamlpro.com>
To: Nicolas Trangez <nicolas@incubaid.com>
Cc: Caml-list <caml-list@inria.fr>
Subject: Re: [Caml-list] Strange interaction between recursive modules, GADT exhaustiveness checking and type-checking?
Date: Sun, 29 Jun 2014 16:49:29 +0200	[thread overview]
Message-ID: <20140629144929.GB32483@mlqds.hnr.gr> (raw)
In-Reply-To: <1403996132.26873.18.camel@chi.nicolast.be>

Hi,

  I believe the presence of recursive module has nothing to do with
this particular warning. But, in general, the exhaustiveness-check
can't assume that two abstract types are distinct, as they may be
equal in some other typing context. In your code example, there is no
such typing context. But, for instance, imagine the following program:

  module rec W : sig

    type a
    type b

    type (_, _) t =
      | AA : (a, a) t
      | AB : (a, b) t
      | BB : (b, b) t

    type 'a w =
      | W : ('a, 'b) W.t * 'b -> 'a w

    val x : a w

  end = struct

    type a = unit
    type b = unit

    type (_, _) t =
      | AA : (a, a) t
      | AB : (a, b) t
      | BB : (b, b) t

    type 'a w =
      | W : ('a, 'b) W.t * 'b -> 'a w

    let x : a w = W (BB, ())

  end

  open W

  type s =
    | A of a
    | B of b

  let f : a w -> s = function
    | W (AA, a) -> A a
    | W (AB, b) -> B b
    (* Warning *)

  let _ = f x (* Match_failure *)

In general, it is rather difficult to decide if there exists a typing
context where two abstract types may be considered as equal. And even
more complex with separate compilation.

As a consequence, while testing the exhaustiveness of a pattern-matching,
the type-checker has to assume that 'W.a' and 'W.b' might be equal.

  let f3 : a w -> s = function
    | W (AA, a) -> A a
    | W (AB, b) -> B b
    | W (BB, b) -> B b

In particular, while type-checking the third branch of [f3], the
type-checker assumes that 'W.a = W.b'. This is safe, as the existence
of a value 'W (BB, b)' of type 'a w' can be seen as a proof of this
equality: in the typing context where the value was built, the two
types have to be equals.

Your first example is a particular case where we could assume that
'W.a' and 'W.b' are distinct. They are 'fully abstract types', they
have no inhabitants. They were introduced in current typing context
(and not imported from the signature of another module) and we known
they are distinct.

HTH,
Grégoire

  reply	other threads:[~2014-06-29 14:49 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-06-28 22:55 Nicolas Trangez
2014-06-29 14:49 ` Grégoire Henry [this message]
2014-06-30 11:07   ` Ben Millwood

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=20140629144929.GB32483@mlqds.hnr.gr \
    --to=gregoire.henry@ocamlpro.com \
    --cc=caml-list@inria.fr \
    --cc=nicolas@incubaid.com \
    /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).