Tcl/Tk Support

The Logic Server Extension (LSX) for Tcl/Tk, atcltk.lsx, provides:

The primary intention is to allow for the easy development of graphical user interfaces for logicbase applications, but any of the capabilities of Tcl/Tk are available from within a logicbase.

The Tcl and Tk libraries are included with the Amzi! distribution, so it is not necessary to install these independently.

  • Overview
  • Calling Tcl/Tk from a logicbase
  • Making logicbase queries from Tcl/Tk
  • Error Handling
  • Distribution
  • Tutorial Example
  • Implementation
  • References
  • Overview

    Tcl is a powerful scripting language that can be embedded in other applications, and also extended with custom commands (very much like the Amzi! Logic Server implementation). When Tcl is embedded in another application, that application can then start Tcl interpreters that have access to any custom commands.

    Tk is an extension to Tcl that provides a clean and powerful way of creating graphical user interfaces.

    A logicbase creates a Tcl interpreter by calling the extended predicate tcl_init. This returns an ID for a Tcl interpreter. The logicbase can then call tcl_eval to evaluate any Tcl/Tk commands for purposes such as:

    The Tcl/Tk command language is extended to allow logicbase queries. So, for example, event-response actions can trigger logicbase queries using the ls_query function, which can, in turn, further manipulate the graphical environment through calls to tcl_eval.

    The result is a very powerful architecture that combines the best of Tcl/Tk's clean windowing interface with the declarative power of a logicbase.

    Calling Tcl/Tk from a logicbase

    These are the extended predicates that allow the creation and use of Tcl/Tk from a logicbase.

    tcl_init(TclInterpreter, TclLibrary, Result)
    Create a Tcl interpreter and return the address of the interpreter in TclInterpreter. Result contains the result string returned from initialization. TclLibrary indicates where the Tcl/Tk libraries are. These can be standard locations or something custom. The values of TclLibrary can be:
    tk_mainloop
    Start the Tk event handling loop. This will normally be called after a number of calls creating an initial configuration for a window and its associated widgets.
    tcl_finish(TclInterpreter)
    Shutdown the Tcl interpreter. Note that this should be used instead of the Tcl "exit" command, which shuts down the process which called it as well.
    tcl_eval(TclInterpreter, CommandString, Result)
    Evaluate the Tcl/Tk commands in CommandString using the specified Tcl interpreter. Result contains the result, which might be a return value for some commands.
    tcl_evalfile(TclInterpreter, FileName)
    Evaluate the Tcl/Tk commands in the named file. This can be useful for calling a longer sequence of commands, such as those used to create a complex graphical environment.

    Making logicbase queries from Tcl/Tk

    The Tcl/Tk commands provide a high-level logicbase query capability. A query contains a goal string that may or may not include logical variables, and the result is a Tcl list of alternating variable names and values.

    list ls_query option ?goal?
    list - Returned Tcl list of alternating variable names and values, where the first pair contains 'result' and either 'true' or 'false'. The list can be easily displayed, queried, or turned into a Tcl array where the variable names becomes the keys.
    option - Backtracking option for the query
    once - Execute the query goal once and return the answer list. Requires a goal.
    first - Call the query goal; return the first answer list; and set up the Logic Server to get additional answers. Requires a goal.
    next - Redo the current query goal and return the next answer. The first two elements will be 'result' and 'false' when there are no more answers. Does not need the goal argument.
    clear - Used for the special case when a query using the first and next options has not found all the answers and the query is to be ended. It is not necessary to call ls_query clear, and in fact might trigger an error, when ls_query next has reached a 'result' of 'false'. Does not need the goal argument.
    goal - A logicbase goal, such as might be entered at a Prolog listener prompt (?-).

    Tcl/Tk commands that correspond to the more common Logic Server API calls are also provided. These are:

    term ls_exec goal
    Execute the query goal once, without setting up backtracking. A logical term is returned, which is cast to a Tcl integer. A zero value indicates the query failed.
    term ls_call goal
    Execute the query goal and set up backtracking. A term is returned, which is cast to a Tcl integer. A zero value indicates the query failed.
    term ls_redo term
    Redo the last query set up for backtracking, using the term returned from ls_call. A term is returned, which is cast to a Tcl integer. A zero value indicates the query failed.
    term ls_clearcall term
    If a query set up with ls_call has not been exhausted using ls_redo calls, use a call to ls_clearcall to end backtracking. The term argument is the term from the last ls_call or ls_redo. A term with 0 value is returned.
    string ls_termtostr term
    The term is converted to string, which is returned.
    string ls_termtostrq term
    The term is converted to string, which is returned, with Prolog quoting when necessary, so that it can be used in another query if necessary.
    string ls_getstrarg term int
    From the input term, get the argument at the int position, where 1 is the first argument. Return the argument as a string. This is a bit different from equivalent Logic Server API calls which expect the argument to be a string or atom. The Tcl/Tk version converts any argument to a string, as if the argument were first passed through ls_termtostr.

    Error Handling

    The Tcl extensions for querying logicbases throw exceptions that can be handled using Tcl's error handling.

    The Prolog extended predicates for calling Tcl throw exceptions that can be handled using Prolog's error handling.

    Distribution

    To distribute an application with Tcl/Tk you need:

    When Tcl/Tk is initialized from Prolog, the tcl_init predicates has an argument that specifies where the Tcl/Tk script library is located. For development, that value should be either 'amzi' or 'tcl' depending on whether you have Tcl/Tk independently installed on the machine.

    For distribution that value can be 'here', which means the Tcl/Tk 'lib' directory is in the current directory of the application. In this case you copy the amzi/lsapis/tcltk/lib directory to your application directory.

    The library can also be a specific location, in which case the directory that contains the lib directory is supplied as the second argument to tcl_init.

    Tutorial Example

    See the references below for introductions to Tcl/Tk and see the amzi/samples/tcltk directory for samples.

    Implementation

    The Tcl/Tk interface is implemented in amzi/bin/atcltk.lsx, and requires the tcl and tk shared libraries and the tcl and tk libraries of the Tcl scripts. All of these are included with the Amzi! distribution.

    The Tcl/Tk LSX contains both the extended predicates that allow Prolog to call Tcl, and the Tcl extensions that allow Tcl to call Prolog.

    References

    http://www.scriptics.com/ - Tcl/Tk developer site, with downloads.

    http://www.pconline.com/~erc/tcl.htm - Eric Foster Johnson's site with Tcl/Tk resources and a quick reference.

    http://www.csc.calpoly.edu/~dbutler/tutorials/winter96/tcl/ - Alex Samonte's Tcl/Tk tutorial.

    Tcl and the Tk Toolkit by John Ousterhout [Addison Wesley] - The Tcl/Tk creator's book on the subject.

    Practical Programming in Tcl and Tk by Brent Welch, Ken Jones, Jeffrey Hobbs [Prentice Hall 2003] - Another good book on Tcl/Tk programming.

    Copyright ©1987-2011 Amzi! inc. All Rights Reserved. Amzi! is a registered trademark and Logic Server is a trademark of Amzi! inc.