230 likes | 368 Views
COMP313A Programming Languages. Logic Programming (6). Some More Prolog. structures. 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
E N D
COMP313A Programming Languages Logic Programming (6)
Some More Prolog • structures
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
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 = 31 X = "freddy" Yes (0.00s cpu)
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, _, _)
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
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 family family(person(_, fox, _, _), _, _). % the fox family family(_, person(_,armstrong, _, _), _) % or the armstrong family family(_, _, [_, _, _]). % any three child family family(_,person(Name, Surname, _, _), [_, _, _, | _]). % all married women who have at least three children
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
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) ??
but ?? child(pat) child(X) :- family(_, _, Children), mem_children (X, Children). mem_children (X, [person(X, _, _, _) | Tail]). mem_children (X, [person(Y, _, _, _) | Tail]) :- mem_children1(X, Tail).
Selectors Define relation which allow us to access particular components of a structure without knowing the details the structure This is data abstraction These selectors are useful when we want to create instances of families, for example husband(family(Husband, _, _), Husband). wife(family(_, Wife, _), Wife). children(family(_, _, Childlist), Childlist).
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).
nth_member([X|_], 1, X). nth_member([_| L], N, Child) :- N1 is N-1, nth_member(L, N1, Child).
Using them.. • Tom Fox and Jim Fox belong to the same family and Jim is the second child of Tomfirstname(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, _,_) | _])
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.
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?
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
L = [movie(X1, wizardofoz), movie(X2, drwhoandtheDaleks), movie(X3, starwars), movie(X4, shortcircuit)],
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
L = [movie(X1, wizardofoz), movie(X2, drwhoandtheDaleks), movie(X3, starwars), movie(X4, shortcircuit)], Robotslist = [tinman, dalek, r2d2, johnnyfive],
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, There are just two more things left to do
solution(L):- 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).
print_movies([A|B]) :- !, write(A), nl, print_movies(B). print_movies([]).