|
List Info
Thread: Re: "ocaml_beginners"::[] quick question on mutually recursive types
|
|
| Re: "ocaml_beginners"::[]
quick question on mutually recursive
types |
  United States |
2007-03-05 12:51:12 |
|
--- In ocaml_beginners%40yahoogroups.com">ocaml_beginners yahoogroups.com, Jon Harrop <jon ...> wrote:
>
> On Monday 05 March 2007 08:22, overbored wrote:
> > how do i make a class obj containing a Set of obj? it's like the
> > chicken and egg problem - if i first instantiate the module, obj
is
> > undefined, but if i first define the class, i haven't instantiated
> > the Set yet. thanks in advance for any help.
>
> You must use mutually recursive modules:
>
> # module rec A : sig
> type t = <get: ASet.t>
> val compare : t -> t -> int
> end = struct
> type t = <get: ASet.t>
> let compare = compare
> end
> and ASet : Set.S = Set.Make(A);;
> module rec A : sig type t = < get : ASet.t > val compare : t -> t -
> int end
> and ASet : Set.S
>
> However, as Richard has said you are probably using the wrong tool
for the
> job. Try posting problems rather than proposed solutions without
the problem
> and you'll probably get more helpful advice.
sure. i'm trying to write an (initially simple) object database. the
rest of this paragraph is some background on this project. i've tried
using python, haskell, and scala so far, with no luck. in python, my
code was turning mostly into a dynamic type-checker. in haskell,
dealing with cyclic references and mutable data structures (basically
need to write my own non-functional hashsets, etc.) was a hassle
whose magnitude i underestimated. in scala, i got much farther, but
encountered a road block when it came to persistence - i basically
need to write my own pickling combinators for standard data
structures like set, hashset, hashmap, etc. although this is a
slightly smaller roadblock, i'm considering this as an opportunity to
learn ocaml. for now, i'm first making sure i can do all the things i
need to do in ocaml before i rewrite the application logic again,
such as persistence (Marshal) and whether the non-functional paradigm
is usable (subject of this thread). (aside: ideally, i can use static
shape analysis to ensure data structure consistency, which is a very
prominent part of my application, but no such usable language or tool
exists that doesn't require expertise in interactive theorem provers,
overly explicit annotations, etc.)
here's the source code for the scala version:
http://paste.lisp.org/display/37746
you can guess from my mention of persistence that i'm not too worried
about performance at this point; i'd just like to get something
working, and reads/writes won't be very frequent (yet). otoh, i do
need to put thousands of references in these sets, so (at least in
the long run) i'd like efficiency here (Set.t ref are O(log(n))?). i
have these general questions:
- i realize Set is immutable, so should i build my own hashset type
based on Hashtbl instead? any mutable hashset implementations for
ocaml? (will i be able to serialize these things with Marshal out-of-
the-box?)
- why can't i just use Sets like Hashtbls? why do i deal with these
module functors, whereas i don't need to with Hashtbl or Marshal or
other (seemingly more complex than Set) utilities?
i've perused the caml hump for any promising libs, to no avail. any
other suggestions on how to pursue my goal in ocaml (or any other
language really) are much appreciated. thanks!
>
> --
> Dr Jon D Harrop, Flying Frog Consultancy Ltd.
> OCaml for Scientists
> http://www.ffconsultancy.com/products/ocaml_for_scientists
>
__._,_.___
.
__,_._,___
|
| Re: "ocaml_beginners"::[]
quick question on mutually recursive
types |
  United States |
