Expression Pattern Language (eXPL)

eXPL Calculator

Calculator processes information to shape the solution to meet specific requirements. Calculator is a template with additional evaluation features. Calculator can take parameters, implement algorithms, and control flow of execution. Calculator can also execute queries. A calculator can be chained to another query to transform the results or generate aggregrate values.

Declaration Format

Calculator and template are similar in format. The main difference is that a calculator declaration starts with keyword calc, while a template declaration starts with template. However, as the role of calculator is to generate information while template has the role of establishing facts. Therefore calculator is capable of doing far more than template. Most of the additional functionality concerns what can be placed inside a template, but unique to calculator is the ability to include parameters in the declaration.

Calculator variables can be set using parameters passed in the declaration by appending a parameter list to it. Here is an example where parameter "factorial" is set to 1 and "i" is set to 1 too:

calc factorial (
...
)(factorial = 1, i = 1);

A reason for setting parameters in the declaration may be to easily allow adjustments to be made at a later time. Note that parameter initialization overrides variable initialization inside the calculator.

Query Format

The way a calculator is referenced in a query resembles that of template. A calculator query can be used on it's own or chained the same as a Logic Query using ">>" notation.

( [ axiom-name : ] calculator-name [ ( parameter-list ) ] ) [ >> ... ]

The axion-name part is optional, and the query proceeds directly to evaluation when the axiom-name is not present. Also, when an axiom name appears in a calculator chain, only the first axiom from the source is unified with the calculator in a single step before proceeding down the chain.

A calculator may take values from the solution or is self-contained, hence both axiom-name and parameter-list are optional. Independent calculator examples are counting or performing some other aggregation operation.

Evaluation Sequences

An evaluation sequence is a sequence of terms and statements enclosed in braces {}. An unconditional sequence starts with the leading brace and executes in a loop expecting at least one short curcuit to cause an exit from the loop. Here is an example of an unconditional loop to calculate the factorial of a number:

{
  factorial *= i,
  ? i++ < n
}

This sequence iterates to calculate the factorial and exits the loop when i++ < n evaluates to false.

A conditional sequence starts with a short circuit operator and boolean expression before the leading brace. Execution completes at the end of the sequence unless a short circuit occurs beforehand. Here is an example of an conditional sequence which adjusts surface area from km2 to square miles if boolen variable "imperial" is true:

? imperial
{
  surface_area *= 0.3861
}

Conversion Example

This script takes a list of countrys and surface areas in km2 and converts each surface area to square miles. The calculator, named "km2_to_mi2" is appended as a chain query showing that the results of an existing query can easily be adapted without touching it.

include "surface-land.xpl";
template surface_area(country, double surface_area_Km2);
calc km2_to_mi2 (country, double surface_area_mi2 =
  surface_area.surface_area_Km2 *= 0.3861);
list surface_area(km2_to_mi2);
query surface_area_mi2(surface_area : surface_area)
>> (km2_to_mi2);

The tutorial9 CalculateSquareMiles code example runs this query. The calculator results are output to a Axiom list named "surface_area". The solution when the query is run can be compared to the input to show all surface areas are reduced by the correct factor.