Skip to content

2.5 Predicate Engine in Tamgu (en)

Claude Roux edited this page Oct 19, 2022 · 1 revision

Tamgu's predicates

Version française

We have integrated into Tamgu a real predicate engine, close to Prolog. Not only is it now possible to call Prolog code from Tamgu, but it is also possible to call Tamgu code from a Prolog clause.

Some differences

If Tamgu's Prolog is quite close to the Edinburgh syntax, there are some minor differences. In particular, the notion of atoms and variables has been slightly modified. First of all, Tamgu does not know the notion of atoms, which are replaced here by Tamgu strings. Then, the variable names are replaced by a canonical form: ?X, a little like SPARQL.

Thus the clause: parent(mary, john) is replaced by parent ("mary", "john").

In the same way, the clause: parent(Mother,Son) where the presence of a capital letter indicates that Mother and Son are variables is replaced by parent(?Mother,?Son).

Variables

The presence of "?" at the beginning of the variable allows differentiating between purely Prolog variables and Tamgu variables.

In particular, Tamgu accepts the following code:

string father = "Jean";
parent (father, "Pierre").

vector v = parent(?X,?Y);

v is now [parent ("Jean", "Pierre")]

Objects

Predicates can work with any types of object. For some of them, such as vectors or terms, the unification mechanism has been modified so that their processing is specifically Prolog. On the other hand, for the other objects, unification is reduced to a simple comparison of equality.

Calling a Prolog clause

There are different ways to call Prolog from Tamgu, depending on whether you want a single or a complete evaluation. Simply change the receiving variable to tell Prolog how to evaluate an expression.

If the receiving variable is a Boolean then Tamgu will stop at the first complete unification. If the variable is a container, Tamgu will perform all the evaluations.

DCG (Definite Clause Grammar)

A DCG module is also available. DCG clauses are automatically translated into Prolog clauses.

sentence --> nominal_group, verbal_group.
nominal_group --> det, noun.
verbal_group --> verb, adjective.

det --> ["the"].
det --> ["one"].

noun --> ["flat"].

adjective --> ["red"].
verb --> ["is"].

vector v=sentence(?X,[]);

Calling Tamgu from Prolog

It is also possible to call Tamgu from a Prolog clause:

grandparent(?X,?Y): - parent(?X,?Z), println(?Z), parent(?Z,?Y).

In the above case, println is called in the clause and displays the content of the variable ?Z.

Using your own function

You can still define your own function. This function must return True or False; if it returns False, it will cause the evaluation of the clause to fail.

function tst(int i, int j) {
   if (i > j)
       return true;
  return false;
}

check(?X,?Y):- tst(?X,?Y), trick(?X,?Y).

In the above case, if ?X is less than ?Y then check will fail.

Clone this wiki locally