Top Banner
Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING (during a joint degree with Fudan University in Software Engineering) DEPT. OF COMPUTER SCIENCE UCD LOGIC PROGRAMMING (WEEK 3) Eleni E. Mangina Department of Computer Science University College Dublin General module information Books Introduction - Declarative programming The Prolog Language
63

Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING (during a joint degree with Fudan University in Software Engineering) DEPT. OF COMPUTER SCIENCE UCD LOGIC.

Dec 21, 2015

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING (during a joint degree with Fudan University in Software Engineering) DEPT. OF COMPUTER SCIENCE UCD LOGIC.

Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING(during a joint degree with Fudan University in Software Engineering)

DEPT. OF COMPUTER SCIENCEUCD

LOGIC PROGRAMMING(WEEK 3)

Eleni E. ManginaDepartment of Computer Science

University College Dublin

• General module information• Books• Introduction - Declarative programming• The Prolog Language

Page 2: Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING (during a joint degree with Fudan University in Software Engineering) DEPT. OF COMPUTER SCIENCE UCD LOGIC.

Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING(during a joint degree with Fudan University in Software Engineering)

DEPT. OF COMPUTER SCIENCEUCD

Lecture 7

Page 3: Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING (during a joint degree with Fudan University in Software Engineering) DEPT. OF COMPUTER SCIENCE UCD LOGIC.

Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING(during a joint degree with Fudan University in Software Engineering)

DEPT. OF COMPUTER SCIENCEUCD

Books

• The recommended book is:

Programming in Prolog

4th Edition

W. Clocksin & C.S. Mellish

• Another book:

The Art of Prolog

2nd edition

L. Sterling & E. Shapiro

Page 4: Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING (during a joint degree with Fudan University in Software Engineering) DEPT. OF COMPUTER SCIENCE UCD LOGIC.

Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING(during a joint degree with Fudan University in Software Engineering)

DEPT. OF COMPUTER SCIENCEUCD

Introduction to Prolog• Programming languages are of two kinds:

- procedural (BASIC, ForTran, C++, Pascal)- declarative (Lisp, Prolog, ML)

• In procedural programming, we tell the computer how to solve a problem

• In declarative programming, we tell the computer what problem we want solved

• (However, in Prolog, we often forced to give clues as to the solution method)

• Other AI languages: LISP, C/C++, CLIPS

Page 5: Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING (during a joint degree with Fudan University in Software Engineering) DEPT. OF COMPUTER SCIENCE UCD LOGIC.

Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING(during a joint degree with Fudan University in Software Engineering)

DEPT. OF COMPUTER SCIENCEUCD

Some facts about Prolog

(For those who know other languages)

• • No global variables.

• • No assignment statement.

• • No real iterative constructs.

• • No conditional (if/then) statement.

• • Recursion very central.

Page 6: Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING (during a joint degree with Fudan University in Software Engineering) DEPT. OF COMPUTER SCIENCE UCD LOGIC.

Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING(during a joint degree with Fudan University in Software Engineering)

DEPT. OF COMPUTER SCIENCEUCD

Basic elements of Prolog

• We give a database of facts and rules:

- some are always true

- some are dependent of others

• To run a program we ask questions about the facts

Page 7: Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING (during a joint degree with Fudan University in Software Engineering) DEPT. OF COMPUTER SCIENCE UCD LOGIC.

Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING(during a joint degree with Fudan University in Software Engineering)

DEPT. OF COMPUTER SCIENCEUCD

The central ideas of Prolog• SUCCESS/FAILURE: any computation can

“succeed” or “fail”, and this is also used as a “test” mechanism.

• MATCHING: any two data items can be compared for similarity, and values can be bound to variables in order to allow a match to succeed.

• SEARCHING: the whole activity of the Prolog system is to search through various options to find a combination that succeeds.

• BACKTRACKING: when the system fails during its search, it returns to previous choices to see if making a different choice would allow success.

Page 8: Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING (during a joint degree with Fudan University in Software Engineering) DEPT. OF COMPUTER SCIENCE UCD LOGIC.

Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING(during a joint degree with Fudan University in Software Engineering)

DEPT. OF COMPUTER SCIENCEUCD

Prolog in English• Example:John is the father of JimJane is the mother of JimJack is the father of John

Person 1 is a parent of Person 2 ifPerson 1 is the father of Person 2 orPerson 1 is the mother of Person 2.

Person 1 is a grandparent of Person 2 ifsome Person 3 is a parent of Person 2 andPerson 1 is a parent of Person 3.

Who is Jim’s father?Is Jane the mother of Fred?Is Jane the mother of Jim?Does Jack have a grandchild?

