Debian Codegraph @

doc/guide/src/PropertyList.adoc

raw
1 = PropertyList
2
3 A property list is a dictionary-like data structure into which
4 properties (name-value pairs) can be inserted and from which
5 properties can be looked up by name. The term comes from the Lisp
6 language, where every symbol has a property list for storing
7 information, and where the names are typically symbols and keys can be
8 any type of value.
9
10 Here is an SML signature for property lists such that for any type of
11 value a new property can be dynamically created to manipulate that
12 type of value in a property list.
13
14 [source,sml]
15 ----
16 signature PROPERTY_LIST =
17 sig
18 type t
19
20 val new: unit -> t
21 val newProperty: unit -> {add: t * 'a -> unit,
22 peek: t -> 'a option}
23 end
24 ----
25
26 Here is a functor demonstrating the use of property lists. It first
27 creates a property list, then two new properties (of different types),
28 and adds a value to the list for each property.
29
30 [source,sml]
31 ----
32 functor Test (P: PROPERTY_LIST) =
33 struct
34 val pl = P.new ()
35
36 val {add = addInt: P.t * int -> unit, peek = peekInt} = P.newProperty ()
37 val {add = addReal: P.t * real -> unit, peek = peekReal} = P.newProperty ()
38
39 val () = addInt (pl, 13)
40 val () = addReal (pl, 17.0)
41 val s1 = Int.toString (valOf (peekInt pl))
42 val s2 = Real.toString (valOf (peekReal pl))
43 val () = print (concat [s1, " ", s2, "\n"])
44 end
45 ----
46
47 Applied to an appropriate implementation `PROPERTY_LIST`, the `Test`
48 functor will produce the following output.
49
50 ----
51 13 17.0
52 ----
53
54
55 == Implementation
56
57 Because property lists can hold values of any type, their
58 implementation requires a <<UniversalType#>>. Given that, a property
59 list is simply a list of elements of the universal type. Adding a
60 property adds to the front of the list, and looking up a property
61 scans the list.
62
63 [source,sml]
64 ----
65 functor PropertyList (U: UNIVERSAL_TYPE): PROPERTY_LIST =
66 struct
67 datatype t = T of U.t list ref
68
69 fun new () = T (ref [])
70
71 fun 'a newProperty () =
72 let
73 val (inject, out) = U.embed ()
74 fun add (T r, a: 'a): unit = r := inject a :: (!r)
75 fun peek (T r) =
76 Option.map (valOf o out) (List.find (isSome o out) (!r))
77 in
78 {add = add, peek = peek}
79 end
80 end
81 ----
82
83
84 If `U: UNIVERSAL_TYPE`, then we can test our code as follows.
85
86 [source,sml]
87 ----
88 structure Z = Test (PropertyList (U))
89 ----
90
91 Of course, a serious implementation of property lists would have to
92 handle duplicate insertions of the same property, as well as the
93 removal of elements in order to avoid space leaks.
94
95 == Also see
96
97 * MLton relies heavily on property lists for attaching information to
98 syntax tree nodes in its intermediate languages. See
99 https://github.com/MLton/mlton/blob/master/lib/mlton/basic/property-list.sig[`property-list.sig`] and
100 https://github.com/MLton/mlton/blob/master/lib/mlton/basic/property-list.fun[`property-list.fun`].
101
102 * The <<MLRISCLibrary#>> <<References#LeungGeorge99,uses property lists
103 extensively>>.