Artificial Intelligence

Slides:



Advertisements
Similar presentations
Artificial Intelligence: Natural Language and Prolog
Advertisements

CS4026 Formal Models of Computation Part II The Logic Model Lecture 1 – Programming in Logic.
Formal Models of Computation Part II The Logic Model
First Order Logic Logic is a mathematical attempt to formalize the way we think. First-order predicate calculus was created in an attempt to mechanize.
Inference Rules Universal Instantiation Existential Generalization
SLD-resolution Introduction Most general unifiers SLD-resolution
Computational Models The exam. Models of computation. –The Turing machine. –The Von Neumann machine. –The calculus. –The predicate calculus. Turing.
Declarative Programming Lists in PROLOG Autumn 2014.
LING 388: Language and Computers Sandiway Fong Lecture 5: 9/5.
Introduction to PROLOG ME 409 Lab - 1. Introduction to PROLOG.
Prolog: List © Patrick Blackburn, Johan Bos & Kristina Striegnitz.
Prolog The language of logic. History Kowalski: late 60’s Logician who showed logical proof can support computation. Colmerauer: early 70’s Developed.
1 Introduction to Prolog References: – – Bratko, I., Prolog Programming.
1 Logic Programming. 2 A little bit of Prolog Objects and relations between objects Facts and rules. Upper case are variables. parent(pam, bob).parent(tom,bob).
Prolog programming  unification/matching programs as deductive databases  resolution, goal reduction declarative vs. procedural readings  code tracing.
About prolog  History  Symbolic Programming Language  Logic Programming Language  Declarative Programming Language.
Chapter 12 - Logic Programming
MB: 2 March 2001CS360 Lecture 31 Programming in Logic: Prolog Prolog’s Declarative & Procedural Semantics Readings: Sections
For Friday Read “lectures” 1-5 of Learn Prolog Now: prolog-now/
Logic & Prolog  predicate calculus syntax: terms, predicates, sentences semantics: interpretations, logical consequence, inference rules, proof procedures.
Artificial Intelligence Lecture 2. Logic & Prolog Predicate calculus –syntax: terms, predicates, sentences –semantics: interpretations, logical consequence,
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Controlling Backtracking Notes for Ch.5 of Bratko For CSCE 580 Sp03 Marco Valtorta.
Constraint Logic Programming Ryan Kinworthy. Overview Introduction Logic Programming LP as a constraint programming language Constraint Logic Programming.
CSE (c) S. Tanimoto, 2005 Logic Programming 1 Logic Programming Outline: Motivation Examples: The Grandmother relation Formulation in Prolog Logic,
INFERENCE IN FIRST-ORDER LOGIC IES 503 ARTIFICIAL INTELLIGENCE İPEK SÜĞÜT.
Logic Programming Languages
Formal Models of Computation Part II The Logic Model
Notes for Chapter 12 Logic Programming The AI War Basic Concepts of Logic Programming Prolog Review questions.
FATIH UNIVERSITY Department of Computer Engineering Controlling Backtracking Notes for Ch.5 of Bratko For CENG 421 Fall03.
CS 403: Programming Languages Lecture 19 Fall 2003 Department of Computer Science University of Alabama Joel Jones.
Cs7120 (Prasad)L16-Meaning1 Procedural and Declarative Meaning of Prolog
14/10/04 AIPP Lecture 7: The Cut1 Controlling Backtracking: The Cut Artificial Intelligence Programming in Prolog Lecturer: Tim Smith Lecture 7 14/10/04.
30/09/04 AIPP Lecture 3: Recursion, Structures, and Lists1 Recursion, Structures, and Lists Artificial Intelligence Programming in Prolog Lecturer: Tim.
Logic Programming Module 2AIT202 Website Lecturer: Dave Sharp Room: AG15
Control Algorithms 1 Chapter 6 Control Algorithms 1 Chapter 6 Pattern Search.
For Wednesday No new reading Prolog handout 2 Chapter 9, exercise 4.
For Wednesday Read “lectures” 7-10 of Learn Prolog Now Chapter 9, exs 4 and 6. –6 must be in Horn clause form Prolog Handout 2.
1 Knowledge Based Systems (CM0377) Lecture 3 (Last modified 5th February 2001)
MB: 5 March 2001CS360 Lecture 41 Programming in Logic: Prolog Lists and List Operations Readings: Sections 3.1 & 3.2.
1 Prolog and Logic Languages Aaron Bloomfield CS 415 Fall 2005.
CIS2326 Week2: Logic and Prolog Lee McCluskey First term:
PROLOG SYNTAX AND MEANING Ivan Bratko University of Ljubljana Faculty of Computer and Info. Sc. Ljubljana, Slovenia.
CS Introduction to AI Tutorial 8 Resolution Tutorial 8 Resolution.
Rules Statements about objects and their relationships Expess ◦ If-then conditions  I use an umbrella if there is a rain  use(i, umbrella) :- occur(rain).
Logic Programming and Prolog Goal: use formalism of first-order logic Output described by logical formula (theorem) Input described by set of formulae.
Automated Reasoning Early AI explored how to automated several reasoning tasks – these were solved by what we might call weak problem solving methods as.
Automated Reasoning Early AI explored how to automate several reasoning tasks – these were solved by what we might call weak problem solving methods as.
For Monday Read “lectures” 1-5 of Learn Prolog Now: prolog-now/
UNIVERSITI TENAGA NASIONAL CMPB454 ARTIFICIAL INTELLIGENCE (AI) CHAPTER 6 LOGIC PROGRAMMING USING PROLOG CHAPTER 6 LOGIC PROGRAMMING USING PROLOG Instructor:
Introduction to Prolog. Outline What is Prolog? Prolog basics Prolog Demo Syntax: –Atoms and Variables –Complex Terms –Facts & Queries –Rules Examples.
© Kenneth C. Louden, Chapter 12 - Logic Programming Programming Languages: Principles and Practice, 2nd Ed. Kenneth C. Louden.
Artificial Intelligence
Programming Language Concepts Lecture 17 Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Logic Programming.
Logic Programming Tarik Booker. What will we cover?  Introduction  Definitions  Predicate Calculus  Prolog  Applications.
MB: 26 Feb 2001CS Lecture 11 Introduction Reading: Read Chapter 1 of Bratko Programming in Logic: Prolog.
For Monday Exam 1 is Monday Takehome due Prolog Handout 3 due.
For Friday No reading Prolog Handout 2. Homework.
Prolog 3 Tests and Backtracking 1. Arithmetic Operators Operators for arithmetic and value comparisons are built-in to Prolog = always accessible / don’t.
07/10/04 AIPP Lecture 5: List Processing1 List Processing Artificial Intelligence Programming in Prolog Lecturer: Tim Smith Lecture 5 07/10/04.
For Wednesday No reading Prolog handout 2 Chapter 9, exercise 4.
1 TP #2: How does Prolog answer questions? n Miscellaneous info; n Last TP exercises solved; n How does Prolog answer questions? n Recursive Prolog programs;
Prolog Fundamentals. 2 Review Last Lecture A Prolog program consists of a database of facts and rules, and queries (questions). –Fact:.... –Rule:... :-....
1 Artificial Intelligence CS370D Prolog programming Declarative meaning of Prolog programs and Lists representation.
O A procedure: a set of axioms (rules and facts) with identical signature (predicate symbol and arity). o A logic program: a set of procedures (predicates),
Logic Programming Lecture 2: Unification and proof search.
Pengenalan Prolog Disampaikan Oleh : Yusuf Nurrachman, ST, MMSI.
For Friday No reading Prolog handout 3 Chapter 9, exercises 9-11.
For Wednesday Read “lectures” 7-10 of Learn Prolog Now:
Tests, Backtracking, and Recursion
Presentation transcript:

