300 likes | 427 Views
Rule Based Systems (Prolog). M. Reza Zakerinasab mrzakeri@ucalgary.ca Please include [CPSC433] in the subject line of any emails regarding this course. Slides originally created by Andrew M Kuipers. Some slides adopted from 600.325/425 Declarative Methods - J. Eisner.
E N D
Rule Based Systems(Prolog) M. Reza Zakerinasab mrzakeri@ucalgary.ca Please include [CPSC433] in the subject line of any emails regarding this course. Slides originally created by Andrew M Kuipers. Some slides adopted from 600.325/425 Declarative Methods - J. Eisner CPSC 433 Artificial Intelligence
Prolog as constraint programming • An ordinary constraint between two variables: Person and Food • Prolog makes you name this constraint. Here’s a program that defines it: • eats(sam, dal). eats(josie, samosas). • eats(sam, curry). eats(josie, curry). • eats(rajiv, dal). … • Now it acts like a subroutine! At the Prolog prompt you can type • eats(Person1, Food1). % constraint over two variables • eats(Person2, Food2). % constraint over two other variables CPSC 433 Artificial Intelligence
Using Prolog • eats(sam, dal). eats(josie, samosas). • eats(sam, curry). eats(josie, curry). • eats(rajiv, burgers). eats(rajiv, dal). … • eats(Person1, Food), eats(Person2, Food). • Person1=sam, Person2=josie, Food=curry • Person1=josie, Person2=sam, Food=curry … Your program file (compiled) Sometimes called the “database” “Query” that you type interactively Prolog’s answer CPSC 433 Artificial Intelligence
Adding more rules… eats(sam, dal). eats(josie, samosas). eats(sam, curry). eats(josie, curry). eats(rajiv, burgers). eats(rajiv, dal). compatible(Person1, Person2) :- eats(Person1, Food), eats(Person2, Food). compatible(Person1, Person2) :- watches(Person1, Movie), watches(Person2, Movie). compatible(hal, Person2) :- female(Person2), rich(Person2). CPSC 433 Artificial Intelligence
Basic Prolog Process Negate query, set as current goal. while current goal is non-empty: choose the leftmost subgoal if a rule applies to the subgoal: select the first applicable rule (top-to-bottom) perform resolution on subgoal and selected rule else: backtrack if possible, otherwise fail success CPSC 433 Artificial Intelligence
Sir Bedevere’s Infamous Deduction witch(X) :- burns(X),woman(X). woman(girl). burns(X) :- madeofwood(X). madeofwood(X) :- floats(X). floats(duck). floats(Y) :- floats(X),sameweight(X,Y). sameweight(duck,girl). Logically sound?!! ?- witch(X). Goals CPSC 433 Artificial Intelligence
Sir Bedevere’s Infamous Deduction witch(X) :- burns(X),woman(X). woman(girl). burns(X) :- madeofwood(X). madeofwood(X) :- floats(X). floats(duck). floats(Y) :- floats(X),sameweight(X,Y). sameweight(duck,girl). ?- witch(X). Goals witch(_G0) CPSC 433 Artificial Intelligence
Sir Bedevere’s Infamous Deduction witch(X) :- burns(X),woman(X). woman(girl). burns(X) :- madeofwood(X). madeofwood(X) :- floats(X). floats(duck). floats(Y) :- floats(X),sameweight(X,Y). sameweight(duck,girl). ?- witch(X). Goals witch(_G0) CPSC 433 Artificial Intelligence
Sir Bedevere’s Infamous Deduction witch(X) :- burns(X),woman(X). woman(girl). burns(X) :- madeofwood(X). madeofwood(X) :- floats(X). floats(duck). floats(Y) :- floats(X),sameweight(X,Y). sameweight(duck,girl). ?- witch(X). Goals witch(_G0) burns(_G0) woman(_G0) CPSC 433 Artificial Intelligence
Sir Bedevere’s Infamous Deduction witch(X) :- burns(X),woman(X). woman(girl). burns(X) :- madeofwood(X). madeofwood(X) :- floats(X). floats(duck). floats(Y) :- floats(X),sameweight(X,Y). sameweight(duck,girl). ?- witch(X). Goals witch(_G0) burns(_G0) woman(_G0) CPSC 433 Artificial Intelligence
Sir Bedevere’s Infamous Deduction witch(X) :- burns(X),woman(X). woman(girl). burns(X) :- madeofwood(X). madeofwood(X) :- floats(X). floats(duck). floats(Y) :- floats(X),sameweight(X,Y). sameweight(duck,girl). ?- witch(X). Goals witch(_G0) burns(_G0) woman(_G0) madeofwood(_G0) CPSC 433 Artificial Intelligence
Sir Bedevere’s Infamous Deduction witch(X) :- burns(X),woman(X). woman(girl). burns(X) :- madeofwood(X). madeofwood(X) :- floats(X). floats(duck). floats(Y) :- floats(X),sameweight(X,Y). sameweight(duck,girl). ?- witch(X). Goals witch(_G0) burns(_G0) woman(_G0) madeofwood(_G0) CPSC 433 Artificial Intelligence
Sir Bedevere’s Infamous Deduction witch(X) :- burns(X),woman(X). woman(girl). burns(X) :- madeofwood(X). madeofwood(X) :- floats(X). floats(duck). floats(Y) :- floats(X),sameweight(X,Y). sameweight(duck,girl). • ?- witch(X). • Goals • witch(_G0) • burns(_G0) woman(_G0) • madeofwood(_G0) • floats(_G0) CPSC 433 Artificial Intelligence
Sir Bedevere’s Infamous Deduction witch(X) :- burns(X),woman(X). woman(girl). burns(X) :- madeofwood(X). madeofwood(X) :- floats(X). floats(duck). floats(Y) :- floats(X),sameweight(X,Y). sameweight(duck,girl). • ?- witch(X). • Goals • witch(_G0) • burns(_G0) woman(_G0) • madeofwood(_G0) • floats(_G0) CPSC 433 Artificial Intelligence
Sir Bedevere’s Infamous Deduction witch(X) :- burns(X),woman(X). woman(girl). burns(X) :- madeofwood(X). madeofwood(X) :- floats(X). floats(duck). floats(Y) :- floats(X),sameweight(X,Y). sameweight(duck,girl). • ?- witch(X). • Goals • witch(_G0) • burns(_G0) woman(_G0) • madeofwood(_G0) • floats(duck) CPSC 433 Artificial Intelligence
Sir Bedevere’s Infamous Deduction witch(X) :- burns(X),woman(X). woman(girl). burns(X) :- madeofwood(X). madeofwood(X) :- floats(X). floats(duck). floats(Y) :- floats(X),sameweight(X,Y). sameweight(duck,girl). • ?- witch(X). • Goals • witch(_G0) • burns(_G0) woman(_G0) • madeofwood(duck) CPSC 433 Artificial Intelligence
Sir Bedevere’s Infamous Deduction witch(X) :- burns(X),woman(X). woman(girl). burns(X) :- madeofwood(X). madeofwood(X) :- floats(X). floats(duck). floats(Y) :- floats(X),sameweight(X,Y). sameweight(duck,girl). ?- witch(X). Goals witch(_G0) burns(duck) woman(duck) CPSC 433 Artificial Intelligence
Sir Bedevere’s Infamous Deduction witch(X) :- burns(X),woman(X). woman(girl). burns(X) :- madeofwood(X). madeofwood(X) :- floats(X). floats(duck). floats(Y) :- floats(X),sameweight(X,Y). sameweight(duck,girl). ?- witch(X). Goals witch(_G0) burns(duck) woman(duck) fail! backtrack... CPSC 433 Artificial Intelligence
Sir Bedevere’s Infamous Deduction witch(X) :- burns(X),woman(X). woman(girl). burns(X) :- madeofwood(X). madeofwood(X) :- floats(X). floats(duck). floats(Y) :- floats(X),sameweight(X,Y). sameweight(duck,girl). • ?- witch(X). • Goals • witch(_G0) • burns(_G0) woman(_G0) • madeofwood(_G0) • floats(_G0) CPSC 433 Artificial Intelligence
Sir Bedevere’s Infamous Deduction witch(X) :- burns(X),woman(X). woman(girl). burns(X) :- madeofwood(X). madeofwood(X) :- floats(X). floats(duck). floats(Y) :- floats(X),sameweight(X,Y). sameweight(duck,girl). • ?- witch(X). • Goals • witch(_G0) • burns(_G0) woman(_G0) • madeofwood(_G0) • floats(_G0) • floats(_G1) sameweight(_G1, _G0) CPSC 433 Artificial Intelligence
Sir Bedevere’s Infamous Deduction witch(X) :- burns(X),woman(X). woman(girl). burns(X) :- madeofwood(X). madeofwood(X) :- floats(X). floats(duck). floats(Y) :- floats(X),sameweight(X,Y). sameweight(duck,girl). • ?- witch(X). • Goals • witch(_G0) • burns(_G0) woman(_G0) • madeofwood(_G0) • floats(_G0) • floats(_G1) sameweight(_G1, _G0) CPSC 433 Artificial Intelligence
Sir Bedevere’s Infamous Deduction witch(X) :- burns(X),woman(X). woman(girl). burns(X) :- madeofwood(X). madeofwood(X) :- floats(X). floats(duck). floats(Y) :- floats(X),sameweight(X,Y). sameweight(duck,girl). • ?- witch(X). • Goals • witch(_G0) • burns(_G0) woman(_G0) • madeofwood(_G0) • floats(_G0) • floats(duck) sameweight(duck, _G0) CPSC 433 Artificial Intelligence
Sir Bedevere’s Infamous Deduction witch(X) :- burns(X),woman(X). woman(girl). burns(X) :- madeofwood(X). madeofwood(X) :- floats(X). floats(duck). floats(Y) :- floats(X),sameweight(X,Y). sameweight(duck,girl). • ?- witch(X). • Goals • witch(_G0) • burns(_G0) woman(_G0) • madeofwood(_G0) • floats(_G0) • floats(duck) sameweight(duck, _G0) CPSC 433 Artificial Intelligence
Sir Bedevere’s Infamous Deduction witch(X) :- burns(X),woman(X). woman(girl). burns(X) :- madeofwood(X). madeofwood(X) :- floats(X). floats(duck). floats(Y) :- floats(X),sameweight(X,Y). sameweight(duck,girl). • ?- witch(X). • Goals • witch(_G0) • burns(_G0) woman(_G0) • madeofwood(_G0) • floats(_G0) • floats(duck) sameweight(duck, girl) CPSC 433 Artificial Intelligence
Sir Bedevere’s Infamous Deduction witch(X) :- burns(X),woman(X). woman(girl). burns(X) :- madeofwood(X). madeofwood(X) :- floats(X). floats(duck). floats(Y) :- floats(X),sameweight(X,Y). sameweight(duck,girl). • ?- witch(X). • Goals • witch(_G0) • burns(_G0) woman(_G0) • madeofwood(_G0) • floats(girl) CPSC 433 Artificial Intelligence
Sir Bedevere’s Infamous Deduction witch(X) :- burns(X),woman(X). woman(girl). burns(X) :- madeofwood(X). madeofwood(X) :- floats(X). floats(duck). floats(Y) :- floats(X),sameweight(X,Y). sameweight(duck,girl). • ?- witch(X). • Goals • witch(_G0) • burns(_G0) woman(_G0) • madeofwood(girl) CPSC 433 Artificial Intelligence
Sir Bedevere’s Infamous Deduction witch(X) :- burns(X),woman(X). woman(girl). burns(X) :- madeofwood(X). madeofwood(X) :- floats(X). floats(duck). floats(Y) :- floats(X),sameweight(X,Y). sameweight(duck,girl). ?- witch(X). Goals witch(_G0) burns(girl) woman(girl) CPSC 433 Artificial Intelligence
Sir Bedevere’s Infamous Deduction witch(X) :- burns(X),woman(X). woman(girl). burns(X) :- madeofwood(X). madeofwood(X) :- floats(X). floats(duck). floats(Y) :- floats(X),sameweight(X,Y). sameweight(duck,girl). ?- witch(X). Goals witch(_G0) burns(girl) woman(girl) works this time CPSC 433 Artificial Intelligence
Sir Bedevere’s Infamous Deduction witch(X) :- burns(X),woman(X). woman(girl). burns(X) :- madeofwood(X). madeofwood(X) :- floats(X). floats(duck). floats(Y) :- floats(X),sameweight(X,Y). sameweight(duck,girl). ?- witch(X). Goals witch(girl) CPSC 433 Artificial Intelligence
Sir Bedevere’s Infamous Deduction witch(X) :- burns(X),woman(X). woman(girl). burns(X) :- madeofwood(X). madeofwood(X) :- floats(X). floats(duck). floats(Y) :- floats(X),sameweight(X,Y). sameweight(duck,girl). ?- witch(X). X = girl CPSC 433 Artificial Intelligence