17
2.1 2. Introduction To PROLOG World view of imperative languages. World view of relational languages. A PROLOG program. Running a PROLOG program. A PROLOG query. A PROLOG search space.

2.1 2. Introduction To PROLOG World view of imperative languages. World view of relational languages. A PROLOG program. Running a PROLOG program. A PROLOG

Embed Size (px)

Citation preview

Page 1: 2.1 2. Introduction To PROLOG World view of imperative languages. World view of relational languages. A PROLOG program. Running a PROLOG program. A PROLOG

2.12.1

2. Introduction To PROLOG2. Introduction To PROLOG

• World view of imperative languages.

• World view of relational languages.

• A PROLOG program.

• Running a PROLOG program.

• A PROLOG query.

• A PROLOG search space.

Page 2: 2.1 2. Introduction To PROLOG World view of imperative languages. World view of relational languages. A PROLOG program. Running a PROLOG program. A PROLOG

2.22.2

World View Of Imperative LanguagesWorld View Of Imperative Languages

• A Von Neumann Machine.

• Processor executes instructions stored in memory.

– Instructions modify memory contents, read input and / or write output.

• Contents of memory + input not yet read + output written so far = state.

Memory

Processorinput output

bytes

Page 3: 2.1 2. Introduction To PROLOG World view of imperative languages. World view of relational languages. A PROLOG program. Running a PROLOG program. A PROLOG

2.32.3

World View Of Imperative Languages IIWorld View Of Imperative Languages II

• Imperative world view is based on state.

– Imperative program = sequence of instructions which modify state (i.e. statements).

– Result of computation is state after the computation has terminated.

• Most computers are Von Neumann machines.

– Have the same world view as imperative languages.

– Little translation overhead.

– Imperative languages run efficiently on them.

• Imperative languages tend to use Von Neumann hardware as efficiently as possible.

• Imperative languages tend to use programmer time as inefficiently as possible.

Page 4: 2.1 2. Introduction To PROLOG World view of imperative languages. World view of relational languages. A PROLOG program. Running a PROLOG program. A PROLOG

2.42.4

World View Of Relational LanguagesWorld View Of Relational Languages

• Usually written :

program(input, output)

• The program is a relation which holds between the input and the output.

• No notion of a state which can be modified so no statements.

• No memory in the model so no variables to assign to.

• Relational programs do have variables but they are logical variables.

– Logical variables take on the values required to make the relations containing them hold.

programinput output

Page 5: 2.1 2. Introduction To PROLOG World view of imperative languages. World view of relational languages. A PROLOG program. Running a PROLOG program. A PROLOG

2.52.5

World View Of Relational Languages IIWorld View Of Relational Languages II

• Relational programs tend to be a lot shorter because there is no consideration of memory, input and output.

• Relational program a lot less efficient (on a Von Neumann machine) because there is no consideration of memory, input and output.

• There is no real notion of memory, input or output.

• Can run a relational program backwards : give the program the output and it returns the input.

• Relational programs are bi-directional.

Page 6: 2.1 2. Introduction To PROLOG World view of imperative languages. World view of relational languages. A PROLOG program. Running a PROLOG program. A PROLOG

2.62.6

PROLOGPROLOG

• PROLOG is the most commonly used relational language.

– PROLOG stands for “PROgramming in LOGic”.

– Often (incorrectly) written Prolog.

• PROLOG is sometimes referred to as a “Logic Programming Language”.

– This is incorrect, PROLOG only implements first-order logic. Horn Clause logic.

• First implemented by Alain Colmeraurer and his team at Marseille University in the early 1970s.

• Based on fundamental work by Alan Robinson and Robert Kowalski in the 1960s.

Page 7: 2.1 2. Introduction To PROLOG World view of imperative languages. World view of relational languages. A PROLOG program. Running a PROLOG program. A PROLOG

2.72.7

A PROLOG ProgramA PROLOG Program

• A PROLOG program is a set of facts and rules.

• A simple program with just facts :

parent(alice, jim).parent(jim, tim).parent(jim, dave).parent(jim, sharon).parent(tim, james).parent(tim, thomas).

• c.f. a table in a relational database.

• Each line is a fact (a.k.a. a tuple or a row).

• Each line states that some person X is a parent of some (other) person Y.

• In GNU PROLOG the program is kept in an ASCII file.

Page 8: 2.1 2. Introduction To PROLOG World view of imperative languages. World view of relational languages. A PROLOG program. Running a PROLOG program. A PROLOG

2.82.8

Running a PROLOG ProgramRunning a PROLOG Program

• To get information from the program we query it in much the same way as a relational database.

• First, run GNU PROLOG :

$ gprologGNU Prolog 1.2.1By Daniel DiazCopyright (C) 1999,2000 Daniel Diaz| ?-

• The last line is the GNU PROLOG prompt. Next, load the file containing the program (consulting the program) :

| ?- [lect1]. % File is called lect1.plcompiling ...yes| ?-

• PROLOG always say yes if something works.

Page 9: 2.1 2. Introduction To PROLOG World view of imperative languages. World view of relational languages. A PROLOG program. Running a PROLOG program. A PROLOG

2.92.9

A PROLOG QueryA PROLOG Query

• Now we can ask PROLOG questions :

| ?- parent(alice, jim).yes| ?- parent(jim, herbert).no| ?-

• Not very exciting. But what about this :

| ?- parent(alice, Who).Who = jimyes| ?-

• Who is called a logical variable.

– PROLOG will set a logical variable to any value which makes the query succeed.

Page 10: 2.1 2. Introduction To PROLOG World view of imperative languages. World view of relational languages. A PROLOG program. Running a PROLOG program. A PROLOG

2.102.10

A PROLOG Query IIA PROLOG Query II

• Logical variables start with an upper case letter. Anything that starts with a lower case letter is a literal constant.

• Sometimes there is more than one correct answer to a query.

• PROLOG gives the answers one at a time. To get the next answer type ;.

| ?- parent(jim, Who).Who = tim ? ;Who = dave ? ;Who = sharon ? ;yes| ?-

• After finding that jim was a parent of sharon GNU PROLOG detects that there are no more alternatives for parent and ends the search.

NB : The ; do not

actually appear on the screen.

NB : The ; do not

actually appear on the screen.

Page 11: 2.1 2. Introduction To PROLOG World view of imperative languages. World view of relational languages. A PROLOG program. Running a PROLOG program. A PROLOG

2.112.11

A PROLOG Search SpaceA PROLOG Search Space

• The easiest way to understand how a PROLOG program works is to draw a search space.

– Sometimes called a search tree.

– A pictorial representation of Robinson’s Unification Algorithm in action.

• PROLOG is not very bright - it simply searches the program from top to bottom checking each fact in turn.

• For the query to succeed PROLOG must be able to make the query the same as the fact it is checking against.

– The query and the fact must MATCH.

• C marks a choice point : a place where PROLOG has more than one way of obtaining a match.

– Choice points are used in backtracking.

Page 12: 2.1 2. Introduction To PROLOG World view of imperative languages. World view of relational languages. A PROLOG program. Running a PROLOG program. A PROLOG

2.122.12

A PROLOG Search Space IIA PROLOG Search Space II

• Match 1 : alice \= jim, jim = Who.

– Since Who is a logical variable PROLOG can set it to any value required to obtain a match, in this case Who is set to jim.

– This is no help as alice and jim are different literal constants. There is no way they can be made equal.

– The match FAILS.

• PROLOG automatically backtracks to the closest choice point (C) and checks the next fact for a match.

• Match 2 : jim = jim, tim = Who.

– jim occurs in both the query and the fact. A constant always matches with itself.

– Since Who is a logical variable PROLOG can set it to any value required to obtain a match, in this case Who is set to tim.

– The match SUCCEEDS.

Page 13: 2.1 2. Introduction To PROLOG World view of imperative languages. World view of relational languages. A PROLOG program. Running a PROLOG program. A PROLOG

2.132.13

A PROLOG Search Space IIIA PROLOG Search Space III

• At this point PROLOG prints

Who = tim ?

• PROLOG is asking whether we want any more answers. If we press RETURN the search stops and we get the normal PROLOG prompt. If we press ; the search continues (we force backtracking to the closest choice point).

• Match 3 : jim = jim, dave = Who.

– jim occurs in both the query and the fact. A constant always matches with itself.

– Since Who is a logical variable PROLOG can set it to any value required to obtain a match, in this case Who is set to dave.

– The match SUCCEEDS. PROLOG prints

Who = dave ?

Page 14: 2.1 2. Introduction To PROLOG World view of imperative languages. World view of relational languages. A PROLOG program. Running a PROLOG program. A PROLOG

2.142.14

A PROLOG Search Space IVA PROLOG Search Space IV

• Match 4 : jim = jim, sharon = Who.

– jim occurs in both the query and the fact. A constant always matches with itself.

– Since Who is a logical variable PROLOG can set it to any value required to obtain a match, in this case Who is set to sharon.

– The match SUCCEEDS. PROLOG prints

Who = sharon ?

• Match 5 : tim \= jim, james = Who.

– tim and jim are two different constants. They do not match.

– Since Who is a logical variable PROLOG can set it to any value required to obtain a match, in this case Who is set to james.

– The match FAILS.

Page 15: 2.1 2. Introduction To PROLOG World view of imperative languages. World view of relational languages. A PROLOG program. Running a PROLOG program. A PROLOG

2.152.15

A PROLOG Search Space VA PROLOG Search Space V

• PROLOG automatically backtracks to C to find another alternative.

• Match 6 : tim \= jim, thomas = Who.

– tim and jim are two different constants. They do not match.

– Since Who is a logical variable PROLOG can set it to any value required to obtain a match, in this case Who is set to thomas.

– The match FAILS.

• PROLOG automatically backtracks to C to find another alternative.

• There are no more alternatives at C. They have all been tried.

• The overall query FAILS.

– Remember, it has already had three successes.

– GNU PROLOG prints yes. Some implementations would print no in this case.

Page 16: 2.1 2. Introduction To PROLOG World view of imperative languages. World view of relational languages. A PROLOG program. Running a PROLOG program. A PROLOG

2.162.16

SummarySummary

• Relational world view : program(input, output).

– No memory, no I/O.

– No state.

• Logical variables take on the values required to make the relations containing them hold.

• Relational programs are bi-directional.

• PROLOG is an implementation of first-order logic.

– Horn clauses.

• A PROLOG program is a set of facts and rules.

Page 17: 2.1 2. Introduction To PROLOG World view of imperative languages. World view of relational languages. A PROLOG program. Running a PROLOG program. A PROLOG

2.172.17

Summary IISummary II

• PROLOG searches the program from top to bottom, left to right.

• PROLOG tries to match the query with the current fact (or the head of the current rule) by picking appropriate values for the logical variables in the query and / or the current fact (or the head of the current rule).

– PROLOG tries to make things the same.

• A search space is a pictorial representation of how PROLOG solves a particular query.

– Robinson’s Unification Algorithm.

• In the exam you will be asked to write at least one search space.