Artificial Intelligence Lecture 4

Prolog programming unification/matching resolution, goal reduction programs as deductive databases resolution, goal reduction declarative vs. procedural readings code tracing lists

Prolog unification when variables are present, inference requires finding appropriate matches between facts & rules i.e., must Universally Instantiate variables so that facts & rules match mother(M, C) :- parent(M, C), female(M). ?- mother(laura, charlie).  requires instance mother(laura,charlie) :- parent(laura,charlie), female(laura). ?- mother(laura, jack). mother(laura,jack) :- parent(laura,jack), female(laura).

Prolog unification (2) unification is an algorithm for determining the variable instantiations needed to make expressions match query: mother(laura, charlie) rule conclusion: mother(M, C) these expressions unify/match with the variable instantiations: M = laura, C = charlie

Prolog unification (3) Prolog's unification algorithm (for matching expressions t1 and t2) if t1 and t2 are constants, then they match if they are the same object if t1 is a variable, then they match with t1 instantiated to t2 (i.e., t1 = t2) if t1 and t2 are function/predicate expressions, then they match if they have the same function/predicate symbol, AND all of their arguments match (simultaneously)

Prolog unification (4) & X = f(a) foo(you) foo(you) match since identical foo(X) foo(you) match, with X = you foo(X) foo(Y) match, with X = Y bar(X,b) bar(a,Y) match, with X = a, Y = b bar(X,f(X)) bar(a,f(a)) match, with X = a foo(X) boo(X) no match, with foo  boo foo(X,X) foo(a,f(a)) no match, requires X = a & X = f(a)

