CS61A Lecture 27 Logic Programming Jom Magrotker UC Berkeley EECS August 2, 2012.

Slides:



Advertisements
Similar presentations
The behavior of SAT solvers in model checking applications K. L. McMillan Cadence Berkeley Labs.
Advertisements

Exploiting SAT solvers in unbounded model checking
CS61A Lecture 26 Logic Programming Jom Magrotker UC Berkeley EECS August 1, 2012.
CS61A Lecture 25 Delayed Sequences Jom Magrotker UC Berkeley EECS July 31, 2012.
Inference Rules Universal Instantiation Existential Generalization
Chapter 11 :: Logic Languages
LING 388: Language and Computers Sandiway Fong Lecture 5: 9/8.
Hoare’s Correctness Triplets Dijkstra’s Predicate Transformers
Inference and Reasoning. Basic Idea Given a set of statements, does a new statement logically follow from this. For example If an animal has wings and.
The Game of Algebra or The Other Side of Arithmetic The Game of Algebra or The Other Side of Arithmetic © 2007 Herbert I. Gross by Herbert I. Gross & Richard.
Inferences The Reasoning Power of Expert Systems.
Algebra Problems… Solutions Algebra Problems… Solutions © 2007 Herbert I. Gross Set 22 By Herbert I. Gross and Richard A. Medeiros next.
Expert System Human expert level performance Limited application area Large component of task specific knowledge Knowledge based system Task specific knowledge.
1 Introduction to Computability Theory Lecture12: Reductions Prof. Amos Israeli.
Constraint Logic Programming Ryan Kinworthy. Overview Introduction Logic Programming LP as a constraint programming language Constraint Logic Programming.
CSE115/ENGR160 Discrete Mathematics 02/07/12
Catriel Beeri Pls/Winter 2004/5 type reconstruction 1 Type Reconstruction & Parametric Polymorphism  Introduction  Unification and type reconstruction.
1 Pertemuan 6 RULE-BASED SYSTEMS Matakuliah: H0383/Sistem Berbasis Pengetahuan Tahun: 2005 Versi: 1/0.
Chapter 2: Design of Algorithms
Using Decision Procedures for Program Verification Christopher Lynch Clarkson University.
Lesson 6. Refinement of the Operator Model This page describes formally how we refine Figure 2.5 into a more detailed model so that we can connect it.
Introduction Solving inequalities is similar to solving equations. To find the solution to an inequality, use methods similar to those used in solving.
Gateway Quiz Reminders: The next Gateway will be given in class next week or the week after )check your course calendar.) Before then, each student who.
Algebra Problems… Solutions
CS61A Lecture 28 Distributed Computing Jom Magrotker UC Berkeley EECS August 6, 2012.
CS61A Lecture 14 Object-Oriented Programming Jom Magrotker UC Berkeley EECS July 11, 2012.
Please open your laptops, log in to the MyMathLab course web site, and open Quiz 2.6/7. You will have access to the online calculator on your laptop during.
Towers of Hanoi. Introduction This problem is discussed in many maths texts, And in computer science an AI as an illustration of recursion and problem.
Notes for Chapter 12 Logic Programming The AI War Basic Concepts of Logic Programming Prolog Review questions.
RMIT University; Taylor's College1 Lecture 6  To apply the Principle of Mathematical Induction  To solve the Towers of Hanoi puzzle  To define a recurrence.
10.4 How to Find a Perfect Matching We have a condition for the existence of a perfect matching in a graph that is necessary and sufficient. Does this.
1 Programming Languages (CS 550) Simple Query Language* Jeremy R. Johnson *Material for this lecture comes from SICP chapter 4.
CS61A Lecture 21 Scheme Jom Magrotker UC Berkeley EECS July 24, 2012.
CS61A Lecture 15 Object-Oriented Programming, Mutable Data Structures Jom Magrotker UC Berkeley EECS July 12, 2012.
1 CS 177 Week 16 Recitation Recursion. 2 Objective To understand and be able to program recursively by breaking down a problem into sub problems and joining.
CS61A Lecture 13 Object-Oriented Programming Jom Magrotker UC Berkeley EECS July 10, 2012.
Basic & Advanced Reporting in TIMSNT ** Part Two **
CS61A Lecture 12 Immutable Trees Jom Magrotker UC Berkeley EECS July 9, 2012.
1 Prolog and Logic Languages Aaron Bloomfield CS 415 Fall 2005.
Sequences and Summations Section 2.4. Section Summary Sequences. – Examples: Geometric Progression, Arithmetic Progression Recurrence Relations – Example:
CS61A Lecture 23 Py Jom Magrotker UC Berkeley EECS July 26, 2012.
When I want to work with SQL, I start off as if I am doing a regular query.
Logic Programming CSC 358/ Outline Pattern matching Unification Logic programming.
CHAPTER 15: Tests of Significance The Basics ESSENTIAL STATISTICS Second Edition David S. Moore, William I. Notz, and Michael A. Fligner Lecture Presentation.
Automated Reasoning Early AI explored how to automated several reasoning tasks – these were solved by what we might call weak problem solving methods as.
Ch. 13 Ch. 131 jcmt CSE 3302 Programming Languages CSE3302 Programming Languages (notes?) Dr. Carter Tiernan.
Conditional Probability Mass Function. Introduction P[A|B] is the probability of an event A, giving that we know that some other event B has occurred.
Basic Problem Solving Search strategy  Problem can be solved by searching for a solution. An attempt is to transform initial state of a problem into some.
Problem Reduction So far we have considered search strategies for OR graph. In OR graph, several arcs indicate a variety of ways in which the original.
CSE (c) S. Tanimoto, 2008 Predicate Calculus II 1 Predicate Calculus 2 Outline: Unification: definitions, algorithm Formal interpretations and satisfiability.
CS61A Lecture 20 Object-Oriented Programming: Implementation Jom Magrotker UC Berkeley EECS July 23, 2012.
Lecture on Set! And Local CS 2135 Copyright Kathi Fisler, 2002 This material requires Advanced Language Level.
CS61A Lecture 9 Immutable Data Structures Jom Magrotker UC Berkeley EECS July 2, 2012.
CS412/413 Introduction to Compilers Radu Rugina Lecture 13 : Static Semantics 18 Feb 02.
Expression or Equation? 3x + 7 4y – 10 = 54 5z + 32 = 47 6x + 2y (8x – 1) 2 + y 2x = 16.
Gateway Quiz Reminders: The next Gateway will be given in class next week (check your course calendar.) Before then, each student who did not score 8/8.
Forward and Backward Chaining
CS61A Lecture 10 Immutable Data Structures Jom Magrotker UC Berkeley EECS July 3, 2012.
CS61A Lecture 11 Immutable Trees Jom Magrotker UC Berkeley EECS July 5, 2012.
CHAPTER 15: Tests of Significance The Basics ESSENTIAL STATISTICS Second Edition David S. Moore, William I. Notz, and Michael A. Fligner Lecture Presentation.
CS61A Lecture 29 Parallel Computing Jom Magrotker UC Berkeley EECS August 7, 2012.
Unit 2: Absolute Value Absolute Value Equations and Inequalities
CSE15 Discrete Mathematics 02/08/17
Gateway Quiz Reminders:
Nondeterministic Evaluation
Chapter 11 :: Logic Languages
Introduction Solving inequalities is similar to solving equations. To find the solution to an inequality, use methods similar to those used in solving.
CS2136: Paradigms of Computation
Presentation transcript:

CS61A Lecture 27 Logic Programming Jom Magrotker UC Berkeley EECS August 2, 2012

2 C OMPUTER S CIENCE IN THE N EWS

3 T ODAY Review: PyGic Unification

4 D ECLARATIVE P ROGRAMMING In declarative programming, we describe what the properties of the required solution are, and the computer discovers how to find the solution. Logic programming is a type of declarative programming that uses mathematical logic and logical inference to solve a problem. New paradigm!

5 R EVIEW : P Y G IC Suppose we asserted the following fact: P?> fact doctor( ) What would the interpreter print in response to the following queries? P?> doctor(?who) P?> doctor( )

6 R EVIEW : P Y G IC Suppose we asserted the following fact: P?> fact doctor( ) What would the interpreter print in response to the following queries? P?> doctor(?who) ?who = P?> doctor( ) No. P?> doctor( ) ?ninth = christopher, ?tenth = david, ?eleventh = matt P?> doctor( ) ?ninth = christopher, ?rest = P?> doctor( ) ?tenth = david, ?eleventh =

7 P Y G IC : R ULES P?> rule grandfather(?person, ?grandson):... father(?person, ?son)... father(?son, ?grandson) The conclusion is true only if the hypotheses are true. = Can variables be replaced with values such that the hypotheses are true? If so, the conclusion is true too. C ONCLUSION H YPOTHESES

8 P Y G IC : R ULES FOR L ISTS We can also define rules for lists. For example, say we want to check if two (flat) lists are equal. What are some facts we know about equal lists?

9 P Y G IC : R ULES FOR L ISTS Fact 1: The empty list is only equal to itself. P?> fact equal_lists(<>, <>) A fact, by the way, is equivalent to a rule with True in the body: P?> rule equal_lists(<>, <>):... True True is a PyGic keyword that is, well, true.

10 P Y G IC : R ULES FOR L ISTS Fact 2: Two lists are equal if their first elements are equal, and if the rest of their elements are equal. P?> rule equal_lists(, ):... equal_lists(?rest1, ?rest2) C ONCLUSION H YPOTHESES The same variable is used in two places. A list can therefore only match if the first elements have the same value. The same variable is used in two places. A list can therefore only match if the first elements have the same value.

11 P Y G IC : R ULES FOR L ISTS We want to append one list to another: P?> append(,, ) Yes. P?> append(,, ?what) Yes. ?what =

12 P Y G IC : R ULES FOR L ISTS What are some facts we know about the problem? Fact 1: Appending the empty list to any other list gives us the other list. Fact 2: Appending one list to another is equivalent to adding the first element of the first list to the result of appending the second list to the rest of the first list. What facts or rules should we then define?

13 P Y G IC : R ULES FOR L ISTS P?> fact append(<>, ?z, ?z) P?> rule append(, ?v, ):... append(?u, ?v, ?w) We indicate that the first element of the first list and of the result must be the same.

14 P Y G IC : R ULES FOR L ISTS P?> fact append(<>, ?z, ?z) P?> rule append(, ?v, ):... append(?u, ?v, ?w) What about the rest of the first list, the second list, and the result? The result is the second list appended to the rest of the first list.

15 P Y G IC : R ULES FOR L ISTS (P RACTICE ) We can now run append “backwards”. P?> append(, ?what, ) Yes. ?what = P?> append(?what, ?other, ) Yes. ?what = <> ?other = P?> more? Yes. ?what = ?other =

16 P Y G IC : R ULES FOR L ISTS (P RACTICE ) Write the rule(s) reverse that will match only if the second list has elements in the reverse order as the first. P?> reverse(, ) Yes. P?> reverse(, ) No. P?> reverse(, ?what) Yes. ?what = (Hint: You may find append useful here.)

17 P Y G IC : R ULES FOR L ISTS (P RACTICE ) P?> fact reverse(<>, <>) P?> rule reverse(, ?rev):... reverse(?rest, ?rest_rev)... append(?rest_rev,, ?rev)

18 P Y G IC : R ULES FOR L ISTS (P RACTICE ) Write the rule(s) palindrome that will match only if the list is a palindrome, where the list reads the same forwards and backwards. P?> palindrome( ) No. P?> palindrome( ) Yes. (Hint: You have defined reverse and equal_lists.)

19 P Y G IC : R ULES FOR L ISTS (P RACTICE ) P?> rule palindrome(?list):... reverse(?list, ?list_rev)... equal_lists(?list, ?list_rev)

20 A NNOUNCEMENTS Homework 13 due Saturday, August 4. – Includes Py, streams, iterators, and generators – Also includes the Project 4 contest. Project 4 due Tuesday, August 7. – Partnered project, in two parts. – Twelve questions, so please start early! – Two extra credit questions. De-stress potluck tonight from 7pm to 10pm in the Wozniak Lounge (Soda, 4 th floor). – Food and games. – Come and leave when you want.

21 A NNOUNCEMENTS : F INAL Final is Thursday, August 9. – Where? 1 Pimentel. – When? 6PM to 9PM. – How much? All of the material in the course, from June 18 to August 8, will be tested. Closed book and closed electronic devices. One 8.5” x 11” ‘cheat sheet’ allowed. No group portion. We have ed you if you have conflicts and have told us. If you haven’t told us yet, please let us know by toda Final review sessions on Monday, August 6 and Tuesday, August 7, from 8pm to 9:30pm in the HP Auditorium (306 Soda).

22 H OW P Y G IC W ORKS Assume that we have asserted these facts: P?> fact father(james, harry) Yes. P?> fact father(harry, albus_severus) Yes. P?> fact father(harry, james_sirius) Yes. What happens in response to the query: P?> father(?who, ?child)

23 H OW P Y G IC W ORKS PyGic starts off with a “global” empty frame. There are no bindings in this frame yet.

24 H OW P Y G IC W ORKS PyGic first searches for the facts that match the query and the rules whose conclusions match the query. There are three such facts: father(james, harry) father(harry, albus_severus) father(harry, james_sirius)

25 H OW P Y G IC W ORKS PyGic picks a fact: father(james, harry) father(harry, albus_severus) father(harry, james_sirius)

26 H OW P Y G IC W ORKS PyGic prepares an empty frame that extends the global frame. It makes a new empty frame for every query. The current frame

27 H OW P Y G IC W ORKS PyGic attempts to unify the query with the fact. Unification is a generalized form of pattern matching, where either or both of the patterns being matched may contain variables.

28 H OW P Y G IC W ORKS : U NIFICATION To match the query father(?who, ?child) with the fact father(james, harry), PyGic must check if ?who = james, ?child = harry.

29 H OW P Y G IC W ORKS : U NIFICATION PyGic checks if the variables ?who or ?child have any values in the current frame.

30 H OW P Y G IC W ORKS : U NIFICATION There are none! So, PyGic binds the variables to these values in the current frame. Now, trivially, we know that ?who = james, ?child = harry is true. ?whojames ?childharry

31 H OW P Y G IC W ORKS : U NIFICATION PyGic is done with the query, since it successfully matched the query to a fact. PyGic returns the frame and its bindings to be printed back to the user.

32 H OW P Y G IC W ORKS : B ACKTRACKING What happens if the user asks for more? PyGic returns to the last point at which it made a choice (a choice point), and ignores all the frames that it made as a result of that choice. There, it tries to make another choice, if it can. If not, it goes to the choice point before that, and attempts to make another choice.

33 H OW P Y G IC W ORKS : B ACKTRACKING In this example, it made a choice when it chose which fact to unify. As a result, a new frame is created with possibly new bindings. If it cannot choose another fact or rule to unify, and if there are no more choice points, then there are no more ways to satisfy the rules.

34 H OW P Y G IC W ORKS : B ACKTRACKING This is known as chronological backtracking. PyGic backtracks to the last point at which it made a choice and attempts to make another one to find another solution.

35 H OW P Y G IC W ORKS Now, say that we have the following rule: P?> rule grandfather(?who, ?grandson):... father(?who, ?son)... father(?son, ?grandson) What happens in response to the query: P?> grandfather(james, ?grandson)

36 H OW P Y G IC W ORKS PyGic first searches for the facts that match the query and the rules whose conclusions match the query. There is only one such rule: grandfather(?who, ?grandson) PyGic picks this rule.

