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:
+ 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:
