CSC 173 Tues. Dec 10, 2002 ================== ======================================================================== Proofs in Predicate Logic A proof in predicate logic has much the same form as a proof in propositional logic. We begin with a set of axioms (or hypotheses) A1..An, and using the rules of inference, we construct a sequence of expressions that follow from those axioms. We can use the rules of inference from propositional logic as inference rules in predicate logic, including modus ponens, DeMorgan's laws, and the substitution of equals. We require that each hypothesis and line in the proof be a closed expression (ie, there are no free variables whose scope extends beyond a line in the proof). ------------------------------------------------------------------------ Implication, entailment, and proof It's important to distinguish among three related concepts: A -> B "A implies B". This is a logical statement. It may be true or false. A =| B "A entails B". This is a "meta-statement" about truth. B is true whenever A is true; more precisely, B is true in all models in which A is true. If we know that A =| B then we know that A -> B is a tautology. A -| B "B can be proven from A". This is a weaker meta-statement. It says that under some given set of proof rules and interpretations for predicates, if we are given A as premise we can derive B. ------------------------------------------------------------------------ Substitution Rule The law of variable substitution is an inference rule for use in proofs in predicate logic. Informally, this rule states that having established that a general fact (or expression) is true, we can assert that a specific instance of that general expression is also true. In particular, if we can prove (or assert as an axiom) a logical expression L1 containing free variables, then if we substitute constants or bound variables for some of the free variables in L1 to create expression L2, then the law of substitution states that L1 -> L2 is a tautology, and we can assert L2 in the proof. Consider the following assertion about the domain of real numbers: Lt(x,y) -> (E z) (Lt(x,z) AND Lt(z,y)) If we substitute x=2 and y=5 in the original expression, then we can assert: Lt(2,5) -> (E z) (Lt(2,z) AND Lt(z,5)) In other words, if the original expression holds for all x and y, then it must hold for x=2 and y=5. Note that choosing x=5 and y=2 makes Lt(x,y) = false, and the entire expression is still true (since false->anything is true). ------------------------------------------------------------------------ Structure of a Proof in Predicate Logic The simplest proofs in predicate logic consist of: * facts, which are ground atomic formulas male(Adam) female(Ann) parent(Adam,Barney) * rules, which are the conjunction of one or more atomic formulae that imply another atomic formula parent(y,x) AND male(x) -> son(x,y) parent(y,x) AND female(x) -> daughter(x,y) The left-hand side of a rule contains hypotheses (called the body of the rule); each atomic formula is a hypothesis or subgoal. The right-hand side is the goal (or head of the rule). Rules are general principles that we can apply to facts to prove new facts. * Assert a rule that is known to be true (that is, the body of the rule implies the head of the rule) * Find facts that (via substitution) match the atomic formulae of the body of the rule * Make consistent variable substitutions in the body and the head of the rule * Assert the head (or goal) as proven ------------------------------------------------------------------------ Example Database of Facts and Rules Facts 1. male(Adam) 2. male(Barney) 3. male(Bob) 4. male(Carl) 5. female(Ann) 6. female(Beth) 7. female(Barb) 8. female(Carol) 9. parent(Adam,Barney) 10. parent(Ann,Barney) 11. parent(Adam,Beth) 12. parent(Ann,Beth) 13. parent(Adam,Bob) 14. parent(Ann,Bob) 15. parent(Adam,Barb) 16. parent(Barney,Carl) 17. parent(Carol,Carl) Rules 1. parent(y,x) AND male(x) -> son(x,y) 2. parent(y,x) AND female(x) -> daughter(x,y) 3. male(x) AND (E z)(parent(z,x) AND parent(z,y)) -> brother(x,y) 4. female(x) AND (E z)(parent(z,x) AND parent(z,y)) -> sister(x,y) 5. male(x) AND (E y)(parent(x,y) AND parent(y,z)) -> grandfather(x,z) 6. female(x) AND (E y)(parent(x,y) AND parent(y,z)) -> grandmother(x,z) ------------------------------------------------------------------------ Example Proof using Substitution Prove son(Barney,Adam) 1. male(Adam) Fact 1 2. male(Barney) Fact 2 3. parent(Adam,Barney) Fact 9 4. parent(y,x) AND male(x) -> son(x,y) Rule 1 5. parent(Adam,Barney) AND male(Barney) L2,L3 6. parent(Adam,Barney) AND male(Barney) -> son(Barney,Adam) L5, L4, sub. 7. son(Barney,Adam) L5, L6, m.p. Note that we selected the facts and the rule that would help us prove son(Barney,Adam). * There's only one rule that allows us to infer the son relationship, so we included that rule. * We need facts about the parent relationship that include reference to Adam and Barney. * We need facts about the male relationship that include (possibly) Adam and Barney. ------------------------------------------------------------------------ Example Proof with Quantifiers Prove brother(Barney,Beth): 1. male(Barney) Fact 2 2. parent(Adam,Barney) Fact 9 3. parent(Adam,Beth) Fact 11 4. male(x) AND (E z)(parent(z,x) AND parent(z,y)) -> brother(x,y) Rule 3 5. male(Barney) AND (E z)(parent(z,Barney) AND parent(z,Beth)) -> brother(Barney,Beth) L4, sub. 6. male(Barney) AND parent(Adam,Barney) AND parent(Adam,Beth) L1, L2, L3 7. brother(Barney,Beth) L5, L6, m.p. ======================================================================== Computation in Predicate Logic Prolog is a programming language based on 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 Clauses 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 a left-pointing 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. ------------------------------------------------------------------------ Example: Greatest Common Divisor Using Euclid's algorithm, we can compute the GCD of two positive integers in Prolog as follows: /* Prolog program to compute GCD */ gcd(A, 0, A). gcd(A, A, A). gcd(A, B, D) :- (A>B),(B>0), R is A mod B, gcd(B,R,D). gcd(A, B, D) :- (A10) gcd(10,5,D) (10>5) (5>0) R=0 gcd(5,0,D) D=5 ------------------------------------------------------------------------ What can we specify with Horn clauses? Ignoring quantifiers for the moment, consider a statement in CNF. Since everything in the Prolog database is assumed to be true, we'll be set if we can come up with a Horn clause for each clause of the CNF expression. Consider a single clause. Some of its terms are negated; others are plain. In general, x1 | x2 | ... | xN | ~y1 | ~y2 | ... | ~yM == (x1 | x2 | ... | xN) <- (y1 & y2 & ... & yM). If there is a single plain term and no negated terms, we have a Prolog fact. As a Horn clause, this is x1 <- true. If we have a single plain term and a bunch of negated terms, we have a Prolog rule. If we have zero plain terms we have a goal. If we choose to write this as false <- y1 & y2 & ... & yM then we can add it to our database and attempt to find a proof via contradiction. If we have more than one plain term we're stuck: we can't express our CNF clause as a Horn clause. In summary: we can express a CNF clause as a Horn clause iff it has exactly one plain term. Now what about quantifiers: recall that free variables that appear in the head of a rule are universally quantified; free variables that appear only in the body are existentially quantified. If we need different quantifiers, again we're stuck.