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).
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. >
To start the listener from the IDE either:
To exit the listener from the IDE either:
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.
==>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.
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
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:
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.
>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.
From the IDE there are a number of additional ways to consult files into the listener.
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).
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.
Enters the Debugger. See the Debugger section for more information.
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.
Reconsults a project file.
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.