Prolog Logic Programming Language (Natural Language Processing) History of Prolog • • • • Prolog evolved out of research at the University of Aix-Marseille back in the late 60's and early 70's. Alain Colmerauer and Phillipe Roussel, both of University of Aix-Marseille, collaborated with Robert Kowalski of the University of Edinburgh to create the underlying design of Prolog as we know it today. Kowalski contributed the theoretical framework on which Prolog is founded while Colmerauer's research at that time provided means to formalize the Prolog language. 1972 is referred to by most sources as the birthdate of Prolog. Since its birth it has branched off in many different dialects. Two of the main dialects of Prolog stem from the two Universities of its origin: Edinburgh and Aix-Marseille. At this time the first Prolog interpreter was built by Roussel. The first Prolog compiler was credited to David Warren, an expert on Artificial Intelligence at the University of Edinburgh. To this day Prolog has grown in use throughout North America and Europe. Prolog was used heavily in the European Esprit programme and in Japan where it was used in building the ICOT Fifth Generation Computer Systems Initiative. The Japanese Government developed this project in an attempt to create intelligent computers. Prolog was a main player in these historical computing endeavours. Prolog became even more pervasive when Borland's Turbo Prolog was released in the 1980's. The language has continued to develop and be used by many scientists and industry experts. Now there is even and ISO Prolog standardization (1995) where all of its individual parts have been defined to ensure that the core of the language remains fixed. Prolog • Prolog (Programming in Logic) is a programming language based on Predicate Logic. It uses the restricted syntax of Horn Clauses: Predicates, e.g., researches(), etc. Operators <=, and, not Logical variables X, GP, etc. Constants $, professor, etc. Function names f(), left(), ... Facts employee(professor). rules procure(X) <= tenure(X). queries ? employee(professor). Quantification of logical variables is implicit -- universal for facts and rules, existential for queries. Predicate Logic • Introduction to Prolog, programming in logic Predicates (truth functions): p(), q(), odd(), parent(,), etc. Operators: Quantifiers: and (&, ×, .), or (∨, +), not (¬), =>, <=, <=> ∀ (for all), ∃ (there exists) Logical variables: X, Y, Child, Parent, etc. Constants: 123, charles, etc. Function names: f(), hcf(,), etc. e.g., ∀ C, P, GP, parent(C,P) and parent(P,GP) => grandParent(C,GP). Computation in Predicate Logic • A Prolog program attempts to prove a goal, such as brother(Barney,x), from a set of facts and rules. • In the process of proving the goal to be true, using substitution and the other rules of inference, Prolog substitutes values for the variables in the goal, thereby "computing" an answer. • How does Prolog know which facts and which rules to use in the proof? • Prolog uses unification to determine when two clauses can be made equivalent by a substitution of variables. • The unification procedure is used to instantiate the variables in a goal clause based on the facts and rules in the database. Horn Clause A clause (i.e., a disjunction of literals) is called a Horn clause if it contains at most one positive literal. Horn clauses are usually written as or where and is the only positive literal. A definite clause is a Horn clause that has exactly one positive literal. A Horn clause without a positive literal is called a goal. Horn clauses express a subset of statements of first-order logic. Programming language Prolog is built on top of Horn clauses. Prolog programs are comprised of definite clauses and any question in Prolog is a goal. Horn Clause • To simplify the resolution process in Prolog, statements must be expressed in a simplified form, called Horn clauses. • Statements are constructed from terms. • Each statement (clause) has (at most) one term on the left hand side of an implication symbol ( :- ). • Each statement has a conjunction of zero or more terms on the right hand side. • Prolog has three kinds of statements, corresponding to the structure of the Horn clause used. • A fact is a clause with an empty right hand side. • A question (or goal) is a clause with an empty left hand side. • A rule is a clause with terms on both sides. Terms • There are three kinds of terms in Prolog: • A constant is an atom or a number. An atom is a quoted character string or a string of letters, digits, and underscores that starts with a lower-case letter. A number resembles the real or integer constants used in most programming languages. • A variable is a string of letters, digits, and underscores that starts with an upper-case letter. There are no type declarations; types are discovered implicitly by the interpreter. • A structure represents an atomic proposition of predicate calculus, and has the form "atom(parameter list)". Facts and Rules • • • • • • The Prolog environment maintains a set of facts and rules in its database. Facts are axioms; relations between terms that are assumed to be true. Rules are theorems that allow new inferences to be made. Example facts: male(adam). female(anne). parent(adam,barney). Example rules: son(X,Y) :- parent(Y,X) , male(X) daughter(X,Y) :- parent(Y,X) , female(X) The first rule is read as follows: for all X and Y, X is the son of Y if there exists X and Y such that Y is the parent of X and X is male. • The second rule is read as follows: for all X and Y, X is the daughter of Y if there exists X and Y such that Y is the parent of X and X is female. Observations about Prolog Rules • The implication is from right to left! • The scope of a variable is the clause in which it appears. • Variables whose first appearance is on the left hand side of the clause have implicit universal quantifiers. • Variables whose first appearance is on the right hand side of the clause have implicit existential quantifiers. Executing a Prolog Program • To run a Prolog program the user must ask a question (goal) by stating a theorem (asserting a predicate) which the Prolog interpreter tries to prove. • If the predicate contains variables, the interpreter prints the values of the variables used to make the predicate true. • The interpreter uses backward chaining to prove a goal. It begins with the thing it is trying to prove, and works backwards looking for things that would imply it, until it gets to facts. Significant Language Features • Prolog is a rich collection of data structures in the language and human reasoning, and a powerful notation for encoding end-user applications. It has its logical and declarative aspects, interpretive nature, compactness, and inherent modularity. • Intelligent Systems - programs which perform useful tasks by utilizing artificial intelligence techniques. • Expert Systems - intelligent systems which reproduce decision-making at the level of a human expert. • Natural Language Systems - which can analyze and respond to statements made in ordinary language as opposed to approved keywords or menu selections. • Relational Database Systems Pros and Cons • Pros • • The "advantage" (such as it is) is just a different way of thinking about the world, and sometimes changing the way you ask a question provides a better tool for solving a problem. If your program can be stated easily as declarative formal logic statements, Prolog (or another language in that family) will give the fastest development time. One of the best times to use Prolog is when you have a problem suited to solving with backtracking. • Cons • • • Difficulties with compilation. To many different compilers. Slower with mathematical computations. • Prolog Compared to Other Languages • • • • • • • • • • • Prolog is a logic programming language that is declarative. Fortran, BASIC, and C are imperative programming languages. C++, Java, and VB.NET are object oriented programming languages. Statements in Prolog include rules and facts. Statements in other languages can be thought of as the smallest standalone element of an imperative programming language. Prolog is more like a database language, where facts already known to the program are verified. Lisp is more of a traditional language, where you enter data and the program performs calculations on it. Prolog uses a much more limited set of keywords. Lisp has a large number of keywords and built-in functions, so many types of programs can be easily created with it. Because it is a smaller language, Prolog is easier to learn. But because it handles a wider variety of tasks, Lisp is often easier to use. Areas of Application • • • • • • • • • • The technology is now in widespread use in a wide range of domains, including finance, defense, telecommunications, law, medicine, agriculture, engineering, manufacturing, and education. PigE/Auspig—and expert system for raising pigs, which won the award for the best presentation at The First International Conference on the Practical Applications of Prolog. MM4 Weather Modeling System Air Pollution Control System, developed using the Hungarian product MPROLOG RoadWeather is a 24-hour weather prediction system for snow and ice control on highways. Boeing Corp., the aircraft giant, is a major user of Prolog technology. Connector Assembly Specifications Expert (CASEy) is an expert system that guides shop floor personnel in the correct usage of electrical process specifications. A Generalized Query System, developed by Boeing Computer Services, uses a client/server architecture and provides a high-level abstraction mechanism for access to diverse sources of data and knowledge. John Dowding maintains Rotisserie/Fantasy Basketball leagues, keeps weekly NBA statistics in a Prolog database, and ranks NBA players and teams. IBM Watson question answering system won the JeopardyMan vs. Machine Challenge by defeating two former grand champions, Ken Jennings and Brad Rutter. Clarissa, a fully voice-operated procedure browser has been developed by the NASA Intelligent Systems Division. Sample Programs • • • • • • • • • • • • • • • • • • • • • • • • • • • • • Simple Prolog L.A. Monash Comp Sci 2/8/89 1: witch(X) <= burns(X) and female(X). 2: burns(X) <= wooden(X). 3: wooden(X) <= floats(X). 4: floats(X) <= sameweight(duck, X). 5: 6: female(girl). {by observation} 7: sameweight(duck,girl). {by experiment } 8: 9: ? witch(girl). 10: 11: {\fB After Monty Python (Sir Bedevere). \fP} 12: 13: --- end of parsing --witch(X) <= burns(X), female(X). , burns(X) <= wooden(X). , wooden(X) <= floats(X). , floats(X) <= sameweight(duck, X). , female(girl). , sameweight(duck, girl). ?witch(girl) --- running --witch(girl) yes --- finished --- • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • Simple Prolog L.A. Monash Comp Sci 2/8/89 1: orbits(mercury, sun). {facts} 2: orbits(venus, sun). 3: orbits(earth, sun). 4: orbits(mars, sun). 5: 6: orbits(moon, earth). 7: 8: orbits(phobos, mars). 9: orbits(deimos, mars). 10: 11: planet(P) <= orbits(P, sun). {rules} 12: satellite(S) <= orbits(S, P) and planet(P). 13: 14: ? satellite(S). {query} 15: 16: --- end of parsing --orbits(mercury, sun). , orbits(venus, sun). , orbits(earth, sun). , orbits(mars, sun). , orbits(moon, earth). , orbits(phobos, mars). , orbits(deimos, mars). , planet(P) <= orbits(P, sun). , satellite(S) <= orbits(S, P), planet(P). ?satellite(S) --- running --satellite(moon) yes satellite(phobos) yes satellite(deimos) yes --- finished --- Related Links • • • • • • • • • • • • http://www.cs.nmsu.edu/ALP/2011/03/natural-language-processing-with-prolog-in-the-ibm-watsonsystem/ http://www.cs.cmu.edu/Groups/AI/html/faqs/lang/prolog/prg/part1/faq.html http://www.swi-prolog.org/ http://www.dai.ed.ac.uk/groups/ssp/bookpages/quickprolog/quickprolog.html http://www.comp.mq.edu.au/units/comp248/prolog.html http://kti.ms.mff.cuni.cz/~bartak/prolog/index.html http://www.cs.bham.ac.uk/~pjh/prolog_course/sem242.html http://www.gprolog.org/ http://www.allisons.org/ll/Logic/Prolog/ http://www.csse.monash.edu.au/~lloyd/tildeLogic/Prolog.toy/Ch/ http://www.sics.se/isl/sicstuswww/site/index.html http://progopedia.com/language/prolog/