|
Terms
Updated 09 Oct 2002
|
Upper levels: - Amzi Python Interface 0.2... - 4. Manual |
|
4.3. Terms |
[ - - ] |
Prolog terms are wrapped in the Python class AmziTerm, which is the output of the various query methods, and can also be built and manipulated directly from Python. |
|
Index |
|
Building |
[ Top ] |
|
Terms are returned by the query methods discussed in 'Calling Prolog from Python', and can also be assembled by the engine methods .strTerm() and pyTerm:
|
|
Conversion to Python |
[ Top ] |
|
AmziTerms print out as a string, and numerical types can be coerced to their corresponding Python type, e.g. term = strTerm(2.3) f = float(term) i = int(float(term)) |
|
Accessing Members |
[ Top ] |
|
AmziTerm members can be accessed by indexes, 0 for the functor, etc., as illustrated in 'Basic Ideas and Use' Tuples of their members can also be gotten by slicing. So after executing:
term = eng.strTerm('gave(john,mary,book)')
|
|
Unification |
[ Top ] |
|
Two terms can be unified with a term-method unify: term1.unify(term2)
term1.unify(('likes',None,('lizards',))
But to unify in parts of term, it's better to use item and slice-assignment"
term1[2] = ('lizards',)
term2[1:3] = ('bill',),('snakes',)
A wart is that in Python, assignment is meant to succeed, unless there's a serious error, so there's no built-in way to signal a benign failure, as when the terms just aren't unifiable. So when this happens with the item/slice methods, the esception AmziProlog.FailedUni is thrown, which can be caught with the try/except construction. (example in tests.py). Some examples are in test.py in the Tests folder. It might be cool to be able to do unification by assignment, e.g.: term1 = term2 |
|
|
[ - Top - ] |