caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Phil Eaton <me@eatonphil.com>
To: "Rémy El Sibaïe Besognet" <remy.el-sibaie@lip6.fr>
Cc: "caml-list@inria.fr" <caml-list@inria.fr>
Subject: Re: [Caml-list] Generic usage of Hashtbl.S with first class module
Date: Tue, 16 Jun 2015 09:03:33 -0400	[thread overview]
Message-ID: <CAByiw+p8M9_QZZJiMoVTJ8HJhG_ap1UASPk=_bk6V4uX3Sb_ig@mail.gmail.com> (raw)
In-Reply-To: <CAKAHPK3nxbZot86DkZfj4ShYnLNHJZ1fJSytj7m=kyBZmz8arg@mail.gmail.com>

[-- Attachment #1: Type: text/plain, Size: 1793 bytes --]

Until you get a better response, I can point you in a direction. You want
to look up first-class modules
<https://realworldocaml.org/v1/en/html/first-class-modules.html>. It looks
like you will want something like this (taken from that page):

let to_int m =
    let module M = (val m : X_int) in
    M.x

Except yours will look more like:


let find h ht x =
  let module H = (val h : Hashtbl.S) in
  H.find *ht* x


Sorry about the awful formatting.


On Tue, Jun 16, 2015 at 8:15 AM, Rémy El Sibaïe Besognet <
remy.el-sibaie@lip6.fr> wrote:

> Hi all,
>
> Considering this function
>
> let find ht x =
>   Hashtbl.find ht x
>
>
> I would like to write a more general `find` function which takes the
> module
> as first class module in parameter with the possibility to use it on
> different implentations of Hashtbl.S.
>
> let find (module H : Hashtbl.S) ht x =
>   H.find *ht* x
>
>
> but it failed with this error (on the underlined identifier) :
>
> *Error: This expression has type 'a H.t but an expression was expected of
> type         'a H.t       The type constructor H.t would escape its scope*
>
>
>
> I also tried by specifying the implementation but had the same problem:
>
> let find (type a) (module H : Hashtbl.S with type key = a) (ht : 'a Ht.t) (x : a) =
>
> H.find ht x
>
> Anyone has an idea how to achieve this thing ?
>
> Thanks,
> - Rémy El Sibaïe, LIP6
>
>
>
> PS: at the begining, I was trying on a memoization function which has more
> interest than the previous function :
>
> let memo_rec (module H : Hashtbl.S) h f =
>   let rec g x =
>     try H.find h x with
>     | Not_found ->
>       let y = f g x in
>       H.add h x y; y
>   in g
>
>


-- 
Phil Eaton
http://eatonphil.com

[-- Attachment #2: Type: text/html, Size: 10268 bytes --]

  reply	other threads:[~2015-06-16 13:03 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-06-16 12:15 Rémy El Sibaïe Besognet
2015-06-16 13:03 ` Phil Eaton [this message]
2015-06-16 14:30 ` Jacques Garrigue
2015-06-16 14:45   ` Rémy El Sibaïe Besognet

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='CAByiw+p8M9_QZZJiMoVTJ8HJhG_ap1UASPk=_bk6V4uX3Sb_ig@mail.gmail.com' \
    --to=me@eatonphil.com \
    --cc=caml-list@inria.fr \
    --cc=remy.el-sibaie@lip6.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).