Prolog programs as databases matching/unification alone can provide for computation consider a database of facts about movies at a video store %%% to see if Bull Durham is available ?- movie(_,title('Bull Durham'),_,_,_). Yes %%% to find all comedies ?- movie(type(comedy),title(T),_,_,_). T = 'My Man Godfrey' ; T = 'Bull Durham' ; No %%% to find movies starring costner ?- movie(_,title(T),year(Y), actors(kevinCostner,_),_). T = 'Dances with Wolves' Y = 1990 ; T = 'Bull Durham' Y = 1988 ; %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%% movies.pro Dave Reed 1/25/02 movie(type(comedy), title('My Man Godfrey'), year(1936), actors(williamPowell,caroleLombard), director(gregoryLaCava)). movie(type(drama), title('Dances with Wolves'), year(1990), actors(kevinCostner,maryMcDonnell), director(kevinCostner)). title('Bull Durham'), year(1988), actors(kevinCostner,susanSarandon), director(ronShelton)). '_' is the anonymous variable, matches anything (but not reported)

Deductive databases but Prolog databases also allow for deduction can define new relations that require computation %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%% movies.pro Dave Reed 1/25/02 %%% movie info as before costars(M,F) :- movie(_,_,_,actors(M,F),_). versatile(P) :- movie(_,_,_,actors(P,_),_), movie(_,_,_,_,director(P)). movie(_,_,_,actors(_,P),_), modern(P) :- movie(_,_,year(Y),actors(P,_),_), Y >= 1990. movie(_,_,year(Y),actors(_,P),_), movie(_,_,year(Y),_,director(P)), ?- costars(M, F). M = williamPowell F = caroleLombard ; M = kevinCostner F = maryMcDonnell Yes ?- versatile(Who). Who = kevinCostner ?- modern(Who). Who = kevinCostner ; Who = maryMcDonnell ; No

Resolution the proof procedure for answering queries in Prolog is known as resolution, or goal reduction can think of a query as a goal (or sequence of goals) to be solved or proven can think of the rule H :- B1, B2. as saying "to solve/prove goal H, solve/prove subgoal B1 and then subgoal B2 "

Resolution (2) to solve/prove goals G1, …, Gn: if n = 0, then the answer is 'Yes' else find a fact or rule whose conclusion matches G1 (searching top-to-bottom) if no match, the answer is 'No' rename variables so there is no overlap with goal variables replace G1 with the premises (rhs) of the rule if matched a fact, simply remove G1 from the goal list instantiate all goals according to the above match recursively solve the new goal list if successful, the answer is 'Yes' and report all variable bindings else BACKTRACK (go back to goals G1, …, Gn and try next fact/rule)

Resolution example matches the father rule parent(dave, charlie). parent(dave, jack). parent(laura, charlie). parent(laura, jack). male(dave). male(charlie). male(jack). female(laura). father(F, C) :- parent(F, C), male(F). mother(M, C) :- parent(M, C), female(M). ?- father(F, charlie). matches the father rule rename variables in the rule: F1, C1 match binds: F = F1, C1 = charlie replace goal with premises, apply binding  ?- parent(F1, charlie), male(F1). first goal matches the top fact match binds: F1 = dave remove first goal, apply binding ?- male(dave). goal matches fact remove goal ?- no goals, SUCCESS answer is the variable binding: F = F1 = dave F = dave Yes

