caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] Generic usage of Hashtbl.S with first class module
@ 2015-06-16 12:15 Rémy El Sibaïe Besognet
  2015-06-16 13:03 ` Phil Eaton
  2015-06-16 14:30 ` Jacques Garrigue
  0 siblings, 2 replies; 4+ messages in thread
From: Rémy El Sibaïe Besognet @ 2015-06-16 12:15 UTC (permalink / raw)
  To: caml-list

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

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

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

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [Caml-list] Generic usage of Hashtbl.S with first class module
  2015-06-16 12:15 [Caml-list] Generic usage of Hashtbl.S with first class module Rémy El Sibaïe Besognet
@ 2015-06-16 13:03 ` Phil Eaton
  2015-06-16 14:30 ` Jacques Garrigue
  1 sibling, 0 replies; 4+ messages in thread
From: Phil Eaton @ 2015-06-16 13:03 UTC (permalink / raw)
  To: Rémy El Sibaïe Besognet; +Cc: caml-list

[-- 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 --]

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [Caml-list] Generic usage of Hashtbl.S with first class module
  2015-06-16 12:15 [Caml-list] Generic usage of Hashtbl.S with first class module Rémy El Sibaïe Besognet
  2015-06-16 13:03 ` Phil Eaton
@ 2015-06-16 14:30 ` Jacques Garrigue
  2015-06-16 14:45   ` Rémy El Sibaïe Besognet
  1 sibling, 1 reply; 4+ messages in thread
From: Jacques Garrigue @ 2015-06-16 14:30 UTC (permalink / raw)
  To: Rémy El Sibaïe Besognet; +Cc: Mailing List OCaml

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

Dear Rémy,

The trouble here is that Hashtbl.S defines an abstract type for hash
tables, and that each time you open this module, using the (module H :
Hashtbl.S) pattern, your are talking about a fresh abstract type,
incompatible with anything from the outside world. In particular, there is
no way the parameter ht could have this type, and this causes an error.

Contrary to real functors, first-class modules do not let you connect
abstract parameterized types with the outside world. So in this case, the
only solution I see would be to pack your hash table inside the module
itself, like for an object, so that you don't need to receive ht as a
separate argument.

Jacques Garrigue
2015/06/16 21:16 "Rémy El Sibaïe Besognet" <remy.el-sibaie@lip6.fr>:

> 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
>
>

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

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [Caml-list] Generic usage of Hashtbl.S with first class module
  2015-06-16 14:30 ` Jacques Garrigue
@ 2015-06-16 14:45   ` Rémy El Sibaïe Besognet
  0 siblings, 0 replies; 4+ messages in thread
From: Rémy El Sibaïe Besognet @ 2015-06-16 14:45 UTC (permalink / raw)
  To: Jacques Garrigue; +Cc: Mailing List OCaml

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

@Phil Eaton :
I already tried this solution but it just moves the pattern matching from
parameter to a let binding and does not fix the extrusion scope error.
Thank anyway.



Le mar. 16 juin 2015 à 16:30, Jacques Garrigue <garrigue@math.nagoya-u.ac.jp>
a écrit :

> Dear Rémy,
>
> The trouble here is that Hashtbl.S defines an abstract type for hash
> tables, and that each time you open this module, using the (module H :
> Hashtbl.S) pattern, your are talking about a fresh abstract type,
> incompatible with anything from the outside world. In particular, there is
> no way the parameter ht could have this type, and this causes an error.
>
> Contrary to real functors, first-class modules do not let you connect
> abstract parameterized types with the outside world. So in this case, the
> only solution I see would be to pack your hash table inside the module
> itself, like for an object, so that you don't need to receive ht as a
> separate argument.
>
> Jacques Garrigue
>
Thank you. This was the kind of answer I expected.
During this time I tried my solution without the `ht` parameter which
solves my original problem. But your solution of giving the `ht` value in
the module is way more general.


> 2015/06/16 21:16 "Rémy El Sibaïe Besognet" <remy.el-sibaie@lip6.fr>:
>
> 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
>>
>>

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

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2015-06-16 14:45 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-06-16 12:15 [Caml-list] Generic usage of Hashtbl.S with first class module Rémy El Sibaïe Besognet
2015-06-16 13:03 ` Phil Eaton
2015-06-16 14:30 ` Jacques Garrigue
2015-06-16 14:45   ` Rémy El Sibaïe Besognet

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).