Formal Models of Computation

PDF Created with deskPDF PDF Writer - Trial :: http://www.docudesk.com
CS4026
Formal Models of Computation
Part II
The Logic Model
Lecture 8 – Search and conclusions
Programming in Prolog
PDF Created with deskPDF PDF Writer - Trial :: http://www.docudesk.com
• Further example – a search problem
• Conclusions from Part II
formal models of computation
2
Searching with Prolog
PDF Created with deskPDF PDF Writer - Trial :: http://www.docudesk.com
• Some problems can be formulated as search
problems
• In search problems, we have
– An initial state (or configuration) S0 of the problem
– A final state (or configuration) Sf
we want to find
– How can we get from S0 to Sf ?
• For instance,
– Search for a path between two nodes of a graph
• Sometimes we just want to know if some state can
be reached
formal models of computation
3
The 8-Queens problem
PDF Created with deskPDF PDF Writer - Trial :: http://www.docudesk.com
• The challenge:
– If possible, place 8 queens in a chessboards so that they
don’t attack each other; else fail.
• Constraints:
– Queens attack along rows, columns and diagonals
Is there a solution?
(sorry about the
kings!)
formal models of computation
4
The 8-Queens problem (Cont’d)
PDF Created with deskPDF PDF Writer - Trial :: http://www.docudesk.com
• First issue: representing the problem!
– A solution becomes much easier if we represent the
problem adequately.
– We don’t need to represent graphically a chessboard nor
the queen’s crown!
• In this example, all we need to represent is where
each queen will be placed in each row
– There cannot be two queens in one same row…
• Simple way to represent the problem: a list
[1/Q1,2/Q2,3/Q3,4/Q4,5/Q5,6/Q6,7/Q7,8/Q8]
Q1,…,Q8 are the columns (1-8)
formal models of computation
5
The 8-Queens problem (Cont’d)
PDF Created with deskPDF PDF Writer - Trial :: http://www.docudesk.com
• In our representation, the list
[1/1,2/3,3/5,4/7|_]
stands for the configuration
row 4, column 7
row 3, column 5
row 2, column 3
row 1, column 1
formal models of computation
6
The 8-Queens problem (Cont’d)
PDF Created with deskPDF PDF Writer - Trial :: http://www.docudesk.com
• The problem now consists in
finding a combination of 8 numbers ranging from 1 to 8
• However, these numbers must satisfy some
constraints:
– They cannot be the same (attack via column)
– They cannot be on the diagonals of other queens
formal models of computation
7
The 8-Queens problem (Cont’d)
PDF Created with deskPDF PDF Writer - Trial :: http://www.docudesk.com
• A search space is defined:
and so on…
formal models of computation
8
The 8-Queens problem (Cont’d)
PDF Created with deskPDF PDF Writer - Trial :: http://www.docudesk.com
• A program:
main(T):board(T),
queens(T).
% gets initial board
% tries to put queens
board([1/_,2/_,3/_,4/_,5/_,6/_,7/_,8/_]). % initial board
queens([]).
queens([R/C|Rest]):queens(Rest),
member(C,[1,2,3,4,5,6,7,8]),
safe(R/C,Rest).
safe(_,[]).
safe(R/C,[R1/C1|RCs]):C =\= C1,
C1 - C =\= R1 - R,
C1 - C =\= R - R1,
safe(R/C,RCs).
%
%
%
%
%
empty board is OK
otherwise, for each row
queens in remaining rows
add one more queen
check if it is safe
% empty board is safe
% R/C does not attack R1/C1
% if they are not same column
% and not same diagonal /
% and not same diagonal \
% R/C doesn't attach rest
formal models of computation
9
The 8-Queens problem (Cont’d)
PDF Created with deskPDF PDF Writer - Trial :: http://www.docudesk.com
• Results:
?- main(L).
L = [1/4,2/2,3/7,4/3,5/6,6/8,7/5,8/1] ? ;
formal models of computation
10
The 8-Queens problem (Cont’d)
PDF Created with deskPDF PDF Writer - Trial :: http://www.docudesk.com
• Results:
?- main(L).
L = [1/4,2/2,3/7,4/3,5/6,6/8,7/5,8/1] ? ;
L = [1/5,2/2,3/4,4/7,5/3,6/8,7/6,8/1] ? ;
formal models of computation
11
The 8-Queens problem (Cont’d)
PDF Created with deskPDF PDF Writer - Trial :: http://www.docudesk.com
• Results:
?- main(L).
L = [1/4,2/2,3/7,4/3,5/6,6/8,7/5,8/1] ? ;
L = [1/5,2/2,3/4,4/7,5/3,6/8,7/6,8/1] ? ;
L = [1/3,2/5,3/2,4/8,5/6,6/4,7/7,8/1] ?
formal models of computation
12
Modules in Prolog
PDF Created with deskPDF PDF Writer - Trial :: http://www.docudesk.com
• Most Prolog systems provide module mechanisms –
the details vary
• SWI Prolog provides a predicate based module
system, with a flat set of modules.
• Typical file defining module myprog and using
predicates defined in module hisprog:
:- module(myprog,[p/3,q/4]).
:- use_module(hisprog).
p(X,Y,Z) :-
…. .
% export
q(A,B,C,D) :- …. % export
r(X,Y) :- … % private
formal models of computation
13
Conclusions: Haskell vs Prolog
PDF Created with deskPDF PDF Writer - Trial :: http://www.docudesk.com
• In some ways, very similar languages:
–
–
–
–
–
Declarative basis
Lack of destructive assignment
The “clause/case based” definitions look quite similar
Similar syntax for variables, lists, etc.
Recursion, not iteration
• Main differences arise from the fact that in Haskell
one defines functions, whereas in Prolog relations:
– Single result vs multiple results
– Deterministic reduction vs search
– Single direction vs reversibility
• Different approaches to typing
formal models of computation
14
Conclusions: Logic Programming/Prolog
PDF Created with deskPDF PDF Writer - Trial :: http://www.docudesk.com
• Prolog is a realistic realisation of SLD resolution
• Writing Prolog programs involves a concern with
both logic (what it means, what is true) and control
(how this information is to be used)
• In practice, a Prolog programmer has to worry
about control more than one would like (wouldn’t it
be nice just to be able to write down logic?)
• Prolog probably matches logical purity less well
than Haskell matches the lambda calculus.
• But that’s because it is trying to do more…
formal models of computation
15