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.
The PrologJ interpreter can be started as an application in one of two ways:
prologj.jar file as an application in the
platform-specific way (typically by double-clicking it).java -jar prologj.jar [ options ](where
options are discussed below)
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.
-l[ibrary] librarieslibraries
is some combination of the letters corresponding to the predicate categories
desired, or - to use just the subset of core predicates that are
common to both ISO and "traditional" Prolog. If this command option is not
specified, all built-in predicate categories are available.-f[lags] flagsflags is one of the following:s specifies initial flag settings to produce behavior
that conforms strictly to the defaults specified in the ISO standard.
r specifies a "relaxed" version of the ISO settings which
might be more useful in conjunction with code development using the
interpreter. With these settings, an attempt to call an undefined
predicate results in a warning, rather than an error; and the rules
pertaining to predicate contiguity, appearance in multiple source
files, and dynamic and public predicates are not enforced.
t specifies "traditional" behavior consistent with
[ Clocksin and Mellish 1994 ]. With these settings, an attempt to call
an undefined predicate results in simple failure, rather than an error
or a warning; the rules pertaining to predicate contiguity, appearance
in multiple source files, and dynamic and public predicates are not
enforced; and the operator / means integer division when
both of its arguments are integers.
-w[indow]
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.
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.
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:
No.Yes._0.) . and press enter), or the user can enter ; and
press enter. The former corresponds to accepting the variable bindings
printed, the latter to requesting that the goal be redone so that another
set of bindings can be found, which are, in turn printed.Yes. to indicate that the goal succeeded; if the nterpreter
cannot find a way to re-satisfy the goal upon some request to redo it, it
prints No.
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.
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.)