1 / 17

Logic Programming (PROLOG)

Logic Programming (PROLOG). Taif University Fall 2009. Predicate Calculus. Prolog is actually based on predicate calculus. It can be used to represent a subset of predicate calculus. The main elements of predicate calculus are: Variables: These are the free unbound entities.

skah
Télécharger la présentation

Logic Programming (PROLOG)

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Logic Programming (PROLOG) Taif University Fall 2009

  2. Predicate Calculus • Prolog is actually based on predicate calculus. It can be used to represent a subset of predicate calculus. • The main elements of predicate calculus are: • Variables: These are the free unbound entities. • Constants: These include logical operators and names of related entities and the predicate, i.e. name of the relation. • Formulas: These are constructed from predicates and logical operators. A formula with no free variable is called a sentence.

  3. Logical Operators:

  4. Download SWI PROLOG • Google search “SWI Prolog”. • First hit is SWI Prolog home, (http://www.swi-prolog.org/). • On the left pane click on the Download link, (http://www.swi-prolog.org/Download.html). • From the available versions, download the stable release.

  5. Prolog Constants A constant is an atom or a number (integer or real). An atom can be: • A word that starts with a lower case letter followed by any letter, digit or underscore. • A quoted item i.e. ‘any thing between single quotes!’ • Symbol such as +, -, *, /, ^, \, <, >, :, ?, #, @, &, …. • Special item such as [], {}, ;, !, %, and , Examples: likes_apples, ‘What is there?’, ++*//, ::= • All predicate names must be constants.

  6. Rules of writing predicates in PROLOG • Must be an atom. • If the predicate has arguments, there should be no space between the predicate name and the left parenthesis. • Start the predicate name with small letters. • Every predicate must end with period followed by white space. • Arguments or objects of predicates with known or constant values must begin with small letters. • If the arguments of predicates are variables then they must begin with capital letters.

  7. Predicates A predicate may be defined by multiple clauses! (Same predicate name and same number of arguments.) Predicate arity is the number of its arguments. A predicate may have 0, 1, 2 or more arguments and is referred to as predicate_name/n where n is its arity.

  8. PROLOG Clauses • Clauses: Formulas that use only and, or, not, and if connectors. • Clauses consist of 2 parts, the head and the body. • Prolog clauses are known as horn clauses. • In a horn clause one conclusion (head) is followed by zero or more conditions (body).

  9. Simple (unit) clauses(Unconditional facts, with no body) • Amina is Ali’s mother. • Jeddah is the harbor of SA. • Ali is skinny • Omer broke Hassan’s hand. • mother(amina, ali). • harbor(sa, jeddah). • harbor_SA(jeddah). • jeddah_harbor_SA. • skinny(ali). • ali(skinny). (Which one???) • broke(omer, hassan, hand).

  10. Multiple definitions of a predicate (Disjunction, OR) • Square root of 4 is 2 or -2 • Square root of 9 is 3 or -3 • Omer will go to class, home or to the game. • square_root(4, 2). • square_root(4, -2). • square_root(9, 3). • square_root(9, -3). • will_go(omer, home). • will_go(omer, class). • will_go(omer, game).

  11. Rules in Prolog • A head followed by a body. • No more than one goal in the head. Example: A number is divisible by 2 if it is even: divisible_by_two(X):- even(X).

  12. Rules and Conjunction (and) • A man is happy if he is rich and famous. happy(Person):- man(Person), rich(Person), famous(Person). • Ali and Fatima are married if Ali’s wife is Fatima and Fatima’s husband is Ali. married(ali, fatima):- wife(ali, fatima), husband(fatima, ali).

  13. Rules and Conjunction (continued) • Anyone is an executive if he/she is an employee with a salary greater than $100,000: (i.e. The salary of an employee being >$100,000 implies that he/she is an executive) executive(Name, Salary):- employee(Name, Salary), Salary > 100000.

  14. Rules and Conjunction (continued) • A tourist is the one who travels from one country to another. tourist(Person):- travels(Person, Country1, Country2), not_same(Country1, Country2).

  15. Disjunction (OR) and Conjunction (AND) • A man is happy if he is rich or famous. happy(Person):- man(Person), rich(Person). happy(Person):- man(Person), famous(Person). • All Sudanese that belong to the Dinka or Nubian tribes are poor. poor(X):- country(X, sudan), tribe(X, dinka). poor(X):- country(X, sudan), tribe(X, nuba).

  16. Variable Scope • Two or more uses of the same name for a logical variable refer to the same Object only if they are within the same clause, like the use of Person, in the following. happy(Person):- man(Person), rich(Person). • In the following X in happy is different than the X in wise. happy(X):- healthy(X). wise(X):- old(X). • A logical variable cannot be overwritten with a new value.

  17. Declarative and procedural reading of a clause executive(Name, Salary):- employee(Name, Salary), Salary > 100000. Predicate reading: Anyone is an executive if he/she is an employee with a salary greater than $100,000. Procedural reading: One way to find an executive is first find an employee then verify that the salary of the employee is greater than $100,000.

More Related