Expression Pattern Language (eXPL)

Calculator

Calculator is a template with additional features to allow procedural programming operations:

  • Procedural instructions
  • Call parameters
  • Branching
  • Looping
  • Function calls

These features are incompatible with the logic programming paradigm, so when a calculator is used in a query, it performs just one unification/evaluation step before either passing on the solution or triggering a short circuit.

Declaration Format

A calculator declaration starts with keyword calc. What follows can be everything defined for a template, plus procedural options placed in term locations. Parameters can also be specified. These are applied by unification prior to calculator execution.

Query Format

In the query format, calculator and template are interchangeable. However, placing a calculator at the head of the query, paired to an axiom collection, causes the eXPL runtime to insert a logic query in front of the calculator to sequentially transfer the axiom collection to it. A calculator is also permitted to have parameters. The parameters are placed within perentheses immediately following the calculator.

Simple Calculator Example

The CalculateSquareMiles application of tutorial8 takes a list of British Commonwealth countries and their surface areas and converts it to a list where areas are in square miles instead of square kilometers. A total of all the areas is also calculated. A calcluator nameed "km2_to_mi2" does tne conversion:

calc km2_to_mi2
+ export list<term> totals(totals);
(
  country,
  double surface_area_mi2 = surface_area_Km2 *= 0.3861,
. totals->total_area += surface_area_mi2
);

The total surface area is exported in a term list as only the final aggregated value is required. The total surface area term is set to integer type using a macro, otherwise the type ends up as double, which is not very readable when displayed as a string value:

axiom totals (total_area) { integer(0) };
Head Query

The first of four queries provided with the example places the calculator at the head of the query paired with the British Commonwealth surface areas axiom collection:

query<axiom> convert(surface_area : km2_to_mi2);

This spans the entire collection even though the calculator does not cascade. The trick is the runtime converts the query to the format demonstrated in the next query.

Chain Query

The next example chains the calculator to a cascading template:

template country_data(country, double surface_area_Km2);

query<axiom> surface_area_mi2(surface_area : country_data)
  -> (km2_to_mi2);

The template layout mirrors that of the axioms in the collection, which is why automated generation is possible. To run this query, use a selection value of "2" as an application argument. Note that in the query, the calculator is not paired to an axiom collection as it simply unifies with the solution of the previous query step.

Cascade Query

A calculator can be linked into a cascade sequence as the next query demonstrates. The outcome is no different from the previous chain query as the calculator behaves the same way:

query<axiom> cascade(surface_area : country_data, km2_to_mi2);

To run this query, use selection value "3". Note that if a calculator is paired with an axiom collection, it just unifies with the first axiom of the collection before each evaluation.

Query Parameters

The next query takes it's input from parameters, which specify a non-British Commonwealth nation:

query<axiom> convert_afghan(km2_to_mi2)
(country = "Afghanistan", surface_area_Km2 = "652,230.00");

To run this query, use selection value "4".