Go to the first, previous, next, last section, table of contents.


Built-In Predicates

Not every standard built-in predicate found in most Prolog evaluators are avaliable in DyALog and will not be because DyALog is mostly devoted to build parsers.

Input / Output

Input / Output in DyALog is done through streams that can associated either to a file, a string or a device.

A stream can be connected to a filename or UNIX file descriptor for input or output by calling the predicate open/3.

The possible formats of a stream are:

n
A stream connected to some file. n is an integer.
symbol
where symbol has been aliased to a stream using add_stream_alias/2. Note that user_input, user_output, and user_error are by default aliased to the UNIX stdin, stdout, and stderr streams.

Shell-like expansions of filenames is provided.

Reading-in Programs

None.

Input and Output of Terms

read(?Term)
read(+Stream,?Term)
The next term, delimited by a full-stop (i.e. a ., possibly followed by layout text), is read from Stream and is unified with Term. The syntax of the term must agree with current operator declarations. If a call read(Stream, Term) causes the end of Stream to be reached, Term is unified with the term eof. Further calls to read/2 for the same stream will then fail, unless the stream is connected to the terminal.
read_term(?Term,+Vars)
read_term(+Stream,?Term,+Vars)
Same as read/1-2 with a third argument +Vars. This argument is unified with a list of Name=Var pairs, where each Name is an atom indicating the name of a non-anonymous variable in the term, and Var is the corresponding variable.
write(?Term)
write(+Stream,?Term)
The term Term is written onto Stream according to current operator declarations.
writeln(?Term)
writeln(+Stream,?Term)
Same as write/1-2 except that a newline is send,
display(?Term)
The term Term is displayed onto the standard output stream (which is not necessarily the current output stream) in standard parenthesized prefix notation.
writeq(?Term)
writeq(+Stream,?Term)
Similar to write(Stream,Term), but the names of atoms and functors are quoted where necessary to make the result acceptable as input to read/2, provided the same operator declarations are in effect.

Character Input/Output

get_char(?C)
get_char(+Stream,?C)
C is the next character read from Stream (or by default, from stream user_input).
put_char(+C)
put_char(+Stream,+C)
Character C is output onto Stream (or by default, onto stream user_output).

Stream IO

