Martin Kay Stanford University Martin Kay Introduction to Prolog 1
21 Slides198.50 KB

Martin Kay Stanford University Martin Kay Introduction to Prolog 1

Topics Books Starting and stopping Prolog Consulting and reconsulting files Assertions and queries Constants and variables Nondeterminism Lists Facts and rules Martin Kay Introduction to Prolog 2

Books Bratko, I. (1986) Prolog Programming for Artificial Intelligence. International Computer Science Series, Addison Wesley. Clocksin, W. F. and C. S. Mellish (1981) Programming in Prolog. Springer-Verlag, Berlin. König, E. and R. Seiffert (1989) Grudkurs Prolog für Linguisten. UTB für Wissenschaft; Uni-Taschenbücher, A. Franke Verlag, Tübingen. O'Keefe, R. (1990) The Craft of Prolog. MIT Press, Cambridge, Massachusetts. Ross, P. (1989) Advanced Prolog. Addison Weseley. Sterling, L. and E. Shapiro (1986) The Art of Prolog. MIT Press, Cambridge, Massachusetts. Martin Kay Introduction to Prolog 3

Sicstus Prolog Swedish Institute of Computer Science (SICS) Setting the path environment variable (on Leland) elaine27: more .cshrc # @(#) Leland Systems .cshrc version 3.0.4 # elaine27: more .cshrc # @(#) Leland Systems .cshrc version 3.0.4 # . set path ( /afs/ir/class/ling138/bin path) . Martin Kay Introduction to Prolog 4

Sicstus Prolog Swedish Institute of Computer Science (SICS) 1 sicstus SICStus 2.1 #9: Fri Oct 21 16:31:41 PDT 1994 ?- halt. 2 The Herald Martin Kay Introduction to Prolog The Period! 5

b q a p d r Martin Kay c Introduction to Prolog 6

q b a p d c r Martin Kay Introduction to Prolog 7

q b a p d c r Martin Kay Introduction to Prolog 8

q b a p d c r Martin Kay Introduction to Prolog 9

Queries q b a p d c r Martin Kay Introduction to Prolog 10

Queries Now we can make this a real link! q b a p d c r Martin Kay Introduction to Prolog 11

q p b a d c r Martin Kay Introduction to Prolog 12

Consulting files ?- consult(maize). {consulting /tmp mnt/tilde/kay/pl/parsers/maize.pl.} {/tmp mnt/tilde/kay/pl/parsers/maize.pl consulted, 160 msec 1056 bytes} yes ?- consult(‘maize.pl’). [maize]. [‘maize.pl’]. Martin Kay Introduction to Prolog 13

Facts and Rules past(dive, dived). past tense(dive, dove). pres part(dive, diving). sing3(dive, dives). past tense(write, wrote). past part(write, written). pres part(write, writing). sing3(write, writes). Head Goal past part(Verb, Word) :4 terms, past(Verb, Word). 2 clauses past tense(Verb, Word) :past(Verb, Word). Martin Kay Introduction to Prolog Facts Rules 14

The /2 operator ?- X foo. X foo ? yes ?- X Y. Y X ? yes ?- foo(A, b) foo(a, B). A a, B b ? yes Martin Kay Introduction to Prolog 15

Unification ?- p(a, Q, b) p(A, A, A). no ?- p(a, Q, R) p(A, A, A). A a, Q a, R a yes ?- p(q(a, a), r(X, X), s(Y, Y)) p(X, Y, Z). X q(a,a), Y r(q(a,a),q(a,a)), Z s(r(q(a,a),q(a,a)),r(q(a,a),q(a,a))) ? yes Martin Kay Introduction to Prolog 16

Ordered Pairs ?- X [a [b [c []]]]. X [a, b, c] yes ?- X [a b]. X [a b] yes Martin Kay Introduction to Prolog 17

member/2 ?- member(b, [a,b,c]). yes ?- member(X, [a, b, c]). X a ? ; X b ? ; member(H, member(H, [H ]). [H ]). member(X, member(X, [ T]) [ T]) ::member(X, member(X, T). T). X c ? ; no ?- member(a, [X, b, Y]). X a ? ; Y a ? ; no Martin Kay Introduction to Prolog 18

append/3 ?- append([a, b, c], [p, q, r], X). X [a,b,c,p,q,r] ? yes ?- append([a, b, c], X, [a, b, c, d, e, f]). X [d,e,f] ? append([], append([], A, A, A). A). append([C D], append([C D], A, A, [C B]) [C B]) ::append(D, append(D, A, A, B). B). yes ?- append(X, [d, e, f],[a, b, c, d, e, f]). X [a,b,c] ? yes Martin Kay Introduction to Prolog 19

(Naive) reverse/2 ?- reverse([a, b, c], X). X [c,b,a] ? yes ?- reverse(X, [a, b, c]). X [c,b,a] ? reverse([], reverse([], []). []). reverse([H T], reverse([H T], Rev) Rev) ::reverse(T, reverse(T, RT), RT), append(RT, append(RT, [H], [H], Rev). Rev). yes ?- X [a, b, B, A], reverse(X, X). A a, B b, X [a,b,b,a] ? yes Martin Kay Introduction to Prolog 20

Interrupting Prolog foo(a, b). C Prolog interruption (h for help)? a {Execution aborted} ?- Martin Kay Introduction to Prolog 21