Evaluation in eXPL also entails unification as variables are used in expressions and unification populates variables prior to evaluation. The following script provides a simple example of this, where the top 10 Asian cities are found from a set of facts about the highest populated cities of the world:
// {1,"Tokyo","Japan","Asia",37900000}
// {2,"Delhi","India","Asia",26580000}...
include "mega_city.xpl";
integer count = 0;
template asia_top_ten (
Megacity ? Continent == "Asia" && count++ < 10,
Country, Population);
query asia_top_ten (mega_city : asia_top_ten);
Notice the short curcuit statement in the second term of the "asia_top_ten" template:
Here the "Continent" variable is used in an expression and populated by the unification with the "Continent" term in the "mega_city" axiom. Here is the solution:
Megacity = Delhi, Country = India, Population = 26580000
Megacity = Seoul, Country = South,Korea, Population = 26100000
Megacity = Shanghai, Country = China, Population = 25400000
Megacity = Mumbai, Country = India, Population = 23920000
Megacity = Beijing, Country = China, Population = 21650000
Megacity = Jakarta, Country = Indonesia, Population = 20500000
Megacity = Karachi, Country = Pakistan, Population = 20290000
Megacity = Osaka, Country = Japan, Population = 20260000
Megacity = Manila, Country = Philippines, Population = 20040000
In the Megacity example, we can also see an aggregation variable "count" which counts the number of Asian cities in the solution. The count variable is declared and set to zero before it is used in the template. A variable can be used in a similar manner to produce averages, counts and other aggregation values.
The tutorial5 MegaCities code example runs this query.