Page 9: Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING (during a joint degree with Fudan University in Software Engineering) DEPT. OF COMPUTER SCIENCE UCD LOGIC.

Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING(during a joint degree with Fudan University in Software Engineering)

DEPT. OF COMPUTER SCIENCEUCD

Prolog in Prologfather(john,jim).mother(jane,jim).father(jack,john)

parent(Person1,Person2):-father(Person1,Person2).

parent(Person1, Person2):-mother(Person1, Person2)

grandparent (Person1, Person2):-parent(Person3, Person2),parent(Person1, Person3).

?- father( Who, jim).?- mother( jane, fred).?- mother( jane, jim).?- grandparent( jack, _).

Page 10: Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING (during a joint degree with Fudan University in Software Engineering) DEPT. OF COMPUTER SCIENCE UCD LOGIC.

Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING(during a joint degree with Fudan University in Software Engineering)

DEPT. OF COMPUTER SCIENCEUCD

Using Prolog (1)

• First write your program• Then, type it into a Unix file, with a .pl extension• Then type

sicstus• Then, “consult” your file (omitting the .pl):

[yourfilename]• Then, you can ask your questions• Sometimes, you can get multiple answers, by typing ‘;’• If you edit your program file (eg. to correct something),

be sure to consult it again afterwards!

Page 11: Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING (during a joint degree with Fudan University in Software Engineering) DEPT. OF COMPUTER SCIENCE UCD LOGIC.

Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING(during a joint degree with Fudan University in Software Engineering)

DEPT. OF COMPUTER SCIENCEUCD

Using Prolog (2)

• To exit from Prolog, type halt.

• or Control/D

• The Prolog comment characters:– Single line comments:

% This is a commentThis is not a comment, but an error

– Multiple line comments/* This is a multi-line comment which must be closed with a */

Page 12: Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING (during a joint degree with Fudan University in Software Engineering) DEPT. OF COMPUTER SCIENCE UCD LOGIC.

Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING(during a joint degree with Fudan University in Software Engineering)

DEPT. OF COMPUTER SCIENCEUCD

Simple interaction with PrologOn a Unix workstation, type:

sicstus

this should give:

SICStus 3 #5: Fri Jul 4 12:29:00 BST 1997

| ?-

Then you can type a simple command:

write(’hello’).

which will give the response:

hello

yes

Or a sequence of commands, separated by COMMAS:

write(’hello’), nl, write(’friend’).

giving:

hello

friend

yes

To exit from Prolog, type

halt.

or

Control/D

Page 13: Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING (during a joint degree with Fudan University in Software Engineering) DEPT. OF COMPUTER SCIENCE UCD LOGIC.

Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING(during a joint degree with Fudan University in Software Engineering)

DEPT. OF COMPUTER SCIENCEUCD

Definining and loading programs

Start up Sicstus Prolog.• ‘consult’ your file (omitting the .pl):consult(myprog).There is an abbreviated form, using square

brackets:[myprog].• If you edit your program file (e.g. to correct

something),be sure to “consult” it again afterwards!

Page 14: Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING (during a joint degree with Fudan University in Software Engineering) DEPT. OF COMPUTER SCIENCE UCD LOGIC.

Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING(during a joint degree with Fudan University in Software Engineering)

DEPT. OF COMPUTER SCIENCEUCD

Defining a programA procedure (or predicate) consists of one or more clauses.Here is a simple clause:salute :-write(’Hi,’), tab(2), write(’pal!’).This defines a procedure whose head issaluteand whose body iswrite(’Hi,’), tab(2), write(’pal!’).If we put this in a file, then consult that file, then :?- salute.Hi, pal!yes

Page 15: Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING (during a joint degree with Fudan University in Software Engineering) DEPT. OF COMPUTER SCIENCE UCD LOGIC.

Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING(during a joint degree with Fudan University in Software Engineering)

DEPT. OF COMPUTER SCIENCEUCD

ChoicesDefinitions can have several clauses:greet(hamish):-write(’How are you doing, pal?’).greet(amelia):-write(’How awfully nice to see you!’).greet(mike):-write(’Hi there’).If this definition is consulted into Prolog, then:?- greet(amelia).How awfully nice to see you!yesThe goal is matched against the heads of the clauses, in order.The first matching clause is selected.The body of that clause is performed.

Page 16: Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING (during a joint degree with Fudan University in Software Engineering) DEPT. OF COMPUTER SCIENCE UCD LOGIC.

Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING(during a joint degree with Fudan University in Software Engineering)

DEPT. OF COMPUTER SCIENCEUCD

Failure?- greet(ebenezer).noIf no clauses match the goal, the goal fails.When the goal typed in at top-level fails, the

Prologuser-interface prints no.THIS IS DIFFERENT FROM AN ERROR.For example, trying to use an undefined

predicate is an error:?- say(’hello’).{EXISTENCE ERROR: say(hello):procedure user:say/1 does not exist}

Page 17: Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING (during a joint degree with Fudan University in Software Engineering) DEPT. OF COMPUTER SCIENCE UCD LOGIC.

Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING(during a joint degree with Fudan University in Software Engineering)

DEPT. OF COMPUTER SCIENCEUCD

Variables in definitionsAn argument to a predicate can be variable:greet(Name):-write(’Hullo, ’), write(Name).With this defintion, we get:?- greet(alphonse).Hullo, alphonseyesThe matching of the goal with the head of the clause causesthe variable Name to be bound to the value alphonse.This “binding” is then used when the body of the clause isbeing performed.

Page 18: Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING (during a joint degree with Fudan University in Software Engineering) DEPT. OF COMPUTER SCIENCE UCD LOGIC.

Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING(during a joint degree with Fudan University in Software Engineering)

DEPT. OF COMPUTER SCIENCEUCD

Clauses in ordergreet(hamish):-write(’How are you doing, pal?’).greet(amelia):-write(’How awfully nice to see you!’).greet(Name):-write(’Hullo, ’), write(Name).Then:?- greet(hamish).How are you doing, pal?yes?- greet(daisy).Hullo, daisyyes?- greet(amelia).How awfully nice to see you!yesClauses are tried in order from the top. The one with the variable will match any argument.

Page 19: Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING (during a joint degree with Fudan University in Software Engineering) DEPT. OF COMPUTER SCIENCE UCD LOGIC.

Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING(during a joint degree with Fudan University in Software Engineering)

DEPT. OF COMPUTER SCIENCEUCD

Variables in goalsA goal can also have variable arguments.Suppose we have consulted a file with all the clauses ofgreet shown so far.?- greet(Person).How are you doing, pal?Person = hamish ?The (successful) matching of the goal with the head of thefirst clause causes the variable Person to be bound to thevalue hamish.The Prolog interface reports this “binding”.

Page 20: Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING (during a joint degree with Fudan University in Software Engineering) DEPT. OF COMPUTER SCIENCE UCD LOGIC.

Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING(during a joint degree with Fudan University in Software Engineering)

DEPT. OF COMPUTER SCIENCEUCD

Summary so far• A Prolog program may succeed or fail.• Prolog programs consist of predicate definitions• Predicate definitions consist of clauses• Clauses consist of a head and and (so far) a body• A clause head has a predicate name and sometimes some

arguments.• A goal is a predicate name and sometimes some• arguments.• A goal matches against clause heads in order.• If no clause matches, that goal fails.• Successful matching may cause variables to be bound to values.• If a variable in the top-level goal becomes bound, the user-

interface reports this.

Page 21: Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING (during a joint degree with Fudan University in Software Engineering) DEPT. OF COMPUTER SCIENCE UCD LOGIC.

Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING(during a joint degree with Fudan University in Software Engineering)

DEPT. OF COMPUTER SCIENCEUCD

Lecture 8

Page 22: Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING (during a joint degree with Fudan University in Software Engineering) DEPT. OF COMPUTER SCIENCE UCD LOGIC.

Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING(during a joint degree with Fudan University in Software Engineering)

DEPT. OF COMPUTER SCIENCEUCD

Anatomy• Prolog programs consist of predicate definitions like

parent• Predicate definitions consist of clauses

parent(P1,P2) :- mother (P1, P2).

• Clauses consist of a heade.g. parent(P1,P2)And sometimes a bodye.g. mother(P1,P2)

• A clause head has a predicate name and sometimes arguments (P1, P2)

• The body is a collection of literals, which match clause heads

Page 23: Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING (during a joint degree with Fudan University in Software Engineering) DEPT. OF COMPUTER SCIENCE UCD LOGIC.

Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING(during a joint degree with Fudan University in Software Engineering)

DEPT. OF COMPUTER SCIENCEUCD

What makes a good Prolog program?

• In order of decreasing importance– Correctness and careful design– clarity and comments– speed

(at least for our purposes in this course)

Because we understand the logical meaning of logic programs we can reply on the computer to transform elegant but inefficient programs into ugly (but invisible) efficient ones

Page 24: Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING (during a joint degree with Fudan University in Software Engineering) DEPT. OF COMPUTER SCIENCE UCD LOGIC.

Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING(during a joint degree with Fudan University in Software Engineering)

DEPT. OF COMPUTER SCIENCEUCD

What makes a bad Prolog program?

• In no particular order:– Hacking undesigned code at the terminal– Using obscure or meaningless variable and

predicate names– Not commenting code– abusing the procedural aspects of Prolog

to produce logically meaningless code

