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
Embed
Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING (during a joint degree with Fudan University in Software Engineering) DEPT. OF COMPUTER SCIENCE UCD LOGIC.
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
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
Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING(during a joint degree with Fudan University in Software Engineering)
DEPT. OF COMPUTER SCIENCEUCD
Lecture 7
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
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:
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!
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 */
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
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!
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
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.
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}
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.
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.
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”.
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.
Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING(during a joint degree with Fudan University in Software Engineering)
DEPT. OF COMPUTER SCIENCEUCD
Lecture 8
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
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
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
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)
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)
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
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”
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.
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
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.
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
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.
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
Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING(during a joint degree with Fudan University in Software Engineering)
DEPT. OF COMPUTER SCIENCEUCD
Lecture 9
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.
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).
Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING(during a joint degree with Fudan University in Software Engineering)
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).
Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING(during a joint degree with Fudan University in Software Engineering)
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
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.
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)
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
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.
Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING(during a joint degree with Fudan University in Software Engineering)