1 Knowledge Based Systems (CM0377) Lecture 3 (Last modified 5th February 2001)

Slides:



Advertisements
Similar presentations
Artificial Intelligence: Natural Language and Prolog
Advertisements

Formal Models of Computation Part II The Logic Model
AI/ES (Artificial Intelligence / Expert System) Visual Prolog: Part 2
SLD-resolution Introduction Most general unifiers SLD-resolution
Chapter 11 :: Logic Languages
Prolog.
Introduction to PROLOG ME 409 Lab - 1. Introduction to PROLOG.
11/10/04 AIPP Lecture 6: Built-in Predicates1 Combining Lists & Built-in Predicates Artificial Intelligence Programming in Prolog Lecturer: Tim Smith Lecture.
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).
1 Knowledge Based Systems (CM0377) Lecture 8 (Last modified 5th March 2001)
CS 330 Programming Languages 12 / 02 / 2008 Instructor: Michael Eckmann.
CSE 425: Logic Programming I Logic and Programs Most programs use Boolean expressions over data Logic statements can express program semantics –I.e., axiomatic.
CS 330 Programming Languages 12 / 12 / 2006 Instructor: Michael Eckmann.
For Friday Read “lectures” 1-5 of Learn Prolog Now: prolog-now/
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Controlling Backtracking Notes for Ch.5 of Bratko For CSCE 580 Sp03 Marco Valtorta.
CIA2326 Week3: Prolog: List Processing Lee McCluskey First term:
LING 388: Language and Computers Sandiway Fong Lecture 4: 8/31.
LING 388: Language and Computers Sandiway Fong Lecture 3: 8/29.
1 CILOG User Manual Bayesian Networks Seminar Sep 7th, 2006.
(9.1) COEN Logic Programming  Logic programming and predicate calculus  Prolog statements  Facts and rules  Matching  Subgoals and backtracking.
Prolog Programming Lecture Module 13. Objective ● What is Prolog? ● Prolog program ● Syntax of Prolog ● Prolog Control Strategy ● Execution of Prolog.
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.
LING 388: Language and Computers Sandiway Fong Lecture 7.
CS 321 Programming Languages and Compilers Prolog part 2.
CS 403: Programming Languages Lecture 19 Fall 2003 Department of Computer Science University of Alabama Joel Jones.
1 Lecture 6 Logic Programming introduction to Prolog, facts, rules Ras Bodik Shaon Barman Thibaud Hottelier Hack Your Language! CS164: Introduction to.
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.
F28PL1 Programming Languages Lecture 16: Prolog 1.
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.
Introduction To PROLOG World view of imperative languages. World view of relational languages. A PROLOG program. Running a PROLOG program. A PROLOG.
CSC 270 – Survey of Programming Languages Prolog Lecture 2 – Unification and Proof Search.
1 Prolog and Logic Languages Aaron Bloomfield CS 415 Fall 2005.
PROLOG SYNTAX AND MEANING Ivan Bratko University of Ljubljana Faculty of Computer and Info. Sc. Ljubljana, Slovenia.
Logic Programming CSC 358/ Outline Pattern matching Unification Logic programming.
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).
Ch. 13 Ch. 131 jcmt CSE 3302 Programming Languages CSE3302 Programming Languages (notes?) Dr. Carter Tiernan.
Lab Lecture#4 Lecturer : Sheriff Nafisa TA : Mubarakah Otbi, Duaa al Ofi, Huda al Hakami.
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.
Programming Languages Third Edition Chapter 4 Logic Programming.
1 Knowledge Based Systems (CM0377) Lecture 6 (last modified 20th February 2002)
LING/C SC/PSYC 438/538 Lecture 15 Sandiway Fong. Did you install SWI Prolog?
CS 152: Programming Language Paradigms March 12 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak.
Programming Language Concepts Lecture 17 Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Logic Programming.
Knowledge Based Information System
In The Name Of Allah Lab 03 1Tahani Aldweesh. objectives Searching for the solution’s. Declaration. Query. Comments. Prolog Concepts. Unification. Disjunction.
For Friday No reading Prolog Handout 2. Homework.
1 Knowledge Based Systems (CM0377) Lecture 9 (Last modified 11th April 2002)
Prolog 3 Tests and Backtracking 1. Arithmetic Operators Operators for arithmetic and value comparisons are built-in to Prolog = always accessible / don’t.
Logic Programming Lecture 8: Term manipulation & Meta-programming.
07/10/04 AIPP Lecture 5: List Processing1 List Processing Artificial Intelligence Programming in Prolog Lecturer: Tim Smith Lecture 5 07/10/04.
COP4020 Programming Languages Logic Programming Prof. Xin Yuan.
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.
Logic Programming Lecture 2: Unification and proof search.
The portion of a Prolog interpreter that executes queries (goals) is known as the inference engine. An inference engine is a kind of theorem prover, using.
Section 16.5, 16.6 plus other references
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
Chapter 3: Prolog (Lists, Arithmetic, Operators)
Chapter 12 :: Logic Languages
Programming Paradigms and Languages
Programming Techniques
Chapter 2: Prolog (Introduction and Basic Concepts)
Presentation transcript:

1 Knowledge Based Systems (CM0377) Lecture 3 (Last modified 5th February 2001)

2 Recall: Resolution - General pattern To answer a query find a rule such that A matches with Q 1 and answer query: instead (resolution)

3 More complicated rules Consider the goal ?- brother_of(X, fred). brother_of(B, fred):-parent_of(P,B),parent_of(P,fred),male(B),B\==fred. parent_of(bill,jim), parent_of(bill,fred) parent_of(jane,jim), parent_of(jane,fred), male(jim), jim\==fredX=jim parent_of(bill,albert), parent_of(bill, fred) parent_of(jane,albert),parent_of(jane,fred),male(albert), albert\==fredX=albert parent_of(jim,alice), parent_of(jim,fred), male(alice) parent_of(jane,alice),parent_of(jane,fred), male(alice) parent_of(jim,fred),parent_of(jim,fred),male(fred), fred\==fred parent_of(jane,fred),parent_of(jane,fred),male(fred), fred\==fred parent_of(jim,john),parent_of(jim,fred),male(john),john\==fredX=john parent_of(jane,john),parent_of(jane,fred),male(john),john\==fred X=john

4 Backtracking If Prolog fails to satisfy a sub-goal of a clause, it backtracks from right to left, searching for a sub-goal that can be satisfied differently. If there is more than one clause (e.g. the fact example that follows), then having failed completely on one clause Prolog proceeds to the next.

5 Factorial fact(0,1). fact(X, F):- X>0, X1 is X - 1, fact(X1,F1), F is X * F1. NB X is X-1 is never true: if X has a value, this tests whether X is equal to X-1; if not, X-1 can’t be evaluated and Prolog crashes X1 = X-1 makes X1 equal to a structure ' -'(X,1). Use is to evaluate arith. expressions

6 Tracing the goal fact(2, X) | ?- fact(2, X). 1 1 Call: fact(2,_170) ? 2 2 Call: 2>0 ? 2 2 Exit: 2>0 ? 3 2 Call: _659 is 2-1 ? 3 2 Exit: 1 is 2-1 ? 4 2 Call: fact(1,_651) ? 5 3 Call: 1>0 ? 5 3 Exit: 1>0 ? 6 3 Call: _2650 is 1-1 ? 6 3 Exit: 0 is 1-1 ? 7 3 Call: fact(0,_2642) ? ? 7 3 Exit: fact(0,1) ? 8 3 Call: _651 is 1*1 ? 8 3 Exit: 1 is 1*1 ? ? 4 2 Exit: fact(1,1) ? 9 2 Call: _170 is 2*1 ? 9 2 Exit: 2 is 2*1 ? ? 1 1 Exit: fact(2,2) ? X = 2 ? yes {trace} | ?-