Page 25: Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING (during a joint degree with Fudan University in Software Engineering) DEPT. OF COMPUTER SCIENCE UCD LOGIC.

Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING(during a joint degree with Fudan University in Software Engineering)

DEPT. OF COMPUTER SCIENCEUCD

• Recall that a Prolog program is made up of predicates

…. And a predicate is made up of clauses

• A clause has a head and maybe a body• The head of a clause always takes one of two

forms:prednamepredname( argument1, argument2, … )

• If the predicate is always true, there is no body, and we finish with a full-stop (period).

More about Prolog clause syntax (1)

Page 26: Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING (during a joint degree with Fudan University in Software Engineering) DEPT. OF COMPUTER SCIENCE UCD LOGIC.

Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING(during a joint degree with Fudan University in Software Engineering)

DEPT. OF COMPUTER SCIENCEUCD

• If the predicate is conditional, we connect it to a body of subgoals with the if operator, :-

• Each subgoal is a Prolog literal… which is either- like a head, as before; or- A negated head, written

\+ predname\+ predname (argument1, …)

• Note the space after the \+ operator• We will return to \+ later

More about Prolog clause syntax (2)

Page 27: Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING (during a joint degree with Fudan University in Software Engineering) DEPT. OF COMPUTER SCIENCE UCD LOGIC.

Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING(during a joint degree with Fudan University in Software Engineering)

DEPT. OF COMPUTER SCIENCEUCD

More about Prolog clause syntax (3)

• We can combine literals in Prolog clauses with the operators- , (comma) meaning “and” – conjuction- ; (semicolon) meaning “or” - disjunction

You should never nee dto use ; in a program because you can express disjunction via multiple clauses. It makes Prolog

compilation less effective

Page 28: Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING (during a joint degree with Fudan University in Software Engineering) DEPT. OF COMPUTER SCIENCE UCD LOGIC.

Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING(during a joint degree with Fudan University in Software Engineering)

DEPT. OF COMPUTER SCIENCEUCD

• There is also (minus, greater than) supposedly meaning “implies”

• We can complex expressions using brackets

( l1(a1,a2); l2(a3)), l3(a1)

l1(a1,a2); l2(a3)), l3(a1) l1(a1,a2); l2(a3), l3(a1))

More about Prolog clause syntax (4)

You should never need to use and it is best avoided because it does not mean the same as “logical implies”

Page 29: Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING (during a joint degree with Fudan University in Software Engineering) DEPT. OF COMPUTER SCIENCE UCD LOGIC.

Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING(during a joint degree with Fudan University in Software Engineering)

DEPT. OF COMPUTER SCIENCEUCD

More about Prolog goal syntax

• Prolog goal syntax is the same as the syntax of the clause body

• Literals are combined with “and”, “or”, “not” and “implies”

• We run a Prolog program by asking a question or more precisely, stating a goalp(x):-q(x)

|?- q(x)

Page 30: Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING (during a joint degree with Fudan University in Software Engineering) DEPT. OF COMPUTER SCIENCE UCD LOGIC.

Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING(during a joint degree with Fudan University in Software Engineering)

DEPT. OF COMPUTER SCIENCEUCD

Proof strategy

• Prolog solves questions by attempting to prove them

• Suppose we have consulted the ancestor program and we ask the question:

ancestor(alan,dave).

• ancestor is defined as:

ancestor(A,B) :- parent (A,B).

ancestor(A,B) :- parent (A,C), ancestor (C,B)

Page 31: Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING (during a joint degree with Fudan University in Software Engineering) DEPT. OF COMPUTER SCIENCE UCD LOGIC.

Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING(during a joint degree with Fudan University in Software Engineering)

DEPT. OF COMPUTER SCIENCEUCD

Proof strategy (2)• To prove this, prolog starts at the top of the

database and tried to find a predicate called ancestor

• Then it looks at each clause in turn, and tries to unify its head with the goal

• Once unification is complete, attempt to prove the literals in the body, in order of appearance

This is the prolog built-in proof strategy. However, it is possible to override it – we will cover this later

Page 32: Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING (during a joint degree with Fudan University in Software Engineering) DEPT. OF COMPUTER SCIENCE UCD LOGIC.

Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING(during a joint degree with Fudan University in Software Engineering)

DEPT. OF COMPUTER SCIENCEUCD

Unification• Unification works by comparing the structure

of literals:– First compare the predicate name;– Then, for each argument;

• If the gioal argument is a variable, then make itthe same as the program argument, and unification succeeds;

• Otherwise, if the program argument is a variable, then make it the same as the goal argument, and unification succeeds;

• Otherwise, if both arguments are the same, unification succeeds;

• Otherwise, unification fails

We will cover unification in more detail later

