SUBMITTED TO:Lect. Gurpreet Singh

SUBMITTED BY:Ravi Thakur CSE 685034240

In addition. Or they may provide convenient facilities just to save each programmer from having to define them himself. By built-in predicate we mean that the predicate‟s definition is provided in advance by the Prolog system. you may be able to correct it without having to read in all the files again. Entering New Clauses There are two basic built-in predicates for reading in new clauses: consult and reconsult. reconsult(X) this predicate is just like consult. of course. reconsult is appropriate for correcting programming mistakes. Because of this.consult(myfile).Built-in Predicates We have some of the built-in predicates that a Prolog system might provide. If you read in several files of clauses and then discover that there is a mistake in one clause. except that the clauses read in are taken to supersede all existing clauses for the same predicates. ?. depen on your particular computer. The argument must be an atom giving the name of the file the clauses are to be taken from. . The list notation Prolog provides a special notation that makes it more convenient to satisfy consult and reconsult goals. and giving that list as a goal to be satisfied. ?. instead of by our own clauses. Built-in Predicates may provide facilities that cannot be obtained by definition in pure Prolog. 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.consult(„/usr/john/pl/chat‟). especially when you want Prolog to look at more than one file. The notation involves simply putting the file name into a list. Which atom giving constitute a legal file name will.

2‟].?. fail This always fails. ?. phh(Y). Another place to use fail is where you explicitly want another goal to backtrack through all solutions. fail.-file2.var(X).event(X.1‟.var(23) No . and it fails when there is no way to satisfy it.Y). a goal succeeds when it can be satisfied. as clauses and goals can be recorded or recombined to obviate any use of true. The following predicates allow the programmer to put these extra conditions in his clauses.-„fred.[file1. it is useful to be able to distinguish in the definition what should be done for each possible type. It is not actually necessary.-„bill. Yes ?. Thus we would expect the following behaviour: ?. Classifying Terms If we wish to define predicates which will be used with a wide variety of argument types. var(X) The goal var(X) succeeds if X is currently an uninstantiated variable. Success and Failure In normal course of executing a Prolog program. true this goal always succeeds. There are two places where it is helpful. One place is the “cut fail” combination.

This is particularly straightforward. yes ?-atom(„/us/chris/pl. represented by given structure. Indeed. !. # Add the clause. fail. yes integer(X) the goal integer(X) succeeds if X currently stands for an integer. from the database. no ?. As a result. the following behaviour takes place: ?.atom(apples). represented by given structure. it could be defined in Prolog by: nonvar(X):.123‟).nonvar(X) The goal nonvar(X) succeeds if X is not currently an uninstantiated variable. The predicate nonvar is therefore the opposite of var. # Remove a clause. Treating Clauses as Terms Prolog allows the programmer to examine and alter the program.var(X).atom(23). Therefore Prolog provides built-in predicates to allow the programmer to: # Construct a structure representing s clause in the database. . to the database. atom(X) the goal atom(X) succeed if X currently stands for a Prolog atom. because a clause can be seen as just an ordinary Prolog structure.

The predicate takes a single argument. appenD(_47. yes the listing of the reverse clauses reveals that the atom append was misspelled in program.[_44]. the programmer discover that he has not defined reverse properly. where A is instantiated to an atom. asserta(X). The term must be sufficiently instantiated that the predicate of the clause can be determined. test consulted yes ?._38). ?.listing(reverse) reverse([]. in the following example session. causes all the clauses with the atom as predicate to be written out.listing(A) Satisfying a goal of the form listing(A). When an attempt is to made to satisfy a goal retract(X). retract(X) It enables program to remove clauses from the database. X is matched with the .X). No ?. representing a terms that the clause is to match. except that asserta adds a clause to the beginning of the database.[]) reverse([_44|_45]. The predicates act in exactly the same way.reverse([a. on the current output file.c._47).b. whereas assertz adds a clause to end._38):reverse(_45. as Prolog term.d]. For instance. assertz(X) The two built-in predicates asserta & assertz allow one to add new clause to database.[test].

F= ..F.g(Z)). we normally just provide a separate clause for each kind of structure.b.Z).N).N).first clause in the database that it can be matched with. ”T is a structure with functor F and arity N”. F=+.N=3 ?.c].F. and that clause is removed. N=2 ?.N). F=f. ?.functor(a+b.F.N) The predicate functor is defined in such a way that functor(T. F is matched with the functor and N is matched with the integer giving the arity of the functor.F. It can be used in basically two ways. If T is an atom or structure. functor(T.b.F. That is.a. no . The goals fails if T is not an atom or a structure. Constructing and Accessing Components of Structures Normally when we want to access a structure of a certain kind of a Prolog program.N) means. we do so by just “mentioning” such a structure.functor([a.functor(f(a. N=2 ?.b. if a predicate needa to handle a variety of different kinds of structures appearing in an argument position. In first way T is already instantiated.functor([a. Z=_23.c].

Thus arg(N. a+(b+c).A) succeeds if the Nth argument of T is A.X).X).arg(1. get succeed only once. get0 succeeds 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.related(john. The first argumrnt of arg specifies whish argument is required.c]. get(X) This goal succeeds if X can be matched with the next printing character encountered on the current input stream.[a.c] Input and Output get0(X) This goal succeds if X can be matched with the next character encountered on the current input stream. X=[b. It is used to access a particular argument of a structure. . Prolog finds the appropriate argument and then tries to match it with the third argument. X=a ?. Printing characters have an ASCII code that is greater than 32. X=mother(jane) ?. Any non-printing characters are skipped.arg(N.arg(2. A skip succeeds only once.A) the predicate arg must always be used with its first two arguments instantiated. ?.mother(jane)).b.T.arg(2.T.X). The second specifies the structure that the argument is to be found inside.

It succeed only once. . expect that it ignores any operator declaration. write(X) this goal writes the term X to the current output stream. A read succeeds only once. Sharing variables within same argument to write have the same number when they are printed out. When display is This goal reads the next term from the current input stream and matches it with X. Any uninstantiated variables in X are written as uniquely numbered variable beginning with an underscore. any structure is printed out with the functor first and the arguments in brackets afterwards. such as “_239”. and at least one non-printing character. The term must be followed by a dot ”. An error if X is not instantiated. it succeed only once. An error if X is not instantiated. tab(X) writes a quantity of X “space” characters to the current output streams. display(X) the predicate display works in exactly the same way as write.”. put(X) this goals writes the integer Xas a character on the current output stream. which does not become a part of the term. put succeeds only once.

Sign up to vote on this title
UsefulNot useful