* Defining type that requires hashtables with recursive definition
@ 2009-01-19 17:26 Hugo Ferreira
2009-01-19 17:34 ` [Caml-list] " Jacques Carette
0 siblings, 1 reply; 6+ messages in thread
From: Hugo Ferreira @ 2009-01-19 17:26 UTC (permalink / raw)
To: caml-list
Hello,
I am attempting to define a type so:
type node =
| Node of links
| Leaf of int
And I want to implement links as a
hashtable whose keys and values are
also of type node. Note that the idea
is to use object address comparison
for the keys so:
module H =
struct
type t = node
let equal (e1:node) (e2:node) = (==) e1 e2
let hash (e:node) = Hashtbl.hash e
end
module J = Hashtbl.Make( H )
However this requires a recursive definition on
the hashtable. How may a declare node and hashtable
to allow this?
TIA,
Hugo F.
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [Caml-list] Defining type that requires hashtables with recursive definition
2009-01-19 17:26 Defining type that requires hashtables with recursive definition Hugo Ferreira
@ 2009-01-19 17:34 ` Jacques Carette
2009-01-19 18:03 ` Thomas Gazagnaire
2009-01-19 19:34 ` Hugo Ferreira
0 siblings, 2 replies; 6+ messages in thread
From: Jacques Carette @ 2009-01-19 17:34 UTC (permalink / raw)
To: Hugo Ferreira; +Cc: caml-list
Hugo Ferreira wrote:
> I am attempting to define a type so:
>
> type node =
> | Node of links
> | Leaf of int
>
> And I want to implement links as a
> hashtable whose keys and values are
> also of type node.
type node =
| Node of links
| Leaf of int
and links = (node, node) Hashtbl.t
should do it.
Jacques
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [Caml-list] Defining type that requires hashtables with recursive definition
2009-01-19 17:34 ` [Caml-list] " Jacques Carette
@ 2009-01-19 18:03 ` Thomas Gazagnaire
2009-01-19 19:36 ` Hugo Ferreira
2009-01-20 10:09 ` Hugo Ferreira
2009-01-19 19:34 ` Hugo Ferreira
1 sibling, 2 replies; 6+ messages in thread
From: Thomas Gazagnaire @ 2009-01-19 18:03 UTC (permalink / raw)
To: Jacques Carette; +Cc: Hugo Ferreira, caml-list
[-- Attachment #1: Type: text/plain, Size: 1048 bytes --]
or if you really want to define your own equality, you can use recursive
modules:
module rec H : Hashtbl.HashedType =
struct
type node =
| Node of node J.t
| Leaf of int
type t = node
let equal (e1:node) (e2:node) = (==) e1 e2
let hash (e:node) = Hashtbl.hash e
end
and J : Hashtbl.S = Hashtbl.Make( H )
2009/1/19 Jacques Carette <carette@mcmaster.ca>
> Hugo Ferreira wrote:
>
>> I am attempting to define a type so:
>>
>> type node =
>> | Node of links
>> | Leaf of int
>>
>> And I want to implement links as a
>> hashtable whose keys and values are
>> also of type node.
>>
>
> type node =
> | Node of links
> | Leaf of int
> and links = (node, node) Hashtbl.t
>
> should do it.
>
> Jacques
>
>
> _______________________________________________
> Caml-list mailing list. Subscription management:
> http://yquem.inria.fr/cgi-bin/mailman/listinfo/caml-list
> Archives: http://caml.inria.fr
> Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
> Bug reports: http://caml.inria.fr/bin/caml-bugs
>
[-- Attachment #2: Type: text/html, Size: 2077 bytes --]
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [Caml-list] Defining type that requires hashtables with recursive definition
2009-01-19 17:34 ` [Caml-list] " Jacques Carette
2009-01-19 18:03 ` Thomas Gazagnaire
@ 2009-01-19 19:34 ` Hugo Ferreira
1 sibling, 0 replies; 6+ messages in thread
From: Hugo Ferreira @ 2009-01-19 19:34 UTC (permalink / raw)
To: Jacques Carette; +Cc: caml-list
Jacques Carette wrote:
> Hugo Ferreira wrote:
>> I am attempting to define a type so:
>>
>> type node =
>> | Node of links
>> | Leaf of int
>>
>> And I want to implement links as a
>> hashtable whose keys and values are
>> also of type node.
>
> type node =
> | Node of links
> | Leaf of int
> and links = (node, node) Hashtbl.t
>
> should do it.
>
I already had considered this but I assume that
the hash-table equality is a structural comparison
and not the (==) that I need.
Thanks for the suggestion.
R.
Hugo F.
> Jacques
>
> _______________________________________________
> Caml-list mailing list. Subscription management:
> http://yquem.inria.fr/cgi-bin/mailman/listinfo/caml-list
> Archives: http://caml.inria.fr
> Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
> Bug reports: http://caml.inria.fr/bin/caml-bugs
>
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [Caml-list] Defining type that requires hashtables with recursive definition
2009-01-19 18:03 ` Thomas Gazagnaire
@ 2009-01-19 19:36 ` Hugo Ferreira
2009-01-20 10:09 ` Hugo Ferreira
1 sibling, 0 replies; 6+ messages in thread
From: Hugo Ferreira @ 2009-01-19 19:36 UTC (permalink / raw)
To: Thomas Gazagnaire; +Cc: Jacques Carette, caml-list
Thomas Gazagnaire wrote:
> or if you really want to define your own equality, you can use recursive
> modules:
>
> module rec H : Hashtbl.HashedType =
> struct
> type node =
> | Node of node J.t
> | Leaf of int
>
> type t = node
> let equal (e1:node) (e2:node) = (==) e1 e2
> let hash (e:node) = Hashtbl.hash e
> end
>
> and J : Hashtbl.S = Hashtbl.Make( H )
>
Yes, looks like what I need.
Thanks,
Hugo F.
> 2009/1/19 Jacques Carette <carette@mcmaster.ca <mailto:carette@mcmaster.ca>>
>
> Hugo Ferreira wrote:
>
> I am attempting to define a type so:
>
> type node =
> | Node of links
> | Leaf of int
>
> And I want to implement links as a
> hashtable whose keys and values are
> also of type node.
>
>
> type node =
> | Node of links
> | Leaf of int
> and links = (node, node) Hashtbl.t
>
> should do it.
>
> Jacques
>
>
> _______________________________________________
> Caml-list mailing list. Subscription management:
> http://yquem.inria.fr/cgi-bin/mailman/listinfo/caml-list
> Archives: http://caml.inria.fr
> Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
> Bug reports: http://caml.inria.fr/bin/caml-bugs
>
>
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [Caml-list] Defining type that requires hashtables with recursive definition
2009-01-19 18:03 ` Thomas Gazagnaire
2009-01-19 19:36 ` Hugo Ferreira
@ 2009-01-20 10:09 ` Hugo Ferreira
1 sibling, 0 replies; 6+ messages in thread
From: Hugo Ferreira @ 2009-01-20 10:09 UTC (permalink / raw)
To: Thomas Gazagnaire; +Cc: Jacques Carette, caml-list
Hello,
Thomas Gazagnaire wrote:
> or if you really want to define your own equality, you can use recursive
> modules:
>
> module rec H : Hashtbl.HashedType =
> struct
> type node =
> | Node of node J.t
> | Leaf of int
>
> type t = node
> let equal (e1:node) (e2:node) = (==) e1 e2
> let hash (e:node) = Hashtbl.hash e
> end
>
> and J : Hashtbl.S = Hashtbl.Make( H )
Just to inform you, in case you are interested, that the definition
above does not quite solve my problem. A small test shows that I still
have the same issue I originally had with the hash-table keys:
# open H ;;
# let no_jumps = J.create 13 ;;
val no_jumps : '_a J.t = <abstr>
# let empty = Node(no_jumps) ;;
val empty : H.node = Node <abstr>
# let _ = J.add no_jumps empty empty ;;
This expression has type H.node but is here used with type J.key
To solve this we need to explicitly declare that
Hashtbl.S.key = H.node. So I use:
module rec H :
sig
type node =
| Node of node J.t
| Leaf of int
type t = node
val equal : t -> t -> bool
val hash : t -> int
end =
struct
type node =
| Node of node J.t
| Leaf of int
type t = node
let equal (e1:node) (e2:node) = (==) e1 e2
let hash (e:node) = Hashtbl.hash e
end
and J : Hashtbl.S with type key = H.node = Hashtbl.Make( H )
;;
(Note: I made the type "t" of H visible to facilitate coding)
And it now works as expected:
# open H ;;
# let no_jumps = J.create 13 ;;
val no_jumps : '_a J.t = <abstr>
# let empty = Node(no_jumps) ;;
val empty : H.node = Node <abstr>
# let _ = J.add no_jumps empty empty ;;
- : unit = ()
# let x = J.find no_jumps empty ;;
val x : H.node = Node <abstr>
# let r = x == empty ;;
val r : bool = true
Once again,
Thank you,
Hugo F.
>
> 2009/1/19 Jacques Carette <carette@mcmaster.ca <mailto:carette@mcmaster.ca>>
>
> Hugo Ferreira wrote:
>
> I am attempting to define a type so:
>
> type node =
> | Node of links
> | Leaf of int
>
> And I want to implement links as a
> hashtable whose keys and values are
> also of type node.
>
>
> type node =
> | Node of links
> | Leaf of int
> and links = (node, node) Hashtbl.t
>
> should do it.
>
> Jacques
>
>
> _______________________________________________
> Caml-list mailing list. Subscription management:
> http://yquem.inria.fr/cgi-bin/mailman/listinfo/caml-list
> Archives: http://caml.inria.fr
> Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
> Bug reports: http://caml.inria.fr/bin/caml-bugs
>
>
>
> ------------------------------------------------------------------------
>
> _______________________________________________
> Caml-list mailing list. Subscription management:
> http://yquem.inria.fr/cgi-bin/mailman/listinfo/caml-list
> Archives: http://caml.inria.fr
> Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
> Bug reports: http://caml.inria.fr/bin/caml-bugs
^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~2009-01-20 10:09 UTC | newest]
Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2009-01-19 17:26 Defining type that requires hashtables with recursive definition Hugo Ferreira
2009-01-19 17:34 ` [Caml-list] " Jacques Carette
2009-01-19 18:03 ` Thomas Gazagnaire
2009-01-19 19:36 ` Hugo Ferreira
2009-01-20 10:09 ` Hugo Ferreira
2009-01-19 19:34 ` Hugo Ferreira
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox