1 Knowledge Based Systems (CM0377) Lecture 9 (Last modified 11th April 2002)

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.
© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 10: Cuts and Negation Theory –Explain how to control Prolog`s backtracking behaviour with.
AI/ES (Artificial Intelligence / Expert System) Visual Prolog: Part 2
Higher Order Predicates Higher order predicates in PROLOG. Higher order declarative predicates : – findall – bagof – setof – =.. Higher order non-declarative.
Simply Logical – Chapter 3© Peter Flach, 2000 :-teaches(peter,ai_techniques) :-teaches(peter,expert_systems) :-teaches(peter,computer_science) ?-student_of(S,peter)
1 Knowledge Based Systems (CM0377) Lecture 11 (Last modified 26th March 2001)
SLD-resolution Introduction Most general unifiers SLD-resolution
Chapter 11 :: Logic Languages
Prolog.
Logic Programming – Part 2 Lists Backtracking Optimization (via the cut operator) Meta-Circular Interpreters.
Logic Programming – Part 2 Lists Backtracking Optimization (via the cut operator) Meta-Circular Interpreters.
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.
”assert” and ”retract”
INTRODUCTION TO PROLOG. PROLOG BASICS Atoms - most primitive terms that the language manipulates start with lower case letter includes strings (‘inside.
1 Knowledge Based Systems (CM0377) Lecture 8 (Last modified 5th March 2001)
Logic Programming Lecture 5: Nonlogical features, continued: negation-as-failure, collecting solutions, assert/retract.
Comp 307 Lecture 4:1 Prolog I, II Prolog was taught as a procedural language Control structures: if, while, recursion Data structures: structured terms,
For Monday Take home exam due Exam 1. For Wednesday Read chapter 10, sections 1-2 Prolog Handout 4.
MB: 2 March 2001CS360 Lecture 31 Programming in Logic: Prolog Prolog’s Declarative & Procedural Semantics Readings: Sections
1 Prolog II. 2 The Notion of Unification Unification is when two things “become one” Unification is kind of like assignment Unification is kind of like.
For Friday Read “lectures” 1-5 of Learn Prolog Now: prolog-now/
11-Jun-15 Prolog II Unification and clause order.
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering More Built-in Predicates Notes for Ch.7 of Bratko For CSCE 580 Sp03 Marco Valtorta.
”assert” and ”retract”  Modifying Programs Dynamically  Prolog’s Execution Tree.
1 CILOG User Manual Bayesian Networks Seminar Sep 7th, 2006.
Methods of Proof & Proof Strategies
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.
1 Knowledge Based Systems (CM0377) Lecture 12 (Last modified 2nd May 2002)
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.
1 Knowledge Based Systems (CM0377) Lecture 4 (Last modified 5th February 2001)
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.
15/11/04 AIPP Lecture 14: Database Manipulation1 Database Manipulation Artificial Intelligence Programming in Prolog Lecturer: Tim Smith Lecture 14 15/11/04.
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.
Prolog Programming in Logic. 2 SWI-Prolog SWI-Prolog is a good, standard Prolog for Windows and Linux Can be installed on Macintosh with a little more.
B. Ross Cosc 4f79 1 Inference mechanisms Backward chaining - goal-driven reasoning which gathers data as needed - Prolog's default mechanism - good for.
1 Knowledge Based Systems (CM0377) Lecture 3 (Last modified 5th February 2001)
1 Prolog and Logic Languages Aaron Bloomfield CS 415 Fall 2005.
Logic Programming and Prolog Goal: use formalism of first-order logic Output described by logical formula (theorem) Input described by set of formulae.
Ch. 13 Ch. 131 jcmt CSE 3302 Programming Languages CSE3302 Programming Languages (notes?) Dr. Carter Tiernan.
Logic Programming Lecture 5: Nonlogical features, continued: negation-as-failure, assert/retract, collecting solutions.
Introduction to Prolog. Outline What is Prolog? Prolog basics Prolog Demo Syntax: –Atoms and Variables –Complex Terms –Facts & Queries –Rules Examples.
1 Knowledge Based Systems (CM0377) Lecture 10 (Last modified 19th March 2001)
1 Knowledge Based Systems (CM0377) Introductory lecture (Last revised 28th January 2002)
1 Knowledge Based Systems (CM0377) Lecture 6 (last modified 20th February 2002)
Prolog Unification and clause order. The notion of Unification Unification is when two things “become one” Unification is kind of like assignment Unification.
Programming Language Concepts Lecture 17 Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Logic Programming.
MB: 26 Feb 2001CS Lecture 11 Introduction Reading: Read Chapter 1 of Bratko Programming in Logic: Prolog.
In The Name Of Allah Lab 03 1Tahani Aldweesh. objectives Searching for the solution’s. Declaration. Query. Comments. Prolog Concepts. Unification. Disjunction.
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.
23-Feb-16 Prolog II Unification and clause order.
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;
C. Varela1 Logic Programming (PLP 11, CTM 9.3) Prolog Imperative Control Flow: Backtracking, Cut, Fail, Not Lists, Append Carlos Varela Rennselaer Polytechnic.
Prolog Fundamentals. 2 Review Last Lecture A Prolog program consists of a database of facts and rules, and queries (questions). –Fact:.... –Rule:... :-....
Logic Programming Lecture 8: Term manipulation & Meta-programming.
Logic Programming Lecture 8: Term manipulation & Meta-programming.
Logic Programming Lecture 2: Unification and proof search.
Carlos Varela Rensselaer Polytechnic Institute November 17, 2017
Tests, Backtracking, and Recursion
Unification and clause order
Chapter 2: Prolog (Introduction and Basic Concepts)
Carlos Varela Rennselaer Polytechnic Institute November 15, 2016
Presentation transcript:

1 Knowledge Based Systems (CM0377) Lecture 9 (Last modified 11th April 2002)

2 Reasoning about programs Prolog allows us to write programs that reason about - and even modify - themselves: –Meta-predicates take object-level clauses as arguments; –Second-order predicates take first-order predicates as arguments. We’ll use such predicates to write: –various Prolog interpreters –a forward chaining inference engine for an expert system –interpreters for non-classical reasoning

3 Motivating Example Consider the following program: parent_of(bill, jim). parent_of(jane, jim). parent_of(jim, john). parent_of(john, fred). male(bill). male(jim). male(john). male(fred). female(jane). father_of(F, C):- parent_of(F, C), male(F). mother_of(M, C):- parent_of(M, C), female(M).

4 Motivating example (ctd.) Would be nice if we could take two lists such as [bill, jim, john] and [jim, john, fred], and check whether each person in first list is a parent of corresponding person in second list. One way is: parents([], []). parents([P | Ps], [C | Cs]):- parent_of(P, C), parents(Ps, Cs). e.g. ?- parents([bill, jim, john], [jim, john, fred])

5 Motivating example (ctd.) But would be nice if we could do the same for mother_of and father_of. We can use the ‘univ’ built-in predicate to build a term from a list, e.g. ?- Term =.. [parent_of, X, jim] Term = parent_of(X, jim) ?- parent_of(jane, Y) =.. List List = [parent_of, jane, Y]

6 Motivating example (ctd.) So we can define a second-order predicate rel that we can give the name of the predicate we actually want to test, e.g. rel(parent_of, Ps, Cs) the code is: rel(R, [], []) rel(R, [X | Xs], [Y | Ys]):- Goal =.. [R, X, Y], Goal, rel(R, Xs, Ys).

7 Motivating example (ctd.) So the previous ‘parents’ goal is replaced by ?- rel(parent_of, [bill, jim, john], [jim, john, fred]) And we could also try, e.g. ?- rel(mother_of, [jane], [jim]). (And, of course, queries that would fail.) Unassessed exercise: draw SLD trees for these queries.

8 Finding all solutions Now suppose we want to find all children. One way would be to add clause: find_kids:-parent_of(P, C), write(C), nl, fail. find_kids. But this only writes them out!

9 The findall predicate To get a list of all the children, use findall: | ?- findall(C, parent_of(Parent, C), Children). Children = [jim,jim,john,fred] Note this doesn’t find a specific value for Parent: it tries each in turn. See Flach pp for other related predicates, bagof and setof, if interested

10 Manipulating clauses We have already seen how call and not can be used to treat terms as if they are atoms, and use them as queries. Prolog also allows us to: –Construct and add a new clause to a program dynamically –Remove a clause dynamically –‘Dissect’ a clause

11 Clauses and terms A clause such as: father_of(X, Y):- parent_of(X, Y), male(X). is identical, internally, to: ':-'(father_of(X, Y), ','(parent_of(X, Y), male(X))). Using assert we can construct a term then add it to a program...

12 Using assert Example: | ?- A=father_of(X, Y), B=parent_of(X, Y), C=male(X), assert((A:-B, C)). A = father_of(X,Y), B = parent_of(X,Y), C = male(X) ? yes | ?- listing.... father_of(A, B) :- parent_of(A, B), male(A).

13 Using assert (ctd.) Alternatives: | ?- A=father_of(X, Y), B=parent_of(X, Y), C=male(X), assert(':-'(A, ','(B, C))). and: | ?- assert((father_of(X, Y):- parent_of(X, Y), male(X))).

14 Retract... Similarly, can remove one or more clauses using retract. retract(Clause) retracts the first clause which unifies with Clause from our program. On backtracking, it’ll remove the next one, etc. E.g. with the ‘motivating example’ given earlier, we could do the following: | ?- Z=parent_of(X, Y), retract(Z), write(Z), nl, fail. parent_of(bill,jim) parent_of(jane,jim) parent_of(jim,john) parent_of(john,fred) no | ?-

15 The clause predicate clause allows us to find out what clauses are in a program. A goal of the form clause(H,B) will unify H with the head and B with the body of the first clause that unifies with H:-B. E.g., in the above program, clause(parent_of(X, Y), Z) would have X=bill, Y=jim, Z=true as first solution.

16 BUT... This is only guaranteed to work (as is assert/retract) if any predicate is declared as dynamic in the program. E.g., at start of program: :- dynamic(parent_of/2). The number after the ‘/’ is the arity of the predicate so declared. Alternatively, load a program making everything dynamic by using: ?- load_files([ ], [compilation_mode(assert_all)]). e.g. ?- load_files([famil], [compilation_mode(assert_all)]).

17 A Prolog interpreter in Prolog! We can define an interpreter for ‘pure’ Prolog in Prolog itself: prove(Goal):-(i) clause(Goal, Body), prove(Body). prove((Goal1, Goal2)):-(ii) prove(Goal1), prove(Goal2). prove(true).(iii)

18 Example Suppose we have the program: male(mark). parent_of(mark, jim). father_of(X, Y):-parent_of(X, Y), male(X). Then the query ?-prove(father_of(X, Y)) is processed thus: –Clause (i) finds a clause, Goal=father_of(X, Y), Body= ', ' (parent_of(X, Y), male(X)). –To prove Body, clause (ii) is used. It tries to prove parent_of(X, Y) and male(X). –To prove parent_of(X, Y), clause (i) finds a clause, Goal=parent_of(mark, jim) and Body=true. –To prove true, clause (iii) succeeds trivially. –ETC.

19 A spanner in the works clause(true, Body) will cause an error (similarly for any built-in predicate). A small modification fixes this: prove(Goal):- \+(Goal = true), clause(Goal, Body), prove(Body). prove((Goal1, Goal2)):- prove(Goal1), prove(Goal2). prove(true).

20 A meta-circular interpreter If our first interpreter had worked, it could have interpreted itself. To make this new one interpret itself, it also has to be able to handle not (\+), = and ‘,’ specially: prove(clause(X, Y)):-clause(X, Y). prove(A=A). prove(\+ A):- \+ prove(A). prove(Goal):- \+(Goal = true), \+(Goal = ','(_, _)), \+(Goal = \+(_)), \+(Goal = '='(_, _)), \+(Goal = clause(_, _)), clause(Goal, Body), prove(Body). prove((Goal1, Goal2)):- prove(Goal1), prove(Goal2). prove(true).

21 The meta-circular interpreter Now the goal ?-prove(prove(father_of(X, Y))) will give the correct answer, as will ?-prove(prove(prove(father_of(X, Y)))), etc. These techniques will be useful for implementing alternative inference strategies and expert systems. Further reading: see Flach, pp , especially 66/67 and 70/71. Also, see pp for some useful list-handling techniques.