next up previous index
Next: Calling Prolog from Up: Calling Tcl from Previous: Passing Prolog Variables

Returning Values to Prolog

The previous two predicates invoke a Tcl command from Prolog, but return no value. The predicate tcl/3   is the most general interface predicate:

tcl(CommandTemplate, VarList, ReturnValue).
It inserts all variables from the VarList into the CommandTemplate, calls the resulting Tcl command and its return value is unified with ReturnValue. For example, we can modify the square/3 predicate so that it returns the ID of the created square, which can then be used to move it around:
square(X, Y, Colour, ID) :-
    Size = 10,
    X1 is X + Size,
    Y1 is Y + Size,
    tcl('.c create rectangle ## ## ## ## -fill ##', [X, Y, X1, Y1, Colour], ID).

move_square(ID, Xof, Yof) :-
    tcl('.c move ## ## ##', [ID, Xof, Yof]).

If we initialise the display like in the previous example,

[eclipse 30]: tk([]), tcl 'canvas .c -width 100 -height 80; pack .c'.

yes.
[eclipse 31]: square(10, 20, blue, S1), square(30, 50, yellow, S2),
        square(60, 25, red, S3).

S1 = 1
S2 = 2
S3 = 3
yes.

and then move the first and second square

[eclipse 32]: move_square(1, 20, -10), move_square(2, 40, -30).
we see that the squares have indeed changed their positions:



Micha Meier
Tue Jul 2 09:49:39 MET DST 1996