The most useful input output predicates are write/1, read/1 and nl/0.
write(term) is true if term is a Prolog term. As a side-effect, it causes term to appear on the current output device (e.g. your screen in the Winterm window).
nl is always true, and as a side-effect, sends a newline instruction to the current output device. So the conjunction of a write/1 instruction and a nl/0 instruction will result in a term being displayed on the screen, followed by a carriage return.
read(X) is true if the user types a term followed by a full-stop. X becomes instantiated to the term. E.g.
?- read(X).
|: this. X = this Yes |
read/1 can be used to input a list of words (because a list is a term), but the list has to be typed complete with brackets and commas:
?-read(X).
|:[this,is,a,list]. X = [this,is,a,list] Yes |
Getting Prolog to accept a string of atoms and convert them into a list is quite heavy-duty Prolog programming and involves the use of get/1 and get/0, which accept single characters (in ASCII numerical format); the building of a list of such numbers; their conversion from a list of numbers into a Prolog atom, using the predicate name/2 and, finally the construction of a list of such atoms.
display/1 was mentioned on page 66. This is useful for showing the meaning of operator declarations.
listing/0 and listing/1 are predicates which display the contents of the Prolog database on the screen.
listing/0 will write all the predicates known to Prolog (the ones defined in the files you have consulted and also a number of built-in ones).
listing/1 takes the name of a predicate as argument and writes its definition to the screen.
These are useful for checking that Prolog has actually ingested the information you have supplied it with. Sometimes, if there is a bug in your program, it may not load completely, or it may load in a form you don’t expect. listing/n will enable you to check your anticipated version against what Prolog believes. (In the event of a disagreement, Prolog is right!)
Here is an example.
Welcome to SWI-Prolog (Multi-threaded, Version 5.2.0)
Copyright (c) 1990-2003 University of Amsterdam. SWI-Prolog comes with ABSOLUTELY NO WARRANTY. This is free software, and you are welcome to redistribute it under certain conditions. Please visit http://www.swi-prolog.org for details. For help, use ?- help(Topic). or ?- apropos(Word). ?- listing. % Foreign: rl_read_init_file/1 % Foreign: rl_add_history/1 Yes ?- |
No files have been consulted, so Prolog knows nothing. So now consult a file called lists.pl and try again.
?- [lists].
% lists compiled 0.00 sec, 132 bytes Yes ?- listing. % Foreign: rl_read_init_file/1 % Foreign: rl_add_history/1 my_append([], A, A). my_append([A|B], C, [A|D]) :- my_append(B, C, D). my_member(A, [A|B]). my_member(A, [B|C]) :- my_member(A, C). Yes |
Now Prolog knows about member/2 and append/3. Here is an example of the use of listing/1 which here just shows the definition of member/2.
?- listing(my_member).
my_member(A, [A|B]). my_member(A, [B|C]) :- my_member(A, C). Yes |
Finally, if the requested predicate is not known to Prolog, an error message results:
?- listing(foo).
ERROR: No predicates for `foo' No |