LOGIC PROGRAMMING (WEEK 1) Eleni E. Mangina Department of Computer Science University College Dublin Introduction to Logic Distinction between Logic Programming and Conventional Programming Propositional & Predicate Calculi Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING (during a joint degree with Fudan University in Software Engineering) DEPT. OF COMPUTER SCIENCE UCD Lecture 1 Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING (during a joint degree with Fudan University in Software Engineering) DEPT. OF COMPUTER SCIENCE UCD General module information • Module consists of 24 lectures and 5 practicals • Final mark: 40% practical mark & 60% exam mark • All course material will be available on the web • Please note: this module starts slowly – but it SPEEDS UP!! Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING (during a joint degree with Fudan University in Software Engineering) DEPT. OF COMPUTER SCIENCE UCD LOGIC PROGRAMMING Course Objectives • To understand the placement of Logic Programming within different Programming Paradigms • To provide the student with an understanding of First Order Logic • To examine in detail the theories and principles which underpins automated deduction within First Order Logic • To compare a particular Logic Programming language, namely Prolog with the Pure Logical Foundations upon which it is based • To understand the operation of Prolog (not to become proficient programmers with the language) • To review application of Logic Programming • Review new developments in Logic Programming eg. Parallelism Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING (during a joint degree with Fudan University in Software Engineering) DEPT. OF COMPUTER SCIENCE UCD LOGIC PROGRAMMING Lecture 1-2-3 Objectives • To introduce the Importance of Logic Programming • To outline a Taxonomy of Logics • To distinguish between First Order Logics & Higher Order Logics • To review the fundamental ingredients of Predicate Calculus • To understand the basic inference rules & propoties associated with First Order Logic Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING (during a joint degree with Fudan University in Software Engineering) DEPT. OF COMPUTER SCIENCE UCD Introduction to Logic • ICOT (Institute for New Generation Computing) has identified logic as the core programming formalism for its fifth generation computers. • The suitability of logic for the representation of real world knowledge has been widly accepted. • In the Fifth Generation Programme numerous relevant techniques and technologies were investigated and this year has seen the announcement of its successor the Real World Computing (RWC) Programme. This 10 year strategic funding initiative to the value of some $400 million will include work on DAI, attention is given to the distribution and integration of information, autonomous and cooperative control and mechanisms for the integration of such functions as sensing, perception, planning and action. Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING (during a joint degree with Fudan University in Software Engineering) DEPT. OF COMPUTER SCIENCE UCD Advantages of Logic • Well understood, ideas of logic have been well refined. • Concise notation. • Mathematical deduction may be commissioned to deduce new knowledge from old knowledge. Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING (during a joint degree with Fudan University in Software Engineering) DEPT. OF COMPUTER SCIENCE UCD Disadvantages of Logic • Lots of notation and terminology. • Some people find programming in logic extremely difficult. It differs significantly from conventional programming in that the description of the logical structure of problems must be given as opposed to the description of how to solve them. Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING (during a joint degree with Fudan University in Software Engineering) DEPT. OF COMPUTER SCIENCE UCD Taxonomy of Logics I • Predicate Calculus, Predicate logic and first order logic are one and the same. • Predicate Calculus is an extension of Propositional Calculus. • Three particular subsets of Predicate Calculus, which avoid the full syntactic complexity of the aforementioned formalisms are Conjunctive Normal Form (CNF), Clausal Form, The Horn Clause Subset. Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING (during a joint degree with Fudan University in Software Engineering) DEPT. OF COMPUTER SCIENCE UCD Taxonomy of Logics II • Non-standard or higher order logics exist. Non-standard logic refers to any other logic other than those of Propositional or Predicate. These may be divided into: a) Those that compete with these Classical logics Multi-valued logic, Fuzzy logic, Intuitionistic logic b) Those which extend these Classical logics Modal logic, Temporal logic. Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING (during a joint degree with Fudan University in Software Engineering) DEPT. OF COMPUTER SCIENCE UCD Propositional Calculus Consists of: a) Propositions b) Constants The essential difference between Propositional Calculus and Predicate Calculus are the latter:- 1) Incorporates Variables 2) Permits Quantification Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING (during a joint degree with Fudan University in Software Engineering) DEPT. OF COMPUTER SCIENCE UCD NEWELL & SIMON All sciences characterize the essential nature of the systems they study. These characteristics are invariably qualitative in nature, for they set the terms with which more detailed knowledge can be developed… The study of logic and computers has revealed to us that intelligence resides in physical symbol system. This is computer science’s most basic law of qualitative structure. Symbol systems are collections of patterns and processes, the latter being capable of producing, destroying and modifying the former. The most important property of patterns is that they can designate objects, processes or other patterns and that when they designate processes they can be interpreted… A second law of qualitative structure for artificial intelligence is that symbol systems solve problems by generating potential solutions and testing them – that is by searching. Solutions are usually sought by creating symbolic expressions and modifying them sequentially until they satisfy the conditions for a solution. ACM Turing Award Lecture (1976) Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING (during a joint degree with Fudan University in Software Engineering) DEPT. OF COMPUTER SCIENCE UCD Physical Symbol System Hypothesis Intelligent activity, in either human or machine, is achieved through the use of: 1. Symbol patterns to represent significant aspects of a problem domain 2. Operations on these patterns to generate potential solutions to problems 3. Search to select a solution from among these possibilities Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING (during a joint degree with Fudan University in Software Engineering) DEPT. OF COMPUTER SCIENCE UCD Predicate Calculus P: “it rained on Wednesday” P:”weather (wednesday, rain) Variable: X Weather(X, rain) Predicate Calculus Symbols: The alphabet that makes up the symbols of the predicate calculus consists of: 1. The set of letter, both upper- and lowercase of the English alphabet 2. The set of digits 0,1,…,9 3. The underscore, _. Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING (during a joint degree with Fudan University in Software Engineering) DEPT. OF COMPUTER SCIENCE UCD Predicate Calculus Symbols and terms: Predicate calculus symbols include: 1. Truth symbols, true or false 2. Constant symbols are symbol expressions having the first character lowercase 3. Variable symbols are symbol expressions beginning with an uppercase character 4. Function symbols are symbol expressions having the first character lowercase Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING (during a joint degree with Fudan University in Software Engineering) DEPT. OF COMPUTER SCIENCE UCD Predicate Calculus Propositional Calculus Symbols: • Symbols of: P, Q, R, … • Truth symbols: true, false • Connectives: ^, , ¬, , Propositional Calculus Sentences: • Every propositional symbol and truth symbol: true, P, Q, … • The negation of a sentence: ¬P, ¬false • The conjunction, or and of two sentences: P^ ¬P • The disjunction, or or of two sentences: P ¬P • The implication of one sentence for another: P Q • The equivalence of two sentences: PQR Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING (during a joint degree with Fudan University in Software Engineering) DEPT. OF COMPUTER SCIENCE UCD Truth Tables Truth table lists all possible truth value assignments to the atomic proposition of an expression and gives the truth value of the expression for each assignment. Truth table enumerates all possible worlds of interpretation. ¬(¬P) = P (P Q) = (¬P Q) The contra positive law: (P Q) = (¬Q ¬P) De Morgan’s law: ¬(P Q) = (¬P^¬Q) Commutative law: (P^Q) = (Q^ P) Distributive law: P^(Q R) = (P^Q) (P^R) P Q P^Q T T T T F F F T F F F F Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING (during a joint degree with Fudan University in Software Engineering) DEPT. OF COMPUTER SCIENCE UCD Examples • John loves none but Mary ¬Y (loves(John, Mary) ^ loves(John,Y) ^(YMary) ¬Y (loves(John, Y)) ^ (¬ Y=Mary) Y ¬loves(John,Y) Y=Mary Y ¬loves(John,Y) Y=Mary • Everybody loves exactly one person X Y loves(X,Y) ^ (Z loves(X,Z) Y=Z) X Y (Z loves(X,Z) Y=Z) • There are at least two (different) X such that A(X) X Y (X Y ^ A(X) ^ A(Y)) • There is exactly one X such that A(X) X A(X) ^ (A(Y) Y=X) X Y (A(Y) Y=X A(Y) Y=X ^ Y=X A(Y) Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING (during a joint degree with Fudan University in Software Engineering) DEPT. OF COMPUTER SCIENCE UCD Examples • He who is late is to be punished X isLate(X) punished(X) • Boys who are late are to be punished Y (isLate(Y) ^ isBoy(Y) punished(Y) • Everyone here is older than everyone there X,Y (isHere(X) ^ isThere(Y)) olderThan(X,Y) X isHere(X) ¬ (Y isThere(Y) ^ olderThan(Y,X) Y ¬ isThere(Y) ¬olderThan(Y,X) Y isThere(Y) olderThan(Y,X) • Everyone admires himself X human(X) admire(X,X) • John loves Mary but Mary loves someone else Loves(John,Mary) ^ Y loves(Mary,Y) ^ (YJohn) Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING (during a joint degree with Fudan University in Software Engineering) DEPT. OF COMPUTER SCIENCE UCD Examples Example of the use of predicate calculus to describe a simple world. The domain of discourse is a set of family relationships in a biblical genealogy: mother (eve,abel) = Eve is the mother of Abel mother(eve,cain) = … father(adam, abel) = … father(adam,cain) = … In this example we use the predicates mother and father to define a set of parent – child relationships. The implication give general definitions of other relationships inference rules! parent(X,Y) ^ parent(X,Z) sibling(Y,Z) YZ parent(X,Y) father(X,Y) mother(X,Y) Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING (during a joint degree with Fudan University in Software Engineering) DEPT. OF COMPUTER SCIENCE UCD Examples If Jones envies Smith or vice versa but they do not envy each other, then Jones envies Smith, if and only if Smith does not envy Jones. 1. ((Jones envies Smith) (Smith envies Jones)) ^ 2. ¬ ((Jones envies Smith) ^ (Smith envies Jones)) 3. (Jones envies Smith) 4. ¬ (Smith envies Jones) (AB) ^ (¬ (A^B) (A ¬ B) (A B) ^ (¬ (A ^ B) (A ¬ B) T T T F F T T T T T F F T T F T T T F F T T T T F T T T T F F T T F T F F F F F T F F F T F F T Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING (during a joint degree with Fudan University in Software Engineering) DEPT. OF COMPUTER SCIENCE UCD Lecture 2 Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING (during a joint degree with Fudan University in Software Engineering) DEPT. OF COMPUTER SCIENCE UCD A Logic-Based Financial Advisor 1. 2. 3. The function of the advisor is to help a user decide whether to invest in a savings account or the stock market. Some investors may want to to split their money between the two. The investment that will be recommended for individual investors depends on their income and the current amount they have saved according to the following criteria: Individuals with an inadequate savings account should always make increasing the amount saved their first priority, regardless of their income. Individuals with an adequate savings account and an adequate income should consider a riskier but potentially more profitable investment in the stock market. Individuals with a lower income who already have an adequate savings account may want to consider splitting their surplus income between savings and stocks, to increase the cushion in savings while attempting to increase their income through stocks The adequacy of both savings and income is determined by the number of dependents an individual must support. Our rule is to have at least $5000 in the bank for each dependent. An adequate income must be a steady income and supply at least $15000 per year plus an additional $4000 for each dependent. Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING (during a joint degree with Fudan University in Software Engineering) DEPT. OF COMPUTER SCIENCE UCD Solution • First task: Determine the major features savings_account and income (adequate and inadequate arguments): savings_account(adequate) savings_account(inadequate) income(adequate) income(inadequate) investment (stock, savings and combination arguments) • Second task: Determine the rules First rule: (1) savings_account(inadequate) investment(savings) Second rule: (2) savings_account(adequate) ^ income(adequate) investment(stocks) Third rule: (3) savings_account(adequate) ^ income(inadequate) investment(combination) Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING (during a joint degree with Fudan University in Software Engineering) DEPT. OF COMPUTER SCIENCE UCD Solution (cont.) • To determine the minimum adequate savings the function minsavings is defined: (4) X amountSaved(X) ^ Y (dependents(Y) ^greater(X,minsavings(Y)) savings_account(adequate) (5) X amountSaved(X) ^ Y (dependents(Y) ^greater(X,minsavings(Y)) savings_account(inadequate) Where: minsavings(X) = 5000*X and minincome(X) = 15000 + (4000*X) (6) X earnings(X,steady) ^ Y (dependents(Y) ^greater(X,minincome(Y)) income(adequate) (7) X earnings(X,steady) ^ Y (dependents(Y) ^ greater(X,minincome(Y)) income(inadequate) (8) X earnings(X,unsteady) income(inadequate) In order to perform a consultation, a description of a particular investor is added to this set of predicate calculus sentences using the predicates amountSaved, earnings and dependents. Thus an individual with 3 dependents, $22000 in savings and a steady income of $25000 would be described by: Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING (during a joint degree with Fudan University in Software Engineering) DEPT. OF COMPUTER SCIENCE UCD Solution (cont.) • • • • • • amount_saved(22000) earnings(25000, steady) dependents(3) minsavings(3) = 15000 minincome(3) = 27000 Earnings(25000,steady) ^ dependents(3) ^ greater(25000,minincome(3)) income(inadequate) • Amount_saved(22000) ^ dependents(3) ^ greater(22000,minsavings(3)) savings_account(adequate) • Savings_account(adequate) ^ income (inadequate) investment(combination) This is the suggested investment for this individual! Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING (during a joint degree with Fudan University in Software Engineering) DEPT. OF COMPUTER SCIENCE UCD Predicate Calculus Predicate Calculus is comprised of the following basic building blocks: a) Predicates b) Functions c) Constants d) Variables e) Connectives f) Quantifiers Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING (during a joint degree with Fudan University in Software Engineering) DEPT. OF COMPUTER SCIENCE UCD Predicates, Constants, Variables & Functions I A logic program is a collection of sentences. Sentences are constructed from predicates, constants, variables, functions, formulae, connectives and quantifiers. Predicates are chosen from a particular vocabularly. Some examples are... hates (greg, computer_science) Predicates Constant arguments loves (greg, cakes) The argument greg must denote a particular object. Thus we assume that it denotes a particular person called greg. The object denoted by a term does not have to be a person it could be a class, an event, a thing depending on the domain. Once, however, the context of a term has been decided it remains fixed. If we decided greg refers to greg o’hare then it cannot be concluded that greg howard hates computer science or that all gregs love cake. Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING (during a joint degree with Fudan University in Software Engineering) DEPT. OF COMPUTER SCIENCE UCD Predicates, Constants, Variables & Functions II The arguments of a predicate can be either a) Constants b) Variable c) Function is_a (greg, lecturer) greg and lecturer are constants, they refer to a particular object or class of object. is_a (X, lecturer) is a predicate which has as terms a variable X and a constant lecturer. Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING (during a joint degree with Fudan University in Software Engineering) DEPT. OF COMPUTER SCIENCE UCD Predicates, Constants, Variables & Functions III At some stage a variable may take a particular value, say greg. The variable would then be said to have been instantiated. Consider the following predicates... loves (greg, eating_icecream, hot) loves (greg, home) loves (Y, sleeping, morning) An incompatibility exists, loves takes differing number of arguments, with the second argument taking a distinct meaning in each case. Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING (during a joint degree with Fudan University in Software Engineering) DEPT. OF COMPUTER SCIENCE UCD Predicates, Constants, Variables & Functions IV If we wished to use both forms we would have to introduce unique predicates. loves (greg, sister (mandy)) sister (mandy) is a function which refers to an object that is the sister of mandy. loves (greg, sweet_and_fattening (X)) greg loves any object that is sweet and fattening. supports (chris, topoftable (X)) chris supports an object (team) that is at top of the table. loves_action (T, eating_icecream, hot) loves_object (greg, home) loves_action (Y, sleeping, morning) Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING (during a joint degree with Fudan University in Software Engineering) DEPT. OF COMPUTER SCIENCE UCD Predicates, Constants, Variables & Functions V Note • Number of arguments can vary • Once number, order and interpretation decided at the outset it must be maintained throughout. In general because the meaning or denotation of predicate calculus is dependent upon the meaning of its parts, it is said to exhibit compositional semantics. Consequently when introducing a function or predicate the number and meaning and indeed order of each of its predicates must be stated exactly. Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING (during a joint degree with Fudan University in Software Engineering) DEPT. OF COMPUTER SCIENCE UCD Connectives & Quantifiers I We combine formuals by use of connectives. Within Predicate Calculus these are as follows... AND OR NOT ~ IF IF AND ONLY IF (IFF) Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING (during a joint degree with Fudan University in Software Engineering) DEPT. OF COMPUTER SCIENCE UCD Connectives & Quantifiers II We will concentrate on the first four of these, suffice to note that the distinction between IF and IF and only IF is illustrated in the following truth table... p T T F F q T F T F Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING (during a joint degree with Fudan University in Software Engineering) p -> q T F T T p <-> q T F F T DEPT. OF COMPUTER SCIENCE UCD Connectives & Quantifiers III • It should be noted that p -> q is equivalent to ~p v q This can be verified. • Furthermore such compound predicates are Commutative p,q=q,p Distributive p , (q v r) = (p , q) v (p , r) p v (q , r) = (p v q) , (p v r) Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING (during a joint degree with Fudan University in Software Engineering) DEPT. OF COMPUTER SCIENCE UCD Connectives & Quantifiers IV • Associative p , (q , r) = (p , q) , r p v (q v r) = (p v q) v r • Obey De Morgan’s Laws ~(p , q) = ~p v ~q ~(p v q) = ~p , ~q • Annihilation ~(~p) = p Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING (during a joint degree with Fudan University in Software Engineering) DEPT. OF COMPUTER SCIENCE UCD Example Formula shy (paula) , pretty (paula) -> attractive(paula) equal_sides (Shape) , equals (angles , 4) , equal_angles (Shape) -> square(Shape) wears (X , tanktop) v ~(trendy (X)) -> computer_scientist (X) thief (X) , valuable (Y) -> steal (X , Y) Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING (during a joint degree with Fudan University in Software Engineering) DEPT. OF COMPUTER SCIENCE UCD Connectives & Quantifiers V • Quantifiers in Predicate Calculus indicate the number of instantiations of a variable that need to be true in order that the entire proposition is true. They come in two forms... a) Universal quantifier b) Existential quantifier (X) - all instantiations must be true in order for entire proposition to hold true. (X) - only some instantiations (at least one) need be true. (X) (mortal (X) -> will_die (X)) (Y) (rectangle (Y) , square (Y)) Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING (during a joint degree with Fudan University in Software Engineering) DEPT. OF COMPUTER SCIENCE UCD Quantifier Order • If both universal and existential quantifiers are commissioned in the same proposition the ordering may prove significant. According to Alty and Coombs a typical such example may be (X) (Y) (employee (X) -> manager (Y, X)) - for every X there exists a Y which manages them. (Y) (X) (employee (X) -> manager (Y , X)) - there exists a manager who manages every employee. Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING (during a joint degree with Fudan University in Software Engineering) DEPT. OF COMPUTER SCIENCE UCD What exactly are Formulas? Well formed formulas (wff) are formed recursively by applying one or more of the following rules. a) If P is a 0-ary predicate, P is a wff b) If P is an n-ary predicate and term1...termN are terms then P(t1...tn) is a wff. c) Wffs connected by the connectives V , |, ->, <-> are well formed formulas. d) If P is a wff then ~(P) is a wff. e) Any wff surrounded by either a universal or existential quantifiers are wff. Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING (during a joint degree with Fudan University in Software Engineering) DEPT. OF COMPUTER SCIENCE UCD Some Terminology • A sentence is a wff in which all variables are bound. That is none are free. • A clause is a wff consisting of a disjunction of terms. Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING (during a joint degree with Fudan University in Software Engineering) DEPT. OF COMPUTER SCIENCE UCD Things to Do ! • Experiment expressing relationships between objects in Predicate Calculus. Describe the world of your bedroom as a series of sentences in Predicate Calculus • Find out clearly what is meant by Second Order Logics • Become confident with the distinction between Propositional and Predicate Logic • Expand your notes on Monotonic and NonMonotonic Logic Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING (during a joint degree with Fudan University in Software Engineering) DEPT. OF COMPUTER SCIENCE UCD Lecture 3 Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING (during a joint degree with Fudan University in Software Engineering) DEPT. OF COMPUTER SCIENCE UCD Scope of Variables I Consider the following proposition (P) (X) (train_to (X, london) -> ( (Y) (first_class_carriage (Y) v second_class_carriage (Y)) , (Z) (buffer_car (Z)) , will_become (X , manchester_train))) Some of quantifiers generally follows laws observed in say Pascal. In the above... Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING (during a joint degree with Fudan University in Software Engineering) DEPT. OF COMPUTER SCIENCE UCD Scope of Variables III Scope X - A and any proposition nested within, namely B and C Scope Y - B and any propositions nested within Scope Z - C and any propositions nested within Scope P - free variable unbounded Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING (during a joint degree with Fudan University in Software Engineering) DEPT. OF COMPUTER SCIENCE UCD Automated Deduction I To process knowledge represented in predicate logic we must be able to take a given set of assertions and implications (or facts and rules as they are often referred to) and be able to infer new facts and rules based on this corpus of knowledge. In addition such an automated technique should perform it’s inferences in such a manner that it’s conclusions can be viewed with confidence. There exists many inference rules which include: Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING (during a joint degree with Fudan University in Software Engineering) DEPT. OF COMPUTER SCIENCE UCD Inference Rules I 1) Modus Ponendo Ponens (MPP) A -> B If we know A we can conclude B 2) Modus Tollendo Tollens (MTT) A -> B If we know ~B then we can conclude ~A 3) Double Negation (DN) A We know ~(~A) Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING (during a joint degree with Fudan University in Software Engineering) DEPT. OF COMPUTER SCIENCE UCD Inference Rules II 4) And Introduction (&I) A B We can conclude A , B 5) Reductio Ad Absurdum (RAA) A -> B A ->~B We conclude ~A Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING (during a joint degree with Fudan University in Software Engineering) DEPT. OF COMPUTER SCIENCE UCD Inference Rules III 6) Universal Specialisation (US) (X) W(X) , A We conclude W(A) eg. Unreliable (Computers) Vax Computer Unreliable(Vax) Where A is a specific instance of X Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING (during a joint degree with Fudan University in Software Engineering) DEPT. OF COMPUTER SCIENCE UCD The Choice of Inference Rule Often a single inference rule or a group of inference rules may be commissioned. Two problems prevail: 1) Choice of inference rule Which rule to employ at a particular instance. 2) Generation of numerous uninteresting propositions How to direct or focus deduction? Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING (during a joint degree with Fudan University in Software Engineering) DEPT. OF COMPUTER SCIENCE UCD Resolution I Having encoded real world knowledge in the form of predicate calculus the next stage is that of utilising the advantages the formalism affords. The precise, unambiguous and formal nature of the logic make it easy to automate the deductive process. Many Computer Scientists argue high-level languages and notations sholud be natural and easy to use this is hardly the case with logic. One method of overcoming these is to use a more general technique. One such technique is that of Resolution Theorem Proving, and it is this very technique which is employed within PROLOG. Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING (during a joint degree with Fudan University in Software Engineering) DEPT. OF COMPUTER SCIENCE UCD Resolution II Resolution reduces the proof procedure to the application of a single rule. Resolution is a procedure that carries out the variety of processes involved when reasoning with a series of predicate calculus statements. Resolution produces proofs by Refutation. That is to say in order to prove a statement, it attempts to show that the negation of the statement produces a contradiction, with the existing facts and rules. Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING (during a joint degree with Fudan University in Software Engineering) DEPT. OF COMPUTER SCIENCE UCD Resolution - The Algorithm In generalist form it involves...(assume we want to prove A) 1) Assume ~A True. 2) Show ~A and axioms lead to us concluding something is True that cannot be True. 3) Result ~A cannot be true since given rise to a contradiction. 4) Conclude A since ~A cannot be True. In order to execute efficiently Resolution operates upon statements which have been converted to a very convenient standard form, namely Clausal form. Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING (during a joint degree with Fudan University in Software Engineering) DEPT. OF COMPUTER SCIENCE UCD A Problem Yellow Green D B C A H E F Blue Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING (during a joint degree with Fudan University in Software Engineering) DEPT. OF COMPUTER SCIENCE UCD The Conversion Process A Worked Example Consider the following well formed formula... (X) [P(X) -> ( (Y) [Q(X,Y), R(Y)] , ~ (Y) [Q(X,Y), Q(Y,X)] , (Y) [~P(Y) -> ~S(X,Y)])] We will now work through the nine stages involved in converting this Predicate Calculus sentence to Clausal Form. Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING (during a joint degree with Fudan University in Software Engineering) DEPT. OF COMPUTER SCIENCE UCD The Conversion Process Converting Predicate Calculus sentences to the normalised form of Clausal form involves a sequence of nine steps. These steps are:1. Eliminate Implications 2. Move Negations Down to Atomic Formula 3. Eliminate Existential Quantifiers 4. Rename Variables 5. Move Universal Quantifiers to the Left 6. Convert Matrix into a Conjunction of Disjuncts 7. Eliminate Conjunctions 8. Rename Variables 9. Purge Universal Quantifiers Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING (during a joint degree with Fudan University in Software Engineering) DEPT. OF COMPUTER SCIENCE UCD Eliminate Implications I Remove implications by replacing a -> b by ~a v b Two nested implications exist here. Where more than one implication exists we always remove innermost first. Work from the inside out. Removing first the innermost implication gives.... (X) [P(X) -> ( (Y) [Q(X,Y), R(Y)] , ~ (Y) [Q(X,Y), Q(Y,X)] , (Y) [~(~P(Y)) v ~S(X,Y)])] Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING (during a joint degree with Fudan University in Software Engineering) DEPT. OF COMPUTER SCIENCE UCD Eliminate Implications II Then removing the outermost... (X) [~P(X) v ( (Y) [Q(X,Y), R(Y)] , ~ (Y) [Q(X,Y), Q(Y,X)] , (Y) [~(~P(Y)) v ~S(X,Y)])] Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING (during a joint degree with Fudan University in Software Engineering) DEPT. OF COMPUTER SCIENCE UCD Move Negations Down to the Atomic Formula I Reduce scope of negation by employing the following rules... ~(~P) = P (1) ~(a , b) = ~a v ~b (2) ~(a v b) = ~a , ~b (3) and ~ (X) P(X) = (X) ~ P(X) (4) ~ (X) P(X) = (X) ~ P(X) (5) Rules (2) and (3) are those of De Morgans laws Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING (during a joint degree with Fudan University in Software Engineering) DEPT. OF COMPUTER SCIENCE UCD Move Negations Down to Atomic Formula II Consequently we get... (X) [~P(X) v ( (Y) [Q(X,Y), R(Y)] , (Y) [~Q(X,Y) v ~Q(Y,X)]* , (Y) [P(Y) v ~S(X,Y)]) ** ] * From (2) and (5) ** From (1) Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING (during a joint degree with Fudan University in Software Engineering) DEPT. OF COMPUTER SCIENCE UCD Eliminate Existential Quantifiers I If a variable contains an existentially quantified variable it means there is a certain value of the variable which will make the formula true. We may eliminate the quantifier by substituting a reference to a function that will produce the required value. We do not need to know how the function works or the form it will take. All we need to know is that such a function will exist. Consider the following simple example: (Y) Lecturer (Y) could be replaced by Lecturer (f1) Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING (during a joint degree with Fudan University in Software Engineering) DEPT. OF COMPUTER SCIENCE UCD Skolem Constants Lecturer (f1) f1 is a function with no arguments, sometimes called a Skolem Constants that produces a value that satisfies lecturers. More particularly if existential quantifiers exist within the scope of universal quantifiers the value that satisfies the predicate may depend on the value of the universal quantifier. eg (X) (Y) father_of (Y, X) Thus the value y depends on the value x thus substitute function f2 which returns the appropriate value given the dependence on x (i.e. accepts x as an argument to the function) (X) father_of( f2(X), X) Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING (during a joint degree with Fudan University in Software Engineering) DEPT. OF COMPUTER SCIENCE UCD Skolem Functions These functions are known as Skolem Functions. In general the process of removing existential quantifiers is known as Skolemization after Thoraf Skolem the logician. In our particular wff we only have one occurence of an existential quantifier namely Y. The scope of Y lies within that of the universal quantification of X, and consequently we replace Y by a Skolem function lets call it f1. The value f1 returns is dependent upon the value of X. Thus we introduce the skolem function f1(X). This function returns the appropriate value of Y dependent upon the value of X. Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING (during a joint degree with Fudan University in Software Engineering) DEPT. OF COMPUTER SCIENCE UCD Examples of Skolemisation Consider the following WFF’s (X) (Q) (Z) related_to (X,Z,Q) => (X) (Q) related_to (X, g(X,Q),Q) What if the WFF looked like... (Z) (X) (Q) related_to (X,Z,Q) => (X) (Q) related_to (X,g,Q) Where g is a skolem constant in the latter case and a skolem function in the former. Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING (during a joint degree with Fudan University in Software Engineering) DEPT. OF COMPUTER SCIENCE UCD Skolemising Hence returing to our wff skolemising results in... (X) [ ~P(X) v ([Q(X, f1(X), R(f1(X))] , (Y) [~Q(X,Y) v ~Q(Y,X)] , (Y) [P(Y) v ~S(X,Y)]) ] Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING (during a joint degree with Fudan University in Software Engineering) DEPT. OF COMPUTER SCIENCE UCD Rename Variables Rename variables so that each quantifier within each expression has a unique name. For example the formula: (X) rich(X) v (X) tall(X) Note : Different X’s used, not the same variable (cf scope). Could become (X) rich(X) v (Y) tall(Y) Thus each quantifier binds to a unique variable. Since variables only dummy names this won’t affect true value of formula. The logical equivalence of the formula will be preserved. Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING (during a joint degree with Fudan University in Software Engineering) DEPT. OF COMPUTER SCIENCE UCD Rename Variables II Having performed Stage 4 our wff looks like... (X)[~P(X) v ([Q(X, f1(X)), R(f1(X))] , (Y) [~Q(X,Y) v ~Q(Y,X)] , (Z) [ P(Z) v ~S(X,Z)] ) ] Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING (during a joint degree with Fudan University in Software Engineering) DEPT. OF COMPUTER SCIENCE UCD Move Universal Quantifiers to the Left I This may be permitted since each quantifier now has a unique name and consequently the leftward migration of the universal quantifiers will not cause any associated confusion. Our wff now simply becomes ...... (X), (Y), (Z) [ ~ P(X) v ( [ Q(X,f1(X)), R(f1(X)) ] , [ ~ Q(X,Y) v ~ Q(Y,X)] , [ P(Z) v ~ S(X,Z)] ) ] Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING (during a joint degree with Fudan University in Software Engineering) DEPT. OF COMPUTER SCIENCE UCD Move Universal Quantifiers to the Left II At this stage the formula is said to be in PRENEX NORMAL FORM. Such formula consists of a set of prefixing quantifiers followed by a quantifier free Matrix. Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING (during a joint degree with Fudan University in Software Engineering) DEPT. OF COMPUTER SCIENCE UCD Convert Matrix to a Conjunction of Disjuncts I In simpler terms this merely involves moving OR’s inside AND’s This involves the commissioning of the Distributive (Dist) and Associative (Ass) laws thus:- Dist Ass p v (q,r) (p v q), (p v r) p v (q v r) (p v q) v r pvqvr Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING (during a joint degree with Fudan University in Software Engineering) DEPT. OF COMPUTER SCIENCE UCD Convert Matrix into a Conjunction of Disjuncts II Firstly, distributing P(X) we get (X), (Y), (Z) [(~ P(X) v ( [Q(X) f1(X)), R(f1(X)) ] ) , ( ~ P(X) v [ ~ Q(X,Y) v ~ Q(Y,X) ] , ( ~ P(X) v [ P(Z) v ~ S(X, Z) ] ) ) ] Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING (during a joint degree with Fudan University in Software Engineering) DEPT. OF COMPUTER SCIENCE UCD Convert Matrix into a Conjunction of Disjuncts III Employing Distributive and Associative laws..... (X), (Y), (Z) [ ~ (P(X) v Q(X, f1(X)) , ( ~ P(X) v R(f1(X)) , ( ~ P(X) v ~ Q(X,Y) v ~ Q(Y,X)) , ( ~ P(X) v P(Z) v ~ S(X,Z)) ] The wff is said to be in Conjunctive Normal Form (CNF). This is a particular normalised form of Predicate Calculus. CNF represents the original wff as a conjunction of disjunctions of literals. Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING (during a joint degree with Fudan University in Software Engineering) DEPT. OF COMPUTER SCIENCE UCD Eliminate the Conjunctions Remove the explicit conjunctions, writing each as if it were a separate axiom. Each separate conjunct is called a Clause. We can do this since in a conjunction all parts must be true in order for the entire wff to be true. Hence each component can be stated as a separate axiom. This produces ..... (X) [ ~ P(X) v Q(X, f1(X)) ] (X) [ ~ P(X) v R(f1(X)) ] (X), (Y) [ ~ P(X) v ~ Q(X,Y) v ~ Q(Y,X) ] (X), (Z) [ ~ P(X) v P(Z) v ~ S(X,Z) ] Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING (during a joint degree with Fudan University in Software Engineering) DEPT. OF COMPUTER SCIENCE UCD Rename Variables We rename variables so that each clause references different variables. Giving: (X) [ ~ P(X) v Q(X, f1(X)) ] (W) [ ~ P(W) v R(f1(W))] (U), (V) [ ~ P(U) v ~ Q(U,V) v ~ Q(V,U)] (A), (B) [ ~ P(A) v P(B) v ~ S(A,B) ] Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING (during a joint degree with Fudan University in Software Engineering) DEPT. OF COMPUTER SCIENCE UCD Purge Universal Quantifiers Remove universal quantifiers so that these become implicit. [ ~ P(X) v Q(X, f1(X)) ] [ ~ P(W) v R(f1(W)) ] [ ~ P(U) v ~ Q(U,V) v ~ Q(V,U) ] [ ~ P(A) v P(B) v ~ S(A,B) ] Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING (during a joint degree with Fudan University in Software Engineering) DEPT. OF COMPUTER SCIENCE UCD Example Conversions Convert the Following to Clausal Form.... 1. (X), (Y) L(X, Y) , Q(X, Y) -> Z(X, Y) 2. ~ (X), (Q), M(X,Q) -> S(X, Q) 3. ( ~ (X) P(X)) -> ( (X), P(X)) Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING (during a joint degree with Fudan University in Software Engineering) DEPT. OF COMPUTER SCIENCE UCD Conversion Exercises II Convert the following well formed formula into Conjunctive Normal Form. (X) [ P(X) -> ( (Y) [ Q(X, Y) , R(Y) ] , ~ (Y) [ Q(X, Y) , Q(Y, X) ] ) ] Ensure that your answer clearly highlights the various discrete stages involved in the conversion. [14 Marks] Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING (during a joint degree with Fudan University in Software Engineering) DEPT. OF COMPUTER SCIENCE UCD Has Logical Equivalence Been Preserved ? All Steps with the exception of Step 3 that of Skolemisation preserves the logical form of the original sentence. If no skolemisation is demanded within the conversion process then we can say that the conjunction of all the resulting clauses (RC) are logically equivalent to the original sentence (OS). OS RC If skolemisation is necessitated then we can not say that such an equivalence holds. We can only state that :• RC are satisfiable iff OS is satisfiable and • RC implies OS. Is this set of weaker properties sufficient for our purposes? Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING (during a joint degree with Fudan University in Software Engineering) DEPT. OF COMPUTER SCIENCE UCD Things to Do List 1. Review the worked example. 2. Rework the example without reference to your notes. 3. Attempt the Skolemisation Examples Presented 4. Supplement your notes on Skolemisation 5. Consult past examination papers and attempt conversion related questions. Remember that past papers together with model answers are held in the library behind the issue desk. Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING (during a joint degree with Fudan University in Software Engineering) DEPT. OF COMPUTER SCIENCE UCD 1st assignment! Due within next 10 days! Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING (during a joint degree with Fudan University in Software Engineering) DEPT. OF COMPUTER SCIENCE UCD