<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head> <meta name="robots" content="index,nofollow"> <title>PropertyList - MLton Standard ML Compiler (SML Compiler)</title> <link rel="stylesheet" type="text/css" charset="iso-8859-1" media="all" href="common.css"> <link rel="stylesheet" type="text/css" charset="iso-8859-1" media="screen" href="screen.css"> <link rel="stylesheet" type="text/css" charset="iso-8859-1" media="print" href="print.css"> <link rel="Start" href="Home"> </head> <body lang="en" dir="ltr"> <script src="http://www.google-analytics.com/urchin.js" type="text/javascript"> </script> <script type="text/javascript"> _uacct = "UA-833377-1"; urchinTracker(); </script> <table bgcolor = lightblue cellspacing = 0 style = "border: 0px;" width = 100%> <tr> <td style = " border: 0px; color: darkblue; font-size: 150%; text-align: left;"> <a class = mltona href="Home">MLton MLTONWIKIVERSION</a> <td style = " border: 0px; font-size: 150%; text-align: center; width: 50%;"> PropertyList <td style = " border: 0px; text-align: right;"> <table cellspacing = 0 style = "border: 0px"> <tr style = "vertical-align: middle;"> </table> <tr style = "background-color: white;"> <td colspan = 3 style = " border: 0px; font-size:70%; text-align: right;"> <a href = "Home">Home</a> <a href = "TitleIndex">Index</a> </table> <div id="content" lang="en" dir="ltr"> A property list is a dictionary-like data structure into which properties (name-value pairs) can be inserted and from which properties can be looked up by name. The term comes from the Lisp language, where every symbol has a property list for storing information, and where the names are typically symbols and keys can be any type of value. <p> Here is an SML signature for property lists such that for any type of value a new property can be dynamically created to manipulate that type of value in a property list. </p> <pre class=code> <B><FONT COLOR="#0000FF">signature</FONT></B> PROPERTY_LIST = <B><FONT COLOR="#0000FF">sig</FONT></B> <B><FONT COLOR="#A020F0">type</FONT></B><B><FONT COLOR="#228B22"> t </FONT></B><B><FONT COLOR="#A020F0">val</FONT></B> new: unit -> t <B><FONT COLOR="#A020F0">val</FONT></B> newProperty: unit -> {add: t * 'a -> unit, peek: t -> 'a option} <B><FONT COLOR="#0000FF">end</FONT></B> </PRE> <p> </p> <p> Here is a functor demonstrating the use of property lists. It first creates a property list, then two new properties (of different types), and adds a value to the list for each property. </p> <pre class=code> <B><FONT COLOR="#0000FF">functor</FONT></B> Test (P: PROPERTY_LIST) = <B><FONT COLOR="#0000FF">struct</FONT></B> <B><FONT COLOR="#A020F0">val</FONT></B> pl = P.new () <B><FONT COLOR="#A020F0">val</FONT></B> {add = addInt: P.t * int -> unit, peek = peekInt} = P.newProperty () <B><FONT COLOR="#A020F0">val</FONT></B> {add = addReal: P.t * real -> unit, peek = peekReal} = P.newProperty () <B><FONT COLOR="#A020F0">val</FONT></B> () = addInt (pl, <B><FONT COLOR="#5F9EA0">13</FONT></B>) <B><FONT COLOR="#A020F0">val</FONT></B> () = addReal (pl, <B><FONT COLOR="#5F9EA0">17.0</FONT></B>) <B><FONT COLOR="#A020F0">val</FONT></B> s1 = Int.toString (valOf (peekInt pl)) <B><FONT COLOR="#A020F0">val</FONT></B> s2 = Real.toString (valOf (peekReal pl)) <B><FONT COLOR="#A020F0">val</FONT></B> () = print (concat [s1, <B><FONT COLOR="#BC8F8F">" "</FONT></B>, s2, <B><FONT COLOR="#BC8F8F">"\n"</FONT></B>]) <B><FONT COLOR="#0000FF">end</FONT></B> </PRE> <p> </p> <p> Applied to an appropriate implementation <tt>PROPERTY_LIST</tt>, the <tt>Test</tt> functor will produce the following output. <pre>13 17.0 </pre> </p> <h2 id="head-8781d615fd77be9578225c40ac67b9471394cced">Implementation</h2> <p> Because property lists can hold values of any type, their implementation requires a <a href="UniversalType">UniversalType</a>. Given that, a property list is simply a list of elements of the universal type. Adding a property adds to the front of the list, and looking up a property scans the list. </p> <pre class=code> <B><FONT COLOR="#0000FF">functor</FONT></B> PropertyList (U: UNIVERSAL_TYPE): PROPERTY_LIST = <B><FONT COLOR="#0000FF">struct</FONT></B> <B><FONT COLOR="#A020F0">datatype</FONT></B><B><FONT COLOR="#228B22"> t </FONT></B>=<B><FONT COLOR="#228B22"> <FONT COLOR="#B8860B">T</FONT> <B><FONT COLOR="#A020F0">of</FONT></B> U.t list ref </FONT></B><B><FONT COLOR="#A020F0">fun</FONT></B> new () = T (ref []) <B><FONT COLOR="#A020F0">fun</FONT></B> <B><FONT COLOR="#228B22">'a</FONT></B> newProperty () = <B><FONT COLOR="#A020F0">let</FONT></B> <B><FONT COLOR="#A020F0">val</FONT></B> (inject, out) = U.embed () <B><FONT COLOR="#A020F0">fun</FONT></B> add (T r, a: 'a): unit = r := inject a :: (!r) <B><FONT COLOR="#A020F0">fun</FONT></B> peek (T r) = Option.map (valOf o out) (List.find (isSome o out) (!r)) <B><FONT COLOR="#A020F0">in</FONT></B> {add = add, peek = peek} <B><FONT COLOR="#A020F0">end</FONT></B> <B><FONT COLOR="#0000FF">end</FONT></B> </PRE> <p> </p> <p> If <tt>U: UNIVERSAL_TYPE</tt>, then we can test our code as follows. <pre class=code> <B><FONT COLOR="#0000FF">structure</FONT></B> Z = Test (PropertyList (U)) </PRE> </p> <p> Of course, a serious implementation of property lists would have to handle duplicate insertions of the same property, as well as the removal of elements in order to avoid space leaks. </p> <h2 id="head-a4bc8bf5caf54b18cea9f58e83dd4acb488deb17">Also see</h2> <p> MLton relies heavily on property lists for attaching information to syntax tree nodes in its intermediate languages. See <a href = "http://mlton.org/cgi-bin/viewsvn.cgi/mlton/tags/on-MLTONWIKIVERSION-release/lib/mlton/basic/property-list.sig?view=markup"><img src="moin-www.png" alt="[WWW]" height="11" width="11">property-list.sig</a> <a href = "http://mlton.org/cgi-bin/viewsvn.cgi/mlton/tags/on-MLTONWIKIVERSION-release/lib/mlton/basic/property-list.fun?view=markup"><img src="moin-www.png" alt="[WWW]" height="11" width="11">property-list.fun</a>. </p> <p> The <a href="MLRISCLibrary">MLRISCLibrary</a> <a href = "References#LeungGeorge98">uses property lists extensively</a>. </p> </div> <p> <hr> Last edited on 2010-03-02 15:17:55 by <span title="fenrir.cs.rit.edu"><a href="MatthewFluet">MatthewFluet</a></span>. </body></html>