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).
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:
At the command line prompt enter the command alis. You will then see the ?- prompt. To exit, type quit.
>alis Amzi! Prolog Listener Type 'quit.' to exit ?- write(hello). hello yes ?- 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.
>alis dw_main dw_rules dw_data
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.
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.
?- assert( likes(ella, crackers) ). yes
?- likes(ella, pizza) :- true. Term asserted
?- consult(user). | likes(leona, lettuce). | likes(leona, pool). | quit. yes
?- add. | likes(basil, carrots). | likes(basil, hay). | quit. yesClauses 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.
?- retractall( likes(basil, X) ). yes
For example, after adding the clauses as shown in the section above:
?- listing. user:likes(ella, crackers). user:likes(ella, pizza). user:likes(leona, lettuce). user:likes(leona, pool). user:likes(basil, carrots). user:likes(basil, hay). yes
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.
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:
?- likes(ella, X). X = crackers ; X = pizza ; no ?- likes(X, Y). X = ella Y = crackers ; X = ella Y = pizza % [Enter] key pressed yes ?- likes(X, A), likes(X, B), A \= B. X = ella A = crackers B = pizza ; X = leona A = lettuce B = pool ; noIf 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
?- X = foo(A,B,A). X = foo(H8,H9,H8) A = H8 B = H9
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:
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:
These predicates, except pp and user_pp can be entered only at the ?- prompt in the listener.
Allows you to add dynamic clauses to the logicbase. This is equivalent to consult(user).
Enters the Command-Line Debugger. See the Command-Line Debugger section for more information. There is also a Source Code Debugger.
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).
Exits the current listener.
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.