Mailing list for all users of the OCaml language and system.
 help / color / mirror / Atom feed
From: "Neuhaeusser, Martin" <martin.neuhaeusser@siemens.com>
To: "caml-list@inria.fr" <caml-list@inria.fr>
Subject: [Caml-list] Exhaustiveness check and GADTs
Date: Thu, 14 Jan 2016 13:24:34 +0000	[thread overview]
Message-ID: <965631B03C670145ABB9F693E51622530D0D8DCF@DENBGAT9EK5MSX.ww902.siemens.net> (raw)

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

Dear all,

when trying to use some GADTs, I stumbled across a strange behavior of OCaml's exhaustiveness check (4.02.3 and 4.03.0+trunk).
The following piece of code triggers Warning 8 in function get_root_id. Although evaluating get_root always yields a root node, the compiler expects a match for LeafNode:

type instance
type abstract

(** triggers Warning 8: *)
type root = abstract * (instance, int) Hashtbl.t * instance
type leaf = abstract * root * instance

(** any node can either be a root, or a leaf *)
type _ node =
  | RootNode : root -> root node
  | LeafNode : leaf -> leaf node

(** @return the root of any node *)
let get_root : type t. t node -> root node =
  fun some_node ->
    match some_node with
    | RootNode _ -> some_node
    | LeafNode (_, root, _) -> RootNode root

(** @return the id (here just 1) of any node's root *)
let get_root_id : type t. t node -> int =
  fun some_node ->
    let root_node = get_root some_node in
    match root_node with
    | RootNode _ -> 1

If the order of the tuple elements in the definition of leaf is changed,
the exhaustiveness check succeeds:

type instance
type abstract

(** does not trigger Warning 8: *)
type root = abstract * (instance, int) Hashtbl.t * instance
type leaf = root * abstract * instance

(** any node can either be a root, or a leaf *)
type _ node =
  | RootNode : root -> root node
  | LeafNode : leaf -> leaf node

(** @return the root of any node *)
let get_root : type t. t node -> root node =
  fun some_node ->
    match some_node with
    | RootNode _ -> some_node
    | LeafNode (root, _, _) -> RootNode root

(** @return the id (here just 1) of any node's root *)
let get_root_id : type t. t node -> int =
  fun some_node ->
    let root_node = get_root some_node in
    match root_node with
    | RootNode _ -> 1

It seems that the failure of the exhaustiveness check is related to the use of a module (here Hashtbl.t).
If (instance, int) Hashtbl.t is replaced by a different type (e.g. int), the exhaustiveness check succeeds
in both code fragments.

I would be very interested, to see the reason for the difference.

Best regards,
Martin


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

             reply	other threads:[~2016-01-14 13:24 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-01-14 13:24 Neuhaeusser, Martin [this message]
2016-01-14 13:34 ` Drup

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=965631B03C670145ABB9F693E51622530D0D8DCF@DENBGAT9EK5MSX.ww902.siemens.net \
    --to=martin.neuhaeusser@siemens.com \
    --cc=caml-list@inria.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