open(+FileName,+Mode,-Stream)
If FileName is a valid file name, the file is opened in mode Mode (invoking the UNIX function fopen) and the resulting stream is unified with Stream. Mode is one of:
read
Open the file for input.
write
Open the file for output. The file is created if it does not already exist, the file will otherwise be truncated.
append
Open the file for output. The file is created if it does not already exist, the file will otherwise be appended to.
close(+Stream)
If Stream is a stream the stream is closed.
absolute_file_name(+RelativeName,?AbsoluteName)
This predicate is used by all predicates that refer to filenames for resolving these. The argument RelativeName is interpreted as a filename according to the filename syntax rules (see section Input / Output). If the specified file is found (possibly with a `.pl' extension), AbsoluteName is unified with the full path name of this file.
current_input(?Stream)
Unify Stream with the current input stream. The current input stream is also accessed by the C variable SP_curin.
current_output(?Stream)
Unify Stream with the current output stream. The current output stream is also accessed by the C variable SP_curout.
set_input(+Stream)
Set the current input stream to Stream.
set_output(+Stream)
Set the current output stream to Stream.
flush_output
flush_output(+Stream)
Flush all internally buffered characters for Stream to the operating system.
from_alias_to_stream(+Alias,?Stream)
Unify Stream with the stream aliased to Alias.
add_stream_alias(+Stream_or_Alias,+Alias)
Alias the stream given by Stream_or_Alias with Alias.

Socket IO

None.

DEC-10 Prolog File IO

The DEC-10 prolog IO predicates are available with the library `dec10':

see(+File)
File File becomes the current input stream. File may be a stream previously opened by see/1 or a filename. If it is a filename, the following action is taken: If there is a stream opened by see/1 associated with the same file already, then it becomes the current input stream. Otherwise, the file File is opened for input and made the current input stream.
seeing(?FileName)
FileName is unified with the name of the current input file, if it was opened by see/1, with the current input stream, if it is not user_input, otherwise with user.
seen
Closes the current input stream, and resets it to user_input.
tell(+File)
File File becomes the current output stream. File may be a stream previously opened by tell/1 or a filename. If it is a filename, the following action is taken: If there is a stream opened by tell/1 associated with the same file already, then it becomes the current output stream. Otherwise, the file File is opened for output and made the current output stream.
telling(?FileName)
FileName is unified with the name of the current output file, if it was opened by tell/1, with the current output stream, if it is not user_output, otherwise with user.
told
Closes the current output stream, and resets it to user_output.

Arithmetic

Arithmetic is performed by built-in predicates which take as arguments arithmetic expressions and evaluate them. An arithmetic expression is a term built from numbers, variables, and functors that represent arithmetic functions. At the time of evaluation, each variable in an arithmetic expression must be bound to a non-variable expression. An expression evaluates to a number, which may be an integer.

Only certain functors are permitted in an arithmetic expression. These are listed below, together with an indication of the functions they represent. X and Y are assumed to be arithmetic expressions. Unless stated otherwise, the arguments of an expression may be any numbers.

+(X)
The value is X.
-X
The value is the negative of X.
X+Y
The value is the sum of X and Y.
X-Y
The value is the difference of X and Y.
X*Y
The value is the product of X and Y.
X//Y
The value is the integer quotient of X and Y.
X mod Y
The value is the integer remainder after dividing X by Y, i.e. integer(X)-integer(Y)*(X//Y).
X/\Y
The value is the bitwise conjunction of the integers X and Y.
X\/Y
The value is the bitwise disjunction of the integers X and Y.
X#Y
The value is the bitwise exclusive or of the integers X and Y.
\(X)
The value is the bitwise negation of the integer X.
X<<Y
The value is the integer X shifted left by Y places.
X>>Y
The value is the integer X shifted right by Y places.
abs(X)
The value is the absolute value of X.
min(X,Y)
The value is the lesser value of X and Y.
max(X,Y)
The value is the greater value of X and Y.

Arithmetic expressions, as described above, are just data structures. If you want one evaluated you must pass it as an argument to one of the built-in predicates listed below. Note that it only evaluates one of its arguments, whereas all the comparison predicates evaluate both of theirs. In the following, X and Y stand for arithmetic expressions, and Z for some term.

Z is X
X, which must be an arithmetic expression, is evaluated and the result is unified with Z.
X =:= Y
The numeric values of X and Y are equal.
X =\= Y
The numeric values of X and Y are not equal.
X < Y
The numeric value of X is less than the numeric value of Y.
X > Y
The numeric value of X is greater than the numeric value of Y.
X =< Y
The numeric value of X is less than or equal to the numeric value of Y.
X >= Y
The numeric value of X is greater than or equal to the numeric value of Y.

Comparison of Terms

These built-in predicates are meta-logical. They treat uninstantiated variables as objects with values which may be compared, and they never instantiate those variables. They should not be used when what you really want is arithmetic comparison (see section Arithmetic) or unification.

The predicates make reference to a standard total ordering of terms, which is as follows:

These are the basic predicates for comparison of arbitrary terms:

Term1 == Term2
Tests if the terms currently instantiating Term1 and Term2 are literally identical (in particular, variables in equivalent positions in the two terms must be identical). For example, the query
%>dyalog -s "?-X==Y. "
fails (answers `no') because X and Y are distinct uninstantiated variables. However, the query
%>dyalog -s "?-X=Y,X==Y. "
Answer : Y = X 
succeeds because the first goal unifies the two variables (see section Miscellaneous).
Term1 \== Term2
Tests if the terms currently instantiating Term1 and Term2 are not literally identical.
Term1 @< Term2
Term Term1 is before term Term2 in the standard order.
Term1 @> Term2
Term Term1 is after term Term2 in the standard order.
Term1 @=< Term2
Term Term1 is not after term Term2 in the standard order.
Term1 @>= Term2
Term Term1 is not before term Term2 in the standard order.

Control

+P , +Q
Prove P and if it succeeds, then prove Q.
+P ; +Q
Prove P or Q.
\+ +Guard
If the guard Guard has a solution, fail, otherwise succeed.
+Guard -> +Q ; +R
+Q is called for every possible solutions of Guard. +R is only called if Guard has no solutions.
+Guard -> +Q
Analogous to +Guard -> +Q;fail
true
Always succeed.
fail
Always fail.
wait(+Goal)
Wait the full completion of the evaluation of Goal before evaluating its continuation. Still very experimental!

The library `call' provide the additionnal predicate:

call(Goal)
If Goal is instantiated to a term which would be acceptable as the body of a clause, then the goal call(Term) is executed exactly as if that term appeared textually in its place. There are some restrictions on Goal.

Error and Exception Handling

DyALog treats very poorly errors. There is only one predicate to raise errors (but no way to catch them).

error(Error)
Display Error and fail. There is no exit of the program.

Information about the State of the Program

None.

Meta-Logic

The predicates in this section are meta-logical and perform operations that require reasoning about the current instantiation of terms or decomposing terms into their constituents. Such operations cannot be expressed using predicate definitions with a finite number of clauses.

var(?X)
Tests whether X is a variable
nonvar(?X)
Tests whether X is not a variable. This is the opposite of var/1.
ground(?X)
Tests whether X is free of unbound variables.
atom(?X)
Checks that X is an atom.
integer(?X)
Checks that X is an integer.
number(?X)
Checks that X is a number.
atomic(?X)
Checks that X is an atom or number.
simple(?X)
Checks that X is a variable, an atom or a number.
compound(?X)
Checks that X is currently a term of arity > 0 i.e. a list or a structure.
functor(+Term,?Name,?Arity)
functor(?Term,+Name,+Arity)
The principal functor of term Term has name Name and arity Arity, where Name is either an atom or, provided Arity is 0, an integer. Initially, either Term must be instantiated, or Name and Arity must be instantiated to, respectively, either an atom and an integer in [0..256) or an atomic term and 0. In the case where Term is initially uninstantiated, the result of the call is to instantiate Term to the most general term having the principal functor indicated.
arg(+ArgNo,+Term,?Arg)
Initially, ArgNo must be instantiated to a positive integer and Term to a compound term. The result of the call is to unify Arg with the argument ArgNo of term Term. (The arguments are numbered from 1 upwards.)
+Term =.. ?List
?Term =.. +List
List is a list whose head is the atom corresponding to the principal functor of Term, and whose tail is a list of the arguments of Term. e.g.
%>dyalog -s "?-product(0, n, n-1) =.. L. "
Answer : L = [product,0,n,n - 1] 
%>dyalog -s "?-n-1 =.. L. "
Answer : L = [-,n,1] 
%>dyalog -s "?-product =.. L. "
Answer : L = [product] 
If Term is uninstantiated, then List must be instantiated either to a list of determinate length whose head is an atom, or to a list of length 1 whose head is a number.
name(+Const,?CharList)
name(?Const,+CharList)
If Const is an atom or number then CharList is a list of the character codes of the characters comprising the name of Const. e.g.
%>dyalog -s "?-name(product,L). "
Answer : L = [0'p,0'r,0'o,0'd,0'u,0'c,0't] 

%>dyalog -s "?-name(1976,L). "
Answer : L = [0'1,0'9,0'7,0'6] 
If Const is uninstantiated, CharList must be instantiated to a list of characters. If CharList can be interpreted as a number, Const is unified with that number, otherwise with the atom whose name is CharList.
atom_chars(+Const,?CharList)
atom_chars(?Const,+CharList)
The same as name(Const,CharList), but Const is constrained to be an atom.
number_chars(+Const,?CharList)
number_chars(?Const,+CharList)
The same as name(Const,CharList), but Const is constrained to be a number.
term_subsumer(+Term1, +Term2, -General)
Binds General to the most specific term that generalizes Term1 and Term2. This process is sometimes called anti-unification, as it is the dual of unification.
%>dyalog -s "?- term_subsumer(f(g(1,h(_))), f(g(_,h(1))), T). "
Answer : T = f(g(B__2,h(A__2))) 

%>dyalog -s "?- term_subsumer(f(1+2,2+1), f(3+4,4+3), T). "
Answer : T = f(B__2 + C__2,C__2 + B__2) 

Modification of the Program

None.

Internal Database

The predicates described in this section store arbitrary terms in the database without interfering with the clauses which make up the program.

record(+Term)
An entry associated with Term is added to the internal database.
recorded(+Term)
The internal database is searched for terms unifiable with Term.
erase(+Term)
Any entry in the internal database unifiable with Term is erased.

All Solutions

The predicates described in this section works on the whole set of solutions that may be computed for a goal.

bestof(X,Generator,Y^Test)
Test must denote a total binary relation defined as a guard and is used to compute the best X element for this relation in those generated by Generator. X is unified with this best element.
%>dyalog -s "?-bestof(X,domain(X,[1,-2,3]),Y^(X<Y)). "
Answer : X = -2 
iterate( Iterator, Generator )
Iterator must be either an elementary iterator or a list of elementary iterator. An elementary iterator New^(Init,X^Old^Updater) computes the iterate value of Init by repeated application of Updater to each value X generated by Generator.
%>dyalog -s "?-iterate(Y^(Y is 0,X^Old^(Y is X+Old)),domain(X,[1,2,3])). "
Answer : Y = 6 
Note that iterate doesn't fail if there is no answer Generator but binds New variables to Init values.
group_by(Generator,Grouping,Collector)
Collector of the form New^Current^(Old^Updater,Init) almagates values Current build by Generator

Note that group_by fails if Generator has no answer.

Debugging

None

Execution Profiling

None

Definite Clause Grammars

Definite Clause Grammars are available in DyALog using the standard notations,

Terminals to be recognized may be provided either by a PROLOG list or a set of tokens. A token has the form 'C'(Left,T,Right) and means that a terminal L is present between the markers Left and Right. Anything may be used as markers, may integers are usually employed.

phrase(:Phrase,?List,?Remainder)
phrase(:Phrase,?Left,?Right)
According to the current grammar rules, Phrase is found between List and Remainder for the first form and the markers Left and Right for the second form.

Miscellaneous

?X = ?Y
Defined as if by the clause Z=Z.; i.e. X and Y are unified.
length(?List,?Length)
If List is instantiated to a list of determinate length, then Length will be unified with this length. If List is of indeterminate length and Length is instantiated to an integer, then List will be unified with a list of length Length. The list elements are unique variables. If Length is unbound then Length will be unified with all possible lengths of List.
copy_term(?Term,?CopyOfTerm)
CopyOfTerm is a renaming of Term, such that brand new variables have been substituted for all variables in Term.
argv(?Args)
Args is unified with a list of atoms of the program arguments supplied after the `-a' option on the command line.
cd
Change the current working directory to the home directory.
shell(+Command,-Status)
Pass Command to a new UNIX shell named in the Unix environment variable $SHELL for execution. Unify Status with the returned status of Command.
system(+Command,-Status)
Pass Command to a new UNIX sh process for execution. Unify Status with the returned status of Command.
mktemp(+Template,-FileName)
Interface to the C-function mktemp(3). A unique file name is created and unified with FileName. Template should contain a file name with six trailing Xs. The file name is that template with the six Xs replaced with a letter and the process id.
access(+Path,+Mode)
Tests if Mode is the accessability of Path as in the C-function access(2).
getwd(?Path)
Unify Path with the atom representation of the current working directory.
getenv(+Name,?Value)
Unify Value with the atom representation of the value of the environment variable given by Name.
gensym(-Id)
Unify Id with a fresh integer.
domain(x?X,+List)
A built-in oriented version of member/2.


Go to the first, previous, next, last section, table of contents.