Declarative Sorting

Steven Pemberton, CWI, Amsterdam

Abstract

XForms is a declarative programming language for writing applications for the web and elsewhere.

One of the strengths of the language is how data is described: types, constraints, and relationships between values declaratively describe the structure, validity, and interconnection of data, as global properties.

A central part of this is invariants that describe relationships between values, such that if a value changes or is changed, its related values specified in the invariants get updated automatically. This is much like how spreadsheets work, though more general. A major advantage of this approach is that much administrative work is taken out of the hands of the programmer, and left to the computer: the programmer specifies the relationships, the computer does the work.

Sorting data is one of the major tasks of many programs, and this is usually done programmatically: the data is, or becomes, unsorted, and a function is then called to order the data suitably.

On the other hand, being sorted is often just a necessary property of the data for the rest of the program to function properly, which could just be stated declaratively, leaving the work to ensure it remain sorted to the computer.

This paper discusses the issues involved, and how the question was approached when adding declarative sorting to XForms, as well as touching on implementation details and advantages.

Contents

Introduction

Declarative techniques are about getting things done by describing what you want, rather than how to get it.

For instance XForms [xf], which was first released in 2003 and is still being maintained, with two new implementations being announced this year, is a declarative programming language.

There are many examples of huge time and resource savings achieved by programming in XForms, such as John Chelsom's CityHCR health records system [CityEHR], that he rewrote single-handedly in XForms in 3 years, based on a failed 10 year/70 programmer Java-based system [Lorenzo].

XForms splits programs into two parts:

Data can have properties assigned, such as

Additionally, and most important, a value may be calculated from other values. Largely automatically, data values and properties get updated as necessary; this is like spreadsheets, but much more general.

The controls for inputting, altering, and displaying values are intent based: they specify the intention of the control, such as selecting a value from a list of possible values, while not requiring any particular presentation.

Controls are a two-way link with their bound data: they are automatically updated by the system as and when the data changes, and the data is changed as new values are input via controls.

For instance here are three different presentations of the same control that allows you to select a single value from a list:

[image]

Here is an example of a clock written in XForms in about 50 lines of code (compared to at least 1000 lines in procedural languages):

[clock]

Background: The ABC Programming Language

ABC [abc] is a procedural programming language designed at the CWI in the 80's, which later formed the basis of Python. It is still maintained and used -- the first implementation of ixml was programmed in it [ixampl] -- and a new 64 bit implementation with Unicode support will be released shortly.

ABC was designed in an unusual way (for programming languages): as an HCI product.

Programming languages are an interface between humans and computers, so it stands to reason that designing them should be based on HCI principles.

Part of the design included doing requirements analysis, and designing several different type systems to see which best supported the requirements.

One conclusion of that analysis was that while most programming languages are designed with low-level, machine-oriented types, and tools to allow you to build higher-level structures, what programmer's actually need in day-to-day life are the high-level tools, not the low-level ones. Out of that came the conclusion that sorting and searching are an essential part of programming and therefore should be primitives of the language, not something you have to build yourself.

Consequently, ABC ended up with a very simple, very high-level data architecture, with only 5 types:

On top of this, the language is strongly typed but without declarations: types are inferred from use.

The upshot of this analysis and design is that by default lists and tables are kept sorted:

Additionally, since it is central to the kernel of the language, it is really, really fast: ABC programs, which are interpreted, have been seen to run faster than equivalent compiled programs, simply because sorting and searching is so fast. And programming is much, much, easier (typically 10×).

Sorting in XForms

Goin back to XForms, there are two basic methods of sorting:

1. Keeping it sorted as you update the structure, for instance

<insert position="after"
ref="data[date >= new/date]" origin="new"/>

2. Using an XPath [xpath] sorting function to sort the whole structure when necessary.

Since being sorted is usually a required property of the data being used, and therefore a declarative property of data, the XForms Working Group decided to try and add declarative sorting to the language. The aim was to create a design that allowed you to specify that a particular structure was to be kept sorted, relieving you of any work.

The Design

The basic idea is to attach a property to entries, indicating that they must be kept sorted.

While the default is ascending order, you can also sort descending.

The sort attribute gives the expression that is used to specify order:

<bind ref="member" sort="name/family"/>
<bind ref="purchase" sort="date" direction="down"/>

Of course, you may want to sort on several fields; you then nest the properties:

<bind ref="entry" sort="date" direction="down">
    <bind sort="name"/>
    <bind sort="age"/>
</bind>

each bind is lower priority.

Insertions

What this means is that data with this property is always kept sorted.

Instead of

<insert position="after"
ref="data[date >= new/date]" origin="new"/>

you can now simply say

<insert ref="data" origin="new"/>

Controls

In XForms, a control inherits the properties of the data it is bound to. If its bound data is readonly, then so is the control.

A new feature of XForms 2.0 is that controls can add their own properties too, that only apply to the use of the data in the control, but do not propagate back to the data itself. For instance,

<output ref="result" relevant=". > 0"/>

In this case the relevance only applies to this control, and not to the data it is bound to. Thus the value will only be visible if the value is positive, but other controls using the same value may still be visible.

In a similar way, controls that bind to nodesets may now also carry sorting properties:

<repeat ref="member" sort="name">
   <output ref="name"/>
</repeat>

In this case the list of members will be output in name order, even if the data is unsorted, or sorted on another property such as date.

Collation

Whether something is sorted or not, depends on its 'collation', which is usually a property of how a culture or language sorts its values.

For example, in German "Ä" comes between "A" and "B", while in Swedish it comes after "Z". In fact, a culture may have more than one collation, such as in the Netherlands, where the character ij sorts with the i's in dictionaries, but with the y's in phone books.

While the default collation for XForms is just the order that characters come in Unicode, for other sorting orders, we adoped the solution straight out of XPath, a collation property that is a URI pointing to collation details, such as

http://www.w3.org/2005/xpath-functions/collation/
     html-ascii-case-insensitive

Advantages

The advantages of the declarative approach are

Computational advantage

If you don't know if a set of nodes are sorted, then updating or searching is O(n).

If you know they are ordered, then those operations are suddenly O(log(n)).

This is a big difference for large lists.

If you double the size of the list:

Put another way,

(See my talk at Balisage 2021) [ref]

Conclusion

Being sorted is typically just a required invariant of a set of data.

By recognising this as a declarative property, it makes programming considerably simpler.

References

[xf]

[CityEHR]

[Lorenzo] https://en.wikipedia.org/wiki/Lorenzo_(electronic_health_record)

https://www.theguardian.com/society/2013/sep/18/nhs-records-system-10bn