20 ALS Prolog - TclTk Interface

20.1 Introduction and Overview

The interface between ALS Prolog and Tcl/Tk allows prolog programs to create, manipulate and destroy Tcl/Tk interpreters, to submit Tcl/Tk expressions for evaluation in those interpreters, and to allow expressions being evaluated to make calls back into Prolog. Computed data can be passed in both directions:

20.2 Prolog to Tcl Type Conversion

20.3 Prolog to Tcl Interface Predicates

20.3.1 tcl_new(?Interpreter)


tcl_new/1 creates a new Tcl interpreter. If the Interpreter argument is an uninstantiated (Prolog) variable, then a unique name is generated for the interpreter. If Interpreter is a atom, the new Tcl interpreter is given that name.

tk_new/1 functions in the same manner as tcl_new/1, except that the newly created Tcl interpreter is initialized with theTk package.


tcl_new(i). Succeeds, creating a Tcl interpreter named i.
tcl_new(X). Succeeds, unifying X with the atom 'interp1'.


20.3.2 tcl_call(+Interpreter, +Script, ?Result)

tcl_eval(+Interpreter, +ArgList, ?Result)

tcl_call and tcl_eval both execute a script using the Tcl interpreter and returns the Tcl result in Result. tcl_call passes the Script argument as a single argument toTcl’s eval command. tcl_eval passes the elements of ArgList as arguments to the Tcl’s eval command, which concatenates the arguments before evaluating them.

tcl_call’s Script can take the following form:


tcl_call(i, [puts, abc], R). Prints 'abc' to standard output, and bind R to ''.
tcl_call(i, [set, x, 3.4], R). Sets the Tcl variable x to 3.4 and binds R to 3.4.
tcl_call(i, 'set x', R). Binds R to 3.4.
tcl_eval(i, ['if [file exists ', Name, '] puts file-found'], R).


20.3.3 tcl_coerce_number(+Interpreter, +Object, ?Number)

20.3.4 tcl_coerce_atom(+Interpreter, +Object, ?Atom)

tcl_coerce_list(+interpreter, +Object, ?List)

These three predicates convert the object Object to a specific Prolog type using the Tcl interpreter Interpreter. Object can be an number, atom or list. If the object is already the correct type, then it is simple bound to the output argument. If the object cannot be converted, an error is generated.

tcl_coerce_number(i, ' 1.3', N) Succeeds, binding N to the float 1.3
tcl_coerce_number(i, 1.3, N) Succeeds, binding N to the float 1.3
tcl_coerce_number(i, 'abc', N) Generates an error.
tcl_coerce_atom(i, [a, b, c], A) Succeeds, binding A to 'a b c'
tcl_coerce_atom(i, 1.4, A) Succeeds, binding A to '1.4'
tcl_coerce_list(i, 'a b c', L) Succeeds, binding L to [a, b, c]
tcl_coerce_list(i, 1.4, L) Succeeds, binding L to [1.4]
tcl_coerce_list(i, '', L) Succeeds, binding L to []


20.3.5 tcl_delete(+Interpreter)


Tcl_delete deletes the interpreter named Interpreter. Tcl_delete_all deletes all Tcl interpreters created by tcl_new/1.

20.4 Tcl Prolog Interface

20.4.1 prolog - call a prolog term

prolog option ?arg arg… ?

The prolog command provides methods for executing a prolog query in ALS Prolog from Tcl. Option indicates how the query is expressed. the valid options are:

prolog call module predicate ?-type arg …?

Directly calls a predicate in a module with type-converted arguments. The command returns 1 if the query succeeds, or 0 if it fails. The arguments can take the following forms: *number arg
Passes arg as an integer or floating point number. *atom arg
Passes arg as an atom.
*list arg
Passes arg as a list.
*var varName
Passes an unbound Prolog variable. When the Prolog variable is bound, the Tcl variable with the name varName is set to the binding.

prolog read_call termString ?varName …?

The string termString is first read as a prolog term and then called. The command returns 1 if the query succeeds, or 0 if it fails. The optional variables named by the varName arguments are set when a Prolog variable in the query string is bound. The prolog variables are matched to varNames in left-to-right depth first order.


prolog call builtins append -atom a -atom b -var x
    Returns 1, and the Tcl variable x is set to {a b}.

prolog read_call append(a, b, X) x
    Returns 1, and the Tcl variable x is set to {a b}.

20.5 Stand-Alone TCL

Normally Tcl/Tk is installed as a system independent of ALS Prolog. Typically the Tcl/Tk shared/dynamic libraries are stored in a system directory (/usr/local/lib on Linux, including Mac OS, and \winnt\system32 on Windows NT). Tcl/Tk support libraries are similarly stored in a global location.

When creating a stand alone Prolog/Tcl-Tk application, it is sometimes convenient to create a package which includes Tcl/Tk so that the application will run correctly even on systems without Tcl/Tk.

Basicly this is done by moving the Tcl/Tk shared/dynamic libraries and support libraries into the same directory as ALS Prolog. Here are sample directories for Linux, including Mac OS X, and Win32: Linux


On Solaris, unlike other Unix systems, the search path list for shared objects does not include the executable’s directory. To ensure that the Tcl/Tk shared objects are found, the current directory ‘.’ must be added to the LD_LIBRARY_PATH environment variable.