Lecture course: Heuristics & Prolog for Artificial Intelligence Applications Notes on Prolog from Professor Bruce Batchelor, DSc.

Slides:



Advertisements
Similar presentations
Artificial Intelligence: Natural Language and Prolog
Advertisements

Formal Models of Computation Part II The Logic Model
Some Prolog Prolog is a logic programming language
Chapter 11 :: Logic Languages
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.
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.
COMMONWEALTH OF AUSTRALIA Copyright Regulations 1969 WARNING This material has been reproduced and communicated to you by or on behalf of Monash University.
Prolog The language of logic. History Kowalski: late 60’s Logician who showed logical proof can support computation. Colmerauer: early 70’s Developed.
CS344 : Introduction to Artificial Intelligence Pushpak Bhattacharyya CSE Dept., IIT Bombay Lecture 16- Prolog.
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).
1Lecture 12 Introduction to Prolog SWI-Prolog. 2Lecture 12 Introduction to Prolog Welcome to SWI-Prolog (Version 4.0.9) Copyright (c) University.
About prolog  History  Symbolic Programming Language  Logic Programming Language  Declarative Programming Language.
MB: 2 March 2001CS360 Lecture 31 Programming in Logic: Prolog Prolog’s Declarative & Procedural Semantics Readings: Sections
CSE 3302 Programming Languages Chengkai Li Spring 2008 Logic Programming: Prolog (II) Lecture 22 – Prolog (II), Spring CSE3302 Programming Languages,
CS 330 Programming Languages 12 / 12 / 2006 Instructor: Michael Eckmann.
For Friday Read “lectures” 1-5 of Learn Prolog Now: prolog-now/
Constraint Logic Programming Ryan Kinworthy. Overview Introduction Logic Programming LP as a constraint programming language Constraint Logic Programming.
LING 364: Introduction to Formal Semantics Lecture 3 January 19th.
1 CILOG User Manual Bayesian Networks Seminar Sep 7th, 2006.
Prolog Ross (Tate). Filling in the Blanks Rather than reverse((a,b)) returns (b,a) Rather than reverse((a,b)) returns (b,a) What X makes reverse((a,b),
CSE (c) S. Tanimoto, 2005 Logic Programming 1 Logic Programming Outline: Motivation Examples: The Grandmother relation Formulation in Prolog Logic,
(9.1) COEN Logic Programming  Logic programming and predicate calculus  Prolog statements  Facts and rules  Matching  Subgoals and backtracking.
1 Software Development Topic 2 Software Development Languages and Environments.
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.
CS 321 Programming Languages and Compilers Prolog part 2.
COP4020 Programming Languages Logical programming with Prolog Prof. Xin Yuan.
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.
15/11/04 AIPP Lecture 14: Database Manipulation1 Database Manipulation Artificial Intelligence Programming in Prolog Lecturer: Tim Smith Lecture 14 15/11/04.
Control Algorithms 1 Chapter 6 Control Algorithms 1 Chapter 6 Pattern Search.
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.
1 CS2136: Paradigms of Computation Class 04: Prolog: Goals Backtracking Syntax Copyright 2001, 2002, 2003 Michael J. Ciaraldi and David Finkel.
1 COMP 205 Introduction to Prolog Dr. Chunbo Chu Week 14.
1 Prolog and Logic Languages Aaron Bloomfield CS 415 Fall 2005.
1Lecture 12 Introduction to Prolog Logic Programming Prolog.
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.
Ch. 13 Ch. 131 jcmt CSE 3302 Programming Languages CSE3302 Programming Languages (notes?) Dr. Carter Tiernan.
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:
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 21.
CS 337 Programming Languages Logic Programming I (Logic, Intro to Prolog)
Negation Chapter 5. Stating Negative Conditions n Sometimes you want to say that some condition does not hold n Prolog allows this –not/1this is a predicate.
CS 403: Programming Languages Lecture 18 Fall 2003 Department of Computer Science University of Alabama Joel Jones.
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.
Knowledge Based Information System
For Monday Exam 1 is Monday Takehome due Prolog Handout 3 due.
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.
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.
Prolog Fundamentals. 2 Review Last Lecture A Prolog program consists of a database of facts and rules, and queries (questions). –Fact:.... –Rule:... :-....
Copyright 1999Paul F. Reynolds, Jr. Foundations of Logic Programming.
1 Lecture 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line of Text 2.3Another Simple C Program: Adding.
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 12 :: Logic Languages
Programming Paradigms and Languages
Programming Techniques
Presentation transcript:

Lecture course: Heuristics & Prolog for Artificial Intelligence Applications Notes on Prolog from Professor Bruce Batchelor, DSc.

SWI-Prolog Welcome to SWI-Prolog (Version 3.4.2) Copyright (c) University of Amsterdam. Copy policy: GPL-2 (see For help, use ?- help(Topic). or ?- apropos(Word). ?- To start SWI-Prololg, type swipl. Prolog responds as shown below:

Built-in predicate consult/1 consult/1 reads a program from file starting.pl into the database. Simplified form: ['/Users/bruce/prolog/starting.pl']. ?- consult(' /Users/bruce/prolog/starting.pl '). % relations compiled 0.00 sec, 0 bytes Yes ?-

Operators Prolog has some pre-defined operators and the ability to define new ones. (We will look at the definition of operators later.) An operator is just a predicate for which a special abbreviated syntax is supported

The = operator The goal A = B succeeds if and only if A and B can be unified: ?- likes(mary,paul)= likes(mary,X). X = paul We can also use likes(mary,X), X = paul and X = paul, likes(mary,X)

Sharing Two uninstantiated variables can be shared using the = operator. The following compound goal succeeds. Both X and Y are inastantiated to paul. X = Y, likes(mary,X), likes(peter,Y)

Arithmetic Operators Predicates +, -, *, /,and ^ are arithmetic operators, with the usual precedence and associativity. To evaluate an arithmetic expression use is not =. ?- Q is (2+3)*4. Q = 20 Yes ?- Q = (2+3)*4. Q = (2+3)*4 Yes

Lists in Prolog PrologComments [] Empty list [a,b,c] Three elements [a,2,3,5,b,7] Six elements [a,[2,3,5],b,[7]] 4 elements. 2 sub-lists [[]] 1 element (i.e. the empty list) not([a,b,c] = [c,b,a]) List order is important length(X,N) List X has N elements [A|B] = [a,2,3,5,b,7]A = a. B = [2,3,5,b,7] [a,[2,3,5],b,[7]] 4 elements. 2 sub-lists

Alternative List Notation Usual List NotationAlternative Notation [] [1].(1,[]) [1,2,3].(1,.(2,.(3,[]))) [1,likes(X,Y)].(1,.(likes(X,Y),[])) This topic is not examinable; it is included here merely to indicate how Prolog actually stores lists.

Heads, Tails & Pattern Matching PrologComments [a|X] Head is a. Tail is X ( X is a list) [A,B|X]= [a,b,c,d,e]A=a. B=b. X=[c,d,e] [_|b,c,d,e] Any list whose tail is [b,c,d,e] [a|_] [a|X] Any list whose head is a [_] [X] Any list with 1 element [_,a,_] [X,a,Y] Any 3-elt. list whose 2nd elt. is a List with 2 1-elt. sub-lists at front [[X],[Y]|Z] Pattern matching is a powerful feature of Prolog and will be useful when we study Definite Clause Grammars for Natural Language Understanding.

Pretty Printer, pp/3 % Terminating clause pretty_print([]) :- nl. % Recursive clause pretty_print([A|B]) :- tab(3), writenl(A), !, pretty_print(B).

Built-in Predicate member/2 member(A,[A|_]). member(A,[_|B]) :- member(A,B). member/2 is built-in and need not be defined by the user. Much of Prolog is defined in terms of Prolog. (E.g. not/1, repeat/0, append/3, etc.) If you can write member/2 from scratch, then you can program in Prolog!

Built-in Predicate append/3 append(X,Y,Z) succeeds if and only if Z is the result of appending list Y onto the end of list X The goal append([a,b,c],[d,e],Z) succeeds with Z instantiated to [a,b,c,d,e] Y = [a,b,c,d,e], append(X,[d,e],Y) succeeds with Y = [a, b, c, d, e] and X = [a, b, c] append/3 can be used with any pattern of instantiation (i.e. with variables in any positions)

Defining append/3 append/3 is pre-defined but it can be defined in terms of "core" Prolog: % Terminating clause append([],A,A). %Recursive clause append([H|Ta],B,[H|Tc]) :- append(Ta, B, Tc). This definition is non-examinable and is included here merely to demonstrate the power of Prolog

More List Predicates PredicateDescription member(X,Y) Succeeds if list Y contains the element X. cull(X,Y,Z) Instantiate Z to Y with all occurences of X deleted. reverse(A,B) Instantiate B to list A with its elements written in reverse order subset(A,B) Succeeds if all elements of A are also contained in Y.

Delete Defined Elements cull/3 cull(_,[],[]).%Terminating clause cull(A,[A|B], C) :-% A is at head of the list !,% Cut - ignore this for the moment cull(A,B,C).% Now get to work on the tail cull(A,[B|C], [B|D]) :-% A is not at head of the list !,% Cut - ignore this for the moment cull(A,C,D).% Again, work on the tail

List Reversal: reverse/2 This is not an efficient way to reverse a list! % Terminating clause reverse([],[]). %Recursive clause reverse([H|T],X) :- reverse(T,Y), append(Y,[H],X).

Cut (!) Cut (!) is always satisfied when progressing forwards through a program. Cut does not allow back-tracking. All instantiations are "frozen" when we pass cut. Cut does not add anything to a program; no extra solutions are available. Cut can be useful to improve execution speed, by eliminating unnecesary solutions. Sometimes, it is possible to run a program using cut, when it would be impossible without it.

Uses of Cut (!) Cut improves program speed. Cut reduces memory requirments. Prolog does not continue searching for further solutions when one has been found. Cut often makes it possible to improve the range of a program e.g. fibonacci/2 Failure by negation.

Negation As Failure % What to do when X succeeds not(X) :- call(X), !, fail. % X did not succeed, so not(X)succeeds not(_). To prove not(X), Prolog attempts to prove X not(X) succeeds if X fails

Simple Queries A query asks Prolog to prove something The answer is Yes or No ( No = not proven) Some queries (e.g. consult/1, write/1, nl/0 ) are executed only for their side-effects. (E.g. write/1 always succeeds.) ?- likes(mary,paul). Yes ?- likes(mary,sue). No ?-

Final Period Queries can occupy several lines If there is no final period, Prolog prompts for more input by printing " |". ?- likes(paul,mary) |. Yes ?- User typed "."

Queries With Variables Term with variables can appear as a query. Prolog shows the bindings necessary to prove the query. (In this case, P = john.) ?- likes(mary,Q). P = john Yes ?- likes(paul,nelly). No Prolog waits here for input. Press RETURN to proceed.

Flexibility Variables can appear in any position: – ?- likes(mary,john). – ?- likes(mary,W). – ?- likes(X,john). – ?- likes(Y,Z). If we run the last query, hitting semi-colon (;) instead of RETURN, we obtain multiple solutions.

Synonyms Equivalent ways of saying the same thing – Goal satisfaction – Theorem proving – Satisfying a query Note: We do not refer to executing, or running, a Prolog program, as there are no instructions.

Stopping Goal Satisfaction Click CTRL/D to terminate goal satisfaction. Usefule when a program is seemingly stuck in an infinite loop. Warning: Clicking CTRL/D again, in response to the prompt (?-), terminates SWI-Prolog and returns control to the UNIX shell.

Compound Goals / Conjunctions A compound goal has a list of query terms (sub-goals), separated by commas Prolog tries prove them all, using a single set of bindings. ?- likes(mary,paul),likes(paul,mary). Yes

Conjunctions - 2 A conjunctive query has a list of query terms separated by commas Prolog tries prove them all, using a single set of bindings. X is instantiated during satisfactionof this goal. ?- likes(mary,X),likes(X,mary). X = paul ; No

Multiple Solutions Here, there is more than one solution (i.e.way to prove the query) Typing ; rather than RETURN, asks Prolog to find more solutions. X is repeatedly instantiated / uninstantiated here. ?- likes(mary,X). X = john ; X = paul ; X = bonzo ; X = kissing ; No ?-

Back-tracking Consider the compound goal go :-likes(mary,X),write(X),nl,likes(X,mary). X is initially instantiated to john. (First sub-goal) Prolog prints X ( john ) then tries to prove that likes(john,mary) is true. However, there is no such clause. So, Prolog then back- tracks to the first sub-goal and then tries to find another solution. One is found requiring that X be instantiated to paul. This value is then printed. Finally, likes(paul,mary) is proved true, so go/0 succeeds.

Rules friends(A, B) :- likes(A, B), likes(B, A). Head Read as "can be proved to be true if" or more simply "if". Conjunction. AND sub-goals. Two sub-goals form the body of the rule

Rules To prove the head of a rule, satisfy each of its sub-goals To prove friends(A,B), find some A and B for which you can prove likes(A,B), then likes(B,A)

Programs With Rules female/1 consists of 6 clauses (i.e. a fact or a rule), ending with a period. male(albert). male(henry). male(john). male(paul). male(peter). % Facts female(mary). female(florence). female(mary). female(sue). % Rules female(A) :- likes(A,_), not(male(A)). female(A) :- likes(_,A), not(male(A)). male(albert). male(henry). male(john). male(paul). male(peter). % Facts female(mary). female(florence). female(mary). female(sue). % Rules female(A) :- likes(A,_), not(male(A)). female(A) :- likes(_,A), not(male(A)).

Rules Using Other Rules opposite_sex(A,B) :- male(A),female(B). opposite_sex(A,B) :- female(A),male(B). female/1 is defined in terms of male/1 and likes/2. ( not/1 is a built-in predicate.) Another example Notice the hierarchical structure of the program.

Rules: Scope of Variables The scope of a variable is local to the clause that contains it opposite_sex(A,B) :- male(A),female(B). opposite_sex(C,D) :- female(C),male(D). A and B have no relevance to second clause Following definition has the same effect as in the previous example, despite the change of variable names.

Recursive Rules X is an ancestor of Y if: (i) X is a parent of Y (Terminating clause) OR (ii) There is some Z such that Z is a parent of Y AND X is an ancestor of Z % Terminating clause ancestor(X,Y) :- parent(X,Y). % Recursive clause ancestor(X,Y) :- parent(Z,Y), ancestor(X,Z).

Recursive Rules Some predicates may have several clauses, which may be facts or rules. Prolog tries to prove the query using clauses in the order they appear in the program, so it is usual to put terminating rules / facts first.