1 / 44

Logical queries in Prolog: Interpreting a database

Learn how to run logical queries on a database using a Prolog interpreter, including examples of conjunctive, disjunctive, and negated queries.

jsanson
Download Presentation

Logical queries in Prolog: Interpreting a database

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Notes for CS3310 Artificial IntelligencePart 3: Logical queries to a databaseProf. Neil C. Rowe Naval Postgraduate School Version of July 2009

  2. Interpreters for programming languages • Interpreters run programs without compiling them, working directly on the source code. • Example interpretive languages: • Prolog (focused on logic expressions) • Lisp (focused on linked lists) • Matlab (focused on two-dimensional arrays) • Mathematica (focused on algebraic expressions) • Python • Advantages of interpreters: Faster prototyping, faster debugging • Disadvantages: Slower execution

  3. Running a Prolog interpreter on Windows machines • You can install it on your home computer. Get an open-source interpreter. We recommend Gnu Prolog from http://gnu-prolog.inria.fr. Unzip this at the top level of the C folder. • A Gnu Prolog interepreter is also installed on many of the general computer-science laboratory machines. • Use a text editor to create a file of your facts and (possibly) rules. If C: drive unavailable, store on H: drive. • Start Prolog by clicking on the icon created during installation. A black window should appear, and a ?- should appear inside it.

  4. Running an interpreter, cont. • Do change_directory with argument the name of the directory containing your files enclosed in apostrophes, e.g. change_directory('c:/prolog/progs3310'). (note the period). You must use "/" rather than "\" to indicate subdirectories. • Load your file of facts and rules by doing ['filename’]. (note the period) where filename is the name of your file. • Once your file is loaded, you can query any of the facts and rules in it by the methods discussed in class. • When done with Prolog, type halt. (note the period). • For a user's manual, click on "Help". Prolog dialects do not differ very much. • To preserve a record of your run, push “printscreen” and paste, or use a scripting facility.

  5. Usual operation of Prolog software Source code in the Prolog language User runs edits consult Prolog interpreter queries answers Prolog database loads

  6. incumbent(csprofessor, baer). incumbent(csprofessor, berzins). incumbent(csprofessor, rowe). incumbent(cschairman,boger). incumbent(dean05,boger). incumbent(dean06, panholzer). incumbent(provost, elster). incumbent(director_milops, bley). incumbent(superintendent, chapin). bossed_by(csprofessor, cschairman). bossed_by(cschairman, dean05). bossed_by(dean05, provost). bossed_by(dean06, provost). bossed_by(provost, superintendent). bossed_by(director_milops, superintendent). An example Prolog database

  7. Basic use of a Prolog interpreter • The interpreter expects the user to type in a question, a "query” (like database query systems and Web search engines). It types ?- when ready. • The interpreter then tries to find a matching fact in its database. The database consists of all the files you have loaded with a consult. • A query answer is either "no", "yes", or a set of variable bindings. For instance: ?- bossed_by(csprofessor,cschairman). yes.

  8. Prolog variables • Variables are any word starting with a capital letter. • So constants like tom and nps cannot be capitalized. • Example: X and Y in father_of(X,Y); this says some arbitrary X is the father of some arbitrary Y. • Variables can get bound in query matching (e.g., X=tom_jones and Y=dick_deadeye in answering the query ?- father_of(X,Y).).

  9. ?-incumbent(csprofessor, X). X=baer ?- incumbent(J,rowe). J=csprofessor; no. (A semicolon typed by the user means "Try to find another answer"; a carriage return means to stop.). ?- incumbent(X,P). X=csprofessor, P=baer; X=csprofessor, P=berzins ?- incumbent(csprofessor, rowe). yes ?- Simple example queries

  10. Conjunctive (“anded”) queries You can "and" together (take the conjunction of) several predicate expressions by putting commas between them in a query. ?- integer(X), less_than(0,X),less_than(X,10). This asks for an integer X that is greater than 0 and less than 10. Predicate expressions are checked initially from left to right; facts are searched from top to bottom in the database.

  11. Disjunctive (“ored”) queries You can also "or" expressions with a semicolon: ?- left_of(X,aircraft461); left_of(aircraft461,X). The leftmost expression is matched to the database; if it fails, the next expression to the right is matched.

  12. Negations in queries You can also negate expressions: ?- a_kind_of(X,destroyer), property(X,nuclear), \+ owns(X, us). This checks whether some country besides the U.S. owns a nuclear destroyer. If a negated expression finds a match to the database, it fails; otherwise it succeeds. Negated expressions cannot bind variables.

  13. Composite query examples ?- bossed_by(csprofessor,X), bossed_by(X,Y). ?- bossed_by(X,Y), incumbent(X,rowe), incumbent(Y,Z). ?- incumbent(dean05,X); incumbent(dean_05,X). ?- (bossed_by(J,provost); bossed_by(provost,J)), incumbent(J,P). ?- bossed_by(J,provost), \+ incumbent(J,boger). (Some dialects of Prolog use not(incumbent(P,boger)))

  14. Database for queries example: incumbent(csprofessor,baer). incumbent(csprofessor,berzins). incumbent(csprofessor,rowe). incumbent(cschairman,boger). incumbent(dean05,boger). incumbent(dean06,panholzer). incumbent(provost,elster). incumbent(director_milops,bley). incumbent(superintendent,chapin). bossed_by(csprofessor,cschairman). bossed_by(cschairman,dean05). bossed_by(dean05,provost). bossed_by(dean06,provost). bossed_by(provost,superintendent). bossed_by(director_milops,superintendent).

  15. Matching (unification) for queries Assuming no variables have been previously bound: • p(3) in query matches p(3) in database • p(X) in query matches p(3) in database • p(3) in query matches p(X) in database • p(X) in query matches p(Y) in database • p(3,X) in query matches p(3,4) in database • p(X,Y) in query matches p(3,4) in database • p(A4,B3) in query matches p(3,4) in database • p(4) in query doesn't match p(3) in database • p(X,5) in query doesn't match p(3,4) in database • p(X,X) in query doesn't match p(3,4) in database

  16. Logic symbols of predicate calculus, Prolog, and Java

  17. Venn diagrams • Often it’s useful to draw a box representing the world and circles representing things in the world having properties. We can often visualize logical relationships better. • Below, everything in the “p” circle is something for which p is true. • Everything in the area in overlap between the “p” and “r” circles but not in the “q” circle is something for which is true. p q r

  18. Equivalences of propositional calculus • Unduplication: pp  p; pp  p • Tautology: p~p  true • Contradiction: p  ~p  false • Constants: ptrue  p, pfalse  false, ptrue  true, pfalse  p • Commutativity: pq  qp, pq  qp • Associativity: (pq)r  p(qr), (pq)r  p(qr) • Distributivity: (pq)r  (pr)(qr), (pq)r  (pr)(qr) (p, q, and r represent any logical expression)

  19. Equivalences of propositional calculus (2) • Double negation: ~ ~ p  p • DeMorgan’s Laws: ~ (p  q)  ~ p  ~ q, ~ (p  q)  ~ p  ~ q • Implication disjunctive: (p  q)  (p  ~ q) (hence "p is true" is written as p, not truep) • Contrapositive: (p  q)  (~ q  ~ p) • Absorption: ((pq)  p)  p, ((pq)  p)  p • Negative absorption: ((~pq)  p)  (qp), ((~pq)  p)  (qp)

  20. Proof methods of propositional calculus • Modus ponens: If (p  q) and q is true, conclude p. • Modus tollens: If (p  q) and p is false, conclude q is false. • Adjunction: If p is true, and q is true, then conclude (p  q). • Conjunctive simplification: If (p  q), then conclude p is true. • Disjunctive addition: If p is true, then conclude (p  q).

  21. Proof methods of propositional calculus (2) • Transitivity of implication: If (p  q) and (q  r), then conclude (p  r). • Proof by contradiction: If when you assume p you can prove “false”, conclude ~p. • Resolution: If (p  q) and (~ p  r), conclude (q  r).

  22. Predicate calculus • It’s just propositional calculus plus variables and quantifiers. • Every variable in predicate calculus must be quantified, either universally or existentially. • But in Prolog, quantifiers are not used and are implicit.

  23. Additional equivalences of predicate calculus Quantifier negation: ~X(p(X))  X(~ p(X)), ~X(p(X))  X(~ p(X)) Universal scope change: X(p(X)  q(X))  (Xp(X)  X(q(X)) Quantifier interchange: XY(p(X,Y))  YX(p(X,Y)), XY(p(X,Y))  YX(p(X,Y)) Quantifier elimination: X(p) p, X(p) p (p represents any predicate name; X and Y represent any variable)

  24. Additional proof methods of predicate calculus • Universal instantiation: If X(p(X)) then conclude p(a) is true. • Existential generalization: If p(a) is true, conclude X(p(X)). • Quantified modus ponens form 1: If q(a) and X(p(X)  q(X)), conclude p(a). • Quantified modus ponens form 2: If X(q(X)) and X(p(X)  q(X)), conclude X(p(X)).

  25. Rules for propositional calculus expressions A propositional calculus expression must be either: • a predicate expression without variables (but it may have constants as arguments); • or a propositional calculus expression without variables with a ~ symbol in front of it; • or two propositional calculus expressions with one of the symbols  ,  , or  between them • or a parenthesized propositional calculus expression (parentheses are mandatory when the expression is ambiguous otherwise) These are the only symbols allowed.

  26. Rules for predicate calculus expressions A predicate calculus expression is like a propositional calculus expression except predicate expressions can contain variables. Whenever an expression contains a variable, the variable must be quantified either by  or  followed immediately by the name of the variable, placed somewhere before the expression, and with brackets or parentheses indicating the scope of the quantification.

  27. Proofs in predicate calculus • A series of steps starting with “given”s and ending with the statement to be proved. • Each line must be justified with a reason, which must be one of the equivalences or proof methods of predicate calculus (including those of propositional calculus). • Number the steps so each line can explain which steps it depends on.

  28. Example proof #1 in predicate calculus Prove given p. • p (given) • (disjunctive addition on 1) • (disjunctive addition on 2) • (a DeMorgan’s Law on 3) • (disjunction equivalent of an implication on 4)

  29. Example proof #2 in predicate calculus Given fact p(1,2), statement , and statement , prove r(2,2). • p(1,2) (given) • (given) • q(1,3) (resolution of 1 and 2) • (given) • r(2,2) (modus ponens on 3 and 4)

  30. Example proof #3 in predicate calculus Prove given and . • (given) • (given) • (distributive law on 2) • (conjunctive simplification on 3) • (universal instantiation on 1) • (modus tollens on 4 and 5)

  31. From English to queries Again, look for groups of words that together correspond to a predicate expression. The special words ("and", "or", "if", "not", etc.) will help break the English into pieces. "What", "who", "which", etc. correspond to variables. Example: "What American civilians are bossed by and younger than Prof. Boger?” Representation: ?- nationality(X,american), a_kind_of(X,civilian), bossed_by(X,prof_boger), younger_than(X,prof_boger).

  32. Query simplification problem (1) • Check whether either you can't start the car and you left the lights on all night, or you can't start the car and the radio won't play, or you can't start the car and the radio plays but it plays faintly. • Write this as best you can as an efficient Prolog query using only 4 predicate expressions without variables. • Assume a database of appropriate facts.

  33. Query simplification problem (2) • Find an X such that X is a civilian employee and has a Ph.D., or if X is a military employee and has a Ph.D., or if X is not a military employee and does not have a Ph.D. and is called an "adjunct". • Write this as best you can as an efficient Prolog query using only 4 predicate expressions and one variable X. • Assume a database of appropriate facts.

  34. Automatic backtracking in Prolog When an expression in an "and" fails, the Prolog interpreter returns to the previous expression (if any) and tries to find a new way to satisfy it. Example: ? - a(X,Y), b(Y,Z), \+ c(Z), \+ d(Z). If b(Y,Z) fails, backtrack to a(X,Y) and try to find new pair of X and Y; otherwise fail. A negation always fails when backtracked to. To implement backtracking: The interpreter keeps a database pointer for every expression. These are pushed on and popped from a stack.

  35. ?- a(X,Y), b(X,Y), a(Y,Y). with database: a(1,1). a(2,1). a(3,2). a(4,4). b(1,2). b(1,3). b(2,3). b(3,2). b(4,4). Backtracking example

  36. Another backtracking example Query: ?- a(X,Y), a(X,Z), \+ a(Y,X), a(Y,Z). Database: a(1,1). a(3,4). a(3,1). a(1,4).

  37. public static andc1c2a (boolean c1, boolean c2) {return (c1 and c2); } public static andc1c2b (boolean c1, boolean c2) {boolean flag = true; if (~c1) then flag = false; if (~c2) then flag = false; return flag; } public static orc1c2a (boolean c1, boolean c2) {return (c1 or c2); } public static orc1c2b (boolean c1, boolean c2) {boolean flag = false; if (c1) then flag = true; if (c2) then flag = true; return flag; } Ands and ors in Java

  38. Be careful with negated unbound variables Consider: ?- a(X), \+ b(X). ?- \+ b(X), a(X). The second query won't work right: unlike the first query, it won't succeed with the database: a(1). b(2). The second query succeeds only if there are no "b" facts. Since X can't be bound inside the "not", the X inside the second "not" could as well be a Y.

  39. The logic of unbound variables So "and" is not commutative in Prolog when an expression anded has a "not" with unbound variables. So be careful with such expressions. Logic explanation: variables in Prolog queries are existentially quantified. The scope of the quantification is the entire query, except when inside a "not", when the scope is the inside of the "not". So the two queries above appear in predicate calculus as: X(a(X)  ~b(X)) X(~b(X))  X(a(X))

  40. Generate-and-test versus case-based reasoning • So far, queries are considered as "generate-and-test": a value is generated for a variable the first time it is mentioned, and then that value is tested at subsequent mentions of the variable. If the tests fail, processing backtracks and generates another value. • An alternative is "case-based" reasoning or "satisficing". Sets of solution values are found, then ranked as to how well they pass all the tests.

  41. Finding the best-matching case • The ranking can be based on how many variables match and how close the misses are. • With numeric arguments, the match closeness can be the absolute value of difference in numbers. • With nonnumeric arguments, the match closeness can be the number of "a_kind_of" links that must be followed to get from one value to the other. • Each variable can have a weight. The best-matching fact gives the first answer, the second-best the second answer, etc.

  42. Example data for case-based reasoning Consider ship observation facts: (1) observed length, (2) observed ship type, (3) observed radar, and (4) ship name. observed(50, fishing_vessel, none, totor). observed(150, destroyer, radar, pequod). observed(100, minesweeper, radar, nelson). observed(700, carrier, radar, enterprise). observed(200, cruise_ship, none, bali_hai).

  43. Example run of case-based reasoning Now if we query: observed(140, destroyer, none, X). Case-based reasoning would likely find that the second fact is the best match and X=pequod. If we query: observed(110, patrol, radar, X). Case-based reasoning would likely find that the third fact is the best match and X=nelson.

  44. The best case using the inner product Application: Find the Web page whose words are most similar to the words of an example page. Compute: s(j) = (fij * ei) / ((fij)(ei)) where s(j) is the similarity of example e to casej, fij is count of word i in case j and ei is count of word i in the test example, and  means summation over all words i. Example: Suppose your example page mentions "Sidewinder" and "F-18” a lot. You get high similarity for pages that mention those words a lot, and zero value for pages that do not mention the words at all.

More Related