Template-Axiom-Query (TAQ)

Queries

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<axiom> high_cities (city_altitude : high_city)

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.

query<axiom> customer_delivery(shipping:freight,
customer:customer_freight)

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 freight(charge, city)
template customer_freight
(
name,
city ? freight.city,
charge = freight.charge
)

query<axiom> customer_delivery(shipping:freight,
customer:customer_freight)

axiom list shipping (city, charge)
{"Athens", 23.99 }
{"Sparta", 13.99 }
{"Milos", 17.99 }

axiom list customer (name, city)
{"Marathon Marble", "Sparta"}
{"Acropolis Construction", "Athens"}
{"Agora Imports", "Sparta"}
{"Spiros Theodolites", "Milos"}

The solution is

customer_freight(
 name=Acropolis Construction, city=Athens, charge=23.99
 name=Marathon Marble, city=Sparta, charge=13.99
 name=Agora Imports, city=Sparta, charge=13.99
 name=Spiros Theodolites, city=Milos, charge=17.99
)
Chaining Stages Example

logic-chain.taq uses chaining stages to add payment and freight charges to customer account details.

template customer(name, city, code)
template account(code ? customer.code, fee)
template delivery(name = customer.name, city ? customer.city,
fee = account.fee, freight)

query<axiom> greek_business(customer:customer)
-> (code_fee:account) -> (shipping:delivery)

axiom list customer()
{"Marathon Marble", "Sparta", "MRT"}
{"Acropolis Construction", "Athens", "ACR"}
{"Agora Imports", "Sparta", "AGR"}
{"Spiros Theodolites", "Milos", "SPT"}

axiom list code_fee (code, fee)
{"MRT", 61}
{"ACR", 47}
{"AGR", 49}
{"SPT", 57}

axiom list shipping (city, freight)
{"Athens", 5 }
{"Sparta", 16 }
{"Milos", 22}

The solution is

delivery(
 name=Marathon Marble, city=Sparta, fee=61, freight=16
 name=Acropolis Construction, city=Athens, fee=47, freight=5
 name=Agora Imports, city=Sparta, fee=49, freight=16
 name=Spiros Theodolites, city=Milos, fee=57, freight=22
)

Query Parameters

A query can be parameterized to expose important values on which the query depends or can be customized. The parameters are set using key/value pairs appended to the query declaration and enclosed in parentheses. high_cities2.taq sets the threshold elevation for a high city as a parameter

template high_city(city ? altitude > threshold, altitude)

query<axiom> high_cities (city_altitude : high_city)
(threshold=5000)

Note that query parameters can be the sole input to a query, in which case the solution can only consist of a single axiom.