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.2 World 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 Processor inputoutput bytes
2.3 World 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.
2.4 World 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. program inputoutput
2.5 World 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.
2.6 PROLOG 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.
2.7 A 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.
2.8 Running 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 : $ gprolog GNU Prolog By Daniel Diaz Copyright (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.pl compiling... yes | ?- PROLOG always say yes if something works.
2.9 A 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 = jim yes | ?- Who is called a logical variable. –PROLOG will set a logical variable to any value which makes the query succeed.
2.10 A 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.
2.11 A 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.
2.12 A 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.
2.13 A 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 ?
2.14 A 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.
2.15 A 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.
2.16 Summary 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.
2.17 Summary 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.