Artificial Intelligence programming and Prolog Language Tutorial.

122
Artificial Intelligence programming and Prolog Language Tutorial

Transcript of Artificial Intelligence programming and Prolog Language Tutorial.

Page 1: Artificial Intelligence programming and Prolog Language Tutorial.

Artificial Intelligence programming and

Prolog Language Tutorial

Page 2: Artificial Intelligence programming and Prolog Language Tutorial.

Contents

• What is logic programming?• Small Examples• The Basics• Another Example: Towers of Hanoi• Other Examples

Page 3: Artificial Intelligence programming and Prolog Language Tutorial.

What is Logic Programming?

Page 4: Artificial Intelligence programming and Prolog Language Tutorial.

What is Logic Programming?

• “The use of mathematical logic for computer programming.” (Wikipedia)

• “A declarative, relational style of programming based on first-order logic.” (Dictionary.com)

Page 5: Artificial Intelligence programming and Prolog Language Tutorial.

History of Prolog

• Developed in 1972 by Alain Colmerauer and Philippe Roussel

• Name comes from “PROgramming in LOGic”

• A solution to the debate about which kinds of logic to use

Page 6: Artificial Intelligence programming and Prolog Language Tutorial.

History of Logic Programming

• Came about in 1960s and 1970s due to debates about using declarative or procedural representations in AI

• Stanford and Edinburgh – declarative • MIT - procedural

– Developed Planner in 1969 (first language in the proceduralistic paradigm).

Page 7: Artificial Intelligence programming and Prolog Language Tutorial.

Systems for Logic Programming

• ALF • CLP• ECLiPSe• Elf• Fish• Flang• Gödel • KLIC• LIFE• MONA• Oz System • RELFUN• SAMPLE• XSB

Just to name a few….

Page 8: Artificial Intelligence programming and Prolog Language Tutorial.

Systems for Logic Programming, cont.

• Prolog is the most common system

• Prolog has many variations:– &-Prolog, And-parallel Prolog. – ACE, And-Or-parallel Prolog. – Actor Prolog – Andorra-I, an Or- and (deterministic) and-parallel Prolog. – Aurora, Or-parallel Prolog. – cu-Prolog, a constraint logic programming language – lambda Prolog– LeanTaP, a small theorem prover written in SICStus Prolog. – Logtalk, an extension for Object-Oriented Programming in Prolog. – Mixtus, an automatic partial evaluator for full Prolog. – Muse, Or-parallel Prolog.

Page 9: Artificial Intelligence programming and Prolog Language Tutorial.

Prolog Structure

Page 10: Artificial Intelligence programming and Prolog Language Tutorial.

Prolog Structure

• Prolog facts – a database of predicates and associations.

• Prolog rules – define new predicates by using Prolog facts.

• Note: – Prolog considers capital letters to denote variables,

not predicates.

Page 11: Artificial Intelligence programming and Prolog Language Tutorial.

Prolog Structure – Queries

• A query searches the database for the first fact that satisfies its goal.

• If a fact is found then it either unifies the variable with a constant or Prolog returns yes.

• If a fact is not found that meets that condition then Prolog returns no.

Page 12: Artificial Intelligence programming and Prolog Language Tutorial.

Prolog Structure – disjunction and conjunction.

• Use a semi-colon to request subsequent answers.

• In other words, a semi-colon signifies

disjunction.

• A comma signifies conjunction.

Page 13: Artificial Intelligence programming and Prolog Language Tutorial.

How Prolog Works: Example 1

Example of data base:instructor (perkowski, ee271)

instructor (perkowski, ee171)instructor (perkowski, ee478)enrolled (alan-cheng, ee475) enrolled (matthew, ee171)

enrolled (alan-cheng,ee171) enrolled (alan-cheng,ee271)

enrolled (chris-clark,ee271) enrolled (edison-tsai, ee171) enrolled (chris-clark, ee171)

This is the database of Prolog facts.

Page 14: Artificial Intelligence programming and Prolog Language Tutorial.

How Prolog Works, cont.

Prolog rules:teaches (P,S) :- instructor (P,C), enrolled (S,C)

This is to say that an instructor only teaches if he teaches a class and students are enrolled in that class.

teaches (Professor, Student) :- instructor (Professor, Class), enrolled (Student,Class)

teaches (Professor, Student) instructor (Professor, Class), enrolled (Student,Class)

instructor (Professor, Class), enrolled (Student,Class) teaches (Professor, Student)

Page 15: Artificial Intelligence programming and Prolog Language Tutorial.

How Prolog Works, cont.• Prolog answers queries based off of the database that has been

given.?enrolled (chris-clark, ee271) yes

?enrolled (X, ee271)alan-chengchris-clark

?teaches (X, alan-cheng)perkowski

?teaches (X, chris-clark)PerkowskiJeskegreenwood

Page 16: Artificial Intelligence programming and Prolog Language Tutorial.

Expanding Database in Prolog

• Imagine what happens if we expand the database:

• instructor (perkowski, ee271)• instructor (perkowski, ee171)• instructor (perkowski, ee478)• enrolled (jeske, ee171)• enrolled (greenwood, ee171) • enrolled (alan-chen,ee171)• enrolled (alan-chen,ee271) • enrolled (chris-clark,ee271)• enrolled (edison-tsai, ee171) • enrolled (chris-clark, ee171)• instructor (bebis, cs365)• instructor (looney, cs311)• instructor (yuksel, cs446)• instructor (helfand, cs493)• instructor (quint, math486)• enrolled (ben, cs365)• enrolled (bill, cs365)• enrolled (bill, cs446)• enrolled (brian, cs311)• enrolled (brian, cs365)• enrolled (brittney, cs311)• enrolled (brittney, cs365)• enrolled (brittney, cs446)• enrolled (cody, cs311)• enrolled (cody, cs365)• enrolled (danielle, cs365)• enrolled (danielle, cs446)• enrolled (danielle, cs493)• enrolled (david, cs365)• enrolled (javier, cs365)• enrolled (jeffrey, cs365)• enrolled (jessica, cs311)• enrolled (jessica, cs446)• enrolled (jessica, math486)

• enrolled (joel, cs365)• enrolled (joseph, cs311)• enrolled (joseph, cs365)• enrolled (joseph, cs446)• enrolled (joseph, cs493)• enrolled (joseph, math486)• enrolled (kellen, cs365)• enrolled (matts, cs311)• enrolled (matts, cs365)• enrolled (mattw, cs311)• enrolled (mattw, cs365)• enrolled (mattw, cs446)• enrolled (miran, cs365)• enrolled (ryan, cs365)• enrolled (samuel, cs365)• enrolled (shane, cs311)• enrolled (shane, cs365)• enrolled (shane, cs446)• enrolled (tiffany, cs311)• enrolled (tiffany, cs365)• enrolled (tiffany, cs446)

Page 17: Artificial Intelligence programming and Prolog Language Tutorial.

How Prolog Works, asking questions to data base

?enrolled (X, cs365)benbillbrianbrittneycodydanielledavidjavierjeffrey

joeljosephkellenmattsmattwmiranryansamuelshanetiffany

• This list now gives us the entire roster of students in CS 365.

Page 18: Artificial Intelligence programming and Prolog Language Tutorial.

How Prolog Works, more complicated querries

Queries can be more complicated to compare more data:

classmates (S1, S2) :- enrolled (S1, C), enrolled (S2, C)

?classmates (joseph, danielle)yes

?classmates (joseph, jessica)yes

?classmates (jessica, danielle)no

class

c

s2s1

enrolled enrolled

c

s2s1

enrolled enrolled

classmates

Page 19: Artificial Intelligence programming and Prolog Language Tutorial.

How Prolog Works, more complicated querries

classmates (S1, S2, C) :- enrolled (S1, C), enrolled (S2, C)

?classmates (joseph, danielle, C)cs365cs446cs493no

?classmates (joseph, jessica, C)math

?classmates (jessica, danielle, C)no

Page 20: Artificial Intelligence programming and Prolog Language Tutorial.

Family Data base

Page 21: Artificial Intelligence programming and Prolog Language Tutorial.

parent(X,Y) :- father(X,Y).parent(X,Y) :- mother(X,Y).grandparent(X,Z) :- parent(X,Y), parent(Y,Z).ancestor(X,Z) :- parent(X,Z).ancestor(X,Y) :- parent(X,Y), ancestor(Y,Z).sibling(X,Y) :- mother(M,X), mother(M,Y), father(F,X), father(F,Y), X \= Y.cousin(X,Y) :- parent(U,X), parent(V,Y), sibling(U,V).

father(albert, jeffrey).mother(alice, jeffrey).father(albert, george).mother(alice, george).father(john, mary).mother(sue, mary).father(george, cindy).mother(mary, cindy).father(george, victor).mother(mary, victor).

Page 22: Artificial Intelligence programming and Prolog Language Tutorial.

?- [kinship].% kinship compiled 0.00 sec, 3,016 bytesYes

?- ancestor(X, cindy), sibling(X, jeffrey).X = george Yes

?- grandparent(albert, victor).Yes

?- cousin(alice, john).No

?- sibling(A,B).A = jeffrey, B = george ; A = george, B = jeffrey ; A = cindy, B = victor ; A = victor, B = cindy ; No

SWI Prolog

Page 23: Artificial Intelligence programming and Prolog Language Tutorial.

Unification

Page 24: Artificial Intelligence programming and Prolog Language Tutorial.

Prolog Structure - Unification

• A query resolves by unifying all of its elements.

• A constant unifies with itself and any variable.

• Scope is limited to the rule in which a variable occurs.

• When a variable is unified with a constant in a rule, all instances of that variable in that rule are unified to that constant.

Page 25: Artificial Intelligence programming and Prolog Language Tutorial.

1. Process of making one predicate same as another.

2. A query resolves by unifying all of its elements.

3. A constant unifies with itself and any variable.

4. Scope is limited to the rule in which a variable occurs.

5. When a variable is unified with a constant in a rule, all instances of that variable in that rule are unified to that constant.

PROLOG STRUCTURE - UNIFICATION

Page 26: Artificial Intelligence programming and Prolog Language Tutorial.

Example of Unification

Page 27: Artificial Intelligence programming and Prolog Language Tutorial.

Prolog Structure - Backtracking

1. In more complex examples, Prolog uses backtracking to find possible solutions.

2. Prolog will attempt to resolve the first fact of its rule, unifying any variables with the first constant that satisfies that fact

3. It then attempts to resolve the rest of that rules facts.

4. If it is unable to do so under those conditions it “backs up” and tries again with the next available unifying constant.

Page 28: Artificial Intelligence programming and Prolog Language Tutorial.

Clauses

• Programs are constructed from A number of clauses: <head> :- <body>

• Clauses have three forms:– hypotheses (facts)– conditions (rules)– goals

• Both <head> and <body> are composed of relationships (also called predications or literals)

assertions (database)

questions

Page 29: Artificial Intelligence programming and Prolog Language Tutorial.

Relationships

• Represent properties of and relations among the individuals

• A relationship is application of a predicate to one or more terms

• Terms:– atoms (or constants): john, 25, …– variables (begin with uppercase letters): X, …– compounds

• Horn clause form: At most one relationship in <head>

Page 30: Artificial Intelligence programming and Prolog Language Tutorial.

World of Toys

Page 31: Artificial Intelligence programming and Prolog Language Tutorial.

World of Toys - Example 2• Let us consider the following description of a “system”;

– Ann likes every toy she plays with. – A doll is a toy. – A train is a toy. – Ann plays with trains. – John likes everything Ann likes.

• To express this in Prolog we must: 1. Identify the entities, or actual things, mentioned in the description 2. Identify the types of properties that things can have, as well as the

relations that can hold between these things 3. Figure out which properties/relations hold for which entities

• There is really no unique way of doing this; – we must decide the best way to structure our data

• (based on what we want to do with it).

Page 32: Artificial Intelligence programming and Prolog Language Tutorial.

A Small Example

• We will choose the following: – Things:

• Ann, Sue, doll, train – Properties:

• “... is a toy”– Relations:

• “... likes ...”, “... plays with ...”

• Constructing our knowledge base then consists of writing down:– which properties hold for which things– which relationships hold for which things

World of Toys - Example 2

Page 33: Artificial Intelligence programming and Prolog Language Tutorial.

A Small Example

• We write:

likes(ann,X) :- toy(X), plays(ann,X).toy(doll). toy(train).plays(ann,train).likes(john,Y) :- likes(ann,Y).

World of Toys - Example 2

Page 34: Artificial Intelligence programming and Prolog Language Tutorial.

A Small Example – What It Means

• There are three important logical symbols:- if

, and

; or

• X and Y are variables – ann, john, doll and train are constants – likes, toy and plays are predicate symbols

World of Toys - Example 2

Page 35: Artificial Intelligence programming and Prolog Language Tutorial.

A Small Example – What It Means

• A variable represents some unspecified element of the system

• A constant represents a particular, known, member of the system

• A predicate represents some relation or property in the system.

• Note that: – Variables always start with an upper-case letter or an

underscore – Predicates and constants always start with a lower-case

letter or digit

World of Toys - Example 2

Page 36: Artificial Intelligence programming and Prolog Language Tutorial.

A Small Example – What It Means

• Each line in a Prolog program is called a clause– There are two types of clauses - facts and rules

• Rules are clauses which contain the “:-” symbol• Facts are clauses which don't

– Each fact consists of just one predicate– Each rule consists of a predicate, followed by a “:-” symbol, followed

by a list of predicates separated by “,” or “;”

• Every clause is terminated by a “.” (full-stop). • In a rule, the predicate before the “:-” is called the head of

the rule• The predicates coming after the ``:-'' are called the body

World of Toys - Example 2

Page 37: Artificial Intelligence programming and Prolog Language Tutorial.

A Small Example – What It Means

• For example:

likes(ann,X) :- toy(X), plays(ann,X).

<---Head---> <-------Body------->

World of Toys - Example 2

Page 38: Artificial Intelligence programming and Prolog Language Tutorial.

A Small Example – What It Means• We “define a predicate” by writing down a number of clauses which have

that predicate at their head

• The order in which we write these down is important

• Any predicates mentioned in the body must either:– be defined somewhere else in the program, or – be one of Prolog's “built-in” predicates.

• Defining a predicate in Prolog corresponds roughly to defining a procedure

• Predicates occurring in the body of a clause correspond roughly to procedure calls

• Note also that: – Constants and variables will never appear “on their own” in a clause. They can

only appear as the “arguments” to some predicate. – Predicates will (almost) never appear as arguments to another predicate

World of Toys - Example 2

Page 39: Artificial Intelligence programming and Prolog Language Tutorial.

What It Says:

• So, after all that, what does our little program say?

• Having all the relations expressed as a predicate followed by arguments is not particularly intuitive, so with some suitable swapping-around we get:

• For any X, (ann likes X) if (X is-a-toy) and (ann plays-with X). • (doll is-a-toy). • (train is-a-toy). • (ann plays-with train). • For any Y, (john likes Y) if (ann likes Y).

World of Toys - Example 2

Page 40: Artificial Intelligence programming and Prolog Language Tutorial.

Running It

• So how do we run it? – We run it by giving Prolog a query to prove

• A query has exactly the same format as a clause-body: one or more predicates, separated by “,” or “;”, terminated by a full-stop

• Thus, we might enter in the following as a query: – likes(john,Z).

• Logically, this can be interpreted as – “is there a Z such that john likes Z?”

• From a relational point of view, we can read it as: – “List all those Z's that john likes”

World of Toys - Example 2

Page 41: Artificial Intelligence programming and Prolog Language Tutorial.

• In general terms we call the query our “goal”, and say that Prolog is being asked to (find ways to) “satisfy” the goal

• This process is also known as inferencing:– Prolog has to infer the solution to the query from the

knowledge base• Note that solving a query results in either:

– failure, in which case “no” is printed out, or – success, in which case all sets of values for the variables in

the goal (which cause it to be satisfied) are printed out

World of Toys - Example 2

Page 42: Artificial Intelligence programming and Prolog Language Tutorial.

How It Works• So how does Prolog get an answer? • We have to solve likes(john, Y), so we must examine all the

clauses which start with the predicate likes. – The first one is of no use at this point, since it only tells us what ann

likes. – The second rule for likes tells us that in order to find something that

john likes, we need only to find something which ann likes. So now we have a new goal to solve - likes(ann,Z).

– To solve this we again examine all the rules for likes. This time the first rule matches (and the second doesn't), and so we are told that in order to find something which ann likes, we must find something which is a toy, and which ann plays with.

World of Toys - Example 2

Page 43: Artificial Intelligence programming and Prolog Language Tutorial.

• So first of all we try to find a toy. – To do this we examine the clauses with toy at their head. – There are two possibilities here: a toy is either a doll or train.

• We now take these two toys, and test to see which one ann plays with; – that is, we generate two new sub-goals to solve: plays(ann,doll) and plays(ann,train).

• In general, to solve these, we must look at the clauses for plays. – There is only one: since it is for train, we conclude with the

answer: Z = train.

World of Toys - Example 2

Page 44: Artificial Intelligence programming and Prolog Language Tutorial.

Exercises

• Example: toys.pl

1. Does Ann like dolls? 2. Who likes trains? 3. What does John like? 4. Who plays with trains?

World of Toys - Example 2

Page 45: Artificial Intelligence programming and Prolog Language Tutorial.

A Small Example - Exercises• Translate the following sentences into Prolog:

– John eats all kinds of food. Apples are food. Oysters are food. Anything anyone eats is food. Tom eats snakes. Sue eats everything that Tom eats. Save the program in a file called food.pl. Now read them into Prolog, and formulate queries to find out:

1. What John eats 2. What Sue eats 3. If there is anything which both John and Sue eat. 4. Who eats snakes

Page 46: Artificial Intelligence programming and Prolog Language Tutorial.

Lists

Page 47: Artificial Intelligence programming and Prolog Language Tutorial.

Prolog Lists: concept• Lists are a collection of terms inside [ and ]

• [ chevy, ford, dodge]

• loc_list([apple, broccoli, crackers], kitchen).• loc_list([desk, computer], office).• loc_list([flashlight, envelope], desk).• loc_list([stamp, key], envelope). loc_list(['washing

machine'], cellar). • loc_list([nani], 'washing machine'). • loc_list([], hall)

Page 48: Artificial Intelligence programming and Prolog Language Tutorial.

Prolog Lists: unification

• Unification works on lists just as it works on other data structures.

• loc_list(X, kitchen). X = [apple, broccoli, crackers] ?- [_,X,_] = [apples, broccoli, crackers]. X = broccoli

• The patterns won't unify unless both lists have the same number of elements.

Page 49: Artificial Intelligence programming and Prolog Language Tutorial.

Prolog Lists: member and append

• List functions– [H|T]

• separate list into head and tail

– member• test if X is a member of a list

– append• append two lists to form a third list

Page 50: Artificial Intelligence programming and Prolog Language Tutorial.

Prolog Lists: head and tail

• Head and Tail of a List• Syntax

[H|T]• Examples• ?- [a|[b,c,d]] = [a,b,c,d]. %We check identity

yes• ?- [a|b,c,d] = [a,b,c,d].

no

Page 51: Artificial Intelligence programming and Prolog Language Tutorial.

Prolog Lists: more on head and tail

• More Examples?- [H|T] = [apple, broccoli, refrigerator].

H = apple T = [broccoli, refrigerator]

?- [H|T] = [a, b, c, d, e]. H = a T = [b, c, d, e]

?- [H|T] = [apples, bananas]. H = apples T = [bananas]

Page 52: Artificial Intelligence programming and Prolog Language Tutorial.

Prolog Lists: more on head, tail and list structure

• More Examples?- [One, Two | T] = [apple, sprouts, fridge, milk].

One = apple Two = sprouts T = [fridge, milk]

?- [a|[b|[c|[d|[]]]]] = [a,b,c,d]. yes

Page 53: Artificial Intelligence programming and Prolog Language Tutorial.

Prolog Lists: member

• Testing if an element is in a list.• Syntax

– member(X, L).• Example

– member(apple, [apple, broccoli, crackers]). – member(X, CarList).

• Full Predicate defined as:member(H,[H|T]). member(X,[H|T]) :- member(X,T).

Page 54: Artificial Intelligence programming and Prolog Language Tutorial.

Prolog Lists: append

• Appending two lists to form a third.• Syntax

– append(L1, L2, L3).• Example

– append( [a,b,c], [d,e,f], X).– X = [a,b,c,d,e,f]

• Full predicate defined as:append([],X,X). append([H|T1],X,[H|T2]) :- append(T1,X,T2).

Page 55: Artificial Intelligence programming and Prolog Language Tutorial.

Prolog is much More Than Just Information: lists and trees

• Prolog rules can also be used write programs that do more than find the answers to simple database queries.– append([], L, L).– append([H|T], L, [H|L1]) :- append(T, L, L1).

This will append a list to another list recursively.

• A binary tree can be defined as follows– tree(nil).– tree(node(_ , Left, Right) :- tree(left), tree(right).

Page 56: Artificial Intelligence programming and Prolog Language Tutorial.

Data Structures in Prolog

Page 57: Artificial Intelligence programming and Prolog Language Tutorial.

Comments in Prolog

• Single line comments use the “%” character

• Multi-line comments use /* and */

Page 58: Artificial Intelligence programming and Prolog Language Tutorial.

PROLOG DATA STRUCTURES• Prolog's single data type is the term.

• Terms are either – atoms, – numbers,– variables – or compound terms.

• atoms are: x, blue, 'Some atom', and [].• Numbers can be

– floats or – integers

• Variables are denoted by a string consisting of letters, numbers and underscore characters, and beginning with an upper-case letter or underscore.

• A compound term has a functor and a number of arguments, which are again terms.

Page 59: Artificial Intelligence programming and Prolog Language Tutorial.

Simple Input-Output in Prolog

• Simple I/O in Prolog– Use the write statement

• write(‘hello’)• write(‘Hello’), write(‘World’)

– Use a Newline • write(‘hello’), nl, write(‘World’)

Page 60: Artificial Intelligence programming and Prolog Language Tutorial.

Reading and writing in Prolog

• Reading a value from stdin

• Prolog Syntax:– read(X)

• Exampleread(X), write(X).

Page 61: Artificial Intelligence programming and Prolog Language Tutorial.

Arithmetic in Prolog

• Using Arithmetic– Different to what you may have seen with other

languages.– Operators

• < <= == != => >• + - * /

– Arithmetic is done via evaluation then unification

Page 62: Artificial Intelligence programming and Prolog Language Tutorial.

Arithmetic

• Arithmetic Example

• X is Y– compute Y then unify X and Y

• X is Y * 2• N is N - 1

Page 63: Artificial Intelligence programming and Prolog Language Tutorial.

The difference between identity relation and unification

• X == Y– This is the identity relation. In order for this to be true, X

and Y must both be identical variables (i.e. have the same name), or both be identical constants, or both be identical operations applied to identical terms

• X = Y– This is unification– It is true if X is unifiable with Y

Page 64: Artificial Intelligence programming and Prolog Language Tutorial.

Different “assignment” operators in Prolog

• X=:=Y– This means “compute X, compute Y, and see if they both

have the same value”– both X and Y must be arithmetic expressions

• X is Y– This means compute Y and then unify X and Y– Y must be an arithmetic expression– X can either be an arithmetic expression (of the same

form), or a variable

Page 65: Artificial Intelligence programming and Prolog Language Tutorial.

Comparison of assignments

• Arithmetic Exercises1. X = 2, Y is X+1 2. X = 2, Y = X+1 3. X = 2, Y == X+1 4. X = 2, Y =:= X+1 5. X = 2, 3 =:= X+1

– Check these on the Prolog system.

Page 66: Artificial Intelligence programming and Prolog Language Tutorial.

Recursion in Prolog

Page 67: Artificial Intelligence programming and Prolog Language Tutorial.

Example: GCD• Arithmetic Examples

gcd(X,X,X). gcd(X,Y,Z) :- X<Y, Y1 is Y-X, gcd(X,Y1,Z).gcd(X,Y,Z) :- X>Y, X1 is X-Y, gcd(X1,Y,Z).

Page 68: Artificial Intelligence programming and Prolog Language Tutorial.

Example: Factorial

fact(0,1). fact(X,F) :- X>0, X1 is X-1, fact(X1,F1), F is X*F1.

Page 69: Artificial Intelligence programming and Prolog Language Tutorial.

Example: Towers of Hanoi• The Problem

– A group of over-proud monks in a Hanoi monastery were assigned a task to perform: they had to move 100 discs from one peg to another with the help of a third peg.

– There are only two rules: • Only one disc can be moved at a time • The discs are all of different sizes, and no disc can be placed on top

of a smaller one

• We want to write a Prolog program to solve this.

Page 70: Artificial Intelligence programming and Prolog Language Tutorial.

Towers of Hanoi

• The Rules!!!!– In order to work out a recursive solution we must

find something to "do" the recursion on, that is, something with:

• a base case • an inductive case that can be expressed in terms of

something smaller

– We will choose to proceed by induction on the number of discs that we want to transfer

Page 71: Artificial Intelligence programming and Prolog Language Tutorial.

Towers of Hanoi• Moving a disc

– The basic activity will be moving a single disc from one peg to another. – Suppose we want to define a predicate for this called move; thus:

• move(A,B) means move the topmost disc from peg A to peg B.

• So how should we define move?

• If we were doing the problem in reality then we would want to formulate some instructions to a robot arm (attached to the computer) to move the pegs.

Page 72: Artificial Intelligence programming and Prolog Language Tutorial.

Towers of Hanoi

• Moving a disk (cont.)– For our purposes, we will assume that what we

want is a list of instructions for the monks; thus we define:

• move(A,B) :- nl, write('Move topmost disc from '), write(A), write(' to '), write(B).

– Every time we call move, the appropriate instruction will be printed out on screen.

Page 73: Artificial Intelligence programming and Prolog Language Tutorial.

Towers of Hanoi

• Base Case– An initial attempt might select 1 as the base case. To

transfer one disc from A to B, simply move it: • transfer(1,A,B,I) :- move(A,B).

– In fact there is an even simpler base case - when N=0! If we have no discs to transfer, then the solution is to simply do nothing. That is, transfer(0,A,B,I) is satisfied by default.

– We write this as a fact: • transfer(0,A,B,I).

Page 74: Artificial Intelligence programming and Prolog Language Tutorial.

Towers of Hanoi

• Inductive Case– To do the inductive case, suppose we are trying to

transfer N discs from A to B. By induction, we may assume that we have a program that transfers N-1 discs.

– The way we proceed is: • Transfer the top N-1 discs from A to I • Transfer the last disc from A to B • Transfer the N-1 discs from I to B

• Example: Towers of Hanoi

Page 75: Artificial Intelligence programming and Prolog Language Tutorial.

Other Examples

• Example: Making Change• Example: Who owns what car• Example: Things in my kitchen

Page 76: Artificial Intelligence programming and Prolog Language Tutorial.

Control Structures in Prolog

• Looping…Repeat until user enters “end”

command_loop:- repeat, write('Enter command (end to exit): '), read(X), write(X), nl, % new lineX = end.

Page 77: Artificial Intelligence programming and Prolog Language Tutorial.

More on Data

Structures

Page 78: Artificial Intelligence programming and Prolog Language Tutorial.

Primitives and Constructors

• Few primitives and No constructors.

• Data types and data structures are defined implicitly by their properties.

Page 79: Artificial Intelligence programming and Prolog Language Tutorial.

Example (datatype)• Natural number arithmetic

sum(succ(X), Y, succ(Z)) :- sum(X,Y,Z).sum(0,X,X).dif(X,Y,Z) :- sum(Z,Y,X).

:-sum(succ(succ(0)),succ(succ(succ(0))),A).A = succ(succ(succ(succ(succ(0)))))

• Very inefficient! (Why such a decision?)• Use of ‘is’ operator (unidirectional)

Page 80: Artificial Intelligence programming and Prolog Language Tutorial.

Principles of logic programming languages

• Simplicity– Small number of built-in data types and operations

• Regularity– Uniform treatment of all data types as predicates

and terms

Page 81: Artificial Intelligence programming and Prolog Language Tutorial.

Data Structures from other languages

• Compound terms can represent data structures

• Example: Lists in LISP

(car (cons X L)) = X(cdr (cons X L)) = L(cons (car L) (cdr L)) = L, for nonnull L

Page 82: Artificial Intelligence programming and Prolog Language Tutorial.

LISP in Prolog

• Using compound terms:car( cons(X,L), X).cdr( cons(X,L), L).list(nil).list(cons(X,L)) :- list(L).null(nil).

• What about null(L)?

• How to accomplish (car (cons ‘(a b) ‘(c d)))?

Page 83: Artificial Intelligence programming and Prolog Language Tutorial.

Some Syntactic Sugar

• Using ‘.’ infix functor (in some systems) instead of cons:– Clauses?

• Most Prolog systems allow the abbreviation:– [X1, X2, …, Xn] = X1. X2. … .Xn.nil– [ ] = nil– ‘.’ is right associative!

Page 84: Artificial Intelligence programming and Prolog Language Tutorial.

Component Selection in Prolog• Implicitly done by pattern matching (unification).

append( [ ], L, L).append( X.P, L, X.Q) :- append(P,L,Q).

• Compare with LISP append:(defun append (M L) (if (null M) L (cons (car M) (append (cdr M) L)) ))

• Taking apart in terms of putting together!– What X and P are cons’d to create M?– What number do I add to 3 to get 5 (instead of 5-3)

• Efficient!?

Page 85: Artificial Intelligence programming and Prolog Language Tutorial.

Complex Structures in Prolog• A tree using lists (in LISP):

– (times (plus x y) (plus y 1))

• Using compound terms directly (as records):– times(plus(x, y), plus(y, 1))

• Using predicates directly:– sum(x, y, t1).– sum(y, 1, t2).– prod(t1, t2, t3).

• Which is better?

Page 86: Artificial Intelligence programming and Prolog Language Tutorial.

Why Not Predicates?

Symbolic differentiation using predicate structured expressions:

d(X,W,Z) :- sum(U,V,W), d(X,Y,DU), d(X,V,DV), sum(DU,DV,Z).d(X,W,Z) :- prod(U,V,W), d(X,U,DU), d(X,V,DV), prod(DU,V,A),

prod(U,DV,B), sum(A,B,Z).d(X,X,1).d(X,C,0) :- atomic(C), C \= X.

Page 87: Artificial Intelligence programming and Prolog Language Tutorial.

Example: Symbolic Differentiation

• It is more convenient to describe individuals without giving them names (expressions or compounds as terms).

• using functors (tags):d(X, plus(U,V), plus(DU,DV)) :- d(X,U,DU), d(X,V,DV).

• or using infix functors:d(X, U+V, DU+DV) :- d(X,U,DU), d(X,V,DV).

• instead ofd(X,W,Z) :- sum(U,V,W), d(X,U,DU), d(X,V,DV), sum(DU,DV,Z).

• with less readability and some other things…

Page 88: Artificial Intelligence programming and Prolog Language Tutorial.

Why Not Predicates? (cont.)

• Waste use of intermediate (temporary) variables

• Less readability• Unexpected answers!

sum(x,1,z).:- d(x,z,D).No– Why? What did you expect? – How to correct it?

Page 89: Artificial Intelligence programming and Prolog Language Tutorial.

Closed World Model• All that is true is what can be proved on the basis of the facts

and rules in the database.

• Very reasonable in object-oriented apps (modeling a real or imagined world)– All existing objects are defined.– No object have a given property which cannot be found in db.

• Not suitable for mathematical problems (Why?)– An object is generally take to exist if its existance doesn’t contradict

the axioms.

• Predicates are better for OO-relationships, Compounds for mathematical ones (Why?)– We cannot assume existance of 1+0 whenever needed.

Page 90: Artificial Intelligence programming and Prolog Language Tutorial.

An Argument!

• What’s the answer?equal(X,X).:- equal(f(Y),Y).?

• What’s the logical meaning? (occurs check)• Any other meaning?• Can it be represented in a finite amount of

memory?• Should we detect it?

Page 91: Artificial Intelligence programming and Prolog Language Tutorial.

Control Structures

Page 92: Artificial Intelligence programming and Prolog Language Tutorial.

Algorithm = Logic + Control• N. Wirth: Program = data structure + algorithm• R. Kowalski: Algorithm = logic + control

• In conventional programming:– Logic of a program is closely related to its control– A change in order of statements alters the meaning of program

• In (pure) logic programming:– Logic (logic phase) is determined by logical interrelationships of the

clauses not their order.– Control (control phase) affects the order in which actions occur in time

and only affects the efficiency of programs.

• Orthogonality Principle

Page 93: Artificial Intelligence programming and Prolog Language Tutorial.

Top-Down vs. Bottom-Up Control

• Top-down ≈ Recursion:– Try to reach the hypotheses

from the goal.

• Bottom-up ≈ Iteration:– Try to reach the goal from the

hypotheses.

• Hybrid:– Work from both the goals and

the hypotheses and try to meet in the middle.

• Which one is better? fib(0,1). fib(1,1).fib(N,F) :- N=M+1, M=K+1, fib(M,G),

fib(K,H), F=G+H, N>1.

Page 94: Artificial Intelligence programming and Prolog Language Tutorial.

Procedural Interpretation• We have seen logical and record (data structure) interpretations.

• Clauses can also be viewed as procedure invocations:– <head>: proc. definition– <body>: proc. body (a series of proc. calls)– Multiple definitions: branches of a conditional (case)– fib() example…

• Procedure calls can be executed in any order or even concurrently! (pure logic)

• Input/Output params are not distinguished!– fib(3,3) ↔ true. fib(3,F) ↔ F=3. fib(N,3) ↔ N=3. fib(N,F) ↔ ?

Page 95: Artificial Intelligence programming and Prolog Language Tutorial.

Unify, Fail, Redo…• Heavy use of unification, backtracking and recursion.• Unification (Prolog pattern matching – from Wikipedia):

– One-time assignment (binding)– uninst. var with atom/term/another uninst. var (aliasing) (occurs

check)– atom with the same atom– compound with compound if top predicates and arities of the terms

are identical and if the parameters can be unified simultaneously– We can use ‘=‘ operator to explicitly unify two terms

• Backtracking:– Make another choice if a choice (unif./match) failes or want to find

other answers.– In logic prog. It is the rule rather than the exception. – Very expensive!

• Example: len([ ], 0). len(X.T, L+1) :- len(T,L).

Page 96: Artificial Intelligence programming and Prolog Language Tutorial.

Prolog’s Control Regime• Prolog lang. is defined to use depth-first search:

– Top to bottom (try the clauses in order of entrance)– Left to right– In pure logic prog., some complete deductive algorithm such as

Robinson’s resolution algorithm must be implemented.

• DFS other than BFS– Needs much fewer memory– Doesn’t work for an infinitely deep tree (responsibility of programmer)

• Some programs may fail if clauses and subgoals are not ordered correctly (pp.471-474)

• Predictable execution of impure predicates (write, nl, read, retract, asserta, assertz, …)

Page 97: Artificial Intelligence programming and Prolog Language Tutorial.

[trace] ?- ancestor(X, cindy), sibling(X,jeffrey).Event Depth Subgoal==================================Call: (1) ancestor(X, cindy) Call: (2) parent(X, cindy) Call: (3) father(X, cindy) Exit: (3) father(george, cindy) Exit: (2) parent(george, cindy) Exit: (1) ancestor(george, cindy) Call: (1) sibling(george, jeffrey) Call: (2) mother(M, george) Exit: (2) mother(alice, george) Call: (2) mother(alice, jeffrey) Exit: (2) mother(alice, jeffrey) Call: (2) father(F, george) Exit: (2) father(albert, george) Call: (2) father(albert, jeffrey) Exit: (2) father(albert, jeffrey) Call: (2) george\=jeffrey Exit: (2) george\=jeffrey Exit: (1) sibling(george, jeffrey)

X = georgeYes

SWI Prolog

Page 98: Artificial Intelligence programming and Prolog Language Tutorial.

If we move parent(X,Y) :- father(X,Y) before parent(X,Y) :- mother(X,Y), we have:Event Depth Subgoal==================================Call: (1) ancestor(X, cindy)Call: (2) parent(X, cindy)Call: (3) mother(X, cindy)Exit: (3) mother(mary, cindy)Exit: (2) parent(mary, cindy)Exit: (1) ancestor(mary, cindy)Call: (1) sibling(mary, jeffrey)Call: (2) mother(M, mary)Exit: (2) mother(sue, mary)Call: (2) mother(sue, jeffrey)Fail: (2) mother(sue, jeffrey)Redo: (2) mother(M, mary)Fail: (2) mother(M, mary)Fail: (1) sibling(mary, jeffrey)Redo: (3) mother(X, cindy)Fail: (3) mother(X, cindy)Redo: (2) parent(X, cindy)…

SWI Prolog

Page 99: Artificial Intelligence programming and Prolog Language Tutorial.

Cut! • ‘!’: Discard choice points of parent frame and frames created

after the parent frame.

• Always is satisfied.• Used to guarantee termination or control execution order.

• i.e. in the goal :- p(X,a), !– Only produce the 1st answer to X– Probably only one X satisfies p and trying to find another one leads to

an infinite search!

• i.e. in the rule color(X,red) :- red(X), !.– Don’t try other choices of red (mentioned above) and color if X

satisfies red– Similar to then part of a if-then-elseif

Fisher, J.R., Prolog Tutorial, http://www.csupomona.edu/~jrfisher/www/prolog_tutorial/contents.html

Page 100: Artificial Intelligence programming and Prolog Language Tutorial.

Red-Green Cuts (!)

• A ‘green’ cut– Only improves efficiency– e.g. to avoid additional unnecessary computation

• A ‘red’ cut– e.g. block what would be other consequences of

the program– e.g. control execution order (procedural prog.)

Fisher, J.R., Prolog Tutorial, http://www.csupomona.edu/~jrfisher/www/prolog_tutorial/contents.html

Page 101: Artificial Intelligence programming and Prolog Language Tutorial.

Three Examplesp(a).p(X) :- s(X), r(X).p(X) :- u(X).

r(a). r(b).

s(a). s(b). s(c).

u(d).

:- p(X), !:- r(X), !, s(Y).:- r(X), s(Y), !:- r(X), !, s(X).

part(a). part(b). part(c). red(a). black(b).

color(P,red) :- red(P),!. color(P,black) :- black(P),!. color(P,unknown).

:- color(a, C).:- color(c, C).:- color(a, unknown).

Fisher, J.R., Prolog Tutorial, http://www.csupomona.edu/~jrfisher/www/prolog_tutorial/contents.html

max(X,Y,Y) :- Y>X, !. max(X,Y,X). :- max(1,2,D).:- max(1,2,1).

See also MacLennan’s example p.476

Page 102: Artificial Intelligence programming and Prolog Language Tutorial.

Higher-Order Rules• Logic programming is limited to first-order logic: can’t

bind variables to predicates themselves.

• e.g. red (f-reduction) is illegal: (p(x,y,z) ↔ z=f(x,y))red(P,I,[ ],I).red(P,I,X.L,S) :- red(P,I,L,T), P(X,T,S).

• But is legal if the latter be defined as:red(P,I,X.L,S):- red(P,I,L,T), Q=..[P,X,T,S], call(Q).

– What’s the difference?

Page 103: Artificial Intelligence programming and Prolog Language Tutorial.

Higher-Order Rules (cont.)

• In LISP, both code and data are first-order objects, but in Prolog aren’t.

• Robinson resolution algorithm is refutation complete for first-order predicate logic.

• Gödel’s incompleteness theorem: No algorithm is refutation complete for higher-order predicate logic.

• So, Prolog indirectly supports higher-order rules.

Page 104: Artificial Intelligence programming and Prolog Language Tutorial.

Negative Facts• How to define nonsibling? Logically…

nonsibling(X,Y) :- X = Y.nonsibling(X,Y) :- mother(M1,X), mother(M2,Y), M1 \= M2.nonsibling(X,Y) :- father(F1,X), father(F2,Y), F1 \= F2.

• But if parents of X or Y are not in database?– What is the answer of nonsibling? Can be solved by…nonsibling(X,Y) :- no_parent(X).nonsibling(X,Y) :- no_parent(Y).– How to define no_parent?

Page 105: Artificial Intelligence programming and Prolog Language Tutorial.

Negative Facts (cont.)

• Problem: There is no positive fact expressing the absence of parent.

• Cause: – Horn clauses are limited to– C :- P1,P2,…,Pn ≡ C holds if P1^P2^…^Pn hold.– No conclusion if P1^P2^…^Pn don’t hold! – If, not iff

Page 106: Artificial Intelligence programming and Prolog Language Tutorial.

Cut-failSolutions:• Stating all negative facts such as no_parent

– Tedious– Error-prone– Negative facts about sth are usually much more than positive facts

about it

• “Cut-fail” combination– nonsibling(X,Y) is satisfiable if sibling(X,Y) is not (i.e. sibling(X,Y) is

unsatisfiable)– nonsibling(X,Y) :- sibling(X,Y), !, fail.– nonsibling(X,Y).– how to define ‘fail’ ?!

Page 107: Artificial Intelligence programming and Prolog Language Tutorial.

negation :- unsatisfiablility

• ‘not’ predicate– not(P) is satisfiable if P is not (i.e. is unsatisfiable).– not(P) :- call(P), !, fail.– not(P).– nonsibling(X,Y) :- not( sibling(X,Y) ).

• Is ‘not’ predicate the same as ‘logical negation’? (see p.484)

Page 108: Artificial Intelligence programming and Prolog Language Tutorial.

5th-Generation Languages and

Philosophy of Prolog

Page 109: Artificial Intelligence programming and Prolog Language Tutorial.

5th-Generation Languages

1. Declarative (nonprocedural)1. Functional Programming2. Logic Programming

2. Imperative– Object Oriented Programming

Page 110: Artificial Intelligence programming and Prolog Language Tutorial.

Nonprocedural ProgrammingSorting procedurally:

1. Find the min in the remained numbers.2. Swap it with the first number.3. Repeat steps 1,2 until no number remains.

Sorting nonprocedurally:4. B is a sorting of A ↔ B is a permutation of A and B is

ordered.5. B is ordered ↔ for each i<j: B[i] ≤ B[j]

Which is higher level?

Page 111: Artificial Intelligence programming and Prolog Language Tutorial.

Automated Theorem Proving• A.T.P: Developing programs that can construct formal proofs

of propositions stated in a symbolic language.

• Construct the desired result to prove its existence (most A.T.P.’s).

• In Logic Programming, programs are expressed in the form of propositions and the theorem prover constructs the result(s).

• J. A. Robinson: A program is a theory (in some logic) and computation is deduction from the theory.

Page 112: Artificial Intelligence programming and Prolog Language Tutorial.

Programming In Logic (Prolog)• Developed in Groupe d’Intelligence Artificielle (GIA)

of the University of Marseilles (early 70s) to process a natural language (French).

• Interpreters: Algol-W (72), FORTRAN (73), Pascal (76), Implemented on many platforms (Now)

• Application in AI since mid-70s• Successor to LISP for AI apps

• Not standardized (but has ISO standard now)

Page 113: Artificial Intelligence programming and Prolog Language Tutorial.

Properties of Prolog

• Logic programs are self-documenting• Pure logic programs separate logic and control• Prolog falls short of logic programming• Implementation techniques are improving• Prolog is a step toward nonprocedural

programming

Page 114: Artificial Intelligence programming and Prolog Language Tutorial.

Self-documentation

• Programming in a higher-level, …• Application orientation and…• Transparency

– programs are described in terms of predicates and individuals of the problem domain.

• Promotes clear, rapid, accurate programming

Page 115: Artificial Intelligence programming and Prolog Language Tutorial.

Separation of Logic and Control

• Simplifies programming• Correctness only deals with logic• Optimization in control cannot affect

correctness• Obeys Orthogonality Principle

Page 116: Artificial Intelligence programming and Prolog Language Tutorial.

Prolog vs. Logic Programming

• Definite control strategy– Programmers make explicit use of it and the result

have little to do with logic– Reasoning about the order of events in Prolog is

comparable in difficaulty with most imperative of conventional programming languages

• Cut doesn’t make any sense in logic!• not doesn’t correspond to logical negation

Page 117: Artificial Intelligence programming and Prolog Language Tutorial.

Improving Efficiency

• Prolog is far from an efficient language.• So, it’s applications are limited to apps in

which:– Performance is not important– Difficult to implement in a conventional lang.

• New methods are invented• Some compilers produce code comparable to

LISP

Page 118: Artificial Intelligence programming and Prolog Language Tutorial.

Toward Nonprocedural Programming

• Pure logic programs prove the possibility of nonprocedural programming.

• In Prolog, DFS requires programmers to think in terms of operations and their proper ordering in time (procedurally).

• And Prolog’s control regime is more unnatural than conventional languages.

• So, there is still much more important work to be done before nonprocedural programming becomes practical.

Page 119: Artificial Intelligence programming and Prolog Language Tutorial.

Presentation References• Colmerauer, Alain, Philippe Roussel, The Birth of Prolog, Nov. 1992, URL:

http://www.lim.univ-mrs.fr/~colmer/ArchivesPublications/HistoireProlog/19november92.pdf

• Fisher, J.R., Prolog Tutorial, 2004, URL: http://www.csupomona.edu/~jrfisher/www/prolog_tutorial/contents.html

• MacLennan, Bruce J., Principles of Programming Languages: Design, Evaluation and Implementation, 3rd ed, Oxford University Press, 1999

• Merritt, Dennis, “Prolog Under the Hood: An Honest Look”, PC AI magazine, Sep/Oct 1992

• “Unification”, Wikipedia, the free encyclopedia, 25 Sep. 2005, URL: http://en.wikipedia.org/wiki/Unification

Page 120: Artificial Intelligence programming and Prolog Language Tutorial.

Free Prolog Access

• SWI-Prologhttp://www.swi-prolog.org/

• YAProlog http://www.ncc.up.pt/~vsc/Yap/

• Strawberry Prologhttp://www.dobrev.com/

Page 121: Artificial Intelligence programming and Prolog Language Tutorial.

Sources

• http://www.afm.sbu.ac.uk/logic-prog/• http://en.wikipedia.org/wiki/Logic_programming• http://dictionary.reference.com/browse/logic%20programmin

g• Pages 45-46 of our textbook• CS 326 lectures on Prolog (written by Dr. Mircea Nicolescu)

Page 122: Artificial Intelligence programming and Prolog Language Tutorial.

sourcesDanielle and Joseph Bennett

Michael Scherger Giles OatleyMacLennan

Sadegh Dorri Nogourani