caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Leo White <lpw25@cam.ac.uk>
To: Ivan Gotovchits <ivg@ieee.org>
Cc: caml-list@inria.fr
Subject: Re: [Caml-list] GADT: equal functions typechecks differently
Date: Fri, 19 Jul 2013 16:32:33 +0100	[thread overview]
Message-ID: <87mwpitvji.fsf@kingston.cl.cam.ac.uk> (raw)
In-Reply-To: <877ggmx2pc.fsf@golf.niidar.ru> (Ivan Gotovchits's message of "Fri, 19 Jul 2013 14:29:35 +0400")

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

The difference between id and pool is that 'a id is equal to 'b id for
any types 'a and 'b, since they are both equal to int, while 'a pool
only equals 'b pool if 'a equals 'b. For example:

  # let f (x: int id) : float id = x;;
  val f : int id -> float id = <fun>

  # let f (x: int pool) : float pool = x;;
  Characters 35-36:
    let f (x: int pool) : float pool = x;;
                                       ^
  Error: This expression has type int pool
         but an expression was expected of type float pool
         Type int is not compatible with type float 

Interestingly, 'a pool is a subtype of 'b pool for any 'a and 'b, so the
following definition is allowed:

  # let f (x: int pool) : float pool = (x :> float pool);;
  val f : int pool -> float pool = <fun>

This is why:

>   # 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>

is accepted, but 

>   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

is disallowed. We could, however, write this function using subtyping:

  # 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 :> t2 pool)
      | None -> None;;
  val of_tpool : 't1 sys -> tpool -> 't2 pool option = <fun>

Note that these functions are not really using any of the GADT
constraints. They don't need to since any 'a id can be used as a t2 id
and any 'a pool can be subtyped into a t2 pool.

Regards,

Leo

      reply	other threads:[~2013-07-19 15:32 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-07-19 10:29 Ivan Gotovchits
2013-07-19 15:32 ` Leo White [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=87mwpitvji.fsf@kingston.cl.cam.ac.uk \
    --to=lpw25@cam.ac.uk \
    --cc=caml-list@inria.fr \
    --cc=ivg@ieee.org \
    /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).