caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Gabriel Scherer <gabriel.scherer@gmail.com>
To: Thomas Braibant <thomas.braibant@gmail.com>
Cc: OCaML Mailing List <caml-list@inria.fr>
Subject: Re: [Caml-list] GADT and local modules
Date: Fri, 23 May 2014 10:24:48 +0200	[thread overview]
Message-ID: <CAPFanBFW6FTME9kx_iLcdD7tg1biSaXYngVehsB0M-2iChB2pg@mail.gmail.com> (raw)
In-Reply-To: <CAHR=VkxembFPVePtEG+atG1ShiKYeo78QHYxbbLN+TXCBSQqJQ@mail.gmail.com>

You will be familiar with the solution from the Coq world.

  match t with
   | Pack l ->
     begin fun (type a') (type b') l ->
       let module C = struct
         type a = a'
         type b = b'
         let foo = l
      end in
      let module R = M(C) in
      R.result
    end l

On Fri, May 23, 2014 at 10:15 AM, Thomas Braibant
<thomas.braibant@gmail.com> wrote:
> Hi list,
>
> I would like to find a way to name (or capture) the variables that are
> existentially quantified in a GADT constructor, to put them inside a
> module and apply a functor on the said module. Attached below is a
> mockup of what I would like to have. (I can live without this coding
> pattern, but I would like to know whether or not it is possible to
> make it work, even if I suspect that the answer is no).
>
> Best,
> Thomas
>
>
> type t = Pack : 'a * 'b list -> t
>
> (* I want to unpack a t, and put its content in a module of signature S *)
> module type S = sig
>   type a
>   type b
>   val foo : (a * b) list
> end
>
> (* I want to apply the functor M on my module to produce a result,
> whose type does not depend on the types in S.  I am using a functor as
> a way to structure the computations that happen inside. I started with
> a version without modules, which requires to write quite a few type
> annotations... *)
> module M (C:S) : sig val result : int end= struct
>
>   include C
>
>  (* Here I want bar to have type (a * b) list. Of course, I could have
> bar s a function that takes two type variables as arguments, and a
> list, and returns a list of the right type. *)
>   let bar = List.rev foo
>   let result = List.length bar
>
> end
>
> (* Here, I do not know of a way to retrieve the type variables
> quantified in the GADT constructor Pack, to feed them in C *)
> let f (t : t) =
>   match t with
>   | Pack l -> let module C = .... in M(C)
>
> (* alternative with modules, which is not satisfying. *)
>
> module type S = sig
>   val t : t
> end
>
> module M (C:S) : sig val result : int end = struct
>
>   include C
>
>   (* Now, I can unpack the t locally in each function, but I cannot
>      open the existential in the whole module, which is painful. *)
>
> end
>
> --
> Caml-list mailing list.  Subscription management and archives:
> https://sympa.inria.fr/sympa/arc/caml-list
> Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
> Bug reports: http://caml.inria.fr/bin/caml-bugs

  reply	other threads:[~2014-05-23  8:25 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-05-23  8:15 Thomas Braibant
2014-05-23  8:24 ` Gabriel Scherer [this message]
2014-05-23  8:28   ` Thomas Braibant

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=CAPFanBFW6FTME9kx_iLcdD7tg1biSaXYngVehsB0M-2iChB2pg@mail.gmail.com \
    --to=gabriel.scherer@gmail.com \
    --cc=caml-list@inria.fr \
    --cc=thomas.braibant@gmail.com \
    /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).