2007-03-05 13:54:56 |
|
--- In ocaml_beginners%40yahoogroups.com">ocaml_beginners yahoogroups.com, "overbored" <overbored ...>
wrote:
>
> --- In ocaml_beginners%40yahoogroups.com">ocaml_beginners yahoogroups.com, Jon Harrop <jon > wrote:
> >
> > On Monday 05 March 2007 08:22, overbored wrote:
> > > how do i make a class obj containing a Set of obj? it's like the
> > > chicken and egg problem - if i first instantiate the module,
obj
> is
> > > undefined, but if i first define the class, i haven't
instantiated
> > > the Set yet. thanks in advance for any help.
> >
> > You must use mutually recursive modules:
> >
> > # module rec A : sig
> > type t = <get: ASet.t>
> > val compare : t -> t -> int
> > end = struct
> > type t = <get: ASet.t>
> > let compare = compare
> > end
> > and ASet : Set.S = Set.Make(A);;
> > module rec A : sig type t = < get : ASet.t > val compare : t -> t
-
> > int end
> > and ASet : Set.S
> >
> > However, as Richard has said you are probably using the wrong
tool
> for the
> > job. Try posting problems rather than proposed solutions without
> the problem
> > and you'll probably get more helpful advice.
>
> sure. i'm trying to write an (initially simple) object database.
the
> rest of this paragraph is some background on this project. i've
tried
> using python, haskell, and scala so far, with no luck. in python,
my
> code was turning mostly into a dynamic type-checker. in haskell,
> dealing with cyclic references and mutable data structures
(basically
> need to write my own non-functional hashsets, etc.) was a hassle
> whose magnitude i underestimated. in scala, i got much farther, but
> encountered a road block when it came to persistence - i basically
> need to write my own pickling combinators for standard data
> structures like set, hashset, hashmap, etc. although this is a
> slightly smaller roadblock, i'm considering this as an opportunity
to
> learn ocaml. for now, i'm first making sure i can do all the things
i
> need to do in ocaml before i rewrite the application logic again,
> such as persistence (Marshal) and whether the non-functional
paradigm
> is usable (subject of this thread). (aside: ideally, i can use
static
> shape analysis to ensure data structure consistency, which is a
very
> prominent part of my application, but no such usable language or
tool
> exists that doesn't require expertise in interactive theorem
provers,
> overly explicit annotations, etc.)
>
> here's the source code for the scala version:
>
> http://paste.lisp.org/display/37746
>
> you can guess from my mention of persistence that i'm not too
worried
> about performance at this point; i'd just like to get something
> working, and reads/writes won't be very frequent (yet). otoh, i do
> need to put thousands of references in these sets, so (at least in
> the long run) i'd like efficiency here (Set.t ref are O(log(n))?).
i
> have these general questions:
>
> - i realize Set is immutable, so should i build my own hashset type
> based on Hashtbl instead? any mutable hashset implementations for
> ocaml? (will i be able to serialize these things with Marshal out-
of-
> the-box?)
for part of this question, i just found http://wwwteor.mi.infn.it/
~pernici/ocaml/ocaml.html. i still need to see if this is what i seek.
>
> - why can't i just use Sets like Hashtbls? why do i deal with these
> module functors, whereas i don't need to with Hashtbl or Marshal or
> other (seemingly more complex than Set) utilities?
>
> i've perused the caml hump for any promising libs, to no avail. any
> other suggestions on how to pursue my goal in ocaml (or any other
> language really) are much appreciated. thanks!
>
> >
> > --
> > Dr Jon D Harrop, Flying Frog Consultancy Ltd.
> > OCaml for Scientists
> > http://www.ffconsultancy.com/products/ocaml_for_scientists
> >
>
__._,_.___
.
__,_._,___
|
| Re: "ocaml_beginners"::[]
quick question on mutually recursive
types |
  United Kingdom |
2007-03-07 16:16:05 |
|
On Monday 05 March 2007 18:51, overbored wrote:
> sure. i'm trying to write an (initially simple) object database. the
> rest of this paragraph is some background on this project. i've tried
> using python, haskell, and scala so far, with no luck. in python, my
> code was turning mostly into a dynamic type-checker. in haskell,
> dealing with cyclic references and mutable data structures (basically
> need to write my own non-functional hashsets, etc.) was a hassle
> whose magnitude i underestimated.
Can you explain why parts of your solution must be imperative?
> in scala, i got much farther, but
> encountered a road block when it came to persistence - i basically
> need to write my own pickling combinators for standard data
> structures like set, hashset, hashmap, etc. although this is a
> slightly smaller roadblock, i'm considering this as an opportunity to
> learn ocaml.
You probably want to write picklers in OCaml too.
> for now, i'm first making sure i can do all the things i
> need to do in ocaml before i rewrite the application logic again,
> such as persistence (Marshal) and whether the non-functional paradigm
> is usable (subject of this thread).
That sounds like your proposed solution rather than the original problem.
> you can guess from my mention of persistence that i'm not too worried
> about performance at this point; i'd just like to get something
> working, and reads/writes won't be very frequent (yet). otoh, i do
> need to put thousands of references in these sets, so (at least in
> the long run) i'd like efficiency here (Set.t ref are O(log(n))?).
Set.find is O(log n), yes.
> i have these general questions:
>
> - i realize Set is immutable, so should i build my own hashset type
> based on Hashtbl instead?
I don't understand why you want to avoid purely functional data structures.
They are usually much simpler to use and can be faster.
> any mutable hashset implementations for
> ocaml? (will i be able to serialize these things with Marshal out-of-
> the-box?)
The one you cite in your next e-mail is fine.
> - why can't i just use Sets like Hashtbls? why do i deal with these
> module functors, whereas i don't need to with Hashtbl or Marshal or
> other (seemingly more complex than Set) utilities?
Easy: OCaml's hash tables use an implicit hashing function that doesn't always
do what you want. With Sets, you provide the comparison function explicitly.
> i've perused the caml hump for any promising libs, to no avail. any
> other suggestions on how to pursue my goal in ocaml (or any other
> language really) are much appreciated. thanks!
Your task sounds like an interpreter for a dynamically-typed object oriented
language, which should be easy to do in many languages including OCaml.
However, I am concerned that you are still stating your proposed solution
(e.g. mutable sets) and not the original problem.
--
Dr Jon D Harrop, Flying Frog Consultancy Ltd.
OCaml for Scientists
http://www.ffconsultancy.com/products/ocaml_for_scientists
__._,_.___
.
__,_._,___
|
[1-3]
|
|