1 / 23

COMP313A Programming Languages

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

hinda
Download Presentation

COMP313A Programming Languages

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. COMP313A Programming Languages Logic Programming (6)

  2. Some More Prolog • structures

  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

  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 = 31 X = "freddy" Yes (0.00s cpu)

  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, _, _)

  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

  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 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

  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

  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) ??

  10. 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).

  11. 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).

  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).

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

  14. 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, _,_) | _])

  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.

  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?

  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

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

  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

  20. L = [movie(X1, wizardofoz), movie(X2, drwhoandtheDaleks), movie(X3, starwars), movie(X4, shortcircuit)], Robotslist = [tinman, dalek, r2d2, johnnyfive],

  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, There are just two more things left to do

  22. 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).

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

More Related