* symbol table containing symbol tables
@ 2007-01-03 1:59 William W Smith
2007-01-03 2:26 ` [Caml-list] " Jon Harrop
2007-01-03 2:29 ` Jonathan Roewen
0 siblings, 2 replies; 4+ messages in thread
From: William W Smith @ 2007-01-03 1:59 UTC (permalink / raw)
To: caml-list
[-- Attachment #1: Type: text/plain, Size: 1188 bytes --]
In pidgin OCaml I'd like to write something like
type complicated =
IVal of int
| StrVa, of string
| SymTableVal of symTable
and
symTable = Map(string -> complicated)
I tried many variations on using the Map module to implement a recursive data structure like this. I failed miserably. (The above wasn't the syntax I ever used, but it gets the idea across.)
However, when I create an object
class [ 'key, 'content ] table : ('key -> 'key -> int) ->
object
,,,
end
I can successfully declare
type complicated =
IVal of int
| StrVal of string
| SymTableVal of (string, complicated) table
that does what I want. Ithis isn't the whole declaration of complicated, but I believe once I get this declaration working, the more quirky variations work too.
Do I need one of the more advanced features of OCaml that I don't currently understand to use Map the way that I want without writing a whole table class? I don't even see how I can use Map from inside the table class to do what I want which would also be acceptable.
I don't want to have to break open the Map module to modify it and thus change the licensing of my final program.
Thanks
Bill
[-- Attachment #2: Type: text/html, Size: 1434 bytes --]
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [Caml-list] symbol table containing symbol tables
2007-01-03 1:59 symbol table containing symbol tables William W Smith
@ 2007-01-03 2:26 ` Jon Harrop
2007-01-03 2:33 ` Jonathan Roewen
2007-01-03 2:29 ` Jonathan Roewen
1 sibling, 1 reply; 4+ messages in thread
From: Jon Harrop @ 2007-01-03 2:26 UTC (permalink / raw)
To: caml-list
On Wednesday 03 January 2007 01:59, William W Smith wrote:
> Do I need one of the more advanced features of OCaml that I don't currently
> understand to use Map the way that I want without writing a whole table
> class? I don't even see how I can use Map from inside the table class to
> do what I want which would also be acceptable.
You need recursive modules, something like this:
# module rec StringMap : Map.S = Map.Make(String)
and Symbols : sig
type t =
| IVal of int
| StrVal of string
| SymTableVal of t StringMap.t
end = struct
type t =
| IVal of int
| StrVal of string
| SymTableVal of t StringMap.t
end;;
module rec StringMap : Map.S
and Symbols :
sig
type t = IVal of int | StrVal of string | SymTableVal of t StringMap.t
end
HTH.
--
Dr Jon D Harrop, Flying Frog Consultancy Ltd.
Objective CAML for Scientists
http://www.ffconsultancy.com/products/ocaml_for_scientists
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [Caml-list] symbol table containing symbol tables
2007-01-03 1:59 symbol table containing symbol tables William W Smith
2007-01-03 2:26 ` [Caml-list] " Jon Harrop
@ 2007-01-03 2:29 ` Jonathan Roewen
1 sibling, 0 replies; 4+ messages in thread
From: Jonathan Roewen @ 2007-01-03 2:29 UTC (permalink / raw)
To: William W Smith; +Cc: caml-list
I don't believe you can eliminate the type variable (for the values
associated with the keys) from Map's signature. I would personally
just use a Hashtbl instead.
type complicated = Val of int | StrVal of string | SymTableVal of symTable
and symTable = (string, complicated) Hashtbl.t
Jonathan
On 1/3/07, William W Smith <sesquized@sbcglobal.net> wrote:
> In pidgin OCaml I'd like to write something like
>
> type complicated =
> IVal of int
> | StrVa, of string
> | SymTableVal of symTable
> and
> symTable = Map(string -> complicated)
>
> I tried many variations on using the Map module to implement a recursive
> data structure like this. I failed miserably. (The above wasn't the syntax
> I ever used, but it gets the idea across.)
>
> However, when I create an object
> class [ 'key, 'content ] table : ('key -> 'key -> int) ->
> object
> ,,,
> end
>
> I can successfully declare
> type complicated =
> IVal of int
> | StrVal of string
> | SymTableVal of (string, complicated) table
> that does what I want. Ithis isn't the whole declaration of complicated,
> but I believe once I get this declaration working, the more quirky
> variations work too.
>
> Do I need one of the more advanced features of OCaml that I don't currently
> understand to use Map the way that I want without writing a whole table
> class? I don't even see how I can use Map from inside the table class to do
> what I want which would also be acceptable.
>
> I don't want to have to break open the Map module to modify it and thus
> change the licensing of my final program.
>
> Thanks
>
> Bill
>
> _______________________________________________
> 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] 4+ messages in thread
* Re: [Caml-list] symbol table containing symbol tables
2007-01-03 2:26 ` [Caml-list] " Jon Harrop
@ 2007-01-03 2:33 ` Jonathan Roewen
0 siblings, 0 replies; 4+ messages in thread
From: Jonathan Roewen @ 2007-01-03 2:33 UTC (permalink / raw)
To: Jon Harrop; +Cc: caml-list
Ooh, that's clever. An actual use case for recursive modules that's
also simple :-)
On 1/3/07, Jon Harrop <jon@ffconsultancy.com> wrote:
> On Wednesday 03 January 2007 01:59, William W Smith wrote:
> > Do I need one of the more advanced features of OCaml that I don't currently
> > understand to use Map the way that I want without writing a whole table
> > class? I don't even see how I can use Map from inside the table class to
> > do what I want which would also be acceptable.
>
> You need recursive modules, something like this:
>
> # module rec StringMap : Map.S = Map.Make(String)
> and Symbols : sig
> type t =
> | IVal of int
> | StrVal of string
> | SymTableVal of t StringMap.t
> end = struct
> type t =
> | IVal of int
> | StrVal of string
> | SymTableVal of t StringMap.t
> end;;
> module rec StringMap : Map.S
> and Symbols :
> sig
> type t = IVal of int | StrVal of string | SymTableVal of t StringMap.t
> end
>
> HTH.
>
> --
> Dr Jon D Harrop, Flying Frog Consultancy Ltd.
> Objective CAML for Scientists
> http://www.ffconsultancy.com/products/ocaml_for_scientists
>
> _______________________________________________
> 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] 4+ messages in thread
end of thread, other threads:[~2007-01-03 2:33 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2007-01-03 1:59 symbol table containing symbol tables William W Smith
2007-01-03 2:26 ` [Caml-list] " Jon Harrop
2007-01-03 2:33 ` Jonathan Roewen
2007-01-03 2:29 ` Jonathan Roewen
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox