Practical Introduction to Prolog

1 / 18

# Practical Introduction to Prolog - PowerPoint PPT Presentation

## Practical Introduction to Prolog

- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -
##### Presentation Transcript

1. Practical Introduction to Prolog Basics II Dr. David A Ferrucci -- Logic Programming and AI Lecture Notes

2. Logic Programming Excursions II • Negation and the Cut • Backward and Forward Chaining • Explicit Negation, Belief and the Mystery Solver • Intermediate Prolog Programming Examples • Language Acceptors • Transitive Closure • Sub terms • Trees Dr. David A Ferrucci -- Logic Programming and AI Lecture Notes

3. Negation and the Cut • Negation as Failure • Negation succeeds if search fails. • Not Constructive - Unification does not produce any bindings. • Consistent Interpretation depends on Closed World Assumption • The Cut ‘!’ • A device for controlling the search • Used to increase efficiency • BUT can alter semantics of a program -- change its solution set. Dr. David A Ferrucci -- Logic Programming and AI Lecture Notes

4. single_student(X) :- not(married(X)), student(X). student(bill). student(joe). married(joe). :- single_student(bill).  yes. :- single_student(joe).  no. Negation as Failure • ?- single_student(X)  no. Dr. David A Ferrucci -- Logic Programming and AI Lecture Notes

5. single_student(X) :- student(X), not(married(X)). student(bill). student(joe). married(joe). :- single_student(bill).  yes. :- single_student(joe).  no. Negation as Failure 2nd Try • ?- single_student(X)  X=bill. Dr. David A Ferrucci -- Logic Programming and AI Lecture Notes

6. Closed World Assumption • Assumption that the world is defined in its entirety • The representation is “complete”/”closed” • No true statement is missing from the representation • In practice, assumed for conventional databases • “Sorry, sir you must NOT exist your social security number is NOT IN our database, bye, bye”. • From a LP, P, allows us to conclude • the negation of A • IF A is NOT IN the meaning of P Dr. David A Ferrucci -- Logic Programming and AI Lecture Notes

7. single_student(X) :- student(X), not(married(X)). student(bill). student(joe). married(joe). student(jim) :- single_student(bill).  yes. :- single_student(joe).  no. :- single_student(jim).  yes. Negation as Failure & the CWA • But Jim IS married. • Maybe I should read up on the CWA. Dr. David A Ferrucci -- Logic Programming and AI Lecture Notes

8. The Cut (!) • The one and only ‘!’ • There are GOOD, BAD and Ugly ones (usages). • GREEN and RED ones (usages). • Goals before a cut produce first set and only the first set of bindings for named variables • Commits a choice • No alternative matches considered upon backtracking. • Green Cuts • Exclude clauses (solution attempts), but NOT solutions. • Removal of Cut does NOT change the meaning of the program. The cut’s positioning just effects efficiency. • Red Cuts • Alter the actual meaning of the program. • Bad Cut • A cut used in such a way as to make the actual meaning diverge from the intended meaning. • Ugly Cut • Obscures intended meaning but does not loose it Dr. David A Ferrucci -- Logic Programming and AI Lecture Notes

9. A Green Cut fact(N,1) :- N = 0, !. fact(N,F) :- N > 0, M is N -1, fact(M,F1) F is N * F1. If N = 0 in first clause we do not need to consider second clause. The second will fail...so we CUT to prune unnecessary consideration of the second clause. With or without the cut the program produces the same solutions. Its intended meaning is intact. Dr. David A Ferrucci -- Logic Programming and AI Lecture Notes

10. if_then_else(If,Then,Else) :- If, !, Then. if_then_else(If, Then, Else) :- Else. ?- if_then_else(true, write(equal), write(not_equal)) equal yes. ?- if_then_else(false, write(equal), write(not_equal)) not_equal yes. A Good Red Cut If we take out the cut we change the meaning -- so the cut is RED. But it is used to produce the meaning we want -- so the cut is GOOD. if_then_else(If,Then,Else) :- If, Then. if_then_else(If,Then,Else) :- Else. ?- if_then_else(true, write(equal), write(not_equal)) equal not_equal yes. Dr. David A Ferrucci -- Logic Programming and AI Lecture Notes

11. R1. pension(X,disabled) :- disabled(X),!. R2. pension(X,senior) :- over65(X), paid_up(X),!. R3. pension(X,supplemental) :- over65(X),!. R4. pension(X,nothing). %"The Default" If everything else fails. F1. disabled(joe). F4. over65(lou). F2. over65(joe). F5. paid_up(lou). F3. paid_up(joe). A BAD Red Cut The cut is used to implement the default case -- Yike! Q1. ?- pension(joe, nothing) -> yes. OOPS! "I'm sorry Mr. Joe...yes Mr. Joe you are entitled, it was a small computer error...really Mr. Joe computers DO make mistakes...I'm sorry what was that about intended meaning?". Q2. ?- pension(joe,P) -> P = disabled Does Joe get more than one pension payment? Q3. ?- pension(X, senior) -> X = joe. What happened to Lou's pension? Isn’t he a senior? Dr. David A Ferrucci -- Logic Programming and AI Lecture Notes

12. R1. pension(X,disabled) :- disabled(X). R2. pension(X,senior) :- over65(X), paid_up(X). R3. pension(X,supplemental) :- over65(X). R4. entitled(X,Pension) :- pension(X,Pension). R5. entitled(X,nothing) :- not(pension(X,Pension)). %%%%%R5. entitled(X,nothing). F1. disabled(joe). F4. over65(lou). F2. over65(joe). F5. paid_up(lou). F3. paid_up(joe). Joe's Revenge Q1. ?- entitled(joe,nothing) -> no. Q2. ?- entitled(joe,P) -> 1. P = disabled, 2. P=senior, 3. P=supplemental Q3. ?- entitled(X,senior_pension) -> 1. X = joe 2. X = lou Q4. ?- entitled(X,disabled_pension) -> 1. X = joe. Dr. David A Ferrucci -- Logic Programming and AI Lecture Notes

13. Is it Good, Bad or Just Ugly? not(P) :- P,!,fail. not(P). Dr. David A Ferrucci -- Logic Programming and AI Lecture Notes

14. Deductive Reasoning Mechanisms • Backward Chaining • Theorem Proving • Goal-Directed Reasoning • Start with a goal and try to proof it by working backwards • G is true if A, A is true if B, B is true if C. C is true. • Typically used for diagnostic systems • Systems that start with hypothesis and try to substantiated them by working backwards. • Forward Chaining • Data-Directed Reasoning • Production Rule Systems: If X is true then make Y true. • Looks for patterns in data, updates the data and trys again. • Moves forward from one state to the next. • Typical used for control or simulation systems Dr. David A Ferrucci -- Logic Programming and AI Lecture Notes

15. Programming Workshop 0 Represent a very simple blocks world where blocks are either on top of each other or next to each other. Build a simple agent using a forward chaining mechanism that finds any two blocks next to each other stacks on top of the other. Dr. David A Ferrucci -- Logic Programming and AI Lecture Notes

16. Programming Workshop 1 "Your hands, my dear sir!, Your right hand is quite a size larger than your left. You must have worked with it and the muscles are more developed...The fish you have tattooed immediately above your wrist could only have been done in China. That trick of staining the fishes' scales of a delicate pink is quite peculiar to China. What else can be indicated by that right cuff so very shiny for five inches, and the left one with the smooth patch near the elbow where you rest it upon the desk..." Develop a general representation, inspired by the above quote from Sherlock Holmes, for a detective's knowledge base that will enable deductions about a person's travels, their occupations and other habits based on observable data. Dr. David A Ferrucci -- Logic Programming and AI Lecture Notes

17. Programming Workshop 2 “Vic has been murdered and Art, Burt and Carl are suspects. Art says he did not do it. He says that Burt was the victim’s friend, but that Carl hated the victim. Burt says he was out of town on the day of the murder and besides he didn’t even know the guy. Carl says that he is innocent and that he saw Art and Burt just before the murder.” Assuming that everyone, except possibly for the murderer is telling the truth, build a detective (an automated reasoning system) in Prolog and solve the crime. Dr. David A Ferrucci -- Logic Programming and AI Lecture Notes

18. Detective’s Brain Detective's Thinking Engine Possible Worlds Reasoning Deduction in separate belief spaces Problem Solving Strategy IF only one suspect is lying then the only the guilty person’s story might contradict all the other suspect’s stories. Deductive Reasoning Engine Resolution, Explicit Negation Case Facts Art claims/believes friend(bert,vic) etc. General Detective's Knowledge friend -> know hate -> know etc. Dr. David A Ferrucci -- Logic Programming and AI Lecture Notes