caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Ivan Gotovchits <ivg@ieee.org>
To: caml-list@inria.fr
Subject: [Caml-list] GADT: equal functions typechecks differently
Date: Fri, 19 Jul 2013 14:29:35 +0400	[thread overview]
Message-ID: <877ggmx2pc.fsf@golf.niidar.ru> (raw)


I've found a case when two function, that differs very slightly (almost
identical), causes different reactions of a typechecker. For me, this
looks like a bug, but may be there is something (well of course there
is) that I do not understand:

I have two types, an id and a pool of id. Both are protected with
phantom type. The definitions is very small, so I will provide them:

  module Set =
    Set.Make(struct type t = int let compare = compare end)

  type 'a pool = {
    set : Set.t;
    min : int;
    max : int;
  }
  type 'a id = int

  type our
  type ads
  type ows
  type ext


I define two GADT's and two injections:

  type _ sys =
    | Our : our sys
    | Ows : ows sys
    | Ads : ads sys
    | Ext : ext sys

  type tid   = Id : 'a sys * 'a id -> tid
  type tpool = Pool : 'a sys * 'a pool -> tpool

  let to_tid sys id = Id (sys,id)
  let to_tpool sys pool = Pool (sys,pool)

Using common idiom (it is common, isn't?) I can typesafely compare my
systems.

  type (_,_) eq = Eq : ('a,'a) eq

  let try_sys_equal: type a b. (a sys * b sys) -> (a,b) eq option =
   function
   | Our,Our -> Some Eq
   | Ows,Ows -> Some Eq
   | Ads,Ads -> Some Eq
   | Ext,Ext -> Some Eq
   | _       -> None


Next, I provide two injections:

  # let of_tid: type t1. t1 sys -> tid -> t1 id option =
    fun sys1 (Id (sys2,id)) ->
      match try_sys_equal (sys1,sys2) with
      | Some Eq -> Some id
      | None -> None;;

  val of_tid : 't1 sys -> tid -> 't1 id option = <fun>

  # let of_tpool: type t1. t1 sys -> tpool -> t1 pool option =
    fun sys1 (Pool (sys2,pool)) ->
      match try_sys_equal (sys1,sys2) with
      | Some Eq -> Some pool
      | None -> None;;

  val of_tpool : 't1 sys -> tpool -> 't1 pool option = <fun>

As you can see, they typechecks. But here is the strange behavior - if I
write the first function in a slightly different manner, it typechecks:

  # let of_tid: type t1 t2. t1 sys -> tid -> t2 id option =
    fun sys1 (Id (sys2,id)) ->
      match try_sys_equal (sys1,sys2) with
      | Some Eq -> Some id
      | None -> None;;
  val of_tid : 't1 sys -> tid -> 't2 id option = <fun>

But if I add the same t2 type to the second function it fails:

  let of_tpool: type t1 t2. t1 sys -> tpool -> t2 pool option =
    fun sys1 (Pool (sys2,pool)) ->
      match try_sys_equal (sys1,sys2) with
      | Some Eq -> Some pool
      | None -> None;;
          Characters 158-162:
        | Some Eq -> Some pool
                          ^^^^
  Error: This expression has type ex#7 pool
       but an expression was expected of type t2 pool
       Type ex#7 = t1 is not compatible with type t2 


Thank in advance for any clues
Ivan

             reply	other threads:[~2013-07-19 10:29 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-07-19 10:29 Ivan Gotovchits [this message]
2013-07-19 15:32 ` Leo White

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=877ggmx2pc.fsf@golf.niidar.ru \
    --to=ivg@ieee.org \
    --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;
as well as URLs for NNTP newsgroup(s).