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

Command Line

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

IDE

To start the listener from the IDE either:

To exit the listener from the IDE either:

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.

    ==>NOTE: 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

    From Within a Listener

    From within the listener, there are two ways to get predicates from a Prolog source file into the logicbase as dynamic predicates.

    ==>NOTE: 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:

    From the Command Line

    When running the listener from the command line (alis), 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.

    IDE Options

    From the IDE there are a number of additional ways to consult files into the listener.

    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).

    consult_project(ProjectFile)

    Consults all the files and libraries in a project file. Note that if a project is already the current project, consult automatically does a reconsult of the project.

    debug

    Enters the Debugger. See the Debugger section for more information.

    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.

    reconsult_project(ProjectFile)

    Reconsults a project file.

    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 ©1995-2006 Amzi! inc. All Rights Reserved. Privacy Policy.
    Amzi!, KnowledgeWright and WebLS are registered trademarks and Adventure in Prolog, Logic Server, Logic Explorer, Adventure in Prolog and Subscription Plus are trademarks of Amzi! inc.