caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Leo White <lpw25@cam.ac.uk>
To: Lukasz Stafiniak <lukstafi@gmail.com>
Cc: Goswin von Brederlow <goswin-v-b@web.de>,  Caml <caml-list@inria.fr>
Subject: Re: [Caml-list] GADTs and associative container
Date: Wed, 10 Jul 2013 10:52:34 +0100	[thread overview]
Message-ID: <87ehb6lp25.fsf@kingston.cl.cam.ac.uk> (raw)
In-Reply-To: <CAJMfKEVVZem2rrSZ0GUz7LvwN8_nGWW9+zMzcB-zwesPrXqEjQ@mail.gmail.com> (Lukasz Stafiniak's message of "Tue, 9 Jul 2013 22:52:20 +0200")

Lukasz Stafiniak <lukstafi@gmail.com> writes:

> On Tue, Jul 9, 2013 at 10:43 PM, Goswin von Brederlow <goswin-v-b@web.de> wrote:
>
>     Hi,
>    
>     I'm wondering if one can have an ascociative container, like a Hashtbl.t
>     with dependent types (GADTs as the key, value depending on the key).
>     Something like this:
>    
>     module H = struct
>       type ('a, 'b) t = ('a, 'b) Hashtbl.t
>       let create : type a b . int -> (a b, a) t =
>                      fun x -> Hashtbl.create x
>       let add : type a b . (a b, a) t -> a b -> a -> unit =
>                   fun h k v -> Hashtbl.add h k v
>       let find : type a b . (a b, a) t -> a b -> a =
>                   fun h k -> Hashtbl.find h k
>     end
>
>  
>
>     BUT:
>    
>         let create : type a b . int -> (a b, a) t =
>                                         ^^^
>     Error: Unbound type constructor b
>
>     Is there some special syntax I'm missing or is it simply impossible to
>     declare such a container in the abstract?
>
> I think you need higher kinded types, not GADTs. Haskell has them, for example you can write code that only depends on
> the type class of "b" (which is parameterized by "a"), and "b" has signature "* -> *" or something like that.

That type is indeed higher-kinded. OCaml does support higher-kinded
types but not in the core type system: you need to use functors.

For example,

>       let create : type a b . int -> (a b, a) t =
>                      fun x -> Hashtbl.create x

could be written as:

  module Create (B: sig type 'a t end) = struct
    let f x : ('a B.t, 'a) Hashtbl.t = Hashtbl.create x
  end;;

Note that this is not actually the correct type for writing an
associative container. See Jeremy's post for details.

Regards,

Leo

  reply	other threads:[~2013-07-10  9:52 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-07-09 20:43 Goswin von Brederlow
2013-07-09 20:52 ` Lukasz Stafiniak
2013-07-10  9:52   ` Leo White [this message]
2013-07-10 10:16   ` Alain Frisch
2013-07-10  2:22 ` Jeremy Yallop
2013-07-11 14:17   ` Goswin von Brederlow
2013-07-11 14:28     ` Jeremy Yallop

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=87ehb6lp25.fsf@kingston.cl.cam.ac.uk \
    --to=lpw25@cam.ac.uk \
    --cc=caml-list@inria.fr \
    --cc=goswin-v-b@web.de \
    --cc=lukstafi@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).