-
Notifications
You must be signed in to change notification settings - Fork 13
2.5 Predicate Engine in Tamgu (en)
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.
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).
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")]
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.
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.
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,[]);
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.
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.