23
1 COMP313A Programming Languages Logic Programming (6)

1 COMP313A Programming Languages Logic Programming (6)

Embed Size (px)

Citation preview

Page 1: 1 COMP313A Programming Languages Logic Programming (6)

1

COMP313A Programming Languages

Logic Programming (6)

Page 2: 1 COMP313A Programming Languages Logic Programming (6)

2

Some More Prolog

• structures

Page 3: 1 COMP313A Programming Languages Logic Programming (6)

3

I/O in Prolog

• Files associated with input and output streams• I/O from terminal – user • In some Prologs only two files/streams are

active at any one time– current input stream– current output stream

• see switches input streams• tell switches output streams• In prolog I/O is a side effect of the predicate

Page 4: 1 COMP313A Programming Languages Logic Programming (6)

4

Tkeclipse Prolog• stdin, stdout

– standard input and output streams

?- write("fred").Yes (0.00s cpu)

• open a stream for input or output– open(SourceSink, Mode, Stream)

? open('//C/Documents and Settings/mjeff/My Documents/prolog/fred.txt', read, Fred), read_string(Fred, "\r\n ", 10, X).

Fred = 31X = "freddy"Yes (0.00s cpu)

Page 5: 1 COMP313A Programming Languages Logic Programming (6)

5

Bits and pieces

• Anonymous variables

family(person(tom, fox, date(7,may, 1950), employed),

person(ann, fox, date(9, may, 1951), employed),

[person(pat, fox, date(5, may, 1973), unemployed),

person(jim, fox, date(5, may, 1973), unemployed)]).

husband(X) :- family(X,Y, Z).

Y and Z are singleton variables

husband(X) :- family(X, _, _)

Page 6: 1 COMP313A Programming Languages Logic Programming (6)

6

More bits and pieces

• Comparison operators– X > Y– X < Y– X >= Y– X =< Y– X = Y 1 + A = B + 2 1 + A = 2 + B– X \= Y– X =:= Y 1 + 2 =:= 2 + 1 1 + A =:=

B + 2– X =\= Y

Page 7: 1 COMP313A Programming Languages Logic Programming (6)

7

Structuresa family database

