append/3 A Drosophila of L.P. As functions: append([], L) = L - - PowerPoint PPT Presentation

append 3 a drosophila of l p
SMART_READER_LITE
LIVE PREVIEW

append/3 A Drosophila of L.P. As functions: append([], L) = L - - PowerPoint PPT Presentation

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


slide-1
SLIDE 1

1

Logic Programming School of Informatics, University of Edinburgh

append/3 A Drosophila of L.P.

As functions: append([], L) = L append([ H | T ], L) = [H | append(T, L) ] As a logic program append([], L, L) append([ H | T ], L, [ H | R ]) append(T, L, R)

slide-2
SLIDE 2

append/3 A Drosophila of L.P.

2

Logic Programming School of Informatics, University of Edinburgh

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:

slide-3
SLIDE 3

3

Logic Programming School of Informatics, University of Edinburgh

Introduction to Negation as Failure

In Prolog the goal \+ G succeeds if no instance

  • f G can be found by Prolog’s proof mechanism;
  • therwise it fails.

p(X) :- q(X). q(1). q(2). r(2). s(3). Given: Goals: yes Results: \+ (p(1), r(1)). \+ (p(2), r(2)). \+ p(X). \+ (p(X), s(X)). no no yes

slide-4
SLIDE 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. Results: Goals: Given: S = [1,2] 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). p(X) :- q(X). q(1). q(2). r(2). s(3). S = [2] S = [1,2,3] no

slide-5
SLIDE 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.

slide-6
SLIDE 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)]

slide-7
SLIDE 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.

slide-8
SLIDE 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).

slide-9
SLIDE 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) ).