Page 33: Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING (during a joint degree with Fudan University in Software Engineering) DEPT. OF COMPUTER SCIENCE UCD LOGIC.

Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING(during a joint degree with Fudan University in Software Engineering)

DEPT. OF COMPUTER SCIENCEUCD

Proof Strategy (3)Prove: ancestor(alan,dave)Find: ancestor clause 1Unify: A = alan, B = daveProve: parent(alan, dave) FAILTry again:Find: ancestor clause 2Unify: A = alan, B = daveProve: parent(alan, C)Find: parent clause 1Unify: C = clive SUCCEEDNext goal:Prove: ancestor(clive, dave)Find: ancestor clause 1Unify: A = clive, B = daveProve: parent(clive, dave) SUCCEEDAnswer: yes.

Page 34: Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING (during a joint degree with Fudan University in Software Engineering) DEPT. OF COMPUTER SCIENCE UCD LOGIC.

Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING(during a joint degree with Fudan University in Software Engineering)

DEPT. OF COMPUTER SCIENCEUCD

Summary so far

• Using the Prolog system• Programming style• Prolog clause syntax

– Predicates– Clauses– Heads & bodies– Variables– Full-stops (periods)– Logical operators: , ; \+– Brackets

Page 35: Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING (during a joint degree with Fudan University in Software Engineering) DEPT. OF COMPUTER SCIENCE UCD LOGIC.

Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING(during a joint degree with Fudan University in Software Engineering)

DEPT. OF COMPUTER SCIENCEUCD

So far..• A Prolog program may succeed or fail.• Prolog programs consist of predicate definitions• Predicate definitions consist of clauses• Clauses consist of a head and and (so far) a body• A clause head has a predicate name and sometimes some

arguments.• A goal is a predicate name and sometimes some arguments.• A goal matches against clause heads in order.• If no clause matches, that goal fails.• Successful matching may cause variables to be bound to

values.• If a variable in the top-level goal becomes bound, the user-

interface reports this.

Page 36: Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING (during a joint degree with Fudan University in Software Engineering) DEPT. OF COMPUTER SCIENCE UCD LOGIC.

Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING(during a joint degree with Fudan University in Software Engineering)

DEPT. OF COMPUTER SCIENCEUCD

Tests“Success” can act as “true”, “failure” as “false”:| ?- 5 < 7.yes| ?- 7 < 5.noSo the comma acts like a (left-to-right) “AND” :| ?- 3 < 7, 2 < 4, 10 < 12.yes| ?- 3 < 7, 4 < 2, 10 < 12.no

Page 37: Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING (during a joint degree with Fudan University in Software Engineering) DEPT. OF COMPUTER SCIENCE UCD LOGIC.

Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING(during a joint degree with Fudan University in Software Engineering)

DEPT. OF COMPUTER SCIENCEUCD

Tests in clausesThis allows more precise selection of a clause:

bigger(N, M):- N < M,

write(’Bigger number is ’), write(M).

bigger(N, M) :- M < N,

write(’Bigger number is ’), write(N).

bigger(N, M) :- write(’Numbers are the same’).

If a test fails, then the system backtracks, and tries to choose a later clause.

No need for an equality test in 3rd clause here – system tries this one only when other two have failed, and hence N and M must be equal.

Page 38: Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING (during a joint degree with Fudan University in Software Engineering) DEPT. OF COMPUTER SCIENCE UCD LOGIC.

Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING(during a joint degree with Fudan University in Software Engineering)

DEPT. OF COMPUTER SCIENCEUCD

Passing resultsDON’T return values by printing messages.Return values by causing suitable variables to become bound.larger(N, M, M):- N < M.larger(N, M, N) :- M < N.larger(N, M, M).e.g. :| ?- larger(8,3,Result).Result = 8yes| ?- larger(6,6,Value).Value = 6yes| ?- larger(2,5,Value).Value = 5yes

Page 39: Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING (during a joint degree with Fudan University in Software Engineering) DEPT. OF COMPUTER SCIENCE UCD LOGIC.

Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING(during a joint degree with Fudan University in Software Engineering)

DEPT. OF COMPUTER SCIENCEUCD

Variable bindinglarger ( 8 , 3 , Result )

larger ( N, M, M ) :- N < M, .....

M bound to 3, and M bound to Result, so Result bound to 3.

Clause body fails. Bindings discarded. Next clause tried:

larger ( 8 , 3 , Result )

larger ( N, M, N ):- M < N, .....

Since N bound to 8, and N bound to Result, Result bound to 3.

Clause body succeeds. Result binding is retained and

displayed.

Page 40: Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING (during a joint degree with Fudan University in Software Engineering) DEPT. OF COMPUTER SCIENCE UCD LOGIC.

Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING(during a joint degree with Fudan University in Software Engineering)

