* [Caml-list] how to define a property list
@ 2004-04-23 17:56 Christoph Bauer
2004-04-23 18:00 ` Alain.Frisch
2004-04-23 18:06 ` Richard Jones
0 siblings, 2 replies; 6+ messages in thread
From: Christoph Bauer @ 2004-04-23 17:56 UTC (permalink / raw)
To: OCaml List
Hi List,
I want to create a tree. This tree should be a very simple accessible
for algorithms and easy to understand. The nodes in the tree can have
different properties of different types.
An example of such a tree is the DOM-tree in JavaScript.
There are several ways how to design such a tree.
1.) Using Records. Each node is a records, and the
records contain a field for each possible property.
Maybe a better approach is a type like this
type ('a,'b) node = { <common fields>; mutable properties : 'a * 'b list }
The list `properties' should be a key-value list. A Lisp-like alist would
be perfect. Unfortunately the type 'b isn't fix. It can be string or int
or something completely different.
2a) Using dynamic typing.
type property_value = Int of int | String of String.
type 'a node = { <common fields>; mutable properties : 'a * property_value list }
If this is the solution, I'll better switch to Scheme, which has better support
for dynamic types ;-)
2b) Add for each type a property list to node.
type 'a node = {
<common fields>;
mutable int_properties : 'a * int list
mutable string_properties : 'a * string list
...
}
This is my preferred solution so far.
3) Using OOP. (I'm not sure, whether OOP in general is good or bad.)
class node =
object end;;
class ['a, 'b] sub_node a b=
object(s)
inherit node
val mutable property1 : 'a = a ;
val mutable property2 : 'b = b;
method property1 = property1;
method set_property1 = property1
method property2 = property2;
method set_property2 = property2
end;;
(* lot of typing for nothing so far*)
let a = new sub_node 1 "a" :> node;;
Question: How can I now access property1?
Thanks for comments,
Christoph Bauer
--
beginfig(1)u=3cm;draw fullcircle scaled 2u;x0=x1=y1=x2=y3=0;-y0=y2=x3=1u;
filldraw z0..{left}z1{left}..z2{curl 1}..z3..z0..cycle;def t(expr p)=fullcircle
scaled .25u shifted(0,p*u);enddef;unfill t(.5);fill t(-.5);endfig;bye
-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [Caml-list] how to define a property list
2004-04-23 17:56 [Caml-list] how to define a property list Christoph Bauer
@ 2004-04-23 18:00 ` Alain.Frisch
2004-04-23 18:51 ` Christoph Bauer
2004-04-23 18:06 ` Richard Jones
1 sibling, 1 reply; 6+ messages in thread
From: Alain.Frisch @ 2004-04-23 18:00 UTC (permalink / raw)
To: Christoph Bauer; +Cc: OCaml List
On Fri, 23 Apr 2004, Christoph Bauer wrote:
> I want to create a tree. This tree should be a very simple accessible
> for algorithms and easy to understand. The nodes in the tree can have
> different properties of different types.
Have a look at:
http://caml.inria.fr/archives/200105/msg00175.html
-- Alain
-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [Caml-list] how to define a property list
2004-04-23 17:56 [Caml-list] how to define a property list Christoph Bauer
2004-04-23 18:00 ` Alain.Frisch
@ 2004-04-23 18:06 ` Richard Jones
2004-04-23 19:02 ` Christoph Bauer
1 sibling, 1 reply; 6+ messages in thread
From: Richard Jones @ 2004-04-23 18:06 UTC (permalink / raw)
Cc: OCaml List
On Fri, Apr 23, 2004 at 07:56:54PM +0200, Christoph Bauer wrote:
> Hi List,
>
> I want to create a tree. This tree should be a very simple accessible
> for algorithms and easy to understand. The nodes in the tree can have
> different properties of different types.
Could well be missing the point entirely here, but what about:
type 'a tree = Leaf of 'a | Node of 'a tree * 'a * 'a tree
and then use something simple like an assoc-list for storing the
properties, so your final type would be:
(string * string) list tree
eg:
let t = Node (Leaf [], [ "prop1", "value1" ], Leaf []);;
val t : (string * string) list tree = (* ... *)
This seems to separate out the 'treeness' from what is stored at each
node, so you can use generic algorithms to operate over the tree.
Rich.
--
Richard Jones. http://www.annexia.org/ http://www.j-london.com/
Merjis Ltd. http://www.merjis.com/ - improving website return on investment
MAKE+ is a sane replacement for GNU autoconf/automake. One script compiles,
RPMs, pkgs etc. Linux, BSD, Solaris. http://www.annexia.org/freeware/makeplus/
-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [Caml-list] how to define a property list
2004-04-23 18:00 ` Alain.Frisch
@ 2004-04-23 18:51 ` Christoph Bauer
2004-04-24 8:15 ` Christoph Bauer
0 siblings, 1 reply; 6+ messages in thread
From: Christoph Bauer @ 2004-04-23 18:51 UTC (permalink / raw)
To: OCaml List
Alain.Frisch@ens.fr writes:
> On Fri, 23 Apr 2004, Christoph Bauer wrote:
>
>> I want to create a tree. This tree should be a very simple accessible
>> for algorithms and easy to understand. The nodes in the tree can have
>> different properties of different types.
>
> Have a look at:
> http://caml.inria.fr/archives/200105/msg00175.html
Wow, supercool. But, btw, there are some missing
prop := None
in get_properties and the second version of put_properties.
Just for the case that someone wants to get the same property
of two different atoms.
Regards,
Christoph Bauer
--
beginfig(1)u=3cm;draw fullcircle scaled 2u;x0=x1=y1=x2=y3=0;-y0=y2=x3=1u;
filldraw z0..{left}z1{left}..z2{curl 1}..z3..z0..cycle;def t(expr p)=fullcircle
scaled .25u shifted(0,p*u);enddef;unfill t(.5);fill t(-.5);endfig;bye
-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [Caml-list] how to define a property list
2004-04-23 18:06 ` Richard Jones
@ 2004-04-23 19:02 ` Christoph Bauer
0 siblings, 0 replies; 6+ messages in thread
From: Christoph Bauer @ 2004-04-23 19:02 UTC (permalink / raw)
To: OCaml List
Hi,
> Could well be missing the point entirely here, but what about:
>
> type 'a tree = Leaf of 'a | Node of 'a tree * 'a * 'a tree
>
> and then use something simple like an assoc-list for storing the
> properties, so your final type would be:
>
> (string * string) list tree
^^^^^^ this could be a int or a string in the same tree!
> eg:
>
> let t = Node (Leaf [], [ "prop1", "value1" ], Leaf []);;
> val t : (string * string) list tree = (* ... *)
>
> This seems to separate out the 'treeness' from what is stored at each
> node, so you can use generic algorithms to operate over the tree.
Of course I could convert my integer to strings (and back),
but this solutions isn't so satisfying
Regards,
Christoph Bauer
--
beginfig(1)u=3cm;draw fullcircle scaled 2u;x0=x1=y1=x2=y3=0;-y0=y2=x3=1u;
filldraw z0..{left}z1{left}..z2{curl 1}..z3..z0..cycle;def t(expr p)=fullcircle
scaled .25u shifted(0,p*u);enddef;unfill t(.5);fill t(-.5);endfig;bye
-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [Caml-list] how to define a property list
2004-04-23 18:51 ` Christoph Bauer
@ 2004-04-24 8:15 ` Christoph Bauer
0 siblings, 0 replies; 6+ messages in thread
From: Christoph Bauer @ 2004-04-24 8:15 UTC (permalink / raw)
To: OCaml List
Hi,
>> http://caml.inria.fr/archives/200105/msg00175.html
I rewrote this code as a module. Differences are
* there is no `prop := None' bug (maybe others)
* it uses a hashtable to find the unit->unit functions (should be much faster)
* Symbols can be created from strings, variants, ints, ...
* properties can have trigger-functions, one for get and one for put. These
function will be called on each reading or modifying of the property.
This is maybe more than someone wants, but it's cheap to implement.
Is there one of those ExtLibs which accepts this module as contribution?
Regards,
Christoph Bauer
(* symbols.mli *)
(** Symbols with properties of various types. *)
module type KEY_TYPE = sig type t end
(**
KEYTYPE abstracts from the concret type of a symbol.
*)
module Make :
functor (Key_type : KEY_TYPE) ->
sig
type 'a property
type symbol
val make : Key_type.t -> symbol
(** [make key] creates a symbol. *)
val key_of_symbol : symbol -> Key_type.t
(** [key_of_symbol symbol] returns the key of a symbol. *)
val make_property :
?put_trigger:(symbol -> 'a -> unit) ->
?get_trigger:(symbol -> 'a -> unit) -> unit -> 'a property
(** [make_property ?put_tigger ?get_trigger ()] creates a new property.
[put symbol this_property] calls the [put_trigger] (if specified)
and [get symbol this_property] calls the [get_trigger]. *)
val put : symbol -> 'a property -> 'a -> unit
(** [put symbol prop value] puts a property with value [value] to a symbol.
[put] calls the put_trigger for property.
*)
val get : symbol -> 'a property -> 'a
(** [get symbol property] returns the value of the property of the symbol.
[get] calls the get_trigger for property.
*)
end
(** [StringSymbol = Make(String)] *)
module StringSymbol :
sig
type 'a property
type symbol
val symbol_table : (String.t, symbol) Hashtbl.t
val make : String.t -> symbol
val key_of_symbol : symbol -> String.t
val make_property :
?put_trigger:(symbol -> 'a -> unit) ->
?get_trigger:(symbol -> 'a -> unit) -> unit -> 'a property
val put : symbol -> 'a property -> 'a -> unit
val get : symbol -> 'a property -> 'a
end
(* symbols.ml *)
module type KEY_TYPE =
sig
type t
end
let some_of =
function
Some x -> x
| None -> invalid_arg "some_of"
module Make(Key_type: KEY_TYPE) =
struct
type 'a property = {
id : int;
mutable value : 'a option;
get_trigger : (symbol -> 'a -> unit) option; (* maybe mutable ? *)
put_trigger : (symbol -> 'a -> unit) option; (* maybe mutable ? *)
}
and symbol = {
name : Key_type.t;
mutable properties : (int, unit->unit) Hashtbl.t;
}
let symbol_table = Hashtbl.create 127
let make name =
try Hashtbl.find symbol_table name
with Not_found ->
let s = { name = name; properties = Hashtbl.create 13 } in
Hashtbl.add symbol_table name s;
s
let key_of_symbol symbol = symbol.name
let pcounter = ref ~-1
let make_property ?put_trigger ?get_trigger () = (* not thread safe *)
incr pcounter;
{
id = !pcounter;
value = None;
get_trigger = get_trigger;
put_trigger = put_trigger;
}
let put symbol prop value =
Hashtbl.add symbol.properties prop.id (fun () -> prop.value <- Some value);
match prop.put_trigger with
Some g -> g symbol value
| None -> ()
let get symbol prop =
let f = Hashtbl.find symbol.properties prop.id in
f ();
(match prop.get_trigger with
Some g -> g symbol (some_of prop.value);
| None -> ());
some_of prop.value
end
module StringSymbol = Make(String)
(* symbol_test.ml *)
module Symbol = Symbol.StringSymbol
let () =
let symbol = Symbol.make "hello" in
let string_property = Symbol.make_property
~get_trigger: (fun s v -> print_endline ("get on `" ^ Symbol.key_of_symbol s ^ "' returns `" ^ v ^ "'")) ()
and int_property = Symbol.make_property
~put_trigger: (fun s v -> print_endline ("put `" ^ string_of_int v ^ "' to `" ^ Symbol.key_of_symbol s ^ "'")) ()
and bool_property = Symbol.make_property () in
Symbol.put symbol string_property "test";
Symbol.put symbol int_property 42;
Symbol.put symbol bool_property true;
let s = Symbol.get symbol string_property
and i = Symbol.get symbol int_property
and b = Symbol.get symbol bool_property in
print_endline
("s=" ^ s ^ "\n"
^ "i=" ^ string_of_int i ^ "\n"
^ "b=" ^ if b then "true" else "false")
--
beginfig(1)u=3cm;draw fullcircle scaled 2u;x0=x1=y1=x2=y3=0;-y0=y2=x3=1u;
filldraw z0..{left}z1{left}..z2{curl 1}..z3..z0..cycle;def t(expr p)=fullcircle
scaled .25u shifted(0,p*u);enddef;unfill t(.5);fill t(-.5);endfig;bye
-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~2004-04-24 8:09 UTC | newest]
Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-04-23 17:56 [Caml-list] how to define a property list Christoph Bauer
2004-04-23 18:00 ` Alain.Frisch
2004-04-23 18:51 ` Christoph Bauer
2004-04-24 8:15 ` Christoph Bauer
2004-04-23 18:06 ` Richard Jones
2004-04-23 19:02 ` Christoph Bauer
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox