You are on page 1of 10
Introduction to Prolog ees EIST 4) PROGRAMMING LANGUAGES igned to express the computations that can be performed}; ‘The programming languages are desi I h machine, particularly a computer. Programming languages can be used _as a mode of hur communication to create programs that control the behavior of a machine. Thousands of differ: programming languages have been created with many more being created every year. M programming languages describe computation inan imperative style, i.e. as a sequence of comm’, although some languages, such as those that support functional programming OF Togic program use alternative forms of description. ~~ a 4.2 TYPES OF PROGRAMMING LANGUAGES . ‘ 4.2.1 Procedural/Imperative Programming Languages ‘These are the programming languages which tell the computer about what to do and how to chic ayoal, by giving'a sequence of instructions. Using a procedural language, the programe writes the algorithms and then defines €ach step precisely to perform 5 particular uk programmicr must know about whacisto be accomplished and provides the step-by-step TE on how the task is to be done. Imperative i ; stored reat ee jansuages reflect the-architecture of the underlying Vo" M4 p =“ mputer i.e. the programs consist of i Gone seed teary wis counters determining which instruction to ee Se SO he ; jnroduetion to Prolog be filled in by a compiler. In spite of all these benefits, Programs in these languages are still made gpofcommands that work by changing values stored in emory locations, Programs are understood sn tems of WHat hen a computer obeys the com - For this reason, programming fanguages such as these are often ‘described as imperative. Xamples of imperative languages are FORTRAN, Pascal, Basic, C+, java and COBOL, Prolog and Lisp comes under the declarative programming languages, but these are not entirely declarative, they have some imperative features for input and output. 4.2.2 Descriptive/Declarative Programming Languages The declarative programming | programming languages in which the solution to be discovered is expressed as 2 question to be aagvered of a goal to be achieved. The programier specifies what constitutes a solution to « problem, and then an underlying system of deduction reasons about the answer. The distinguishing fature of declarative programming Janguagesis remade up not of commands to beexecuted, but of definitions and statements about the problem to be solved, In declarative languages the programmers only give the problem and the language finds itself how to solve the problem. Declarative programming expresses the logic of a computation without describing its control flow. The Declarative programs contain no explicit instructions to be followed by the computer that executes them. Instead, the job of the computer is to manipulate the information contained in the rogram so as to derive the solution to a given problem. A declarative style is non-procedural in the _ Saase that a program written according to this style does not state exactly How the computational ied out. Rather, a program Consists of mainly-a number of declarations facts and rules concerning the problem domain. Declarative programming considers programs as theories of a formal logic, and computations "deductions in that logic-space. Examples of declarative languages are Prolog, OBJ3. Haskell and Lisp. juire a more descriptive style i.e. these are the 42.3. Procedural vs. Declarative Programming Languages The Procedural languages require the programmer to specify the detailed_steps which are to be ‘ccuted in order to. accomplish its intended task. Whereas in declarative languages, the programmer Provides (in the program) a definition of the task to-be-accomiplished, but is not concerned with the ‘alls of how the computer will use the definition. : The most important difference between logic programming languages and conventional [clara languages is that the logic programming languages only express the logic of the problem. tie programming, the description of the problem and the method for solving it are exp ily lenin“ ftom cach other i.e. the logic programming languages separates the logic aan contol, ip the control to the program executor. This separation has been expressed by R. owals| * following equation: algorithm = logic + control Im i i bolic Logic a 2 ‘Symbolic Logic and Pr The term “logic” in this equation indicates the deseriptive somos a a Mati he description of the problem; the term ‘control’ indicates the pane tal 7 7 i soln aking the description of the problem as a point of departure. a t + ee i Hi aie " oe ha the algorithm is supposed to do and the control component ind ical a nd the contol’ ONC. The logic component fora specific problem is generally called a logic prog ‘a “Chi ompong employs logical deduction or reasoning for deriving new facts from the logic program, thus solyin, the given problem, This means that the user have to consider the logic only without having th, concem aver the control. ‘Whereas in conventional, imperative languages such as C++, Java and Pascal, a program is specification of a sequence of instructions to be executed, one after the other. This means that the user should describe both logic and control. The description of the problem is-incorporated implicitly in this specification, and usually it is not possible to clearly distinguish between the description of the problem and the method used for its solution. The following table shows the difference between procedural and declarative programming languages: Procedural: Declarative © Ail The declarative languages define WHAT the problem is or in other words WHAT the The procedural languages define HOW TO solve the problem or in other words HOW the output is obtained. output should be. ‘Program It is the sequence of commands. Itis the set of statements. | Advantages Fast, specialized programs General, readable programs. | ‘Examples C, Fortran, Ada, Java Prolog, Pure Lisp, Haskell, ML 43 LOGIC PROGRAMMING Logic programming isa programming paradigm based on mathematical logic. In this paradizm' Bepgranme specifies the relationships among data values (this constitutes a logic program) 2% ‘ a es Bs quan : the execution environment (usually an interactive interpreter) in order 10% See oe a ionstips hold. In logic programming, a program consists of a collectio" af defines a base of keovledee oreo ombolie logic, Patting this in another wa, a logic Pr $6 extracel. 'g¢ (through explicit facts and rules) from which implicit knowledg? Logic programmi : i propiain cloents od aa Sbecial type of declarative style of programming, in which the va ive program eae kbressed in the notations similar to that of predicat 108 is aie ethos allows the programmer to disregard the precise sequen answers, for example, the regia es fy jxceuted. In checking that the program sive makes 2 tue statement about only needs to check that each loge formula in He pe parts of the program, n, and need not worry about its relationship Wi"? soauction © Prolog ‘There are the tules of inference that allows anew sorantce that ifthe old formulas are true, the new oes can be expressed in purely nipulation that can be cartied out by a comy isi rogram, until it finds one that expresses the solution to the problein hy oh nee Fanulas in the program are true, then so are the formulas that the nach eine ese the ipeanswers it gives will be correct. To ensure thatthe program gives const gone the patel se checks that the program contains only true statements, and that it contai yy the . SSitons to be derived for all the problems that are of interest The vee one he p The progra concerned to ensure that the derivations the machine must carry out na eae ie rnachine can find answers quickly, this may affect the form in which definitions are made and properties are stated in the program, Nevertheless, each formula can be understood in isolation as. ‘atruc statement about the problem to be solved. Logic programming systems solve goals by systematically searching for a way to derive the answer from the program. If the conditions for a certain class of problems can be formalized within a suitable logic as a set of premises, and if a problem to be solved can be stated as a sentence in the logic, then a solution might be ‘found by constructing a formal proof of the problem statement from the premises. ‘Two interesting features of logic programs are non-determinism and backtracking, + Non-determinism: A non-deterministic program may find a number of solutions, rather than just one, to a given problem. ae to be derived from old ones with the can also be true. Because th os c ese rules of symbolic terms, applying them is the kind of symbol Backtracking: It is the mechanism that allows the exploration of potential alternative directions for solutions, when some direction, currently being investigated, fails to find an appropriate solution. Ua INTRODUCTION TO PROLOG (PROGRAMMING IN LOGIC) The PROLOG is a simple, yet powerful programming language, based on the principles of first order predicate logie=Ttis the most widely available language in the logic progrannine paradigm. Itisa logic language that is particularly used in programs that use non-num objects. Logic and therefore Prolog is based on the mathematical notions-of rélations and logical inference. Itis tuned outto be an excellent median’ for implementing symbolic (as against numerical) problems in genera, and linguistic structures in particular. It is a programming language that looks and works somes like formal logic, To be efficient and easy to use, Prolog omits some important features s fama logic. Prolog was the first reasonable, attempt to create & logic programming language. It 1s and relations between objects. Suited fo) i involve objects r solving problems that involve obj ation of the problem, meaning fic Using Prolog, the programmer gives a declarative “Perr onsists of a data base of rather than describing how to compute a solution, & PATE paid fr the given cts and logical relationships (rules) which describe the relations » lication, oor Ug ang (ia) . m1 eed not to tell the computer what to do, but To get intone se ermstion, the run time system searches through the data base asks a query. Sia (by logical deduetion) the answer. Prolog has a specific way of 0%: au aoe a sete knowledge database from top to bottom, processes clauses from lef fe, queries, it Sr eracking method to recover from bad choices. At the heart of Prolog ig ak, anal _ i sine that generates answers to questions posed by the user, based on the data aah ee ee Prolog uses the closed world assumption i.¢. an assumption that the date. aes ‘so if it cannot deduce and answer, then the answer is NO. Table 4.2: English vs, PROLOG 3 English i | PROLOG 5 Database = Ram is the father of Mohan. father(ram, mohan). | Seeta is the mother of Mohan. mother(seeta, mohan), | Sham is the father of Ram. father(sham, ram) | Person | is a parent of Person 2 if parent(personl, person2):- | Person 1 is a father of Person 2 or father(personl, person2), | Person 1 is the mother of Person 2. mother(person1, person2). | Person 3 is a grandparent of Person | if grandparent(person3, personl)~ { Person 3 is a parent of Person 2 and parent(person3, person2), } Person 2 is a parent of Person 1 arent(person2, person!). | zB “Queries = i Who is Mohan’s father? 2 father(Who. mohan). | Is Seeta the mother of Geeta? ?- mother(seeta, geeta). | Is Seeta the mother of Mohan? ?- mother(seeta, mohan). | | Does Sham have a grandchild? ?- grandparent(sham, _) | 4.5 DIFFERENCE BETWEEN PROLOG CONVENTIONAL PROGRAMMIN' LANGUAGES: The Prolog can be thought of as a declarative language ie. a Prolog program can be views! stating “what” is computed, independent of a particular method for computation. Convent. Programming Languages such as Pascal, on the other hand, is procedural, in which 2 P) definable only in terms of “how” it performs the computation. cd The Prolog programs are structured in terms of relations whereas traditional languaseS ns Most part are structured in terms of functions. The notions of calling a function, returning ® iliar to Prolog. Instead, Pr . are unfamiliar 8. ‘ad, Prolog expresses relation it 0g Pro ¢ ns among entities. This diffe: rendous result or Saree it means that variables play a completely ten ana than they do in cor a ional igeaas From this relational Structure, it follows that Prolog pro; ae are nondeterministic, since the several clements can be defined in a particular relation a i i eI pinees pa ae a Bie hokene are geared toward functions, (the relations in which one ce y 'erms of the others) the com; ically i ee ) putation proceeds deterministically in Unlike procedural languages, Prolog is a language in which statements are logical expressions. For the most part, the programmer is freed from having to worry about control structures and transfer of control mechanisms. This is done automatically in Prolog These properties of Prolog make it quite different from other i ; rake programming languages. Consequently, a different way of thinking about programs and programming is necessary in sing Prolog. Learning a new programming language can often be aided by analogy with previously eamed languages. But Prolog might be the most easily learned language, without the previous experience with other programming languages. ‘The PROLOG was designed in carly 1970s by Alain Colmerauer and P. Roussel at the University of Aix-Marseille (France), influenced by the ideas of Robert A. Kowalski that it is possible to use logic to represent knowledge and to write programs. More precisely, Prolog uses a subset of predicate logic and draws its structure from theoretical works of earlier logicians such as Herbrand (1930) and Robinson (1965) on the automation of theorem proving. The name of PROLOG has since then been connected with a new programming style, known as logic Programming. Kowalski’s research on the ficld of automated deduction led in 1972 to the development with Colmerauer of the use of formal logic as a programming language. Kowalski’s esearch provided the theoretical framework, while Colmerauer’s gave rise to the programming language Prolog. : Until the end of seventies, the use of PROLOG was limited to the academic world. Only after development of an efficient PROLOG interpreter and compiler by D.H.D. Warren and Fon Peteira, at the University of Edinburgh, the language entered the world outside the beat ists. interest in the language has increased steadily, However, PROLOG is still mainly used by ‘searchers, even though it allows for the development of serious and saan prograt ‘rection of the time needed to develop a C or Java program with similar functionality Then the several dialects of PROLOG were developed: * PROLOG * SWLPROLOG * Siestus-PROLOG * LPA-PROLOG. introduction to Prolog ; Sara In particular, Prolog has been Successfully applied in some robl solving of | equations, planning, databases, general prob Problem ngua; i a of pega ft ona Osten ax Qualitative simulation, architectural design, machine Jeaming, ¢ y tobi nn 2nd Natural language rocessi ae : snd reasoning, Patter matching, planning and sere ay P ing, Knowledge representation in the areas such as graphics or numerical algorithms, reps i areas such as symbolic Sm solving, prototyping, implementation * Artificial Intelligence: The fifth generation computers are based on the concept of Artificial Intelligence. Prolog is the best suited language for imparting artificial intelligence. It is helpful for making and converting simple English sentences very easily. * Building Expert Systems: In an expert system, facts and rules are described in the program ‘o form the ‘expert knowledge’, and a user can then query the program to obtain arse to the problems, given that certain facts or conditions are true. In Prolog, the facts and rales are used for the representation of knowledge, and the inference engine is used to derive conclusions. Prolog has a built-in backward chaining inference engine which can be used to partially implement some expert systems. Other portions of the system, such as the user interface, must be coded using Prolog as a programming language. The Prolog inference engine does simple backward chaining. Each rule has a goal and a ‘number of sub-goals. The Prolog inference engine either proves or disproves each goal. There is no aeraity associated with the results. The expressiveness of Prolog is due to three major Saas of the language: rule based programming, built-in pattern matching, bt pateecene., The Tule-based programming allows the program code to be written in a form w ae declarative than procedural. This is made possible by the builtin pattern match 2a backtracking, Together these features make it possible to elegantly implement ry ty of expert systems. . 2. by the * NASA’s Research: Due to the increase in popularity and useful a 7 ae ee Year 2005 NASA decided to use Prolog in its new satellites and rol otic t son i i The Robotics was programmed with.the help of Prolog so that they side "Tha fabotics oan pommands and work in the outer space with the help of these comman “nce be easily controlled. 2 vith the techniques for Game Playing: The Prolog in this field is geasidered with nee ate, We can Playing multiplayer logical games such as chess, minesweeper ane anodhbing pattern jPlement the program based upon the knowledge based app! 1owledge. | Symbolic Logic ang ssing: The Prolog is also suited to the processing of ter to understand ordinary Enel Chinese OF any euaee) because cach of these languages has its own syntax rules which can be sia. LE ee he the computer decide whether a group of yon nee a senten; " what it means. With the help of Prolog, the robotics can understand and interpret the me: of simple English sentences. So we can make them to understand our language ani y.° according to our commands. Natural Language Proce: Tanguage (trying to get a compu ae & FEATURES OF PROLOG The basic features of Prolog includes a powerful pattern-matching facil that searches for proofs, uniform data structures from which programs are built, and the genera interchangeability of input and output. Other features of Prolog make it a particularly powerful tj for building robust commercial applications. These features include interfaces to other languzes; and database products, stand-alone application generators, and more recently, support for techniges such as object-oriented and constraint based programming. ‘The declarative nature of logic programming with additional features such as object-oriew=i extensions and constraint-based techniques results in very efficient and powerful Prolog syst These facilities, together with increased integration to fourth-generation languages, are helpi Position Prolog as an effective information-management tool. By adding value to mainstream é=2 Processing environments, Prolog will provide the key to handling the complex informais _ Fepositories of the future. ‘The following points describe various features of PROLOG: . bent and Seana ‘Programs: One of the major advantages of Prolog is that it ass lementing very short and compact problem solving meth: ifficult problems, wi | are easily readable and understandable, ns nos fF dificult peble i + Uses Logical Variables: x . pattern matching wrth Prolog uses logical variables which takes the values bas! ® | fetter. ‘ymbols, The variables are indicated by an initial uppet = | For example, consider the folloy 9 father(X, rahul) Here X is the variabl imi f that can repre: eatin P? * Mitted Restrictions: in general we uicr above all, easy to read. The Protege NM Program, So we ean insert space ene + No need to write from having 10 ay onto mechanisms: For the js done automatically by Prolog. "t! Stuctures and transfer ce ory a EE Thsé er of control mec! - wing query: fo make our programs look nice and td Us almost full freedom in choosing the layou" New lines as it best suits our taste. got | I sroaction to Prolog Easy to Define an Interpreter: ‘We can defi Prolog itself. An interpreter is a me ; ine an interpreter for the extended language in interp ‘Program in which it uses other programs as d og ise tz r lata. By writing specialized interpreters, tis possible not only to cover new Tahdvabe constructs, but also to try out new evaluation Strategies for a language. : List Handling: It has built-in list handling techniques whi = which sequences, trees, and so on, iq ich are very useful for representing Non Determinis : It has the feature of hon-determinism, in which a program may find a number of solutions, rather than just one, toa given problem. Object Orientation: The Object-oriented ¢ attractiveness and expressive power of the |. flexible object-orientated development. xtensions to Prolog have increased the language which can provide a powerful and Constraints: The constraints replace Prolog’s usual pattern-matching mechanism which is the powerfull way to reduce the size of the search space and thus increase the efficiency of the scheduler. Backtracking: Prolog has a built-in backtracking search mechanism, so, unlike in conventional languages, execution can go both forward and back. When it goes backward, logical variables become unbound from their previous values, and the execution starts forward again and the variables can become bound to new'values. For example, the pattern “X-Y” can be used to pick games froma list of games, where the list of games looks like {1-2,1-3,...2-1, 2-3, ...]. If the game causes a constraint violation, backtracking will automatically lead to new values of X and Y as another possible game is selected. Recursion: Many languages support recursion, but in Prolog recursion plays an important role for many applications. 49 LIMITATIONS OF PROLOG! * Lacks functional notation: The obvious way to write a predicate is unlikely to be efficient. Because it lacks functional notation, predicates can become cumbersome. For example Consider the following expression: — f(N, F) :-N1 is N- 1, f(N1, FI), Fis N * Fl. Nwould be much nicer to write — f(N)=N * KN-1), "put ond Ouput ae are some features which are not standardized in Prolog, For ‘xample formatted input and output, file-handling, sorting predicates. tin * Impossible to Implement Arrays: You ean't r-assigh to parts of datas . Rakes it impossible to implement arrays. ; ccmn't allow Negative Facts: Prolog doesn’t allow foter®, A Rot able to use the direct statements that some! Wing sentence cannot be expressed in Prolog: : usions not” (negative) facts or cone not" (neat false For instanee, the = 20 Symbolic Logic and Prolsg — Ta light does not come on when we tum on its switch, but another light in the same oom comes on when we turn on its switch, we can conclude that it is false that thereis a power failure. + Limited form of Existential Quantification: Prolog doesn’t allow most facts or conclusions | bbaving existential quantification. Prolog does have a limited form of existential quantification ‘for local variables in rules. + Limited to First Order Logic: Prolog doesn’t directly allow second-order logic. predicate names as variables,

You might also like