260ae6f736edad7282c559420c332699.ppt
- Количество слайдов: 25
Programming in Logic: Prolog Introduction Reading: Read Chapter 1 of Bratko MB: 26 Feb 2001 CS 360 - Lecture 1 1
Declarative Programming 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. n MB: 26 Feb 2001 CS 360 - Lecture 1 2
Advantages of Declarative Style of Programming 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: n – – – Is Mary Peter’s sister? Who is whose sister? MB: 26 Feb 2001 CS 360 - Lecture 1 3
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) – n An inference engine A Knowledge Base uses its facts, rules, and inference engine to answer questions. – – Is Socrates mortal? yes MB: 26 Feb 2001 CS 360 - Lecture 1 4
Logic Programming & Knowledge Bases 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. n MB: 26 Feb 2001 CS 360 - Lecture 1 5
Simple Example: Families n Define the relevant relationships: – n Store the basic facts: – n mother, father, brother, sister, aunt, uncle, cousin, ancestor, descendant parents, siblings, and gender Ask your queries: – Who is whose sister? MB: 26 Feb 2001 CS 360 - Lecture 1 6
Some Rules mother. Of(M, O) : - parent. Of(M, O), female(M). n sister. Of(S, P) : - sibling. Of(S, P), female(S). n aunt. Of(A, N) : - sister. Of(A, X), parent. Of(X, N). n grandmother. Of(G, P) : mother. Of(G, X), parent. Of(P). n ancestor(A, P) : - parent. Of(A, X), ancestor(X, P). n. . . n MB: 26 Feb 2001 CS 360 - Lecture 1 7
Some Facts male(john). n female(mary). n male(peter). n parent. Of(john, mary). n sibling. Of(mary, peter). n parent. Of(ann, john). n parent. Of(mark, ann). n. . . n MB: 26 Feb 2001 CS 360 - Lecture 1 8
Some Queries ? - sister. Of(mary, peter). n ? - sister. Of(mary, Who). n ? - sister. Of(Sis, peter). n ? - sister. Of(Sister, Sibling). n ? - ancestor. Of(A, P). n MB: 26 Feb 2001 CS 360 - Lecture 1 9
Their Answers n ? - sister. Of(mary, peter). – n ? - sister. Of(mary, Who). – – n yes Who = peter ? ; no ? - sister. Of(Sis, peter). – – Sis = mary ? ; no MB: 26 Feb 2001 CS 360 - Lecture 1 10
More Answers n ? - sister. Of(Sister, Sibling). – – – Sibling = peter, Sister = mary ? ; no MB: 26 Feb 2001 CS 360 - Lecture 1 11
Last Answer n ? - ancestor. Of(A, P). – – – – – A = john, P = mary ? ; A = ann, P = john ? ; A = mark, P = ann ? ; A = ann, P = mary ? ; . . . MB: 26 Feb 2001 CS 360 - Lecture 1 12
Running Prolog Create knowledge base using favorite editor. n Type /usr/local/bin/prolog on cs 26. n Load that knowledge base into Prolog: n – n Ask queries: – n [‘my. Knowledge. Base. pl’]. sister. Of(X, Y). Exit Prolog: – halt. MB: 26 Feb 2001 CS 360 - Lecture 1 13
Prolog Knowledge Base Anatomy n Knowledge Base – Relations n. Clauses – Terms MB: 26 Feb 2001 CS 360 - Lecture 1 14
Terms n Terms are things like atoms, numbers, variables, and structures: – n tom, 25. 3, X, name(mike, barley) 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 2001 CS 360 - Lecture 1 15
Anatomy of a Clause All clauses terminated by full-stop(“. ”). n Clauses have the form: n head : - body. n MB: 26 Feb 2001 CS 360 - Lecture 1 16
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(***). likes(mike, X) : - true. likes(mike, X). n Clauses with missing bodies are called facts. n Facts with variables are called universal facts. n MB: 26 Feb 2001 CS 360 - Lecture 1 17
Body of a Clause Body is an expression composed of terms. n When the clause head is missing then the body is executed at load-time. n MB: 26 Feb 2001 CS 360 - Lecture 1 18
Anatomy of a Relation n A relation is identified by its name and its arity (# of arguments) - name/arity – n likes/2 is a different relation from likes/3 A relation is defined by the clauses whose heads match the relation id, e. g. , the clause – ancestor(A, P) : - parent. Of(A, P). is part of the definition of ancestor/2 MB: 26 Feb 2001 CS 360 - Lecture 1 19
Anatomy of a Query 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. n MB: 26 Feb 2001 CS 360 - Lecture 1 20
Quick Quiz 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? n MB: 26 Feb 2001 CS 360 - Lecture 1 21
Quick Quiz cont’d 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. n MB: 26 Feb 2001 CS 360 - Lecture 1 22
Summary 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. n MB: 26 Feb 2001 CS 360 - Lecture 1 23
Summary cont’d 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) n MB: 26 Feb 2001 CS 360 - Lecture 1 24
Summary cont’d n Clauses have following forms: – – – n head : - body. head. : - body. Queries are entered by the user (i. e. , not in knowledge base) and have form of clause body. MB: 26 Feb 2001 CS 360 - Lecture 1 25