Executing SWI-Prolog


A version of Prolog called SWI- Prolog is available on the Linux workstations in the Computer Science labs (105 & 301 MLH). Programs are constructed using any text editor, and the file name can optionally end with the suffix '.pl'. Prolog is initiated with the command 'pl' (use 'man pl' for detailed info). If file 'xxx.pl' contains a program you wish to use, then you enter the query 'consult(xxx).' --  this is referred to as "consulting" the file and all the clauses in the file are appended to your current program. You will be notified of errors.  Note that 'xxx' must not contain special characters (e.g., the pathname separator '/'); to use such names, enclose them in single quotes.  Consulting can be repeated for as many files as you wish.


After being started, Prolog prompts (?-) for a query which must be terminated with a '.', and then responds with a potential series of answers.  After each answer is reported, there is a pause -- if you enter the "Return" key, the query is terminated; if you enter ';' , then another answer is reported; when there are no more answers, 'no' is printed and a prompt for the next query is issued. Printing of deeply  nested terms is truncated by default -- a truncated value can be fully printed by entering 'w' at the pause. Execution is interrupted by control-c, giving options for how to continue. Prolog is terminated by entering the end-of-file (control-d) or the query "halt.".


On-line documentation is available on the Web at the SWI-Prolog Homepage. An abbreviated description of selected pre-defined predicates appears below -- complete descriptions of these and a vastly bigger list appears on the SWI  Web site:

= and \= -- solve for variable values yielding identical terms, no arithmetic

== and \== -- check identical terms, no solving, no arithmetic

help(topic) -- manual info on named topic

atom(X) -- checks if X is currently instantiated to an atom

number(X) -- checks if X is currently instantiated to a number

integer(X) -- checks if X is currently instantiated to an integer

var(X) -- checks if X is currently uninstantiated

nonvar(X) -- checks if X is currently instantiated to a non-variable term

ground(Term) -- checks if all variables in Term are completely instantiated

name(Atom,String) -- converts between atoms and their character strings

length(List,Integer) -- relational list length

member(Item,List) -- succeeds if Item belongs to List

append(List1,List2,Result) -- succeeds when Result is the concatenation of List1 and List2

between(LB,UB,X) -- succeeds repeatedly with X taking on successive values between the lower bound LB and the upper bound UB

Term =.. List -- convert between Term and List with functor and arguments

Var is Term -- numerically evaluate right-argument and unify with left-argument

arithmetic operations -- +, -, *, /, ^,//, mod, plus functions abs, sqrt, min, max, and others

numeric comparison -- =:=, =\=, <, >, =<, >= -- evaluate expressions and compare

sort(List1,List2) -- succeeds with List2 the sorted version (without duplicates) of List1

call(Term) -- make Term the current goal

Goal1 ; Goal2 -- succeeds if either Goal1 or Goal2 succeeds

\+Goal -- negation as failure - succeeds if Goal fails, and vice-versa

findall(Term,Goal,List) -- finds all variable instantiations that solve Goal and creates List with each applied to Term

assert(Clause) -- adds Clause to the current program (predicate must be dynamic)

retract(Clause) -- removes first matching clause from program (predicate must be dynamic)

listing -- lists all clauses in current program

listing(A) -- lists all program clauses for argument A

trace -- turns on debugger in step mode

notrace -- turns off tracing

debug -- initiates messages when spy points reached

nodebug -- disables spy points and turns off debugger

statistics -- reports resource usage

read(Term) -- reads from standard input until '.' and unifies with Term

write(Term) -- writes Term to standard output

nl -- write a "new line" to standard output