Listener

Prolog programs, unlike conventional programs, are collections of predicates, which are like logical rules. A Prolog program is constructed of an interconnected network of these predicates, referring to each other. So a Prolog program could be called a logic base instead.

The Prolog listener is an interactive environment that allows a programmer to query any part of the Prolog logic base, or program. It is a powerful application development and test tool.

Amzi! provides two listeners, one activated from the command line, the other from the Interactive Development Environment (IDE).

Starting and Stopping the Listener

IDE

To start the listener from the IDE without consulting any Prolog source files:

To start the listener consulting a single source file, open or highlight a .pro file and:

To start the listener consulting an entire project, open a .pro file in the project for editing or select the Prolog Project in the Navigator View:

This will consult all the .pro files in the project directory. If you want to exclude one or more files, specify them in your Project Properties. Also any libraries and Logic Server Extensions specified in the project are automatically loaded. All the consulted and loaded files are listed when the listener starts as shown below:

To exit the listener either:

Command Line

At the command line prompt enter the command alis. You will then see the ?- prompt. To exit, type quit.

When running the listener from the command line, you can optionally specify a list of files to be consulted as arguments. These files are then consulted before the listener prompt is shown.

main/0 - if the files that are loaded from the command line contain a main/0 predicate, then it is called immediately. So alis can be used to immediately run a Prolog program from its main/0 predicate.

Entering and Editing Commands

You enter commands and queries after the ?- prompt. You can use the up/down arrows to peruse the queue of prior lines that you typed into the listener. You can also edit those lines using the left/right arrows and delete key. There are copy and paste commands on the edit menu to copy some or all of the output.

Adding and Removing Clauses

Once in the listener, dynamic clauses can be added to the logicbase in a number of ways.

assert

assert, asserta, and assertz can all be used to directly add clauses. By default, the clauses will be added to the default module, user. If other modules are available, then assert can take a module specifier as well.

Adding a Rule

A term with a neck symbol, ":-", is a rule. If the rule has no body, then use the body true to ensure the clause is added.

Consulting user

add

Clauses can be replaced by using replace, just like add. The only difference is the old clauses for the predicate are removed first, and replaced with the new ones.

Removing Clauses

Once in the logicbase, dynamic clauses can be removed with any of the built-in predicates that provide that service, such as retract and abolish. See the Prolog Logicbase section for details.

Listing Clauses

The listing predicate displays a listing of your clauses. It has a few forms.
  • listing - list all of the dynamic clauses in the logicbase
  • listing(Module) - list all of the clause in module Module.
  • listing([Module:]Name) - list all of the clauses of predicate Name in optionally specified Module. Note that this will display clauses of different arities that have the same name.
  • listing([Module:]Name/Arity) - list all of the clauses of Name/Arity in optionally specified Module.
  • For example, after adding the clauses as shown in the section above:

    Note that the clauses were all asserted to the default module, user.

    The listing is displayed using the built-in predicate pp/1, the "pretty printer." You can define your own pretty printer and call it user_pp/1. If so, listing and other built-in predicates will use your pretty printer.

    Variable names are not preserved when clauses are asserted to the logicbase, so listing generates new names for the variables as it displays. The names are of the form _n where n is an integer.

    Querying the Logic Base

    At the listener prompt "?-" You may enter any Prolog goal, including compound conjunctions of goals, separated by ",", and disjunctions, separated by ";".

    The listener attempts to prove the goal, returning values of any variables. You can then enter:

    When there are no more answers, the listener responds with no and returns to the ?- prompt.

    Examples:

    If the value of a variable is undefined, then the internal notation for the variable is displayed. It is of the form Hn, where n is an integer. For example

    Consulting Prolog Source Files

    Normally, you will run a single file or project from the Run | Run As... menu and let the IDE automatically consult your Prolog source code files. However, if you need to add or replace files, there are two ways to get predicates from a Prolog source file into the logicbase as dynamic predicates.

    Reconsult only retracts clauses for predicates that are about to be reconsulted. If a predicate in the logicbase does not have a replacement in the source file being reconsulted, then that predicate is not removed.

    Consult has a few forms:

    Reconsult has two forms:

    Loading Compiled Prolog Code

    From within the listener you can load compiled (.plm) Prolog files. The predicates can be used as goals, just as for consulted (.pro) files. The compiled predicates will be faster and use less stack and heap resources than the interpreted (consulted) predicates, but you will not be able to see them with listing or the debugger.

    There are a few ways to load a compiled (.plm) file:

    Listener Commands

    These predicates, except pp and user_pp can be entered only at the ?- prompt in the listener.

    add

    Allows you to add dynamic clauses to the logicbase. This is equivalent to consult(user).

    debug

    Enters the Command-Line Debugger. See the Command-Line Debugger section for more information. There is also a Source Code Debugger.

    pp(Term), user_pp(Term)

    pp first tries to prove user_pp(Term). If this fails then it will pretty print Term at the user terminal (labeling variables using numbervars/3 and printing separate goals on separate lines).

    quit

    Exits the current listener.

    replace

    Allows you to replace dynamic clauses in the logicbase. This is equivalent to reconsult.

    replace expects a sequence of clauses, followed by [ctrl-z] or 'quit.' rather than a single clause.

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