DEPT. OF COMPUTER SCIENCEUCD

More on variable matching

• a variable can be bound to another variable (sharing)

• a shared set can contain any number of variables• if variable A is bound to variable B, then variable

B is bound to variable A• shared variables cannot be bound to different

non-variable values• when one of a shared set of variables is bound to

a value, all the variables in that shared set are bound to the same value

Page 41: Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING (during a joint degree with Fudan University in Software Engineering) DEPT. OF COMPUTER SCIENCE UCD LOGIC.

Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING(during a joint degree with Fudan University in Software Engineering)

DEPT. OF COMPUTER SCIENCEUCD

Lecture 9

Page 42: Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING (during a joint degree with Fudan University in Software Engineering) DEPT. OF COMPUTER SCIENCE UCD LOGIC.

Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING(during a joint degree with Fudan University in Software Engineering)

DEPT. OF COMPUTER SCIENCEUCD

Unit clausesA clause may have an empty body –no goals, and omit the “:-” symbol.greet(nasty). % Clause in your programEverything works as before:| ?- greet(nasty).yes| ?- greet(Who).Who = nasty?yesThese are unit clauses.

Page 43: Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING (during a joint degree with Fudan University in Software Engineering) DEPT. OF COMPUTER SCIENCE UCD LOGIC.

Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING(during a joint degree with Fudan University in Software Engineering)

DEPT. OF COMPUTER SCIENCEUCD

Unit clauses as facts%% A simple set of clauses describing

%% some family relationships

man(paul).

man(david).

man(peter).

woman(louise).

woman(helen).

woman(mandy).

wifeof(paul, louise).

wifeof(peter, helen).

sonof(paul, peter).

daughterof(peter, mandy).

Use constant symbols to represent objects, and predicates for properties (e.g. woman) or relationships (e.g. sonof).

Page 44: Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING (during a joint degree with Fudan University in Software Engineering) DEPT. OF COMPUTER SCIENCE UCD LOGIC.

Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING(during a joint degree with Fudan University in Software Engineering)

DEPT. OF COMPUTER SCIENCEUCD

Querying this “database”

| ?- man(peter).yes| ?- man(louise).no| ?- woman(Someone).Someone = louise;Someone = helen;Someone = mandy;no

Page 45: Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING (during a joint degree with Fudan University in Software Engineering) DEPT. OF COMPUTER SCIENCE UCD LOGIC.

Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING(during a joint degree with Fudan University in Software Engineering)

DEPT. OF COMPUTER SCIENCEUCD

| ?- wifeof(paul, Hiswife).Hiswife = louiseyes| ?- wifeof(Herhusband, louise).Herhusband = paulyes| ?- daughterof(Father, mandy).Father = peteryes| ?- sonof(Father, Son).Father = paulSon = peteryes

Page 46: Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING (during a joint degree with Fudan University in Software Engineering) DEPT. OF COMPUTER SCIENCE UCD LOGIC.

Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING(during a joint degree with Fudan University in Software Engineering)

DEPT. OF COMPUTER SCIENCEUCD

Facts and Rules togetherAlongside facts (unit clauses) we can have full clauses:husbandof(Woman, Man):-wifeof(Man, Woman).“For a goal involving husbandof, try a goal usingwifeof, with the arguments in the other order.”| ?- husbandof(helen, peter).yesWe could also have:parentof(Person1, Person2):-daughterof(Person1, Person2).parentof(Person1, Person2):-sonof(Person1, Person2).

Page 47: Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING (during a joint degree with Fudan University in Software Engineering) DEPT. OF COMPUTER SCIENCE UCD LOGIC.

Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING(during a joint degree with Fudan University in Software Engineering)

DEPT. OF COMPUTER SCIENCEUCD

| ?- parentof(peter, Child).Child = mandyyes| ?- parentof(louise, peter).no| ?- parentof(Parent, peter).Parent = paulyes| ?- parentof(Parent, Child).Child = mandyParent = peteryes

Page 48: Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING (during a joint degree with Fudan University in Software Engineering) DEPT. OF COMPUTER SCIENCE UCD LOGIC.

Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING(during a joint degree with Fudan University in Software Engineering)

DEPT. OF COMPUTER SCIENCEUCD

More on backtracking

Suppose we add:

grandparent(OldPerson, YoungerPerson):-

parentof(OldPerson, Another),

parentof(Another, YoungerPerson).

and try the goal:

| ?- grandparent(Oldone, Youngone).

Page 49: Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING (during a joint degree with Fudan University in Software Engineering) DEPT. OF COMPUTER SCIENCE UCD LOGIC.

Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING(during a joint degree with Fudan University in Software Engineering)

