MB: 26 Feb 2001CS Lecture 11 Introduction Reading: Read Chapter 1 of Bratko Programming in Logic: Prolog
MB: 26 Feb 2001CS Lecture 12 Declarative Programming n Declarative programming describes what to compute rather than how to compute it. n E.g., blueprints for a house are declarative, they describe what to build not how to build it. n Describing “what” is often much easier than describing “how” (but not always). n Algorithm = Logic + Control (R. Kowalski, 1979) n Logic expressions are declarative.
MB: 26 Feb 2001CS Lecture 13 Advantages of Declarative Style of Programming n Simply encode your knowledge without worrying how the knowledge will be used. n The underlying inference engine uses that knowledge to answer the user’s queries. n Knowledge can be used in many ways: – Is Mary Peter’s sister? – Who is Peter’s sister? – Who is whose sister?
MB: 26 Feb 2001CS Lecture 14 Knowledge Bases n A Knowledge Base has: – Knowledge in the form of: n Facts (e.g., Socrates is a man) n Rules (e.g., All men are mortals) – An inference engine n A Knowledge Base uses its facts, rules, and inference engine to answer questions. – Is Socrates mortal? – yes
MB: 26 Feb 2001CS Lecture 15 Logic Programming & Knowledge Bases n Logic programming languages are one way to implement knowledge bases. n Encode your knowledge base and your queries then the underlying inference engine will attempt to answer your queries. n The inference engine answers your queries by building constructive proofs that your queries are entailed by the knowledge base.
MB: 26 Feb 2001CS Lecture 16 Simple Example: Families n Define the relevant relationships: – mother, father, brother, sister, aunt, uncle, cousin, ancestor, descendant n Store the basic facts: – parents, siblings, and gender n Ask your queries: – Who is whose sister?
MB: 26 Feb 2001CS Lecture 17 Some Rules n motherOf(M,O) :- parentOf(M,O), female(M). n sisterOf(S,P) :- siblingOf(S,P), female(S). n auntOf(A,N) :- sisterOf(A,X), parentOf(X,N). n grandmotherOf(G,P) :- motherOf(G,X), parentOf(P). n ancestor(A,P) :- parentOf(A,P). n ancestor(A,P) :- parentOf(A,X), ancestor(X,P). n...
MB: 26 Feb 2001CS Lecture 18 Some Facts n male(john). n female(mary). n male(peter). n parentOf(john, mary). n siblingOf(mary, peter). n parentOf(ann, john). n parentOf(mark, ann). n...
MB: 26 Feb 2001CS Lecture 19 Some Queries n ?- sisterOf(mary, peter). n ?- sisterOf(mary, Who). n ?- sisterOf(Sis, peter). n ?- sisterOf(Sister, Sibling). n ?- ancestorOf(A,P).
MB: 26 Feb 2001CS Lecture 110 Their Answers n ?- sisterOf(mary, peter). – yes n ?- sisterOf(mary, Who). – Who = peter ? ; – no n ?- sisterOf(Sis, peter). – Sis = mary ? ; – no
MB: 26 Feb 2001CS Lecture 111 More Answers n ?- sisterOf(Sister, Sibling). – Sibling = peter, – Sister = mary ? ; – no
MB: 26 Feb 2001CS Lecture 112 Last Answer n ?- ancestorOf(A,P). – A = john, – P = mary ? ; – A = ann, – P = john ? ; – A = mark, – P = ann ? ; – A = ann, – P = mary ? ; –...
MB: 26 Feb 2001CS Lecture 113 Running Prolog n Create knowledge base using favorite editor. n Type /usr/local/bin/prolog on cs26. n Load that knowledge base into Prolog: – [‘myKnowledgeBase.pl’]. n Ask queries: – sisterOf(X,Y). n Exit Prolog: – halt.
MB: 26 Feb 2001CS Lecture 114 Prolog Knowledge Base Anatomy n Knowledge Base – Relations n Clauses – Terms
MB: 26 Feb 2001CS Lecture 115 Terms n Terms are things like atoms, numbers, variables, and structures: – tom, 25.3, X, name(mike, barley) n In happy(P) :- paid(P, X), spend(P,Y), X>Y happy(P), :-, paid(P, X), spend(P,Y), X>Y, P, X, and Y are all terms.
MB: 26 Feb 2001CS Lecture 116 Anatomy of a Clause n All clauses terminated by full-stop(“.”). n Clauses have the form: n head :- body.
MB: 26 Feb 2001CS Lecture 117 Head of a Clause n The head may be the relation name with arguments or may be missing, Examples: – likes(X,Z) :- likes(X,Y), likes(Y,Z). – likes(mike,X) :- true. – :- write(***). n likes(mike,X) :- true. likes(mike,X). n Clauses with missing bodies are called facts. n Facts with variables are called universal facts.
MB: 26 Feb 2001CS Lecture 118 Body of a Clause n Body is an expression composed of terms. n When the clause head is missing then the body is executed at load-time.
MB: 26 Feb 2001CS Lecture 119 Anatomy of a Relation n A relation is identified by its name and its arity (# of arguments) - name/arity – likes/2 is a different relation from likes/3 n A relation is defined by the clauses whose heads match the relation id, e.g., the clause – ancestor(A, P) :- parentOf(A, P). is part of the definition of ancestor/2
MB: 26 Feb 2001CS Lecture 120 Anatomy of a Query n Queries are input by the user (rather than part of the knowledge base). n Queries have clause body syntax & semantics, notably variables are existentially quantified. n When query has variables, & Prolog succeeds in proving it follows from KB, Prolog displays variable bindings used in proof.
MB: 26 Feb 2001CS Lecture 121 Quick Quiz n What do you ignore (at least initially) in declarative-style programming? n What are the two main components of a knowledge-based system? n What is the type of knowledge encoded in a Prolog knowledge base?
MB: 26 Feb 2001CS Lecture 122 Quick Quiz cont’d n By what two things are relations identified? n In a Prolog knowledge base, what constitutes the definition of a relation? n What forms can a clause take? n What are the two parts of a clause? n What terminates a clause? n Give examples of different types of terms.
MB: 26 Feb 2001CS Lecture 123 Summary n Declarative programming focuses on specifying what you want, not on how to get it. n Knowledge based systems provide an underlying inference engine, the user provides (in declarative form) the knowledge and the queries. n Prolog can be viewed as a type of knowledge based programming system.
MB: 26 Feb 2001CS Lecture 124 Summary cont’d n Prolog knowledge base = relation collection. n Relation identified by name/arity. n Relation defined by clauses whose heads agree with that id (i.e., name & number of arguments)
MB: 26 Feb 2001CS Lecture 125 Summary cont’d n Clauses have following forms: – head :- body. – head. – :- body. n Queries are entered by the user (i.e., not in knowledge base) and have form of clause body.