/* INPUT AND OUTPUT: Reading and writing characters (= one letter terms): get_char(X) put_char(X) Reading and writing strings (= any term): read(X) write(X) If X is uninstantiated, next char or string (string ended by . and newline) will be read and X will be instantiated. If X is already instantiated, the goal will be satisfied iff X matches what is read. These goals will only succed once. Examples: | ?- put_char(a). a | ?- write(abc). abc | ?- read(X). xyz. X = xyz | ?- write(X). _16 | ?- write(f(a,b)). f(a,b) | ?- write(2+3*4). 2+3*4 | ?- write_canonical(2+3*4). +(2,*(3,4)) The predicate nl: flushes output, adding a newline. File access: open(X,Y,Z) [X filename, Y is read or write, Z is "filehandle"] close(Z) set_input(Z) set_output(Z) current_input(Z) current_output(Z) Reading and writing acts on "current stream", initially keyboard and screen. This can be changed using filehandles, obtained by open. The change is done by set_input and set_output. The current_input and current_output predicates get filehandles for current streams. Gprolog has many more predicates for stream handling and for reading from and writing to streams. */ /* OPERATORS: The predicate 'op' makes infix (prefix, postfix) operators (= functors with speical syntax). In the use op(Precedence, Specification, Name) Precedence is its binding power (an integer between 1 and 1200, lowest is strongest), Name is the operator functor, and Specification chooses infix/prefix/postfix as well as associativity. In specification, x means strictly stronger (lower) precedence required in that direction, and y means stronger or equal precende required. Example> */ ?- op(800,yfx,'++'). ?- op(200,fy,'-'). /* Here, xfy means infix and left-associative, and fy means prefix and (right-) associative. And xfx means infix and non-associative. The specification yfy is not allowed (but mentioned in the book). */ /* CLASSIFYING TERMS: var(X) [is X currently an uninstantiated variable (possibly sharing)?] nonvar(X) [the opposite] atom(X) [is X an atom (non-number atom)?] number(X) [is X a number?] atomic(X) [is X an atom or a number?] */ /* CLAUSES AS TERM (PROGRAM AS DATA and VICA VERSA): The following will match all clauses in database with given functor: ':-' is an operator: | ?- write_canonical((f(X):-a)). :-(f(_16),a) clause(X,Y) [for X instantiated to at least functor(...), matches the clauses (X = head, Y = body) for this functor] See also in Chapter 6: asserta, assertz, retract, functor, arg, and =.. */