Example with backtracking ?- mother(M, charlie). matches the mother rule rename variables in the rule: M1, C1 match binds: M = M1, C1 = charlie replace goal with premises, apply binding  ?- parent(M1, charlie), female(M1). first goal matches the top fact match binds: M1 = dave remove first goal, apply binding ?- female(dave). no match, FAILURE BACKTRACK to previous goal list continue search after top fact, matches next fact match binds: M1 = laura ?- female(laura). matches fact, remove goal ?- no goals, SUCCESS answer is the variable binding: M = M1 = laura M = laura Yes parent(dave, charlie). parent(dave, jack). parent(laura, charlie). parent(laura, jack). male(dave). male(charlie). male(jack). female(laura). father(F, C) :- parent(F, C), male(F). mother(M, C) :- parent(M, C), female(M).

Dual readings Prolog programs can be read procedurally H :- B1, B2. "to solve H, solve B1 and B2" or declaratively: H :- B1, B2. B1  B2  H "H is true if B1 and B2 are true"

Dual readings under the declarative view: programming is defining relations using a subset of the predicate calculus a goal G is true (follows logically from the program) if there is a fact that matches G, or there is an instance of a rule with conclusion G whose premises are all true (recursively) in short, Prolog computation is logical inference  logic programming

Declarative vs. procedural the declarative reading of programs is generally more intuitive easier to reason with (centuries of research in logic) static however, must keep the procedural reading in mind

Declarative vs. procedural (2) in reality, rule and premise ordering matters ancestor(A,P) :- parent(A,P). ancestor(A,P) :- parent(A,C), ancestor(C,P). is more efficient than which is still better than ancestor(A,P) :- ancestor(C,P), parent(A,C). WHY?

Ancestor tracing (1) built-in predicates: listing will display all facts & relations defining a predicate trace will cause each inference step to be displayed in this case, an answer is found quickly since the "simple" case is listed first ?- consult(family). % family compiled 0.16 sec, 72 bytes ?- listing(ancestor). ancestor(A, B) :- parent(A, B). parent(A, C), ancestor(C, B). ?- trace. Yes [trace] ?- ancestor(Who, jack). Call: (6) ancestor(_G383, jack) ? creep Call: (7) parent(_G383, jack) ? creep Exit: (7) parent(dave, jack) ? creep Exit: (6) ancestor(dave, jack) ? creep Who = dave

?- listing(ancestor). ancestor(A, B) :- parent(A, C), ancestor(C, B). parent(A, B). Yes ?- trace. [trace] ?- ancestor(Who, jack). Call: (7) ancestor(_G386, jack) ? creep Call: (8) parent(_G386, _G441) ? creep Exit: (8) parent(winnie, dave) ? creep Call: (8) ancestor(dave, jack) ? creep Call: (9) parent(dave, _G441) ? creep Exit: (9) parent(dave, charlie) ? creep Call: (9) ancestor(charlie, jack) ? creep Call: (10) parent(charlie, _G441) ? creep Fail: (10) parent(charlie, _G441) ? creep Redo: (9) ancestor(charlie, jack) ? creep Call: (10) parent(charlie, jack) ? creep Fail: (10) parent(charlie, jack) ? creep Fail: (9) ancestor(charlie, jack) ? creep Redo: (9) parent(dave, _G441) ? creep Exit: (9) parent(dave, jack) ? creep Call: (9) ancestor(jack, jack) ? creep Call: (10) parent(jack, _G441) ? creep Fail: (10) parent(jack, _G441) ? creep Redo: (9) ancestor(jack, jack) ? creep Call: (10) parent(jack, jack) ? creep Fail: (10) parent(jack, jack) ? creep Fail: (9) ancestor(jack, jack) ? creep Fail: (9) parent(dave, _G441) ? creep Redo: (8) ancestor(dave, jack) ? creep Call: (9) parent(dave, jack) ? creep Exit: (8) ancestor(dave, jack) ? creep Exit: (7) ancestor(winnie, jack) ? creep Who = winnie Ancestor tracing (2) in this case, finding an answer requires extensive backtracking harder, recursive case is first leads to many dead ends

