<slides>
<title>XML challenges to programming language design</title>
<slide id="splash">
<caption>XML challenges to programming language design</caption>
<h2>Per Bothner</h2>
<h3><code>&lt;per@bothner.com&gt;</code></h3>
<h5>June 2004</h5>
</slide>

<slide id="intro">
<caption>What this talk is about</caption>
<ul>
<li>This talk does not describe a language&#8212;yet.</li>
<li>It describes ideas that I think might be useful.</li>
<li>Some of these are inspired by XML applications and tools.</li>
<li>Some inspired by XQuery.</li>
<li>Some ideas have been implemented in <strong>q2</strong>.</li>
</ul>
</slide>

<slide id="elements">
<caption>Constructing XML Elements</caption>
<ul>
<li>XSLT and JSP use XML syntax for both
logic and creating XML. (Verbose, hard-to-read.)</li>
<li>
XQuery uses functional syntax for logic,
and XML syntax for element construction. (Less integrated.)</li>
<li>
In the Lisp world, we can use S-expressions (SXML).
(No dynamic validation or static typing.)</li>
<li>LAML maps each HTML tag to a <q>mirror function</q> with the same name.</li>
<li>
Calling a mirror function generates a <q>node</q> object.</li>
</ul>
</slide>

<slide id="constructors">
<caption>Constructor functions</caption>
<ul>
<li>We create elements using <dfn>constructor</dfn> functions.</li>
<li>Constructor parameters becomes child nodes.</li>
<li>Should use the same syntax and mechanism as for
creating objects.</li>
<li>Should avoid noise words like <tt>make</tt> or <tt>new</tt>.</li>
<li>Short-hand syntax for defining new constructors functions:
<pre>
(define-element html (head body))
(define-element body any) ;; if lazy
</pre>
</li>
<li>
Need tool to create constuctor functions from DTD or Schema.</li>
</ul>
</slide>

<slide id="Namespaces">
<caption>Namespaces</caption>
<ul>
<li>XML namespaces are similar to Common Lisp packages.</li>
<li>Even uses same syntax: <tt>prefix:local-name</tt></li>
<li>However, namespace is just a URL string.</li>
<li>Namespace declarations map <tt>prefix</tt> to a URL.</li>
<li>Namespace declarations can be lexically nested<br />
(hence must be done <em>after</em> read-time).</li>
<pre>
(define-namespace html "some-unique-string")
(html:html (html:body (html:p "Hello")))
</pre>
</ul>
</slide>

<slide id="Attributes">
<caption>Attributes</caption>
<ul>
<li>XML elements have have named attributes.</li>
<li>Specify attributes using keyword parameters to constructor functions.</li>
<pre>
(html:a href: "http://gnu.org/" "GNU")
</pre>
<li>Attributes can be arbitrary expressions.</li>
</ul>
</slide>

<slide id="Sequence">
<caption>Sequences as multiple values</caption>
<ul>
<li>An XQuery expression returns a <dfn>sequence</dfn>.</li>
<li>A sequence is zero or more values.</li>
<li>Sequences do not nest.</li>
<li>A one-value sequence is the same as that value.</li>
<li>Sequences are first-class, but don't have identity.</li>
<li>Similar to Lisp/Scheme <dfn>multiple values</dfn>.</li>
</ul>
</slide>

<slide id="Sequence-advantages">
<caption>Sequences versus lists</caption>
<ul>
<li>
Non-nesting sequences handle common case easier:<br />
<tt>list</tt>, <tt>cons</tt>, <tt>append</tt> are combined.</li>
<li>
Non-nesting sequences may be easier for beginners.<br />
<it>E.g.</it> a char and a one-char string are the same.</li>
<li>
Symmetry between parameter list and result sequence.</li>
<li>
Integrates expressions and <q>statements</q>.</li>
</ul>
</slide>

<slide id="Arrays">
<caption>Arrays</caption>
<ul>
<li>Since sequences don't nest, we need something that does.</li>
<li>An array maps integer keys to a value.</li>
<li>An array is like a function, and is indexed using function call notation.</li>
<li>Note that a string is a character sequence, not a vector.</li>
</ul>
</slide>

<slide id="Statements">
<caption>Expressions and Statements</caption>
<ul>
<li>A Scheme <i>&lt;body&gt;</i> is one or more expresions.<br />
Its value is that the of last expression.</li>
<li>
Instead, make value of <i>&lt;body&gt;</i> be concatenation of
values of sub-expressions.</li>
<li>
Define <tt>define</tt>, <tt>set!</tt> etc to return zero values.</li>
<li>
If we really need to discard result, use explicit <tt>discard</tt>.</li>
</ul>
</slide>

<slide id="Comprehensions">
<caption>Sequence Comprehensions</caption>
<ul>
<pre>
(define x
  (let r ((i 0)) ;; R5RS "named let"
    (+ 100 i)
    (if (&lt; i 5)
      (r (+ i 1)))
    i))
</pre>
<p>evaluates to:</p>
<pre>
100 101 102 103 104 105 5 4 3 2 1 0
</pre>
<li>Add extra constructs to iterate over sequences.</li>
</ul>
</slide>

<slide id="Graphics">
<caption>Application: GUI constructors</caption>
<ul>
<li>Mozilla's XUL and Microsoft's XAML let you lay out
GUI applications using XML to describe nested widgets.</li>
<li>Easy to use, less verbose than Java/C#.</li>
<li>Integrates poorly with event handlers, awkward repetition.</li>
<li>Attributes and children are literal, not calculated.</li>
<li>Element constructors have same benefits and solve problems.</li>
</ul>
</slide>

<slide id="Buttons">
<caption>Example: a button bar</caption>
<ul>
<li>A row of 10 buttons, one for each digit.</li>
<pre>
(do ((i 1 (+ i 1)))
    ((&gt; i 10))
  (button
     label: (number->string i)
     onpress: (lambda () (handle-digit i))))
</pre>
</ul>
</slide>

<!--
<slide id="Concatenation">
<caption>Implicit concatenation</caption>
</slide>
-->

<slide id="Functions-and-Patterns">
<caption>Functions and Patterns</caption>
<ul>
<li>In XQuery each parameter is a sequence.</li>
<li>Suggest instead: Parameter list is a sequence.</li>
<li>Combines <tt>apply</tt> and <tt>call-with-values</tt>.</li>
<li>Needs syntax to take apart parameter sequence.</li>
<li>Using patterns (as in ML etc) is convenient.</li>
<li>Seems promising: see XDuce/CDuce/CQL family.</li>
</ul>
</slide>

<slide id="Indentation">
<caption>Indentation</caption>
<ul>
<li>Nice to use parentheses for grouping rather than application,
as in ML etc.</li>
<li>Also nice to use juxtaposition for sequence concatenation;
avoid noise syntax in input not in output.</li>
<table><tr><td>
<pre>
fun1 a b c
       fun2 d e
       fun3 f
</pre></td>
<td align="center" width="50"> --> </td>
<td>
<pre>
(fun1 a (b c)
        (fun2 d e)
        (fun3 f))
</pre>
</td></tr></table>
</ul>
</slide>

</slides>
