180 likes | 395 Views
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.
E N D
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. • 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.
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.
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.
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.
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.
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).
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).
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).
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).
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).
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.
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).
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).
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.
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.