DEPT. OF COMPUTER SCIENCEUCD

Looks for a parentof relation.First clause says try daughterof.SUCCESS with daughterof(peter, mandy)So Another = mandy.Looks for parentof(mandy, YoungerPerson).First clause says try daughterof.None with mandy. FAILURE.Second clause suggests sonof.None with mandy. FAILURE.So second parentof goal FAILS.Try first parentof goal again.Second clause suggests sonof.SUCCESS with sonof(paul, peter)So OldPerson = paul, Another = peter.Looks for parent(peter, YoungerPerson).First clause says try daughterof.SUCCESS with daughterof(peter, mandy)So YoungerPerson) = mandyBoth parentof goals successful.So grandparent goal successful(with bindings Oldone = paul, Youngone = mandy).

Page 50: Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING (during a joint degree with Fudan University in Software Engineering) DEPT. OF COMPUTER SCIENCE UCD LOGIC.

Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING(during a joint degree with Fudan University in Software Engineering)

DEPT. OF COMPUTER SCIENCEUCD

Tracing“spy” shows you what is going on inside your program.| ?- spy(larger).{The debugger will first leap -- showing spypoints {Spypoint placed on

user:larger/3}yes{debug}| ?- larger(8,9, Output).+ 1 1 Call: larger(8,9,_195) ?2 2 Call: 8<9 ?2 2 Exit: 8<9 ?+ 1 1 Exit: larger(8,9,9) ?Output = 9 ?yes{debug}

Page 51: Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING (during a joint degree with Fudan University in Software Engineering) DEPT. OF COMPUTER SCIENCE UCD LOGIC.

Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING(during a joint degree with Fudan University in Software Engineering)

DEPT. OF COMPUTER SCIENCEUCD

| ?- larger(9,8, Result).+ 1 1 Call: larger(9,8,_195) ?2 2 Call: 9<8 ?2 2 Fail: 9<8 ?2 2 Call: 8<9 ?2 2 Exit: 8<9 ?+ 1 1 Exit: larger(9,8,9) ?Result = 9 ?yes

Page 52: Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING (during a joint degree with Fudan University in Software Engineering) DEPT. OF COMPUTER SCIENCE UCD LOGIC.

Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING(during a joint degree with Fudan University in Software Engineering)

DEPT. OF COMPUTER SCIENCEUCD

?- spy(grandparent).

{The debugger will first leap -- showing spypoints {Spypoint placed on user:grandparent/2}

yes

{debug}

Page 53: Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING (during a joint degree with Fudan University in Software Engineering) DEPT. OF COMPUTER SCIENCE UCD LOGIC.

Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING(during a joint degree with Fudan University in Software Engineering)

DEPT. OF COMPUTER SCIENCEUCD

| ?- grandparent(Oldone, Youngone).+ 1 1 Call: grandparent(_193,_221) ?2 2 Call: parentof(_193,_485) ?3 3 Call: daughterof(_193,_485) ?3 3 Exit: daughterof(peter,mandy) ?2 2 Exit: parentof(peter,mandy) ?4 2 Call: parentof(mandy,_221) ?5 3 Call: daughterof(mandy,_221) ?5 3 Fail: daughterof(mandy,_221) ?5 3 Call: sonof(mandy,_221) ?5 3 Fail: sonof(mandy,_221) ?4 2 Fail: parentof(mandy,_221) ?2 2 Redo: parentof(peter,mandy) ?3 3 Redo: daughterof(peter,mandy) ?3 3 Fail: daughterof(_193,_485) ?3 3 Call: sonof(_193,_485) ?3 3 Exit: sonof(paul,peter) ?2 2 Exit: parentof(paul,peter) ?4 2 Call: parentof(peter,_221) ?5 3 Call: daughterof(peter,_221) ?5 3 Exit: daughterof(peter,mandy) ?4 2 Exit: parentof(peter,mandy) ?+ 1 1 Exit: grandparent(paul,mandy) ?Oldone = paul,Youngone = mandy ?yes

Page 54: Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING (during a joint degree with Fudan University in Software Engineering) DEPT. OF COMPUTER SCIENCE UCD LOGIC.

Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING(during a joint degree with Fudan University in Software Engineering)

DEPT. OF COMPUTER SCIENCEUCD

Negation

• So far, everything we have said fits in with the idea of programming in logic

• So far, truth in terms of our intended interpretation has been exactly the same as provability in the Prolog database

• However, one aspect of Prolog does not fit well with logic, even though we need it often

• Negation is the option of reserving the truth or falsehood of a predicate

Page 55: Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING (during a joint degree with Fudan University in Software Engineering) DEPT. OF COMPUTER SCIENCE UCD LOGIC.

Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING(during a joint degree with Fudan University in Software Engineering)

DEPT. OF COMPUTER SCIENCEUCD

Negation (2)• For example, given the database

p(a)

p(b)

the question

\+ p(a)

yields the answer

no.• Similarly, the question

\+ p(c)

yields the answer

yes.

Page 56: Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING (during a joint degree with Fudan University in Software Engineering) DEPT. OF COMPUTER SCIENCE UCD LOGIC.

Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING(during a joint degree with Fudan University in Software Engineering)

DEPT. OF COMPUTER SCIENCEUCD

Negation (3)

• Negation in Prolog works by– Trying to prove the negated goal;– If the goal succeeds, fail the current proof;– If it fails, then proceed to the next goal as

though it had succeeded

• We will consider how this is implemented later on

It is called Negation As Failure (NAF)

Page 57: Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING (during a joint degree with Fudan University in Software Engineering) DEPT. OF COMPUTER SCIENCE UCD LOGIC.

Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING(during a joint degree with Fudan University in Software Engineering)

DEPT. OF COMPUTER SCIENCEUCD

Floundering• However, consider this goal:

X = 5, \+ (X = 2)

• Now consider this goal:\+ (X = 2), X = 5

• Logically, these two are the same – both should succeed, with X=5

However, they are not the same in Prolog, because the logical semantics (meaning) diverges from the procedural semantics

(the way of working things out) at this point

Page 58: Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING (during a joint degree with Fudan University in Software Engineering) DEPT. OF COMPUTER SCIENCE UCD LOGIC.

Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING(during a joint degree with Fudan University in Software Engineering)

DEPT. OF COMPUTER SCIENCEUCD

Proof Strategy• Prolog proof strategy works as follows:

To prove a goal true:– Start at the top of the database; and scan down it

until you find a clause whose head can unify with the goal. If there is no such clause, go back to the last clause you chose and try to find an alternative to it further down the database

– If that clause has a body, take the body, with any values added by unification, and start again (recurse), with the first goal

– If that clause doesn’t have a body, proceed to the next unproved goal

– When there are no more proven goals, stop.

Page 59: Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING (during a joint degree with Fudan University in Software Engineering) DEPT. OF COMPUTER SCIENCE UCD LOGIC.

Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING(during a joint degree with Fudan University in Software Engineering)

DEPT. OF COMPUTER SCIENCEUCD

Proof strategy (2)Given the database and query:

a(1).a(2).b(Y) :- a(Y), \+ c(Y).c(1).d(1).d(2).?- b(X), d(X).

What happens?– Select leftmost goal: b(x)– Start at top of database, and scan down for match

under unification, which fgives Y = X

Page 60: Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING (during a joint degree with Fudan University in Software Engineering) DEPT. OF COMPUTER SCIENCE UCD LOGIC.

Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING(during a joint degree with Fudan University in Software Engineering)

DEPT. OF COMPUTER SCIENCEUCD

Proof strategy (3)a(1).a(2).b(Y) :- a(Y), \+ c(Y).c(1).d(1).d(2).

• Now we have these goals to consider:a(X), \+ c(X), d(X).

• Looking for a proof of a(X), we first unify with a(1) to give X = 1:

\+ c(1), d(1).

• But c(1) is provable, so \+ c(1) fails!

Page 61: Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING (during a joint degree with Fudan University in Software Engineering) DEPT. OF COMPUTER SCIENCE UCD LOGIC.

Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING(during a joint degree with Fudan University in Software Engineering)

DEPT. OF COMPUTER SCIENCEUCD

Proof strategy (4)a(1).

a(2).

b(Y) :- a(Y), \+ c(Y).

c(1).

d(1).

d(2).• The last match we made was with a(1), so retry that

… and this time a(2) matches.• So now we have to prove

\+ c(2), d(2).• We cannot prove c(2), so \+ c(2) suceeds

Page 62: Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING (during a joint degree with Fudan University in Software Engineering) DEPT. OF COMPUTER SCIENCE UCD LOGIC.

Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING(during a joint degree with Fudan University in Software Engineering)

DEPT. OF COMPUTER SCIENCEUCD

Proof Strategy (5)a(1).a(2).b(Y) :- a(Y), \+ c(Y).c(1).d(1).d(2).

• We now have to prove d(2) – which succeeds.

• We finish with the answerX = 2yes

Page 63: Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING (during a joint degree with Fudan University in Software Engineering) DEPT. OF COMPUTER SCIENCE UCD LOGIC.

Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING(during a joint degree with Fudan University in Software Engineering)

DEPT. OF COMPUTER SCIENCEUCD

Summary

• So far:– Negation as Failure– Prolog standard proof (search) strategy

• Next:– Terms– Lists– Unification