family(person(tom, fox, date(7,may, 1950), employed), person(ann, armstrong, date(9, may, 1951), employed), [person(pat, fox, date(5, may, 1973, unemployed), person(jim, fox, date(5, may, 1973), unemployed)]).

family(_,_,_). % any familyfamily(person(_, fox, _, _), _, _). % the fox

familyfamily(_, person(_,armstrong, _, _), _) % or the armstrong family

family(_, _, [_, _, _]). % any three child family

family(_,person(Name, Surname, _, _), [_, _, _, | _]). % all married women

who have at least three children

Page 8: 1 COMP313A Programming Languages Logic Programming (6)

8

husband(X) :- family(X, _, _).wife(X) :- f amily(_,X,_).

?- husband(X).X = person(tom, fox, date(7, may, 1950), employed)Yes (0.00s cpu)

?- wife(X).X = person(ann, armstrong, date(9, may, 1951), employed)Yes (0.00s cpu)

Problem – this may be too simplistic

Page 9: 1 COMP313A Programming Languages Logic Programming (6)

9

husband2(X,Y) :- family(person(X,Y,_,_), _, _).

married(X,Y) :- family(person(X, _, _, _), person(Y,_, _, _),_).married(X,Y) :- married(Y,X).

child(X) :- family(_, _, Children), member(X, Children).

?? child(X)??

Page 10: 1 COMP313A Programming Languages Logic Programming (6)

10

child(X) :- family(_, _, Children), mem_children (X, Children).

mem_children (X, [person(X, _, _, _) | Tail]).

mem_children (X, [person(Y, _, _, _) | Tail]) :- mem_children1(X,

Tail).

but

?? child(pat)

Page 11: 1 COMP313A Programming Languages Logic Programming (6)

11

Selectors

husband(family(Husband, _, _), Husband).

wife(family(_, Wife, _), Wife).

children(family(_, _, Childlist), Childlist).

Define relation which allow us to access particular components of a structure without knowing the detailsthe structure

This is data abstraction

These selectors are useful when we want to create instances of families, for example

Page 12: 1 COMP313A Programming Languages Logic Programming (6)

12

Selectors…

firstchild(Family, First) :- children(Family, [First | _]).

secondchild(Family, Second) :- children(Family, [_, Second | _]).

nthchild(N, Family, Child) :- children(Family, ChildList),

nth_member(ChildList, N, Child).

firstname(person(Name, _, _,_), Name).

surname(person(_, Surname, _, _), Surname).

Page 13: 1 COMP313A Programming Languages Logic Programming (6)

13

nth_member([X|_], 1, X).nth_member([_| L], N, Child) :- N1 is N-1, nth_member(L, N1, Child).

Page 14: 1 COMP313A Programming Languages Logic Programming (6)

14

Using them..

• Tom Fox and Jim Fox belong to the same family and Jim is the second child of Tom

firstname(Person1, tom), surname(Person1, fox), % person1 is Tom Fox

firstname(Person2, jim), surname(Person2, fox),%person2 is Jim Fox

husband(Family, Person1), secondchild(Family, Person2).

Person1 = person(tom, fox, _, _)Person2 = person(jim, fox, _, _)Family = family(person(tom, fox, _, _), _, [_, person(jim, fox, _,_) |

_])

Page 15: 1 COMP313A Programming Languages Logic Programming (6)

15

Logic Puzzles

• Use the following clues to write a prolog program to determine the movies the robots tinman, r2d2, johnny five, and a dalek starred in.– Neither Tinman nor Johnny five were one of

the daleks in Dr Who and the Daleks– The movie Short Circuit did not star Tinman.– R2d2 wowed the audiences in Star Wars.– A dalek did not star in the Wizard of Oz.

Page 16: 1 COMP313A Programming Languages Logic Programming (6)

16

Structure is important

• Solution(L) :-

We want a binding for L which will contain the result we are after

• What is the result we want?

Page 17: 1 COMP313A Programming Languages Logic Programming (6)

17

L = [movie(X1, wizardofoz), movie(X2, drwhoandtheDaleks), movie(X3, starwars), movie(X4, shortcircuit)],

Now we have to supply a mechanism for instantiating X1..X4

We need a way of selecting a value and then checking it against some constraints

Page 18: 1 COMP313A Programming Languages Logic Programming (6)

18

L = [movie(X1, wizardofoz), movie(X2, drwhoandtheDaleks), movie(X3, starwars), movie(X4, shortcircuit)],

Page 19: 1 COMP313A Programming Languages Logic Programming (6)

19

L = [movie(X1, wizardofoz), movie(X2, drwhoandtheDaleks), movie(X3, starwars), movie(X4, shortcircuit)],

Robotslist = [tinman, dalek, r2d2, johnnyfive],

We will draw the values for X1..X2, from Robotslist

We do this using the member predicate

Page 20: 1 COMP313A Programming Languages Logic Programming (6)

20

L = [movie(X1, wizardofoz), movie(X2, drwhoandtheDaleks), movie(X3, starwars), movie(X4, shortcircuit)],

Robotslist = [tinman, dalek, r2d2, johnnyfive],

Page 21: 1 COMP313A Programming Languages Logic Programming (6)

21

L = [movie(X1, wizardofoz), movie(X2, drwhoandtheDaleks), movie(X3, starwars), movie(X4, shortcircuit)],

Robotslist = [tinman, dalek, r2d2, johnnyfive], member(X1, Robotslist), X1 \= dalek, member(X2, Robotslist), X2 \= tinman, X2 \= johnnyfive, member(X3, Robotslist), X3 = r2d2, member(X4, Robotslist), X4 \= tinman,

X1 \= dalek,

There are just two more things left to do

Page 22: 1 COMP313A Programming Languages Logic Programming (6)

22

L = [movie(X1, wizardofoz), movie(X2, drwhoandtheDaleks), movie(X3, starwars), movie(X4, shortcircuit)],

Robotslist = [tinman, dalek, r2d2, johnnyfive], member(X1, Robotslist), X1 \= dalek, member(X2, Robotslist), X2 \= tinman, X2 \= johnnyfive, member(X3, Robotslist), X3 = r2d2, member(X4, Robotslist), X4 \= tinman, X2 \= X1, X2 \= X3, X2 \= X4, X3 \= X1, X3 \= X4, X4 \= X1, print_movies(L).

solution(L):-

Page 23: 1 COMP313A Programming Languages Logic Programming (6)

23

print_movies([A|B]) :- !, write(A), nl, print_movies(B).print_movies([]).