martin kayintroduction to prolog1 martin kay stanford university

21
Martin Kay Introduction to Prolog 1 Martin Kay Stanford University

Upload: emil-lawrence

Post on 29-Jan-2016

218 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Martin KayIntroduction to Prolog1 Martin Kay Stanford University

Martin Kay Introduction to Prolog 1

Martin Kay

Stanford University

Page 2: Martin KayIntroduction to Prolog1 Martin Kay Stanford University

Martin Kay Introduction to Prolog 2

Topics

Books

Starting and stopping Prolog

Consulting and reconsulting files

Assertions and queries

Constants and variables

Nondeterminism

Lists

Facts and rules

Page 3: Martin KayIntroduction to Prolog1 Martin Kay Stanford University

Martin Kay Introduction to Prolog 3

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.

Page 4: Martin KayIntroduction to Prolog1 Martin Kay Stanford University

Martin Kay Introduction to Prolog 4

Swedish Institute of Computer Science (SICS)

Sicstus Prolog

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)

....

Setting the path environment variable (on Leland)

Page 5: Martin KayIntroduction to Prolog1 Martin Kay Stanford University

Martin Kay Introduction to Prolog 5

The Period!

The Herald

Swedish Institute of Computer Science (SICS)

Sicstus Prolog

1 ~ sicstusSICStus 2.1 #9: Fri Oct 21 16:31:41 PDT 1994| ?- halt.

2 ~

Page 6: Martin KayIntroduction to Prolog1 Martin Kay Stanford University

Martin Kay Introduction to Prolog 6

ab

c

d

pq

r

Page 7: Martin KayIntroduction to Prolog1 Martin Kay Stanford University

Martin Kay Introduction to Prolog 7

ab

cd

pq

r

Page 8: Martin KayIntroduction to Prolog1 Martin Kay Stanford University

Martin Kay Introduction to Prolog 8

ab

cd

pq

r

Page 9: Martin KayIntroduction to Prolog1 Martin Kay Stanford University

Martin Kay Introduction to Prolog 9

ab

cd

pq

r

Page 10: Martin KayIntroduction to Prolog1 Martin Kay Stanford University

Martin Kay Introduction to Prolog 10

Queries

ab

cd

pq

r

Page 11: Martin KayIntroduction to Prolog1 Martin Kay Stanford University

Martin Kay Introduction to Prolog 11

Queries

ab

cd

pq

r

Now we can makethis a real link!

Page 12: Martin KayIntroduction to Prolog1 Martin Kay Stanford University

Martin Kay Introduction to Prolog 12

ab

cd

pq

r

Page 13: Martin KayIntroduction to Prolog1 Martin Kay Stanford University

Martin Kay Introduction to Prolog 13

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’].

Page 14: Martin KayIntroduction to Prolog1 Martin Kay Stanford University

Martin Kay Introduction to Prolog 14

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).••••past_part(Verb, Word) :- past(Verb, Word).past_tense(Verb, Word) :- past(Verb, Word).

Facts and Rules

Facts

Rules4 terms, 2 clauses

Head

Goal

Page 15: Martin KayIntroduction to Prolog1 Martin Kay Stanford University

Martin Kay Introduction to Prolog 15

The =/2 operator| ?- X=foo.

X = foo ?

yes

| ?- X=Y.

Y = X ?

yes

| ?- foo(A, b)=foo(a, B).

A = a,

B = b ?

yes

Page 16: Martin KayIntroduction to Prolog1 Martin Kay Stanford University

Martin Kay Introduction to Prolog 16

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

Page 17: Martin KayIntroduction to Prolog1 Martin Kay Stanford University

Martin Kay Introduction to Prolog 17

Ordered Pairs

| ?- X=[a | [b | [c | []]]].X=[a, b, c]

yes| ?- X=[a | b].X=[a | b]

yes

Page 18: Martin KayIntroduction to Prolog1 Martin Kay Stanford University

Martin Kay Introduction to Prolog 18

member/2| ?- member(b, [a,b,c]).

yes| ?- member(X, [a, b, c]).

X = a ? ;

X = b ? ;

X = c ? ;

no| ?- member(a, [X, b, Y]).

X = a ? ;

Y = a ? ;

no

member(H, [H|_]).member(X, [_|T]) :- member(X, T).

member(H, [H|_]).member(X, [_|T]) :- member(X, T).

Page 19: Martin KayIntroduction to Prolog1 Martin Kay Stanford University

Martin Kay Introduction to Prolog 19

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

yes

| ?- append(X, [d, e, f],[a, b, c, d, e, f]).

X = [a,b,c] ?

yes

append([], A, A).append([C|D], A, [C|B]) :- append(D, A, B).

append([], A, A).append([C|D], A, [C|B]) :- append(D, A, B).

Page 20: Martin KayIntroduction to Prolog1 Martin Kay Stanford University

Martin Kay Introduction to Prolog 20

(Naive) reverse/2| ?- reverse([a, b, c], X).

X = [c,b,a] ?

yes

| ?- reverse(X, [a, b, c]).

X = [c,b,a] ?

yes

| ?- X=[a, b, B, A], reverse(X, X).

A = a,

B = b,

X = [a,b,b,a] ?

yes

reverse([], []).reverse([H|T], Rev) :- reverse(T, RT), append(RT, [H], Rev).

reverse([], []).reverse([H|T], Rev) :- reverse(T, RT), append(RT, [H], Rev).

Page 21: Martin KayIntroduction to Prolog1 Martin Kay Stanford University

Martin Kay Introduction to Prolog 21

Interrupting Prolog

foo(a, b).

^C

Prolog interruption (h for help)? a

{Execution aborted}

| ?-