The PrologJ Interpreter

Overview

PrologJ includes an interpreter that can read and call goals typed by the user. The interpreter can be run as a stand-alone application, or it can be invoked from within compiled Prolog code. The interpreter conforms closely to the description found in [ Clocksin and Mellish, 1994 ], including support for interactive debugging.

Running the Interpreter as an Application

The PrologJ interpreter can be started as an application in one of two ways:

If the interpreter is started up by double-clicking the prologj.jar file, or is started from the command line or a script with the -w[indow] option specified, it uses a window for interaction with the user . If the interpreter is started up from the command line or a script without the -w[indow] option, it uses standard input and standard output for interacting with the user. (This means that operations using the interpreter can be embedded in a script or can appear as part of a pipeline.)

If the interpreter is started up by double-clicking the prologj.jar file, or is started from the command line or a script with the -w[indow] option specified, the Library Components menu can be used to select which built-in predicates are available, and the Flags menu can be used to specify the settings of various flags. If the interpreter is started up from the command line or a script, the following options may be specified. (If it is started from the command line with the -w[indow] option specified, either approach may be used.)

Each command-line option name may be abbreviated to as little as just its first letter. Note that these options are similar to those available when starting the compiler, but not identical. The interpreter is started just when no files are specified on the command line.

Starting the Interpreter from within Prolog code

The interpreter can be invoked from within Prolog code by using the built-in predicate break/0. In this case, the interpreter uses standard input and output if the program was started from the command line or a script without the -w[indow] option, and a console window if the program was started some other way. The predicates that are available are those that were available to the running program, and the values of the flags are those that were in effect at the time the break/0 predicate is executed. The program that calls break/0 is suspended until the interpreter exits.

Starting the Interpreter from within Java code

Because any Java program can execute a Prolog predicate by using the call method of class PrologJ, any Java program can start the interpreter by calling break/0. In this case, the interpreter always uses standard input and output for interacting with the user. If the Java program was run without standard input and output streams (e.g. it was in a double-clicked .jar file), it will not be possible to interact with the interpreter - the console window is only created when PrologJ is itself started as an application.

Interaction with the Interpreter

When the interpreter is started, it prompts for a query by displaying ?- . Typically, the user types a callable Prolog goal, terminated by a period. The interpreter calls the goal specified by the term, and then does one of the following:

In any case, once the interpreter has printed No. or Yes. or an error message, it prompts for the user to enter another query. The following show some interactions illustrating these possibilities:

?- atom(1).
No.

?- atom(a).
Yes.

?- atom_concat(X, Y, ab).
X = 
Y = ab? ;
X = a
Y = b? ;
X = ab
Y = ? .
Yes.

?- atom_concat(X, b, ab).
X = a? ;
No.

?- call(once(call(once(3)))).
Type error: callable 3
in: once / 1
called from: call / 1
called from: once / 1
called from: call / 1

?-

The interpreter can be exited by end-of-file, which is entered as control-D when reading from the command line, or by clicking the EOF button when reading from the Console window. If the interpreter is run as an application, this terminates the application; if it is run by using break/0, it terminates the "break-loop" and resumes execution at the place where break/0 was called.

Entering Facts or Clauses from Files or Interactively

The interpreter allows the user to consult or reconsult source files at any time. This can be done either by using the built-in predicates consult/1 or reconsult/1, or by specifying a list of files (often just one) as a command; in which case preceding a file name by - will cause it to be reconsulted instead of consulted. For example, entering the query

[ foo, -bar, baz ].

would cause the file "foo" to be consulted, "bar" to be reconsulted, and then "baz" to be consulted.

When specifying files to consult/reconsult using either the built-in predicates or a list, it is possible to specify interactive consultation from the console by using either user or user_input. (PrologJ treats these as equivalent, and either can be preceded by - to specify reconsult rather than consult). When interactive consultation/reconsultation is specified, the interpreter's prompt changes to | , and each term that is entered is treated as a fact or clause to assert into the database, rather than as a goal to be proved. Therefore, No. or Yes. is not printed after each clause. Interactive consultation is terminated by end-of-file, which is entered as control-D when reading from the command line, or by clicking the EOF button when reading from the Console window. This causes the interpreter to return to query mode. Example:

?- [user].
| president(washington).
| president(adams).
| president(jefferson).
| ^D       
?- president(X).
X = washington? ;
X = adams? ;
X = jefferson? ;
No.

?- [-user].
| president(lincoln).
| ^D
?- president(X).
X = lincoln? ;
No.

?- 

To simplify interactive program development, when a new predicate is created as a result of interactively entering a clause for it, the newly created predicate will have its attributes set as if it had been created by asserta/1 or assertz/1 - i.e. it will be dynamic and public. (However, if a predicate is first created as a result of explicitly entering a directive, or as a result of reading a clause from a file, only directives explicitly entered will apply.)

Copyright © 2005 - Russell C. Bjork. See the file See file COPYING in the root directory for copyright information.

Valid XHTML 1.0!