prolog: resolution, unification, & cuts

32
CS 152: Programming Language Paradigms Prof. Tom Austin San José State University Prolog: Resolution, Unification, & Cuts

Upload: others

Post on 03-Dec-2021

4 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Prolog: Resolution, Unification, & Cuts

CS 152: Programming Language Paradigms

Prof. Tom Austin San José State University

Prolog: Resolution, Unification, & Cuts

Page 2: Prolog: Resolution, Unification, & Cuts

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/

Page 3: Prolog: Resolution, Unification, & Cuts

Review: Facts

likes(batman, gotham).

likes(batman, justice).

likes(ras_al_ghul, justice). likes(ras_al_ghul, revenge).

Page 4: Prolog: Resolution, Unification, & Cuts

Review: Queries & Variables

What do Batman and Ra's al Ghul both like?

?- likes(batman,X), likes(ras_al_ghul,X).

X is a variable

comma is "and"

Page 5: Prolog: Resolution, Unification, & Cuts

How does Prolog resolve queries?

Through 2 processes: • Resolution • Unification

Page 6: Prolog: Resolution, Unification, & Cuts

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

Page 7: Prolog: Resolution, Unification, & Cuts

likes(batman, gotham). likes(batman, justice). likes(ras_al_ghul, justice). likes(ras_al_ghul, revenge).

likes(batman, X), likes(ras_al_ghul, X).

Query:

Knowledge Base:

Page 8: Prolog: Resolution, Unification, & Cuts

likes(batman, gotham). likes(batman, justice). likes(ras_al_ghul, justice). likes(ras_al_ghul, revenge).

likes(batman, X), likes(ras_al_ghul, X).

Query:

Knowledge Base: Finds match for first sub-query; sets a marker

Page 9: Prolog: Resolution, Unification, & Cuts

likes(batman, gotham). likes(batman, justice). likes(ras_al_ghul, justice). likes(ras_al_ghul, revenge).

likes(batman, gotham), likes(ras_al_ghul, gotham).

Query:

Knowledge Base: X is bound to gotham

Page 10: Prolog: Resolution, Unification, & Cuts

likes(batman, gotham). likes(batman, justice). likes(ras_al_ghul, justice). likes(ras_al_ghul, revenge).

likes(batman, gotham), likes(ras_al_ghul, gotham).

Query:

Knowledge Base: No match found:

fails and backtracks to marker

Page 11: Prolog: Resolution, Unification, & Cuts

likes(batman, gotham). likes(batman, justice). likes(ras_al_ghul, justice). likes(ras_al_ghul, revenge).

likes(batman, X), likes(ras_al_ghul, X).

Query:

Knowledge Base: Finds another match for first

sub-query

Page 12: Prolog: Resolution, Unification, & Cuts

likes(batman, gotham). likes(batman, justice). likes(ras_al_ghul, justice). likes(ras_al_ghul, revenge).

likes(batman, justice), likes(ras_al_ghul, justice).

Query:

Knowledge Base: X is bound to justice

Page 13: Prolog: Resolution, Unification, & Cuts

likes(batman, gotham). likes(batman, justice). likes(ras_al_ghul, justice). likes(ras_al_ghul, revenge).

likes(batman, justice), likes(ras_al_ghul, justice).

Query:

Knowledge Base: Match found, and the result

is returned

Page 14: Prolog: Resolution, Unification, & Cuts

More facts:

villain(joker). villain(penguin). villain(catwoman). villain(scarecrow). kills_people(joker). kills_people(penguin). power(scarecrow, fear). romantic_interest(catwoman). romantic_interest(talia).

Page 15: Prolog: Resolution, Unification, & Cuts

Rules

scary(V) :- villain(V),

kills_people(V).

scary(V) :- villain(V), power(V,_).

"Head" of the rule

Queries

Page 16: Prolog: Resolution, Unification, & Cuts

Who is scary? (in-class)

Page 17: Prolog: Resolution, Unification, & Cuts

Math in Prolog

Page 18: Prolog: Resolution, Unification, & Cuts

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.

Page 19: Prolog: Resolution, Unification, & Cuts

?- combined_heists(catwoman,

scarecrow,

T). T = 31+42.

Page 20: Prolog: Resolution, Unification, & Cuts

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.

Page 21: Prolog: Resolution, Unification, & Cuts

The Cut Operator

"Learn Prolog Now" section 10.2

Page 22: Prolog: Resolution, Unification, & Cuts

The Cut Operator

Motivation: • Prolog may needlessly backtrack • We wish to stop the backtracking

to optimize our code.

Page 23: Prolog: Resolution, Unification, & Cuts

max example (no cuts)

max(X,Y,Y):- X =< Y. max(X,Y,X):- X > Y.

Page 24: Prolog: Resolution, Unification, & Cuts

Using max ?- max(2,3,M). M = 3 ; false. ?- max(2,1,M). M = 2 ; false.

Why continue the search?

Page 25: Prolog: Resolution, Unification, & Cuts

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"

Page 26: Prolog: Resolution, Unification, & Cuts

max example (no cuts)

max(X,Y,Y):- X =< Y. max(X,Y,X):- X > Y.

If true, no need to

keep searching

Page 27: Prolog: Resolution, Unification, & Cuts

max example, with green cut

max(X,Y,Y):- X =< Y, !. max(X,Y,X):- X > Y.

Page 28: Prolog: Resolution, Unification, & Cuts

Red Cut Example

Batman is enemies with all villains, unless the villain is also a romantic interest.

Page 29: Prolog: Resolution, Unification, & Cuts

Red Cut Example

enemy(batman, X) :- romantic_interest(X),

!, fail.

enemy(batman, X) :- villain(X).

No backtracking once we make it here.

Page 30: Prolog: Resolution, Unification, & Cuts

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).

Page 31: Prolog: Resolution, Unification, & Cuts

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).

Page 32: Prolog: Resolution, Unification, & Cuts

Lab

For the rest of the class, continue with the lab from last class.