Vous êtes sur la page 1sur 8

ASSIGNMENT-3

FOR

SLLP
(SYMBOLIC LOGIC & LOGICAL PROCESSING) CONTENTS:# Built-in Predicates

SUBMITTED TO:Lect. Gurpreet Singh

SUBMITTED BY:Ravi Thakur CSE 685034240

Built-in Predicates
We have some of the built-in predicates that a Prolog system might provide. By built-in predicate we mean that the predicates definition is provided in advance by the Prolog system, instead of by our own clauses. Built-in Predicates may provide facilities that cannot be obtained by definition in pure Prolog. Or they may provide convenient facilities just to save each programmer from having to define them himself.

Entering New Clauses


There are two basic built-in predicates for reading in new clauses: consult and reconsult. In addition, there is a convenient notation for when you want to read in clauses from more than one file: the list notation.
consult(X)

It is meant for those situations when you want the clauses in some file to augment those already in database. The argument must be an atom giving the name of the file the clauses are to be taken from. Which atom giving constitute a legal file name will, of course, depen on your particular computer. ?- consult(myfile). ?- consult(/usr/john/pl/chat).
reconsult(X)

this predicate is just like consult, except that the clauses read in are taken to supersede all existing clauses for the same predicates. Because of this, reconsult is appropriate for correcting programming mistakes. If you read in several files of clauses and then discover that there is a mistake in one clause, you may be able to correct it without having to read in all the files again.
The list notation

Prolog provides a special notation that makes it more convenient to satisfy consult and reconsult goals, especially when you want Prolog to look at more than one file. The notation involves simply putting the file name into a list, and giving that list as a goal to be satisfied.

?- [file1,-file2,-fred.1,-bill.2].

Success and Failure


In normal course of executing a Prolog program, a goal succeeds when it can be satisfied, and it fails when there is no way to satisfy it.
true

this goal always succeeds. It is not actually necessary, as clauses and goals can be recorded or recombined to obviate any use of true.
fail

This always fails. There are two places where it is helpful. One place is the cut fail combination. Another place to use fail is where you explicitly want another goal to backtrack through all solutions. ?- event(X,Y), phh(Y), fail.

Classifying Terms
If we wish to define predicates which will be used with a wide variety of argument types, it is useful to be able to distinguish in the definition what should be done for each possible type. The following predicates allow the programmer to put these extra conditions in his clauses.
var(X)

The goal var(X) succeeds if X is currently an uninstantiated variable. Thus we would expect the following behaviour: ?- var(X). Yes ?- var(23) No

nonvar(X)

The goal nonvar(X) succeeds if X is not currently an uninstantiated variable. The predicate nonvar is therefore the opposite of var. Indeed, it could be defined in Prolog by: nonvar(X):- var(X), !, fail.
atom(X)

the goal atom(X) succeed if X currently stands for a Prolog atom. As a result, the following behaviour takes place: ?- atom(23). no ?- atom(apples). yes ?-atom(/us/chris/pl.123). yes
integer(X)

the goal integer(X) succeeds if X currently stands for an integer.

Treating Clauses as Terms


Prolog allows the programmer to examine and alter the program. This is particularly straightforward, because a clause can be seen as just an ordinary Prolog structure. Therefore Prolog provides built-in predicates to allow the programmer to: # Construct a structure representing s clause in the database. # Add the clause, represented by given structure, to the database. # Remove a clause, represented by given structure, from the database.

listing(A)

Satisfying a goal of the form listing(A), where A is instantiated to an atom, causes all the clauses with the atom as predicate to be written out, as Prolog term, on the current output file. For instance, in the following example session, the programmer discover that he has not defined reverse properly. ?- [test]. test consulted yes ?- reverse([a,b,c,d],X). No ?- listing(reverse) reverse([],[]) reverse([_44|_45],_38):reverse(_45,_47), appenD(_47,[_44],_38). yes the listing of the reverse clauses reveals that the atom append was misspelled in program.
asserta(X), assertz(X)

The two built-in predicates asserta & assertz allow one to add new clause to database. The predicates act in exactly the same way, except that asserta adds a clause to the beginning of the database, whereas assertz adds a clause to end.
retract(X)

It enables program to remove clauses from the database. The predicate takes a single argument, representing a terms that the clause is to match. The term must be sufficiently instantiated that the predicate of the clause can be determined. When an attempt is to made to satisfy a goal retract(X), X is matched with the

first clause in the database that it can be matched with, and that clause is removed.

Constructing and Accessing Components of Structures


Normally when we want to access a structure of a certain kind of a Prolog program, we do so by just mentioning such a structure. That is, if a predicate needa to handle a variety of different kinds of structures appearing in an argument position, we normally just provide a separate clause for each kind of structure.
functor(T,F,N)

The predicate functor is defined in such a way that functor(T,F,N) means, T is a structure with functor F and arity N. It can be used in basically two ways. In first way T is already instantiated. The goals fails if T is not an atom or a structure. If T is an atom or structure, F is matched with the functor and N is matched with the integer giving the arity of the functor. ?- functor(f(a,b,g(Z)),F,N). Z=_23, F=f,N=3 ?- functor(a+b,F,N). F=+, N=2 ?- functor([a,b,c],F,N). F= ., N=2 ?- functor([a,b,c],a,Z). no

arg(N,T,A)

the predicate arg must always be used with its first two arguments instantiated. It is used to access a particular argument of a structure. The first argumrnt of arg specifies whish argument is required. The second specifies the structure that the argument is to be found inside. Prolog finds the appropriate argument and then tries to match it with the third argument. Thus arg(N,T,A) succeeds if the Nth argument of T is A. ?- arg(2,related(john,mother(jane)),X). X=mother(jane) ?- arg(1, a+(b+c),X). X=a ?- arg(2,[a,b,c],X). X=[b,c]

Input and Output


get0(X)

This goal succeds if X can be matched with the next character encountered on the current input stream. get0 succeeds only once.
get(X)

This goal succeeds if X can be matched with the next printing character encountered on the current input stream. Printing characters have an ASCII code that is greater than 32. Any non-printing characters are skipped. get succeed only once.
skip(X)

This goals reads and skips over characters from the current input stream until a character is found that matches with X. A skip succeeds only once.

read(X)

This goal reads the next term from the current input stream and matches it with X. A read succeeds only once. The term must be followed by a dot ., which does not become a part of the term, and at least one non-printing character.
put(X)

this goals writes the integer Xas a character on the current output stream. put succeeds only once. An error if X is not instantiated.
tab(X)

writes a quantity of X space characters to the current output streams. It succeed only once. An error if X is not instantiated.
write(X)

this goal writes the term X to the current output stream. it succeed only once. Any uninstantiated variables in X are written as uniquely numbered variable beginning with an underscore, such as _239. Sharing variables within same argument to write have the same number when they are printed out.
display(X)

the predicate display works in exactly the same way as write, expect that it ignores any operator declaration. When display is used, any structure is printed out with the functor first and the arguments in brackets afterwards.

Vous aimerez peut-être aussi