37 H OW P Y G IC W ORKS PyGic will rename the variables to avoid confusion with other rules that may have the same variable names. The rule is now grandfather(?who#1, ?grandson#2): father(?who#1, ?son#3) father(?son#3, ?grandson#2)

38 H OW P Y G IC W ORKS PyGic prepares an empty frame, where no variables have yet been bound to any value.

39 H OW P Y G IC W ORKS : U NIFICATION To match the query grandfather(james, ?grandson) with the rule conclusion grandfather(?who#1, ?grandson#2), PyGic must check if ?who#1 = james, ?grandson#2 = ?grandson.

40 H OW P Y G IC W ORKS : U NIFICATION PyGic checks if the variables ?who#1, ?grandson#2, ?grandson have any values in the current frame.

41 H OW P Y G IC W ORKS : U NIFICATION There are none! So, PyGic makes the proper bindings in the current frame. ?who#1james ?grandson#2?grandson

42 H OW P Y G IC W ORKS : H YPOTHESES However, this is a rule! PyGic needs to determine if the hypotheses are true to infer that the conclusion is true. PyGic will consider each hypothesis as a new query, and determine if each hypothesis is true.

43 H OW P Y G IC W ORKS The new query is father(?who#1, ?son#3) PyGic searches for facts and rule conclusions that match this query. There are three such facts: father(james, harry) father(harry, albus_severus) father(harry, james_sirius)

44 H OW P Y G IC W ORKS PyGic picks a fact: father(james, harry) father(harry, albus_severus) father(harry, james_sirius)

45 H OW P Y G IC W ORKS PyGic prepares an empty frame, which extends the previous frame. This is very different from the environment diagrams we studied earlier. Variables could not be assigned to variables! This is very different from the environment diagrams we studied earlier. Variables could not be assigned to variables! ?who#1james ?grandson#2?grandson

46 H OW P Y G IC W ORKS : U NIFICATION To match the query father(?who#1, ?son#3) with the fact father(james, harry), PyGic must check if ?who#1 = james, ?son#3 = harry.

47 H OW P Y G IC W ORKS : U NIFICATION PyGic checks if the variables ?who#1 or ?son#3 have any values in the current frame or its parent. ?who#1james ?grandson#2?grandson

48 H OW P Y G IC W ORKS : U NIFICATION ?who#1 has a value, but that value matches james. There are no bindings for ?son#3. PyGic binds ?son#3 with harry. The query has been successfully unified with a fact. ?who#1james ?grandson#2?grandson ?son#3harry

49 H OW P Y G IC W ORKS The first hypothesis in the body of the grandfather rule is true. Now, we check the second hypothesis father(?son#3, ?grandson#2) PyGic searches for facts and rule conclusions that match this query. There are three such facts: father(james, harry) father(harry, albus_severus) father(harry, james_sirius)

50 H OW P Y G IC W ORKS PyGic picks a fact: father(james, harry) father(harry, albus_severus) father(harry, james_sirius)

51 H OW P Y G IC W ORKS PyGic prepares another empty frame, which extends the previous frame. ?who#1james ?grandson#2?grandson ?son#3harry

52 H OW P Y G IC W ORKS : U NIFICATION To match the query father(?son#3, ?grandson#2) with the fact father(james, harry), PyGic must check if ?son#3 = james, ?grandson#2 = harry.

53 H OW P Y G IC W ORKS : U NIFICATION PyGic checks if the variables ?son#3 or ?grandson#2 have any values in the current frame or its parents. ?who#1james ?grandson#2?grandson ?son#3harry

54 H OW P Y G IC W ORKS : U NIFICATION ?son#3 has a value, which does not match james. The query is not true, given the existing bindings. ?who#1james ?grandson#2?grandson ?son#3harry

55 H OW P Y G IC W ORKS PyGic then backtracks and picks another fact: father(james, harry) father(harry, albus_severus) father(harry, james_sirius)

56 H OW P Y G IC W ORKS PyGic prepares another empty frame, which extends the previous frame. Notice that the “previous frame” is the frame before the last choice point. ?who#1james ?grandson#2?grandson ?son#3harry

57 H OW P Y G IC W ORKS : U NIFICATION To match the query father(?son#3, ?grandson#2) with the fact father(harry, albus_severus), PyGic must check if ?son#3 = harry, ?grandson#2 = albus_severus.

58 H OW P Y G IC W ORKS : U NIFICATION PyGic checks if the variables ?son#3 or ?grandson#2 have any values in the current frame or its parents. ?who#1james ?grandson#2?grandson ?son#3harry

59 H OW P Y G IC W ORKS : U NIFICATION ?son#3 has a value, but that value matches harry. ?grandson#2 has a value ?grandson. PyGic binds ?grandson with albus_severus. The query has been unified with the fact. ?who#1james ?grandson#2?grandson ?son#3harry ?grandsonalbus_severus

60 H OW P Y G IC W ORKS Both the hypotheses of the grandson rule are true, so the conclusion must also be true. The values for the variables in the original query ( ?grandson ) are looked up in the environment and printed.

61 H OW P Y G IC W ORKS : C ODE (S IMPLIFIED ) def prove_query(query, env, ruledb): if is_true_expr(query): yield env else: matching_rules = ruledb.find_rules_matching(query) for rule in matching_rules: rule = rule.rename() newenv = pygic.environments.Environment(env) if unify_expr_lists(query, rule.conclusion, new_env): for result in prove_queries(rule.hypotheses, newenv, ruledb): yield result To prove a query in the given environment and with a given rule database... If the query is the true expression, yield the current environment. Find all the rules in the database whose conclusions match the query. Remember that facts are also rules, each with a body of True. Find all the rules in the database whose conclusions match the query. Remember that facts are also rules, each with a body of True.

62 H OW P Y G IC W ORKS : C ODE (S IMPLIFIED ) def prove_query(query, env, ruledb): if is_true_expr(query): yield env else: matching_rules = ruledb.find_rules_matching(query) for rule in matching_rules: rule = rule.rename() newenv = pygic.environments.Environment(env) if unify_expr_lists(query, rule.conclusion, new_env): for result in prove_queries(rule.hypotheses, newenv, ruledb): yield result For every rule that matches returned a renamed rule.

63 H OW P Y G IC W ORKS : C ODE (S IMPLIFIED ) def prove_query(query, env, ruledb): if is_true_expr(query): yield env else: matching_rules = ruledb.find_rules_matching(query) for rule in matching_rules: rule = rule.rename() newenv = pygic.environments.Environment(env) if unify_expr_lists(query, rule.conclusion, new_env): for result in prove_queries(rule.hypotheses, newenv, ruledb): yield result Extend the environment. Try to unify the query with the conclusion of the rule, which may add bindings in the frames. If the hypotheses in the query can be proved, yield the environment that results from the proof.

64 H OW P Y G IC W ORKS : C ODE (S IMPLIFIED ) def prove_query(query, env, ruledb): if is_true_expr(query): yield env else: matching_rules = ruledb.find_rules_matching(query) for rule in matching_rules: rule = rule.rename() newenv = pygic.environments.Environment(env) if unify_expr_lists(query, rule.conclusion, new_env): for result in prove_queries(rule.hypotheses, newenv, ruledb): yield result yield allows us to continue where we left off, or to “backtrack” to a choice point.

65 H OW P Y G IC W ORKS : C ODE (S IMPLIFIED ) def prove_queries(queries, env, ruledb): if len(queries) == 0: yield env else: for new_env in prove_query(queries[0], env, ruledb): for result in prove_queries(queries[1:], new_env, ruledb): yield result If there are no more queries to check, we successfully yield the current environment.

66 H OW P Y G IC W ORKS : C ODE (S IMPLIFIED ) def prove_queries(queries, env, ruledb): if len(queries) == 0: yield env else: for new_env in prove_query(queries[0], env, ruledb): for result in prove_queries(queries[1:], new_env, ruledb): yield result Prove the first query and obtain a resulting new environment. Prove the rest of the queries in the new environment.

67 B REAK

68 A PPLICATIONS Declarative programming is useful in database applications. For example, if we have a database of student records, and want to get all the records of sophomore year students, we run the query SELECT * FROM STUDENT_DB WHERE YEAR = 2

69 A PPLICATIONS Notice that in the query SELECT * FROM STUDENT_DB WHERE YEAR = 2 all that we have specified are the properties we expect from our output. We could, of course, iterate through all the records and filter out the ones we need, but it is a common enough operation that it is better to specify what we want from the output, rather than how we want to get it.

70 C ONCLUSION Under the hood, PyGic matches the query against all of its rules and facts. It then picks one, and attempts to unify the query with the fact (or rule conclusion) by finding a consistent assignment to the variables in either. Declarative programming is useful in situations where we know what we expect of the output. Preview: Write your own chat client.