Prolog: Resolution, Unification, & Cuts

Prolog: Resolution, Unification, & Cuts

CS 152: Programming Language Paradigms Prolog: Resolution, Unification, & Cuts Prof. Tom Austin San José State University References for Prolog • "Learn Prolog Now", http://www.learnprolognow.org • SWI-Prolog website (contains manual and tutorials), http://www.swi-prolog.org • "NLP with Prolog in the IBM Watson System", http://www.cs.nmsu.edu/ALP/2011/03/ natural-language-processing-with-prolog-in- the-ibm-watson-system/ Review: Facts likes(batman, gotham). likes(batman, justice). likes(ras_al_ghul, justice). likes(ras_al_ghul, revenge). Review: Queries & Variables What do Batman and Ra's al Ghul both like? X is a comma variable is "and" ?- likes(batman,X), likes(ras_al_ghul,X). How does Prolog resolve queries? Through 2 processes: • Resolution • Unification Resolution & Unification • Resolution: The process of matching facts & rules to perform inferencing – infer: derive logical conclusions from the rules. – If a subgoal matches the head of another rule, we can replace it with the body of the matching rule. • Unification: Instantiation of variables via pattern matching Query: likes(batman, X), likes(ras_al_ghul, X). Knowledge Base: likes(batman, gotham). likes(batman, justice). likes(ras_al_ghul, justice). likes(ras_al_ghul, revenge). Query: likes(batman, X), likes(ras_al_ghul, X). Finds match for first sub-query; Knowledge Base: sets a marker likes(batman, gotham). likes(batman, justice). likes(ras_al_ghul, justice). likes(ras_al_ghul, revenge). Query: likes(batman, gotham), likes(ras_al_ghul, gotham). Knowledge Base: likes(batman, gotham). X is bound likes(batman, justice). to gotham likes(ras_al_ghul, justice). likes(ras_al_ghul, revenge). Query: likes(batman, gotham), likes(ras_al_ghul, No match found: gotham). fails and backtracks Knowledgeto marker Base: likes(batman, gotham). likes(batman, justice). likes(ras_al_ghul, justice). likes(ras_al_ghul, revenge). Query: likes(batman, X), likes(ras_al_ghul, X). Knowledge Base: Finds another match for first likes(batman, gotham). sub-query likes(batman, justice). likes(ras_al_ghul, justice). likes(ras_al_ghul, revenge). Query: likes(batman, justice), likes(ras_al_ghul, justice). Knowledge Base: X is bound to likes(batman, gotham). justice likes(batman, justice). likes(ras_al_ghul, justice). likes(ras_al_ghul, revenge). Query: likes(batman, justice), likes(ras_al_ghul, justice). Knowledge Base: likes(batman, gotham). Match found, and the result likes(batman, justice). is returned likes(ras_al_ghul, justice). likes(ras_al_ghul, revenge). villain(joker). More villain(penguin). facts: villain(catwoman). villain(scarecrow). kills_people(joker). kills_people(penguin). power(scarecrow, fear). romantic_interest(catwoman). romantic_interest(talia). Rules Queries scary(V) :- villain(V), "Head" of kills_people(V). the rule scary(V) :- villain(V), power(V,_). Who is scary? (in-class) Math in Prolog Arithmetic in Prolog heists(joker, 97). heists(penguin, 18). heists(catwoman, 31). heists(scarecrow, 42). combined_heists(X, Y, Total) :- heists(X,XN), heists(Y,YN), Total = XN + YN. ?- combined_heists(catwoman, scarecrow, T). T = 31+42. Using "is" operator combined_heists(X, Y, Total) :- heists(X,XN), heists(Y,YN), Total is XN + YN. ... ?- combined_heists(catwoman, scarecrow, T). T = 73. The Cut Operator "Learn Prolog Now" section 10.2 The Cut Operator Motivation: • Prolog may needlessly backtrack • We wish to stop the backtracking to optimize our code. max example (no cuts) max(X,Y,Y):- X =< Y. max(X,Y,X):- X > Y. Using max ?- max(2,3,M). M = 3 ; Why continue false. the search? ?- max(2,1,M). M = 2 ; false. Two types of cuts (!) • A green cut – improves performance or memory usage – Does not alter results • A red cut – controls resolution to prevent future matches – changes the results – is considered "bad form" max example (no cuts) max(X,Y,Y):- X =< Y. max(X,Y,X):- X > Y. If true, no need to keep searching max example, with green cut max(X,Y,Y):- X =< Y, !. max(X,Y,X):- X > Y. Red Cut Example Batman is enemies with all villains, unless the villain is also a romantic interest. Red Cut Example enemy(batman, X) :- romantic_interest(X), !, No backtracking once fail. we make it here. enemy(batman, X) :- villain(X). Red Cut Example bad_breakup(batman, talia). bad_breakup(batman, poison_ivy). enemy(batman, X) :- romantic_interest(X), !, bad_breakup(batman, X). enemy(batman, X) :- villain(X). Avoiding red cut bad_breakup(batman, talia). bad_breakup(batman, poison_ivy). enemy(batman, X) :- villain(X), \+ romantic_interest(X). enemy(batman, X) :- villain(X), bad_breakup(batman,X). Lab For the rest of the class, continue with the lab from last class. .

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    32 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us