Ancestor tracing (3) ?- listing(ancestor). ancestor(A, B) :- ancestor(C, B), parent(A, C). parent(A, B). Yes ?- trace. [trace] ?- ancestor(Who, jack). Call: (7) ancestor(_G386, jack) ? creep Call: (8) ancestor(_G440, jack) ? creep Call: (9) ancestor(_G440, jack) ? creep Call: (10) ancestor(_G440, jack) ? creep Call: (11) ancestor(_G440, jack) ? creep Call: (12) ancestor(_G440, jack) ? creep Call: (13) ancestor(_G440, jack) ? creep Call: (14) ancestor(_G440, jack) ? creep Call: (15) ancestor(_G440, jack) ? creep Call: (16) ancestor(_G440, jack) ? creep Call: (17) ancestor(_G440, jack) ? creep . in this case, the initial recursive call leads to an infinite loop GENERAL ADVICE: put simpler cases first (e.g., facts before rules) for recursive rules, put the recursive call last, after previous inferences have constrained variables

Another example %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%% connect.pro Dave Reed 2/5/02 road(a, b). road(b, c). road(c, a). road(c, b). road(c, d). road(d, e). road(e, d). connected(City1, City2) :- road(City1, City2). road(City1, City3), connected(City3, City2). ?- connected(a, c). Yes ?- connected(c, a). ?- connected(b, d). ?- connected(b, e). ERROR: Out of local stack

Prolog lists the most general data structure in Prolog is the list a list is a (possibly empty) sequence of items, enclosed in [ ] [] [foo] [cityA, cityB, cityC]

Prolog lists (2) for a non-empty list, we say the first item is the HEAD and the rest of the list is the TAIL [foo]  HEAD = foo, TAIL = [] [cityA, cityB, cityC]  HEAD = cityA, TAIL = [cityB, cityC] lists are useful & flexible structures (also central to LISP/Scheme)

List notation [ ] notation for lists is syntactic sugar for function expressions the dot functor combines the HEAD and TAIL of a list [foo]  .(foo, []) [cityA, cityB, cityC]  .(cityA, .(cityB, .(cityC, [])))

List notation (2) the dot notation highlights the recursive nature of lists a list is either empty, OR a structure with a HEAD (an item) and a TAIL (a list) in Prolog, can use either notation ?- [a,b] = .(a, .(b, [])). Yes

List matching can match variables to specific list items since a list is really a function expression, can access via matching ?- [X,Y] = [foo, bar]. X = foo Y = bar Yes ?- [H|T] = [a, b, c]. H = a T = [b, c] ?- [H1,H2|T] = [a, b, c]. H1 = a H2 = b T = [c] can match variables to specific list items can match variables to the HEAD and TAIL using '|' can match variables to multiple items at the front, then the TAIL

List operations there are numerous predefined predicates for operating on lists member(X, L): X is a member of list L recursive definition: X is a member of L if either X is the HEAD of L, OR X is a member of the TAIL of L member is predefined, but we could define it ourselves if we had to: member(X, [X|_]. member(X, [_|T) :- member(X, T). ?- member(a, [a, b, c]). Yes ?- member(a, [b, a, c]). ?- member(a, [b, c]). No

Example revisited can add loop checking to the connected relation %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%% connect.pro Dave Reed 2/5/02 road(a, b). road(b, c). road(c, a). road(c, b). road(c, d). road(d, e). road(e, d). connected(City1, City2) :- pathConnected([City1], City2). pathConnected([City1|_], City2) :- road(City1, City2). pathConnected([CityX|Rest], City2) :- road(CityX, CityY), not(member(CityY,Rest)), pathConnected([CityY,CityX|Rest], City2). can add loop checking to the connected relation need to keep track of the path traveled so far at each step, make sure a city hasn't been visited before adding it to path here, code utilizes a help function with (reversed) path so far as first arg makes use of member to test if visited, built-in not predicate to reverse ?- connected(b, e). Yes ?- connected(e, a). No

Next week… more Prolog Read Chapter 14 Be prepared for a quiz on built-in predicates operators AI example Read Chapter 14 Be prepared for a quiz on this week’s lecture (moderately thorough) the reading (superficial)

Questions & Answers ???????