A query statement is the launching point for TAQ execution. A good query name reflects the purpose of the query. A query declaration defines one or more logical evaluation stages that must be successfully traversed for a solution to be returned by a query. There are a number of ways a solution can be provided depending on the data type and whether it is expected to be a single item or a list. The most common solution format encountered in the reference is for a query to return an axiom list. This is specified in the query declaration by appending to the query keyword, the axiom list type notation <axiom>,. high_cities.taq declares it returns an axiom list solution with this declaration
Query Stages
A query stage is shaped by the facts, in the form of axioms, it is fed and the new facts, also in the form of axioms, it is expected to produce. The agent for performing this operation is the template which absorbs the incoming axioms through unification and then, if the unification succeeds, evaluates to either create a solution or quit due to a failed test of a logical condition (short circuit).
Normally, a query stage is expected to exhaustively consume all of it's input to find all possible solutions. This is a "logical"" stage. However, another mode of operation is for a stage to complete upon the first solution found. This is a ""chaining" stage and it optimizes the case where it is known in advance that only one solution is possible. This is how a chaining stage differs from a logical one
- A right arrow -> is used to link the stage to the query
- All the following stages, if any, must be chaining stages too
- It is permitted to name the template to process the stage without pairing it to an axiom source with a colon
Logical Stages Example
customer-charge.taq has an example of a query consisting of 2 logical stages.
With multiple logical stages, all are enclosed in parentheses and comma-delimited. The ""customer_delivery query is said to 'cascade' from left to right as it finds every charge and customer combination belonging to the same city. Here is the code in full:
template customer_freight
(
city ? freight.city,
charge = freight.charge
query<axiom> customer_delivery(shipping:freight,
axiom list shipping (city, charge)
{"Sparta", 13.99 }
{"Milos", 17.99 }
axiom list customer (name, city)
{"Acropolis Construction", "Athens"}
{"Agora Imports", "Sparta"}
{"Spiros Theodolites", "Milos"}