Template-Axiom-Query (TAQ)

Cursor

Cursor provides iterative access to a list progressing in either the forward or reverse direction. There are a number of operations a cursor can perform, summarized as follows:

Postfix 
++Step forward
--Step backward
Prefix 
+Move to first position
-Move to last position
Expression 
cursorReference item at current position
cursor[n]Reference item at relative position n
cursor = nMove to specified position n
cursor += nMove to relative position n
fact cursortrue if position is in range
cursor.index()Current position of cursor

Notice that cursor[0] is identical to just cursor. In addition, if the cursor is bound to an axiom list, then cursor-> can reference terms of the axion at the current position by name.

Format

There are two types of cursor reflecting how the binding to a list occurs:

  • Closed - the list is bound by unification to a list with the same name as the cursor
  • Open - the bound list is referenced by qualified name

The closed type hides the bound list so it can only be accessed via the cursor. The open type type allows the list to be modified independently of the cursor. Both cursor types can be declared in template scope, but a closed cursor can also be declared in a flow body.

Open Cursor

An open cursor is always declared in template scope. The bound list name is a single identifier which is resolved to a fully qualified name by performing a list search. The cursor may optionally be assigned a type if type conversion is desired, for example, decimal to currency:

[ reverse ] cursor [ <type> ] name(list-name)

The the cursor by default points th the first item in the list in preparation for stepping forward. A direction keyword of 'reverse' can be prepended so the cursor points to the last item in the list in preparation for stepping backward. Note that the list-name can be appended with "@scope to reference a context list meaning there is expected to be a list with the same name in every scope that the cursor is used.

Closed Cursor

The closed cursor format is similar to the open one, but there is no list name and specifying list type is mandatory. Type conversion is also not available, The cursor and list must have the same item type.

cursor <type> name [ reverse ]

A closed cursor appeared in personalities2.taq

cursor<string> traits

This cursor needs to access one of 12 zodiac lists. For example, "gemini_traits" are "gentle", "affectionate", "curious". Cursor binding by unification is possible because every zodiac list is referenced from an axiom term simply named "traits".

Pet Names Example

pet-names.taq uses an open cursor named "pet" to iterate through a list of pet details to export pets' names as a list. There are two queries, the second of which lists the names in reverse. This is the the forward cursor declaration

cursor pet(pets_info)

and this is the reverse declaration

reverse cursor pet(pets_info)

The forward cursor is incremented pet++ and the reverse cursor is decremented pet--

Type Conversion

An open cursor may be assigned a type so that it performs type conversion when there is a type mismatch. currency-cursor.taq declares a currency cursor thus

cursor<currency.EUR> euro_amount(euro_amounts)

Assigning a currency type to the cursor allows it to convert a text Euro amount to decimal. The conversion takes place seamlessly as a variable declared as "currency total" is updated using the cursor.

total += euro_amount

The following line appends appends the Euro amount to a list declared as "list<currency> amount_list"

amount_list += euro_amount++