caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Leo P White <lpw25@cam.ac.uk>
To: Romain Bardou <romain.bardou@inria.fr>
Cc: caml-list@inria.fr
Subject: Re: [Caml-list] Constrained existential types
Date: 23 Oct 2012 13:31:42 +0100	[thread overview]
Message-ID: <Prayer.1.3.5.1210231331420.27886@hermes-1.csi.cam.ac.uk> (raw)
In-Reply-To: <508677C4.5040802@inria.fr>

It is a bit convoluted, but you can achieve this by using a GADT:

type 'a t constraint 'a = [< `A | `B ];;

module type SIG =
sig
   type a = private [< `A | `B ]
   val x: a t
end;;

type 'a aux = Aux: 'a t -> ([< `A | `B] as 'a) aux;;

let create y =
  let helper: type u. u aux -> unit =
    fun (Aux y) -> 
        let module M: SIG =
        struct
          type a = u
          let x = y
        end
        in
        ()
  in
    helper (Aux y)
;;
 
Regards,

Leo


On Oct 23 2012, Romain Bardou wrote:

>(This is the 4th time I send this e-mail because it does not seem to 
>work. I'm trying with another SMTP server.)
>
>Hello list,
>
>I'm trying to use first-class modules to have existential types. But my 
>existential type must be constrained. I have the following code:
>
>type 'a t constraint 'a = [< `A | `B ]
>
>module type SIG =
>sig
>   type a = private [< `A | `B ]
>   val x: a t
>end
>
>let create (type u) (y: u t) =
>   let module M: SIG =
>     struct
>       type a = u
>       let x = y
>     end
>   in
>   ()
>
>It does not compile, because of the following error:
>
>Error: This type u should be an instance of type [< `A | `B ]
>
>In the manual I did not see any way to constrain type u. If I write 
>something like this instead:
>
>let create (y: 'a t) =
>   let module M: SIG =
>     struct
>       type a = 'a
>       let x = y
>     end
>   in
>   ()
>
>Then the definition "type a = 'a" is not correct, because 'a is not bound.
>
>Is there any way to have constrained existential types?
>
>Thanks,
>
>


  reply	other threads:[~2012-10-23 12:31 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-10-23 10:56 Romain Bardou
2012-10-23 12:31 ` Leo P White [this message]
2012-10-23 12:35   ` Romain Bardou
2012-10-23 13:54   ` Jacques Garrigue

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=Prayer.1.3.5.1210231331420.27886@hermes-1.csi.cam.ac.uk \
    --to=lpw25@cam.ac.uk \
    --cc=caml-list@inria.fr \
    --cc=romain.bardou@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).