caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: "David Allsopp" <dra-news@metastack.com>
To: "OCaml List" <caml-list@inria.fr>
Subject: [Caml-list] Mixing two GADTs
Date: Mon, 1 Jul 2013 08:39:24 +0100	[thread overview]
Message-ID: <001001ce762e$1bedde30$53c99a90$@metastack.com> (raw)

Suppose I have the following two GADTs:

type _ foo = A : int foo
           | B : string foo
           | C : bool foo

type _ bar = X : int bar
           | Y : float bar
           | Z : string bar

In my actual use case, these two GADTs cannot be altered. Suppose I then
define the following two functions:

let g : type s . s bar -> s = function
  X -> 42
| Y -> 42.0
| Z -> "42"

let get1 : type s . s foo -> s = function
  A -> int_of_string (g Z)
| B -> string_of_float (g Y)
| C -> (=) 1 (g X)

So far, so good. Now what I'd like to try to do is alter get1 to be of the
form:

let get2 : type s . s foo -> s = fun attr ->
  let (f, retr) =
    match attr with
      A -> (int_of_string, Z)
    | B -> (string_of_float, Y)
    | C -> ((=) 1, X)
  in
    f (g retr)

Obviously, I need type annotations for f and retr. I'd tried:

let get2 : type s . s foo -> s = fun attr ->
  let x : type t . (t -> s) * t bar =
    match attr with
      A -> (int_of_string, Z)
    | B -> (string_of_float, Y)
    | C -> ((=) 1, X)
  in
    let (f, retr) = x
    in
      f (g retr)

But I get an error with int_of_string being of type string -> int instead of
type t -> s.

(Incidentally, the notation I expected to be able to use of [(let ((f, retr)
: type t . (t -> s) * t bar)] resulted in a syntax error although looking at
7.13 in the manual, I think that's probably correct?)

Is there a type annotation which will allow this to work? Any help (or a
statement that this happens to be impossible!) much appreciated...


David


             reply	other threads:[~2013-07-01  7:38 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-07-01  7:39 David Allsopp [this message]
2013-07-01  7:50 ` Alain Frisch

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='001001ce762e$1bedde30$53c99a90$@metastack.com' \
    --to=dra-news@metastack.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;
as well as URLs for NNTP newsgroup(s).