Download presentation

Presentation is loading. Please wait.

Published byDana Netter Modified over 2 years ago

1
1 Logic Programming School of Informatics, University of Edinburgh append/3 A Drosophila of L.P. append([], L, L) append([ H | T ], L, [ H | R ]) append(T, L, R) append([], L) = L append([ H | T ], L) = [H | append(T, L) ] As functions: As a logic program

2
2 Logic Programming School of Informatics, University of Edinburgh append/3 A Drosophila of L.P. append([], L, L) append([ H | T ], L, [ H | R ]) append(T, L, R) Goals: append([a,b,c], [d,e], X) append(X, [d,e], [a,b,c,d,e]) append([a,b,c], X, [a,b,c,d,e]) append(X, Y, [a,b,c,d,e]) append([a,b,c], X, Y) X = [a,b,c,d,e] X = [a,b,c] X = [d,e] X = [], Y = [a,b,c,d,e] ; X = [a], Y = [b,c,d,e]; etc Y = [a,b,c | X ] Results:

3
3 Logic Programming School of Informatics, University of Edinburgh Introduction to Negation as Failure In Prolog the goal \+ G succeeds if no instance of G can be found by Prolog’s proof mechanism; otherwise it fails. p(X) :- q(X). q(1). q(2). r(2). s(3). Given: Goals: \+ (p(1), r(1)). \+ (p(2), r(2)). \+ p(X). \+ (p(X), s(X)). yes no yes Results:

4
4 Logic Programming School of Informatics, University of Edinburgh Generating a Set of Results setof(X, Goal, Set) generates the set, S, of instances of X generated by exhaustively satisfying the given Goal. p(X) :- q(X). q(1). q(2). r(2). s(3). Given: Goals: setof(X, p(X), S). setof(X, (p(X), r(X)), S). setof(X, (p(X) ; s(X)), S). setof(X, (p(X), s(X)), S). S = [1,2] S = [2] S = [1,2,3] no Results:

5
5 Logic Programming School of Informatics, University of Edinburgh Building Larger Definitions: Map Colouring The four colour theorem says that any 2- dimensional map can be coloured, with no bordering country sharing the same colour, using only four different colours.

6
6 Logic Programming School of Informatics, University of Edinburgh A Logic Program Requirement Define a predicate colour_countries(X) that generates in X a list of terms [c(S,C),…], where S is a country and C is one of four colours. The predicate must be able to generate each combination of countries and colours satisfying the four-colour theorem but no combinations that do not satisfy the theorem. An example behaviour is: | ?- colour_countries(X). X=[c(austria,red), c(belgium,green), c(denmark,yellow), c(france,red), c(germany,blue), c(italy,blue), c(netherlands,yellow), c(portugal,blue), c(spain,yellow), c(switzerland,yellow)]

7
7 Logic Programming School of Informatics, University of Edinburgh A Representation of a Map ngb(portugal, [spain]). ngb(spain, [portugal,france]). ngb(france, [spain,belgium,switzerland,germany,italy]). ngb(belgium, [france,germany,netherlands]). ngb(netherlands, [belgium,germany]). ngb(germany, [netherlands,belgium,france,switzerland,austria,denmark]). ngb(switzerland, [france,germany,austria,italy]). ngb(austria, [germany,switzerland,italy]). ngb(italy, [france,switzerland,austria]). ngb(denmark, [germany]). Note this is not the only representation we could have used. The art is to describe the salient and essential details of the problem, and no more than this.

8
8 Logic Programming School of Informatics, University of Edinburgh Generating Individual Neighbours neighbour(Country, Country1) :- ngb(Country, Neighbours), member(Country1, Neighbours). member(X, [ X | _ ]). member(X, [ _ | T ]):- member(X, T).

9
9 Logic Programming School of Informatics, University of Edinburgh Colouring the Countries colour_countries(Colours) :- setof(c(Country,_), X^ngb(Country,X), Colours), colours(Colours). colours([]). colours([ c(Country,Colour) | Rest ]) :- colours(Rest), member(Colour, [yellow,blue,red,green]), \+ ( member(c(Country1,Colour), Rest), neighbour(Country, Country1) ).

Similar presentations

© 2017 SlidePlayer.com Inc.

All rights reserved.

Ads by Google