Download presentation

Presentation is loading. Please wait.

Published byConnor Porter Modified over 2 years ago

1
CS4026 Formal Models of Computation Part II The Logic Model Lecture 8 – Search and conclusions

2
formal models of computation 2 Programming in Prolog Further example – a search problem Conclusions from Part II

3
formal models of computation 3 Searching with Prolog Some problems can be formulated as search problems In search problems, we have –An initial state (or configuration) S 0 of the problem –A final state (or configuration) S f we want to find –How can we get from S 0 to S f ? For instance, –Search for a path between two nodes of a graph Sometimes we just want to know if some state can be reached

4
formal models of computation 4 The 8-Queens problem The challenge: –If possible, place 8 queens in a chessboards so that they dont attack each other; else fail. Constraints: –Queens attack along rows, columns and diagonals Is there a solution? (sorry about the kings!)

5
formal models of computation 5 The 8-Queens problem (Contd) First issue: representing the problem! –A solution becomes much easier if we represent the problem adequately. –We dont need to represent graphically a chessboard nor the queens 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)

6
formal models of computation 6 The 8-Queens problem (Contd) In our representation, the list [1/1,2/3,3/5,4/7|_] stands for the configuration row 1, column 1 row 2, column 3 row 3, column 5 row 4, column 7

7
formal models of computation 7 The 8-Queens problem (Contd) 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

8
formal models of computation 8 The 8-Queens problem (Contd) A search space is defined: and so on…

9
formal models of computation 9 The 8-Queens problem (Contd) A program: main(T):- board(T), % gets initial board queens(T). % tries to put queens board([1/_,2/_,3/_,4/_,5/_,6/_,7/_,8/_]). % initial board queens([]). % empty board is OK queens([R/C|Rest]):- % otherwise, for each row queens(Rest), % queens in remaining rows member(C,[1,2,3,4,5,6,7,8]), % add one more queen safe(R/C,Rest). % check if it is safe safe(_,[]). % empty board is safe safe(R/C,[R1/C1|RCs]):- % R/C does not attack R1/C1 C =\= C1, % if they are not same column C1 - C =\= R1 - R, % and not same diagonal / C1 - C =\= R - R1, % and not same diagonal \ safe(R/C,RCs). % R/C doesn't attach rest

10
formal models of computation 10 The 8-Queens problem (Contd) Results: ?- main(L). L = [1/4,2/2,3/7,4/3,5/6,6/8,7/5,8/1] ? ?- main(L). L = [1/4,2/2,3/7,4/3,5/6,6/8,7/5,8/1] ? ;

11
formal models of computation 11 The 8-Queens problem (Contd) 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] ? ?- 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] ? ;

12
formal models of computation 12 The 8-Queens problem (Contd) 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] ?

13
formal models of computation 13 Modules in Prolog 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

14
formal models of computation 14 Conclusions: Haskell vs Prolog 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

15
formal models of computation 15 Conclusions: Logic Programming/Prolog 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 (wouldnt 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 thats because it is trying to do more…

Similar presentations

© 2017 SlidePlayer.com Inc.

All rights reserved.

Ads by Google