7 What’s going on? Consider: 3 2 Call: _659 is 2-1 ? Left-most number is, in effect, the step number of the corresponding node in the proof tree being constructed Next number is the recursive depth _659 is an internal variable name...

8 Recursive use of fact This clause is used twice in the proof: fact(X, F):- X>0, X1 is X - 1, fact(X1,F1), F is X * F1. Internally, Prolog invents names for each distinct variable. First time, the goal is: fact(2, _170):- 2>0, _659 is 2 - 1, fact(_659,_651), _170 is 2 * _651. Second time, by which _659 is bound to the value 1, the goal is: fact(1, _651):- 1>0, _2650 is 1 - 1, fact(_2650,_2642), _651 is 1 * _2642.

9 Proof tree for fact(2, Fact) (exercise for reader to complete this!)

10 The box model of goal execution CALL port: initial invocation of goal EXIT: successful return from goal REDO: backtrack to find another solution FAIL: the goal couldn’t be satisfied any more times

11 Structured terms Prolog allows us to have record-like, nested structures, e.g. owns(bill, book(’A.N. Other’, ’The laws of confusion’, ’Thomson’, 1993)) These can be used to determine behaviour that is specific to the form of the term, i.e. its functor (‘book’, in the above example) and arguments.

12 Example display_item(book(Auth, Title, Pub, Yr)):- write('a book: '), nl, write(Auth), nl, write('"'), write(Title), write('" Publisher: '), write(Pub), write(' Year: '), write(Yr), nl. display_item(car(Manuf, Mod, Miles, Yr)):- write('a car: '), nl, write(Manuf), write(' '), write(Mod), write(' Mileage: '), write(Miles), write(' Year: '), write(Yr), nl, nl. (See ‘owns’ program on separate sheet)

13 NB write( ) writes out an atom; nl moves to a new line; fail simply fails.

14 NB (ctd.) It’s always best to implement deterministic predicates (things that perform a task, rather than do reasoning) so as to succeed. Hence: go:- owns(X, Item), write(X), write(' owns '), display_item(Item), fail. go.

15 Thus we can have lists! parent_of(bill, list(jim, list(albert, empty))). father_of(F, Ch):- parent_of(F, List), member(Ch, List), male(F). member(X, list(X, _)). member(X, list(_, Others)):- member(X, Others).

16 Prolog’s special list notation A list in Prolog is a sequence of items surrounded by square brackets, e.g. [1, 2, 3] which is equivalent, entirely, to: '. ' (1, '. ' (2, '. ' (3, []))) Empty list has the special representation []

17 List notation (ctd.) Vertical bar ‘|’ in a list - what’s to the right is the remainder of the list. So the following patterns match: [1, 2, 3] = [X | Y], where X=1 and Y=[2, 3] [1, 2, 3] = [X, Y | Z], where X=1, Y=2 and Z= [3] [a, b] = [X, Y|Z] where X=a, Y=b and Z=[]

18 Revised genealogy program parent_of(jim, [alice, fred, john]). parent_of(jane, '.'(jim, '.'(albert, []))). father_of(F, Ch):- parent_of(F, List), member(Ch, List), male(F). member(X, [X|_]). member(X, [_|T]):-member(X, T).

19 Unassessed exercise (highly recommended!) Download the files for today’s examples: fact.pl, owns.pl, new_fam.pl and new_fam1.pl and try them out using SICStus. Things you need to know: –start Prolog by typing: sicstus –load programs in by typing [ ], but leaving off the ‘.pl’ –run goals by typing the goal followed by full stop –exit Prolog by typing D –break into execution by C, then type a to abort execution.

20 Things you need to know (ctd.) to turn tracing on, type the goal: ?- trace. to turn it off again, type the goal: ?- notrace. you can browse the Sicstus manuals at: