Logic Programming (PROLOG) Taif University Fall 2009 Text Book Prolog Programming, A First Course, by Paul Brna, 2001 References: PROLOG A Relational Language and its Applications, by John Malpas, Prentice Hall Using Turbo Prolog, QUE Corporation (For PROLOG), by Yin K. and Solomon D Course Description This course will introduce The theory of functional and logic based approaches. Declarative programming will be introduced using the Prolog programming language. Course Objectives Students should: Become familiar with the basic syntax of Prolog language. Be able to give a declarative and procedural reading of a Prolog program. Be able to pursue any course that makes use of predicate calculus or Prolog. Course Policy Class participation and regular attendance is expected. Students are responsible for bringing themselves up-to-date on class material and assignments. Exams will be a combination of material presented in lectures, and homework problems. Homework and assignments should be completed by the due date. Student Evaluation Assignments and Homework 30% Midterm exam 20% Final examination 50% Introduction Types of programming languages Imperative languages: Describe the steps that are required to solve a given problem. The programmer must know an algorithm that tells the computer what to do. That is how to get the output from a given input. (Examples: C++, C#, Pascal, Algol, … etc.) Declarative languages: Declare the logic by which the program solves a problem (the logic of problem solving is declared in the program). The programmer must know what relations hold between various entities. (Examples: LISP, PROLOG, … etc.) PROLOG PROLOG: PROgramming in LOGic. It was developed from a foundation of logical theorem proving and originally used for research in natural language processing Its popularity has been mainly in the artificial intelligence (AI), where it has been used in expert systems, natural language, and intelligent databases. It is based on First Order Predicate Logic Elements of a PROLOG program Facts: A fact is an assertion that a particular relation holds. Examples on facts Ali owns the book. The relationship between Ali and the book is own. Abdu likes apples. The relationship between Abdu and apples is like. Fatima is female. The relationship between Fatimaand gender is female. Every mother loves her children. The relationship between mother and children is love. Amina is the mother of Ali. The relationship between Amina and Ali is mother. Fact as a Relation Relationship Object 1 Object 2 Own Ali Book Like Abdu Apples Female Fatima Gender Love Mother Child Mother Amina Ali Elements of a PROLOG program Rules: A rule is a fact whose truth value depends on the truth value of other facts.. Examples of rules Ahmed likes something if Omer likes it. (i.e. Ahmed likes anything that Omer likes) Two persons are sisters if both are female and they have the same parents. Suhaib will pass if he gets more than 60%. Elements of a PROLOG program Goals: A goal is what is to be determined. PROLOG attempts to match the goal with the facts and rules within the program. If the goal is a fact, PROLOG responds by its truth value. If goal contains variables, PROLOG responds with values that constitute a solution. Examples on goals What does Abdu like? What does Ahmed own? Is Fatima a female? Are Suha and Ruba sisters? FACT and RULES = Knowledge Base Facts: don’t need to be proved, accepted as is. Rules: Truth value depends on the truth value of the facts on which it depends. Predicate logic in PROLOG (i) " Every mother loves her children" (ii) Amina is the mother of Ali. From the above we can conclude: (iii ) Amina loves Ali We need to define a syntax to express the facts and formalize the rules: Predicates Facts Syntax Zahra is Female female(Zahra). Ali likes apples likes(Ali, apples). Ahmed owns the book owns(ahmed, book). Predicate name Predicate arguments (objects) Predicate End Indicator Example Rules Ahmed like anything Omer likes: likes(Ahmed, X):- likes(Omer, X). (:- Stands for if) X and Y are sisters if: X and Y are female and have same parents: sister_of(X, Y):female(X), female(Y), parents(X, F, M), parents(Y, F, M). The (,) stands for and Example Goals likes(Ali, X). owns(ahmed, Y). female(Fatima). sister_of(suha, ruba). sunny. This one can be a fact, a goal (a query!)